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 Dot: consisting of a location, where the location is a Posn
/*
+--------+
| AShape |
+--------+
+--------+
/ \
---
|
---------------------------------------
| | |
+-------------+ +------------+ +---------------+
| Circle | | Rect | | Dot |
+-------------+ +------------+ +---------------+
| Posn center |-+ | Posn nw |-+ | Posn location |-+
| int radius | | | int width | | +---------------+ |
+-------------+ | | int height | | |
+-------+ | +------------+ | |
| Posn |<----+-----------------+--------------------+
+-------+
| int x |
| int y |
+-------+
*/
// to represent one point on a canvas
class Posn {
int x;
int y;
Posn(int x, int y) {
this.x = x;
this.y = y;
}
}
// to represent geometric shapes
abstract class AShape {
}
// to represent a circle
class Circle extends AShape {
Posn center;
int radius;
Circle(Posn center, int radius) {
this.center = center;
this.radius = radius;
}
}
// to represent a rectangle
class Rect extends AShape {
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 dot
class Dot extends AShape {
Posn location;
Dot(Posn location) {
this.location = location;
}
}
/* Interactions:
Examples of the use of the constructors for the classes representing shapes
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);
Dot d1 = new Dot(new Posn(10, 20));
Dot d2 = new Dot(new Posn(40, 20));
Examples of the use of field selectors for sub-classes of AShape
c1.center -- expected: new Posn(10, 20)
c1.center.x -- expected: 10
c1.center.y -- expected: 20
c1.radius -- expected: 15
c2.center -- expected: new Posn(40, 20)
c1.center.x -- expected: 40
c2.center.y -- expected: 20
c2.radius -- expected: 10
r1.nw -- expected: new Posn(10, 20)
r1.nw.x -- expected: 10
r1.nw.y -- expected: 20
r1.width -- expected: 20
r1.height -- expected: 40
r2.nw -- expected: new Posn(40, 20)
r2.nw.x -- expected: 40
r2.nw.y -- expected: 20
r2.width -- expected: 50
r2.height -- expected: 10
d1.location -- expected: new Posn(10, 20)
d1.location.x -- expected: 10
d1.location.y -- expected: 20
d2.location -- expected: new Posn(40, 20)
d2.location.x -- expected: 40
d2.location.y -- expected: 20
*/