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

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 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...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT