Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java class ReadNumbers.java The file will read a text file numbers.txt.

ID: 3567626 • Letter: W

Question

Write a java class ReadNumbers.java

The file will read a text file numbers.txt.

Each line of numbers.txt contains an integer number.

The first line of numbers.txt will contain how many numbers it will contain.

After first line there will be integers at each line. -1 will indicate the end of file.

Example:

10

1

20

37

3

4

23

90

11

13

19

-1

In the above file the first line contains a 10. So there will be 10 lines after the first line. Each of the 10 lines will contain an integer. At the end when -1 is read it is considered as end of file and there is no more input to read.

The program will initialize an array with size equal to the first integer in the file. In our above example it will initialize the array with size 10.

Then it will read each integer after the first line and place it in the array. The array for our above example will be like:

1

20

37

3

4

23

90

11

13

19

The program will then determine the maximum, minimum, average and sum for the integers in the array.

The program then creates another output text file as output.txt

Then it writes the maximum, minimum, average and total into the output.txt file

The format of the output.txt file for our above example will be like:

Maximum: 90

Minimum: 1

Total: 221

Average: 22

Use proper exception handling for files.

1

20

37

3

4

23

90

11

13

19

Explanation / Answer

import java.io.*;
class ReadNumbers{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("numbers.txt");
int i, sum=0;
float avg;
int array = new Int[fr.read()];
while((i=fr.read())!=-1)
{
   for (int k = 0; k< j ;k++)
   {
       j[k]=i;
   }
}
fin.close();
}catch(Exception e){system.out.println(e);}

   for ( int c = 0; c < ( n - 1 ); c++) {
       for (int d = 0; d < n - c - 1; d++) {
       if (array[d] > array[d+1]) /* For descending order use < */
       {
       swap = array[d];
       array[d] = array[d+1];
       array[d+1] = swap;
            }
}

}

for (int a= 0 ;a <i; a++)
{
   sum = sum + array [a];
}
avg = sum/i;
try{

File file = new File("/users/mkyong/output.txt");

   FileWriter fw = new FileWriter(file.getAbsoluteFile());
       BufferedWriter bw = new BufferedWriter(fw);
       bw.write(Maximum: array[0]);
       bw.write(Minimum: array[i-1]);
       bw.write(Maximum: Total: sum);
       bw.write(Minimum: Average: avg);

       bw.close();

       System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       }

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote