In: Computer Science
JAVA program:
Calculate geometric area for 3 shapes(square, rectangle and circle).
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
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;
}
}
}