C M S C     2 1 4
C o m p u t e r   S c i e n c e   I I
S p r i n g   2 0 0 2


Project #2

Due Saturday, Mar 16th, by 11pm
Due Wednesday, Mar 13th, by 11pm

Latest Posting: March 13, 2002 (NEW!)
(Errata: Fixed BNF for VERIFYS)
Originally Posted: February 28, 2002
2nd version Posted: March 10, 2002 (NEW!) 3rd version Posted: March 11, 2002 (NEW!)
(Added README section, link to Project 2 Proposal, and corrected the BNF, which appears in red)

Purpose

  1. To templatize a class. In particular, convert MovieSortedList and MovieArrayList into template classes, SortedList and ArrayList.
  2. To write recursive functions. In particular, convert all loops in MovieSortedList to recursion.
  3. To extend the features of the MovieArrayList by adding an erase() and being able to call a sort on the list.
  4. To extend the features of the tester classes.
  5. To gain experience with a "function object".
  6. To write documentation for the proposed classes for Project 4.
  7. To debug code from previous project, so that it doesn't give you problems on this one.

Academic Integrity Statement

Please note that *all* programming projects in this course (including this one) are to be done independently or with the assistance of the instructional staff of this course only.

Please review the policies outlined on the class syllabus concerning the use of class computer accounts and concerning the University's Code of Academic Integrity. The instructors of this course will review the programs submitted by students for potential violations of the Code of Academic Integrity and if it is believed that a violation has occurred it will be referred to the Office of Judicial Programs and the Student Honor Council.

Hardcoding is considered a violation of academic integrity

Checklist

Click here before submitting.

Project 2 Checklist

Background

Although this project is shorter than the previous project, you should devote enough time early, so you don't fall behind.

Background Reading

Please read the following as you do the project.

Files Provided

In addition to the primary input and primary output, the following files are in the posting account under Projects/P2.

Files Submitted

Use Checklist (above) as final check for project submission. The following is a PRELIMINARY list, and may not be complete.

Submit the following files.

Furthermore, you will need to submit the following files.

Tasks

The following is a suggested way to approach the project.

Task 0: Modify StyleExercise.cpp (NEW! File renamed)

This is a file which has hardly been indented. Using the style guide, modify so it looks nice. Make sure the comments also look nice, based on the style guide. Several files starting with "Style" are now in posting account. Modify StyleExercise.cpp instead of StyleExercise.txt. The directions for how to modify are provided in a separate file.

Task 1: Finish up any code you didn't write, and documentation from Project 1.

If you didn't write MovieArrayList, you should complete that. It will be easier to templatize. That is, rather than templatizing first, and changing code later on.

If you didn't write documentation for MovieArrayList and MovieSortedList, you should now write the documentation for ArrayList and SortedList. If you did, it should be just renaming the classes and adding a method here or there, but otherwise, very little work.

The new documents will be called ArrayList.doc and SortedList.doc. (NEW) You should submit this as part of your tar file.

Task 2: Make following changes to Movie and MovieSortedList

Task 3: Extend MovieArrayList

Add the following method to the PUBLIC section of MovieArrayList class in the header file.

Add/modify the following methods to the PUBLIC section of MovieArrayList::Iterator.

Add/modify the following methods to the PUBLIC section of MovieArrayList::ConstIterator. (NEW) These methods are to emulate pointer arithmetic, since iterators behave like pointers. Pointer arithmetic is part of the C/C++ language.

Make sure to test all of this.

Task 4: Remove loops from MovieSortedList.

This is an exercise in recursion. To remove a loop will require converting everything to recursion. There is background reading about converting loops to recursion, and recursion in general. It is located in the Tutorial section that can reached from the main webpage. This should hopefully converting 5 or fewer loops to recursive calls.

Writing recursive function calls will require you to write recursive helper functions. Functions are the basis of recursion, so your code should get smaller.

Note: this is merely an exercise to get you to learn recursion. In reality, you wouldn't opt to use recursion, unless it makes sense to do so, since recursion uses up to O(n) stack space where as iteration with loops often uses O(1). Nevertheless, the goal is to learn recursion. Just keep in mind that it may not always be space efficient.

(NEW) There is a new link on converting loops to recursion.

Task 5: Templatize MovieSortedList and MovieArrayList

Convert MovieSortedList to a template class called SortedList. SortedList uses a two input template parameter:
template <class Data, class Comparator>
where Data is a class like Movie, and Comparator is a comparator class like MovieComparator. The comparator class is useful if Data happens to be a pointer type.

Convert MovieArrayList to a template class called ArrayList. ArrayList uses a one input template parameter:

template <class Data>
where Data will be a class like Movie. It assumes that the class has overloaded comparison operators.

Task 6: Write the sort214 function to sort ArrayList...

A header file will be provided to you called Algorithm.h. You will implement sort214 method in Algorithm.cpp. This function will take two parameters: two Iterators pointing to the first element to start sorting, and one past the last element. You may assume that they point to the same list.

You may use any sorting algorithm you wish. This may require looking up your 114 textbook or the current 214 textbook. The choices you have are: insertion sort, selection sort, bubble sort, and quicksort.

You will need to modify the algorithm in the following way. Normally, sorting algorithms assume you have an array. However, you can actually sort with pointers too, if you are careful.

If you have an algorithm for, say, bubble sort, convert it as follows:

void bubblesort( int *first, int *end )
{
   // code
}
where first points to the first element in the array to be sorted, and end points to one past the last element to be sorted. Once you have this version working, you should be able to easily convert it to a version using iterators since iterators are essentially pointers.

sort214 will be a template function.

You should also write a template function for compare(). The compare() method should now be removed from . You do NOT have to write a template function for compare() (unless you want to). You use the MovieComparator object instead to compare Movie objects. (3/11) (CORRECTION) You should use the MovieComparator object to compare Movies in SortedList, but use relational operators to compare ArrayList<Movie>.

Task 7: Convert vector in the Movie class to ArrayList

In your Movie class, a vector of strings was used. Instead, use an ArrayList of strings. Change this in the header file, and make any needed changes in the .cpp file. There should be few changes, and those that occur are only due to the ArrayList having less functionality than a real vector class.

The purpose of this is to show that you can write a class that's very similar to an STL vector class (though the vector class is still perhaps more sophisticated

Run your test driver runTestDriver() to make sure it's still behaving correctly (thus, the reason for the test drivers).

Task 8: Convert Tester Classes

In MovieSortedListTester, MovieSortedList are used used. It's part of SimpleMap and you see it being constructed in constructOp(). Where you see MovieSortedList, convert it to (CORRECTION) SortedList<Movie, MovieComparator>.

Similarly, MovieArrayListTester uses MovieArrayList. Instead of this, use ArrayList<Movie>.

Task 9: Add new commands to tester classes

There will be commands to add iterators to the input files. You can do one of two things: either create tester classes that specifically handle iterators, or add a new SimpleMap object for storing maps of string objects to MovieSortedList::ConstIterator * (3/11 CORRECTION: Iterator was changed to ConstIterator) objects. (PREVIOUS paragraph reworded to make it easier to read)

The list of commands will appear in the next section.

(NEW) Task 10: Modify runTestDriver() for ArrayList and SortedList template classes.

Again, just to see if it works.

Additional Commands

The same commands that were used in project 1 are used in Project 2. The main difference is "behind the scenes". The tester classes now use templatized versions of MovieSortedList and MovieArrayList. In fact, MovieSortedList and MovieArrayList should no longer be part of your tester classes. They should be replaced by SortedList<Movie> and ArrayList<Movie>. The names of the testers will remain the same, however.

Here are additional commands for the categories

Movie

No additional commands for Movie.

However, there is a change to the MOVIE PRINT command. In the MovieTester code, you will need to print out the object by simply doing:

cout << (*this)[ varName ] << endl;
That's because Movie print() method now prints the proper title of the movie in double quotes, three spaces, "Earnings: ", and the earnings (but no newline). The newline will be taken care of when printing the object (as above).

MovieSortedListTester

Here are the list of new commands. (BNF to be added later)

MovieArrayListTester

The commands used for the ConstIterator in MovieSortedListTester will also be applicable to ConstIterator for MovieArrayListTester as well as Iterator for MovieArrayListTester. (BNF to be posted).

(NEW) The following is an explicit text for what the previous paragraph says.

In addition, there are additional commands specific to MovieArrayListTester.

Project 4 Background

In the following link, you will see a sample input file, and sample results of what your code should be able to handle. Use this a guide. The actual input files may change some, but shouldn't be too drastic.

In a plain text file called Proposal.two.doc, begin to figure out what data members you will use. For more sophisticated classes, you will use SortedList and ArrayList template classes. Start to write out documentation similar to the ones you wrote in Project 1 for such classes. If you have more than four classes, you will only be required to write documentation for the four simplest classes (realize that if the documentation is complete now, then you may avoid work later on).

For the proposed classes, also write class headers. Each class you write will start with a lowercase 'p. For example, if you have a class you want to call "Calendar", then you would actually write a class called "pCalendar". The purpose of this is to make it easier to identify your proposed classes.

(3/11) (CORRECTION) Put all classes descriptions in the Proposal.two.doc file. That file will contain all the class descriptions. In project 3, you will begin to split them up into other .doc files

(CORRECTION) No longer have to do following part that is crossed out).

You should have a runTestDriver() method, which should "print" out methods. Have "stub" functions, i.e., write out the function names, and parameters, but leave the bodies empty. If the function has to return something, return the simplest thing that makes it work (for example, if it has an int return value, return 0, if it has a bool, return true).

In runTestDriver(), you should have code that "tests" the various methods. Read the tutorial on "Writing Test Drivers" in the Tutorial section of the main webpage. You should have "output" for the various commands so that, in theory, you are printing out the actual result and the "predicted" result. If your test driver actually ran, a person looking at your output should see the actual and predicted result.

The goal of writing a test driver prior to coding is to get you to think about how the class ought to behave before you worry about how you want the class to be implemented. Thinking about it's behavior is a good way to think abstractly.

Of course, once you start to implement the object, you may realize that, for efficiency reasons, you may have to implement it a different way, but the goal is to concentrate on class behavior first, and to worry about the actual implementation details later on. After all, that's how the user of a class will think. They only see the abstraction you provide as a class writer.

Once you begin to implement the actual classes, you may discover that you make bigger changes. That's fine. The idea is for you to worry about design prior to coding, and writing test cases prior to coding is a step in that direction.

Here is the Project 4 Preview, Part 2 (POSTED)

Debugging Hints

When you write the code, obviously, use a test driver. You need to submit it anyway, so use it.

Also, for ArrayList, it's a good idea to print a message indicating you've gone out-of-bounds, if the index is out-of-bounds. Notice that regular arrays won't tell you such things.

README

The README has been posted in the posting account (and README.directions). Copy this to your directory, and answer the questions in it.

How to Submit Your Project

In order to successfully submit your project, it must pass the primary input and produce the primary output. The primary input will be sent to your project using input redirection.

   p2 < primary.input
Your program MUST produce the executable p2, EXACTLY, when the command "make" is run with no arguments. Thus, you must use a makefile named exactly "Makefile".

Your program must use cxx with the options -w0 -std strict_ansi. Unfortunately, while we have aliased this for you, it doesn't always work with Makefile, so use the full name in your makefiles.

We will use "diff -bwi" to test your code. This ignores case, treats N blanks as 1 blank, treats N blank lines as 1 blank line. We will also strip all blank lines from your input when matching to ours. However, diff is sensitive to punctuation, number of dashes, etc. so you should attempt to make your output match ours as closely as possible otherwise you may not be able to submit your program. If this is the case (that your program does not submit) you must fix your code until the output it produces matches that of the primary output. Note: "hardcoding" output is a violation of the University's Code of Academic Integrity.

To submit your project you must first combine the following files (and only these files) into a single tar file using the Unix tar command:

You may name your tarfile anything you want, but p2.tar is the suggested name.

NOTE! You should make a backup copy of all your files BEFORE attempting to create your tarfile!!!! You have been warned.

After creating the tar file you should submit that one file using the following command:

submit p2.tar 2
where p2.tar can be replaced by whichever tar file you have created, and 2 refers to the current project,

See the class syllabus for policies concerning email
Last Modified: Wed Mar 13 20:20:09 EST 2002
left up down right home

Web Accessibility