2020-08-31 13:45:34 +02:00
|
|
|
public class Cylinder extends Circle {
|
|
|
|
|
|
|
|
private double height;
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* Construct a cylinder at origin with Radius r and height h
|
|
|
|
* @param r radius
|
|
|
|
* @param h height
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public Cylinder(double r, double h){
|
2020-09-23 12:36:57 +02:00
|
|
|
super(r);
|
2020-08-31 13:45:34 +02:00
|
|
|
// Circle with location 0, 0 and radius r.
|
|
|
|
this.height = h;
|
|
|
|
//now a cylinder with radius r and height h at 0,0
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* Construct a cylinder at (x,y) with Radius r and height h
|
|
|
|
* @param x X
|
|
|
|
* @param y Y
|
|
|
|
* @param r radius
|
|
|
|
* @param h height
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public Cylinder(int x, int y,double r, double h){
|
|
|
|
super(r, x, y); //a circle with radius r at x,y
|
|
|
|
this.height = h;
|
|
|
|
//cylinder with radius r and height h at x,y
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* get surface area of cylinder
|
|
|
|
* @return surface area
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public double getSurfaceArea(){
|
|
|
|
return 2*(Math.PI*getRadius()*getHeight() + Math.PI*Math.pow(getRadius(),2));
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* get volume of cylinder
|
|
|
|
* @return volume
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public double getVolume(){
|
|
|
|
return Math.PI*Math.pow(getRadius(), 2)*getHeight();
|
|
|
|
}
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* set height h
|
|
|
|
* @param h
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public void setHeight(double h){ this.height = h;}
|
|
|
|
|
2020-09-05 21:48:38 +02:00
|
|
|
/**
|
|
|
|
* get height
|
|
|
|
* @return height
|
|
|
|
*/
|
2020-08-31 13:45:34 +02:00
|
|
|
public double getHeight(){ return this.height; }
|
|
|
|
|
|
|
|
public String toString(){
|
2020-08-31 15:03:51 +02:00
|
|
|
String format = "%sCylinder's height = %.2f ,Surface area = %.2f , Volume = %.2f";
|
2020-08-31 13:45:34 +02:00
|
|
|
return String.format(format, super.toString(), getHeight(), getSurfaceArea(), getVolume());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|