Question
Circle code public class Circle { private double radius; public double getRadius() { return this.radius; } private void setRadius(double radius) { if (radius >= 0)
Circle code
public class Circle { private double radius;
public double getRadius() { return this.radius; }
private void setRadius(double radius) { if (radius >= 0) this.radius = radius; else this.radius = 0; }
public void resize(double newRadius) { this.setRadius(newRadius); }
public Circle clone() { Circle c = new Circle();
c.resize(this.getRadius());
return c; }
public boolean equals(Circle guest) { return guest.getRadius() == this.getRadius(); } public void print() { System.out.print("The circle\'s radius is " + this.getRadius()); } }
CircleDriver code
import java.util.Scanner;
public class CircleDriver
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
Circle big = new Circle();
Circle small = new Circle();
System.out.println("");
System.out.print("Big : ");
big.print();
System.out.println("");
System.out.print("Small : ");
small.print();
System.out.println(" ");
double bigRadius;
System.out.print("Enter the radius of a big circle : ");
bigRadius = stdIn.nextDouble();
big.resize(bigRadius);
double smallRadius;
System.out.print("Enter the radius of small circle : ");
smallRadius = stdIn.nextDouble();
small.resize(smallRadius);
System.out.println("");
System.out.print("Big : ");
big.print();
System.out.println("");
System.out.print("Small : ");
small.print();
System.out.println("");
if (big.equals(small))
System.out.println(" big equals small ");
else
System.out.println(" big does not equals small ");
System.out.println("");
Circle bigCopy = big.clone();
System.out.print("BigCopy : ");
bigCopy.print();
System.out.println("");
if (bigCopy.equals(big))
System.out.println(" bigCopy equals big ");
else
System.out.println(" bigCopy does not equals big ");
System.out.println("");
}
}
Main topics:
Writing Classes
Declaring / Using Instance Variables
Writing Instance Methods
Accessors and Mutators
public vs private
Constructors
static vs non-static
Exercise
This week we will be practicing writing a complete class, starting from where you left off last week, which includes all of the standard mutators as well as an instance count. In addition careful attention to the public, private and static specifiers will be made.
Requirements
Circle.java
A simple class which models a circle by its one defining characteristic, which is its radius. This class is not complete and must be modified as such:
1. All class variables must be private
2. The initial instance count must be 0
3. Include a public accessor for the instance count
4. Include a private mutator for the instance count - which should only be called in constructor(s).
5. By default all instances have a radius of 1.0
6. Include the standard default constructor All access of instance data by this constructor must be made via the the specifying constructor.
7. Include the standard specifying constructor All access of class and instance data by this constructor must be made via the accessors and mutators.
8. Include the standard copy constructor All access of instance data by this constructor must be made via the the specifying constructor.
9. Modify the Circle clone() method so that it makes use of the copy constructor, instead of using the default constructor, followed by the resize() method.
CircleDriver.java
A simple driver class to test the Circle class. This class is not complete and must be modified as such:
1. Add / modify lines of code and test that each of your constructors are functioning properly.
2. Add / modify lines of code and test that your instance count is being maintained properly.
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