CS 1713-001 Introduction to Computer Science
Spring 2001 -- Exam 2
Friday, 13 April 2001

Directions: Use your own paper for answers. When you are done you may keep this exam sheet and should pick up an answer sheet.

  1. Fill in the portions of the following java program by answering the questions below:
    // NumTest.java:  Used for question 1 on Exam 2
    public class NumTest {
       public static void main (String[] args) {
          // Exam 2 part 1.a. 
          // Exam 2 part 1.b. 
          // Exam 2 part 1.c.
          // Exam 2 part 1.d.  
       }
    }
    
    1. Give a code segment to create an array named nums of ints holding the values 23, 40, 15, 37, 19, and 63.
    2. Give a code segment to add these numbers, leaving the sum in a variable named numsSum.
    3. Give a code segment to find the smallest number in the array nums, leaving this smallest number in a variable named smallest.
    4. Assuming that there is a class Sorts with a static method insertionSort, give a code segment to sort nums into order.


  2. Write code for the following two public methods. (You do not need code to test 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.
    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.


  3. This is a question about an array of Strings.
    1. Give a code segment that will create an array of Strings named animals holding the following strings: lion, tiger, bear, goat, and horse.
    2. Write a segment of code that will check if the element of animals in position 0 is equal to the element in position 3.
    3. Write a segment of code that will find the element of animals that comes last in alphabetic order. (Your segment must be complete and should not call a sort method which you do not define. Recall that str1.compareTo(str2) returns a number < 0 in case str1 comes before str2 in alphabetic order, and returns a number > 0 in case str1 comes after str2 in alphabetic order.)

     


  4. For this question, you are play a game 1000 times. (The game is called Chuck-a-Luck.) For the game you roll three dice and see how many times a 4 comes up. (Each time the game is played, there will be either 0, 1, 2, or 3 fours showing.) Each time you play you bet one dollar. In the end, if you have 0 fours, you lose a dollar. Otherwise you win as many dollars as there are fours showing. (You win 1 dollar with 1 four showing, 2 dollars with 2 fours showing, and you win 3 dollars with 3 fours showing.)

    Specifically, fill in the portions of the following java program by answering the questions below:

    // 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.a. here
          // create an array of four counters
          // Answer to Question 4.b. here
          // play Chuck-a-Luck RUNS many times
          for (int i = 0; i < RUNS; i++) {
             // roll the three dice
             // Answer to Question 4.c. here
             // determine how many fours
             // Answer to Question 4.d. here
             // record the results in the array counters
             // Answer to Question 4.e. here
          }
          // Total amount of money won or lost.
          //   0 fours loses a dollar, 1 four wins a dollar
          //   2 fours wins 2 dollars, and 3 fours 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. Assume there is a public Dice class. Create an instance of this class with the name dice.
    2. Create an array named counters of four ints to use as counters.
    3. The loop shown will repeat RUNS many times (here RUNS == 1000). Suppose the Dice class has a non-static method roll() that will return an integer from 1 to 6 inclusive. (You don't have to write the code for Dice or for roll().) Show 3 calls to roll() that will 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 four comes up.)
    4. After the three rolls, determine how many times a four came up (either 0, 1, 2, or 3). (This part could be combined in with the previous part.)
    5. 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-25, 2-20, 3-25, 4-30.