java实现三角形面积和周长的计算
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3.先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个对象,求给定三点的三角形的周长、面积。3.将上题的方法setTri改用构造方法实现初始化。
主类:Trianglele
属性:Piont point
Int side
方法:Trianglete(point1,point2,point3);
girth(trianglete);
Area(tiranglete);
类名:point
属性:int xcoord
int ycoord
方法:Point(x,y)
package Three;
import java.util.Scanner;
public class Trianglele {
Point point1,point2,point3;
double side1;
double side2;
double side3;
public Trianglele(Point point1,Point point2,Point point3){
//利用构造函数创建一个
三角形,并求出边长
this.point1=point1;
this.point2=point2;
this.point3=point3;
double sidex=point1.pointLen(point2, point3);
this.side1=sidex;
double sidey=point2.pointLen(point1, point3);
this.side2=sidey;
double sidez=point3.pointLen(point1, point2);
this.side3=sidez;
}
/**下面来求周长和面积
* @param args
*/
public double area(){ //求面积
double x1,x2,y1,y2;
double heigth,area;
double sidemax,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=point2.xpoint;
y1=point2.ypoint;
x2=(point3.xpoint+point1.xpoint)*0.5;
y2=(point3.ypoint+point1.ypoint)*0.5;
}
if(sidemax sidemin2=sidemax; sidemax=side3; x1=point3.xpoint; y1=point3.ypoint; x2=(point1.xpoint+point2.xpoint)*0.5; y2=(point1.ypoint+point2.ypoint)*0.5; }else{ sidemin2=side3; } if(sidemax*sidemax==sidemin1*sidemin1+sidemin2*sidemin2){ area=sidemin1*sidemin2*0.5; } else{ Point point=new Point(x1,y1); Point po=new Point(x2,y2); heigth=point.pointLen(point,po); area=sidemax*heigth*0.5; } return area; } public double girth(){ double girth; girth=side1+side2+side3; return girth; } public static void main(String[] args) { // TODO Auto-generated method stub boolean TF=false; while(TF==false){ System.out.println("请输入你想要的三角形的三个坐标"); Scanner s=new Scanner(System.in); Double x1=s.nextDouble(); Double y1=s.nextDouble(); Double x2=s.nextDouble(); Double y2=s.nextDouble(); Double x3=s.nextDouble(); Double y3=s.nextDouble(); Trianglele tri=new Trianglele(new Point(x1,y1),new Point(x2,y2),new Point(x3,y3)); if((tri.side1 { double area=tri.area(); double gir=tri.girth(); System.out.println("这个三角形的面积为:"+area+"这个三角形的周长为 "+gir+" "+"程序到此结束!!"); TF=true;} else{ TF=false; System.out.println("你所输入的三角形错误"); } } } } package Three; import ng.Math; class Point { double xpoint; double ypoint; public Point(double x,double y){ xpoint=x; ypoint=y; } public double pointLen(Point po1,Point po2){ //求两个点之间的距离double length; double len=((po1.xpoint-po2.xpoint)*(po1.xpoint-po2.xpoint)+(po1.ypoint-po2. ypoint)*(po1.ypoint-po2.ypoint)); length=Math.sqrt(len); return length;