Circle.java A simple class which models a circle by its one defining characteris
ID: 3584983 • Letter: C
Question
Circle.java A simple class which models a circle by its one defining characteristic, which is its radius. This class is moderately complete and must be modified as such:
1. Both the default and copy constructors simply call the specifying constructor, with the appropriate argument
2. Include a class constant for PI Whose value is the best approximation of PI you can store ( public or private? static or not? final or not? )
3. Include an instance constant for units Whose default value is ’’cm’’ ( public or private? static or not? final or not? )
4. The print() method is modified to include the units the radius is given in
5. Include an overloaded 0-arity mutator for the radius value that sets the radius value to 0 ( where is the one and only place it make sense to call this?
6. Include an area method that returns the area of the circle in units squared
My code so far for Circle.Java:
public class Circle
{
private static int instanceCnt = 0;
private double radius;
public static int getInstanceCnt()
{
return Circle.instanceCnt;
}
private static void setInstanceCnt(int insCnt)
{
Circle.instanceCnt = insCnt;
}
public Circle()
{
}
public Circle(double newRadius)
{
Circle.setInstanceCnt(Circle.getInstanceCnt() + 1);
this.setRadius(newRadius);
}
public Circle(Circle guest)
{
}
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()
{
return new Circle(this.getRadius());
}
public boolean equals(Circle guest)
{
return guest.getRadius() == this.getRadius();
}
public void print()
{
System.out.print("The circle's radius is " +
this.getRadius());
}
}
CircleDriver.java
A simple driver class to test your additions to the Circle class. This class you are writing from scratch, but must include sufficient code to test all of the new features listed above.
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
_______________
Circle.java
public class Circle {
//Declaring constants
private final double PI = 1.14159;
private final String UNITS = "cm";
//Declaring instance variables
private double radius;
//Zero argumented constructor
public Circle() {
this(0.0);
}
//Parameterized constructor
public Circle(double radius) {
setRadius(radius);
}
//Copy Constructor
public Circle(Circle c) {
this(c.getRadius());
}
//getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if (radius > 0.0)
this.radius = radius;
else
this.radius = radius;
}
//This method will calculate the area of the circle
public double area() {
return PI * radius * radius;
}
//This method will display the area of the circle along with the units
public void print() {
System.out.println("The radius of the circle is " + radius + UNITS);
}
}
_____________________
CircleDriver.java
import java.util.Scanner;
public class CircleDriver {
public static void main(String[] args) {
// Declaring variables
double radius;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.print("Enter the radius of Circle#1:");
radius = sc.nextDouble();
//Creating an Circle class object the user entered input as argument
Circle c = new Circle(radius);
// Displaying the output
c.print();
System.out.printf("The Area of Circle#1:%.2f ", c.area());
// Getting the input entered by the user
System.out.print("Enter the radius of Circle#2:");
radius = sc.nextDouble();
//Creating an Circle class object the user entered input as argument
Circle c1 = new Circle(radius);
//calling the copy constructor
Circle c2 = c1;
// Displaying the output
c2.print();
System.out.printf("The Area of Circle#2:%.2f ", c.area());
}
}
___________________
Output:
Enter the radius of Circle#1:3.5
The radius of the circle is 3.5cm
The Area of Circle#1:13.98
Enter the radius of Circle#2:4.5
The radius of the circle is 4.5cm
The Area of Circle#2:13.98
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.