⌛ Date and time¶
The exercise is returned as a Maven project. Place the pom.xml
file
in the round5/datetime
directory of your local repository
and create to this directory the src/main/java
subdirectory. Create class files
Date.java, DateTime.java and DateException.java and attach them to the fi.tuni.prog3.datetime
package.
Your files should be in the round5/datetime/src/main/java/fi/tuni/prog3/datetime
directory.
This is an introductory exercise about inheritance. Implement the following public classes for handling dates and times.
Class
Date
that stores a date, that is, day of month, month and year. 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 typeDateException
with a message of form “Illegal date day.month.year”. Both day and month are expressed using two digits.You may use, for example, the example code from the course’s earlier Java material for checking, if a date is legal.
Member functions
int getYear()
,int getMonth()
andint 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. Tip:String.format
and the"%02d"
format specifier.
Class
DateTime
that inherits the classDate
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 typeDateException
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()
andint getSecond()
that return the corresponding values.Member function
String toString()
that returns a string formed by first the string returned bytoString
of the superclassDate
, 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 keywordsuper
, that is, assuper.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 the Java library classException
and which has a constructor of formDateException(String msg)
that simply passes the parametermsg
to the constructor of the superclassException
.
The automatic tests, and the ones given below, assume that you make the following definitions
in your pom.xml
project file:
The value of
artifactId
isdatetime
.The value of
version
is1.0
.The values of the
maven.compiler.source
andmaven.compiler.target
elements are17
or lower. The grader uses Java 17, so any newer versions won’t work.A Onejar plugin definition where the value of
mainClass
isDateTimeTest
which is the name of the given test class (see below).
Example tests¶
The automated grader tests your class implementations with roughly the following test program:
import java.util.ArrayList;
import fi.tuni.prog3.datetime.Date;
import fi.tuni.prog3.datetime.DateException;
import fi.tuni.prog3.datetime.DateTime;
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 -jar target/datetime-1.0.one-jar.jar "2015 1 1" "1999 12 31 23 59 59" "2012 13 20"
fi.tuni.prog3.datetime.DateException: Illegal date 20.13.2012
Date #1: 01.01.2015
Date #2: 31.12.1999 23:59:59
java -jar target/datetime-1.0.one-jar.jar "1998 01 24" "1999 02 03 17 60 17" "2010 01 20 6 55 12" "2012 07 28" "2021 02 02"
fi.tuni.prog3.datetime.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.