Write a code in Java programming You are working in a company, and your new task is creating an encoding algorithm to secure a given data. Given a code, write a method called encode() that takes a String and returns a new String based on the frequency of the characters. All the even-frequency characters should be placed before the odd-frequency characters, and finally, all characters should be sorted in increasing order. Your colleague is sick again, and he cannot finish his task. He gave you an unfinished code like the following: public static String encode(String code) { // MyFunction mf = // TODO return mf.encode(code); } Your job is to complete the implementation of the functional interface MyFunction that encode() method should use to encode the given String code. The MyFunction interface should have the following signature: interface MyFunction {
Write a code in Java programming
You are working in a company, and your new task is creating an encoding
All the even-frequency characters should be placed before the odd-frequency characters, and finally, all characters should be sorted in increasing order.
Your colleague is sick again, and he cannot finish his task. He gave you an unfinished code like the following:
public static String encode(String code) {
// MyFunction mf = // TODO
return mf.encode(code);
}
Your job is to complete the implementation of the functional interface MyFunction that encode() method should use to encode the given String code.
The MyFunction interface should have the following signature:
interface MyFunction {
String encode(String code);
}
Additionally, your algorithm should handle the following cases:
1. If the input String is null, the encode() method should return null.
2. If the input string is empty or contains only whitespaces, the encode() method should return an empty string.
3. If the input String contains non-alphanumeric characters, the encode() method should ignore them.
Thus, if you complete the above code correctly, you should have an output like the following:
encode("98089911223") returns "11228803999"
encode("333000111") returns "000111333"
encode("001119772") returns "007711129"
encode("") returns ""
encode(null) returns null
encode(" \t\n") returns ""
encode("aBcD-12!") returns "12Bac"
Note that you should not change the method signature of the MyFunction interface or the encode() method.
Step by step
Solved in 3 steps