File task3.java contains an incomplete program. The goal of the program is to ta
ID: 3676785 • Letter: F
Question
File task3.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 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:
Explanation / Answer
Answer:
import java.util.Scanner;
public class task3
{
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... ");
break;
}
print_coded(word, sources, targets);
System.out.printf(" ");
}
}
public static void print_coded(String word,String sources,String targets)
{
boolean k=false;
for(int i=0; i<word.length(); i++){
char ch = word.charAt(i);
if(ch >= 97 && ch <= 122) {
k= false;
}
else
k= true;
//if(k==true)
//System.out.print(y);
}
if(k==true)
System.out.println(word);
//System.out.println(k);
if(k!=true){
System.out.print(word.charAt(0));
for(int j=1;j<word.length();j++){
int charValue = word.charAt(j);
String next_char = targets.valueOf( (char) (charValue + 1));
System.out.print(next_char);}}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.