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 PointD class that we already defined.
public class LineSegment
TODO: two PointD 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.
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
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.
Data Fields
A LineSegment object has a start point
This is a PointD object
All data will be private
A LineSegment object also has an end point.
Also a PointD object
This is called class composition, where objects of one class contain objects of another class as instance variables.
Constructors
A constructor with no parameters.
Sets the start point and the end point to the origin
A constructor that takes a start point and an end point as arguments.
This should check that the start and end points are nonnull 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 PointD copy constructors.
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 PointD copy constructor.
This prevents privacy leaks. Read more about Privacy Leaks in Section
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 PointD class to get the coordinates for each point.
public boolean equalsObject other
A method that determines if two LineSegments are equal
ifother null other instanceof LineSegment return false; use this as the first line
LineSegment that LineSegment other; after this line, use this vs that
return start and end points are equal, requires an equals in the PointD 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
