import java.util.*; import tester.*; /** * Examples of uses of loops and iterators * @author vkp * @since 10-30-2013 */ class LoopAlgorithms { /** * find whether the given ArrayList contains * the given String * @param slist the given ArrayList * @param s the given String * @return true if the String appears in the * ArrayList */ boolean containsWhile(ArrayList slist, String s) { int index = 0; while(index < slist.size()) { if (s.equals(slist.get(index))) { return true; } index = index + 1; } return false; } /** * find whether the given ArrayList contains * the given String * @param slist the given ArrayList * @param s the given String * @return true if the String appears in the * ArrayList */ boolean containsFor(ArrayList slist, String s) { for (int index = 0; index < slist.size(); index = index + 1) { if (s.equals(slist.get(index))) { return true; } } return false; } /** * find whether the given ArrayList contains * the given String * @param slist the given ArrayList * @param s the given String * @return true if the String appears in the * ArrayList */ boolean containsForEach(ArrayList slist, String s) { for (String sx : slist) { if (s.equals(sx)) { return true; } } return false; } /** * find whether the given ArrayList contains * the given String * @param slist the given ArrayList * @param s the given String * @return true if the String appears in the * ArrayList */ boolean containsIterator(Iterable slist, String s) { Iterator iter = slist.iterator(); while (iter.hasNext()) { if (s.equals(iter.next())) { return true; } } return false; } } /** * Class to compare two Strings lexicographically * @author vkp * @version 10-30-2013 */ class StringComparator implements Comparator { public int compare(String s1, String s2) { return s1.compareTo(s2); } } /** * Examples and tests for the loop methods * @author vkp * @version 10-30-2013 */ class ExamplesLoopVariants { ArrayList slist1 = new ArrayList(Arrays.asList("ahoy", "bye", "ciao")); ArrayList slist2 = new ArrayList(Arrays.asList("aloha", "hello", "hi")); ArrayList slist3 = new ArrayList(Arrays.asList("sayonara", "servus")); ArrayList slistR12 = new ArrayList(Arrays.asList("ahoy", "aloha", "bye", "ciao", "hello", "hi")); ArrayList slistR13 = new ArrayList(Arrays.asList("ahoy", "bye", "ciao", "sayonara", "servus")); LoopAlgorithms la = new LoopAlgorithms(); Comparator scomp = new StringComparator(); /** * Test the loop methods - at least two tests per method * @param t the Tester that runs the tests */ void testContains(Tester t) { t.checkExpect(this.la.containsWhile(this.slist1, "ciao"), true); t.checkExpect(this.la.containsWhile(this.slist2, "ciao"), false); t.checkExpect(this.la.containsFor(this.slist1, "ciao"), true); t.checkExpect(this.la.containsFor(this.slist2, "ciao"), false); t.checkExpect(this.la.containsForEach(this.slist1, "ciao"), true); t.checkExpect(this.la.containsForEach(this.slist2, "ciao"), false); t.checkExpect(this.la.containsIterator(this.slist1, "ciao"), true); t.checkExpect(this.la.containsIterator(this.slist2, "ciao"), false); } /** * The main method that runs the program * @param argv ignored */ public static void main(String[] argv){ ExamplesLoopVariants elv = new ExamplesLoopVariants(); Tester.run(elv); } }