//Circle class Circle;
import java.util.Scanner;
class Circle {
private double radius;//Store the radius of the circle
public double getRadius() {//Get the radius
return radius;
}
public void setRadius(double radius) {// set radius
this.radius = radius;
}
public Circle(){//No parameter constructor, set the radius to 0.
this.radius=0;
}
public Circle(double r ){//Constructor with parameters, initialize the radius to r.
this.radius=r;
}
public double getArea(){//Get the area of the circle
double r=this.radius;
double area=r*r*3.14;//The area formula of the circle
return area;
}
public double getPerimeter(){//Get the perimeter of the circle
double perimeter=this.radius*2*3.14;//The perimeter formula of the circle
return perimeter;
}
public void show(){//Output the radius, perimeter and area of the circle to the screen.
System.out.println("Please enter the radius of the circle");
Scanner scanner = new Scanner(System.in);
this.setRadius(scanner.nextInt());
System.out.println("Radius of the circle"+this .getRadius());
System.out.println("Circle circumference"+this.getPerimeter());
System.out.println("Circle area"+this.getArea());
}
}
//Cylinder class
class Cylinder extends Circle {//Define a cylinder on the basis of circle.
private double height;//The height of the cylinder
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//Construction method
public Cylinder (double r, double h){
this. height=h;
this.setRadius(r);
}
public double getVolume( ) {//Find the volume of the cylinder
double volume=this.getArea()*this.height;//The volume formula of the cylinder
return volume;
}
public void showVolume(){//Display the volume of the cylinder to the screen.
System.out.println("Volume of cylinder"+this.getVolume());
}
}
//Main program entry
public class Test {
public static void main(String[] args) {
Circle circle=new Circle();
circle .show();
Cylinder cylinder=new Cylinder(2,4);//The radius of the bottom circle of the cylinder and the height of the
cylinder cylinder.showVolume();
}
}
Show results:
Please enter the radius of the circle
4
The radius of the circle 4.0
The circumference of the circle 25.12
The area of the circle 50.24
The volume of the cylinder 50.24