X459 - Introduction to C++, Assignments
This page lists the readings and assignments given each week. It will also
contain pointers to useful resources, when applicable.
Assignments by week:
- For 4/21/97:
The Noah's Ark program.
The scenario is this: you are on Noah's ark with a set of animals. As the months pass, the animals begin to breed. You need to keep track of your animal inventory down below.
The ark has 3 types of animals: mammals, birds, and fish. Your program should have at least 2 kinds of mammals (cat, dog), 2 kinds of fish (goldfish, shark), and 2 kinds of birds (eagle, parakeet). You should start the program with 2 animals of each species on the ark, one male and one female, for a total of 12 animals.
The program should give you a menu such as follows:
You have been on the ark for 11 months. Would you like to:
1 - Let another month pass
2 - Check inventory
3 - Quit
If you let another month pass, all of the animals on the ark are aged by a month. Each female animal in heat gives birth to another animal of the same species. You will then get the same menu:
You have been on the ark for 12 months. Would you like to:
1 - Let another month pass
2 - Check inventory
3 - Quit
If you hit 2, you should see something like this:
Animal #1: I am a female cat, 12 months old
Animal #2: I am a male cat, 12 months old
Animal #3: I am a male cat, 0 months old
Animal #4 I am a male shark, 12 months old
Animal #5: I am a female shark, 12 months old
Animal #6: I am a male shark, 6 months old
.........
All female mammals give birth once every 12 months. All female fish give birth once every 6 months. All female birds give birth once every 9 months.
I have provided a set of classes in Animals.h and Animals.cpp. They include the following:
AnimalList holds a list of Animal pointers for you. You should use this class to hold the main Animal list and to return new animals when Animals give birth. The interface to this class allows you to add animals, get the total number of animals, append animal lists together, and iterate through the list by using the resetIterator() and nextAnimal() functions.
The Animal base class is the base class for the rest of the hierarchy. Some of the functions have been implemented, and others are pure virtual.
Hints:
- You probably won't need to make any changes to Animal. If you use them as they are you are more likely to get a good solution. Do not make anything that is private public or protected, and do not add anything unless you are adding more features to the problem just for fun or unless you see a short cut. One short cut in particular will allow you to set data in Animal in the constructors of its subclasses to simplify some of the subclass logic. If you think of one of these, then you can modify Animal slightly.
- No changes to AnimalList are recommended.
- Mammal, Fish, and Bird will also be abstract classes, but they should implement the functionality that is specific to each of them.
- If you find yourself using 'switch' statements in animals.cpp, you are doing something wrong. The model should be to ask a class to do something ("class, print yourself") and not to use a switch or if statement to examine the class and take action (if cat, print "cat", if dog, print "dog", ...) That is what virtual functions are for.
- Most of your methods should be one or two lines.
- You should start the program by declaring an AnimalList and by calling addAnimal 12 times to populate the ark with the original cargo (all newborns). When you want to loop through the list, you need to call resetIterator() to start iterating through the list at the beginning and then make calls to nextAnimal() over and over until you get a NULL pointer.
- The private data and methods of Animal aren't accessible to the subclasses, but that's OK since you have public interfaces into them.
- Remember that methods defined in base classes can call methods defined in derived classes. For example, a method in Mammal can call giveBirth(), even if you have giveBirth() implemented in Cat. This is because there is a prototype for giveBirth() in Animal.
For 4/7/97:
The MIDTERM!!!
For 3/31/97:
Read chapters 13-14.
Tips 93, listing 5, and 110, listing 7.
Programming assignment:
Copy Mystring.h and Mystring.cpp into your programming environment. Build a project using this library and a sample main program, like my main.cpp. Convince yourself that it works.
Now, add the following methods to the Mystring class:
void reverse();
void upper();
void lower();
And, update your main program to prove that they work.
If you are really motivated, add some of the additional operators in the string class defined in tip 110. Again, write some code to prove that they work.
For 3/17/97:
Read chapters 11-12. We will cover mostly 11 next week.
Read tips 84-92.
Programming assignment, due 3/24: The Banking Assignment, part 2
This assignment will allow you to make some improvements and add some features
to your banking program. It should exercise some basic class concepts and some
memory allocation ideas. If you use classes correctly in this assignment,
your will have somewhat of an OO design. Please follow the following steps to
build your solution:
- Add a class called Transaction. This class should be enough to allow
you to print a statement of all transactions after the user is done
banking. It should contain the following
private data members: transaction type (open, withdraw, deposit), dollar amount,
and a text comment. It needs a constructor, but that might be the only method
you need to add for now. The prototype for the class should be in
Transaction.h, and the implementation should be in Transaction.cpp.
- Convert your Account structure into an Account class. Make the data
that was in the struct into private members of the class. Add an additional data
element to store a list of transactions. This should be done with an array
of Transaction*, and you should use new and delete when you add and remove
transactions from this list. Add methods to construct
the class, get and set the data, and handle opening, withdrawing,
depositing, and printing a statement. The method that prints a statement
should loop through all of the elements of the transaction list and print
each transaction, along with the final balance at the bottom. Put the
prototype of this class into Account.h, and put the implementation (the methods)
into Account.cpp. No code that is not part of the Account class should be in either of these
files. One or both of these files will need to include Transaction.h. Neither should
include Transaction.cpp (you should almost never include a .cpp file).
Your main program should allow a user to open an account, do
transactions, and then print a statement.
Due 3/10/97:
Read chapters 9-11. We will cover through 10 next week but might touch on 11.
Read tips 66-84.
Programming assignment: The Banking Assignment
This assignment will exercise reference parameters, value parameters, struct, and (most important) program decomposition.
This program will act as a bank. The general behavior will be:
- User walks into the bank
- User opens an account. This consists of:
- Asking the user for a name.
- Asking the user for an account balance.
- Asking the user for an interest rate. (OK, this is a very nice bank!)
- The user now gets to perform transactions until leaving the bank. These transactions are:
- Print a statement. This statement should say something like, "Hello (your name), your account balance is (amount) and your current interest rate is (rate)". You can make this more fancy if you want.
- Make a deposit. This should ask you for the amount, add it, and then print your balance (or, call the function which prints a statement)
- Make a withdrawal. Same as deposit. You could add some error checking to this routine as well.
- Financial planning. Ask the user for the current month and year. Ask for the retirement month and year. Using the number of years between and the current interest rate, project the amount the user will have upon retirement.
Hints:
- Declare a structure.
- Declare a function for each of the operations. Declare more functions to support them if needed.
- No globals that aren't constants. Pass the account info around as a function argument.
- Try to use references and not pointers where possible.
- Feel free to embellish operations, especially the financial planning one. You can also add more operations if you are motivated.
Due 3/3/97:
Read tips 44-68, Enough Rope to Shoot Yourself in the Foot.
Read Ch. 7-10, C++ from the Ground Up.
Programming assignment:This assignment should exercise string, pointer, and function constructs from Ch. 6-7. In this assignment, you will create a string utility library. Please implement the following functions:
- A function to reverse a string ("spam" gets turned into "maps").
- Functions to turn strings into uppercase and lowercase.
- Function to reverse and uppercase a string.
- Function to reverse and lowercase a string.
- An encrypt function. Invent your own encyption algorithm.
- A "bonus" function. This should take an array of strings and perform some operations with all of them. Be creative.
Please follow these guidelines:
- The library should be put into a separate header (strUtil.h) and implementation (strUtil.cpp) file. The main program should be put into main.cpp. It should contain some code to read strings from the user and then exercise the string utility functions to show that they work.
- Documentation is important.
- Use either array or pointer syntax. Either can be appropriate for most routines. Be efficient, though.
- We will probably use pieces of this assignment for future assignments, so keep it around.
- Due 2/25/97:
Read tips 22-43, Enough Rope to Shoot Yourself in the Foot.
Read Ch. 4-6, C++ from the Ground Up.
Programming assignment:The main objective of this assignment is to exercise the concepts from Ch. 2-4. The major things I would like to see covered are:
- cin and cout
- if then else
- for, while, and/or do loops
- character arrays
- C string functions (strcpy(), strcat(), strcmp(), strlen())
I made one suggestion for a problem to solve during class. It is a "guess the word game". The problem is as follows:
- The computer picks a word, either by asking the user's opponent a word through cin, or by including an array of predefined words (p. 105 for syntax) and using the rand() function to pick one (this is the preferred method).
- The computer tells the user how many characters long the word is and asks the user to guess it.
- The user gets to keep guessing until she gets it right.
- If the user guesses a word that is not the right length, the computer says to try again.
- Otherwise, the computer tells which characters the user guessed right. For example, if the word is "computer" and the user guessed "contains", the feed back should be: "The following characters were correct: 1, 2, 7, and 8."
- Once the user gets it right, the computer congratulates her and asks if she wants to play again.
- Due 2/11/97:
Read tips 1 through 21, Enough Rope to Shoot Yourself in the Foot. This book
gives good guidelines about how to be an effective developer, including
some general development tips and some about C++ specifically. We will read and discuss
one or two chapters a week.
Read Ch. 1, 2, and 3, C++ from the Ground Up. These chapters include
some background info about C++, a review of some C features and introduction of a few
new C++ features. Most of this should be review, but pay special attention to the
parts that are new for C++, as well as to any C issues which you may have forgotten.
Programming assignment: the primary objective this week is to get set up in a programming
environment and learn how to use it. Pick a small problem that you would like
to solve via a C++ program. Please make the solution to the problem contain the
following attributes:
- At least one function which takes in arguments and/or returns values
- Input from the user into a variable using the cin> construct
- Output to the user using the cout> construct
- Documentation
Get your program to compile and run on the platform and compiler of your choice. I also recommend you
learn to use the debugger on your platform and learn to step through your program, set break points,
and print variables.
Last updated Feb. 4, 1997