S A M P L E Quiz #5 Name ___________________ Honor Code________________________ Section _____ A. 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"); ... ------------------------------------- mA |The number of matches is XXX | ------------------------------------- where XXX is an integers equal to the number of times "Smith" is found int the array names. If there were 3 Smith's in names, then it would look like this: ------------------------------------- mA |The number of matches is 3 | ------------------------------------- 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 B. The following code fragment "processes" an array of 500 integers called data. Complete it to find the largest value in the array. The lines suggest places to put code. (There may be too many lines.) Declarations are omitted. largest = data[0]; n = 500; k = 1; while (k < n) { if (data[k] > largest) { largest = data[k]; } k = k + 1; } mAnswer.setText("The largest number found is " + largest); C. Something where we pass parameters into a method to do something. (e.g. the Chain and Dia methods shown in lecture.)