Jamez-s 6 lat temu
rodzic
commit
2cf2dfaee9
4 zmienionych plików z 178 dodań i 95 usunięć
  1. 59
    11
      NumberUtilities.java
  2. 100
    65
      NumberUtilitiesTest.java
  3. 0
    0
      README.TXT
  4. 19
    19
      package.bluej

+ 59
- 11
NumberUtilities.java Wyświetl plik

@@ -1,32 +1,80 @@
1
- 
2
-
3 1
 
4 2
 public class NumberUtilities {
5 3
     
6 4
     public static String getRange(int start) {
7
-        return null;
8
-    }
5
+        String range = "";
6
+        for (int i = 0; i < start; i++) {
7
+            range += i;
8
+        }
9
+        return range;
10
+        }
9 11
     
10 12
     public static String getRange(int start, int stop) {
11
-        return null;
13
+        
14
+        String range = "";    
15
+        for (int i = start; i < stop; i++) {
16
+         range += i;
17
+        }
18
+        
19
+        return range;
12 20
     }
13
-
14
-
21
+    
15 22
     public static String getRange(int start, int stop, int step) {
16
-        return null;
23
+        String range = ""; 
24
+        
25
+        for (int i = start; i < stop; i += step) {
26
+            range += i;
27
+        }
28
+        return range;
17 29
     }
18 30
     
19 31
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
32
+         String evenNumbers = "";
33
+        
34
+        if(start % 2 == 0) {
35
+            for(int i = start; i < stop; i += 2){
36
+                evenNumbers += i;            
37
+            }
38
+        }else if(start % 2 == 1){
39
+            for(int i = start + 1; i < stop; i += 2){
40
+                evenNumbers += i;
41
+            }            
42
+        }
43
+        return evenNumbers;
21 44
     }
22 45
 
23 46
 
24 47
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
48
+        
49
+        String oddNumbers = "";
50
+
51
+        if (start % 2 == 1){
52
+            for(int i = start; i < stop; i += 2) {
53
+                oddNumbers += i;
54
+            }
55
+        } else if (start % 2 == 0){
56
+            for(int i = start + 1; i < stop; i += 2){
57
+                oddNumbers += i;
58
+            }
59
+        }
60
+        return oddNumbers;
26 61
     }
27 62
 
28 63
 
29 64
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
65
+        
66
+        String expon = "";
67
+
68
+        if (exponent == 2){
69
+            for (int i = start; i < stop + 1; i++){
70
+                expon += i * i;
71
+            }
72
+        } else if (exponent == 3){
73
+            for (int i = start; i < stop + 1; i++){
74
+                
75
+                expon += i * i * i;
76
+            }
77
+        }
78
+        return expon; 
31 79
     }
32 80
 }

+ 100
- 65
NumberUtilitiesTest.java Wyświetl plik

@@ -4,12 +4,11 @@ import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
6 6
 public class NumberUtilitiesTest {
7
-    
8 7
     @Test
9
-    public void testGetRangeForSmallRange() {
8
+    public void testGetRange1A() {
10 9
         // : Given
11
-        String expected = "012";
12
-        int stop = 3;
10
+        String expected = "0123456789";
11
+        int stop = 11;
13 12
 
14 13
         // : When
15 14
         String actual = NumberUtilities.getRange(stop);
@@ -17,13 +16,12 @@ public class NumberUtilitiesTest {
17 16
         // : Then
18 17
         Assert.assertEquals(expected, actual);
19 18
     }
20
-    
21
-    
19
+
22 20
     @Test
23
-    public void testGetRangeToTwoDigits() {
21
+    public void testGetRange2A() {
24 22
         // : Given
25
-        String expected = "01234567891011";
26
-        int stop = 12;
23
+        String expected = "01234";
24
+        int stop = 5;
27 25
 
28 26
         // : When
29 27
         String actual = NumberUtilities.getRange(stop);
@@ -33,25 +31,35 @@ public class NumberUtilitiesTest {
33 31
     }
34 32
 
35 33
     @Test
36
-    public void testGetRangeWithAStartNumber() {
34
+    public void testGetRange3A() {
37 35
         // : Given
38
-        String expected = "5678910111213141516171819";
39
-        int start = 5;
36
+        String expected = "012345678910111213141516171819";
40 37
         int stop = 20;
41 38
 
42 39
         // : When
43
-        String actual = NumberUtilities.getRange(start, stop);
40
+        String actual = NumberUtilities.getRange(stop);
44 41
 
45 42
         // : Then
46 43
         Assert.assertEquals(expected, actual);
47 44
     }
48 45
 
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
49 57
     @Test
50
-    public void testGetRangeStartAt100() {
58
+    public void testGetRange1B() {
51 59
         // : Given
52
-        String expected = "100101102103104105106107108109";
53
-        int start = 100;
54
-        int stop = 110;
60
+        String expected = "5678910111213141516171819";
61
+        int start = 5;
62
+        int stop = 20;
55 63
 
56 64
         // : When
57 65
         String actual = NumberUtilities.getRange(start, stop);
@@ -60,17 +68,16 @@ public class NumberUtilitiesTest {
60 68
         Assert.assertEquals(expected, actual);
61 69
     }
62 70
 
63
-    
71
+
64 72
     @Test
65
-    public void testGetRangeWithOneStep() {
73
+    public void testGetRange2B() {
66 74
         // : Given
67
-        String expected = "012345678910111213141516171819";
68
-        int start = 0;
69
-        int stop = 20;
70
-        int step = 1;
75
+        String expected = "101112131415161718192021222324";
76
+        int start = 10;
77
+        int stop = 25;
71 78
 
72 79
         // : When
73
-        String actual = NumberUtilities.getRange(start, stop, step);
80
+        String actual = NumberUtilities.getRange(start, stop);
74 81
 
75 82
         // : Then
76 83
         Assert.assertEquals(expected, actual);
@@ -78,22 +85,33 @@ public class NumberUtilitiesTest {
78 85
 
79 86
 
80 87
     @Test
81
-    public void testGetRangeWithTwoStep() {
88
+    public void testGetRange3B() {
82 89
         // : Given
83
-        String expected = "0246810";
84
-        int start = 0;
85
-        int stop = 11;
86
-        int step = 2;
90
+        String expected = "100101103104105106107108109";
91
+        int start = 100;
92
+        int stop = 110;
87 93
 
88 94
         // : When
89
-        String actual = NumberUtilities.getRange(start, stop, step);
95
+        String actual = NumberUtilities.getRange(start, stop);
90 96
 
91 97
         // : Then
92 98
         Assert.assertEquals(expected, actual);
93 99
     }
94 100
 
95
-        @Test
96
-    public void testGetRangeWithFiveStep() {
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+    @Test
114
+    public void testGetRange1C() {
97 115
         // : Given
98 116
         String expected = "51015";
99 117
         int start = 5;
@@ -107,56 +125,72 @@ public class NumberUtilitiesTest {
107 125
         Assert.assertEquals(expected, actual);
108 126
     }
109 127
 
110
-    
128
+
111 129
     @Test
112
-    public void testGetEvenNumbersStartAndEndAtEven() {
130
+    public void testGetRange2C() {
113 131
         // : Given
114
-        String expected = "4681012141618";
115
-        int start = 4;
132
+        String expected = "012345678910111213141516171819";
133
+        int start = 0;
116 134
         int stop = 20;
135
+        int step = 1;
117 136
 
118 137
         // : When
119
-        String actual = NumberUtilities.getEvenNumbers(start, stop);
138
+        String actual = NumberUtilities.getRange(start, stop, step);
120 139
 
121 140
         // : Then
122 141
         Assert.assertEquals(expected, actual);
123 142
     }
124 143
 
125
-    
144
+
126 145
     @Test
127
-    public void testGetEvenNumbersStartAndEndAtOdd() {
146
+    public void testGetRange3C() {
128 147
         // : Given
129
-        String expected = "68101214161820";
130
-        int start = 5;
131
-        int stop = 21;
148
+        String expected = "0246810";
149
+        int start = 0;
150
+        int stop = 11;
151
+        int step = 2;
132 152
 
133 153
         // : When
134
-        String actual = NumberUtilities.getEvenNumbers(start, stop);
154
+        String actual = NumberUtilities.getRange(start, stop, step);
135 155
 
136 156
         // : Then
137 157
         Assert.assertEquals(expected, actual);
138 158
     }
139 159
 
140
-    
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
141 173
     @Test
142
-    public void testGetOddNumbersStartAndEndAtOdd() {
174
+    public void testGetEvenNumbers() {
143 175
         // : Given
144 176
         String expected = "5791113151719";
145 177
         int start = 5;
146
-        int stop = 21;
178
+        int stop = 20;
147 179
 
148 180
         // : When
149
-        String actual = NumberUtilities.getOddNumbers(start, stop);
181
+        String actual = NumberUtilities.getEvenNumbers(start, stop);
150 182
 
151 183
         // : Then
152 184
         Assert.assertEquals(expected, actual);
153 185
     }
154 186
 
155
-    public void testGetOddNumbersStartAndEndAtEven() {
187
+    @Test
188
+    public void testGetOddNumbers() {
156 189
         // : Given
157
-        String expected = "579111315";
158
-        int start = 4;
159
-        int stop = 16;
190
+        String expected = "681012141618";
191
+        int start = 5;
192
+        int stop = 20;
193
+        int step = 5;
160 194
 
161 195
         // : When
162 196
         String actual = NumberUtilities.getOddNumbers(start, stop);
@@ -166,34 +200,35 @@ public class NumberUtilitiesTest {
166 200
     }
167 201
 
168 202
 
203
+
169 204
     @Test
170
-    public void testGetExponentiationNumbersForSquare() {
205
+    public void testGetExponentsNumbersForSquare() {
171 206
         // : Given
172
-        String expected = "4916253649";
173
-        int start = 2;
174
-        int stop = 7;
175
-        int exponent = 2;
207
+        String expected = "25100225";
208
+        int start = 5;
209
+        int stop = 20;
210
+        int step = 5;
176 211
 
177 212
         // : When
178
-        String actual = NumberUtilities.getExponentiations(start, stop, exponent);
213
+        String actual = NumberUtilities.getExponentiations(start, stop, step);
179 214
 
180 215
         // : Then
181 216
         Assert.assertEquals(expected, actual);
182 217
     }
183
-    
184
-    
218
+
219
+
185 220
     @Test
186 221
     public void testGetExponentiationNumbersForCube() {
187 222
         // : Given
188
-        String expected = "182764";
189
-        int start = 1;
190
-        int stop = 4;
191
-        int exponent = 3;
223
+        String expected = "25100225";
224
+        int start = 5;
225
+        int stop = 20;
226
+        int step = 5;
227
+        int exponent = 2;
192 228
 
193 229
         // : When
194 230
         String actual = NumberUtilities.getExponentiations(start, stop, exponent);
195 231
 
196
-        // : Then
197 232
         Assert.assertEquals(expected, actual);
198 233
     }
199
-}
234
+}

+ 0
- 0
README.TXT Wyświetl plik


+ 19
- 19
package.bluej Wyświetl plik

@@ -8,18 +8,18 @@ dependency2.type=UsesDependency
8 8
 dependency3.from=NumberUtilitiesTest
9 9
 dependency3.to=NumberUtilities
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=0
12
-editor.fx.0.width=0
13
-editor.fx.0.x=0
14
-editor.fx.0.y=0
11
+editor.fx.0.height=722
12
+editor.fx.0.width=800
13
+editor.fx.0.x=192
14
+editor.fx.0.y=322
15 15
 objectbench.height=164
16 16
 objectbench.width=286
17 17
 package.divider.horizontal=0.6004098360655737
18 18
 package.divider.vertical=0.7560627674750356
19 19
 package.editor.height=523
20 20
 package.editor.width=382
21
-package.editor.x=20
22
-package.editor.y=57
21
+package.editor.x=106
22
+package.editor.y=84
23 23
 package.frame.height=759
24 24
 package.frame.width=508
25 25
 package.numDependencies=3
@@ -37,7 +37,7 @@ target1.name=TableUtilitiesTest
37 37
 target1.showInterface=false
38 38
 target1.type=UnitTestTargetJunit4
39 39
 target1.width=110
40
-target1.x=100
40
+target1.x=90
41 41
 target1.y=270
42 42
 target2.height=50
43 43
 target2.name=TriangleUtilitiesTest
@@ -52,7 +52,7 @@ target3.name=TableUtilities
52 52
 target3.showInterface=false
53 53
 target3.type=ClassTarget
54 54
 target3.width=110
55
-target3.x=70
55
+target3.x=60
56 56
 target3.y=300
57 57
 target4.association=NumberUtilitiesTest
58 58
 target4.height=50
@@ -60,20 +60,20 @@ target4.name=NumberUtilities
60 60
 target4.showInterface=false
61 61
 target4.type=ClassTarget
62 62
 target4.width=120
63
-target4.x=80
64
-target4.y=70
63
+target4.x=110
64
+target4.y=80
65
+target5.association=TriangleUtilitiesTest
65 66
 target5.height=50
66
-target5.name=NumberUtilitiesTest
67
+target5.name=TriangleUtilities
67 68
 target5.showInterface=false
68
-target5.type=UnitTestTargetJunit4
69
+target5.type=ClassTarget
69 70
 target5.width=120
70
-target5.x=110
71
-target5.y=40
72
-target6.association=TriangleUtilitiesTest
71
+target5.x=70
72
+target5.y=190
73 73
 target6.height=50
74
-target6.name=TriangleUtilities
74
+target6.name=NumberUtilitiesTest
75 75
 target6.showInterface=false
76
-target6.type=ClassTarget
76
+target6.type=UnitTestTargetJunit4
77 77
 target6.width=120
78
-target6.x=70
79
-target6.y=190
78
+target6.x=140
79
+target6.y=50