I told Woraholic if he wanted to do this it is reserved for him, but I will post here until I hear from him.
Im thinking
1200 fg for this one
We just learned how to implement new methods and arrays so im sure youll use those both.
You will have to use some
● You are to implement the class STDcalculator.
Start with the code from the demo exercise ( this will be labeled further down in this message ) and add the method to calculate standard deviation. This method should be called calculateSTD and it should
take one argument, an array of doubles, and it should return a double which is the standard deviation.● The equation for this calculation is as follows:
● Where n is the number of values, x is the ith value and x bar is the average of all the values.
● Your output should match the following:
*****Code from Demo exercise*****public static double[] loadNumberFromFile(String fileName) throws Exception
{
File inFile = new File(fileName);
Scanner inScan = new Scanner(inFile);
double [] numbers = new double[100];
int numberCounter = 0;
while(inScan.hasNext())
{
numbers[numberCounter] = inScan.nextDouble();
numberCounter++;
}
inScan.close();
return numbers;
}
/**
* This method takes an array of double and returns the average
* @param numbers array of numbers
* @return average of array taken as argument
*/
public static double averageNumbers(double[] numbers)
{
double sum = 0;
for(int dex =0; dex < numbers.length; dex++)
{
sum += numbers[dex];
}
return sum/numbers.length;
}
public static String enterFileName()
{
//The scope of this scanner object is only within this method
Scanner keyboard = new Scanner(System.in);
String prompt = "Enter the filename for file\ncontaining decimal numbers!";
System.out.println(prompt);
String fName = keyboard.nextLine();
return fName;
}
/**
* This method displays an introduction to the program
*/
public static void displayGreeting()
{
String greeting = "This program will calculate the average of numbers\n"+
"loaded from a file containing one decimal number per line.\n"+
"First you will be asked to enter the file name, then\n"+
"the average will be displayed.\n";
System.out.println(greeting);
}
public static void main(String[] args) throws Exception
{
displayGreeting();
String numberFileName = enterFileName();
double[] decNumbers = loadNumberFromFile(numberFileName);
double numAverage = averageNumbers(decNumbers);
String results = "The average of the numbers in file: "+numberFileName+
"\nis "+numAverage+"\n\nProgram terminating.";
System.out.println(results);
}
}