Starting from:

$30

CS235-Project 2 Roster “is-a” Bag of Students Solved

In  we will work with the ArrayBag class discussed in lecture 4. This project consists of two parts:

1.     Modify the ArrayBag class

2.     Implement a class Roster which inherits from ArrayBag and stores Student objects.

The ArrayBag class (as discussed in lecture) will be distributed via GitHub Classroom. 

First you must read the ArrayBag interface and understand how it works. You will need to know how to use ArrayBag objects by understanding its interface. 

Note: Reading interfaces will be the way you learn to use language libraries, so this is great practice for that!

Implementation - 2 parts:

Work incrementally! Start from Part 1 (implement and test), when that runs correctly then move on to Part2.

Part1- ArrayBag modifications:

1.     Modify the add method so that it will not allow duplicate items to be added to the ArrayBag (conceptually the Bag becomes a Set). Hint: you can use other ArrayBag operations to implement this.


2.     Implement public method display() to display the contents of the bag to standard output in the form “item1, item2, … , itemN\n”




  /**@post prints the contents of items_ to the standard output             separated by commas and followed by a new line.**/      void display() const;


3.     Overload public operator+= to implement Set Union. Hint:  you can use other ArrayBag operations to implement this.




 /** 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 items from a_bag as space allows

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


Note: Because ArrayBag is of fixed size, += will only copy as many items from a_bag as there is space available without deleting its original contents. NOTICE HOW FIXED SIZE CAN BE AN ISSUE AND FORCE UNINTUITIVE IMPLEMENTATIONS! We will address the fixed-size problem soon.


4.     Overload public operator-= to implement Set Difference. Hint:  you can use other ArrayBag operations to implement this.




 /** 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 from this (the calling) bag

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

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

IMPORTANT: Please remember that you DO NOT compile (or include in your project if using an IDE) the implementation (.cpp) of a Template class. Please look at slides 57-59 from Lecture 3 and make sure you understand separate compilation with templates (it will probably help if, as suggested on slide 61, you first run a dummy test and make sure you can compile a simple/trivial template class)

Part2 - Roster class:




Write a class, Roster, that inherits from ArrayBag but it is not a template, instead it stores Student objects. The roster class should have at least the following public methods:

1.     Roster(); //default constructor for empty roster


2.     /**parameterized constructor

     @pre the input file is expected to be in CSV         (comma separated value) where each line has format:

        “id,first_,name_,last_name\n”

     @param input_file_name the name of  the input csv file

     @post Student objects are added to roster as per the data

           in the input file      **/

    Roster(std::string input_file_name);


3.     /**@post displays all students in roster, one per line             in the form "first_name_ last_name_\n"

     **/     void display();

In the starter files you will also find the Person class with overloaded operator== . This is necessary otherwise you will have a problem when the modified add() methods tries to compare two Student objects to add them to the Roster. This statement is not trivial, if you don’t understand please ask!

 

Testing:

Testing your modification to ArrayBag:

Before you move to part2, YOU MUST  make sure that your modifications to ArrayBag work correctly. To do so write your own main function (not for submission) that does the following:

•       Instantiate two ArrayBag objects that stores integers

•       Add integers to the two bags (some integers should be common to the two bags)

•       Call += on one of the bags, display its contents and make sure the operation worked correctly (i.e. bag1∪bag2) and that your modification to add worked s.t. there are no duplicates.

•       Call —= on one of the bags, display its contents and make sure the operation worked correctly (i.e. bag1 — bag2).

Testing the Roster class:

Again in a main function (not for submission) do the following

•       Instantiate a Roster object with the name of an input file

•       Display the roster and make sure that all students in the input file were added to the Roster.

The data: The input file will be in csv (comma separated value) format, and each line corresponds to the information necessary to create a Student object. Each line in the input csv has the following format:

id,fist_name,last_name

A sample input file named roster.csv is available in the distribution repo for GitHub Classroom. Review — reading the input:

In C++ to read input from a file you need a file stream

#include <fstream

Since we are only reading input you can use an ifstream object. 

Since we are reading from a csv file, every student is on a line. On each line, each data item is separated by a comma. 

You may use string::getline() to read lines from the ifstream. To use getline() you must

#include <string

You may find it useful to use a stringstream to then read each piece of data (id, first_name and last_name) from each line you read from the input file.

#include <sstream

You may use getline() to read data from the sstream as well. Remember that getline() may take a delimiter. The default delimiter is ‘\n’, but if you are reading comma-separated values you can use ‘,’ as the delimiter. getline(stream, variable, delimiter); Don’t forget to:

-     Open the stream before reading.

-     Check that opening the stream did not fail before reading, and output an error message if it does fail.

-     Close the stream after reading.

Note: Reading from input file should be familiar from CSci 135. If you need to review, lookup ifstream, sstream and string::getline()

References for each of these are easily found online:
http://www.cplusplus.com/reference/fstream/ifstream/
http://www.cplusplus.com/reference/sstream/stringstream/
http://www.cplusplus.com/reference/string/string/getline/

If you need help with this even after having reviewed the documentation, please don’t hesitate to ask for help. After this project you will be expected to be able to read from csv files.

More products