Browse Source

First Commit

Madeline Bowe 6 years ago
parent
commit
4b351b37c2

+ 42
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java View File

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
 
40
 
20
     public static String getRange(int start, int stop, int step) {
41
     public static String getRange(int start, int stop, int step) {
21
-        return null;
42
+
43
+        StringBuilder sb  = new StringBuilder();
44
+
45
+        for (int i = start; i < stop; i += step) {
46
+            sb.append(i).toString();
47
+        }
48
+
49
+        return sb.toString();
50
+
22
     }
51
     }
23
 
52
 
24
 
53
 
54
+
25
     public static String getExponentiations(int start, int stop, int step, int exponent) {
55
     public static String getExponentiations(int start, int stop, int step, int exponent) {
26
-        return null;
56
+        StringBuilder sb  = new StringBuilder();
57
+
58
+        for (int i = start; i < stop; i += step) {
59
+            sb.append((int) Math.pow(i, exponent));
60
+        }
61
+
62
+        return sb.toString();
63
+
27
     }
64
     }
28
 }
65
 }

+ 45
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java View File

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
 }

+ 33
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java View File

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 = 1; row < numberOfRows; row++) {
8
+            for (int col = 0; col < row; col++) {
9
+                builder.append("*");
10
+            }
11
+            builder.append("\n");
12
+        }
13
+
14
+        return builder.toString();
15
+
7
     }
16
     }
8
 
17
 
9
     public static String getRow(int numberOfStars) {
18
     public static String getRow(int numberOfStars) {
10
-        return null;
19
+        String asterisk = "";
20
+        for (int i = 0; i < numberOfStars; i++) {
21
+            asterisk += "*";
22
+        }
23
+        return asterisk;
11
     }
24
     }
12
 
25
 
13
     public static String getSmallTriangle() {
26
     public static String getSmallTriangle() {
14
-        return null;
27
+        StringBuilder builder = new StringBuilder();
28
+        for (int row = 1; row <= 4; row++) {
29
+            for (int col = 0; col < row; col++) {
30
+                builder.append("*");
31
+            }
32
+            builder.append("\n");
33
+        }
34
+
35
+        return builder.toString();
15
     }
36
     }
16
 
37
 
17
     public static String getLargeTriangle() {
38
     public static String getLargeTriangle() {
18
-        return null;
39
+        StringBuilder builder = new StringBuilder();
40
+        for (int row = 1; row <= 9; row++) {
41
+            for (int col = 0; col < row; col++) {
42
+                builder.append("*");
43
+            }
44
+            builder.append("\n");
45
+        }
46
+
47
+        return builder.toString();
19
     }
48
     }
20
 }
49
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java View File

51
     @Test
51
     @Test
52
     public void testGetEvenNumbers() {
52
     public void testGetEvenNumbers() {
53
         // : Given
53
         // : Given
54
-        String expected = "5791113151719";
54
+        String expected = "681012141618";
55
         int start = 5;
55
         int start = 5;
56
         int stop = 20;
56
         int stop = 20;
57
 
57
 
65
     @Test
65
     @Test
66
     public void testGetOddNumbers() {
66
     public void testGetOddNumbers() {
67
         // : Given
67
         // : Given
68
-        String expected = "681012141618";
68
+        String expected = "5791113151719";
69
         int start = 5;
69
         int start = 5;
70
         int stop = 20;
70
         int stop = 20;
71
         int step = 5;
71
         int step = 5;