欢迎来到天天文库
浏览记录
ID:31739625
大小:68.18 KB
页数:8页
时间:2019-01-17
《java课后实验三》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实验三类与对象1、(1)创建一个Rectangle类,添加两个(private)属性width与height(2)创建一个构造方法,选择width-tjheight为形式参数。(3)在Rectangle类中添加两个方法计算矩形的周长和面积1234567910111213曰141516^1718packagekehoushiyan3;importjava.util2、ht;//尸二W、:privat已.s^width^heightpublicRectangle(doublewidth》doubleheight){//二忙应芒亏,=?fwidthvh亡ight力书比撐this•width»width;thim.height-height;}publicdoublegetPerimeter(){I・%連亏性return2•(width♦height);}publicdoublegetArea(){returnwidth•height;2121222324252627282930}p3、ublicstaticvoidmain(String[]args){Scan门皀「sc=newScanner(System・in);System,out•printInw);doublewidth«sc.nextDouble();doubleheight=sc.nextDouble();Rectangletyn=newRectangle(width,height);System•out.println(z:"+tyn•getPeriineter());System.out.println(,,*^?:ztr三:"+4、tyn.getArea());}Rectai1.52.5毛弓半豆长三:8•02、创建三角形、梯形和圆形的类封装:分别定义三角形、梯形和圆形类,要求每个类中包含构造方法、求周长及面积的方法。最后在主方法main中为三角形、梯形和圆形类创建对彖并打印输出各种图形的周长及面积。(一个文件中包含三个class禾口main)a)Trangle类具有类型为double的三个边,以及周长、面积属性,Trangle类具有返回周长、而积以及修改三个边的功能。另外,Trangle类述具有一个boolean型的属5、性,该属性用来判断三个属能否构成-个三角形。b)Lader类具有类型double的上底、下底、高、而积屈性,具冇返回而积的功能。c)Circle类具冇类型为double的半径、周长和面积属性,具有返回周长、面积的功能。packagekehoushiyan?;classTrangle{doublesideA^sideB,sideC^areaperimetre;booleanboo;publicTrangle(doublea^doubleb^doublec){sideA=a;sideB=b;sideC=c;//^fea6、£^sideA?sideB?sideCif(a+b>c&&b+c>a&&a+c>b)/人血“式三兰弓海二云乂{boo=true;}//^boo^^.else{bco=false;}//'^boo^i.・}doublegetPerimet厂e(){perimetre=sideA+sideB+sideC;returnperimetre;//方:H妾求丁理出楚吒旳士St叵}doublegetArea(){if(boo){doublep=(sideA+sideB+sideC)/2>0;area=Math■sqrt(p*(p7、-sideA)*(p-sideB)*(p-side€));returnarea;}else{System.out.println('>能构成一个三豹乡,无活计算贡評J;return0;classLader{doubleabove,bottom,height,area;publicLader(doublea,doubleb.,doubleh){aix)ve=a;bott(xii=b;height^h;Hu區tom』height}doublegetArea(){aw(above十bottom)・height/2;ret8、urnarea;II、"zareallclassCircle{doubleradius,area;Circle(doubler)//*e^{radius«r;}doublegetArea(){・return3.14*radius*radius;}doublegetPerimetre(){return2*3.14*radius;publicclasstes亡n
2、ht;//尸二W、:privat已.s^width^heightpublicRectangle(doublewidth》doubleheight){//二忙应芒亏,=?fwidthvh亡ight力书比撐this•width»width;thim.height-height;}publicdoublegetPerimeter(){I・%連亏性return2•(width♦height);}publicdoublegetArea(){returnwidth•height;2121222324252627282930}p
3、ublicstaticvoidmain(String[]args){Scan门皀「sc=newScanner(System・in);System,out•printInw);doublewidth«sc.nextDouble();doubleheight=sc.nextDouble();Rectangletyn=newRectangle(width,height);System•out.println(z:"+tyn•getPeriineter());System.out.println(,,*^?:ztr三:"+
4、tyn.getArea());}Rectai1.52.5毛弓半豆长三:8•02、创建三角形、梯形和圆形的类封装:分别定义三角形、梯形和圆形类,要求每个类中包含构造方法、求周长及面积的方法。最后在主方法main中为三角形、梯形和圆形类创建对彖并打印输出各种图形的周长及面积。(一个文件中包含三个class禾口main)a)Trangle类具有类型为double的三个边,以及周长、面积属性,Trangle类具有返回周长、而积以及修改三个边的功能。另外,Trangle类述具有一个boolean型的属
5、性,该属性用来判断三个属能否构成-个三角形。b)Lader类具有类型double的上底、下底、高、而积屈性,具冇返回而积的功能。c)Circle类具冇类型为double的半径、周长和面积属性,具有返回周长、面积的功能。packagekehoushiyan?;classTrangle{doublesideA^sideB,sideC^areaperimetre;booleanboo;publicTrangle(doublea^doubleb^doublec){sideA=a;sideB=b;sideC=c;//^fea
6、£^sideA?sideB?sideCif(a+b>c&&b+c>a&&a+c>b)/人血“式三兰弓海二云乂{boo=true;}//^boo^^.else{bco=false;}//'^boo^i.・}doublegetPerimet厂e(){perimetre=sideA+sideB+sideC;returnperimetre;//方:H妾求丁理出楚吒旳士St叵}doublegetArea(){if(boo){doublep=(sideA+sideB+sideC)/2>0;area=Math■sqrt(p*(p
7、-sideA)*(p-sideB)*(p-side€));returnarea;}else{System.out.println('>能构成一个三豹乡,无活计算贡評J;return0;classLader{doubleabove,bottom,height,area;publicLader(doublea,doubleb.,doubleh){aix)ve=a;bott(xii=b;height^h;Hu區tom』height}doublegetArea(){aw(above十bottom)・height/2;ret
8、urnarea;II、"zareallclassCircle{doubleradius,area;Circle(doubler)//*e^{radius«r;}doublegetArea(){・return3.14*radius*radius;}doublegetPerimetre(){return2*3.14*radius;publicclasstes亡n
此文档下载收益归作者所有