/
Shapes.java
138 lines (124 loc) · 3.45 KB
/
Shapes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
public class Shapes {
/*======== public static String line() ==========
Inputs: String c (single character String)
int n
Returns: A String that contains n copies of c
ex: Shapes.line( "^", 4 ); ==> "^^^^"
====================*/
public static String line(String c, int n) {
String s = "";
while (n > 0) {
s += c;
n--;
}
return s;
}
/*======== public static String box() ==========
Inputs: String c (single character)
int rs
int cs
Returns: A String made up of a rs x cs grid of c
ex: Shapes.box("*", 2, 3) ==> "***
***"
====================*/
public static String box(String c, int rs, int cs) {
String s = "";
while (rs > 0) {
s += line(c, cs);
s += "\n";
rs--;
}
return s;
}
/*======== public String tri1() ==========
Inputs: String c
int h
Returns: A "triangle" of c, h lines high
ex: Shapes.tri1("|", 3); ==> "|
||
|||"
====================*/
public static String tri1(String c, int h) {
String s = "";
for (int count = 1; count <= h; count++) {
s += line(c, count);
s += "\n";
}
return s;
}
/*======== public String tri2() ==========
Inputs: String c
int h
Returns: A differnt "triangle" of c, h lines high
ex: Shapes.tri2("|", 3); ==> " |
||
|||"
====================*/
public static String tri2(String c, int h){
String s="";
for (int count = 1; count <= h; count++) {
s += line(" ", h - count);
s += line(c, count);
s += "\n";
}
return s;
}
/*======== public String tri3() ==========
Inputs: String c
int h
Returns: A differnt "triangle" of c, h lines high
ex: Shapes.tri2("|", 3); ==> " |
|||
|||||"
====================*/
public static String tri3(String c, int h) {
String s="";
final int baseLength = 2 * h - 1;
for (int count = 0, col = 1; count < h; count++, col += 2) {
s += line(" ", (baseLength - col) / 2);
s += line(c, col);
s += "\n";
}
return s;
}
/*======== public diamond() ==========
Inputs: String c
int h
Returns: A "diamond" of c, h lines high
ex: Shapes.tri2("|", 5); ==> " |
|||
|||||"
|||
|"
====================*/
public static String diamond(String c, int h){
String s = "";
final int baseLength = 2 * h - 1;
s += tri3(c, h);
for (int col = baseLength - 2; col > 0; col -= 2) {
s += line(" ", (baseLength - col) / 2);
s += line(c, col);
s += "\n";
}
return s;
}
public static void main(String[] args) {
System.out.println("Line Test:");
System.out.println(Shapes.line("%",10));
System.out.println("Box Test:");
System.out.println(Shapes.box("#", 5,4));
System.out.println(Shapes.box("#", 4,5));
System.out.println();
System.out.println("Tri1 Test:");
System.out.println(Shapes.tri1("@", 5));
System.out.println();
System.out.println("Tri2 Test:");
System.out.println(Shapes.tri2("+", 5));
System.out.println();
System.out.println("Tri3 Test:");
System.out.println(Shapes.tri3("|", 4));
System.out.println();
System.out.println("Diamond Test:");
System.out.println(Shapes.diamond("*", 5));
}
}