欢迎来到天天文库
浏览记录
ID:20279261
大小:59.00 KB
页数:6页
时间:2018-10-09
《三角形正方形 圆和正五边形周长和面积计算 java实现》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、JAVA实现三角形正方形圆正五边形的周长和面积的计算新建一个包lab_1包里面包括点三角形正方形圆正五边形还有主函数类六个类;代码如下:packagelab_1;//新建一个点类publicclassPoint{doublex;doubley;publicPoint(doublex,doubley){this.x=x;this.y=y;}publicdoubleGetx(){returnx;}publicdoubleGety(){returny;}}packagelab_1;//三角形类publicclassTriangle{Poi
2、ntA,B,C;//定义三角形三个顶点publicdoubleL,p1,S;//L为周长S为面积publicTriangle(PointA,PointB,PointC){//构造函数this.A=A;this.B=B;this.C=C;}publicdoublePerimeter(){//利用海伦公式计算周长doubleAB=Math.sqrt((A.Getx()-B.Getx())*(A.Getx()-B.Getx())+(A.Gety()-B.Gety())*(A.Gety()-B.Gety()));doubleBC=Math.
3、sqrt((B.Getx()-C.Getx())*(B.Getx()-C.Getx())+(B.Gety()-C.Gety())*(B.Gety()-C.Gety()));doubleAC=Math.sqrt((A.Getx()-C.Getx())*(A.Getx()-C.Getx())+(A.Gety()-C.Gety())*(A.Gety()-C.Gety()));returnL=AB+BC+AC;}publicdoublearea(){//利用海伦公式计算面积doubleAB=Math.sqrt((A.Getx()-B.Get
4、x())*(A.Getx()-B.Getx())+(A.Gety()-B.Gety())*(A.Gety()-B.Gety()));doubleBC=Math.sqrt((B.Getx()-C.Getx())*(B.Getx()-C.Getx())+(B.Gety()-C.Gety())*(B.Gety()-C.Gety()));doubleAC=Math.sqrt((A.Getx()-C.Getx())*(A.Getx()-C.Getx())+(A.Gety()-C.Gety())*(A.Gety()-C.Gety()));p1=
5、(AB+BC+AC)/2;returnS=Math.sqrt((p1*(p1-AB)*(p1-BC)*(p1-AC)));//返回面积}}packagelab_1;//正方形类publicclassSquare{PointD,E;//定义正方形的对角定点publicSquare(PointD,PointE){//构造函数this.D=D;this.E=E;}publicdoublePerimeter(){doublea,L;//a正方形边长,L正方形周长doubleDE=Math.sqrt((D.Getx()-E.Getx())*(
6、D.Getx()-E.Getx())+(D.Gety()-E.Gety())*(D.Gety()-E.Gety()));a=Math.sqrt(DE*DE/2);returnL=4*a;}publicdoublearea(){doubleS;//定义面积doubleDE=Math.sqrt((D.Getx()-E.Getx())*(D.Getx()-E.Getx())+(D.Gety()-E.Gety())*(D.Gety()-E.Gety()));doubleb=Math.sqrt(DE*DE/2);returnS=b*b;//返
7、回面积}}packagelab_1;//圆类publicclassCircle{PointF;//圆心坐标doubler;//r为圆的半径publicCircle(PointF,doubler){//构造函数this.F=F;this.r=r;}publicdoublePerimeter(){doubleL=Math.PI*2*r;returnL;}publicdoublearea(){doubleS=Math.PI*r*r;returnS;}}packagelab_1;//正五边形类publicclassPentagon{Poin
8、tG;//正五边形中心点PointH;//正五边形任意一个顶点publicPentagon(PointG,PointH){this.G=G;this.H=H;}publicdoublePerimeter(){doubleGH=Math
此文档下载收益归作者所有