Write a class that implements the BagInterface. BagInterface should have methods
ID: 3794776 • Letter: W
Question
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[]Explanation / Answer
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 <cstddef>
template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor
template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add
template<class ItemType>
bool ArrayBag<ItemType>::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<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
} // end clear
template<class ItemType>
int ArrayBag<ItemType>::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<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template<class ItemType>
std::vector<ItemType> ArrayBag<ItemType>::toVector() const
{
std::vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end toVector
// private
template<class ItemType>
int ArrayBag<ItemType>::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 ItemType>
class ArrayBag : public BagInterface<ItemType>
{
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<ItemType> toVector() const;
}; // end ArrayBag
#include "ArrayBag.cpp"
#endif
BagDriver.cpp-------->
//-----------------------
// C++ includes
//-----------------------
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//-----------------------
// 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<string> bags[]); //processes menu input
void displayBag(ArrayBag<string>& bag); //display the bag's entire contents
void restoreBag(ArrayBag<string>& bag); //prompts for filename with items
void saveBag(ArrayBag<string>& bag); //prompts for filename to store items in
int main()
{
int bagNum; //The bag number to use
ArrayBag<string> 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<string>& bag)
{
string item;
vector<string> bagVector = bag.toVector();
if(bagVector.size() > 0)
{
cout << "The bag contains: " << endl;
for(unsigned int i=0; i<bagVector.size(); i++){
cout << bagVector[i] << ", ";
}
cout<<endl;
}
else
{
cout << "The bag is empty." << endl;
}
}
void processOption(char &menuChar, ArrayBag<string> 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<string>& 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<<endl;
}
} while (!fin);
//read file until eof reached
while(success && getline(fin,item)){
success = bag.add(item);
}
if (!success) cout << "Not all items added to the bag!" << endl;
cout<< bag.getCurrentSize() << " items read into the bag ";
fin.close();
}//restoreBag()
void saveBag(ArrayBag<string>& 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 <vector>
template<class ItemType>
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<ItemType> toVector() const = 0;
virtual ~BagInterface() { }
}; // end BagInterface
#endif
CardGuesser.cpp------->
#include <iostream> // For cout and cin
#include <string> // 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<string> 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 <iostream>
#include <sstream>
using namespace std;
void printMenu();
int getInt();
string getItem();
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.