/*********************************************** * CS2510 Fall 2011 * Lecture #12.3 * Equality, the Java(-ish) way ***********************************************/ 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); } // 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 */ // Is that shape the same as this Circ public boolean sameShape(IShape that){ if (that.getClass() == Circ.class) { return (this.loc.sameLoc(((Circ)that).loc) && this.rad == ((Circ)that).rad); } else { return false; } } // Does not obey symmetry in conjunction with UnitCirc's sameShapeBAD public boolean sameShapeBAD(IShape that){ if (that instanceof Circ) { return (this.loc.sameLoc(((Circ)that).loc) && this.rad == ((Circ)that).rad); } else { return false; } } } // A circle with radius 1 class UnitCirc extends Circ { /** * Template * * Fields: * this.loc -- Loc * this.rad -- int * Methods: * this.sameShape(IShape) -- boolean */ UnitCirc(Loc loc) { super(loc, 1); } // Is that shape the same as this Circ public boolean sameShape(IShape that){ if (that.getClass() == UnitCirc.class) { return this.loc.sameLoc(((UnitCirc)that).loc); } else { return false; } } // Does not obey symmetry in conjunction with Circ's sameShapeBAD public boolean sameShapeBAD(IShape that){ if (that instanceof UnitCirc) { return this.loc.sameLoc(((UnitCirc)that).loc); } else { 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){ if (that.getClass() == Rect.class) { return (this.loc.sameLoc(((Rect)that).loc) && this.w == ((Rect)that).w && this.h == ((Rect)that).h); } else { 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){ if (that.getClass() == Combo.class) { return (this.top == ((Combo)that).top && this.bot == ((Combo)that).bot); } else { return false; } } } // Exaustive 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); IShape c4 = new Circ(new Loc(50,50),1); IShape uc1 = new UnitCirc(new Loc(50,50)); IShape uc2 = new UnitCirc(new Loc(50,50)); // 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) && t.checkExpect(this.uc1.sameShape(uc2),true) && // By symmetry of equality this should be true for any 2 objects t.checkExpect(this.c4.sameShape(uc1),this.uc1.sameShape(c4))); } // 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)); } }