$29
This programming assignment gives you experience with defining and using functions, parameters, and classes, the fundamental construct of object-oriented programming.
Readings
A Computer Science Tapestry Sections 2.4 - 2.6
A Computer Science Tapestry Section 3.4
A Computer Science Tapestry Sections 4.5 - 4.6
A Computer Science Tapestry Section 5.4
A Computer Science Tapestry Chapter 6 (excluding Sections 6.3.5, 6.3.7, and 6.5)
A Computer Science Tapestry Chapter 7
A Computer Science Tapestry Appendix D
Starter Code
Please download the starter code from above.
Cat and Mouse
The scene is an urban park; a cat watches a mouse run around the base of a statue of the actors from Monty Python. Over the course of a second, the somewhat witless mouse moves one meter counterclockwise around the statue’s base, which is circular and two meters in diameter. Every second, the cat pursues the mouse as follows: If the cat can see the mouse, the cat moves one meter toward the statue. If the cat can’t see the mouse, the cat circles 1.25 meters counterclockwise around the statue.
The cat plans eventually to get close enough to the mouse to make it a juicy lunch. The mouse by accident, however, may manage to keep completely out of sight of the cat, since both the cat and the mouse are moving counter-clockwise.
Should the chase go on more than 30 seconds, however, the cat will get tired and wander off.
Your Task
Complete the program found in cat+mouse.cpp to produce a simulation of this situation and determine if the cat catches the mouse. Don't change any of the code already provided in that file. The program uses a Position class declared in positions.h; you are also to provide a file positions.cppthat correctly implements the operations of this class.
The program first calls the GetPositions function to ask the user for the initial positions of the mouse (angle in degrees) and the cat (both radius and angle in degrees). You may assume that the user provides legal position values.
The program then calls the RunChase function to simulate the chase using the rules listed above. (The cat moves first.) Your code should use member functions of the Position class where appropriate. It should print the position of the cat and mouse after each move, followed by the results of the chase (either the mouse becomes lunch, or the cat gives up after 30 minutes).
Useful Information for Writing positions.cpp
The cos function appears in the C++ math function library. Note that the argument to this function must be specified in radians, not degrees. A source file that uses any of the math functions must include the line
#include <cmath
or, in an old C++ environment,
#include <math.h
It's legal and appropriate to assign one object to another, for instance by saying
oldCatPosition = newCatPosition;
Note also that an object's member function (e.g. IsBetween) can access the private data of any other object of the same class.
Trigonometric Information
The cat sees the mouse if
(cat
radius) * cos (cat angle - mouse angle) = 1.0 (angles in radian)
When the cat circles distance d around the statue, its radius does not change, and the change in its angle can be calculated from the relationship
d
= angle * radius (angles in radian)
The cat catches the mouse when it (the cat) moves past the mouse while at the base of the statue, i.e. when the cat radius is 1.0 and the mouse angle lies between the old cat angle and the new cat angle. An angle B is between angles A and C in the following circumstances:
cos
(B - A) cos (C - A), and cos
(C - B) cos (C - A) (angles in radian)
The difference C - A is assumed to be less than 90°, or π/2 radians.
Note that the cat cannot move inside the statue’s base; hence if the cat is, say, at radius 1.7 and sees the mouse, it can move in only 0.7 meters, up to the base of the statue.
Remember that angles a, a + 2π, a + 4π, ... are all equal.
Testing
Test each function and method in your program with enough different inputs to show that it works. When testing a method, print out the input, the expected result, and the actual result.
It would probably be helpful for debugging purposes to have a debug flag available that, when true, dumped the position and heading of all characters involved (cat and mouse) at each time step.
You should test the methods of each Class in isolation, using appropriate test data, to see if they work independently before calling them from a driver function. Tutors may ask to see the results of these tests. Your test data should include the following values:
(Note: Cat radius is calculated from the center of Statue)
Cat Radius (m)
Cat Angle (degree)
Mouse Angle (degree)
Test Case Features
1.0
35.0°
396.0°
angle 360; mouse caught immediately
3.2
0.0°
-57.0°
negative angle;
cat radius has fractional part;
mouse caught at time 17
8.1
0.0°
45.0°
8.1
150.0°
240.0°
4.0
0.0°
-57.0°
...plus the following cases:
cat catches mouse at time 30;
cat would catch mouse at time 31 if the simulation ran that long;
whatever other values are necessary to ensure that all statements in your program have been executed at least once.
Be sure to check that the output makes sense for these tests, since the output is what most tutors will check first. Students are sometimes embarrassed to have a tutor point out that their programs have the cat moving deeper and deeper inside the statue, or moving away from the mouse instead of approaching it.
Checklist
Correctly working code.
No changes made to functions already provided in cat+mouse.cpp, or to the public interface in positions.h.
Sufficient testing, with output sufficient to verify test correctness:
tests on specified values for cat and mouse positions;
evidence of independent tests of all functions in positions.cpp;
tests sufficient to exercise all statements in the program.
Listings of program text, tests, and test results.
Adherence to CS 9F style standards:
appropriate use of indenting and white space;
avoidance of "forbidden C++";
variable and function names that reflect their use;
informative comments at the head of each function;
no routine more than twenty-four lines of code.
Clean case analysis and simple loop structuring.
Suitable input prompts and informative output, including at least the positions of the cat and mouse after each move, and the final result of the chase.