Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please read the instructions carefully and use dialog boxes for input and output

ID: 3726680 • Letter: P

Question

Please read the instructions carefully and use dialog boxes for input and output messages( JOptionPane) and please make sure the program works. Thank you.

Objective

To build a complete working Java program that offer practice with Java string manipulation.

Overview & Instruction                        

Write a Java program that performs encryption of a given message.

Your program is designed to send a coded message to an overseas embassy. The message will be a one-line comma-delimited string. The first token of the string should be a three-character code representing the country you wish to communicate with along with a priority code suffix.

The following list includes all possible countries:

FR - France GB - Great Britain

CA - Canada JA - Japan

RU - Russia GE - Germany

AU - Australia MX - Mexico

The following list includes all possible criticality codes:

1 - URGENT

2 - IMPORTANT

3 - ROUTINE

For example, if this part of the message includes GB1, you should use Great Britain in the message header and further indicate that the message is URGENT. The next part of the string will include the message to be delivered to the embassy. This will need to be "encrypted".

Error checking should be included so that any invalid country code or criticality level would push the user into a loop until they enter a correct pattern. Check further to be sure that there is exclusively one comma and that the message following the comma is not blank.

A sample message could be:                GB1,It is time for tea

Your encryption routine should:

Convert the message to all uppercase characters

Replace all blanks with underscore characters

Reverse the string

Swap pairs of characters (left-to-right, two at a time)

Exchange the characters of the string with the randomized key string

The key string should be stored in a file (that might change daily in a real-world scenario) that will look like this:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

KQBZCGOAWPMHVLFXEDURIYSJNT

A key file for the assignment is provided here:   key.txt

Finally, design your solution using an object oriented approach. This implies a Message class that will include the abilities to validate and manage the country code, criticality index, and to encrypt the message. A suggested design would be to read the user entry into the Message object as it is being constructed.

You output should be one String object ready for transmission in this form:

To Embassy: Great Britain|URGENT|xx_xxx_xxxx_xx

Note the use of the | symbol for delimiters. Note also substring after the last | symbol will be the encrypted message (only a rough sample provided here).

Your main driver application class can then be narrowed to managing the user interaction as well as method calls to the one Message object in the solution. Utilize dialog boxes for both input and output.

The following list includes all possible countries:

FR - France GB - Great Britain

CA - Canada JA - Japan

RU - Russia GE - Germany

AU - Australia MX - Mexico

The following list includes all possible criticality codes:

1 - URGENT

2 - IMPORTANT

3 - ROUTINE

Explanation / Answer

CODE:

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

public class Message {

    private static final String INPUTMESSAGEDELIMITER = ",";

    private static final String OUTPUTMESSAGEDELIMITER = "|";

    private static final String LOOPUSERWITHFAKEREPLY = "LOOP";

    private static Map<Integer,String> criticalityCodes = new HashMap<Integer,String>();

    private static Map<String,String> countryCodes = new HashMap<String,String>();

    static void populateCountryCodesAndCriticalityMap(){

        countryCodes.put("FR","France");

        countryCodes.put("GB","Great Britain");

        countryCodes.put("CA","Canada");

        countryCodes.put("JA","Japan");

        countryCodes.put("RU","Russia");

        countryCodes.put("GE","Germany");

        countryCodes.put("AU","Australia");

        countryCodes.put("MX","Mexico");

        criticalityCodes.put(1,"URGENT");

        criticalityCodes.put(2,"IMPORTANT");

        criticalityCodes.put(3,"ROUTINE");

    }

    static String decodeCountryCodeAndCriticality(String code){

        String countryCode = code.substring(0,2); //Splits in 2nd position to get first two letters of country code and criticality code

        Integer criticality =Integer.parseInt(code.substring(2,3)); //Splits in 2nd position to get first two letters of country code and criticality code

        if(countryCodes.containsKey(countryCode) && criticalityCodes.containsKey(criticality)){ //Validation for country code and criticality

            return "" + countryCodes.get(countryCode) + OUTPUTMESSAGEDELIMITER + criticalityCodes.get(criticality) + OUTPUTMESSAGEDELIMITER;

        }

        return LOOPUSERWITHFAKEREPLY;

    }

    static String encryptMessage(String message){

        message = message.trim(); // Remove unwanted blank spaces at the head and tail of String

        message = message.toUpperCase();

        message = message.replace(" ","_"); // Replace all blank spaces with underscore

        StringBuilder temp = new StringBuilder(message); //There mutiple ways where you can reverse a string, Here we have used StringBuilder for the purpose

        message = String.valueOf(temp.reverse()); // reverse StringBuilder temp and Wrap in String valueOf

        message = swapPairsOfCharacters(message);

        File key = new File("key.txt");

        String keyData = readFileToString(key);

        message = replaceCharactersByKeyData(keyData,message);

        return message;

    }

    static String replaceCharactersByKeyData(String keyData, String message){

        Map<Character,Character> charMap = new HashMap<Character,Character>();

        keyData = keyData.trim();

        String[] tokens = keyData.split(" "); // Get ABCDEFGHIJKLMNOPQRSTUVWXYZ KQBZCGOAWPMHVLFXEDURIYSJNT in String array as baseString and encString

        String baseString = tokens[0];

        String encString = tokens[1];

        for(int i=0; i<baseString.length(); i++){

            charMap.put(baseString.charAt(i),encString.charAt(i));

        }

        char[] result = new char[message.length()];

        for(int i=0; i<message.length(); i++){

            char temp = message.charAt(i);

            if(temp == '_')

                result[i] = temp;

            else

                result[i] = charMap.get(temp);

        }

        return String.valueOf(result);

    }

    static String swapPairsOfCharacters(String encMessage){

        char[] charArray = encMessage.toCharArray();

        for(int i=0; i+2 < encMessage.length(); i++){ // To pair swap the characters i.e char at index 0 with 2, 1 with 3 so on..

            char temp = charArray[i];

            charArray[i] = charArray[i+2];

            charArray[i+2] = temp;

        }

        return String.valueOf(charArray);

    }

    static String readFileToString(File key){

        String str = "";

        String fileData = "";

        try{

            BufferedReader in = new BufferedReader(new FileReader(key));

            while ((str = in.readLine()) != null){

                fileData += str + " ";

            }

            return fileData;

        } catch (IOException e) {

            System.out.println("Key File Not Found!!!!");

        }

        return str;

    }

    public static void main(String[] args){

        String input = "GB1,It is time for tea"; //Assuming input from command line. Can be changed accordingly

        String output = "";

        int commaCount = input.length() - input.replace(INPUTMESSAGEDELIMITER, "").length(); //Gets the count of comma in input String

        String[] tokens = input.split(INPUTMESSAGEDELIMITER);

        if(commaCount == 1 && tokens[1].length() > 0) { //Check for one comma and if the message is there or not

            populateCountryCodesAndCriticalityMap();

            output += decodeCountryCodeAndCriticality(tokens[0]);

            output += encryptMessage(tokens[1]);

            System.out.println("To Embassy: " + output);

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote