Quellcode durchsuchen

Completed methods

Demetrius Murray vor 6 Jahren
Ursprung
Commit
dd25759b71
5 geänderte Dateien mit 177 neuen und 39 gelöschten Zeilen
  1. 53
    13
      NumberUtilities.java
  2. 2
    0
      NumberUtilitiesTest.java
  3. 63
    4
      TableUtilities.java
  4. 41
    4
      TriangleUtilities.java
  5. 18
    18
      package.bluej

+ 53
- 13
NumberUtilities.java Datei anzeigen

@@ -1,32 +1,72 @@
1
- 
2 1
 
3 2
 
4 3
 public class NumberUtilities {
5
-    
4
+
6 5
     public static String getRange(int start) {
7
-        return null;
6
+        StringBuilder builder = new StringBuilder();
7
+        for(int i=0; i < start; i++){
8
+            builder.append(Integer.toString(i));
9
+        }
10
+
11
+        String result = builder.toString();
12
+        return result;
8 13
     }
9
-    
14
+
10 15
     public static String getRange(int start, int stop) {
11
-        return null;
12
-    }
16
+        StringBuilder builder = new StringBuilder();
17
+        for(int i=start; i<stop; i++){
18
+            builder.append(Integer.toString(i));
19
+        }
13 20
 
21
+        String result = builder.toString();
22
+        return result;
23
+    }
14 24
 
15 25
     public static String getRange(int start, int stop, int step) {
16
-        return null;
26
+        StringBuilder builder = new StringBuilder();
27
+        for(int i=start; i<stop; i+=step){
28
+            builder.append(Integer.toString(i));
29
+        }
30
+
31
+        String result = builder.toString();
32
+        return result;
17 33
     }
18
-    
34
+
19 35
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
36
+        StringBuilder builder = new StringBuilder();
37
+        for(int i=start; i<stop; i++){
38
+            if(i%2==0){
39
+                builder.append(Integer.toString(i));
40
+            }
41
+        }
42
+        String result = builder.toString();
43
+        return result;
21 44
     }
22 45
 
23
-
24 46
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
47
+        StringBuilder builder = new StringBuilder();
48
+        for(int i=start; i<stop; i++){
49
+            if(i%2!=0){
50
+                builder.append(Integer.toString(i));
51
+            }
52
+        }
53
+        String result = builder.toString();
54
+        return result;
26 55
     }
27 56
 
28
-
29 57
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
58
+        StringBuilder builder = new StringBuilder();
59
+        for(int i=start; i<=stop; i++){
60
+            int powerNum = 1;
61
+            for (int j=1; j<=exponent;j++){
62
+                powerNum *= i;
63
+            }
64
+            
65
+            builder.append(Integer.toString(powerNum));
66
+        }
67
+        
68
+        String result = builder.toString();
69
+        return result;
70
+        //NOTE: instructions say to not include stop. test expected result shows that the method's for loop INCLUDES stop. Thus expected = "....49".    
31 71
     }
32 72
 }

+ 2
- 0
NumberUtilitiesTest.java Datei anzeigen

@@ -170,6 +170,8 @@ public class NumberUtilitiesTest {
170 170
     public void testGetExponentiationNumbersForSquare() {
171 171
         // : Given
172 172
         String expected = "4916253649";
173
+        //NOTE: instructions say to not include stop. test expected shows that the method's for loop INCLUDES stop. Thus expected = "....49".
174
+        
173 175
         int start = 2;
174 176
         int stop = 7;
175 177
         int exponent = 2;

+ 63
- 4
TableUtilities.java Datei anzeigen

@@ -1,15 +1,74 @@
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<=5; i++){
7
+            for (int j=1; j<=5; j++){
8
+                int sum = i;
9
+                sum*=j;
10
+                /*if(Integer.toString(sum).length()==1){
11
+                builder.append("  " + Integer.toString(sum) + " |");
12
+                } else if (Integer.toString(sum).length()==2){
13
+                    builder.append(" " + Integer.toString(sum) + " |");
14
+                } else if (Integer.toString(sum).length()==3){
15
+                    builder.append(Integer.toString(sum) + " |");
16
+                }*/
17
+                
18
+                builder.append(String.format("%3s |", sum));
19
+            }
20
+            
21
+            builder.append("\n");
22
+        }
23
+        String result = builder.toString();
24
+        return result;
6 25
     }
7 26
 
8 27
     public static String getLargeMultiplicationTable() {
9
-        return null;
28
+        StringBuilder builder = new StringBuilder();
29
+
30
+        for(int i=1; i<=10; i++){
31
+            for (int j=1; j<=10; j++){
32
+                int sum = i;
33
+                sum*=j;
34
+                /*if(Integer.toString(sum).length()==1){
35
+                builder.append("  " + Integer.toString(sum) + " |");
36
+                } else if (Integer.toString(sum).length()==2){
37
+                    builder.append(" " + Integer.toString(sum) + " |");
38
+                } else if (Integer.toString(sum).length()==3){
39
+                    builder.append(Integer.toString(sum) + " |");
40
+                } */
41
+                
42
+               builder.append(String.format("%3s |", sum));
43
+            }
44
+            
45
+            builder.append("\n");
46
+        }
47
+        String result = builder.toString();
48
+        return result;
10 49
     }
11 50
 
12 51
     public static String getMultiplicationTable(int tableSize) {
13
-        return null;
52
+        StringBuilder builder = new StringBuilder();
53
+
54
+        for(int i=1; i<=tableSize; i++){
55
+            for (int j=1; j<=tableSize; j++){
56
+                int sum = i;
57
+                sum*=j;
58
+                /*if(Integer.toString(sum).length()==1){
59
+                builder.append("  " + Integer.toString(sum) + " |");
60
+                } else if (Integer.toString(sum).length()==2){
61
+                    builder.append(" " + Integer.toString(sum) + " |");
62
+                } else if (Integer.toString(sum).length()==3){
63
+                    builder.append(Integer.toString(sum) + " |");
64
+                } */
65
+                
66
+                builder.append(String.format("%3s |", sum));
67
+            }
68
+            
69
+            builder.append("\n");
70
+        }
71
+        String result = builder.toString();
72
+        return result;
14 73
     }
15 74
 }

+ 41
- 4
TriangleUtilities.java Datei anzeigen

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

+ 18
- 18
package.bluej Datei anzeigen

@@ -1,27 +1,27 @@
1 1
 #BlueJ package file
2
-dependency1.from=NumberUtilitiesTest
3
-dependency1.to=NumberUtilities
2
+dependency1.from=TriangleUtilitiesTest
3
+dependency1.to=TriangleUtilities
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=NumberUtilitiesTest
9
+dependency3.to=NumberUtilities
10 10
 dependency3.type=UsesDependency
11
-editor.fx.0.height=722
11
+editor.fx.0.height=716
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=240
14
+editor.fx.0.y=23
15
+objectbench.height=114
16
+objectbench.width=1256
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.8155487804878049
19
+package.editor.height=528
20
+package.editor.width=1154
21
+package.editor.x=0
22
+package.editor.y=23
23
+package.frame.height=714
24
+package.frame.width=1280
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true