How would I use three methods to produce an output like this?
Please enter the fill character: "z"
Please enter the size of the box (0 to 80): "3"
+---+
|zzz|
|zzz|
|zzz|
+---+
My code is able to produce a box, however I am having issues understanding the use of other methods to create the border around it.
import java.util.Scanner;
public class SolidBoxes
{
public static void main(String[] args)
{
int start = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the fill character: ");
String character = scan.next();
System.out.print("Please enter the size of the box (0 to 80): ");
int size = scan.nextInt();
if ( size > 80 || size < 0)
{
System.out.println("Please enter the size of the box (0 to 80): ");
size = scan.nextInt();
}
for ( int i = 0; i < size; i++)
{
System.out.println();
for ( int j = 0; j < size; j++)
{
System.out.print(character);
}
}
}
}
This gives me the output:
Please enter the fill character: z
Please enter the size of the box (0 to 80): 3
zzz
zzz
zzz
How is it possible to add two other methods for the “+—+” and another method “|”?
Methods provide a way to compartmentalize portions of your code into various tasks you want to perform. Typically a method will perform all actions required for a specific task. Methods are called from other code when that calling code wants to perform the specific task that you implemented in the method. For example if you want to draw a shape in your main you might have a method that looks something like this:
Then inside your main whenever you want to draw that shape you would just call the method like this:
In the method above the
publicpart is the access modifier which tells us that this method is publicly available to any code that can “see” it. Thevoidpart is the return type, in this case it is not returning anything. ThedrawShapeis the method name.In your case it looks like you need to provide three separate methods. To start with you should define a method for outputting your first line and then getting the fill character and returning that to the main. Then provide a second method to output the second line and return the size of the box to main. Finally provide a third method to output the box based on the first two inputs that you received. When you are finished writing those three methods then call them in the right order from main to run the complete program.