Java work:Need in 24 hours
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.
import java.util.*;
import java.io.*;
public class IOLab
{
private double [][] data; // rows = data.length;
// cols = data[i].length;
public static void main( String [] args )
throws IOException
{
IOLab fileTest = new IOLab();
fileTest.readData();
System.out.println( fileTest.toString() );
fileTest.writeData();
}
public void readData()
throws IOException
{
Scanner console = new Scanner( System.in );
System.out.print( "Please enter a file name to read: " );
String inFile = console.next();
File file = new File( inFile ); // connect to the file
Scanner tokens = new Scanner( file ); // buffer the data
int nrows = tokens.nextInt(); // get the number of rows.
// 1. get the number of cols.
// 2. allocate space for the 2-d data array instance field:
// 3. write a nested loop to read the data into the data array.
// 4. close the file:
} // end readData()
public void writeData()
throws IOException
{
// 1. prompt the user for a file name to write.
// 2. Allocate the file and allow buffering with printf
// 3. write the number of rows into the file
// 4. write the number of cols into the file
// 5. Format the output using format-elements, numbers should be
// aligned on the decimal point and rounded to 4 digits to the
// right of the decimal.
// 6. use a nested loop to write data stored in the data array.
// 7. Write your name into the file.
// 8. close the file.
} // end of writeData
} // end class
For your reference: Contents of file, twodim.txt follows:
6
4
45.27
89.1
-65.33333
12.0
94.975
-27.678
97.21
-42.3
33.81
-89.8
-123.34
45.6
73.482
18.112
9.7
41.43
-67.545454
73.86
-19.09
5.555
29.3133
-49.7
80.18181
40.229