Create a listing "Month". It is necessary to determine in the constructor and save the number of days. Add methods to get the previous and next month, as well as a function that returns the season for each month. To provide for the withdrawal of months in Russian. Create a static output function for all months by overriding the toString() method. Test the enumeration in the test-class main() function. Help override the toString() method.

 enum Month12 { JANUARY(31), FEBRUARY(28), MARCH(31), APRIL(30), MAY(31), JUNE(30), JULY(31), AUGEST( 31), SEPTEMBER(30), OCTOBER(31), NOVEMBER(30), DECEMBER(31); private Integer days; private Month12(Integer days) { this.days = days; } public Integer getDays() { return days; } public String toString() { switch (this) { case JANUARY: return "Январь"; case FEBRUARY: return "Февраль"; case MARCH: return "Март"; case APRIL: return "Апрель"; case MAY: return "Май"; case JUNE: return "Июнь"; case JULY: return "Июль"; case AUGEST: return "Август"; case SEPTEMBER: return "Сентябрь"; case OCTOBER: return "Октябрь"; case NOVEMBER: return "Ноябрь"; case DECEMBER: return "Декабрь"; } return "not month"; } Month12 next() { Month12 a = values()[(ordinal() + 1) % values().length]; return a; } Month12 before() { Month12 a = values()[(ordinal() - 1 + 12) % values().length]; return a; } public String season() { switch (this) { case JANUARY: case FEBRUARY: case DECEMBER: return "Зима"; case MARCH: case APRIL: case MAY: return "Весна"; case JUNE: case JULY: case AUGEST: return "Лето"; case SEPTEMBER: case OCTOBER: case NOVEMBER: return "Осень"; default: return "not month"; } } } public class Month { public static void main(String[] args) { Month12 a = Month12.JANUARY; System.out.println("Введенный месяц: " + a); System.out.println(a.next()); System.out.println(a.before()); System.out.println(a.season()); } } 
  • And what is wrong in the code given by you? - VladD
  • I don’t know how to create a static output function for all months by overriding the toString () method - gridkat
  • 2
    Static? Impossible, toString() not a static function. / thread - VladD
  • Is not it easier in Ename not only to keep the number of days, but also the name of the month? Then it will be easier to overwrite toString (). - Yevgeny Karpov

2 answers 2

Use Eclipse. Create a class, in the free space Ctrl + Space - autocomplete will appear. Select toString () there and a rewritten method will be created, even with an annotation.

A little rewrote enam from boredom ...

  package main; public enum Month12 { JANUARY(31, "Январь", "Зима"), FEBRUARY(28, "Февраль", "Зима"), MARCH(31, "Март", "Весна"), APRIL(30, "Апрель", "Весна"), MAY(31, "Май", "Весна"), JUNE(30, "Июнь", "Лето"), JULY(31, "Июль", "Лето"), AUGEST(31, "Август", "Лето"), SEPTEMBER(30, "Сентябрь", "Осень"), OCTOBER(31, "Октябрь", "Осень"), NOVEMBER(30, "Ноябрь", "Осень"), DECEMBER(31, "Декабрь", "Зима"); private final int _days; private final String _name; private final String _season; Month12(int days, String name, String season) { _days = days; _name = name; _season = season; } public int getDays() { return _days; } public String getName() { return _name; } public String getSeason() { return _season; } @Override public String toString() { return getName() + "(" + getDays() + " в месяце, сезон - " + getSeason() + ")"; } public Month12 getNext() { int next = ordinal() + 1; return next >= values().length ? JANUARY : values()[next]; } public Month12 getPrevious() { int next = ordinal() - 1; return next < 0 ? DECEMBER : values()[next]; } } 

And more ... All the methods of the enam elements are static (if specified for an element).

  • Eugene, thank you) - gridkat
  • Thanks to the button they poke and the answer is accepted)) - Evgeny Karpov
  • Season is better to allocate a separate Enum - Pavel Azanov

Change

 public String toString() { //.. } 

on:

 @Override public String toString() { //.. } 
  • one
    As VladD already said, you cannot create a static function toString. - Pavel Azanov