Explorar el Código

Merge 35a1a77ac2be4cad8ccd07c291d50d86c0590add into 24118cb7b3c6ce6a0b3be331c7987add797baa81

ahsonali hace 6 años
padre
commit
07666c7155
Ninguna cuenta está vinculada al correo electrónico del colaborador

+ 2
- 0
src/main/java/io/zipcoder/microlabs/mastering_loops/MainApp.java Ver fichero

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

+ 50
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Ver fichero

@@ -3,17 +3,54 @@ 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
+
7
+        String outcome = "";
8
+
9
+        for (int i = start; i <= stop; i++){
10
+
11
+            if (i % 2 == 0)
12
+            {
13
+
14
+            outcome += i;
15
+
16
+            }
17
+
18
+        }
19
+
20
+        return outcome;
7 21
     }
8 22
 
9 23
 
10 24
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
25
+
26
+        String outcome = "";
27
+
28
+        for (int i = start; i <= stop; i++)
29
+
30
+        {
31
+            if (i % 2 != 0)
32
+            {
33
+
34
+                outcome += i;
35
+
36
+            }
37
+        }
38
+
39
+
40
+        return outcome;
12 41
     }
13 42
 
14 43
 
15 44
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
45
+
46
+        String outcome = "";
47
+
48
+        for (int i = start; i < stop; i += step)
49
+        {
50
+             outcome += i * i;
51
+        }
52
+
53
+        return outcome;
17 54
     }
18 55
 
19 56
     public static String getRange(int start) {
@@ -26,7 +63,16 @@ public class NumberUtilities {
26 63
 
27 64
 
28 65
     public static String getRange(int start, int stop, int step) {
29
-        return null;
66
+
67
+        String outcome = "";
68
+
69
+        for (int i = start; i < stop; i += step)
70
+        {
71
+            outcome += i;
72
+
73
+        }
74
+
75
+        return outcome;
30 76
     }
31 77
 
32 78
 

+ 65
- 6
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Ver fichero

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

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

@@ -3,18 +3,38 @@ 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
+
7
+        String triangle = "";
8
+
9
+        for (int i = 1; i < numberOfRows; i++){
10
+
11
+            triangle += TriangleUtilities.getRow(i) + "\n";
12
+        }
13
+
14
+        return triangle;
7 15
     }
8 16
 
9 17
     public static String getRow(int numberOfStars) {
10
-        return null;
18
+
19
+        String row = "";
20
+
21
+        for (int i = 1; i <= numberOfStars; i++)
22
+        {
23
+            row += "*";
24
+
25
+        }
26
+
27
+        return row;
11 28
     }
12 29
 
30
+    //
13 31
     public static String getSmallTriangle() {
14
-        return null;
32
+
33
+       return getTriangle(5);
15 34
     }
16 35
 
17 36
     public static String getLargeTriangle() {
18
-        return null;
37
+
38
+        return getTriangle(10);
19 39
     }
20 40
 }

+ 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;