圆的面积:
public class Circle{
private double radius;
Circle(){
radius=1;
}
Circle(double r){
radius=r;
}
Circle(Circle c){
radius=c.radius;
}
public void setRadius(double r){
radius=r;
}
public double getRadius(){
return radius;
}
public double getArea(){
return Math.PI*radius*radius;
}
}
public class Example{
public static void main(String args[]){
Circle c=new Circle();
c.setRadius(2);
System.out.println(c.getArea());
}
}
圆柱的面积:
public class Cylinder{
private double bottom;
private double height;
Cylinder(){
bottom=1;
height=1;
}
Cylinder (double b,double h){
bottom=b;
height=h;
}
Cylinder(Cylinder cy){
bottom=cy.bottom;
height=cy.height;
}
public void setBottom(double b){
bottom=b;
}
public void setHeight(double h){
height=h;
}
public double getBottom(){
return bottom;
}
public double getHeight(){
return height;
}
public double getArea(){
return bottom*height;
}
}
public class Example1{
public static void main(String args[]){
Cylinder c1=new Cylinder();
c1.setBottom(5);
c1.setHeight(2);
System.out.println(c1.getArea());
}
}