资源描述:
《java程序类方法的使用.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、TheSecondExperiment—JavaObject-OrientedprogrammingQ1.DesignaclassnamedPoint3D,whichisthesubclassofPoint2Ddiscussedintheclass.Besidesthecoordinates’variables,themethodsthatsupportcoordinates’setting/getting,distance’calculationandcoordinates’shiftingshouldbeprovided.Javacodes:packageoop_java;publicc
2、lasspoint2D{protecteddoublex;protecteddoubley;voidPoint2D(doublex2,doubley2){x=0;y=0;}point2D(doublex,doubley){this.x=x;this.y=y;}//Thesearetwoconstructorsandtheyareusedtoinitialize.publicdoubleDistance(){returnMath.sqrt(x*x+y*y);}}packageoop_java;publicclasspoint3Dextendspoint2D{//Thepoint3dhavein
3、heritedpoint2D.privatedoublez;point3D(doublex,doubley,doublez){super(x,y);//Point3Dinvokedtheconstructorofpoint2Dbyusing“super”.this.z=z;//Thevariable“z”needtobeinitializedinpoint3D.}publicvoidsetxy(doublex,doubley,doublez){this.x=x;this.y=y;this.z=z;}//Thisisthemethodofsetting.publicdoublegetx(){r
4、eturnx;}publicdoublegety(){returny;}publicdoublegetz(){returnz;}//Thesearemethodsofgetting.publicdoubleDistance(){returnMath.sqrt(x*x+y*y+z*z);}//ThemethodofDistancecangettheresultofthedistanceoftwopoints.publicdoubleDistance(doublea,doubleb,doublec){returnMath.sqrt((a-x)*(a-x)+(b-y)*(b-y)+(c-z)*(c
5、-z));}//ThisistheoverloadingofDistance.Wecangetthedistanceof(x,y,z)and(a,b,c).publicvoidsymmetry(doubleh,doublei,doublej){x=-h;y=-i;z=-j;}//Itisthemethodtogetthecoordinatesaftersymmetryby(0.0).publicvoidshift(doubled,doublee,doublef){x=x+d;y=y+e;z=z+f;}}packageoop_java;publicclassusepoint{publicsta
6、ticvoidmain(String[]args){point2Dp1=newpoint2D(2.3,4.1);System.out.println("thedistancebetweenp1and(0.0)is"+""+p1.Distance());point3Dp2=newpoint3D(3.2,2.1,5.2);//Wecandefineanewobjectbyusing“new”.System.out.println("thedistancebetweenp2and(0.0)is"+""+p2.Distance());point3Dp3=newpoint3D(2.1,3.4,1.9)
7、;System.out.println("thedistancebetweenp2andp3is"+""+p2.Distance(2.1,3.4,1.9));point3Dp4=newpoint3D(2.0,3.0,4.0);p4.symmetry(2.0,3.0,4.0);System.out.println("thecoordinatesaftersymmetryis"+""+"("+p4.getx()+