Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The LineSegment Class Class Composition (Has a) & Privacy Leaks Find your Point2D.java class that you built in the previous lab or use Point2D.java. The

The LineSegment Class Class Composition (Has a) & Privacy

Leaks

Find your Point2D.java class that you built in the previous lab or use Point2D.java. 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). This is similar in spirit to your next homework, where well build a few simple classes called Money and Date, and then build a meta-class or container class that is composed of both a Money and Date object well call this a Bill object, and we can state that a Bill has a Money and has a Date object inside of it. Note that when a class offers getters/setters for a primitive, pass-by-value ensures that changes to copies of a private primitive wont 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, well 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.

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 startPoints toString() and endPoints 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
  • 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 nulls 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?

Sample Output

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

Points2D

public class Point2D {

private int x; // x-coordinate private int y; // y-coordinate

public int getX() { return x; }

public int getY() { return y; }

public void setX(int x) { this.x = x; }

public void setY(int y) { this.y = y; } public void resetToOrigin() { this.x=0; this.y=0; } public void translate(int dx,int dy) { this.x+=dx; this.y+=dy; } @Override public String toString() { return "(" + x + ","+ y + ')'; }

public boolean equals(Point2D that) { if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } final Point2D other = (Point2D) that; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } public static void main(String[] args) { Point2D a= new Point2D(); a.setX(5); a.setY(2); System.out.println("Point2D at ("+a.getX()+", "+a.getY()+")"); a.translate(-1, -1); System.out.println("Point2D at ("+a.getX()+", "+a.getY()+")"); a.resetToOrigin(); System.out.println("Point2D at ("+a.getX()+", "+a.getY()+")"); Point2D b=new Point2D(); Point2D c=new Point2D(); System.out.println(b.toString()); System.out.println(c);//because we have override the to string method in the point class System.out.println("Are b ans c equal: "+b.equals(c)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

What is meant by the critical path?

Answered: 1 week ago

Question

3. How frequently do the assessments occur?

Answered: 1 week ago