/*********************************************** * CS2510 Fall 2011 * Lecture #19 * ZombieWorld Recap; Multi-Dispatch ***********************************************/ import tester.*; import world.*; import image.*; // Design the method boolean hasDups // Design the method ILoI removeDups // Design the method ILoI incrementDups interface ILoI { boolean hasDups(); boolean contains(int n); ILoI removeDups(); ILoI incrementDups(); ILoI incrementBobs(int bob); // averageAllWith: Averages with given int ILoI averageAllWith(int y); } class MtLoI implements ILoI { public boolean hasDups() { return false; } public boolean contains(int n) { return false; } public ILoI removeDups() { return this; } public ILoI incrementDups() { return this; } public ILoI incrementBobs(int bob) { return this; } public ILoI averageAllWith(int y) { return this; } } class ConsLoI implements ILoI { int first; ILoI rest; ConsLoI(int first, ILoI rest) { this.first = first; this.rest = rest; } public boolean hasDups() { return rest.contains(first) || rest.hasDups(); } public boolean contains(int n) { return (this.first == n) || this.rest.contains(n); } public ILoI removeDups() { if (rest.contains(first)) { return rest.removeDups(); } else { return new ConsLoI(first,rest.removeDups()); } } // Increment all duplicates by 1 public ILoI incrementDups() { if (rest.contains(first)) { return new ConsLoI(first + 1, rest.incrementBobs(first).incrementDups()); //return this.incrementBobs(first).incrementDups(); } else { return new ConsLoI(first,rest.incrementDups()); } } // Increments all elements that match bob public ILoI incrementBobs(int bob) { if(this.first == bob) { return new ConsLoI(first+1,rest.incrementBobs(bob)); } else { return new ConsLoI(first,rest.incrementBobs(bob)); } } // averageAllWith : Averages ... public ILoI averageAllWith(int y) { return new ConsLoI(Math.round((first + y)/2), rest.averageAllWith(y)); } } // Example of lastScene class ZombieWorld { ILoZ zombies; public Scene onDraw() { ... } public Scene lastScene() { if (zombies.allDeadHuh()) { return onDraw().placeImage(new Text("You win! Yay!!!",24,"Blue"),300,300); } else { return onDraw().placeImage(new Text("Sad for you... Try again.", 24, "Red"),300,300); } } } // Must test all methods we designed (and make instances of all classes) //Design the method boolean hasDups //Design the method ILoI removeDups //Design the method ILoI incrementDups //Design the method ILoI averageAllWith class LecExamples { ILoI list = new ConsLoI(1,new ConsLoI(3,new ConsLoI(3,new ConsLoI(1, new ConsLoI(6,new MtLoI()))))); ILoI listremdups = new ConsLoI(3,new ConsLoI(1, new ConsLoI(6,new MtLoI()))); ILoI listinc = new ConsLoI(2,new ConsLoI(4,new ConsLoI(4,new ConsLoI(2, new ConsLoI(6,new MtLoI()))))); ILoI nodups = new ConsLoI(1,new ConsLoI(2,new ConsLoI(3,new ConsLoI(4, new ConsLoI(6,new MtLoI()))))); ILoI listincavg = new ConsLoI(6,new ConsLoI(7, new ConsLoI(7, new ConsLoI(6,new ConsLoI(8, new MtLoI()))))); // Testing our Dups methods void testDups(Tester t) { t.checkExpect(list.hasDups(),true); t.checkExpect(nodups.hasDups(),false); t.checkExpect(list.removeDups(),listremdups); t.checkExpect(nodups.removeDups(),nodups); t.checkExpect(list.incrementDups(),listinc); t.checkExpect(nodups.incrementDups(),nodups); } // Testing our averageAllWith void testAvg(Tester t) { t.checkExpect(listinc.averageAllWith(10),listincavg); } }