$25
You are going to become a junior student next semester and you expect your workload to be high as you plan to take some upper level technical electives. To better organize the tasks in all your future courses, you decide to use the skills you learned in VE280 and plan to write a program that can store, update and print all unfinished tasks in a course. Before starting to code, you need to choose a representation for tasks and courses.
For tasks, you decide to use the struct type. Tasks are identified by their types and indices, which means two different tasks will not have both the same type and the same index. A due date is also specified in a task.
For courses, you decide to use an ADT Course with three operations: updateTask adds a new task or updates the due date of an existing task; finishTask removes a task from the unfinished tasks of a course; print prints the unfinished tasks of a course.
For implementation details, please refer to the exercises below.
Ex.1
Related Topics: virtual function, interface
First of all, in order to hide all implementation details, you decide to realize the ADT Course as a homonym virtual base class in course.h . Regarding the implementation of this base class, you decide that the unfinished tasks are managed with an array tasks .
The three operations of this ADT are defined as follows in the base class:
1 void updateTask(const std::string &type, int index, int dueMonth, int dueDay);
2 // REQUIRES: dueMonth and dueDay are in normal range.
3 // MODIFIES: this 4 // EFFECTS: adds/updates Task index of type; throw exception if fails to add Task
5 void finishTask(const std::string &type, int index, int finishMonth, int finishDay);
6 // REQUIRES: Task index of type exists in tasks. finishMonth and finishDay are in normal range.
7 // MODIFIES: this 8 // EFFECTS: removes Task index of type
9 void print();
10 // EFFECTS: prints all unfinished tasks of this Course
"in normal range" means that there is no date like dueMonth=14 or dueMonth=4, dueDay=31 . In updateTask , if array tasks is full, then you need to throw an exception of the tooManyTasks type, which is a class defined in course.h .
print will print the course code and all elements in tasks in order. The implementation of print is already given to you, please do not modify it.
This ADT is implemented as a derived class TechnicalCourse in course.cpp . It has four protected data members:
1 Task *tasks; // Array of current unfinished tasks 2 int sizeTasks; // Maximum number of unfinished tasks in the array 3 int numTasks; // Number of current unfinished tasks in the array 4 std::string courseCode; // Course code, e.g. "VE280"
This derived class represents courses like VE280, which require to submit labs/projects via the online judgement system and submit other work via canvas.
When you create a new TechnicalCourse , numTasks should be initialized to 0, courseCode should be initialized according to the input. If the input argument size is specified, sizeTasks should be initialized as size ; otherwise, it should be initialized as the default value MAXTASKS , which is 4.
1 TechnicalCourse(const std::string &courseCode, int size = MAXTASKS);
As for the three methods:
updateTask takes the type , the index , the dueMonth and the dueDay of the task to be
updated as inputs. If the task already exists in the array tasks , you should update its dueMonth and dueDay . If it is a new task, inserts it at the end of tasks and throws an
exception of type tooManyTasks if tasks is full.
After inserting tasks whose type is "Lab" or "Project", you need to print a message:
1 <courseCode> <type> <index> is released! Submit it via oj!
After inserting tasks of type other than "Lab" and "Project", you need to print another message:
Example:
1 // ve281 is a pointer to an instance of TechnicalCourse with courseCode "VE281"
2 ve281->updateTask("Assignment", 1, 5, 10); 3 ve281->updateTask("Lab", 1, 5, 20); 4 ve281->updateTask("Project", 1, 5, 30);
5 ve281->updateTask("Lab", 1, 5, 15); // no message is printed since it already exists in tasks
If the task is finished before/on the due date, you need to print:
If you fail to finish the task before the due date, you need to print:
Example:
Example output:
Ex.2
Related Topics: subclass, inheritance, virtual function
You will start to take upper level technical courses next semester. UpperLevelTechnicalCourse inherits from TechnicalCourse , while there are two differences between them.
First, there is a new type of tasks, "Team Project". For this type of tasks, you need to cooperate with your teammates and share the codes through GitHub. So after inserting tasks of "Team Project", a message should be printed:
Second, since the number of tasks in a upper level technical course are a lot larger than the number of tasks in a technical course, you can only finish all the tasks before due by following the principle of "Earliest Deadline First". So the tasks in these courses will be ordered according to their due dates, where the task with earlier due date will be put in the front.
So, you need to rewrite updateTask of this class. When inserting a new task, it needs to decide the position of this task in array tasks according to its due date. Also, when updating the due date of an existing task, it should also modify the position of this task in tasks . If the task exists and the due date does not change, updateTask will do nothing. If two tasks have the same due dates, the one that is updated earlier will be in the front.
Example:
Example output:
Testing
Since more than one instance of the class is needed, a function, with overload, is provided to create them dynamically:
1 Course *create(const std::string &classType, const std::string &courseCode); 2 Course *create(const std::string &classType, const std::string &courseCode, int taskSize);
classType could be either "Technical" or "Upper Level Technical". courseCode specifies the
course code of the course you want to create. If classType is "Technical", then it returns a pointer to an instance of TechnicalCourse with courseCode ; if classType is "Upper Level
Technical", then it returns a pointer to an instance of UpperLevelTechnicalCourse with courseCode ; if classType is not "Technical" or "Upper Level Technical", it returns a null pointer.
Besides, if taskSize is true, the maximum number of tasks in this course at a time is specified by taskSize ; otherwise, the maximum number of tasks is the default value MAXTASKS .
Example:
Example output:
This simpletest.cpp is just an example and you still need to write test cases yourself to get full scores.
Please make sure there is no memory leak!
You can use valgrind --leak-check=full ./lab7 to run the program and check memory leaks.
Or you can add -fsanitize=leak -fsanitize=address when compiling the program.