Browse Source

added a bunch of methods to this lab

Margaret Pierse 6 years ago
parent
commit
69d762a571
4 changed files with 137 additions and 40 deletions
  1. 59
    15
      NumberUtilities.java
  2. 26
    13
      NumberUtilitiesTest.java
  3. 28
    3
      TableUtilities.java
  4. 24
    9
      TriangleUtilities.java

+ 59
- 15
NumberUtilities.java View File

1
- 
2
-
1
+import java.util.*; 
3
 
2
 
4
 public class NumberUtilities {
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
     public static String getRange(int start, int stop) {
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
     public static String getRange(int start, int stop, int step) {
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
     public static String getEvenNumbers(int start, int stop) {
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
     public static String getOddNumbers(int start, int stop) {
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
     public static String getExponentiations(int start, int stop, int exponent) {
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 View File

1
- 
2
 
1
 
3
 import org.junit.Assert;
2
 import org.junit.Assert;
4
 import org.junit.Test;
3
 import org.junit.Test;
5
 
4
 
6
 public class NumberUtilitiesTest {
5
 public class NumberUtilitiesTest {
7
-    
6
+
8
     @Test
7
     @Test
9
     public void testGetRangeForSmallRange() {
8
     public void testGetRangeForSmallRange() {
10
         // : Given
9
         // : Given
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 testGetRangeToTwoDigits() {
24
         // : Given
22
         // : Given
60
         Assert.assertEquals(expected, actual);
58
         Assert.assertEquals(expected, actual);
61
     }
59
     }
62
 
60
 
63
-    
64
     @Test
61
     @Test
65
     public void testGetRangeWithOneStep() {
62
     public void testGetRangeWithOneStep() {
66
         // : Given
63
         // : Given
76
         Assert.assertEquals(expected, actual);
73
         Assert.assertEquals(expected, actual);
77
     }
74
     }
78
 
75
 
79
-
80
     @Test
76
     @Test
81
     public void testGetRangeWithTwoStep() {
77
     public void testGetRangeWithTwoStep() {
82
         // : Given
78
         // : Given
92
         Assert.assertEquals(expected, actual);
88
         Assert.assertEquals(expected, actual);
93
     }
89
     }
94
 
90
 
95
-        @Test
91
+    @Test
96
     public void testGetRangeWithFiveStep() {
92
     public void testGetRangeWithFiveStep() {
97
         // : Given
93
         // : Given
98
         String expected = "51015";
94
         String expected = "51015";
107
         Assert.assertEquals(expected, actual);
103
         Assert.assertEquals(expected, actual);
108
     }
104
     }
109
 
105
 
110
-    
111
     @Test
106
     @Test
112
     public void testGetEvenNumbersStartAndEndAtEven() {
107
     public void testGetEvenNumbersStartAndEndAtEven() {
113
         // : Given
108
         // : Given
122
         Assert.assertEquals(expected, actual);
117
         Assert.assertEquals(expected, actual);
123
     }
118
     }
124
 
119
 
125
-    
126
     @Test
120
     @Test
127
     public void testGetEvenNumbersStartAndEndAtOdd() {
121
     public void testGetEvenNumbersStartAndEndAtOdd() {
128
         // : Given
122
         // : Given
137
         Assert.assertEquals(expected, actual);
131
         Assert.assertEquals(expected, actual);
138
     }
132
     }
139
 
133
 
140
-    
141
     @Test
134
     @Test
142
     public void testGetOddNumbersStartAndEndAtOdd() {
135
     public void testGetOddNumbersStartAndEndAtOdd() {
143
         // : Given
136
         // : Given
165
         Assert.assertEquals(expected, actual);
158
         Assert.assertEquals(expected, actual);
166
     }
159
     }
167
 
160
 
168
-
169
     @Test
161
     @Test
170
     public void testGetExponentiationNumbersForSquare() {
162
     public void testGetExponentiationNumbersForSquare() {
171
         // : Given
163
         // : Given
180
         // : Then
172
         // : Then
181
         Assert.assertEquals(expected, actual);
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
     @Test
191
     @Test
186
     public void testGetExponentiationNumbersForCube() {
192
     public void testGetExponentiationNumbersForCube() {
187
         // : Given
193
         // : Given
196
         // : Then
202
         // : Then
197
         Assert.assertEquals(expected, actual);
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 View File

2
 
2
 
3
 public class TableUtilities {
3
 public class TableUtilities {
4
     public static String getSmallMultiplicationTable() {
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
     public static String getLargeMultiplicationTable() {
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
     public static String getMultiplicationTable(int tableSize) {
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 View File

1
- 
1
+import java.io.*;
2
+import java.util.*;
3
+import java.lang.*;
2
 
4
 
3
 public class TriangleUtilities {
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
     public static String getTriangle(int numberOfRows) {
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
     public static String getSmallTriangle() {
29
     public static String getSmallTriangle() {
15
-        return null;
30
+        return getTriangle(4);
16
     }
31
     }
17
 
32
 
18
     public static String getLargeTriangle() {
33
     public static String getLargeTriangle() {
19
-        return null;
34
+        return getTriangle(9);
20
     }
35
     }
21
 }
36
 }