File PrintCodedjava shown above contains an incomplete progr

File PrintCoded.java (shown above) 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 printCoded function, that satisfies the following specs:

Function printCoded takes three arguments, called word, sources, targets. They are all strings.

Function printCoded 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 P of targets.

Otherwise, the function prints X.

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:

Solution

PrintCoded.java

import java.util.Scanner;

public class PrintCoded
{
  
  
  
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
  
String sources = \"abcdefghijklmnopqrstuvwxyz\";
String targets = \"bcdefghijklmnopqrstuvwxyza\";
  
while (true)
{
System.out.printf(\"Enter some word, or q to quit: \");
String word = in.next();
if (word.equals(\"q\"))
{
System.out.printf(\"Exiting...\ \");
System.exit(0);
}
printCoded(word, sources, targets);
System.out.printf(\"\ \ \");
}
}
public static void printCoded(String word, String sources, String targets){
   for(int i=0; i<word.length(); i++){
       char ch = word.charAt(i);
       boolean found = false;
       for(int j=0; j<sources.length(); j++){
           if(ch == sources.charAt(j)){
               found = true;
               System.out.print(targets.charAt(j));
           }
       }
       if(!found){
           System.out.print(word.charAt(i));
       }
   }
   System.out.println();
}

}

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: Exiting
Eyjujoh


Enter some word, or q to quit: q
Exiting...

File PrintCoded.java (shown above) contains an incomplete program. The goal of the program is to take as input strings from the user, and then print out coded (
File PrintCoded.java (shown above) contains an incomplete program. The goal of the program is to take as input strings from the user, and then print out coded (

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site