Quellcode durchsuchen

Merge 5707f3c470f163e6deb82308c768b81158f0a4a3 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

gjarant vor 6 Jahren
Ursprung
Commit
e5dd9796ce
Es ist kein Account mit dieser Commiter-Email verbunden

+ 63
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Datei anzeigen

@@ -3,17 +3,51 @@ 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
+
7
+        StringBuilder evenNumbers = new StringBuilder();
8
+
9
+        for(int i = start; i < stop; i++){
10
+
11
+            if (i % 2 == 0){
12
+                evenNumbers.append(i);
13
+            }
14
+        }
15
+
16
+
17
+        return evenNumbers.toString();
7 18
     }
8 19
 
9 20
 
10 21
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
22
+
23
+        StringBuilder oddNumbers = new StringBuilder();
24
+
25
+        for(int i = start; i < stop; i++){
26
+
27
+            if (i % 2 != 0){
28
+                oddNumbers.append(i);
29
+            }
30
+        }
31
+
32
+
33
+        return oddNumbers.toString();
34
+
12 35
     }
13 36
 
14 37
 
15 38
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
39
+
40
+        StringBuilder squareNumbers = new StringBuilder();
41
+
42
+        for(int i = start; i < stop; i += step){
43
+
44
+
45
+
46
+                squareNumbers.append((int)(Math.pow(i,2)));
47
+            }
48
+
49
+
50
+        return squareNumbers.toString();
17 51
     }
18 52
 
19 53
     public static String getRange(int start) {
@@ -26,11 +60,35 @@ public class NumberUtilities {
26 60
 
27 61
 
28 62
     public static String getRange(int start, int stop, int step) {
29
-        return null;
63
+
64
+        StringBuilder rangeNumbers = new StringBuilder();
65
+
66
+        for(int i = start; i < stop; i += step){
67
+
68
+
69
+
70
+            rangeNumbers.append(i);
71
+        }
72
+
73
+
74
+        return rangeNumbers.toString();
75
+
30 76
     }
31 77
 
32 78
 
33 79
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
80
+        StringBuilder exponentNumbers = new StringBuilder();
81
+
82
+        for(int i = start; i < stop; i += step){
83
+
84
+
85
+
86
+            exponentNumbers.append((int)(Math.pow(i,exponent)));
87
+        }
88
+
89
+
90
+        return exponentNumbers.toString();
35 91
     }
92
+
36 93
 }
94
+

+ 52
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Datei anzeigen

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

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

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

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

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