Skip to content

Commit

Permalink
Adding the examples and exercises for Lesson 5.
Browse files Browse the repository at this point in the history
  • Loading branch information
joonspoon committed Mar 18, 2019
0 parents commit 11ee94d
Show file tree
Hide file tree
Showing 30 changed files with 775 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .classpath
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>
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.settings/
bin/
.DS_Store
17 changes: 17 additions & 0 deletions .project
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 added doc/Gridworld Reference.pdf
Binary file not shown.
Binary file added lib/Tortoise.jar
Binary file not shown.
Binary file added lib/gridworld.jar
Binary file not shown.
Binary file added sounds/patrick.wav
Binary file not shown.
Binary file added sounds/quack.wav
Binary file not shown.
Binary file added sounds/spongebob.wav
Binary file not shown.
Binary file added sounds/squidward.wav
Binary file not shown.
30 changes: 30 additions & 0 deletions src/examples/Athlete.java
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;
}

}
27 changes: 27 additions & 0 deletions src/examples/Disease.java
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;
}

}
27 changes: 27 additions & 0 deletions src/examples/DiseaseSorter.java
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());
}
}

}

}
42 changes: 42 additions & 0 deletions src/examples/Duck.java
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();
}
}

}
33 changes: 33 additions & 0 deletions src/examples/DuckBreeder.java
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();

}
}
21 changes: 21 additions & 0 deletions src/examples/Marathon.java
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());
}

}
14 changes: 14 additions & 0 deletions src/examples/MessingAroundWithJUnit.java
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);

}
}
46 changes: 46 additions & 0 deletions src/exercises/Cat.java
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!

}
}



14 changes: 14 additions & 0 deletions src/exercises/Gridworld.java
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 {}
36 changes: 36 additions & 0 deletions src/exercises/HarryPotter.java
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
}

}
50 changes: 50 additions & 0 deletions src/exercises/MinionTest.java
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());
// }

}



Loading

0 comments on commit 11ee94d

Please sign in to comment.