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]
2 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]2 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
Write a program, NumberStatistics.java, that reads an unspecified number of integers. The program ends with the input 0. The pro
Lerok [7]

Answer:

Following are the program, which can be below:

import java.util.*; //defining header file

public class NumberStatistics  //defining class NumberStatistics

{

public static void main(String[] arc) //defining main method

{

int total_number=0,max_val=0,min_val=0,neg_val=0,pos_val=0,neg_count=0,pos_count=0,x; //defining variables

Scanner ox= new Scanner(System.in); //creating Scanner class Object

System.out.println("Enter positive and negative value with sign and when complete inter 0:"); //print message  

x= ox.nextInt(); //input value from user

while(x!=0) //defining loop to check input value is 0

{

total_number++; // increment value

if(total_number == 1)  //defining conditional statement to check minimum and maximum value

{

max_val = x; //assign value in max  

min_val = x;//assign value in min

}

else  //defining else block

{

if(max_val < x) //check maximum value

{

max_val = x; //assign maximum value

}

if(min_val > x) //check minimum value

{

min_val = x; //assign minimum value

}

}

if(x > 0) //input values  

{

pos_count++; //count positive value

pos_val+=x; // add positive values

}

else  

{

neg_count++;//count negative value

neg_val+=x; // add negative values

}

x = ox.nextInt(); //input values

}

// defining conditionl statement

if(total_number != 0)  // defining if block to print all value

{

System.out.println("positive value average= "+(pos_val/(double)pos_count));

System.out.println("negative values Average= "+(neg_val/(double)neg_count));

System.out.println("all values average= "+(pos_val+neg_val)/(double)total_number);

System.out.println("maximum value = "+max_val+"\n"+" minimum value = "+min_val);

}

else  

{

System.out.println("no positive values found");

System.out.println("no negative values found");

}

}

}

Output:

Enter positive and negative value with sign and when complete inter 0:

12

8

04

22

-22

-5

9

0

positive value average= 11.0

negative values Average= -13.5

all values average= 4.0

maximum value = 22

minimum value = -22

Explanation:

The Description of the above java program can be described as follows:

  • In the above code first we import the package for user input, then defines the variable "total_number,max_val,min_val,neg_val,pos_val,neg_count, pos_count,x" to store the value prints its value.
  • In the next step, a scanner class object is created for user input and defines a while loop, that checks input value isn't equal to 0, inside the loop a conditional statement defines that cont and calculates all positive, negative, and all values addition.
  • At the another conditional statement is defines, that prints all value average, and also prints maximum, minimum value.
6 0
3 years ago
What is the next line?
lianna [129]

Answer:

4

Explanation:got it right on edg.

4 0
2 years ago
A videogame designer would be most interested in the _____ elements of beowulf.
adelina 88 [10]
I think he would be interested in a. epic elements of beowulf.
5 0
3 years ago
Read 2 more answers
Did the Z3 computer invented by Konrad Zuse have a negative effect on society?
musickatia [10]

Answer:

Explanation:

Không

4 0
2 years ago
Read 2 more answers
Whoever helps me with these questions i will help them with the same number of questions in any subject: 1. As the operations ma
mezya [45]
I'm going have to go with with 1.b and 2.d hope this helps and I don't need help right now.
6 0
3 years ago
Other questions:
  • All of the following are qualities of an aprenticeship except
    12·1 answer
  • true or false manual handling of materials accounts for the primary source of energy in the workplace
    13·2 answers
  • If you have a list of words that you wish me to make into a bulleted list, you must first highlig or ______ The words with the c
    13·1 answer
  • Side mirror using convex mirror or concave mirror?​
    5·2 answers
  • Assume that the array arr has been defined and initialized with the values {4, 2, 5, 3, 1}. What are the values in the array arr
    15·1 answer
  • Two threads try to acquire the same resource at the same time and both are blocked. Then, they continually change their states i
    14·1 answer
  • The _____ component of a decision support system (DSS) includes mathematical and statistical models that, along with the databas
    14·1 answer
  • Which statement is most likely to be true about a computer network?
    12·2 answers
  • Create a basic program that accomplishes the following requirements: Allows the user to input 2 number , a starting number x and
    12·1 answer
  • ________ helps in determining the cause of a security threat in an incident response plan. Question 7 options: A) Restricting sy
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!