Quellcode durchsuchen

finished tables and triangles and refactored numbers

Dominique Clarke vor 6 Jahren
Ursprung
Commit
78ce0ff5eb

+ 3
- 0
src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java Datei anzeigen

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

+ 27
- 80
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Datei anzeigen

@@ -2,125 +2,72 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class NumberUtilities {
4 4
     public static String getEvenNumbers(int start, int stop) {
5
-        int i = 0;
6
-        int j = start;
7
-        StringBuilder builder = new StringBuilder();
8
-
9
-        while(j < stop) {
10
-            if(j % 2 == 0)
11
-            {
12
-                builder.append(j);
13
-                j+=2;
14
-                i++;
15
-            }
16
-            else
17
-            {
18
-                j++;
19
-            }
20
-        }
21
-
22
-        return builder.toString();
5
+        return getNumberSequence(start, stop, true);
23 6
     }
24 7
 
25
-
26 8
     public static String getOddNumbers(int start, int stop) {
27
-        int i = 0;
28
-        int j = start;
29
-        StringBuilder builder = new StringBuilder();
30
-
31
-        while(j < stop)
32
-        {
33
-            if(j % 2 != 0)
34
-            {
35
-                builder.append(j);
36
-                j+=2;
37
-                i++;
38
-            }
39
-            else
40
-            {
41
-                j++;
42
-            }
43
-        }
44
-
45
-        return builder.toString();
9
+        return getNumberSequence(start, stop, false);
46 10
     }
47 11
 
48 12
     public static String getSquareNumbers(int start, int stop) {
49
-        int i = start;
50
-        StringBuilder builder = new StringBuilder();
51
-
52
-        while(i < stop)
53
-        {
54
-            builder.append(i * i);
55
-            i++;
56
-        }
57
-
58
-        return builder.toString();
13
+        return getSquareNumbers(start, stop, 1);
59 14
     }
60 15
 
61
-
62 16
     public static String getSquareNumbers(int start, int stop, int step) {
63
-        int i = start;
64
-        StringBuilder builder = new StringBuilder();
65
-
66
-        while(i < stop)
67
-        {
68
-            builder.append(i * i);
69
-            i += step;
70
-        }
71
-
72
-        return builder.toString();
17
+        return getExponentiations(start, stop, step, 2);
73 18
     }
74 19
 
75 20
     public static String getRange(int stop) {
76
-        int i = 0;
77
-        StringBuilder builder = new StringBuilder();
78
-
79
-        while(i < stop)
80
-        {
81
-            builder.append(i);
82
-            i++;
83
-        }
84
-
85
-        return builder.toString();
21
+        return getRange(0, stop, 1);
86 22
     }
87 23
 
88 24
     public static String getRange(int start, int stop) {
25
+        return getRange(start, stop, 1);
26
+    }
27
+
28
+    public static String getRange(int start, int stop, int step) {
89 29
         int i = start;
90 30
         StringBuilder builder = new StringBuilder();
91 31
 
92 32
         while(i < stop)
93 33
         {
94 34
             builder.append(i);
95
-            i++;
35
+            i += step;
96 36
         }
97 37
 
98 38
         return builder.toString();
99 39
     }
100 40
 
101
-
102
-    public static String getRange(int start, int stop, int step) {
41
+    public static String getExponentiations(int start, int stop, int step, int exponent) {
103 42
         int i = start;
104 43
         StringBuilder builder = new StringBuilder();
105 44
 
106 45
         while(i < stop)
107 46
         {
108
-            builder.append(i);
109
-            i+=step;
47
+            builder.append((int)Math.pow(i, exponent));
48
+            i += step;
110 49
         }
111 50
 
112 51
         return builder.toString();
113 52
     }
114 53
 
115
-
116
-    public static String getExponentiations(int start, int stop, int step, int exponent) {
117
-        int i = start;
54
+    private static String getNumberSequence(int start, int stop, boolean isEven) {
55
+        int i = 0;
56
+        int j = start;
118 57
         StringBuilder builder = new StringBuilder();
119 58
 
120
-        while(i < stop)
59
+        while(j < stop)
121 60
         {
122
-            builder.append((int)Math.pow(i, exponent));
123
-            i += step;
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
+            }
124 71
         }
125 72
 
126 73
         return builder.toString();

+ 29
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Datei anzeigen

@@ -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 Datei anzeigen

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

+ 13
- 15
src/test/java/io/zipcoder/microlabs/mastering_loops/TableUtilitiesTest.java Datei anzeigen

@@ -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 Datei anzeigen

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