소스 검색

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,29 +4,61 @@
4 4
 public class NumberUtilities {
5 5
     
6 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 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 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 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 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 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,14 +2,41 @@
2 2
 
3 3
 public class TableUtilities {
4 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 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 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,19 +3,47 @@
3 3
 public class TriangleUtilities {
4 4
 
5 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 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 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 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,27 +1,27 @@
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 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 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 23
 package.frame.height=759
24
-package.frame.width=508
24
+package.frame.width=845
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true