---
title: Computer Programming II Lab 2 v1.0
---
<h1 style='border: none'><center>Computer Programming II Lab 2</center></h1>
<h2 style='border: none'><center>Introduction to Object-Oriented Programming</br>Classes and Objects</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Authors: Usama R. Al Zayan<span style="float:right">2023/02/08</span></h6>
<h6>Parts of this Lab were adapted from work done by Mohammed Nafiz ALMadhoun</h6>
---
## Lab 2: Time and Plan
| Tasks | Timing |
| -------- | -------- |
| Quiz 1 | 10 min |
| Task 1 | 10 min |
| Task 2 | 40 min |
| Task 3 | 20 min |
## Lab Task
Create class called MyDate, which models a date instance, is defined as shown in the class diagram.

### Task 1 (5 marks)
<p>The <code>MyDate</code> class contains the following <code>private</code> instance variables:</p>
<ul>
<li><code>year</code> (<code>int</code>): Between <code>1</code> to <code>9999</code>.</li>
<li><code>month</code> (<code>int</code>): Between <code>1</code> (Jan) to <code>12</code> (Dec).</li>
<li><code>day</code> (<code>int</code>): Between <code>1</code> to <code>28|29|30|31</code>, where the last day depends on the month and whether it is a leap year for Feb (<code>28|29</code>).</li>
</ul>
<p>It also contains the following <code>public</code> <code>static final </code> variables (drawn with underlined in the class diagram):</p>
<ul>
<li><code>MONTHS</code> (<code>String[]</code>), <code>DAYS</code> (<code>String[]</code>), and <code>DAY_IN_MONTHS</code> (<code>int[]</code>): <code>static</code> variables, initialized as shown, which are used in the methods.</li>
</ul>
<p>The <code>MyDate</code> class has the following <code>public</code> <code>static</code> methods (drawn with underlined in the class diagram):</p>
<ul>
<li><code>isLeapYear(int year)</code>: returns <code>true</code> if the given <code>year</code> is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400.</li>
<li><code>isValidDate(int year, int month, int day)</code>: returns <code>true</code> if the given <code>year</code>, <code>month</code>, and <code>day</code> constitute a valid date. Assume that <code>year</code> is between <code>1</code> and <code>9999</code>, <code>month</code> is between <code>1</code> (Jan) to <code>12</code> (Dec) and <code>day</code> shall be between <code>1</code> and <code>28|29|30|31</code> depending on the <code>month</code> and whether it is a leap year on Feb.</li>
<li><code>getDayOfWeek(int year, int month, int day)</code>: returns the day of the week, where <code>0</code> for Sun, <code>1</code> for Mon, ..., <code>6</code> for Sat, for the given date. Assume that the date is valid. Read the <a href="https://www3.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html#dateutil">earlier exercise on how to determine the day of the week</a> (or Wiki "Determination of the day of the week").</li>
</ul>
### Task 2 (20 marks)
<p>The <code>MyDate</code> class has one constructor, which takes 3 parameters: <code>year</code>, <code>month</code> and <code>day</code>. It shall invoke <code>setDate()</code> method (to be described later) to set the instance variables.</p>
<p>The <code>MyDate</code> class has the following <code>public</code> methods:</p>
<ul>
<li><code>setDate(int year, int month, int day)</code>: It shall invoke the <code>static</code> method <code>isValidDate()</code> to verify that the given <code>year</code>, <code>month</code> and <code>day</code> constitute a valid date.
<li><code>setYear(int year)</code>: It shall verify that the given <code>year</code> is between <code>1</code> and <code>9999</code>.
<li><code>setMonth(int month)</code>: It shall verify that the given <code>month</code> is between <code>1</code> and <code>12</code>.
<li><code>setDay(int day)</code>: It shall verify that the given <code>day</code> is between <code>1</code> and <code>dayMax</code>, where <code>dayMax</code> depends on the <code>month</code> and whether it is a leap year for Feb.
<li><code>getYear()</code>, <code>getMonth()</code>, <code>getDay()</code>: return the value for the <code>year</code>, <code>month</code> and <code>day</code>, respectively.</li>
<li><code>toString()</code>: returns a date string in the format "<code>xxxday d mmm yyyy</code>", e.g., "Tuesday 14 Feb 2012".</li>
<li><code>nextDay()</code>: update <code>this</code> instance to the next day and return <code>this</code> instance. Take note that <code>nextDay()</code> for <code>31 Dec 2000</code> shall be <code>1 Jan 2001</code>.</li>
<li><code>nextMonth()</code>: update <code>this</code> instance to the next month and return <code>this</code> instance. Take note that <code>nextMonth()</code> for <code>31 Oct 2012</code> shall be <code>30 Nov 2012</code>.</li>
<li><code>nextYear()</code>: update <code>this</code> instance to the next year and return <code>this</code> instance. Take note that <code>nextYear()</code> for <code>29 Feb 2012</code> shall be <code>28 Feb 2013</code>.
<li><code>previousDay()</code>, <code>previousMonth()</code>, <code>previousYear()</code>: similar to the above.</li>
</ul>
### Task 3 (5 marks)
Write the code for the MyDate class.
Use the following test statements to test the MyDate class:
```java=
MyDate d1 = new MyDate(2012, 2, 28);
System.out.println(d1); // Tuesday 28 Feb 2012
System.out.println(d1.nextDay()); // Wednesday 29 Feb 2012
System.out.println(d1.nextDay()); // Thursday 1 Mar 2012
System.out.println(d1.nextMonth()); // Sunday 1 Apr 2012
System.out.println(d1.nextYear()); // Monday 1 Apr 2013
MyDate d2 = new MyDate(2012, 1, 2);
System.out.println(d2); // Monday 2 Jan 2012
System.out.println(d2.previousDay()); // Sunday 1 Jan 2012
System.out.println(d2.previousDay()); // Saturday 31 Dec 2011
System.out.println(d2.previousMonth()); // Wednesday 30 Nov 2011
System.out.println(d2.previousYear()); // Tuesday 30 Nov 2010
MyDate d3 = new MyDate(2012, 2, 29);
System.out.println(d3.previousYear()); // Monday 28 Feb 2011
// MyDate d4 = new MyDate(2099, 11, 31); // Invalid year, month, or day!
// MyDate d5 = new MyDate(2011, 2, 29); // Invalid year, month, or day!
```
###### tags: `Computer Programming II` `Lab` `IUG` `Computer Engineering`
<center>End Of Lab 2</center>