#41 loops_triangels_mexiliang

Open
mexiliang wants to merge 3 commits from mexiliang/CR-MicroLabs-Loops-NumbersTrianglesTables:master into master
5 changed files with 154 additions and 32 deletions
  1. BIN
      .DS_Store
  2. 68
    9
      NumberUtilities.java
  3. 37
    3
      TableUtilities.java
  4. 33
    4
      TriangleUtilities.java
  5. 16
    16
      package.bluej

BIN
.DS_Store View File


+ 68
- 9
NumberUtilities.java View File

3
 
3
 
4
 public class NumberUtilities {
4
 public class NumberUtilities {
5
     
5
     
6
-    public static String getRange(int start) {
7
-        return null;
6
+    public static String getRange(int stop) {
7
+        StringBuilder anw = new StringBuilder();
8
+        for (int i=0; i<stop; i++){
9
+           
10
+            anw.append(i);
11
+    }
12
+       
13
+        
14
+      
15
+        return anw.toString();
8
     }
16
     }
9
     
17
     
10
     public static String getRange(int start, int stop) {
18
     public static String getRange(int start, int stop) {
11
-        return null;
19
+        StringBuilder anw = new StringBuilder();
20
+        for (int i=start; i<stop;i++){
21
+            anw.append(i);
22
+        }
23
+       
24
+        return anw.toString();
12
     }
25
     }
13
 
26
 
14
 
27
 
15
     public static String getRange(int start, int stop, int step) {
28
     public static String getRange(int start, int stop, int step) {
16
-        return null;
29
+        StringBuilder anw = new StringBuilder();
30
+        for (int i=start; i<stop; i=i+step){
31
+            anw.append(i);
32
+        }
33
+        return anw.toString();
17
     }
34
     }
18
     
35
     
19
     public static String getEvenNumbers(int start, int stop) {
36
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
37
+    StringBuilder anw = new StringBuilder();
38
+    if (!isEven(start)){
39
+        start += 1;
21
     }
40
     }
22
-
41
+          
42
+       for (int i=start;i<stop;i=i+2){
43
+         
44
+           anw.append(i);
45
+        }
46
+        
47
+    
48
+   
49
+    
50
+    return anw.toString();
51
+}
52
+    
53
+    
23
 
54
 
24
     public static String getOddNumbers(int start, int stop) {
55
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
56
+        StringBuilder anw = new StringBuilder();
57
+        if (isEven(start)){
58
+        start = start+1;
59
+        }
60
+        for (int i=start;i<stop;i=i+2){
61
+           
62
+            anw.append(i);
63
+        }
64
+        return anw.toString();
65
+    }
66
+   
67
+    public static boolean isEven(int num){
68
+        return num % 2 == 0;
26
     }
69
     }
27
 
70
 
28
-
71
+    public static String getSquareNumbers(int start, int stop){
72
+        StringBuilder anw = new StringBuilder();
73
+        for (int i=start;i<=stop;i++){
74
+            anw.append ((int) (Math.pow(i,2)));
75
+            
76
+        }
77
+        
78
+        return anw.toString();
79
+    }
80
+    
29
     public static String getExponentiations(int start, int stop, int exponent) {
81
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
82
+        StringBuilder anw = new StringBuilder();
83
+        for (int i=start; i<=stop; i++){
84
+            anw.append((int)(Math.pow(i,exponent)));
85
+        
86
+        
31
     }
87
     }
88
+        return anw.toString();
89
+}
90
+
32
 }
91
 }

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

+ 33
- 4
TriangleUtilities.java View File

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

+ 16
- 16
package.bluej View File

1
 #BlueJ package file
1
 #BlueJ package file
2
-dependency1.from=NumberUtilitiesTest
3
-dependency1.to=NumberUtilities
2
+dependency1.from=TableUtilitiesTest
3
+dependency1.to=TableUtilities
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
 dependency2.from=TriangleUtilitiesTest
5
 dependency2.from=TriangleUtilitiesTest
6
 dependency2.to=TriangleUtilities
6
 dependency2.to=TriangleUtilities
7
 dependency2.type=UsesDependency
7
 dependency2.type=UsesDependency
8
-dependency3.from=TableUtilitiesTest
9
-dependency3.to=TableUtilities
8
+dependency3.from=NumberUtilitiesTest
9
+dependency3.to=NumberUtilities
10
 dependency3.type=UsesDependency
10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=722
11
+editor.fx.0.height=709
12
 editor.fx.0.width=800
12
 editor.fx.0.width=800
13
-editor.fx.0.x=537
14
-editor.fx.0.y=28
15
-objectbench.height=164
13
+editor.fx.0.x=480
14
+editor.fx.0.y=23
15
+objectbench.height=152
16
 objectbench.width=484
16
 objectbench.width=484
17
 package.divider.horizontal=0.6
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
18
+package.divider.vertical=0.7557603686635944
19
+package.editor.height=485
20
+package.editor.width=383
21
+package.editor.x=146
22
+package.editor.y=23
23
+package.frame.height=709
24
 package.frame.width=508
24
 package.frame.width=508
25
 package.numDependencies=3
25
 package.numDependencies=3
26
 package.numTargets=6
26
 package.numTargets=6
43
 target2.name=TriangleUtilitiesTest
43
 target2.name=TriangleUtilitiesTest
44
 target2.showInterface=false
44
 target2.showInterface=false
45
 target2.type=UnitTestTargetJunit4
45
 target2.type=UnitTestTargetJunit4
46
-target2.width=120
46
+target2.width=130
47
 target2.x=100
47
 target2.x=100
48
 target2.y=160
48
 target2.y=160
49
 target3.association=TableUtilitiesTest
49
 target3.association=TableUtilitiesTest
74
 target6.name=TriangleUtilities
74
 target6.name=TriangleUtilities
75
 target6.showInterface=false
75
 target6.showInterface=false
76
 target6.type=ClassTarget
76
 target6.type=ClassTarget
77
-target6.width=120
77
+target6.width=130
78
 target6.x=70
78
 target6.x=70
79
 target6.y=190
79
 target6.y=190