In: Computer Science
I need an answer only for the bold part of the question.
1. Circle:
The class has two private instance variables: radius (of the type double) and color (of the type String).
The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated.
Construction:
A constructor that constructs a circle with the given color and sets the radius to a default value of 1.0.
A constructor that constructs a circle with the given, radius and color.
Once constructed, the value of the radius must be immutable (cannot be allowed to be modified)
Behaviors:
Accessor and Mutator aka Getter and Setter for the color attribute
Accessor for the radius.
getArea() and getCircumference() methods, hat return the area and circumference of this Circle in double.
Hint: use Math.PI (https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#PI (Links to an external site.))
2. Rectangle:
The class has two private instance variables: width and height (of the type double)
The class also has a private static variable: numOfRectangles (of the type long) which at all times will keep track of the number of Rectangle objects that were instantiated.
Construction:
A constructor that constructs a Rectangle with the given width and height.
A default constructor.
Behaviors:
Accessor and Mutator aka Getter and Setter for both member variables.
getArea() and getCircumference() methods, that return the area and circumference of this Rectangle in double.
a boolean method isSquare(), that returns true is this Rectangle is a square.
Hint: read the first 10 pages of Chapter 5 in your text.
3. Container
The class has two private instance variables: rectangle of type Rectangle and circle of type Circle.
Construction:
No explicit constructors.
Behaviors:
Accessor and Mutator aka Getter and Setter for both member variables.
an integer method size(), that returns 0, if all member variables are null, 1 either of the two member variables contains a value other than null, and 2, if both, the rectangle and circle contain values other than null.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
1)
// Circle.java
public class Circle {
private double radius;
private String color;
private static long noOfCircles;
public Circle(double radius, String color) {
this.radius = radius;
this.color = color;
noOfCircles++;
}
public Circle(String color) {
this.radius = 1.0;
this.color = color;
noOfCircles++;
}
public double getRadius() {
return radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea()
{
return Math.PI*radius*radius;
}
public double getCircumference()
{
return 2*Math.PI*radius;
}
}
_____________________________
2)
// Rectangle.java
public class Rectangle {
private double height;
private double width;
private long numOfRectangles;
public Rectangle(double height, double width, long
numOfRectangles) {
this.height = height;
this.width = width;
this.numOfRectangles++;
}
public Rectangle() {
this.height = 1;
this.width = 1;
this.numOfRectangles++;
}
public double getArea() {
return width * height;
}
public double getCircumference() {
return 2 * (width + height);
}
public boolean isSquare() {
if (width == height)
return
true;
else
return
false;
}
}
_______________________
3)
// Container.java
public class Container {
private Rectangle rectangle;
private Circle circle;
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle)
{
this.rectangle = rectangle;
}
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public int size() {
int val = 0;
if (rectangle == null &&
circle == null) {
val = 0;
} else if (rectangle != null ||
circle != null) {
val = 1;
} else if (rectangle != null
&& circle != null) {
val = 2;
}
return val;
}
}
_______________Could you plz rate me well.Thank You