#31 4.2_JevitTith_Loops,Numbers,Triangles,Tables_Lab

開啟中
jtith 請求將 1 次程式碼提交從 jtith/CR-MicroLabs-Loops-NumbersTrianglesTables:master 合併至 master
共有 4 個文件被更改,包括 88 次插入47 次删除
  1. 51
    27
      NumberUtilities.java
  2. 10
    3
      TableUtilities.java
  3. 14
    4
      TriangleUtilities.java
  4. 13
    13
      package.bluej

+ 51
- 27
NumberUtilities.java 查看文件

@@ -1,32 +1,56 @@
1
- 
2
-
3 1
 
4 2
 public class NumberUtilities {
5
-    
6
-    public static String getRange(int start) {
7
-        return null;
8
-    }
9
-    
10
-    public static String getRange(int start, int stop) {
11
-        return null;
12
-    }
13
-
14
-
15
-    public static String getRange(int start, int stop, int step) {
16
-        return null;
17
-    }
18
-    
19
-    public static String getEvenNumbers(int start, int stop) {
20
-        return null;
21
-    }
22
-
23
-
24
-    public static String getOddNumbers(int start, int stop) {
25
-        return null;
26
-    }
27 3
 
4
+   public static String getRange(int stop) {
5
+       String rangeOfNumbers = "";
6
+       for (int i = 0;i < stop; i++){
7
+       rangeOfNumbers = rangeOfNumbers + i;
8
+       }
9
+       return rangeOfNumbers;
10
+   }
11
+
12
+   public static String getRange(int start, int stop) {
13
+       String rangeOfNumbers = "";
14
+       for (int i = start;i < stop; i++){
15
+       rangeOfNumbers = rangeOfNumbers + i;
16
+       }
17
+       return rangeOfNumbers;
18
+   }
19
+
20
+
21
+   public static String getRange(int start, int stop, int step) {
22
+       String rangeOfNumbers = "";
23
+       for (int i = start;i < stop; i += step){
24
+       rangeOfNumbers = rangeOfNumbers + i;
25
+       }
26
+       return rangeOfNumbers;
27
+   }
28
+
29
+   public static String getEvenNumbers(int start, int stop) {
30
+       String evenNumbers="";
31
+       for (int i = start; i < stop; i++)
32
+       if (i % 2 == 0){
33
+         evenNumbers = evenNumbers + i;
34
+       }
35
+       return evenNumbers;
36
+   }
37
+
38
+
39
+   public static String getOddNumbers(int start, int stop) {
40
+       String oddNumbers="";
41
+       for (int i = start; i < stop; i++)
42
+       if (i % 2 != 0){
43
+         oddNumbers = oddNumbers+i;
44
+       } return oddNumbers;
45
+   }
46
+
47
+
48
+   public static String getExponentiations(int start, int stop, int exponent)  {
49
+       String exponents="";
50
+       for (int i = start; i <= stop; i++) {
51
+       exponents += (int)Math.pow(i,exponent);
52
+   }
53
+       return exponents;
28 54
 
29
-    public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
31
-    }
32 55
 }
56
+}

+ 10
- 3
TableUtilities.java 查看文件

@@ -2,14 +2,21 @@
2 2
 
3 3
 public class TableUtilities {
4 4
     public static String getSmallMultiplicationTable() {
5
-        return null;
5
+        return getMultiplicationTable(5);
6 6
     }
7 7
 
8 8
     public static String getLargeMultiplicationTable() {
9
-        return null;
9
+        return getMultiplicationTable(10);
10 10
     }
11 11
 
12 12
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
13
+        StringBuilder sb = new StringBuilder();
14
+        for (int column =1; column <= tableSize; column++) {
15
+            for (int row =1; row<=tableSize; row++){
16
+                sb.append(String.format("%3d |", row * column));
17
+            }
18
+            sb.append("\n");
19
+        }
20
+        return sb.toString();
14 21
     }
15 22
 }

+ 14
- 4
TriangleUtilities.java 查看文件

@@ -3,19 +3,29 @@
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getRow(int numberOfStars) {
6
-        return null;
6
+        String stars = "";
7
+        for (int i =0; i<numberOfStars;i++){
8
+            stars = stars + "*";
9
+        } return stars;
7 10
     }
8 11
     
9 12
     public static String getTriangle(int numberOfRows) {
10
-        return null;
13
+        String string = "";
14
+        for (int column = 0; column < numberOfRows; column++){
15
+            for(int row = 0; row < column + 1; row++){
16
+                string = string + "*";
17
+            }
18
+            string = string + "\n";
19
+        } 
20
+        return string;
11 21
     }
12 22
 
13 23
 
14 24
     public static String getSmallTriangle() {
15
-        return null;
25
+        return getTriangle(4);
16 26
     }
17 27
 
18 28
     public static String getLargeTriangle() {
19
-        return null;
29
+        return getTriangle(9);
20 30
     }
21 31
 }

+ 13
- 13
package.bluej 查看文件

@@ -1,23 +1,23 @@
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
-editor.fx.0.height=722
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
11
+editor.fx.0.height=0
12
+editor.fx.0.width=0
13
+editor.fx.0.x=0
14
+editor.fx.0.y=0
15
+objectbench.height=154
16
+objectbench.width=468
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7560627674750356
19
-package.editor.height=523
20
-package.editor.width=382
18
+package.divider.vertical=0.7556904400606981
19
+package.editor.height=491
20
+package.editor.width=378
21 21
 package.editor.x=20
22 22
 package.editor.y=57
23 23
 package.frame.height=759