-
-
Save bytecodeman/470292871024299b021ce855f9a70175 to your computer and use it in GitHub Desktop.
CSC-111 HW8 Calculates the Standard Deviation of Numbers in a File
This file contains 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
/* | |
* Name: Antonio C. Silvestri | |
* Date: 11/28/2018 | |
* Course Number: CSC-111 | |
* Course Name: Introduction to Java | |
* Problem Number: HW8 | |
* Email: silvestri@stcc.edu | |
* Description: Calculates the Standard Deviation of Numbers in a File | |
*/ | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
public class StdDevApp { | |
private static void report(double[] data, double mean, double dev) { | |
// Print Data | |
for (int i = 0; i < data.length; i++) { | |
System.out.printf("%7.2f", data[i]); | |
if ((i + 1) % 10 == 0) | |
System.out.println(); | |
} | |
System.out.println(); | |
System.out.printf("The mean is %7.2f\n", mean); | |
System.out.printf("The standard deviation is %7.5f\n", dev); | |
} | |
//********************************************************** | |
private static double deviation(double[] data) { | |
if (data.length < 2) | |
return Double.NaN; | |
double mean = mean(data); | |
double sum = 0; | |
for (int i = 0; i < data.length; i++) | |
sum += Math.pow(data[i] - mean, 2); | |
double dev = Math.sqrt(sum / (data.length - 1)); | |
return dev; | |
} | |
//********************************************************** | |
private static double mean(double[] data) { | |
if (data.length < 1) | |
return Double.NaN; | |
double sum = 0; | |
for (int i = 0; i < data.length; i++) | |
sum += data[i]; | |
double ave = sum / data.length; | |
return ave; | |
} | |
//********************************************************** | |
private static double[] getData(Scanner sc) { | |
int n = sc.nextInt(); | |
double data[] = new double[n]; | |
for (int i = 0; i < n; i++) | |
data[i] = sc.nextDouble(); | |
return data; | |
} | |
//********************************************** | |
private static void process(Scanner sc, String args[]) throws FileNotFoundException { | |
System.out.print("Enter Datafile name: "); | |
String filename = sc.nextLine(); | |
File file = new File(filename); | |
Scanner scfile = new Scanner(file); | |
double data[] = getData(scfile); | |
scfile.close(); | |
double mean = mean(data); | |
double dev = deviation(data); | |
report(data, mean, dev); | |
} | |
//********************************************** | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
public static void main(String args[]) throws FileNotFoundException { | |
final String TITLE = "Standard Deviation Calculator App V1.0"; | |
final String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment