Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following constructor to your Line class from the preceding exercises: C

ID: 3564628 • Letter: A

Question

Add the following constructor to your Line class from the preceding exercises:

Constructs a new line that contains the given two points.

(You don't need to write the class header or declare the fields; assume that this is already done for you. Just write your constructor's complete code in the box provided.) See the previous exercises for a description of the Line class and its public members.

the previous excercise result:

public class Line{

private Point point1, point2;

public Line(Point p1, Point p2){
point1 = p1;
point2 = p2;
}

public Point getP1(){
return point1;
}

public Point getP2(){
return point2;
}

public String toString(){
return "[(" + point1.x + ", " + point1.y + "), (" + point2.x + ", " + point2.y +")]";
}
  public double getSlope() throws IllegalStateException
{
if((getP2().getX()-getP1().getX())==0)
throw new IllegalStateException("Undefined Slope");
else
return (double)(getP2().getY()-getP1().getY())/(double)(getP2().getX()-getP1().getX());
}
}

Explanation / Answer

//Line constructor is added to the below Line and

//an error in toString is also corrected.

//Compile :javac Line.java

//Run : java Line

import java.awt.Point;

public class Line
{
   private Point point1, point2;
   //The parameterized Line constructor that accepts x1,y1,x2 and y2 as its
   //arguments and instantiates the point1 object with (x1,y1)
   //and instantiates the point1 object with (x2,y2)
   public Line(int x1,int y1,int x2,int y2)
   {
       //Instantiate the point1 object
       point1=new Point(x1, y1);
       //Instantiate the point2 object
       point2=new Point(x2, y2);
   }
  
  
   public Line(Point p1, Point p2)
   {
       point1 = p1;
       point2 = p2;
   }

   public Point getP1()
   {
       return point1;
   }

   public Point getP2()
   {
       return point2;
   }

   //Error modified:
   //Modified this toString method point1.getX() returns the x value of point1 class.
   //and similarly for point1.getY() ,point2.getX() and point2.getY()
   //point1.x,point1.y ,point2.x and point2.y will throw an errors .
   public String toString()
   {
       return "[(" + point1.getX() + ", " + point1.getY() + "), " +
               "(" + point2.getX() + ", " + point2.getY() +")]";
   }
   public double getSlope() throws IllegalStateException
   {
       if((getP2().getX()-getP1().getX())==0)
           throw new IllegalStateException("Undefined Slope");
       else
           return (double)(getP2().getY()-getP1().getY())/(double)(getP2().getX()-getP1().getX());
   }
}

--------------------------------------------------------------------------------------------------------------------------

//Tester class of the Line Construtor

//LineTester.java

//Compile :javac LineTester.java

//Run:java LineTester
public class LineTester
{
   public static void main(String[] args)
   {
      
       //Construct a line object with Line with four integer arguments
       Line line1=new Line(0, 0, 5, 5);
       System.out.println(line1.toString());
      
       //Construct a line object with Line with four integer arguments
       Line line2=new Line(10, 10, 50, 50);
       System.out.println(line2.toString());  
   }
}

----------------------------------------------------------------------------------------------------

Sample Output:

[(0.0, 0.0), (5.0, 5.0)]
[(10.0, 10.0), (50.0, 50.0)]

Hope this would helpful to you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote