Spaces:
Running
Running
Create 14b Starsquare
Browse files
Week 3: Objects, files and exceptions/14b Starsquare
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
StringBuilder square(int sideLength)
|
| 4 |
+
|
| 5 |
+
which returns a square of asterisks in one StringBuilder string.
|
| 6 |
+
You can make a line break inside a string with the character "\n", e.g.
|
| 7 |
+
|
| 8 |
+
System.out.println("aaa\nbbb");
|
| 9 |
+
|
| 10 |
+
prints:
|
| 11 |
+
aaa
|
| 12 |
+
bbb
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
Example method call:
|
| 17 |
+
public static void main(String[] args){
|
| 18 |
+
System.out.println(square(3));
|
| 19 |
+
System.out.println();
|
| 20 |
+
|
| 21 |
+
StringBuilder largerSquare = square(6);
|
| 22 |
+
System.out.println(largerSquare);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
Program outputs:
|
| 26 |
+
***
|
| 27 |
+
***
|
| 28 |
+
***
|
| 29 |
+
|
| 30 |
+
******
|
| 31 |
+
******
|
| 32 |
+
******
|
| 33 |
+
******
|
| 34 |
+
******
|
| 35 |
+
******
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
import java.util.Random;
|
| 43 |
+
|
| 44 |
+
public class Test{
|
| 45 |
+
public static void main(String[] args){
|
| 46 |
+
final Random r = new Random();
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
int[] p = {2,4,6,3};
|
| 50 |
+
for (int pa : p) {
|
| 51 |
+
System.out.println("Testing with parameter " + pa);
|
| 52 |
+
StringBuilder square = square(pa);
|
| 53 |
+
|
| 54 |
+
if (square.charAt(square.length() -1) == '\n') {
|
| 55 |
+
square.deleteCharAt(square.length() -1);
|
| 56 |
+
}
|
| 57 |
+
System.out.println(square);
|
| 58 |
+
System.out.println("");
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
public static StringBuilder square(int sideLength) {
|
| 64 |
+
StringBuilder sb = new StringBuilder();
|
| 65 |
+
for (int i = 0; i < sideLength; i++) {
|
| 66 |
+
for (int j = 0; j < sideLength; j++) {
|
| 67 |
+
sb.append("*");
|
| 68 |
+
}
|
| 69 |
+
sb.append("\n");
|
| 70 |
+
}
|
| 71 |
+
return sb;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
Testing with parameter 2
|
| 86 |
+
**
|
| 87 |
+
**
|
| 88 |
+
|
| 89 |
+
Testing with parameter 4
|
| 90 |
+
****
|
| 91 |
+
****
|
| 92 |
+
****
|
| 93 |
+
****
|
| 94 |
+
|
| 95 |
+
Testing with parameter 6
|
| 96 |
+
******
|
| 97 |
+
******
|
| 98 |
+
******
|
| 99 |
+
******
|
| 100 |
+
******
|
| 101 |
+
******
|
| 102 |
+
|
| 103 |
+
Testing with parameter 3
|
| 104 |
+
***
|
| 105 |
+
***
|
| 106 |
+
***
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|