L
luch
Help! I'm trying to do a simple program, where the user is prompted to
input the number of dimensions to specify for a box; then, 1 of 3
classes of box() are implemented:
0 dimensions // then box box1 = new box();
1 dimension // then box box1 = new box(len);
3 dimensions // then box box1 = new box(len, wid, ht)
but when I compile, I get the message:
Z:\Chapter 08\box.java:107: cannot find symbol
symbol : variable box1
location: class box
System.out.println(box1.toString());
The class assignments are done within an if statement (the commented
switch structure also gave me an error, something about how box1 is
already assigned, when it gets to the second case.)
My code is below - any help is greatly appreciated.
**********************************************************
import java.util.*;
import java.io.*;
public class box
{
public static double len, wid, ht, larger;
public static double largerlen, largerwid, largerht;
static String outmsg;
static Scanner console = new Scanner(System.in);
static char dimen;
public double boxVol(double len, double wid, double ht)
{
return len * wid * ht;
}
public double boxArea(double len, double wid, double ht)
{
return (2 * len * wid) + (2 * len * ht) + (2 * wid * ht);
}
public void makeLarger(double larger)
{
this.len = this.len * larger;
this.wid = this.wid * larger;
this.ht = this.ht * larger;
outmsg = "After each dimension is multiplied by " +
String.format("%.2f",larger)
+ ", the box measurements are:\n";
}
public void makeLarger(double largerlen, double largerwid, double
largerht)
{
this.len = this.len + largerlen;
this.wid = this.wid + largerwid;
this.ht = this.ht + largerht;
outmsg = "After length is increased by " +
String.format("%.2f",largerlen)
+ ",\n width is increased by " +
String.format("%.2f",largerwid)
+ ",\n and height is increased by " +
String.format("%.2f",largerht)
+ ",\n the box measurements are: \n";
}
public String toString()
{
String str = "";
str = "Box dimensions:\n"
+ "Length = " + String.format("%.2f",len)
+ " Width: = " + String.format("%.2f",wid)
+ " Height = " + String.format("%.2f",ht) + "\n";
str = str + "Area = " + String.format("%.2f",boxArea(len, wid, ht))
+ " Volume = " + String.format("%.2f",boxVol(len, wid, ht));
return str;
}
public static void main (String[] args) throws FileNotFoundException
{
System.out.println("This program will calculate the dimensions of a
box based on the\n"
+ "values you enter.");
System.out.println("Enter the number of dimensions you want to
specify:\n:"
+ "0 for no dimensions specified (default of 1) or\n"
+ "1 for the same dimension to be applied to all 3 (len, wid, ht)
or\n"
+ "3 to specify each individual dimension\n");
dimen = console.next().charAt(0);
if (dimen == '0')
// switch (dimen)
// {
// case '0':
{
box box1 = new box();
}
// break;
else if (dimen == '1')
{
// case '1':
System.out.println("Enter the length of the box:");
len = console.nextDouble();
box box1 = new box(len);
// break;
}
else
// case '3':
{
System.out.println("Enter the length of the box:");
len = console.nextDouble();
System.out.println("Enter the width of the box:");
wid = console.nextDouble();
System.out.println("Enter the height of the box:");
ht = console.nextDouble();
box box1 = new box(len,wid,ht);
}
// break;
// }
System.out.println(box1.toString());
System.out.println("-----------------------------");
System.out.println();
System.out.println("Enter one value to multiply each dimension by:");
larger = console.nextDouble();
box1.makeLarger(larger);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();
System.out.println("=============================");
System.out.println("Enter a value to add to the length:");
largerlen = console.nextDouble();
System.out.println("Enter a value to add to the width:");
largerwid = console.nextDouble();
System.out.println("Enter a value to add to the height:");
largerht = console.nextDouble();
box1.makeLarger(largerlen,largerwid,largerht);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();
System.out.println("=============================");
}
public box()
{
this.len = 1.0;
this.wid = 1.0;
this.ht = 1.0;
}
public box(double len)
{
this.len = len;
this.wid = len;
this.ht = len;
}
public box (double len, double wid, double ht)
{
this.len = len;
this.wid = wid;
this.ht = ht;
}
}
input the number of dimensions to specify for a box; then, 1 of 3
classes of box() are implemented:
0 dimensions // then box box1 = new box();
1 dimension // then box box1 = new box(len);
3 dimensions // then box box1 = new box(len, wid, ht)
but when I compile, I get the message:
Z:\Chapter 08\box.java:107: cannot find symbol
symbol : variable box1
location: class box
System.out.println(box1.toString());
The class assignments are done within an if statement (the commented
switch structure also gave me an error, something about how box1 is
already assigned, when it gets to the second case.)
My code is below - any help is greatly appreciated.
**********************************************************
import java.util.*;
import java.io.*;
public class box
{
public static double len, wid, ht, larger;
public static double largerlen, largerwid, largerht;
static String outmsg;
static Scanner console = new Scanner(System.in);
static char dimen;
public double boxVol(double len, double wid, double ht)
{
return len * wid * ht;
}
public double boxArea(double len, double wid, double ht)
{
return (2 * len * wid) + (2 * len * ht) + (2 * wid * ht);
}
public void makeLarger(double larger)
{
this.len = this.len * larger;
this.wid = this.wid * larger;
this.ht = this.ht * larger;
outmsg = "After each dimension is multiplied by " +
String.format("%.2f",larger)
+ ", the box measurements are:\n";
}
public void makeLarger(double largerlen, double largerwid, double
largerht)
{
this.len = this.len + largerlen;
this.wid = this.wid + largerwid;
this.ht = this.ht + largerht;
outmsg = "After length is increased by " +
String.format("%.2f",largerlen)
+ ",\n width is increased by " +
String.format("%.2f",largerwid)
+ ",\n and height is increased by " +
String.format("%.2f",largerht)
+ ",\n the box measurements are: \n";
}
public String toString()
{
String str = "";
str = "Box dimensions:\n"
+ "Length = " + String.format("%.2f",len)
+ " Width: = " + String.format("%.2f",wid)
+ " Height = " + String.format("%.2f",ht) + "\n";
str = str + "Area = " + String.format("%.2f",boxArea(len, wid, ht))
+ " Volume = " + String.format("%.2f",boxVol(len, wid, ht));
return str;
}
public static void main (String[] args) throws FileNotFoundException
{
System.out.println("This program will calculate the dimensions of a
box based on the\n"
+ "values you enter.");
System.out.println("Enter the number of dimensions you want to
specify:\n:"
+ "0 for no dimensions specified (default of 1) or\n"
+ "1 for the same dimension to be applied to all 3 (len, wid, ht)
or\n"
+ "3 to specify each individual dimension\n");
dimen = console.next().charAt(0);
if (dimen == '0')
// switch (dimen)
// {
// case '0':
{
box box1 = new box();
}
// break;
else if (dimen == '1')
{
// case '1':
System.out.println("Enter the length of the box:");
len = console.nextDouble();
box box1 = new box(len);
// break;
}
else
// case '3':
{
System.out.println("Enter the length of the box:");
len = console.nextDouble();
System.out.println("Enter the width of the box:");
wid = console.nextDouble();
System.out.println("Enter the height of the box:");
ht = console.nextDouble();
box box1 = new box(len,wid,ht);
}
// break;
// }
System.out.println(box1.toString());
System.out.println("-----------------------------");
System.out.println();
System.out.println("Enter one value to multiply each dimension by:");
larger = console.nextDouble();
box1.makeLarger(larger);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();
System.out.println("=============================");
System.out.println("Enter a value to add to the length:");
largerlen = console.nextDouble();
System.out.println("Enter a value to add to the width:");
largerwid = console.nextDouble();
System.out.println("Enter a value to add to the height:");
largerht = console.nextDouble();
box1.makeLarger(largerlen,largerwid,largerht);
System.out.println(outmsg);
System.out.println(box1.toString());
System.out.println();
System.out.println("=============================");
}
public box()
{
this.len = 1.0;
this.wid = 1.0;
this.ht = 1.0;
}
public box(double len)
{
this.len = len;
this.wid = len;
this.ht = len;
}
public box (double len, double wid, double ht)
{
this.len = len;
this.wid = wid;
this.ht = ht;
}
}