CSE 131 Module 2: Choice & Iteration


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.


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:

    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.

    Submitting your work (read carefully)

    Last modified 08:59:37 CST 09 January 2015
    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
    • If you request propagation, it does not happen immediately, but should be posted in the next day or so

    Last name WUSTL Key Propagate?
    (or your numeric ID) Do not propagate
    e.g. Smith j.smith
    1 Copy from 1 to all others
    2 Copy from 2 to all others
    3 Copy from 3 to all others
    4 Copy from 4 to all others

    TA: Password: