Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data structures: java 3. Consider this code that creates some Location objects L

ID: 3599977 • Letter: D

Question

Data structures: java

3. Consider this code that creates some Location objects Location a, b, c; a - new Location (10,20); b = (Location)a.clone ( ); c=a; c.shift (2,0); After this code executes, what are the values of these boolean expressions? a=-b a.equals(b) a.equals(c) b.equals (c) 4. Here is a simple Point and Circle class class Point class Circle f private double x, y; I private Point c; // center private double r; // radius public Point(double x, double y) { this.x=x; public Circle (Point c, double r) { this·r=r; this. y y ; this.c- c; public double getX() return x; > public double getY (Of return y; ) // more stuff a) The constructor in Circle has a "privacy leak". Explain why. Hint: Consider the following code. Point p = new Point (1,2); Circle c - new Circle(p, 10); p.setX(100); (b) Rewrite the Circle constructor to fix this problem

Explanation / Answer

4) Privacy leak : If there is a chance of accessing a private member of a class directly then it is called privacy leak.

In the given code of Circle, private member is made equal to the reference of passed object. So when we are using p.setX(100) it indirectly changes the point reference {private member} in the Circle. It is a privacy leak.

We can change the code as below to prevent privacy leak :

------------------------------------------------------------------------------

class Circle{

                  private Point c;          // center

                   private double r;        // radius

                   public Circle( Point p, double r){

                                    // here a new object with the values x,y of point p

                                      this.c = new Point(p.getX(), p.getY());

                                      this.r = r;

                  }

            // more stuff

}

----------------------------------------------------------------------------------------

In this way we prevent privacy leak in the above code.

3) clone() creates a new object with the same values of passed object. Assignment makes transfers the reference directly.

Now in this case :

a == b : it results false because clone() creates a new object. so when comparing by == it try to compare the references. In this case those are different references. So it returns false.

a == c : It results true because by assignment the object reference gets copies and when comparing object references get compared .So it is true.

b == c : It results false same as a == c.

a.equals(b) : it results false because equals() is a method of Location object which returns true if both the objects are pointing to same x and y. In this case at the end c shifts the point to (2,0) which indirectly moves a to (2,0). So a and b are not same locations.

b.equals(c) : It is same as a.equals(b) ( because a and c refers to same location reference)

a.equals(c) : It is true as both pointing to same location references. They both have common x and common y.

/* hope this helps */

/* thank you */

/* if any queries please comment */