|
1 | | -import java.awt.Point; |
2 | | -import java.awt.Rectangle; |
3 | | - |
4 | | -/** |
5 | | - * Demonstates use of Point and Rectangle classes. |
6 | | - */ |
7 | | -public class PointRect { |
8 | | - |
9 | | - public static void main(String[] args) { |
10 | | - Point blank; |
11 | | - blank = new Point(3, 4); |
12 | | - System.out.println(blank); |
13 | | - |
14 | | - int x = blank.x; |
15 | | - System.out.println(blank.x + ", " + blank.y); |
16 | | - int sum = blank.x * blank.x + blank.y * blank.y; |
17 | | - |
18 | | - Rectangle box = new Rectangle(0, 0, 100, 200); |
19 | | - moveRect(box, 50, 100); |
20 | | - System.out.println(box); |
21 | | - box.translate(50, 100); |
22 | | - |
23 | | - Rectangle box1 = new Rectangle(0, 0, 100, 200); |
24 | | - Rectangle box2 = box1; |
25 | | - |
26 | | - System.out.println(box2.width); |
27 | | - box1.grow(50, 50); |
28 | | - System.out.println(box2.width); |
29 | | - } |
30 | | - |
31 | | - /** |
32 | | - * Prints the attributes of a Point object. |
33 | | - */ |
34 | | - public static void printPoint(Point p) { |
35 | | - System.out.println("(" + p.x + ", " + p.y + ")"); |
36 | | - } |
37 | | - |
38 | | - /** |
39 | | - * Computes the distance between two points. |
40 | | - */ |
41 | | - public static double distance(Point p1, Point p2) { |
42 | | - int dx = p2.x - p1.x; |
43 | | - int dy = p2.y - p1.y; |
44 | | - return Math.sqrt(dx * dx + dy * dy); |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Finds the center of a Rectangle and returns a new Point. |
49 | | - */ |
50 | | - public static Point findCenter(Rectangle box) { |
51 | | - int x = box.x + box.width / 2; |
52 | | - int y = box.y + box.height / 2; |
53 | | - return new Point(x, y); |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Moves a Rectangle by modifying the x and y attributes. |
58 | | - */ |
59 | | - public static void moveRect(Rectangle box, int dx, int dy) { |
60 | | - box.x = box.x + dx; |
61 | | - box.y = box.y + dy; |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * Exercise on returning objects. |
66 | | - */ |
67 | | - public static void exercise2() { |
68 | | - Point blank = new Point(5, 8); |
69 | | - |
70 | | - Rectangle rect = new Rectangle(0, 2, 4, 4); |
71 | | - Point center = findCenter(rect); |
72 | | - |
73 | | - double dist = distance(center, blank); |
74 | | - System.out.println(dist); |
75 | | - } |
76 | | - |
77 | | - /** |
78 | | - * Exercise on aliasing. |
79 | | - */ |
80 | | - public static void exercise3() { |
81 | | - Rectangle box1 = new Rectangle(2, 4, 7, 9); |
82 | | - Point p1 = findCenter(box1); |
83 | | - printPoint(p1); |
84 | | - |
85 | | - box1.grow(1, 1); |
86 | | - Point p2 = findCenter(box1); |
87 | | - printPoint(p2); |
88 | | - } |
89 | | - |
90 | | -} |
| 1 | +import java.awt.Point; |
| 2 | +import java.awt.Rectangle; |
| 3 | + |
| 4 | +/** |
| 5 | + * Demonstates use of Point and Rectangle classes. |
| 6 | + */ |
| 7 | +public class PointRect { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + Point blank; |
| 11 | + blank = new Point(3, 4); |
| 12 | + System.out.println(blank); |
| 13 | + |
| 14 | + int x = blank.x; |
| 15 | + System.out.println(blank.x + ", " + blank.y); |
| 16 | + int sum = blank.x * blank.x + blank.y * blank.y; |
| 17 | + |
| 18 | + Rectangle box = new Rectangle(0, 0, 100, 200); |
| 19 | + moveRect(box, 50, 100); |
| 20 | + System.out.println(box); |
| 21 | + box.translate(50, 100); |
| 22 | + |
| 23 | + Rectangle box1 = new Rectangle(0, 0, 100, 200); |
| 24 | + Rectangle box2 = box1; |
| 25 | + |
| 26 | + System.out.println(box2.width); |
| 27 | + box1.grow(50, 50); |
| 28 | + System.out.println(box2.width); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Prints the attributes of a Point object. |
| 33 | + */ |
| 34 | + public static void printPoint(Point p) { |
| 35 | + System.out.println("(" + p.x + ", " + p.y + ")"); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Computes the distance between two points. |
| 40 | + */ |
| 41 | + public static double distance(Point p1, Point p2) { |
| 42 | + int dx = p2.x - p1.x; |
| 43 | + int dy = p2.y - p1.y; |
| 44 | + return Math.sqrt(dx * dx + dy * dy); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Finds the center of a Rectangle and returns a new Point. |
| 49 | + */ |
| 50 | + public static Point findCenter(Rectangle box) { |
| 51 | + int x = box.x + box.width / 2; |
| 52 | + int y = box.y + box.height / 2; |
| 53 | + return new Point(x, y); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Moves a Rectangle by modifying the x and y attributes. |
| 58 | + */ |
| 59 | + public static void moveRect(Rectangle box, int dx, int dy) { |
| 60 | + box.x = box.x + dx; |
| 61 | + box.y = box.y + dy; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Exercise on returning objects. |
| 66 | + */ |
| 67 | + public static void exercise2() { |
| 68 | + Point blank = new Point(5, 8); |
| 69 | + |
| 70 | + Rectangle rect = new Rectangle(0, 2, 4, 4); |
| 71 | + Point center = findCenter(rect); |
| 72 | + |
| 73 | + double dist = distance(center, blank); |
| 74 | + System.out.println(dist); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Exercise on aliasing. |
| 79 | + */ |
| 80 | + public static void exercise3() { |
| 81 | + Rectangle box1 = new Rectangle(2, 4, 7, 9); |
| 82 | + Point p1 = findCenter(box1); |
| 83 | + printPoint(p1); |
| 84 | + |
| 85 | + box1.grow(1, 1); |
| 86 | + Point p2 = findCenter(box1); |
| 87 | + printPoint(p2); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments