CSC 153 Grinnell College Spring, 2005
 
Computer Science Fundamentals
Laboratory Exercise
 

Iteration and Arrays in Java

Goals

This lab provides practices with two Java constructs, while loops and arrays, and applies them to solve several problems.

while Loops

  1. Place the following two loops into a Java program:

    
    i = 1;
    factorial = 1;
    while (i <= n) {
      factorial = i*factorial;
      i = i + 1;
    }
    
    i = 1;
    factorial = 1;
    do
      { factorial = i*factorial;
        i = i + 1;}
    while (i <= n);
    
    

    Add a read statement to determine the value of n and a print statements to determine the results after each code segment. Compile and run the program for several values of n. Check that the code runs correctly for positive values of n. What happens if 0 or a negative number is entered? Explain briefly.

  2. These while and do--while loops do not contain the printing of the factorial variable as the loop progresses. Add the printing statement out.println (factorial); at the appropriate place within the loop, so that the output of these loops exactly matches the output of the following for loop:

    
    for (i = 1, factorial = 1; i <= n; factorial = factorial * i, i = i + 1)
       out.println (factorial);   
    
    

Arrays

Program ~walker/java/examples/arrays/ArrayExample.java reads 10 numbers, prints them out in reverse order, and finds their maximum and minimum.

  1. What, if anything, happens if final is omitted in the declaration of Number? Why do you think this happens?

  2. With final still omitted, what happens if Number is changed to the value 15 later in the program? Does the program still compile? Does it run correctly?

  3. Retain the line changing Number to 15, but re-insert final when Number is declared and initialized to 10. Again, does the program compile? Does it run correctly?

  4. Suppose int variables i and j were both declared in the program. Suppose also the first loop using index i were retained, but the second loop were changed, so all variables i in the second loop only were changed to j. Does the program still work? Explain your answer.

Application: The Sieve of Eratosthenes

Program ~walker/java/examples/arrays/PrimeSieve.java implements the Sieve of Eratosthenes as a mechanism for determining all prime numbers up to a specified number M.

  1. Copy primeSieve.java to your account, compile, and run it.

  2. Why do you think array crossedOut is a boolean array in this program?

  3. Array indices in Java always start at 0, and the array initialization new boolean [N] creates an array with N elements -- numbered 0 through N-1. In primeSieve.java, what happens if crossedOut is declared as size M rather than M+1? Explain your answer briefly.

  4. Some extra code is included at the end of primeSieve.java, so the primes will be printed out in reasonably nice columns. Explain how this formatting is implemented.

Some Simulations

The following problems ask you to write some simple simulations. These simulations typically should use Java's random method, in the Math class, to obtain a range of results.

  1. Rolling 2's: Write a program that reads an integer N, rolls a die N times, counts the number of times a 2 is thrown, and the fraction of the rolls that are 2's.

  2. Rolling a 2: Write a (static) method getATwo that returns the number of rolls of a die until a 2 is thrown.

    1. Write a program that calls getATwo 100 times, and computes the minimum, maximum, and average number of rolls needed to roll a 2.

      Modify the program in part a to get a program that calls getATwo 1000 times and records, in an array of large enough size (perhaps 100), the number of times a 2 is obtained in 1 roll, in 2 rolls, in 3 rolls, etc. The program should then print the frequency of the number of rolls in a table, such as the following:

      
       Number    Frequency
      of Rolls     Count
         2          138
         3          116
         4  ...
      
      

      If these data were received, one would conclude that a 2 was rolled on the second roll for 138 of the 1000 experiments, while a 2 was rolled (for the first time) on the third roll in 116 experiments. Et cetera.

  3. Rolling Two in a Row: Write a program that rolls a die 1000 times and counts the number of times the same result occurs twice in a row. In your counting, you should consider a sequence 3 3 3 as containing two sets of identical rolls -- the middle 3 matches the first, and the third 3 matches the second. Thus, the following sequence of rolls would be counted as having the same result occur twice in a row for 4 times.

    
    1 5 3 2 4 4 6 5 5 5 3 6 2 1 1 2
    
    
  4. Rolling a Pair of Dice: Write a method pairOfDice that rolls a pair of dice and returns their sum. Use this method in a program that computes the number of times a 2, 3, 4, ..., 11, 12 are rolled in 1000 experiments. Show your results in a table.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp05/labs/lab-iteration-arrays.shtml

created April 18, 2000
last revised April 18, 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.