bucket.cpp
bucket.h
Problem 3: Programming The Bucket data structure: Collections (or containers) are a very important part of programming. A "bucket" data structure is a container that follows the form and function of many data structures. Its purpose is to collect and store any type of values, your bucket will store only doubles. Using simple operations, like add, remove, count, etc., one can simply store and retrieve large quantities of values using a small amount of code. The bucket is an unordered collection. Simply put, an unordered collection of elements are stored in a way that their order cannot be predicted. Other data structures like stacks and queues store their elements in a predictable order (these are known as ordered collections), buckets do not. Implement the bucket class from the header file provided. The header will be called, "bucket.h". It contains a variable of type size_t to show the default size of the bucket. Initially, the array is declared at this size. Because Bucket is a class it must be defined in bucket.h and bucket.cpp. Requirements • The bucket is a utility class. It is used by client code to simply store and retrieve items of double type. There is no console input or output in the bucket class. • The bucket dynamically resizes when it gets full. Its array is created on the heap. The array should double in size when an add operation is called but the bucket is full. Be sure to deallocate the old array after copying its data to the new array. Use new to create the new array and deletell to deallocate the old one. • You must implement the bucket in two files: the header file (.h) will contain the declaration of the bucket (tis file is provided), and the definition file (.cpp) will contain the definition of the bucket's functions. Do not add to or change the public members of the class. You can add private utility functions and data members but do not alter the public section of the class. Do not include your own header files in this class. This class should be a stand-alone class that requires only C++ standard library includes. Read the pre and post conditions for each member function in the public section of bucket.h to understand its responsibilities. • You will write a test program called, "test_bucket.cpp" to thoroughly test your Bucket class' functions. What to Submit for Lab 5 Activity • This document completed. bucket.h bucket.cpp test_bucket.cpp
#include "barrel.h"
// include guard #ifndef bucket_H #define bucket_H // includes (only include these headers from the std library) #include <stddef.h> #include <stdexcept> /* The bucket class */ class Bucket { private: const size_t DEFAULT_SIZE; // must be initialized size_t itemCount; // the number of items stored in the bucket double items*; // The storage facility for the added items /** Resizes the bucket and copies old values to new resized "items" array destroys old items arry when done */ void resize(); public: 7** Constructors: there are two constructors. One takes in the initial size of the bucket and the other sets it to a default of size 10. The parameterless constructor must call the parametrized constructor with the default value */ Bucket(); /** Constructor sets the initial size of the bucket @param: initial size of the bucket */ Bucket(const size_t); /** Gets the current number of entries in this bucket. @return the integer number of entries currently in the bucket. */ size_t count() const; /** Sees whether this bucket is empty. @return True if the bucket is empty, or false if not. */ bool isEmpty() const; /** Adds a new item to this bucket. @param item The object to be added as a new item. resizes the bucket if the bucket is full. */ void add(const double item); /** Removes one unspecified item from this bucket, if possible. @return the removed item or @throws runtime_error if the bucket was empty. */ double remove(); /** Removes *one* occurrence of a given item from this bucket. @param item The item to be removed. This item will not be altered. @return True if the removal was successful, or false otherwise. */ bool remove(const double item);
/** Removes all entries from this bucket. sets the used elements to NULL */ void clear(); /** Counts the number of times a given item appears in this bucket. @param item The item to be counted. This item will not be altered. @return The number of times item appears in the bucket. */ size_t getFrequency(const double item) const; /** Tests whether this bucket contains a given item. @param item The item to locate. This item will not be altered. @return True if the bucket contains item, or false otherwise */ bool contains(const double item) const; }; #endif // end include guard
Problem 3: Programming The Bucket data structure: Collections (or containers) are a very important part of programming.
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Problem 3: Programming The Bucket data structure: Collections (or containers) are a very important part of programming.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!