Added Cylinder class
Changed toString methods to work inhirit correctly.
This commit is contained in:
parent
c1a575da75
commit
732124669c
@ -21,8 +21,8 @@ public class Circle extends _Point {
|
||||
public double getCircumfrence() { return 2*Math.PI*this.radius; }
|
||||
|
||||
public String toString() {
|
||||
return String.format("Position %s, Radius %f," +
|
||||
"Area %f, Circumference %f ", super.toString(), getRadius(), getArea(), getCircumfrence());
|
||||
String format = "%sCircle's radius = %f ,area = %f, circumfrence = %f\n";
|
||||
return String.format(format, super.toString(), getRadius(), getArea(), getCircumfrence());
|
||||
}
|
||||
|
||||
}
|
||||
|
35
lab-01/src/Cylinder.java
Normal file
35
lab-01/src/Cylinder.java
Normal file
@ -0,0 +1,35 @@
|
||||
public class Cylinder extends Circle {
|
||||
|
||||
private double height;
|
||||
|
||||
public Cylinder(double r, double h){
|
||||
super(r);//Recommended by IDE. Invokes Circle(double radius).
|
||||
// Circle with location 0, 0 and radius r.
|
||||
this.height = h;
|
||||
//now a cylinder with radius r and height h at 0,0
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
public double getSurfaceArea(){
|
||||
return 2*(Math.PI*getRadius()*getHeight() + Math.PI*Math.pow(getRadius(),2));
|
||||
}
|
||||
|
||||
public double getVolume(){
|
||||
return Math.PI*Math.pow(getRadius(), 2)*getHeight();
|
||||
}
|
||||
|
||||
public void setHeight(double h){ this.height = h;}
|
||||
|
||||
public double getHeight(){ return this.height; }
|
||||
|
||||
public String toString(){
|
||||
String format = "%sCylinder's height = %f ,Surface area = %f , Volume = %f";
|
||||
return String.format(format, super.toString(), getHeight(), getSurfaceArea(), getVolume());
|
||||
}
|
||||
|
||||
}
|
@ -32,6 +32,6 @@ public class _Point {
|
||||
|
||||
public void setY(int y){ this.y = y; }
|
||||
|
||||
public String toString(){ return String.format("Position (%d, %d)", this.x, this.y); }
|
||||
public String toString(){ return String.format("Current (x,y) location (%d,%d)\n", this.x, this.y); }
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user