Starting from:
$35

$29

Assignment 1 – A Collection Class Solution


Objectives: This assignment gives you some experience with designing and writing C++ classes using “big five” (see the textbook), operator overloading, exception, and templates. Also you will learn how to use the command line interface (CLI) and makefiles.

(5 points) Create a text file, called README, in which you provide:

– Your First Name, Last Name, UIN, Section Number, User Name, E-mail address

– State the Aggie Honor statement:


I certify that I have listed all the sources that I used to develop the solutions and code to the submitted work.

On my honor as an Aggie, I have neither given nor received any unauthorized help on this academic work.

Your Name    Date


– List any resources used such as webpages (provide URL). Do not mention the textbook and discussions with the Instructor, TA, or Peer Teachers.

– List any known problems with the assignment you are turning in. For example, if you know your code does not run correctly, state that. This should be a short explanation.

– Provide a short description or pseudocode.

– Write how you tested your program(s) for correctness and how you used exceptions for the error handling.

– Submit to eCampus an electronic version of the file README along with your C++ code for the problems listed below and a hard copy to your TA.

– The assignment will be graded focusing on: program design, correctness.

– Your programs will be tested on a CSE Linux machine.

– Provide all tests done to verify correctness of your program in the report. Give an explanation why have you selected such tests.

– Your program will be tested on a TA’s input file.


Problem Description – Part 2 (100 pts)


    1. (35 points) The class Collection defines a collection as an array that can be automatically resized as neces-sary using dynamically allocated arrays.

You are not allowed to use the STL class vector.

        (a) Write a C++ class Collection which uses the class Stress_ball. The collection holds stress balls of different colors and sizes (see Part 1) and can contain many stress balls of the same color and size. The class Collection should have three private members:

Stress_ball *array; //pointer to dynamically allocated memory int size; //logical size of array - the number of elements in use int capacity; //physical size of array

Note that size <= capacity.

        (b) There are the following functions defined for a collection:


1

constructor with no arguments, size and capacity are 0, and array is nullptr. constructor with one argument which is the required size of the collection

copy constructor – makes a copy of a collection

copy assignment – overwrites an exiting collection by another collection

destructor – destroys a collection (deallocates allocated memory, set to zero size and capacity) move constructor – efficiently creates a new collection from an existing one

move assignment – efficiently copies a collection during an assignment insert a stress ball to the collection:

void insert_item(const Stress_ball& sb);

If the collection is full, increase the array by doubling its size. Use the private helper function resize() to complete this task. The function resize() should double the size of the array and correctly copy elements from the old array to a new array.

check if a stress ball of a given color and size is in the collection; return true if it is there and false otherwise:
bool contains(const Stress_ball& sb) const;

remove and return a random stress ball (you have no control which stress ball is selected): Stress_ball remove_any_item();

Do not decrease the size of the array. Also, be sure that there are no gaps between elements of the array. Throw an exception if the collection is already empty.

remove a stress ball with a specific color and size from the collection: void remove_this_item(const Stress_ball& sb);

Do not decrease the size of the array. Also, be sure that there are no gaps between elements of the array. Throw an exception if the collection is already empty.

make the collection empty (deallocate allocated memory, set to zero size and capacity): void make_empty();

check if the collection is empty; return true if it is empty and false otherwise: bool is_empty() const;

return the total number of stress balls in the collection: int total_items() const;

return the number of stress balls of the same size in the collection: int total_items(Stress_ball_sizes s) const;

return the number of stress balls of the same color in the collection: int total_items(Stress_ball_colors t) const;

print all the stress balls in the collection (print color and size of a stress ball, see the class Stress_ball): void print_items() const;

    (c) To directly access a stress ball in a collection, overload operator[]. It will access a stress ball in array at position i where i starts from 0 through size-1:
Stress_ball& operator[](int i);

    (d) To directly access a stress ball in a const collection, overload operator[]. It will have the exact same body as the above overload, but the function header should read:
const Stress_ball& operator[](int i) const;


The class Collection should be presented to your TA or PT during the labs by January 29. You should test all the implemented functions/operators of this class. No submission of the above part to eCompus.


    2. (45 points) Add these functions for manipulating collections. They are not part of the class Collection.

input operator (reading from a file):

istream& operator(istream& is, Collection& c);

reads from the istream is pairs in this format: color size (no parentheses or colons, use space to separate

them). As colors use strings (you can use STL class string here): red, blue, yellow, green, and as sizes

use strings: small, medium, large. You need to read from an input file where each pair is in one line. Use

at least two input files: stress_ball1.data and stress_ball2.data for two collections.

2

output operator:

ostream& operator <<(ostream& os, const Collection& c);

prints to the ostream os all the collection items in format: (color, size), each in one line. Use cout as output but you can use an output file either.

a union operation that combines the contents of two collections into a third collection (the contents of c1 and c2 are not changed):
Collection make_union(const Collection& c1, const Collection& c2);

a swap operation that swaps two collections:

void swap(Collection& c1, Collection& c2);

Use the move constructor and move assignment to do this. Do not copy the collection elements.

A sort function that sorts the collection with respect to the size of its elements (small < medium < large):
void sort_by_size(Collection& c, Sort_choice sort);

Then elements will be sorted with respect to their size in array (we do not sort them with respect to color). You need to implement 3 different sorting algorithms (do not use the STL sort): bubble sort, insertion sort, and selection sort. Here is the enum class Sort_choice:

enum class Sort_choice { bubble_sort, insertion_sort, selection_sort }; Use switch statement to choose the required one. You need to test all three sort functions.

    3. (15 points) Write a program for testing collections where you will test the class Collection

    (a) Call the file collection_test.cpp. It should contain the main() function. Use makefile to compile your program.

    (b) Create two collection objects. Fill them with stress balls: read stress balls from a user input file. Use between 10 and 15 stress balls.

    (c) Sort and print out their contents: print out the number of stress balls of a given color and the total amount of stress balls in the first and the second collection.

    (d) Create a third collection which is the union of the two objects from (b). Print its elements. Print the number of stress balls of the same color and size. Print the total number of stress balls.

    (e) Swap two collections in (b). Print its elements to check if they correctly swapped.

    (f) Sort the first collection in (b). Print its elements to check if it is correctly sorted.

    (g) You can do other tests not mentioned above in order to be sure that all you implemented functions are correct.


The class Collection and the above functions should be presented to your TA or PT during the labs by Febru-ary 5th and submitted to eCampus. You should test all the implemented functions/operators.






















3

More products