java Part 5: (20 points) Create a java class named Cylinder. The cylinder should
ID: 3903052 • Letter: J
Question
java Part 5: (20 points) Create a java class named Cylinder. The cylinder should have a double precision real number representing the radius of its circular base,two double precision real numbers representing the x and y components of the center of the base, and a double precision real number representing the height of the cylinder. The cylinder must fully encapsulate its components. This class should have all the necessary methods to construct the cylinder and set the radius, center, and height components to the appropriate and corresponding passed parameters. The cylinder must be capable of calculating its volume using the value 3.14 for t. (I do not expect the syntax to be 100 % accurate but I do not want to see pseudo code either)Explanation / Answer
Java code:
//class named Cylinder
public class Cylinder
{
//components of Cylinder
private double radius,x,y,height;
private static final double PIE=3.14;
//default constructor of Cylinder
public Cylinder() {}
//parameterize constructor of Cylinder
public Cylinder(double radius,double height)
{
this.radius = radius;
this.height = height;
}
//getter and setter methods for components
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//method volume which calculates the volume of the Cylinder
public double volume(double radius,double height)
{
//formula to calculate the volume of the Cylinder
double volume=PIE*radius*radius*height;
return volume;
}
//main method to run the program
public static void main(String[] args)
{
//creating object of Cylinder
Cylinder cylinder=new Cylinder();
//calling volume() method of Cylinder class
double volume=cylinder.volume(3.0,5.0);
//printing result
System.out.printf("Volume of the cylinder=%.2f",volume);
}
}
Output:
Volume of the cylinder=141.30
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.