CSE 131 Module 3: Iteration
Studio
Review studio procedures before
starting.
Feel free to participate in a different group than last time. This is totally
up to you, but try to find a group that makes it easy for you to participate.
By the end of this session, a TA will have completed a
Studio Observation form for your group.
You will have to attach your team's observations so keep track and type
things in as you go along for printing by the end of studio.
Be careful how you use the web. You are required to develop solutions
as a group by thinking not by finding solutions that have been thought
out by others. You must be able to explain anything that you have done.
Warmup
-
First, form a group:
- If you are in Cupples II 300, a group of 3-4 people is fine.
- If you are in a Sever or Lopata lab, form a group of 2 or 3 people.
- One member of the group should get a studio checkin/checkout sheet
from a TA.
The sheet will bear a sticker with the name of the repo workspace you
will use for this studio session.
- Gather around one computer:
- If you are upstairs in Cu II 300 attic,
then you can form a group at each table and use the wall-mounted displays.
- Otherwise you'll have to crowd around one computer.
-
One member of your group should log in, launch eclipse, and
open the SVN Repository Exploring perspective:
Window...Open Perspective...Other...SVN Repository Exploring
- Click on the New Repository Location icon (looks like a
gold battery with a green plus sign).
- Copy the following URL using your mouse:
svn+ssh://XXXXX@grid.cec.wustl.edu/project/cec/class/cse131/fall09/studios/studio3-ZZZZZZ
After pasting:
- Change XXXXX to your username that you use to log into cec computers. For example, jdl2.
- Be sure the directory after fall09 is studios and
not students.
- Change ZZZZZZ to the word on your sticker.
For example, animal.
- If prompted, type in your name and password.
- If the repository is validated, keep going; otherwise get help.
- Right-click on the project name and Check Out the workspace.
- Return to the Java perspective.
- You will take turns using the keyboard but your
work will be done in one workspace.
Problem 1: Computing Pi by throwing darts
Computer scientists often use simulation as a means of
modeling, understanding, and predicting real-world phenomena.
Your group is auditioning
for Lost
by proving your group's ability to compute
Pi using
only the materials at hand, as follows:
- A unit-square dart board (say, 1 meter by 1 meter). Unit-square
dart boards are astoundingly resilient in plane crashes,
and yours is nicely intact.
- Some darts, suitable for throwing at the dart board.
- A string and a stylus, standard safety-kit issue, suitable for
inscribing a unit circle in your unit-square dartboard.
- A dart-throwing expert. However, since the plane crash, the
expert is left with the (uncanny) ability to throw darts that always land
somewhere, uniformly and randomly, within the unit-square dart board.
While the thrower never misses the unit square, the darts
land sometimes within the inscribed circle, sometimes not.
-
As a group, develop an approach for computing
Pi based on the above
materials.
- Implement your approach using iteration.
You can start with the following
Pi.java file that you can paste into a new
Java class in one of your lab projects.
This may be the first new Class you have developed, but eclipse makes
it easy:
- Right-click on the package name in which you want to define the
new class. In this case, use studio3.
- Click New...
- Choose Class
- Pick the name Pi for this class, since the code you
will paste is for class Pi.
Java style dictates that its classes should begin with a capital letter!
- When the editor opens for your new class, copy and paste
the code from
Pi.java into the class.
You will need to simulate a random dart thrower. The function
math.random() will help,
as it returns a nonnegative double
less than 1.0.
You may also find the Math.sqrt() function
to be helpful.
- Investigate and discuss how well your
technique computes Pi.
Problem 2: loop invariants
Fill in the following iterative method for computing the
sum of the square of the first n positive integers:
// do this on paper, no need to code this
int sumSquared(int n) {
// Initialization
int ans = ??,
i = ??;
while ( i != n ) {
// place A
i = i + 1;
ans = ans + ??
// place B
}
return ans;
}
- What is the termination condition for the loop?
- What loop invariant will help you prove that the loop
computes
ans == 1*1 + 2*2 + 3*3 + ... + n*n ?
- Complete the initialization so your loop invariant holds before
the loop executes.
- Write a proof that the loop body preserves the loop invariant.
You want to show the following:
if prior to the loop body (place A), the following holds:
ans == 1*1 + 2*2 + ... + i*i
then at place B the following holds:
ans' == 1*1 + 2*2 + ... + i'*i'
where ans' and i' are the new values computed by the loop body.
Further investigations
If you have time,
pick one or both of the following:
- Investigate the fairness of the
math.random() method.
- What normative criteria should a random number possess?
- How can you measure the fairness of a random number generator?
- Implement some tests and discuss your results amongst yourselves and
other groups.
- There are other ways of
computing Pi.
Try some of these and study their effectiveness in terms of the number of
iterations you use.
Submitting your work (read carefully)
- Complete the feedback.txt file and be sure to list
your group members' names and ID last-3-digits.
- Answer the other questions in the feedback.txt file.
- You must commit all of your work to your repository. It's best to do this
from the top-most level of your repository, which bears your name and student ID.
- You must have the TA clear you by signing your studio group sheet.
Be sure your names and IDs are written
clearly on the TA's demo sheet.
Last modified 22:14:02 CST 11 November 2009
by Ron K. Cytron