/* +-----------------------+ | +-------------------+ | | | | | v v | | +--------+ | | | IShape | | | +--------+ | | / \ | | --- | | | | | -------------------------------- | | | | | | | | | | | | +--------------+ +-------+ +------------+ | | | CircleShape | | Rect | | Combo | | | +--------------+ +-------+ +------------+ | | | int x | | int x | | IShape top |--+ | | int y | | int y | | IShape bot |----+ | int rad | | int w | +------------+ +--------------+ | int h | +-------+ */ import tester.*; // to represent a geometric shape interface IShape{} // to represent a circle class CircleShape implements IShape{ int x; int y; int rad; CircleShape(int x, int y, int rad){ this.x = x; this.y = y; this.rad = rad; } /* TEMPLATE: Fields: ... this.x ... -- int ... this.y ... -- int ... this.rad ... -- int Methods: ... this.area() ... -- double ... */ // compute area of this circle // area: Circle -> Num // (define (area cir) (* PI (* (circle-rad cir) (circle-rad cir)))) // (check-expect (area (make-circle (20, 30, 10)) 314.15926) double area(){ return Math.PI * this.rad * this.rad; } } // to represent a rectangle class Rect implements IShape{ int x; int y; int w; int h; Rect(int x, int y, int w, int h){ this.x = x; this.y = y; this.w = w; this.h = h; } /* TEMPLATE: Fields: ... this.x ... -- int ... this.y ... -- int ... this.w ... -- int ... this.h ... -- int Methods: ... */ } // to represent a combined shape class Combo implements IShape{ IShape top; IShape bot; Combo(IShape top, IShape bot){ this.top = top; this.bot = bot; } /* TEMPLATE: Fields: ... this.top ... -- IShape ... this.bot ... -- IShape Methods: ... */ } class ExamplesShapes{ ExamplesShapes(){} // make examples of all classes IShape circle = new CircleShape(50, 50, 50); IShape rleft = new Rect(20, 20, 20, 20); IShape rBot = new Rect(20, 60, 60, 20); IShape addMouth = new Combo(this.rBot, this.circle); IShape addLeftEye = new Combo(this.rleft, this.addMouth); IShape face = new Combo(new Rect(60, 20, 20, 20), this.addLeftEye); CircleShape cir2 = new CircleShape(20, 30, 10); CircleShape cir3 = new CircleShape(20, 30, 2); // test the method area defined in the class Circle boolean testAreaCircle(Tester t){ return t.checkInexact(cir2.area(), 314.15926, 0.1) && t.checkInexact(cir3.area(), 4 * 3.1415926, 0.1); } }