瀏覽代碼

new commit for approval

Ben Blinebury 6 年之前
父節點
當前提交
2e61a2d6ee
共有 3 個檔案被更改,包括 73 行新增15 行删除
  1. 41
    7
      NumberUtilities.java
  2. 13
    3
      TableUtilities.java
  3. 19
    5
      TriangleUtilities.java

+ 41
- 7
NumberUtilities.java 查看文件

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

+ 13
- 3
TableUtilities.java 查看文件

@@ -2,14 +2,24 @@
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
+        
10
+        return getMultiplicationTable(10);
10 11
     }
11 12
 
12 13
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
14
+        StringBuilder rows = new StringBuilder();
15
+        
16
+        for (int i = 1; i <= tableSize; i++){
17
+            for (int j = 1; j <= tableSize; j++) {
18
+                rows.append(String.format("%3d |", i*j));
19
+                
20
+            }
21
+            rows.append("\n");
22
+        }
23
+        return rows.toString();
14 24
     }
15 25
 }

+ 19
- 5
TriangleUtilities.java 查看文件

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