Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Foundations The objective is for you to use Exceptions, file input/output, and 2D arrays. You must catch all thrown exceptions. You are being provided

Java Foundations

The objective is for you to use Exceptions, file input/output, and 2D arrays. You must catch all thrown exceptions. You are being provided with a class (DataFileFactory) that will generate text files with numbers in them. Further, this class will introduce some errors in these files which you will to detect and discard. You will need to read these files into a 2D array, print that array, and sum the columns.

You will create a class Main that will contain all the code necessary to execute the behavior listed below. You must have a public static void main(String[] args) method in this file.

Expected Program Behavior

1. Ask the user for the number of files to create and the number of data points in each file. You should read them from System.in. If they do not type an integer, your program must display a warning (e.g. That is not a number.), and ask for the number again.

2. Create an object of type DataFileFactory. Use this objects writeFiles() method to create the data files. The argument to this method is the probability of errors on each line. It should be a value between 0 and 1. For debugging, you may use the method without argument, or with a value of 0.0. However, before you turn in this project you must use a error rate of 25% ( writeFiles(0.25)).

The data files created by DataFileFactory contain numbers (integers), one per line. There will be as many numbers as the elementsPerFile argument to the constructor. It will create as many files as the numberOfFiles argument to the constructor.

3. Create a 2D array of ints. It should be of size number of files by number of data points in each file.

4. Read in all data files into the 2D array. Each row of your 2D array should be filled with the integers from one data file. Use the getFileName(int fileNumber) to get the name of each of the data files generated in step 2. You should also expect that the data in the data files will have some errors. Thus, when you read a line, you need to convert that line to an int and catch any errors. If the line is not an integer, skip that line.

5. Print out the 2D to the screen. It must be printed out with all the values of each row on a line separated by commas.

6. Sum each column of the array and print the resulting value to the screen. An example of how to do this with a 3x3 array is:

 #5 printing array: 16, 95, 57 59, 48, 95 17, 36, 81 #6 Sum of each column: Column 0 has sum=92 Column 1 has sum=179 Column 2 has sum=233 

7. Remove the data files with the DataFileFactorys method removeFiles();

DataFileFactory.java

package edu.unca.csci202;

import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.Random;

public class DataFileFactory { private int numberOfFiles; private int elementsPerFile; private Random rand; public DataFileFactory(int numberOfFiles, int elementsPerFile) { this.elementsPerFile = elementsPerFile; this.numberOfFiles = numberOfFiles; this.rand = new Random(); } public String getFileName(int fileNumber) throws IOException { if(fileNumber >= 0 && fileNumber < this.numberOfFiles ) { String fileName = "data_file_"+ fileNumber +".txt"; return fileName; } throw new FileNotFoundException("index out of range"); } public void writeFiles() throws IOException { writeFiles(0.0); }

public void writeFiles(double errorRate) throws IOException { for(int i = 0; i< this.numberOfFiles; i++) { // open the file for writing String fileName = "data_file_"+ i +".txt"; System.out.print("writing file "+fileName+" "); FileOutputStream fos = new FileOutputStream(fileName); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); // count how many error were generated int errorCount = 0; for(int j=0; j< this.elementsPerFile; j++) { // check if we should write an error to the file float probabilityOfError = this.rand.nextFloat(); if( probabilityOfError < errorRate ) { bw.write(generateRandomString(5)); bw.newLine(); errorCount++; } // write a random number to the file int randNumber = this.rand.nextInt(100); bw.write( Integer.toString(randNumber) ); bw.newLine(); } // close the file bw.flush(); fos.close(); System.out.print("\t wrote " + this.elementsPerFile + " number to file, "); System.out.println("and " + errorCount + " errors."); } } private String generateRandomString(int length) { String returnString = ""; for(int i=0; i

public void removeFiles() { for(int i = 0; i< this.numberOfFiles; i++) { String fileName = "data_file_"+ i +".txt"; File file = new File(fileName); if(file.delete()) { System.out.println(fileName+" deleted"); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions