Question
I got a comment from my teacher. Can you please help me to fix it ? Here is a comment. ( default constructor should call
I got a comment from my teacher. Can you please help me to fix it ? Here is a comment.
( default constructor should call the specifying constructor. clone method is not correct )
Instruction: Please don't use ( Clone interface or Cloneable ). Just try to fix it with simple clone method
public class Circle { private static int count=0; private double radius=1.0; public static int getCount() { return count; } public static void setCount(int count) { Circle.count = count; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public Circle() { count++; } public Circle(double radius) { setCount(++count); setRadius(radius); } public Circle(Circle c) { this(c.getRadius()); } protected Circle clone() throws CloneNotSupportedException { return this; }
}
Driver Class
public class CircleDriver { public static void main(String[] args) throws CloneNotSupportedException { Circle c1=new Circle(); Circle c2=new Circle(4.2); Circle c3=new Circle(c2); c1.setRadius(1.6); Circle c4=c1.clone(); //printing radius of all circles System.out.println("Radius of Circle 1 : "+c1.getRadius()); System.out.println("Radius of Circle 2 : "+c2.getRadius()); System.out.println("Radius of Circle 3 : "+c3.getRadius()); System.out.println("Radius of Circle 4 : "+c4.getRadius()); System.out.println("Total objects are : "+Circle.getCount()); }
}
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