Question: JAVA QUESTION Calculating the average of a file All exercises in this assignment must be placed in a single java file, and This will make

JAVA QUESTION Calculating the average of a file

All exercises in this assignment must be placed in a single java file, and This will make a menu system that allows the user to perform three different actions. The following actions are: 1. Calculate average from a file

2. Save calculated average to a file

3. Display contents of a file

4. Stop the program

Example: 1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit ->

5 Input not understood.

1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit ->

4 Exiting.

To begin: 1. Use a while loop that allows the user to input a choice from 1-4, and use a switch block to choose the command to execute. After the command is performed, the program should display the menu again for the next command. If the user inputs a number not from 1-4, the program should print an error message and ask for input again.

2. To calculate the average from a file, you should a. Ask the user to input a filename to read from using the next() method from Scanner(System.in). b. Open the file as a FileInputStream. When opening the file, you must be sure to catch the FileNotFoundException exception using a try block. c. Create a Scanner for the opened file. d. Continously read floats in a while loop using the nextFloat() method. The loop shall terminate once it has caught the NoSuchElementException using try and catch blocks. e. After the values have been read, the opened file should be closed using the close() method, where you must catch the IOException exception. f. Calculate the average by summing up the floats and dividing by the total number of floats read. Be sure to keep the average in case the user wants to save it to a file. Handle the exceptions appropriately by printing a message indicating if an error occured (you may use your own message or the getMessage() method of the exception for this).

Example: 1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 1 Please enter name of file: badfilename.in badfilename.in (No such file or directory)

1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 1 Please enter name of file: file.in File found and loaded. Reading value 5.0 Reading value 2.5 Reading value 4.3 Reading value 2.3 Reading value 4.4 The mean is 3.7000 1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 4 Exiting.

3. To save the calculated average to a file, you should a. Determine if you have a stored average value. If not, do not attempt to open any files or save any data. b. Ask the user to input a filename to write to using the next() method from Scanner(System.in). c. Open the file as a PrintStream; you may use the familiar print/f/ln statements to save the average. When opening the file, you must be sure to catch the IOException exception using a try block.

Example: 1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 2 No calculated mean value to save.

1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 1 Please enter name of file: file.in File found and loaded. Reading value 5.0 Reading value 2.5 Reading value 4.3 Reading value 2.3 Reading value 4.4 The mean is 3.7000 1. Calculate average of a file 2. Save average to a file 3. Display contents of a file 4. Quit -> 2 Enter name of file to save: mean.out Mean saved successfully. 1. Calculate average of a file 2. Save average to a file 3. Display contents of a file 4. Quit -> 4 Exiting. 4. To display a file, you should a. Ask the user to input a filename to read from using the next() method from Scanner(System.in). b. Open the file as a FileReader. When opening the file, you must be sure to catch the FileNotFoundException exception using a try block. c. Use a while loop to continuously read the file using the read() method. Make sure to exit the loop if the returned value is -1. If the returned value is not -1, convert it to a char and print it to the screen. Be sure to catch the IOException exception when using the read() method. d. After file has been read and printed to the screen, it should be closed using the close() method, where you must catch the IOException exception. e. The program must print the total number of lines and characters in the file. Count lines by checking if read() returned . Make sure to not count as a character. The returned value is a character otherwise.

Example: 1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit ->3 Please enter name of file: badfilename.in badfilename.in (No such file or directory)

1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 3 Please enter name of file: file.in 5. 2.5 4.3 2.3 4.4 Number of lines: 5 Number of chars: 14 1. Calculate average of a file 2. Save average to a file 3. Display contents of a file 4. Quit -> 3 Please enter name of file: quote.txt WE LEARN WISDOM FROM FAILURE MUCH MORE THAN FROM SUCCESS. WE DISCOVER WHAT WILL DO BY FINDING OUT WHAT WILL NOT DO... AND HE WHO NEVER MADE A MISTAKE NEVER MADE A DISCOVERY Number of lines: 3 Number of chars: 170

1. Calculate average of a file

2. Save average to a file

3. Display contents of a file

4. Quit -> 4 Exiting.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!