forked from nus-cs2113-AY2223S2/Circus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class Animal { | ||
|
||
public String speak() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Bird extends Animal { | ||
public void fly() { | ||
System.out.println("Whee ..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
public class Circus { | ||
private static Animal[] animals = { | ||
new Duck(), | ||
new Parrot() | ||
}; | ||
private static Equipment[] equipments = { | ||
new Ladder(50), | ||
new Cannon(5), | ||
new Cannon(100) | ||
}; | ||
|
||
private static void makeAnimalsTalk() { | ||
for (Animal a : animals) { | ||
System.out.println(a); | ||
System.out.println(a.speak()); | ||
} | ||
} | ||
|
||
private static int calculateValue(Equipment[] equipments) { | ||
int total = 0; | ||
for (Equipment e : equipments) { | ||
if (e.getValue() <= 5) { | ||
System.out.println("Ignoring low value item: " + e.getValue()); | ||
} else { | ||
total += e.getValue(); | ||
System.out.println("Adding item value: " + e.getValue()); | ||
// some | ||
// more | ||
// code | ||
// here ... | ||
} | ||
} | ||
return total; | ||
} | ||
|
||
public static void main(String[] args) { | ||
makeAnimalsTalk(); | ||
System.out.println("Total value of equipments " + calculateValue(equipments)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Duck extends Bird { | ||
@Override | ||
public String speak() { | ||
return "Quack Quack"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "I'm a Duck"; | ||
} | ||
|
||
public void swim() { | ||
System.out.println("I'm swimming..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class Parrot extends Bird { | ||
@Override | ||
public String speak() { | ||
return "Polly wants a cracker"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "I'm a parrot"; | ||
} | ||
} |