Question

In: Computer Science

Design a Geometry class with the following methods: * A static method that accepts the radius...

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.

Solutions

Expert Solution

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


Related Solutions

Design an application with a single class and two static methods: method main and method isLeap....
Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 except if it is divisible by 400. Examples 2007 is not a leap year 2008 is leap year 1700...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an...
Language: JavaScript Create a public class CountLetters providing a single static method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Integers. (You can reject null arguments using assert.) The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts. As a reminder, you can...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter...
In java Write a static method named consecutiveDigits that accepts an integer n as a parameter and that returns the highest number of consecutive digits in a row from n that have the same value. For example, the number 3777785 has four consecutive occurrences of the number 7 in a row, so the call consecutiveDigits(3777785) should return 4. For many numbers the answer will be 1 because they don't have any adjacent digits that match. Below are sample calls on...
Write a complete static method named addUp that accepts, in order, an integer number and an...
Write a complete static method named addUp that accepts, in order, an integer number and an integer thisMany as parameters and that prints a complete line of output reporting all the numbers that result from adding number to itself thisMany number of times (starting at 1 and ending at thisMany). For example, the following calls: addUp(3, 5); addUp(7, 3); should produce this output: Adding up 3 for 5 times gives: 3, 6, 9, 12, 15 Adding up 7 for 3...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a class that has three overloaded static methods for calculating the areas of the following...
Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: - circles - rectangles - cylinders Here are the formulas for calculating the area of the shapes. Area of a circle: Area = π r2, where p is Math.PI and r is the circle's radius Area of a rectangle: Area = Width x Length Area of a cylinder: Area = π r2 h, where p is Math.PI, r is the radius of...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
Coding Java Assignment Write the following static methods. Assume they are all in the same class....
Coding Java Assignment Write the following static methods. Assume they are all in the same class. Assume the reference variable input for the Scanner class and any class-level variables mentioned are already declared. All other variables will have to be declared as local unless they are parameter variables. Use printf. A method that prompts for the customer’s name and returns it from the keyboard. A method called shippingInvoice() that prompts for an invoice number and stores it in a class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT