JAVA实现
三角形正方形圆正五边形的周长和面积的计算
新建一个包lab_1
包里面包括点 三角形 远山姐弟正方形 圆 正五边形还有主函数类六个类;
代码如下:
package lab_1;
//新建一个点类
public class Point {
double x;
double y;
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double Getx(){
return x;
}
public double Gety(){
return y;
}
}
package lab_1;
//三角形类
public class Triangle {
Point A,B,C;//定义三角形三个顶点
public double L,p1,S;//L为周长S为面积
public Triangle(Point A,Point B,Point C){
//构造函数
this.A=A;
this.B=B;
this.C=C;
}
public double Perimeter(){
//利用海伦公式计算周长
double AB=Math.sqrt((A.Getx()-B.Getx())*(A.Getx()-B.Getx())+(A.Gety()-B.Gety())*(A.Gety()-B.Gety()));
double BC=Math.sqrt((B.Getx()-C.Getx())*(B.Getx()-C.Getx())+(B.Gety()-C.Gety())*(B.Gety()-C.Gety()));
double AC=Math.sqrt((A.Getx()-C鹅鹅鹅舞蹈.Getx())*(A.Getx()-C.Getx())+(A.Gety()-C.Gety())*(A.Gety()-C.Gety()));
return L=AB+BC+AC;
}
public double area(){
//利用海伦公式计算面积
double AB=Math.sqrt((A.Getx()-B.Getx())*(A.Getx()-B.Getx())+(A.Gety()-B.Gety())*(A.Gety()-B.Gety()));
double BC=Math.sqrt((B.Getx()-C.Getx())*(B.Getx()-C.Getx())+(B.Gety()-C.Gety())*(B.Gety()-C.Gety()));
double AC=Math.sqrt((A.Getx()-土豆干C.Getx())*(A.Getx()-C.Getx())+(A.Gety()-C.Gety())*(A.Gety()-C.Gety()));
p1=(AB+BC+AC)/2;
return S=Math.sqrt((p1*(p1-AB)*(p1-BC)*(p1-AC)));//返回面积
}
}
package lab_1;
//正方形类
public class Square {
Point D,E;//定义正方形的对角定点
public Square(Point D,Point E){
//构造函数
this.D=D;
this.E=E;
}
public double Perimeter(){
double a,L;//a正方形边长,L正方形周长
double DE=Math.sqrt((D.Getx()-E.Getx())*(D.Getx()-E.Getx())+(D.Gety()-E.Gety())*(D.Gety()-E.Gety()));
a=Math.sqrt(DE*DE/2);
return L=4*a;
}
public double area(){
double S;//定义面积
double DE=Math.sqrt((D.Getx()-E.Getx())*(D.Getx()-E.Getx())+(D.Gety()-E.Gety())*(D.Gety()-E.Gety()));
double b=Math.sqrt(DE*DE/2);
return S=b*b;//返回面积
}
}
package lab_1;
//圆类
public class Circle {
Point F;//圆心坐标
double r;//r为圆的半径
public Circle(Point F,double r){
//构造函数
this.F=F;
this.r=r;
}
public double Perimeter(){
double L=Math.PI*2*r;
return L;
}
public double area(){
double S=Math.PI*r铁蹄下的黑玫瑰*r;
return S;
}
}
package lab_1;
//正五边形类
public八拳 class Pentagon {
Point G;//正五边形中心点
Point H;//正五边形任意一个顶点
public Pentagon( Point G,Point H){
this.G=G;
this.H=H;
}
public double Perimeter(){
double GH=Math.sqrt((G.Getx()-H.Getx())*(G.Getx()-H.Getx())+(G.Gety()-H.Gety())*(G.Gety()-H.Gety()));
double f=GH*(Math.sin(36*Math.PI/180));//f为边的一半
double L=10*f;//计算周长
return L;
}
public double area(){
double GH=Math.sqrt((G.Getx()-H.Getx())*(G.Getx()-H.Getx())+(G.Gety()-H.Gety())*(G.Gety()-H.Gety()));
double g=GH*(Math.cos(36*Math.PI/180));//计算三角形的高
double f=GH*(Math.sin(36*Math.PI/180));//f为边的一半
double S=5*g*f;//计算面积
return S;
}
}
package精彩美文 lab_1;
//主函数调用类
public删除文件需要administrators权限 class main {
public static void main(String[] args) {
//三角形的三个顶点坐标
Point A=new Point(1,0);
Point B=new Point(4,2);
Point C=new Point(2,4);
Triangle x1=new Triangle(A,B,C);
System.out.println("三角形的面积是: "+x1.area());
System.out.println("三角形的周长是: "+x1.Perimeter());
//正方形的对角点坐标
Point D=new Point(1,1);班主任兵法
Point E=new Point(4,4);
Square x2=new Square(D,E);
System.out.println("正方形的面积是: "+x2.area());
System.out.println("正方形的周长是: "+x2.Perimeter());
//圆的圆心和半径
Point F=new Point(4,5);
double r=4.00;
Circle x3=new Circle(F,r);
System.out.println("圆的面积是: "+x3.area());
System.out.println("圆的周长是: "+x3.Perimeter());
//新建正五边形的中心点和一个圆点
Point G=new Point(3,3);
Point H=new Point(5,5);
Pentagon x4=new Pentagon(G,H);
System.out.println("正五边形的面积是: "+x4.area());
System.out.println("正五边形的周长是: "+x4.Perimeter());
}
}
运行结果:
三角形的面积是: 5.000000000000003
三角形的周长是: 10.557084025827841
正方形的面积是: 8.999999999999998
正方形的周长是: 11.999999999999998
圆的面积是: 50.26548245743669
圆的周长是: 25.132********8345
.021*********
正五边形的周长是: 16.625077511098137