Share on Google+Share on Google+

Guily Gwo
Hollow Box
2 Answer(s)      6 years and 6 months 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.

Ads

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
Tutorials   
Java Spring Hibernate Struts Training What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized how to convert war file into .exe file using java code

Ads

 
Advertisement null

Ads