gramming Exercise: (Rectangular Prism Calculator) Write a program to calculate t
ID: 3744694 • Letter: G
Question
gramming Exercise: (Rectangular Prism Calculator) Write a program to calculate the surface area and volume of a rectangular prism and a capsule shape. Once, the user has entered the following input: length (), width (w) and height (h) for the rectangular prism radius (r) and side length (a) for the capsule It will display the results in a formatted text output. Please it is important to declare all variables with the appropriate data type. Use the following formulas: Capsule Volume-tr(43)r + a) Surface Area 2r(2r + a) Rectangular Prism Volume = Iwh Surface Area 2(1w+lh + wh)Explanation / Answer
import java.util.Scanner; public class RectangularCalculator { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter length of the rectangular prism: "); double lengthOfPrism = in.nextDouble(); System.out.print("Enter width of the rectangular prism: "); double widthOfPrism = in.nextDouble(); System.out.print("Enter height of the rectangular prism: "); double heightOfPrism = in.nextDouble(); double volumeOfPrism = lengthOfPrism * widthOfPrism * heightOfPrism; double surfaceAreaOfPrism = 2*(lengthOfPrism*widthOfPrism + lengthOfPrism*heightOfPrism + widthOfPrism*heightOfPrism); System.out.println("Volume of rectangular prism is " + volumeOfPrism); System.out.println("Surface Area of rectangular prism is " + surfaceAreaOfPrism); System.out.print("Enter radius of the capsule: "); double radiusOfCapsule = in.nextDouble(); System.out.print("Enter side length of the capsule: "); double sideLengthOfCapsule = in.nextDouble(); double volumeOfCapsule = Math.PI*Math.pow(radiusOfCapsule, 2)*(((4.0/3)*radiusOfCapsule)+sideLengthOfCapsule); double surfaceAreaOfCapsule = 2*Math.PI*radiusOfCapsule*(2*radiusOfCapsule+sideLengthOfCapsule); System.out.println("Volume of capsule is " + volumeOfCapsule); System.out.println("Surface area of capsule is " + surfaceAreaOfCapsule); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.