Lab 11 Pre-Lab Assignment      Name ____________________________________________

  1. Find the error in each of the following segments of code. In each case, modify the code so there will be no error. (Assume the Scanner object scan has already been instantiated.)
    1. final int MAX = 10;
      int[] square = new int[MAX];
      for (int i = 0; i <= MAX; i++)
        square[i] = i * i;
      
      
      
      
    2. String[5] name;
      for (int i = 0; i < 5; i++)
      {
        System.out.print ("Enter your name: ");
        name[i] = scan.nextLine();
      }
      
      
      
      
    3. String[] course = new String[5];
      course = "CPSC 120";
      
      
      
      
      

  2. The following code reads in a sequence of salaries and stores them in an array.
    1. Add code to find the average of the salaries (as they are read in - not in a new loop). Declare and initialize any variables you need.
    2. Add a loop to find out how many salaries are above average. Declare and initialize any variables you need.
            int numSalaries;
            Scanner scan = new Scanner (System.in);
            System.out.print("How many salaries will be entered? ");
            numSalaries = scan.nextInt();
      
            // create the array
            double[] salary = new double[numSalaries];
      
      
      
      
      
            // read in the salaries and find the average
            for (int i = 0; i < salary.length; i++)
            {
               System.out.print ("Enter salary " + (i+1) + ": ");
               salary]i] = scan.nextDouble();
      
      
      
      
      
      
             }
      
      
      
      
            // loop to find out how many salaries are above average
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  3. Study the LetterCount program on pages 332 and 333 of the textbook to understand how the relationship between characters and their integer Unicode (ASCII) value is used. You will need to look at the chart on page 788.

    1. What character is (char) ('&' + 4)?    _____________________

    2. What is the value of 'b' - 5?     _______________

    3. What is (char) ('b' - 5)?     _______________

    4. What is the value of 'z' - 'a'? ________________

    5. What would the following loop print?
          char first = '!';
          for (int i = 0; i < 4; i++)
            System.out.println ( (char) (first + i));
      
      
      
      
      
  4. Write a method int findCDIndex(String title) for the CDCollection class on pp. 343-345 ofthe textbook. The method should take a String parameter representing the title of a CD and search the CD array to see if there is a CD with that title. If there is, the method should return the index of the CD in the array. If the title is not found the method should return -1.