The-Second-Experiment-—-Java-OOP.docx
文本预览下载声明
The Second Experiment — Java OOP1441906221毛君宇Q1. Expand the class of Point2D discussed in the class, make it stronger and more powerful, besides the existing methods of Point2D, three more methods must be designed and implemented. Code:package e2;class E2Q1 {privatedoublex;privatedoubley;E2Q1() {x=0;y=0;}E2Q1(doublex,doubley){this.x=x;this.y=y;}publicdouble Distance() {returnMath.sqrt(x*x+y*y);}publicdoublex_Distance() {returnx;}publicdoubley_Distance() {returny;}publicstaticvoid main(String[]args){E2Q1 p1=newE2Q1(3.1,5.4);System.out.println(点到原点的距离为:+p1.Distance());System.out.println(点到X轴的距离为:+p1.x_Distance());System.out.println(点到Y轴的距离为:+p1.y_Distance());}}Q2. Design a class named Point3D, which is the subclass of your Point2D class, to describe the 3-D coordinate system.Code:package e2;publicclass E2Q2 extends E2Q1{publicdouble Distance(doublex,doubley,doublez){returnMath.sqrt(x*x+y*y+z*z);}publicdoublexy_Distance(doublex,doubley,doublez){returnz;}publicdoubleyz_Distance(doublex,doubley,doublez){returnx;}publicdoublexz_Distance(doublex,doubley,doublez){returny;}publicstaticvoid main(String[] args){E2Q2 a=newE2Q2();System.out.println(点到原点的距离为:+a.Distance(3,4,5));System.out.println(点到XY平面的距离为:+a.xy_Distance(3,4,5));System.out.println(点到XZ平面的距离为:+a.xz_Distance(3,4,5));System.out.println(点到YZ平面的距离为:+a.yz_Distance(3,4,5));}}Q3. Design a class that provides the methods below: 3.1 check whether an integer is an even number. 3.2 check whether a value is positive. 3.3 return the times that a specified substring appears in a string. 3.4 sort an array by bubble and selection algorithms. 3.5 left shift an array by K bits, i.e., left shift the array [2, 4, 6, 8] by 2 bits, the array becomes [6, 8, 2, 4]. 3.6 right shift an array by K bits, just like 3.5. 3.5 and 3.6 must return the shifted array. 3.4, 3.5 and 3.6 must employ overloading to fit the arrays which have different data types, such as int, double, float, etc. 3.7 check whether an integer is a palindro
显示全部