Advertisement
jinglis

Shapes in Java

Jul 28th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. /* Name: James Inglis
  2.  * Syntax: Java
  3.  * Date: 7/28/14
  4.  * Description: This program reads in the shape and computes the different processes within each shape.
  5.  
  6.  
  7.  
  8.  
  9. import java.util.*;
  10. import java.lang.*;
  11.  
  12. public class Ch4_PrExercise4
  13. {
  14.       static Scanner console = new Scanner(System.in);
  15.       static final double PI = 3.14159;
  16.    public static void main(String[] args)
  17.    {
  18.       String shape;
  19.       double length, width, height, radius;
  20.      
  21.     // Getting the shape.
  22.       System.out.print("Enter the shape type: (rectangle, circle, cylinder) ");
  23.       shape = console.next();
  24.       System.out.println();
  25.      
  26.       // If shape is rectangle...
  27.       if(shape.compareTo("rectangle") == 0)
  28.       {
  29.          System.out.print("Enter the length of the rectangle: ");
  30.          length = console.nextDouble();
  31.          System.out.println();
  32.          
  33.          
  34.          System.out.print("Enter the width of the rectangle: ");
  35.          width = console.nextDouble();
  36.          System.out.println();
  37.          
  38.          System.out.printf("Area of the rectangle = %.2f%n", (length * width));
  39.          System.out.printf("Perimeter of the rectangle = %.2f%n",(2 * (length + width)));
  40.       }
  41.      
  42.     // If shape is circle...
  43.       else if(shape.compareTo("circle") == 0)
  44.       {
  45.          System.out.print("Enter the radius of the circle: ");
  46.          radius = console.nextDouble();
  47.          System.out.println();
  48.          
  49.          
  50.          System.out.printf("Area of the circle = %.2f%n",(PI * Math.pow(radius, 2.0)));
  51.          System.out.printf("Circumference of the circle: %.2f%n",
  52.                           (2 * PI * radius));
  53.          
  54.       }
  55.      
  56.     // If shape is cylinder...
  57.       else if(shape.compareTo("cylinder") == 0)
  58.       {
  59.          System.out.print("Enter the radius of the base of " + "the cylinder: ");
  60.          radius = console.nextDouble();
  61.          System.out.println();
  62.          
  63.          
  64.          System.out.print("Enter in the height of the of cylinder: ");
  65.          height = console.nextDouble();
  66.          System.out.println();
  67.          
  68.          System.out.printf("Volume of the cylinder = %.2f%n",
  69.                       (PI * Math.pow(radius, 2.0) * height));
  70.                      
  71.          System.out.printf("Surface area of the cylinder: %.2f%n",
  72.          (2 * PI * radius * height
  73.          + 2 * PI * Math.pow(radius, 2.0)));
  74.                          
  75.       }
  76.      
  77.       else
  78.          System.out.println("The program does not handle "
  79.                              + shape);
  80.                              
  81.       }
  82.      
  83.      
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement