用java实现正n边形的问题
public class RegularPolygon {
// 多边形的边数
private int n;
// 存储边的长度
private double side;
// 多边形中点的x坐标
private double x;
// 多边形中点的y坐标
private double y;
public RegularPolygon() {
n=3;
side=1;
x=0;
y=0;
}
public RegularPolygon(int n, double side) {
this.n = n;
this.side = side;
x=0;
y=0;
}
public RegularPolygon(int n, double side, double x, double y) {
this.n = n;
this.side = side;
this.x = x;
this.y = y;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getPerimeter(){
return n*side;
}
public double getArea(){
return ((n*Math.pow(side,2))/(4*Math.tan(Math.PI/n)));
}
public static void main(String[] args) {
RegularPolygon regularPolygon1=new RegularPolygon();
System.out.println("周长为:\t"+regularPolygon1.getPerimeter());
System.out.println("面积为:\t"+regularPolygon1.getArea());
RegularPolygon regularPolygon2=new RegularPolygon(6,4);
System.out.println("周长为:\t"+regularPolygon2.getPerimeter());
System.out.println("面积为:\t"+regularPolygon2.getArea());
RegularPolygon regularPolygon3=new RegularPolygon(10,4,5.6,7.8);
System.out.println("周长为:\t"+regularPolygon3.getPerimeter());
System.out.println("面积为:\t"+regularPolygon3.getArea());
}
}
posted on 2024-06-28 12:29 Indian_Mysore 阅读(10) 评论(0) 收藏 举报
浙公网安备 33010602011771号