Repository navigation
Expand file tree
/
Copy pathtask_4.java
More file actions
246 lines (205 loc) · 5.89 KB
/
Copy pathtask_4.java
File metadata and controls
246 lines (205 loc) · 5.89 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
public class task_4 {
public static void main(String[] args) {
// Circle class test
Circle circle = new Circle(5.4, "black", true);
System.out.println(circle);
// Rectangle class test
Rectangle rectangle = new Rectangle(5.4 , 4.5);
System.out.println("Rectanle area: " + rectangle.get_area());
System.out.println(rectangle);
// Square class test
Square square = new Square(10, "white", false);
square.set_side(11);
System.out.println(square);
}
}
abstract class Shape {
private String color;
private boolean filled;
Shape() {
System.out.println("\n[+] Shape object was created");
}
Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
System.out.println("\n[+] Shape object was created");
}
// Color getter
String get_color() {
return this.color;
}
// Color setter
void set_color(String color) {
this.color = color;
System.out.println("Shape color setted as: " + this.color);
}
// Filled getter
boolean is_filled() {
return this.filled;
}
// Filled getter
void set_filled(boolean filled) {
this.filled = filled;
System.out.println("Shape filledness setted as: " + this.filled);
}
// Area getter
double get_area() {
return 0.0;
}
// Perimetr getter
double get_perimeter() {
return 0.0;
}
public String toString() {
return "Shape object: is filled: " + this.filled + ", color: " + this.color;
}
}
class Circle extends Shape {
protected double radius;
Circle() {
super.set_color("blue");
super.set_filled(false);
this.radius = 1;
System.out.println("[+] Circle object was created");
}
Circle(double radius) {
super.set_color("blue");
super.set_filled(false);
this.radius = radius;
System.out.println("[+] Circle object was created");
}
Circle(double radius, String color, boolean filled) {
super.set_color(color);
super.set_filled(filled);
this.radius = radius;
System.out.println("[+] Circle object was created");
}
// Radius getter
double get_radius() {
return this.radius;
}
// Radius setter
void set_radius(double radius) {
if (radius > 0) {
this.radius = radius;
System.out.println("Circle radius setted as: " + this.radius);
} else {
System.out.println("Circle radius must be > 0");
}
}
@Override
double get_area() {
return Math.PI * this.radius * this.radius;
}
@Override
double get_perimeter() {
return 2 * Math.PI * radius;
}
@Override
public String toString() {
System.out.println(super.toString());
return "Shape: circle, radius: " + this.radius;
}
}
class Rectangle extends Shape {
protected double width;
protected double length;
Rectangle() {
super.set_color("blue");
super.set_filled(false);
System.out.println("[+] Rectangle object was created");
}
Rectangle(double width, double length) {
super.set_color("blue");
super.set_filled(false);
this.width = width;
this.length = length;
System.out.println("[+] Rectangle object was created");
}
Rectangle(double width, double length, String color, boolean filled) {
super.set_color("blue");
super.set_filled(false);
this.width = width;
this.length = length;
System.out.println("[+] Rectangle object was created");
}
// Width getter
double get_width() {
return this.width;
}
// Width setter
void set_width(double width) {
if (width > 0) {
this.width = width;
System.out.println("Rectangle width setted as: " + this.width);
} else {
System.out.println("Rectangle width must be > 0");
}
}
// Length getter
double get_length() {
return this.length;
}
// Width setter
void set_length(double length) {
if (length > 0) {
this.length = length;
System.out.println("Rectangle length setted as: " + this.length);
} else {
System.out.println("Rectangle length must be > 0");
}
}
@Override
double get_area() {
return this.length * this.width;
}
@Override
double get_perimeter() {
return 2 * (this.length + this.width);
}
@Override
public String toString() {
System.out.println(super.toString());
return "Shape: rectangle, length: " + this.length + ", width: " + this.width;
}
}
class Square extends Rectangle{
Square() {
super.set_color("blue");
super.set_filled(false);
System.out.println("[+] Square object was created");
}
Square(double side) {
super.set_color("blue");
super.set_filled(false);
this.width = side;
this.length = side;
System.out.println("[+] Square object was created");
}
Square(double side, String color, boolean filled) {
super.set_color("blue");
super.set_filled(false);
this.width = side;
this.length = side;
System.out.println("[+] Square object was created");
}
// Side getter
double get_side() {
return this.width;
}
// Side setter
void set_side(double side) {
if (side > 0) {
this.width = side;
this.length = side;
System.out.println("Square side setted as: " + this.width);
} else {
System.out.println("Square side must be > 0");
}
}
@Override
public String toString() {
System.out.println(super.toString());
return "Shape: square, side: " + this.width;
}
}