Question

Cipher Program Specifications: You are to write a program called, "Cipher123" that reads in an encoded...

Cipher

Program Specifications: You are to write a program called, "Cipher123" that reads in an encoded message using the 1-2-3 cipher and returns the decoded message. The program should continue prompting for additional encoded messages until the user chooses to quit the program.

When decoding a message, we take the first letter of the first word, the second letter of the second word and the third letter of the third word and concatenate them. We repeat this same process 1-2-3 until there are no more words in the phrase. Suppose we received the message "Randy's lucky penny" that we know is encrypted using 1-2-3 encryption. We would take the first character from the first word ( 'R' ) and concatenate it with the second character of the second word ( 'u' ) and concatenate that with the third character of the third word ( 'n' ). Put it all together and we know that someone is telling us to “Run”.

Program Decomposition:

  1. public static String getUserInput(Scanner scnr) This method accepts a scanner parameter, prompts the user for the encoded message and returns the encoded message to the user. The user may enter the message on multiple lines and signals that they have finished entering the message by hitting the enter key on a line by itself. Data validation: The user must not enter an empty string as the decoded message. See sample run above.
  2. public static String decodeMessage(Scanner wordScanner) This method accepts a scanner parameter that is pointing to the encoded message. The method processes the string word by word using the 123 Cipher method and returns the deciphered string for printing. If while decoding, you come across a word that is smaller than the character needed (e.g., you need character 3 and the word is "go", you should print an appropriate message and stop decoding.
  3. Add more methods as needed

Sample Run

Enter the encoded message on one or several lines. Enter an empty line when you are finished typing your message:

Randy's lucky penny

<-- user hits carriage return to signal message is complete

You entered: Randy's lucky penny

The decoded message is: Run

Again? (Y/N)? y

Enter the encoded message on one or several lines. Enter an empty line when you are finished typing your message:

Randy's

lucky

penny

<-- user hits carriage return to signal message is complete

You entered: Randy's

lucky

penny

The decoded message is: Run

Again? (Y/N)? y

Enter the encoded message on one or several lines. Enter an empty line when you are finished typing your message:

<-- user hits carriage return for the message which is an invalid message

Please enter a message.

Funny Al's aly

<-- user hits carriage return to signal message is complete

You entered: Funny Al's aly

The decoded message is: Fly

Again? (Y/N)? n

If user enters a bad message (here the second word needs to have two letters - end the program and display “Sorry - this message cannot be decoded using 123 Cipher. Again? (Y/N)?"

Enter the encoded message on one or several lines. Enter an empty line when you are finished typing your message:

What a crazy message

<-- user hits carriage return to signal message is complete

You entered: What a crazy message

Sorry - this message cannot be decoded using 123 Cipher.

Again? (Y/N)? n

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Scanner;

public class Main {
  
   // function take scanner object and returns input line
   public static String getUserInput(Scanner scnr){
       String line;
       String fullString="";
       //loop until empty line is entered
       while ((line = scnr.nextLine()).length() > 0)
       {
           fullString=fullString+line+"\n";
       }
       //returning concatenated string
       return fullString;
   }
  
   //function to decode the string. Takes scanner as input
   public static String decodeMessage(Scanner wordScanner) {
       //declaring variables
       String decodedValue="";
       int count=0;
       //loop while scanner has next words
       while(wordScanner.hasNext()){
           //getting first character and adding to decoded value
           try{//try catch for index errors
           decodedValue=decodedValue+wordScanner.next().charAt(count);
           count++;//increasing count to check the number of words
           if(count==3)
               break;
           }catch(Exception e){
               return null;
           }
          
       }
       //if count is 3 then input is correct
       if(count==3)
       return decodedValue;
       else //else return null
           return null;
   }
  
   //main function
   public static void main(String[] args) {
       //declaring variables
       String encodedText;
       String decodedText;
       char choice='Y';
       Scanner scnr =new Scanner(System.in);
      
       //loop while choice is not N or n
       while(choice!='n' && choice!='N'){
           //prompting for input
           System.out.println("Enter the encoded message on one or several lines. Enter an empty line when you are finished typing your message:");
           //setting encoded text
           encodedText=getUserInput(scnr);
           //if entered line is not empty continue or print error message  
           if(encodedText!=""){
               // print entered input
                   System.out.println("You entered:"+encodedText);
                   //creating scanner object to be used as tokernizer
                   Scanner lineTokenizer = new Scanner(encodedText);
                   decodedText=decodeMessage(lineTokenizer);//get decoded message
                   if(decodedText!=null){//if not null print decoded text
                       System.out.println("The decoded message is:"+decodedText);
                       System.out.print("Again? (Y/N)? ");
                   }else{
                       //if null print error message
                       System.out.println("Sorry - this message cannot be decoded using 123 Cipher. Again? (Y/N)?");  
                   }
                   //closing scanner object
                   lineTokenizer.close();
               }else{
                   //print error message for empty input
                   System.out.println("Sorry - this message cannot be decoded using 123 Cipher. Again? (Y/N)?");
               }
               //getting choice
               choice = scnr.nextLine().charAt(0);
               //loop while choice is not valid
               while(choice!='n' && choice!='N' && choice!='Y' && choice!='y'){
                   System.out.println("Please give valid choice");
                   choice = scnr.next().charAt(0);
               }
           }
       System.out.println("Program ended");
       scnr.close();  
   }
}

Output:

Add a comment
Know the answer?
Add Answer to:
Cipher Program Specifications: You are to write a program called, "Cipher123" that reads in an encoded...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...

    Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher, to encrypt and decrypt a message. Your encryption key and message are to be read in from two different files and then the encrypted message will be output to third file. Then the encrypted message is read in and decrypted to a fourth file. A monoalphabetic cipher starts with an encryption word, removes the redundant letters from the word, and assigns what’s left to...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher...

    Write a program that implements an elementary bit stream cipher. An elementary level bit stream cipher is an encryption algorithm that encrypts 1 byte of plain text at a time. This one uses a given 4-bit bit pattern as the key. The size of the encrypted message that we want to be able to send has a maximum length of 200 characters. You must: 1. prompt the user to input the clear text to be encrypted. You must use printf()...

  • Do this using the C language. show me the code being executed and also copy and...

    Do this using the C language. show me the code being executed and also copy and paste the code so i can try it out for myseld Instructions A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (ie. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter...

  • requirements write c++ code for the above programs use only 1D arrays with loops and if...

    requirements write c++ code for the above programs use only 1D arrays with loops and if else statement don't use any other way as it would be useful Answer all four parts with comments for better understanding A top-secret message containing letters from A-Z is encoded to numbers using the following mapping: A -> 1 B- 2 Z-> 26 You are a SPY. You have to determine the total number of ways that message can be decoded. Note: An empty...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT