A Circle may be represented as three floating point values. One value represents
ID: 3833654 • Letter: A
Question
A Circle may be represented as three floating point values. One value represents the change in X, another represents the change in Y, and the last value represents the radius. In addition, each Circle also has a color value which is either “Red”, “Green”, or “Blue”. Write a class to represent Circles. The class should have only one constructor which takes the four values as parameters. The class should also provide get methods for each of the data member variables.
Add a method to the Circle class definition which calculates the area of a Circle.
Add a method to the Circle class definition which returns true if the Circle is the specified color and false otherwise.
Write a static method in a Class called CircleUtils which when given an ArrayList of Circle objects as a parameter, calculates the total area of all the Circle objects in the ArrayList.
Add a static method to the CircleUtils class which takes an ArrayList of Circle objects as a parameter and a String representing a color. The method should return the Circle with the largest area and the specified color.
Write a toString() method for the Circle class.
Explanation / Answer
Please find my implementation.
public class Circle{
private double x;
private double y;
private double radius;
private String color;
// constructor
public Circle(double x, double y, double radius, String color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
// getters
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getRadius() {
return radius;
}
public String getColor() {
return color;
}
public double calcArea() {
return Math.PI*radius*radius;
}
public double calcCircumference() {
return Math.PI*2*radius;
}
public boolean hasColor(String color){
return this.color.equalsIgnoreCase(color);
}
}
#############
import java.util.ArrayList;
public class CircleUtils {
public static double totalCircleArea(ArrayList<Circle> circles){
double totalArea = 0;
for(Circle c : circles)
totalArea = totalArea + c.calcArea();
return totalArea;
}
public static Circle getLargestAreaCircleWithColor(ArrayList<Circle> circles, String color){
Circle cir = null;
for(Circle c: circles){
if(c.hasColor(color)){
if(cir == null){
cir = c;
}else{
if(c.calcArea() > cir.calcArea())
cir = c;
}
}
}
return cir;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.