XYPoint

In this program you'll write a basic instantiable class, the XYPoint class. You'll also write code in your main class that will create and use several XYPoint objects.

  1. Go into the CS labs (see this map for details) and log onto a Windows machine.
  2. Start up a new project, called XYPoint, with a main class called Main.java. Refer to Lesson 2 if you are unsure how to do this. You do not need to include javabook2.jar in your class files. But be sure to delete TrivialApplication.java and set Main.java as the main file.
  3. In the blank file Main.java, write the following code:
  4. public class Main {
    
    	public static void main(String[] args) {
    
    	}
    
    }
    
  5. Create another new blacnk text file called XYPoint.java, and add it to the project. You should not set it as the main class, since it is not.
  6. In XYPoint.java, type in the following code:
  7. public class XYPoint {
    
    	private int x;
    	private int y;
    
    	public XYPoint(int x, int y) {
    		setX(x);
    		setY(y);
    	}
    
    	public XYPoint() {
    		this(0,0);
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
            public static double distance(XYPoint p1, XYPoint p2) {
                    return Math.sqrt( Math.pow( p1.getX()-p2.getX() ,2) +
                                      Math.pow( p1.getY()-p2.getY() ,2));
            }
    
    	public void setX(int x) {
    		this.x = x;
    	}
    
    	public void setY(int y) {
    		this.y = y;
    	}
    
    	public String toString() {
    		return "(" + getX() + "," + getY() + ")";
    	}
    
    }
    
  8. Now go back to Main.java, and add the following code:
  9. public class Main {
    
    	public static void main(String[] args) {
    
    		XYPoint a = null;
    		System.out.println(a);
    
    		a = new XYPoint();
    		System.out.println(a);
    
    		a = new XYPoint(0,0);
    		System.out.println(a);
    
    		a = new XYPoint(3,4);
    		System.out.println(a);
    
    		System.out.println(XYPoint.distance(a,a));
    
    		int x = a.getX();
    		int y = a.getY();
    		System.out.println("(" + x + "," + y + ")");
    		
    		XYPoint b = a;
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    		a.setX(15);
    		System.out.println(a);
    		System.out.println(b);
    
    		b.setY(32);
    		System.out.println(a);
    		System.out.println(b);
    
    		b.setX(a.getY());
    		System.out.println(a);
    		System.out.println(b);
    
    		b = new XYPoint(a.getX(),b.getY());
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    		a.setX(15);
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    		b.setY(32);
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    		b.setX(a.getY());
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    		a = null;
    		System.out.println(a);
    		System.out.println(b);
    
    		a = b;
    		System.out.println(a);
    		System.out.println(b);
    		System.out.println(XYPoint.distance(a,b));
    
    	}
    
    }
    
  10. Compile and run your program. You should get the following output:
  11. null                                    
    (0,0)
    (0,0)
    (3,4)
    0.0
    (3,4)
    (3,4)
    (3,4)
    0.0
    (15,4)
    (15,4)
    (15,32)
    (15,32)
    (32,32)
    (32,32)
    (32,32)
    (32,32)
    0.0
    (15,32)
    (32,32)
    17.0
    (15,32)
    (32,32)
    17.0
    (15,32)
    (32,32)
    17.0
    null
    (32,32)
    (32,32)
    (32,32)
    0.0
    
  12. Press enter, as the console window says, and the black console window will disappear.