Starting from:

$25

COMP1213 - Activity - Using Classes and Objects - Solved

Terminology: 

new operator

class object

reference variable instantiation null

method static method constructor
dot (.) operator parameter return alias

garbage collection String class immutable Java API package
import declaration

Random class

Math class

NumberFormat class DecimalFormat class wrapper class autoboxing unboxing
 

Course Coding Standard Additions 

•       When using the import declaration, import classes from the same package individually (i.e. do not use the * notation).  For example, you should have import java.util.Scanner and import java.util.Random rather than having import java.util.* which imports everything in the java.util package. 

•       When making calculations with π, use Math.PI rather than the literal value such as 3.141. You should always use constants when they are available.  

•       From now on, when using a Scanner object to scan input (e.g., from the keyboard), use nextLine to get numerical input from the user and convert it to a number using methods from the corresponding wrapper class.   

For example to read in an int value from Scanner userInput:   

      int myInt = Integer.parseInt(userInput.nextLine()); 

For example to read in a double value from Scanner scan:   

      double myDouble = Double.parseDouble(userInput.nextLine()); 
 

 

             

 Goals:

By the end of this activity you should be able to do the following: Ø Understand how to instantiate an object using the new operator

Ø  Use the Java standard library (the Java API) to look up a class

Ø  Use the String class and the Scanner class

  

 

Program Description: 

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 lower case 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 lower case 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 lower case 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. 

 

Web-CAT – After you have completed the program in this activity, you should submit your

MessageConverter.java file to Web-CAT.  This is the only file you will submit for this activity. 

             

 of 11    
Using the Viewer Canvas with your MessageConverter program  

 

Now we want to create a viewer canvas so we can see what is happening in the MessageConverter program step-by-step as it runs.  Note that we could have done this step as soon as the variables had been declared above.  For your project assignments, you should consider creating a canvas at any point that you need insight into exactly what is happening in your program.   

 

•       With MessageConverter.java in the CSD edit window, click the Run in Canvas button   on the toolbar.  In the CSD window, you should see that the program is stopped at the first executable statement, and you should see an empty viewer canvas window as shown below.   

  

 

•        As indicated by the message in the canvas, click the debug step button  to execute the statement to create a Scanner object on System.in which is the keyboard.  You should now see the variable name userInput in the Debug tab.  Now click the step button   three more times so that you see message, result, and outputType in the Debug tab.  Now drag each of these variables into the canvas window and arrange them as shown in the figure below.  Click the Save button

 

   

 of 11    
•     Now click the play button  , and your program will begin auto-stepping until is pauses waiting for input.  After you enter the requested input each time, the viewers on canvas will be updated.  The run in canvas will continue until your program terminates at which time the viewers on the canvas will display their last values as shown in the example below for message “This is a test” and outputType = 4.

 

  

 

 Although we created the canvas for the MessageConverter after the program was completed, it would have been even more appropriate to create the canvas as the program was being coded, especially if you were encountering logical errors in your program. For example, if you sense that an error has occurred at particular place in the program, you should set a breakpoint on the statement where you want to examine the behavior.  [To set a breakpoint, move the mouse over the margin to the left of the statement until you see the red octagon (stop sign symbol) and then left-click the mouse.]  Then instead of clicking the Run in Canvas button   on the toolbar, click the Debug button  .  The program will run to the breakpoint and stop.  You can single-step   (or perhaps step-in  ) and observe the variables in the canvas and debug tab to help determine the nature of the error so that you can correct it.

   

More products