Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In lecture #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# The Circus Project
# The circus.Circus Project
6 changes: 0 additions & 6 deletions src/main/java/Animal.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/circus/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package circus;

public abstract class Animal implements Asset {
public abstract String speak();
}
5 changes: 5 additions & 0 deletions src/main/java/circus/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package circus;

public interface Asset {
int getValue();
}
4 changes: 3 additions & 1 deletion src/main/java/Bird.java → src/main/java/circus/Bird.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public class Bird extends Animal {
package circus;

public abstract class Bird extends Animal {
public void fly() {
System.out.println("Whee ...");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Cannon extends Equipment {

public Cannon(int purchasePrice) {
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/Circus.java → src/main/java/circus/Circus.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Circus {
private static Animal[] animals = {
new Duck(),
Expand All @@ -16,25 +18,28 @@ private static void makeAnimalsTalk() {
}
}

private static int calculateValue(Equipment[] equipments) {
private static int calculateValue(Asset[] assets) {
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 ...
for (Asset a : assets) {
if (a.getValue() <= 5) {
System.out.println("Ignoring low value item: " + a.getValue());
continue;
}

total += a.getValue();
System.out.println("Adding item value: " + a.getValue());
// some
// more
// code
// here ...
}

return total;
}
}

public static void main(String[] args) {
makeAnimalsTalk();
System.out.println("Total value of equipments " + calculateValue(equipments));
System.out.println("Total value of animals " + calculateValue(animals));
}
}
9 changes: 8 additions & 1 deletion src/main/java/Duck.java → src/main/java/circus/Duck.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Duck extends Bird {
@Override
public String speak() {
Expand All @@ -6,10 +8,15 @@ public String speak() {

@Override
public String toString() {
return "I'm a Duck";
return "I'm a circus.Duck";
}

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

@Override
public int getValue() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public abstract class Equipment {
package circus;

public abstract class Equipment implements Asset {
protected int purchasePrice;

public Equipment(int purchasePrice) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Ladder extends Equipment {

public Ladder(int purchasePrice) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Parrot extends Bird {
@Override
public String speak() {
Expand All @@ -8,4 +10,9 @@ public String speak() {
public String toString() {
return "I'm a parrot";
}

@Override
public int getValue() {
return 0;
}
}
12 changes: 7 additions & 5 deletions src/main/java/Trainer.java → src/main/java/circus/Trainer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package circus;

public class Trainer {
public static void main(String[] args) {
Duck d = new Duck();
Expand All @@ -7,13 +9,13 @@ public static void main(String[] args) {
System.out.println(a.speak());
Duck d2 = (Duck) a; // downcasting
train(new Duck());
// train(new Parrot());
Animal a2 = new Animal();
Bird b2 = new Bird();
// train(new circus.Parrot());
}

private static void train(Bird bird) {
Duck d = (Duck) bird;
d.swim();
if(bird instanceof Duck) {
Duck d = (Duck) bird;
d.swim();
}
}
}
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
I'm a Duck
I'm a circus.Duck
Quack Quack
I'm a parrot
Polly wants a cracker
Expand Down
4 changes: 2 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ REM delete output from previous run
del ACTUAL.TXT

REM compile the code into the bin folder
javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\*.java
javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\circus\*.java
IF ERRORLEVEL 1 (
echo ********** BUILD FAILURE **********
exit /b 1
)
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Circus > ACTUAL.TXT
java -classpath ..\bin circus.Circus > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT
2 changes: 1 addition & 1 deletion text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ then
fi

# run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ../bin Circus > ACTUAL.TXT
java -classpath ../bin circus.Circus > ACTUAL.TXT

# compare the output to the expected output
diff ACTUAL.TXT EXPECTED.TXT
Expand Down