/*********************************************** * CS2510 Spring 2011 * Lecture #19 Morning * ArrayList "Wrappers": Traversals, Stacks, and Queues ***********************************************/ import tester.*; import java.util.ArrayList; /** A Wrapper class that implements Traversal over an ArrayList */ class ALTrav implements Traversal{ ArrayList mike; int index; /** Give me an ArrayList, we begin at the beginning */ ALTrav(ArrayList mike){ this(mike, 0); } /** Give me an ArrayList, tell me where to start */ private ALTrav(ArrayList mike, int index){ this.mike = mike; this.index = index; } /** Get the first/current Element */ public D getFirst(){ return this.mike.get(index); } /** Move to the next/rest of the Elements */ public ALTrav getRest(){ return new ALTrav(this.mike, this.index+1); } /** Are we done/out of Elements? */ public boolean isEmpty(){ return index >= this.mike.size(); } } /** A Wrapper that implements a "Stack" using an ArrayList */ class Stack{ ArrayList jim = new ArrayList(); /** */ void push(D d){ this.jim.add(d); } /** */ D pop(){ return this.jim.remove(this.jim.size()-1); } /** */ D peek(){ return this.jim.get(this.jim.size()-1); } /** */ boolean isEmpty(){ return this.jim.isEmpty(); } } /** A Wrapper that implements a "Queue" using an ArrayList */ class Queue{ ArrayList tim = new ArrayList(); /** */ void enqueue(D d){ this.tim.add(d); } /** */ D dequeue(){ if(this.isEmpty()) throw new RuntimeException("No Elements!!!!!"); return this.tim.remove(0); } /** */ D front(){ if(this.isEmpty()) throw new RuntimeException("No Front!!!!!"); return this.tim.get(0); } /** */ boolean isEmpty(){ return this.tim.isEmpty(); } } /** Examples and Tests */ class LectureExamples{ /** Boxed/Auto-box Example(s) */ Double d1 = 4.5 * new Double(100.3); double d2 = d1 * 53; ArrayList loi = new ArrayList(); /** Initialize the Array/List */ void init(){ loi.clear(); this.loi.add(1); this.loi.add(5); this.loi.add(3); // this.loi.add(new Integer(7)); this.loi.add(8); } /** Test/try out the ArrayList methods */ void testArrayList(Tester t){ this.init(); t.checkExpect(this.loi.get(2), 3); t.checkExpect(this.loi.get(4), 8); t.checkExpect(this.loi.set(4, 85), 8); t.checkExpect(this.loi.set(4, 99), 85); t.checkExpect(this.loi.remove(3), 7); t.checkExpect(this.loi.remove(3), 99); } /** Test our Traversal wrapper */ void testTraversal(Tester t){ this.init(); ALTrav dennis = new ALTrav(this.loi); t.checkExpect(dennis.getFirst(), 1); t.checkExpect(dennis.isEmpty(), false); t.checkExpect(dennis.getRest().getFirst(), 5); t.checkExpect(dennis.getRest() .getRest().getRest().getRest().getFirst(), 8); t.checkExpect(dennis.getRest() .getRest().getRest() .getRest().getRest().isEmpty(), true); } /** Test our Stack wrapper */ void testStack(Tester t){ Stack bob = new Stack(); bob.push(5.5); bob.push(100.0); bob.push(200.4); t.checkExpect(bob.peek(), 200.4); t.checkExpect(bob.pop(), 200.4); t.checkExpect(bob.pop(), 100.0); t.checkExpect(bob.isEmpty(), false); t.checkExpect(bob.pop(), 5.5); t.checkExpect(bob.isEmpty(), true); } /** Test our Queue wrapper */ void testQ(Tester t){ Queue bob = new Queue(); bob.enqueue(5.5); bob.enqueue(100.0); bob.enqueue(200.4); t.checkExpect(bob.front(), 5.5); t.checkExpect(bob.dequeue(), 5.5); t.checkExpect(bob.dequeue(), 100.0); t.checkExpect(bob.isEmpty(), false); t.checkExpect(bob.dequeue(), 200.4); t.checkExpect(bob.isEmpty(), true); } }