Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


CS2201

Spring 2022

Assignment No. 1

Purpose: Gain experience in defining a C++ class. Gain experience in using and manipulating static C++ arrays, particularly partially-filled arrays.

Assignment: This assignment deals with an implementation of a calendar which stores a collection ofreminders. In this   case, the reminders will be stored in a static array. The reminders will be stored in sorted (chronological) order. The array will be able to contain up to MAX_REM (currently defined to be 50) reminders. Since not all elements ofthe array may   contain reminders that are a part of the calendar (i.e., we have a partiallyfilled array), we need to keep track ofthe            number of elements that we are using in the array. The Calendar.h file describes all the functions provided by the class.

You will be provided with the source code that supports the reminders. Yourjob is to implement the Calendar class. Functional Specifications: You will be supplied seven files:

Date.h: declaration ofthe Date class (DNC)

Date.cpp: definition ofthe Date class (DNC)

Reminder.h: declaration ofthe Reminder class (DNC)

Reminder.cpp: definition ofthe Reminder class (DNC)

Calendar.h: declaration ofthe Calendar class.

Calendar.cpp: definition of the Calendar class, which currently holds only method stubs. Your job is to replace the method stubs with correct code to implement each ofthe methods.

CalTest.cpp: initial test program. You should replace this with the test program you developed in Project 0.

The Calendar you have to implement is a static array ofReminder objects. A predetermined maximum size ofthe static     array is defined by the constant identifier MAX_REM. The Calendar class consists of the static array named remArr,      and a variable numRem that keeps track of the number of array elements that are currently being used. You should store    Reminders in the array in sorted order (based on the chronological order of the dates). The existing method stubs in           Calendar.cpp have detailed comments describing the desired behavior of the method (they are the same as the comments   in Calendar.h). You should not change files marked DNC (DNC == Do Not Change). Changes to Calendar.h are limited to only adding private helper methods (which are optional; helper methods are not required).

Here are the methods ofthe Calendar class that you are to write:

Calendar class

Methods

Function

Calendar()

Default constructor creates an empty Calendar

getNumRem()

Return the total number ofReminders in the Calendar

addRem(const Reminder &r)

Add a reminder to the Calendar

getRem(size_t index)

Returns the reminder at the specified index

toString ()

Return a string representation of the calendar, i.e., a string of all the reminders in chronological order

displayRem(size_t index)

Return a string ofthe reminder at a particular index

displayRem(const string& str)

Return a string of all reminders whose message matches the provided string

displayRem(const Date& d)

Return a string of all reminders for a given date

displayRem(const Date& d1, const Date& d2)

Return a string of all reminders in a range oftwo given dates

findRem(const Date& d)

Find first reminder for the given date and return its index

findRem(const string& str)

Find first reminder with the given message and return its index

deleteRem()

Deletes all reminders earlier than today’s date

deleteRem(size_t index)

Deletes the reminder at a provided index position

deleteRem(const string& str)

Delete all reminders whose message matches a given string

deleteRem(const Reminder& rem)

Delete all occurrences ofthe given reminder

deleteRem(const Date& d)

Deletes all reminders on a particular date

deleteRem(const Date& d1,

const Date& d2)

Deletes all reminders between a range oftwo given dates

Implementation details: Here are a few notes that might be helpful:

1.   You are to download a zip file that is provided with this specification. The zip file contains a CLion project that

includes the starter code for this project. You must unzip/extract the files before you work on them. Once you unzip    the file, you can open the project by starting CLion, then specify that you want to “Open Project”, and then navigate    to the folder you just extracted (please review the instructions that were given with project #0 on how to open              provided CLion projects). When you open the project, let CLion do its initialization work and load symbols. The code as provided compiles cleanly and the program should run as given – though the results are incorrect since required       functionality is missing. Again, you must unzip/extract the files before you work on them. Again, please review the     instructions that were given with project #0 on how to open provided CLion projects and make sure your                      CMakeLists.txt file contains the required compiler flags.

2.   Write your code in terms ofMAX_REM (the predefined maximum size of the static array) and not the constant 50.      We may want to change the size of the array in the future, and such a change should only require changing one line of code.

3.   Note that you do not need to implement the methods in the order they appear in the file. You can implement them in   any order. So THINK first! It is also highly recommended that you write your code incrementally. You should            implement one or two methods at a time and fully test them before moving on to other methods. You can simply         comment out code in the CalTest.cpp program that is testing methods that you have not yet implemented, then            uncomment the code when you want to reactivate those tests (CLion has the ability to quickly comment/uncomment   highlighted code with an option found under the Code drop-down menu). You may also want to enhance your test      code as you go along, so that it does a better job of testing “edge conditions” – conditions that are valid but are out of the norm.

4. You will likely be performing some operations with C++ strings. C++ string operations are described here.

5.   The class constructors are required to use the base member initialization list as much as possible.

7.   An additional stipulation ofthe addRem() method is that it must run in linear time. This implies that you must insert the newly added reminder in its correct location, shifting other reminders in the array up to make room for it. Ifyou     simply add the new reminder at the end of the array and then sort the array you will lose points for efficiency.

8.   Several methods of the Calendar class have parameters of type size_t (an unsigned integer type). The compiler may      give you conversion warning messages ifyou attempt to assign/compare such a parameter to a variable oftype int.       Your homework submissions are expected to compile cleanly (no errors or warnings) in CLion with the clang               compiler. To eliminate warning about size_t conversions, you should use variables oftype size_t when appropriate, or you can cast the value of the size_t parameter to an int when necessary, or cast int values to size_t as appropriate.

9.   The Calendar.h specifies that some methods throw an exception if someone attempts to perform an operation that is    not allowed (e.g., adding a reminder to a full calendar). To have your method throw an exception in C++, you can use the follow statement: throw std::overflow_error("Array is full."); This statement requires that  you include the following at the top ofyour Calendar.cpp file:   #include . Note 1: leave the         #include statement in the .cpp file regardless ofwhat the CLion IDE tells you. Note 2: there is no “new” operation      needed to throw exceptions as in Java. You do not need to specify that the method may throw an exception; you can    simply throw the exception ifthe error condition occurs. More information on C++ exceptions can be found in            Chapter 21 of our text (zyBooks). Note: the Calendar class simply throws exceptions in error situations – it does not   attempt to handle the error. Thus your Calendar class should not contain any try-catch blocks – that is the job ofthe    program that is using the Calendar class (i.e., the CalTest.cpp program).

10. Since we are using a static array in the Calendar class, the C++ compiler takes care of creating the array for you     whenever a Calendar object is created. Your constructor should not have any “new” operations (I state this for the benefit of the Java programmers in the class).


11. The array ofReminders used to represent the Calendar will be apartially-filled array. See the information on        partially-filled arrays below. Note that you will lose points ifyour class methods do unnecessary work; i.e., ifyou process elements of the array that are not currently being used to represent the Calendar.

12. Ifyou do not understand how a method should work from its description, then can go back to project #0 and try things out – you were given a working Calendar class after all.

Partially filled arrays: Here is some additional information on what is meant by a "partiallyfilled array".

In C++ (and Java too), once an array is created its size is set. You cannot make it bigger, nor can you make it smaller. If  you ever wanted the array to be bigger/smaller, you would have to reallocate a completely new array (something we will see in lecture very soon). In this assignment, we are never reallocating a new array (nor can we do so due to the way the  array was declared).

In C++, once an array is created every element ofthe array contains a data item ofthe declared type ofthe array. I.e.,        every element of an integer array contains an integer; and every element of a Reminder array contains a Reminder object.

Now, there are times when we don't know ahead of time how many items we will want to store in our array. In such cases, we have to make a reasonable guess at the maximum size needed and allocate an array ofthat size. In this situation, we      will not be using all the array elements all the time, so we have to keep track ofthe number of array elements that we are   actually using in the array and we will use the contiguous array elements starting at index zero. This is known as a partiallyfilled array. To make this work we need to keep track ofthree separate things:

1) the array where we are storing data (e.g., remArr)

3) the number of elements of the array that are currently being used (e.g., numRem); again, the numRem data items are stored in the array from index 0 to index numRem- 1.

In this partially filled array, you cannot set array elements to some special value to indicate that the element is currently   "not used" or "deleted". What value would you use? It would have to be some value that could not be a valid value added by the user -- but any value you can create to indicate "not used" the user could also potentially create to add to the array.

If you ever need to know if an array element is currently being used, you only need to consider the value ofnumRem. All  elements from index 0 to index numRem- 1 are used, and all elements from index numRem to index MAX_REM- 1 are not used.

To add an item to a partially filled array (assuming there is space available), you need to increment the count of used        elements and place the new item in the appropriate position of the array (shifting existing data up ifnecessary). To delete an item from a partially filled array, you need to decrement the count of used elements and shift data down to fill in the    hole left by the deleted element. Deleting will cause some elements of the array to change from being in the "used"           portion of the array to being in the "unused" portion ofthe array. There is no need to set these elements to some special    value -- since they are now in the unused portion of the array, we should not be accessing those values any longer.

When processing the data of a partially filled array, you only process the array elements that lie in the portion that is         currently being used. Any operations you perform on the unused portion of the array are unnecessary (and thus wasteful).