/* FILE NAME: LabOpsIteration.java * AUTHOR: Stella K. * * DATE: Nov 3, 07 * * COMMENTS: Solutions to lab methods on StringLists using iteration (tail rec, while and for-loops). * 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 LabOpsIteration 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 isMemberRec() System.out.println("\nTesting isMemberRec()"); System.out.println("stella is member in " + testList0.toString() + " --> " + isMemberRec("stella", testList0)); System.out.println("stella is member in " + testList1.toString() + " --> " + isMemberRec("stella", testList1)); System.out.println("person is member in " + testList2.toString() + " --> " + isMemberRec("person", testList2)); System.out.println("desk is member in " + testList2.toString() + " --> " + isMemberRec("desk", testList2)); //Testing isMemberWhile() System.out.println("\nTesting isMemberWhile()"); System.out.println("stella is member in " + testList0.toString() + " --> " + isMemberWhile("stella", testList0)); System.out.println("stella is member in " + testList1.toString() + " --> " + isMemberWhile("stella", testList1)); System.out.println("person is member in " + testList2.toString() + " --> " + isMemberWhile("person", testList2)); System.out.println("desk is member in " + testList2.toString() + " --> " + isMemberWhile("desk", testList2)); System.out.println("I is member in " + testList8.toString() + " --> " + isMemberWhile("I", testList8)); System.out.println("ham is member in " + testList8.toString() + " --> " + isMemberWhile("ham", testList8)); System.out.println("after: " + testList8.toString()); //Testing listSize() System.out.println("\nTesting listSize()"); System.out.println("listSize of " + testList0.toString() + " --> " + listSize(testList0)); System.out.println("listSize of " + testList1.toString() + " --> " + listSize(testList1)); System.out.println("listSize of " + testList2.toString() + " --> " + listSize(testList2)); System.out.println("listSize of " + testList8.toString() + " --> " + listSize(testList8)); //Testing implodeFor() System.out.println("\nTesting implodeFor()"); System.out.println("implodeFor " + testList0.toString() + " --> " + implodeFor(testList0)); System.out.println("implodeFor " + testList1.toString() + " --> " + implodeFor(testList1)); System.out.println("implodeFor " + testList2.toString() + " --> " + implodeFor(testList2)); System.out.println("implodeFor " + testList8.toString() + " --> " + implodeFor(testList8)); } //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 implodeFor (StringList l) { int size = listSize(l); String result = ""; for (int i =0; i < size; i=i+1) { result = result + head(l); l = tail(l); } return result; } //returns the number of elements in the input list public static int listSize(StringList l) { int size = 0; while(!isEmpty(l)) { size = size + 1; l = tail(l); } return size; } public static boolean isMemberWhile(String s, StringList L) { boolean found = false; //"found" starts out false, //and will become true when/if s is found String current; //"current" will hold the head of L as we walk down the list while (!isEmpty(L) && !found) { current = head(L); if (current.equals(s)) //string s was found! found = true; else L = tail(L); } return found; } public static boolean isMemberRec(String s, StringList l) { if (isEmpty(l)) return false; if (s.equals(head(l))) return true; return isMemberRec(s, tail(l)); } }