101k views
0 votes
Create a program named CalArea that calculates and displays the area of a circle, rectangle, or triangle. The program Main() should present a prompt for choosing the area to calculate, offering three choices: circle, rectangle, and triangle. Upon selecting an option, Main() should prompt for input parameters accordingly. For a circle, only the radius is needed; for a rectangle, both length and width are required; for a triangle, three sides are necessary. Once parameters are entered, Main() should call the corresponding overloaded methods named DisplayArea() for the chosen shape. These three overloaded methods should share the same names but have different numbers of parameters. The formula for the area of a triangle with three sides (a, b, and c) is given by A = sqrt(s * (s - a) * (s - b) * (s - c)), where s = (a + b + c) / 2. A sample output is illustrated below.

Please choose the area to calculate:
1 for circle
2 for rectangle
3 for triangle

3

Please enter side one ≫ 3
Please enter side two ≫ 4
Please enter side three ≫ 5

Please choose the area to calculate:
1 for circle
2 for rectangle
3 for triangle

3

Please enter side one ≫ 3
Please enter side two ≫ 4
Please enter side three ≫ 5

The area of the triangle is 6.00


The area of the triangle is 6.00

User Atomiks
by
8.4k points

1 Answer

1 vote

Final answer:

The CalArea program calculates the area of a circle, rectangle, or triangle by using overloads of a "DisplayArea" method for each shape.

Step-by-step explanation:

Users input dimensions after selecting a shape, and the area is calculated and displayed with attention to significant figures for precision.

To create a program named CalArea that calculates and displays the area of a circle, rectangle, or triangle, we will write a set of overloads for the "DisplayArea" method. The different overloads will correspond to the different shapes, with the circle's area determined by A = πr², the rectangle's area by A = length × width, and the triangle’s area by Heron’s formula A = sqrt(s × (s - a) × (s - b) × (s - c)), where s = (a + b + c) / 2.

Here's a simplified example:

  • Circle area called with one parameter (radius)
  • Rectangle area called with two parameters (length, width)
  • Triangle area called with three parameters (side1, side2, side3)

User input is required for the parameters after they select the shape they want to calculate the area for. The main method displays the area using the appropriate overloaded version of "DisplayArea" after the calculation is completed. To maintain accuracy, the calculation uses the appropriate number of significant figures, especially when dealing with measurements provided to a certain degree of precision.

User Alvin Quezon
by
7.2k points