Commit 42a06346 authored by Adam Blank's avatar Adam Blank
Browse files

After lecture 05.

parent 6f67538d
Showing with 195 additions and 1 deletion
+195 -1
...@@ -48,6 +48,7 @@ public class IntBag { ...@@ -48,6 +48,7 @@ public class IntBag {
*/ */
public int pick() { public int pick() {
Random r = new Random(); Random r = new Random();
return this.list.get(r.nextInt(min - 1, max)); //return this.list.get(r.nextInt(min - 1, max));
return 0;
} }
} }
import java.util.Iterator;
public class ArraySet<E> implements ISet<E> {
private static final int DEFAULT_CAPACITY = 10;
private static final double GROW_FACTOR = 2.0;
private E[] data;
private int size;
public ArraySet() {
this.data = (E[])new Object[DEFAULT_CAPACITY];
}
private void ensureCapacity(int size) {
if (this.capacity() < size) {
E[] newData = (E[])new Object[(int)(this.capacity()*GROW_FACTOR)];
for (int i = 0; i < this.size; i++) {
newData[i] = this.data[i];
}
this.data = newData;
}
}
private int capacity() {
return this.data.length;
}
public int size() {
return this.size;
}
@Override
public void add(E elem) {
if (!this.contains(elem)) {
this.ensureCapacity(this.size + 1);
this.data[this.size] = elem;
this.size++;
}
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public Iterator<E> iterator() {
return new ArraySetIterator();
}
/*
[1, 2, 3]
idx=0, next() => 1
idx=1
hasNext() 1 < 3 true
idx=1, next() => 2
hasNext() 2 < 3 true
*/
private class ArraySetIterator implements Iterator<E> {
private int idx;
public ArraySetIterator() {
this.idx = 0;
}
@Override
public boolean hasNext() {
return this.idx < size();
}
@Override
public E next() {
E toReturn = data[this.idx];
this.idx++;
return toReturn;
}
}
@Override
public String toString() {
if (this.isEmpty()) {
return "[]";
}
String result = "[";
for (int i = 0; i < this.size; i++) {
result += this.data[i] + ", ";
}
result = result.substring(0, result.length() - 2);
return result + "]";
}
}
\ No newline at end of file
import java.util.Iterator;
public class ArraySetMain {
public static void main(String[] args) {
ISet<Integer> set = new ArraySet<>();
set.add(1);
set.add(0);
System.out.println(set.contains(0));
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()) {
Integer i = iterator.next();
System.out.println(i);
}
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ArraySetTests {
@Test
public void simpleSizeTest() {
ISet<Integer> set = new ArraySet<>();
set.add(0);
set.add(0);
set.add(0);
set.add(0);
set.add(0);
assertEquals(1, set.size(), "repeatedly adding the same element results in it only being added once");
}
@Test
public void simpleToStringTest() {
ISet<Integer> set = new ArraySet<>();
assertEquals("[]", set.toString(), "adding consecutive values results in the right string output");
set.add(0);
assertEquals("[0]", set.toString(), "adding consecutive values results in the right string output");
set.add(1);
assertEquals("[0, 1]", set.toString(), "adding consecutive values results in the right string output");
set.add(2);
assertEquals("[0, 1, 2]", set.toString(), "adding consecutive values results in the right string output");
set.add(3);
assertEquals("[0, 1, 2, 3]", set.toString(), "adding consecutive values results in the right string output");
}
@Test
public void peopleTest() {
ISet<Person> set = new ArraySet<>();
assertEquals("[]", set.toString(), "adding person values results in the right string output");
Person p = new Person("Adam");
set.add(p);
assertEquals("[Adam]", set.toString(), "adding person values results in the right string output");
set.add(p);
assertEquals("[Adam]", set.toString(), "adding person values results in the right string output");
}
}
\ No newline at end of file
public interface ISet<E> extends Iterable<E> {
public int size();
public default boolean contains(E elem) {
for (E x : this) {
if (x.equals(elem)) {
return true;
}
}
return false;
}
public void add(E elem);
public boolean isEmpty();
}
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String toString() {
return this.getName();
}
public boolean equals(Object o) {
if (!(o instanceof Person)) {
return false;
}
return ((Person)o).name.equals(this.name);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment