Help with calculating area of cube in java? it won't go on to the second part?
- husoskiLv 76 months agoFavorite Answer
If the "second part" is the calcArea() method; you have to call it in main() to get it to run. The Java runtime automatically calls main() to get things started. Every other method must be called in order to run.
Right after reading in the height value (either before or after closing the Scanner object), add:
double area = calcArea(length, width, height);
After that, you can deal with the fact that your calcArea() method is actually computing volume of a cuboid (aka rectangular prism) and not the area of a cube.
The area of a cuboid is 2.0*width*height + 2.0*width*length + 2.0*height*length.
If you write it that way, you won't get any weird results on very large cuboids. On the other hand, your current code multiplies length * width * height. Those are all integer parameters, so the computation is done in integers; and only afterward will the product be converted to double. It would be better to make the parameters double. If you can't do that, you should use double constants (like 2.0 instead of just 2) and/or casting to (double) to force computations to be done in double precision.
- Anonymous6 months ago
where did you called the function calcArea() under main.
- EddieJLv 76 months ago
You need to call calcArea (), and pass it the numbers that the program read in.
However, the formula calculates the volume of the cube, NOT the surface area.
You should always supply the actual text -- not just a fuzzy photo.