From 42a06346c842b46f84e1d44303691dfc998df629 Mon Sep 17 00:00:00 2001 From: blank <blank@caltech.edu> Date: Wed, 18 Jan 2023 16:31:46 -0800 Subject: [PATCH] After lecture 05. --- 00/IntBag.java | 3 +- 05/ArraySet.java | 95 +++++++++++++++++++++++++++++++++++++++++++ 05/ArraySetMain.java | 17 ++++++++ 05/ArraySetTests.java | 46 +++++++++++++++++++++ 05/ISet.java | 13 ++++++ 05/Person.java | 22 ++++++++++ 6 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 05/ArraySet.java create mode 100644 05/ArraySetMain.java create mode 100644 05/ArraySetTests.java create mode 100644 05/ISet.java create mode 100644 05/Person.java diff --git a/00/IntBag.java b/00/IntBag.java index 20012d3..5ea283b 100644 --- a/00/IntBag.java +++ b/00/IntBag.java @@ -48,6 +48,7 @@ public class IntBag { */ public int pick() { Random r = new Random(); - return this.list.get(r.nextInt(min - 1, max)); + //return this.list.get(r.nextInt(min - 1, max)); + return 0; } } diff --git a/05/ArraySet.java b/05/ArraySet.java new file mode 100644 index 0000000..882c3ca --- /dev/null +++ b/05/ArraySet.java @@ -0,0 +1,95 @@ +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 diff --git a/05/ArraySetMain.java b/05/ArraySetMain.java new file mode 100644 index 0000000..420d256 --- /dev/null +++ b/05/ArraySetMain.java @@ -0,0 +1,17 @@ +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); + } + + } +} diff --git a/05/ArraySetTests.java b/05/ArraySetTests.java new file mode 100644 index 0000000..862d695 --- /dev/null +++ b/05/ArraySetTests.java @@ -0,0 +1,46 @@ +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 diff --git a/05/ISet.java b/05/ISet.java new file mode 100644 index 0000000..c9d73ad --- /dev/null +++ b/05/ISet.java @@ -0,0 +1,13 @@ +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(); +} diff --git a/05/Person.java b/05/Person.java new file mode 100644 index 0000000..2b42447 --- /dev/null +++ b/05/Person.java @@ -0,0 +1,22 @@ +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); + } +} -- GitLab