CS 112b
Spring 2002
Ileana Streinu
Homework 4
Thursday Feb. 21, 2001
Due Wednesday 02/27/2001
by midnight
Outline
This homework is mostly technical. Its goal is to make sure that
you know how to handle the techical details of developing
programs using templated functions and basic classes.
Parts I and II can (and should be) done before the Tuesday
class, possibly with some help from the TAs on the technical
aspects regarding the new style of compilation using makefiles.
Should technical problems arise with Part II, you'll
get extra help from me in the lab on Tuesday.
Part III is a little more sophisticated than Part II, but not
much. I will give you extra hints on it on Tuesday in class,
should there be a need.
To avoid conflicts (at least in the usage of the makefile) you
must develop each program in a separate directory, and submit
them in separate directories.
Part I
Develop a full program for Insertion Sort, using the model
developed in class today.
- Insertion sort on an array of integers:
complete the program we started writing in class today for
insertion sort on integer arrays (the incomplete model is posted
here). Expand it to work also for an array
of characters and an array of CustRec's.
This program should be called sort.cpp.
- Insertion sort with templates and functions passed as parameters:
modify the templated program we wrote for
selection sort to implement insertion sort. The structure you
want to use is started
here. Test your program on arrays of
integers, characters and CustRec, sorting both by id and balance.
This program should be called sortTP.cpp.
- Test both programs and submit a single typescript for them,
called typescript.
Part II
Getting started with classes and object-oriented programming in C++.
- Use the class notes for
lecture 8. Copy,
compile and execute the JarType files to make sure you know how to.
- Modify the JarType class to include new functions for
removing cookies and testing the status of a cookie jar:
- delete: takes in a number k, and removes k cookies from the jar.
- IsEmpty: returns true or false, depending on whether the
cookie jar is empty or not.
- IsFull: returns true or false, depending on whether the jar
is full or not. To do this part, you will have to add a new
private variable maxNum in the description of the
JarType. This variable will have to be initialized in the
constructor, and will give the maximum capacity of the jar.
- Test these functions and prepare a typescript named
typescript to show the execution of your program.
Part III
- Define a new class CookieJar, which will hold not just the
number of cookies in the jar, but also keep track of their
identity, in the order in which they've been added to the jar.
- Technically
speaking, you'll have to use an array to store the "cookies".
Assume that each "cookie" is identified by an integer
number (think of it as an id number, something like a bar code on
a grocery item).
- In the beginning, a jar is empty (i.e. the constructor
initializes it to be empty).
- The class should contain a function to add a
cookie to the jar (takes in an integer, "stores" it in the array
at the next position), and a function to "dispense" a cookie
(returns the integer corresponding to the last cookie that was
added).
-
Other functions you should include in your class are: IsEmpty,
IsFull and Print (this function should just print the id's of the
cookies stored in the jar).
- Test the new class using an appropriately designed test driver.
- Create a makefile for compiling the program for testing the
CookieJar class, test it and save a typescript as typescript.
Submit as follows:
- Using submit Hw4I:
sort.cpp, sortTP.cpp and typescript
- Using submit Hw4II:
JarType.h, JarType.cpp, testJarType.cpp, makefile, typescript
- Using submit Hw4III:
CookieJar.h, CookieJar.cpp, testCookieJar.cpp, makefile, typescript
Grading guidelines
- Part I and Part II, done perfectly, will get you a grade of B+.
- To get a grade in the A category, you must do Part III. A
working program with static testing (i.e. similar to the one
linked to from the lecture notes)
is worth an A-.
For an A, you
must design a better (menu-based) test-driver, which will allow
the user to choose which function to test next, among the
available choices in the CookieJar class.
- A clarification: the menu-based testdriver should NOT have
options for constructor(s). It should statically allocate a
variable of CookieJar type, and apply on it the functions chosen
by the user (via the menu-based interface)
Ileana Streinu