资源描述:
《java代码(面向对象程序设计)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、JAVA代码1.一元二次方程定义方法:classEquationA{doublea,b,c;doubledelta;EquationA(doublex,doubley,doublez){a=x;this.b=y;c=z;getDela();}privatevoidgetDela(){delta=b*b-4*a*c;}doublegetRoot1(){doubler1=0;r1=(-b+Math.sqrt(delta))/(2*a);returnr1;}doublegetRoot2(){doubler1
2、=0;r1=(-b-Math.sqrt(delta))/(2*a);returnr1;}publicStringtoString(){Stringstr=a+"x*x"+"+"+b+"x"+"+"+c+"=0";returnstr;}}测试类:publicclasstestE{publicstaticvoidmain(String[]args){EquationAe1=null;doubler1=0,r2=0;e1=newEquationA(1,-14,-3);System.out.println(e
3、1.toString());if(e1.delta>0){System.out.println("两个根");r1=e1.getRoot1();r2=e1.getRoot2();System.out.println(r1);System.out.println(r2);}elseif(e1.delta==0){System.out.println("相同根");System.out.println(e1.getRoot2());}elseSystem.out.println("无根");Equatio
4、nAe2=newEquationA(11,114,3);System.out.println(e2);}}2.矩形、圆形(一)Circle(圆类):publicclassCircle{doublex,y,r;publicCircle(doublex,doubley,doubler){this.x=x;this.y=y;this.r=r;}publicdoubleGetPerimeter(){doubleperimeter=0;perimeter=2*r*3.14;returnperimeter;}pu
5、blicdoubleGetArea(){doublearea=0;area=3.14*r*r;returnarea;}publicbooleanInside(doublex1,doubley1){booleanb;doubled;d=(x1-this.x)*(x1-this.x)+(y1-this.y)*(y1-this.y);b=(d>r*r)?false:true;returnb;}publicvoidCompareCirclesArea(Circleo2){doubles1,s2;s1=this
6、.GetArea();s2=o2.GetArea();if(s1>s2){System.out.println("圆形1面积更大");}elseif(s2>s1){System.out.println("圆形2面积更大");}else{System.out.println("两个圆形面积一样大");}}publicvoidCompareCirclesPeri(Circleo2){doubles1,s2;s1=this.GetPerimeter();s2=o2.GetPerimeter();if(s1>
7、s2){System.out.println("圆形1周长更大");}elseif(s2>s1){System.out.println("圆形2周长更大");}else{System.out.println("两个圆形周长一样大");}}}Rect(矩形)://defineaclassforRectanglespublicclassRect{doublex1,y1,x2,y2;//leftlowerangleandrightupperanglepointdefinethelocationdoublew
8、idth,length;publicRect(doublex1,doubley1,doublex2,doubley2){this.x1=x1;this.y1=y1;this.x2=x2;this.y2=y2;length=this.x2-this.x1;width=this.y2-this.y1;//没有分情况讨论,默认右上角点坐标大于左下角坐标}publicdoubleGetPerimeter(){doubleperimeter=0;perimeter