Implement the class called Cylinder shown in the Unified Modeling Language (UML)
ID: 3685125 • Letter: I
Question
Implement the class called Cylinder shown in the Unified Modeling Language (UML) diagram below. The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. The class also include methods that calculate and return the volume and surface area of the Cylinder. Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString(), change one parameter (your choice) in each, and display them again.
Here's what I have so far:
Explanation / Answer
Cylinder.java
public class Cylinder {
private Circle base;
private double height;
/**
* Constructor with default color, radius and height
*/
public Cylinder() {
base = new Circle(); // Call the constructor to construct the Circle
height = 1.0;
}
/**
* Constructor with default radius of base, color but given height
*
* @param height
*/
public Cylinder(double heigth) {
base = new Circle();
this.height = heigth;
}
/**
* Constructor with given base and height
*
* @param base
* @param height
*/
public Cylinder(Circle base, double height) {
this.base = base;
this.height = height;
}
/**
* Constructor with default color, but given radius, height
*
* @param radius
* @param height
*/
public Cylinder(double radius, double height) {
base = new Circle(radius);
this.height = height;
}
/**
* Getter for height
*
* @return height of cylinder
*/
public double getHeight() {
return height;
}
/**
* Setter for height
*
* @param height
*/
public void setHeight(double height) {
this.height = height;
}
/**
* Getter for base
*
* @return base
*/
public Circle getBase() {
return base;
}
/**
* Setter for base
*
* @param base
*/
public void setBase(Circle base) {
this.base = base;
}
/**
* Setter for base
*
* @param radius
* of base
*/
public void setBase(double radius) {
base.setRadius(radius);
}
/**
* A public method for computing the volume of cylinder
*
* @return volume of cylinder
*/
public double getVolume() {
return base.getArea() * height;
}
public double getArea() {
return 2 * Math.PI * base.getRadius() * height + 2 * base.getArea();
}
@Override
public String toString() {
return "Cylinder: base " + base.toString() +
" height= " + height;
}
}
Circle.java
public class Circle {
private double radius;
private String color;
/**
* Default constructor
*/
public Circle() {
radius = 1.0; // default radius
color = "red"; // default color
}
/**
* Constructor with given radius
* @param radius
*/
public Circle(double radius) {
this.radius = radius;
color = "red"; // default color
}
/**
* Constructor with given radius and color
* @param radius
*/
public Circle(double radius, String color) {
this.radius = radius;
this.color = color;
}
/**
* Getter for instance variable radius
* @return radius
*/
public double getRadius() {
return radius;
}
/**
* Getter for instance variable color
* @return color
*/
public String getColor() {
return color;
}
/**
* Getter for instance area
* @return area of the circle
*/
public double getArea() {
return radius*radius*Math.PI; // Area = Pi * r^2
}
/**
* Setter for instance variable radius
* @param radius
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* Setter for instance variable color
* @param color
*/
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Circle [radius=" + radius + ", color=" + color + ", area="
+ getArea() + "]";
}
}
CylinderTest.java
public class CylinderTest {
public static void main(String[] args) {
Circle circle = new Circle();
Cylinder cylinder1 = new Cylinder(0.5, 18);
System.out.println(cylinder1); //Cylinder: base Circle [radius=0.5, color=red, area=0.7853981633974483] height= 18.0
cylinder1.setBase(circle);
cylinder1.setHeight(2.0);
System.out.println(cylinder1); //Cylinder: base Circle [radius=1.0, color=red, area=3.141592653589793] height= 2.0
Cylinder cylinder2 = new Cylinder();
System.out.println(cylinder2);
System.out.println(cylinder2.getBase());
System.out.println("Cylinder area=" + cylinder2.getArea() + " volume=" + cylinder2.getVolume());
}
}
sample output
Cylinder: base Circle [radius=0.5, color=red, area=0.7853981633974483] height= 18.0
Cylinder: base Circle [radius=1.0, color=red, area=3.141592653589793] height= 2.0
Cylinder: base Circle [radius=1.0, color=red, area=3.141592653589793] height= 1.0
Circle [radius=1.0, color=red, area=3.141592653589793]
Cylinder area=12.566370614359172 volume=3.141592653589793