A Shape is on of:
a Circle: consisting of a center and a radius, where the center is a Posn and the radius is an integer
a Rectangle: consisting of a nw, a width, and a height, where the nw is a Posn, and the width and the height are integers
a Combo: consisting of a top and a bottom, where the top and the bottom are each a Shape
Note: For clarity, we start the names of abstract classes with the letter A.
/*
+-------------+
| AComboShape |<-------------------------+
+-------------+ |
+-------------+ |
/ \ |
--- |
| |
----------------------------------------- |
| | | |
+-------------+ +------------+ +--------------------+ |
| Circle | | Rect | | Combo | |
+-------------+ +------------+ +--------------------+ |
| Posn center | | Posn nw | | AComboShape top |----+
| int radius | | int width | | AComboShape bottom |----+
+-------------+ | int height | +--------------------+
+------------+
*/
// to represent a combination of shapes
abstract class AComboShape {
}
// to represent a circle on the canvas
class Circle extends AComboShape {
Posn center;
int radius;
Circle(Posn center, int radius) {
this.center = center;
this.radius = radius; }
}
// to represent a rectangle on a canvas
class Rect extends AComboShape {
Posn nw;
int width;
int height;
Rect(Posn nw, int width, int height) {
this.nw = nw;
this.width = width;
this.height = height; }
}
// to represent a combination of two shapes
class Combo extends AComboShape {
AComboShape top;
AComboShape bottom;
Combo(AComboShape top, AComboShape bottom) {
this.top = top;
this.bottom = bottom;
}
}
// Intreractions:
Examples of the use of the constructors for the subclasses of AComboShape
Circle c1 = new Circle(new Posn(10, 20), 15);
Circle c2 = new Circle(new Posn(40, 20), 10);
Rect r1 = new Rect(new Posn(10, 20), 20, 40);
Rect r2 = new Rect(new Posn(40, 20), 50, 10);
Combo combo1 = new Combo(c1, r1);
Combo combo2 = new Combo(r2, combo1);
Combo combo3 = new Combo(combo2, c2);
// Challenge: paint this shape
// assume the colors are c1: red, c2: blue, r1: green, r2: yellow