Starting from:

$30

CSI301-Lab 4 Using Enumerations and If-Else statements Solved

In this lab, you will create a Java enumeration called MessageCategory inside of ClassifyMessage that lists several message categories (NEED, OFFER, ALERT, INFO, UNKNOWN). Your program will parse the text of a user specified message, identify the category of the message, and assign this value to a variable (category) declared to be of type MessageCategory (only values from the enumeration can be assigned to the variable). You will also identify the latitude and longitude specified in the message and determine whether these are within ranges defined elsewhere in the program. 

 

Exercise – Using Enumerations and If-Else statements
Since the primary purpose of this lab is not String manipulation, we will use a simpler message format than that found in lab 3. You will still use the Scanner and String classes, however. 

 

You may assume that the messages processed by the program all have the following format: 

 category latitude longitude payload

 

where category is a single keyword indicating the message category (its type in Lab 3), latitude and longitude are both floating point numbers, and payload is a string of text (potentially containing arbitrary characters) constituting the primary body of the message.  For instance, the message  

 

offer 40.022 -105.226 free essential supplies 4 evacs pets, 2323 55th st, boulder

conforms to the above format. Additional sample messages are provided at the end of this document.  You should use them to test your code. 

Instructions  
1.      Create a new class called ClassifyMessage (stored in a file called ClassifyMessage.java). 

2.      Declare the enumeration MessageCategory by adding the following line:

enum MessageCategory {NEED, OFFER, ALERT, INFO, UNKNOWN}

•        Where does this line of code belong?  In the main method?  In ClassifyMessage?  Think about this

•        You should declare the following variables in your program:

o
catString:  
String
// The raw text of the message’s category 
o
payload:         
String
// The primary content of the message 
o latitude:                    double     // The latitude indicated in the message o longitude:          double     // The longitude indicated in the message o isInRange:       boolean // A “flag” indicating whether the latitude and 

                         // longitude values are within bounds o category:    MessageCategory // The message’s category

3.      Additionally, you should declare the following double variables and initialize them to the shown values. These define geographic boundaries that the program uses. Some messages will originate from within those bounds, other messages will not.  Is it a good idea to make these constants?  

o   south               double     39.882343                    // southernmost latitude o north           double     40.231315                    // northernmost latitude o west     double     -105.743511                 // westernmost longitude o east  double           -104.907864                 // easternmost longitude

4.      After the variables have been declared, write a statement to prompt the user with the following message: 

Please enter a formatted message:

5.      Use the keyboard object’s next(),  nextDouble(), and nextLine() methods to read in values for catString, latitude, longitude, and payload. You may assume that the message is entered as a single line of text and formatted as described earlier.

6.      For payload (and catString, if needed) you should trim (using the trim() method of the String class, if needed) any leading and trailing white spaces from the text. 

7.      Use a multi-branch if-else statement to match the value stored in catString to one of the elements of the enumeration  MessageCategory.  The conditions should be the following: 

o   If the value of catString is one of "fire" or "smoke", then category should be assigned the value MessageCategory.ALERT.

o   Otherwise, if the value of catString is "need", then category should be assigned the value MessageCategory.NEED. o Otherwise, if the value of catString is "offer", then category should be assigned the value MessageCategory.OFFER.

o   Otherwise, if the value of catString is one of "structure", "road", "photo", or "evac", then category should be assigned the value MessageCategory.INFO. o Otherwise, category should be assigned the value MessageCategory.UNKNOWN.

When comparing the strings, you should use the equalsIgnoreCase method.  Why use the equalsIgnoreCase method?  Why not use ==? 

8.      Use another if-else statement to determine whether the latitude and longitude specified in the message are within the geographic boundaries indicated by north, south, east, and west. Variable isInRange should be assigned the value true if and only if both of the below conditions are met.

Otherwise, isInRange should be assigned the value false. 

I.            If latitude ≥ south and latitude ≤ north.  

II.          If longitude ≥ west and longitude ≤ east.

More products