In: Computer Science
Design a Geometry class with the following methods:
* A static method that accepts the radius of a circle and returns the area of the circle.
Use the following formula:
Area=?r^2
* A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula:
Area = Length x Width
* A static method that accepts the length of a triangle's base and the triangle's hight. The method should return the area of the triangle. Use the following formula:
Area = Base x Height x 0.5
The methods should display and error message if negative values are used for the circle's radius, the rectangle's length or width, or the triangel's base or height.
Next write a program to test the class, which displays the following menu and responds to the user's selection:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
Entre your choice (1 - 4):
Display an error message if the user enters a number outside the range of 1 through 4 when selection an item from the menu.
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
import java.util.Scanner;
public class Geometry
{
//function to calculate and return the area of a circle
static double circle_area(double radius){
double pi = 3.14, area = 0;
if(radius < 0)
System.out.println("Radius of a circle can't be a negative value!!!");
else
area = pi * radius * radius; //calculating the area
return area; //return the area calculated
}
//function to calculate and return the area of a rectangle
static double rectangle_area(double length, double width){
double area = 0;
if(length < 0 || width < 0)
System.out.println("Length/ Width of a rectangle can't be a negative value!!!");
else
area = length * width; //calculating the area
return area; //return the area calculated
}
//function to calculate and return the area of a triangle
static double triangle_area(double base, double height){
double area = 0;
if(base < 0 || height < 0)
System.out.println("Base/ Height of a triangle can't be a negative value!!!");
else
area = base * height * 0.5; //calculating the area
return area; //return the area calculated
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in); //creating scanner object
boolean repeat = true;
double area = 0;
while(repeat) { //loop untill repeat is true
//displaying the menu
System.out.println("\nGeometry Calculator");
System.out.println("1. Calculate the Area of a Circle");
System.out.println("2. Calculate the Area of a Rectangle");
System.out.println("3. Calculate the Area of a Triangle");
System.out.println("4. Exit");
System.out.print("Enter your choice (1 - 4): ");
int choice = s.nextInt(); //Reading user's choice
//if choice is 1, read radius of circle and calculate and display the area of circle
if(choice == 1){
System.out.print("Enter the radius of the circle: ");
double radius = s.nextDouble(); //Reading radius of circle
area = circle_area(radius); //calling circle_area()
System.out.println("Area of the circle: " + area); //displaying the area of circle
}
//if choice is 2, read length and width of rectangle and calculate and display the area of rectangle
else if(choice == 2){
System.out.print("Enter the length of the rectangle: ");
double length = s.nextDouble(); //Reading length of rectangle
System.out.print("Enter the width of the rectangle: ");
double width = s.nextDouble(); //Reading width of rectangle
area = rectangle_area(length, width); //calling rectangle_area()
System.out.println("Area of the rectangle: "+ area); //displaying the area of rectangle
}
//if choice is 3, read base and height of triangle and calculate and display the area of triangle
else if(choice == 3){
System.out.print("Enter the base of the triangle: "); //Reading base of triangle
double base = s.nextDouble();
System.out.print("Enter the height of the triangle: "); //Reading height of triangle
double height = s.nextDouble();
area = triangle_area(base, height); //calling triangle_area()
System.out.println("Area of the triangle: "+ area); //displaying the area of triangle
}
//if choice is 4, end the loop by setting repeat as false
else if(choice == 4)
repeat = false;
//Display an error message if the user enters a number outside the range of 1 through 4
else
System.out.println("Invalid entry. Please retry...");
}
}
}
OUTPUT