Quellcode durchsuchen

Finished Loops, etc

Nathan Hall vor 6 Jahren
Ursprung
Commit
d1cca3fcd1
4 geänderte Dateien mit 145 neuen und 27 gelöschten Zeilen
  1. 46
    6
      NumberUtilities.java
  2. 40
    3
      TableUtilities.java
  3. 46
    5
      TriangleUtilities.java
  4. 13
    13
      package.bluej

+ 46
- 6
NumberUtilities.java Datei anzeigen

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

+ 40
- 3
TableUtilities.java Datei anzeigen

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

+ 46
- 5
TriangleUtilities.java Datei anzeigen

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

+ 13
- 13
package.bluej Datei anzeigen

@@ -2,25 +2,25 @@
2 2
 dependency1.from=NumberUtilitiesTest
3 3
 dependency1.to=NumberUtilities
4 4
 dependency1.type=UsesDependency
5
-dependency2.from=TriangleUtilitiesTest
6
-dependency2.to=TriangleUtilities
5
+dependency2.from=TableUtilitiesTest
6
+dependency2.to=TableUtilities
7 7
 dependency2.type=UsesDependency
8
-dependency3.from=TableUtilitiesTest
9
-dependency3.to=TableUtilities
8
+dependency3.from=TriangleUtilitiesTest
9
+dependency3.to=TriangleUtilities
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=722
11
+editor.fx.0.height=714
12 12
 editor.fx.0.width=800
13
-editor.fx.0.x=537
14
-editor.fx.0.y=28
15
-objectbench.height=164
13
+editor.fx.0.x=410
14
+editor.fx.0.y=24
15
+objectbench.height=154
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.753822629969419
19
+package.editor.height=486
20 20
 package.editor.width=382
21
-package.editor.x=20
22
-package.editor.y=57
23
-package.frame.height=759
21
+package.editor.x=11
22
+package.editor.y=26
23
+package.frame.height=712
24 24
 package.frame.width=508
25 25
 package.numDependencies=3
26 26
 package.numTargets=6