Explorar el Código

Merge 92aee78fc9c49be6801c3e623585706ac9a45fb1 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

EricBarnaba hace 6 años
padre
commit
930c8b79b1
Ninguna cuenta está vinculada al correo electrónico del colaborador

+ 30
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Ver fichero

@@ -3,17 +3,32 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 
4 4
 public class NumberUtilities {
5 5
     public static String getEvenNumbers(int start, int stop) {
6
-        return null;
6
+        StringBuilder answer = new StringBuilder();
7
+
8
+        for (int i = start%2 == 1 ? start+1 : start; i < stop; i = i +2){
9
+            answer.append(i);
10
+        }
11
+        return answer.toString();
7 12
     }
8 13
 
9 14
 
10 15
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
16
+        StringBuilder answer = new StringBuilder();
17
+
18
+        for (int i = start%2 == 0 ? start+1 : start; i < stop; i = i +2){
19
+            answer.append(i);
20
+        }
21
+        return answer.toString();
12 22
     }
13 23
 
14 24
 
15 25
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
26
+        StringBuilder answer = new StringBuilder();
27
+
28
+        for (int i = start; i < stop; i = i +step){
29
+            answer.append(i*i);
30
+        }
31
+        return answer.toString();
17 32
     }
18 33
 
19 34
     public static String getRange(int start) {
@@ -26,11 +41,21 @@ public class NumberUtilities {
26 41
 
27 42
 
28 43
     public static String getRange(int start, int stop, int step) {
29
-        return null;
44
+        StringBuilder answer = new StringBuilder();
45
+
46
+        for (int i = start; i < stop; i = i +step){
47
+            answer.append(i);
48
+        }
49
+        return answer.toString();
30 50
     }
31 51
 
32 52
 
33 53
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
54
+        StringBuilder answer = new StringBuilder();
55
+
56
+        for (int i = start; i < stop; i = i +step){
57
+            answer.append((int) Math.pow(i,exponent));
58
+        }
59
+        return answer.toString();
35 60
     }
36 61
 }

+ 63
- 3
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Ver fichero

@@ -2,14 +2,74 @@ package io.zipcoder.microlabs.mastering_loops;
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        StringBuilder answer = new StringBuilder();
6
+
7
+        int columnCounter = 1;
8
+        int rowCounter = 1;
9
+        int input = 1;
10
+        for (int i = 1; i<= 25; i++){
11
+
12
+            String cell = String.format("%3d |", input);
13
+            answer.append(cell);
14
+            columnCounter++;
15
+            input = rowCounter*columnCounter;
16
+            if (columnCounter == 6){
17
+                answer.append("\n");
18
+                columnCounter = 1;
19
+                rowCounter++;
20
+                input = rowCounter;
21
+
22
+            }
23
+        }
24
+
25
+        return answer.toString();
6 26
     }
7 27
 
8 28
     public static String getLargeMultiplicationTable() {
9
-        return null;
29
+        StringBuilder answer = new StringBuilder();
30
+
31
+        int columnCounter = 1;
32
+        int rowCounter = 1;
33
+        int input = 1;
34
+        for (int i = 1; i<= 100; i++){
35
+
36
+            String cell = String.format("%3d |", input);
37
+            answer.append(cell);
38
+            columnCounter++;
39
+            input = rowCounter*columnCounter;
40
+            if (columnCounter == 11){
41
+                answer.append("\n");
42
+                columnCounter = 1;
43
+                rowCounter++;
44
+                input = rowCounter;
45
+
46
+            }
47
+        }
48
+
49
+        return answer.toString();
10 50
     }
11 51
 
12 52
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
53
+        StringBuilder answer = new StringBuilder();
54
+
55
+        int columnCounter = 1;
56
+        int rowCounter = 1;
57
+        int input = 1;
58
+        for (int i = 1; i<= tableSize*tableSize; i++){
59
+
60
+            String cell = String.format("%3d |", input);
61
+            answer.append(cell);
62
+            columnCounter++;
63
+            input = rowCounter*columnCounter;
64
+            if (columnCounter == tableSize+1){
65
+                answer.append("\n");
66
+                columnCounter = 1;
67
+                rowCounter++;
68
+                input = rowCounter;
69
+
70
+            }
71
+        }
72
+
73
+        return answer.toString();
14 74
     }
15 75
 }

+ 47
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Ver fichero

@@ -3,18 +3,61 @@ package io.zipcoder.microlabs.mastering_loops;
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getTriangle(int numberOfRows) {
6
-        return null;
6
+        StringBuilder answer = new StringBuilder();
7
+        int rowCounter = 1;
8
+        for(int i = 1; i<= numberOfRows-1; i++){
9
+            for(int j = 1; j<=rowCounter; j++) {
10
+                answer.append("*");
11
+                if (j == rowCounter) {
12
+                    answer.append("\n");
13
+                    rowCounter++;
14
+                    break;
15
+                }
16
+            }
17
+        }
18
+        return answer.toString();
7 19
     }
8 20
 
9 21
     public static String getRow(int numberOfStars) {
10
-        return null;
22
+        StringBuilder answer = new StringBuilder();
23
+        for(int i = 1; i<= numberOfStars; i++){
24
+            answer.append("*");
25
+        }
26
+        return answer.toString();
11 27
     }
12 28
 
13 29
     public static String getSmallTriangle() {
14
-        return null;
30
+        StringBuilder answer = new StringBuilder();
31
+        int rowCounter = 1;
32
+        for(int i = 1; i<= 4; i++){
33
+            for(int j = 1; j<=rowCounter; j++) {
34
+                answer.append("*");
35
+                if (j == rowCounter) {
36
+                    answer.append("\n");
37
+                    rowCounter++;
38
+                    break;
39
+                }
40
+            }
41
+        }
42
+        return answer.toString();
43
+
15 44
     }
16 45
 
17 46
     public static String getLargeTriangle() {
18
-        return null;
47
+
48
+        StringBuilder answer = new StringBuilder();
49
+        int rowCounter = 1;
50
+        for(int i = 1; i<= 9; i++){
51
+            for(int j = 1; j<=rowCounter; j++) {
52
+                answer.append("*");
53
+                if (j == rowCounter) {
54
+                    answer.append("\n");
55
+                    rowCounter++;
56
+                    break;
57
+                }
58
+            }
59
+        }
60
+        return answer.toString();
61
+
19 62
     }
20 63
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Ver fichero

@@ -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,7 +187,7 @@ 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 193
         int step = 5;