// A class diagram for rectangles /* +----------------------------+ | Rect | +----------------------------+ | int height | | int width | +----------------------------+ */ // A class representing rectangles class Rect { int height; //height in inches int width; //width in inches Rect(int height, int width) { this.height = height; this.width = width; } } // Class diagram for Loc and the new Rect /* +-----------------------+ | Rect | +-----------------------+ | Loc loc |---+ | int height | | | int width | | +-----------------------+ | | +-----------------------+ | | Loc |<--+ +-----------------------+ | int vert | | int horiz | +-----------------------+ */ // A class representing locations class Loc { int vert; //pixels down from the top-left corner of the screen int horiz; //pixels left from the top-left corern of the screen Loc(int vert, int horiz) { this.vert = vert; this.horiz = horiz; } } // Another class representing rectangles, this time with a location class Rect2 { Loc loc; //location of upper-left corner int height; //height in pixels int width; //width in pixels Rect2(Loc loc, int height, int width) { this.loc = loc; this.height = height; this.width = width; } } // A class for creating example instances class Examples { Examples() { } Rect rectSquare = new Rect(3, 3); Rect rectWide = new Rect(4, 10); Rect rectTall = new Rect(12, 5); // Examples with containment Loc loc1 = new Loc(10, 30); Loc loc2 = new Loc(42, 42); Rect2 rectSquare2 = new Rect2(this.loc1, 3, 3); Rect2 rectWide2 = new Rect2(this.loc2, 4, 10); Rect2 rectTall2 = new Rect2(new Loc(123, 55), 12, 5); }