Repository navigation
Expand file tree
/
Copy pathChapter04.java
More file actions
175 lines (142 loc) · 4.72 KB
/
Copy pathChapter04.java
File metadata and controls
175 lines (142 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import ch02.ex17.Queue;
import ch04.ex06.DiscountedItem;
import ch04.ex06.Item;
import ch04.ex07.Color;
import ch04.ex08.ClassToString;
import org.junit.Test;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Chapter04 {
@Test
public void ex01() {
// ch04.ex01.Point
// ch04.ex01.LabeledPoint
}
@Test
public void ex0201() {
ch04.ex02.Point p1 = new ch04.ex02.Point(1, 1);
ch04.ex02.Point p2 = new ch04.ex02.Point(1, 1);
System.out.println("p1.toString() = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("p2.toString() = " + p2);
System.out.println("p2.hashCode() = " + p2.hashCode());
System.out.println("p1 == p2 ? " + (p1 == p2));
System.out.println("p1.equals(p2) ? " + p1.equals(p2));
System.out.println("p2.equals(p1) ? " + p2.equals(p1));
}
@Test
public void ex0202() {
ch04.ex02.LabeledPoint p1 = new ch04.ex02.LabeledPoint("a", 1, 1);
ch04.ex02.LabeledPoint p2 = new ch04.ex02.LabeledPoint("a", 1, 1);
System.out.println("p1.toString() = " + p1);
System.out.println("p1.hashCode() = " + p1.hashCode());
System.out.println("p2.toString() = " + p2);
System.out.println("p2.hashCode() = " + p2.hashCode());
System.out.println("p1 == p2 ? " + (p1 == p2));
System.out.println("p1.equals(p2) ? " + p1.equals(p2));
System.out.println("p2.equals(p1) ? " + p2.equals(p1));
}
@Test
public void ex03() {
// ch04.ex03.Point
// ch04.ex03.LabeledPoint.toString()
}
@Test
public void ex04() {
// circle
ch04.ex04.Point p = new ch04.ex04.Point(10, 10);
ch04.ex04.Circle c = new ch04.ex04.Circle(p, 20);
System.out.println("center = " + c.getCenter());
// rectangle
ch04.ex04.Rectangle r = new ch04.ex04.Rectangle(new ch04.ex04.Point(10, 10), 100, 200);
System.out.println("rectangle center = " + r.getCenter());
// line
ch04.ex04.Point from = new ch04.ex04.Point(30, 20);
ch04.ex04.Point to = new ch04.ex04.Point(10, 10);
ch04.ex04.Line line = new ch04.ex04.Line(from, to);
System.out.println("line center = " + line.getCenter());
}
@Test
public void ex05() {
// circle
ch04.ex05.Circle c1 = new ch04.ex05.Circle(new ch04.ex05.Point(10, 10), 20);
ch04.ex05.Circle c2 = c1.clone();
System.out.println(c1);
System.out.println(c2);
// rectangle
ch04.ex05.Rectangle r1 = new ch04.ex05.Rectangle(new ch04.ex05.Point(10, 10), 100, 200);
ch04.ex05.Rectangle r2 = r1.clone();
System.out.println(r1);
System.out.println(r2);
// line
ch04.ex05.Line l1 = new ch04.ex05.Line(new ch04.ex05.Point(30, 20), new ch04.ex05.Point(10, 10));
ch04.ex05.Line l2 = l1.clone();
System.out.println(l1);
System.out.println(l2);
}
@Test
public void ex06() {
DiscountedItem x = new DiscountedItem("Item", 1000, 200);
Item y = new Item("Item", 1000);
DiscountedItem z = new DiscountedItem("Item", 1000, 100);
System.out.println(x.equals(y)); // true
System.out.println(y.equals(z)); // true
System.out.println(x.equals(z)); // false
}
@Test
public void ex07() {
Color color = Color.getBlue();
System.out.println(color);
}
@Test
public void ex08() {
String[] strings = new String[]{"a", "b", "c"};
System.out.println("* array type");
ClassToString.toString(strings.getClass());
System.out.println();
System.out.println("* generic type");
ClassToString.toString(Override.class);
System.out.println();
System.out.println("* inner class");
ClassToString.toString(Queue.Iterator.class);
System.out.println();
System.out.println("* primitive ");
ClassToString.toString(double.class);
}
@Test
public void ex09() {
ch04.ex09.Employee e = new ch04.ex09.Employee("antop", 5000);
System.out.println(e.toString());
}
@Test
public void ex11() throws Exception {
Class<?> clazz = Class.forName("java.lang.System");
// out field
Field field = clazz.getDeclaredField("out");
Object out = field.get(null);
// out.println method
Method method = PrintStream.class.getDeclaredMethod("println", new Class[]{String.class});
method.invoke(out, "Hello World!");
}
@Test
public void ex1201() {
ch04.ex05.Point p1 = new ch04.ex05.Point(10, 20);
ch04.ex05.Point p2 = new ch04.ex05.Point(25, 30);
ch04.ex05.Line line = new ch04.ex05.Line(p1, p2);
for (int i = 0; i < 10000; i++) {
line.moveBy(10, 10);
}
}
@Test
public void ex1202() throws Exception {
ch04.ex05.Point p1 = new ch04.ex05.Point(10, 20);
ch04.ex05.Point p2 = new ch04.ex05.Point(25, 30);
ch04.ex05.Line line = new ch04.ex05.Line(p1, p2);
for (int i = 0; i < 10000; i++) {
Method moveBy = ch04.ex05.Line.class.getDeclaredMethod("moveBy", new Class[]{double.class, double.class});
moveBy.invoke(line, new double[]{10, 10});
}
}
}