Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.Math.*;
  3. import java.text.DecimalFormat;
  4.  
  5. // Java program to calculate the volume of a sphere, cube or tetrahedron
  6.  
  7. class VolumeOfShapes {
  8.  
  9. private static DecimalFormat df2 = new DecimalFormat("#.##");
  10.  
  11. public static void main(String [] args) {
  12.  
  13. Shape();
  14.  
  15. }
  16.  
  17. public static void Shape(){
  18.  
  19. Scanner in = new Scanner(System.in);
  20. System.out.println("What shape would you like me to calculate the volume of?");
  21. String shape;
  22. shape = in.nextLine();
  23.  
  24. switch(shape)
  25. {
  26. case "sphere":
  27. System.out.println("What is the radius of the sphere?");
  28. double radius = in.nextDouble();
  29. Sphere(radius);
  30. break;
  31. case "cube":
  32. System.out.println("What is the radius of the cube?");
  33. double width = in.nextDouble();
  34. Cube(width);
  35. break;
  36. case "tetrahedron":
  37. System.out.println("What is the radius of the tetrahedron?");
  38. double edge = in.nextDouble();
  39. Tetrahedron(edge);
  40. break;
  41. case "quit":
  42. break;
  43. default:
  44. System.out.println("Please enter 'sphere', 'cube' or 'tetrahedron' to continue. Enter 'quit' to exit program.");
  45. Shape();
  46.  
  47. }
  48.  
  49. }
  50.  
  51. private static double Sphere(double radius){
  52.  
  53. double x = (4.0/3.0);
  54. double y = (radius * radius * radius);
  55. double volume = (x * Math.PI) * y;
  56. System.out.println("Math.pi = " + Math.PI + "4/3 or x = " + x + "r squared or y = " + y + "volume = " + volume);
  57. System.out.println("The volume of this sphere with radius " + radius + " is " + df2.format(volume));
  58. return volume;
  59.  
  60. }
  61.  
  62. private static double Cube(double width){
  63.  
  64. double volume = (width * width * width);
  65. System.out.println("The volume of this cube of width " + width + " is " + df2.format(volume));
  66. return volume;
  67.  
  68. }
  69.  
  70. private static void Tetrahedron(double edge){
  71.  
  72. double a = (edge * edge * edge);
  73. double x = 6 * (Math.sqrt(2));
  74. double volume = a / x;
  75. System.out.println("The volume of this tetrahedron with edge " + edge + " is " + df2.format(volume));
  76. return volume;
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement