Przeglądaj źródła

Merge 38ad53ef6ab553c7d9bcb30c0c15a30018fc7e9b into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Brian 6 lat temu
rodzic
commit
2eda4c7597
Brak konta powiązanego z e-mailem autora

+ 26
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Wyświetl plik

3
 
3
 
4
 public class NumberUtilities {
4
 public class NumberUtilities {
5
     public static String getEvenNumbers(int start, int stop) {
5
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
6
+git
7
+        StringBuilder evensOnly = new StringBuilder();
8
+        for(int i = start; i <= stop; i++) {
9
+            if(i % 2 == 0) {
10
+                evensOnly.append(i);
11
+            }
12
+        }
13
+        return evensOnly.toString();
7
     }
14
     }
8
 
15
 
9
 
16
 
10
     public static String getOddNumbers(int start, int stop) {
17
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
18
+        StringBuilder oddsOnly = new StringBuilder();
19
+        for(int i = start; i <= stop; i++) {
20
+            if(i % 2 != 0) {
21
+                oddsOnly.append(i);
22
+            }
23
+        }
24
+        return oddsOnly.toString();
12
     }
25
     }
13
 
26
 
14
 
27
 
15
     public static String getSquareNumbers(int start, int stop, int step) {
28
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
29
+        return getExponentiations(start, stop, step, 2);
17
     }
30
     }
18
 
31
 
19
     public static String getRange(int start) {
32
     public static String getRange(int start) {
26
 
39
 
27
 
40
 
28
     public static String getRange(int start, int stop, int step) {
41
     public static String getRange(int start, int stop, int step) {
29
-        return null;
42
+        StringBuilder range = new StringBuilder();
43
+        for(int i = start; i < stop; i += step) {
44
+            range.append(i);
45
+        }
46
+        return range.toString();
30
     }
47
     }
31
 
48
 
32
 
49
 
33
     public static String getExponentiations(int start, int stop, int step, int exponent) {
50
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
51
+        StringBuilder exponentials = new StringBuilder();
52
+        for(int i = start; i < stop; i += step) {
53
+            exponentials.append((int)Math.pow(i, exponent));
54
+        }
55
+        return exponentials.toString();
35
     }
56
     }
36
 }
57
 }

+ 11
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Wyświetl plik

2
 
2
 
3
 public class TableUtilities {
3
 public class TableUtilities {
4
     public static String getSmallMultiplicationTable() {
4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        return getMultiplicationTable(5);
6
     }
6
     }
7
 
7
 
8
     public static String getLargeMultiplicationTable() {
8
     public static String getLargeMultiplicationTable() {
9
-        return null;
9
+        return getMultiplicationTable(10);
10
     }
10
     }
11
 
11
 
12
     public static String getMultiplicationTable(int tableSize) {
12
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
13
+        StringBuilder table = new StringBuilder();
14
+        for(int i = 1; i < tableSize + 1; ++i) {
15
+            for(int j = 1; j < tableSize + 1; ++j) {
16
+                String row = String.format("%3d |", i * j);
17
+                table.append(row);
18
+            }
19
+            table.append("\n");
20
+        }
21
+        return table.toString();
14
     }
22
     }
15
 }
23
 }

+ 13
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Wyświetl plik

3
 public class TriangleUtilities {
3
 public class TriangleUtilities {
4
 
4
 
5
     public static String getTriangle(int numberOfRows) {
5
     public static String getTriangle(int numberOfRows) {
6
-        return null;
6
+        StringBuilder triangle = new StringBuilder();
7
+        for (int i = 1; i < numberOfRows; i++) {
8
+            triangle.append(getRow(i));
9
+            triangle.append("\n");
10
+        }
11
+        return triangle.toString();
7
     }
12
     }
8
 
13
 
9
     public static String getRow(int numberOfStars) {
14
     public static String getRow(int numberOfStars) {
10
-        return null;
15
+        StringBuilder rowOfStars = new StringBuilder();
16
+        for (int i = 0; i < numberOfStars; i++) {
17
+            rowOfStars.append("*");
18
+        }
19
+        return rowOfStars.toString();
11
     }
20
     }
12
 
21
 
13
     public static String getSmallTriangle() {
22
     public static String getSmallTriangle() {
14
-        return null;
23
+        return getTriangle(5);
15
     }
24
     }
16
 
25
 
17
     public static String getLargeTriangle() {
26
     public static String getLargeTriangle() {
18
-        return null;
27
+        return getTriangle(10);
19
     }
28
     }
20
 }
29
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Wyświetl plik

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

+ 1
- 1
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java Wyświetl plik

18
     @Test
18
     @Test
19
     public void getTriangleTest1() {
19
     public void getTriangleTest1() {
20
         String expected =
20
         String expected =
21
-                "*\n" +
21
+                        "*\n" +
22
                         "**\n" +
22
                         "**\n" +
23
                         "***\n" +
23
                         "***\n" +
24
                         "****\n" +
24
                         "****\n" +