Write an interface called Shape
which declares (non-static) methods to
- compute area and perimeter: area ()
& perimeter ()
- create a bounding rectangle: getBounds ()
(return a Rectangle
)
Then define classes Rectangle, Circle
and (if time) Triangle
which implement Shape
. Write a simple test program too.
You may have to devise other classes (such as Point
) too, or look in the API documentation to find suitable ones to use (suggest looking in java.awt).
You may need to compile all together, with javac *.java
, because of circular dependencies.
Circle: area = Math.PI * r * r;
perimeter = Math.PI * 2.0 * r;
Rectangle: area = width * height;
perimeter = 2 * (width + height);
Triangle: sides: a = distance (p1, p2); b = distance (p2, p3);
c = distance (p3, p1);
area: s = (a + b + c) / 2.0;
area = Math.sqrt (s * (s - a) * (s - b) * (s - c));
perimeter = a + b + c;
where
distance (p, q) = Math.sqrt (square (p.x - q.x)
+ square (p.y - q.y));
square (x) = x * x;