Explorar el Código

finished and refactored :D

Nicholas Maidanos hace 6 años
padre
commit
099a995d4b
Se han modificado 4 ficheros con 59 adiciones y 138 borrados
  1. 30
    34
      DuplicateDeleter.java
  2. 11
    51
      IntegerDuplicateDeleter.java
  3. 12
    47
      StringDuplicateDeleter.java
  4. 6
    6
      package.bluej

+ 30
- 34
DuplicateDeleter.java Ver fichero

@@ -34,7 +34,7 @@ public abstract class DuplicateDeleter<T> {
34 34
      */
35 35
     abstract public T[] removeDuplicatesExactly(int exactNumberOfDuplications);
36 36
     
37
-    protected int numOfDupUnder(int num) {
37
+    protected int numOfArray(int num, String type) {
38 38
         
39 39
         int numToReturn = 0;
40 40
         int dup = 0;
@@ -46,9 +46,16 @@ public abstract class DuplicateDeleter<T> {
46 46
                 }
47 47
             }
48 48
             
49
-            if(dup < num){
50
-                numToReturn++;
49
+            if(type.equals("max")){
50
+                if(dup < num){
51
+                    numToReturn++;
52
+                }                
53
+            } else if(type.equals("exact")){
54
+               if(dup != num){
55
+                    numToReturn++;
56
+                }  
51 57
             }
58
+
52 59
             dup = 0;
53 60
         }
54 61
         
@@ -56,48 +63,37 @@ public abstract class DuplicateDeleter<T> {
56 63
         
57 64
     }
58 65
     
59
-    protected int numOfExactDup(int exact) {
66
+    protected T[] createArray(T[] array, int dupNum, String type){
60 67
         
61
-        int numToReturn = 0;
62 68
         int dup = 0;
63
-       
69
+        int indexOf = 0;
70
+        
71
+        
64 72
         for(int i = 0; i < this.array.length; i++) {    
65 73
             for(int j = 0; j < this.array.length; j++){
66
-                if(this.array[i].equals(this.array[j])){
74
+                if(this.array[i] == this.array[j]){
67 75
                     dup++;
68 76
                 }
69 77
             }
70 78
             
71
-            if(dup != exact){
72
-                numToReturn++;
79
+            
80
+            if(type.equals("exact")){
81
+                if(dup != dupNum){
82
+                    array[indexOf] = this.array[i];
83
+                    indexOf++;
84
+                }            
85
+            } else if(type.equals("max")) {
86
+                if(dup < dupNum){
87
+                    array[indexOf] = this.array[i];
88
+                    indexOf++;
89
+                }     
73 90
             }
91
+            
92
+
74 93
             dup = 0;
75 94
         }
76
-        
77
-        return numToReturn;
95
+        this.array = array;    
96
+        return array;
78 97
     }
79 98
     
80
-    // protected T[] createArray(int size, String type){
81
-        // T[] newArray = new Integer[size];
82
-        
83
-        // int dup = 0;
84
-        // int indexOf = 0;
85
-        
86
-        
87
-        // for(int i = 0; i < this.array.length; i++) {    
88
-            // for(int j = 0; j < this.array.length; j++){
89
-                // if(this.array[i] == this.array[j]){
90
-                    // dup++;
91
-                // }
92
-            // }
93
-            
94
-            // if(dup != exactNumberOfDuplications){
95
-                // newArray[indexOf] = this.array[i];
96
-                // indexOf++;
97
-            // }
98
-            // dup = 0;
99
-        // }
100
-        // this.array = newArray;    
101
-    // }
102
-    
103 99
 }

+ 11
- 51
IntegerDuplicateDeleter.java Ver fichero

@@ -10,62 +10,22 @@ public class IntegerDuplicateDeleter extends DuplicateDeleter<Integer>{
10 10
     
11 11
     @Override
12 12
     public Integer[] removeDuplicates(int maxNumberOfDuplications){
13
-        //Arrays.sort(this.array);
14
-        
15
-        int sizeOfArray = this.numOfDupUnder(maxNumberOfDuplications);
16
-        Integer[] newArray = new Integer[sizeOfArray];
17
-        
18
-        
19
-        int dup = 0;
20
-        int indexOf = 0;        
21
-        
22
-        for(int i = 0; i < this.array.length; i++) {    
23
-            for(int j = 0; j < this.array.length; j++){
24
-                if(this.array[i] == this.array[j]){
25
-                    dup++;
26
-                }
27
-            }
28
-            
29
-            if(dup < maxNumberOfDuplications){
30
-                newArray[indexOf] = this.array[i];
31
-                indexOf++;
32
-            }
33
-            dup = 0;
34
-        }
35
-        this.array = newArray;
36
-        return this.array;        
13
+
14
+        int sizeOfArray = this.numOfArray(maxNumberOfDuplications, "max");
15
+        return this.createArray(new Integer[sizeOfArray],
16
+                                    maxNumberOfDuplications,
17
+                                    "max");     
18
+                                    
37 19
     };
38 20
     
39 21
     @Override
40 22
     public Integer[] removeDuplicatesExactly(int exactNumberOfDuplications){
41 23
         
42
-        //Arrays.sort(this.array);
43
-        
44
-        int sizeOfArray = this.numOfExactDup(exactNumberOfDuplications);
45
-        
46
-        
47
-        Integer[] newArray = new Integer[sizeOfArray];
48
-        
49
-        int dup = 0;
50
-        int indexOf = 0;
51
-        
52
-        
53
-        for(int i = 0; i < this.array.length; i++) {    
54
-            for(int j = 0; j < this.array.length; j++){
55
-                if(this.array[i] == this.array[j]){
56
-                    dup++;
57
-                }
58
-            }
59
-            
60
-            if(dup != exactNumberOfDuplications){
61
-                newArray[indexOf] = this.array[i];
62
-                indexOf++;
63
-            }
64
-            dup = 0;
65
-        }
66
-        this.array = newArray;
67
-        return this.array; 
68
-        
24
+        int sizeOfArray = this.numOfArray(exactNumberOfDuplications, "exact");
25
+        return this.createArray(new Integer[sizeOfArray],
26
+                                    exactNumberOfDuplications,
27
+                                    "exact");
28
+                                    
69 29
     };
70 30
     
71 31
     

+ 12
- 47
StringDuplicateDeleter.java Ver fichero

@@ -9,57 +9,22 @@ public final class StringDuplicateDeleter extends DuplicateDeleter<String> {
9 9
     
10 10
     @Override
11 11
     public String[] removeDuplicates(int maxNumberOfDuplications){
12
-        //Arrays.sort(this.array);
13
-        
14
-        int sizeOfArray = this.numOfDupUnder(maxNumberOfDuplications);
15
-        String[] newArray = new String[sizeOfArray];
16
-        
17
-        int dup = 0;
18
-        int indexOf = 0;        
19
-        
20
-        for(int i = 0; i < this.array.length; i++) {    
21
-            for(int j = 0; j < this.array.length; j++){
22
-                if(this.array[i].equals(this.array[j])){
23
-                    dup++;
24
-                }
25
-            }
26
-            
27
-            if(dup < maxNumberOfDuplications){
28
-                newArray[indexOf] = this.array[i];
29
-                indexOf++;
30
-            }
31
-            dup = 0;
32
-        }
33
-        this.array = newArray;
34
-        return this.array;
12
+
13
+        int sizeOfArray = this.numOfArray(maxNumberOfDuplications, "max");
14
+        return this.createArray(new String[sizeOfArray],
15
+                                    maxNumberOfDuplications,
16
+                                    "max");  
17
+                                    
35 18
     };
36 19
     
37 20
     @Override
38 21
     public String[] removeDuplicatesExactly(int exactNumberOfDuplications){
39
-        //Arrays.sort(this.array);
40
-        
41
-        int sizeOfArray = this.numOfExactDup(exactNumberOfDuplications);
42
-        String[] newArray = new String[sizeOfArray];
43
-        
44
-        int dup = 0;
45
-        int indexOf = 0;
46
-        
47
-        
48
-        for(int i = 0; i < this.array.length; i++) {    
49
-            for(int j = 0; j < this.array.length; j++){
50
-                if(this.array[i].equals(this.array[j])){
51
-                    dup++;
52
-                }
53
-            }
54
-            
55
-            if(dup != exactNumberOfDuplications){
56
-                newArray[indexOf] = this.array[i];
57
-                indexOf++;
58
-            }
59
-            dup = 0;
60
-        }
61
-        this.array = newArray;
62
-        return this.array;         
22
+
23
+        int sizeOfArray = this.numOfArray(exactNumberOfDuplications, "exact");
24
+        return this.createArray(new String[sizeOfArray],
25
+                                    exactNumberOfDuplications,
26
+                                    "exact");   
27
+                                    
63 28
     };
64 29
     
65 30
     

+ 6
- 6
package.bluej Ver fichero

@@ -11,18 +11,18 @@ dependency3.type=UsesDependency
11 11
 dependency4.from=StringDuplicateDeleterTest
12 12
 dependency4.to=StringDuplicateDeleter
13 13
 dependency4.type=UsesDependency
14
-editor.fx.0.height=671
15
-editor.fx.0.width=800
16
-editor.fx.0.x=240
17
-editor.fx.0.y=23
14
+editor.fx.0.height=642
15
+editor.fx.0.width=878
16
+editor.fx.0.x=170
17
+editor.fx.0.y=30
18 18
 objectbench.height=140
19 19
 objectbench.width=565
20 20
 package.divider.horizontal=0.59958071278826
21 21
 package.divider.vertical=0.7601957585644372
22 22
 package.editor.height=459
23 23
 package.editor.width=848
24
-package.editor.x=306
25
-package.editor.y=23
24
+package.editor.x=344
25
+package.editor.y=26
26 26
 package.frame.height=671
27 27
 package.frame.width=974
28 28
 package.numDependencies=4