$29
Your 6 year-old neighbor has just excitedly told you that she has discovered this ‘really neat code’ which she and her friends are using to send messages to each other. The code, she explains, involves writing a number in place of each letter of the alphabet. The coding equivalences are:
A=1, B=2, C=3, D=4, E=5, … Z=26 (your neighbor doesn’t worry about lower case letters).
Furthermore, they are using zero for each space between words. Your neighbor, however, says that it takes a ‘really long time’ to translate her messages into code – or to decode the messages she receives from friends, so she is wondering if you could write a computer program to do those jobs for her (and you have agreed!).
Thus, if your program is ‘Encoding’ it then accepts a simple character message (letters and spaces only) and produces a list of numbers that ‘code’ the messages according to the scheme described above. At this time you are only operating with uppercase characters and the space – no lowercase or other special characters. Thus, you should transform any input into uppercase; if, during encoding, you find a character you are not expecting, use the number 99 in the output stream to represent it.
Conversely, if decoding you will accept a series of integer values from the operator and produce the words (letter and space) equivalents represented by those numbers. If any unexpected numbers appear in the list (numbers outside the range 0 to 26) you should print a ? (question mark) in that position of the decoded message. (Note that brute-force type methods for performing this operation - such as checking for each letter separately from A to Z - should be avoided.)
We will develop this using an OOP style of separate classes for the ‘view’ and ‘logic’ portions of the assignment. The form for this program would be:
Your name should appear in the title area after ‘LetterCode.’ When running, the screen might look as follows:
: The part A program will be the Encode function:
Part B: The Part B program will be the Decode function:
You do not need to add any menu options of your own – just accept the default ‘File-Exit’ menu.
Write a separate class called LetterCodeLogic.java to handle the actual encoding and decoding operations. In this project, use STATIC methods in the LetterCodeLogic class so that an ‘instance’ of the class is not needed (the Encode and Decode methods can be called directly). The LetterCodeLogic class will have the following:
An empty constructor (which is not used in our case)
A static ENCODE(String m) method that receives the string to be encoded and returns a fully built result string for output to the form.
A static DECODE(String m) method that receives the string to be decoded and returns a fully built result string for output to the form.
If an error occurs in either static method, the string returned should simply contain the error message.
Call the netbeans project LetterCode and zip the entire project before submitting through the assignment link.
Extra Credit (15 points):
As an extra credit option for this assignment, add a new Decode function which parses the message string recursively and returns the proper result string. The main view form would now be:
Where DecodeR is a call to a new public static method in the LetterCodeLogic class that recursively parses the string from the message text box.
The recursive conversion method will need to look for commas in the string message, which can be done with the .indexOf() method available to variables of type String. Once a comma is located, only part of the string is processed, from position zero up to the comma: this can be done with the .substring() method of the String class. You would also use the substring() method in the recursive call to send the ‘next’ version of the method only the remaining part of the message string.
For example. If the original message to be decoded was: “1,2” then the new recursive method would work as follows:
First call to DecodeR: message = 1,2; comma is found at position 1 of string; substring of position zero is extracted and processed just as in regular decode method; then, substring starting at position 2 is sent to DecodeR (recursive call).
Second call to DecodeR: message = “2” only; no comma found, so message is processed as a single value and returned;
One trick here is how to ‘store’ the result of each recursive call, since (unlike the recursive Dec2Bin algorithm) you are not printing the values during each call to DecodeR: you must accumulate them and return them all together. This can be done by careful construction of the return command. You must NOT use a global variable here, because this is a static class method (where globals are not appropriate inside the class because there is no instantiation of the object).
** You may wait on doing this extra credit until after we have completed the next assignment, which will include a recursive process (The BinDecConverter program – but even so this is still the hardest extra credit option in the course.