1. (In Java) Write static methods for the following • publicstaticdoublespher
ID: 3686275 • Letter: 1
Question
1. (In Java) Write static methods for the following • publicstaticdoublesphereVolume(doubler) • publicstaticdoublesphereSurface(doubler) • publicstaticdoublecylinderVolume(doubler,doubleh) • publicstaticdoublecylinderSurface(doubler,doubleh) • publicstaticdoubleconeVolume(doubler,doubleh) • publicstaticdoubleconeSurface(doubler,doubleh) that compute the volume and surface area of a sphere with a radius r, a cylinder with a circular base with radius r and height h, and a cone with a circular base with radius r and height h. Place them into a 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. Incorporate proper exception handling for all methods as well as the program.
Explanation / Answer
Hello there ,
Kindly find below code and its output.
/**
* This class provides various static methods to calculate surface and volume.
* @author dipal.prajapati
*
*/
public class Geometry
{
public static double sphereVolume(double r)
{
return (4.0 / 3.0) * Math.PI * r * r * r;
}
public static double sphereSurface(double r)
{
return 4.0 * Math.PI * r * r;
}
public static double cylinderVolume(double r, double h)
{
return h * Math.PI * r * r;
}
public static double cylinderSurface(double r, double h)
{
return (2.0 * r * Math.PI * h) + (2.0 * Math.PI * r * r);
}
public static double coneVolume(double r, double h)
{
return (1.0 / 3.0) * Math.PI * r * r * h;
}
public static double coneSurface(double r, double h)
{
return Math.PI * r * (h + r);
}
}
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* It calculate various geometry's surface and volume.
* @author dipal.prajapati
*
*/
public class SurfaceVolumeCalculation {
static DecimalFormat df = new DecimalFormat("####0.00");
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the radius: ");
double r = in.nextDouble();
System.out.println("Please enter the height: ");
double h = in.nextDouble();
System.out.println("The volume of the sphere is: "+ df.format(Geometry.sphereVolume(r)));
System.out.println("The surface area of the sphere is: "+ df.format(Geometry.sphereSurface(r)));
System.out.println("The volume of the cylinder is: "+ df.format(Geometry.cylinderVolume(r, h)));
System.out.println("The surface area of the cylinder is: "+ df.format(Geometry.cylinderSurface(r, h)));
System.out.println("The volume of the cone is: "+ df.format(Geometry.coneVolume(r, h)));
System.out.println("The surface area of the cone is: "+df.format(Geometry.coneSurface(r, h)));
}
}
===O/P===
Please enter the radius:
23.57
Please enter the height:
34.9
The volume of the sphere is: 54848.83
The surface area of the sphere is: 6981.18
The volume of the cylinder is: 60910.82
The surface area of the cylinder is: 8659.10
The volume of the cone is: 20303.61
The surface area of the cone is: 4329.55
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.