Season is not abstract and does not override abstract method printMonths() in Season

huangapple go评论60阅读模式
英文:

Season is not abstract and does not override abstract method printMonths() in Season

问题

public enum Season {
    WINTER(-5.0, new String[]{"December", "January", "February"}),
    SPRING(15.0, new String[]{"March", "April", "May"}),
    SUMMER(25.0, new String[]{"June", "July", "August"}),
    FALL(10.0, new String[]{"September", "October", "November"});

    private final double averageTemperature;
    private final String[] months;

    Season(double averageTemperature, String[] months) {
        this.averageTemperature = averageTemperature;
        this.months = months;
    }

    public double getAverageTemperature() {
        return averageTemperature;
    }

    public void printInfo() {
        System.out.println("Season: " + this.name());
        System.out.println("Average temperature: " + this.getAverageTemperature() + " degrees Celsius");
        System.out.print("Months: ");
        this.printMonths();
        System.out.println();
    }

    public abstract void printMonths();
}
英文:

"Write an enum named Season with 4 constants: WINTER, SPRING, SUMMER, FALL. It contains a final field named averageTemperature, initialized by a constructor and with a getter method. Additionally, it also contains an abstract method named printMonths() which will display the months in that respective season. Traverse all values of this enum and display the season, average temperature, and the months of that season."

public enum Season {
    WINTER(-5.0, new String[]{"December", "January", "February"}),
    SPRING(15.0, new String[]{"March", "April", "May"}),
    SUMMER(25.0, new String[]{"June", "July", "August"}),
    FALL(10.0, new String[]{"September", "October", "November"});
    @Override
    public abstract void printMonths(){
        this.months = months;
        this.averageTemperature = averageTemperature;
    };
    private final double averageTemperature;
    private final String[] months;

    Season(double averageTemperature, String[] months) {
        this.averageTemperature = averageTemperature;
        this.months = months;
    }

    public double getAverageTemperature() {

        return averageTemperature;
    }

    public void printInfo() {
        System.out.println("Season: " + this.name());
        System.out.println("Average temperature: " + this.getAverageTemperature() + " degrees Celsius");
        System.out.print("Months: ");
        this.printMonths();
        System.out.println();
    }
}

答案1

得分: 1

由于该方法是抽象的,它不应该有具体的实现,应该在每个实例中实现:每个季节

然后像这样使用

for (Season s : Season.values()) {
    s.printInfo();
}

<!-- -->

Season: WINTER
Average temperature: -5.0 摄氏度
月份: 十二月,一月和二月

Season: SPRING
平均温度: 15.0 摄氏度
月份: 三月,四月和五月

Season: SUMMER
平均温度: 25.0 摄氏度
月份: 六月,七月和八月

Season: FALL
平均温度: 10.0 摄氏度
月份: 九月,十月和十一月
英文:

As the method is abstract, it shouldn't have a body and should be implemented in each instance : each season

enum Season {
    WINTER(-5.0) {
        @Override
        public void printMonths() {
            System.out.println(&quot;December, January and February&quot;);
        }
    },
    SPRING(15.0) {
        @Override
        public void printMonths() {
            System.out.println(&quot;March, April and May&quot;);
        }
    },
    SUMMER(25.0) {
        @Override
        public void printMonths() {
            System.out.println(&quot;June, July and August&quot;);
        }
    },
    FALL(10.0) {
        @Override
        public void printMonths() {
            System.out.println(&quot;Septembre, October and November&quot;);
        }
    };

    private final double averageTemperature;

    Season(double averageTemperature) {
        this.averageTemperature = averageTemperature;
    }

    public abstract void printMonths();

    public double getAverageTemperature() {
        return averageTemperature;
    }

    public void printInfo() {
        System.out.println(&quot;Season: &quot; + this.name());
        System.out.println(&quot;Average temperature: &quot; + this.getAverageTemperature() + &quot; degrees Celsius&quot;);
        System.out.print(&quot;Months: &quot;);
        this.printMonths();
        System.out.println();
    }
}

Then use like

for (Season s : Season.values()) {
    s.printInfo();
}

<!-- -->

Season: WINTER
Average temperature: -5.0 degrees Celsius
Months: December, January and February
Season: SPRING
Average temperature: 15.0 degrees Celsius
Months: March, April and May
Season: SUMMER
Average temperature: 25.0 degrees Celsius
Months: June, July and August
Season: FALL
Average temperature: 10.0 degrees Celsius
Months: Septembre, October and November

huangapple
  • 本文由 发表于 2023年2月18日 19:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定