Przeglądaj źródła

added a bunch of methods to this lab

Margaret Pierse 6 lat temu
rodzic
commit
69d762a571
4 zmienionych plików z 137 dodań i 40 usunięć
  1. 59
    15
      NumberUtilities.java
  2. 26
    13
      NumberUtilitiesTest.java
  3. 28
    3
      TableUtilities.java
  4. 24
    9
      TriangleUtilities.java

+ 59
- 15
NumberUtilities.java Wyświetl plik

@@ -1,32 +1,76 @@
1
- 
2
-
1
+import java.util.*; 
3 2
 
4 3
 public class NumberUtilities {
5
-    
6
-    public static String getRange(int start) {
7
-        return null;
4
+    public static String getRange(int stop) {
5
+        String numRange = "";
6
+        for (int i=0; i<stop; i++) {
7
+            numRange += String.valueOf(i);
8
+        }
9
+        return numRange;
10
+
8 11
     }
9
-    
12
+
10 13
     public static String getRange(int start, int stop) {
11
-        return null;
14
+        String specNumRange = "";
15
+        for (int i=start; i<stop; i++) {
16
+            specNumRange += String.valueOf(i);
17
+        }
18
+        return specNumRange;
12 19
     }
13 20
 
14
-
15 21
     public static String getRange(int start, int stop, int step) {
16
-        return null;
22
+        String stepNumRange = "";
23
+        for (int i=start; i<stop; i+=step) {
24
+            stepNumRange += String.valueOf(i);
25
+        }
26
+        return stepNumRange;
17 27
     }
18
-    
28
+
19 29
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
30
+        String evenNumRange = "";
31
+        if (start%2 != 0) {
32
+        start += 1;
33
+        }
34
+        for (int i=start; i<stop; i+=2) {
35
+            evenNumRange += String.valueOf(i);
36
+        }
37
+        return evenNumRange;
21 38
     }
22 39
 
23
-
24 40
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
41
+        String oddNumRange = "";
42
+        if (start%2 == 0) {
43
+        start += 1;
44
+        }
45
+        for (int i=start; i<stop; i+=2) {
46
+            oddNumRange += String.valueOf(i);
47
+        }
48
+        return oddNumRange;
26 49
     }
27 50
 
28
-
51
+    public static String getSquareNumbers(int start, int stop) {
52
+        String squareNumRange = "";
53
+        for (int i=start; i<stop; i++) {
54
+            squareNumRange += String.valueOf(i*i);
55
+        }
56
+        return squareNumRange;
57
+    }
58
+    
29 59
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
60
+       return getExponentiationsWithStep(start, stop, 1, exponent);
61
+    }
62
+    
63
+    // getExponentiationsWithStep 
64
+    public static String getExponentiationsWithStep(int start, int stop, int step, int exponent) {
65
+        String exNumRange = "";
66
+        int exponentialNum = start;
67
+        int result = 0;
68
+        for (int i=start; i<=stop; i+=step) {
69
+            result = (int)Math.pow(i,exponent);
70
+            //System.out.println(result);
71
+            
72
+            exNumRange += String.valueOf(result);
73
+        }
74
+        return exNumRange;
31 75
     }
32 76
 }

+ 26
- 13
NumberUtilitiesTest.java Wyświetl plik

@@ -1,10 +1,9 @@
1
- 
2 1
 
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
5 4
 
6 5
 public class NumberUtilitiesTest {
7
-    
6
+
8 7
     @Test
9 8
     public void testGetRangeForSmallRange() {
10 9
         // : Given
@@ -17,8 +16,7 @@ public class NumberUtilitiesTest {
17 16
         // : Then
18 17
         Assert.assertEquals(expected, actual);
19 18
     }
20
-    
21
-    
19
+
22 20
     @Test
23 21
     public void testGetRangeToTwoDigits() {
24 22
         // : Given
@@ -60,7 +58,6 @@ public class NumberUtilitiesTest {
60 58
         Assert.assertEquals(expected, actual);
61 59
     }
62 60
 
63
-    
64 61
     @Test
65 62
     public void testGetRangeWithOneStep() {
66 63
         // : Given
@@ -76,7 +73,6 @@ public class NumberUtilitiesTest {
76 73
         Assert.assertEquals(expected, actual);
77 74
     }
78 75
 
79
-
80 76
     @Test
81 77
     public void testGetRangeWithTwoStep() {
82 78
         // : Given
@@ -92,7 +88,7 @@ public class NumberUtilitiesTest {
92 88
         Assert.assertEquals(expected, actual);
93 89
     }
94 90
 
95
-        @Test
91
+    @Test
96 92
     public void testGetRangeWithFiveStep() {
97 93
         // : Given
98 94
         String expected = "51015";
@@ -107,7 +103,6 @@ public class NumberUtilitiesTest {
107 103
         Assert.assertEquals(expected, actual);
108 104
     }
109 105
 
110
-    
111 106
     @Test
112 107
     public void testGetEvenNumbersStartAndEndAtEven() {
113 108
         // : Given
@@ -122,7 +117,6 @@ public class NumberUtilitiesTest {
122 117
         Assert.assertEquals(expected, actual);
123 118
     }
124 119
 
125
-    
126 120
     @Test
127 121
     public void testGetEvenNumbersStartAndEndAtOdd() {
128 122
         // : Given
@@ -137,7 +131,6 @@ public class NumberUtilitiesTest {
137 131
         Assert.assertEquals(expected, actual);
138 132
     }
139 133
 
140
-    
141 134
     @Test
142 135
     public void testGetOddNumbersStartAndEndAtOdd() {
143 136
         // : Given
@@ -165,7 +158,6 @@ public class NumberUtilitiesTest {
165 158
         Assert.assertEquals(expected, actual);
166 159
     }
167 160
 
168
-
169 161
     @Test
170 162
     public void testGetExponentiationNumbersForSquare() {
171 163
         // : Given
@@ -180,8 +172,22 @@ public class NumberUtilitiesTest {
180 172
         // : Then
181 173
         Assert.assertEquals(expected, actual);
182 174
     }
183
-    
184
-    
175
+
176
+    @Test
177
+    public void testReadMe(){
178
+        String expected = "25100225400";
179
+        int start = 5;
180
+        int stop = 20;
181
+        int step = 5;
182
+        int exponent = 2;
183
+
184
+        // : When
185
+        String actual = NumberUtilities.getExponentiationsWithStep(start, stop, step, exponent);
186
+
187
+        // : Then
188
+        Assert.assertEquals(expected, actual);
189
+    }
190
+
185 191
     @Test
186 192
     public void testGetExponentiationNumbersForCube() {
187 193
         // : Given
@@ -196,4 +202,11 @@ public class NumberUtilitiesTest {
196 202
         // : Then
197 203
         Assert.assertEquals(expected, actual);
198 204
     }
205
+
206
+    @Test
207
+    public void testGetSquareNumbers()
208
+    {
209
+        Assert.assertEquals("1491625", NumberUtilities.getSquareNumbers(1, 6));
210
+    }
199 211
 }
212
+

+ 28
- 3
TableUtilities.java Wyświetl plik

@@ -2,14 +2,39 @@
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        String sumOf = "";
6
+        for (int i=1; i<=5; i++){
7
+        for (int b=1; b<=5; b++){
8
+            sumOf += String.format( "%3d |", b*i);
9
+        }
10
+        sumOf += "\n";
11
+        }
12
+        
13
+        return sumOf;
6 14
     }
15
+    
7 16
 
8 17
     public static String getLargeMultiplicationTable() {
9
-        return null;
18
+        String sumOf = "";
19
+        for (int i=1; i<=10; i++){
20
+        for (int b=1; b<=10; b++){
21
+            sumOf += String.format( "%3d |", b*i);
22
+        }
23
+        sumOf += "\n";
24
+        }
25
+        
26
+        return sumOf;
10 27
     }
11 28
 
12 29
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
30
+        String sumOf = "";
31
+        for (int i=1; i<=tableSize; i++){
32
+        for (int b=1; b<=tableSize; b++){
33
+            sumOf += String.format( "%3d |", b*i);
34
+        }
35
+        sumOf += "\n";
36
+        }
37
+        
38
+        return sumOf;
14 39
     }
15 40
 }

+ 24
- 9
TriangleUtilities.java Wyświetl plik

@@ -1,21 +1,36 @@
1
- 
1
+import java.io.*;
2
+import java.util.*;
3
+import java.lang.*;
2 4
 
3 5
 public class TriangleUtilities {
4
-
5
-    public static String getRow(int numberOfStars) {
6
-        return null;
6
+    public static String getRow(int width) {
7
+        String stars = "";
8
+        for (int i=0; i<width; i++) {
9
+            stars += "*";
10
+        }
11
+        return stars;
7 12
     }
8
-    
13
+
9 14
     public static String getTriangle(int numberOfRows) {
10
-        return null;
11
-    }
15
+        String stars = "";
16
+
17
+        for (int i=0; i<numberOfRows; i++) {
12 18
 
19
+            for (int b=0; b<=i; b++) {
20
+                stars += "*";
21
+
22
+            }
23
+            stars+= "\n";
24
+        }
25
+
26
+        return stars;
27
+    }
13 28
 
14 29
     public static String getSmallTriangle() {
15
-        return null;
30
+        return getTriangle(4);
16 31
     }
17 32
 
18 33
     public static String getLargeTriangle() {
19
-        return null;
34
+        return getTriangle(9);
20 35
     }
21 36
 }