-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPoly.java
77 lines (56 loc) · 1.73 KB
/
TestPoly.java
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
public class TestPoly {
public static void main(String[] args) {
Object object_1 = new CircleFromSimpleGeometricObject(1);
Object object_2 = new RectangleFromSimpleGeometricObject(1 , 1);
displayObject(object_1);
displayObject(object_2);
}
public static void displayObject(Object object) {
if (object instanceof CircleFromSimpleGeometricObject)
System.out.println("The circle area is " + ((CircleFromSimpleGeometricObject) object).getArea());
else if (object instanceof RectangleFromSimpleGeometricObject)
System.out.println("The rectangle area is " + ((RectangleFromSimpleGeometricObject) object).getArea());
}
}
class CircleFromSimpleGeometricObject {
double radius = 0;
CircleFromSimpleGeometricObject() {
}
CircleFromSimpleGeometricObject(double radius) {
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * this.radius * this.radius;
}
}
class RectangleFromSimpleGeometricObject {
double x = 0;
double y = 0;
RectangleFromSimpleGeometricObject() {
}
RectangleFromSimpleGeometricObject(double x , double y) {
this.x = x;
this.y = y;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public double getArea() {
return this.y * this.x;
}
}