-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAnimals.java
More file actions
100 lines (90 loc) · 1.98 KB
/
TestAnimals.java
File metadata and controls
100 lines (90 loc) · 1.98 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
abstract class Animal{
protected int legs;
protected Animal(int legs) {
this.legs=legs;
}
public abstract void eat();
public void walk() {
System.out.println("This animal walks by "+legs+" legs");
}
}
class Spider extends Animal{
public Spider(){
super(8);
}
public void eat() {
System.out.println("Spider eats insects");
}
}
interface Pet{
public String getName();
public void setName(String name);
public void play();
}
class Cat extends Animal implements Pet{
private String name;
public Cat(String name) {
super(4);
this.name=name;
}
Cat(){
this("");
}
public void eat() {
System.out.println("Cat eat fishes");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public void play() {
System.out.println("The Cat is playing");
}
}
class Fish extends Animal implements Pet{
String name;
public Fish() {
super(0);
}
public void eat() {
System.out.println("Fish eats plants");
}
public void play() {
System.out.println("The Fish is playing");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
@Override
public void walk() {
System.out.println("Fish has no legs");
}
}
public class TestAnimals{
public static void main(String args []) {
Fish fish =new Fish();
Cat cat =new Cat("Fluffy");
Spider spider =new Spider();
System.out.println("FISH");
fish.setName("Mimi");
System.out.println("The fish's name is "+fish.getName());
fish.eat();
fish.walk();
fish.setName("Momo");
System.out.println("The fish's name is "+fish.getName());
System.out.println("\nCAT");
System.out.println("The cat's name is "+cat.getName());
cat.walk();
cat.eat();
cat.setName("Moose");
System.out.println("The cat's name is "+cat.getName());
System.out.println("\nSPIDER");
spider.eat();
spider.walk();
}
}