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

This is a java data structure is a data structure problem , not a program. Write

ID: 663171 • Letter: T

Question

This is a java data structure is a data structure problem , not a program.

Write a class called Line that represents a line segment between two Points. Your Line objects should have the following methods:

Constructs a new line that contains the given two points.

Returns this line's first endpoint.

Returns this line's second endpoint.

Returns a string representation of this line, such as "[(22, 3), (4, 7)]".

Add the following method to your Line class from the previous exercise:

Returns the slope of this line. The slope of a line between points (x1, y1) and (x2, y2) is equal to (y2 - y1) / (x2 - x1). If the two points have the same x-coordinates, the denominator is zero and the slope is undefined, so you should throw an IllegalStateException in this case.

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

Constructs a new line that contains the given two points.

Requirement for best answer:

Data fields listed at top of Class, and they are all private.

Fields are not initialized in these private declarations.

Only exception to above are public static final constants, which never change.

Constructors (that's plural) follow the data field specifications, and those initialize all the fields.

Always have a default constructor that defines the assumed defaults for all fields.

No redundant constructor code, the multiple argument version is used for others with this() calls.

No redundant method code, there fore a detailed math formula is in one place.

Accessor methods are grouped together. Mutators grouped together. No random placements in Class.

System.out calls is removed.

Explanation / Answer

class Point

{

private int x;

private int y;

  

   Point(int x, int y)

   {

       this.setX(x);

       this.setY(y);

   }

   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 class Line {

  

   private Point p1;

   private Point p2;

  

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

   {

       p1 = new Point(x1, y1);

       p2 = new Point(x2, y2);

   }

  

   public Line(Point p1, Point p2)

   {

       this.setP1(p1);

       this.setP2(p2);

   }

   public Point getP1() {

       return p1;

   }

   public void setP1(Point p1) {

       this.p1 = p1;

   }

   public Point getP2() {

       return p2;

   }

   public void setP2(Point p2) {

       this.p2 = p2;

   }

  

   public String toString()

   {

       return "[("+p1.getX()+","+p1.getY()+"),("+p2.getX()+","+p2.getY()+")]";

   }

  

   public double getSlope() throws IllegalStateException

   {  

       if(p1.getX() == p2.getX())

           throw new IllegalStateException();

      

       return (p2.getY() - p1.getY())/(p2.getX() - p1.getX());

   }

}

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