CSC111 Homework 8
This homework assignment is due on April Fool's Day, 4/1/10, at midnight. You can work on Problem #1 in pairs. Problem #2 is optional and extra-credit, and if you work on it, you must work on it on your own. |
Problem #1
Statement
Write a program called hw8a.py that moves two balls on the graphics screen, in fashion similar to what you did in Lab 8, with the following twists:
- the two balls should bounce off the walls (border of the graphics window)
- the two balls should bounce off each other.
- there should be three black boxes in the graphics window, and if any one of the two balls happens to move completely inside any one of the boxes, it should stop there.
- the program should stop and display click to End" when the simulation has gone through 300 steps, or as soon as the two balls have been immobilized in boxes.
- there should be a fourth box, this one white, which should act as an obstacle. Any time a ball hits the white box, it should bounce off the walls of the box, in a same way it bounces off the walls.
Requirements
- Use boolean functions
- Use a list to hold the black boxes.
- You are free to select the size of the white and black boxes, but make them big enough to allow balls to hit them regularly, but not too big so as to make the balls "fall" in a black box before having time to hit a few walls or the white box.
- Include your account name(s) or your first name(s) in the title of the graphics window to make it easier to identify printed screen captures of your running program:
win = GraphWin( W, H, "111c-xx 111c-yy" )
Realism
- It is not easy nor possible with our current knowledge of Python to make the bouncing of the balls off each other realistic. When a ball hits a wall, the rule is very nice and clear: we change the sign of either dx or dy, depending on which is perpendicular to the wall. When the balls hit each other, both dx and dy would normally change. The real equation for how dx and dy change is too complicated for us. Invent your own laws of physics and implement them in python!
Submission
- Make sure the header of your program contains your names and account numbers, if working in pairs, or just your name and account number if working individually.
- Submit your program as follows:
submit hw8 hw8a.py
Additional Information
- You may find the following function useful for computing the distance between two graphics points:
from math import * # this should be at the beginning of the program def distance( P1, P2 ): """Computes the distance between Point P1 and Point P2. The returned value is a float""" return sqrt( pow( P1.getX() - P2.getX(), 2 ) + pow( P1.getY() - P2.getY() ) )
Problem #2: Optional and Extra Credits
This part is extra credit and optional. You cannot work on it in pairs, but you must work on it on your own. If you submit a program that is
- well documented, and that
- runs flawlessly when tested,
you will get an additional 1/3 point on your midterm grade. So, if you had gotten B+, this extra-credit part will bring you to A- total.
- Your assignment is to write a program that will read a long string of characters which represents the results of a computer experiment.
- In this experiment, many different computers (88) are asked to compute a number and return this result to a main computer. The main computer outputs all the different answers in a long listing. It is this listing that your program has to read, analyze, and process.
- To see what the list looks like, use getcopy to get two programs into your account:
getcopy hw8b.py getcopy montecarlo.pyc
- montecarlo.pyc is a module that is used by hw8b.py.
- Run the hw8b.py program and observe its output:
python hw8b.py
------------------------------
N= 10000000
pi=3.16159265359
Job 1 stopped: Execution time:3.073 seconds
------------------------------
N= 10000000
pi=3.15159265359
Job 2 stopped: Execution time:3.023 seconds
------------------------------
N= 10000000
pi=3.15159265359
Job 3 stopped: Execution time:3.029 seconds
------------------------------
N= 10000000
pi=3.14159265359
Job 4 stopped: Execution time:3.015 seconds
...
...
...
------------------------------
N= 10000000
pi=3.14159265359
Job 88 stopped: Execution time:3.021 seconds
- 88 different results are computed and reported in 88 groups of 4 lines. Each group starts with a line of dashes. The next line in a group, N= 10000000, is not important for us. It just indicates that 10,000,000 iterations were performed to estimate each value of Pi. The third line of a group, pi=3.14159265359, shows the result of computing Pi on one of the computers. This result is an estimation and is not exactly Pi. The last line of each group, Job XX stopped: Execution time:n.nnn seconds indicates that the program ran on Computer #XX, and took n.nnn seconds to run.
- Your assignment is to modify hw8b.py so that it will process this collection of lines (stored in the variable output in the program hw8b.py), extract all the values of pi listed, average them out (i.e. sum them all, then divide by the number of values), and print the average.
- Your program should also keep track of the smallest and largest execution times reported.
- The output of your program should be something like this:
88 valid values read average value of pi: 3.14159257298131 minimum execution time recorded: 3.000 seconds maximum execution time recorded: 3.311 seconds
Complications
- The complications come from the fact that every so often one of the computers will crash, or will not return data. If you run the hw8b.py program several times, you will observe these two cases from time to time.
- In the first case, when computers crash, we will get results that will be messed up, as shown below:
------------------------------ N= 10000000 pi= Job 82 crashed NA seconds
- Note that the number of seconds is not available any more, and that the string NA is reported instead.
- In the second case, when computers do not return results, we simply get fewer than 88 results.
Requirements
- Make your program work regardless of the errors that might happen. Your program should not crash when the list contains corrupted results as shown above, or when the number of results is less than 88.
- Use the try/catch construct.
Submission
- Submit your program as follows:
submit hw8 hw8b.py
- (You do not need to submit montecarlo.pyc)