1
2
3 import junit.framework.*;
4
5 abstract class Shape {
6 public abstract double area();
7 public abstract double circumference();
8 }
9
10 class Circle extends Shape {
11 protected double r;
12 public Circle(double aRadius) {
13 this.r = aRadius;
14 }
15 public double getRadius() {
16 return r;
17 }
18 public double area() {
19 return Math.PI * r * r;
20 }
21 public double circumference() {
22 return 2 * Math.PI * r;
23 }
24 }
25
26 class Rectangle extends Shape {
27 protected double w, h;
28 public Rectangle(double aWidth, double aHeight) {
29 this.w = aWidth;
30 this.h = aHeight;
31 }
32 public double getWidth() {
33 return w;
34 }
35 public double getHeight() {
36 return h;
37 }
38 public double area() {
39 return w * h;
40 }
41 public double circumference() {
42 return 2 * (w+h);
43 }
44 }
45
46 class Square extends Shape {
47 protected double s;
48 public Square(double aSide) {
49 s = aSide;
50 }
51 public double getSide() {
52 return s;
53 }
54 public double area() {
55 return s * s;
56 }
57 public double circumference() {
58 return 4 * s;
59 }
60 }
61
62 interface Centered {
63 public void setCenter(double aX, double aY);
64 public double getCenterX();
65 public double getCenterY();
66 }
67
68 class CenteredRectangle extends Rectangle implements Centered {
69 private double cx, cy;
70 public CenteredRectangle(double aCx, double aCy,
71 double aWidth, double aHeight) {
72 super(aWidth, aHeight);
73 cx = aCx;
74 cy = aCy;
75 }
76 public void setCenter(double aCx, double aCy) {
77 cx = aCx; cy = aCy;
78 }
79 public double getCenterX() {
80 return cx;
81 }
82 public double getCenterY() {
83 return cy;
84 }
85 }
86
87 class CenteredCircle extends Circle implements Centered {
88 private double cx, cy;
89 public CenteredCircle(double aCx, double aCy, double aRadius) {
90 super(aRadius);
91 cx = aCx;
92 cy = aCy;
93 }
94 public void setCenter(double aCx, double aCy) {
95 cx = aCx;
96 cy = aCy;
97 }
98 public double getCenterX() {
99 return cx;
100 }
101 public double getCenterY() {
102 return cy;
103 }
104 }
105
106 class CenteredSquare extends Square implements Centered {
107 private double cx, cy;
108 public CenteredSquare(double aCx, double aCy, double aSide) {
109 super(aSide);
110 cx = aCx;
111 cy = aCy;
112 }
113 public void setCenter(double aCx, double aCy) {
114 cx = aCx;
115 cy = aCy;
116 }
117 public double getCenterX() {
118 return cx;
119 }
120 public double getCenterY() {
121 return cy;
122 }
123 }
124
125
126
127 public class ShapeTest extends TestCase {
128 public static void main(String[] args){
129 junit.textui.TestRunner.run(ShapeTest.class);
130 }
131 public void testCircle() {
132 Circle myCircle = new Circle(5.5);
133 assertEquals(5.5, myCircle.getRadius(), 0.001);
134 assertEquals(95.0331, myCircle.area(), 0.001);
135 assertEquals(34.557, myCircle.circumference(), 0.001);
136 }
137 public void testRectangle() {
138 Rectangle myRec = new Rectangle(3,4);
139 assertEquals(3, myRec.getWidth(), 0.001);
140 assertEquals(12, myRec.area(), 0.001);
141 }
142 public void testAbstractClass() {
143 Shape[] shapes = new Shape[3];
144 shapes[0] = new Circle(2.0);
145 shapes[1] = new Rectangle(1, 3.0);
146 shapes[2] = new Rectangle(4.0, 2.0);
147 double total_area = 0;
148 for(int i=0; i < shapes.length; i++)
149 total_area += shapes[i].area();
150 assertEquals(23.566, total_area, 0.001);
151 }
152 public void testSquare() {
153 Square mySquare = new Square(3);
154 assertEquals(3.0, mySquare.getSide(), 0.001);
155 }
156 public void testCenteredRectangle() {
157 CenteredRectangle myCRec = new CenteredRectangle(1,2,3,4);
158 assertEquals(1.0, myCRec.getCenterX(), 0.001);
159 }
160 public void testCenteredInterface() {
161 Shape[] shapes = new Shape[3];
162 shapes[0] = new CenteredCircle(1.0, 1.0, 1.0);
163 shapes[1] = new CenteredSquare(2.5, 2, 3);
164 shapes[2] = new CenteredRectangle(2.3, 4.5, 3, 4);
165
166 double totalArea = 0;
167 double totalDistance = 0;
168 for(int i=0; i < shapes.length; i++) {
169 totalArea += shapes[i].area();
170 if(shapes[i] instanceof Centered) {
171 Centered c = (Centered)shapes[i];
172 double cx = c.getCenterX();
173 double cy = c.getCenterY();
174 totalDistance += Math.sqrt(cx*cx + cy*cy);
175 }
176 }
177 assertEquals(24.1415, totalArea, 0.001);
178 assertEquals(9.669, totalDistance, 0.001);
179 }
180 }