Skip to content

Commit

Permalink
Add names to animals
Browse files Browse the repository at this point in the history
  • Loading branch information
okkhoy committed Feb 10, 2023
1 parent e8574fd commit 36ba3f1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/circus/Circus.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public class Circus {
private static Animal[] animals = {
new Duck(),
new Parrot()
new Duck("Drake"),
new Parrot("Polly")
};
private static Equipment[] equipments = {
new Ladder(50),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/circus/Trainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Trainer {
public static void main(String[] args) {
Duck d = new Duck();
Duck d = new Duck("Donald");
getToSpeak(d);

Bird b = (Bird) d; // upcasting
Expand All @@ -19,7 +19,7 @@ public static void main(String[] args) {
Duck d2 = (Duck) a; // downcasting
getToSpeak(d2);

train(new Duck());
train(new Duck("Daisy"));
// train(new animal.Parrot());
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/circus/animal/Animal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

public abstract class Animal implements Asset {

public String name;
public abstract String speak();
}
18 changes: 11 additions & 7 deletions src/main/java/circus/animal/Duck.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
public class Duck extends Bird {
@Override
public String speak() {
return "Quack Quack";
}

@Override
public int getValue() {
return 10;
return toString() + " Quack Quack";
}

@Override
public String toString() {
return "I'm a Duck";
return "I'm " + name + ". I am a Duck!";
}

public void swim() {
System.out.println("I'm swimming...");
}

@Override
public int getValue() {
return 10;
}

public Duck(String name) {
this.name = name;
}
}
12 changes: 8 additions & 4 deletions src/main/java/circus/animal/Parrot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
public class Parrot extends Bird {
@Override
public String speak() {
return "Polly wants a cracker";
return name + " wants a cracker";
}

@Override
public String toString() {
return "My name is " + name + ". I am a Parrot!";
}

@Override
public int getValue() {
return 20;
}

@Override
public String toString() {
return "I'm a parrot";
public Parrot(String name) {
this.name = name;
}
}

0 comments on commit 36ba3f1

Please sign in to comment.