欢迎来到天天文库
浏览记录
ID:35342202
大小:56.16 KB
页数:4页
时间:2019-03-23
《实验二:java继承和包》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、计算机科学系实验报告实验二:java继承和包实验口期:2010年10月8口一、实验目的1.理解面向对象编程中的继承;2.掌握Java语言的继承用法。二、实验仪器设备与实验环境条件PC机WindowsXPInternet连接三、实验内容、步骤、结果内容:Java继承和包编程下图显示的是一些常见的儿何形状,但在现实中除此以外还有许多其他的儿何形状。请使用Java语言创建您所知道的几何形状,包括二维和三维的,它们一起构成完整的形状树。要求:Shape类是所有形状的顶级基类。TwoDimensionalShape和ThreeDimensionalShape类必须继承Shape类。在适当的层次位置增加
2、您所知道的其他类,女口Quadrilateral和Sphere等。所有类都位于java.shapes包。源代码如下://Shape类packageMyShape;publicclassShape{publicShape(){return;}}//TwoDimensionalShape类packageMyShape;publicclassTwoDimensionalShapeextendsShape{doublelength,width,radius;TwoDimensionalShape(doublea,doubleb){length=a;width=b;}TwoDimensionalSha
3、pe(doubler){radius=r;}TwoDimensionalShape(){return;}1//ThreeDimensionalShape类packageMyShape;publicclassThreeDimensionalShapeextendsShape)doublelength,width,high,radius;ThreeDimensionalShape(doublea,doubleb,doublec){length=a;width=b;high=c;)ThreeDimensionalShape(doubler){radius=r;}ThreeDimensionalSh
4、ape(){return;)}//Circle类packageMyShape;publicclassCircleextendsTwoDimensionalShape{doubleZhuoChang(doubler){return2*3.14*r;}doublearea(doubler){return3」4*r*r;}}//Square类packageMyShape;publicclassSquareextendsTwoDimensionalShape{doubleZhuoChang(doublea,doubleb){return2*(a+b);}doublearea(doublea,doub
5、leb){returna*b;}}//Sphere类packageMyShape;publicclassSphere{doubleSurfaceArea(doubler){return3.14*r*r;}doubleVolume(doubler){return4*3.14*r*r*r/3;}}//Cube类packageMyShape;publicclassCubeextendsThreeDimensionalShape{doubleSurfaceArea(doublea,doubleb,doublec){return2*(a*b+b*c+a*c);}doubleVolume(doublea
6、,doubleb,doublec){returna*b*c;}//Mymain类packageMyShape;importjava.io.BufferedReader;importjava.io.InputStreamReader;publicclassMymain{publicstaticvoidmain(String[]args)throwsjava.io.IOException{System.out.println(HHello!H);System.out.println(HifyouwanttoalterTwoDimensionalShape,Pleaseinput2;”);Syst
7、em.out.println(nifyouwanttoalterThreeDimensionalShape,Pleaseinput3.n);BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));intnumber=Integer.parseInt(in.readLine());if(number==2){System.out.p
此文档下载收益归作者所有