Define a method named countAll that accepts a String as an argument. The method
ID: 673757 • Letter: D
Question
Define a method named countAll that accepts a String as an argument. The method must
return an array of int of size 27, such that the value in position 0 is a count of the number of
‘a’ and ‘A’ characters in the input, the value in position 1 is a count of the number of ‘b’ and
‘B’ characters in the input, … the value in position 25 is a count of the number of ‘z’ and ‘Z’
characters in the input, and the value in position 26 is a count of all the non-alphabetic
characters in the input. For example,
• if s is "", then all entries in the array must be 0.
• if s is "a", then all entries in the array must be 0 except for entry 0, which must be 1.
• if s is "Baaa!", then all entries in the array must be 0 except for:
o entry 0, which must be 3,
o entry 1, which must be 1, and
o entry 26, which must be 1.
The only methods you may call on the String s are charAt(int) and length(). You may use
the static toLowerCase method defined in the Character class, which maps a char to its
lower-case equivalent. For example,
• Character.toLowerCase('a') returns 'a'
• Character.toLowerCase('A') returns 'a'
• Character.toLowerCase('%') returns '%'
You may, if you wish, define private helper method that you call from your character
counting method.
Explanation / Answer
public Set decode(String prefix, String code) { Set set = new HashSet(); if (code.length() == 0) { set.add(prefix); return set; } if (code.charAt(0) == '0') return set; set.addAll(decode(prefix + (char) (code.charAt(0) - '1' + 'a'), code.substring(1))); if (code.length() >= 2 && code.charAt(0) == '1') { set.addAll(decode( prefix + (char) (10 + code.charAt(1) - '1' + 'a'), code.substring(2))); } if (code.length() >= 2 && code.charAt(0) == '2' && code.charAt(1)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.