Question

In: Computer Science

C++ Class involving duplication The goal is to make add() not allow duplicates. Bag<int> bag1; bag1.add(1);...

C++ Class involving duplication

The goal is to make add() not allow duplicates.

Bag<int> bag1;

bag1.add(1); //ok

bag1.add(2); //ok

bag1.add(1); //ignore but do not break or terminate. The submission will go through but will not get added to array

#include <iostream>
#include <string>

#include <vector>
using namespace std;

template<class T>
class Bag
{
public:
    Bag();
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const T& new_entry); //Modify this
    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;

    void display() const;  // displays the output 


protected:
    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
    int getIndexOf(const T& target) const;


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

template<class T>
int Bag<T>::getCurrentSize() const
{
    return item_count_;
}  // end getCurrentSize

template<class T>
bool Bag<T>::isEmpty() const
{
    return item_count_ == 0;
}  // end isEmpty


template<class T>
bool Bag<T>::add(const T& new_entry)
{
    bool has_room = (item_count_ < DEFAULT_CAPACITY);
    //bool notDup = items_.getFrequencyOf(new_entry) == 0; 
    if (has_room) //&& notDup) 
    {
        items_[item_count_] = new_entry;
        item_count_++;
        return true;
    }  // end if

    return false;
}  // end add

template<class T>
void Bag<T>::display() const {
    for(int x = 0; x < item_count_;x++)
        cout << items_[x] << ", "; 
}


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

template<class T>
int Bag<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

template<class T>
bool Bag<T>::contains(const T& an_entry) const
{
    return getIndexOf(an_entry) > -1;
}  // end contains

template<class T>
std::vector<T> Bag<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

Solutions

Expert Solution

/******************************************************************************
The error is because of this line items_.getFrequencyOf(new_entry) == 0;
getFrequencyOf is method of class Bag
you are trying to acces it on items which is int in this case.

*******************************************************************************/

#include <iostream>

#include <string>

#include <vector>
using namespace std;

template < class T > class Bag
{
public:
Bag ();
int getCurrentSize () const;
bool isEmpty () const;
bool add (const T & new_entry);   //Modify this
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;

void display () const;   // displays the output


protected:
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
int getIndexOf (const T & target) const;


};

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

template < class T > int Bag < T >::getCurrentSize () const const
{
return item_count_;
}               // end getCurrentSize

template < class T > bool Bag < T >::isEmpty () constconst
{
return item_count_ == 0;
}               // end isEmpty


template < class T > bool Bag < T >::add (const T & new_entry)
{
bool has_room = (item_count_ < DEFAULT_CAPACITY);
bool notDup = getFrequencyOf (new_entry) == 0;
if (has_room && notDup)
{
items_[item_count_] = new_entry;
item_count_++;
return true;
}               // end if

return false;
}               // end add

template < class T > void Bag < T >::display () const const
{
for (int x = 0; x < item_count_; x++)
cout << items_[x] << ", ";
}


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

template < class T > int Bag < T >::getFrequencyOf (const T & an_entry) const 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

template < class T > bool Bag < T >::contains (const T & an_entry) const const
{
return getIndexOf (an_entry) > -1;
}               // end contains

template < class T > std::vector < T > Bag < T >::toVector () constconst
{
std::vector < T > bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back (items_[i]);

return bag_contents;
}               // end toVector

using
namespace
std;

int
main ()
{

Bag < int >
bag;
bag.add (1);
bag.add (2);
bag.add (1);
bag.add (3);
bag.display ();

return 0;
}


Related Solutions

C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the...
Given the following Java code: class C { public int foo(C p) { return 1; }...
Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...
C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5...
C++ Class class Billfold { private: int twenties{}, tens{}, fives{}, dollars{}; public: /* Step 1. (5 points) In class Billfold, write two Billfold constructors: default: set all bill counts to zero; do not allow the bill counts to be uninitialed. 2nd: set all bill counts to non-negative initial values; use parameters for: twenty, ten, five, dollar. IMPORTANT: prohibit negative values - if negative bill value given, use 0 for that bill instead. Accept other bills if >= 0. Tip: you...
Make a LandTract class with the following fields: • length - an int containing the tract's...
Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods: • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if...
Make a LandTract class with the following fields: • length - an int containing the tract's...
Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods: • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if...
USE C# to write this. 1.To the capsule, add a script called “MoveIt”. This will make...
USE C# to write this. 1.To the capsule, add a script called “MoveIt”. This will make the capsule move repeatedly between (3, 0, 0) and (-3, 0, 0), moving 1 unit per second, moving in the positive direction initially. [Note: The capsule should move toward (3,0,0) from its initial location of (0,1,0) and then move toward (-3,0,0), etc.] 2.To the yellow cube, add a script called “RotateIt”. This will make the cube rotate (30, 60, 90) per second. 3. Lastly,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT