/*********************************************** * CS2510 Fall 2011 * Lecture #23 * Exam 2 Review ***********************************************/ import java.util.ArrayList; import java.util.Arrays; import tester.*; // A class to walk our ArrayList and get each element in sequence class Traversal { int index; ArrayList list; Traversal(ArrayList list) { this.list = list; this.index = 0; } Traversal(ArrayList list, int index) { this.list = list; this.index = index; } // first: Returns the current element in the Traversal X first() { return list.get(index); } // rest: Returns a new Traversal for the next element Traversal rest() { return new Traversal(list,index + 1); } // drest: Advances this Traversal to the next element destructively void drest() { index = index + 1; } // isEmpty: Are we out of elements? boolean isEmpty() { return index >= list.size(); } } // Represents an astronaut along with some uncommon characteristics class Astronaut { String name; int timesInSpace; boolean laser; boolean isEvil; Astronaut(String name, int timesInSpace, boolean laser, boolean isEvil) { this.name = name; this.timesInSpace = timesInSpace; this.laser = laser; this.isEvil = isEvil; } } class Algs { // Returns the number of Astronauts in our list with lasers using recursion int numLasers(ArrayList alist) { return numLasersH(alist,0,0); } // Helper to store the position in the list and number of lasers seen so far int numLasersH(ArrayList alist, int index, int acc) { if (index < alist.size()) { if (alist.get(index).laser) { return numLasersH(alist, index + 1, acc + 1); } else { return numLasersH(alist, index + 1, acc); } } else { return acc; } } // Returns the number of Astronauts in our list with lasers using while loop int numLasersW(ArrayList alist) { // Initialize Index and Accumulator int acc = 0; int index = 0; // Loop with predicate while(index < alist.size()) { // Update function if (alist.get(index).laser) { acc = acc + 1; } // Increment index index = index + 1; } // Return Accumulator return acc; } // Returns the number of Astronauts in our list with lasers using for loop int numLasersF(ArrayList alist) { // Initialize Index and Accumulator int acc = 0; // Loop with predicate for(int index = 0; index < alist.size(); index = index + 1) { // Update function if (alist.get(index).laser) { acc = acc + 1; } } // Return Accumulator return acc; } // Returns the number of Astronauts in our list with lasers using traversal int numLasersT(ArrayList alist) { // Initialize Index and Accumulator int acc = 0; // Loop with predicate for(Traversal trav = new Traversal(alist); !trav.isEmpty(); trav = trav.rest()) { // Update function if (trav.first().laser) { acc = acc + 1; } } // Return Accumulator return acc; } } class ReviewExamples { Astronaut a1 = new Astronaut("Buzz Aldrin", 50, true, false); Astronaut a2 = new Astronaut("Buzz Lightyear", 0, true, false); Astronaut a3 = new Astronaut("Fizz Darkmonth", 1, false, true); ArrayList noastros = new ArrayList(); ArrayList astros = new ArrayList(Arrays.asList(a1,a2,a3)); // Test our traversal void testTraversal(Tester t) { Traversal atrav = new Traversal(astros); t.checkExpect(atrav.first(),a1); t.checkExpect(atrav.rest(), new Traversal(new ArrayList(Arrays.asList(a1,a2,a3)),1) ); t.checkExpect(atrav.rest().first(),a2); atrav.drest(); t.checkExpect(atrav,new Traversal(new ArrayList(Arrays.asList(a1,a2,a3)),1)); } // Test all our NumLasers methods void testNumLasers(Tester t) { Algs algs = new Algs(); t.checkExpect(algs.numLasers(noastros),0); t.checkExpect(algs.numLasers(astros),2); t.checkExpect(algs.numLasersW(noastros),0); t.checkExpect(algs.numLasersW(astros),2); t.checkExpect(algs.numLasersF(noastros),0); t.checkExpect(algs.numLasersF(astros),2); t.checkExpect(algs.numLasersT(noastros),0); t.checkExpect(algs.numLasersT(astros),2); } }