-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.java
More file actions
180 lines (144 loc) · 4.2 KB
/
Animal.java
File metadata and controls
180 lines (144 loc) · 4.2 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
public abstract class Animal {
private String name;
private int age;
private String color;
public Animal() {
this.name = "Unnamed";
this.age = 0;
this.color = "colour";
}
public Animal(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public abstract void makeSound();
public abstract void move();
}
class Cat extends Animal {
private boolean hasClaws;
private static int catCount = 0;
public Cat() {
super();
this.hasClaws = true;
catCount++;
}
public Cat(String name, int age, String color, boolean hasClaws) {
super(name, age, color);
this.hasClaws = hasClaws;
catCount++;
}
public boolean isHasClaws() {
return hasClaws;
}
public void setHasClaws(boolean hasClaws) {
this.hasClaws = hasClaws;
}
public static int getCatCount() {
return catCount;
}
@Override
public void makeSound() {
System.out.println("Meow!");
}
@Override
public void move() {
System.out.println("Cat walks, runs, jumps.");
}
}class Parrot extends Animal {
private String favoritePhrase;
public Parrot() {
super();
this.favoritePhrase = "Hello!";
}
public Parrot(String name, int age, String color, String favoritePhrase) {
super(name, age, color);
this.favoritePhrase = favoritePhrase;
}
public String getFavoritePhrase() {
return favoritePhrase;
}
public void setFavoritePhrase(String favoritePhrase) {
this.favoritePhrase = favoritePhrase;
}
@Override
public void makeSound() {
System.out.println(favoritePhrase);
}
@Override
public void move() {
System.out.println("Parrot flies, jumps.");
}
}
class Fish extends Animal {
private String swimmingStyle;
public Fish() {
super();
this.swimmingStyle = "Calm";
}
public Fish(String name, int age, String color, String swimmingStyle) {
super(name, age, color);
this.swimmingStyle = swimmingStyle;
}
public String getSwimmingStyle() {
return swimmingStyle;
}
public void setSwimmingStyle(String swimmingStyle) {
this.swimmingStyle = swimmingStyle;
}
@Override
public void makeSound() {
System.out.println("Blub-blub!");
}
@Override
public void move() {
System.out.println("Fish swims: " + swimmingStyle);
}
}
class Main {
public static void main(String[] args) {
Cat cat1 = new Cat("Barsik", 5, "Gray", true);
Parrot parrot1 = new Parrot("Kesha", 2, "Green", "Cock-a-doodle-doo!");
Fish fish1 = new Fish("Nemo", 1, "Orange", "Fast");
System.out.println("Cat:");
System.out.println("Name: " + cat1.getName());
System.out.println("Age: " + cat1.getAge());
System.out.println("Color: " + cat1.getColor());
System.out.println("Has claws: " + cat1.isHasClaws());
cat1.makeSound();
cat1.move();
System.out.println("\nParrot:");
System.out.println("Name: " + parrot1.getName());
System.out.println("Age: " + parrot1.getAge());
System.out.println("Color: " + parrot1.getColor());
System.out.println("Favorite phrase: " + parrot1.getFavoritePhrase());
parrot1.makeSound();
parrot1.move();
System.out.println("\nFish:");
System.out.println("Name: " + fish1.getName());
System.out.println("Age: " + fish1.getAge());
System.out.println("Color: " + fish1.getColor());
System.out.println("Swimming style: " + fish1.getSwimmingStyle());
fish1.makeSound();
fish1.move();
System.out.println("\nCats created: " + Cat.getCatCount());
}
}