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

Task 2 (10 pts.) - Encrypt words File hw4 task2.java contains an incomplete prog

ID: 3677580 • Letter: T

Question

Task 2 (10 pts.) - Encrypt words File hw4 task2.java contains an incomplete program. The goal of the program is to take as input strings from the user, and then print out coded (encrypted) versions of those strings, by replacing each letter with another letter. Complete that program, by defining a print_coded function, that satisfies the following specs: Function print_coded takes three arguments, called word, sources, targets. They are all strings. Function print_coded processes the letters of word one by one, in the order in which they appear in word. For every such letter X, the function processes X as follows If X is equal to the character at position P of sources, then the function prints the character at position Otherwise, the function prints X. P of targets o Note that arguments sources and targets are hardcoded in the main function, the user cannot change those values. The user can only specify the value of word. IMPORTANT: you are NOT allowed to modify in any way the main function. This is an example run of the complete program: Enter some word, or q to quit: hello ifmmp Enter some word, or q to quit: HELLO HELLO Enter some word, or q to quit: Arlington Asmjohupo Enter some word, or q to quit: Texas Tfybt Enter some word, or q to quit: q BYE

Explanation / Answer

//hw4_task2.java
/**The java program hw4_task2 reads strings from the user
* and encrypts them based on a letter mapping
* hardcoded in main.*/
import java.util.Scanner;
public class hw4_task2 {

   public static void main(String[] args) {
      
       //Create a Scanner class object
       Scanner in = new Scanner(System.in);

       String sources = "abcdefghijklmnopqrstuvwxyz";
       String targets = "bcdefghijklmnopqrstuvwxyza";

       System.out.printf("Enter some word, or q to quit: ");
       String word = in.next();
      
       while (!word.equals("q")) {
           //call the method printCoded
           printCoded(word, sources, targets);          
           System.out.printf(" ");
           System.out.printf("Enter some word, or q to quit: ");
           word = in.next();
       }
       System.out.printf("BYE ");
   }

  
  
   /**The method printCoded that takes three string word, sources and targets
   * and covnerts the word into using source and targets*/
   private static void printCoded(String word, String sources, String targets) {
      
       //declare two variables
       String encrypt="";
       char ch=' ';
      
       //Run the for loop for the size of the word
       for (int i = 0; i < word.length(); i++) {
           //Get the inndex of the word at sources string using indexOf
           int pos=sources.indexOf(word.charAt(i));
                  
           //Check if pos is not -1
           if(pos!=-1)
           {
               //Get the character from the targets
               ch=targets.charAt(pos);              
               //Append the ch to the encrypt string
               encrypt+=ch;
           }
           else
               //Otherwise append the original letter from word
               encrypt+=word.charAt(i);
       }
      
       //print the encrypt
       System.out.println(encrypt);
      
   }

   // Implement the printCoded method here.
}

-----------------------   -----------------------   ----------------------- -----------------------

Sample Output:

Enter some word, or q to quit: hello
ifmmp


Enter some word, or q to quit: HELLO
HELLO


Enter some word, or q to quit: Arlington
Asmjohupo


Enter some word, or q to quit: Texas
Tfybt


Enter some word, or q to quit: q
BYE

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