Ver código fonte

Loops-Numbers of TrianglesTables

Nira Parikh 6 anos atrás
pai
commit
fe0b66f248
4 arquivos alterados com 114 adições e 42 exclusões
  1. 38
    16
      NumberUtilities.java
  2. 31
    4
      TableUtilities.java
  3. 31
    8
      TriangleUtilities.java
  4. 14
    14
      package.bluej

+ 38
- 16
NumberUtilities.java Ver arquivo

@@ -1,32 +1,54 @@
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 num = "";
5
+        for (int x=0; x <stop; x++){
6
+            num += x;
7
+        }
8
+        return num;
8 9
     }
9
-    
10
+
10 11
     public static String getRange(int start, int stop) {
11
-        return null;
12
+        String num = "";
13
+        for (int x= start; x < stop; x++){
14
+            num += x;
15
+        }
16
+        return num;
12 17
     }
13 18
 
14
-
15 19
     public static String getRange(int start, int stop, int step) {
16
-        return null;
20
+        String num = "";
21
+        for (int x= start; x < stop; x+=step){
22
+            num += x;
23
+        }
24
+        return num;
17 25
     }
18
-    
26
+
19 27
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
28
+        String num = "";
29
+        for (int x = start; x < stop; x++){
30
+            if (x%2 == 0){
31
+                num+=x;
32
+            }
33
+        }
34
+        return num;
21 35
     }
22 36
 
23
-
24 37
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
38
+        String num = "";
39
+        for (int x = start; x < stop; x++){
40
+            if (x%2 != 0){
41
+                num+=x;
42
+            }
43
+        }
44
+        return num;
26 45
     }
27 46
 
28
-
29 47
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
48
+        String num ="";
49
+        for (int x = start; x <= stop; x++){
50
+            num += (int)Math.pow(x,exponent);
51
+        }
52
+        return num;
53
+    }
31 54
     }
32
-}

+ 31
- 4
TableUtilities.java Ver arquivo

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

+ 31
- 8
TriangleUtilities.java Ver arquivo

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

+ 14
- 14
package.bluej Ver arquivo

@@ -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=709
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
17
-package.divider.horizontal=0.6
18
-package.divider.vertical=0.7560627674750356
19
-package.editor.height=523
13
+editor.fx.0.x=480
14
+editor.fx.0.y=23
15
+objectbench.height=152
16
+objectbench.width=286
17
+package.divider.horizontal=0.6004098360655737
18
+package.divider.vertical=0.7557603686635944
19
+package.editor.height=485
20 20
 package.editor.width=382
21 21
 package.editor.x=20
22
-package.editor.y=57
23
-package.frame.height=759
22
+package.editor.y=23
23
+package.frame.height=709
24 24
 package.frame.width=508
25 25
 package.numDependencies=3
26 26
 package.numTargets=6