/* FILE NAME: LabOps.java * AUTHOR: Stella K. * * DATE: Oct 26, 07 * * COMMENTS: Solutions to lab methods on StringLists. * Notice that this class extends StringListOps, which extends StringList. * Therefore, all methods defined in these two classes are inhereted here. * * In particular: * The following methods are inherited from StringList: * public static StringList empty(); * public static boolean isEmpty (StringList L); * public static StringList prepend (String s, StringList L); * public static String head (StringList L); * public static StringList tail (StringList L); * public static boolean equals (StringList L1, StringList L2); * public static String toString (StringList L); * public static String fromString (String s); * * The following methods are inherited from StringList: * public static int length (StringList L); * public static StringList append (StringList L1, StringList L2); * public static StringList postpend (StringList L, String s); * public static StringList reverse (StringList L); * * All the above methods can be used in this file without the explicit * "StringList." or "StringListOps." prefixes. * For example, you can write "head(L)" rather than "StringList.head(L)", * and "length(L)" rather than "StringListOps.length(L)". * * MODIFICATION HISTORY: */ public class LabOpsSolutionsStella extends StringListOps { // Write your methods here: public static void main (String [] args) { //testing the f=given auxiliary methods System.out.println("first(\"cs111\") = \"" + first("cs111") + "\""); System.out.println("butFirst(\"cs111\") = \"" + butFirst("cs111") + "\""); System.out.println("fromString(\"[I,do,not,like,green,eggs,and,ham]\") = " + fromString("[I,do,not,like,green,eggs,and,ham]")); // Add your testing code here: //Lists for testing StringList testList0 = empty(); StringList testList1 = prepend("person", empty()); StringList testList2 = prepend("cat", prepend("desk", empty())); StringList testList3 = prepend("hello", prepend("low", prepend("sun", empty()))); StringList testList8 = fromString(("[I,do,not,like,green,eggs,and,ham]")); //Testing mapPluralize() System.out.println("Testing mapPluralize()"); StringList result2 = mapPluralize(testList2); System.out.println(testList0.toString() + " mapPularize --> " + mapPluralize(testList0)); System.out.println(testList1.toString() + " mapPularize --> " + mapPluralize(testList1)); System.out.println(testList2.toString() + " mapPularize --> " + result2); System.out.println(testList3.toString() + " mapPularize --> " + mapPluralize(testList3)); //Testing mapUpperCase() System.out.println("\nTesting mapUpperCase()"); System.out.println(testList0.toString() + " mapUpperCase --> " + mapUpperCase(testList0)); System.out.println(testList1.toString() + " mapUpperCase --> " + mapUpperCase(testList1)); System.out.println(testList2.toString() + " mapUpperCase --> " + mapUpperCase(testList2)); System.out.println(testList3.toString() + " mapUpperCase --> " + mapUpperCase(testList3)); //Testing isMember() System.out.println("\nTesting isMember()"); System.out.println("stella is member in " + testList0.toString() + " --> " + isMember("stella", testList0)); System.out.println("stella is member in " + testList1.toString() + " --> " + isMember("stella", testList1)); System.out.println("person is member in " + testList2.toString() + " --> " + isMember("person", testList2)); System.out.println("desk is member in " + testList2.toString() + " --> " + isMember("desk", testList2)); //Testing explode() System.out.println("\nTesting explode()"); System.out.println("stella" + " explode --> " + explode("stella")); System.out.println("desk explode --> " + explode("desk")); System.out.println("spaceship explode --> " + explode("spaceship")); System.out.println("empty string explode --> " + explode("")); //Testing implode() System.out.println("\nTesting implode()"); System.out.println("implode" + testList0.toString() + " --> " + implode(testList0)); System.out.println("implode " + testList1.toString() + " --> " + implode(testList1)); System.out.println("implode " + testList2.toString() + " --> " + implode(testList2)); System.out.println("implode " + testList3.toString() + " --> " + implode(testList3)); System.out.println("implode(" + testList8.toString() + " --> " + implode(testList8)); // Testing filterMatches() System.out.println("\nTesting filterMatches()"); StringList result = filterMatches("com", fromString("[computer,program,incomparable,intercom,Java]")); System.out.println("filterMatches(com, [computer,program,incomparable,intercom,Java] --> " + result); result = filterMatches("com", fromString("[cat,dog,mouse]")); System.out.println("filterMatches(com, [cat,dog,mouse] --> " + result); // Testing insert() System.out.println("\nTesting insert()"); System.out.println(insert("e", fromString("[b,d,f]"))); } public static StringList mapPluralize (StringList l) { if (isEmpty(l)) return empty(); String s = head(l) + "s"; return prepend(s, mapPluralize(tail(l))); } public static StringList mapUpperCase (StringList l) { if (isEmpty(l)) return empty(); String s = head(l).toUpperCase(); return prepend(s, mapUpperCase(tail(l))); } public static boolean isMember(String s, StringList l) { if (isEmpty(l)) return false; if (s.equals(head(l))) return true; return isMember(s, tail(l)); } //It takes a string as an argument and returns a list of single-character strings //formed out of the characters of the given string s. //for example: "hello" -->["h","e","l","l","o"] public static StringList explode (String s) { if (s.equals("")) return empty(); String str = first(s); return prepend(str, explode(butFirst(s))); } //It takes a string list and returns a string that is the result of concatenating //all the strings in the input list in order. public static String implode (StringList l) { if (isEmpty(l)) return ""; return head(l)+implode(tail(l)); } //Checks whether the String sub is a sub-string of the String str public static boolean isSubstring(String sub, String str) { int res = str.indexOf(sub); //find the index of the sub into str if (res < 0) return false; // if it was found there, return true, false otherwise. return true; } //It takes a string and a string list, and returns //the list of strings in the input list that contain the input string as a substring. public static StringList filterMatches(String str, StringList L) { if (isEmpty(L)) return empty(); //base case //System.out.println("str = " + str); //System.out.println("head(L) = " + head(L)); //System.out.println(isSubstring(str, head(L))); StringList res = filterMatches(str, tail(L)); if (isSubstring(str, head(L))) return prepend(head(L), res); else return res; } //Returns a new (n+1)-element list //of alphabetically sorted strings that contains the input string //in addition to all the elements of L. public static StringList insert (String s, StringList L) { if (isEmpty(L)) return prepend(s, empty()); if (s.compareTo(head(L)) <= 0) return prepend(s, L); else { // insert s in the right place within the tail of L return prepend(head(L), insert(s, tail(L))); } } //--------------------------------------------------------------- // The following auxiliary methods are helpful for defining explode() // Given a string s, returns a one-character string containing // the first character of s. public static String first (String s) { return new Character(s.charAt(0)).toString(); } // Given a string s, returns the substring of s that contains // all characters except the first. public static String butFirst (String s) { return s.substring(1,s.length()); } }