Hollow Box
Write an application program that will draw a hollow box (a box with empty space in between). The program will ask the user to key in a whole number larger or equals to 5. An error message would appear if the user keys in a number less than 5. The program should only exit if the user chooses to exit. For example, if the user keys in 8, the hollow box (of length and width of 8 characters) should be as follows :
88888888
88888888
88 88
88 88
88 88
88 88
88888888
88888888
The walls of the box are fixed at 2 layers. Hence, another example, if the user keys in 7, the hollow box (of length and width of 7 characters) should be as follows :
7777777
7777777
77 77
77 77
77 77
7777777
7777777
Provide a tester class to test the implementation of your solution. Writing the entire program in a single Class is not allowed.
View Answers
October 28, 2010 at 4:07 PM
Hello Friend,
Try this:
import java.util.*;
class Box{
public void check(Scanner input,int key){
if(key<5){
System.out.println("Error!Enter another key (>=5): ");
key=input.nextInt();
}
for(int i=1;i<=2;i++){
for(int j=1;j<=key;j++){
System.out.print(key);
}
System.out.print(" ");
}
int n=key-4;
int c=n*2;
for(int i=1;i<=c;i++){
for(int j=1;j<=2;j++){
System.out.print(key);
}
System.out.print(" ");
}
for(int i=1;i<=2;i++){
for(int j=1;j<=key;j++){
System.out.print(key);
}
System.out.print(" ");
}
}
}
public class HollowBox{
public static void main(String[] args){
Box b=new Box();
Scanner input=new Scanner(System.in);
int menu = 0;
System.out.println("Choose Option");
System.out.println("1. Draw");
System.out.println("2. Exit");
boolean quit = false;
do{
System.out.println();
System.out.print("Please enter your choice: ");
menu = input.nextInt();
System.out.println();
switch(menu) {
case 1:
System.out.println("Enter key (>=5): ");
int key=input.nextInt();
b.check(input,key);
break;
case 2:
quit = true;
break;
}
}
while (!quit);
}
}
Thanks
November 2, 2010 at 8:03 PM
thx so much buddy.... so helpfull ^^
Related Tutorials/Questions & Answers:
Hollow BoxHollow Box Write an application program that will draw a
hollow box... to exit. For example, if the user keys in 8, the
hollow box (of length and width..., another example, if the user keys in 7, the
hollow box (of length and width
printing hollow squareprinting
hollow square Write an application (PrintingSquares.java) that prompts the user to enter the size of the side of a square, then displays a
hollow square of that size made of asterisks. Your program should work
Advertisements
ModuleNotFoundError: No module named 'box-box'ModuleNotFoundError: No module named '
box-
box' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
box-
box'
How to remove the ModuleNotFoundError: No module named '
box-
box ModuleNotFoundError: No module named 'box-box'ModuleNotFoundError: No module named '
box-
box' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
box-
box'
How to remove the ModuleNotFoundError: No module named '
box-
box input box input box give me the code of input
box in core java
input box input box give me the code of input
box in core java
select box and text box validationsselect
box and text
box validations hi,
any one please tell me how to set validations for select
box and text boxes using bean classes?
thank you
Please visit the following link:
http://www.roseindia.net/jsp/user
dropdown boxdropdown box i need to have country,state and city in drop down
box using ajax and use db2 database
Have a look at the following link:
JSP dependent dropdown
dialog boxdialog
box to ask to quit or continue hello.
write an application that displays a series of at least four interview questions and each question... of the interview, use a dialog
box to ask whether the user wants to
- enter another set
dialog boxdialog
box to show latest question hello.
write an application that displays a series of at least four interview questions and each question should... of the interview, use a dialog
box to ask whether the user wants to
- enter another set
combo boxcombo box Hi,
[_|] dropdown
box
[ ] [INCLUDE... a screen like this using jsp-servlet(or DAO,DTO),in that drop down
box i should get
dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog boxdialog box hello.
write an application that displays a series of at least four interview questions and each question should have at least three possible numeric choice answers.
at the end of the interview, use a dialog
box dialog box.
at the end of the interview, use a dialog
box to ask whether the user wants
dialog box of the interview, use a dialog
box to ask whether the user wants to
- enter another set
dialog box.
at the end of the interview, use a dialog
box to ask whether the user wants
box +15box +15 i want help to complete this program.
import java.util.*;
class Mgic2{
public static void main(String []args){
Random r=new Random();
int [][] magic=new int[3][3];
boolean v15=true;
while(v15){
int
check box left justificationcheck
box left justification how to make a check
box left justification which is at right end?
thanks in advance
autosuggest box - Ajaxautosuggest
box Java example How to implement auto suggest
box using Ajax-DWR technology in jsp/html
Text box CheckingText
box Checking I want to Write only the word A-Z in a text
box What is the code