File tree 5 files changed +81
-11
lines changed
07-inheritance-in-java/inheritance-in-java/src
5 files changed +81
-11
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,12 +15,15 @@ public class Main {
15
15
/**
16
16
* List of animals in the zoo.
17
17
*/
18
+
19
+
20
+
18
21
private static List <Animal > animals = List .of (
19
22
// TODO: Uncomment these two lines after implementing the Parrot class
20
- /*
23
+
21
24
new Parrot ("Polly" ),
22
- new Parrot("Loki"),
23
- */
25
+ new Parrot ("Loki" ));
26
+
24
27
// TODO: Uncomment these two lines after implementing the Lion class
25
28
/*
26
29
new Lion("Simba"),
@@ -31,7 +34,7 @@ public class Main {
31
34
new Elephant("Dumbo"),
32
35
new Elephant("Jumbo")
33
36
*/
34
- );
37
+
35
38
36
39
/**
37
40
* Main method to manage the zoo.
@@ -73,4 +76,5 @@ public static void main(String[] args) {
73
76
}
74
77
}
75
78
}
76
- }
79
+ }
80
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
4
4
public class ExerciseTests {
5
5
// TODO: Uncomment the following tests after you've implemented the Lion class
6
- /*
6
+
7
7
@ Test
8
8
public void TestLionEat () {
9
9
org .example .Lion lion = new org .example .Lion ("Simba" );
@@ -21,10 +21,10 @@ public void TestLionSleep() {
21
21
org .example .Lion lion = new org .example .Lion ("Simba" );
22
22
assertEquals ("Simba is sleeping. Simba dreams of a delicious steak." , lion .sleep ());
23
23
}
24
- */
24
+
25
25
26
26
// TODO: Uncomment the following tests after you've implemented the Elephant class
27
- /*
27
+
28
28
@ Test
29
29
public void TestElephantEat () {
30
30
org .example .Elephant elephant = new org .example .Elephant ("Dumbo" );
@@ -42,10 +42,10 @@ public void TestElephantSleep() {
42
42
org .example .Elephant elephant = new org .example .Elephant ("Dumbo" );
43
43
assertEquals ("Dumbo is sleeping." , elephant .sleep ());
44
44
}
45
- */
45
+
46
46
47
47
// TODO: Uncomment the following tests after you've implemented the Parrot class
48
- /*
48
+
49
49
@ Test
50
50
public void TestParrotEat () {
51
51
org .example .Parrot parrot = new org .example .Parrot ("Polly" );
@@ -69,5 +69,5 @@ public void TestParrotFly() {
69
69
org .example .Parrot parrot = new org .example .Parrot ("Polly" );
70
70
assertEquals ("Polly is flying around the zoo." , parrot .fly ());
71
71
}
72
- */
72
+
73
73
}
You can’t perform that action at this time.
0 commit comments