// Lab 3 // CS U213 Spring 2009 // strings-acc.java import java.util.*; // to represent a list of Strings interface ILoS{ // combine all Strings in this list into one String combine(); // find the length of the longest word in this list int maxLength(); } // to represent an empty list of Strings class MtLoS implements ILoS{ MtLoS(){} // combine all Strings in this list into one String combine(){ return ""; } // find the length of the longest word in this list int maxLength(){ return 0; } } // to represent a nonempty list of Strings class ConsLoS implements ILoS{ String first; ILoS rest; ConsLoS(String first, ILoS rest){ this.first = first; this.rest = rest; } /* TEMPLATE FIELDS: ... this.first ... -- String ... this.rest ... -- ILoS METHODS FOR FIELDS: ... this.first.length() ... -- int ... this.first.concat(String) ... -- String ... this.first.equals(String) ... -- boolean - and many more methods pre-defined for String class - ... this.rest.combine() ... -- String ... this.rest.maxLength() ... -- int */ // combine all Strings in this list into one String combine(){ return this.first.concat(this.rest.combine()); } // find the length of the longest word in this list int maxLength(){ return Math.max(this.first.length(), this.rest.maxLength()); } } // to represent examples for lists of strings class Examples{ Examples(){} ILoS mtlos = new MtLoS(); ILoS mary = new ConsLoS("Mary ", new ConsLoS("had ", new ConsLoS("a ", new ConsLoS("little ", new ConsLoS("lamb.", this.mtlos))))); // test for the method combine for the list of Strings boolean testCombine = (check this.mtlos.combine() expect "") && (check this.mary.combine() expect "Mary had a little lamb."); // test for the method maxLength for the list of Strings boolean testMaxLength = (check this.mtlos.maxLength() expect 0) && (check this.mary.maxLength() expect 7); }