Answer: Provided in the explanation section
Explanation:
Code provided below in a well arranged format
inputNos.txt
70
95
62
88
90
85
75
79
50
80
82
88
81
93
75
78
62
55
89
94
73
82
___________________
StandardDev.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StandardDev {
public static void main(String[] args) {
//Declaring variables
String filename;
int count = 0, i = 0;
Scanner sc1 = null;
int min, max;
double mean, stdDev;
int nos[] = null;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter the name of the filename :");
filename = sc.next();
try {
//Opening the file
sc1 = new Scanner(new File(filename));
//counting no of numbers in the file
while (sc1.hasNext()) {
sc1.nextInt();
count++;
}
sc1.close();
sc1 = new Scanner(new File(filename));
//Creating an Integer based on the Count
nos = new int[count];
//Populating the values into an array
while (sc1.hasNext()) {
nos[i] = sc1.nextInt();
i++;
}
sc1.close();
//calling the methods
min = findMinimum(nos);
max = findMaximum(nos);
mean = calMean(nos);
stdDev = calStandardDev(nos, mean);
//Displaying the output
System.out.println("Read from file :" + count + " values");
System.out.println("The Minimum Number is :" + min);
System.out.println("The Maximum Number is :" + max);
System.out.printf("The Mean is :%.2f\n", mean);
System.out.printf("The Standard Deviation is :%.2f\n", stdDev);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//This method will calculate the standard deviation
private static double calStandardDev(int[] nos, double mean) {
//Declaring local variables
double standard_deviation = 0.0, variance = 0.0, sum_of_squares = 0.0;
/* This loop Calculating the sum of
* square of eeach element in the array
*/
for (int i = 0; i < nos.length; i++) {
/* Calculating the sum of square of
* each element in the array
*/
sum_of_squares += Math.pow((nos[i] - mean), 2);
}
//calculating the variance of an array
variance = ((double) sum_of_squares / (nos.length - 1));
//calculating the standard deviation of an array
standard_deviation = Math.sqrt(variance);
return standard_deviation;
}
//This method will calculate the mean
private static double calMean(int[] nos) {
double mean = 0.0, tot = 0.0;
// This for loop will find the minimum and maximum of an array
for (int i = 0; i < nos.length; i++) {
// Calculating the sum of all the elements in the array
tot += nos[i];
}
mean = tot / nos.length;
return mean;
}
//This method will find the Minimum element in the array
private static int findMinimum(int[] nos) {
int min = nos[0];
// This for loop will find the minimum and maximum of an array
for (int i = 0; i < nos.length; i++) {
// Finding minimum element
if (nos[i] < min)
min = nos[i];
}
return min;
}
//This method will find the Maximum element in the array
private static int findMaximum(int[] nos) {
int max = nos[0];
// This for loop will find the minimum and maximum of an array
for (int i = 0; i < nos.length; i++) {
// Finding minimum element
if (nos[i] > max)
max = nos[i];
}
return max;
}
}
_____________________
Output:
Enter the name of the filename :inputNos.txt
Read from file :22 values
The Minimum Number is :50
The Maximum Number is :95
The Mean is :78.45
The Standard Deviation is :12.45
cheers i hope this helped !!!