CPSC 170 PreLab 1: Arrays

  1. Tell whether each of the following will produce a compile-time error, a runtime error, or no error. If there is an error, explain why; if no error, tell what the code will print.
    a)   String[] names;
         System.out.println(names[0]);
    
    
    b)   String[] names;
         names = new String[10];
         System.out.println(names[5]);
    
    
    c)   String[] names;
         names = new String[10];
         System.out.println(names[5].length());
    
    
    d)   String[] names;
         names = new String[10];
         for (int i=0; i<10; i++)
            names[i] = "joe" + i;
         System.out.println(names[5]);
    
    
    e)   String[] names;
         names = new String[10];
         for (int i=0; i<10; i++)
            names[i] = "joe" + i;
         System.out.println(names[5].length());
    
    
    

  2. Write a method boolean findCD(String title) for the CDCollection class on p. 337-338 of the text. This method should take a string and return true if a CD with that title is in the collection, false otherwise. Important: Assume that you have already added a public String getTitle() method to the CD class (p. 340) that returns the title for a CD.



















  3. Consider the array below:

    5 8 9 11 12 14 15 20 22 25

    Use binary search to search for values

    1. 15
    2. 16
    in this array. For each search, show the values of lo, hi, and mid at each step.