欢迎来到天天文库
浏览记录
ID:37852242
大小:188.00 KB
页数:10页
时间:2019-06-01
《JAVA实验2复习》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.设计一个名为figure的图形软件包(package)。包中包含三角形、矩形、圆三个类。要求:(1)每个类都要构造方法并为成员设置get和set方法;(2)每个类都要有计算周长和面积的成员方法;(3)完成该软件包后的编码后,在另一个包的含有main方法的类中编写代码,分别使用图形软件包中的三个类,生成三个对象,并打印出其周长和面积。******************************************************packagefigure;publicclassjuxing{
2、//矩形的计算函数privatedoublelenth,width;publicjuxing(){lenth=0;width=0;}publicjuxing(doublex,doubley){lenth=x;width=y;}publicvoidsetLenth(doublelenth){this.lenth=lenth;}publicdoublegetLenth(){returnlenth;}publicvoidsetWidth(doublewidth){this.width=width;}public
3、doublegetWidth(){returnwidth;}publicdoubleperimeter(){returnlenth*width;}publicdoubleArea(){return(lenth+width)*2;}}******************************************************packagefigure;importjava.math.*;publicclasssanjiaoxing{//三角形的计算函数privatedoubleside1;p
4、rivatedoubleside2;privatedoubleside3;publicsanjiaoxing(){side1=0;side2=0;side3=0;}publicsanjiaoxing(doublex,doubley,doublez){side1=x;side2=y;side3=z;}publicvoidsetSide1(doubleside1){this.side1=side1;}publicdoublegetSide1(){returnside1;}publicvoidsetSide2(
5、doubleside2){this.side2=side2;}publicdoublegetSide2(){returnside2;}publicvoidsetSide3(doubleside3){this.side3=side3;}publicdoublegetSide3(){returnside3;}publicdoubleperimeter(){returnside1+side2+side3;}publicdoubleArea(){doublep=(side1+side2+side3)/2;retu
6、rnMath.sqrt(p*(p-side1)*(p-side2)*(p-side3));}}******************************************************packagefigure;importjava.math.*;publicclassyuanxing{//圆形的计算函数privatedoubleradius;publicyuanxing(){radius=0;}publicyuanxing(doublex){radius=x;}publicvoidse
7、tRadius(doubleradius){this.radius=radius;}publicdoublegetRadius(){returnradius;}publicdoubleperimeter(){return2*Math.PI*radius;}publicdoubleArea(){returnMath.PI*radius*radius;}}******************************************************packagefigure_show;impor
8、tfigure.juxing;importfigure.sanjiaoxing;importfigure.yuanxing;publicclassmain{publicstaticvoidmain(String[]args){sanjiaoxingt=newsanjiaoxing(3,4,5);System.out.println("三角形的周长:"+t.perimeter());System.out.println("三角形
此文档下载收益归作者所有