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

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

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

问题

  1. public enum Season {
  2. WINTER(-5.0, new String[]{"December", "January", "February"}),
  3. SPRING(15.0, new String[]{"March", "April", "May"}),
  4. SUMMER(25.0, new String[]{"June", "July", "August"}),
  5. FALL(10.0, new String[]{"September", "October", "November"});
  6. private final double averageTemperature;
  7. private final String[] months;
  8. Season(double averageTemperature, String[] months) {
  9. this.averageTemperature = averageTemperature;
  10. this.months = months;
  11. }
  12. public double getAverageTemperature() {
  13. return averageTemperature;
  14. }
  15. public void printInfo() {
  16. System.out.println("Season: " + this.name());
  17. System.out.println("Average temperature: " + this.getAverageTemperature() + " degrees Celsius");
  18. System.out.print("Months: ");
  19. this.printMonths();
  20. System.out.println();
  21. }
  22. public abstract void printMonths();
  23. }
英文:

"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."

  1. public enum Season {
  2. WINTER(-5.0, new String[]{"December", "January", "February"}),
  3. SPRING(15.0, new String[]{"March", "April", "May"}),
  4. SUMMER(25.0, new String[]{"June", "July", "August"}),
  5. FALL(10.0, new String[]{"September", "October", "November"});
  6. @Override
  7. public abstract void printMonths(){
  8. this.months = months;
  9. this.averageTemperature = averageTemperature;
  10. };
  11. private final double averageTemperature;
  12. private final String[] months;
  13. Season(double averageTemperature, String[] months) {
  14. this.averageTemperature = averageTemperature;
  15. this.months = months;
  16. }
  17. public double getAverageTemperature() {
  18. return averageTemperature;
  19. }
  20. public void printInfo() {
  21. System.out.println("Season: " + this.name());
  22. System.out.println("Average temperature: " + this.getAverageTemperature() + " degrees Celsius");
  23. System.out.print("Months: ");
  24. this.printMonths();
  25. System.out.println();
  26. }
  27. }

答案1

得分: 1

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

然后像这样使用

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

<!-- -->

  1. Season: WINTER
  2. Average temperature: -5.0 摄氏度
  3. 月份: 十二月,一月和二月
  4. Season: SPRING
  5. 平均温度: 15.0 摄氏度
  6. 月份: 三月,四月和五月
  7. Season: SUMMER
  8. 平均温度: 25.0 摄氏度
  9. 月份: 六月,七月和八月
  10. Season: FALL
  11. 平均温度: 10.0 摄氏度
  12. 月份: 九月,十月和十一月
英文:

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

  1. enum Season {
  2. WINTER(-5.0) {
  3. @Override
  4. public void printMonths() {
  5. System.out.println(&quot;December, January and February&quot;);
  6. }
  7. },
  8. SPRING(15.0) {
  9. @Override
  10. public void printMonths() {
  11. System.out.println(&quot;March, April and May&quot;);
  12. }
  13. },
  14. SUMMER(25.0) {
  15. @Override
  16. public void printMonths() {
  17. System.out.println(&quot;June, July and August&quot;);
  18. }
  19. },
  20. FALL(10.0) {
  21. @Override
  22. public void printMonths() {
  23. System.out.println(&quot;Septembre, October and November&quot;);
  24. }
  25. };
  26. private final double averageTemperature;
  27. Season(double averageTemperature) {
  28. this.averageTemperature = averageTemperature;
  29. }
  30. public abstract void printMonths();
  31. public double getAverageTemperature() {
  32. return averageTemperature;
  33. }
  34. public void printInfo() {
  35. System.out.println(&quot;Season: &quot; + this.name());
  36. System.out.println(&quot;Average temperature: &quot; + this.getAverageTemperature() + &quot; degrees Celsius&quot;);
  37. System.out.print(&quot;Months: &quot;);
  38. this.printMonths();
  39. System.out.println();
  40. }
  41. }

Then use like

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

<!-- -->

  1. Season: WINTER
  2. Average temperature: -5.0 degrees Celsius
  3. Months: December, January and February
  4. Season: SPRING
  5. Average temperature: 15.0 degrees Celsius
  6. Months: March, April and May
  7. Season: SUMMER
  8. Average temperature: 25.0 degrees Celsius
  9. Months: June, July and August
  10. Season: FALL
  11. Average temperature: 10.0 degrees Celsius
  12. 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:

确定