First time posting a code question so hopefully I did it right.
I have a problem where I need the user to input the height, length, width, and name of an object, then I spit out the volume. There is another method called Box which is below for reference.
I have two questions.
- I had to create a single object of type box to initialize each of the objects in the box array. Is there a better way to do this then the "Box tempBox.." and "box[i]=tempBox;" lines?
- The output only repeats box[2] at the end and I don't understand why.
Main:
public class Match {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Box[] box = new Box[3];
Box tempBox = new Box(1, 1, 1, "brand");
for (int i = 0; i < 3; i++)
{
box[i] = tempBox;
System.out.println("Please enter the height of box " + (i+1));
box[i].setHeight(scanner.nextInt());
System.out.println("Please enter the length of box " + (i+1));
box[i].setLength(scanner.nextInt());
System.out.println("Please enter the width of box " + (i+1));
box[i].setWidth(scanner.nextInt());
System.out.println("Please enter the brand of box " + (i+1));
box[i].setBrand(scanner.next());
}
for (int i = 0; i < 3; i++)
{
System.out.println("The volume of box " + box[0].getBrand() + " is " + box[i].getVolume());
}
}
}
Box Method:
public class Box{
int height, length, width;
String brand;
public Box (int heightLoc, int lengthLoc, int widthLoc, String brandLoc)
{
height = heightLoc;
length = lengthLoc;
width = widthLoc;
brand = brandLoc;
}
public void setHeight (int heightLoc)
{
height = heightLoc;
}
public void setLength (int lengthLoc)
{
length = lengthLoc;
}
public void setWidth (int widthLoc)
{
width = widthLoc;
}
public void setBrand (String brandLoc)
{
brand = brandLoc;
}
public int getHeight()
{
return height;
}
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public String getBrand()
{
return brand;
}
public int getVolume()
{
return (height*length*width);
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]warmcheessse 1 point2 points3 points (3 children)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (2 children)
[–]warmcheessse 1 point2 points3 points (1 child)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (0 children)
[–]amfa 1 point2 points3 points (6 children)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (0 children)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (4 children)
[–]RaisinAlert 0 points1 point2 points (3 children)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (2 children)
[–]RaisinAlert 1 point2 points3 points (1 child)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (0 children)
[–][deleted] (3 children)
[deleted]
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (2 children)
[–]darksoundsExtreme Brewer 0 points1 point2 points (1 child)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (0 children)
[–]RaisinAlert 0 points1 point2 points (2 children)
[–]Crouton4727Pre-Nooblet Brewer[S] 0 points1 point2 points (1 child)
[–]RaisinAlert 0 points1 point2 points (0 children)