X431: Get Largest Red Ball

For the question below, assume the following implementation of the Ball class

public  class Ball{
    private String color;
    private double diameter;

    public Ball(String co, double di) {
        color = co;
        diameter = di;

    public String getColor() {
        return color;
    public double getDiameter() {
        return diameter;


For this problem, create a method that will take in three ball objects and return the largest red ball. No two balls will have the same diameter. If only one ball is red, return that ball. If multiple balls are red, return the one with the largest diameter. If none of the balls are red, return null.

For this program, color will always be a string where all letters are lower case. So a red ball will always have color set to the string "red", you won't have to worry about strings like "RED" or "Red" or "rEd" or anything else.

Remember when comparing strings you cannot use ==. Instead you must use .equals()

Your Answer:


Practice a different Java exercise


Your feedback will appear here when you check your answer.