Thursday, October 30, 2014

Java Point Pro



Point in java program

public class Point 
{
 private int x, y;   
 public Point(int x, int y) 
{
      this.x = x;
      this.y = y;
   }
  public Point() 
 {    
      x = 0;
      y = 0;
  }
  public int getX() 
 { 
      return x; 
  }
  public void setX(int x) 
  { 
      this.x = x; 
   }
   public int getY() 
  { 
      return y; 
   }
   public void setY(int y) 
  { 
      this.y = y; 
   }
   public String toString() 
   { 
      return "(" + x + "," + y + ")"; 
   }
}
Line in java program

public class Line 
{
Point begin, end;   
public Line(int x1, int y1, int x2, int y2) 
{
      begin = new Point(x1, y1);  
      end   = new Point(x2, y2);
  }
 public Line(Point begin, Point end) 
{
      this.begin = begin;  
      this.end   = end;
  }
 public Point getBegin() 
 {
      return begin;
   }
   public Point getEnd() 
  {
      return end;
   }
   public void setBegin(Point begin) 
   {
      this.begin = begin;
   }
   public void setEnd(Point end) 
  {
      this.end = end;
   }
   public int getBeginX() 
  {
      return begin.getX();
   }
   public void setBeginX(int x) 
  {
      begin.setX(x);
   }
   public int getBeginY() 
  {
      return begin.getY();
   }
   public void setBeginY(int y) 
  {
      begin.setY(y);
   }
   public void setBeginXY(int x, int y) 
  {
      begin.setX(x);
      begin.setY(y);
   }
   public int getEndX() 
  {
      return end.getX();
   }
   public void setEndX(int x) 
  {
      end.setX(x);
   }
   public int getEndY() 
   {
      return end.getY();
   }
   public void setEndY(int y) 
  {
      end.setY(y);
   }
   public void setEndXY(int x, int y) 
  {
      end.setX(x);
      end.setY(y);
   }
   public String toString() 
   {
      return "Line from " + begin + " to " + end;
    }
   public double getLength() 
  {
      int xDiff = begin.getX() - end.getX();
      int yDiff = begin.getY() - end.getY();
      return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
   }
}
Test Line in java
 
                public class TestLine 
{
 public static void main(String[] args) 
{
      Line l1 = new Line(0, 3, 4, 0);
      System.out.println(l1);   
      System.out.println(l1.getLength());
      l1.setBeginXY(1, 2);
      l1.setEndXY(3, 4);
      System.out.println(l1);
      Point p1 = new Point(3, 0);
      Point p2 = new Point(0, 4);
      Line l2 = new Line(p1, p2);
      System.out.println(l2);
      System.out.println(l2.getLength());
      l2.setBegin(new Point(5, 6));
      l2.setEnd(new Point(7, 8));
      System.out.println(l2);
   }
}


No comments:

Post a Comment