Skip to content

Commit ed777e9

Browse files
committed
inheritance completed
1 parent 98d7be8 commit ed777e9

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example;
2+
3+
public class Elephant extends Animal {
4+
public Elephant(String name) {
5+
super(name);
6+
}
7+
8+
@Override
9+
public String eat() {
10+
return name + " is eating.";
11+
}
12+
13+
@Override
14+
public String sleep() {
15+
return name + " is sleeping.";
16+
}
17+
@Override
18+
public String speak() {
19+
return name + " makes a loud trumpet sound.";
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example;
2+
3+
public class Lion extends Animal {
4+
public Lion(String name) {
5+
super(name);
6+
}
7+
8+
@Override
9+
public String eat() {
10+
return name + " is eating.";
11+
}
12+
13+
@Override
14+
public String sleep() {
15+
return name + " is sleeping. " + name + " dreams of a delicious steak.";
16+
}
17+
@Override
18+
public String speak() {
19+
return name + " lets out a loud ROAR!";
20+
}
21+
}

07-inheritance-in-java/inheritance-in-java/src/main/java/org/example/Main.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ public class Main {
1515
/**
1616
* List of animals in the zoo.
1717
*/
18+
19+
20+
1821
private static List<Animal> animals = List.of(
1922
// TODO: Uncomment these two lines after implementing the Parrot class
20-
/*
23+
2124
new Parrot("Polly"),
22-
new Parrot("Loki"),
23-
*/
25+
new Parrot("Loki"));
26+
2427
// TODO: Uncomment these two lines after implementing the Lion class
2528
/*
2629
new Lion("Simba"),
@@ -31,7 +34,7 @@ public class Main {
3134
new Elephant("Dumbo"),
3235
new Elephant("Jumbo")
3336
*/
34-
);
37+
3538

3639
/**
3740
* Main method to manage the zoo.
@@ -73,4 +76,5 @@ public static void main(String[] args) {
7376
}
7477
}
7578
}
76-
}
79+
}
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example;
2+
3+
public class Parrot extends Animal {
4+
public Parrot(String name) {
5+
super(name);
6+
}
7+
8+
@Override
9+
public String eat() {
10+
return name + " is eating a cracker.";
11+
}
12+
13+
@Override
14+
public String sleep() {
15+
return name + " is sleeping.";
16+
}
17+
@Override
18+
public String speak() {
19+
return name + " says '" + name + " wants a cracker!'";
20+
}
21+
public String fly() {
22+
return name + " is flying around the zoo.";
23+
}
24+
}

07-inheritance-in-java/inheritance-in-java/src/test/java/ExerciseTests.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
public class ExerciseTests {
55
// TODO: Uncomment the following tests after you've implemented the Lion class
6-
/*
6+
77
@Test
88
public void TestLionEat() {
99
org.example.Lion lion = new org.example.Lion("Simba");
@@ -21,10 +21,10 @@ public void TestLionSleep() {
2121
org.example.Lion lion = new org.example.Lion("Simba");
2222
assertEquals("Simba is sleeping. Simba dreams of a delicious steak.", lion.sleep());
2323
}
24-
*/
24+
2525

2626
// TODO: Uncomment the following tests after you've implemented the Elephant class
27-
/*
27+
2828
@Test
2929
public void TestElephantEat() {
3030
org.example.Elephant elephant = new org.example.Elephant("Dumbo");
@@ -42,10 +42,10 @@ public void TestElephantSleep() {
4242
org.example.Elephant elephant = new org.example.Elephant("Dumbo");
4343
assertEquals("Dumbo is sleeping.", elephant.sleep());
4444
}
45-
*/
45+
4646

4747
// TODO: Uncomment the following tests after you've implemented the Parrot class
48-
/*
48+
4949
@Test
5050
public void TestParrotEat() {
5151
org.example.Parrot parrot = new org.example.Parrot("Polly");
@@ -69,5 +69,5 @@ public void TestParrotFly() {
6969
org.example.Parrot parrot = new org.example.Parrot("Polly");
7070
assertEquals("Polly is flying around the zoo.", parrot.fly());
7171
}
72-
*/
72+
7373
}

0 commit comments

Comments
 (0)