Question
JAVA PROGRAMMING public static String textBoxString(int rows, int cols, char c1, char c2) The returned String value, when printed, displays the outline of a rectangle
JAVA PROGRAMMING
public static String textBoxString(int rows, int cols, char c1, char c2)
The returned String value, when printed, displays the outline of a rectangle of rows rows and cols columns using alternating c1 and c2 characters. Note that the first printed character should be c1, and every other printed character should be c1. The second printed character should be c2, and every other printed character should be c2. For example, in the example below, the end of the first line is 'x' and the beginning of the second line is 'o'. The next printed character, at the end of the second line is 'x'.
String s = textBoxString(3, 5, 'x', 'o'); System.out.println(s); will print
xoxox
o x
oxoxo
This is what I have so far:
public static String textBoxString(int rows, int cols, char c1, char c2) { String result1 = ""; for (int i = 1; i <= rows; i++) { if (i % 2 == 0) { // if the row is even (row 2) for (int j = 1; j <= cols; j++) { if ( i == 1 || i == rows || j == 1 || j == cols) { if (j % 2 == 0) { result1 += c1; // print"x" for the even column } else { result1 += c2; // print "o" for the odd column } } }result1 += " "; // go to next line } else { // if the row is odd (row 1 and 3) for (int j = 1; j <= cols; j++) { if ( i == 1 || i == rows || j == 1 || j == cols) { if (j % 2 == 0) { result1 += c2; // print "O" for the even column } else { result1 += c1; // print "x" for the odd column } } }result1 += " "; } } return result1; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started