Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package wynnbe01_Project2;
- import java.util.Scanner;
- import java.io.*;
- /*
- * Brandon Wynn
- * CS160-05 Fall
- * Project 2
- */
- public class ExamStatistics {
- static int gradeA = 85;
- static int gradeB = 75; // static integers in the class, but outside of the main method, as requested, to store lower limits of each grade.
- static int gradeC = 65;
- static int gradeD = 55;
- public static void main(String[] args) throws IOException {
- int next; // to hold place for next integer
- int total = 0; // Total number of exams
- int minScore = 100; // the lowest score, initialized at 100, otherwise score would never change from 0
- int maxScore = 0; // the highest scores
- int totalA = 0; // Total number of A's
- int totalB = 0; // Total number of B's
- int totalC = 0; // Total number of C's
- int totalD = 0; // Total number of D's
- int totalF = 0; // Total number of F's
- double aPercentage; // Percentage of A's
- double bPercentage; // Percentage of B's
- double cPercentage; // Percentage of C's
- double dPercentage; // Percentage of D's
- double fPercentage; // Percentage of F's
- double sum = 0.0; // total sum of all exam scores, initialized as 0.0 because it will be a running total.
- double average; // average scores of all exam scores
- double psd = 0; // population standard deviation of exam scores
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Enter the file name to be tested:");
- String filename = keyboard.nextLine(); // solicits the user for the filename to be tested and stores name under "file"
- File file = new File(filename);
- while (!file.exists())
- {
- System.out.println("File does not exist");
- System.out.println("Enter the file name to be tested:");
- filename = keyboard.nextLine();
- }
- Scanner inputFile = new Scanner(file);
- while (inputFile.hasNext())
- { next = inputFile.nextInt();
- sum = sum + next;
- total = total + 1;
- if(next > maxScore)
- {
- maxScore = next;
- }
- if(next < minScore)
- {
- minScore = next;
- }
- if(next< gradeD)
- {
- totalF = totalF + 1;
- }
- else if(next< gradeC)
- {
- totalD = totalD + 1;
- }
- else if(next< gradeB)
- {
- totalC = totalC + 1;
- }
- else if(next< gradeA)
- {
- totalB = totalB + 1;
- }
- else if(next> gradeA)
- totalA = totalA + 1;
- else
- System.out.println("Invalid score");
- }
- inputFile.close(); // To close scanner
- average = (Math.round((sum/total)*100.0)/100.0); // To calculate average while also rounding to 2 decimal digits
- 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.
- 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.
- 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.
- 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.
- 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.
- File filePSD = new File(filename);
- Scanner inputPSD = new Scanner(filePSD);
- if (!filePSD.exists())
- {
- System.out.println("File does not exist");
- }
- while (inputPSD.hasNext())
- { next = inputPSD.nextInt();
- psd = psd + (average - next) * (average - next);
- }
- psd =(Math.round((Math.sqrt(psd / total))*100.0)/100.0); // Calculating PSD while also rounding it to 2 decimal digits
- inputPSD.close(); // To close Scanner
- String message = "Exam Statistics \n " // Composing message using String variable so literals do not need to be used.
- + "\nTotal: " + total
- + "\nAverage score: " + average
- + "\nPopulation standard deviation of the scores: " + psd
- + "\n# of A, 85-100:\t" + totalA + "\t" + aPercentage + "%"
- + "\n# of B, 75--84:\t" + totalB + "\t" + bPercentage + "%"
- + "\n# of C, 65--74:\t" + totalC + "\t" + cPercentage + "%"
- + "\n# of D, 55--64:\t" + totalD + "\t" + dPercentage + "%"
- + "\n# of F, 00--54:\t" + totalF + "\t" + fPercentage + "%"
- + "\nMinimum score: " + minScore
- + "\nMaximum score: " + maxScore
- ;
- System.out.println(message);
- PrintWriter outputFile = new PrintWriter("ScoreStatistics.txt");
- outputFile.println(message);
- // 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,
- // I went ahead and wrote a literal message that will look right for notepad.
- /*outputFile.println("Exam Statistics");
- outputFile.println(" ");
- outputFile.println("Total: " + total);
- outputFile.println("Population standard deviation of the scores: " + psd);
- outputFile.println("# of A, 85-100: " + totalA + " " + aPercentage + "%");
- outputFile.println("# of B, 75--84: " + totalB + " " + bPercentage + "%");
- outputFile.println("# of C, 65--74: " + totalC + " " + cPercentage + "%");
- outputFile.println("# of D, 55--64: " + totalD + " " + dPercentage + "%");
- outputFile.println("# of F, 00--54: " + totalF + " " + fPercentage + "%");
- outputFile.println("Minimum score: " + minScore);
- outputFile.println("Maximum score: " + maxScore);
- */
- outputFile.close(); // To close PrintWriter
- keyboard.close(); // To close input scanner
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement