/*********************************************** * CS2510 Spring 2011 * Lecture #12.1 * Equality via double-dispatch ***********************************************/ import tester.*; // Represents a Location class Loc{ int x; int y; Loc(int x, int y){ this.x = x; this.y = y; } // Is this Loc the same as the given one boolean sameLoc(Loc that){ return (that.x == this.x && that.y == this.y); } } // Represents a Shape interface IShape{ // Is that shape the same as this Shape public boolean sameShape(IShape that); // Is that Circle the same as this Shape public boolean sameCirc(Circ that); // Is that Rect the same as this Shape public boolean sameRect(Rect that); // Is that Combo the same as this Shape public boolean sameCombo(Combo that); } // Represents a Circle class Circ implements IShape{ Loc loc; int rad; Circ(Loc loc, int rad) { this.loc = loc; this.rad = rad; } /** Template: * Fields: * ... this.loc ... -- Loc * ... this.rad ... -- int * * Methods: * ... this.sameShape(IShape) ... -- boolean * ... this.sameCirc(Circ) ... -- boolean * ... this.sameRect(Rect) ... -- boolean * ... this.sameCombo(Combo) ... -- boolean * * Methods of Fields: * ... this.loc.sameLoc() ... -- boolean */ // Is that Shape the same as this Circ public boolean sameShape(IShape that){ return that.sameCirc(this); } // Is that Circle the same as this Circ public boolean sameCirc(Circ that){ return (this.loc.sameLoc(that.loc) && this.rad == that.rad); } // Is that Rect the same as this Circ public boolean sameRect(Rect that){ return false; } // Is that Combo the same as this Circ public boolean sameCombo(Combo that){ return false; } } // Represents a Rectangle class Rect implements IShape{ Loc loc; int w; int h; Rect(Loc loc, int w, int h) { this.loc = loc; this.w = w; this.h = h; } // Is that Shape the same as this Rect public boolean sameShape(IShape that){ return that.sameRect(this); } // Is that Circ the same as this Rect public boolean sameCirc(Circ that){ return false; } // Is that Rect the same as this Rect public boolean sameRect(Rect that){ return (this.loc.sameLoc(that.loc) && this.w == that.w && this.h == that.h); } // Is that Combo the same as this Rect public boolean sameCombo(Combo that){ return false; } } // Represents a Combo class Combo implements IShape{ IShape top; IShape bot; Combo(IShape top, IShape bot) { this.top = top; this.bot = bot; } // Is that Shape the same as this Combo public boolean sameShape(IShape that){ return that.sameCombo(this); } // Is that Circ the same as this Combo public boolean sameCirc(Circ that){ return false; } // Is that Rect the same as this Combo public boolean sameRect(Rect that){ return false; } // Is that Combo the same as this Combo public boolean sameCombo(Combo that){ return (that.top.sameShape(this.top) && that.bot.sameShape(this.bot)); } } // Exhaustive Testing... class LectureExamples{ IShape c1 = new Circ(new Loc(50, 50), 10); IShape c2 = new Circ(new Loc(60, 40), 30); IShape c3 = new Circ(new Loc(50, 50), 10); IShape r1 = new Rect(new Loc(50, 50), 10, 20); IShape r2 = new Rect(new Loc(60, 40), 30, 50); IShape r3 = new Rect(new Loc(50, 50), 10, 20); IShape comb1 = new Combo(this.c1, this.c2); IShape comb2 = new Combo(this.r1, this.c1); IShape comb3 = new Combo(this.c1, this.c2); IShape comb4 = new Combo(this.comb1, this.comb2); IShape comb5 = new Combo(this.comb1, this.comb2); IShape comb6 = new Combo(this.comb1, this.comb1); // Test the same kinds of things (Circ/Circ, Rect/Rect) boolean testSameOnes(Tester t){ return (t.checkExpect(this.c1.sameShape(this.c1), true) && t.checkExpect(this.c1.sameShape(this.c2), false) && t.checkExpect(this.c3.sameShape(this.c1), true) && t.checkExpect(this.r1.sameShape(this.r1), true) && t.checkExpect(this.r1.sameShape(this.r2), false) && t.checkExpect(this.r3.sameShape(this.r1), true) && t.checkExpect(this.comb1.sameShape(this.comb1), true) && t.checkExpect(this.comb1.sameShape(this.comb2), false) && t.checkExpect(this.comb4.sameShape(this.comb5), true) && t.checkExpect(this.comb5.sameShape(this.comb6), false)); } // Test different kinds of things (Circ/Rect, Rect/Combo) all false boolean testSameDiffs(Tester t){ return (t.checkExpect(this.r1.sameShape(this.c1), false) && t.checkExpect(this.comb1.sameShape(this.c2), false) && t.checkExpect(this.c1.sameShape(this.r1), false) && t.checkExpect(this.comb1.sameShape(this.r2), false) && t.checkExpect(this.c1.sameShape(this.comb1), false) && t.checkExpect(this.r2.sameShape(this.comb2), false)); } }