Answer:
Here is the JAVA program to calculate volume of cuboid:
import java.util.Scanner; // Scanner class is used to take input from user
public class CuboidVol { // class to calculate volume of cuboid
public static void main(String[] args) { // start of main() function body
Scanner input= new Scanner(System.in); //create Scanner class object
// prompts user to enter length of cuboid
System.out.println("Enter the cuboid length:");
double length=input.nextDouble(); //reads the input length value from user
// prompts user to enter width of cuboid
System.out.println("Enter the cuboid width:");
double width=input.nextDouble(); //reads the input width from user
// prompts user to enter height of cuboid
System.out.println("Enter the cuboid height:");
double height=input.nextDouble(); //reads the input height from user
/* the following formula is to calculate volume of a cuboid by multiplying its length width and height and a double type variable cuboidVolume is defined to store the value of the resultant volume to it */
double cuboidVolume= length*width*height; //calculates cuboid volume
//displays volume of cuboid and result is displayed up to 2 decimal places
System.out.printf("Volume of the cuboid (length " + length + "/ height " + height + "/ width" +width +" is: " + "%.2f",cuboidVolume); } }
Step-by-step explanation:
The formula for the volume of a cuboid is as following:
Volume = Length × Width × Height
So in order to calculate the volume of cuboid three variable are required for length, width and height and one more variable cuboidVolume to hold the resultant volume of the cuboid.
The program is well explained in the comments added to each statement of the program. The program prompts the user to enter the value of height width and length of cuboid and the nextDouble() method is used to take the double type input values of height length and width. Then the program declares a double type variable cuboidVolume to hold the result of the volume of cuboid. Then the last printf statement is used to display the volume of cuboid in the format format "Volume of the cuboid (length / height / width ) is" and the result is displayed up to 2 decimal places.
The screenshot of the program along with its output is attached.