Question

Part1 - ArrayBag modifications: 1. Modify the add method so that it will not allow duplicate items to be added to the ArrayBa

/ Animal.hpp

#ifndef ANIMAL_H_

#define ANIMAL_H_

#include <string>

class Animal

{

public:

Animal();

Animal(std::string, bool domestic=false, bool predator=false);

std::string getName() const;

bool isDomestic() const;

bool isPredator() const;

void setName(std::string);

void setDomestic();

void setPredator();

protected: // protected so that derived class can directly access them

std::string name_;

bool domestic_;

bool predator_;

};

#endif /* ANIMAL_H_ */

//end of Animal.h

// /////////////////////////////////////////////////////////////////Animal.cpp

#include "Animal.h"

Animal::Animal(): name_(""),domestic_(false), predator_(false){

}

Animal::Animal(std::string name, bool domestic, bool predator):

name_(name),domestic_(domestic), predator_(predator)

{

}

std::string Animal::getName() const{

return name_;

}

bool Animal::isDomestic() const {

return domestic_;

}

bool Animal::isPredator() const {

return predator_;

}

void Animal::setName(std::string name){

name_=name;

}

void Animal::setDomestic(){

domestic_=true;

}

void Animal::setPredator(){

predator_=true;

}

//end of Animal.cpp

//////////////////////////////////////////////////////////////////////ArrayBag.cpp

#include "ArrayBag.hpp"

/** default constructor**/
template<class T>
ArrayBag<T>::ArrayBag(): item_count_(0)
{
} // end default constructor


/**
@return item_count_ : the current size of the bag
**/
template<class T>
int ArrayBag<T>::getCurrentSize() const
{
return item_count_;
} // end getCurrentSize


/**
@return true if item_count_ == 0, false otherwise
**/
template<class T>
bool ArrayBag<T>::isEmpty() const
{
return item_count_ == 0;
} // end isEmpty


/**
@return true if new_etry was successfully added to items_, false otherwise
**/
template<class T>
bool ArrayBag<T>::add(const T& new_entry)
{
bool has_room = (item_count_ < DEFAULT_CAPACITY);
if (has_room)
{
items_[item_count_] = new_entry;
item_count_++;
return true;
} // end if

return false;
} // end add


/**
@return true if an_etry was successfully removed from items_, false otherwise
**/
template<class T>
bool ArrayBag<T>::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<class T>
void ArrayBag<T>::clear()
{
item_count_ = 0;
} // end clear


/**
@return the number of times an_entry is found in items_
**/
template<class T>
int ArrayBag<T>::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<class T>
bool ArrayBag<T>::contains(const T& an_entry) const
{
return getIndexOf(an_entry) > -1;
} // end contains


/**
@return a vector having the same cotntents as items_
**/
template<class T>
std::vector<T> ArrayBag<T>::toVector() const
{
std::vector<T> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);

return bag_contents;
} // end toVector

// ********* PRIVATE METHODS **************//

/**
@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<class T>
int ArrayBag<T>::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

////////////////////////////////////////////////////////////////////////////////////////////ArrayBag.hpp

#ifndef ARRAY_BAG_
#define ARRAY_BAG_

#include <vector>

template<class T>
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<T> toVector() 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;

}; // end ArrayBag

#include "ArrayBag.cpp"
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// ArrayBag.hpp

#ifndef ARRAY_BAG_

#define ARRAY_BAG_

#include <vector>

template<class T>

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<T> toVector() const;

/**@post prints the contents of items_ to the standard output

* separated by commas and followed by a new line

*/

void display() const;

/** implements Set Union

* The union of two sets A and B is the set of elements which are in A , in B or in both A and B

* @param a_bag to be combined with the contents of this (the calling bag)

* @post adds as many as items from a_bag as space allows

*/

void operator+=(const ArrayBag<T> &a_bag);

/**implements Set Difference

* The (set) difference between two sets A and B is the set that consists

* of the elements of A which are not elements of B

* @param a_bag to be subtracted form this (the calling) bag

* @post removes all data from items_ that is also found in a_bag

*/

void operator-=(const ArrayBag<T> &a_bag);

/**implements Set Intersection

* the intersection of two sets A and B is the set that consists of

* the elements that are in both A and B

* @param a_bag to be intersected with this (the calling) bag

* @post items_ no longer contains data not found in a_bag

*/

void operator/=(const ArrayBag<T> &a_bag);

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;

};

#include "ArrayBag.cpp"

#endif

//end of ArrayBag.hpp

// ArrayBag.cpp

#include "ArrayBag.hpp"

#include <iostream>

using namespace std;

/** default constructor**/

template<class T>

ArrayBag<T>::ArrayBag(): item_count_(0)

{

} // end default constructor

/**

@return item_count_ : the current size of the bag

**/

template<class T>

int ArrayBag<T>::getCurrentSize() const

{

return item_count_;

} // end getCurrentSize

/**

@return true if item_count_ == 0, false otherwise

**/

template<class T>

bool ArrayBag<T>::isEmpty() const

{

return item_count_ == 0;

} // end isEmpty

/**

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

doesn't allow addition of duplicate items

**/

template<class T>

bool ArrayBag<T>::add(const T& new_entry)

{

       // if bag doesn't contain new_entry, then add

       if(!contains(new_entry))

       {

             bool has_room = (item_count_ < DEFAULT_CAPACITY);

             if (has_room) {

                    items_[item_count_] = new_entry;

                    item_count_++;

                    return true;

             } // end if

       }

       return false;

} // end add

/**

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

**/

template<class T>

bool ArrayBag<T>::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<class T>

void ArrayBag<T>::clear()

{

item_count_ = 0;

} // end clear

/**

@return the number of times an_entry is found in items_

**/

template<class T>

int ArrayBag<T>::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<class T>

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

{

return getIndexOf(an_entry) > -1;

} // end contains

/**

@return a vector having the same cotntents as items_

**/

template<class T>

std::vector<T> ArrayBag<T>::toVector() const

{

std::vector<T> bag_contents;

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

bag_contents.push_back(items_[i]);

return bag_contents;

} // end toVector

// ********* PRIVATE METHODS **************//

/**

@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<class T>

int ArrayBag<T>::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

// function to display the contents of the bag

template <class T>

void ArrayBag<T>::display() const

{

       // check if bag is not empty

       if(item_count_ > 0)

       {

             // loop over the items_ array, printing each element separated by a comma

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

                    cout<<items_[i]<<", ";

             cout<<items_[item_count_-1]<<endl;

       }else

             cout<<"Empty Bag"<<endl; // empty bag

} //end display

// function to perform union of this and a_bag

template <class T>

void ArrayBag<T>:: operator+=(const ArrayBag<T> &a_bag)

{

       // loop over the items of a_bag , adding each element of a_bag into this bag

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

             this->add(a_bag.items_[i]);

} //end operator+=

// function to perform (set) difference of this bag and a_bag

template <class T>

void ArrayBag<T>:: operator-=(const ArrayBag<T> &a_bag)

{

       // loop over the items of a_bag

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

       {

             // if this bag contains the element, remove from this bag

             if(this->contains(a_bag.items_[i]))

                    this->remove(a_bag.items_[i]);

       }

} //end operator-=

// function to perform set intersection of this and a_bag

template <class T>

void ArrayBag<T>:: operator/=(const ArrayBag<T> &a_bag)

{

       // loop over the items of this bag

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

       {

             // if a_bag doesn't contain the item, remove from this bag

             if(!a_bag.contains(items_[i]))

                    this->remove(items_[i]);

       }

} //end operator/=

//end of ArrayBag.cpp

Add a comment
Know the answer?
Add Answer to:
/ Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • 5. Below i s the class deciaration for the Bag class from your text. Refer to...

    5. Below i s the class deciaration for the Bag class from your text. Refer to this hea he Ted implementation @file Bag.h #ifndet BAG #define BAG template <class ItemType> class Bag private: static const int DEFAULT BAG SIZE 6; Il current count of Bag items /I max capacity of the Bag ItemType items[DEFAULT BAG SIZE]; //array of Bag items int itemCount; int maxitems; /l Returns either the index of the element in the array items that ll contains the...

  • Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides...

    Use a B-Tree to implement the set.h class. #ifndef MAIN_SAVITCH_SET_H #define MAIN_SAVITCH_SET_H #include <cstdlib> // Provides size_t namespace main_savitch_11 { template <class Item> class set { public: // TYPEDEFS typedef Item value_type; // CONSTRUCTORS and DESTRUCTOR set( ); set(const set& source); ~set( ) { clear( ); } // MODIFICATION MEMBER FUNCTIONS void operator =(const set& source); void clear( ); bool insert(const Item& entry); std::size_t erase(const Item& target); // CONSTANT MEMBER FUNCTIONS std::size_t count(const Item& target) const; bool empty( ) const...

  • #include <iostream #include <string> #ifndef ACCOUNT H #define ACCOUNT H class Account { public: Account(std::string, int);...

    #include <iostream #include <string> #ifndef ACCOUNT H #define ACCOUNT H class Account { public: Account(std::string, int); Account(){}; void deposit(int); void withdraw(int); int getBalance() const; 15 private: int balance{@}; std::string name{}; 18 19 20 Verdif - Account.cpp saved #include "Account.h" Account :: Account(std::string accountName, int startingBalance) : name{accountName) 4 5 if (startingBalance > 0) balance = startingBalance; B 10 void Account::deposit(int depositAmount) { if (depositAmount > 0) balance + depositAmount: ) 12 13 14 15 16 void Account::withdraw(int withdrawAmount) { If...

  • lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream;...

    lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • HELP PLEASE ANSWER 3(c). Consider the following function object: #include <string> class CustomCompare public: bool operator...

    HELP PLEASE ANSWER 3(c). Consider the following function object: #include <string> class CustomCompare public: bool operator (const atd:istringk 1hs, const std::string& rhs) if (1hs. length) 0 && rhs.lengthO 0) return false; int 1 lhs. length )-1; intr rhs. length)-1; return (ro&&10) I Write one C++ statement that defines a variable myCustomPQ, where myCustomPQ is a priority queue storing strings and using CustomCompare.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT