浏览代码

lab completed

ThuyKhong 6 年前
父节点
当前提交
a75df53999
共有 3 个文件被更改,包括 115 次插入17 次删除
  1. 42
    8
      NumberUtilities.java
  2. 36
    3
      TableUtilities.java
  3. 37
    6
      TriangleUtilities.java

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

+ 36
- 3
TableUtilities.java 查看文件

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

+ 37
- 6
TriangleUtilities.java 查看文件

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