You are on page 1of 3

#Point and Line Implementation

*Point

package pointnline;

public class Point {


private int x ;
private int y ;

Point (int x, int y)


{
this.x = x ;
this.y = y ;
}
Point ()
{
this(0,0);
}
void setX (int i)
{
this.x = i ;
}
void setY (int j)
{
this.y = j ;
}
int getX ()
{
return this.x ;
}
int getY ()
{
return this.y ;
}

public String toString ()


{
return "x = " + this.x + ", y = " + this.y ;
}

public double distance (Point p)


{
int x2 = p.getX() ;
int x1 = this.x ;
int y2 = p.getY() ;
int y1 = this.y ;
return Math.sqrt(Math.pow(x2 -x1, 2) + Math.pow(y2 - y1, 2)) ;
}

}
*Line

package pointnline;

public class line{


private Point start;
private Point end;

line (Point start, Point end){


this.start=start;
this.end=end;
}
line (int x1,int x2,int y1,int y2){
this(new Point(x1,y1),new Point(x2,x2));
}
public Point getStart() {
return start;
}
public Point getEnd() {
return end;
}
public void setStart(Point start) {
this.start=start;
}
public void setEnd(Point end) {
this.end=end;
}
public double length() {
return start.distance(end);
}
}

*Main
package pointnline;

public class Main{


public static void main(String[] args) {
Point p1=new Point(1,2);
Point p2= new Point(3,5);
line ln= new line(p1,p2);

System.out.println("Point 1:" + p1);


System.out.println("Point 2:"+ p2);
System.out.println("Line start:" + ln.getStart());
System.out.println("Line end:" + ln.getEnd());
System.out.println("Line length: "+ ln.length());

}
}
Output:

You might also like