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:


Additionally,


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:



Solution:

View Solution

SecretMessageCreatorClass.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 Output



Java project files (with Runner code):


<-- Back to Additional FRQ Practice Home to Additional FRQ Practice-->