Question

In: Computer Science

JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...

JAVA program:

Calculate geometric area for 3 shapes(square, rectangle and circle).

  1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input
    1. Invalid input should throw a message for the user. Example: Invalid input, please try again
  2. Each options should ask users for relevant data. HINT: use scanner object to take in length for square, length/width for rectangle and radius for circle
    1. At the class level store a constant variable for PI that cannot be changed. You can use 3.14 for the value
  3. Each option should call relevant method that is created at the class level. Methods should take in input received from the users in the step above and return values. HINT: one method for square area calculation, one for rectangle area calculation and one for circle area calculation. Square and rectangle should return int, circle should return double

Expected output:
***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

S

Please enter length of the square:

2

Area of your square is: 4

***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

R

Please enter length of the rectangle:

2

Please enter length of the rectangle:

3

Area of your rectangle is: 6

***Area Calculator***

S  --  Square

R  --  Rectangle

C  --  Circle

C

Please enter the redius of the circle:

3

Area of your circle is: 28.26

Solutions

Expert Solution

CODE:

package demo;

import java.util.Scanner;

public class GeometricArea {
  
//   THIS method calculates the area of square
//   this takes side as a parameter
//   returns the area of square
   static int squareArea(int side) {
         
//       calculate the area
       int area = side * side;
      
//       return area
       return area;
   }
     
//   this method calculates the area of rectangle
//   this method takes two parameters length and width
//   return the area of the rectangle
   static int rectangleArea(int length,int width) {
       int area;
//       calculate the area of rectangle
       area = length*width;
//       return the area
       return area;
   }
     
//   this method calculates the area of circle
//   this takes the radius as parameter
//   returns the double value as area
   static double circleArea(int radius) {
//       declare a constant value for PI
       final double PI = 3.14;
         
       double area;
         
//       calculate the area of circle
       area = PI *radius*radius;
         
//       return the area
       return area;
   }
  
//   main function
   public static void main(String[] args) {
      
//       declare the variable for the user choice
       String choice;
      
//       variables for length radius side and width
       int length,width,side,radius;
      
//       create an object for scanner class that takes the input from user
       Scanner in = new Scanner(System.in);
      
//       display the user to choose
       System.out.println("***Area Calculator***");
       System.out.println("S -- Square");
       System.out.println("R -- Rectangle");
       System.out.println("C -- Circle");
      
//       read the user choice
       choice = in.nextLine();
      
//       switch case to switch according to the user
       switch (choice) {
      
//       if the user choice is S then
       case "S":
          
//           read the side of the square from the user
           System.out.println("Please enter length of the square:");
           side = in.nextInt();
          
//           call the method squareArea and pass the side as parameter
           int square_area = squareArea(side);
          
//           print the square_area
           System.out.println("Area of your square is:"+square_area);
          
           break;
          
//       IF THE USER SELECTS R THEN
       case "R":
          
//           ask the user to enter length and width
           System.out.println("Please enter length of the rectangle:");
           length = in.nextInt();
          
           System.out.println("Please enter width of the rectangle:");
           width = in.nextInt();
          
//           call the function rectangleArea and pass the parameters length and width
           int recta_area = rectangleArea(length,width);
          
//           print the area of rectangle
           System.out.println("Area of your rectangle is:"+recta_area);
          
           break;
          
//       if the user selects c then
       case "C":
          
//           ask user to enter the radius of cirlce
           System.out.println("Please enter the redius of the circle:");
          
           radius = in.nextInt();
          
//           call the function circleArea pass parameter radius
           double cricle_area = circleArea(radius);
          
//           print the result of circle area
           System.out.printf("Area of your circle is:%.2f",cricle_area);
          
           break;
//          
//           if the user enters the invalid input the print invalid
       default:
          
           System.out.println("Invalid input, please try again");
           break;
       }
      
      
   }

}


Related Solutions

REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
Write a java program that contains 3 overloaded static methods for calculating area of a circle,...
Write a java program that contains 3 overloaded static methods for calculating area of a circle, area of a cylinder and volume of a cylinder. Also create an output method which uses JOptionPaneto display instance field(s) and the result of the computing. Then code a driver class which will run and test calling each of these overloaded methods with hard-coded data and display the data and the result of the calculation by calling output method. Thanks!!
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle,...
/* calculating area of a circle, calculating area of a rectangle, calculating area of a triangle, and quit. */ import java.util.Scanner; public class GeoCalculator {    public static void main(String arg[]) { int geoCalc; //number selection of user Scanner get = new Scanner(System.in); //display section. System.out.println("Geometry Calculator"); System.out.println("Please select from the following menu:"); System.out.println("1. Calculate the Area of a Cirlce."); System.out.println("2. Calculate the Area of a Rectangle."); System.out.println("3. Calculate the Area of a Triangle."); System.out.println("4. QUIT"); System.out.println("Please make a selection:...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter. Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code. Run your program twice using different input. Copy the output and place it at the bottom...
The area of a particular rectangle is 12 times the area of a certain square, and...
The area of a particular rectangle is 12 times the area of a certain square, and the width of the rectangle is three times the length of a side of the square. The perimeter of the rectangle is 30 units greater than the perimeter of the square. Find the dimensiuons of both the rectangle and the square.
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT