CS 1043 Lab 11: Childrens Topics Covered In This Lab IO And
Cs 1043 Lab 11childresstopics Covered In This Lab Io And Exceptionsw
CS 1043 Lab 11 Childress Topics covered in this Lab: IO and Exceptions. Write a program described on the next page of this document. You must use a Java IDE to complete this assignment. Use “IOLab.java†to do the following: 1. Your finished program must exclude all the throws IOException lines. Remove the throws IOException from the readData first. Don't remove the throws IOExcpetion from the main method until the program is completed and everything else working. 2. Include competent exception handlers for the readData and writeData methods in this assignment. 3. See the example IO programs posted on the course web-site for assistance. 4. The file to read is located on the last page of this document. Copy and paste the data on the last page into a simple text file. 5. The first two entries in the file represent: 1) the number of rows, 2) and the number of columns. Read the remaining numbers from the input file data and fill a two-dimensional array by rows. For example, if the number of rows is 6 and the number of columns is 4, then the next 4 numbers in the input file make up the first row of numbers in your 2-D array. Use a nested loop to fill the 2-D array. 6. Write an instance method and name it toString. This method will return a string containing the 2-D array as a table. Use the String.format( ) method to build your string. The decimal point should be aligned in each column of output. 7. Prompt the user for an output file-name to write. Write the number of rows and columns into the first two lines of the file. The output file name must be different than the input file name. 8. Write the contents of your 2-D data array into the file using the printf method to format your values in a row-column format using format elements. The decimal point should be aligned in each column. Write the data with four digits to the right of the decimal. a. Write your name into the data-file. Submit your data-file to the lab instructor along with your program assignment. 9. Add logic to recover from an exception in the readData method. If the user enters a non- existent file name, prompt the user for another file name.
Paper For Above instruction
This lab assignment focuses on practicing Java's Input/Output (IO) operations combined with exception handling techniques. The goal is to read numeric data from a file, store it in a two-dimensional array, and then output the data to another file with formatted alignment. Alongside, robust exception handling is essential to manage potential errors such as nonexistent input files or IO failures, reinforcing the importance of writing resilient Java programs.
The program, named IOLab.java, begins execution with the main method that calls readData() to read numeric data from a file specified by the user. The program then outputs the data in a formatted table and prompts the user for an output file name to save the processed data. To accomplish this, the program uses Scanner for reading user input, File and Scanner for reading from files, and PrintWriter for writing to output files.
The input file's first two integers denote the number of rows and columns, respectively. Following this, the remaining values are read and stored into a two-dimensional array. The dimensions (rows and columns) are critical for creating and filling the array correctly. If the user enters a filename that does not exist, the program should prompt for a new filename, utilizing exception handling to trap FileNotFoundException and guide the user towards correction without terminating unexpectedly.
The toString() method constructs a string representation of the array formatted as a table, with decimal points aligned across columns. String.format() is used with proper formatting specifiers to ensure numeric data is aligned in columns with four decimal places, offering a clear, professional output.
When writing data to the output file, the program writes the number of rows and columns initially, then proceeds to print the array data in a formatted, row-column structured layout. Each value is formatted to have four decimal places, ensuring precision and alignment. The program also writes the author's name into the output file, providing identification. After outputting all data, the program closes files to prevent resource leaks.
Exception handling throughout ensures the program remains robust. In readData(), if the file cannot be found or opened, the user is prompted repeatedly until a valid filename is provided. Likewise, IOExceptions during reading or writing are caught, and appropriate messages guide the user, maintaining program stability.
Overall, this assignment emphasizes the integration of file IO and exception handling in Java, fostering skills in creating user-friendly, error-resistant applications that manage external data effectively while ensuring proper formatting and resource management.
References
- Deitel, P., & Deitel, H. (2014). Java: How to Program. Pearson.
- Horstmann, C. (2018). Core Java Volume I--Fundamentals (11th Edition). Pearson.
- Gosling, J., Joy, B., Steele, G., & Bracha, G. (2014). The Java Language Specification, Java SE 8 Edition. Addison-Wesley.
- Java Documentation. (2023). Handling Exceptions. Oracle. https://docs.oracle.com/javase/tutorial/essential/exceptions/
- String.format() Method. (2023). Oracle Java Documentation. https://docs.oracle.com/en/java/javase/17/docs/api/java/lang/String.html#format(java.lang.String,java.lang.Object...)
- Effective Java (3rd Edition). (2018). Joshua Bloch. Addison-Wesley.
- Java IO Basics. (2023). Oracle Java Tutorials. https://docs.oracle.com/javase/tutorial/essential/io/index.html
- Exception Handling in Java. (2022). Baeldung. https://www.baeldung.com/java-exceptions
- File Handling in Java. (2023). JournalDev. https://www.journaldev.com/711/java-fileexample-read-and-write-file
- Java Programming: Principles and Practice. (2016). K. N. King. Cengage Learning.