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

Write a class with a constructor that accepts a file name as its argument. Assum

ID: 3554571 • Letter: W

Question

Write a class with a constructor that accepts a file name as its argument. Assume the file contains a series of numbers, each written on a separate line. The class should read the contents of the file into an array, and then displays the following data.
* The lowest number in the array
* The highest number in the array
* The total of the numbers in the array
* The average of the numbers in the array.

The file, Numbers.txt used for the above program contains these twelve numbers:
8.71
7.94
3.01
29.27
9.23
82.76
12.6
47.99
63.89
1.09
22.23
79.17

Explanation / Answer

-----------------------------------------------------------------------------------

The Driver Class:

-------------------------------------------------

import java.io.IOException;
import java.util.Scanner;

public class Driver {
  
    public static void main(String args[]){
        try{
        System.out.println("Please Enter th filename:");
        Scanner sc1=new Scanner(System.in);
        String filename=sc1.nextLine();                             //takes in the user input for the filename
        System.out.println(" ");
        Array1 arr=new Array1(filename);}
        catch(IOException e){                                          //Exception if file name entered is wrong , i.e. file does not exists
            System.out.println("File Not Found. Please check the filename entered");
        }
    }
  
}

-----------------------------------------------------------------------------------

The Class Which scans the file:

-------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class Array1 {
    Double[] array=new Double[200];                              //initializing the array of size 200
  
    public Array1(){                                            //constructor without filename
        System.out.println("Please enter a filename and run again");
    }
  
    public Array1(String filename) throws FileNotFoundException, IOException{
      
        Scanner s = new Scanner(new File(filename));
        int i=0;
      
        while (s.hasNext()){                                    //till there are lines in the file keeps scanning
            array[i]=Double.parseDouble(s.next());              //s.next() returns the next string in the file, this string is parsed to a double array            i++;
        }
        computeArray();           
     }
  
    //Finds the highest,lowest,sum and average of all the elements in Double array
    private void computeArray(){
        double min,max,sum = 0;   
        min=array[0];                 //initializing the minimum element equal to the first element of the array
        max=array[0];                //initializing the maximum element equal to the first element of the array
        for(int i=0;i<array.length;i++){
            if(min>array[i])
                min=array[i];
            if(max<array[i])
                max=array[i];
            sum+=array[i];            //keeps adding array[i] to the sum as i keeps incrementing
        }
        System.out.println("The Lowest number in the array is: "+min);
        System.out.println("The Highest number in the array is: "+max);
        System.out.println("The total of the numbers in the array is: "+sum);
        System.out.println("The average of the numbers in the array is: "+sum/array.length);    //average = (sum of element)/no of elements
      
    }
  
}

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