Created
March 8, 2014 15:21
-
-
Save desrtfx/9432767 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Scanner; //Scanner import | |
public class StandardDeviation { | |
// Constants to check from where to read the file | |
public static final int READ_DATA_FROM_FILE = 1; | |
public static final int READ_DATA_FROM_KEYBOARD = 2; | |
public static void main(String[] args) | |
{ | |
// declare the variable for the dataset, but don't initialise it yet. | |
double[] dataset = null; | |
Scanner sc = new Scanner(System.in); | |
// declare a String variable for the filename | |
String fileName; | |
// declare a boolean to decide from where to get the data | |
// First, check if the user passed anything as commandline argument | |
// args is the array for commandline parameters. If no argument | |
// was passed, the length = 0, if no arguments were passed, display a | |
// Menu to let | |
// the use choose. | |
if ((args.length == 0) || (args.length == 2)) { // no commandline parameters | |
// or 2 parameters given | |
int choice = processMenu(sc); // Returns 1 or 2 depending on user Choice | |
if (choice == READ_DATA_FROM_FILE) { | |
fileName = getFileName(sc); // getFileName returns a valid filename in any case | |
// Get File and process file | |
// declare a variable for a File | |
File aFile = getValidFile(fileName); | |
dataset = getDataFromFile(aFile); | |
} | |
else { | |
// get the data from the keyboard | |
dataset = getDataFromKeyboard(sc); | |
} | |
} | |
if (args.length == 1) { // 1 commandline parameter -> Filename | |
fileName = args[0]; | |
// get File and process file | |
if (!isValidFile(fileName)) { // if the filename entered is not valid | |
// ask the user to enter a valid file. | |
fileName = getFileName(sc); | |
} | |
// declare a variable for a File | |
File aFile = getValidFile(fileName); | |
dataset = getDataFromFile(aFile); | |
} | |
if (args.length > 2) { // if more than 2 parameters are given, the | |
//args[] are assumed to be the data | |
dataset = convertStringArrayToDoubleArray(args); | |
} | |
// All options are evaluated, | |
// the dataset is filled with data | |
// calculate and display the statistics | |
sc.close(); | |
showStatistics(dataset); | |
} | |
static void showStatistics(double[] dataset) { | |
// calculate the results | |
double mean = getMeanValue(dataset); | |
double max = getMaxValue(dataset); | |
double min = getMinValue(dataset); | |
double std = getStdDev(dataset); | |
// Show results | |
System.out.println("Standard Deviation Information:"); | |
System.out.println("ô¿ô- ô¿ô- ô¿ô- ô¿ô- ô¿ô- ô¿ô- "); | |
System.out.println("The mean is:" + mean); | |
System.out.println("The minimum is:" + min); | |
System.out.println("The Maximum is:" + max); | |
System.out.println("The Standard deviation:" + std); | |
} | |
// Method for Data Entry | |
public static double[] getDataFromKeyboard(Scanner sc) { | |
int numberOfDataPoints = 0; | |
do { | |
System.out.println("Statistical data is to be entered from keybord"); | |
System.out.println(); | |
System.out.println("Enter number of items (minimum 3 items):"); | |
numberOfDataPoints = sc.nextInt(); | |
if (numberOfDataPoints < 3) { | |
System.out | |
.println("The number of data points should be greater than 2."); | |
} | |
} while (numberOfDataPoints < 3); | |
// declare an array of n size to store integral data points | |
double[] dataset = new double[numberOfDataPoints]; | |
// allow user inputs | |
for (int i = 0; i < numberOfDataPoints; i++) { | |
System.out.print("[" + i + "]:"); | |
dataset[i] = sc.nextDouble(); | |
} | |
return dataset; | |
} | |
// Method for reading data from file | |
public static double[] getDataFromFile(File aFile) { | |
ArrayList<Double> tmp = new ArrayList<Double>(); | |
double[] arrtmp = null; | |
Scanner scan = null; | |
try { | |
scan = new Scanner(aFile).useDelimiter("\\s*\\n"); | |
while(scan.hasNextDouble()) | |
{ | |
tmp.add(scan.nextDouble()); | |
} | |
arrtmp = new double[tmp.size()]; | |
for (int i = 0; i < tmp.size(); i++) { | |
arrtmp[i] = (double)tmp.get(i); | |
} | |
} catch (FileNotFoundException e1) { | |
e1.printStackTrace(); | |
} | |
if (scan != null) { | |
scan.close(); | |
} | |
return arrtmp; | |
} | |
// Method to calculate the mean | |
public static double getMeanValue(double[] data) { | |
double sum = 0.0; | |
for (double d : data) { | |
sum += d; | |
} | |
return sum / data.length; | |
} | |
// Method to find the maximum | |
public static double getMaxValue(double[] data) { | |
double max = Double.MIN_VALUE; | |
for (double d : data) { | |
if (d > max) { | |
max = d; | |
} | |
} | |
return max; | |
} | |
// Method to find the minimum | |
public static double getMinValue(double[] data) { | |
double min = Double.MAX_VALUE; | |
for (double d : data) { | |
if (d < min) { | |
min = d; | |
} | |
} | |
return min; | |
} | |
// Method to find the standard Deviation | |
public static double getStdDev(double[] data) { | |
double temp = 0.0; | |
double mean = getMeanValue(data); | |
for (double d : data) { | |
temp = temp + Math.pow(d - mean, 2); | |
} | |
double std = Math.sqrt(temp / (data.length - 1)); | |
return std; | |
} | |
public static boolean isValidFile(String fileName) { | |
File theFile = new File(fileName); | |
return theFile.isFile(); | |
} | |
public static File getValidFile(String fileName) { | |
File theFile = new File(fileName); | |
return theFile; | |
} | |
public static int processMenu(Scanner sc) { | |
int response = 0; | |
do { | |
System.out.println("Statistics evaluation"); | |
System.out.println("---------------------"); | |
System.out.println(); | |
System.out.println("Please select:"); | |
System.out.println("1..to enter a filename and read data from a file"); | |
System.out.println("2..to enter the data from the keyboard"); | |
System.out.println("------------------------------------------------"); | |
System.out.println(); | |
System.out.print("Please enter 1 or 2:"); | |
response = sc.nextInt(); | |
} while ((response < 1) && (response > 2)); | |
return response; | |
} | |
public static String getFileName(Scanner sc) { | |
String aFileName; | |
do { | |
System.out.println("Please enter a valid filename: "); | |
aFileName = sc.nextLine(); | |
} while (!isValidFile(aFileName)); | |
return aFileName; | |
} | |
// Helper method to convert a | |
// String array to a double array | |
public static double[] convertStringArrayToDoubleArray(String[] data) { | |
double[] doubleData = new double[data.length]; | |
for (int i = 0; i < data.length; i++) { | |
doubleData[i] = Double.parseDouble(data[i]); | |
} | |
return doubleData; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment