Answer to Question #288162 in Java | JSP | JSF for Riven

Question #288162

The statements in the following program are in incorrect order. Rearrange the statements so that it prompts the user to input the shape type (rectangle, circle, or cylinder), and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter; for a circle, it outputs the area and circumference; and for a cylinder, it outputs the volume and surface area. After rearranging the statements, your program should be properly indented. 


1
Expert's answer
2022-01-17T12:58:37-0500
import java.util.Scanner;

class Main {

    private static Scanner scanner;

    public static void main(String[] args) {
        scanner = new Scanner(System.in);
        System.out.println("Choose shape(rectangle, circle, cylinder):");
        Shape shape = chooseShape(scanner.nextLine());
        System.out.println(shape.getInfo());
    }

    private static Shape chooseShape(String input) {
        switch (input.toLowerCase()) {
            case "rectangle":
                System.out.println("Enter width:");
                double width = scanner.nextDouble();
                System.out.println("Enter length:");
                double length = scanner.nextDouble();
                return new Rectangle(width, length);
            case "circle":
                System.out.println("Enter radius:");
                return new Circle(scanner.nextDouble());
            case "cylinder":
                System.out.println("Enter radius:");
                double radius = scanner.nextDouble();
                System.out.println("Enter height:");
                double height = scanner.nextDouble();
                return new Cylinder(radius, height);
            default:
                return null;
        }
    }
}

abstract class Shape {

    public abstract String getInfo();
}

class Rectangle extends Shape {

    private double width;
    private double length;

    public Rectangle(double width, double length) {
        this.width = width;
        this.length = length;
    }

    public double getPerimeter() {
        return (width + length) * 2;
    }

    public double getArea() {
        return width * length;
    }

    @Override
    public String getInfo() {
        return "Rectangle\nArea: %.2f\nPerimeter: %.2f\n".formatted(getArea(), getPerimeter());
    }
}

class Circle extends Shape {

    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getCircumference() {
        return 2 * Math.PI * radius;
    }

    @Override
    public String getInfo() {
        return "Circle\nArea: %.2f\nCircumference: %.2f\n".formatted(getArea(), getCircumference());
    }
}

class Cylinder extends Shape {

    private double radius;
    private double height;

    public Cylinder(double radius, double height) {
        this.radius = radius;
        this.height = height;
    }

    public double getVolume() {
        return Math.PI * radius * radius * height;
    }

    public double getSurfaceArea() {
        return (2 * Math.PI * radius * height) +
                (2 * Math.PI * radius * radius);
    }

    @Override
    public String getInfo() {
        return "Cylinder\nVolume: %.2f\nSurface area: %.2f\n".formatted(getVolume(), getSurfaceArea());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog