In: Computer Science
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. HINT: use scanner object to take in length for square, length/width for rectangle and radius for circle
a. 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
Submissions:
PART 1 ==>> A flow chart showing the actions and logical
decisions for the JAVA program. You can use any tool to draw the
flow chart. Lucid chart is a good one and it has a free option. -
https://app.lucidchart.com/users/register/personal?try=1
PART 2 ==>> The Java program itself
Rubrics:
1)flowchart:
2)Javaprogram:
// menudriven program for finding areas
import java.util.*;
import java.util.Scanner;
public class Main
{
// Declaring global varaible for PI value
public static double PI=3.14;
public static void main(String[] args)
{
char choice;
//Scanner class
Scanner scann=new Scanner(System.in);
// printing menu
System.out.println("***Area Calculator***");
System.out.println("S --
Square");
System.out.println("R --
Rectangle");
System.out.println("C --
Circle");
// reading user choice
choice=scann.next().charAt(0);
while(true)
{
switch(choice)
{
// in case of square
case 'S':
{
int length,Area;
System.out.print("Please enter
length of the square : ");
length=scann.nextInt();
// function calling for area of
square
Area=AreaSquare(length);
System.out.println("Area of your
square is : "+Area);
break;
}
// in case of Rectangle
case 'R':
{
int length,breadth,Area;
System.out.print("Please enter
length of the Rectangle : ");
length=scann.nextInt();
System.out.print("Please enter length of the Rectangle : ");
breadth=scann.nextInt();
// function calling for area of
Rectangle
Area=AreaRectangle(length,breadth);
System.out.println("Area of your
Rectangle is : "+Area);
break;
}
//in case of circle
case 'C':
{
int radius;
double Area;
System.out.print("Please enter
radius of the circle : ");
radius=scann.nextInt();
// function calling for area of
Circle
Area=AreaCircle(radius);
System.out.println("Area of your
circle is : "+Area);
break;
}
// default case
default:
{
System.out.println("Invalid input,
please try again");
// exiting
System.exit(0);
}
}
// printing menu
System.out.println("***Area Calculator***");
System.out.println("S --
Square");
System.out.println("R --
Rectangle");
System.out.println("C --
Circle");
// reading user choice
choice=scann.next().charAt(0);
}
}
// Method for area of square
public static int AreaSquare(int length)
{
return length*length;
}
// method for area of Rectangle
public static int AreaRectangle(int length,int
breadth)
{
return length*breadth;
}
// metod for area of ClassCircularityError
public static double AreaCircle(int radius)
{
return Main.PI*radius*radius;
}
}
output: