Skip to content

Commit 59b33d8

Browse files
committed
REFACTOR: Moved animal construction to base class
* Added Animal constructor and moved age, weight, and name * Called super() in animal children * Updated logging statements
1 parent 3ad429b commit 59b33d8

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/Zoo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Zoo
1515
public Zoo()
1616
{
1717
penguins = new Penguin[NUMBER_OF_PENGUINS];
18+
System.out.println( "\nCreating a penguin!" );
1819
penguins[0] = new Penguin( "Waddles", // Name
1920
10, // Age
2021
150, // Weight
@@ -24,6 +25,7 @@ public Zoo()
2425
12.0f, // Left foot length
2526
6.0f ); // Beak length
2627

28+
System.out.println( "\nCreating a penguin!" );
2729
penguins[1] = new Penguin( "Fluffy", // Name
2830
2, 2000, // Age & weight
2931
"black", "black", // Eyes
@@ -50,7 +52,7 @@ public static void main( String[] argv )
5052
{
5153
penguin.waddle();
5254
penguin.swim();
53-
penguin.eat( new Fish( "Nemo" ) );
55+
penguin.eat( new Fish( "Nemo", 0, 25 ) );
5456
System.out.println( "\t" + penguin.toString() );
5557
System.out.println();
5658
}

src/animals/Animal.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
*/
88
public abstract class Animal
99
{
10-
String name;
11-
int age = 0;
12-
int weight = 0;
10+
String name = "";
11+
int age = 0;
12+
int weight = 0;
13+
14+
public Animal( String name, int age, int weight )
15+
{
16+
System.out.println( "\tAnimal()" );
17+
System.out.println( "\t--Creating an animal named " + name + " that is " + age + " years old and weighs " + weight + " pounds." );
18+
19+
this.name = name;
20+
this.age = age;
21+
this.weight = weight;
22+
}
1323
}

src/animals/Fish.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88
public class Fish extends Animal
99
{
10-
public Fish( String name )
10+
public Fish( String name, int age, int weight )
1111
{
12+
super( name, age, weight );
1213
System.out.println( "\t--Creating a fish with name " + name );
1314
this.name = name;
1415
}

src/animals/Penguin.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ public Penguin( String name,
2828
float leftFootLength,
2929
float beakLength )
3030
{
31-
System.out.println( "\nCreating a penguin!" );
32-
33-
// Store values
34-
this.name = name;
35-
this.age = age;
36-
this.weight = weight;
31+
super( name, age, weight );
32+
System.out.println( "\tPenguin()" );
3733

3834
isHungry = true;
3935

0 commit comments

Comments
 (0)