Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class FunWithHistograms {
- public static String txtFile = "";
- public static void main(String[] args) throws FileNotFoundException{
- //Intialization
- Scanner console = new Scanner(System.in);
- String answer = "yes";
- //Method Calls
- introMessage();
- while(answer.equalsIgnoreCase("yes")){
- int[] data = readData(console);
- System.out.print("Output file name? ");
- String txtFileOut = console.next();
- PrintStream histogramFile = new PrintStream(txtFileOut);
- System.out.println();
- meanData(data);
- findMaxIndex(data);
- int[] counter2 = getCounts(data);
- histogramFile.println("Histogram for " + txtFile);
- histogramFile.println();
- printHistogram(data, histogramFile);
- summaryStats(data, counter2, histogramFile);
- System.out.println("Histogram Created!");
- System.out.print("Do you want to create another histogram? ");
- answer = console.next();
- if(answer.equalsIgnoreCase("yes")) {
- System.out.println();
- }
- } // end of while loop
- System.out.println();
- System.out.println("Thanks for having fun with histograms!");
- } // end of main method
- //This method prints out a series of strings to inform the user of the program's intent
- //Parameters: none
- //Returns: none
- public static void introMessage(){
- System.out.println("The program will analyze data from a dataset of");
- System.out.println("non-negative integer values. It will produce a");
- System.out.println("histogram of the data and some statistics.");
- System.out.println("The result will be written to an output file.");
- System.out.println();
- } // end of introMessage method
- //This method takes user input to ultimately create an array (based on a file)
- //Parameters: Scanner console (for user input)
- //Returns: int[] fileArray (the array of the user's chosen file)
- public static int [] readData(Scanner console) throws FileNotFoundException{
- System.out.print("Input file name? ");
- txtFile = console.next();
- File fileTxt = new File(txtFile);
- Scanner fileScan = new Scanner(fileTxt);
- int arrayNum = fileScan.nextInt();
- int[] fileArray = new int[arrayNum];
- for(int i = 0; i < fileArray.length; i++){
- fileArray[i] = fileScan.nextInt();
- }
- return fileArray;
- } // end of readData method
- //This method finds the maximum index value of a given array
- //Parameters: int [] data (any given array)
- //Returns: int maxVal (the int that corresponds with the index number)
- public static int findMaxIndex(int[] data) {
- int maxVal = 0;
- for(int i = 0; i < data.length; i++){ // runs for the whole dataset
- if(data[i] > data[maxVal]){ //checks the index via []
- maxVal = i; //updates maxVal into the i index
- }
- }
- return maxVal;
- } // end of findMaxIndex method
- //This method count the occurrences of every value in an array & arranges them based on index
- //Parameters: int [] data (any given array)
- //Returns: int[] counter (an array that stores the counts of all the values)
- public static int[] getCounts(int[] data) {
- int index = data[0]; // gets us at index 0
- for(int i = 0; i < data.length; i++){
- if(index < data[i]){
- index = data[i]; // starts storing the value
- }
- }
- //needs to return a new array
- //array creation
- int[] counter = new int[index + 1]; // has to b. e +1 because index is [0]
- for(int i = 0; i < data.length; i++){
- counter[data[i]]++;
- }
- return counter;
- }
- //This method computes the average of the data in the array
- //Parameters: int[] arr (the array of the data from the file)
- //Returns: double mean (the average of the data set)
- public static double meanData(int[] arr){
- int sum = 0;
- for(int i = 0; i < arr.length; i++){
- sum += arr[i];
- }
- double mean = sum / (double) arr.length;
- return mean;
- } // end of meanData method
- //This method creates the histogram into the output file the user enters
- /*Parameters: int[] data (array of the file's data),
- PrintStream histogramFile (prints to user's file)*/
- //Returns: none
- public static void printHistogram(int[] data, PrintStream histogramFile){
- int[] counter2 = getCounts(data);
- for (int i = 0; i < counter2.length; i++){
- histogramFile.print(i + "| ");
- for(int j = 0; j < counter2[i]; j++){ // needs to go along with the occurences
- histogramFile.print("*");
- } // end of inner for loop
- histogramFile.println();
- }
- histogramFile.println();
- } // end of printHistogram method
- //This method prints out the primary statistics of the data set into the user's output file
- /*Parameters: int[] data (array of the file's data),
- int[] counts (array made in getCounts method),
- PrintStream histogramFile (prints to user's file)*/
- //Returns: none
- public static void summaryStats(int[] data, int[] counts, PrintStream histogramFile){
- //Num Values printing
- int arrayNum2 = data.length;
- histogramFile.println("Num. Values: " + arrayNum2);
- //Mean printing
- double meanData2 = meanData(data);
- histogramFile.println("Mean: " + meanData2);
- //Mode printing
- int mode = findMaxIndex(counts);
- histogramFile.println("Mode: " + mode);
- } // end of summaryStats method
- } // end of public class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement