$30
Programming Fundamentals - 1/5 -
CSIT111/811 ⎯ Programming Fundamentals
Assignment 3 (10 marks)
General Requirements:
• You should create your programs with good programming style and form using proper blank
spaces, indentation and braces to make your code easy to read and understand;
• You should create identifiers with sensible names;
• You should make comments to describe your code segments where they are necessary for
readers to understand what your code intends to achieve.
• Logical structures and statements are properly used for specific purposes.
• Read the assignment specification carefully, and make sure that you follow whatever
directed in this assignment. In every assignment that you will submit in this subject, you
must put the following information in the header of your assignment:
/*------------------------------------------------------
My name:
My student number:
My course code:
My email address:
Assignment number: 3
-------------------------------------------------------*/
Objectives
This assignment requires you to write a program in Java that calculates the day of week and the
week of month for a given date, as well as to print the calendar of the month where the given date
exists. Please note, you are not allowed to use any predefined date class in Java.
Background
Programming Fundamentals - 2/5 -
For a given date, we can use the Gregorian calendar to find the corresponding the day of the week,
and the week of the month. For example, May 24 2020 is a Sunday and locates in the fourth week
of May 2020 (note: Monday is considered the first day of a week). In general, we can use the
following formula (Zeller’s congruence) to calculate the day of a week for any given date after
October 15 1582.
where
• h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, 3=Tuesday, 4=Wednesday,
5=Thursday, 6 = Friday)
• q is the day of the month
• m is the month (3 = March, 4 = April, 5 = May, 6=June, 7=July, 8=August, 9=September,
10=October, 11=November, 12=December, 13=January, 14 = February)
• K is the year of the century (year mod 100).
• J is the zero-based century (year/100). For example, the zero-based centuries for 1995 and 2000
are 19 and 20 respectively.
For example, For 1 January 2000, the date would be treated as the 13th month of 1999, so the
values would be: q=1, m=13, K=99, J=19, so the formula is
h = (1+[13*(13+1)/5]+99+[99/4]+[19/4]+5*19) mod 7
= (1+[182/5]+99+[99/4]+[19/4]+95) mod 7
= (1+[36.4]+99+[24.75]+[4.75]+95) mod 7
= (1+36+99+24+4+95) mod 7
= 0
= Saturday
However, for 1 March 2000, the date is treated as the 3rd month of 2000, so the values become q=1,
m=3, K=00, J=20, so the formula is
h = (1+[13*(3+1)/5]+00+[00/4]+[20/4]+5*20] mod 7
= (1+[10.4]+0+0+5+100) mod 7
= (1+10+5+100) mod 7
= 4
= Wednesday
Programming Fundamentals - 3/5 -
Tasks
You will create one program called MyCalendar.java. You should first implement the
MyCalendar and MyDate classes from the following UML class diagrams. You can add extra
attributes and methods, but the attributes and methods shown in the following UML diagrams can’t
be modified.
1. MyCalendar Class
MyCalendar
- myDate: MyDate
+ Main(String[] args)
+ MyCalendar(MyDate newDate)
+ MyCalendar()
+ dayOfWeek(): WeekDay
+ weekOfMonth(): int
+ printMonth(): void
• myDate is a field data and refers to the object of Class MyDate
• Main(String[] args) method read a given date from the command line, the format
of the given date is dd/mm/yyyy. You can assume the date input format is always correct,
but the input date may be invalid. For example, 31/02/2017 is not a valid date. If the
input date is not a valid date, the program will ask user to input another valid date. This
process will be repeated until a valid date is input or the input is the text “quit”. If the input
is “quit”, the program shall display “Quit MyCalendar, See You!”, then quit the program.
Otherwise, the valid input date shall be used to create the object (myDate) of class
MyDate, then the object (myCalendar) of class MyCalendar. The dayOfWeek()
method, weekOfMonth() method, and printMonth() method are used to display the
day of the week, the week of the month, and the calendar of the month for the input date by
the user.
• MyCalendar(MyDate newDate) is the parameterized constructor of class
MyCalendar.
• MyCalendar()is the default constructor of class MyCalendar.
• dayOfWeek() method returns the day of the week (WeekDay) for myDate.
• WeekDay is an enumeration data type which contains the days of the week, i.e., Monday,
Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday.
• weekOfMonth() method returns the week of the month for myDate.
• printMonth() method prints the calendar of the month for myDate
Programming Fundamentals - 4/5 -
2. MyDate Class
MyDate
- day: int
- month: int
- year: int
+ MyDate(int day, int month, int year)
+ MyDate()
+ getDay(): int
+ getMonth(): int
+ getYear(): int
+ getDate(): String
+ isValid(): Boolean
• MyDate(int day, int month, int year) is the parameterized constructor of
class MyDate.
• MyDate() is the default constructor of class MyDate.
• getDay() method returns the day of myDate object.
• getMonth() method returns the month of myDate object.
• getYear() method returns the year of myDate object.
• getDate() method returns a string in the format of “day/month/year” (such as
24/05/2020)
• isValid() method returns a Boolean value, i.e., true when the myDate object is valid,
and false otherwise (notes: January, March, May, July, August, October, December has
31 days. April, June, September, November has 30 days. February has 29 days in a leap year,
and 28 days in a common year.).
Programming Fundamentals - 5/5 -
Program Output
Your program must have same output as the following examples (note: user’s inputs are highlighted
by yellow color).
Example 1:
java MyCalendar 29/02/2019
29/02/2019 is not a valid date, please input a valid date or quit: quit
Quit MyCalendar, See You!
Example 2:
java MyCalendar 29/02/2019
29/02/2019 is not a valid date, please input a valid date or quit:
24/05/2020
24/05/2020 is a Sunday and locates in the fourth week of May 2020
The calendar of May 2020 is:
MON TUE WED THU FRI SAT SUN
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
(Note: Each column indicates a day and the column width are fixed to 3 characters. The distance
between two adjacent columns is fixed to three whitespaces as well.)
Submission:
- Please submit the single java file MyCalendar.java to Moodle
- The submitted file name must be MyCalendar.java. Files with other names will not be
marked.
- Submission via email is NOT acceptable.
NOTES:
Enquiries about the marks can only be made within a maximum of 1 week after the assignment results are
published. After 1 week the marks cannot be changed.
Mark deductions: compilation errors, incorrect result, program is not up to spec, poor comments,
poor indentation, meaningless identifiers, required numeric constants are not defined, the program
uses approaches which has not been covered in the lectures. The deductions here are merely a
guide. Marks may also be deducted for other mistakes and poor practices.