Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Not sure how to write this code in Java. Introduction This lab builds upon lab 3, using if-else and other branching statements to classify short

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Not sure how to write this code in Java.

Introduction 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 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 . o You should declare the following variables in your program: catString: String // The raw text of the message's category payload: String // The primary content of the message latitude: double // The latitude indicated in the message o 0 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? south double 39.882343 // southernmost latitude north double 40.231315 // northernmost latitude west double -105.743511 // westernmost longitude east double -104.907864 Il easternmost longitude 4. After the variables have been declared, write a statement to prompt the user with the following message: O O o o Please enter a formatted message: o 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: 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. 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 MessageCategory.INFO. Otherwise, category should be assigned the value MessageCategory. UNKNOWN. When comparing the strings, you should use the equals IgnoreCase 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. o o I. If latitude ? south and latitude s north. II. If longitude west and longitude S 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 10 (input/output) that does not match our instructions or examples exactly (unless otherwise stated in an assignment's instructions). Your program's 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 deadline's 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" 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 Be careful of a combination of tabs and spaces in your files (use one or the other)! . . o O Sample Input 1. smoke 40.499812 -105.012075 its raining ash 2. offer 40.022 -105.226 free essential supplies 4 evacs pets, 2323 55th st, boulder 3. structure 40.029854 -105.391055 damaged: 224 left fork road (shed) (house okay) 4. wind 40.3523 -105.2045 just switched, and now the smoke is thick around our house 5. fire 40.0515 -105.332 firefighter sees a glow: sw from west coach rd toward sunshine canyon 6. structure 40.050904 -105.373941 damaged: unknown number, by 204 gold run road 7. evac 40.383 -105.113 overflow shltr 4 evacuees at walt clark middle school, loveland 8. nofire 40.367 -105.292 activity: west of the pinewood res dam 9. photo 40.052304 -105.319374 local: wild horse 10. info 40.499812 -105.012075 its raining ash: windsor, co 11. need 40.011471 -105.28638 volunteers sun 8a-10p donation ctr 2 help w/donations for victims & firefighters: 3111 28th st 12. need 40.031131 -105.259259 people to help sort donations Sept 12 8am-9pm 3111 28th st 13. open 40.072208 -105.354437 evacuees can return to homes: lee hill 14. open 40.076431 -105.309757 lee hill dr. Sample Input and Output These are sample runs of the program. Input is indicated in red. Your input and output should be consistent with what is shown here except for the line longs of input or output (these long lines word wrap in this document, but in your program, these long lines of input or output should be on a single line). Please enter a formatted message: smoke 40.499812 -105.012075 its raining ash Category: ALERT Raw Cat: smoke Message: its raining ash Latitude: 40.499812 Longitude: - 105.012075 In Range: false Please enter a formatted message: offer 40.622 -105.226 free essential supplies 4 evacs pets, 2323 55th st, boulder Category: OFFER Raw Cat: offer Message: free essential supplies 4 evacs pets, 2323 55th st, boulder Latitude: 40.022 Longitude: - 105.226 In Range: true Please enter a formatted message: structure 40.029854 - 105.391055 damaged: 224 left fork road (shed) (house okay) Category: INFO Raw Cat: structure Message: damaged: 224 left fork road (shed) (house okay) Latitude: 40.029854 Longitude: - 105.391055 In Range: true Please enter a formatted message: wind 40.3523 -105. 2045 just switched, and now the smoke is thick around our house Category: UNKNOWN Raw Cat: wind Message: just switched, and now the smoke is thick around our house Latitude: 40.3523 Longitude: - 105.2045 In Range: false Please enter a formatted message: fire 40.6515 -105.332 firefighter sees a glow: sw from west coach rd toward sunshine canyon Category: ALERT Raw Cat: fire Message: firefighter sees a glow: sw from west coach rd toward sunshine canyon Latitude: 40.0515 Longitude: - 105.332 In Range: true Please enter a formatted message: structure 40.050994 - 105.373941 damaged: unknown number, by 204 gold run road Category: INFO Raw Cat: structure Message: damaged: unknown number, by 204 gold run road Latitude: 40.050904 Longitude: - 105.373941 In Range: true Please enter a formatted message: evac 40.383 - 105. 113 overflow shltr 4 evacuees at walt clark middle school, loveland Category: INFO Raw Cat: evac Message: overflow shltr 4 evacuees at walt clark middle school, loveland Latitude: 40.383 Longitude: - 105.113 In Range: false

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions