## 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 at a table with a 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.
• All but one member of your group should have this web page open so you can follow along and see the instructions as you work.
• All members of the group should update their repositories:
• Open your repository in eclipse
• Right-click (control-click on a mac) on your repository name
• Drag down to Team...
• Choose Update
• Plan to have one computer at which your team does its work. Initially, one of you will be in charge of typing at that computer.
• Throughout the studio, you should trade who is in charge of the keyboard. Before doing so, commit your work to make sure your work is saved.

Note for Fall 2013: Because we missed Thursday of last week, just pick ONE of the following problems for studio (Problem 1 or Problem 2), and work on the other one at your convenience.

Studio today will be the first 45 minutes. In the remaining time, you will demo Lab 1, have recitation on Module 2, and take Quiz 1.

### 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:

1. 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.
2. Your first task is to generate a random string of DNA, as follows:
1. 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.
2. Use iteration, conditionals, and the random-number generator to generate a random String of DNA.
3. Print that string so we can see it.
3. 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.

4. Now let's compute the CG content of your generated DNA.
1. 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:
• Case matters. A G and a g are two different characters. Be sure you are looking for the proper case.
• If you have a String s, then the caracter at position i can be gotten by:
```  char r = s.charAt(i);
```
• Note that the first chracter in s occurs at location 0. If s has n characters, then its last character occurs at location n-1.
• To compare r to a character constant, surround that character constant with single quotes, as in:
```   if (r == 'G') {
System.out.println("I saw a G.");
}
```
2. Print an informative message that shows the fraction or percent of the generated DNA that is G or C.
3. 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.
4. 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 Survivor 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.
1. As a group, develop an approach for computing Pi based on the above materials.
2. 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.

3. Investigate and discuss how well your technique computes Pi.

### Further investigations

If you have time, pick one or both of the following:

1. Investigate the fairness of the math.random() method.
1. What normative criteria should a random number possess?
2. How can you measure the fairness of a random number generator?
3. Implement some tests and discuss your results amongst yourselves and other groups.
2. There are other ways of computing Pi. Try some of these and study their effectiveness in terms of the number of iterations you use.

• 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.