public class Exercise9_9 {
public static void main(String[] args) {
RegularPolygon a0 = new RegularPolygon();
RegularPolygon a1 = new RegularPolygon(6,4);
RegularPolygon a2 = new RegularPolygon(10,4,5.6,7.8);
System.out.println(“周长:”+a0.getPerimeter()+" 面积:"+a0.getArea());
System.out.println(“周长:”+a1.getPerimeter()+" 面积:"+a1.getArea());
System.out.println(“周长:”+a2.getPerimeter()+" 面积:"+a2.getArea());
}
}
class RegularPolygon{
private int n;
private double size;
private double x;
private double y;
public RegularPolygon(){
n = 3;
size = 1;
x = 0;
y = 0;
}//带默认值的无参构造方法
public RegularPolygon(int newn, double newside){
n = newn;
size = newside;
x = 0;
y = 0;
}//一个能创建带指定边数和边长度、中心在(0,0)的正多边形的构造方法
public RegularPolygon(int newn, double newside,double newx, double newy){
n = newn;
size = newside;
x = newx;
y = newy;
}//一个能创建带指定边数和边长度、中心在(x,y)的正多边形的构造方法
public void setN(int newn){
n = newn;
}//设置边数
public void setSide(double newsize){
size = newsize;
}//设置边长
public void setX(double newx){
x = newx;
}//设置X
public void setY(double newy){
y = newy;
}//设置y
public double getPerimeter(){
return n * size;
}//返回多边形周长
public double getArea(){
return nsizesize/4/Math.tan(Math.PI/n);
}//返回多边形面积
}
标签:Java,RegularPolygon,double,void,newn,9.9,第十版,public,size
来源: https://blog.csdn.net/weixin_43996103/article/details/101384572