Преглед на файлове

Merge 8877e8f85ecc2a1b36b44fe4f3846503ef5e2e53 into 7f0bced90f8d4e71dd79ce8164661b025060d21c

seanwillmis преди 6 години
родител
ревизия
ee737bd74f
No account linked to committer's email

+ 29
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Целия файл

@@ -3,17 +3,33 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 
4 4
 public class NumberUtilities {
5 5
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
6
+        String evens = "";
7
+        for(int i = start; i < stop; i++) {
8
+            if(i % 2 == 0) {
9
+                evens += Integer.toString(i);
10
+            }
11
+        }
12
+        return evens;
7 13
     }
8 14
 
9 15
 
10 16
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
17
+        String odds = "";
18
+        for(int i = start; i < stop; i++) {
19
+            if(i % 2 != 0) {
20
+                odds += Integer.toString(i);
21
+            }
22
+        }
23
+        return odds;
12 24
     }
13 25
 
14 26
 
15 27
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
28
+        String squares = "";
29
+        for(int i = start; i < stop; i+=step) {
30
+                squares += Integer.toString(i*i);
31
+        }
32
+        return squares;
17 33
     }
18 34
 
19 35
     public static String getRange(int start) {
@@ -26,11 +42,19 @@ public class NumberUtilities {
26 42
 
27 43
 
28 44
     public static String getRange(int start, int stop, int step) {
29
-        return null;
45
+        String range = "";
46
+        for(int i = start; i < stop; i+=step) {
47
+            range += Integer.toString(i);
48
+        }
49
+        return range;
30 50
     }
31 51
 
32 52
 
33 53
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
54
+        String expo = "";
55
+        for(int i = start; i < stop; i+=step) {
56
+            expo += (int) Math.pow(i, exponent);
57
+        }
58
+        return expo;
35 59
     }
36 60
 }

+ 27
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Целия файл

@@ -2,14 +2,38 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        StringBuilder smallMult = new StringBuilder();
6
+        for (int row = 1; row <= 5; row++) {
7
+            for (int col = 1; col <= 5; col++) {
8
+                int colRow = col * row;
9
+                smallMult.append(String.format("%3d |", colRow));
10
+            }
11
+            smallMult.append("\n");
12
+        }
13
+        return smallMult.toString();
6 14
     }
7 15
 
8 16
     public static String getLargeMultiplicationTable() {
9
-        return null;
17
+        StringBuilder largeMult = new StringBuilder();
18
+        for (int row = 1; row <= 10; row++) {
19
+            for (int col = 1; col <= 10; col++) {
20
+                int colRow = col * row;
21
+                largeMult.append(String.format("%3d |", colRow));
22
+            }
23
+            largeMult.append("\n");
24
+        }
25
+        return largeMult.toString();
10 26
     }
11 27
 
12 28
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
29
+        StringBuilder multTab = new StringBuilder();
30
+        for (int row = 1; row <= tableSize; row++) {
31
+            for (int col = 1; col <= tableSize; col++) {
32
+                int colRow = col * row;
33
+                multTab.append(String.format("%3d |", colRow));
34
+            }
35
+            multTab.append("\n");
36
+        }
37
+        return multTab.toString();
14 38
     }
15 39
 }

+ 30
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Целия файл

@@ -3,18 +3,44 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getTriangle(int numberOfRows) {
6
-        return null;
6
+        StringBuilder newTri = new StringBuilder();
7
+
8
+        for(int row=1; row < numberOfRows; row++) {
9
+            for(int col=0; col < row; col++) {
10
+                newTri.append("*");
11
+            }
12
+            newTri.append("\n");
13
+        }
14
+        return newTri.toString();
7 15
     }
8 16
 
9 17
     public static String getRow(int numberOfStars) {
10
-        return null;
18
+        StringBuilder newRow = new StringBuilder();
19
+        for(int i=0; i < numberOfStars; i++) {
20
+            newRow.append("*");
21
+        }
22
+        return newRow.toString();
11 23
     }
12 24
 
13 25
     public static String getSmallTriangle() {
14
-        return null;
26
+        StringBuilder smallTri = new StringBuilder();
27
+        for(int row=1; row < 5; row++) {
28
+            for(int col=0; col < row; col++) {
29
+                smallTri.append("*");
30
+            }
31
+            smallTri.append("\n");
32
+        }
33
+        return smallTri.toString();
15 34
     }
16 35
 
17 36
     public static String getLargeTriangle() {
18
-        return null;
37
+        StringBuilder getTri = new StringBuilder();
38
+        for(int row=1; row < 10; row++) {
39
+            for(int col=0; col < row; col++) {
40
+                getTri.append("*");
41
+            }
42
+            getTri.append("\n");
43
+        }
44
+        return getTri.toString();
19 45
     }
20 46
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Целия файл

@@ -173,7 +173,7 @@ public class NumberUtilitiesTest {
173 173
     @Test
174 174
     public void testGetEvenNumbers() {
175 175
         // : Given
176
-        String expected = "5791113151719";
176
+        String expected = "681012141618";
177 177
         int start = 5;
178 178
         int stop = 20;
179 179
 
@@ -187,7 +187,7 @@ public class NumberUtilitiesTest {
187 187
     @Test
188 188
     public void testGetOddNumbers() {
189 189
         // : Given
190
-        String expected = "681012141618";
190
+        String expected = "5791113151719";
191 191
         int start = 5;
192 192
         int stop = 20;
193 193
         int step = 5;