/*********************************************** * CS2510 Spring 2011 * Exam 2 Review * List, Function-objects and Accumulators ***********************************************/ import tester.*; /** Function-Objects * Accumulators * List Recursion */ class ExamReview{ ByEyeShade eyes = new ByEyeShade(); Laptop l1 = new Laptop("red", true, 5); Laptop l2 = new Laptop("blue", false, 12); Laptop l3 = new Laptop("white", true, 100); ILoL lol = new ConsLoL(this.l1, new ConsLoL(this.l2, new ConsLoL(this.l3, new MtLoL()))); ILoL lolagain = new ConsLoL(this.l1, new ConsLoL(this.l2, new ConsLoL(this.l2, new ConsLoL(this.l3, new MtLoL())))); void testComp(Tester t){ t.checkExpect(eyes.isBefore(this.l1, this.l2), false); t.checkExpect(eyes.isBefore(this.l2, this.l1), true); t.checkExpect(eyes.isBefore(this.l1, this.l1), true); t.checkExpect(this.lol.isSorted(new ByHeight()), true); t.checkExpect(this.lol.mostest(new ByHeight()), this.l3); t.checkExpect(this.lol.removeDups(), this.lol); t.checkExpect(new ConsLoL(this.l1, this.lol).removeDups(), this.lol); t.checkExpect(this.lolagain.removeDups(), this.lol); } } /** Represents a Laptop */ class Laptop{ String eyecolor; boolean teethHuh; int height; Laptop(String eyecolor, boolean teethHuh, int height) { this.eyecolor = eyecolor; this.teethHuh = teethHuh; this.height = height; } /** Is this Laptop the same as that one */ boolean sameLaptop(Laptop that){ return (this.eyecolor.equals(that.eyecolor) && this.teethHuh == that.teethHuh && this.height == that.height); } } /** Compare two Laptops */ interface CompLaptop{ /** Does the first Laptop belong before the second */ public boolean isBefore(Laptop l1, Laptop l2); } /** Compare two Laptops by Eyecolor */ class ByEyeShade implements CompLaptop{ public boolean isBefore(Laptop l1, Laptop l2){ return l1.eyecolor.compareTo(l2.eyecolor) <= 0; } } /** Compare Laptops by Height */ class ByHeight implements CompLaptop{ public boolean isBefore(Laptop l1, Laptop l2){ return l1.height <= l2.height; } } /** Custom Exception */ class Up extends RuntimeException{ Up(){ super("Ka-Boom!!"); } } /** Represents a List of Laptops */ interface ILoL{ /** Is this List Sorted? */ boolean isSorted(CompLaptop comp); /** Is this List Sorted? Acc Style * The ACC represents the previous element in the list */ boolean isSortedAcc(CompLaptop comp, Laptop acc); /** Find the Laptop with the most goodness (the best one, for * the given definition of better) */ Laptop mostest(CompLaptop comp); /** Find the Laptop with the most goodness * The ACC is the Mostest so far */ Laptop mostestAcc(CompLaptop comp, Laptop acc); /** Does this list contain duplicate laptops? */ boolean hasDups(); /** Does this list contain the given laptop? */ boolean contains(Laptop l); /** Remove the duplicates */ ILoL removeDups(); } /** Represents the empty List */ class MtLoL implements ILoL{ /** Is this List Sorted? */ public boolean isSorted(CompLaptop comp){ return true; } /** Is this List Sorted? Acc Style * The ACC represents the previous element in the list */ public boolean isSortedAcc(CompLaptop comp, Laptop acc){ return true; } /** Find the Laptop with the most goodness (the best one, for * the given definition of better) */ public Laptop mostest(CompLaptop comp){ throw new Up(); } /** Find the Laptop with the most goodness * The ACC is the Mostest so far */ public Laptop mostestAcc(CompLaptop comp, Laptop acc){ return acc; } /** Does this list contain duplicate laptops? */ public boolean hasDups(){ return false; } /** Does this list contain the given laptop? */ public boolean contains(Laptop l){ return false; } /** Remove the duplicates */ public ILoL removeDups(){ return this; } } /** Represents a nonempty List */ class ConsLoL implements ILoL{ Laptop first; ILoL rest; ConsLoL(Laptop first, ILoL rest) { this.first = first; this.rest = rest; } /** Is this List Sorted? */ public boolean isSorted(CompLaptop comp){ return this.rest.isSortedAcc(comp, this.first); } /** Is this List Sorted? Acc Style * The ACC represents the prvious element in the list */ public boolean isSortedAcc(CompLaptop comp, Laptop acc){ /* Template: * Fields: * this.first -- Laptop * this.rest -- ILoL * * Methods of Field: * this.rest.isSorted(CompLaptop) * this.rest.isSortedAcc(CompLaptop, Laptop) * this.rest.mostest(CompLaptop) * * comp.isBefore(Laptop, Laptop) */ return (comp.isBefore(acc, this.first) && this.rest.isSortedAcc(comp, this.first)); } /** Find the Laptop with the most goodness (the best one, for * the given definition of better) */ public Laptop mostest(CompLaptop comp){ return this.rest.mostestAcc(comp, this.first); } /** Find the Laptop with the most goodness * The ACC is the Mostest so far */ public Laptop mostestAcc(CompLaptop comp, Laptop acc){ if(comp.isBefore(acc, this.first)){ return this.rest.mostestAcc(comp, this.first); }else{ return this.rest.mostestAcc(comp, acc); } } /** Does this list contain duplicate laptops? */ public boolean hasDups(){ return (this.rest.contains(this.first) || this.rest.hasDups()); } /** Does this list contain the given laptop? */ public boolean contains(Laptop l){ return (this.first.sameLaptop(l) || this.rest.contains(l)); } /** Remove the duplicates */ public ILoL removeDups(){ if(this.rest.contains(this.first)){ return this.rest.removeDups(); }else{ return new ConsLoL(this.first, this.rest.removeDups()); } } }