Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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 {

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.

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education