In: Computer Science
Please convert this code written in Python to Java:
import string
import random
#function to add letters
def add_letters(number,phrase):
#variable to store encoded word
encode = ""
#for each letter in phrase
for s in phrase:
#adding each letter to encode
encode = encode + s
for i in range(number):
#adding
specified number of random letters adding to encode
encode = encode
+ random.choice(string.ascii_letters)
#returns encoded word
return encode
#function to remove letters
def remove_letters(number,phrase):
#variable to store decoded word
decode = ""
#iterating each letter in phrase
for i in range(len(phrase)):
#if i % number + 1 is zero
if i % (number + 1) == 0:
#adding letters
in specified position to decode
decode = decode
+ phrase[i]
#returns decoded word
return decode
#repeat until user wants to quit
while True:
print("")
#prompt for user option
userOption = input("(e)ncode, (d)ecode, (q)uit:
")
#if option is encode
if userOption == "e":
#prompt for number
number = int(input("Enter a number
between 1 and 5: "))
#repeat until number is valid
while True:
#if number is
valid
if number >=
1 and number <= 5:
break
#if number is
not valid
else:
print("Try Again...")
#prompt for number
number = int(input("Enter a number between 1 and
5: "))
#prompt for phrase to encode
phrase = input("Enter a phrase to
encode: ")
#calling function add_letters and
printing encoded word
print("Your encoded word is:
"+add_letters(number,phrase))
#if option is decode
elif userOption == "d":
#prompt for number
number = int(input("Enter a number
between 1 and 5: "))
#repeat until number is valid
while True:
#if number is
valid
if number >=
1 and number <= 5:
break
#if number is
not valid
else:
print("Try Again...")
#prompt for number
number = int(input("Enter a number between 1 and
5: "))
#prompt for phrase to decode
phrase = input("Enter a phrase to
decode: ")
#calling function remove_letters
and printing decoded word
print("Your decoded word is:
"+remove_letters(number,phrase))
#if option is quit
elif userOption == "q":
break
#if option is wrong option
else:
print("Try Again...")
Here is the converted code for this problem in Java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// EncodeDecode.java
import java.util.Scanner;
public class EncodeDecode {
// function to add letters
static String add_letters(int number, String phrase) {
// variable to store encoded word
String encode = "";
// variable to store all letters in ascii table
String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// for each letter in phrase
for (Character c : phrase.toCharArray()) {
// adding each letter to encode
encode = encode + c;
for (int i = 0; i < number; i++) {
// adding specified number of random letters adding to encode
encode = encode
+ letters.charAt((int) (Math.random() * letters
.length()));
}
}
// returns encoded word
return encode;
}
// function to remove letters
static String remove_letters(int number, String phrase) {
// variable to store decoded word
String decode = "";
// iterating each letter in phrase
for (int i = 0; i < phrase.length(); i++)
// if i % number + 1 is zero
if (i % (number + 1) == 0) {
// adding letters in specified position to decode
decode = decode + phrase.charAt(i);
}
// returns decoded word
return decode;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// repeat until user wants to quit
while (true) {
System.out.println("\n(e)ncode, (d)ecode, (q)uit: ");
// prompt for user option
char userOption = scanner.nextLine().toLowerCase().charAt(0);
// if option is encode
if (userOption == 'e') {
// prompt for number
System.out.print("Enter a number between 1 and 5: ");
int number = Integer.parseInt(scanner.nextLine());
// repeat until number is valid
while (true) {
// if number is valid
if (number >= 1 && number <= 5) {
break;
}
// if number is not valid
else {
System.out.println("Try Again...");
// prompt for number
System.out.print("Enter a number between 1 and 5: ");
number = Integer.parseInt(scanner.nextLine());
}
}
// prompt for phrase to encode
System.out.print("Enter a phrase to encode: ");
String phrase = scanner.nextLine();
// calling function add_letters and printing encoded word
System.out.println("Your encoded word is: "
+ add_letters(number, phrase));
}
// if option is decode
else if (userOption == 'd') {
// prompt for number
System.out.print("Enter a number between 1 and 5: ");
int number = Integer.parseInt(scanner.nextLine());
// repeat until number is valid
while (true) {
// if number is valid
if (number >= 1 && number <= 5) {
break;
}
// if number is not valid
else {
System.out.println("Try Again...");
// prompt for number
System.out.print("Enter a number between 1 and 5: ");
number = Integer.parseInt(scanner.nextLine());
}
}
// prompt for phrase to decode
System.out.print("Enter a phrase to decode: ");
String phrase = scanner.nextLine();
// calling function remove_letters and printing decoded word
System.out.println("Your decoded word is: "
+ remove_letters(number, phrase));
}
// if option is quit
else if (userOption == 'q') {
break;
} else
System.out.println("Try Again...");
}
}
}
/*OUTPUT*/
(e)ncode, (d)ecode, (q)uit:
e
Enter a number between 1 and 5: 4
Enter a phrase to encode: hello world
Your encoded word is: hyaemenGpMlhWuYlgfwOoESnt RPQwwiOoqokjttrbwWrlDZTRdofOr
(e)ncode, (d)ecode, (q)uit:
d
Enter a number between 1 and 5: 4
Enter a phrase to decode: hyaemenGpMlhWuYlgfwOoESnt RPQwwiOoqokjttrbwWrlDZTRdofOr
Your decoded word is: hello world
(e)ncode, (d)ecode, (q)uit:
e
Enter a number between 1 and 5: 6
Try Again...
Enter a number between 1 and 5: 1
Enter a phrase to encode: java
Your encoded word is: jdaMvEam
(e)ncode, (d)ecode, (q)uit:
d
Enter a number between 1 and 5: 1
Enter a phrase to decode: jdaMvEam
Your decoded word is: java
(e)ncode, (d)ecode, (q)uit:
q