This course has already ended.

⌛ Date and time

Place your code into files named Date.java, DateTime.java and DateException.java in folder Round5/datetime in your Git repository.

This is a simple exercise about inheritance. Implement the following public classes for handling dates and times.

  • Class Date that stored a date, that is, day of month, month and years. Public members:

    • Constructor Date(int year, int month, int day) that initializes the object to represent the date specified by the parameters. The constructor checks if the date is legal, and if it is not, throws an exception of type DateException with a message of form “Illegal date day.month.year”. Both day and month are expressed using two digits.

      • You may use e.g. the example code from the course’s first Java text material for checking if a date is legal.

    • Member functions int getYear(), int getMonth() and int getDay() that return the corresponding values.

    • Member function String toString() that returns a string that describes the date of this object in the form “day.month.year”. Both day and month are expressed using two digits.

  • Class DateTime that inherits the class Date and in addition stores information about time, that is, hour, minute and second. Public members:

    • Constructor DateTime(int year, int month, int day, int hour, int minute, int second) that initializes the object according to the parameters. The constructor checks if the time is legal (hour 0-23, minute and second 0-59), and if it is not, throws an exception of type DateException with a message of form “Illegal time hour:minute:second”. Each part of the time is expressed using two digits.

    • Member functions int getHour(), int getMinute() and int getSecond() that return the corresponding values.

    • Member function String toString() that returns a string formed by first the string returned by toString of the superclass Date, then a space, and then a string that represents this object’s time in the form “hour:minute:second”. Each part of the time is expressed using two digits.

      • The function may call toString of the superclass via the keyword super, ie. as super.toString(). First perform this call and then append the result with a space and a time string of the form described above.

  • Class DateException that inherits Java library class Exception and which has a constructor of form DateException(String msg) that simply passes the parameter msg to the constructor of the superclass Exception.

Example tests

The automated grader tests your class implementations with roughly the following test program:

public class DateTimeTest {
  public static void main(String args[]) {
    ArrayList<Date> dates = new ArrayList<>();
    for(String arg : args) {
      try {
        String[] parts = arg.split(" ");
        if(parts.length == 3) {
          dates.add(new Date(Integer.parseInt(parts[0]), Integer
                  .parseInt(parts[1]),
                  Integer.parseInt(parts[2])));
        }
        else if(parts.length == 6) {
          dates.add(new DateTime(Integer.parseInt(parts[0]), Integer.parseInt(
                  parts[1]), Integer.parseInt(parts[2]), Integer
                  .parseInt(parts[3]), Integer.parseInt(parts[4]), Integer
                  .parseInt(parts[5])));
        }
      }
      catch(DateException e) {
        System.out.println(e);
      }
    }
    for(int i = 0; i < dates.size(); i++) {
      System.out.format("Date #%d: %s%n", i + 1, dates.get(i));
    }
  }
}

Below are two example test program runs and their expected ouputs:

java DateTimeTest "2015 1 1" "1999 12 31 23 59 59" "2012 13 20"

DateException: Illegal date 20.13.2012
Date #1: 01.01.2015
Date #2: 31.12.1999 23:59:59

java DateTimeTest "1998 01 24" "1999 02 03 17 60 17" "2010 01 20 6 55 12" "2012 07 28" "2021 02 02"

DateException: Illegal time 17:60:17
Date #1: 24.01.1998
Date #2: 20.01.2010 06:55:12
Date #3: 28.07.2012
Date #4: 02.02.2021

A+ presents the exercise submission form here.

Posting submission...