Java

OOP GUIDE / WORK

 

LINE CLASS 2 SOLUTIONS

 

 

TASK – PART 1 – SETUP – LINE CLASS & POINT CLASS

 

No work

 

 

TASK – PART 2 – USE ENCAPSULATION ON THE POINT CLASS

 

Here is my encapsulated Point class:

 

public class Point

{

     private double x;

     private double y;

    

     public Point(double x, double y)

     {

           this.x = x;

           this.y = y;

     }

    

     public double getX()

     {

           return x;

     }

    

     public double getY()

     {

           return y;

     }

    

     public void setX(double newx)

     {

           x = newx;

     }

    

     public void setY(double newy)

     {

           y = newy;

     }

}

 

 

 

TASK – PART 3 – USE ENCAPSULATION ON THE LINE CLASS

 

Here is my encapsulated Line class:

 

 

public class Line

{

     private Point p1;

     private Point p2;

    

     public Line(double x1, double y1, double x2, double y2)

     {

           p1 = new Point(x1, y1);

           p2 = new Point(x2, y2);

     }

    

     public Point getP1()

     {

           return p1;

     }

    

     public Point getP2()

     {

           return p2;

     }

    

     public void setP1(Point newP1)

     {

           p1 = newP1;

     }

    

     public void setP2(Point newP2)

     {

           p2 = newP2;

     }

}

 

 

 

TASK – PART 4 – TOSTRING METHODS

Here is my toString() method for the Point class:

 

     public String toString()

     {

           return "(" + x + ", " + y + ")";

     }

 

Here is my toString() method for the Line class:

 

      public String toString()

     {

           return p1 + " to " + p2;

     }

 

Notice that in the Line class’ toString() method, when we return p1 and p2 as part of a String concatenation, we are actually calling the toString() from the Point class.  So that is the same as:

 

     public String toString()

     {

           return p1.toString() + " to " + p2.toString();

     }

 

 

TASK – PART 5 – TESTING THE LINE CLASS

 

Do the following:

 

a)    Create a Line object that has points (4,2) and (9,1).

b)    Output the Line object to screen.

c)     Write the statement needed to output x1 to screen.  (It should be 4.)

d)    Write the statement needed to change y2 to 13 and then output the line to screen.

e)    Output the first point of the line to screen.

f)      Change p1 so that it is now at (2,3).  Output the line to screen.

Here is my tester class:

 

 

public class LineTester

{

   public static void main(String[] args)

   {

      //A

      Line l1 = new Line(4, 2, 9, 1);

      

      //B

      System.out.println(l1);

      

      //C

      double x = l1.getP1().getX();

      System.out.println(x);

      

      //C (option 2)

      System.out.println(l1.getP1().getX());

      

      //D

      l1.getP2().setY(13);

      System.out.println(l1);

      

      //E

      Point p = l1.getP1();

      System.out.println(p);

      

      //E (option 2)

      System.out.println(l1.getP1());

      

      //F

      l1.setP1(new Point(2,3));

      System.out.println(l1);

      

      //F (option 2)

      Point tmp = new Point(2,3);

      l1.setP1(tmp);

      System.out.println(l1);

      

      //F (option 3)

      l1.getP1().setX(2);

      l1.getP2().setY(3);

      System.out.println(l1);

   }

}