Create a class called Cylinder.java that contains two double-precision instance
ID: 3531498 • Letter: C
Question
Create a class called Cylinder.java that contains two double-precision instance variables named radius and height. The class should include a constructor that initializes the radius and height variables. Also, you need a class method named volume() that returns the volume of a Cylinder object. The volume of a cylinder is given by its radius squared times its height times Pi (radius * radius * height * Math.PI). You can either use the value 3.1416 for Pi or use the Java provided value named Math.PI.
Write a class called CylinderTest.java and declare an array of three Cylinder objects to call the methods you declared in the Cylinder class. Make sure that all class methods are called from main(). Have main() display the value returned by volume() and verify the returned value by hand calculations (paper/pencil). Prompt the user to enter the values for the radius and height of each Cylinder object in the array.
Grading Rubric
Cylinder
Points
Description
Standard header included
1
Program compiles
2
Program executes
2
Created Cylinder class
3
Constructor created that initializes variables
2
Contains a volume method
2
CylinderTest class, which drives the application, is created
4
Correct output is displayed
4
Subtotal
20
Cylinder
Points
Description
Standard header included
1
Must contain program's name, student name, and description of the programProgram compiles
2
Program does not have any errorProgram executes
2
Program runs without any errorCreated Cylinder class
3
Program contains the Cylinder classConstructor created that initializes variables
2
A constructor method exists in the Cylinder class and initializes radius and heightContains a volume method
2
The volume method exists in the Cylinder class and it returns the volume of a Cylinder objectCylinderTest class, which drives the application, is created
4
A driver class with the main methodCorrect output is displayed
4
Program displays the radius, height, and volume of three Cylinder objectsSubtotal
20
Explanation / Answer
import java.lang.Math.* // Imports the Math class
public class Cylinder { // creates the Cylinder class
// instance double variables for radius and height should always be declared private
private double radius, height;
private double final Pi = 3.1416;
private double volume;
public Cylinder () { //default constructorJava will not provide this to you since you are overloading it
}
// constructor that initializes the radius and height variables.
public Cylinder (double height, double radius) {
this.height = height;
this.radius = radius;
}
// accessor method for height
public double getHeight() {
return height;
}
// accessor method for radius
public double getRadius() {
return radius;
}
// volume method
public double volume() {
volume = Math.pow(radius, 2) * Pi // main calculation
return volume;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.