I need to make the following code calculates the volume and prints the dimensions and volume like so:
cube one dimensions are: w =7, h=2 d=3, volume = 42
cube two dimensions are: w =5, h=8, d=2, volume =80
and so on
Can somebody please help me?
public static void main(String[] args)
Cube[] cube = new Cube[5];

cube[0] = new Cube(2.0, 3.0, 5.0);
cube[1] = new Cube(1.0, 4.5, 3.3);
cube[2] = new Cube(5.6, 10.0, 2.9);
cube[3] = new Cube(4.5, 7.8, 22.0);
cube[4] = new Cube(9.0, 7.7, 5.1);

for (Cube c : cube)
System.out.println("Cube dimentions are: Width= " + c.getWidth()
+ " Height= " + c.getHeight()
+ " Depth= " + c.getDepth()
+ " and Volume= " + c.getVolume());

class Cube
double width, height, depth, volume;

public Cube(double w, double h, double d, double v)
width = w;
height = h;
depth = d;
volume = v;

public double getWidth()
return width;

public double getHeight()
return height;

public double getDepth()
return depth;

public double getVolume()
return (width * height * depth);



  • I'm not sure I fully understand your question. But instead of the System.out.println(...) call, you could override toString() in the Cube class allowing you to pass the cube in the iteration to the println method.

    As for the original question if your asking if the Java API has anything in it to help you, I don't believe it does. Manual calculation is probably the only way you'll achieve what you're trying to achieve.
    We'll be an army of theives
    Of self-freed slaves
    Of mild-mannered maids
    We'll fight with whispers and blades
    So get ready, a new day is dawning
    - The New Wild West -- Jewel
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion