CSC 207 Grinnell College Fall, 2014
Algorithms and Object-Oriented Design

Control Structures and Arrays in Java


This lab provides practice with conditionals, loops, and arrays in Java, by applying these constructs to solve several problems.

Background Reading

Before working on this lab, be sure you have read the reading on control structures in Java.

while Loops

The reading shows the following code to compute and print factorials.

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

The reading then translates this code into the following loops, ignoring the printing.

   i = 1;
   factorial = 1;
   while (i <= n) {
     factorial = i*factorial;
     i = i + 1;
   i = 1;
   factorial = 1;
     { factorial = i*factorial;
       i = i + 1;}
   while (i <= n);
  1. Incorporate the above code segments into a new program that is established within Eclipse. Add println statements at the end of the while and do..while loops. 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 System.out.println (factorial); at the appropriate place within the loop, so that the output of these loops exactly matches the output of the initial for loop.

Application: Approximating Pi

Program runs a simulation involving picking points within a box to approximate Pi.

  1. Import this program into a new mathComputations package within Eclipse, compile the program and run it several times. Record your results.

  2. Remove the import statement at the start of the program, and try to compile and run the program. Describe what happens. (Restore the code to its original form after you complete this part.)

  3. Remove the phrase throws Exception at the start of the main method, and try to compile and run the program. Describe what happens. (Restore the code to its original form after you complete this part.)

  4. Change N to 5; and print out the values of x, y, and count each time through the loop. Trace the code to confirm that the program is correctly computing the points that lie within the unit circle.

Be sure you understand how this program works before proceeding with this lab.

Application: The Sieve of Eratosthenes

Program implements the Sieve of Eratosthenes as a mechanism for determining all prime numbers up to a specified number M.

  1. Import Class into a mathComputations package within Eclipse, 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, 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, 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. As in the program, you should use Java's random method, in the Math class, to obtain a range of results.

Specifically, Math.random() returns statistically random double numbers that are greater than or equal to zero and less than 1.0. With this in mind:

The above computation illustrates how random can be used to generate statistically random results for integers or other ranges of numbers.

  1. Rolling 2's: Write a program that creates an array that stores the results of rolling a die 100 times. (The program then should count the number of times a 2 is thrown, and the fraction of the rolls that are 2's. Use a variable N for the number of times the die is rolled, so that it is easy to change N before the program is run.)

  2. 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
  3. 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

created 18 April 2000 for iteration and arrays
revised 24 March 2005 (while and do...while loops and arrays)
revised 22 January 2012 for conditionals, loops, and arrays
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at

Copyright © 2011-2014 Henry M. Walker.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .