Browse Source

Merge 323448a3be7443bda280c87110a31e79cdee27da into 24118cb7b3c6ce6a0b3be331c7987add797baa81

okmurphy 6 years ago
parent
commit
cf16f50d3a
No account linked to committer's email

+ 73
- 7
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java View File

@@ -2,18 +2,62 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 
4 4
 public class NumberUtilities {
5
+
5 6
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
7
-    }
8 7
 
9 8
 
10
-    public static String getOddNumbers(int start, int stop) {
11
-        return null;
9
+        String n = "";
10
+
11
+        for (int i = start; i <stop; i++) {
12
+
13
+
14
+            if (i % 2 == 0) {
15
+               n = n + i;
16
+            }
17
+
18
+        }
19
+
20
+        return n;
12 21
     }
13 22
 
14 23
 
24
+
25
+        public static String getOddNumbers(int start, int stop) {
26
+
27
+            String n = "";
28
+
29
+            for (int i = start; i <stop; i++) {
30
+
31
+
32
+                if (i % 2!= 0) {
33
+                    n = n + i;
34
+                }
35
+
36
+            }
37
+
38
+            return n;
39
+        }
40
+
41
+
42
+
43
+
15 44
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
45
+
46
+        String n = "";
47
+
48
+
49
+
50
+        for (int i = start; i <stop; i+=step) {
51
+
52
+
53
+                n = n + (i*i);
54
+            }
55
+
56
+
57
+
58
+        return n;
59
+
60
+
17 61
     }
18 62
 
19 63
     public static String getRange(int start) {
@@ -26,11 +70,33 @@ public class NumberUtilities {
26 70
 
27 71
 
28 72
     public static String getRange(int start, int stop, int step) {
29
-        return null;
73
+
74
+        String n = "";
75
+
76
+        for (int i = start; i <stop; i+=step){
77
+
78
+            n = n + i;
79
+        }
80
+
81
+        return n;
30 82
     }
31 83
 
32 84
 
33 85
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
86
+
87
+        String n = "";
88
+
89
+        for (int i = start; i <stop; i+=step){
90
+
91
+          int x = (int)Math.pow(i, exponent);
92
+          n += String.valueOf(x);
93
+
94
+        }
95
+
96
+
97
+
98
+
99
+
100
+        return n;
35 101
     }
36 102
 }

+ 42
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java View File

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

+ 64
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java View File

@@ -3,18 +3,78 @@ 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
+
7
+        StringBuilder outcome = new StringBuilder();
8
+
9
+
10
+        for (int i=1; i < numberOfRows; i++) {
11
+
12
+            for (int j=0; j < i; j++){
13
+
14
+                outcome.append("*");
15
+            }
16
+                outcome.append("\n");
17
+
18
+            }
19
+
20
+
21
+            return outcome.toString();
7 22
     }
8 23
 
9 24
     public static String getRow(int numberOfStars) {
10
-        return null;
25
+
26
+        StringBuilder outcome = new StringBuilder();
27
+
28
+
29
+        for (int i=0; i < numberOfStars; i++){
30
+
31
+            outcome.append("*");
32
+
33
+        }
34
+
35
+
36
+        return outcome.toString();
11 37
     }
12 38
 
13 39
     public static String getSmallTriangle() {
14
-        return null;
40
+        StringBuilder outcome = new StringBuilder();
41
+
42
+
43
+        for (int i=1; i <=4; i++) {
44
+
45
+            for (int j=0; j < i; j++){
46
+
47
+                outcome.append("*");
48
+            }
49
+            outcome.append("\n");
50
+
51
+        }
52
+
53
+
54
+        return outcome.toString();
55
+
56
+
57
+
15 58
     }
16 59
 
17 60
     public static String getLargeTriangle() {
18
-        return null;
61
+
62
+        StringBuilder outcome = new StringBuilder();
63
+
64
+
65
+        for (int i=1; i <=9; i++) {
66
+
67
+            for (int j=0; j < i; j++){
68
+
69
+                outcome.append("*");
70
+            }
71
+            outcome.append("\n");
72
+
73
+        }
74
+
75
+
76
+        return outcome.toString();
77
+
78
+
19 79
     }
20 80
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java View File

@@ -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;