Define a method pyramidVolume with double parameters baseLength, baseWidth, and pyramidHeight, that returns as a double the volume of a pyramid with a rectangular base. Relevant geometry equations:

Volume = base area x height x 1/3
Base area = base length x base width.
(Watch out for integer division)

import java.util.Scanner;

public class CalcPyramidVolume {

/* Your solution goes here */

public static void main (String [] args) {
System.out.println("Volume for 1.0, 1.0, 1.0 is: " + pyramidVolume(1.0, 1.0, 1.0));
return;

Respuesta :

Answer:

Explanation:

Its not a good practice to write all the functions including main() in the same class.

But as you opted for this, the code goes here like this:

public class CalcPyramidVolume {

/* Your solution goes here */

static double pyramidVolume(double baseLength, double baseWidth, double pyramidHeight)

{

double Volume,baseArea;

baseArea = baseLength * baseWidth;

Volume = baseArea * pyramidHeight * 1/3;

return Volume;

}

public static void main (String [] args) {

System.out.println("Volume for 1.0, 1.0, 1.0 is: " + pyramidVolume(1.0, 1.0, 1.0));

return;

}

}

And there is not caveat of integer division, as you are declaring all your variables of type double.

Answer:  Volume = 8/3

Explanation:

Let x represent the base length, base width and height  

Volume = 2x X 2x X 2x X 1/3 = 8x³/3

When x = 1.0

∴ Volume = 8(1.0)³/3 = 8/3