-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbsShapes.java
More file actions
146 lines (115 loc) · 3.09 KB
/
Copy pathAbsShapes.java
File metadata and controls
146 lines (115 loc) · 3.09 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
abstract class TwoDShape {
private double width;
private double height;
private String name;
TwoDShape() {
width = height = 0.0;
name = "none";
}
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
}
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
}
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
String getName() { return name; };
void showDim() {
System.out.println("Width & height - " + width + " & " + height);
}
abstract double area();
}
class Triangle extends TwoDShape {
private String style;
Triangle() {
super();
style = "none";
}
Triangle(String s, double w, double h) {
super(w, h, "Triangle");
style = s;
}
Triangle(double x) {
super(x, "Triangle");
style = "painted";
}
Triangle(Triangle ob) {
super(ob);
style = ob.style;
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println("Triangle " + style);
}
}
class Rectangle extends TwoDShape {
Rectangle() {
super();
}
Rectangle(double w, double h) {
super(w, h, "Rectangle");
}
Rectangle(double x) {
super(x, "Rectangle");
}
Rectangle(Rectangle ob) {
super(ob);
}
boolean isSquare(){
if(getWidth() == getHeight()) return true;
return false;
}
double area() {
return getWidth() * getHeight();
}
}
class Circle extends TwoDShape {
Circle() {
super();
}
Circle(double x) {
super(x, "Circle");
}
Circle(Circle ob) {
super(ob);
}
double area() {
return Math.PI * getWidth() * getWidth();
}
}
class AbsShapes {
public static void main(String args[]) {
Triangle triangle1 = new Triangle("outlinear", 8.0, 12.0);
TwoDShape shapes[] = new TwoDShape[6];
shapes[0] = triangle1;
shapes[1] = new Rectangle(10);
shapes[2] = new Rectangle(10, 4);
shapes[3] = new Triangle(7.0);
shapes[4] = triangle1;
shapes[5] = triangle1;
Circle circle1 = new Circle(5.0);
System.out.println("Object - " + circle1.getName());
System.out.println("Area - " + circle1.area());
System.out.println(triangle1.getClass());
System.out.println(triangle1.hashCode());
System.out.println(triangle1.toString());
for(int i = 0; i < shapes.length; i++) {
System.out.println("Object - " + shapes[i].getName());
System.out.println("Area - " + shapes[i].area());
System.out.println();
}
}
}