2.6 Example: Definition of the class Rect
/*
+------------+ +-------+
| Rect | +-->| Posn |
+------------+ | +-------+
| Posn nw |--+ | int x |
| int width | | int y |
| int height | +-------+
+------------+
*/
// to represent a rectangle on canvas
class Rect {
Posn nw;
int width;
int height;
Rect(Posn nw, int width, int height) {
this.nw = nw;
this.width = width;
this.height = height;
}
}
/* Interactions:
Examples of the use of the constructor for the class Rect
Rect r1 = new Rect(new Posn(10, 20), 40, 30);
Rect r2 = new Rect(new Posn(20, 40), 10, 50);
Exampples of the use of the selectors for the class Rect
r1.nw -- expected: new Posn(10, 20)
r1.width -- expected: 40
r1.height -- expected: 30
r2.nw -- expected: new Posn(20, 40)
r2.width -- expected: 10
r2.height -- expected: 50
r1.nw.x -- expected 10
r1.nw.y -- expected 20
r2.nw.x -- expected 20
r2.nw.y -- expected 40
*/