I308 C307 Data Representation

I308 C307 Homework 12

Due Date: Wednesday, April 20, 2022.

In this homework, you will write a small program that reads some information from a file and outputs something based on that.

Ex. 1. List of States

Create a Java project called hw12 and a class inside called StatesFile that contains the main method. This program will only involve this one class.

a. Static Array

Declare a static array in the class of type String called states and containing the names of all the states in the country, with just the first letter capitalized. Declare a second boolean array of the same size called check. Initialize all the elements of this array to false.

b. File Read

Write a static function called readFile that takes one parameter called name of type String. This function should open a file by that name, check if it was opened properly, and if yes, declare a Scanner that can input from it. You can look at the function readStopList from Homework 10 for an example.

Then you should have a loop that reads one line at a time from the file while the scanner still has a next, meaning while we haven't reached the end of the file. Then for each line, you should have a loop that goes through the state names array, and if the line contains that state name, then set check[i] to true, where i is the index of that state name. This will verify which of the states are present in the file and which ones are not. At the end of the function, close both the scanner and the file.

c. Name Check

Write another static function called nameCheck with no parameter. It should have a loop that goes through the check array and verifies which elements are still equal to false. For each of them, output a message that says that the state with that name is not in the file. After the loop, if all the elements of the array are equal to true, output a message saying that all the states are present in the file.

d. Main

In the main, declare a String for a file name, then ask the user to enter it. Call the function readFile with that name, and the function nameCheck after that.

Test the program with the following two files. The first one contains all the state names. The second one is missing some of them.

statesInfo.txt

zipCodes.txt

Homework Submission

Upload the file StatesFile.java to Canvas - Assignments - Homework 12.