forked from joonspoon/madecraft-oop
-
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.
Adding the examples and exercises for Lesson 5.
- Loading branch information
0 parents
commit 11ee94d
Showing
30 changed files
with
775 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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> | ||
<attributes> | ||
<attribute name="module" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="lib" path="lib/Tortoise.jar"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,3 @@ | ||
.settings/ | ||
bin/ | ||
.DS_Store |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Lesson5</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,30 @@ | ||
package examples; | ||
|
||
public class Athlete { | ||
|
||
private String name; | ||
private int speed; | ||
private static String location; | ||
private int bibNumber; | ||
private static int numberOfRunners; | ||
|
||
public Athlete(String name, int speed) { | ||
this.name = name; | ||
this.speed = speed; | ||
this.bibNumber = numberOfRunners++; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + " " + speed + " at " + location + ", bib number " + this.bibNumber; | ||
} | ||
|
||
public void setLocation(String string) { | ||
this.location = string; | ||
} | ||
|
||
public static int getNumberOfRunners() { | ||
return numberOfRunners; | ||
} | ||
|
||
} |
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,27 @@ | ||
package examples; | ||
|
||
public class Disease { | ||
|
||
//instance variables | ||
private String name; | ||
private boolean curable; | ||
|
||
Disease(String title, boolean isCurable){ | ||
this.curable = isCurable; | ||
this.name = title; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public boolean isCurable() { | ||
return curable; | ||
} | ||
|
||
|
||
void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
} |
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,27 @@ | ||
package examples; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DiseaseSorter { | ||
|
||
public static void main(String[] args) { | ||
Disease polio = new Disease("polio", false); | ||
Disease flu = new Disease("influenza", true); | ||
Disease tuberculosis = new Disease("tuberculosis", false); | ||
|
||
List<Disease> diseases = new ArrayList<Disease>(); | ||
diseases.add(polio); | ||
diseases.add(flu); | ||
diseases.add(tuberculosis); | ||
|
||
/* print all the incurable diseases */ | ||
for (Disease disease : diseases) { | ||
if (!disease.isCurable()) { | ||
System.out.println(disease.getName()); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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,42 @@ | ||
package examples; | ||
|
||
import java.io.File; | ||
|
||
import javax.sound.sampled.AudioInputStream; | ||
import javax.sound.sampled.AudioSystem; | ||
import javax.sound.sampled.Clip; | ||
|
||
public class Duck { | ||
|
||
private String favoriteFood; | ||
private int lifeExpectancy; | ||
|
||
|
||
public Duck(String favoriteFood, int lifeExpectancy) { | ||
this.favoriteFood = favoriteFood; | ||
this.lifeExpectancy = lifeExpectancy; | ||
} | ||
|
||
public void waddle() { | ||
lifeExpectancy++; | ||
System.out.println("waddle waddle"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "This duck likes to eat " + this.favoriteFood + " and will live to be " + this.lifeExpectancy + "."; | ||
} | ||
|
||
public void quack() { | ||
try { | ||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("sounds/quack.wav")); | ||
Clip clip = AudioSystem.getClip(); | ||
clip.open(audioInputStream); | ||
clip.start(); | ||
Thread.sleep(3400); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
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,33 @@ | ||
package examples; | ||
|
||
public class DuckBreeder { | ||
public static void main(String[] args) { | ||
|
||
|
||
// Duck morrisey = new Duck("celery", 90/3); | ||
// | ||
// Duck carniverousCarla = new Duck("frogs", 60/3); | ||
// | ||
// carniverousCarla.waddle(); | ||
// carniverousCarla.waddle(); | ||
// carniverousCarla.waddle(); | ||
// | ||
// System.out.println(morrisey); | ||
// | ||
// System.out.println(carniverousCarla); | ||
// | ||
// morrisey.quack(); | ||
// morrisey.quack(); | ||
|
||
|
||
|
||
// 1. Create an instance of a Duck called Daffy | ||
//<type> <name> = <value> | ||
Duck daffy = new Duck("", 0); | ||
|
||
// 2. Get Daffy to quack | ||
|
||
daffy.quack(); | ||
|
||
} | ||
} |
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,21 @@ | ||
package examples; | ||
|
||
public class Marathon { | ||
|
||
//Athletes: | ||
//instance specific data: name, speed | ||
//static data: number of runners, location of marathon | ||
|
||
public static void main(String[] args) { | ||
Athlete john = new Athlete("john", 10); | ||
Athlete betsy = new Athlete("betsy", 12); | ||
|
||
john.setLocation("San Diego"); | ||
betsy.setLocation("London"); | ||
System.out.println(betsy); | ||
System.out.println(john); | ||
|
||
System.out.println("Runners in race: " + Athlete.getNumberOfRunners()); | ||
} | ||
|
||
} |
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,14 @@ | ||
package examples; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class MessingAroundWithJUnit { | ||
|
||
@Test | ||
public void testName() throws Exception { | ||
assertEquals(4, 2+2); | ||
|
||
} | ||
} |
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,46 @@ | ||
package exercises; | ||
|
||
public class Cat { | ||
|
||
private String name; | ||
private int lives = 9; | ||
|
||
Cat(String name) { | ||
this.name = name; | ||
} | ||
|
||
void meow() { | ||
System.out.println("meeeeeooooooooooowwwwwwwww!!"); | ||
} | ||
|
||
public void printName() { | ||
if (name == null) | ||
System.out.println("i don't know my own name!"); | ||
else | ||
System.out.println("my name is " + name); | ||
} | ||
|
||
void kill() { | ||
lives--; | ||
if (lives > 0) | ||
System.out.println("nice try, but I still have " + lives + " lives left"); | ||
else if (lives < 0) | ||
System.out.println("that's overkill yo!"); | ||
else | ||
System.out.println("DEAD CAT :("); | ||
} | ||
|
||
public static void main(String[] args) { | ||
/* Do the following things without changing the Cat class */ | ||
|
||
// 1. Make the Cat meow | ||
|
||
// 2. Get the Cat to print it's name | ||
|
||
// 3. Kill the Cat! | ||
|
||
} | ||
} | ||
|
||
|
||
|
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,14 @@ | ||
package exercises; | ||
/* | ||
* Use the Gridworld Reference document in the "doc" directory to complete the following tasks; | ||
* | ||
* 1. Figure out how to get the World to show. | ||
* 2. Figure out how to add a Bug to the world (clue: you will need the Bug and Location objects) | ||
* 3. Add another bug at a random location in the world. | ||
* 4. Change the color of that bug to blue. | ||
* 5. Make the bug face to the right. | ||
* 6. Add flowers to the left and right of the bug. | ||
* 7. Fill the whole world with flowers! | ||
*/ | ||
|
||
public class Gridworld {} |
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,36 @@ | ||
package exercises; | ||
|
||
public class HarryPotter { | ||
|
||
private boolean cloakOn; | ||
|
||
HarryPotter() { | ||
System.out.println("making Harry Potter..."); | ||
} | ||
|
||
void castSpell(String spell) { | ||
System.out.println("casting spell: " + spell); | ||
} | ||
|
||
void makeInvisible(boolean invisible) { | ||
this.cloakOn = invisible; | ||
|
||
if (cloakOn) | ||
System.out.println("Harry is invisible"); | ||
else | ||
System.out.println("Harry is visible"); | ||
} | ||
|
||
void spyOnSnape() { | ||
System.out.println("Harry sees Professor Snape doing nefarious things."); | ||
} | ||
|
||
public static void main(String[] args) { | ||
// 1. make harry potter | ||
// 2. become invisible | ||
// 3. spy on professor snape | ||
// 4. become visible again | ||
// 5. cast a “stupefy” spell | ||
} | ||
|
||
} |
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,50 @@ | ||
package exercises; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* To make these tests pass, you will need to create a Minion class with the member variables below. | ||
* | ||
* <code> | ||
* private String name; | ||
* private int eyes; | ||
* private String color; | ||
* private String master; | ||
* </code> | ||
* | ||
* Create a constructor, and getters and setters for the member variables. If they’re done right, these tests will pass. | ||
* | ||
* **/ | ||
|
||
public class MinionTest { | ||
|
||
// @Test | ||
// public void testConstructor() { | ||
// Minion stuart = new Minion("Stuart", 1, "yellow", ""); | ||
// assertEquals("Stuart", stuart.getName()); | ||
// assertEquals(1, stuart.getEyes()); | ||
// assertEquals("yellow", stuart.getColor()); | ||
// | ||
// Minion dave = new Minion("Dave", 2, "yellow", ""); | ||
// assertEquals("Dave", dave.getName()); | ||
// assertEquals(2, dave.getEyes()); | ||
// assertEquals("yellow", dave.getColor()); | ||
// } | ||
// | ||
// @Test | ||
// public void testSetters() { | ||
// Minion stuart = new Minion("Stuart", 1, "yellow", ""); | ||
// | ||
// stuart.setMaster("T. Rex"); | ||
// assertEquals("T. Rex", stuart.getMaster()); | ||
// | ||
// stuart.setMaster("Napoleon"); | ||
// assertEquals("Napoleon", stuart.getMaster()); | ||
// } | ||
|
||
} | ||
|
||
|
||
|
Oops, something went wrong.