Implementing tasks

Preparation
  1. Download and unzip above exam.zip. You should see a directory »Exam« containing a pom.xml file.

  2. Open this project in your IDEA IDE by selecting the Exam/pom.xml file as a project.

Task

exercise No. 203

Q:

Open the Readme.md file in your project's root. It contains all necessary instructions for solving the implementation tasks.

A:

A simple solution will implement the matches(...) method:

private boolean matches(Month month, int day) {
  return fromMonth == month && fromDay <= day ||
         toMonth == month && day <= toDay;
}

This method will return e.g. Aries for month == APR and day == 7. We are now able providing a first solution :

static public Zodiac getZodiac(final Month month, final int day) {
  for (Zodiac zodiac : Zodiac.values()) { // Looping over all zodiacs
    if (zodiac.matches(month, day)) {  // Exactly one zodiac must match mounth and day
      return zodiac;
    }
  }
  return null; // Unreachable for correct month and day values but required for keeping the compiler happy
}

Apart from requiring the ugly return null statement this approach is also ineffective due to possibly looping over all 12 zodiacs before finding the desired match. A better approach creates a two-dimensional array private static final Zodiac[][] zodiacByMonthByDay of zodiacs beforehand using month.ordinal() and day as index values:

          1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
JAN:  0 Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu
FEP:  1 Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Aqu Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis
MAR:  2 Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Pis Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari
APR:  3 Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Ari Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau
MAY:  4 Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Tau Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem
JUN:  5 Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Gem Can Can Can Can Can Can Can Can Can
JUL:  6 Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Can Leo Leo Leo Leo Leo Leo Leo Leo Leo
AUG:  7 Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Leo Vir Vir Vir Vir Vir Vir Vir Vir Vir
SEP:  8 Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Vir Lib Lib Lib Lib Lib Lib Lib Lib
OCT:  9 Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Lib Sco Sco Sco Sco Sco Sco Sco Sco
NOV: 10 Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sco Sag Sag Sag Sag Sag Sag Sag Sag Sag
DEC: 12 Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Sag Cap Cap Cap Cap Cap Cap Cap Cap Cap Cap

This table uses shortcuts e.g. Aqu denoting Aquarius. Note the variable month lengths in the second (day) dimension varying from 29 days for February to 31.

For exact implementation details see winter 2024 exam. The approach uses a so called static initializer block:

public enum Zodiac {
...
static {
  // First array dimension: 12 months
  zodiacByMonthByDay = new Zodiac[Month.values().length][];
   ...
  }
}

static blocks get processed before starting main(...). This guarantees our array being initialized before being accessed. Our final solution now simply reads:

static public Zodiac getZodiac(final Month month, final int day) {
  return zodiacByMonthByDay[month.ordinal()][day - 1];
}
Warning
  • When approaching end of examination check your input for completeness prior to being automatically logged out by the system. Remember: There is 120 minutes for the examination and another 5 minutes to check for completeness.

  • Projects residing just on your local workstation's file system cannot be recovered after finishing the exam.