Reputation: 75
This is my task that I have to do:
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.
Here is my code:
import java.util.Scanner;
public class CalcPyramidVolume {
public static void pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
baseLength = 1.0;
baseWidth = 1.0;
pyramidHeight = 1.0;
double pyramidVolume = ((baseLength * baseWidth) * pyramidHeight) / 3;
}
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;
}
}
I can edit only the section of code where I created the pyramidVolume method call. I am getting an error that says 'void' type not allowed here and it is pointing the to system.out line which i can not edit. I am very confused on why it is giving me an error on that line.
Upvotes: 0
Views: 42703
Reputation: 1
static double calcPyramidVolume(double baseLength, double baseWidth, double
pyramidHeight) {
double Volume, baseArea;
baseArea = baseLength * baseWidth;
Volume = baseArea * pyramidHeight * 1/3;
return Volume;
}
is actually the correct code for this assignment.
Upvotes: -1
Reputation: 4029
pyramidVolume
return type is void
. Change return type to double
as below:
public static double pyramidVolume (double baseLength, double baseWidth, double pyramidHeight) {
double pyramidVolume = ((baseLength * baseWidth) * pyramidHeight) / 3;
return pyramidVolume;
}
Upvotes: 3