The Point2D should store an x and y coordinate pair, and will be used to build a new class via class composition. A Point2D has a x and a y, while a LineSegment has a start point and an end point (both of which are represented as Point2Ds).
The Point2D should store an x and y coordinate pair, and will be used to build a new class via class composition. A Point2D has a x and a y, while a LineSegment has a start point and an end point (both of which are represented as Point2Ds).
class Invariants
- The start and end points of a line segment should never be null
- Initialize these to the origin instead.
Data
- A LineSegment has a start point
- This is a Point2D object
- All data will be private
- A LineSegment also has an end point.
- Also a Point2D object
Methods
- Create getters and setters for your start and end points
- public Point2D getStartPoint() {
- public void setStartPoint(Point2D start) {
- Create a toString() function to build a string composed of the startPoint’s toString() and endPoint’s toString()
- Should look like “Line start(0,0) and end(1,1)”
- Create an equals method that determines if two LineSegments are equal
- public boolean equals(Object other) {
- 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
- public boolean equals(Object other) {
- Uncomment the method call in ClassDesignIIDriver.java to invoke the driver associated with the LineSegment code.
- Fix each error as you encounter them in the driver for LineSegment
- Create a default, no-arg constructor
- This should define the start point and the end point to be at the origin
- Create an overloaded constructor that takes a start point and an end point
- This should check for null’s for the start and end point
- Create a copy constructor (also overloaded) that takes a LineSegment object and initializes this using other.
- public LineSegment(LineSegment other) {
- Create a distance() function that will calculate the line distance using the distance formula
- Hint: Math.sqrt(), Math.abs()
- Run the driver code in ClassDesignIIDriver.java to test your LineSegment class.
- Answer the following questions as comments in your LineSegment code:
- What is a privacy leak?
- Do your getters or setters have privacy leaks?
- Where else could a privacy leak occur?
Driver:
/*LineSegment a = new LineSegment();
LineSegment b = new LineSegment(new Point2D(1,1), new Point2D(2,2));
LineSegment c = new LineSegment(b);
a.setStartPoint(new Point2D(3,3));
a.setEndPoint(new Point2D(4,4));
System.out.println("Line a: " +a.toString());
System.out.println("Line b: " + b.toString());
System.out.println("Line c: " + c.toString());
System.out.println("Line b's distance between points: " + b.distance());
System.out.println("Does a equal b? " + a.equals(b));
System.out.println("Does a equal c? " + a.equals(c));
System.out.println("Does b equal c? " + b.equals(c));
Sample Output
- Line a: LineSegment [startPoint=Point2D [x=3, y=3], endPoint=Point2D [x=4, y=4]]
- Line b: LineSegment [startPoint=Point2D [x=1, y=1], endPoint=Point2D [x=2, y=2]]
- Line c: LineSegment [startPoint=Point2D [x=1, y=1], endPoint=Point2D [x=2, y=2]]
- Line b's distance between points: 1.41421356237
- Does a equal b? false
- Does a equal c? false
- Does b equal c? true
Note that when a class offers getters/setters for a primitive, pass-by-value ensures that changes to copies of a private primitive won’t affect the original primitive. When we pass objects to and from methods (as we do with getters and setters), objects are shared due to "pass-by-reference". This results in a privacy leak: objects you marked as private are still directly accessible due to the memory semantics involved with passing a reference. As a result, when we get and set objects, extra care in the form of cloning objects is required to avoid such privacy leaks. In the end, we’ll copy our objects and emulate pass-by-value with our objects so that people who try to “get” our private objects actually get a clone or copy of the object – if they destroy the clone, your private state objects will not be affected. Pay special attention to the getters/setters associated with your start and end points; notice how eclipse incorrectly writes this code, too.
Step by step
Solved in 4 steps with 5 images