CSE 271 Lab Assignment 02 File IO, Try-Catch Block, Random Numbers solution




5/5 - (3 votes)

In this lab you will create a well-commented java project that will contain a class name FileIO. At
first, create a project in Eclipse called Lab2 and then create a new class, FileIO in the src folder.
Include the main() method in the class FileIO. You must do the following in your main method
step by step: [Before you start, please review the code we worked together in the lecture. It’s
available on Canvas in the module section.]
• Arrays and File IO
1. Take an integer input, N from the keyboard (user).
2. Create a file (through code) called “numbers.txt”.
3. Now write N random integers (between 1 and 100) to the file (“numbers.txt”).
4. Close numbers.txt file.
5. Create an array of integers of size N.
6. Read all the numbers from the numbers.txt file and store it on the array.
7. Create another file called “output.txt”.
8. Write all the numbers from the array to the file output.txt with a header “Numbers:”.
9. Sort the array using Arrays.sort() method.
10. Write (append to output.txt file) all the numbers from the sorted array to the file again,
now with the header “Sorted Numbers:”.
11. Iterate through the array and count the total number of Odd and Even numbers.
12. Write (append to output.txt file) number of Odd and Even numbers.
13. Iterate through the array and the find smallest and largest numbers.
14. Write (append to output.txt file) the smallest and largest numbers.
15. Find the mean and write (append) to output.txt file with a header.
16. Close the output.txt file.
Hints: Use PrintWriter class to write data to the file and Scanner class to read data from the file.
Use try-catch block to handle exception related to file operations.
Grading Rubric:
Input from Keyboard 5
Create file numbers.txt 5
Generate N random integers 10
Write random integers to the file 5
Create array of N integers 5
Read numbers from the file numbers.txt 10
Store numbers on the array 5
Create output.txt file and write numbers from the array to the file 10
Sort numbers and write (append) sorted numbers to the file (output.txt) 10
Count number of odd and even numbers 5
Write (append) number of odd and even numbers to the file (output.txt) 5
Find smallest and largest numbers 5
Write (append) smallest and largest numbers to the file (output.txt) 5
Compute mean 5
Write mean to the file (output.txt) 5
Close all the files 5
Total 100
Output doesn’t follow sample run format -5
Not well commented [write a sentence for each step] -5
Lab Assignment 02, Object-Oriented Programming, CSE 271, Spring 2020
Department of Computer Science and Engineering, Miami University
File IO, Try-Catch Block, Random Numbers
Important Note:
You must follow the sample run for the formatting your input and output. Make sure the file name
is correct and code is well commented. No late submission. You will get zero for late submission.
Submit ONLY java file to the appropriate submission folder on the Canvas by the due time.
Sample Run:
Enter an integer, N :10
[33, 6, 84, 77, 68, 83, 6, 60, 49, 3]
Sorted Numbers:
[3, 6, 6, 33, 49, 60, 68, 77, 83, 84]
Number of odd numbers: 5
Number of even numbers: 5
Smallest number: 3
Largest number: 84
Mean: 46.9