/*********************************************** * CS2510 Spring 2011 * Lecture #5 * More Methods for Unions and Lists ***********************************************/ import tester.*; // Represents a Location (X/Y) class Loc{ int x; int y; Loc(int x, int y){ this.x = x; this.y = y; } /* Template * Fields * ... this.x ... -- int * ... this.y ... -- int */ } // Represents a Shape interface IShape{ // Calculate the area of this Shape double area(); } // Represents a Circle Shape class Circ implements IShape{ Loc c; int r; Circ(Loc c, int r){ this.c = c; this.r = r; } /* Template * Fields * ... this.c ... -- Loc * ... this.r ... -- int * * Methods * ... this.area() ... -- double * * Methods of Fields */ // Calculate the area of this Circle double area(){ return Math.PI * this.r * this.r; } } // Represents a Rectangle Shape class Rect implements IShape{ Loc tl; int w; int h; Rect(Loc tl, int w, int h){ this.tl = tl; this.w = w; this.h = h; } // Calculate the area of this Rectangle double area(){ return this.w * this.h; } } // Represent the combination of two IShapes 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 * ... this.area() ... -- double * * Methods of Fields * ... this.top.area() -- double * ... this.bot.area() -- double */ // Calculate the total area of this Combo double area(){ return this.top.area() + this.bot.area(); } } // Represents a List of Shapes interface ILoS{ // Calculate the total area of all shapes in the list double totalArea(); } // Represents an empty List of Shapes class MtLoS implements ILoS{ MtLoS(){} /* Template * Fields * Methods * ... this.totalArea() ... -- double * * Methods of Fields */ // Calculate the total area of all shapes in this empty list double totalArea(){ return 0.0; } } // Represents a nonempty List of Shapes class ConsLoS implements ILoS{ IShape first; ILoS rest; ConsLoS(IShape first, ILoS rest){ this.first = first; this.rest = rest; } /* Template * Fields * ... this.first ... -- IShape * ... this.rest ... -- ILoS * * Methods * ... this.totalArea() ... -- double * * Methods of Fields * ... this.first.area() ... -- double * ... this.rest.totalArea() ... -- double * */ // Calculate the total area of all shapes in this nonempty list double totalArea(){ return this.first.area() + this.rest.totalArea(); } } // Examples and Tests for Shapes and ILoSs class ShapeExamples{ ShapeExamples(){} IShape rect = new Rect(new Loc(4, 3), 20, 5); IShape circ = new Circ(new Loc(12, 9), 10); IShape combo = new Combo(this.rect, this.circ); ILoS empty = new MtLoS(); ILoS los = new ConsLoS(this.combo, this.empty); ILoS los2 = new ConsLoS(this.combo, new ConsLoS(this.circ, this.empty)); ILoS los2sorted = new ConsLoS(this.circ, new ConsLoS(this.combo, this.empty)); // Test IShape.area() boolean testArea(Tester t){ return (t.checkExpect(this.rect.area(), 100.0) && t.checkInexact(this.circ.area(), Math.PI*100.0, 0.001) && t.checkInexact(this.combo.area(), 100+Math.PI*100.0, 0.001)); } // Test ILoS.totalArea() boolean testTotalArea(Tester t){ return (t.checkInexact(this.empty.totalArea(), 0.0, 0.001) && t.checkInexact(this.los.totalArea(), 100+Math.PI*100.0, 0.001) && t.checkInexact(this.los2.totalArea(), 100+Math.PI*200.0, 0.001)); } }