/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 10: September 27, 2006 Abstracting over data Definitions Part 3: Comparing for equality */ /* +---------------------------------+ | IShape | +---------------------------------+ +---------------------------------+ | boolean sameShape(IShape that) | | boolean sameCircle(Circle that) | | boolean sameCircle(Circle that) | +---------------------------------+ | / \ --- | +------------------------------------------+ | AShape | +------------------------------------------+ +---------------| CartPt loc | | +------------------------------------------+ | | abstract boolean sameShape(IShape that) | | | boolean sameCircle(Circle that) | | | boolean sameSquare(Square that) | | +------------------------------------------+ | | | / \ | --- | | | ------------------------------------- | | | | +---------------------------------+ +---------------------------------+ | | Circle | | Square | | +---------------------------------+ +---------------------------------+ | | int radius | | int size | | | String color | | String color | | +---------------------------------+ +---------------------------------+ | | boolean sameShape(IShape that) | | boolean sameShape(IShape that) | | | boolean sameCircle(Circle that) | | boolean sameCircle(Circle that) | | +---------------------------------+ +---------------------------------+ +----+ | v +--------+ | CartPt | +--------+ | int x | | int y | +--------+ */ // to represent a geometric shape interface IShape { // is this shape the same as that shape boolean sameShape(IShape that); // is this shape the same as that circle boolean sameCircle(Circle that); // is this shape the same as that square boolean sameSquare(Square that); } // to represent a geometric shape abstract class AShape implements IShape{ CartPt loc; AShape(CartPt loc){ this.loc = loc; } // is this shape the same as that shape abstract boolean sameShape(IShape that); // is this shape the same as that circle boolean sameCircle(Circle that){ return false; } // is this shape the same as that square boolean sameSquare(Square that){ return false; } } // to represent a circle class Circle extends AShape { int radius; String color; Circle(CartPt loc, int radius, String color) { super(loc); this.radius = radius; this.color = color; } /* // ** TEMPLATE ** public returnType methodName() { ... this.loc ... -- CartPt ... this.radius ... -- int ... this.color ... -- String ... this.sameShape(IShape) ... -- boolean ... this.sameCircle(Circle) ... -- boolean */ // is this shape the same as that shape boolean sameShape(IShape that){ return that.sameCircle(this); } // is this shape the same as that circle boolean sameCircle(Circle that){ return this.loc.sameCartPt(that.loc) && this.radius == that.radius && this.color.equals(that.color); } } // to represent a square class Square extends AShape { int size; String color; Square(CartPt loc, int size, String color) { super(loc); this.size = size; this.color = color; } /* // ** TEMPLATE ** returnType methodName() { ... this.loc ... -- CartPt ... this.size ... -- int ... this.color ... -- String ... this.sameShape(IShape) ... -- boolean ... this.sameSquare(Square) ... -- boolean */ // is this shape the same as that shape boolean sameShape(IShape that){ return that.sameSquare(this); } // is this shape the same as that square boolean sameSquare(Square that){ return this.loc.sameCartPt(that.loc) && this.size == that.size && this.color.equals(that.color); } } /* +--------+ | CartPt | +--------+ | int x | | int y | +--------+ */ // to represent a Cartesian point class CartPt { int x; int y; CartPt(int x, int y) { this.x = x; this.y = y; } // is this cartesian points the same as that cartesian point boolean sameCartPt(CartPt that){ return this.x == that.x && this.y == that.y; } } class Examples { Examples() {} CartPt pt1 = new CartPt(0, 0); CartPt pt2 = new CartPt(3, 4); CartPt pt3 = new CartPt(7, 1); Circle c1 = new Circle(new CartPt(50, 50), 10, "red"); Circle c2 = new Circle(new CartPt(50, 50), 30, "red"); Circle c3 = new Circle(new CartPt(30, 100), 30, "blue"); Square s1 = new Square(new CartPt(50, 50), 30, "red"); Square s2 = new Square(new CartPt(50, 50), 50, "red"); Square s3 = new Square(new CartPt(20, 40), 50, "green"); // test the method sameCartPt in the class CartPt boolean testSameCartPt = (check this.pt1.sameCartPt(this.pt2) expect false) && (check this.pt2.sameCartPt(new CartPt(3, 4)) expect true); // test the method sameShape in the class Circle boolean testCircleSameShape = (check this.c1.sameShape(this.c2) expect false) && (check this.c2.sameShape(this.c3) expect false) && (check this.c2.sameShape(new Circle(new CartPt(50, 50), 30, "red")) expect true) && (check this.c1.sameShape(this.s1) expect false); // test the method sameCircle in the class Circle boolean testCircleSameCircle = (check this.c1.sameCircle(this.c2) expect false) && (check this.c2.sameCircle(this.c3) expect false) && (check this.c2.sameCircle(new Circle(new CartPt(50, 50), 30, "red")) expect true); // test the method sameSquare in the class Circle boolean testCircleSameSquare = (check this.c1.sameSquare(this.s2) expect false) && (check this.c2.sameSquare(this.s3) expect false); // test the method sameShape in the class Square boolean testSquareSameShape = (check this.s1.sameShape(this.s2) expect false) && (check this.s2.sameShape(this.s3) expect false) && (check this.s2.sameShape(new Square(new CartPt(50, 50), 50, "red")) expect true) && (check this.s1.sameShape(this.c1) expect false); // test the method sameSquare in the class Square boolean testSquareSameSquare = (check this.s1.sameSquare(this.s2) expect false) && (check this.s2.sameSquare(this.s3) expect false) && (check this.s2.sameSquare(new Square(new CartPt(50, 50), 50, "red")) expect true); // test the method sameCircle in the class Square boolean testSquareSameCircle = (check this.s1.sameCircle(this.c2) expect false) && (check this.s2.sameCircle(this.c3) expect false); }