Final Exam Information for V22.0101

Fall 2002

The exam takes place:

Topics covered in regular sections: Chapters 1 through 8 of the textbook. There will be a question on recursion (Chapter 19). There will be special emphasis on arrays (Chapter 6) including two-dimensional arrays. The exam is closed book, without notes.

General instructions: write all your answers on the exam. All questions will ask you to write methods, either static (class) methods, or instance (non-static) methods that access instance variables of a class.

None of the methods you write should read from the console or write to the output screen (in particular, there should be no calls to System.out in your solutions). None of the questions will involve applets. Read the questions carefully.

For questions involving strings, you may find the following methods useful:


You can expect about 6 questions on the exam. The following questions would be typical. There will be at least one additional question for the honors section (topics for the honors section are discussed in class).


  1. Write a static method
    public static int longestBlank(String str)
    that returns the maximum number of consecutive blanks in the String parameter str. For example, if the string is "abc def geh ijk", the method would return 2. If there are no blanks in the string, it returns 0.

  2. Write a static method
    public static int toInteger(String str)
    that converts the string given by the parameter str to a corresponding integer. For example, if str is the string "1234", the method returns the integer 1234. If the string contains any non-digit characters, the method returns 0. You may not use the parseInt method of the Integer class, but you may use the length, charAt and subString methods of the String class (see above).

  3. Write a static method
           public static String shortest(String[ ] words)
    
    that returns the string in the array that has the shortest length. If there are more than one string that has the shortest length, the method should return the first one encountered. If the array contains: "Hello", "this", "is", "a" and "test", the method should return the string "a". Remember that the elements of the array are words[0] through words[k], where k is one less than words.length, the length of the array (no parentheses, since this is not a method call).

  4. Write a static method
           public static char pickOne(char c1, char c2)
    
    that returns either the character given by the parameter c1 or the character given by the parameter c2, chosen at random. Your method should either use the library method Math.random, which returns a double between 0 and 1, or, if you prefer, the Random class (see Section 6.5 [p. 262] of the text).

  5. Consider the following class and its instance variables:
    public class Bingo
    {   //   methods go here.....
        int size;     // these are instance variables
        char [ ][ ] board;
    }
    
    Write a constructor for the class Bingo:
          public Bingo(int n, char c1, char c2)
    
    The constructor should (a) initialize the instance variable size to the parameter n and (b) initialize the instance variable board to a two dimensional array of characters with size rows and size columns, with each character initialized to either the character given by the parameter c1 or the character given by the parameter c2, each one chosen randomly by calling your method written to answer Question 9 (which you can assume is available as part of the Bingo class).

  6. This question also refers to class Bingo. Write a method (an instance method)
          public boolean rowBingo(char c)
    
    that returns true if board has at least one row where every element in the row is the character given by the parameter c. Otherwise it returns false. (The first index of the array is the row index.)
  7. This question also refers to class Bingo. Write a method (an instance method)
          public boolean allSame()
    
    that returns true if every element in the two dimensional array board is the same character (this could be any character). Otherwise it returns false.