-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_5.java
More file actions
207 lines (173 loc) · 5.15 KB
/
task_5.java
File metadata and controls
207 lines (173 loc) · 5.15 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
public class task_5 {
public static void main(String[] args) {
MovableCircle movable_circle = new MovableCircle(10, 0, 0, 1, 1);
System.out.println(movable_circle);
movable_circle.moveUp();
movable_circle.moveUp();
movable_circle.moveRight();
System.out.println(movable_circle);
movable_circle.moveDown();
movable_circle.moveDown();
movable_circle.moveDown();
movable_circle.moveDown();
System.out.println(movable_circle);
}
}
abstract class Shape {
private String color;
private boolean filled;
Shape() {
System.out.println("\n[+] Shape abstract 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;
}
}
interface Movable {
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
}
class MovablePoint implements Movable {
private int x;
private int y;
private int xSpeed;
private int ySpeed;
MovablePoint(int x, int y, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
System.out.println("\n[+] MovablePoint object was created");
}
public String toString() {
return "MovablePoint object - x: " + this.x + ", y: " + this.y + ", xSpeed: " + this.xSpeed + ", ySpeed: " + this.ySpeed;
}
public void moveUp() {
this.y += this.ySpeed;
System.out.println("MovablePoint object moved up by " + this.ySpeed);
}
public void moveDown() {
this.y -= this.ySpeed;
System.out.println("MovablePoint object moved down by " + this.ySpeed);
}
public void moveLeft() {
this.x -= this.xSpeed;
System.out.println("MovablePoint object moved left by " + this.xSpeed);
}
public void moveRight() {
this.x += this.xSpeed;
System.out.println("MovablePoint object moved right by " + this.xSpeed);
}
}
class MovableCircle implements Movable {
private int radius;
private MovablePoint center;
MovableCircle(int radius, int x, int y, int xSpeed, int ySpeed) {
this.center = new MovablePoint(x, y, xSpeed, ySpeed);
if (radius > 0) {
this.radius = radius;
} else {
this.radius = 1;
}
System.out.println("[+] MovableCircle object was created");
}
public String toString() {
return "MovableCircle object - radius: " + this.radius + ", center: " + center;
}
public void moveUp() {
this.center.moveUp();
System.out.println("MovableCircle object moved up");
}
public void moveDown() {
this.center.moveDown();
System.out.println("MovableCircle object moved down");
}
public void moveLeft() {
this.center.moveLeft();
System.out.println("MovableCircle object moved left");
}
public void moveRight() {
this.center.moveRight();
System.out.println("MovableCircle object moved right");
}
}