资源描述:
《用java求三角形的周长和面积》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、用java求三角形的周长和面积用java求三角形的周长和面积?publicclassJs{privatedoublea;privatedoubleb;privatedoublec;Js(doublea,doubleb,doublec){this.a=a;this.b=b;this.c=c;}publicdoublegetA(){returna;}publicdoublegetB(){returnb;}publicdoublegetC(){returnc;}publicdoublearea(){doubles1=(a+b+c)/2;doubles2=
2、s1*(s1-a)*(s1-b)*(s1-c);doubleresult=Math.sqrt(s2);returnresult;}publicdoublejsLength(){returna+b+c;}}classJxcomputer{publicstaticvoidmain(String[]args){Jsjs=newJs(3,6,8);System.out.println("三角形三边长度分别为:"+"a:"+js.getA()+"tb:"+js.getB()+"tc:&
3、quot;+js.getC());System.out.println("三角形面积为:"+js.area());System.out.println("三角形周长为:"+js.jsLength());}}【编译与运行】编译:javacJs.java运行:javaJxcomputer计算三角形的面积和周长./**@(#)Point.java2008-1-4*/packagecn.com.robert.baidu.triangle;/***@authorR*/publicclassPoint{privatein
4、tx;privateinty;/**构造函数*/publicPoint(intX,intY){x=X;y=Y;}publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}}--------------------------/**@(#)Triangle.java2008-1-4*/packagecn.com.robert.baidu.triangle;/***@authorR*//*
5、*构造函数*/publicclassTriangle{privatePointa;privatePointb;privatePointc;publicTriangle(PointA,PointB,PointC){a=A;b=B;c=C;}/**三条边的长度*/privatedoublel1=Math.sqrt(Math.pow(a.getX()-b.getX(),2)+Math.pow(a.getY()-b.getY(),2));privatedoublel2=Math.sqrt(Math.pow(c.getX()-b.getX(),2)+Math
6、.pow(c.getY()-b.getY(),2));privatedoublel3=Math.sqrt(Math.pow(a.getX()-c.getX(),2)+Math.pow(a.getY()-c.getY(),2));/**定义Heron公式*/privatedoubleheron(doublex,doubley,doublez){doublep=(x+y+z)/2;doublearea=Math.sqrt(p*(p-x)*(p-y)*(p-z));returnarea;}/**周长是三边之和*/publicdoublegetVolume
7、(){returnl1+l2+l3;}/**调用海伦公式求面积*/publicdoublegetArea(){returnheron(l1,l2,l3);}/**三点不一线*/publicbooleanisTriangle(){return((a.getX()-b.getX())/(a.getY()-b.getY()))!=((a.getX()-c.getX())/(a.getY()-c.getY()));}}######################################################################
8、##交谈中请勿轻信汇款、中奖信息、陌生电话,勿使用外挂软件。候雷(78665444)13:16:31publicclass