CS12, Program 3
Assigned: April 26th,
2002
Due: May 6th,
2002, 6am
Important:
This program has two
parts, both due on the same day.
The two parts are independent; you can start with either one. Please note that the two parts have to
be turned in separately. There are
two different turnins; you will need to select the correct one. Programs turned into the wrong
directory will not be graded. Please play close attention: when you go to the turnin page, the
default assignment is Jumper, which is part 1 of this program. To turn in part 2, you will need to
click on the down arrow to the right of the assignment name, and select Swamp from the menu that shows up.
Topics
·
Operator
overloading
The program
In this program
you will add some operators to the Frog class you created in program 2 to allow
the frog some more directed motion.
If you did not complete program 2, download the solution files frog.cpp
and frogwalk.cpp.
To do
1. Download the new and improved frog.h,
which has prototypes for all the overloaded operators.
2. Add the following operators to the Frog
class:
·
Operator
<< as a friend function for output.
If kermit is a frog object that is at position 3,
having wandered as far as a distance of 15 from the origin, and landing on the
origin 2 times during its hopping, then the statement cout << kermit will print the following:
The frog is at point 3.
Its farthest distance it traveled
from the origin was 15.
On its way, it jumped on the origin 2 times.
·
Operators +=
and -= to advance the Frog a specified number of steps. For example.
Frog freddie;
freddie += 5;
cout << freddie;
will print
The frog is at point 5.
Its farthest distance it traveled
from the origin was 5.
On its way, it jumped on the origin 0 times.
·
Operators +
and – to create a new Frog that is moved by the specified number of
steps. All other information
(farthest, orig) should be carried over.
This operation does not change the invoking object. For example
Frog freddie;
freddie += 1;
freddie -= 1; // position
is now 0, orig is 1
freddie += 5;
Frog luther = freddie – 8;
cout << “printing freddie’s
info “ << endl;
cout << freddie;
cout << endl << “printing
luther’s info “ << endl;
cout << luther;
will print
printing
freddie’s info
The frog is at point 5.
Its farthest distance it traveled
from the origin was 5.
On its way, it jumped on the origin 1 times.
printing
luther’s info
The frog is at point -3.
Its farthest distance it traveled
from the origin was 5.
On its way, it jumped on the origin 1 times.
3. Write a program in a file called opfrog.cpp
that tests your operators, similar to the examples above. Please hardwire all the values (do not
ask for user input).
What to turn
in
Five files, all inside
one folder with a name of your choice:
* dice.h
* dice.cpp
* frog.h
* frog.cpp
* opfrog.cpp
Please clean up
and remove all other files and folders from this folder before you turn it in.
Extra credit
Implement the
increment and decrement operators (++ and --) to advance the Frog one
step to the right or left.
You will need to change frog.h as well as frog.cpp.
For most credit, implement both the postincrement and
preincrement operators. Add a test
of the operators in opfrog.cpp.
Topics
·
Classes
composed of objects from other classes
·
Thinking
objectively
·
More
operator overloading
·
Pointers and
dynamic memory allocation
The program
In this part of
the program you will create a class for implementing a swamp full of
frogs. You may use the basic frog
class from program 2.
The swamp will
contain an array of Frog objects.
The array is dynamically allocated to a size specified as a parameter to
the constructor. If no size is
specified, the number is set to some default value. A swamp object will keep
track of how many frogs there are in the swamp at any time, as well as some
global information for the swamp, such as the farthest any frog has ever wandered from the origin
(max of all frog farthest variables), and the number of times the origin was
jumped on (sum of all frog orig variables.). Individual Frogs can be accessed and manipulated via the
overloaded [] operator. The swamp
object also has a method that allows all the frogs in the swamp to perform a
random walk.
To do
1. Download swamp.h.
2. Write swamp.cpp
3. Write a main program file that does the
following:
·
Create a
swamp with 5 frogs
·
Walk all the
frogs 10 times using the partyHop() method
·
Output the
position of each frog
·
Output the
global orig and farthest, as well as which frog (array index) was farthest
What to turn
in
Seven
files, all inside one folder with a name of your choice:
* dice.h
* dice.cpp
* frog.h
* frog.cpp
* swamp.h
* swamp.cpp
* swampmain.cpp
Please clean up
and remove all other files and folders from this folder before you turn it in.
Extra credit
Overload the
<< operator to print a picture of the swamp that shows the number of
frogs in each position. For
example, a swamp with 10 frogs, in positions 1, 0 4, 0, -1, 2, 0, 2, 2, -1 will
look like this
2 3 1 3 0 1
*
where the *
denotes the origin.
Please
note:
·
Do
not use any other names for your files.
These names are case sensitive – do not use capital letters
anywhere in the file names.
Failure to name your files as above will cost you 10 points.
·
Please
make sure that at least one of your files includes your name, login and lab
section. Each is worth 4 points in
each
part of this assignment.