Question
In this lab, we are going to explore File IO (Input/Output) with Java as well as handling exceptions with try-catch blocks. We will do this
In this lab, we are going to explore File IO (Input/Output) with Java as well as handling exceptions with try-catch blocks. We will do this by using a random number generator from the Random object. Your job is to create a project in Eclipse called lab2 and a class FileIO in the corresponding src folder. Make sure to click the box to include the main() method when creating the class. Once created, you must edit your class to complete the following steps in order:
**Note: Please create multiple methods. Do not write the entire code in the main method.
1. Take an integer N input from the user via the keyboard. 2. Create a file (through the code) named numbers.txt. 3. Write N random integers (between 1 and 100) to the numbers.txt file. 4. Close the numbers.txt file. 5. Create an integer array of size N. 6. Read the numbers from the numbers.txt file and store them in the integer array. 7. Append the numbers, formatted as an array (see example output), to the numbers.txt file with a header Numbers:. 8. Sort the array using the Arrays.sort() method. 9. Append the sorted numbers, formatted as an array (see example output) to the numbers.txt file with a header Sorted Numbers:. 10. Find the number of Odd and Even numbers in the array. 11. Append the number of Odd and Even numbers to the numbers.txt file. 12. Find the smallest and largest number in the array. 13. Append the smallest and largest number to the numbers.txt file. 14. Find the mean value of the array. 15. Append the mean value with two decimal places to the numbers.txt file. 16. Close the numbers.txt file.
Hints: Dont forget to close your Scanners and Files when you are done using them! Remember what we discussed in class about using FileWriter and PrintWriter for appending. Also, be specific with your try-catch exception handling. Note: you must include comments detailing each major line/step of the code (approximately a sentence each) so that we can follow your code easily. Also, make sure your output format looks exactly like the test runs output (of course you cannot replicate the numbers as they are random).
Test Run: Console: Enter an integer to generate N random numbers: 7 numbers.txt: 76 62 30 11 89 8 7 Numbers: [76, 62, 30, 11, 89, 8, 7] Sorted Numbers: [7, 8, 11, 30, 62, 76, 89] Number of odd numbers: 3 Number of even numbers: 4 Smallest number: 7 Largest number: 89 Mean: 40.43
Task | Grade |
Rubric: Task Grade Take input from the keyboard | 5 |
Create the numbers.txt file | 5 |
Write N random integers to the numbers.txt file | 10 |
Read integers from the numbers.txt file into an integer array | 10 |
Close the numbers.txt file before reusing it | 5 |
Append the formatted array of integers to the numbers.txt file | 10 |
Append the formatted sorted array of integers to the numbers.txt file | 10 |
Append the number of odd and even numbers to the numbers.txt file | 10 |
Append the smallest and largest number to the numbers.txt file | 10 |
Append the mean value to the numbers.txt file | 10 |
Close the numbers.txt file and any scanners | 5 |
Output follows the sample output format | 5 |
Program is well commented | 5 |
Total | 100 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started