Question: Next, we will design a class to represent a line segment in an ( x , y ) coordinate system. You can describe a line

Next, we will design a class to represent a line segment in an (x, y) coordinate system. You can describe a line segment by specifying the x and y coordinates of its endpoints. Instead of writing code to handle the x and y coordinates for both endpoints, we can just use the Point2D class that we already defined.
public class LineSegment {
//TODO: two Point2D variables for the start and end points
}
Implement the fields, constructors, and methods listed below.
Uncomment and run the lineSegmentDriver method in ClassDesignIIDriver to test your code.
3.1 Class Constraints
The instance variables of a line segment are never null.
The start and end points of a line segment always have nonnegative coordinates (x, y >=0).
The first is important because reference variables like start can have a value of null, which means that they don't point to an object yet. The second is useful for displaying lines in the Java graphics framework, where shapes with negative coordinates will not appear on the screen.
3.2 Data Fields
A LineSegment object has a start point
This is a Point2D object
All data will be private
A LineSegment object also has an end point.
Also a Point2D object
This is called class composition, where objects of one class contain objects of another class as instance variables.
3.3 Constructors
A constructor with no parameters.
Sets the start point and the end point to the origin (0,0).
A constructor that takes a start point and an end point as arguments.
This should check that the start and end points are non-null and have nonnegative coordinates.
A copy constructor that takes a LineSegment object and initializes this using other.
Set this LineSegment to have points with the same coordinates as the points of other.
Use the Point2D copy constructors.
3.4 Methods
Create getters and setters for your start and end points.
For your getters: Don't just return this.end or this.start, use the Point2D copy constructor.
This prevents privacy leaks. Read more about Privacy Leaks in Section 3.5
String toString()
Returns a string representation of this line segment with the format "Line start: (x, y) end: (x, y)".
Call the toString method from your Point2D class to get the coordinates for each point.
public boolean equals(Object other)
A method that determines if two LineSegments are equal
if(other == null ||!(other instanceof LineSegment)) return false; //use this as the first line
LineSegment that =(LineSegment) other; //after this line, use this v.s. that
//return start and end points are equal, requires an equals in the Point2D class
double getDistance()
Returns the Euclidean distance between the start point and end point. Use the functions Math.sqrt and Math.abs in your formula.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!