Question: CSE 240 Homework 9: C++ Class and Input/Output Fall 2017 (50 points) Due Saturday, October 21, 2017 at 11:59PM, plus a 24-Hour grace period Introduction
CSE 240 Homework 9: C++ Class and Input/Output
Fall 2017 (50 points)
Due Saturday, October 21, 2017 at 11:59PM, plus a 24-Hour grace period
Introduction
The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures and input/output. By the end of the assignment, you should have understood
Class, data members and function members
Classes and header files
Memory management and garbage collection
C++ input and output
Reading: Textbook Chapter 3, Sections 3.1, 3.2, and 3.3 on C++ examples, class definition and memory management.
Preparation: Complete the multiple choice questions in the textbook exercise section. The answer keys can be found in the course Web site. These exercises can help you prepare for your weekly quiz and the exam. You are encourage to read the other exercise questions and make sure you understand these questions in the textbook exercise section, which can help you better understand what materials are expected to understand after the lectures and homework on each chapter.
You are expected to do the majority of the assignment outside the class meetings. Should you need assistance, or have questions about the assignment, please contact the instructor or the TA during their office hours.
You are encouraged to ask and answer questions on the course discussion board. However, do not share your answers and code in the course discussion board.
Programming Assignment (50 points)
You are given a partially completed project containing:
3 header files:
Container.h
Pet.h
Checkup.h
4 C++ files:
Container.cpp
Pet.cpp
Checkup.cpp
hw09.cpp
You will create an empty C++ project.
Note, do not create the project in the default folder. You will have a permission error when you run your program. Change the folder to your own location.
Then you add the header files and CPP file into your project, into different folder, respectively, as shown in the following screenshot:
Your job is to follow the instructions given in the comments of the hw09.cpp and Pet.cpp files to complete the missing parts of the project so that the program executes properly.
Q1: Constructor and Accessor Methods for Pet class (5 points)
You will need to write the constructor and accessor methods for the Pet class in the Pet.cpp file. The program will not compile until this part is completed. The constructor and accessor methods are already declared in the Pet.h file. (See Pet.cpp file for details).
Q2: Add Checkup and Last Checkup Methods for Pet class (10 points)
You will need to write these methods for the Pet class in the Pet.cpp file. The program will not compile until this part is completed. These methods are already declared in the Pet.h file. (See Pet.cpp file for further instructions).
Q3: Add Pet Function (5 points)
When inputting a new pet, the user is prompted for the pets name and breed. This function will be used to add a new pet to the head of the global linked list of containers. This means that no sorting is needed for this function. You will notice that the Search Pet function is called before this function, therefore, you are to assume that the Pet that you are looking for is not already on the list. (See hw09.cpp file for further instructions).
Q4: Remove_checkup (5 points)
Write a recursive function to implement the function. You will not receive any point if you use a while loop. This function will be called in the remove-pet function to remove all the checkups linked to the pet.
Use proper memory management to ensure no memory leaks.
Q5: Remove Pet Function (10 points)
When removing a Pet from the list, the user is prompted for the pets name and breed. You will use these two string parameters to search for the Pet on the list and remove them. You will need to do so in a way that ensures no memory leaks. You will notice that the Search Pet function is called before this function, therefore, you are to assume that the Pet that you are looking for is already on the list. You must call the remove_checkups function to remove all the checkups. (See hw09.cpp file for further instructions).
Q6: Implement cin / cout for the 7 lines in main (5 points)
You will notice that main in hw09.cpp uses printf statements and getchar(). Update these statements to use cin and cout without changing the functionality of the statements.
You will also use a recursive function to implement the print_all function. You will not receive any point if you use a while loop. (6 points for the code)
Please use comments to indicate the four steps (1 point each step):
(1) size-n problem, (2) stopping condition and return value, (3) size-(n-1) problem, and (4) construct the solution of size-n problem based on size-(n-1) problems solution.
// READ BEFORE YOU START:
// You are given a partially completed program that creates a list of pets with their list of checkups.
// Each pet has the corresponding information: name, breed, and a linked list of checkups.
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases.
//
// You are to assume that all input is valid:
// Valid name: String containing alphabetical letters beginning with a capital letter
// Valid breed: String containing alphabetical letters beginning with a capital letter
// Valid date: String in the following format: "MM/DD/YYYY" ex: "01/01/2010"
// All input will be a valid length and no more than the allowed amount of memory will be used
//
// Q1 : CLASS METHODS Part 1 : Constructor and Accessor Methods for Pet class in Pet.cpp file ( 5 points)
// Q2 : CLASS METHODS Part 2 : Class methods for Pet class in Pet.cpp file (10 points)
// Q3 : Add Function in hw09.cpp file ( 5 points)
// Q4 : Search Function in hw09.cpp file (10 points)
// Q5 : Remove One Function in hw09.cpp file (15 points)
// Q6 : Implement cin / cout for the lines in main without modifying the functionality ( 5 points)
#include
#include
#include
#include "Container.h"
#include "Pet.h"
#include "Checkup.h"
using namespace std;
// forward declarations
void flush();
void branching(char);
void helper(char);
void add_pet(string, string);
Pet* search_pet(string, string);
void remove_pet(string, string);
void clean_up(Pet*);
void print_all(Container*);
void remove_all();
Container* list = NULL; // global list
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS
char ch = 'i';
do {
// Q6: Implement cin / cout for the lines below without modifying the functionality (5 points)
// (change all printf statements to cout and read the next char using cin)
printf("Please enter your selection ");
printf("\ta: add a new pet to the list ");
printf("\tc: add a new checkup for a pet ");
printf("\tr: remove a pet from the list ");
printf("\tp: print all pets on the list ");
printf("\tq: quit ");
ch = getchar();
// End Q6
flush();
branching(ch);
} while (ch != 'q');
remove_all();
list = NULL;
return 0;
}
void flush()
{
int c;
do c = getchar(); while (c != ' ' && c != EOF);
}
void branching(char c)
{
switch (c) {
case 'a':
case 'c':
case 'r':
case 'p':
helper(c);
break;
case 'q':
break;
default:
printf(" Invalid input! ");
}
}
// The helper function is used to determine how much data is needed and which function to send that data to.
// It uses pointers and values that are returned from some functions to produce the correct ouput.
// There is no implementation needed here, but you should study this function and know how it works.
// It is always helpful to understand how the code works before implementing new features.
// Do not change anything in this function or you risk failing the automated test cases.
void helper(char c)
{
string name, breed;
if (c == 'p')
print_all(list);
else
{
cout << endl << "Please enter the pet's name: " << endl;
cin >> name;
cout << "Please enter the pet's breed: " << endl;
cin >> breed; flush();
Pet* pet_result = search_pet(name, breed);
if (c == 'a') // add pet
{
if (pet_result == NULL)
{
add_pet(name, breed);
cout << endl << "Pet added." << endl << endl;
}
else
cout << endl << "Pet already on list." << endl << endl;
}
else if (c == 'c') // add checkup
{
if (pet_result == NULL)
{
cout << endl << "Pet not found." << endl << endl;
return;
}
string date;
cout << "Please enter the date of the checkup: " << endl;
cin >> date; flush();
pet_result->addCheckup(date);
cout << endl << "Checkup added." << endl << endl;
}
else if (c == 'r') // remove pet
{
if (pet_result == NULL)
{
cout << endl << "Pet not found." << endl << endl;
return;
}
remove_pet(name, breed);
cout << endl << "Pet removed from the list." << endl << endl;
}
}
}
// Q3: Add Pet (5 points)
// This function will be used to add a new pet to the head of you linked list of containers, no need for sorting.
// The search function is called before this function, therefore you can assume the pet is not already on the list.
void add_pet(string name, string breed)
{
// Add your code
}
// This function is already implemented for you.
// This function will be used to search for a pet on the list.
// Pets on the list may have the same name OR the same breed, but should not have the same name AND breed.
// Therefore, you must traverse the list and return a pointer to a 'Pet' with the desired name AND breed.
// If the pet does not exist on the list, return NULL. (See helper function for use of this function).
Pet* search_pet(string name, string breed)
{
Container *container_traverser = list;
while (container_traverser != NULL)
{
if (container_traverser->pet->getName() == name && container_traverser->pet->getBreed() == breed)
return container_traverser->pet;
container_traverser = container_traverser->next;
}
return NULL;
}
// Q4: Remove_checkup (5 points)
// Write a recursive function to implement the function.
// You will not receive any point if you use a while loop.
// This function will be used to remove all the checkups in the container.
// Use proper memory management to ensure no memory leaks.
void remove_checkups(Container *current_container, Checkup *top)
{
// Add your code
}
// Q5: Remove Pet (10 points)
// This function will be used to remove a pet from the list.
// Traverse the list and use the parameters to remove the pet.
// You must call the remove_checkups function to remove all the checkups.
// Use proper memory management to ensure no memory leaks.
void remove_pet(string name, string breed)
{
// Add your code
}
// This function is already implemented for you. It traverses the list and removes all pets to ensure no memory leaks.
void remove_all()
{
while (list != NULL)
{
Container* container_to_be_removed = list;
list = list->next;
while (container_to_be_removed->pet->checkups != NULL)
{
Checkup *checkup_to_be_removed = container_to_be_removed->pet->checkups;
container_to_be_removed->pet->checkups = container_to_be_removed->pet->checkups->next;
delete checkup_to_be_removed;
}
delete container_to_be_removed->pet;
delete container_to_be_removed;
}
}
// Question 6. Print_all (10 points for this question)
// Write a recursive function to implement the print_all function.
// You will not receive any point if you use a while loop. (6 points for the code)
// Please use comments to indicate the four steps (1 point each step):
// (1) size-n problem, (2) stopping condition and return value,
// (3) size-(n-1) problem, (4) construct the solution of size-n problem
void print_all(Container* top)
{
// Add your code
}
#include "Pet.h"
// Q1 : CLASS METHODS Part 1 : Constructor and Accessor Methods for Pet (5 points)
// Constructor // Create a constructor for the class Pet which takes 2 string parameters (see helper function for use of constructor). // Use the 2 string parameters to initialize the 2 private local variables name and breed. // HINT: Don't forget to initialize your linked list of checkups to NULL.
// Code here
// Accessor Methods // Create accessor methods for both private local strings name and breed (see print_all function for use of these methods).
// Code here
// Q2 : CLASS METHODS Part 2 : Class Methods for Pet (10 points)
// Create a method named "addCheckup" which has one string parameter and no return type (see helper function for use). // This method is used to add a new date to the pet's linked list of checkups. The string parameter is the date of checkup. // You should add the date to the tail of the linked list "checkups". Checkups will be added in chronological order.
// Code here
// Create a method named "lastCheckup" which has no parameters and returns a string (see print_all function for use). // This method will be used to return a string for the date of the last checkup for this pet. // If the pet has not yet had a checkups, return an empty string.
// Code here
#include "Container.h"
// Constructor for Container class
Container::Container(){
pet = NULL;
next = NULL;
}
#include "Checkup.h"
Checkup::Checkup(string new_date) {
date = new_date;
}
string Checkup::getDate() {
return date;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
