Jamez-s 6 年前
父节点
当前提交
2cf2dfaee9
共有 4 个文件被更改,包括 178 次插入95 次删除
  1. 59
    11
      NumberUtilities.java
  2. 100
    65
      NumberUtilitiesTest.java
  3. 0
    0
      README.TXT
  4. 19
    19
      package.bluej

+ 59
- 11
NumberUtilities.java 查看文件

1
- 
2
-
3
 
1
 
4
 public class NumberUtilities {
2
 public class NumberUtilities {
5
     
3
     
6
     public static String getRange(int start) {
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
     public static String getRange(int start, int stop) {
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
     public static String getRange(int start, int stop, int step) {
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
     public static String getEvenNumbers(int start, int stop) {
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
     public static String getOddNumbers(int start, int stop) {
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
     public static String getExponentiations(int start, int stop, int exponent) {
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 查看文件

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

+ 0
- 0
README.TXT 查看文件


+ 19
- 19
package.bluej 查看文件

8
 dependency3.from=NumberUtilitiesTest
8
 dependency3.from=NumberUtilitiesTest
9
 dependency3.to=NumberUtilities
9
 dependency3.to=NumberUtilities
10
 dependency3.type=UsesDependency
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
 objectbench.height=164
15
 objectbench.height=164
16
 objectbench.width=286
16
 objectbench.width=286
17
 package.divider.horizontal=0.6004098360655737
17
 package.divider.horizontal=0.6004098360655737
18
 package.divider.vertical=0.7560627674750356
18
 package.divider.vertical=0.7560627674750356
19
 package.editor.height=523
19
 package.editor.height=523
20
 package.editor.width=382
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
 package.frame.height=759
23
 package.frame.height=759
24
 package.frame.width=508
24
 package.frame.width=508
25
 package.numDependencies=3
25
 package.numDependencies=3
37
 target1.showInterface=false
37
 target1.showInterface=false
38
 target1.type=UnitTestTargetJunit4
38
 target1.type=UnitTestTargetJunit4
39
 target1.width=110
39
 target1.width=110
40
-target1.x=100
40
+target1.x=90
41
 target1.y=270
41
 target1.y=270
42
 target2.height=50
42
 target2.height=50
43
 target2.name=TriangleUtilitiesTest
43
 target2.name=TriangleUtilitiesTest
52
 target3.showInterface=false
52
 target3.showInterface=false
53
 target3.type=ClassTarget
53
 target3.type=ClassTarget
54
 target3.width=110
54
 target3.width=110
55
-target3.x=70
55
+target3.x=60
56
 target3.y=300
56
 target3.y=300
57
 target4.association=NumberUtilitiesTest
57
 target4.association=NumberUtilitiesTest
58
 target4.height=50
58
 target4.height=50
60
 target4.showInterface=false
60
 target4.showInterface=false
61
 target4.type=ClassTarget
61
 target4.type=ClassTarget
62
 target4.width=120
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
 target5.height=50
66
 target5.height=50
66
-target5.name=NumberUtilitiesTest
67
+target5.name=TriangleUtilities
67
 target5.showInterface=false
68
 target5.showInterface=false
68
-target5.type=UnitTestTargetJunit4
69
+target5.type=ClassTarget
69
 target5.width=120
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
 target6.height=50
73
 target6.height=50
74
-target6.name=TriangleUtilities
74
+target6.name=NumberUtilitiesTest
75
 target6.showInterface=false
75
 target6.showInterface=false
76
-target6.type=ClassTarget
76
+target6.type=UnitTestTargetJunit4
77
 target6.width=120
77
 target6.width=120
78
-target6.x=70
79
-target6.y=190
78
+target6.x=140
79
+target6.y=50