50.8k views
2 votes
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;

User Stephenye
by
4.7k points

2 Answers

6 votes

Final answer:

To define the method pyramidVolume, use the provided geometry equations and formulas. Calculate the base area by multiplying the base length and width. Finally, use the base area and pyramid height to calculate the volume.

Step-by-step explanation:

To define the method pyramidVolume, we need to use the relevant geometry equations and formulas provided. The volume of a pyramid with a rectangular base can be calculated using the formula: Volume = base area x height x 1/3. The base area of the pyramid can be found by multiplying the base length and base width. Therefore, the method pyramidVolume can be defined as follows:

public static double pyramidVolume(double baseLength, double baseWidth, double pyramidHeight) {
double baseArea = baseLength * baseWidth;
double volume = baseArea * pyramidHeight * 1/3;
return volume;
}
User Fred Collins
by
5.3k points
1 vote

Answer:

Step-by-step 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.

User Olivier Samyn
by
5.3k points