Question
Write two public classes (named exactly), TextBox and TextBoxTester. TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public
Write two public classes (named exactly), TextBox and TextBoxTester. TextBox contains the following overloaded static methods called textBoxString. This method returns a String value. public static String textBoxString (int side) The returned String value, when printed, displays as the outline of 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:
String s = textBoxString(3);
System.out.println(s);
will print
***
* *
***
TextBoxTester has only a main method, and calls each of the textBoxString methods as a test.
- All overloaded methods are implemented in TextBox
- The TextBox class has no main method
- Each method has its correct signature
- Each method returns its appropriate output
- textBoxString methods do not themselves print anything. They only return a String that, when printed by the caller, prints the correct box
- TextBoxTester has a main method and calls each TextBox.textBoxString method with appropriate parameter values
I am confused about TextBoxTester has a main method and the project has two public classes. Can I have two public classes in one file? How do I include the TextBoxTester class in my project?
Here is my solution and it includes 4 testBoxString methods.
public class TextBox { public static void main(String[] args) { String s1 = TextBox.textBoxString(3); System.out.println(s1);
String s2 = TextBox.textBoxString(4, '+'); System.out.println(s2); String s3 = TextBox.textBoxString(3, 4); System.out.println(s3); String s4 = TextBox.textBoxString(3, 5, 'x', 'o'); System.out.println(s4); } public static String textBoxString(int side) { //string type textBoxString method String s=""; for (int i=1;i<=side; i++) { //outer loop prints the row for (int j=1;j<=side; j++) { //inner loop prints the column if(i==1||i==side||j==1||j==side) { s+="*"; } else { s+=" "; //print the space } } s+=" "; } return s; } public static String textBoxString(int side, char bCha) { String s=""; for (int i=1; i<=side; i++) { //outer loop prints the row for (int j=1;j<=side;j++) { //inner loop prints the column if (i==1||i==side||j==1||j==side) { //1st row, 1st column, last row, or last column s+=bCha; } else { s+=" "; } } //ends inner for loop s+=" "; } //ends outer for loop return s; } public static String textBoxString(int rows, int cols) { String s=""; for (int i=1; i<=rows; i++) { for (int j=1;j<=cols;j++) { if (i==1||i==rows||j==1||j==cols) { //1st row, 1st column, last row or last column s+="*"; } else { s+=" "; } } //ends inner for loop s+=" "; } //ends outer for loop return s; } public static String textBoxString(int rows, int cols, char c1, char c2) { String s=""; 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 (j % 2 == 0) { s += c1; //print"x" for the even column } else { s += c2; //print "o" for the odd column } } s += " "; //go to next line } else { //if the row is odd (row 1 and 3) for (int j = 1; j <= cols; j++) { if (j % 2 == 0) { s += c2; //print "O" for the even column } else { s += c1; //print "x" for the odd column } } s += " "; } } return s; } }
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