-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangularSquare (1).java
More file actions
180 lines (148 loc) · 4.19 KB
/
TriangularSquare (1).java
File metadata and controls
180 lines (148 loc) · 4.19 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public class TriangularSquare implements IPattern {
int N = 10;
/**
* Prints the following pattern for n = 10:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
Prints the following pattern for n = 11:
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* prints a square with a diamond within with total width n-1
* and the square with a total length and width n
*/
@Override
public String printPattern(int n) {
String newValue = ""; // empty string
if (n <= 0){
return ("");
}
for(int lines = 1; lines <=(n/2); lines++){ // line iteration
for (int stars = n/2; stars >= lines; stars--){ // stars
if (stars == n/2 ){ // checks if its a side value
newValue += ("*"); // without space on left side
}
else{
newValue +=(" *"); // with space
}
}
for (int spaces = lines*2; spaces >= 2; spaces--){ // spaces
newValue += (" ");
}
for (int stars = n/2; stars >= lines; stars--){ // stars
newValue += ("* "); // stars on right side
}
newValue += ("\n"); // line skip
}
if ( n%2 == 1){ // checks if it's odd, if so adds a line
newValue +="\n";
}
for (int lines = 1; lines <= n/2; lines++) { // bottom half
for (int stars = lines; stars > 0; stars--){ // stars
if (stars == lines ){ // checks if its a side value
newValue += ("*"); // no space
}
else{
newValue += (" *"); // with space
}
}
for (int spaces = 2; spaces <= (n/2 - lines +1)*2; spaces++){
newValue += (" "); // adds spaces
}
for (int stars = lines; stars > 0 ; stars--){
newValue += ("* "); // adds spaces
}
newValue += ("\n"); // line skip
}
return newValue; // returns value
}
/**
*
Printing 3rd item for N=10
* * * * * *
Printing 7th item for N=11
* *
*/
@Override
public String printNthItem(int i) {
String newValue = "";
int n = this.N;
int lines = i;
if (i > n || i <= 0){
return ("");
}
// for(lines = i; lines < i; lines++){ // irrelevant, ignore
for (int stars = n/2; stars >=(lines); stars--){ // line iterate
if (stars == n/2){ // checks if side case
newValue += ("*");
}
else{ // adds left stars
newValue += (" *");
}
}
for(int spaces = lines*2; spaces >= 2; spaces--){ //spaces
newValue += (" ");
}
for(int stars = n/2; stars >= lines; stars--){ //right stars
newValue += ("* ");
// } // irrelevant
}
if (n%2 == 1){ // checks for odd output
newValue += "\n";
}
// irrelevant second try
// for (lines = i; lines <= i; lines++){
/* for(int stars = lines; stars >= 0; stars--){
if (stars == lines){
newValue += ("*");
}
else{
newValue += (" *");
}
}
for(int spaces = 2; spaces <= (n/2 - lines + 1)*2; spaces++){
newValue += (" ");
}
for(int stars = lines; stars >= 0; stars--){
newValue += ("* ");
}
*/
// }
return newValue;
}
public static void main(String[] args) {
IPattern obj = new TriangularSquare();
obj.setParam(10);
System.out.println("Printing 3rd item for N=10");
System.out.println(obj.printNthItem(3));
System.out.println(obj.printNthItem(2));
System.out.println(obj.printNthItem(0));
obj.setParam(11);
System.out.println("Printing 7th item for N=11");
System.out.println(obj.printNthItem(7));
System.out.println("Printing Pattern:");
System.out.println(obj.printPattern(11));
}
/**
* This function sets the value of N
*/
@Override
public void setParam(int arg) {
this.N = arg; // sets parameter
}
}