Objectives

This assignment requires you to create a calendar program to calculate the day of week and the week of month for a given date, as well as to print the calendar as required.

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.
  • Other requirements as stated in Submission section.
  • 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:
/**
* CSIT/DPIT111 or CSIT811 Assignment 2
*
* Student name:
* Student user ID:
* Student number
*/

NOTE: Please indicate your subject code either CSIT/DPIT111 or CSIT811 as different requirements are assessed.

Background

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, 19 October 2018 is a Friday and locates in the third week of October 2018.

In this assignment, you should use the following formula (Zeller's congruence) to calculate the day of a week for any given date after 15 October 1582.

h = (q + (13(m + 1) / 5) + K + (K/4) + (J/4) + 5J) mod 7,

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.

NOTE: In this formula January and February are counted as months 13 and 14 of the previous year. e.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009.

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 evaluates as

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

which is a 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=0,J=20, so the formula evaluates as

h = (1 + (13 * (3 + 1) / 5) + 0 + (0/4) + (20/4) + 5 * 20) mod 7
= (1 + (52/5) + 0 + (0/4) + (20/4) + 100) mod 7
= (1 + 10.4 + 0 + 0 + 5 + 100) mod 7
= (1 + 10 + 0 + 0 + 5 + 100) mod 7
= 4

which is a Wednesday.

You may use any formula to determine the leap year.

Tasks

You will create one program called MyCalendar.java. You should implement the MyCalendar and MyDate classes according to the following UML class diagrams.

You can add additional methods, but the existing attributes and methods cannot be modified.

MyCalendar
-----------------------
- myDate: MyDate
-----------------------
+ main(String[])
+ MyCalendar (MyDate)
+ getDayOfWeek(): Day
+ getWeekOfMonth(): int
+ printDateInfo(): void
+ printCalendar(): void
  • Day is an enumerated data type which contains the days of the week, i.e., Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday.
  • MyCalendar is the constructor of the class MyCalendar.
  • getDayOfWeek method will return the day of the week for the given date.
  • getWeekOfMonth method will return the week of the month for the given date.
  • printDateInfo methods will print the information of the given date. The date is printed as dd/mm/yyyy and the ordinal week of the month in which the date is located.

For example, for the date of 19 October 2018 it should print as

19/10/2018 is a Friday and located in the third week of October 2018
  • printCalendar method will print a calendar as follows. Refer to the Testing section for the required output format.
    • For CSIT/DPIT111 students: it prints the calendar of the given month.
    • For CSIT811 students: it prints the calendar of the given year.
  • main method will take a valid date from the command line as shown in the Testing section. 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 entered by the user. The valid input date will be used to create an instance of the class MyCalendar. The main method will also invoke the print Date Info and printCalendar methods of a MyCalendar object respectively.
MyDate
------------------------
- day: int
- month: int
- year: int
------------------------
+ MyDate (int, int, int)
+ getDay(): int
+ getMonth(): int
+ get Year(): int
+ isDateValid(): boolean
  • MyDate is the constructor of the class MyDate.
  • get Day method will return the day.
  • getMonth method will return the month.
  • getYear method will return the year.
  • isDateValid method will return true when the date 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.).

Testing

The output format shown in this section is a formative part of the requirements that your program should comply with.

The following is a sample screenshot of running the correct and complete program that you can compare with your program.

(User input is in red colour, results in blue colour and prompt lines in black colour. Colours in this paper are for readability only. Your program does not need to print in colour.)

For CSIT/DPIT111 students:

Enter the date as day month year: 10 10 1582
Please enter a valid date
Enter the date as day month year: 29 2 2018
Please enter a valid date
Enter the date as day month year: 32 10 2018
Please enter a valid date
Enter the date as day month year: 19 10 2018

19/10/2018 is a Friday and located in the third week of October 2018

The calendar of October 2018 is:

SUN MON TUE WED THU FRI SAT
___ 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_
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.