Преглед на файлове

Merge ea5f5527a2e73f9fa0706be76778a529de797d27 into 24118cb7b3c6ce6a0b3be331c7987add797baa81

Katrice преди 6 години
родител
ревизия
d179a22efc
No account linked to committer's email

+ 51
- 5
src/main/java/io/zipcoder/microlabs/mastering_loops/NumberUtilities.java Целия файл

@@ -3,17 +3,48 @@ 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 evenvalue = "";
8
+
9
+        for (int i = start; i<stop; i++) {
10
+
11
+            if (i %2 == 0){
12
+
13
+                evenvalue += "" + i;
14
+            }
15
+        }
16
+        return evenvalue;
17
+
7 18
     }
8 19
 
9 20
 
10 21
     public static String getOddNumbers(int start, int stop) {
11
-        return null;
22
+
23
+        String oddvalue = "";
24
+
25
+        for (int i = start; i < stop; i++) {
26
+
27
+            if (i%2 != 0 ) {
28
+
29
+                oddvalue += "" + i;
30
+
31
+            }
32
+        }
33
+        return oddvalue;
34
+
12 35
     }
13 36
 
14 37
 
15 38
     public static String getSquareNumbers(int start, int stop, int step) {
16
-        return null;
39
+
40
+        String storesqnumbers = "";
41
+    //int i is equal and while i is less then stop, i increases by steps increments(may have worded wrong)
42
+        for (int i = start; i < stop; i += step) {
43
+    //I am now concatenating into my storesqnumbers and I had to cast, on Math.pow because I was getting a float
44
+                storesqnumbers += + (int)Math.pow(i,2);
45
+        }
46
+        //returning storesqnumbers
47
+        return storesqnumbers;
17 48
     }
18 49
 
19 50
     public static String getRange(int start) {
@@ -26,11 +57,26 @@ public class NumberUtilities {
26 57
 
27 58
 
28 59
     public static String getRange(int start, int stop, int step) {
29
-        return null;
60
+
61
+    String holder = "";
62
+
63
+    for(int i = start; i < stop; i += step) {
64
+
65
+        holder += i;
66
+    }
67
+
68
+        return holder;
30 69
     }
31 70
 
32 71
 
33 72
     public static String getExponentiations(int start, int stop, int step, int exponent) {
34
-        return null;
73
+
74
+        String exp_num = "";
75
+
76
+        for (int i = start; i < stop; i += step){
77
+            exp_num += "" +(int)Math.pow(i, exponent);
78
+        }
79
+        return exp_num;
35 80
     }
81
+
36 82
 }

+ 56
- 4
src/main/java/io/zipcoder/microlabs/mastering_loops/TableUtilities.java Целия файл

@@ -1,15 +1,67 @@
1 1
 package io.zipcoder.microlabs.mastering_loops;
2 2
 
3
+
3 4
 public class TableUtilities {
4 5
     public static String getSmallMultiplicationTable() {
5
-        return null;
6
+
7
+        //so I don't have to concatenate every string, I named it times
8
+        //my first for loop, created to look ht(height), the dimension is defined in the middle
9
+        //my 2nd for loop, created to look width(width), the dimension is defined in the middle
10
+        /*I actually got this portion from student help but I know what it means. The append
11
+         goes along with the string builder and times is just the title. String format will create a
12
+         formatted string without having to printf it. The %3 represents the field width of, "d" is decimal
13
+         integer, space, pipe, by the value of 1.
14
+         */
15
+        //and then we are returning times to a string format and it passed.
16
+
17
+        //you have to append outside the inside loop because that loop PRINTS A ROW!
18
+
19
+        StringBuilder times = new StringBuilder();
20
+
21
+        for (int ht=1; ht<6; ht++){
22
+            for (int wd=1; wd<6; wd++){
23
+                times.append(String.format("%3d |", wd*ht));
24
+            }
25
+            times.append("\n");
26
+        }
27
+        return times.toString();
6 28
     }
7 29
 
30
+
31
+
32
+
33
+
8 34
     public static String getLargeMultiplicationTable() {
9
-        return null;
35
+        StringBuilder timest = new StringBuilder();
36
+
37
+        for (int h=1; h<11; h++){
38
+            for (int w=1; w<11; w++){
39
+                timest.append(String.format("%3d |", w*h));
40
+            }
41
+            timest.append("\n");
42
+        }
43
+        return timest.toString();
10 44
     }
11 45
 
46
+
47
+
12 48
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
49
+        StringBuilder multit = new StringBuilder();
50
+
51
+        for (int n=1; n<21; n++){
52
+            for (int w=1; w<21; w++){
53
+                multit.append(String.format("%3d |", w*n));
54
+            }
55
+            multit.append("\n");
56
+        }
57
+        return multit.toString();
14 58
     }
15
-}
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+    }
67
+

+ 43
- 6
src/main/java/io/zipcoder/microlabs/mastering_loops/TriangleUtilities.java Целия файл

@@ -2,19 +2,56 @@ 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;
7
-    }
5
+    public static String getTriangle(int numberOfRows) { return null; }
8 6
 
9 7
     public static String getRow(int numberOfStars) {
10
-        return null;
8
+
9
+        //The aim of using StringBuilder, i.e reducing memory. Is it achieved?
10
+        //adding numberOfStars to StringBuilder allows for the length to be known up front
11
+        StringBuilder stars = new StringBuilder(numberOfStars);
12
+        //now we want to add * to the stars StringBuilder for the length of numberOfStars
13
+        stars.append("*");
14
+        //now we are returning our stars back to a string and it passes!!
15
+        return stars.toString();
16
+
11 17
     }
12 18
 
13 19
     public static String getSmallTriangle() {
14
-        return null;
20
+
21
+        StringBuilder shinyStar = new StringBuilder();
22
+
23
+        int h, w;
24
+        for (h = 1 ; h <= 4 ; h++) {
25
+            for (w = 1 ; w <= h ; w++) {
26
+                shinyStar.append("*");
27
+            }
28
+            shinyStar.append("\n");
29
+        }
30
+
31
+        return shinyStar.toString();
15 32
     }
16 33
 
34
+
35
+
36
+
17 37
     public static String getLargeTriangle() {
18
-        return null;
38
+
39
+        StringBuilder bigStar = new StringBuilder();
40
+
41
+        int ht, wd;
42
+        for (ht = 1 ; ht <= 10 ; ht++) {
43
+            for (wd = 1 ; wd <= ht ; wd++) {
44
+                bigStar.append("*");
45
+            }
46
+            bigStar.append("\n");
47
+        }
48
+
49
+        return bigStar.toString();
50
+
51
+
52
+
53
+
54
+
55
+
19 56
     }
20 57
 }

+ 2
- 2
src/test/java/io/zipcoder/microlabs/mastering_loops/NumberUtilitiesTest.java Целия файл

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