forked from Berkeley-CS61B/lectureCode-sp17
-
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.
code pushed for subtype polymorphism lecture
- Loading branch information
Showing
13 changed files
with
63 additions
and
8 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,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); | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
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 |
---|---|---|
|
@@ -8,6 +8,5 @@ public static Comparable max(Comparable[] items) { | |
} | ||
} | ||
return items[maxDex]; | ||
|
||
} | ||
} |
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,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"); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 @@ | ||
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]; | ||
|
||
} | ||
} |
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 @@ | ||
Create a new interface that allows the Maximizer.max method to do its job on ANY object implementing that interface. |