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();
}
}
}