d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 1200 Fg For Help > Basic Java Code
Add Reply New Topic New Poll
Member
Posts: 9,746
Joined: May 4 2013
Gold: 0.00
Nov 14 2016 07:21pm
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);
}
}
Member
Posts: 36,123
Joined: Jul 18 2008
Gold: 2,407.00
Nov 15 2016 09:08am
If you do not find someone else to do this, I can do this, but I won't be able to get to it until Friday.
Member
Posts: 9,746
Joined: May 4 2013
Gold: 0.00
Nov 15 2016 09:45am
Quote (Mastersam93 @ Nov 15 2016 10:08am)
If you do not find someone else to do this, I can do this, but I won't be able to get to it until Friday.


Its due tomorrow :/
Member
Posts: 9,746
Joined: May 4 2013
Gold: 0.00
Nov 15 2016 11:03am
Due in 29 hours!
Member
Posts: 2,754
Joined: Nov 26 2007
Gold: 1,339.81
Nov 15 2016 12:25pm
Code
public static double standardDeviation( double[] decNumbers,
double numAverage ) {
double sum = 0;
for ( int i = 0; i < decNumbers.length; i++ ) {
sum += ((decNumbers[i] - numAverage) * (decNumbers[i] - numAverage));
}
return Math.sqrt( sum / decNumbers.length );
}

...
double sd = standardDeviation( decNumbers, numAverage );

String results = "The average of the numbers in file: " + numberFileName
+ "\nis " + numAverage + " and standard deviation: " + sd
+ "\n\nProgram terminating.";

System.out.println( results );




This post was edited by labatymo on Nov 15 2016 12:26pm
Member
Posts: 9,746
Joined: May 4 2013
Gold: 0.00
Nov 15 2016 12:26pm
Let me give this a try! Might need an hour or 2 if thats okay!

This post was edited by Cold1992 on Nov 15 2016 12:28pm
Member
Posts: 17,809
Joined: Mar 18 2009
Gold: 57,185.00
Nov 22 2016 06:43am
still need? seems like labatymo did it for u?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll