Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Sunday 15 July 2012

Define a class called Cartesian Point, which has two instance variables, x and y. Provide the methods get X() and get Y() 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 Test Cartesian Point, with the main method to test the various methods in the Cartesian Point class.


[ testcartesianpoint.java ]
import java.io.*;
import graphics.*;
class testcartesianpoint
{
public static void main(String args[])
{
CartesianPoint obj;
obj=new CartesianPoint(10); //CONSTRUCTOR WITH ONE PARAMETER
obj.display();

obj=new CartesianPoint(10,20); //CONSTRUCTOR WITH TWO PARAMETER
obj.display();

obj.move(30); //MOVE FUNCTION WITH ONE ARGUMENT
obj.display();

obj.move(30,40); //MOVE FUNCTION WITH TWO ARGUMENT
obj.display();
}
}

[ graphics.java ] 
(save it in graphics folder)

package graphics;
public class CartesianPoint
{
double x,y;
public CartesianPoint()
{
}
public CartesianPoint(double x)
{
System.out.println("CONSTRUCTOR WTIH ONE PARAMETER");
this.x=x;
this.y=x;
}
public CartesianPoint(double x,double y)
{
System.out.println("CONSTRUCTOR WTIH TWO PARAMETER");
this.x=x;
this.y=y;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
public void move(double x,double y)
{
System.out.println("MOVE METHOD WTIH ONE PARAMETER");
this.x=x;
this.y=y;
}
public void move(double x)
{
System.out.println("MOVE METHOD WTIH TWO PARAMETER");
this.x=x;
this.y=x;
}
public void display()
{
System.out.println("Value of x : " + x);
System.out.println("Value of y : " + y);
}
}


1 comment:

  1. GoTo Command Prompt(Win + R)

    HOW TO COMPILE
    javac c:\yourdir\graphics\graphics.java
    javac c:\yourdir\testcartesianpoint.java

    HOW TO EXECUTE
    java c:\yourdir\testcartesianpoint

    ReplyDelete