浏览代码

loops Labs

Nuridalia Hermandez 6 年前
父节点
当前提交
876b194297
共有 5 个文件被更改,包括 114 次插入52 次删除
  1. 42
    15
      NumberUtilities.java
  2. 5
    12
      NumberUtilitiesTest.java
  3. 21
    3
      TableUtilities.java
  4. 32
    8
      TriangleUtilities.java
  5. 14
    14
      package.bluej

+ 42
- 15
NumberUtilities.java 查看文件

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

+ 5
- 12
NumberUtilitiesTest.java 查看文件

@@ -1,10 +1,9 @@
1
- 
2 1
 
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
5 4
 
6 5
 public class NumberUtilitiesTest {
7
-    
6
+
8 7
     @Test
9 8
     public void testGetRangeForSmallRange() {
10 9
         // : Given
@@ -17,8 +16,7 @@ public class NumberUtilitiesTest {
17 16
         // : Then
18 17
         Assert.assertEquals(expected, actual);
19 18
     }
20
-    
21
-    
19
+
22 20
     @Test
23 21
     public void testGetRangeToTwoDigits() {
24 22
         // : Given
@@ -60,7 +58,6 @@ public class NumberUtilitiesTest {
60 58
         Assert.assertEquals(expected, actual);
61 59
     }
62 60
 
63
-    
64 61
     @Test
65 62
     public void testGetRangeWithOneStep() {
66 63
         // : Given
@@ -76,7 +73,6 @@ public class NumberUtilitiesTest {
76 73
         Assert.assertEquals(expected, actual);
77 74
     }
78 75
 
79
-
80 76
     @Test
81 77
     public void testGetRangeWithTwoStep() {
82 78
         // : Given
@@ -92,7 +88,7 @@ public class NumberUtilitiesTest {
92 88
         Assert.assertEquals(expected, actual);
93 89
     }
94 90
 
95
-        @Test
91
+    @Test
96 92
     public void testGetRangeWithFiveStep() {
97 93
         // : Given
98 94
         String expected = "51015";
@@ -107,7 +103,6 @@ public class NumberUtilitiesTest {
107 103
         Assert.assertEquals(expected, actual);
108 104
     }
109 105
 
110
-    
111 106
     @Test
112 107
     public void testGetEvenNumbersStartAndEndAtEven() {
113 108
         // : Given
@@ -122,7 +117,6 @@ public class NumberUtilitiesTest {
122 117
         Assert.assertEquals(expected, actual);
123 118
     }
124 119
 
125
-    
126 120
     @Test
127 121
     public void testGetEvenNumbersStartAndEndAtOdd() {
128 122
         // : Given
@@ -137,7 +131,6 @@ public class NumberUtilitiesTest {
137 131
         Assert.assertEquals(expected, actual);
138 132
     }
139 133
 
140
-    
141 134
     @Test
142 135
     public void testGetOddNumbersStartAndEndAtOdd() {
143 136
         // : Given
@@ -165,7 +158,6 @@ public class NumberUtilitiesTest {
165 158
         Assert.assertEquals(expected, actual);
166 159
     }
167 160
 
168
-
169 161
     @Test
170 162
     public void testGetExponentiationNumbersForSquare() {
171 163
         // : Given
@@ -180,8 +172,9 @@ public class NumberUtilitiesTest {
180 172
         // : Then
181 173
         Assert.assertEquals(expected, actual);
182 174
     }
175
+
183 176
     
184
-    
177
+
185 178
     @Test
186 179
     public void testGetExponentiationNumbersForCube() {
187 180
         // : Given

+ 21
- 3
TableUtilities.java 查看文件

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

+ 32
- 8
TriangleUtilities.java 查看文件

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

+ 14
- 14
package.bluej 查看文件

@@ -1,26 +1,26 @@
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 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
13
+editor.fx.0.x=447
14
+editor.fx.0.y=31
15
+objectbench.height=154
16 16
 objectbench.width=484
17 17
 package.divider.horizontal=0.6
18
-package.divider.vertical=0.7560627674750356
19
-package.editor.height=523
18
+package.divider.vertical=0.7553191489361702
19
+package.editor.height=490
20 20
 package.editor.width=382
21
-package.editor.x=20
22
-package.editor.y=57
23
-package.frame.height=759
21
+package.editor.x=159
22
+package.editor.y=23
23
+package.frame.height=716
24 24
 package.frame.width=508
25 25
 package.numDependencies=3
26 26
 package.numTargets=6