Defines a point class that represents a point in a three-dimensional space (with three coordinates) and requires the following:
1, you can generate a point object with a specific coordinate (construction method);
2, provide a method that can set three coordinates (member method);
3. Provide a method (member method) to calculate the distance from the pity Dorado to another point;
The code is as follows:
Note: The file name is Testpoint.java (main Class)
Public classpoint{Doublex, Y, Z //Construction Method PublicPoint (Double_x,Double_y,Double_z) {x=_x; Y=_y; Z=_z; } //A method that can "set three coordinates" voidSetX (Double_x) {x=_x; } voidSety (Double_y) {y=_y; } voidSetz (Double_z) {Z=_z; }
//method for calculating the square of the average distance of the point from other points (p1) Public Doublegetdistance () {return(x-p.x) * (x-p.x) + (Y-P.Y) * (Y-P.Y) + (z-p.z) * (Z-p.z); } Public classtestpoint{ Public Static voidMain (string[] args) {point P=NewPoint (1.0, 2.0, 3.0);//New ObjectPoint P1 =NewPoint (0.0, 0.0, 0.0);//New Origin ObjectSystem.out.println (P.getdistance (p1));//method of calling the object P (distance of P point from P1 point) }
//recalculate the coordinates of P points from other points after changing the coordinates of x pointsP.setx (5.0); System.out.println (P.getdistance (NewPoint (1.0, 1.0, 1.0))); } }
Java Classroom Exercises