Question

In: Computer Science

Write a java program that prompts the user to see if they wish to encode or...

Write a java program that prompts the user to see if they wish to encode or decode a message.
(a) If they choose to encode a message:
i. Ask them to enter the message and store it in a String variable.
ii. Once the message is entered, ask them for a “shift” integer, similar to Lab 6.
iii. Using that shift, move all alphabetic characters in the message (i.e. excluding spaces and punctuation) using char and int arithmetic. Be sure to account for alphabetical wrap-
around!
iv. Ask the user for a filename (no spaces), and then output the encrypted message to that file
on a single line. Then you may terminate the program, or loop back to the beginning.
(b) If they choose to decode a message:
i. Ask them for the file name that stores their encrypted message.
ii. Ask them for the “shift” that was used to encrypt the message.
iii. Read in the message from the file into a String (assuming it is all on one line).
iv. Using the shift, reverse the encryption procedure by subtracting the shift from each character
of the message, taking into account wrap-around.
v. Print the decrypted message to the user. You may then terminate the program or loop back
to the beginning.

Solutions

Expert Solution

import java.util.Scanner; // Import the Scanner class
import java.io.IOException; // Import the IOException class
import java.io.FileWriter;   // Import the FileWriter class
import java.nio.file.*;
class Main {
  //function to encode the message
  public static void encode()
  {
    Scanner sc = new Scanner(System.in);
    System.out.println(" Input the plaintext message : ");
    String plaintext = sc.nextLine();
    System.out.println(" Enter the value by which each character in the plaintext message gets shifted:"); int shift = sc.nextInt();
    String ciphertext = "";
    char alphabet;
    for(int i=0; i < plaintext.length();i++) 
    {
      // Shift one character at a time
      alphabet = plaintext.charAt(i);
            
      // if alphabet lies between a and z 
      if(alphabet >= 'a' && alphabet <= 'z') 
      {
        // shift alphabet
        alphabet = (char) (alphabet + shift);
       // if shift alphabet greater than 'z'
        if(alphabet > 'z') 
        {
          // reshift to starting position 
            alphabet = (char) (alphabet+'a'-'z'-1);
        }
             ciphertext = ciphertext + alphabet;
        }
            
      // if alphabet lies between 'A'and 'Z'
      else if(alphabet >= 'A' && alphabet <= 'Z') {
      // shift alphabet
      alphabet = (char) (alphabet + shift);    
      // if shift alphabet greater than 'Z'
      if(alphabet > 'Z') 
      {
        //reshift to starting position 
        alphabet = (char) (alphabet+'A'-'Z'-1);
      }
      ciphertext = ciphertext + alphabet;
      }
      else 
      {
        ciphertext = ciphertext + alphabet;   
      }
        
    }
    //user input for filename to store encrypted message
   System.out.println("Enter the file name where you want to store encrypted message:");
   String filename = sc.next();
    //writing encrypted message to the file
    try
    {
      FileWriter fp = new FileWriter(filename);
      fp.write(ciphertext);
      fp.close();
    }
    catch(IOException e)
    {
      System.out.println("Error Occured");
    }
  }
//function to decode the message
  public static void decode() throws Exception
  {
    Scanner sc = new Scanner(System.in);
    //user input for filename
    System.out.println("Enter the file name which have stored encrypted message:");
    String filename = sc.next();
    //shift
     System.out.println(" Enter the value by which each character in the plaintext message gets shifted:"); int shift = sc.nextInt();
     String ciphertext ;
     String decryptMessage = "";
     //reading the encrypted string from the file and store in ciphertext variable
    ciphertext  = new String(Files.readAllBytes(Paths.get(filename)));

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

        {
            // Shift one character at a time
            char alphabet = ciphertext.charAt(i);
            // if alphabet lies between a and z 
            if(alphabet >= 'a' && alphabet <= 'z')
            {
                // shift alphabet
                alphabet = (char) (alphabet - shift);
            
                // shift alphabet lesser than 'a'
                if(alphabet < 'a') {
                    //reshift to starting position 
                    alphabet = (char) (alphabet-'a'+'z'+1);
                }
                decryptMessage = decryptMessage + alphabet;
            }    
                // if alphabet lies between A and Z
            else if(alphabet >= 'A' && alphabet <= 'Z')
            {
             // shift alphabet
                alphabet = (char) (alphabet - shift);
                
                //shift alphabet lesser than 'A'
                if (alphabet < 'A') {
                    // reshift to starting position 
                    alphabet = (char) (alphabet-'A'+'Z'+1);
                }
                decryptMessage = decryptMessage + alphabet;            
            }
            else 
            {
             decryptMessage = decryptMessage + alphabet;            
            } 
        }
        System.out.println(" Decrypted message : " + decryptMessage);
    }

  
  public static void main(String[] args) throws Exception {
    int ch;
    Scanner sc = new Scanner(System.in);
    //choose the operation
    System.out.println("1.Encode Message");
    System.out.println("1.Decode Message");
    System.out.println("Enter your choice:");
    ch = sc.nextInt();
    sc.nextLine();
    //if ch is 1 perform encode operation
    if(ch==1)
    {
      encode();
    }
        //if ch is 2 perform decode operation

    else if(ch==2)
    {
      decode();
    }
    else
    {
      System.out.println("Invalid choice");
    }
  }
}

Output::

Encode operation:

Decode operation::

The encrypt.txt which stores encrypted text::


Related Solutions

JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a Java program to do the following: Specifics: Write an application that prompts a user...
Write a Java program to do the following: Specifics: Write an application that prompts a user for two integers and displays every integer between them. Display a message if there are no integers between the entered values. Make sure the program works regardless of which entered value is larger. Save the file as InBetween.java. Don’t forget to create the application/project  InBetweenTest.java Class that has the main method and an object to use the InBetween class.
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT