File PrintCodedjava contains an incomplete program The goal
File PrintCoded.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 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
import java.util.Scanner;
public class PrintCoded
 {
public static void printCoded(String word, String sources,String targets){
 String ans = \"\"; // final answer will be stored at ans
for (int i =0; i < word.length(); i++ ) {
 // iterate over the whole word
 int ind = sources.indexOf(word.charAt(i));
 // returns index of word charecters in word
if(ind >= 0){ // means charecter exists in source
 ans += targets.charAt(ind);
}else{
 ans += word.charAt(i);
 }
 }
System.out.println(ans);
 }
   
   
   
 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(\"\ \ \");
 }
 }
 }
Here is the output :-
nilmadhab@nilmadhab-Inspiron-3542:~/Desktop/chegg/answers/java$ java PrintCoded 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
 Exiting...


