java定义一个周长类三角形_point类 三点的三角形的周长、面积 编程求解矩形和圆面积 java 三角形的定义...

三角形的定义

1. 先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义构造方法对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。定义一个测试类,在main()中创建一个对象,求给定三点的三角形的周长、面积。 public class Point { int x; int y; Point(){ } Point(int a,int b){ x=a; y=b; } public static double getInstance(Point p1, Point p2) { return ((p1.x - p2.x, 2) + (p1.y - p2.y, 2)); } } public class Triangle { Point n1,n2,n3; boolean isTriangle; public Triangle(Point n1,Point n2,Point n3){ this.n1=n1; this.n2=n2; this.n3=n3; double sideA=tance(n1,n2); double sideB=tance(n2,n3); double sideC=tance(n1,n3); if(sideA+sideB>sideC&&sideB+sideC>sideA){ isTriangle=true; } else{ isTriangle=false; } } public void getPerimeter(){ if(isTriangle){ double sideA=tance(n1,n2); double sideB=tance(n2,n3);

double sideC=tance(n1,n3); n("三角形的周长为"+(sideA+sideB+sideC)); } else{ n("不能构成三角形。。。

"); } } public void getArea(){ if(isTriangle){ double sideA=tance(n1,n2); double sideB=tance(n2,n3); double sideC=tance(n1,n3); double p=(sideA+sideB+sideC)/2.0; n("三角形的面积为"+ (p*(p-sideA)*(p-sideB)*(p-sideC))); } else{ n("不能构成三角形。。

。"); } } } public class Test { public static void main(String[] args) { } 2. 编程求解矩形和圆面积。要求:为了让程序具有较好的扩展性,编写形状接口(J_sharp),并且让矩形类(J_Rectangle)和圆类(J_Circle)均实现其接口。然后定义一个测试类(J_Area)进行测试。

J_sharp接口 public interface J_sharp { public double Area(); } J_ Point n1=new Point(3,0); Point n2 =new Point(0,4); Point n3=new Point(0,0); Triangle sanjiao=new Triangle(n1,n2,n3); imeter(); a(); }

public class J_Rectangle implements J_sharp { double length; double width; double area; J_Rectangle(double width,double length){ =width; =length; } public double Area(){ } area=width*length; return area; } J_ public class J_Circle implements J_sharp{ double r; public J_Circle(double r){ this.r=r; } public double Area(){ return r*r*3.1415; } } J_ public class J_Area { } public static void main(String[] args) { double r=10.0; double width=12.0; double length=4.0; J_Rectangle rectangle=new J_Rectangle(width,length); J_Circle circle =new J_Circle(r); double result=(); n("矩形的面积为"+result); result=(); n("圆的面积为"+result); }

三角形的定义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值