A) Write your own test program to display the results of the implemented functio
ID: 3746327 • Letter: A
Question
A) Write your own test program to display the results of the implemented functions. Be sure to document your test program B) Write a summary of what your test program fully tests the bag class. Include a copy of the output after your summary. C) Answer the following questions about the original code.
1) Are all member functions defined in the implementation file? If not, explain why the code still works.
2) Are all member and non-member functions tested by the original test program? If not, list those that were not tested.
3) Why do you think the “namespace main_savitch” code was used? Can the code be made to work without this code?
// FILE: bag1.h
// CLASS PROVIDED: bag (part of the namespace main_savitch_3)
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
// typedef ____ value_type
// bag::value_type is the data type of the items in the bag. It may be any of
// the C++ built-in types (int, char, etc.), or a class with a default
// constructor, an assignment operator, and operators to
// test for equality (x == y) and non-equality (x != y).
//
// typedef ____ size_type
// bag::size_type is the data type of any variable that keeps track of how many items
// are in a bag.
//
// static const size_type CAPACITY = _____
// bag::CAPACITY is the maximum number of items that a bag can hold.
//
// CONSTRUCTOR for the bag class:
// bag( )
// Postcondition: The bag has been initialized as an empty bag.
//
// MODIFICATION MEMBER FUNCTIONS for the bag class:
// size_type erase(const value_type& target);
// Postcondition: All copies of target have been removed from the bag.
// The return value is the number of copies removed (which could be zero).
//
// void erase_one(const value_type& target)
// Postcondition: If target was in the bag, then one copy has been removed;
// otherwise the bag is unchanged. A true return value indicates that one
// copy was removed; false indicates that nothing was removed.
//
// void insert(const value_type& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been added to the bag.
//
// void operator +=(const bag& addend)
// Precondition: size( ) + addend.size( ) <= CAPACITY.
// Postcondition: Each item in addend has been added to this bag.
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
// size_type size( ) const
// Postcondition: The return value is the total number of items in the bag.
//
// size_type count(const value_type& target) const
// Postcondition: The return value is number of times target is in the bag.
//
// NONMEMBER FUNCTIONS for the bag class:
// bag operator +(const bag& b1, const bag& b2)
// Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY.
// Postcondition: The bag returned is the union of b1 and b2.
//
// VALUE SEMANTICS for the bag class:
// Assignments and the copy constructor may be used with bag objects.
#ifndef MAIN_SAVITCH_BAG1_H
#define MAIN_SAVITCH_BAG1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_3
{
class bag
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
bag( ) { used = 0; }
// MODIFICATION MEMBER FUNCTIONS
size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY]; // The array to store items
size_type used; // How much of array is used
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
}
#endif
// FILE: bag1.cpp
// CLASS IMPLEMENTED: bag (see bag1.h for documentation)
// INVARIANT for the bag class:
// 1. The number of items in the bag is in the member variable used;
// 2. For an empty bag, we do not care what is stored in any of data; for a
// non-empty bag the items in the bag are stored in data[0] through
// data[used-1], and we don't care what's in the rest of data.
#include <algorithm> // Provides copy function
#include <cassert> // Provides assert function
#include "bag1.h"
using namespace std;
namespace main_savitch_3
{
const bag::size_type bag::CAPACITY;
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0;
size_type many_removed = 0;
while (index < used)
{
if (data[index] == target)
{
--used;
data[index] = data[used];
++many_removed;
}
else
++index;
}
return many_removed;
}
bool bag::erase_one(const value_type& target)
{
size_type index;// The location of target in the data array
// First, set index to the location of target in the data array,
// which could be as small as 0 or as large as used-1. If target is not
// in the array, then index will be set equal to used.
index = 0;
while ((index < used) && (data[index] != target))
++index;
if (index == used)
return false; // target isn’t in the bag, so no work to do.
// When execution reaches here, target is in the bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
--used;
data[index] = data[used];
return true;
}
void bag::insert(const value_type& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
void bag::operator +=(const bag& addend)
// Library facilities used: algorithm, cassert
{
assert(size( ) + addend.size( ) <= CAPACITY);
copy(addend.data, addend.data + addend.used, data + used);
used += addend.used;
}
bag::size_type bag::count(const value_type& target) const
{
size_type answer;
size_type i;
answer = 0;
for (i = 0; i < used; ++i)
if (target == data[i])
++answer;
return answer;
}
bagoperator +(const bag& b1, const bag& b2)
// Library facilities used: cassert
{
bag answer;
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY);
answer += b1;
answer += b2;
return answer;
}
}
// FILE: bag_demo.cpp
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "bag1.h" // With value_type defined as an int
using namespace std;
using namespace main_savitch_3;
// PROTOTYPES for functions used by this demonstration program:
void get_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members. These ages have been read and placed in the ages bag, stopping
// when the bag is full or when the user types a negative number.
void check_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members once again. Each age is removed from the ages bag when it is typed,
// stopping when the bag is empty.
int main( )
{
bag ages;
get_ages(ages);
check_ages(ages);
cout <<"May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
void get_ages(bag& ages)
{
int user_input;
cout <<"Type the ages in your family." << endl;
cout <<"Type a negative number when you are done:" << endl;
cin >> user_input;
while (user_input >= 0)
{
if (ages.size( ) < ages.CAPACITY)
ages.insert(user_input);
else
cout <<"I have run out of room and can’t add that age." << endl;
cin >> user_input;
}
}
void check_ages(bag& ages)
{
int user_input;
cout <<"Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0)
{
cin >> user_input;
if (ages.erase_one(user_input))
cout <<"Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl;
}
}
Explanation / Answer
A) No error .Program running perfectly fine.
B)Explanation for typedef value_type was wrong which has been corrected as follows.Chaged/added portion in bold.
// FILE: bag1.h
// CLASS PROVIDED: bag (part of the namespace main_savitch_3)
//
// TYPEDEF and MEMBER CONSTANTS for the bag class:
// typedef ____ value_type
// bag::value_type is the data type of the items in the bag. It is of the type int.
//
// typedef ____ size_type
// bag::size_type is the data type of any variable that keeps track of how many items
// are in a bag.
//
// static const size_type CAPACITY = _____
// bag::CAPACITY is the maximum number of items that a bag can hold.
//
// CONSTRUCTOR for the bag class:
// bag( ) default constructor called when a bag is created initially.
// Postcondition: The bag has been initialized as an empty bag.
//
// MODIFICATION MEMBER FUNCTIONS for the bag class:
// size_type erase(const value_type& target);
// Postcondition: All copies of target have been removed from the bag.
// The return value is the number of copies removed (which could be zero).
//
// void erase_one(const value_type& target)
// Postcondition: If target was in the bag, then one copy has been removed;
// otherwise the bag is unchanged. A true return value indicates that one
// copy was removed; false indicates that nothing was removed.
//
// void insert(const value_type& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been added to the bag.
//
// void operator +=(const bag& addend)
// Precondition: size( ) + addend.size( ) <= CAPACITY.
// Postcondition: Each item in addend has been added to this bag.
//
// CONSTANT MEMBER FUNCTIONS for the bag class:
// size_type size( ) const
// Postcondition: The return value is the total number of items in the bag.
//
// size_type count(const value_type& target) const
// Postcondition: The return value is number of times target is in the bag.
//
// NONMEMBER FUNCTIONS for the bag class:
// bag operator +(const bag& b1, const bag& b2)
// Precondition: b1.size( ) + b2.size( ) <= bag::CAPACITY.
// Postcondition: The bag returned is the union of b1 and b2.
//
// VALUE SEMANTICS for the bag class:
// Assignments and the copy constructor may be used with bag objects.
#ifndef MAIN_SAVITCH_BAG1_H
#define MAIN_SAVITCH_BAG1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_3
{
class bag
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef int value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
bag( ) { used = 0; }
// MODIFICATION MEMBER FUNCTIONS
size_type erase(const value_type& target);
bool erase_one(const value_type& target);
void insert(const value_type& entry);
void operator +=(const bag& addend);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return used; }
size_type count(const value_type& target) const;
private:
value_type data[CAPACITY]; // The array to store items
size_type used; // How much of array is used
};
// NONMEMBER FUNCTIONS for the bag class
bag operator +(const bag& b1, const bag& b2);
}
#endif
C)Comments added inline for explanation of each function
// FILE: bag1.cpp
// CLASS IMPLEMENTED: bag (see bag1.h for documentation)
// INVARIANT for the bag class:
// 1. The number of items in the bag is in the member variable used;
// 2. For an empty bag, we do not care what is stored in any of data; for a
// non-empty bag the items in the bag are stored in data[0] through
// data[used-1], and we don't care what's in the rest of data.
#include <algorithm> // Provides copy function
#include <cassert> // Provides assert function
#include "bag1.h"
using namespace std;
namespace main_savitch_3
{
/**
* CAPACITY is the maximum number of items that a bag can hold.
*
* */
const bag::size_type bag::CAPACITY;
/*
* This method removes all the copies of target.
*/
bag::size_type bag::erase(const value_type& target)
{
size_type index = 0; /*holds index currently accessed .since we traverse the bag from left to right , it is initialized to 0.
* 0 holds the first item and used-1 holds the last item*/
size_type many_removed = 0; //keeps track of the no. of items removed.
while (index < used) //checks if current index is less than max no. of items in bag
{
if (data[index] == target) //if target is present at current index
{
--used; //decrement the total no. of items in bag
data[index] = data[used]; //copy the item at last to the current index there by deleting the current item.
++many_removed; //increment the count for items removed.
}
else
++index; //increment the index to point to next location
}
return many_removed; //returns the total no. of items removed
}
/**
* It removes one copy of target from the bag. If target is not found the bag is unchanged.
* A true return value indicates that one copy was removed; false indicates that nothing was removed.
*/
bool bag::erase_one(const value_type& target)
{
size_type index; // holds index currently accessed .
// To check if an item is found in the bag,we need to traverse the bag from left to right.
//So index is initialized to 0 initially.
index = 0;
while ((index < used) && (data[index] != target)) //search for the target till index becomes greater than used-1
//first item is stored at 0 and last item at used-1
++index; //increment index
if (index == used)
return false; // index has reached used means item not present in bag
// When execution reaches here, target is in the bag at data[index].
// So, reduce used by 1 and copy the last item onto data[index].
--used;
data[index] = data[used];
return true;
}
/**
* Adds a new entry to bag.
* @param entry item to be entered in the bag.
*/
void bag::insert(const value_type& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);//make sure current size is less than max capacity , then only insertion will be done
data[used] = entry; //add the new item to the last of bag
++used; //increment the size of bag after adding a new item.
}
/**
* Overloads the operator+= to merge a second bag to the first.
* Each item of second bag is appended to the first bag.
* @param addend-> the bag object to be appended to the initial object
*/
void bag::operator +=(const bag& addend)
// Library facilities used: algorithm, cassert
{
assert(size( ) + addend.size( ) <= CAPACITY);//ensures if the resuktend size of merged bags are less than the maximum capacity
copy(addend.data, addend.data + addend.used, data + used);//merges to bags
used += addend.used; //assign the total objects in a bag to sum of total objects of both
}
/**
* Returns the no. of times a particular target is found in bag *
* @param target item to be searched for
* @return
*/
bag::size_type bag::count(const value_type& target) const
{
size_type answer; //tracks the frequency of target in bag
size_type i;
answer = 0;
for (i = 0; i < used; ++i) //iterates through the entire bag for the target
if (target == data[i]) // if target is found
++answer; //frequency incremented
return answer; //returns the frequency
}
/**
* Overloads the operator + to add(append) two bags and returns a third resultant bag
* Items of two bags are added to the third one
* @param b1 first bag
* @param b2 second bag
* @return a third bag which is the combination of two bags
*/
bag operator +(const bag& b1, const bag& b2)
// Library facilities used: cassert
{
bag answer; //third bag
assert(b1.size( ) + b2.size( ) <= bag::CAPACITY); //ensures the total capacity of third bag is less than the size of two bags to be added
answer += b1; //appends b1 to answer
answer += b2; //appends b2 to answer
return answer; //return the resultant bag
}
}
D)Comments given inline for documentation
// FILE: bag_demo.cpp
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "bag1.h" // With value_type defined as an int
using namespace std;
using namespace main_savitch_3;
// PROTOTYPES for functions used by this demonstration program:
void get_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members. These ages have been read and placed in the ages bag, stopping
// when the bag is full or when the user types a negative number.
void check_ages(bag& ages);
// Postcondition: The user has been prompted to type in the ages of family
// members once again. Each age is removed from the ages bag when it is typed,
// stopping when the bag is empty.
int main( )
{
bag ages; //an object ages of type bag
get_ages(ages);
check_ages(ages);
cout << "May your family live long and prosper." << endl;
return EXIT_SUCCESS;
}
/**
* Gets the ages for the family till a negative no. is inputted
* @param ages holds all the ages in family
*/
void get_ages(bag& ages)
{
int user_input;
cout << "Type the ages in your family." << endl;
cout << "Type a negative number when you are done:" << endl;
cin >> user_input; //input age
while (user_input >= 0) //input age till a negative number is inputted
{
if (ages.size( ) < ages.CAPACITY) //checks if no. of ages inputted is less than capacity
ages.insert(user_input); //insert the age to the bag
else
cout << "I have run out of room and can’t add that age." << endl; //if size becomes greater than capacity
cin >> user_input;
}
}
/*
* Asks the user to enter the ages once again to confirm .
* These ages are then removed from bag.
*/
void check_ages(bag& ages)
{
int user_input; //holds the entered value
cout << "Type those ages again. Press return after each age:" << endl;
while (ages.size( ) > 0) //checks till size = 0
{
cin >> user_input; //enters age
if (ages.erase_one(user_input)) //erase one instance of age from bag if present
cout << "Yes, I've got that age and will remove it." << endl;
else
cout << "No, that age does not occur!" << endl; //message thrown out if age not present
}
}
E)
// FILE: bag_newdemo.cpp
// This is a small demonstration program showing how the bag class is used.
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include "bag1.h" // With value_type defined as an int
using namespace std;
using namespace main_savitch_3;
// PROTOTYPES for functions used by this demonstration program:
void get_scores(bag& scores);
// Postcondition: The user has been prompted to type in the runs scored in a cricket match.
// These runs are then placed in the scores bag, stopping
// when the bag is full or when the runs for all 11 are inputted since there are only 11 players in a match.
int main( )
{
bag bag1; //bag to hold runs scored in a match for team1
bag bag2; //bag to hold runs scored by players for team2
get_scores(bag1);
get_scores(bag2);
bag b=bag1+bag2; //this bag holds all the scores
cout<<" No. of players who scored zero:"<<b.count(0);
b.erase(0); //erase all zero scores from bag
cout<<" No. of players who scored a century:"<<b.count(100);
cout<<" No. of players who scored more than 0:"<<b.size();
b.erase_one(50); //checks if anybody has scored a 50 and erases it
b.insert(50); //insert a score of 50 into it
return EXIT_SUCCESS;
}
/**
* Gets the runs scored in a cricket match.
* Values are entered for 11 players
* @param bag holds all scores for all players in match
*/
void get_scores(bag& bagin)
{
int user_input;
int count=0;
cout << "Enter runs for players" << endl;
while(count<11)
{
cout << "Player" <<count+1<<":";
cin >> user_input; //input runs scored
if (bagin.size( ) < bagin.CAPACITY) //checks if no. of runs inputted is less than capacity
bagin.insert(user_input); //insert the runs to the bag
else
{
cout << "I have run out of room and can’t add that score." << endl; //if size becomes greater than capacity
break;
}
count++; //increment count to enter next score
}
}
F)
The new test program uses the bag to store runs scored by a team in a cricket match.It also prepares a report on how many players scored 0,how many scored 100 and how many scored more than 0.
get_scores is used to input runs scored by team members.
get_scores is called twice to enter runs scored by two teams and these are stored in bag1 and bag2 respectively.
Overloaded function + is used to merge scores of both teams and saved in a single bag.
count(0) is called to display the number of players who scored 0.
erase(0) is used to delete all those 0 scores.
size() is called to calculate total bag size then ,which gives no. of players who have scored more than 0.
count(100) gives the count of players who scored a century.
erase_one(50) deletes one entry of score 50 if it exists.
insert(50) inserts 50 to the score bag.
Output
Enter runs for players
Player1:23
Player2:98
Player3:56
Player4:45
Player5:0
Player6:67
Player7:34
Player8:100
Player9:100
Player10:0
Player11:23
Enter runs for players
Player1:45
Player2:34
Player3:78
Player4:34
Player5:100
Player6:45
Player7:56
Player8:23
Player9:100
Player10:0
Player11:0
No. of players who scored zero:4
No. of players who scored a century:4
No. of players who scored more than 0:18
RUN SUCCESSFUL (total time: 35s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.