I need help with this Java programming assignment: Write a class that has a main
ID: 3908469 • Letter: I
Question
I need help with this Java programming assignment:
Write a class that has a main method and two more methods for performing calculations on a rectangular prism (a box). The main method should prompt the user to enter the three dimensions of the prism (as doubles). These variables should be used as arguments to call both prism methods. One prism method should calculate and return the surface area of the prism when called in main. The area should be displayed accurate to three decimal places. The other prism method is a void one. It should calculate and print the volume of the prism accurate to two decimal places when called.
Sample Output
Enter the three dimensions of the prism
Separate them by spaces or press Enter after each
2.5 2.5 2.5
The prism surface area is 37.500
The prism volume is 15.63
Explanation / Answer
import java.util.*;
class Prism
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the three dimensions of the rectangular prism : ");
System.out.println("Separate them by spaces or press Enter after each");
double length = input.nextDouble();
double width = input.nextDouble();
double height = input.nextDouble();
System.out.printf("The prism surface area is %.3f",surfaceArea(length,width,height));
System.out.printf(" The prism volume is %.2f",volume(length,width,height));
}
public static double surfaceArea(double length,double width,double height)
{
return 2*( length*width + width*height + length*height);
}
public static double volume(double length,double width,double height)
{
return length*width*height;
}
}
Output:
Enter the three dimensions of the rectangular prism :2.5 2.5 2.5
Separate them by spaces or press Enter after each
The prism surface area is 37.500
The prism volume is 15.63
Do ask if any doubt. Please upvote.