Fall 2004 Cs307 Midterm 1 Suggested Solution and Key 1. The answer as below or -2. Note do not count off for differences in spacing, capitilization, or leading zeros. A. 12 B. 2 4 4 3 C. 1 D. 0.0 0.5 1.0 1.5 E. total54 F. vessel weight: 2000 G. Syntax error H. vessel weight: 2000 vessel weight: 3000 I. vessel weight: 1500 vessel weight: 5000 vessel weight: 1500 J. vessel weight: 2000, number of masts: 2 K. vessel weight: 1000, number of masts: 1 L. vessel weight: 3500, number of masts: 2 M. false ( 0 = -2, f = -1) N. Yes, a null pointer exception could occur if list = null or an array index out of bounds exception could occur if x < 0 or x >= list.length (Or words to that effect. If only one of the sources of a possible exception is mentioned full credit is still given.) O. 7 2. public int longestStreak(int[] readings, int threshold) { int max = 0; int current = 0; for(int i = 0; i < readings.length; i++) { if( readings[i] >= threshold ) { current++; if( current > max ) max = current; } else current = 0; } return max; } 3. public void eliminateTerm(int col) { boolean zeroFound = false; int row = 0; while( row < myCells.length && !zeroFound ) { zeroFound = myCells[row][col] == 0; row++; } if( !zeroFound ) { double factor; for(row = 1; row < myCells.length; row++) { factor = -( myCells[row][col] / myCells[0][col] ); for(int c = 0; c < myCells[0].length; c++) myCells[row][c] += myCells[0][col] * factor; } } return zeroFound; } 4. public class RadioStation { private double dMyFreq; private String myCallLetters; private String myFormat; public RadioStation(doublee freq, String callLetters, String format) { dMyFreq = freq; myCallLetters = callLetters; myFomat = format; } public void changeFormat(String newFormat) { myFormat = format; } public String toString() { return "frequency: " + dMyFreq + ", call letters: " + myCallLetters + ", format: " + myFormat; } public boolean equals(Object other) { RadioStation r = (RadioStation)other; return dMyFreq == r.dMyFreq && myCallLetters.equals( r.myCallLetters ) && myFormat.equals( r.myFormat ); } } 4 points for methods 4 points for instance variables do not take off points for minor syntax errors such as semicolns or capitilization. take off 1 point for each significant error in a method up to 4 per method.