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 (Sec 4.10). There will be special emphasis on arrays (Chapter 5) 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. The Fibonacci numbers are the numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, etc. (each number is obtained by adding the previous two together). Write a static method
     public static int firstFibPast(int n)
    that returns the first Fibonacci number that is bigger than or equal to the parameter n. For example, if n has the value 10, the method would return 13. If n has the value 0, the method would return 0. The method should not use recursion.

  2. 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.

  3. 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).
  4. Write a static method
           public static boolean prime(int n)
    
    that returns true if the parameter n is a prime number, and false otherwise. (A number n is prime if the only positive integer less than n that divides n exactly (with zero remainder) is 1.)

  5. Write a static method
           public static int sumDivisors(int n)
    
    that returns the sum of the positive integers less than the parameter n that divide into n exactly (with zero remainder). For example, if n has the value 8, the method returns the value 7 (1 + 2 + 4) and if if n has the value 9, the method returns the value 4 (1 + 3).

  6. Write a static method
           public static String concat(String[ ] words)
    
    that returns a single string consisting of all the strings in the array words concatenated together. For example, if the elements of the array words are "Hello", "this", "is", "a" and "test", the method should return the string "Hellothisisatest". 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).

  7. 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).

  8. 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).

  9. 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.)
  10. 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.

  11. This question also refers to class Bingo. Write a method (an instance method)
          public int edgeCount(char c)
    
    that counts how many elements on the edge of board (in the first or last row or column) are the character given by the parameter c.