Look at following Java code ----------------------------------------------------
ID: 3870401 • Letter: L
Question
Look at following Java code
-----------------------------------------------------------------------------------------------
class point implements Cloneable {
int x;
int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Circle implements Cloneable {
point center;
int radius;
public Circle(point center, int radius) {
this.center = center;
this.radius = radius;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Circle clone = (Circle) super.clone();
return clone;
}
}
public class JavaApplication6 {
public static void main(String[] args) {
Circle c1 = new Circle(new point(10, 20), 10), c2 = null;
try {
c2 = (Circle) c1.clone();
} catch (CloneNotSupportedException ex) {
System.out.println("Clone failed");
}
System.out.println(c1 == c2);
System.out.println(c1.center == c2.center);
}
}
-----------------------------------------------------------------------------------------------------
If you think that this code does not give you a clone, change the code to get a clone
Explanation / Answer
Note: What ever the code you written to clone the Object sis right.But to Compare the two objects We have to use equals() method instead of using double equals( == ) to Operator.We Use this operator to compare primitive type values.But not objects.So I written equals() method to compare Circle objects.If u have any doubt further .Just message in comment section.So that I will clear.Thank You
______________
JavaApplication6.java
class point implements Cloneable {
int x;
int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "[" + x + "," + y + "]";
}
}
class Circle implements Cloneable {
point center;
int radius;
public Circle(point center, int radius) {
this.center = center;
this.radius = radius;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Circle clone = (Circle) super.clone();
return clone;
}
public boolean equals(Circle other) {
if (center == null) {
if (other.center != null)
return false;
} else if (!center.equals(other.center))
return false;
if (radius != other.radius)
return false;
return true;
}
@Override
public String toString() {
return "Circle [center=" + center + ", radius=" + radius + "]";
}
}
public class JavaApplication6 {
public static void main(String[] args) {
Circle c1 = new Circle(new point(10, 20), 10), c2 = null;
try {
c2 = (Circle) c1.clone();
} catch (CloneNotSupportedException ex) {
System.out.println("Clone failed");
}
System.out.println("Displaying The Circle#1 Obj Info :");
System.out.println(c1.toString());
System.out.println(" Displaying The Circle#2 Obj Info :");
System.out.println(c2.toString());
if (c1.equals(c2)) {
System.out.println(" Circle#1 object is equal to Circle#2 Object");
} else {
System.out.println("Circle#1 object is not equal to Circle#2 Object");
}
}
}
__________________
Output:
Displaying The Circle#1 Obj Info :
Circle [center=[10,20], radius=10]
Displaying The Circle#2 Obj Info :
Circle [center=[10,20], radius=10]
Circle#1 object is equal to Circle#2 Object
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.