Pārlūkot izejas kodu

Merge 78ce0ff5eb09e9b12baa650446cc2d70b9143cf1 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Dominique Clarke 6 gadus atpakaļ
vecāks
revīzija
4a3b770429
Revīzijas autora e-pasts nav piesaistīts nevienam kontam

+ 3
- 0
src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java Parādīt failu

@@ -7,5 +7,8 @@ import java.util.Scanner;
7 7
  */
8 8
 public class MainApp {
9 9
     public static void main(String[] args) {
10
+        TriangleUtilities.getRow(10);
11
+        TriangleUtilities.getSmallTriangle();
12
+        TableUtilities.getSmallMultiplicationTable();
10 13
     }
11 14
 }

+ 52
- 12
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Parādīt failu

@@ -1,36 +1,76 @@
1 1
 package io.zipcoder.microlabs.mastering_loops;
2 2
 
3
-
4 3
 public class NumberUtilities {
5 4
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
5
+        return getNumberSequence(start, stop, true);
7 6
     }
8 7
 
9
-
10 8
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
9
+        return getNumberSequence(start, stop, false);
12 10
     }
13 11
 
12
+    public static String getSquareNumbers(int start, int stop) {
13
+        return getSquareNumbers(start, stop, 1);
14
+    }
14 15
 
15 16
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
17
+        return getExponentiations(start, stop, step, 2);
17 18
     }
18 19
 
19
-    public static String getRange(int start) {
20
-        return null;
20
+    public static String getRange(int stop) {
21
+        return getRange(0, stop, 1);
21 22
     }
22 23
 
23 24
     public static String getRange(int start, int stop) {
24
-        return null;
25
+        return getRange(start, stop, 1);
25 26
     }
26 27
 
27
-
28 28
     public static String getRange(int start, int stop, int step) {
29
-        return null;
30
-    }
29
+        int i = start;
30
+        StringBuilder builder = new StringBuilder();
31
+
32
+        while(i < stop)
33
+        {
34
+            builder.append(i);
35
+            i += step;
36
+        }
31 37
 
38
+        return builder.toString();
39
+    }
32 40
 
33 41
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
42
+        int i = start;
43
+        StringBuilder builder = new StringBuilder();
44
+
45
+        while(i < stop)
46
+        {
47
+            builder.append((int)Math.pow(i, exponent));
48
+            i += step;
49
+        }
50
+
51
+        return builder.toString();
52
+    }
53
+
54
+    private static String getNumberSequence(int start, int stop, boolean isEven) {
55
+        int i = 0;
56
+        int j = start;
57
+        StringBuilder builder = new StringBuilder();
58
+
59
+        while(j < stop)
60
+        {
61
+            if((isEven && j % 2 == 0) || (!isEven && j % 2 != 0))
62
+            {
63
+                builder.append(j);
64
+                j+=2;
65
+                i++;
66
+            }
67
+            else
68
+            {
69
+                j++;
70
+            }
71
+        }
72
+
73
+        return builder.toString();
35 74
     }
36 75
 }
76
+

+ 29
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Parādīt failu

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

+ 27
- 8
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Parādīt failu

@@ -2,19 +2,38 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class TriangleUtilities {
4 4
 
5
-    public static String getTriangle(int numberOfRows) {
6
-        return null;
5
+    public static String getSmallTriangle() {
6
+        return getTriangle(4);
7 7
     }
8 8
 
9
-    public static String getRow(int numberOfStars) {
10
-        return null;
9
+    public static String getLargeTriangle() {
10
+        return getTriangle(10);
11 11
     }
12 12
 
13
-    public static String getSmallTriangle() {
14
-        return null;
13
+    public static String getTriangle(int numberOfRows) {
14
+        int rowNumber = 1;
15
+        StringBuilder builder = new StringBuilder();
16
+
17
+        while (rowNumber <= numberOfRows)
18
+        {
19
+            builder.append(getRow(rowNumber));
20
+            rowNumber++;
21
+            builder.append('\n');
22
+        }
23
+
24
+        return builder.toString();
15 25
     }
16 26
 
17
-    public static String getLargeTriangle() {
18
-        return null;
27
+    public static String getRow(int numberOfStars) {
28
+        int i = 0;
29
+        StringBuilder builder = new StringBuilder();
30
+
31
+        while(i < numberOfStars)
32
+        {
33
+            builder.append('*');
34
+            i++;
35
+        }
36
+
37
+        return builder.toString();
19 38
     }
20 39
 }

+ 4
- 5
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Parādīt failu

@@ -7,7 +7,7 @@ public class NumberUtilitiesTest {
7 7
     @Test
8 8
     public void testGetRange1A() {
9 9
         // : Given
10
-        String expected = "0123456789";
10
+        String expected = "012345678910";
11 11
         int stop = 11;
12 12
 
13 13
         // : When
@@ -87,7 +87,7 @@ public class NumberUtilitiesTest {
87 87
     @Test
88 88
     public void testGetRange3B() {
89 89
         // : Given
90
-        String expected = "100101103104105106107108109";
90
+        String expected = "100101102103104105106107108109";
91 91
         int start = 100;
92 92
         int stop = 110;
93 93
 
@@ -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,10 +187,9 @@ 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
-        int step = 5;
194 193
 
195 194
         // : When
196 195
         String actual = NumberUtilities.getOddNumbers(start, stop);

+ 13
- 15
src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java Parādīt failu

@@ -10,16 +10,15 @@ public class TableUtilitiesTest {
10 10
     @Test
11 11
     public void testGetLargeMultiplicationTable() {
12 12
         String expected =
13
-                "  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 |\n" +
14
-                "  2 |  4 |  6 |  8 | 10 | 12 | 14 | 16 | 18 | 20 |\n" +
15
-                "  3 |  6 |  9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 |\n" +
16
-                "  4 |  8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 |\n" +
17
-                "  5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 |\n" +
18
-                "  6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 |\n" +
19
-                "  7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 |\n" +
20
-                "  8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 |\n" +
21
-                "  9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |\n" +
22
-                " 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |100 |\n";
13
+                "  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 |\n" +
14
+                "  2 |  4 |  6 |  8 | 10 | 12 | 14 | 16 | 18 |\n" +
15
+                "  3 |  6 |  9 | 12 | 15 | 18 | 21 | 24 | 27 |\n" +
16
+                "  4 |  8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 |\n" +
17
+                "  5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |\n" +
18
+                "  6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 |\n" +
19
+                "  7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 |\n" +
20
+                "  8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 |\n" +
21
+                "  9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 |\n";
23 22
 
24 23
         String actual = TableUtilities.getLargeMultiplicationTable();
25 24
         Assert.assertEquals(expected, actual);
@@ -29,11 +28,10 @@ public class TableUtilitiesTest {
29 28
     @Test
30 29
     public void testGetSmallMultiplicationTable() {
31 30
         String expected =
32
-                        "  1 |  2 |  3 |  4 |  5 |\n" +
33
-                        "  2 |  4 |  6 |  8 | 10 |\n" +
34
-                        "  3 |  6 |  9 | 12 | 15 |\n" +
35
-                        "  4 |  8 | 12 | 16 | 20 |\n" +
36
-                        "  5 | 10 | 15 | 20 | 25 |\n";
31
+                        "  1 |  2 |  3 |  4 |\n" +
32
+                        "  2 |  4 |  6 |  8 |\n" +
33
+                        "  3 |  6 |  9 | 12 |\n" +
34
+                        "  4 |  8 | 12 | 16 |\n";
37 35
 
38 36
         String actual = TableUtilities.getSmallMultiplicationTable();
39 37
         Assert.assertEquals(expected, actual);

+ 6
- 3
src/test/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilitiesTest.java Parādīt failu

@@ -26,7 +26,8 @@ public class TriangleUtilitiesTest {
26 26
                         "******\n" +
27 27
                         "*******\n" +
28 28
                         "********\n" +
29
-                        "*********\n";
29
+                        "*********\n" +
30
+                        "**********\n";;
30 31
         String actual = TriangleUtilities.getTriangle(10);
31 32
         Assert.assertEquals(expected, actual);
32 33
     }
@@ -37,7 +38,8 @@ public class TriangleUtilitiesTest {
37 38
                 "*\n" +
38 39
                         "**\n" +
39 40
                         "***\n" +
40
-                        "****\n";
41
+                        "****\n" +
42
+                        "*****\n";
41 43
         String actual = TriangleUtilities.getTriangle(5);
42 44
         Assert.assertEquals(expected, actual);
43 45
     }
@@ -53,7 +55,8 @@ public class TriangleUtilitiesTest {
53 55
                 "******\n" +
54 56
                 "*******\n" +
55 57
                 "********\n" +
56
-                "*********\n";
58
+                "*********\n" +
59
+                "**********\n";
57 60
         String actual = TriangleUtilities.getLargeTriangle();
58 61
         Assert.assertEquals(expected, actual);
59 62
     }