saving only doubledigit numbers into my attribute

This is the best solution I can come up with:

public Season(int seasonNumber) {
    if (seasonNumber < 1 || seasonNumber > 99) {
        throw new IllegalArgumentException("Value between 1 and 99 (both inclusive) required. Found + " seasonNumber);
    }
    this.seasonNumber = seasonNumber;
}

I am not using a method, because the logic is all in the constructor. Though I suspect the person who gave you this hasn’t got his point across with the assignment texts.

If you want the season number returned as 01, 02, etc., you could create a method that returns a String, instead of an int:

public String getSeasonNumber() {
    if (seasonNumber < 10) {
        return "0" + seasonNumber;
    } else {
        return seasonNumber;
    }
}

Leave a Comment