Answer:
Step-by-step explanation:
The following code was written in Java and follows all of the instructions provided. Some instructions were most likely written incorrectly as following them made no sense and would not allow the program to run. Such as placing the main method code into the encryptString method. Since the code inside the main method needed to stay inside the main method for the program to function accordingly. The code that most likely needed to be moved was the EncryptText code to the encryptString method.
package sample;
import java.util.Scanner;
class EncryptTextMethods {
private static String encryptMessage(String message) {
//convert string to character array
char stringChars[] = message.toCharArray();
//for each character
for(int i=0; i< stringChars.length; i++) {
char ch = stringChars[i];
//if character within range of alphabet
if(ch <= 'z' && ch >= 'a') {
//obtain the character value
//add 1 to the integer value
//enter modulus by 26 since z will become a
//once again add the remainder to 'a'
stringChars[i] = (char)('a' + ((int)(ch - 'a') + 1) % 26 );
}
}
//construct the string message
return String.valueOf(stringChars);
}
public static String decryptString(String message) {
char stringChars[] = message.toCharArray();
String decrypted = "";
for (int x = 0; x < stringChars.length; x++) {
int charValue = stringChars[x];
decrypted += String.valueOf( (char) (charValue - 1));
}
return decrypted;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter text to encode: ");
String message = keyboard.nextLine();
message = message.toLowerCase();
String encrypted = encryptMessage(message);
String decrypted = decryptString(encrypted);
System.out.println("Original Message: " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
keyboard.close();
}
}