/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 5: September 14, 2006 Designing Methods for Classes with Containment and Unions of Classes */ /* +------------------------------+ | IShape | +------------------------------+ +------------------------------+ | double area() | | boolean distTo0() | | IShape grow(int x) | | boolean biggerThan(IShape x) | | boolean contains(CartPt x) | +------------------------------+ | / \ --- | ------------------------------------- | | +------------------------------+ +------------------------------+ | Circle | | Square | +------------------------------+ +------------------------------+ +-| CartPt center | +-| CartPt nw | | | int radius | | | int size | | | String color | | | String color | | +------------------------------+ | +------------------------------+ | | double area() | | | double area() | | | boolean distTo0() | | | boolean distTo0() | | | IShape grow(int x) | | | IShape grow(int x) | | | boolean biggerThan(IShape x) | | | boolean biggerThan(IShape x) | | | boolean contains(CartPt x) | | | boolean contains(CartPt x) | | +------------------------------+ | +------------------------------+ +----+ +----------------------------+ | | v v +--------+ | CartPt | +--------+ | int x | | int y | +--------+ */ // to represent a geometric shape interface IShape { // to compute the area of this shape double area(); // to compute the distance form this shape to the origin double distTo0(); // to increase the size of this shape by the given increment IShape grow(int inc); // is the area of this shape is bigger than the area of the given shape? boolean biggerThan(IShape that); // does this shape (including the boundary) contain the given point? boolean contains(CartPt pt); } // to represent a circle class Circle implements IShape { CartPt center; int radius; String color; Circle(CartPt center, int radius, String color) { this.center = center; this.radius = radius; this.color = color; } /* // ** TEMPLATE ** Edit as needed. public returnType methodName() { ... this.center ... -- CartPt ... this.radius ... -- int ... this.color ... -- String } */ // to compute the area of this shape double area(){ return Math.PI * this.radius * this.radius; } // to compute the distance form this shape to the origin double distTo0(){ return this.center.distTo0() - this.radius; } // to increase the size of this shape by the given increment IShape grow(int inc){ return new Circle(this.center, this.radius + inc, this.color); } // is the area of this shape is bigger than the area of the given shape? boolean biggerThan(IShape that){ return this.area() >= that.area(); } // does this shape (including the boundary) contain the given point? boolean contains(CartPt pt){ return this.center.distTo(pt) <= this.radius; } } // to represent a square class Square implements IShape { CartPt nw; int size; String color; Square(CartPt nw, int size, String color) { this.nw = nw; this.size = size; this.color = color; } /* // ** TEMPLATE ** returnType methodName() { ... this.nw ... -- CartPt ... this.size ... -- int ... this.color ... -- String } */ // to compute the area of this shape double area(){ return this.size * this.size; } // to compute the distance form this shape to the origin double distTo0(){ return this.nw.distTo0(); } // to increase the size of this shape by the given increment IShape grow(int inc){ return new Square(this.nw, this.size + inc, this.color); } // is the area of this shape is bigger than the area of the given shape? boolean biggerThan(IShape that){ return this.area() >= that.area(); } // does this shape (including the boundary) contain the given point? boolean contains(CartPt pt){ return (this.nw.x <= pt.x) && (pt.x <= this.nw.x + this.size) && (this.nw.y <= pt.y) && (pt.y <= this.nw.y + this.size); } } /* +--------+ | 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; } // to compute the distance form this point to the origin double distTo0(){ return Math.sqrt(this.x * this.x + this.y * this.y); } // to compute the distance form this point to the given point double distTo(CartPt pt){ return Math.sqrt((this.x - pt.x) * (this.x - pt.x) + (this.y - pt.y) * (this.y - pt.y)); } } class Examples { Examples() {} CartPt pt1 = new CartPt(0, 0); CartPt pt2 = new CartPt(3, 4); CartPt pt3 = new CartPt(7, 1); IShape c1 = new Circle(new CartPt(50, 50), 10, "red"); IShape c2 = new Circle(new CartPt(50, 50), 30, "red"); IShape c3 = new Circle(new CartPt(30, 100), 30, "blue"); IShape s1 = new Square(new CartPt(50, 50), 30, "red"); IShape s2 = new Square(new CartPt(50, 50), 50, "red"); IShape s3 = new Square(new CartPt(20, 40), 10, "green"); // test the method distTo0 in the class CartPt boolean testDistTo0 = (check this.pt1.distTo0() expect 0 within 0.001) && (check this.pt2.distTo0() expect 5 within 0.001); // test the method distTo in the class CartPt boolean testDistTo = (check this.pt1.distTo(this.pt2) expect 5 within 0.001) && (check this.pt2.distTo(this.pt3) expect 5 within 0.001); // test the method area in the class Circle boolean testCircleArea = check this.c1.area() expect 314.15 within 0.01; // test the method grow in the class Circle boolean testSquareArea = check this.s1.area() expect 900 within 0.01; // test the method distTo0 in the class Circle boolean testCircleDistTo0 = (check this.c1.distTo0() expect 60.71 within 0.01) && (check this.c3.distTo0() expect 74.40 within 0.01); // test the methdod distTo in the class Circle boolean testSquareDistTo0 = (check this.s1.distTo0() expect 70.71 within 0.01) && (check this.s3.distTo0() expect 44.72 within 0.01); // test the method grow in the class Circle boolean testCircleGrow = check this.c1.grow(20) expect this.c2; // test the method grow in the class Circle boolean testSquareGrow = check this.s1.grow(20) expect this.s2; // test the method biggerThan in the class Circle boolean testCircleBiggerThan = (check this.c1.biggerThan(this.c2) expect false) && (check this.c2.biggerThan(this.c1) expect true) && (check this.c1.biggerThan(this.s1) expect false) && (check this.c1.biggerThan(this.s3) expect true); // test the method biggerThan in the class Square boolean testSquareBiggerThan = (check this.s1.biggerThan(this.s2) expect false) && (check this.s2.biggerThan(this.s1) expect true) && (check this.s1.biggerThan(this.c1) expect true) && (check this.s3.biggerThan(this.c1) expect false); // test the method contains in the class Circle boolean testCircleContains = (check this.c1.contains(new CartPt(100, 100)) expect false) && (check this.c2.contains(new CartPt(40, 60)) expect true); // test the method contains in the class Square boolean testSquareContains = (check this.s1.contains(new CartPt(100, 100)) expect false) && (check this.s2.contains(new CartPt(55, 60)) expect true); }