please complate the code JAVA The encodingdecoding scheme is

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:

Good Bye!

Run 2:

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!\");
   }

}

Solution

The TODOs are self explanatory.

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(key.length() != 26) return false;
  
// if it contains any non lowercase letter return false
if(key.matches(\".*[A-Z].*\")) return false;
  
// if any duplicates, return false
int table[] = new int[26];
for(int i=0; i<key.length();i++) {
System.out.println(key.charAt(i));
if(table[(int)key.charAt(i)-\'a\'] > 0) return false;
table[(int)key.charAt(i)-\'a\'] ++;
}

  
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
encLett = key.charAt( encLett - \'a\' );
  
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!\");
}
}

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
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
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
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
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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site