瀏覽代碼

Stars, tables, etc

NedRedmond 6 年之前
父節點
當前提交
4c2397efc5
共有 4 個文件被更改,包括 101 次插入25 次删除
  1. 35
    7
      NumberUtilities.java
  2. 28
    5
      TableUtilities.java
  3. 29
    4
      TriangleUtilities.java
  4. 9
    9
      package.bluej

+ 35
- 7
NumberUtilities.java 查看文件

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

+ 28
- 5
TableUtilities.java 查看文件

@@ -1,15 +1,38 @@
1
- 
2 1
 
3
-public class TableUtilities {
2
+public class TableUtilities {    
4 3
     public static String getSmallMultiplicationTable() {
5
-        return null;
4
+        String table = "";
5
+        for (int i = 1; i <= 5; i++) {
6
+            for (int j = 1; j <= 5; j++) {
7
+                table += String.format("%3s |", i*j);
8
+            }
9
+            table += "\n";
10
+        };
11
+
12
+        return table;
6 13
     }
7 14
 
8 15
     public static String getLargeMultiplicationTable() {
9
-        return null;
16
+        String table = "";
17
+        for (int i = 1; i <= 10; i++) {
18
+            for (int j = 1; j <= 10; j++) {
19
+                table += String.format("%3s |", i*j);
20
+            }
21
+            table += "\n";
22
+        };
23
+
24
+        return table;
10 25
     }
11 26
 
12 27
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
28
+        String table = "";
29
+        for (int i = 1; i <= tableSize; i++) {
30
+            for (int j = 1; j <= tableSize; j++) {
31
+                table += String.format("%3s |", i*j);
32
+            }
33
+            table += "\n";
34
+        };
35
+
36
+        return table;
14 37
     }
15 38
 }

+ 29
- 4
TriangleUtilities.java 查看文件

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

+ 9
- 9
package.bluej 查看文件

@@ -1,22 +1,22 @@
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
-editor.fx.0.width=800
13
-editor.fx.0.x=537
12
+editor.fx.0.width=854
13
+editor.fx.0.x=529
14 14
 editor.fx.0.y=28
15
-objectbench.height=164
15
+objectbench.height=165
16 16
 objectbench.width=484
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7560627674750356
19
-package.editor.height=523
18
+package.divider.vertical=0.7546362339514978
19
+package.editor.height=522
20 20
 package.editor.width=382
21 21
 package.editor.x=20
22 22
 package.editor.y=57