Skip to content

Commit

Permalink
Re-organized examples and exercises by chapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
joonspoon committed Nov 20, 2019
1 parent 6ce6454 commit 8ccd438
Show file tree
Hide file tree
Showing 32 changed files with 105 additions and 346 deletions.
13 changes: 9 additions & 4 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/gridworld.jar"/>
<classpathentry kind="lib" path="lib/Tortoise.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="src" path="2. Inside Classes"/>
<classpathentry kind="src" path="3. Getters and Setters"/>
<classpathentry kind="src" path="4. Static"/>
<classpathentry kind="src" path="6. UML"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Objects</name>
<name>madecraft-oop</name>
<comment></comment>
<projects>
</projects>
Expand Down
File renamed without changes.
23 changes: 21 additions & 2 deletions src/examples/Duck.java → 2. Inside Classes/examples/Duck.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
package examples;

import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Duck {

// member variables: data (encapsulated / private)
/* Member Variables: data (encapsulated / private) */

private String name;
private int lifeExpectancy;
private String favoriteFood;

/* The Constructor: for creating instances */

public Duck(String name, int lifeExpectancy, String favoriteFood) {
super();
this.name = name;
this.lifeExpectancy = lifeExpectancy;
this.favoriteFood = favoriteFood;
}

// methods: functionality
/* Methods: functionality */

void waddle() {
lifeExpectancy++;
System.out.println(this.name + " is waddling");
}

void quack() {
System.out.println("quack quack");
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(
"sounds/quack.wav").toURI().toURL());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
Thread.sleep(3400);
} catch (Exception ex) {
ex.printStackTrace();
}
}

@Override
Expand Down
16 changes: 10 additions & 6 deletions src/examples/Pond.java → 2. Inside Classes/examples/Pond.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package examples;


public class Pond {

public static void main(String[] args) {
Duck mobyDuck = new Duck("Moby", 90/3, "celery");
Duck wolfgangDuck = new Duck("Wolfgang", 60/3, "cheeseburgers");

Duck mobyDuck = new Duck("Moby", 90 / 3, "celery");
Duck wolfgangDuck = new Duck("Wolfgang", 60 / 3, "cheeseburgers");

mobyDuck.waddle();
mobyDuck.waddle();
mobyDuck.waddle();
mobyDuck.waddle();
mobyDuck.waddle();
mobyDuck.waddle();
mobyDuck.waddle();

wolfgangDuck.waddle();

System.out.println(mobyDuck);

// Exercise: Make the Duck quack

}

}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/examples/Calculator.java → ...ters and Setters/examples/Calculator.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Calculator {

public static Integer add(int i, int j) {
public static int add(int i, int j) {
return i / j;
}

Expand Down
16 changes: 9 additions & 7 deletions src/examples/Disease.java → 3. Getters and Setters/examples/Disease.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ public class Disease {
this.name = title;
}

public String getName() {
return name;
}
/* SETTERS */

public void setName(String name) {
this.name = name;
}

public boolean isCurable() {
return curable;
}

public void setCurable(boolean curable) {
this.curable = curable;
}

/* GETTERS */

public boolean isCurable() {
return curable;
}

public String getName() {
return name;
}

}
20 changes: 11 additions & 9 deletions src/examples/DiseaseSorter.java → ...s and Setters/examples/DiseaseSorter.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ public static void main(String[] args) {
diseases.add(flu);
diseases.add(malaria);
diseases.add(cancer);

// 1. change the value in the constructor - you can only call a constructor once
// 2. access the member variable directly - breaks encapsulation
// 3. create a setter


/*
* Options for setting the value of "curable":
* 1. change the value in the constructor - you can only call a constructor once
* 2. access the member variable directly - breaks encapsulation
* 3. create a setter
*/

cancer.setCurable(true);

/* print all the incurable diseases */

System.out.println("Curable diseases: ");
for (Disease disease : diseases) {
if(disease.isCurable()) {
if (disease.isCurable()) {
System.out.println(disease.getName());
}
}

}

}
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 12 additions & 12 deletions src/exercises/MinionTest.java → ...ers and Setters/exercises/MinionTest.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@

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 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() {
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion src/examples/DoStuff.java → 4. Static/examples/DoStuff.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class DoStuff {

public static void main(String args[]) {
new Pug().bark();

}

}
File renamed without changes.
File renamed without changes.
14 changes: 5 additions & 9 deletions src/optional/SeaCreature.java → 4. Static/exercises/SeaCreature.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package optional;
package exercises;

/*
* 1. In a Runner class, make a SeaCreature called “Spongebob”. Use the methods below to make him eat, and laugh.
* 2. Make Patrick and Squidward and print their name, have them eat, and make them laugh.
* 1. Create a class called Ocean.
* 2. There make make a SeaCreature called “Spongebob”.
* 3. Use the methods below to make him eat, and laugh.
* 4. Make Patrick and Squidward and print their name, and make them laugh.
*/

import java.io.File;
Expand All @@ -12,12 +14,6 @@

public class SeaCreature {

public static void main(String[] args) {
SeaCreature spong = new SeaCreature("squidward");
spong.laugh();
}


private String name;

SeaCreature(String name) {
Expand Down
File renamed without changes.
58 changes: 26 additions & 32 deletions src/optional/Backpack.java → 6. UML/exercises/Backpack.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
package optional;

package exercises;

public class Backpack {

private Pencil pencil;
private Ruler ruler;
private Textbook textbook;

Backpack(){
Backpack() {
System.out.println("Nice Backpack");
}

public static void main (String[] args){
/* Your mission is to get to school, but first you need to get all of your supplies into your backpack. */


public static void main(String[] args) {
/* Your mission is to get to school, but first you need to get all of your supplies into your backpack. */

}
public void putInBackpack(Supply supply){
if (supply instanceof Pencil){

public void putInBackpack(Supply supply) {
if (supply instanceof Pencil) {
this.pencil = (Pencil) supply;
System.out.println("You put your pencil in your Backpack");
}else if(supply instanceof Ruler){
} else if (supply instanceof Ruler) {
this.ruler = (Ruler) supply;
System.out.println("You put your ruler in your Backpack");
}else if(supply instanceof Textbook){
} else if (supply instanceof Textbook) {
this.textbook = (Textbook) supply;
System.out.println("You put your textbook in your Backpack");
}else{
} else {
System.out.println("That isn't a valid school supply");
}
}
public void goToSchool(){
if(pencil == null || ruler == null || textbook == null){

public void goToSchool() {
if (pencil == null || ruler == null || textbook == null) {
System.err.println("You are not ready for School");
}else{
} else {
System.out.println("Congratulations! You are ready for school");
}
}
Expand All @@ -47,40 +44,37 @@ class Supply {
}

class Pencil extends Supply {
Pencil(){

Pencil() {
this.name = "pencil";
System.out.println("You got your pencil!");
}
public void write(String writing){

public void write(String writing) {
System.out.println(writing);
}
}


class Ruler extends Supply {

Ruler(){
Ruler() {
this.name = "ruler";
System.out.println("You found your ruler!!");
}
public void measure(){

public void measure() {
System.out.println("Now you can measure your mouse!");
}
}

class Textbook extends Supply{
class Textbook extends Supply {

Textbook(){
Textbook() {
this.name = "textbook";
System.out.println("You got your boring textbook");
}
public void read(){

public void read() {
System.out.println("The history of Iceland is long and interesting");
}
}


Binary file removed doc/Gridworld Reference.pdf
Binary file not shown.
Binary file removed lib/Tortoise.jar
Binary file not shown.
Binary file removed lib/gridworld.jar
Binary file not shown.
37 changes: 0 additions & 37 deletions src/examples/Smurf.java

This file was deleted.

Loading

0 comments on commit 8ccd438

Please sign in to comment.