Просмотр исходного кода

Merge ecb1913733e3fa876c45bc688a5bd42ea611c0cb into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Madeline Bowe 6 лет назад
Родитель
Сommit
37190ae846
Аккаунт пользователя с таким Email не найден

+ 42
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Просмотреть файл

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

+ 45
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Просмотреть файл

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

+ 14
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Просмотреть файл

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 builder = new StringBuilder();
7
+        for (int row = 0; row < numberOfRows; row++) {
8
+            builder.append(getRow(row));
9
+        }
10
+        return builder.toString();
7
     }
11
     }
8
 
12
 
9
     public static String getRow(int numberOfStars) {
13
     public static String getRow(int numberOfStars) {
10
-        return null;
14
+        StringBuilder builder = new StringBuilder();
15
+
16
+        for (int i = 0; i <= numberOfStars; i++) {
17
+            builder.append("*");
18
+        }
19
+        builder.append("\n");
20
+        return builder.toString();
11
     }
21
     }
12
 
22
 
13
     public static String getSmallTriangle() {
23
     public static String getSmallTriangle() {
14
-        return null;
24
+        return getTriangle(4);
15
     }
25
     }
16
 
26
 
17
     public static String getLargeTriangle() {
27
     public static String getLargeTriangle() {
18
-        return null;
28
+        return getTriangle(9);
19
     }
29
     }
20
 }
30
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Просмотреть файл

173
     @Test
173
     @Test
174
     public void testGetEvenNumbers() {
174
     public void testGetEvenNumbers() {
175
         // : Given
175
         // : Given
176
-        String expected = "5791113151719";
176
+        String expected = "681012141618";
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;

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java Просмотреть файл

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" +
27
                         "*******\n" +
27
                         "*******\n" +
28
                         "********\n" +
28
                         "********\n" +
29
                         "*********\n";
29
                         "*********\n";
30
-        String actual = TriangleUtilities.getTriangle(10);
30
+        String actual = TriangleUtilities.getTriangle(9);
31
         Assert.assertEquals(expected, actual);
31
         Assert.assertEquals(expected, actual);
32
     }
32
     }
33
 
33