Skip to content

Commit

Permalink
code pushed for subtype polymorphism lecture
Browse files Browse the repository at this point in the history
  • Loading branch information
joshHug committed Feb 8, 2017
1 parent effe2d5 commit 0c2b9d7
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 8 deletions.
10 changes: 10 additions & 0 deletions inheritance3/DIY/DogLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class DogLauncher {
public static void main(String[] args) {
Dog d1 = new Dog("Elyse", 3);
Dog d2 = new Dog("Sture", 9);
Dog d3 = new Dog("Benjamin", 15);
Dog[] dogs = new Dog[]{d1, d2, d3};
System.out.println(Maximizer.max(dogs));
Dog d = (Dog) Maximizer.max(dogs);
}
}
7 changes: 0 additions & 7 deletions inheritance3/DIY/Maximizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,4 @@ public static Object max(Object[] items) {
return items[maxDex];

}

public static void main(String[] args) {
Dog[] dogs = {new Dog("Elyse", 3), new Dog("Sture", 9),
new Dog("Artemesios", 15)};
Dog maxDog = (Dog) max(dogs);
maxDog.bark();
}
}
File renamed without changes.
8 changes: 8 additions & 0 deletions inheritance3/live/Dog.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Comparator;

public class Dog implements Comparable<Dog> {
private String name;
private int size;
Expand All @@ -11,6 +13,12 @@ public void bark() {
System.out.println(name + " says: bark");
}

public static class NameComparator implements Comparator<Dog> {
public int compare(Dog d1, Dog d2) {
return d1.name.compareTo(d2.name);
}
}

/** Returns negative number if this dog is less than the dog pointed at by o, and so forth. */
public int compareTo(Dog uddaDog) {
return this.size - uddaDog.size;
Expand Down
9 changes: 9 additions & 0 deletions inheritance3/live/DogLauncher.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Comparator;

public class DogLauncher {
public static void main(String[] args) {
Dog d1 = new Dog("Elyse", 3);
Expand All @@ -6,5 +8,12 @@ public static void main(String[] args) {
Dog[] dogs = new Dog[]{d1, d2, d3};
System.out.println(Maximizer.max(dogs));
Dog d = (Dog) Maximizer.max(dogs);

Comparator<Dog> cd = new Dog.NameComparator();
if (cd.compare(d1, d3) > 0) {
d1.bark();
} else {
d3.bark();
}
}
}
1 change: 0 additions & 1 deletion inheritance3/live/Maximizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public static Comparable max(Comparable[] items) {
}
}
return items[maxDex];

}
}
13 changes: 13 additions & 0 deletions inheritance3/pauly/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Dog {
private String name;
private int size;

public Dog(String n, int s) {
name = n;
size = s;
}

public void bark() {
System.out.println(name + " says: bark");
}
}
10 changes: 10 additions & 0 deletions inheritance3/pauly/DogLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class DogLauncher {
public static void main(String[] args) {
Dog d1 = new Dog("Elyse", 3);
Dog d2 = new Dog("Sture", 9);
Dog d3 = new Dog("Benjamin", 15);
Dog[] dogs = new Dog[]{d1, d2, d3};
System.out.println(Maximizer.max(dogs));
Dog d = (Dog) Maximizer.max(dogs);
}
}
12 changes: 12 additions & 0 deletions inheritance3/pauly/Maximizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Maximizer {
public static Object max(Object[] items) {
int maxDex = 0;
for (int i = 0; i < items.length; i += 1) {
if (items[i] > items[maxDex]) {
maxDex = i;
}
}
return items[maxDex];

}
}
1 change: 1 addition & 0 deletions inheritance3/pauly/TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create a new interface that allows the Maximizer.max method to do its job on ANY object implementing that interface.

0 comments on commit 0c2b9d7

Please sign in to comment.