Computer Science 225
Advanced Programming
Spring 2017, Siena College
Point BlueJ Project
Click here to download a BlueJ project for Point.
Point Source Code
The Java source code for Point is below. Click on a file name to download it.
/*
* Example Point: an example of a class, used to demonstrate some
* terminology and as a basis for UML class diagram examples
*
* Jim Teresco, Siena College, Computer Science 225, Spring 2017
*
* $Id: templateapp.java 2975 2016-08-30 02:35:29Z terescoj $
*/
public class Point {
/**
* the coodinates of our points
*/
private double x, y, z;
/* static constant to establish a max allowed value */
public static final double MAX_COORD = 1000.0;
/**
* default constructor
*/
public Point() {
this(0.0, 0.0, 0.0);
}
/**
* construct a new Point with the given coordinates
*/
public Point(double x, double y) {
this(x, y, 0.0);
// here, we might be tempted to write:
// x = x;
// y = y;
// but the instance variables x and y are shadowed (important term)
// by the formal parameters x and y
// option 1: we can use the this keyword to force Java to use the
// instance variable versions of these names
//this.x = x;
//this.y = y;
//z = 0.0;
// a second option is to rename the formal parameters to eliminate
// the name conflict that caused the shadowing
}
/**
* construct a new Point with the given coordinates
*/
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double distanceToOrigin() {
return Math.sqrt(x*x + y*y + z*z);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public void setX(double newX) {
x = newX;
}
public void setY(double newY) {
y = newY;
}
public void setZ(double newZ) {
z = newZ;
}
public static void main(String[] args) {
Point p = new Point(3,4);
System.out.println(p.distanceToOrigin());
p = new Point(10, 10, 10);
System.out.println(p.distanceToOrigin());
p = new Point();
System.out.println(p.distanceToOrigin());
}
}