Write a class that implements the BagInterface. BagInterface should have methods to:
getCurrentSize (): integer isEmpty(): boolean add (newEntry: T): boolean remove(): T Rather
than T use int. Also it should have a toString that displays what you have in the bag. Write both
BagInterface and Bag class. Write the main that test the Bag class. Bag +getCurrentSize():
integer +isEmpty(): boolean +add(newEntry: T): boolean +remove(): T +remove(anEntry: T):
Boolean +clear(): void +getFrequencyOf (anEntry: T): integer +contains (anEntry: T): boolean
+toArray(): T[]
Solution
Here is a list of all files (source code is given below one by one save them with given name and
rum):
ArrayBag.cpp-------------------->
#include "ArrayBag.h"
#include
template
ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor
template
int ArrayBag::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template
bool ArrayBag::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template
bool ArrayBag::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template
bool ArrayBag::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
template
void ArrayBag::clear()
{
itemCount = 0;
} // end clear
template
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template
bool ArrayBag::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template
std::vector ArrayBag::toVector() const
{
std::vector bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template
int ArrayBag::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
ArrayBag.h-------------->
#ifndef ARRAY_BAG_
#define ARRAY_BAG_
#include "BagInterface.h"
template
class ArrayBag : public BagInterface
{
private:
static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
std::vector toVector() const;
}; // end ArrayBag
#include "ArrayBag.cpp"
#endif
BagDriver.cpp-------->
//-----------------------
// C++ includes
//-----------------------
#include
#include
#include
#include
//-----------------------
// Application includes
//-----------------------
#include "ArrayBag.h"
#include "io_functions.h"
//-----------------------
// Using statements
//-----------------------
using namespace std;
//----------------------
// Functions Prototypes
//----------------------
int getBagNum(); //prompts user for bag number
void processOption(char& c, ArrayBag bags[]); //processes menu input
void displayBag(ArrayBag& bag); //display the bag's entire contents
void restoreBag(ArrayBag& bag); //prompts for filename with items
void saveBag(ArrayBag& bag); //prompts for filename to store items in
int main()
{
int bagNum; //The bag number to use
ArrayBag bag[3];//bags to test, bag 0 used for intersection/union
string restoreBagFlg; // holds response to restore bag from a file
string menuOption; // holds menu option input from user
//Opening Message
cout<< "Bag Driver - Tests the Bag class using two bags" << endl;
//Restore the bag from a file
cout<< "----------------------------------" << endl;
cout<< "Would you like to restore a bag from a file? (Y/N) ";
//Read user input - use a string to accept all inputs
getline(cin, restoreBagFlg);
if(restoreBagFlg[0] == 'Y' || restoreBagFlg[0] == 'y'){
bagNum = getBagNum();
restoreBag(bag[bagNum]); // opens/reads file into aBag
}
//Menu driven part of main program
//Initial menu display and loop to process menu option
printMenu();
do {
cout << " Enter menu selection (0 for menu): ";
getline(cin, menuOption); //Use a string in case user enters chars
processOption(menuOption[0], bag);
}while(menuOption[0] != '9');
return 0; //Exit program
}//main()
//---------------------------
// Local function definitions
//---------------------------
int getBagNum()
{
int bagNum=0;
while(bagNum != 1 && bagNum != 2)
{
cout << "Which bag (1 or 2)? ";
bagNum = getInt();
}
return bagNum;
} //getBagNum()
void displayBag(ArrayBag& bag)
{
string item;
vector bagVector = bag.toVector();
if(bagVector.size() > 0)
{
cout << "The bag contains: " << endl;
for(unsigned int i=0; i bag[])
{
int bagNum;
string item;
string saveBagFlg;
switch (menuChar)
{
case '0': // Display menu options
printMenu();
break;
case '1': // Output size of the bag
cout << " Bag Size is: " << bag[getBagNum()].getCurrentSize() << endl;
break;
case '2': // Add items to the bag
cout << "Add Item:" << endl;
item = getItem();
bagNum = getBagNum();
if(bag[bagNum].add(item))
{
cout << item << " was successfully added to bag " << bagNum << endl;
}
else
{
cout << item << " was not added to the bag." << endl;
}
break;
//NOTE that options 3 - 8 are left for the student to complete
case '8': // Display entire contents of bag
displayBag(bag[getBagNum()]);
break;
case '9': // Exit the program
case 'Q': // handles multiple quit inputs
case 'q':
cout<< "Would you like to save the bag to a file? (Y/N) ";
getline(cin, saveBagFlg);
if(saveBagFlg[0] == 'Y' || saveBagFlg[0] == 'y')
saveBag(bag[getBagNum()]); // save bag to file
cout<< "Goodbye!"<< endl; // Exit message
menuChar = '9';
break;
default: // Invalid menu option entered
cout << " Error! Invalid option. Please try again.  ";
printMenu();
}
}//processOption()
void restoreBag(ArrayBag& bag)
{
bool success = true;
ifstream fin;
string filename;
string item;
//Data validation to get filename and open it
do{
cout<<"Enter the filename that contains the bag: ";
getline(cin, filename);
fin.clear();
fin.open(filename.c_str());
if(fin == 0){
cout<<"ERROR - could not open file " << filename<& bag)
{
//NOTE this is a function stub. You need to implement it.
cout << "Bag will be saved when implemented" << endl;
}//saveBag()
BagInterface.h------>
#ifndef BAG_INTERFACE_
#define BAG_INTERFACE_
#include
template
class BagInterface
{
public:
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual std::vector toVector() const = 0;
virtual ~BagInterface() { }
}; // end BagInterface
#endif
CardGuesser.cpp------->
#include // For cout and cin
#include // For string objects
#include "Bag.h" // For ADT bag
using namespace std;
int main()
{
string clubs[] = { "Joker", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };
// Create our bag to hold cards.
Bag grabBag;
// Place six cards in the bag.
grabBag.add(clubs[1]);
grabBag.add(clubs[2]);
grabBag.add(clubs[4]);
grabBag.add(clubs[8]);
grabBag.add(clubs[10]);
grabBag.add(clubs[12]);
// Get friends guess and check it.
int guess = 0;
while (!grabBag.isEmpty())
{
cout << "What is your guess?"<< "(1 for Ace to 13 for King):";
cin >> guess;
// Is card in the bag?
if (grabBag.contains(clubs[guess]))
{
// Good guess remove card from the bag.
cout << "You get the card! ";
grabBag.remove(clubs[guess]);
}
else
{
cout << "Sorry, card was not in the bag. ";
} // end if
} // end while
cout << "No more cards in the bag. Game over! ";
return 0;
}; // end main
io_functions.cpp----------->
#include "io_functions.h"
using namespace std;
void printMenu(){
cout << "1) Print the size of the bag" << endl;
cout << "2) Add an item into the bag" << endl;
cout << "3) Remove an item from the bag" << endl;
cout << "4) Check for an item in the bag" << endl;
cout << "8) Display the bag" << endl;
cout << "9) Quit the program" << endl;
}
int getInt(){
int intValue = 0;
string inputStr;
do{
getline(cin, inputStr);
stringstream(inputStr) >> intValue;
if(intValue < 1) cout << "ERROR - please enter a positive integer> ";
} while(intValue < 1);
return intValue;
}
string getItem()
{
string item;
cout << "Enter the bag item> ";
getline(cin, item);
return item;
}
io_functions.h------------>
#ifndef IO_FUNCTIONS_H
#define IO_FUNCTIONS_H
#include
#include
using namespace std;
void printMenu();
int getInt();
string getItem();
#endif

Write a class that implements the BagInterface. BagInterface should .pdf

  • 1.
    Write a classthat implements the BagInterface. BagInterface should have methods to: getCurrentSize (): integer isEmpty(): boolean add (newEntry: T): boolean remove(): T Rather than T use int. Also it should have a toString that displays what you have in the bag. Write both BagInterface and Bag class. Write the main that test the Bag class. Bag +getCurrentSize(): integer +isEmpty(): boolean +add(newEntry: T): boolean +remove(): T +remove(anEntry: T): Boolean +clear(): void +getFrequencyOf (anEntry: T): integer +contains (anEntry: T): boolean +toArray(): T[] Solution Here is a list of all files (source code is given below one by one save them with given name and rum): ArrayBag.cpp--------------------> #include "ArrayBag.h" #include template ArrayBag::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor template int ArrayBag::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool ArrayBag::isEmpty() const { return itemCount == 0; } // end isEmpty template bool ArrayBag::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry;
  • 2.
    itemCount++; } // endif return hasRoomToAdd; } // end add template bool ArrayBag::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove template void ArrayBag::clear() { itemCount = 0; } // end clear template int ArrayBag::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex < itemCount) { if (items[curIndex] == anEntry) { frequency++; } // end if curIndex++; // Increment to next entry } // end while return frequency; } // end getFrequencyOf
  • 3.
    template bool ArrayBag::contains(const ItemType&anEntry) const { return getIndexOf(anEntry) > -1; } // end contains template std::vector ArrayBag::toVector() const { std::vector bagContents; for (int i = 0; i < itemCount; i++) bagContents.push_back(items[i]); return bagContents; } // end toVector // private template int ArrayBag::getIndexOf(const ItemType& target) const { bool found = false; int result = -1; int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped while (!found && (searchIndex < itemCount)) { if (items[searchIndex] == target) { found = true; result = searchIndex; } else { searchIndex++; } // end if } // end while return result; } // end getIndexOf ArrayBag.h-------------->
  • 4.
    #ifndef ARRAY_BAG_ #define ARRAY_BAG_ #include"BagInterface.h" template class ArrayBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag ItemType items[DEFAULT_CAPACITY]; // Array of bag items int itemCount; // Current count of bag items int maxItems; // Max capacity of the bag // Returns either the index of the element in the array items that // contains the given target or -1, if the array does not contain // the target. int getIndexOf(const ItemType& target) const; public: ArrayBag(); int getCurrentSize() const; bool isEmpty() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; std::vector toVector() const; }; // end ArrayBag #include "ArrayBag.cpp" #endif BagDriver.cpp--------> //----------------------- // C++ includes //----------------------- #include #include #include #include
  • 5.
    //----------------------- // Application includes //----------------------- #include"ArrayBag.h" #include "io_functions.h" //----------------------- // Using statements //----------------------- using namespace std; //---------------------- // Functions Prototypes //---------------------- int getBagNum(); //prompts user for bag number void processOption(char& c, ArrayBag bags[]); //processes menu input void displayBag(ArrayBag& bag); //display the bag's entire contents void restoreBag(ArrayBag& bag); //prompts for filename with items void saveBag(ArrayBag& bag); //prompts for filename to store items in int main() { int bagNum; //The bag number to use ArrayBag bag[3];//bags to test, bag 0 used for intersection/union string restoreBagFlg; // holds response to restore bag from a file string menuOption; // holds menu option input from user //Opening Message cout<< "Bag Driver - Tests the Bag class using two bags" << endl; //Restore the bag from a file cout<< "----------------------------------" << endl; cout<< "Would you like to restore a bag from a file? (Y/N) "; //Read user input - use a string to accept all inputs getline(cin, restoreBagFlg); if(restoreBagFlg[0] == 'Y' || restoreBagFlg[0] == 'y'){ bagNum = getBagNum(); restoreBag(bag[bagNum]); // opens/reads file into aBag } //Menu driven part of main program //Initial menu display and loop to process menu option
  • 6.
    printMenu(); do { cout <<" Enter menu selection (0 for menu): "; getline(cin, menuOption); //Use a string in case user enters chars processOption(menuOption[0], bag); }while(menuOption[0] != '9'); return 0; //Exit program }//main() //--------------------------- // Local function definitions //--------------------------- int getBagNum() { int bagNum=0; while(bagNum != 1 && bagNum != 2) { cout << "Which bag (1 or 2)? "; bagNum = getInt(); } return bagNum; } //getBagNum() void displayBag(ArrayBag& bag) { string item; vector bagVector = bag.toVector(); if(bagVector.size() > 0) { cout << "The bag contains: " << endl; for(unsigned int i=0; i bag[]) { int bagNum; string item; string saveBagFlg; switch (menuChar) { case '0': // Display menu options
  • 7.
    printMenu(); break; case '1': //Output size of the bag cout << " Bag Size is: " << bag[getBagNum()].getCurrentSize() << endl; break; case '2': // Add items to the bag cout << "Add Item:" << endl; item = getItem(); bagNum = getBagNum(); if(bag[bagNum].add(item)) { cout << item << " was successfully added to bag " << bagNum << endl; } else { cout << item << " was not added to the bag." << endl; } break; //NOTE that options 3 - 8 are left for the student to complete case '8': // Display entire contents of bag displayBag(bag[getBagNum()]); break; case '9': // Exit the program case 'Q': // handles multiple quit inputs case 'q': cout<< "Would you like to save the bag to a file? (Y/N) "; getline(cin, saveBagFlg); if(saveBagFlg[0] == 'Y' || saveBagFlg[0] == 'y') saveBag(bag[getBagNum()]); // save bag to file cout<< "Goodbye!"<< endl; // Exit message menuChar = '9'; break; default: // Invalid menu option entered cout << " Error! Invalid option. Please try again. "; printMenu(); }
  • 8.
    }//processOption() void restoreBag(ArrayBag& bag) { boolsuccess = true; ifstream fin; string filename; string item; //Data validation to get filename and open it do{ cout<<"Enter the filename that contains the bag: "; getline(cin, filename); fin.clear(); fin.open(filename.c_str()); if(fin == 0){ cout<<"ERROR - could not open file " << filename<& bag) { //NOTE this is a function stub. You need to implement it. cout << "Bag will be saved when implemented" << endl; }//saveBag() BagInterface.h------> #ifndef BAG_INTERFACE_ #define BAG_INTERFACE_ #include template class BagInterface { public: virtual int getCurrentSize() const = 0; virtual bool isEmpty() const = 0; virtual bool add(const ItemType& newEntry) = 0; virtual bool remove(const ItemType& anEntry) = 0; virtual void clear() = 0; virtual int getFrequencyOf(const ItemType& anEntry) const = 0; virtual bool contains(const ItemType& anEntry) const = 0; virtual std::vector toVector() const = 0; virtual ~BagInterface() { }
  • 9.
    }; // endBagInterface #endif CardGuesser.cpp-------> #include // For cout and cin #include // For string objects #include "Bag.h" // For ADT bag using namespace std; int main() { string clubs[] = { "Joker", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; // Create our bag to hold cards. Bag grabBag; // Place six cards in the bag. grabBag.add(clubs[1]); grabBag.add(clubs[2]); grabBag.add(clubs[4]); grabBag.add(clubs[8]); grabBag.add(clubs[10]); grabBag.add(clubs[12]); // Get friends guess and check it. int guess = 0; while (!grabBag.isEmpty()) { cout << "What is your guess?"<< "(1 for Ace to 13 for King):"; cin >> guess; // Is card in the bag? if (grabBag.contains(clubs[guess])) { // Good guess remove card from the bag. cout << "You get the card! "; grabBag.remove(clubs[guess]); } else { cout << "Sorry, card was not in the bag. ";
  • 10.
    } // endif } // end while cout << "No more cards in the bag. Game over! "; return 0; }; // end main io_functions.cpp-----------> #include "io_functions.h" using namespace std; void printMenu(){ cout << "1) Print the size of the bag" << endl; cout << "2) Add an item into the bag" << endl; cout << "3) Remove an item from the bag" << endl; cout << "4) Check for an item in the bag" << endl; cout << "8) Display the bag" << endl; cout << "9) Quit the program" << endl; } int getInt(){ int intValue = 0; string inputStr; do{ getline(cin, inputStr); stringstream(inputStr) >> intValue; if(intValue < 1) cout << "ERROR - please enter a positive integer> "; } while(intValue < 1); return intValue; } string getItem() { string item; cout << "Enter the bag item> "; getline(cin, item); return item; } io_functions.h------------> #ifndef IO_FUNCTIONS_H #define IO_FUNCTIONS_H
  • 11.
    #include #include using namespace std; voidprintMenu(); int getInt(); string getItem(); #endif