115k views
0 votes
The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π(pi) times the square of the radius.

The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized.

/* missing code */

System.out.print(volume);

Which of the following can be used to replace /* missing code */ so that the code segment works as intended?

I. double baseArea = pi * r * r;double volume = baseArea * h;
II. double volume = pi * r * r;volume = volume * h;
III. double volume = pi * r * r * h;

a. I only
b. III only
c. I and III only
d. II and III only
e. I, II, and III

User Tancho
by
7.3k points

1 Answer

2 votes

Answer:

The answer is "Option e".

Step-by-step explanation:

In this question we calculating the volume of a cylinder, that's formula is:


\boxed{\text{Volume}= \pi * r^2 * h}

In the above, we use π that's value is 3.14 or
(22)/(7) and "r and h" is input by the user. In the given choices all were correct it calculates the volume, that can be defined as follows:

  • In choice 1, The double variable "baseArea" is declared, which first calculates
    \pi \cdot r^2 value then multiply the h value and store it into the "volume" variable.
  • In choice 2, The double variable "volume" is declared, which first calculates
    \pi \cdot r^2 value then multiply the h value and store it into the "volume" variable.
  • In choice 2, The double variable "volume" is declared, which directly calculates the volume value by the above-given formula.
User Danijoo
by
8.1k points