#29 Loops

Otwarty
zmalone chce scalić 2 commity/ów z zmalone/CR-MicroLabs-Loops-NumbersTrianglesTables:master do master
5 zmienionych plików z 149 dodań i 40 usunięć
  1. 49
    13
      NumberUtilities.java
  2. 6
    0
      NumberUtilitiesTest.java
  3. 39
    4
      TableUtilities.java
  4. 40
    8
      TriangleUtilities.java
  5. 15
    15
      package.bluej

+ 49
- 13
NumberUtilities.java Wyświetl plik

@@ -1,32 +1,68 @@
1
- 
2 1
 
3 2
 
4 3
 public class NumberUtilities {
5
-    
4
+
6 5
     public static String getRange(int start) {
7
-        return null;
6
+        String range = "";
7
+        for(int i= 0; i<start; i++ ) {
8
+            range+=i;
9
+        }
10
+        return range;
8 11
     }
9
-    
12
+
10 13
     public static String getRange(int start, int stop) {
11
-        return null;
14
+        String range = "";
15
+        for( int i=start; i<stop; i++) {
16
+            range+=i;
17
+        }
18
+        return range;
12 19
     }
13 20
 
14
-
15 21
     public static String getRange(int start, int stop, int step) {
16
-        return null;
22
+        String range = "";
23
+        for(int i=start; i<stop; i+=step ) {
24
+            range+=i;
25
+        }
26
+        return range;
17 27
     }
18
-    
28
+
19 29
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
21
-    }
30
+        String evenNumbers = "";
31
+        for (int i=start; i<stop; i++){
32
+            int remainder= i%2;
33
+            if (remainder==0 ){
34
+                evenNumbers+=i;
35
+            }
36
+        }
22 37
 
38
+        return evenNumbers;
39
+    }
23 40
 
24 41
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
26
-    }
42
+        String oddNumbers = "";
43
+        for (int i=start; i<stop; i++){
44
+            int remainder= i%2;
45
+            if (remainder==1 ){
46
+                oddNumbers+=i;
47
+            }
48
+        }
27 49
 
50
+        return oddNumbers;
51
+    }
28 52
 
29 53
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
54
+        String result = "";
55
+        
56
+        for(int i=start; i<= stop; i++){
57
+            int sum = 1;
58
+            for(int j=1; j<=exponent; j++){
59
+                sum*=i;
60
+            }
61
+            
62
+            result+=sum;
63
+            
64
+        }
65
+
66
+        return result;
31 67
     }
32 68
 }

+ 6
- 0
NumberUtilitiesTest.java Wyświetl plik

@@ -196,4 +196,10 @@ public class NumberUtilitiesTest {
196 196
         // : Then
197 197
         Assert.assertEquals(expected, actual);
198 198
     }
199
+
200
+    @Test
201
+    public void test1()
202
+    {
203
+    }
199 204
 }
205
+

+ 39
- 4
TableUtilities.java Wyświetl plik

@@ -1,15 +1,50 @@
1
- 
2 1
 
3 2
 public class TableUtilities {
4 3
     public static String getSmallMultiplicationTable() {
5
-        return null;
4
+        StringBuilder sb = new StringBuilder();
5
+        
6
+        int i;
7
+        int z;
8
+        for (i=1; i<=5; ++i ){
9
+            for (z=1; z<=5; ++z){
10
+                sb.append(String.format("%3s |",(i*z)));
11
+
12
+            } 
13
+            sb.append("\n");
14
+            
15
+        }
16
+        return sb.toString();
6 17
     }
7 18
 
8 19
     public static String getLargeMultiplicationTable() {
9
-        return null;
20
+        StringBuilder sb = new StringBuilder();
21
+        
22
+        int i;
23
+        int z;
24
+        for (i=1; i<=10; ++i ){
25
+            for (z=1; z<=10; ++z){
26
+                sb.append(String.format("%3s |",(i*z)));
27
+
28
+            } 
29
+            sb.append("\n");
30
+            
31
+        }
32
+        return sb.toString();
10 33
     }
11 34
 
12 35
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
36
+        StringBuilder sb = new StringBuilder();
37
+        int width=tableSize;
38
+        int i;
39
+        int z;
40
+        for (i=1; i<=width; ++i ){
41
+            for (z=1; z<=width; ++z){
42
+                sb.append(String.format("%3s |",(i*z)));
43
+
44
+            } 
45
+            sb.append("\n");
46
+            
47
+        }
48
+        return sb.toString();
14 49
     }
15 50
 }

+ 40
- 8
TriangleUtilities.java Wyświetl plik

@@ -1,21 +1,53 @@
1
- 
2 1
 
3 2
 public class TriangleUtilities {
4
-
5 3
     public static String getRow(int numberOfStars) {
6
-        return null;
4
+        String result= "";
5
+        for(int i=0; i<numberOfStars; i++) {
6
+            result+= "*";
7
+        }
8
+
9
+        return result;
7 10
     }
8
-    
11
+
9 12
     public static String getTriangle(int numberOfRows) {
10
-        return null;
11
-    }
13
+        String triangle="";
14
+        for(int i=1; i<=numberOfRows; i++) {
15
+            for(int z=1; z<=i; z++){
16
+                triangle += "*";
12 17
 
18
+            }
19
+            triangle+="\n";
20
+
21
+        }
22
+
23
+        return triangle;
24
+    }
13 25
 
14 26
     public static String getSmallTriangle() {
15
-        return null;
27
+        String triangle="";
28
+        for(int i=1; i<=4; i++) {
29
+            for(int z=1; z<=i; z++){
30
+                triangle += "*";
31
+
32
+            }
33
+            triangle+="\n";
34
+
35
+        }
36
+
37
+        return triangle;
16 38
     }
17 39
 
18 40
     public static String getLargeTriangle() {
19
-        return null;
41
+        String triangle="";
42
+        for(int i=1; i<=9; i++) {
43
+            for(int z=1; z<=i; z++){
44
+                triangle += "*";
45
+
46
+            }
47
+            triangle+="\n";
48
+
49
+        }
50
+
51
+        return triangle;
20 52
     }
21 53
 }

+ 15
- 15
package.bluej Wyświetl plik

@@ -1,27 +1,27 @@
1 1
 #BlueJ package file
2
-dependency1.from=NumberUtilitiesTest
3
-dependency1.to=NumberUtilities
2
+dependency1.from=TableUtilitiesTest
3
+dependency1.to=TableUtilities
4 4
 dependency1.type=UsesDependency
5 5
 dependency2.from=TriangleUtilitiesTest
6 6
 dependency2.to=TriangleUtilities
7 7
 dependency2.type=UsesDependency
8
-dependency3.from=TableUtilitiesTest
9
-dependency3.to=TableUtilities
8
+dependency3.from=NumberUtilitiesTest
9
+dependency3.to=NumberUtilities
10 10
 dependency3.type=UsesDependency
11 11
 editor.fx.0.height=722
12 12
 editor.fx.0.width=800
13
-editor.fx.0.x=537
14
-editor.fx.0.y=28
15
-objectbench.height=164
16
-objectbench.width=484
13
+editor.fx.0.x=240
14
+editor.fx.0.y=24
15
+objectbench.height=175
16
+objectbench.width=727
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7560627674750356
19
-package.editor.height=523
20
-package.editor.width=382
21
-package.editor.x=20
22
-package.editor.y=57
23
-package.frame.height=759
24
-package.frame.width=508
18
+package.divider.vertical=0.5550122249388753
19
+package.editor.height=220
20
+package.editor.width=609
21
+package.editor.x=32
22
+package.editor.y=140
23
+package.frame.height=467
24
+package.frame.width=751
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true