Selaa lähdekoodia

Curtis Cook's Loopy Lab

Curtis Cook 6 vuotta sitten
vanhempi
commit
447bdda264
3 muutettua tiedostoa jossa 98 lisäystä ja 14 poistoa
  1. 35
    7
      NumberUtilities.java
  2. 27
    3
      TableUtilities.java
  3. 36
    4
      TriangleUtilities.java

+ 35
- 7
NumberUtilities.java Näytä tiedosto

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

+ 27
- 3
TableUtilities.java Näytä tiedosto

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

+ 36
- 4
TriangleUtilities.java Näytä tiedosto

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