瀏覽代碼

Loops LAb

Brandon Defrancis 6 年之前
父節點
當前提交
29d7b6d01b
共有 4 個文件被更改,包括 115 次插入28 次删除
  1. 39
    7
      NumberUtilities.java
  2. 30
    3
      TableUtilities.java
  3. 32
    4
      TriangleUtilities.java
  4. 14
    14
      package.bluej

+ 39
- 7
NumberUtilities.java 查看文件

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

+ 30
- 3
TableUtilities.java 查看文件

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

+ 32
- 4
TriangleUtilities.java 查看文件

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

+ 14
- 14
package.bluej 查看文件

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=722
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
16
-objectbench.width=484
13
+editor.fx.0.x=752
14
+editor.fx.0.y=-956
15
+objectbench.height=165
16
+objectbench.width=821
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
18
+package.divider.vertical=0.7546362339514978
19
+package.editor.height=522
20
+package.editor.width=719
21
+package.editor.x=-155
22
+package.editor.y=-970
23
 package.frame.height=759
23
 package.frame.height=759
24
-package.frame.width=508
24
+package.frame.width=845
25
 package.numDependencies=3
25
 package.numDependencies=3
26
 package.numTargets=6
26
 package.numTargets=6
27
 package.showExtends=true
27
 package.showExtends=true