1. Engineering
  2. Computer Science
  3. consider the following method which is trying to count the...

Question: consider the following method which is trying to count the...

Question details

Consider the following method, which is trying to count the number of characters matching a specific character in a String: int countMatchingchars (String string, char toMatch) int count 0 boolean isMatched false; int i-0; while (i < string·length()) if (string.charAt (i)toMatch) isMatchedtrue; count++ i+t if (isMatched) return count; else return 0 1.(1 point) Why does the method not return the correct value? 2. (1 point) What specific stylistic choice is the cause of the problem? 3. (1 point) Rewrite the method to return the correct result. Remove unnecessary variables and convert the while loop to a for loop. 4(1 point) Write a method which, given a string, returns true if and only if the count of the number of times the lowercase a appears in the string is divisible by 7. The methods signature should be boolean acountDivisibleBySeven (String string). Reminder: you can use the modulus (remainder) operator % to get the remainder of an integer division. For example, five divided by three is one, with a remainder of two. Java knows this too: ( 5 / 3-1) and ( 5 % 3-2). (Yes, you can refer to the countMatchingchars method you wrote above.)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution