Write and test several overloaded methods called textBoxString . This method ret
ID: 3798089 • Letter: W
Question
Write and test several overloaded methods called textBoxString. This method returns a String value.
public static String textBoxString (int side)
The returned String value, when printed, displays as a square of side characters. The character you use is up to you. Don't forget that ' ' will force a newline character into the returned String. For example, let's assume I want to use * as the character for my box:
will print
public static String textBoxString(int side, char bChar)
The returned String value, when printed, displays as a square of side characters using bChar as the box character. For example,
will print
++++
++++
++++
++++
public static String textBoxString(int rows, int cols)
The returned String value, when printed, displays as a rectangle of rows rows and cols columns using your default box character.
will print
****
****
****
public static String textBoxString(int rows, int cols, char c1, char c2)
The returned String value, when printed, displays a rectangle of rows rows and cols columns using alternating c1 and c2 characters.
will print
xoxox
oxoxo
xoxox
There will be a single test run which is produced by placing all of the code snippets above in main()
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
public class TextBoxString {
public static String textBoxString (int side){
String result = "";
for(int i=0; i<side; i++){
for(int j=0; j<side; j++)
result = result + "*";
result = result + " ";
}
return result;
}
public static String textBoxString(int side, char bChar){
String result = "";
for(int i=0; i<side; i++){
for(int j=0; j<side; j++)
result = result + bChar;
result = result + " ";
}
return result;
}
public static String textBoxString(int rows, int cols){
String result = "";
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
result = result + "*";
result = result + " ";
}
return result;
}
public static String textBoxString(int rows, int cols, char c1, char c2){
String result = "";
char first = c1;
char second = c2;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(j%2 ==0)
result = result +first;
else
result = result +second;
}
result = result + " ";
// swapping first and second
char temp = first;
first = second;
second = temp;
}
return result;
}
public static void main(String[] args) {
String s1 = textBoxString(3);
System.out.println(s1);
String s2 = textBoxString(4, '+');
System.out.println(s2);
String s3 = textBoxString(3, 4);
System.out.println(s3);
String s4 = textBoxString(3, 5, 'x', 'o');
System.out.println(s4);
}
}
/*
Sample run:
***
***
***
++++
++++
++++
++++
****
****
****
xoxox
oxoxo
xoxox
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.