class Date - Exercise 8.8

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
1,072
Modify class \(\displaystyle \text{Date}\) of Fig. \(\displaystyle 8.7\) to perform error checking on the initializer values for instance variables month, day and year (currently it validates only the month and day). Provide a method nextDay to increment the day by one. Write a program that tests method nextDay in a loop that prints the date during each iteration to illustrate that the method works correctly. Test the following cases:

\(\displaystyle \bold{a)}\) incrementing into the next month and
\(\displaystyle \bold{b)}\) incrementing into the next year.
 
Modify class \(\displaystyle \text{Date}\) of Fig. \(\displaystyle 8.7\) to perform error checking on the initializer values for instance variables month, day and year (currently it validates only the month and day). Provide a method nextDay to increment the day by one. Write a program that tests method nextDay in a loop that prints the date during each iteration to illustrate that the method works correctly. Test the following cases:

\(\displaystyle \bold{a)}\) incrementing into the next month and
\(\displaystyle \bold{b)}\) incrementing into the next year.
show us your effort/s to solve this problem.
 
Let us know what is the basic idea of class \(\displaystyle \text{Date}\) before we can modify it. The basic idea is that this class contains three variables:
\(\displaystyle \text{day}\)
\(\displaystyle \text{month}\)
\(\displaystyle \text{year}\)
and an array \(\displaystyle \text{daysPerMonth}\) that contains the days for each month of the year. If you pass any date into this class constructor, something like \(\displaystyle \text{Date(19,5,1945)}\), it will simply tells you that the date is: \(\displaystyle 19/5/1945\).

This class \(\displaystyle \text{Date}\) is very smart. If you pass wrong information into its constructor, something like \(\displaystyle \text{Date(29,2,2025)}\), it will tell you that you entered invalid date as we know that the year \(\displaystyle 2025\) is not a leap year. In other words, the month of February in \(\displaystyle 2025\) contains only \(\displaystyle 28\) days.

While the main idea of class \(\displaystyle \text{Date}\) seems very simple, implementing this class with coding requires advanced techniques. In the next post, we will code the first part of this class and we will randomly print dates including wrong dates. Later when we develop our class, it will validate the input which means only valid data will output dates.

💪😡
 
I created class \(\displaystyle \text{Date}\). It has three parameters, \(\displaystyle \text{day}\), \(\displaystyle \text{month}\) and \(\displaystyle \text{year}\).

I tested my constructor with the following dates:

\(\displaystyle \text{Date(5,6,1979)} \rightarrow \) Correct date: \(\displaystyle 5/6/1979\)
\(\displaystyle \text{Date(5,2,2025)} \rightarrow \) Today's date: \(\displaystyle 5/2/2025\)
\(\displaystyle \text{Date(99,99,1979)} \rightarrow \) Wrong date: \(\displaystyle 99/99/1939\)

The constructor of class \(\displaystyle \text{Date}\) is still stupid. It will output everything you pass through it, even wrong dates. In the next post, we will develop our code so that the constructor only accepts valid dates.

Here is the code so far with a screenshot:

package Moon;

public class Date {
int day;
int month;
int year;

public Date(int day, int month, int year){
this.day = day;
this.month = month;
this.year = year;
}

public String toString(){
return String.format("%d/%d/%d",day,month,year);
}

public static void main(String[] args){
Date date1 = new Date(5,6,1979);
Date date2 = new Date(5,2,2025);
Date date3 = new Date(99,99,1939);
System.out.println("Correct date: " + date1);
System.out.println("Today: " + date2);
System.out.println("Wrong date: " + date3);
}
}

Date.png
 
I added to my code a new array \(\displaystyle \text{daysPerMonth}\) to include the days of each month. I also added \(\displaystyle 7\) methods:

\(\displaystyle \text{setDate}\)
\(\displaystyle \text{setDay}\)
\(\displaystyle \text{setMonth}\)
\(\displaystyle \text{setYear}\)
\(\displaystyle \text{getDay}\)
\(\displaystyle \text{getMonth}\)
\(\displaystyle \text{getYear}\)

With those methods, the date will get validated before it is printed. If the date is wrong, the program will tell you that \(\displaystyle \text{Wrong day}\) or \(\displaystyle \text{Wrong year}\). If the month is wrong, you will get a runtime error. If the date is wrong, the program will print the default values of \(\displaystyle \text{day}\) and \(\displaystyle \text{year}\) which is zero. In the next post, we will fix the \(\displaystyle \text{month}\) runtime error and the printed default values of \(\displaystyle \text{day}\) and \(\displaystyle \text{year}\).

I tested this date:
\(\displaystyle \text{Date(day,month,year)} = \text{Date(29,2,-111)} = 0/2/0\)

Here the code so far with a screenshot:

package Moon;

public class Date {
int day;
int month;
int year;

int daysPerMonth[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

public Date(int day, int month, int year){
setDate(day,month,year);
}

public void setDate(int d, int m, int y){
setDay(d,m);
setMonth(m);
setYear(y);
}

public void setDay(int d, int m){
if (d > 0 && d <= daysPerMonth[m]){
this.day = d;
} else {System.out.println("Wrong day.");}
}

public void setMonth(int m){
if (m > 0 && m <= 12){
this.month = m;
} else {System.out.println("Wrong month.");}
}

public void setYear(int y){
if (y > 0){
this.year = y;
} else {System.out.println("Wrong year.");}
}

public int getDay(){
return day;
}

public int getMonth(){
return month;
}

public int getYear(){
return year;
}

public String toString(){
return String.format("%d/%d/%d",getDay(),getMonth(),getYear());
}

public static void main(String[] args){
Date date = new Date(29,2,-111);
System.out.println("My birthdate is : " + date);
}
}

Date_2.png
 
I updated my code further so that it will tell you exactly what was invalid.

I included the class \(\displaystyle \text{IllegalArgumentException}\) to throw messages whenever the date is wrong. I also added the try and catch technique which is a good way to let the code continue executing even if invalid dates were entered.

I tested the following:

\(\displaystyle \text{Date(day,month,year)} = \text{Date(45,3,1979)} \rightarrow 45/3/1979\). Wrong day: \(\displaystyle 45\).
\(\displaystyle \text{Date(day,month,year)} = \text{Date(5,13,2002)} \rightarrow 5/13/2002\). Wrong month: \(\displaystyle 13\).
\(\displaystyle \text{Date(day,month,year)} = \text{Date(18,7,-11)} \rightarrow 18/7/-11\). Wrong year: \(\displaystyle -11\).
\(\displaystyle \text{Date(day,month,year)} = \text{Date(21,4,2003)} \rightarrow 21/4/2003\). Correct.

\(\displaystyle \text{Date(day,month,year)} = \text{Date(29,2,2024)} \rightarrow 29/2/2024\). Wrong day: \(\displaystyle 29\).

My code thinks the date \(\displaystyle 29/2/2024\) is wrong😱because it still does not understand leap years. In the next post we will fix the issue of the leap year.

Here is a screenshot of I have done so far:

Date_3.png
 
I edited my code to include all the required functionalities before modification.

I tested the following:

\(\displaystyle 45/3/1979: \) Wrong.
\(\displaystyle 5/13/2002: \) Wrong.
\(\displaystyle 18/7/-11: \) Wrong.
\(\displaystyle 21/4/2003: \) Correct.
\(\displaystyle 29/2/2024: \) Correct. Now the code understands if it's a leap year, so we have fixed that issue.

In the next post we are ready to modify the class \(\displaystyle \text{Date}\)💪🤠

Here is a screenshot to give you a taste of the output.

Date_4.png
 
Top