Selaa lähdekoodia

Working on the MySet

Trinh Tong 6 vuotta sitten
vanhempi
commit
10986d61a0
3 muutettua tiedostoa jossa 56 lisäystä ja 5 poistoa
  1. 7
    2
      src/main/java/MyArrayList.java
  2. 24
    1
      src/main/java/MySet.java
  3. 25
    2
      src/test/java/MySetTest.java

+ 7
- 2
src/main/java/MyArrayList.java Näytä tiedosto

@@ -17,6 +17,10 @@ public class MyArrayList<T> {
17 17
         this.size = size;
18 18
     }
19 19
 
20
+    public Object[] getMyArrayList() {
21
+        return myArrayList;
22
+    }
23
+
20 24
     public void add(T data) {
21 25
 
22 26
         Object[] tempArray1 = new Object[getSize() + 1];
@@ -89,7 +93,6 @@ public class MyArrayList<T> {
89 93
         decreaseSize();
90 94
     }
91 95
 
92
-
93 96
     public void set(int i, T data) throws ArrayIndexOutOfBoundsException {
94 97
         try {
95 98
             this.myArrayList[i] = data;
@@ -98,7 +101,6 @@ public class MyArrayList<T> {
98 101
         }
99 102
     }
100 103
 
101
-
102 104
     public void clear() {
103 105
         Object[] tempArr = new Object[INITIAL_SIZE];
104 106
         resetSize();
@@ -131,4 +133,7 @@ public class MyArrayList<T> {
131 133
         return count;
132 134
     }
133 135
 
136
+    public void setMyArrayList(Object[] myArrayList) {
137
+        this.myArrayList = myArrayList;
138
+    }
134 139
 }

+ 24
- 1
src/main/java/MySet.java Näytä tiedosto

@@ -1,7 +1,30 @@
1
-public class MySet<T> extends MyArrayList {
1
+public class MySet<T> extends MyArrayList<T> {
2 2
 
3 3
     public MySet() {
4
+
4 5
     }
5 6
 
6 7
 
8
+    @Override
9
+    public void add(T data){
10
+        // Sets cannot have duplicates
11
+        boolean hasItem = false;
12
+
13
+            for (Object o: getMyArrayList()) {
14
+                if (o.equals(data)) {
15
+                    hasItem = true;
16
+                    break;
17
+                }
18
+            }
19
+
20
+            if (!hasItem) {
21
+            Object[] tempArray1 = new Object[getSize() + 1];
22
+            copy(tempArray1, getMyArrayList());
23
+            tempArray1[getSize()] = data;
24
+            setMyArrayList(tempArray1);
25
+            setSize(getSize() + 1);
26
+
27
+
28
+        }
29
+    }
7 30
 }

+ 25
- 2
src/test/java/MySetTest.java Näytä tiedosto

@@ -1,11 +1,34 @@
1
+import org.junit.Assert;
1 2
 import org.junit.Before;
3
+import org.junit.Test;
2 4
 
3 5
 public class MySetTest {
4 6
 
5
-    MySet<Integer> myset;
7
+    MySet<Integer> mySet;
6 8
 
7 9
     @Before
8 10
     public void setUp() {
9
-        myset = new MySet<>();
11
+        mySet = new MySet<>();
12
+
13
+    }
14
+
15
+    @Test
16
+    public void testAddUnique() {
17
+        int expectedOutput = 1;
18
+        mySet.add(1);
19
+        Assert.assertEquals(expectedOutput, mySet.getMyArrayList()[mySet.getSize()-1]);
20
+    }
21
+
22
+    @Test
23
+    public void testAddNotUnique() {
24
+
25
+        int expectedSize = 2;
26
+        mySet.add(1);
27
+        mySet.add(2);
28
+        mySet.add(1);
29
+
30
+        Assert.assertEquals(expectedSize, mySet.getSize());
10 31
     }
32
+
33
+    
11 34
 }