Starting from:

$25

COMP1210-Activity 3 Using Classes and Objects Solved

You will create a program that exchanges letters in a String to encode a message.  

 

In-lab Directions: 

As you build your program incrementally by entering each of the code segments below, you should compile and run your program after each segment is completed.  This will allow you to verify that your program is correct after each step as you progress toward the completed program.  

MessageConverter.java 

•        This program will read in a message from the user and then require the user to enter a number. The following will happen based on the number entered by user: o If the user enters a 1, the message will be printed trimmed.  

o   If the user enters a 2, the message will be printed in lower case. o If the user enters a 3, the message will be printed in upper case.

o   If the user enters a 4, the message will be printed with all vowels replaced with underscores.

o   If the user enters a 5, the message will be printed without the first and last character.

o   Any other number should generate an appropriate message.

 

•        Create a class called MessageConverter that includes a main method. Also add an import statement for the java.util.Scanner class.  The import statement should be the first line in your Java file (i.e., it comes before the class declaration as well as comments).    

 

•        In the main method, declare the following variables:

-        userInput: Scanner object for reading the user input from the keyboard.

Scanner userInput = new Scanner(System.in);

-        message: String object for the user input; initialized to an empty String.

String message = "";
-        outputType: An int representing the type of output the user wants.

-        result: String object for the converted message; initialized to an empty String.

String result = "";
 

•        Request input from the user then get user input using the nextLine() method of the Scanner class.

  

Now get the user’s input for the output type. Make sure that you understand how the code below works. Notice that this is a call to the print method rather than println.

  

•        Set up an if statement that will print out the appropriate message based on the user's selected output type.  Note that you can write code for alternate conditions in the if statement using else if after the if block.  

  

•        After the entire if-else statement, add the following line of code to print out the result.  

  

Compile now and after each step below as you fill in the if-else-if-else in the following steps.

•        The first condition for outputType equal to 0 sets the result to the original message which may include leading and/or trailing spaces.

  

Because message references an instance of the String class, we can use the methods in the String class to return a new String that is a modified version of the original String referenced by message (the original String object is not changed).  To see the available methods in the String class, go to the Java API at http://docs.oracle.com/javase/8/docs/api/ and find the String class in the lower left pane on the page and left-click on it.  This will open the String page in large right window of the API as shown below.  You should become familiar with the API since it describes all of the classes that come with the JDK.

 

 

•        In the main window on the right side, click method at the top or scroll down to Method Summary to see the methods that our String object message can use.

 

•        String methods do not modify the String object upon which they are called. Instead, if a method has a String return type, then it returns a new String object which may be a modified version of the original String.   

 

The second condition for outputType equal to 1 sets the result to the trimmed value of the message; i.e., we can use the trim method removes any leading or trailing whitespace. The API entry for a method shows the return type for the method in the left and the method name with formal parameters, if any, on the right along with a brief description of what the method does.  So the entry shown below for the trim method indicates that it returns a String and takes no parameters. Remember, the trim method does not modify the String object upon which it is called. Instead it returns a new String object which is the trimmed version of original String.

 

Returns a String             Method name – there are no formal parameters for this method

  

Be sure to compile each time you complete a part of the if-else-if-else below.

  

•        The third condition for outputType equal to 2 sets the result to the lower case value of the message; i.e., we can use the toLowerCase method.  The Java API entry shown below for the toLowerCase method indicates that it returns a String and takes no parameters.

 

             Returns a String             Method name – there are no formal parameters for this method                                                                                     

  

Based on this knowledge, you can now invoke the toLowerCase method on our message object to return its value in lower case. Place the following code in second block of the if statement:

  

 

•        Look at the API description for the toUpperCase method and place the following line in the second block of the if statement:

  

 

•        Now write code that will replace specified characters in the message with other characters. In order to change a character, we need to use the replace method of String to change certain characters of our string. Take a look at the replace method in the Java API.  We see that it returns a String and that it requires two parameters: the character you want to replace and the new character.  Note there are two versions of replace: the first takes parameters of type char (e.g., 'a' or 'e').and the second takes parameters of type CharSequence which includes String literals (e.g., "a" or "abc").  In the code below, we will use the char version.

 

Returns a String             Method name and two formal parameters   

  

 

As with the other String methods, the replace method does not modify the String object it. Instead it returns a new String object in which the new character has replaced the old character.  

 

•        First, use the replace method to change all of the a's to underscores. The following line of code replaces the ‘a’ characters of the message value and then assigns the new version of message to result.  Again, note that message itself remains unchanged.

  

 

Now complete the translation by converting the remaining vowels to underscores for outputType equal to 4 .

  

 

•        Now use the substring method and length method to extract a part (or substring) of the message.  

Returns a String             Method name and formal parameters

  

 

Recall the characters in a String are indexed beginning at 0. Since the requirement here is for result to include everything except the first and last character, use the second version of

substring with 1 as the parameter beginIndex and message.length() - 1 as endIndex.

  

 

•        Finally, if the user enters a number other than 1, 2, 3, 4, or 5, result should be set to an error message.

  

 

•        Test your program under each of the conditions.

 

Original ("as is"): For the message, enter: “     This is a test.”   Be sure to include the five leading spaces, but omit the quotes.  For choice, enter 0.

Type in a message and press enter: 

         This is a test.         

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 0 

 

     This is a test.         

 
 

Trimmed: Use the Up arrow key to retrieve the message you entered above, then for choice, enter 1.  Notice the five leading spaces have been removed in the output.  

Type in a message and press enter: 

         This is a test.         

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 1 

 

This is a test.  
 

Lower case: Use the Up arrow key to retrieve the message you entered above, then for choice, enter 2.  Notice the output is in all lower case but the five leading spaces have not been removed.

Type in a message and press enter: 

         This is a test.         

 

Output types: 

   0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 2 

      this is a test.         

 
 

Upper case: Use the Up arrow key to retrieve the message you entered above, then for choice, enter 3.  Notice the output is in all upper case but the five leading spaces have not been removed.

Type in a message and press enter: 

         This is a test.         

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 3 

 

     THIS IS A TEST.         

 
 

Vowels replaced: Use the Up arrow key to retrieve the message you entered above, then for choice, enter 4.  Notice the output has the vowels removed but the five leading spaces have not been removed.

Type in a message and press enter: 

         This is a test.         

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 
   5: Without first and last character 

Enter your choice: 4 

 

     Th_s _s _ t_st.         

 
 

Without first and last character: For the message, enter “This is a test.”  This time do not enter any leading or trailing spaces.  Then for choice, enter 5.  Notice the output has the first and last character removed.

Type in a message and press enter: 

    This is a test. 

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 5 

 

his is a test 

 
 

Invalid input:  

Type in a message and press enter: 

    This is a test. 

 

Output types:    0: As is  

   1: Trimmed 

   2: lower case 

   3: UPPER CASE 

   4: v_w_ls r_pl_c_d 

   5: Without first and last character 

Enter your choice: 7 

 

Error: Invalid choice input. 

 
 

•    Try the input "A message" for the message and choose to replace all vowels with underscores. What do you notice about the output? Why do you think that one of the vowels was not replaced? How would you go about correcting the issue? You don’t have to make this change - - just think about it. 

More products