Answered You can hire a professional tutor to get the answer.

QUESTION

In this lab you will write a program to encode and decode text using simple character substitutions.

In this lab you will write a program to encode and decode text using simple character substitutions. The only characters permitted in a message will be upper and lower case letters, spaces, commas, periods, semicolons, and colons. Encoded messages will use the characters '1', '2', '3', '4', and '5' together with the upper and lower case letters to represent the message characters. Encoding will consist of replacing each message character by the corresponding character in a code. Here is an example of how a code is specified. Call this CODE1.

HTNZUL5m3lDQchenpIuORws1jPgVtzKWbfBxXSArdayCJkvqGiF2YoM4E

This code is a permutation of the upper and lower case letters and the digits 1 through 5. To encode a message, replace 'A' by 'H', 'B' by 'T', and so on, through replacing 'Z' by 'P', 'a' by 'g', 'b' by 'V' and so on, through replacing 'z' by '2', and then replacing ' ' by 'Y', ',' by 'o', '.' by 'M', ';' by '4' and finally ':' by 'E'.

Using this code, the text "Hello, this is a boring message." would be encoded as follows.

mKSSd oYkfB JYBJY gYVdC BrbYA KJJgb KM

Notice how the resulting encoded message is broken into groups of five characters (possibly excepting the last group) with spaces added in between. When you encode a message, you should break up the result in the same fashion, and when you decode you should also expect the encoded message to be broken up in this fashion.

It is convenient to store an encoded message in a file, and have input to your program be redirected from that file. It is also convenient to output a coded message to a file by redirecting the output of your program to that file. (Ask the TA to explain this if you are unsure.) The way to do both of these things is as follows:

% java Enc1 <in.txt >out.txt

Step 1.Edit a new file called Enc1.java in your lab5 directory (if you don't have a lab5 directory, then create one). The Enc1 class should contain a constant with the following declaration.

private static final String CODE1 = "HTNZUL5m3lDQchenpIuORws1jPgVtzKWbfBxXSArdayCJkvqGiF2YoM4E";

to be used as above. You should also implement the following methods in this class, to encode and decode Strings as above.

public static String encode(String message)

public static String decode(String codedMessage)

In this first step, no encoding or decoding should be done: each method should return exactly the same string that it is given. In addition to this, the encode method should print "encode called" to the console and the decode method should print "decode called" to the console.

Write a third method in the Enc1 class with the following signature.

public static boolean isCoded(String s)

This method should examine its argument to see if it has a space in positions 5, 11, 17, 23 and so on, and if it consists of letters and the digits 1 though 5 in all other positions. If this is the case it should return true, otherwise return false. (The method is not infallible, but it will do for strings that are not too short.)

Write a main method with the usual signature. This method should prompt for a line of input from the console, get that line, callisCodedto determine whether the line looks like encoded text, and if so calldecodeto "decode" it, and print the result to the console. Otherwise it should callencodeto "encode" it and print the result to the console. The methodcharAtin theStringclass may be used to get a character from a given position in a String, and thelengthmethod in theStringclass used to find the length of a string.

Step 2.Copy the file Enc1.java to Enc2.java, then edit Enc2.java so that the name of the public class is Enc2, not Enc1. Now add real encoding to the program by rewriting the body of the encode method . Use the CODE1 String to encode, and test using the example above for input. Use an array of characters (char[]) to store the results of encoding. Then use the String constructor with the signature

public String(char[] a)

to convert this to a String that can be returned. Get some hints from the TAs about how to approach this problem.

Step 3.Copy the file Enc2.java to Enc3.java, then edit Enc3.java so that the name of the public class is Enc3, not Enc2. Now add real decoding to the program by rewriting the body of the decode method. Use the CODE1 String to decode, and test using the example above for input. Use an array of characters to store the results of decoding. Then use the String constructor with the signature

public String(char[] a)

to convert this to a String to return. Get some hints from the TAs about how to approach this problem. When you are confident that your program works correctly, replace the value of CODE1 in your program by

"jznAauwZ4ovW1QOfIGY2myFC3bNKglUVTkPXHBSJMcthsdR5LqeDxiprE"

and decode the following code message.

nMJTh NdRBN dPMJs ExeMR xkN5U xsRgg UssVR BBexl UgMlU lxdkP sxSUs sNTUp

public class Enc1 {private static final String CODE1 =&quot;HTNZUL5m3lDQchenpIuORws1jPgVtzKWbfBxXSArdayCJkvqGiF2YoM4E&quot;; public static String encode(String message) {message = &quot;Encode...
Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question