Creating the input data file: You will need to create an input text file of data called
USCOVID19Data.txt
that will be in the form shown below. I will give you a partial data file to start with but you must add more data to the text file as described below. The input text file will have multiple lines and each line will look like:
state_abbreviation COVID-19_cases state_population
where
state_abbreviation
is the two letter code for any US state or US territory
COVID-19_cases
is the number of reported cases of COVID-19 in that state on the day you pick
state_population
is the population of the US state or US territory you abbreviated
Example data would be:
TX 2552 28995881
TN 1537 6829174
PR 127 3193694
The
COVID-19_cases
value should come from the CDC.gov website. (The values above are from Mar 30. You should update all your COVID-19 data to the same date)
The
state_population
should come from census.gov/quickfacts and should use the estimated 2019 population (V2019)
Your file will have the 3 lines above and then you must add at least 7 more lines of data pulled from the CDC and the Census data. You will end up with a file with 10 lines of data. Other than TX and TN, none of your other 7 states should start with the same letters. This means that you can’t use AL and AK and AR and AS and AZ – only one of those at most.
Writing the program to read input: After creating the data file, you are now going to write the first part of your program
XYZ1234Lab4Part1a
to read the data from the file. For Part1a you are just going to focus on reading the data from the file and printing it to verify that is was read correctly.
First, you need to declare three variables, a String, and two doubles. The String will hold the state abbreviation and the doubles will hold the COVID-19 value and the population value. Next, you need to declare a scanner and connect it to the input file as we have done before. If the file is not found, then connect the scanner to the keyboard and tell the user the data file was not found.
Next, have a formatted print statement that prints a header for a table. The table will be the values you print after you read the file. Your table header should be printed so that the first column is the string “ST” printed in two spaces, then the string “Cases” printed in 10 spaces, then the string “Population” printed in 15 spaces.
Once you have the input file scanner set up and the table header printed, then you need to set up a loop that will read all the lines of data in your text file. Do not set up a counted loop because the file the TA grades with may have more or less lines of data than your file. Use a loop test that checks to see if there is another line of data in the file. Also check to make sure that the data file exists, i.e. is found, before trying to read from the file.
Inside the loop, read one String, one double for COVID-19, and one double for population storing each value into the appropriate variable. DO NOT read all input as strings and convert the strings. After reading in the three values from the line, print out the three values in the following format where the String is printed in 2 spaces, the COVID double is printed in 10 spaces and the population is printed in 15 spaces. Use the formatted output statement to do this.
After you have printed the values from one line, your program should be at the bottom of the loop and then go back to the top of the loop to see if there is another line to read.
After all the lines have been read and printed, your program should print a final message indicating that all data has been read.
Output sample - indented to help show format. No indentation required:
ST Cases Population
TX 2552 28995881
TN 1537 6829174
PR 127 3193694
All data has been read from USCOVID19Data.txt
Make sure that all output data is labeled so that a user would know what is represented by each set of data shown in the output. Make sure that your program runs completely without errors and the correct output is produced. Once your program is working correctly, save your working program as
XYZ1234Lab4Part1a.java
and save the input file you created to use with the program.
[Don’t forget to put a note in your answer file for this question reminding the grader to look for the .java file and the input file
USCOVID19Data.txt
{17 points}
Rubric:
Declare all needed variables {1 pt}
Correctly implement and connect the File variable to the input file {1 pt}
Using a try-catch, correctly connect the Scanner variable to the File variable {1 pt}
Using a try-catch, correctly handle the exception as defined above if the file is not found {1 pt}
Correctly prints table header as described {1 pt}
Correctly insure that the file exists before trying to read values from the file {1 pts}
Correctly reads String, double, double from one line of the input file {3 pts}
Correctly outputs formatted String, double, double data for the table {2 pt}
Correctly reads all lines of the input file {2 pts}
Prints ending message {1 pt}
Correct format and acceptable content in input file USCOVID19Data.txt {3 pts}