Implementing tasks

Preparation
  1. Download and unzip 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. 209

Q:

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

A:

Solution to all implementing tasks:

  1. Standalone solve.zip compressed project solution.

  2. Gitlab link 2024winter/Solve.

We shed some extra light on implementing task 2.

A simple solution will start implementing the matches(...) method in Zodiac:

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 provide a first solution by looping until the desired zodiac is being found by filtering:

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
}

You'll still find this solution being renamed to getZodiac2(...) in favour of the subsequently improved implementation.

The getZodiac2(...) implementation is flawed in two ways:

  1. The final return null can never be reached: For any valid (month, day) combination the for loop will eventually be terminated by the return zodiac statement. But obviously the compiler is unable to detect this fact thus requiring it for the sake of syntactical correctness.

  2. The approach is also ineffective due to possibly looping over all 12 zodiacs before finding the desired match.

This being cumbersome we plan to create a two-dimensional array private static final Zodiac[][] zodiacByMonthByDay of zodiacs beforehand using month.ordinal() as first and day as second dimension index values. We provide an example:

Code Output
final Month month = Month.MAY;
final int day = 17;
Zodiac zodiac = zodiacByMonthByDay[month.ordinal()][day - 1];
System.out.println(zodiac);
Taurus

Having twelve zodiacs month.ordinal() will range from 0 to 11 while a day's range depends on the month in question. We thus require the following two-dimensional array:

          0   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
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
FEB:  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: 11 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

The above table uses shortcuts e.g. Aqu denoting Aquarius. Note the different month lengths in the second (day) array dimension varying from 29 days for February to 31 days for e.g. June.

The two-dimensional array is being created in a so called static initializer block:

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

static blocks get processed even before main(...) is being started. This guarantees our array being initialized prior to any access. Our improved getZodiac(...) implementation no longer requires a filter loop:

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.