/** * Point3d is one of the simplest possible classes we will * show off in this course. This is a newer version of * Point3d that shows off the toString() method. Try * playing with this class in the interactions pane; * then comment out the toString() method below and see * how things are different. * * Walker White * February 2, 2012 */ public class Point3d { /* These are the three coordinates. */ private double x; // x coordinate; no restrictions private double y; // y coordinate; no restrictions private double z; // z coordinate; no restrictions /** * Constructor: a point at the origin * Precondition: None */ public Point3d() { x = 0.0; y = 0.0; z = 0.0; } /** * Constructor: a point with given coordinates (x0,y0,z0) * Precondition: None */ public Point3d(double x0, double y0, double z0) { x = x0; y = y0; z = z0; } /** Yields: this Point's x coordinate */ public double getX() { return x; } /** Assigns value to x coordinate */ public void setX(double value) { x = value; } /** Yields: this Point's y coordinate */ public double getY() { return y; } /** Assigns value to y coordinate */ public void setY(double value) { y = value; } /** Yields: this Point's z coordinate */ public double getZ() { return z; } /** Assigns value to z coordinate */ public void setZ(double value) { z = value; } /** * Yields: Distance to point q * This method demonstrates that a method in one object can access * private fields in an object of the SAME CLASS. */ public double distanceTo(Point3d q) { return Math.sqrt( (x-q.x)*(x-q.x)+ (y-q.y)*(y-q.y)+ (z-q.z)*(z-q.z) ); } /** * Yields: String representing this point in the form (x,y,z) * As long as the method is here, we cannot see the "folder * name" for this object in the interactions pane. */ public String toString() { return "("+x+","+y+","+z+")"; } }