Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: James Inglis
- * Syntax: Java
- * Date: 7/28/14
- * Description: This program reads in the shape and computes the different processes within each shape.
- import java.util.*;
- import java.lang.*;
- public class Ch4_PrExercise4
- {
- static Scanner console = new Scanner(System.in);
- static final double PI = 3.14159;
- public static void main(String[] args)
- {
- String shape;
- double length, width, height, radius;
- // Getting the shape.
- System.out.print("Enter the shape type: (rectangle, circle, cylinder) ");
- shape = console.next();
- System.out.println();
- // If shape is rectangle...
- if(shape.compareTo("rectangle") == 0)
- {
- System.out.print("Enter the length of the rectangle: ");
- length = console.nextDouble();
- System.out.println();
- System.out.print("Enter the width of the rectangle: ");
- width = console.nextDouble();
- System.out.println();
- System.out.printf("Area of the rectangle = %.2f%n", (length * width));
- System.out.printf("Perimeter of the rectangle = %.2f%n",(2 * (length + width)));
- }
- // If shape is circle...
- else if(shape.compareTo("circle") == 0)
- {
- System.out.print("Enter the radius of the circle: ");
- radius = console.nextDouble();
- System.out.println();
- System.out.printf("Area of the circle = %.2f%n",(PI * Math.pow(radius, 2.0)));
- System.out.printf("Circumference of the circle: %.2f%n",
- (2 * PI * radius));
- }
- // If shape is cylinder...
- else if(shape.compareTo("cylinder") == 0)
- {
- System.out.print("Enter the radius of the base of " + "the cylinder: ");
- radius = console.nextDouble();
- System.out.println();
- System.out.print("Enter in the height of the of cylinder: ");
- height = console.nextDouble();
- System.out.println();
- System.out.printf("Volume of the cylinder = %.2f%n",
- (PI * Math.pow(radius, 2.0) * height));
- System.out.printf("Surface area of the cylinder: %.2f%n",
- (2 * PI * radius * height
- + 2 * PI * Math.pow(radius, 2.0)));
- }
- else
- System.out.println("The program does not handle "
- + shape);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement