import tester.*; import java.util.*; class Loops{ // find all words that start with "q" in the given ArrayList // // Here we can use the for-each loop, as we are only observing the values // in the ArrayList ArrayList findAll(ArrayList slist) { // we are producing an ArrayList - so we set up the base (empty list) ArrayList result = new ArrayList(); for (String s : slist) { // for each desired item, add it to the resulting list if (s.startsWith("q")) result.add(s); } // produce the result - would be an empty list if no desired items found return result; } // replace every word in the given ArrayList with one // in all lower case letters // // We cannot use the for-each loop, as we need to change the contents // of the individual elements of the ArrayList void toLowerCase(ArrayList slist) { for (int i = 0; i < slist.size(); i = i + 1) { slist.set(i, slist.get(i).toLowerCase()); } } // find the index of the shortest String in the given ArrayList // // We must use a counted for loop, as we need to record the locations // of the new minimum items we find int findMinLoc(ArrayList slist){ // take care of the empty case first if (slist.size() == 0) return -1; // we are guaranteed that the list has at least one item // record the location and the length of the shortest item int minLoc = 0; int minLength = slist.get(0).length(); // loop through the remaining items (if any) and update the location // and the minimum length as needed for (int i = 1; i < slist.size(); i = i + 1){ if (slist.get(i).length() < minLength){ minLoc = i; minLength = slist.get(i).length(); } } // minimum location is now a valid index - of course, it could be 0 return minLoc; } // is there a word that starts with "q" in the given ArrayList? // we can use the while loop here - as it will check the desired // condition and exit immediately boolean hasQword(ArrayList slist){ // starting with the first item int index = 0; // must check the size first, so we do not use index out of bounds // more items to look at ... while (index < slist.size() && // and we did not find the desired item yet ... !slist.get(index).startsWith("q")){ // do not forget to update the index - or you will loop forever!!! index = index + 1; } // did we go through all items in the list? if yes, we did not find it if (index == slist.size()) return false; // did not go to the end, so item was found else return true; } // is there a word that starts with "q" in the given ArrayList? // but a for-each loop works more nicely and exits immediately boolean hasQword2(ArrayList slist){ // go through all items and exit as soon as you find a desired one for (String s: slist){ if (s.startsWith("q")) return true; } // nothing found, so the answer is false return false; } } // tests for the loops methods class ExamplesLoops { ExamplesLoops () { } Loops lect = new Loops(); ArrayList slist = new ArrayList( Arrays.asList("Bob", "Dan", "Eve", "Jan", "Kim", "Pat")); ArrayList sLengthList = new ArrayList( Arrays.asList("Bobby", "Dan", "Evelyn", "Janet", "Kim", "Patrick")); ArrayList slistLower = new ArrayList( Arrays.asList("bob", "dan", "eve", "jan", "kim", "pat")); ArrayList qlist = new ArrayList( Arrays.asList("Bob", "Dan", "Eve", "Jan", "quinn", "Pat", "quentin")); ArrayList qAllList = new ArrayList( Arrays.asList("quinn", "quentin")); void reset(){ this.slist = new ArrayList( Arrays.asList("Bob", "Dan", "Eve", "Jan", "Kim", "Pat")); this.sLengthList = new ArrayList( Arrays.asList("Bobby", "Dan", "Evelyn", "Janet", "Kim", "Patrick")); this.qlist = new ArrayList( Arrays.asList("Bob", "Dan", "Eve", "Jan", "quinn", "Pat", "quentin")); this.slistLower = new ArrayList( Arrays.asList("bob", "dan", "eve", "jan", "kim", "pat")); } // test the method findAll void testFindAll(Tester t){ reset(); t.checkExpect(lect.findAll(this.slist), new ArrayList()); t.checkExpect(lect.findAll(this.qlist), this.qAllList); } // test the method toLowerCase void testToLowerCase(Tester t){ reset(); lect.toLowerCase(this.slist); t.checkExpect(slist, slistLower); } // test the first variant of hasQWord method void testHasQ(Tester t){ reset(); t.checkExpect(lect.hasQword(this.slist), false); t.checkExpect(lect.hasQword(this.qlist), true); } // test the second variant of hasQWord method void testHasQ2(Tester t){ reset(); t.checkExpect(lect.hasQword2(this.slist), false); t.checkExpect(lect.hasQword2(this.qlist), true); } // test the method findMinLoc void testFindMinLoc(Tester t){ reset(); t.checkExpect(lect.findMinLoc(this.slist), 0); t.checkExpect(lect.findMinLoc(this.sLengthList), 1); } public static void main(String[] argv){ ExamplesLoops el = new ExamplesLoops(); Tester.run(el); } }