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...
/* 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:...
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...
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.
using C# Write a program named RectangleArea tocalculate the area of a rectangle with a...
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
a function named area to calculate the area of a rectangle area = Length x width...
a function named area to calculate the area of a rectangle area = Length x width 2.a function named volume to calculate the volume of a sphere volume = 4/3 x 3.14 x radius x radius x radius 3.a function named volume to calculate the volume of a cuboid volume = Length x width x ht 4. Use a loop structure to calculate the sum of all odd numbers from 1 to 17 5) Use a loop structure to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT