In: Computer Science
JAVA program.
Write two public classes (named exactly), TextBox and TextBoxTester. TextBox contains the following overloaded static methods called textBoxString. This method returns a String value.
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 '\n' 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
***
* *
***
The returned String value, when printed, displays the outline of a square of side characters using bChar as the box character. For example,
String s = textBoxString(4, '+');
System.out.println(s);
will print
++++
+ +
+ +
++++
The returned String value, when printed, displays the outline of a rectangle of rows rows and cols columns using your default box character.
String s = textBoxString(3, 4);
System.out.println(s);
will print
****
* *
****
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
TextBoxTester has only a main method, and calls each of the textBoxString methods as a test.
Grading Elements
/******************************TextBox.java************************/
public class TextBox {
public static String textBoxString(int side)
{
int n = side;
String string = "";
// row
for (int i = 0; i < n; i++)
{
// for first and
last row
if (i == 0 || i
== n - 1) {
// for column
for (int j = 0; j < n; j++) {
string += "*";
}
string += "\n";// next line
}
// for middle
line
else {
string += "*";
for (int j = 1; j < n - 1; j++) {
string += " ";// fill
spaces
}
string += "*\n";
}
}
return string;// return the
string
}
public static String textBoxString(int n, char c)
{
String s = "";
for (int i = 0; i < n; i++)
{
s += c;
for (int j = 1;
j < n - 1; j++) {
if (i == 0 || i == n - 1)
s += " " + c;
else
s += " ";
}
s += " " + c +
"\n";
}
return s;
}
public static String textBoxString(int rows, int cols) {
String s = "";
for (int i = 0; i < rows; i++)
{
if (i == 0 || i
== rows - 1) {
for (int j = 1; j < cols; j++) {
s += "*";
}
} else {
for (int j = 1; j < cols - 1; j++) {
s += "*";
}
}
s +=
"\n";
}
return s;
}
public static String textBoxString(int rows, int cols, char c1, char c2) {
String s = "";
for (int i = 0; i < rows; i++) {
if (i == 0 ||
i == rows - 1) {
for (int j = 0; j < cols; j++) {
if (j % 2 == 0) {
s +=
c1;
} else {
s +=
c2;
}
}
} else {
char tmp = c1;
if (i % 2 == 0) {
c1 = c2;
c2 = tmp;
} else {
tmp = c2;
c2 = c1;
c1 = tmp;
}
s += c1;
for (int j = 0; j < cols - 2; j++) {
s += " ";
}
s += c2;
}
s +=
"\n";
}
return s;
}
}
/*************************************TextBoxTester.java**********************/
public class TextBoxTester {
public static void main(String[] args) {
System.out.println("Method 1:
");// textBoxString (int side)
System.out.println(TextBox.textBoxString(4));
System.out.println();
System.out.println("Method 2:");//
textBoxString(int side, char bChar)
String s = TextBox.textBoxString(4,
'+');
System.out.println(s);
System.out.println("Method 3:");//
textBoxString(int rows, int cols)
System.out.println(TextBox.textBoxString(3, 4));
System.out.println();
System.out.println("Method 4:");//
textBoxString(int rows, int cols, char c1, char c2)
String s1 =
TextBox.textBoxString(3, 5, 'x', 'o');
System.out.println(s1);
}
}
/*************************output******************************/
Method 1:
****
* *
* *
****
Method 2:
+ + + +
+ +
+ +
+ + + +
Method 3:
***
**
***
Method 4:
xoxox
o x
oxoxo
Please let me know if you have any doubt or modify the answer, Thanks :)