Question

In: Computer Science

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 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:

  1. A switch statement to present the selection menu
  2. If statement to validate the input  for (entries must be greater than 0)

Hand in the following:

  1. A detailed and precise design in pseudocode. (design must match 1 to 1 with the source code)
  2. Source code, a java file
  3. Screenshots of all output scenarios.

Solutions

Expert Solution

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

____________________________________________________________________________________________


Related Solutions

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). 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 data. HINT: use scanner object to take in length for square,...
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 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...
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...
/* 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:...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Find the maximum area of a rectangle inscribed in a triangle of area A.(NOTE: the triangle...
Find the maximum area of a rectangle inscribed in a triangle of area A.(NOTE: the triangle need not necessarily be a right angled triangle).
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...
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT