Java Reference
In-Depth Information
LISTING 5.6
The Full Text of Box2.java
7: int y2 = 0;
8:
9: Box2(int x1, int y1, int x2, int y2) {
10: this.x1 = x1;
11: this.y1 = y1;
12: this.x2 = x2;
13: this.y2 = y2;
14: }
15:
16: Box2(Point topLeft, Point bottomRight) {
17: x1 = topLeft.x;
18: y1 = topLeft.y;
19: x2 = bottomRight.x;
20: y2 = bottomRight.y;
21: }
22:
23: Box2(Point topLeft, int w, int h) {
24: x1 = topLeft.x;
25: y1 = topLeft.y;
26: x2 = (x1 + w);
27: y2 = (y1 + h);
28: }
29:
30: void printBox() {
31: System.out.print(“Box: <” + x1 + “, “ + y1);
32: System.out.println(“, “ + x2 + “, “ + y2 + “>”);
33: }
34:
35: public static void main(String[] arguments) {
36: Box2 rect;
37:
38: System.out.println(“Calling Box2 with coordinates “
39: + “(25,25) and (50,50):”);
40: rect = new Box2(25, 25, 50, 50);
41: rect.printBox();
42:
43: System.out.println(“\nCalling Box2 with points “
44: + “(10,10) and (20,20):”);
45: rect= new Box2(new Point(10, 10), new Point(20, 20));
46: rect.printBox();
47:
48: System.out.println(“\nCalling Box2 with 1 point “
49: + “(10,10), width 50 and height 50:”);
50: rect = new Box2(new Point(10, 10), 50, 50);
51: rect.printBox();
52:
53: }
54: }
5
This application produces the same output as the Box application in Listing 5.5.
 
Search WWH ::




Custom Search