231k views
0 votes
In the JavaIntro_Challenges class (in JavaIntro_Challenges.java), create a public static function called "c4" that does the following:

The function will take a string and will analyze each character in the string. If the character represents a number, it will be checked to see if it is odd or even. Count every odd numbers towards the odd count and every even number towards the even count. Return an int array with the odd count as the first value (i.e. value at index 0) and the even count is the second value (i.e. value at index 1). Ignore all characters that are not digits. An empty string should result in {0, 0} being returned.


Examples:
/* call */ JavaIntro_Challenges.c4("48975165")
/* returns */ {5, 3}
/* notes */ There are 5 odd digits and 3 even digits, so {5, 3} is returned.
/* call */ JavaIntro_Challenges.c4("286644000")
/* returns */ {0, 9}
/* notes */ Only even digits are present, and there are 9 of them.
/* call */ JavaIntro_Challenges.c4("5Hello78World00!!")
/* returns */ {2, 3}
/* notes */ We ignore the non-numbers and only consider 57800 as our digits. 5 and 7 are odd and 8, 0 and 0 are even, so we return {2, 3}. For the purpose of this challenge, consider 0 as even.
/* call */ JavaIntro_Challenges.c4("No-Digits_Are Here")
/* returns */ {0, 0}
/* notes */ Since there are no digits, there are no odd nor even digits. So, we return {0, 0}.
/* call */ JavaIntro_Challenges.c4("")
/* returns */ {0, 0}
/* notes */ As stated in the directions, an empty string results in {0, 0}

User Fazila
by
7.7k points

1 Answer

4 votes

Here's the implementation of the `c4` function in the `JavaIntro_Challenges` class:

```java

public class JavaIntro_Challenges {

public static int[] c4(String input) {

int oddCount = 0;

int evenCount = 0;

for (char ch : input.toCharArray()) {

if (Character.isDigit(ch)) {

int digit = Character.getNumericValue(ch);

if (digit % 2 == 0) {

evenCount++;

} else {

oddCount++;

}

}

}

return new int[] { oddCount, evenCount };

}

public static void main(String[] args) {

// Test cases

int[] result1 = c4("48975165");

System.out.println(Arrays.toString(result1)); // [5, 3]

int[] result2 = c4("286644000");

System.out.println(Arrays.toString(result2)); // [0, 9]

int[] result3 = c4("5Hello78World00!!");

System.out.println(Arrays.toString(result3)); // [2, 3]

int[] result4 = c4("No-Digits_Are Here");

System.out.println(Arrays.toString(result4)); // [0, 0]

int[] result5 = c4("");

System.out.println(Arrays.toString(result5)); // [0, 0]

}

}

```

The `c4` function takes a string `input` and analyzes each character in the string. It counts the odd and even digits and returns an integer array with the odd count as the first value and the even count as the second value.

In the function, we initialize `oddCount` and `evenCount` variables to keep track of the counts. We loop through each character in the input string using a `for-each` loop. For each character, we check if it is a digit using `Character.isDigit(ch)`. If it is a digit, we convert it to an integer using `Character.getNumericValue(ch)`. We then check if the digit is odd or even by checking if `digit % 2 == 0`. Based on the result, we increment the corresponding count variable.

Finally, we create a new integer array with the odd count and even count, and return it.

In the `main` method, you can see example test cases that call the `c4` function and print the results. You can add more test cases as needed to further validate the function.

User Cocoatomo
by
8.1k points

No related questions found