Skip to content

Commit e0bb100

Browse files
committed
lecture 16 code
1 parent e5d05ec commit e0bb100

File tree

10 files changed

+288
-10
lines changed

10 files changed

+288
-10
lines changed

lec10_inheritance3/ArraySet.java

+82-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package lec10_inheritance3;
22

3-
public class ArraySet<T> {
3+
import java.util.Iterator;
4+
5+
// Saying implements Iterable<T>
6+
// means that you have a method that returns an Iterator<T> and this method
7+
// is called .iterator
8+
9+
// and in turn, that Iterator has a hasNext and next method.
10+
11+
public class ArraySet<T> implements Iterable<T> {
412
private T[] items;
513
private int size; // the next item to be added will be at position size
614

@@ -57,17 +65,87 @@ public static <Glerp> ArraySet<Glerp> of(Glerp... stuff) {
5765
return returnSet;
5866
} */
5967

68+
// an ArraySetIterator is the seer, it is the wizard
69+
private class ArraySetIterator implements Iterator<T> {
70+
// the position of the wizard
71+
private int wizPos;
72+
73+
ArraySetIterator() {
74+
wizPos = 0;
75+
}
76+
77+
@Override
78+
public boolean hasNext() {
79+
if (wizPos < size) {
80+
return true;
81+
}
82+
return false;
83+
}
84+
85+
@Override
86+
public T next() {
87+
// returns the item
88+
T itemToReturn = items[wizPos];
89+
// advances
90+
wizPos += 1;
91+
92+
return itemToReturn;
93+
}
94+
}
95+
96+
public Iterator<T> iterator() {
97+
return new ArraySetIterator();
98+
}
99+
100+
@Override
101+
public String toString() {
102+
StringBuilder stringToReturn = new StringBuilder("{");
103+
for (T x : this) {
104+
stringToReturn.append(x);
105+
stringToReturn.append(", ");
106+
}
107+
stringToReturn.append("}");
108+
return stringToReturn.toString();
109+
}
110+
111+
@Override
112+
public boolean equals(Object o) {
113+
// check if o is an ArraySet!
114+
if (o instanceof ArraySet uddaSet) {
115+
116+
// if o is actually an ArraySet, now I have uddaset
117+
// which is of type ArraySet
118+
if (this.size != uddaSet.size()) {
119+
return false;
120+
}
121+
for (T x : this) {
122+
if (!uddaSet.contains(x)) {
123+
return false;
124+
}
125+
}
126+
return true;
127+
}
128+
return false;
129+
}
130+
60131
public static void main(String[] args) {
61132
ArraySet<Integer> aset = new ArraySet<>();
62133
aset.add(5);
63134
aset.add(23);
64135
aset.add(42);
65136

66-
/* won't work yet:
137+
/* won't work yet: */
67138
for (int i : aset) {
68139
System.out.println(i);
69140
}
70-
*/
141+
142+
Iterator<Integer> aseer = aset.iterator();
143+
while (aseer.hasNext()) {
144+
int i = aseer.next();
145+
System.out.println(i);
146+
}
147+
148+
System.out.println(aset);
71149

72150
//EXTRA VIDEO CODE
73151
//ArraySet<String> asetOfStrings = ArraySet.of("hi", "I'm", "here");
@@ -79,4 +157,4 @@ public static void main(String[] args) {
79157
2. Implement a toString method.
80158
3. Implement an equals() method.
81159
*/
82-
}
160+
}

lec16_inheritance4/List61B.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package lec16_inheritance4;
2+
3+
// this cannot be instantiated directly
4+
// because it is an interface
5+
public interface List61B<Item> {
6+
public void insert(Item x, int position);
7+
public void addFirst(Item x);
8+
public void addLast(Item x);
9+
public Item getFirst();
10+
public Item getLast();
11+
public Item get(int i);
12+
public int size();
13+
public Item removeLast();
14+
default public void print() {
15+
for (int i = 0; i < size(); i +=1) {
16+
System.out.print(get(i) + " ");
17+
}
18+
}
19+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package lec16_inheritance4;
2+
3+
/* SLList, but with additional rotateRight operation. */
4+
public class RotatingSLList<Item> extends SLList<Item> {
5+
6+
/** To do: Implement RotatingSLList such that code compiles and outputs correct result. */
7+
8+
/** Rotates list to the right. */
9+
public void rotateRight() {
10+
Item x = removeLast();
11+
addFirst(x);
12+
}
13+
14+
public static void main(String[] args) {
15+
RotatingSLList<Integer> rsl = new RotatingSLList<>();
16+
/* Creates SList: [10, 11, 12, 13] */
17+
rsl.addLast(10);
18+
rsl.addLast(11);
19+
rsl.addLast(12);
20+
rsl.addLast(13);
21+
22+
/* Should be: [13, 10, 11, 12] */
23+
rsl.rotateRight();
24+
rsl.print();
25+
}
26+
}

lec16_inheritance4/SLList.java

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package lec16_inheritance4;
2+
3+
/* Represent a list of stuff, where all the "list" work is delegated
4+
* to a naked recursive data structure. */
5+
6+
public class SLList<Blorp> implements List61B<Blorp> {
7+
public class Node {
8+
public Blorp item; /* Equivalent of first */
9+
public Node next; /* Equivalent of rest */
10+
11+
public Node(Blorp i, Node h) {
12+
item = i;
13+
next = h;
14+
}
15+
}
16+
17+
private Node sentinel;
18+
private int size;
19+
20+
/** Creates an empty list. */
21+
public SLList() {
22+
size = 0;
23+
sentinel = new Node(null, null);
24+
}
25+
26+
public SLList(Blorp x) {
27+
size = 1;
28+
sentinel = new Node(null, null);
29+
sentinel.next = new Node(x, null);
30+
}
31+
32+
/** Adds an item of the front. */
33+
public void addFirst(Blorp x) {
34+
Node oldFrontNode = sentinel.next;
35+
Node newNode = new Node(x, oldFrontNode);
36+
sentinel.next = newNode;
37+
size += 1;
38+
}
39+
40+
/** Gets the front item of the list. */
41+
public Blorp getFirst() {
42+
return sentinel.next.item;
43+
}
44+
45+
/** Puts an item at the back of the list. */
46+
public void addLast(Blorp x) {
47+
size += 1;
48+
49+
Node p = sentinel;
50+
51+
/* Move p until it reaches the end. */
52+
while (p.next != null) {
53+
p = p.next;
54+
}
55+
56+
p.next = new Node(x, null);
57+
}
58+
59+
/** Returns the back node of our list. */
60+
private Node getLastNode() {
61+
Node p = sentinel;
62+
63+
/* Move p until it reaches the end. */
64+
while (p.next != null) {
65+
p = p.next;
66+
}
67+
return p;
68+
}
69+
70+
/** Returns last item */
71+
public Blorp getLast() {
72+
Node back = getLastNode();
73+
return back.item;
74+
}
75+
76+
/** Deletes and returns last item. */
77+
public Blorp removeLast() {
78+
Node back = getLastNode();
79+
if (back == sentinel) {
80+
return null;
81+
}
82+
83+
size = size - 1;
84+
Node p = sentinel;
85+
86+
while (p.next != back) {
87+
p = p.next;
88+
}
89+
p.next = null;
90+
return back.item;
91+
}
92+
93+
public int size() {
94+
return size;
95+
}
96+
97+
/** Gets the positionth item of the list. */
98+
public Blorp get(int position) {
99+
if (position == 0) {
100+
return getFirst();
101+
}
102+
Node currentNode = sentinel.next.next;
103+
while (position > 1 && currentNode.next != null) {
104+
position -= 1;
105+
currentNode = currentNode.next;
106+
}
107+
108+
return currentNode.item;
109+
}
110+
111+
/** Inserts item into given position.
112+
* Code from discussion #3 */
113+
public void insert(Blorp item, int position) {
114+
if (sentinel.next == null || position == 0) {
115+
addFirst(item);
116+
return;
117+
}
118+
119+
Node currentNode = sentinel.next.next;
120+
while (position > 1 && currentNode.next != null) {
121+
position -= 1;
122+
currentNode = currentNode.next;
123+
}
124+
125+
Node newNode = new Node(item, currentNode.next);
126+
currentNode.next = newNode;
127+
}
128+
129+
/** TODO: Add a print method that overrides List61B's inefficient print method. */
130+
131+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lec16_inheritance4;
2+
3+
/** SList with additional operation printLostItems() which prints all items
4+
* that have ever been deleted. */
5+
public class VengefulSLList<Item> extends SLList<Item> {
6+
SLList<Item> deletedItems;
7+
8+
public static void main(String[] args) {
9+
VengefulSLList<Integer> vs1 = new VengefulSLList<>();
10+
vs1.addLast(1);
11+
vs1.addLast(5);
12+
vs1.addLast(10);
13+
vs1.addLast(13);
14+
// vs1 is now: [1, 5, 10, 13]
15+
16+
17+
vs1.removeLast();
18+
vs1.removeLast();
19+
// After deletion, vs1 is: [1, 5]
20+
21+
// Should print out the numbers of the fallen, namely 10 and 13.
22+
System.out.print("The fallen are: ");
23+
vs1.printLostItems();
24+
}
25+
}

lec9_inheritance2/doneCode/CollectionsDogDemo.java renamed to lec9_inheritance2/CollectionsDogDemo.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package lec9_inheritance2.doneCode;
1+
package lec9_inheritance2;
22

33
import java.util.ArrayList;
44
import java.util.Collections;
5-
import java.util.Comparator;
65
import java.util.List;
76

87
public class CollectionsDogDemo {

lec9_inheritance2/doneCode/Dog.java renamed to lec9_inheritance2/Dog.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lec9_inheritance2.doneCode;
1+
package lec9_inheritance2;
22

33
import java.util.Comparator;
44

lec9_inheritance2/doneCode/Maximizer.java renamed to lec9_inheritance2/Maximizer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lec9_inheritance2.doneCode;
1+
package lec9_inheritance2;
22

33
public class Maximizer {
44
public static <T extends Comparable<? super T>> T max(T[] items) {

lec9_inheritance2/doneCode/RandomPicker.java renamed to lec9_inheritance2/RandomPicker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lec9_inheritance2.doneCode;
1+
package lec9_inheritance2;
22

33
import java.util.Random;
44

lec9_inheritance2/doneCode/RandomPickerDemo.java renamed to lec9_inheritance2/RandomPickerDemo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lec9_inheritance2.doneCode;
1+
package lec9_inheritance2;
22

33
public class RandomPickerDemo {
44
public static void main(String[] args) {

0 commit comments

Comments
 (0)