Skip to content

Commit d528fb5

Browse files
committed
various generics exercise setup
1 parent 16d28cd commit d528fb5

File tree

18 files changed

+293
-19
lines changed

18 files changed

+293
-19
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.Test;
2+
import static org.junit.Assert.*;
3+
4+
/** Tests goal one for hard mode exercises for lecture 14
5+
* @author Josh Hug
6+
*/
7+
8+
public class TestGoalOne {
9+
@Test
10+
public void testVessel() {
11+
Vessel<Integer> vi = new Vessel<Integer>();
12+
int x = 5;
13+
vi.put(x); //autoboxes into an Integer, Integer is-an Object
14+
int y = vi.peek(); // no unboxing, returns an Object
15+
assertEquals(5, y);
16+
assertEquals((Integer) 5, vi.peek());
17+
18+
Vessel<String> vst = new Vessel<String>();
19+
vst.put("hello");
20+
assertEquals("hello", vst.peek());
21+
}
22+
23+
/** Runs tests. */
24+
public static void main(String[] args) {
25+
jh61b.junit.textui.runClasses(TestGoalOne.class);
26+
}
27+
}

lec14/exercises/hardMode/TestMax.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.Test;
2+
import static org.junit.Assert.*;
3+
4+
/** Tests VesselHelper
5+
* @author Josh Hug
6+
*/
7+
8+
public class TestMax {
9+
@Test
10+
public void testMax() {
11+
Vessel<Integer> v1 = new Vessel<Integer>();
12+
Vessel<Integer> v2 = new Vessel<Integer>();
13+
v1.put(1);
14+
v2.put(2);
15+
assertEquals((Integer) 2, VesselHelper.max(v1, v2));
16+
}
17+
18+
/** Runs tests. */
19+
public static void main(String[] args) {
20+
jh61b.junit.textui.runClasses(TestMax.class);
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import org.junit.Test;
2+
import static org.junit.Assert.*;
3+
4+
/** Tests VesselHelper
5+
* @author Josh Hug
6+
*/
7+
8+
public class TestRatio {
9+
@Test
10+
public void testRatio() {
11+
Vessel<Integer> vInteger = new Vessel<Integer>();
12+
Vessel<Double> vDouble = new Vessel<Double>();
13+
vInteger.put(20);
14+
vDouble.put(2.0);
15+
16+
assertEquals((Double) 10.0, VesselHelper.ratio(vInteger, vDouble), 1e-5);
17+
}
18+
19+
/** Runs tests. */
20+
public static void main(String[] args) {
21+
jh61b.junit.textui.runClasses(TestRatio.class);
22+
}
23+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import org.junit.Test;
2+
import static org.junit.Assert.*;
3+
4+
/** Tests VesselHelper
5+
* @author Josh Hug
6+
*/
7+
8+
public class TestRemove {
9+
10+
@Test
11+
public void testRemove() {
12+
Vessel<Integer> v = new Vessel<Integer>();
13+
v.put(5);
14+
int occupant = VesselHelper.remove(v);
15+
assertEquals(5, occupant);
16+
assertEquals(null, v.peek());
17+
18+
Vessel<String> sV = new Vessel<String>();
19+
sV.put("ketchupFriend");
20+
String occupantString = VesselHelper.remove(sV);
21+
assertEquals("ketchupFriend", occupantString);
22+
assertEquals(null, sV.peek());
23+
}
24+
25+
/** Runs tests. */
26+
public static void main(String[] args) {
27+
jh61b.junit.textui.runClasses(TestRemove.class);
28+
}
29+
}

lec14/exercises/hardMode/Vessel.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** Your job: Convert this into a generic Vessel so that TestGoalOne works.
2+
* @author Josh Hug
3+
*/
4+
5+
public class Vessel {
6+
Integer occupant;
7+
8+
public void put(Integer x) {
9+
occupant = x;
10+
}
11+
12+
public Integer peek() {
13+
return occupant;
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/** Does stuff with vessels in a contrived manner to draw out
2+
* annoying themes with Generics.
3+
* @author Josh Hug
4+
*/
5+
6+
/* Methods can have type parameters too!
7+
* Specify them BEFORE the return type of the method. */
8+
9+
public class VesselHelper {
10+
/* Remove the item from the vessel and return it. */
11+
public static Integer remove(Vessel<Integer> v) {
12+
Integer rVal = v.peek();
13+
v.put(null); // displace the occupant.
14+
return rVal;
15+
}
16+
17+
public static Integer max(Vessel<Integer> x, Vessel<Integer> y) {
18+
Integer xVal = x.peek();
19+
Integer yVal = y.peek();
20+
if (xVal.compareTo(yVal) > 0) {
21+
return xVal;
22+
}
23+
return yVal;
24+
}
25+
26+
public static double ratio(Vessel<Integer> x, Vessel<Integer> y) {
27+
Integer xVal = x.peek();
28+
Integer yVal = y.peek();
29+
return ((double) xVal) / ((double) yVal);
30+
}
31+
}

lec14/exercises/hardMode/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Your task is to complete the tasks listed here: https://docs.google.com/presentation/d/1j2vivowiaZWepIjUoWj6Cx3CdJiyuYslxkg8GsT8kxM/edit#slide=id.g631db3c57_367
2+
3+
We provide you with non-generic versions of Vessel and VesselHelper. Your job is to make them generic so that the provided test files pass. Test files (and goals) should be done in this order:
4+
5+
TestGoalOne (modify Vessel only)
6+
TestRemove (modify VesselHelper.remove only)
7+
TestMax (modify VesselHelper.max only)
8+
TestRatio (modify VesselHelper.remove only)
9+
10+
All code should compile without warnings.

lec14/live1/VesselHelper.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static <Slaggathor> Slaggathor remove(Vessel<Slaggathor> v) {
1414
return rVal;
1515
}
1616

17-
public static <ZX extends Comparable> ZX max(Vessel<ZX> x, Vessel<ZX> y) {
17+
public static <ZX extends Comparable<ZX>> ZX max(Vessel<ZX> x, Vessel<ZX> y) {
1818
ZX xVal = x.peek();
1919
ZX yVal = y.peek();
2020
if (xVal.compareTo(yVal) > 0) {
@@ -23,10 +23,36 @@ public static <ZX extends Comparable> ZX max(Vessel<ZX> x, Vessel<ZX> y) {
2323
return yVal;
2424
}
2525

26+
/* ratio is trickier */
27+
28+
/* Approach below will not work unless you pass EXACTLY a Vessel<Number>.
29+
* For example, we could not pass in a Vessel<Integer>, because a
30+
* a Vessel<Integer> is-not-a Vessel<Number>. */
31+
/*public static double ratio(Vessel<Number> v1, Vessel<Number> v2) {
32+
double d1 = v1.peek().doubleValue();
33+
double d2 = v2.peek().doubleValue();
34+
return d1 / d2;
35+
} */
36+
37+
38+
/** The preferred approach. */
39+
public static double ratio(Vessel<? extends Number> x, Vessel<? extends Number> y) {
40+
Number xVal = x.peek(); // assignment allowed since ? is-a Number,
41+
Number yVal = y.peek(); // even though we don't know what ? is.
42+
return xVal.doubleValue() / yVal.doubleValue();
43+
}
44+
45+
/*
46+
This approach also works, but is a little more verbose than the version above.
47+
48+
Either approach is fine on project 1.
49+
2650
public static <T1 extends Number, T2 extends Number>
2751
double ratio(Vessel<T1> x, Vessel<T2> y) {
2852
T1 xVal = x.peek();
2953
T2 yVal = y.peek();
3054
return xVal.doubleValue() / yVal.doubleValue();
3155
}
56+
*/
57+
3258
}

lec14/live2/VesselHelper.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,36 @@ public static <Plarp extends Comparable<Plarp>> Plarp max(Vessel<Plarp> v1, Vess
2525
return v2val;
2626
}
2727

28-
public static double ratio(Vessel<Number> v1, Vessel<Number> v2) {
28+
/* ratio is trickier */
29+
30+
/* Approach below will not work unless you pass EXACTLY a Vessel<Number>.
31+
* For example, we could not pass in a Vessel<Integer>, because a
32+
* a Vessel<Integer> is-not-a Vessel<Number>. */
33+
/*public static double ratio(Vessel<Number> v1, Vessel<Number> v2) {
2934
double d1 = v1.peek().doubleValue();
3035
double d2 = v2.peek().doubleValue();
3136
return d1 / d2;
32-
}
37+
} */
38+
39+
40+
/** The preferred approach. */
41+
public static double ratio(Vessel<? extends Number> x, Vessel<? extends Number> y) {
42+
Number xVal = x.peek(); // assignment allowed since ? is-a Number,
43+
Number yVal = y.peek(); // even though we don't know what ? is.
44+
return xVal.doubleValue() / yVal.doubleValue();
45+
}
46+
47+
/*
48+
This approach also works, but is a little more verbose than the version above.
49+
50+
Either approach is fine on project 1.
51+
52+
public static <T1 extends Number, T2 extends Number>
53+
double ratio(Vessel<T1> x, Vessel<T2> y) {
54+
T1 xVal = x.peek();
55+
T2 yVal = y.peek();
56+
return xVal.doubleValue() / yVal.doubleValue();
57+
}
58+
*/
59+
3360
}

lec15/exercises/hardMode/Iteration/AListIteratorLauncher2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @author Josh Hug
33
*/
44

5-
public class AListIteratorLauncher {
5+
public class AListIteratorLauncher2 {
66
public static void main(String[] args) {
77
AList L = new AList();
88
L.insertBack(50);

0 commit comments

Comments
 (0)