Here is my assignment due tonight, it is to create two boxes and first default box will show after user presses '1', and the argument '2' leads to a second box which will ask for user input of height, width, and length, then calculate the area and volume for it.

My question is that my argument '2' is not working, can some one help me with this please? It is very urgent and my homework is due in 10 hours :(

public class lliuBox {
private double length; 
private double height;
private double width;

lliuBox(){
    length = 2.0;
    height = 2.0;
    width = 2.0;
}
lliuBox(double l, double h, double w){
    this.length = l;
    this.height = h;
    this.width = w;
}
public void printBox(){
    System.out.println("length: " + length);
    System.out.println("height: " + height);
    System.out.println("width: " + width);
}
int volume(){
    return (int)(length*height*width);
}
int surfaceArea(){
    return (int) (2*length*height + 2*length*width + 2*width*height);
}

public void setLength(double length){
    this.length = length;
}
public void setHeight(double height){
    this.length = height;
}
public void setWidth(double width){
    this.length = width;
}
public double getLength(){
    return length;
}
public double getHeight(){
    return height;
}
public double getWidth(){
    return width;
}
}


import java.io.*;
public class lliuBoxTest {
    public static void main(String args[])
    throws java.io.IOException{
        BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
        String len, hei, wid;
        double valuelength, valueheight,valuewidth;
        char choice;
        lliuBox x = new lliuBox();
        lliuBox y = new lliuBox();
        for (;;){
                do{
            System.out.println("Welcome to create-a-box! What would you like to do?");
            System.out.println("1. Create a box");
            System.out.println("2. Create a box (dimensions specified)");
                  do{
                choice = (char) System.in.read();
                    }while(choice == '\n'|choice == '\r');
                  } while(choice < '1'|choice > '2');
                System.out.println();

                switch(choice){
                case '1': 
                    System.out.println("Selection: ");
                    System.out.println("1");
                    System.out.println("A box appeared!");
                    System.out.println("length: "+ x.getLength());
                    System.out.println("length: "+ x.getHeight());
                    System.out.println("length: "+ x.getWidth());
                    System.out.println("Surface Area: "+ x.surfaceArea());
                    System.out.println("Volume: "+ x.volume());
                    break;

                case '2':
                    System.out.println("Selection: ");
                    System.out.println("2");

                        System.out.println("Please enter the box's length: " );
                        len = br.readLine();
                        valuelength = Double.parseDouble(len);

                        System.out.println("Please enter the box's height: " );
                        hei = br.readLine();
                        valueheight = Double.parseDouble(hei);

                        System.out.println("Please enter the box's width: " );
                        wid = br.readLine();
                        valuewidth = Double.parseDouble(wid);

                        System.out.println("A box appeared!" );

                    y.setHeight(valueheight);
                    y.setLength(valuelength);
                    y.setWidth(valuewidth);
                    y.printBox();

                    System.out.println("Volume: "+ y.surfaceArea());
                    System.out.println("Surface Area: "+ y.volume());
                    break;
                }
                System.out.println();
                }

}
}

Recommended Answers

All 7 Replies

My question is that my argument '2' is not working,

Please explain what you mean by "not working".
Show the console from when you execute the code and add comments to it showing what it wrong with the way the program is executing.

On Windows To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Please edit your post and wrap the code in code tags. Use the [CODE} icon above the input box.
Unformatted code is hard to read.

Here is my assignment due tonight, it is to create two boxes and first default box will show after user presses '1', and the argument '2' leads to a second box which will ask for user input of height, width, and length, then calculate the area and volume for it.

My question is that my argument '2' is not working, can some one help me with this please? It is very urgent and my homework is due in 10 hours

public class lliuBox {
private double length;
private double height;
private double width;

lliuBox(){
length = 2.0;
height = 2.0;
width = 2.0;
}
lliuBox(double l, double h, double w){
this.length = l;
this.height = h;
this.width = w;
}
public void printBox(){
System.out.println("length: " + length);
System.out.println("height: " + height);
System.out.println("width: " + width);
}
int volume(){
return (int)(length*height*width);
}
int surfaceArea(){
return (int) (2*length*height + 2*length*width + 2*width*height);
}

public void setLength(double length){
this.length = length;
}
public void setHeight(double height){
this.length = height;
}
public void setWidth(double width){
this.length = width;
}
public double getLength(){
return length;
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
}


import java.io.*;
public class lliuBoxTest {
public static void main(String args[])
throws java.io.IOException{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String len, hei, wid;
double valuelength, valueheight,valuewidth;
char choice;
lliuBox x = new lliuBox();
lliuBox y = new lliuBox();
for (;{
do{
System.out.println("Welcome to create-a-box! What would you like to do?");
System.out.println("1. Create a box");
System.out.println("2. Create a box (dimensions specified)");
do{
choice = (char) System.in.read();
}while(choice == '\n'|choice == '\r');
} while(choice < '1'|choice > '2');
System.out.println();

switch(choice){
case '1':
System.out.println("Selection: ");
System.out.println("1");
System.out.println("A box appeared!");
System.out.println("length: "+ x.getLength());
System.out.println("length: "+ x.getHeight());
System.out.println("length: "+ x.getWidth());
System.out.println("Surface Area: "+ x.surfaceArea());
System.out.println("Volume: "+ x.volume());
break;

case '2':
System.out.println("Selection: ");
System.out.println("2");

System.out.println("Please enter the box's length: " );
len = br.readLine();
valuelength = Double.parseDouble(len);

System.out.println("Please enter the box's height: " );
hei = br.readLine();
valueheight = Double.parseDouble(hei);

System.out.println("Please enter the box's width: " );
wid = br.readLine();
valuewidth = Double.parseDouble(wid);

System.out.println("A box appeared!" );

y.setHeight(valueheight);
y.setLength(valuelength);
y.setWidth(valuewidth);
y.printBox();

System.out.println("Volume: "+ y.surfaceArea());
System.out.println("Surface Area: "+ y.volume());
break;
}
System.out.println();
}

}
}

My question is that my argument '2' is not working

Please explain what "not working" means?
Does it compile without errors? If not, post the full text of the error messages.
Does it execute and give the wrong results?
If so, show what it outputs and explain what is wrong with it and show what it should be.

Please edit your post and wrap the code in code tags. Use the [CODE} icon above the input box.
Unformatted code is hard to read.

Thanks for the reply, I tried and it is still the same posting I had. I am using eclipse and not using a command prompt window. (not sure I'm referring same thing as you did, because I am not in technology and my master's program requires this java class.)
Regarding not working, the console tells me after compile the error message:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at lliuBoxTest.main(lliuBoxTest.java:45)

I have no idea how to fix it, hope you can help me with it. Appreciate it!

Please explain what you mean by "not working".
Show the console from when you execute the code and add comments to it showing what it wrong with the way the program is executing.

On Windows To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Please edit your post and wrap the code in code tags. Use the [CODE} icon above the input box.
Unformatted code is hard to read.

Look at line 45 in your code where you call the parseDouble() method. The String being passed to the method does not contain a valid double value.

Make sure that String has a number in it before calling the parseDouble() method.

Thanks, I realized it was the problem. Can you suggest a way to fix it? thank you.

Look at line 45 in your code where you call the parseDouble() method. The String being passed to the method does not contain a valid double value.

Make sure that String has a number in it before calling the parseDouble() method.

The String class has methods will show if the String is empty or has a length of 0.
You could use one of those.
For the case that the user can give you no input or bad input, you will want to have a loop that asks the user for input, gets the input, tests the input and either exits the loop if the input is good, or gives an error message and goes back to the top of the loop and asks the user for the input again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.