CS100M Spring 2001 Solutions to Prelim T3 April 17 =============================================================================== Q1. [30 pts] graded out of 34 pts: "extra" pts above 30 become bonus pts =============================================================================== Output: bye Q1 =================================================== || main isEven || || +---+ +---------/---------+ +-----/---+ || || n | 3 | | +-/-+ | | +/--+ | || || +---+ | args |/*-+------+ | | n / 4 | | || || | /---+ | | | /+---+ | || || +---+ | / | | +-/-------+ || || g | *-+-+ | +/--+ | | || || +---+ | | w / *-+--+ | | || || | | /+---+ | | | || || isEven | +-/--------+------+-+ || || main | | | || ===========+=============+======+================== | | | Object | | | String ===========+=============+======+==== =============== || | | | || || || || | V | || || || || | +-----/--+ | || || || || |+----+-* n/ll | 0 | || || || || || +---/-/--+ | || || || || ||+---+-* n/ll | 1 | || || || || ||| +---/----+ V || || || || VVV 2 ? || || || || +---+ || || || || | *-+--------------------++--++-->"bye" || || 0 | *-+----------/---------++--++-->"Hi" || || +---+ || || || || | *-+----------/---------++--++-->"lo" || || 1 | *-+--------------------++--++-->"adieu" || || +---+ || || || || 2 || || || ===================================== =============== =============================================================================== Q2. [30 pts] Bonus: 4 pts to correctly preserve case; 1 pt for concise code =============================================================================== + each blank is worth about 2 points; 2nd $char[]$ is worth 1 about pt, 2nd $.toCharArray()$ is worth about 1 pt + first box is worth about 4 pts + second box is worth about 10 points public class Q2 { // new string with each letter replaced by the one 13 letters later public static String rot13( String s ) { // convert $s$ to array $c$ of characters and array $U$ of characters; // convert all letters in $U$ to upper case char[] c = s.toCharArray(); char[] U = s.toUpperCase().toCharArray(); // loop over each character, converting letters; // inv: already processed positions $[0..i-1]$ int i = 0 ; while ( i != U.length ) { if ( 'A' <= U[i] && U[i] <= 'Z' ) { // character is a letter? // convert letter to an integer $0..25$ int letter = U[i] - 'A'; // compute new letter and store back into array |<--BONUS-->| c[i] = (char) (((letter + 13) % 26) + 'A' + c[i] - U[i] ); } i += 1; } return new String(c); } // an example of a call to $rot13$ public static void main(String[] args) { System.out.println( "CS100M --> " + rot13("CS100M") ); } } // end of main method, class Q2 =============================================================================== Q3. [2 free pts + 18 blanks * 1 pt / blank = 20 pts] =============================================================================== int stop = 100 ; // stopping value TokenReader in = new TokenReader(System.in); System.out.println( "Please enter integer sequence terminated by " + stop ); int num = in.readInt(); // current input value // these variables are based on the input read so far, excluding $num$ int mode = num; // last mode int freq = 0; // frequency of $mode$ String mode2 = "none"; // 2nd-to-last mode as a string int prev = num; // previous input value int prevFreq = 0; // frequency of $prev$ // read $stop$-terminated input sequence while maintaining definitions above while ( num != stop ) { // update previous input value and its frequency if ( num == prev ) { prevFreq = 1 + prevFreq; } else { prevFreq = 1; } prev = num; // update last mode and its frequency and 2nd to last mode if ( prevFreq >= freq ) { // new last mode? if ( prevFreq > freq ) { // new frequency? mode2 = "none"; } else { mode2 = "" + mode; } mode = prev; freq = prevFreq; } num = in.readInt(); // read next input } System.out.println( mode2 ) ; // display 2nd-to-last mode =============================================================================== Q4. [5+5=10 pts] =============================================================================== (a) circle defeats; cross out others (b) SSD winners as letters: A, E, F 1 2 3 4 5 6 7 8 9 2 5 topmost sets 1 - -4- [9] - - - [5] - - /^ \ /^ \ {1, 2, 3} 2 [7] - -7- - - - - - - 9 7 7 7 {4, 5, 6} 3 -8- [9] - - - - - - - / V / V 4 - - - - [7] -2- [5] - - 3 <--9-- 1 4 <--9-- 6 123 winner 5 - - - -5- - [7] - - - \ / drop 7s: 6 - - - [9] -6- - - - - 5 5 2/1 (7) 7 -1- - - -2- - - - [5] -4- V V 1 wins 8 - - - - - - -3- - [7] 7 456 winners 9 - - - - - - [7] -5- - /^ \ drop 7s: 7 5 4/5 (7) / V 5/6 (7) 9 <--7-- 8 5 & 6 win