英文:
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("December, January and February");
}
},
SPRING(15.0) {
@Override
public void printMonths() {
System.out.println("March, April and May");
}
},
SUMMER(25.0) {
@Override
public void printMonths() {
System.out.println("June, July and August");
}
},
FALL(10.0) {
@Override
public void printMonths() {
System.out.println("Septembre, October and November");
}
};
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("Season: " + this.name());
System.out.println("Average temperature: " + this.getAverageTemperature() + " degrees Celsius");
System.out.print("Months: ");
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论