import java.util.Scanner;
public class Circle {
private double radius;//Used to store the radius of the circle;
public Circle ()// Set the radius to 0
{
radius = 0;
}
public Circle (double r){// When creating a Circle object, initialize the radius to r.
radius = r;
}
public Double getArea()//Get the area of the circle
{
return radius*radius*Math.PI;
}
public Double getPerimeter() //Get the circumference of the circle
{
return 2*Math.PI*radius;
}
public void disp() //Output the radius, perimeter and area of the circle to the screen
{
System.out.printf("The radius of the circle is: %.2f, the circumference: % .2f, area: %.2f", radius, getPerimeter1(), getArea1());
}
public class Cylinder extends Circle {
private double hight;
public Cylinder (double r, double h ){
super(r);
this.hight = h;
}
public double getVolume(){
return Math.PI * this.getRadius() * this.getRadius() * hight;
}
public void showVolume( ){
System.out.println("圆柱体的体积:" + this.getVolume());
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("输入半径:");
double r = s.nextDouble();
Circle c = new Circle(r);
c.disp();
}
}
Area of circle, perimeter, volume of cylinder (class encapsulation and abstraction)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324847485&siteId=291194637
Ranking