CS 1713-001 Introduction to Computer Science
Fall 2003 -- Exam 2 (Friday, 14 November 2001)

Directions: Write your answers in the spaces below. Use extra paper if you need to. Do not spend too much time on any one problem.

  1. Fill in the portions of the following java program by answering the questions below (notice that you do not need to print anything):
    // ScoresTest.java:  Used for question 1 on Exam 2
    public class ScoresTest {
       public static void main (String[] args) {
          final int SIZE = 50;
          // Exam 2 part 1.a.
          
          
          int numScores = 0;
          // Exam 2 part 1.b.
          
          
          
          
          int sumScores = 0;
          // Exam 2 part 1.c.
          
          
          
          
          double averageScore;
          // Exam 2 part 1.d.
          
          
       }
    }
    
    1. Declare an array named scores of ints of size equal to SIZE.
    2. Use a loop and the Keyboard class to read individual scores, putting them one-by-one into the array scores. Keep reading until reaching a negative integer, which is not a score, but shows that there are no more scores to read. When you get done, the variable numScores should give the number of scores read. (You may assume that there are fewer than 50 scores to read.)
    3. Give a loop to add these numbers, leaving the sum in a variable named sumScores.
    4. Calculate the average score, leaving the result in a double variable averageScore.


  1. Write code for the following two public methods. (You do not need code to test or call these methods.)
    1. A method public int linearSearch(int[] a, int value) that will return the position (array index) of an int in the array that matches value, and will return -1 if there is no match.
      public int linearSearch(int[] a, int value)
      // Exam 2 part 2.a.
          
    2. A method public boolean noZeros(int[] a) that will return true in case the array has no entries equal to 0 and will return false if there is one or more zeros in the array.
      public boolean noZeros(int[] a)
      // Exam 2 part 2.b.
      
      
      
          


  1. This is a question about an array of Strings.
    1. Give a code segment that will create an array of Strings named birds holding the following strings: "rhea", "redbird", "robin", "raven", "roadrunner", and "raptor".
      
      
      
    2. Write a segment of code that will check if the element of birds in position 0 is equal to the element in position 3 and will print "equal" or "not equal".
      
      
      
    3. Write a loop that will find the element of birds that comes last in alphabetic order. (The "largest" or "maximum" element. Your loop must be complete and should work regardless of how big the array is.)
      
      
      


  1. For this question, you are play a game 1000 times. (The game is called Chuck-a-Luck.) (For a description of Chuck-a-Luck and calculation of odds, see Chuck-a-Luck.) For the game you roll three dice and see how many times a 5 comes up. (Each time the game is played, there will be either 0, 1, 2, or 3 fives showing.) Each time you play you bet one dollar. In the end, if you have 0 fives, you lose a dollar. Otherwise you win as many dollars as there are fives showing. (You win 1 dollar with 1 five showing, 2 dollars with 2 fives showing, and you win 3 dollars with 3 fives showing.) Specifically, fill in the portions of the following java program by answering the questions below:
    // Dice.java: use Random to roll one die
    import java.util.*;
    public class Dice {
       private static Random ran = new Random();
       public int roll() {
          // Answer to Question 4.a. here
          
          
       }
    }
    // ChuckaLuck.java: play the game Chuck-a-Luck 
    public class ChuckaLuck {
       public static final int RUNS = 1000;
       public static void main (String[] args) {
          // create an instance of the Dice class
          // Answer to Question 4.b. here
          
          
          // create an array of four counters
          // Answer to Question 4.c. here
          
          
          // play Chuck-a-Luck RUNS many times
          for (int i = 0; i < RUNS; i++) {
             // roll the three dice
             // Answer to Question 4.d. here
             
             
             
             // determine how many fives
             // Answer to Question 4.e. here
             
             
             
             // record the results in the array counters
             // Answer to Question 4.f. here
             
             
             
          }
          // Total amount of money won or lost.
          //   0 fives loses a dollar, 1 five wins a dollar
          //   2 fives wins 2 dollars, and 3 fives wins 3 dollars
          int totalMoney; // total money won or lost
          totalMoney = counter[0]*(-1) + counter[1]*1 +
             counter[2]*2 + counter[3]*3;
          System.out.print("Total Money (won/lost): $" + Math.abs(totalMoney));
          if (totalMoney < 0) System.out.println(" lost");
          else System.out.println(" won");
       }
    }
    
    1. Fill in code for the method roll in the public Dice class. It should return an integer from 1 to 6 inclusive, each equally likely. (Recall that the Random class has a method nextInt() so that nextInt(10) returns 0 through 9 inclusive. You may use this if you wish.)
    2. Create an instance of this class with the name dice.
    3. Create an array named counters of four ints to use as counters.
    4. The loop shown will repeat RUNS many times (here RUNS == 1000). Use the method roll() in the Dice class to pretend to roll three dice. (Notice that you are not adding the spots on the three dice, but are just seeing how many times a five comes up.)
    5. After the three rolls, determine how many times a five came up (either 0, 1, 2, or 3 times). (This part could be combined in with the previous part if you wish.)
    6. Use the array counters to record the result of the three rolls above. (Just one of the four counters will get incremented.)

Points for each problem: 1-30, 2-20, 3-20, 4-30.