Question
The LineSegment Class Class Composition (Has a) & Privacy Leaks Find your Point2D.java class that you built in the previous lab. The Point2D should store
The LineSegment Class Class Composition (Has a) & Privacy Leaks Find your Point2D.java class that you built in the previous lab. 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 pass-by-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 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 main 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 ClassDesignII.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 Point2D class public class Point2D { private int x; private int y; public void setX(int nX) { this.x = nX; } public int getX() { return x; } public void setY(int nY) { this.y = nY; } public int getY() { return y; } public void resetToOrigin() { // this function sets both x and y to zero this.x = 0; this.y = 0; } public void translate(int dx,int dy) { //this method adds dx to x and dy to y this.x = this.x + dx; this.y = this.y + dy; } //@Override public String toString() { // a string representation of the point return "("+ x + "," + y + ")"; } public boolean equals(Point2D that) { //return if this is equal to 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; } } The Fraction Class Immutable Classes In this section, well build another Fraction class that is unchangeable once initialized and uses the keyword final for its numerator and denominator. Such a Fraction object will have all of its data declared final, and is our first example of building an immutable class. Once a Fraction object is built, its data items will never change for the lifetime of the object. Another way to view this is that the object is completely read-only. As a result, its data will also be declared public, which is the only example of public data youll find in this quarter. If you wish to change a fraction objects numerator or denominator, the old object must be discarded and a new object created in its place. Again, this objects data will be immutable, constant, non-variable, unchangeable, non-editable, or read-only. So, to add two fractions, our add function will return a new Fraction object that is the sum of the two previous (unchangeable) fractions we wish to add. Once youve built this class, uncomment out the appropriate tests in the driver for this lab. Start by building a new Fraction class, and define the following members: Class Invariants Numerators and denominators are unchangeable once set by the constructor. No denominator will be stored as a 0. (i.e., no DivideByZero Exceptions). A Fraction is always in reduced form (reduce in the constructor to ensure this). Data Define a numerator that is public and final. Why dont we make this data private? Define a denominator that is public and final. What data types should these items be? Methods Define a constructor that takes a numerator and a denominator Do not define a no-argument constructor. Why? Define a constructor that takes a Fraction object and makes a copy of it. public Fraction(Fraction other){...} Define a toString() function as weve done for other classes. Define an add function that takes a fraction, adds it to this, then returns a new Fraction object that is the result of the addition of the two public Fraction add(Fraction that) {//add this and that together; remember to consider the denominator here! Define an equals(Object o) function that has the form: public boolean equals(Object other) { if( other != null && ! (other instanceof Fraction ) ) return false; //what does this code do? Fraction that = (Fraction) other; //and this code? //todo: code goes here } Sample Output a:1\2 b:3\4 c:3\4 a:10\8 b:3\4 c:3\4 a.equals(b):false b.equals(c):true
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started