CS100 J Spring 2004
Review/Practice Questions for Prelim 3

Note:
Be sure to review the all the homework questions and their solutions as part of your preparation. Review the two previous prelims and the previous review questions.

Question 1
Part A:   Write a Java static method rowSumsOf2dArray to calculate the row sums of a 2-d numeric array. Method rowSumsOf2dArray has a parameter a referencing the 2-d array to be summed and returns the (reference to a) 1-d array storing the row sums.
Part B:   Write a Java static method colSumsOf2dArray to calculate the column sums of a 2-d numeric array. Method colSumsOf2dArray has a parameter a referencing the 2-d array to be summed and returns the (reference to a) 1-d array storing the column sums. Assume that array a is rectangular.

Question 2
Consider the skeleton code in class ReviewQ3 given below. Complete the methods in order to perform two tasks:
A:   Re-order the rows of the 2-d array so that the row sums are in ascending order. You may assume that all rows exist.
B:   Re-order the columns so that the column sums are in ascending order. You may assume that all rows exist and that the 2-d array is rectangular.
Use the selection sort algorithm for sorting. For task A, the code you write should not assume that the 2-d array is rectangular.
public class ReviewQ3 {

	public static void main(String[] args) {
	    double[][] x = new double[][]{{45,5,6,4,1,9,4,4},
                                          {2,6,3,4,5,5,6,3},
					  {2,3,4,5,6,7,8,9}};    	
            printArray(x);
            selectSortByRows(x);
            printArray(x);
            selectSortByCols(x);
            printArray(x);
	}

        /** Print 2-d array x */
        public static void printArray(double[][] x) {
        }

        /** = sum of 1-d array */
	public static double rowSum(double[] array) {
	}

        /** = sum of column j in 2-d array */
	public static double colSum(double array[][], int j) {
	}

        /** Select sort 2-d array such that row sums are in ascending 
            order */
	public static void selectSortByRows(double[][] array){
	} //method selectSortByRows

        /** Select sort 2-d array such that column sums are in ascending 
            order */
	public static void selectSortByCols(double[][] array){
	} //method selectSortByCols

} //class ReviewQ3

Question 3
Write a Java program fragment to find the frequencies of all vowels in a given String (referenced by variable) str. Do not convert the String into an array of chars. Store the frequencies in an array of length 5. For example, the string
  I loVE prOgramming
has the following vowel frequencies:
  
  a  e  i  o  u
  1  1  2  2  0

Question 4
Write a method to encrypt a sentence that is stored as an array of chars. The sentence contains upper case letters and blanks only. Encrypt the sentence using a given key. A key is an array of chars of length 27 that contains the 26 letters of the alphabet and the blank character in some random order. To encrypt the ith letter in the alphabet, replace that letter with the ith character in the key. For example, to encrypt the character 'Z', use the 26th character in the key. The blank character, shown as an underscore '_' below, is encrypted using the 27th character in the key. For example, if the key is the array QWE_RTYUIOPASDFGHJKLZXCVBNM, then the sentence A_DAB will be encrypted as QM_QW.
    /** = Return a char array containing the encrypted sentence. 
        Encrypt sentence s using array key */
    public static char[] encrypt(char[] s, char[] key)
Note: Do not create a String or char array containing the alphabet (the 26 letters in order) in your code. Do not use any pre-defined methods.

Question 5
Simulate a coin game with two players, A and B. In one game, each player tosses two coins. Both coins are weighted such that heads appears twice as often as tails. Player A wins if she gets the same face for both coins and player B wins if he gets different faces of the coins. There may be no winner in a game. There can be at most one winner: if A gets the same face for both coins and B gets different faces, then A wins. Count the number of times that B wins in 1000 games.
Hint: If heads appears twice as often as tails, then on average heads appears in two out of three tosses.
You may use procedural programming (write all the code in a main method, perhaps with some static function(s) for clarity/conciseness), or OO programming (define classes with fields and methods).