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

Write a program that will find the average of a list of numbers found in a file.

ID: 3817433 • Letter: W

Question

Write a program that will find the average of a list of numbers found in a file. Input Validation: No input validation required. Assume the input file will have the correct type of data. Requirements: The program must ask the user for the name of the file. You must have a method with the following header: public static double deviation(File input File) This method will take a File object as input and use File I/O to calculate and return the standard deviation of a series of numbers found inside of a file. You should create your own test files to test the functionality of your program. Sample Execution: Enter the name of the file: my_nuabers.txt The standard deviation of the values in this file is: 78.29

Explanation / Answer

import java.io.*;
import java.util.*;

public class Solution {
  
    public static double deviation(File inputFile) {
        Scanner scanner = new Scanner(inputFile);
        List<Integer> integerList = new ArrayList<Integer>();
        int sum=0;
        while(scanner.hasNextInt())
        {
            integerList.add(scanner.nextInt());
        }
        int n = integerList.size();
        for (int i = 0; i < n; i++)
            sum += integerList.get(i);
        return ((double) sum) / n;
    }

    public static void main(String[] args) {
        System.out.print("Enter the name of the file: ");
        Scanner scanner = new Scanner(System.in);
        String fileName = scanner.nextLine();
        double avg = deviation(new File(fileName));
                             
        System.out.print("The Standard deviation of the values in the file is :{0}" + avg);      
    }
}

Sample Input :

Enter the name of the file: test.txt

The Standard deviation of the values in the file is 50.00

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