Selaa lähdekoodia

loops tables and triangles

Jose Bedolla 6 vuotta sitten
vanhempi
commit
834fc64c89
5 muutettua tiedostoa jossa 156 lisäystä ja 32 poistoa
  1. 50
    7
      NumberUtilities.java
  2. 0
    0
      README.TXT
  3. 39
    3
      TableUtilities.java
  4. 49
    4
      TriangleUtilities.java
  5. 18
    18
      package.bluej

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

@@ -1,32 +1,75 @@
1
- 
1
+ import static java.lang.Math.pow;
2 2
 
3 3
 
4 4
 public class NumberUtilities {
5 5
     
6 6
     public static String getRange(int start) {
7
-        return null;
7
+        
8
+        String s="";
9
+        
10
+        for(int i =0; i < start; i++)
11
+        {
12
+            s = s +i;
13
+        }
14
+        return s;
8 15
     }
9 16
     
10 17
     public static String getRange(int start, int stop) {
11
-        return null;
18
+        String s2 = "";
19
+        for(int i = start; i<stop; i++)
20
+        {
21
+            s2 = s2 + i;
22
+        }
23
+        return s2;
12 24
     }
13 25
 
14 26
 
15 27
     public static String getRange(int start, int stop, int step) {
16
-        return null;
28
+        String s3 = "";
29
+        for(int i = start; i < stop; i+=step)
30
+        {
31
+            s3 = s3 +i;
32
+        }
33
+        return s3;
17 34
     }
18 35
     
19 36
     public static String getEvenNumbers(int start, int stop) {
20
-        return null;
37
+        String s4 = "";
38
+        for(int i = start; i < stop; i++)
39
+        {
40
+            if((i&1)==0)
41
+            {
42
+                s4+=i;
43
+            }
44
+        }
45
+        
46
+       return s4;
21 47
     }
22 48
 
23 49
 
24 50
     public static String getOddNumbers(int start, int stop) {
25
-        return null;
51
+        String s5 = "";
52
+        for(int i = start; i<stop; i++)
53
+        {
54
+            if((i&1)!=0)
55
+            {
56
+                s5+=i;
57
+            }
58
+        }
59
+       
60
+        return s5;
26 61
     }
27 62
 
28 63
 
29 64
     public static String getExponentiations(int start, int stop, int exponent) {
30
-        return null;
65
+        String s6="";
66
+        
67
+        for(int i = start; i <= stop; i++)
68
+        {
69
+            s6 = s6 + (int)Math.pow(i, exponent);
70
+            
71
+        }
72
+
73
+        return s6;
31 74
     }
32 75
 }

+ 0
- 0
README.TXT Näytä tiedosto


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

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

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

@@ -3,19 +3,64 @@
3 3
 public class TriangleUtilities {
4 4
 
5 5
     public static String getRow(int numberOfStars) {
6
-        return null;
6
+        int counter = 0;
7
+        String star = "*";
8
+        for(int i = 1; i < numberOfStars; i++)
9
+        {
10
+            star = star + "*";
11
+           // counter++; this was for me to count the *s fixing my code
12
+            
13
+        }
14
+        //System.out.println("number: " + counter); ^
15
+        return star;
7 16
     }
8 17
     
9 18
     public static String getTriangle(int numberOfRows) {
10
-        return null;
19
+        int coumter;
20
+        StringBuilder star = new StringBuilder();
21
+
22
+        for(int i =1; i <= numberOfRows; i++)
23
+        {
24
+            for(int j=1;j<=i;j++)
25
+            {
26
+                star.append("*");
27
+            }
28
+                star.append("\n");
29
+        }
30
+
31
+        return star.toString();
32
+  
11 33
     }
12 34
 
13 35
 
14 36
     public static String getSmallTriangle() {
15
-        return null;
37
+        
38
+        StringBuilder star = new StringBuilder();
39
+
40
+        for(int i =1; i <= 4; i++)
41
+        {
42
+            for(int j=1;j<=i;j++)
43
+            {
44
+                star.append("*");
45
+            }
46
+                star.append("\n");
47
+        }
48
+
49
+        return star.toString();
16 50
     }
17 51
 
18 52
     public static String getLargeTriangle() {
19
-        return null;
53
+        StringBuilder star = new StringBuilder();
54
+
55
+        for(int i =1; i <= 9; i++)
56
+        {
57
+            for(int j=1;j<=i;j++)
58
+            {
59
+                star.append("*");
60
+            }
61
+                star.append("\n");
62
+        }
63
+
64
+        return star.toString();
20 65
     }
21 66
 }

+ 18
- 18
package.bluej Näytä tiedosto

@@ -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
-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
-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=505
14
+editor.fx.0.y=29
15
+objectbench.height=135
16
+objectbench.width=539
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.7408759124087592
19
+package.editor.height=399
20
+package.editor.width=437
21
+package.editor.x=215
22
+package.editor.y=47
23
+package.frame.height=606
24
+package.frame.width=563
25 25
 package.numDependencies=3
26 26
 package.numTargets=6
27 27
 package.showExtends=true