Java Program to text Cartesian Point


Define a class called CartesianPoint, which has two instance variables, x and y. Provide the
methods getX() and getY() to return the values of the x and y values respectively, a method
called move() which would take two integers as parameters and change the values of x and y
respectively, a method called display() which would display the current values of x and y. Now
overload the method move() to work with single parameter, which would set both x and y to thesame values, . Provide constructors with two parameters and overload to work with one
parameter as well. Now define a class called TestCartesianPoint, with the main method to test
the various methods in the CartesianPoint class.

//import CartesianPoint.java;



Program:


TestCartesianPoint.java 
public class TestCartesianPoint
{
  public static void main(String args[])
  {
    System.out.println("Default Constructor: ");
    CartesianPoint cp = new CartesianPoint();

    System.out.println("From get methods: ");
    System.out.println("x ="+cp.getX()+" y = "+cp.getY());

    int a = cp.getX();
    int b = cp.getY();
    System.out.println("From move method: ");
    cp.move( a, b);

    System.out.println("From display method: ");
    cp.display();

    System.out.println("From move method with one param: ");
    cp.move(9);

    System.out.println("Constructor with two param: ");
    CartesianPoint cp1 = new CartesianPoint(a, b);

    System.out.println("Constructor with one param: ");
    CartesianPoint cp2 = new CartesianPoint(10);
  }
}

CartesianPoint.java

package edu.gtu.geometry;

class CartesianPoint
{

    double x=4, y=5;

    CartesianPoint(int p, int q)
    {
        x = p;
        y = q;
        display();
        //System.out.println("x ="+x+" y = "+y);
      
    }

    CartesianPoint(int z)
    {
        x = y = z;
        display();
        //System.out.println(x);
        //System.out.println(y);
    }

    CartesianPoint()
    {
        //x = z;
        display();
    }

    public double getX()
    {
        return x;
    }
    public double getY()
    {
        return y;
    }

    public void move(double a, double b)
    {
      
        x = a;
        y = b;
        //display();
        //System.out.println("x ="+x+" y = "+y);
    }  
   
    public void display()
    {
        System.out.println("x ="+x+" y = "+y);
    }

    public void move(double c)
    {
        x = y = c;
        //display();
        //System.out.println("x ="+x+" y = "+y);
    }

   

}


Comments

Popular Posts