Advertisement
Guest User

Untitled

a guest
Mar 5th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. /*
  2.  * Program:  Unit4ClassAverage.java
  3.  * Author:
  4.  * Date: 01/03/2013
  5.  * Mod: 4/03/2013
  6.  * Description: A simple app that calculates the average of a class after given the number of students and letter grades
  7.  *              ,also displays number of passing and failing students.
  8.  */
  9.  
  10. import java.io.*;
  11.  
  12. public class Unit4ClassAverage {
  13.     public static void main(String[] args) {
  14.  
  15.         //Stream and Buffered readers to take user input
  16.         InputStreamReader isr = new InputStreamReader(System.in);
  17.         BufferedReader br = new BufferedReader(isr);
  18.         //variables to store data
  19.         String strInput = ""; //user input variable
  20.         String strGrade = ""; //student's mark
  21.         int intStudents = 0; //number of students in class, also amount of times for loop runs
  22.         int intPassing = 0; //number of passing students
  23.         int intFailing = 0; //number of failing students
  24.         int intMarkTotal = 0; // total of marks
  25.         int intAverage = 0; //final average of class
  26.         boolean boolAccepted = true; //controls whether letter grade was accepted
  27.  
  28.  
  29.         try {
  30.             System.out.println("This program will calculate the average for the given number of student's marks");
  31.             System.out.println("How many students are in the class?");
  32.             System.out.println("Alternatively, type h for a help menu and q to quit");
  33.             strInput = br.readLine();
  34.  
  35.             if (strInput.equals("h") || strInput.equals("H")) {
  36.                 System.out.println("|===========================================================|");
  37.                 //help files
  38.                 System.out.println("|===========================================================|");
  39.             }
  40.             else if (strInput.equals("q") || strInput.equals("Q")) {
  41.                 //does nothing since program is not looped
  42.             }
  43.             else { //if previous conditions are not met program continues uninterrupted
  44.                 intStudents = Integer.parseInt(strInput);
  45.  
  46.                 for (int i = 0; i < intStudents; i++) { //loop repeats for number of students given by user
  47.                     //System.out.println(intMarkTotal); //debugging
  48.  
  49.                     do {
  50.                         boolAccepted = true; //resetting to stop infinite loop when user enters and invalid grade
  51.                         System.out.println("What is Student " + (i + 1) + "'s letter mark?"); // i + 1 since we start at 0
  52.                         strGrade = br.readLine();
  53.  
  54.                         if (strGrade.equals("A") || strGrade.equals("a")) {
  55.                             intMarkTotal += 80; //if student is getting an A, 80 is added to total marks
  56.                             intPassing++; //increases amount of passing students
  57.                         }
  58.                         else if (strGrade.equals("B") || strGrade.equals("b")) {
  59.                             intMarkTotal += 70; //if student is getting a B, 70 is added to total marks
  60.                             intPassing++;
  61.                         }
  62.                         else if (strGrade.equals("C") || strGrade.equals("c")) {
  63.                             intMarkTotal += 60; //if student is getting a C, 60 is added to total marks
  64.                             intPassing++;
  65.                         }
  66.                         else if (strGrade.equals("D") || strGrade.equals("d")) {
  67.                             intMarkTotal += 50; //if student is getting a D, 50 is added to total marks
  68.                             intPassing++;
  69.                         }
  70.                         else if (strGrade.equals("F") || strGrade.equals("f")) {
  71.                             intMarkTotal += 40; //if student is getting an F, 40 is added to total marks
  72.                             intFailing++; //increases amount of failing students
  73.                         }
  74.                         else {
  75.                             System.out.println("Error: Invalid grade or other error! Try Again!");
  76.                             boolAccepted = false;
  77.                         }
  78.                     }
  79.                     while (boolAccepted == false); //if this occurs it asks the user for the same students grade again
  80.                 } // end of for
  81.  
  82.                 intAverage = intMarkTotal / intStudents; //average as a sum of all numbers divided by the number of numbers
  83.                 System.out.println("|===============================================|");
  84.                 System.out.println("The average mark of the class is " + intAverage + "."); //displaying average
  85.                 System.out.println("There are " + intPassing + " student(s) passing the class."); //displaying number of failures
  86.                 System.out.println("There are " + intFailing + " student(s) failing the class."); //displaying number of passes
  87.             }
  88.         }
  89.         catch (IOException ex) {
  90.             ex.printStackTrace();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement