Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Guily Gwo
Hollow Box
2 Answer(s)      4 years and a month ago
Posted in : Java Beginners


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.


Advertisement
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 Box
Hollow 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 square
printing 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
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
HTML in the box
HTML in the box  Can I use any HTML in the box
select box and text box validations
select 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 box
dropdown 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 box
dialog 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 box
dialog 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 box
combo box  Hi, [_|] dropdown box [ ] [INCLUDE... a screen like this using jsp-servlet(or DAO,DTO),in that drop down box i should get
dialog box
dialog 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
dialog 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
dialog 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
dialog 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
dialog 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
dialog 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 +15
box +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
How to create a confirmation box?
How to create a confirmation box?  How to create a confirmation box
What is a prompt box?
What is a prompt box?  What is a prompt box
How to create an input box?
How to create an input box?  How to create an input box
javascript confirm box
javascript confirm box  javascript confirm box
check box left justification
check box left justification  how to make a check box left justification which is at right end? thanks in advance
how to insert check box
how to insert check box   how to insert check box into jtable row in swing
populate Combo Box dynamically
populate Combo Box dynamically  Hi, How to populate the Combo Box dynamically using Flex with Jsp's
Text box Checking
Text box Checking  I want to Write only the word A-Z in a text box What is the code
autosuggest box - Ajax
autosuggest box Java example  How to implement auto suggest box using Ajax-DWR technology in jsp/html
combo box - JSP-Servlet
combo box  how to get a combo box in jsp page which allows editing as well as list box
change appearance of alert box
change appearance of alert box  how to change the appearance of an alert() box
 

 

 

DMCA.com