Question

Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the below code which does the exact work described.

I have also added the required comments on the code itself. Please let me know in the comment section if anything else is required.

import java.util.Scanner;
public class HelloWorld{

    public static void main(String []args){
  
    // this is used to take the input from the user
       Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the shape to calculate area (1-5):\n" +
        "1.Rectangle\n" +
        "2.Square\n" +
        "3.Triangle\n" +
        "4.Circle\n" +
        "5.Parellelogram");

        // validating the required input
        // this prompts the user to enter the input untill the valid input has been given
        while(!scanner.hasNextLine()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        String shape = scanner.nextLine(); // Read user input


        /* below we will have a switch case to switch between the implemented methods to call
        * We have to also pass the scanner object which will be shared by each implemented methods
        */
        switch(shape) {
            case "1":
                rectangle(scanner);
                break;
            case "2":
                square(scanner);
                break;
            case "3":
                triangle(scanner);
                break;
            case "4":
                circle(scanner);
                break;
            case "5":
                parelleogram(scanner);
                break;
            default:
                System.out.println("Please enter the values from 1 to 5 only");
                break;
        }
    }
   
    static void rectangle(Scanner scanner) {
      System.out.println("Enter the length of Rectangle:");

      while(!scanner.hasNextDouble()) {
          System.out.println("Please enter the required input");
          scanner.next();
      }
        double length = scanner.nextDouble();
        System.out.println("Enter the width of Rectangle:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
      }
        double width = scanner.nextDouble();
      
        //Area = length * length
        double area = length*width;
      System.out.println("Area of the rectangle is :" + area);
    }

    static void square(Scanner scanner) {
        System.out.println("Enter Side of Square:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double side = scanner.nextDouble();
      
        //Area = side*side
        double area = side*side;
        System.out.println("Area of Square is: " + area);
    }
    static void triangle(Scanner scanner) {
        System.out.println("Enter the width of the Triangle:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double base = scanner.nextDouble();

        System.out.println("Enter the height of the Triangle:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double height = scanner.nextDouble();

        //Area = (width*height)/2
        double area = (base* height)/2;
        System.out.println("Area of Triangle is: " + area);
    }

    static void circle(Scanner scanner) {
        System.out.print("Enter the radius: ");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double radius = scanner.nextDouble();
      
        //Area = PI*radius*radius
        double area = Math.PI * (radius * radius);
        System.out.println("The area of circle is: " + area);
    }

    static void parelleogram(Scanner scanner) {
        System.out.println("Enter the height:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double d1= scanner.nextDouble();
        System.out.println("Enter the breadth:");
        while(!scanner.hasNextDouble()) {
            System.out.println("Please enter the required input");
            scanner.next();
        }
        double d2= scanner.nextDouble();
       
        //Area = (height*breadth)
        double area=(d1*d2) ;
        System.out.println("Area of Parallelogram is: " + area);
    }
}


Flow chart for the above code:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that prompts user to calculate area of five different shapes using java...

    Write a Java program that prompts user to calculate area of five different shapes using java library, and at least 2 classes - more of OOP, also use divide and conquer methods.

  • Write a program in Java that can calculate and output the area of three different shapes;...

    Write a program in Java that can calculate and output the area of three different shapes; a rectangle, a triangle and a circle. Let the user choose which shape they want by typing the words “rectangle”, “triangle” or “circle” (you may use integers, strings or chars to indicate which shape the user prefers to calculate the area of). Then, have the user enter the values of the dimension(s) appropriate for each shape in the form of doubles.

  • JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

    JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...

  • Write a Java program that prompts the user to enter a number representing a geometric shape....

    Write a Java program that prompts the user to enter a number representing a geometric shape. Options are 1 for square, 2 for rectangle, and 3 for right triangle. According to the user input, the program will collect either 1, 2 or 3 values from the user representing the lengths of the shape sides. The user is responsible for ensuring the 3 sides of the triangle represent a right triangle. The program will print the perimeter of the shape. Use...

  • In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

    In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • solve it with Java please Write a Java program that prompts the user to input length...

    solve it with Java please Write a Java program that prompts the user to input length and width of a shape. If length is equal to width, call a method to calculate the area of the shape and send only one side as parameter to this method. This meth return the area of the shape. If length is not equal to width, call a method to calculate the area of the shape and send both width and length as parameters...

  • #1 Write a java program which calculates the area of either a Triangle or a Rectangle....

    #1 Write a java program which calculates the area of either a Triangle or a Rectangle. Use Scanner to take an input field to tell you if the area to be calculated is for a Triangle or is for a Rectangle. Depending on that input - the program must call either the Triangle method or the Rectangle method. Each of those methods must receive 2 input fields from the user - one for base and one for height in order...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT