Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write static method: public static double sphereVolume(double r) public static d

ID: 3658870 • Letter: W

Question

Write static method: public static double sphereVolume(double r) public static double sphereSurface(double r) public static double cylinderVolume(double r double h) public static double cylinderSurface(double r double h) public static double coneVolume(double r double h) public static double coneSurface(double r double h) That compute the volume and surface area of a sphere with radius r, a cylinder with circular base with radius r and height h, and cone with circular base with radius u and height h. Place them into class Geometry. Then write a program that prompts the user for the values of r and h, calls the six methods, and prints the results.

Explanation / Answer

import java.text.DecimalFormat; import java.util.Scanner; public class Geometry { public static void main(String args[]) { DecimalFormat df2 = new DecimalFormat("###.##"); double sphereVolumeResult = 0.0; double sphereSurfaceResult = 0.0; double cylinderVolumeResult = 0.0; double cylinderSurfaceResult = 0.0; double coneVolumeResult = 0.0; double coneSurfaceResult = 0.0; Scanner scanner = new Scanner(System.in); System.out.println("Please enter the value of radius"); double radius = scanner.nextDouble(); double height = scanner.nextDouble(); sphereVolumeResult = sphereVolume(radius); System.out.println("Volume of Sphere is " + Double.valueOf(df2.format(sphereVolumeResult))); sphereSurfaceResult = sphereSurface(radius); System.out.println("Surface of Sphere is " + Double.valueOf(df2.format(sphereSurfaceResult))); cylinderVolumeResult = cylinderVolume(radius, height); System.out.println("Volume of Cylinder is " + Double.valueOf(df2.format(cylinderVolumeResult))); cylinderSurfaceResult = cylinderSurface(radius, height); System.out.println("Surface of Cylinder is " + Double.valueOf(df2.format(cylinderSurfaceResult))); coneVolumeResult = coneVolume(radius, height); System.out.println("Volume of Cone is " + Double.valueOf(df2.format(coneVolumeResult))); coneSurfaceResult = coneSurface(radius, height); System.out.println("Surface of Cone is " + Double.valueOf(df2.format(coneSurfaceResult))); } public static double sphereVolume(double r) { double volume = 4 / 3 * Math.PI * r * r * r; return volume; } public static double sphereSurface(double r) { double surface = 4 * Math.PI * r * r; return surface; } public static double cylinderVolume(double r, double h) { double volume = Math.PI * r * r * h; return volume; } public static double cylinderSurface(double r, double h) { double surface = 2 * Math.PI * r * r + 2 * Math.PI * r * h; return surface; } public static double coneVolume(double r, double h) { double volume = (Math.PI * r * r * h) / 3; return volume; } public static double coneSurface(double r, double h) { double surface = 2 * Math.PI * r * (h / 2); return surface; } } Result: ************ Please enter the value of radius 2 3 Volume of Sphere is 25.13 Surface of Sphere is 50.27 Volume of Cylinder is 37.7 Surface of Cylinder is 62.83 Volume of Cone is 12.57 Surface of Cone is 18.85