Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public int[] frequency(Scanner scanner) Given a Scanner constructed with a Strin

ID: 3584536 • Letter: P

Question

public int[] frequency(Scanner scanner) Given a Scanner constructed with a String containing a stream of integers in the range of 0..10 (like quiz scores), return an array of 11 integers where the first value (at index 0) is the number of 0s in the Scanner, the second value (at index 1) is the number of ones on the Scanner, and the 11th value (at index 10) is the number of tens in the Scanner. The following assertions must pass. @Test public void testFrequencyWithOneZeroTwoOnesTwoTwosTwoFiveAndNoThreesOrFours() { LoopsAndArrays la = new LoopsAndArrays(); Scanner scanner = new Scanner("5 0 1 2 1 5 2"); int[] result = la.frequency(scanner); assertEquals(11, result.length); assertEquals(1, result[0]); // There was 1 zero assertEquals(2, result[1]); assertEquals(2, result[2]); assertEquals(0, result[3]); assertEquals(0, result[4]); assertEquals(2, result[5]); // There were 2 5s for(int score = 6; score <= 10; score++) { assertEquals(0, result[score]); // There were zero 6s, 7s, 8s, 9s, 10s } }

Explanation / Answer

//take str from scanner and returns array of freq(for num 0-10) public int[] frequency(Scanner scanner) { int[] arr={0,0,0,0,0,0,0,0,0,0,0}; int idx; while(scanner.hasNextInt()){ idx=Integer.parseInt(scanner.next()); arr[idx]++; } return arr; }