Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a c++ class program utilizing ADT. I need to create a class called BagTest.hpp and BagTest.cpp to help test my program. You may

This is a c++ class program utilizing ADT. I need to create a class called BagTest.hpp and BagTest.cpp to help test my program.

You may have to add a default constructor to the CourseMember class for this to work, but everything else (ArrayBag/CourseMember does not need to be changed).

BagTest has one private member function:

ArrayBag bag_;

These are the public member functions:

/** @pre the input file is in csv format as follows: id,first_name,last_name,title @post this function asks the user for an input file name. It extracts the information necessary to create a CourseMember object from each line in the input file, and it adds the corresponding CourseMember object to bag_. @return returns the populated bag_ **/

ArrayBag testCourseMemberBag()

/** @post prints to the standard output all CourseMeber objects found in bag_, one pre line if the format: id first_name last_name **/

void displayCourseMemberBag(const ArrayBag& a_bag);

I was suggested to use the following private member function but you do not have to do if you have an easier way:

ArrayBag createBagFromInput(std::string input_file);

The input file will be in csv (comma separated value) format, and each line corresponds to the information necessary to create a CourseMember object. Each line in the input csv has the following format: id,first_name,last_name,title.

You can ignore the title entry for now, and use id, first_name and last_name to create CourseMember objects.

The .csv file content does not matter but for sake of simplicity, it follows this general pattern:

100,Doe,Jane,Student 101,Doe,John,Student

---------------------------------------------

ArrayBag.hpp

----------------------------------------------

/** Header file for an array-based implementation of the ADT bag.

@file ArrayBag.h */

#ifndef ARRAY_BAG_

#define ARRAY_BAG_

#include

using namespace std;

template

class ArrayBag {

public:

/** default constructor**/

ArrayBag();

/**

@return item_count_ : the current size of the bag

**/

int getCurrentSize() const;

/**

@return true if item_count_ == 0, false otherwise

**/

bool isEmpty() const;

/**

@return true if new_etry was successfully added to items_, false otherwise

**/

bool add(const T& new_entry);

/**

@return true if an_etry was successfully removed from items_, false otherwise

**/

bool remove(const T& an_entry);

/**

@post item_count_ == 0

**/

void clear();

/**

@return true if an_etry is found in items_, false otherwise

**/

bool contains(const T& an_entry) const;

/**

@return the number of times an_entry is found in items_

**/

int getFrequencyOf(const T& an_entry) const;

/**

@return a vector having the same cotntents as items_

**/

std::vector toVector() const;

/**

@return a new bag contains the union of this bag and a_bag

**/

ArrayBag bagUnion(const ArrayBag& a_bag) const;

private:

static const int DEFAULT_CAPACITY = 200; //max size of items_

T items_[DEFAULT_CAPACITY]; // Array of bag items

int item_count_; // Current count of bag items

/**

@param target to be found in items_

@return either the index target in the array items_ or -1,

if the array does not containthe target.

**/

int getIndexOf(const T& target) const;

ArrayBag bagIntersectionNoDuplicates(const ArrayBag& a_bag) const; ArrayBagbagDifference(const ArrayBag& a_bag) const;

}; // end ArrayBag

#endif

--------------------------------------------------------------

ArrayBag.cpp

--------------------------------------------------------------

#include "ArrayBag.hpp"

#include

using namespace std;

/** default constructor**/

template

ArrayBag::ArrayBag() : item_count_(0)

{

} // end default constructor

/**

@return item_count_ : the current size of the bag

**/

template

int ArrayBag::getCurrentSize() const

{

return item_count_;

} // end getCurrentSize

/**

@return true if item_count_ == 0, false otherwise

**/

template

bool ArrayBag::isEmpty() const

{

return item_count_ == 0;

} // end isEmpty

/**

@return true if new_etry was successfully added to items_, false otherwise

**/

template

bool ArrayBag::add(const T& new_entry)

{

bool has_room = (item_count_ < DEFAULT_CAPACITY);

if (has_room)

{

items_[item_count_] = new_entry;

item_count_++;

} // end if

return has_room;

} // end add

/**

@return true if an_etry was successfully removed from items_, false otherwise

**/

template

bool ArrayBag::remove(const T& an_entry)

{

int found_index = getIndexOf(an_entry);

bool can_remove = !isEmpty() && (found_index > -1);

if (can_remove)

{

item_count_--;

items_[found_index] = items_[item_count_];

} // end if

return can_remove;

} // end remove

/**

@post item_count_ == 0

**/

template

void ArrayBag::clear()

{

item_count_ = 0;

} // end clear

/**

@return the number of times an_entry is found in items_

**/

template

int ArrayBag::getFrequencyOf(const T& an_entry) const

{

int frequency = 0;

int cun_index = 0; // Current array index

while (cun_index < item_count_)

{

if (items_[cun_index] == an_entry)

{

frequency++;

} // end if

cun_index++; // Increment to next entry

} // end while

return frequency;

} // end getFrequencyOf

/**

@return true if an_etry is found in items_, false otherwise

**/

template

bool ArrayBag::contains(const T& an_entry) const

{

return getIndexOf(an_entry) > -1;

} // end contains

/**

@return a vector having the same cotntents as items_

**/

template

std::vector ArrayBag::toVector() const

{

std::vector bag_contents;

for (int i = 0; i < item_count_; i++)

bag_contents.push_back(items_[i]);

return bag_contents;

} // end toVector

// PRIVATE

/**

@param target to be found in items_

@return either the index target in the array items_ or -1,

if the array does not containthe target.

**/

template

int ArrayBag::getIndexOf(const T& target) const

{

bool found = false;

int result = -1;

int search_index = 0;

// If the bag is empty, item_count_ is zero, so loop is skipped

while (!found && (search_index < item_count_))

{

if (items_[search_index] == target)

{

found = true;

result = search_index;

}

else

{

search_index++;

} // end if

} // end while

return result;

} // end getIndexOf

/**

@return a new bag contains the union of this bag and a_bag

**/

template

ArrayBag ArrayBag::bagUnion(const ArrayBag& a_bag) const {

//creating an array bag object to store union of both bags

ArrayBag unionBag;

//adding all elements from this bag to unionBag

for (int i = 0; i

unionBag.add(items_[i]);

}

//adding all elements from a_bag to unionBag

for (int i = 0; i

unionBag.add(a_bag.items_[i]);

}

//since add() method is implemented to handle the addition of elements when

//items array is full, we dont have to worry about that.

return unionBag; //returning union bag

}

/**

@return a new bag contains the INTERSECTION with No duplicate of this bag and a_bag

**/

template

ArrayBag ArrayBag::bagIntersectionNoDuplicates(const ArrayBag& a_bag) const {

ArrayBag intersectionBag;

for (int i = 0; i

if (a_bag.contains(items_[i]) && !intersectionBag.contains(items_[i])) {

intersectionBag.add(items_[i]);

}

}

return intersectionBag; //returning intersection

}

template

ArrayBag ArrayBag::bagDifference(const ArrayBag& a_bag) const {

ArrayBag differenceBag;

for (int i = 0; i

if (!a_bag.contains(items_[i]) && !differenceBag.contains(items_[i])) {

differenceBag.add(items_[i]);

}

}

return differenceBag; //returning difference

}

------------------------------------------------------------------------

CourseMember.hpp

------------------------------------

#ifndef CourseMember_hpp #define CourseMember_hpp

#include

class CourseMember { public: /** Parameterized constructor @param id the student's unique identifier @param first the student's first name @param last the student's last name */ CourseMember(int id, std::string first, std::string last);

//********** Accessor Methods ****************

/** @return returns id_; */ int getID() const;

/** @return returns first_name_ */ std::string getFirstName() const;

/** @return returns last_name_ */ std::string getLastName() const;

protected: int id_; /** the CourseMember's ID */ std::string first_name_; /** the CourseMember's first name */ std::string last_name_; /** the CourseMember's last name */

}; //end CourseMember

#endif /* CourseMember_hpp */

----------------------------------------------------

CourseMember.cpp

------------------------------------------------------

#include #include "CourseMember.hpp" #include using namespace std;

CourseMember::CourseMember(int id, std::string first, std::string last) { id_ = id; first_name_ = first; last_name_ = last; }

//Return ID int CourseMember::getID() const { return id_; }

/** @return returns first_name_ */ string CourseMember::getFirstName() const { return first_name_; }

/** @return returns last_name_ */ string CourseMember::getLastName() const { return last_name_; }

---------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions