forked from biblelamp/JavaExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW6Lesson.java
More file actions
65 lines (59 loc) · 1.73 KB
/
HW6Lesson.java
File metadata and controls
65 lines (59 loc) · 1.73 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
/**
* Java. Level 1. Lesson 6. Example of homework
*
* @author Sergey Iryupin
* @version dated Jul 30, 2018
* @link https://github.com/<your_nic> || null
*/
class HW6Lesson {
public static void main(String[] args) {
Animal[] animals = {new Cat(200, 2), new Dog(500, 0.5f, 10)};
for (Animal animal : animals)
System.out.println(
animal.getClass().getName() +
"\nrun: " + animal.run(200) +
"\njump: " + animal.jump(1.5f) +
"\nswim: " + animal.swim(5));
}
}
class Cat extends AnimalImpl {
Cat(int run_limit, float jump_limit, int swim_limit) {
super(run_limit, jump_limit, swim_limit);
}
Cat(int run_limit, float jump_limit) {
super(run_limit, jump_limit, -1);
}
@Override
public boolean swim(int distance) {
return false; // cats cannot swim (by the condition of task)
}
}
class Dog extends AnimalImpl {
Dog(int run_limit, float jump_limit, int swim_limit) {
super(run_limit, jump_limit, swim_limit);
}
}
interface Animal {
boolean run(int distance);
boolean jump(float height);
boolean swim(int distance);
}
abstract class AnimalImpl implements Animal {
protected int run_limit;
protected float jump_limit;
protected int swim_limit;
AnimalImpl(int run_limit, float jump_limit, int swim_limit) {
this.run_limit = run_limit;
this.jump_limit = jump_limit;
this.swim_limit = swim_limit;
}
public boolean run(int distance) {
return distance <= run_limit;
}
public boolean jump(float height) {
return height <= jump_limit;
}
public boolean swim(int distance) {
return distance <= swim_limit;
}
}