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