/ComputeDeviation.java Secret
Last active
November 9, 2020 15:06
-
-
Save bytecodeman/3856f7a2b1a16707b624fa25ff9bf2a4 to your computer and use it in GitHub Desktop.
ComputeDeviation with Silvestri's modifications 11/9/2020
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: Nikolay Ostroukhov (with Silvestri's Modifications) | |
* Date: 11/9/20 | |
* Course Number: 111 | |
* Course Name: CSC | |
* Problem Number: Homework 8 -- Compute Standard Deviation | |
* Email: noostroukhov@student.stcc.edu | |
* Computes standard deviation of a file | |
*/ | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Scanner; | |
public class ComputeDeviation { | |
final static String TITLE = "Niko's Standard Deviation Calculator"; | |
final static String CONTINUE_PROMPT = "\nPerform another calculation? [y/N] "; | |
//********************************************** | |
// Put as many methods you need here | |
private static double deviation(double[] x, double mean) { | |
double result = 0.0; | |
for (int i = 0; i < x.length; i++) | |
result += Math.pow(x[i] - mean, 2); | |
result /= x.length - 1; | |
return Math.sqrt(result); | |
} | |
private static double mean(double[] x) { | |
double sum = 0.0; | |
for (int i = 0; i < x.length; i++) | |
sum += x[i]; | |
return sum / x.length; | |
} | |
private static double[] populateArray(Scanner sc) { | |
int count = sc.nextInt(); | |
double[] result = new double[count]; | |
for (int i = 0; i < count; i++) | |
result[i] = sc.nextDouble(); | |
return result; | |
} | |
private static void printArray(double[] arr, int columns) { | |
for (int i = 0; i < arr.length; i++) { | |
System.out.printf("%10.5f", arr[i]); | |
if ((i+1) % columns == 0) | |
System.out.println(); | |
} | |
if (arr.length % columns != 0) | |
System.out.println(); | |
} | |
// ********************************************** | |
private static void process(Scanner sc, String args[]) throws IOException { | |
// data-500-15-5.txt data-750-10-6.txt data-1000-20-10.txt | |
System.out.print("Enter filename: "); | |
String filename = sc.nextLine(); | |
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/" + filename); | |
Scanner scanURL = new Scanner(url.openStream()); | |
double[] arr = populateArray(scanURL); | |
double mean = mean(arr); | |
double deviation = deviation(arr, mean); | |
printArray(arr, 7); | |
System.out.printf("Mean: %.3f\nDeviation: %.3f\n", mean, deviation); | |
scanURL.close(); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws IOException { | |
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