Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is
Question:
Consider the following instance variable and method findLongest with line numbers added for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. (The same code is used in another question.) For example, if the array nums contains the values [7,10,10,15,15,15,15,10,10,10,15,10,10], the call find- Longest(10) should return 3, the length of the longest consecutive block of 10s. 1 private int[] nums; 2 public int findLongest(int target) { 3 int lenCount = 0; 4 int maxLen = 0; 5 for (int i = 0; i < nums.length; i++) { 6 if (nums[i] == target) { 7 lenCount++; 8 } else { 9 if (lenCount > maxLen) { 10 maxLen = lenCount; 11 } 12 } 13 } 14 if (lenCount > maxLen) { 15 maxLen = lenCount; 16 } 17 return maxLen; 18 }
Horngrens Financial and Managerial Accounting
ISBN: 978-0133866292
5th edition
Authors: Tracie L. Nobles, Brenda L. Mattison, Ella Mae Matsumura