Question

In: Computer Science

JAVA program. Write two public classes (named exactly), TextBox and TextBoxTester.  TextBox contains the following overloaded static...

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.

  • 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 '\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

 *** 
 *  * 
 *** 
  • public static String textBoxString(int side, char bChar)

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

++++
+ +
+ +
++++

  • public static String textBoxString(int rows, int cols)

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

****
* *
****

  • 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


TextBoxTester has only a main method, and calls each of the textBoxString methods as a test.  

Grading Elements

  • 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

Solutions

Expert Solution

/******************************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 :)


Related Solutions

Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Please don't just copy another solution. Write the following Java program: public static String getFlag(int size,...
Please don't just copy another solution. Write the following Java program: public static String getFlag(int size, char color1, char color2, char color3) - This method returns a string where a triangle appears on the left size of the diagram, followed by horizontal lines. For example, calling DrawingApp.getFlag(9, 'R', '.', 'Y'); will generate the string: R............................................ RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRRR.................................... RRRRRRRRR.................................... RRRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY RRYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY R............................................ The diagram has a number of rows that...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT