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
  • 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...

  • 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...

  • NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of...

    NO LOOPS, write a program, in Java, that prompts the user for a hexadecimal string of EXACTLY 4 hex digits, no more, no less, converts the hexadecimal value to its decimal value using string, character parsing, and Math methods learned in this chapter, and then prints the original string and its converted decimal value. NOTE: Hex digits may be in UPPER or lower case.

  • 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...

  • Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...

    Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes. Shape.java will be the parent class and will contain a Point type. Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class Each class (Circle, Triangle, Rectangle) will contain the private class...

  • Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu:...

    Use Python 3 Create a program that uses Turtle to draw shapes. Show the following menu: Enter Circle Enter Rectangle Remove Shape Draw Shapes Exit Circles – User inputs position, radius, and color. The position is the CENTER of the circle Rectangles – User inputs position, height, width, color. The position is the lower left-hand corner Colors – Allow red, yellow, blue, and green only Remove – Show the number of items in the list and let the user enter...

  • 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...

  • Write a program in Java that prompts a user for Name and id number. and then...

    Write a program in Java that prompts a user for Name and id number. and then the program outputs students GPA MAIN import java.util.StringTokenizer; import javax.swing.JOptionPane; public class Main {    public static void main(String[] args) {                       String thedata = JOptionPane.showInputDialog(null, "Please type in Student Name. ", "Student OOP Program", JOptionPane.INFORMATION_MESSAGE);        String name = thedata;        Student pupil = new Student(name);                   //add code here       ...

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
ADVERTISEMENT