Question: public class Point2D { private int x; // x-coordinate private int y; // y-coordinate public int getX() { return x; } public int getY() {

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)); } }

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?

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!