Procházet zdrojové kódy

Merge 23da1dfc77d85fcde10211f97d65c5007f6c40f6 into 6026e5f68429fdeebffb1a314db6451e44f108c4

kbrinker1 před 6 roky
rodič
revize
1ef61edb2c
No account linked to committer's email

+ 42
- 6
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Zobrazit soubor

@@ -3,17 +3,43 @@ 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 even = new StringBuilder();
7
+
8
+        if (start % 2 != 0) {
9
+            start += 1;
10
+        }
11
+
12
+
13
+        for (int i = start; i < stop; i += 2) {
14
+            even.append(i);
15
+        }
16
+        return even.toString();
7 17
     }
8 18
 
9 19
 
10 20
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
21
+        StringBuilder odd = new StringBuilder();
22
+
23
+        if (start % 2 == 0) {
24
+            start -= 1;
25
+        }
26
+
27
+
28
+        for (int i = start; i < stop; i += 2) {
29
+            odd.append(i);
30
+        }
31
+        return odd.toString();
12 32
     }
13 33
 
14 34
 
15 35
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
36
+        StringBuilder square = new StringBuilder();
37
+        for (int i = start; i < stop; i += step) {
38
+            int squarednum = (int) Math.pow(i, 2);
39
+            square.append(squarednum);
40
+
41
+        }
42
+        return square.toString();
17 43
     }
18 44
 
19 45
     public static String getRange(int start) {
@@ -26,11 +52,21 @@ public class NumberUtilities {
26 52
 
27 53
 
28 54
     public static String getRange(int start, int stop, int step) {
29
-        return null;
55
+        StringBuilder range = new StringBuilder();
56
+        for (int i = start; i < stop; i += step) {
57
+            range.append(i);
58
+        }
59
+        return range.toString();
30 60
     }
31 61
 
32 62
 
33 63
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
64
+        StringBuilder expo = new StringBuilder();
65
+        for (int i = start; i < stop; i += step) {
66
+            int exponentVariable = exponent;
67
+            int squarednum = (int) Math.pow(i, exponentVariable);
68
+            expo.append(squarednum);
69
+        }
70
+        return expo.toString();
35 71
     }
36
-}
72
+}

+ 27
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Zobrazit soubor

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

+ 29
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Zobrazit soubor

@@ -3,18 +3,43 @@ 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
+        String output = "";
7
+        for (int i = 1; i < numberOfRows; i++){
8
+            for (int j = 0; j < i; j++){
9
+                output += "*";
10
+            }
11
+            output += '\n';
12
+        }
13
+        return output;
7 14
     }
8 15
 
9 16
     public static String getRow(int numberOfStars) {
10
-        return null;
17
+        String output = "";
18
+        for (int i = 0; i < numberOfStars; i++){
19
+            output += "*";
20
+        }
21
+        return output;
11 22
     }
12 23
 
13 24
     public static String getSmallTriangle() {
14
-        return null;
25
+        String output = "";
26
+        for (int i = 1; i <=4; i++){
27
+    for (int j = 0; j < i; j++){
28
+        output += "*";
29
+    }
30
+    output += '\n';
31
+        }
32
+        return output;
15 33
     }
16 34
 
17 35
     public static String getLargeTriangle() {
18
-        return null;
36
+        String output = "";
37
+        for (int i = 1; i <=9; i++){
38
+            for (int j = 0; j < i; j++){
39
+                output += "*";
40
+            }
41
+            output += '\n';
42
+        }
43
+        return output;
19 44
     }
20 45
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Zobrazit soubor

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