Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. package wynnbe01_Project2;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5.  
  6.  
  7. /*
  8. * Brandon Wynn
  9. * CS160-05 Fall
  10. * Project 2
  11. */
  12.  
  13.  
  14. public class ExamStatistics {
  15.  
  16.  
  17. static int gradeA = 85;
  18. static int gradeB = 75; // static integers in the class, but outside of the main method, as requested, to store lower limits of each grade.
  19. static int gradeC = 65;
  20. static int gradeD = 55;
  21.  
  22. public static void main(String[] args) throws IOException {
  23.  
  24.  
  25. int next; // to hold place for next integer
  26. int total = 0; // Total number of exams
  27. int minScore = 100; // the lowest score, initialized at 100, otherwise score would never change from 0
  28. int maxScore = 0; // the highest scores
  29.  
  30. int totalA = 0; // Total number of A's
  31. int totalB = 0; // Total number of B's
  32. int totalC = 0; // Total number of C's
  33. int totalD = 0; // Total number of D's
  34. int totalF = 0; // Total number of F's
  35.  
  36. double aPercentage; // Percentage of A's
  37. double bPercentage; // Percentage of B's
  38. double cPercentage; // Percentage of C's
  39. double dPercentage; // Percentage of D's
  40. double fPercentage; // Percentage of F's
  41.  
  42. double sum = 0.0; // total sum of all exam scores, initialized as 0.0 because it will be a running total.
  43. double average; // average scores of all exam scores
  44. double psd = 0; // population standard deviation of exam scores
  45.  
  46. Scanner keyboard = new Scanner(System.in);
  47. System.out.println("Enter the file name to be tested:");
  48. String filename = keyboard.nextLine(); // solicits the user for the filename to be tested and stores name under "file"
  49.  
  50. File file = new File(filename);
  51.  
  52.  
  53. while (!file.exists())
  54. {
  55. System.out.println("File does not exist");
  56. System.out.println("Enter the file name to be tested:");
  57. filename = keyboard.nextLine();
  58.  
  59. }
  60.  
  61. Scanner inputFile = new Scanner(file);
  62.  
  63. while (inputFile.hasNext())
  64. { next = inputFile.nextInt();
  65. sum = sum + next;
  66. total = total + 1;
  67.  
  68.  
  69. if(next > maxScore)
  70. {
  71. maxScore = next;
  72. }
  73.  
  74. if(next < minScore)
  75. {
  76. minScore = next;
  77. }
  78.  
  79.  
  80. if(next< gradeD)
  81. {
  82. totalF = totalF + 1;
  83. }
  84. else if(next< gradeC)
  85. {
  86. totalD = totalD + 1;
  87. }
  88.  
  89. else if(next< gradeB)
  90. {
  91. totalC = totalC + 1;
  92. }
  93. else if(next< gradeA)
  94. {
  95. totalB = totalB + 1;
  96. }
  97. else if(next> gradeA)
  98. totalA = totalA + 1;
  99. else
  100. System.out.println("Invalid score");
  101. }
  102.  
  103. inputFile.close(); // To close scanner
  104.  
  105.  
  106. average = (Math.round((sum/total)*100.0)/100.0); // To calculate average while also rounding to 2 decimal digits
  107.  
  108. aPercentage = (Math.round((totalA * 100) / (total*1.0)*100.00)/100.00); // To calculate percentage of A's while also rounding to 2 decimal digits.
  109. bPercentage = (Math.round((totalB * 100) / (total*1.0)*100.00)/100.00); // To calculate percentage of B's while also rounding to 2 decimal digits.
  110. cPercentage = (Math.round((totalC * 100) / (total*1.0)*100.00)/100.00); // To calculate percentage of C's while also rounding to 2 decimal digits.
  111. dPercentage = (Math.round((totalD * 100) / (total*1.0)*100.00)/100.00); // To calculate percentage of D's while also rounding to 2 decimal digits.
  112. fPercentage = (Math.round((totalF * 100) / (total*1.0)*100.00)/100.00); // To calculate percentage of F's while also rounding to 2 decimal digits.
  113.  
  114.  
  115.  
  116.  
  117. File filePSD = new File(filename);
  118. Scanner inputPSD = new Scanner(filePSD);
  119.  
  120.  
  121. if (!filePSD.exists())
  122. {
  123. System.out.println("File does not exist");
  124. }
  125.  
  126. while (inputPSD.hasNext())
  127. { next = inputPSD.nextInt();
  128. psd = psd + (average - next) * (average - next);
  129.  
  130.  
  131.  
  132.  
  133.  
  134. }
  135.  
  136. psd =(Math.round((Math.sqrt(psd / total))*100.0)/100.0); // Calculating PSD while also rounding it to 2 decimal digits
  137.  
  138. inputPSD.close(); // To close Scanner
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. String message = "Exam Statistics \n " // Composing message using String variable so literals do not need to be used.
  146. + "\nTotal: " + total
  147. + "\nAverage score: " + average
  148. + "\nPopulation standard deviation of the scores: " + psd
  149. + "\n# of A, 85-100:\t" + totalA + "\t" + aPercentage + "%"
  150. + "\n# of B, 75--84:\t" + totalB + "\t" + bPercentage + "%"
  151. + "\n# of C, 65--74:\t" + totalC + "\t" + cPercentage + "%"
  152. + "\n# of D, 55--64:\t" + totalD + "\t" + dPercentage + "%"
  153. + "\n# of F, 00--54:\t" + totalF + "\t" + fPercentage + "%"
  154. + "\nMinimum score: " + minScore
  155. + "\nMaximum score: " + maxScore
  156. ;
  157.  
  158.  
  159.  
  160. System.out.println(message);
  161.  
  162. PrintWriter outputFile = new PrintWriter("ScoreStatistics.txt");
  163.  
  164. outputFile.println(message);
  165.  
  166.  
  167. // Formatting of 'message' when using PrintWriter doesn't work when using notepad. When using notepad++, formatting is fine. But since it didn't work properly with notepad,
  168. // I went ahead and wrote a literal message that will look right for notepad.
  169.  
  170. /*outputFile.println("Exam Statistics");
  171. outputFile.println(" ");
  172. outputFile.println("Total: " + total);
  173. outputFile.println("Population standard deviation of the scores: " + psd);
  174. outputFile.println("# of A, 85-100: " + totalA + " " + aPercentage + "%");
  175. outputFile.println("# of B, 75--84: " + totalB + " " + bPercentage + "%");
  176. outputFile.println("# of C, 65--74: " + totalC + " " + cPercentage + "%");
  177. outputFile.println("# of D, 55--64: " + totalD + " " + dPercentage + "%");
  178. outputFile.println("# of F, 00--54: " + totalF + " " + fPercentage + "%");
  179. outputFile.println("Minimum score: " + minScore);
  180. outputFile.println("Maximum score: " + maxScore);
  181. */
  182. outputFile.close(); // To close PrintWriter
  183. keyboard.close(); // To close input scanner
  184.  
  185. System.exit(0);
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement