This lab builds upon lab 3, using if-else and other branching statements to classify short messages (like tweets) based on keywords in the message. This is a task that might reasonably be done in real-world situations. E.g., certain tweets might contain information (e.g., a request for help, a fire sighted) that must be acted on quickly, and computer programs could be used to identify these based upon the text in the message.
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
- Create a new class called ClassifyMessage (stored in a file called java).
- 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 messages 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 messages category
- 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?
- south double 882343 // southernmost latitude o north double 40.231315 // northernmost latitude o west double -105.743511 // westernmost longitude o east double -104.907864 // easternmost longitude
- After the variables have been declared, write a statement to prompt the user with the following message:
Please enter a formatted message:
- Use the keyboard objects 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.
- 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.
- 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:
- If the value of catString is one of fire or smoke, then category should be assigned the value ALERT.
- Otherwise, if the value of catString is need, then category should be assigned the value NEED. o Otherwise, if the value of catString is offer, then category should be assigned the value MessageCategory.OFFER.
- Otherwise, if the value of catString is one of structure, road, photo, or evac, then category should be assigned the value 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 ==?
- 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.
- If latitude south and latitude north.
- If longitude west and longitude east.
Additional Requirements
These are things that make the graders lives easier, and ultimately, you will see in the real world as well. Remember the teaching staff does not want to touch your code after they gave you requirements; they want to see the perfect results they asked for! Here is a checklist of things you can lose points for:
- (10 points) If the source file(s)/class(es) are named incorrectly (case matters!)
- (10 points) If your source file(s) have a package declaration at the top
- (10 points) If any source file you submit is missing your Statement of Academic Honesty at the top of the source file. All submitted source code files must contain your Statement of Academic Honesty at the top of each file.
- (10 points) If you have more than one instance of Scanner in your program. Your program should only have one instance of Scanner.
- (15 points) Inconsistent I/O (input/output) that does not match our instructions or examples exactly (unless otherwise stated in an assignments instructions). Your programs I/O (order, wording, formatting, etc.) must match our examples and instructions.
- (100 points) If the source file(s) are not submitted before the specified deadlines late period ends (48 hours after the deadline) or if they do not compile.
- (25 points) Late penalties will be deducted as per the course syllabus.
- If your (10 points) comments or (10 points) variables are lacking o Here, lacking means that you or a TA can find any lines of code or variables that take more than 10 seconds to understand, and there is no comment, or the variable name does not make sense (variable names like b, bb, bbb, etc. will almost never be acceptable)
- (10 points) Indentation is not consistent throughout your source code o Refresh your memory of indentation patterns in chapter 2 in the course textbook o Be careful of a combination of tabs and spaces in your files (use one or the other)!
If any of the above do not make sense to you, talk to a TA, or ask on Piazza!
Reviews
There are no reviews yet.