欢迎来到天天文库
浏览记录
ID:9403430
大小:43.00 KB
页数:4页
时间:2018-04-30
《java实现三角形面积和周长的计算》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、3.先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个对象,求给定三点的三角形的周长、面积。3.将上题的方法setTri改用构造方法实现初始化。主类:Trianglele属性:PiontpointIntside方法:Trianglete(point1,point2,point3);girth(trianglete);Area(tiran
2、glete);类名:point属性:intxcoordintycoord方法:Point(x,y)packageThree;importjava.util.Scanner;publicclassTrianglele{Pointpoint1,point2,point3;doubleside1;doubleside2;doubleside3;publicTrianglele(Pointpoint1,Pointpoint2,Pointpoint3){//利用构造函数创建一个三角形,并求出边长this.point1=point1;t
3、his.point2=point2;this.point3=point3;doublesidex=point1.pointLen(point2,point3);this.side1=sidex;doublesidey=point2.pointLen(point1,point3);this.side2=sidey;doublesidez=point3.pointLen(point1,point2);this.side3=sidez;}/**下面来求周长和面积*@paramargs*/publicdoublearea(){//求
4、面积doublex1,x2,y1,y2;doubleheigth,area;doublesidemax,sidemin1,sidemin2;if(side1>side2){sidemax=side1;x1=point1.xpoint;y1=point1.ypoint;x2=(point3.xpoint+point2.xpoint)*0.5;y2=(point3.ypoint+point2.ypoint)*0.5;sidemin1=side2;}else{sidemax=side2;sidemin1=side2;x1=poin
5、t2.xpoint;y1=point2.ypoint;x2=(point3.xpoint+point1.xpoint)*0.5;y2=(point3.ypoint+point1.ypoint)*0.5;}if(sidemax6、sidemin2=side3;}if(sidemax*sidemax==sidemin1*sidemin1+sidemin2*sidemin2){area=sidemin1*sidemin2*0.5;}else{Pointpoint=newPoint(x1,y1);Pointpo=newPoint(x2,y2);heigth=point.pointLen(point,po);area=sidemax*heigth*0.5;}returnarea;}publicdoublegirth(){doublegirth;girth=s7、ide1+side2+side3;returngirth;}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubbooleanTF=false;while(TF==false){System.out.println("请输入你想要的三角形的三个坐标");Scanners=newScanner(System.in);Doublex1=s.nextDouble();Doubley1=s.nextDouble();Doublex2=s.nextDoubl8、e();Doubley2=s.nextDouble();Doublex3=s.nextDouble();Doubley3=s.nextDouble();Triangleletri=newTrianglele(newPoint(x1,y1),newPoint(x2,y2),newPoint(
6、sidemin2=side3;}if(sidemax*sidemax==sidemin1*sidemin1+sidemin2*sidemin2){area=sidemin1*sidemin2*0.5;}else{Pointpoint=newPoint(x1,y1);Pointpo=newPoint(x2,y2);heigth=point.pointLen(point,po);area=sidemax*heigth*0.5;}returnarea;}publicdoublegirth(){doublegirth;girth=s
7、ide1+side2+side3;returngirth;}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubbooleanTF=false;while(TF==false){System.out.println("请输入你想要的三角形的三个坐标");Scanners=newScanner(System.in);Doublex1=s.nextDouble();Doubley1=s.nextDouble();Doublex2=s.nextDoubl
8、e();Doubley2=s.nextDouble();Doublex3=s.nextDouble();Doubley3=s.nextDouble();Triangleletri=newTrianglele(newPoint(x1,y1),newPoint(x2,y2),newPoint(
此文档下载收益归作者所有