Advertisement
Guest User

histogramProject

a guest
Mar 9th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class FunWithHistograms {
  4. public static String txtFile = "";
  5. public static void main(String[] args) throws FileNotFoundException{
  6. //Intialization
  7. Scanner console = new Scanner(System.in);
  8. String answer = "yes";
  9.  
  10. //Method Calls
  11. introMessage();
  12. while(answer.equalsIgnoreCase("yes")){
  13. int[] data = readData(console);
  14. System.out.print("Output file name? ");
  15. String txtFileOut = console.next();
  16. PrintStream histogramFile = new PrintStream(txtFileOut);
  17. System.out.println();
  18. meanData(data);
  19. findMaxIndex(data);
  20. int[] counter2 = getCounts(data);
  21. histogramFile.println("Histogram for " + txtFile);
  22. histogramFile.println();
  23. printHistogram(data, histogramFile);
  24. summaryStats(data, counter2, histogramFile);
  25. System.out.println("Histogram Created!");
  26. System.out.print("Do you want to create another histogram? ");
  27. answer = console.next();
  28. if(answer.equalsIgnoreCase("yes")) {
  29. System.out.println();
  30. }
  31. } // end of while loop
  32. System.out.println();
  33. System.out.println("Thanks for having fun with histograms!");
  34. } // end of main method
  35.  
  36. //This method prints out a series of strings to inform the user of the program's intent
  37. //Parameters: none
  38. //Returns: none
  39. public static void introMessage(){
  40. System.out.println("The program will analyze data from a dataset of");
  41. System.out.println("non-negative integer values. It will produce a");
  42. System.out.println("histogram of the data and some statistics.");
  43. System.out.println("The result will be written to an output file.");
  44. System.out.println();
  45. } // end of introMessage method
  46.  
  47. //This method takes user input to ultimately create an array (based on a file)
  48. //Parameters: Scanner console (for user input)
  49. //Returns: int[] fileArray (the array of the user's chosen file)
  50. public static int [] readData(Scanner console) throws FileNotFoundException{
  51. System.out.print("Input file name? ");
  52. txtFile = console.next();
  53.  
  54. File fileTxt = new File(txtFile);
  55. Scanner fileScan = new Scanner(fileTxt);
  56.  
  57. int arrayNum = fileScan.nextInt();
  58. int[] fileArray = new int[arrayNum];
  59.  
  60. for(int i = 0; i < fileArray.length; i++){
  61. fileArray[i] = fileScan.nextInt();
  62. }
  63.  
  64. return fileArray;
  65. } // end of readData method
  66.  
  67. //This method finds the maximum index value of a given array
  68. //Parameters: int [] data (any given array)
  69. //Returns: int maxVal (the int that corresponds with the index number)
  70. public static int findMaxIndex(int[] data) {
  71. int maxVal = 0;
  72. for(int i = 0; i < data.length; i++){ // runs for the whole dataset
  73. if(data[i] > data[maxVal]){ //checks the index via []
  74. maxVal = i; //updates maxVal into the i index
  75. }
  76. }
  77. return maxVal;
  78. } // end of findMaxIndex method
  79.  
  80. //This method count the occurrences of every value in an array & arranges them based on index
  81. //Parameters: int [] data (any given array)
  82. //Returns: int[] counter (an array that stores the counts of all the values)
  83. public static int[] getCounts(int[] data) {
  84. int index = data[0]; // gets us at index 0
  85. for(int i = 0; i < data.length; i++){
  86. if(index < data[i]){
  87. index = data[i]; // starts storing the value
  88. }
  89. }
  90. //needs to return a new array
  91. //array creation
  92. int[] counter = new int[index + 1]; // has to b. e +1 because index is [0]
  93. for(int i = 0; i < data.length; i++){
  94. counter[data[i]]++;
  95. }
  96. return counter;
  97. }
  98.  
  99. //This method computes the average of the data in the array
  100. //Parameters: int[] arr (the array of the data from the file)
  101. //Returns: double mean (the average of the data set)
  102. public static double meanData(int[] arr){
  103. int sum = 0;
  104. for(int i = 0; i < arr.length; i++){
  105. sum += arr[i];
  106. }
  107. double mean = sum / (double) arr.length;
  108.  
  109. return mean;
  110. } // end of meanData method
  111.  
  112. //This method creates the histogram into the output file the user enters
  113. /*Parameters: int[] data (array of the file's data),
  114. PrintStream histogramFile (prints to user's file)*/
  115. //Returns: none
  116. public static void printHistogram(int[] data, PrintStream histogramFile){
  117. int[] counter2 = getCounts(data);
  118.  
  119. for (int i = 0; i < counter2.length; i++){
  120. histogramFile.print(i + "| ");
  121. for(int j = 0; j < counter2[i]; j++){ // needs to go along with the occurences
  122. histogramFile.print("*");
  123. } // end of inner for loop
  124. histogramFile.println();
  125. }
  126. histogramFile.println();
  127. } // end of printHistogram method
  128.  
  129. //This method prints out the primary statistics of the data set into the user's output file
  130. /*Parameters: int[] data (array of the file's data),
  131. int[] counts (array made in getCounts method),
  132. PrintStream histogramFile (prints to user's file)*/
  133. //Returns: none
  134. public static void summaryStats(int[] data, int[] counts, PrintStream histogramFile){
  135. //Num Values printing
  136. int arrayNum2 = data.length;
  137. histogramFile.println("Num. Values: " + arrayNum2);
  138. //Mean printing
  139. double meanData2 = meanData(data);
  140. histogramFile.println("Mean: " + meanData2);
  141. //Mode printing
  142. int mode = findMaxIndex(counts);
  143. histogramFile.println("Mode: " + mode);
  144. } // end of summaryStats method
  145.  
  146. } // end of public class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement