Advertisement
MattPlummer

Exercise 8.5

Nov 25th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Exercises
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Scanner scan = new Scanner(System.in);
  7.                
  8.                 System.out.println("input object's radius:");
  9.                 double ra = scan.nextDouble();
  10.  
  11.                 System.out.println("input object's height:");
  12.                 double he = scan.nextDouble();
  13.                
  14.                 Geometry geometry = new Geometry();
  15.                
  16.                 System.out.println("volume of cube:");
  17.  
  18.                 System.out.println(geometry.cubeVolume());
  19.                
  20.                 System.out.println("surface area of cube:");
  21.  
  22.                 System.out.println(geometry.cubeSurface());
  23.                
  24.                 System.out.println("volume of sphere:");
  25.  
  26.                 System.out.println(geometry.sphereVolume());
  27.                
  28.                 System.out.println("surface area of sphere:");
  29.  
  30.                 System.out.println(geometry.sphereSurface());
  31.  
  32.                 System.out.println("volume of cone:");
  33.  
  34.                 System.out.println(geometry.coneVolume());
  35.  
  36.                 System.out.println("surface area of cone:");
  37.  
  38.                 System.out.println(geometry.coneSurface());
  39.                
  40.                 System.out.println("volume of cylinder:");
  41.  
  42.                 System.out.println(geometry.cylinderVolume());
  43.                
  44.                 System.out.println("surface area of cylinder:");
  45.  
  46.                 System.out.println(geometry.cylinderSurface());
  47.  
  48.                
  49.                
  50.                
  51.         }
  52. }
  53.  
  54. package chapter8;
  55.  
  56. public class Geometry
  57. {
  58.         private double height;
  59.         private double radius;
  60.        
  61.         public Geometry(double h, double r)
  62.         {
  63.                 height = h;
  64.                 radius = r;
  65.         }
  66.         public static double cubeVolume(double h)
  67.         {
  68.                 return h * h * h;
  69.         }
  70.         public static double cubeSurface(double h)
  71.         {
  72.                 return (h * h) * 6;
  73.         }
  74.         public static double sphereVolume(double r)
  75.         {
  76.                 return ((r * r * r) * 3.14)/3;
  77.         }
  78.         public static double sphereSurface(double r)
  79.         {
  80.                 return 4 * ((r * r) * 3.14);
  81.         }
  82.         public static double coneVolume(double h, double r)
  83.         {
  84.                 return 3.14 * (r * r) * (h / 3);
  85.         }
  86.         public static double coneSurface(double h, double r)
  87.         {
  88.                 return 3.14 * r * (r + r + h);
  89.         }
  90.         public static double cylinderVolume(double h, double r)
  91.         {
  92.                 return 3.14 * (r * r) * h;
  93.         }
  94.         public static double cylinderSurface(double h, double r)
  95.         {
  96.                 return 2 * (3.14 * (r * h)) + 2 * ((r * r)* 3.14);
  97.         }
  98.        
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement