Sfoglia il codice sorgente

Completed Loops lab

thulasi 6 anni fa
parent
commit
240ba564c9
4 ha cambiato i file con 99 aggiunte e 31 eliminazioni
  1. 32
    8
      NumberUtilities.java
  2. 22
    3
      TableUtilities.java
  3. 29
    4
      TriangleUtilities.java
  4. 16
    16
      package.bluej

+ 32
- 8
NumberUtilities.java Vedi File

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

+ 22
- 3
TableUtilities.java Vedi File

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

+ 29
- 4
TriangleUtilities.java Vedi File

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

+ 16
- 16
package.bluej Vedi File

@@ -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
-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
12
+editor.fx.0.width=1280
13
+editor.fx.0.x=497
14
+editor.fx.0.y=23
15
+objectbench.height=156
16
+objectbench.width=653
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.7545180722891566
19
+package.editor.height=494
20
+package.editor.width=551
21
+package.editor.x=79
22
+package.editor.y=30
23
+package.frame.height=722
24
+package.frame.width=677
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true