CSE 131 Module 2: Choice & 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.
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 Urbauer 214 with one of the large wall-mounted screens, a group of 3-4 people is fine.
- If you are seated at a table without a large screen, a group of 2 people is better.
- 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:
https://shell.cec.wustl.edu:8443/cse131/svn/studio2-ZZZZZZ
After pasting:
- 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: Determining the GC content of DNA
DNA
is comprised of two long polymers (often called strands) of nucleotides. Each nucleotide
is commonly one of four possible bases denoted by a single character:
A, T, C, or G.
An organism's DNA, once determined, is often represented as a string
whose characters that represent a portion of one strand of the DNA.
One property of interest to biologist is the GC content of DNA: what fraction
of an organism's DNA are the bases G or C?
In this studio, your team should do the following:
- With your repository open, create a GC class in the
studio2 package.
Be sure to check the box that generates public static void main.
Otherwise you will have to type that by hand.
- Your first task is to generate a random string of DNA, as follows:
- Prompt the user to supply an integer that will be the length of the
string you will generate.
If you don't remember how to do this, look at code from a previous
studio or lab.
- Use iteration, conditionals, and the random-number generator to
generate a random String of DNA.
- Print that string so we can see it.
- Run the CG program several times. Check the output to make sure
it looks reasonable in terms of the length and contents.
You should ensure that your code works properly for edge cases. For
example, what happens if the user specifies 0 as the desired
length?
Also, because you are using the random-number generator, your output should
look different among different runs.
- Now let's compute the CG content of your generated DNA.
- Just after you have printed the DNA, write a loop that computes
how many bases in the DNA string are C or G.
Some notes:
- Print an informative message that shows the fraction or percent of
the generated DNA that is G or C.
- Go back and change the probabilities of generating the four specific
bases, so that you are more likely to generate G or C
than A or T.
- Rerun your program and see if your analysis confirms that the
distribution was suitably affected.
Problem 2: 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 studio2.
- 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.
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)
- If your studio contains a feedback.txt file, respond to
the questions and supply any requested information.
- 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.
- Follow the instructions in the green box below to receive credit for your work.
Last modified 11:27:54 CST 30 November 2012
When you done with this studio, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches