Draw contour diagrams to show the state of execution prior to Line 11 of the
class Multiple shown in Fig. 2.27 in Sect. 2.9.
Write a complete program to calculate the volumes of a cone and a hollow
cylinder. The shape of a hollow cylinder is shown below, where r is the radius of
the inner cylinder and R is the radius of the outer cylinder:
First, draw a UML diagram similar to Fig. 2.31 for a class named Cone as
described below and then write the code to implement the Cone class.
*A. The Cone class has two private data members, radius and height, of
type double.
B. Write code for a constructor to set the data members to default values of 0.0.
C. Write code for the accessor methods, getRadius and getHeight, that
return the value of the appropriate data member.
*D. Write code for the mutator methods, setRadius and setHeight, that each
have one formal parameter which is stored as the value of the data member.
E. Write a method named computeVolume to compute the volume of a
cone and return the computed volume to the client. The formula to find the
volume of a cone is 1
3 πr2h.
Second, draw a UML diagram similar to Fig. 2.31 for a class named
HollowCylinder as described below and then write the code to implement
the HollowCylinder class.
F. The HollowCylinder class has three private data members,
innerRadius, outerRadius, and height, of type double.
G. Write code for a constructor to set the data members to 0.0.
H. Write code for the accessor methods, getInnerRadius,
getOuterRadius, and getHeight, that return the value of the appropriate data member.
I. Write code for the mutator methods, setInnerRadius,
setOuterRadius, and setHeight, that each have one formal parameter which is stored as the value of the data member.
J. Write a method named computeVolume to compute the volume of a
hollow cylinder and return the computed volume to the client. The formula
to find the volume of a hollow cylinder is πh(R2 r
2
).
Third, write a client program to test the Cone and HollowCylinder class as
defined above. Name this class CalcVolume. The main method should
perform the following tasks:
K. Allow the user to enter a radius of the cone.
L. Allow the user to enter a height of the cone.
M. Declare and create a Cone object setting the data members to the values
entered by the user.
N. Allow the user to enter an inner radius of the hollow cylinder.
O. Allow the user to enter an outer radius of the hollow cylinder.
P. Allow the user to enter a height of the hollow cylinder.
Q. Declare and create a HollowCylinder object setting the data members
to the values entered by the user.
R. Output the phrase "The volume of the cone with a radius of XX
cm and a height of XX cm is XX cubic cm.", where the XXs are the
input values and the value returned from the method.
S. Output the phrase "The volume of the hollow cylinder with an
inner radius of XX cm, an outer radius of XX cm, and a
height of XX cm is XX cubic cm.", where the XXs are the input
values and the value returned from the method.
Here is some sample input and output:
Input for the cone
Enter the radius: 2.0
Enter the height: 3.0
Input for the hollow cylinder
Enter the inner radius: 2.0
Enter the outer radius: 4.0
Enter the height: 3.0
The volume of the cone with a radius of 2.00 cm and
a height of 3.00 cm is 12.57 cubic cm.
The volume of the hollow cylinder with an inner radius
of 2.00 cm, an outer radius of 4.00 cm, and
a height of 3.00 cm is 113.10 cubic cm.
Finally, draw a UML diagram similar to Fig. 2.32 for the objects created in the
main method.