Skip to content

Commit 25668cd

Browse files
committed
added webcast code
1 parent ed653fa commit 25668cd

13 files changed

+583
-0
lines changed

lec9/exercises/example/IntNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class IntNode {
2+
public int item;
3+
public IntNode next;
4+
5+
public IntNode(int i, IntNode n) {
6+
item = i;
7+
next = n;
8+
}
9+
10+
}

lec9/exercises/example/MaxSList.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Josh Hug
3+
*/
4+
5+
public class MaxSList extends SList {
6+
private int max;
7+
8+
public MaxSList() {
9+
super(); /* call my superclass's constructor */
10+
max = 0;
11+
}
12+
13+
public MaxSList(int x) {
14+
super(x); /* call my superclass's one parameter constructor */
15+
max = x;
16+
}
17+
18+
@Override
19+
public void insertFront(int x) {
20+
System.out.println("The MaxSList version was called.");
21+
if (x > max) {
22+
max = x;
23+
}
24+
super.insertFront(x);
25+
}
26+
27+
@Override
28+
public void insertBack(int x) {
29+
if (x > max) {
30+
max = x;
31+
}
32+
super.insertBack(x);
33+
}
34+
35+
public int max() {
36+
return max;
37+
}
38+
39+
public static void main(String[] args) {
40+
/*MaxSList msl = new MaxSList(10);
41+
msl.insertBack(50);
42+
System.out.println(msl.getFront());
43+
System.out.println(msl.max());*/
44+
SList sl = new MaxSList(10);
45+
sl.insertFront(100);
46+
}
47+
/* your code here */
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @author Josh Hug
3+
*/
4+
5+
/* MaxSList launcher that you can experiment with. */
6+
public class MaxSListLauncher {
7+
public static void main(String[] args) {
8+
MaxSList msl = new MaxSList(0);
9+
msl.insertBack(50);
10+
SList sl = msl;
11+
msl.insertFront(1000);
12+
System.out.println(msl.max());
13+
14+
MaxSList msl3 = (MaxSList) sl;
15+
16+
/* Both cause compilation errors: */
17+
//System.out.println(sl.max());
18+
//MaxSList msl2 = sl;
19+
20+
// causes compilation error
21+
//MaxSList msl4 = (SList) new MaxSList(5);
22+
}
23+
}

lec9/exercises/example/SList.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @author Josh Hug
3+
*/
4+
5+
public class SList {
6+
protected IntNode sentinel;
7+
/* Do not change anything below this line. */
8+
9+
protected int size;
10+
11+
public SList() {
12+
size = 0;
13+
sentinel = new IntNode(999, null);
14+
}
15+
16+
public SList(int x) {
17+
sentinel = new IntNode(x, null);
18+
size = 1;
19+
}
20+
21+
public void insertBack(int x) {
22+
IntNode p = sentinel;
23+
while (p.next != null) {
24+
p = p.next;
25+
}
26+
27+
// p is the LAST node at this point
28+
p.next = new IntNode(x, null);
29+
size = size + 1;
30+
}
31+
32+
public void insertFront(int x) {
33+
System.out.println("The SList version was called.");
34+
35+
IntNode oldFront = sentinel.next;
36+
sentinel.next = new IntNode(x, oldFront);
37+
size = size + 1;
38+
}
39+
40+
public int getFront() {
41+
return sentinel.next.item;
42+
}
43+
44+
public int getBack() {
45+
IntNode p = sentinel;
46+
while (p.next != null) {
47+
p = p.next;
48+
}
49+
50+
// p is the LAST node at this point
51+
return p.item;
52+
}
53+
54+
public int size() {
55+
return size;
56+
}
57+
58+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @author Josh Hug
3+
*/
4+
5+
public class SomeOtherPersonsCode {
6+
/**
7+
*/
8+
9+
public static void main(String[] args) {
10+
SList sl = new MaxSList(10);
11+
sl.insertFront(100);
12+
}
13+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import org.junit.Test;
2+
import static org.junit.Assert.*;
3+
4+
/** Tests the MaxSList class.
5+
* @author Josh Hug
6+
*/
7+
8+
public class TestMaxSList {
9+
/** This Unit tests is fairly complicated.
10+
* It assumes that the constructor, isnertFront
11+
* and getFront all work. */
12+
@Test
13+
public void testInserts() {
14+
MaxSList L = new MaxSList(10);
15+
L.insertFront(50);
16+
assertEquals(50, L.getFront());
17+
18+
L.insertBack(99);
19+
assertEquals(50, L.getFront());
20+
assertEquals(99, L.getBack());
21+
}
22+
23+
@Test
24+
public void testSize() {
25+
MaxSList L = new MaxSList(10);
26+
L.insertFront(50);
27+
L.insertBack(99);
28+
assertEquals(3, L.size());
29+
}
30+
31+
@Test
32+
public void testEmptySize() {
33+
MaxSList L = new MaxSList();
34+
L.insertBack(99);
35+
L.insertFront(50);
36+
assertEquals(2, L.size());
37+
assertEquals(50, L.getFront());
38+
}
39+
40+
@Test
41+
public void textMax() {
42+
MaxSList L = new MaxSList();
43+
L.insertBack(99);
44+
L.insertFront(50);
45+
assertEquals(99, L.max());
46+
47+
L = new MaxSList(29);
48+
L.insertBack(22);
49+
L.insertFront(-58);
50+
assertEquals(29, L.max());
51+
}
52+
53+
public static void main(String[] args) {
54+
jh61b.junit.textui.runClasses(TestMaxSList.class);
55+
}
56+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @author Josh Hug
3+
*/
4+
5+
public class VisualizableMaxSListProblem {
6+
/**
7+
* @author Josh Hug
8+
*/
9+
10+
public static class IntNode {
11+
public int item;
12+
public IntNode next;
13+
14+
public IntNode(int i, IntNode n) {
15+
item = i;
16+
next = n;
17+
}
18+
19+
}
20+
public static class SList {
21+
protected IntNode sentinel;
22+
/* Do not change anything below this line. */
23+
24+
protected int size;
25+
26+
public SList() {
27+
size = 0;
28+
sentinel = new IntNode(999, null);
29+
}
30+
31+
public SList(int x) {
32+
sentinel = new IntNode(x, null);
33+
size = 1;
34+
}
35+
36+
public void insertBack(int x) {
37+
IntNode p = sentinel;
38+
while (p.next != null) {
39+
p = p.next;
40+
}
41+
42+
// p is the LAST node at this point
43+
p.next = new IntNode(x, null);
44+
size = size + 1;
45+
}
46+
47+
public void insertFront(int x) {
48+
IntNode oldFront = sentinel.next;
49+
sentinel.next = new IntNode(x, oldFront);
50+
size = size + 1;
51+
}
52+
53+
public int getFront() {
54+
return sentinel.next.item;
55+
}
56+
57+
public int getBack() {
58+
IntNode p = sentinel;
59+
while (p.next != null) {
60+
p = p.next;
61+
}
62+
63+
// p is the LAST node at this point
64+
return p.item;
65+
}
66+
67+
public int size() {
68+
return size;
69+
}
70+
71+
}
72+
73+
public static class MaxSList extends SList {
74+
private int max;
75+
76+
public MaxSList() {
77+
super(); /* call my superclass's constructor */
78+
max = 0;
79+
}
80+
81+
public MaxSList(int x) {
82+
super(x); /* call my superclass's one parameter constructor */
83+
max = x;
84+
}
85+
86+
@Override
87+
public void insertFront(int x) {
88+
if (x > max) {
89+
max = x;
90+
}
91+
super.insertFront(x);
92+
}
93+
94+
@Override
95+
public void insertBack(int x) {
96+
if (x > max) {
97+
max = x;
98+
}
99+
super.insertBack(x);
100+
}
101+
102+
public int max() {
103+
return max;
104+
}
105+
}
106+
107+
public static void main(String[] args) {
108+
MaxSList msl = new MaxSList(0);
109+
msl.insertBack(50);
110+
SList sl = msl;
111+
msl.insertFront(1000);
112+
System.out.println(msl.max());
113+
}
114+
}

lec9/webcast/IntNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class IntNode {
2+
public int item;
3+
public IntNode next;
4+
5+
public IntNode(int i, IntNode n) {
6+
item = i;
7+
next = n;
8+
}
9+
10+
}

0 commit comments

Comments
 (0)