Jessica Campbell vor 6 Jahren
Ursprung
Commit
77c3af3183

+ 37
- 7
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Datei anzeigen

@@ -3,26 +3,56 @@ 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
+    StringBuilder evenString = new StringBuilder();
7
+        if (start % 2 == 0) {
8
+        for (int i = start; i < stop; i++){
9
+            evenString.append(i);
10
+        }
11
+    }
12
+        return evenString.toString();
7 13
     }
8 14
 
9 15
 
10 16
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
17
+        StringBuilder oddString = new StringBuilder();
18
+        if (start % 2 == 1) {
19
+            for (int i = start; i < stop; i++){
20
+                oddString.append(i);
21
+            }
22
+        }
23
+        return oddString.toString();
12 24
     }
13 25
 
14 26
 
15 27
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
17
-    }
28
+        StringBuilder squareString = new StringBuilder();
29
+
30
+            for (int i = start; i < stop; i++){
31
+                squareString.append(i*i);
32
+            }
33
+        return squareString.toString();
34
+        }
35
+
36
+
18 37
 
19 38
 
20 39
     public static String getRange(int start, int stop, int step) {
21
-        return null;
22
-    }
40
+    StringBuilder range = new StringBuilder();
23 41
 
42
+            for (int i = start; i<stop; i++) {
43
+                range.append(i);
44
+            } return range.toString();
45
+    }
46
+// must add to.String to range bc you made a string builder
24 47
 
25 48
     public static String getExponentiations(int start, int stop, int step, int exponent) {
26
-        return null;
49
+
50
+        StringBuilder exponentString = new StringBuilder();
51
+
52
+        for (int i = start; i < stop-1; i += step){
53
+            exponentString.append(Math.pow(i,exponent));
54
+        }
55
+        return exponentString.toString();
56
+
27 57
     }
28 58
 }

+ 37
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Datei anzeigen

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

+ 46
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Datei anzeigen

@@ -3,18 +3,60 @@ 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
+        System.out.println("Get Triangle");
7
+        String resultString = "";
8
+        System.out.println(resultString);
9
+        for (int i = 1; i < numberOfRows; i++){
10
+
11
+            String rowofStars = "";
12
+            for (int j = 0; j < i; j++){
13
+                rowofStars = rowofStars + "*";
14
+
15
+            }
16
+            resultString = resultString + rowofStars + "\n";
17
+        }
18
+
19
+        return resultString;
7 20
     }
8 21
 
9 22
     public static String getRow(int numberOfStars) {
10
-        return null;
23
+        System.out.println("Get Row");
24
+        String resultString = "";
25
+       for (int i = 0; i < numberOfStars; i++){
26
+           resultString = resultString + "*";
27
+       }
28
+       return resultString;
11 29
     }
12 30
 
13 31
     public static String getSmallTriangle() {
14
-        return null;
32
+        System.out.println("Get Small Triangle");
33
+        String resultString = "";
34
+        System.out.println(resultString);
35
+        for (int i = 1; i < 5; i++){
36
+            String rowofStars = "";
37
+            for (int j = 0; j < i; j++){
38
+                rowofStars = rowofStars + "*";
39
+
40
+            }
41
+            resultString = resultString + rowofStars + "\n";
42
+        }
43
+
44
+        return resultString;
15 45
     }
16 46
 
17 47
     public static String getLargeTriangle() {
18
-        return null;
48
+        System.out.println("Get Large Triangle");
49
+        String resultString = "";
50
+        System.out.println(resultString);
51
+        for (int i = 1; i < 10; i++){
52
+            String rowofStars = "";
53
+            for (int j = 0; j < i; j++){
54
+                rowofStars = rowofStars + "*";
55
+
56
+            }
57
+            resultString = resultString + rowofStars + "\n";
58
+        }
59
+
60
+        return resultString;
19 61
     }
20 62
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Datei anzeigen

@@ -51,7 +51,7 @@ public class NumberUtilitiesTest {
51 51
     @Test
52 52
     public void testGetEvenNumbers() {
53 53
         // : Given
54
-        String expected = "5791113151719";
54
+        String expected = "681012141618";
55 55
         int start = 5;
56 56
         int stop = 20;
57 57
 
@@ -65,7 +65,7 @@ public class NumberUtilitiesTest {
65 65
     @Test
66 66
     public void testGetOddNumbers() {
67 67
         // : Given
68
-        String expected = "681012141618";
68
+        String expected = "5791113151719";
69 69
         int start = 5;
70 70
         int stop = 20;
71 71
         int step = 5;