Methods: Pyramid Volume

Methods: Pyramid Volume

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.

4

1 Answer

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;
}  
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.