Saturday 15 September 2012

Java Practical - 5


    Program Definition 5 ::

     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 the same 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.




class CartesianPoint
{
int x, y;
CartesianPoint(int a)
{
x = y = a;
}
CartesianPoint(int a , int b)
{
x = a; y = b;
}

protected int getX()
{
return x;
}
protected int getY()
{
return y;
}
protected void move(int a)
{
x = y = a;
}
protected void move(int a, int b)
{
x = a; y = b;
}

public void display()
{
System.out.println(" point(x, y) = (" + x +", " + y + ")");
}
}

class TestCartesian
{
public static void main(String args[])
{
CartesianPoint cp1 = new CartesianPoint(5,10);
CartesianPoint cp2 = new CartesianPoint(5);

System.out.println(" point1 = >> " );
cp1.display();
System.out.println(" point2 = >> " );
cp2.display();

cp1.move(5);
cp2.move(6,10);

System.out.print(" After cp1.move(5); point 1 =>>  ");
cp1.display();
System.out.print(" After cp2.move(6,10); point 2 =>>  ");
cp2.display();
}
}

/*
D:\JAVA >javac Prog5.java

D:\JAVA >java TestCartesian
 point1 = >>
 point(x, y) = (5, 10)
 point2 = >>
 point(x, y) = (5, 5)
 After cp1.move(5); point 1 =>>   point(x, y) = (5, 5)
 After cp2.move(6,10); point 2 =>>   point(x, y) = (6, 10)
*/


Kindly Bookmark and Share it:

0 comments :

Post a Comment

Any Query ? any suggestion ? comment here

 

Recent Post

Recent Comments

© 2010 IamLearningHere Template by MBT