Programming Assignments 1B:

Basics of User-defined Classes: Keep Polishing the DateType Class

Purpose: Keep polishing the DateType class.

********************************************************************

 

Programming #1B Steps:

 

1.     Revise DateType.h to add three additional member functions (i.e. methods, services) into the DateType class:

 

Revise the declaration of DateType class in DateType.h by adding

        a new member function void AdvanceDays(int NumDays) that should modify the date by advancing it to a date NumDays days away in the future,

        a new member function void BackDays(int NumDays) that should modify the date by backing it down to a date NumDays days away in the past, and

        a new member function bool IsValidDate(int newMonth, int newDay, int newYear) that would return true if the given information compose a valid date and return false otherwise.

 

Remark: For the void AdvanceDays(int NumDays) member function, if it is called with a negative NumDays value, AdvanceDays(NumDays) should work exactly like BackDays(-NumDays) to back up into the past. Similarly, for the void BackDays(int NumDays) member function, if it is called with a negative NumDays value, BackDays(NumDays) should work exactly like AdvanceDays(-NumDays) to advance into the future.

 

 

2.     Revise DateType.cpp to add C++ code to implement the three new member functions above:

 

Notes about the implementation of AdvanceDays (and BackDays similarly):

                Basic approach for implementing AdvanceDays: for The easiest way to advance the current date some (non-negative) NumDays days into the future is to use a loop that repeats for NumDays iterations. Inside the loop, write your C++ code such that it can correctly just advance the date one day into the future on each iteration of the loop. Note that in the is approach (i) you need to precisely detect whether the current date is in the end of the month, and be careful especially about February since it depends on whether it is a leap year, (ii) when it is not the end of the month advancing one day into the future is very easy, but (iii) when it is the end of the month, you have to carefully advance the date into the first day of the next month, be careful especially when it is December 31.

                Other approaches for implementing AdvanceDays: If you have time, there are additional things you can do to make it work even faster. For example, when NumDays is greater than 365, you can advance one year at a time (i.e. advancing 365 days or 366 days depending on whether Feb. 29 is encountered in the advancement). Similarly you can consider incorporation of advancing by month and advancing by century too.

                Basic approach for implementing BackDays: The easiest way to back down the current date some (non-negative) NumDays days into the past is to use a loop that repeats for NumDays iterations. Inside the loop, write your C++ code such that it can correctly just back down the date one day into the past on each iteration of the loop. Note that in the is approach (i) you need to precisely detect whether the current date is in the beginning of the month, (ii) when it is not the beginning of the month backing down one day into the past is very easy, but (iii) when it is the beginning of the month, you have to carefully back down the date into the very last day of the previous month, and you need to be careful especially when it is Jan. 1 or March 1 (since the previous day could be Feb. 28 or Feb 29 depending on whether it is a leap year).

                Other approaches for implementing AdvanceDays: See similar ideas regarding other approaches for implementing AdvanceDays.

 

 

 

Note about the implementation of IsValidDate

 

                How to determine valid dates: You can reuse what you did in Programming #3 and #5C in CSCI 105 last semester to implement this member function. Essentially, you need to use the if statement or the switch statement to check to return true only when the value in the data member day falls in the range of (i) 1 to 31 if the data member month indicates a month of 31 days, or (ii) 1 to 30 if the data member month indicates a month of 30 days, or (iii) 1 to 28 if the data member month indicates it is February while the data member year indicates it is not a leap year, or (iv) 1 to 29 if the data member month indicates it is February while the data member year indicates it is a leap year.

 

                How to determine leap years: A year is a leap year if it is divisible by 4 except that any year divisible by 100 is a leap year only if it is also divisible by 400. So 1900 is not a leap year, but 2000 is. In other words, (i) a year (e.g. 1996) is a leap year if it is divisible by 4 but not by 100, (ii) a year (e.g. 2000) is a leap year if it is divisible by 400, (iii) otherwise, it is not a leap year.

 

3.     Test the implementation: uncomment the part of code labeled as options A, B, and T (i.e. tests 4, 5, 6) in the main function in DateTest.cpp and then run the program to test your implementation in step 2 above to make sure you have correctly implemented the member functions as we want.

 

4.     Revise the implementation of three existing member functions in DateType.cpp to make them robust enough to reject invalid dates: With the IsValidDate member function implemented, now you can call it to check whether it is a valid date given the day, the month, and the year as three integers. Use IsValidDate and do revisions described in the following.

 

        The second constructor DateType(int month, int day, int year): Now revise the implementation of your second constructor in DateType.cpp so that when an illegal date like 18/99/2003 is given they will output an error message and initialize the date to the default date 1/1/2000 instead.

        ReadDate: Revise the implementation of your ReadDate member function in DateType.cpp such that (i) after the user enter three integers for the month, the day, and the year, you use a loop to check whether they form a valid date, (ii) the loop will repeat as long as what are entered do not form a valid date, and (iii) in each iteration the code in the loop should inform the user it is not a valid date and ask the user to enter a new date again. Such a loop ensures a valid date will be entered eventually.

        SetDate: Revise the implementation of the SetDate member function in DateType.cpp so that (i) when the given date information is valid, it will sets the date accordingly and return true and (ii) when an illegal date like 18/99/2003 is given it will return false and leave the contents of the date unchanged.

5.     Test the implementation: Run the program and use option R and option S to test your implementation in step 4 above to make sure you have correctly implemented the member functions as we want.

 

 

Submit your work for Programming #1B:

       Two things to submit: (i) Compress your entire Program 1B folder into a zip file and upload it through Biola Canvas. (ii) Carefully fill out this self-evaluation report and upload it through BiolaCanvas.

 

       Note that you will receive no point for missing the self-evaluation report or missing the integrity review in the report.

 

********************************************************************