1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vera_Pavlovna [14]
3 years ago
15

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: A r e a = π r 2 Use Math.PI for π and the radius of the circle for r. A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the following formula: A r e a = L e n g t h × W i d t h A static method that accepts the length of a triangle’s base and the triangle’s height. The method should return the area of the triangle. Use the following formula: A r e a = B a s e × H e i g h t × 0.5 The methods should display an error message if negative values are used for the circle’s radius, the rectangle’s length or width, or the triangle’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 4. Quit Enter your choice (1-4): Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu.
Computers and Technology
1 answer:
Svetllana [295]3 years ago
3 0

Answer:

Geometry class

public static class Geometry {

   public static double areaOfCircle(double radius) {

       return Math.PI * radius * radius;

   }

   public static double areaOfRectangle(double length, double width) {

       return length * width;

   }

   public static double areaOfTriangle(double base, double h) {

       return base * h * 0.5;

   }

}

Main and user menu choice method\

public static void main(String[] args) {

   int choice; // The user's menu choice

   do {

       // Get the user's menu choice.

       choice = getMenu();

       if (choice == 1) {

           calculateCircleArea();

       } else if (choice == 2) {

           calculateRectangleArea();

       } else if (choice == 3) {

           calculateTriangleArea();

       } else if (choice == 4) {

           System.out.println("Thanks for calculating!");

       }

   } while (choice != 4);

}

public static int getMenu() {

   int userChoice;

   // keyboard input

   Scanner keyboard = new Scanner(System.in);

   // Display the menu.

   System.out.println("Geometry Calculator\n");

   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. Quit\n");

   System.out.print("Enter your choice (1-4) : ");

   // get input from user

   userChoice = keyboard.nextInt();

   // validate input

   while (userChoice < 1 || userChoice > 4) {

       System.out.print("Please enter a valid range: 1, 2, 3, or 4: ");

       userChoice = keyboard.nextInt();

   }

   return userChoice;

}

Calculate Circle Area

public static void calculateCircleArea() {

   double radius;

   // Get input from user

   Scanner keyboard = new Scanner(System.in);

   System.out.print("What is the circle's radius? ");

   radius = keyboard.nextDouble();

   // Display output

   System.out.println("The circle's area is "

           + Geometry.areaOfCircle(radius));

}

Calculate Rectangle Area

public static void calculateRectangleArea() {

   double length;

   double width;

   // Get input from user

   Scanner keyboard = new Scanner(System.in);

   // Get length

   System.out.print("Enter length? ");

   length = keyboard.nextDouble();

   // Get width

   System.out.print("Enter width? ");

   width = keyboard.nextDouble();

   // Display output

   System.out.println("The rectangle's area is "

           + Geometry.areaOfRectangle(length, width));

}

Calculate Triangle Area

public static void calculateTriangleArea() {

   double base;

   double height;

   // Get input from user

   Scanner keyboard = new Scanner(System.in);

   // Get the base

   System.out.print("Enter length of the triangle's base? ");

   base = keyboard.nextDouble();

   // Get the height

   System.out.print("Enter triangle's height? ");

   height = keyboard.nextDouble();

   // Display the triangle's area.

   System.out.println("The triangle's area is "

           + Geometry.areaOfTriangle(base, height));

}

Output

Geometry Calculator

1. Calculate the Area of a Circle

2. Calculate the Area of a Rectangle

3. Calculate the Area of a Triangle

4. Quit

Enter your choice (1-4) : 1

What is the circle's radius? 10

The circle's area is 314.1592653589793

Geometry Calculator

1. Calculate the Area of a Circle

2. Calculate the Area of a Rectangle

3. Calculate the Area of a Triangle

4. Quit

Enter your choice (1-4) : 2

Enter length? 10

Enter width? 10

The rectangle's area is 100.0

Geometry Calculator

1. Calculate the Area of a Circle

2. Calculate the Area of a Rectangle

3. Calculate the Area of a Triangle

4. Quit

Enter your choice (1-4) : 3

Enter length of the triangle's base? 10

Enter triangle's height? 10

The triangle's area is 50.0

Geometry Calculator

1. Calculate the Area of a Circle

2. Calculate the Area of a Rectangle

3. Calculate the Area of a Triangle

4. Quit

Enter your choice (1-4) : 4

Thanks for calculating!

You might be interested in
A network supplies programming and services to a series of local tv stations, or _____, which contract to preempt time during sp
solong [7]
It is the Affiliates. The vast majority of which are autonomously claimed, legally consent to acquire time amid determined hours for programming gave by the systems and to convey the national promoting inside the program. 
It enables site to all the more effortlessly find and take an interest in subsidiary projects which are reasonable for their site and permits sites offering member projects to contact a bigger crowd by advancing their partner programs.
6 0
4 years ago
Suppose Alice and Bob are sending packets to each other over a computer network. SupposeTrudy positions herself in the network s
Elden [556K]

<u>Explanation:</u>

  • can observe the contents of all the packets sent and even modify the content.
  • can prevent packets sent by both parties from reaching each other.

From a network security standpoint, since we are told that Trudy <em>"</em><em>positions herself in the network so that she can capture all the packets sent", </em>it, therefore implies that the communication between Alice and Bob is vulnerable to modification and deletion.

6 0
3 years ago
Using the Vigenere cipher, does the length of the key matter?
Dafna1 [17]
Yes, the longer the Key the harder the encryption i believe
6 0
4 years ago
1. Trình bày các mô hình mối đe dọa trong hệ thống viễn thông
just olya [345]

Explanation:

please subscribe to my mom channel please

i request you

7 0
3 years ago
What is meant by SEO and how many types of SEO and explain it?
Andreas93 [3]

Answer:

The three kinds of SEO are: On-page SEO – Anything on your web pages – Blogs, product copy, web copy. Off-page SEO – Anything which happens away from your website that helps with your SEO Strategy- Backlinks. Technical SEO – Anything technical undertaken to improve Search Rankings – site indexing to help bot crawling.

Explanation:

<h3><u>PLEASE</u><u> </u><u>MARK</u><u> ME</u><u> BRAINLIEST</u><u> AND</u><u> FOLLOW</u><u> M</u><u> E</u><u> LOTS</u><u> OF</u><u> LOVE</u><u> FROM</u><u> MY</u><u> HEART</u><u> AND</u><u> SOUL</u><u> DARLING</u><u> TEJASWI</u><u> HERE</u><u> ❤️</u></h3>
3 0
3 years ago
Other questions:
  • The chain of command is an unbroken line of authority that extends from the top of the organization to the lowest echelon and cl
    7·1 answer
  • An attacker captures an encrypted message. he tries decrypting with every possible key until the decryption process reveals a se
    14·1 answer
  • The motion of any object can be broken down into it's component (parts of) vectors. true or false
    5·1 answer
  • Two of the following tasks would most likely be completed by a biomedical engineer?
    7·1 answer
  • Consider a planet where everyone belongs to a family of six, every family lives in its own house, each house has a unique addres
    8·1 answer
  • you are teaching a class about basic camera functions like power, basic date/time, menu navigation, and how to properly attach a
    9·1 answer
  • If a folder exists on an NTFS partition, which permission is needed by a user who needs to set security permissions on the folde
    11·1 answer
  • 37) Which of the following statements is true
    14·1 answer
  • Although plants and ainamals are both living things___
    13·1 answer
  • You have a filtered dataset for Customer Sales with some null value rows. You want to remove these rows completely. How will you
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!