I am trying to write a method that accepts 3 Paraments, and return a hollow square.
1st Method parameter = length of string. 2nd Method parameter = how many characters would be there in the one arm of the square. 3rd Method parameter = String.
Condition: Example if 2nd parameter is 4 so total 12 characters required to form a square, if given string is sorter the that then it will be repeated (abcdefghabcd).
I have written a code but that is not satisfying few conditions.
public void patternmaker(int a, int b, String s) {
try {
int p = -2, q = 0, z = 0;
for (int x = 1; x <= b; x++) {
for (int y = 1; y <= b; y++) {
if (x == 1) {
System.out.print(s.charAt(y + b - 2) + " ");
} else if (x == b && y > 1) {
System.out.print(s.charAt(a + z - 1) + " ");
z = z - 1;
} else if (x == b && y == 1) {
System.out.print(s.charAt(0) + " ");
} else if (y == 1 && x > 1 && x < b) {
System.out.print(s.charAt(b + p) + " ");
p = p - 1;
} else if (y == b && x > 1 && x < b) {
System.out.print(s.charAt(2 * b - 2 + x - 1) + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
catch (Exception e) {
}
}
refer image for example
Any suggestion or input?
