Kaynağa Gözat

Loopy Loops

Seth 6 yıl önce
ebeveyn
işleme
d5f09abc51
4 değiştirilmiş dosya ile 106 ekleme ve 36 silme
  1. 46
    15
      NumberUtilities.java
  2. 32
    3
      TableUtilities.java
  3. 18
    8
      TriangleUtilities.java
  4. 10
    10
      package.bluej

+ 46
- 15
NumberUtilities.java Dosyayı Görüntüle

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

+ 32
- 3
TableUtilities.java Dosyayı Görüntüle

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

+ 18
- 8
TriangleUtilities.java Dosyayı Görüntüle

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

+ 10
- 10
package.bluej Dosyayı Görüntüle

@@ -1,25 +1,25 @@
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
-editor.fx.0.height=722
12
-editor.fx.0.width=800
13
-editor.fx.0.x=537
14
-editor.fx.0.y=28
11
+editor.fx.0.height=0
12
+editor.fx.0.width=0
13
+editor.fx.0.x=0
14
+editor.fx.0.y=0
15 15
 objectbench.height=164
16 16
 objectbench.width=484
17 17
 package.divider.horizontal=0.6
18 18
 package.divider.vertical=0.7560627674750356
19 19
 package.editor.height=523
20 20
 package.editor.width=382
21
-package.editor.x=20
22
-package.editor.y=57
21
+package.editor.x=376
22
+package.editor.y=23
23 23
 package.frame.height=759
24 24
 package.frame.width=508
25 25
 package.numDependencies=3