Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3ad429b

Browse files
committed
REFACTOR: Updated output, comments, and moved name to Animal base class
* Added name to constructor of fish* Moved name to animal class so that fish can have names too* Added comments to clarify when reviewing the example* Updated strings to improve console output
1 parentf38d3bb commit3ad429b

File tree

7 files changed

+53
-44
lines changed

7 files changed

+53
-44
lines changed

‎src/Zoo.java‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ public class Zoo
1515
publicZoo()
1616
{
1717
penguins =newPenguin[NUMBER_OF_PENGUINS];
18-
penguins[0] =newPenguin("Waddles",
19-
10,
20-
150,
21-
"green",
22-
"blue",
23-
12.0f,
24-
11.0f,
25-
6.0f );
26-
27-
penguins[1] =newPenguin("Fluffy",
28-
2,
29-
2000,
30-
"black",
31-
"black",
32-
24.0f,
33-
24.0f,
34-
10.0f );
18+
penguins[0] =newPenguin("Waddles",// Name
19+
10,// Age
20+
150,// Weight
21+
"blue",// Right eye color
22+
"green",// Left eye color
23+
11.0f,// Right foot length
24+
12.0f,// Left foot length
25+
6.0f );// Beak length
26+
27+
penguins[1] =newPenguin("Fluffy",// Name
28+
2,2000,// Age & weight
29+
"black","black",// Eyes
30+
24.0f,24.0f,// Feet
31+
10.0f );// Beak
3532
}
3633

3734
publicstaticvoidmain(String[]argv )
@@ -40,13 +37,22 @@ public static void main( String[] argv )
4037
Zoozoo =newZoo();
4138

4239
Penguin[]penguins =zoo.getPenguins();
43-
System.out.println("The zoo has " +penguins.length +" penguins" );
40+
System.out.println();
41+
System.out.println("The zoo has " +penguins.length +" penguins:" );
4442
for (Penguinpenguin :penguins )
4543
{
4644
System.out.println("\t" +penguin.toString() );
45+
}
46+
47+
System.out.println();
48+
System.out.println("Penguin actions:" );
49+
for (Penguinpenguin :penguins )
50+
{
4751
penguin.waddle();
4852
penguin.swim();
49-
penguin.eat(newFish() );
53+
penguin.eat(newFish("Nemo" ) );
54+
System.out.println("\t" +penguin.toString() );
55+
System.out.println();
5056
}
5157
}
5258

‎src/animals/Animal.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
publicabstractclassAnimal
99
{
10+
Stringname;
1011
intage =0;
1112
intweight =0;
1213
}

‎src/animals/Fish.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88
publicclassFishextendsAnimal
99
{
10-
publicFish()
10+
publicFish(Stringname)
1111
{
12-
System.out.println("Creating a fish" );
12+
System.out.println("\t--Creating a fish with name " +name );
13+
this.name =name;
1314
}
1415
}

‎src/animals/Penguin.java‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
*/
1212
publicclassPenguinextendsAnimal
1313
{
14-
Stringname;
15-
1614
EyeleftEye;
1715
EyerightEye;
1816
FootrightFoot;
@@ -24,60 +22,63 @@ public class Penguin extends Animal
2422
publicPenguin(Stringname,
2523
intage,
2624
intweight,
27-
StringleftEyeColor,
2825
StringrightEyeColor,
29-
floatleftFootLength,
26+
StringleftEyeColor,
3027
floatrightFootLength,
28+
floatleftFootLength,
3129
floatbeakLength )
3230
{
33-
System.out.println("Creating a penguin" );
31+
System.out.println("\nCreating a penguin!" );
3432

3533
// Store values
36-
this.name =name;
34+
this.name =name;
3735
this.age =age;
3836
this.weight =weight;
3937

4038
isHungry =true;
4139

4240
// Construct new objects from parameters
43-
leftEye =newEye(leftEyeColor );
44-
rightEye =newEye(rightEyeColor );
45-
rightFoot =newFoot(leftFootLength );
46-
leftFoot =newFoot(rightFootLength );
47-
beak =newBeak(beakLength );
41+
leftEye=newEye(leftEyeColor );
42+
rightEye=newEye(rightEyeColor );
43+
rightFoot =newFoot(leftFootLength);
44+
leftFoot=newFoot(rightFootLength );
45+
beak=newBeak(beakLength );
4846
}
4947

5048
publicvoidwaddle()
5149
{
52-
System.out.println("Waddling" );
50+
System.out.println("\t" +name +" is waddling." );
5351
leftFoot.moveForward();
5452
rightFoot.moveForward();
5553
}
5654

5755
publicvoidswim()
5856
{
59-
System.out.println("Swimming" );
57+
System.out.println("\t" +name +" is swimming." );
58+
6059
rightFoot.flap();
6160
leftFoot.flap();
6261
}
6362

6463
publicvoideat(Fishfish )
6564
{
66-
System.out.println("Eating" );
65+
System.out.println("\t" +name +" is eating fish " +fish.name );
6766
isHungry =false;
6867
}
6968

7069
@Override
7170
publicStringtoString()
7271
{
7372
return"Penguin { " +
74-
"name = " +name +
75-
", left eye " +leftEye +
76-
", right eye " +rightEye +
77-
", left foot " +leftFoot +
73+
"name = " +name +
74+
", age = " +age +
75+
", weight = " +weight +
76+
", right eye " +rightEye +
77+
", left eye " +leftEye +
7878
", right foot " +rightFoot +
79-
", Beak " +beak +
80-
", isHungry = " +isHungry +
79+
", left foot " +leftFoot +
80+
", Beak " +beak +
81+
", isHungry = " +isHungry +
8182
" }";
8283
}
8384
}

‎src/animals/parts/Beak.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Beak
1111

1212
publicBeak(floatlength )
1313
{
14-
System.out.println("Creating a beak" );
14+
System.out.println("\t--Creating a beak with length " +length );
1515

1616
this.length =length;
1717
}

‎src/animals/parts/Eye.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Eye
1111

1212
publicEye(Stringcolor )
1313
{
14-
System.out.println("Creating an eye" );
14+
System.out.println("\t--Creating an eye with color " +color );
1515

1616
this.color =color;
1717
}

‎src/animals/parts/Foot.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Foot
1212

1313
publicFoot(floatlength )
1414
{
15-
System.out.println("Creating a foot" );
15+
System.out.println("\t--Creating a foot with length " +length );
1616
this.length =length;
1717
location =0;
1818
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp