Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please complate the code JAVA The encoding/decoding scheme is very simple. Each letter is substituted by some other letter according to a given mapping such

please complate the code JAVA

The encoding/decoding scheme is very simple. Each letter is substituted by some other letter according to a given mapping such as the one shown below.

abcdefghijklmnopqrstuvwxyz

kngcadsxbvfhjtiumylzqropwe

For example, with the mapping above, every 'a' becomes a 'k' when encoding a text, and every 'k' becomes an 'a' when decoding.

You will write a program, Project1.java, which asks the user to enter a key string like above. If the key is valid, the program asks the user to enter the name of the file which they would like to encode with the given key. The program will then read the file and output to the console its encoded contents. If the key supplied is not valid (the mapping should be complete and each letter should map to a unique letter; e.g. a->k and f->k is not valid), the program rejects the key and asks for another key until a valid key is entered.

Capital letters are mapped the same way as the lower case letters above, but remain capitalized. For example, with the key above, every 'A' becomes a 'K' when encoding a text. Numbers and other characters are not encoded and remain the same.

Below are two separate runs of the program:

Run 1:

Enter encoding key: maxnrslkbpwfzjidouetchgvyq Enter the file name: sampleInput.txt 
************** Encoded Contents **************** Grfxizr ti XE 20A! 
Xizdctrue mur liin mt siffigbjl bjetucxtbije, act jit mt urmnbjl yicu zbjn. 
Bs yic mctizmtr m zree, yic lrt mj mctizmtrn zree. ************************************************ 

Good Bye!

Run 2:

Enter encoding key: key Key is not valid. Enter encoding key: sjwkvputqhoe Key is not valid. Enter encoding key: s5wkvputqhoenzlyrabxigmdcf Key is not valid. Enter encoding key: jjwkvputqhoenzlyrabxigmdcf Key is not valid. Enter encoding key: sjwkvputqhoenzlyrabxigmdcf Enter the file name: sampleInput.txt 
************** Encoded Contents **************** Mvewlnv xl WB 20J! 
Wlnyixvab sav ullk sx pleelmqzu qzbxaiwxqlzb, jix zlx sx avskqzu clia nqzk. 
Qp cli sixlnsxv s nvbb, cli uvx sz sixlnsxvk nvbb. ************************************************ 

Good Bye!

hints

To find the mapping for a letter (e.g. 'a' and 'A' go to index 0, 'b' and 'B' to index 1, etc.) recognize that you can treat a char as a number and do math with it (e.g. 'a' 'a' = 0)

Use the Scanner class to get input from the user or from the file. E.g.: import java.util.Scanner;

... Scanner scanner = new Scanner(System.in); // Scanner scanner = new Scanner(new File(filename)); System.out .println("Enter a line of text"); String text = scanner.nextLine(); System.out .println("You entered " + text); ... scanner.close();

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

public class Project1 {

static boolean validKey(String key) { // TODO 2:

// if length is not exactly 26 return false // if it contains any non lowercase letter return false // if any duplicates, return false return true; } /** * Check if a given character is a lower case letter * * @param c The character * @return true if lower case letter, false otherwise */ public static boolean isLowerCaseLetter(char c) { return c >= 'a' && c <= 'z'; }

/** * Check if a given character is an upper case letter * * @param c The character * @return true if upper case letter, false otherwise */ public static boolean isUpperCaseLetter(char c) { return c >= 'A' && c <= 'Z'; }

/** * Check if a given character is a letter * * @param c The character * @return true if a letter, false otherwise */ public static boolean isLetter(char c) { return isLowerCaseLetter(c) || isUpperCaseLetter(c); }

/** * Convert an upper case letter to lower case * * @param c The upper case character * @return The corresponding lower case letter */ public static char toLowerCase(char c) { return (char) (c + 'a' - 'A'); }

/** * Convert a lower case letter to upper case * * @param c The lower case character * @return The corresponding upper case letter */ public static char toUpperCase(char c) { return (char) (c + 'A' - 'a'); }

/** * Encode a character according to the fixed substitution pattern. * * @param c The character to encode * @return The encoded character */ public static char codeChar(char c, String key) { // return non alphabetical characters unchanged if (!isLetter(c)) { return c; } char encLett = c; // convert upper case letters to lower case if (isUpperCaseLetter(c)) { encLett = toLowerCase(c); } // TODO 1: encode a lower case char by looking it up return encLett; } /** * Encode a given message. * * @param msg The message to encode * @return The encoded message */ public static String codeMsg(String msg, String key) { String newMsg = ""; for (int i = 0; i < msg.length(); i++) { newMsg += codeChar(msg.charAt(i), key); } return newMsg; } public static void main(String[] args) { System.out.print("Enter encoding key: "); Scanner in = new Scanner(System.in); String key = in.nextLine(); while (!validKey(key)) { System.out.println("Key is not valid."); System.out.print("Enter encoding key: "); key = in.nextLine(); } System.out.print("Enter the file name: "); String filename = in.nextLine(); in.close(); System.out.println(); System.out.println("************** Encoded Contents ****************"); try(Scanner fileIn = new Scanner(new File(filename))) { while (fileIn.hasNextLine()) { String line = fileIn.nextLine(); String encodedLine = codeMsg(line, key); System.out.println(encodedLine); } } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("************************************************"); System.out.println(" Good Bye!"); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

compare and contrast various approaches to buying models;

Answered: 1 week ago