Answer:
public class CalcPyramidVolume {
public static void main(String[] args) {
// Test the method pyramidVolume() by printing out a test case
// result.
System.out.println("Volume for 1.0, 1.0, 1.0 is: " + pyramidVolume(1.0, 1.0, 1.0));
return;
}
// Method name is pyramidVolume()
// It receives three parameters of type double - baseLength,
// baseWidth, and baseHeight;
// It returns the calculated volume (which is of type double) of the
// pyramid using the appropriate formula.
public static double pyramidVolume(double baseLength, double baseWidth, double baseHeight) {
// From the parameters given, calculate the base area of the
// pyramid first.
double baseArea = baseLength * baseWidth;
// From the base area, calculate the volume of the pyramid.
// Note the use of 1 / 3.0 rather than 1 / 3. This is to bypass the
// integer division of 1/3 which will give zero(0).
double volume = baseArea * baseHeight * 1 / 3.0;
// Return the calculated volume
return volume;
}
}
Explanation:
There are two versions to this code. The first one allows the user to enter the values for the length, width and height of the pyramid. The second version uses preset values for the length, breadth and height of the pyramid. The second version is represented in the answer above.
The source code files for the two versions have been attached to this response. Kindly download the files and go through the codes especially the comments. Every segment of the codes contains explanatory comments. Carefully go through these comments for explanation of the codes.
Hope this helps!
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark">
java
</span>
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark">
java
</span>