3
answers
0
watching
410
views
31 Dec 2017

3. Consider the following method, which returns the number of times the parameter String word occurs in the parameter array wordArray: public static int countOccurrences (String[] wordArray, String word) { int result; result = 0; for (int i = 0; i < wordArray.length; i++) { if (wordArray[i] == word) { result = result + 1; return result; Unfortunately, the above method contains a bug. This bug causes the method to return 0 instead of the expected value of 2 when it is called in the following code fragment: String[] animalism = { new String ("four"), new String ("legs"), new String ("good"), new String ("two"), new String ("legs"), new String ("bad") }; int count = count Occurrences (animalism, "legs"); System.out.println("Number of occurrences: " + count); Describe what the problem is, and suggest a simple way to fix it. BE BRIEF; overly long answers will be grounds for mark deductions.

For unlimited access to Homework Help, a Homework+ subscription is required.

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in
Already have an account? Log in
Nelly Stracke
Nelly StrackeLv2
3 Jan 2018
Already have an account? Log in

Related questions

Related Documents

Weekly leaderboard

Start filling in the gaps now
Log in