15-110 FALL 2009 (CORTINA)

HOMEWORK 5 - due Friday, October 9, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.

Review the suggested readings from chapters 5 & 7 of your textbook and answer the following questions based on your textbook and the course lecture material.

  1. (2 pts)

    A spinner is implemented with a Java class named Spinner with the following API. Note that the methods are NOT static. (What other classes have you used that had non-static methods?)

    public void spin()
    Spins the spinner to some random value between 1 and 10 on it.

    public int getSpinValue()
    Returns the value currently being pointed to on the spinner.

    Complete the Java method below that takes a Spinner object named spin and spins the spinner repeatedly until the number 5 is spun a total of three times. Your method should print out each number that is spun.

    public static void spinFive(Spinner spinner) {
    
    
    }
    

  2. (2 pts) A "captcha" is a distorted string of characters that is displayed on the screen if an access to a chat room or a request for an email account is received. It is very hard for a computer program to read the text, but humans can read it easily. Captchas are designed to prevent automated programs from gaining access to chat rooms or new email accounts. (By the way, captchas were invented at Carnegie Mellon University!) Here is a sample captcha:

    Write a method named makeCaptcha with no parameters that creates and returns a string of characters for a captcha consisting of some random set of 4-8 uppercase letters, lowercase letters and/or digits. Your method should pick a random number between 4 and 8 (inclusive) and then use a loop to build the string one character at a time starting from an empty string. Once the string is built, return the string.

  3. (1.5 pts) Consider the following array of integers defined in Java:

    int[] data = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 }; 
    

    What is output on the screen for each of the following code fragments? Trace each loop completely for full credit. (Do not just enter these into Eclipse and write down the answers. If you do, you will not get full credit.)

    1. int sum = 0; 
      for (int i = 0; i < data.length; i += 3) 
      	sum += data[i]; 
      System.out.println(sum); 
      

    2. int index = 0; 
      while (index < data.length) { 
      	index += 5; 
      	System.out.println(data[index]); 
      } 
      
    3. int position = data.length - 1; 
      do { 
      	System.out.println(data[position]); 
      	position /= 2; 
      } while (position > 0);  
      

  4. (1 pt) Complete the following Java code fragment to initialize the array data to the same values as shown in the given initialization statement.

    1. int[] data = { 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 }; 
      
      is the same as:
      
      int[] data = new __________________; 
      for (int i = 0; i < data.length; i++) { 
      	data[i] = ___________________; 
      } 
      

    2. int[] data = { 1, 2, 4, 7, 11, 16, 22, 29, 37 };
      
      is the same as:
      
      int[] data = new __________________; 
      data[0] = 1;
      for (int i = 1; i < data.length; i++) { 
      	data[i] = ___________________; 
      } 
      

  5. (2 pts) Let sample be an array of double that stores measurements of a particular pollutant in samples taken from a river once a day. You may assume that the array is completely full with sample readings.

    1. Complete the static method below that takes the array of sample measurements as given in the list of parameters and returns the largest measurement in the array.

      public static double findLargest(double[] sample)  
      { 
       
       
      } 
      

    2. Complete the static method below that takes the array of sample measurements and a threshold value as given in the list of parameters and returns how many measurements fall below the threshold value.

      public static int countBelow(double[] sample, double threshold)  
      { 
       
       
      } 
      

  6. (1.5 pts) Using the methods from the previous problem, write a main method that prompts the user for the number of samples taken from the river, creates an array to hold these samples, prompts the user to enter each sample one at a time, and stores each input sample in the next cell of the array. Once the data is read in, call the methods from the previous problem to output the largest sample and the number of samples below a threshold of 24.0. You may assume in this problem that all input is entered correctly.