Bläddra i källkod

Numbers triangles and tables.

Jennifer Tinkler 6 år sedan
förälder
incheckning
77b3cc20f6
4 ändrade filer med 166 tillägg och 29 borttagningar
  1. 58
    6
      NumberUtilities.java
  2. 38
    4
      TableUtilities.java
  3. 55
    4
      TriangleUtilities.java
  4. 15
    15
      package.bluej

+ 58
- 6
NumberUtilities.java Visa fil

@@ -4,29 +4,81 @@
4 4
 public class NumberUtilities {
5 5
 
6 6
     public static String getRange(int stop) {
7
-        return null;
7
+        StringBuilder sb = new StringBuilder ();
8
+        
9
+        for (int i=0; i < stop; i++) { 
10
+            
11
+            sb.append(i);
12
+        }
13
+        //to convert back to string from int
14
+        return sb.toString();
8 15
     }
9 16
 
10 17
     public static String getRange(int start, int stop) {
11
-        return null;
18
+        StringBuilder sb = new StringBuilder ();
19
+        
20
+        for (int i = start; i < stop; i++) {
21
+        
22
+            sb.append(i);
23
+        }
24
+        return sb.toString(); 
25
+      
12 26
     }
13 27
 
14 28
 
15 29
     public static String getRange(int start, int stop, int step) {
16
-        return null;
30
+        StringBuilder sb = new StringBuilder ();
31
+        //or can use String string " ".
32
+        for (int i = start; i < stop; i += step) { 
33
+            
34
+            sb.append(i);
35
+            
36
+        }
37
+       return sb.toString();
38
+       
17 39
     }
18 40
 
19 41
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
42
+        StringBuilder sb = new StringBuilder ();
43
+        
44
+        for ( int i = start; i < stop; i ++){
45
+               
46
+            if ( i % 2 == 0)
47
+            
48
+            sb.append(i);
49
+        }
50
+        
51
+        
52
+        return sb.toString();
21 53
     }
22 54
 
23 55
 
56
+    
24 57
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
58
+        StringBuilder sb = new StringBuilder ();
59
+        
60
+        for (int i = start; i < stop; i ++) {
61
+           
62
+            if (i % 2 != 0)
63
+            
64
+            sb.append(i);
65
+            
66
+       
67
+        }
68
+        return sb.toString();
26 69
     }
27 70
 
28 71
 
29 72
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
73
+        StringBuilder sb = new StringBuilder();
74
+        
75
+        for (int i = start; i <= stop; i++) {
76
+            sb.append((int)Math.pow(i, exponent));
77
+            
78
+            
79
+    }
80
+     
81
+   
82
+        return sb.toString();
31 83
     }
32 84
 }

+ 38
- 4
TableUtilities.java Visa fil

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

+ 55
- 4
TriangleUtilities.java Visa fil

@@ -3,19 +3,70 @@
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getRow(int numberOfStars) {
6
-        return null;
6
+        StringBuilder sb = new StringBuilder();
7
+        //String str = "";
8
+        for (int i =0; i < numberOfStars; i++){
9
+        //str += "*";
10
+        sb = sb.append("*");
11
+        }
12
+        //return str;
13
+        return sb.toString();
7 14
     }
8 15
     
9 16
     public static String getTriangle(int numberOfRows) {
10
-        return null;
17
+        StringBuilder sb = new StringBuilder();
18
+        
19
+        for (int i = 0; i < numberOfRows; i++) {
20
+        
21
+            for (int j = 0; j <= i; j++) 
22
+            {
23
+                //if (j<i)
24
+                
25
+                sb = sb.append("*");
26
+            }
27
+              sb.append("\n");
28
+        }
29
+      
30
+        return sb.toString();
11 31
     }
12 32
 
13 33
 
14 34
     public static String getSmallTriangle() {
15
-        return null;
35
+        StringBuilder sb = new StringBuilder();
36
+        
37
+        for (int i = 0; i < 4; i++) {
38
+        
39
+            for (int j = 0; j <= i; j++) 
40
+            {
41
+                //if (j<i)
42
+                
43
+                sb.append("*");
44
+            }
45
+            sb.append("\n");
46
+        }
47
+        
48
+        
49
+        System.out.print(sb);
50
+        return sb.toString(); 
16 51
     }
17 52
 
18 53
     public static String getLargeTriangle() {
19
-        return null;
54
+    StringBuilder sb = new StringBuilder();
55
+        
56
+        for (int i = 0; i < 9; i++) {
57
+        
58
+            for (int j = 0; j <= i; j++) 
59
+            {
60
+                //if (j<i)
61
+                
62
+                sb.append("*");
63
+            }
64
+            sb.append("\n");
65
+        }
66
+        
67
+        
68
+        System.out.print(sb);
69
+        return sb.toString(); 
20 70
     }
71
+    
21 72
 }

+ 15
- 15
package.bluej Visa fil

@@ -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=107
14
+editor.fx.0.y=23
15
+objectbench.height=100
16
+objectbench.width=387
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.5977443609022557
19
+package.editor.height=152
20
+package.editor.width=269
21
+package.editor.x=869
22
+package.editor.y=458
23
+package.frame.height=324
24
+package.frame.width=411
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true