资源描述:
《三角形的周长、面积_编程求解矩形和圆面积_java》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义构造方法对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。定义一个测试类,在main()中创建一个对象,求给定三点的三角形的周长、面积。编程求解矩形和圆面积。要求:为了让程序具有较好的扩展性,编写形状接口(J_sharp),并且让矩形类(J_Rectangle)和圆类(J_Circle)均实现其接口。然后定义一个测试类(J_Area)进行测试。Point.classpublicclassPoint{intx;inty;Point()
2、{}Point(inta,intb){x=a;y=b;}publicstaticdoublegetInstance(Pointp1,Pointp2){returnMath.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2));}}Triangle.classpublicclassTriangle{Pointn1,n2,n3;booleanisTriangle;publicTriangle(Pointn1,Pointn2,Pointn3){this.n1=n1;this.n2=n2;this.n3=n3;doublesideA=Poi
3、nt.getInstance(n1,n2);doublesideB=Point.getInstance(n2,n3);doublesideC=Point.getInstance(n1,n3);if(sideA+sideB>sideC&&sideB+sideC>sideA){isTriangle=true;}else{isTriangle=false;}}publicvoidgetPerimeter(){if(isTriangle){doublesideA=Point.getInstance(n1,n2);doublesideB=Point.getInstance(n2,n3);
4、doublesideC=Point.getInstance(n1,n3);System.out.println("三角形的周长为"+(sideA+sideB+sideC));}else{System.out.println("不能构成三角形!!!");}}publicvoidgetArea(){if(isTriangle){doublesideA=Point.getInstance(n1,n2);doublesideB=Point.getInstance(n2,n3);doublesideC=Point.getInstance(n1,n3);doublep=(sideA+sid
5、eB+sideC)/2.0;System.out.println("三角形的面积为"+Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)));}else{System.out.println("不能构成三角形!!!");}}}Test.classpublicclassTest{publicstaticvoidmain(String[]args){Pointn1=newPoint(3,0);Pointn2=newPoint(0,4);Pointn3=newPoint(0,0);Trianglesanjiao=newTriangle(n1,n2,n3
6、);sanjiao.getPerimeter();sanjiao.getArea();}}J_sharp接口publicinterfaceJ_sharp{publicdoubleArea();}J_Rectangle.classpublicclassJ_RectangleimplementsJ_sharp{doublelength;doublewidth;doublearea;J_Rectangle(doublewidth,doublelength){this.width=width;this.length=length;}publicdoubleArea(){area=wid
7、th*length;returnarea;}}J_Circle.classpublicclassJ_CircleimplementsJ_sharp{doubler;publicJ_Circle(doubler){this.r=r;}publicdoubleArea(){returnr*r*3.1415;}}J_Area.classpublicclassJ_Area{publicstaticvoidmain(String[]args){doubler=10.0;doublewidth=12.0