Browse Source

myset is basically done

Jennifer Chao 6 years ago
parent
commit
f0811ca8fa
3 changed files with 284 additions and 1 deletions
  1. 4
    0
      src/main/java/MyArrayList.java
  2. 102
    1
      src/main/java/MySet.java
  3. 178
    0
      src/test/java/MySetTest.java

+ 4
- 0
src/main/java/MyArrayList.java View File

@@ -97,6 +97,10 @@ public class MyArrayList<T> {
97 97
         return false;
98 98
     }
99 99
 
100
+    public T[] toArray(){
101
+        return getOriginalArray();
102
+    }
103
+
100 104
 //    public int indexOf(T value) {
101 105
 //        return 0;
102 106
 //    }

+ 102
- 1
src/main/java/MySet.java View File

@@ -1,2 +1,103 @@
1
-public class MySet {
1
+//MySet must be a generic type, which can store any other type of object
2
+//An instance of MySet must contain no duplicates
3
+//MySet must not use List, ArrayList, or any other built-in collection types in its implementation. You may use Arrays, and you may use your implementation of MyArrayList (if it helps -- you don't have to use it)
4
+//MySet must have all of the methods listed on the Java Set documentation
5
+
6
+import java.lang.reflect.Array;
7
+import java.util.Collection;
8
+import java.util.Set;
9
+
10
+public class MySet<T> {
11
+
12
+    private T[] originalArray;
13
+    private int size;
14
+
15
+    public MySet() {
16
+        this.size = 0;
17
+        this.originalArray = (T[]) new Object[0];
18
+    }
19
+
20
+    public void add(T value) {
21
+        if (!contains(value)) {
22
+            T[] newArray = (T[]) Array.newInstance(originalArray.getClass().getComponentType(), originalArray.length + 1);
23
+
24
+            for (int i = 0; i < originalArray.length; i++) {
25
+                newArray[i] = originalArray[i];
26
+            }
27
+
28
+            newArray[newArray.length - 1] = value;
29
+            setOriginalArray(newArray);
30
+            setSize(newArray.length);
31
+        }
32
+    }
33
+
34
+    public void addAll(Collection<? extends T> collection) {
35
+        for (T element : collection) {
36
+            if (!contains(element)) {
37
+                this.add(element);
38
+            }
39
+        }
40
+    }
41
+
42
+    public void clear() {
43
+        setOriginalArray((T[]) Array.newInstance(originalArray.getClass().getComponentType(), 0));
44
+        setSize(0);
45
+    }
46
+
47
+    public boolean contains(T value) {
48
+        for (T element : originalArray) {
49
+            if (element == value) {
50
+                return true;
51
+            }
52
+        }
53
+        return false;
54
+    }
55
+
56
+    public boolean containsAll(Collection<? extends T> collection) {
57
+        boolean[] allTrue = new boolean[collection.size()];
58
+        int j = 0;
59
+
60
+        for (T element : collection) {
61
+            for (T value : originalArray) {
62
+                if (element == value) {
63
+                    allTrue[j] = true;
64
+                    j++;
65
+                }
66
+            }
67
+        }
68
+
69
+        for (boolean result : allTrue) {
70
+            if (!result) {
71
+                return false;
72
+            }
73
+        }
74
+        return true;
75
+    }
76
+
77
+    public boolean isEmpty() {
78
+        if (size() == 0) {
79
+            return true;
80
+        }
81
+        return false;
82
+    }
83
+
84
+    public T get(int index) {
85
+        return originalArray[index];
86
+    }
87
+
88
+    public T[] getOriginalArray() {
89
+        return originalArray;
90
+    }
91
+
92
+    public void setOriginalArray(T[] originalArray) {
93
+        this.originalArray = originalArray;
94
+    }
95
+
96
+    public int size() {
97
+        return size;
98
+    }
99
+
100
+    public void setSize(int size) {
101
+        this.size = size;
102
+    }
2 103
 }

+ 178
- 0
src/test/java/MySetTest.java View File

@@ -0,0 +1,178 @@
1
+import org.junit.Assert;
2
+import org.junit.Test;
3
+
4
+import java.util.ArrayList;
5
+
6
+public class MySetTest {
7
+
8
+    MySet<Integer> testSet = new MySet<>();
9
+
10
+    public void populateSet() {
11
+        testSet.add(10);
12
+        testSet.add(3);
13
+        testSet.add(9);
14
+        testSet.add(2);
15
+    }
16
+
17
+    @Test
18
+    public void testAdd() {
19
+        testSet.add(10);
20
+
21
+        Integer expected = 10;
22
+        Integer actual = testSet.get(0);
23
+
24
+        Assert.assertEquals(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void testAdd_Multiple() {
29
+        populateSet();
30
+
31
+        Integer expected = 2;
32
+        Integer actual = testSet.get(3);
33
+
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test(expected = IndexOutOfBoundsException.class)
38
+    public void testAdd_IfDuplicate() {
39
+        testSet.add(10);
40
+        testSet.add(10);
41
+
42
+        testSet.get(2);
43
+    }
44
+
45
+    @Test
46
+    public void testAdd_IfDuplicate_Size() {
47
+        populateSet();
48
+        testSet.add(9);
49
+
50
+        int expected = 4;
51
+        int actual = testSet.size();
52
+
53
+        Assert.assertEquals(expected, actual);
54
+    }
55
+
56
+    @Test
57
+    public void testAddAll_NoDuplicates() {
58
+        populateSet();
59
+        ArrayList<Integer> addArray = new ArrayList<>();
60
+        addArray.add(5);
61
+        addArray.add(7);
62
+        addArray.add(8);
63
+
64
+        testSet.addAll(addArray);
65
+
66
+        int expected = 7;
67
+        int actual = testSet.size();
68
+
69
+        Assert.assertEquals(expected, actual);
70
+    }
71
+
72
+    @Test
73
+    public void testAddAll_OneDuplicate() {
74
+        populateSet();
75
+        ArrayList<Integer> addArray = new ArrayList<>();
76
+        addArray.add(5);
77
+        addArray.add(7);
78
+        addArray.add(10);
79
+
80
+        testSet.addAll(addArray);
81
+
82
+        int expected = 6;
83
+        int actual = testSet.size();
84
+
85
+        Assert.assertEquals(expected, actual);
86
+    }
87
+
88
+    @Test
89
+    public void testAddAll_AllDuplicates() {
90
+        populateSet();
91
+        ArrayList<Integer> addArray = new ArrayList<>();
92
+        addArray.add(9);
93
+        addArray.add(3);
94
+        addArray.add(10);
95
+
96
+        testSet.addAll(addArray);
97
+
98
+        int expected = 4;
99
+        int actual = testSet.size();
100
+
101
+        Assert.assertEquals(expected, actual);
102
+    }
103
+
104
+    @Test
105
+    public void testClear(){
106
+        populateSet();
107
+        int expected = 4;
108
+        int actual = testSet.size();
109
+
110
+        Assert.assertEquals(expected, actual);
111
+
112
+        testSet.clear();
113
+        int newExpected = 0;
114
+        int newActual = testSet.size();
115
+
116
+        Assert.assertEquals(newExpected, newActual);
117
+    }
118
+
119
+    @Test
120
+    public void testContains_EmptySet(){
121
+        Assert.assertFalse(testSet.contains(10));
122
+    }
123
+
124
+    @Test
125
+    public void testContains_True(){
126
+        populateSet();
127
+
128
+        Assert.assertTrue(testSet.contains(10));
129
+    }
130
+
131
+    @Test
132
+    public void testContains_False(){
133
+        populateSet();
134
+
135
+        Assert.assertFalse(testSet.contains(7));
136
+    }
137
+
138
+    @Test
139
+    public void testContainsAll_False() {
140
+        populateSet();
141
+        ArrayList<Integer> newArray = new ArrayList<>();
142
+        newArray.add(2);
143
+        newArray.add(3);
144
+        newArray.add(7);
145
+
146
+        Assert.assertFalse(testSet.containsAll(newArray));
147
+    }
148
+
149
+    @Test
150
+    public void testContainsAll_True() {
151
+        populateSet();
152
+        ArrayList<Integer> newArray = new ArrayList<>();
153
+        newArray.add(9);
154
+        newArray.add(3);
155
+        newArray.add(10);
156
+
157
+        Assert.assertTrue(testSet.containsAll(newArray));
158
+    }
159
+
160
+    @Test
161
+    public void testIsEmpty_True(){
162
+        Assert.assertTrue(testSet.isEmpty());
163
+    }
164
+
165
+    @Test
166
+    public void testIsEmpty_False(){
167
+        populateSet();
168
+
169
+        Assert.assertFalse(testSet.isEmpty());
170
+    }
171
+
172
+    @Test
173
+    public void testEquals(){
174
+        MySet<Object> object = new MySet<>();
175
+        testSet.equals(object);
176
+    }
177
+
178
+}