Java code fragment question help?
-
Java code fragment question (fixed version) This is a question that I got wrong on a test, I need a little help understanding it, and also what the correct answers are. Thanks, I appreciate it greatly. Here's the exact test question: Consider the following data field and method findLongest with line number for reference. Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest currently DOES NOT WORK AS INTENDED. For example if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest (10) should return 3, the length of the longest consecutive block of 10's. private int [] nums; public int findLongest (int target) { ...............int lenCount = 0; ...............int maxLen = 0; line 1.......for (int k = 0; k < nums.length; k++) line 2.......{ line 3............if (nums [k] == target) line 4............{ line 5.................lencount++; line 6............} line 7............else line 8............} line 9.................if (lenCount > maxLen) line 10...............{ line 11...................maxLen = lenCount; line 12................} line 13.......} line 14...} line 15...if (lenCount > maxLen) line 16...{ line 17..........maxLen = lenCount; line 18...} line 19...return maxLen; ..........} Question 1: The method findLongest currently does not work as intended. Which of the following best describes the value returned by a call to the current version of findLongest. A) It is the length of the shortest consecutive block of the value target in nums B) It is the length of the array nums C) It is the number of occurrences of the value target in nums D) It is the length of the first consecutive block of the value target in nums E) It is the length of the last consecutive block of the value target in nums Question 2: Which of the following changes should be made so that method findLongest will work as intended? A) Insert the statement lenCount = 0; between lines 2 and 3 B) Insert the statement lenCount = 0; between lines 8 and 9 C) Insert the statement lenCount = 0; between lines 10 and 11 D) Insert the statement lenCount = 0; between lines 11 and 12 E) Insert the statement lenCount = 0; between lines 12 and 13 Wow thats a lot, I really appreciate you time and effort, thanks again!
-
Answer:
lenCount is the length of the current sequence of target elements. maxLen is the maximum value that lenCount attains (in other words, the answer to the problem). The way it's written now, lenCount increases every time the loop passes a "target" element. It never decreases. To work correctly, lenCount should be reset whenever a non-target element is found. Question 1 is C -- lenCount increases each time a target is found and does not decrease. Question 2 is D -- this is the right place to reset lenCount.
Bob at Yahoo! Answers Visit the source
Related Q & A:
- How to Code Encryption Algorithm in Java?Best solution by code2learn.com
- how to close the applet in java code?Best solution by Stack Overflow
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- Can't delete name off messenger address list, error code 40402,need help pls?Best solution by Yahoo! Answers
- I need help with writing up the Java Code.Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.