// Lab 5 // CS U213 Fall 2008 // 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; } // 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 mary = new ConsLoS("Mary ", new ConsLoS("had ", new ConsLoS("a ", new ConsLoS("little ", new ConsLoS("lamb.", new MtLoS()))))); // test for the method combine for the list of Strings boolean testCombine = check this.mary.combine() expect "Mary had a little lamb."; // test for the method maxLength for the list of Strings boolean testMaxLength = check this.mary.maxLength() expect 7; }