S A M P L E CPS 1 (Ramm) Spring 1998 25 February 1998 Quiz #6 Name ___________________ Honor Code________________________ Section _____ A. Assume you have Java code which includes an array of doubles named t. Also assume that we have gotten data into this array as suggested by the diagram below. Also assume there were all of the needed declarations, etc., which are omitted here. 0 1 2 3 4 5 6 7 8 9 ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ t | 30.0 | 70.0 | 15.0 | 85.0 | 30.0 | 70.0 | 80.0 | 20.0 | 25.0 | 75.0 | ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ After the following code runs, what will be shown in the TextField symbolized by the rectangle below? double sumArray(double[] ara, int used) { int k; double sum = 0.0; k = 0; while (k < used) { sum = sum + ara[k]; k = k + 1; } return sum; } ... n = 8; mResult.setText("The sum of "+n+" elements = "+sumArray(t, n)); ... --------------------------------------------------------------------- B. The following code fragment "processes" an array of "size" strings called names. What is written to the TextField mA? Declarations and standard supporting code are omitted. int countNames(String[] names, int size, String key) { int k, count; k = 0; count = 0; while (k < size) { if (names[k].equals(key)) { count = count + 1; } k = k + 1; } return count; } ... mA.setText("The number of matches is " + countNames(names, 100,"Smith"); ... NOTE: When two strings are best checked for equality by the use of the "equals" method of the String class. It returns a boolean (true if the strings are equal and false if they are not). For example: to compare the strings stored in String variables s and r you could either use s.equals(r) or r.equals(s) If we assume that s = "Jones" and r = "Smith", then s.equals(r) returns false r.equals(s) returns false r.equals("Smith") returns true s.equals("Jones") returns true