Consider the aggregated Circle class and the Point class pub

Consider the aggregated Circle class and the Point class public class Circlet { private point center; private int radius; public Circle(Point c, int r) { center = c; radius = r; } public Point getCenter( ) { return center; } } public class Point{ private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public Point getx() { return x; } public int gety() { return y; } } Show the statement(s) required to create a new Circle object Identify any data leaks in the Circle class and rewrite the appropriate code to use deep copy and deep return instead of the shallow references.

Solution

a Ans) Below is the statement that will create the new \'Circle\' object:-

Circle c=new Circle(new Point(3,4) , 4);

b Ans) Below is the given implementation of Circle class showing where shallow references are used

public class Circle {
private Point center;
private int radius;

public Circle(Point c, int r) {
center = c; // this line the shallow copy
radius = r;
}

public Point getCenter() {
return center; // this line the shallow return
}
  
  
  
}

Below is the modified Circle class using deep references

public class Circle {
private Point center;
private int radius;

public Circle(Point c, int r) {
center = new Point(c.getX , c.getY); // this line the deep copy
radius = r;
}

public Point getCenter() {
return new Point(center.getX , center.getY); // this line the deep return
}
  
  
  
}

 Consider the aggregated Circle class and the Point class public class Circlet { private point center; private int radius; public Circle(Point c, int r) { cente

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site