Declare method sphereVolume to calculate and return the volume of the sphere. Use the following statement to calculate the volume:
```
double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3)
```
Write a Java application that prompts the user for the double radius of a sphere, calls sphereVolume
to calculate the volume and displays the result.
The following solution calculates the volume of a sphere, using the radius entered by the user:
```
// Sphere.java
// Calculate the volume of a sphere.
import java.util.Scanner;
public class Sphere {
// obtain radius from user and display volume of sphere
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter radius of sphere: ");
double radius = input.nextDouble();
System.out.printf("Volume is %f%n", sphereVolume(radius));
}
// calculate and return sphere volume
public static double sphereVolume(double radius) {
double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
return volume;
}
}
```
Enter radius of sphere: 4
Volume is 268.082573
You might also like to view...
What are the difference between user interface (U I or G U I) design and user experience (U X) design?
What will be an ideal response?
The ____ value of the @media rule is the distance from the bottom to the top of the targeted display area, and optimally the minimum or maximum measurement.
A. y B. device-aspect-ratio C. aspect ratio D. height
Which structured cabling component provides connectivity to computer equipment in the nearby work area?
A. MDF B. IDF C. Equipment room D. Vertical cabling
The term ____ refers to the gap between those who have access to information and communications technology and those who do not.
A. “digital have not” B. “digital divide” C. “Internet divide” D. “bit divide”