In: Computer Science
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 the Square = Side ^2
Area of the Rectangle = Length * Width
Area of the Triangle = ½ Base * Hight
Area of the Circle = PI * R^2
You MUST apply the following to your program:
Hand in the following:
a. detailed and precise design in pseudocode
input a option
case based on option
case =1 //area of rectangle
Step 1:Input W,L
Step 2: if W <=0 or L<=0
print "W, L should be greater than 0";
continue;
Step 3: A<- L x W
Step 4: print A
case =2 //area of circle
Step 1: Input radius
Step 2: if radius<=0
print "radius should be greater than 0";
continue;
Step 3: A <- 3.14 * radius * radius
Step 4: Print A
case =3: //area of square
Step 1: Input side
Step 2: if side <=0
print "side should be greater than 0";
continue;
Step 3: A <- side * side
Step 4: print A
case =4 //area of triangle
Step 1: input base, height
Step 2: if base<=0 or height<=0
print "base and height should be greater than 0";
continue;
Step 3: A <- 1/2 * base * height
Step 4: print A
case =5 //exit the code
System.exit()
End case
____________________________________________SOURCE CODE__________________________________
create a class with name AreaCalculator.java
import java.util.Scanner;
public class AreaCalculator {
final static double pi = 3.14;
static Scanner s = new Scanner(System.in);
public void circle_area() {
System.out.print("Enter radius of circle:");
int radius = s.nextInt();
while (radius <= 0) {
System.out.println("Radius value should be greater than 0");
radius = s.nextInt();
}
double area = pi * radius * radius;
System.out.println("Area of circle: " + area);
}
public void rectangle_area() {
System.out.println("Enter the length of Rectangle:");
double length = s.nextDouble();
while (length <= 0) {
System.out.println("lenght value should be greater than 0");
length = s.nextDouble();
}
System.out.println("Enter the width of Rectangle:");
double width = s.nextDouble();
while (width <= 0) {
System.out.println("width value should be greater than 0");
width = s.nextDouble();
}
//Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
public void square_area() {
System.out.println("Enter Side of Square:");
//Storing the captured value in a variable
double side = s.nextDouble();
while (side <= 0) {
System.out.println("Side value should be greater than 0");
side = s.nextDouble();
}
//Area of Square = side*side
double area = side * side;
System.out.println("Area of Square is: " + area);
}
public void triangle_area() {
System.out.print("Enter the width of the Triangle:");
double base = s.nextDouble();
while (base <= 0) {
System.out.println("width value should be greater than 0");
base = s.nextDouble();
}
System.out.print("Enter the height of the Triangle:");
double height = s.nextDouble();
while (height <= 0) {
System.out.println("height value should be greater than 0");
height = s.nextDouble();
}
double area = (base * height) / 2;
System.out.println("Area of Triangle is: " + area);
}
public static void main(String[] args) {
//menu
AreaCalculator areaCalculator = new AreaCalculator();
Scanner sc = new Scanner(System.in);
int input = 1;
while (input > 0) {
System.out.println("***************************MENU*************************");
System.out.println(" 1. Circle\n 2. Rectangle\n 3. Square\n 4.
Triangle\n 5. Quit");
System.out.println("********************************************************");
System.out.println("Enter your option:");
input = s.nextInt();
switch (input) {
case 1:
areaCalculator.circle_area();
break;
case 2:
areaCalculator.rectangle_area();
break;
case 3:
areaCalculator.square_area();
break;
case 4:
areaCalculator.triangle_area();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Enter correct option:");
break;
}
}
}
}
__________________________________________OUTPUT__________________________________________
***************************MENU*************************
1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit
********************************************************
Enter your option:
1
Enter radius of circle:12
Area of circle: 452.15999999999997
***************************MENU*************************
1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit
********************************************************
Enter your option:
2
Enter the length of Rectangle:
-1
lenght value should be greater than 0
4
Enter the width of Rectangle:
6
Area of Rectangle is:24.0
***************************MENU*************************
1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit
********************************************************
Enter your option:
3
Enter Side of Square:
4
Area of Square is: 16.0
***************************MENU*************************
1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit
********************************************************
Enter your option:
4
Enter the width of the Triangle:8
Enter the height of the Triangle:4
Area of Triangle is: 16.0
***************************MENU*************************
1. Circle
2. Rectangle
3. Square
4. Triangle
5. Quit
********************************************************
Enter your option:
5
____________________________________________________________________________________________