Просмотр исходного кода

Complete and tested loopy loops

Jennifer Chao 6 лет назад
Родитель
Сommit
dafee87846
4 измененных файлов: 139 добавлений и 50 удалений
  1. 43
    16
      NumberUtilities.java
  2. 40
    4
      TableUtilities.java
  3. 37
    11
      TriangleUtilities.java
  4. 19
    19
      package.bluej

+ 43
- 16
NumberUtilities.java Просмотреть файл

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

+ 40
- 4
TableUtilities.java Просмотреть файл

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

+ 37
- 11
TriangleUtilities.java Просмотреть файл

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

+ 19
- 19
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
-dependency2.from=TriangleUtilitiesTest
6
-dependency2.to=TriangleUtilities
5
+dependency2.from=NumberUtilitiesTest
6
+dependency2.to=NumberUtilities
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
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
11
+editor.fx.0.height=709
12
+editor.fx.0.width=631
13
+editor.fx.0.x=1051
14
+editor.fx.0.y=144
15
+objectbench.height=152
16
+objectbench.width=633
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
23
-package.frame.height=759
24
-package.frame.width=508
18
+package.divider.vertical=0.7557603686635944
19
+package.editor.height=485
20
+package.editor.width=531
21
+package.editor.x=1057
22
+package.editor.y=47
23
+package.frame.height=709
24
+package.frame.width=657
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true