Free Response Practice Question (with solution)- SecretMessageCreatorClass
<-- Back to Additional FRQ Practice | Home to Additional FRQ Practice--> |
Secret Message Creator Class
Part 1: SecretMessageCreatorClass.java
Write the complete class definition for the SecretMessageCreatorClass class. This class is used to encode the contained string attribute to generate secret messages. The SecretMessageCreatorClass class consists of the following:
- String message: represents the message string that is to be encoded.
Additionally,
- The SecretMessageCreatorClass class contains a parametrized constructor, that takes a String object as parameter and initializes the message attribute of the class with the incoming string value.
- Helper method (String getEncodedMessage ()) to encode the message attribute and return the encoded string. The method uses following rules to encode the message string:
Rule 1: In case the alphabet is a vowel, replace it with the next character in the alphabet sequence. For example, ‘a’ is encoded an ‘b’.
Rule 2: In case the alphabet is a consonant, replace it with the previous character in the alphabet sequence. For example, ‘d’ is encoded an ‘c’.
Part 2: SecretMessageRunner.java
Write the complete class definition for the SecretMessageRunner class. The SecretMessageRunner class consists of a main() method that does the following:
- Creates few parameterised objects of the SecretMessageRunner class with strings like “abc”, “zab”, “ABC”, “ZAB”, and “Zua”. These string values will help test various combinations of alphabets and the boundary conditions.
- Invokes the getEncodedMessage() method on all the objects created and display the encoded messages generated.
Solution:
View SolutionSecretMessageCreatorClass.java
public class SecretMessageCreatorClass { private String message=null; public SecretMessageCreatorClass(String message) { this.message = message; } public String getEncodedMessage() { String code=new String(); for (int i=0; i< message.length(); i++) { if ((message.charAt(i) == 'a') || (message.charAt(i) == 'e') || (message.charAt(i) == 'i')|| (message.charAt(i) == 'o') || (message.charAt(i) == 'u')) { code = code + (char)(message.charAt(i) + 1); } else if ((message.charAt(i) == 'A') || (message.charAt(i) == 'E') || (message.charAt(i) == 'I')|| (message.charAt(i) == 'O') || (message.charAt(i) == 'U')) { code = code + (char)(message.charAt(i) + 1); } else code = code + (char)(message.charAt(i) - 1); } return code; } public String toString() { return message; } }
SecretMessageRunner.java
public class SecretMessageRunner { public static void main(String args[]) { SecretMessageCreatorClass msg = new SecretMessageCreatorClass("abc"); System.out.println("Encoded message for \"" + msg.toString() + "\" is: "+ msg.getEncodedMessage()); SecretMessageCreatorClass msg1 = new SecretMessageCreatorClass("zab"); System.out.println("Encoded message for \"" + msg1.toString() + "\" is: "+ msg1.getEncodedMessage()); SecretMessageCreatorClass msg2 = new SecretMessageCreatorClass("ABC"); System.out.println("Encoded message for \"" + msg2.toString() + "\" is: "+ msg2.getEncodedMessage()); SecretMessageCreatorClass msg3 = new SecretMessageCreatorClass("ZAB"); System.out.println("Encoded message for \"" + msg3.toString() + "\" is: "+ msg3.getEncodedMessage()); SecretMessageCreatorClass msg4 = new SecretMessageCreatorClass("Zua"); System.out.println("Encoded message for \"" + msg4.toString() + "\" is: "+ msg4.getEncodedMessage()); } }
Sample Output:
View OutputJava project files (with Runner code):
<-- Back to Additional FRQ Practice | Home to Additional FRQ Practice--> |