Search⌘ K

Solution Review: Message Encryption

Explore how to implement message encryption in Java by applying string methods such as toLowerCase, substring, and replace. Understand the step-by-step process of modifying strings to create encrypted messages, which is essential for mastering Java string manipulation in the AP Computer Science course.

Rubric criteria

Solution

Java
class RunEncryption
{
public static void main(String args[])
{
// Encrypting a String
System.out.println(encrypt("hefOP830nskdd0qSW uIm", '0', '.', "tuy83AB9"));
}
// Method to encrypt the string
public static String encrypt(String stringToBeEncrypted, char oldChar, char newChar, String extraString)
{
String str = stringToBeEncrypted.toLowerCase();
String update = str.substring(0, str.length()/2) + extraString + str.substring(str.length()/2);
return update.replace(oldChar, newChar);
}
}

Rubric-wise explanation


Point 1:

Look at line 10. We declare the header of the encrypt method: public static String encrypt(String str). It takes four parameters: ...