Java Classroom Exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.