In: Computer Science
Write the following functions. Each function needs function comments that describes function and its parameters double sphereVolume( double radius) double sphereSuface( double radius) double cylinderVolume( double radius, double height) double coneSurface( double radius, double height) double coneVolume( double radius, double height) That computes the volume and surface of the sphere with radius, a cylinder with a circular base with radius radius , and height height , and a cone with a circular base with radius radius , and height height . Then write a test program to prompt user to enter values of radius and height, call six functions and display the results.
Solution
Code
import java.util.*;
public class Main {
public static void main(String[] args) {
double radius,height;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of radius: ");
radius = sc.nextDouble();
System.out.print("Enter the value of: ");
height = sc.nextDouble();
sphereVolume(radius , height);
sphereSurface(radius , height);
cylinderVolume(radius , height);
cylinderSurface(radius , height);
coneVolume(radius , height);
coneSurface(radius , height);
output(radius , height);
}
public static double sphereVolume(double radius, double height)
{
double volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);
return volume;
}
public static double sphereSurface(double radius, double height)
{
double surface = 4 * Math.PI * Math.pow(radius, 2.0);
return surface;
}
public static double cylinderVolume(double radius, double
height) {
double volume = Math.PI * Math.pow(radius, 2.0) * height;
return volume;
}
public static double cylinderSurface(double radius, double
height) {
double surface = 2 * Math.PI * radius * height + 2 * Math.PI *
Math.pow(radius, 2.0);
return surface;
}
public static double coneVolume(double radius, double height)
{
double volume = (1.0 / 3.0) * Math.PI * Math.pow(radius, 2.0) *
height;
return volume;
}
public static double coneSurface(double radius, double height)
{
double surface = Math.PI * radius * (radius + Math.pow((
Math.pow(radius, 2.0) + Math.pow(height, 2.0)), .5));
return surface;
}
public static void output(double radius, double height) {
System.out.printf("Volume of sphere: %.2f\n", sphereVolume(radius ,
height));
System.out.printf("Surface area of Sphere: %.2f\n",
sphereSurface(radius , height));
System.out.printf("Volume of cylinder: %.2f\n",
cylinderVolume(radius , height));
System.out.printf("Surface area of cylinder: %.2f\n",
cylinderSurface(radius , height));
System.out.printf("Volume of cone: %.2f\n", coneVolume(radius ,
height));
System.out.printf("Surface area of cone: %.2f\n",
coneSurface(radius , height));
}
}
Screenshot
Output
---
all the best