Last active
December 26, 2015 10:28
-
-
Save joeyv/7136633 to your computer and use it in GitHub Desktop.
Calculates Volume and Surface Area of a sphere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| import java.text.*; | |
| public class Sphere | |
| { | |
| public static void main(String[] args) | |
| { | |
| double pi = Math.PI; | |
| double radius, volume, surfaceArea; | |
| // Get Radius | |
| Scanner scan = new Scanner(System.in); | |
| radius = scan.nextDouble(); | |
| // Calculate volume/area | |
| volume = (double)Math.pow((radius), 3) * pi * (4.0/3.0); | |
| surfaceArea = Math.pow((radius), 2) * pi * 4; | |
| // Format values | |
| DecimalFormat df = new DecimalFormat("0.####"); | |
| // Output values | |
| System.out.println("Volume: " + df.format(volume)); | |
| System.out.println("Surface Area: " + df.format(surfaceArea)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment