Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program: Unit4ClassAverage.java
- * Author:
- * Date: 01/03/2013
- * Mod: 4/03/2013
- * Description: A simple app that calculates the average of a class after given the number of students and letter grades
- * ,also displays number of passing and failing students.
- */
- import java.io.*;
- public class Unit4ClassAverage {
- public static void main(String[] args) {
- //Stream and Buffered readers to take user input
- InputStreamReader isr = new InputStreamReader(System.in);
- BufferedReader br = new BufferedReader(isr);
- //variables to store data
- String strInput = ""; //user input variable
- String strGrade = ""; //student's mark
- int intStudents = 0; //number of students in class, also amount of times for loop runs
- int intPassing = 0; //number of passing students
- int intFailing = 0; //number of failing students
- int intMarkTotal = 0; // total of marks
- int intAverage = 0; //final average of class
- boolean boolAccepted = true; //controls whether letter grade was accepted
- try {
- System.out.println("This program will calculate the average for the given number of student's marks");
- System.out.println("How many students are in the class?");
- System.out.println("Alternatively, type h for a help menu and q to quit");
- strInput = br.readLine();
- if (strInput.equals("h") || strInput.equals("H")) {
- System.out.println("|===========================================================|");
- //help files
- System.out.println("|===========================================================|");
- }
- else if (strInput.equals("q") || strInput.equals("Q")) {
- //does nothing since program is not looped
- }
- else { //if previous conditions are not met program continues uninterrupted
- intStudents = Integer.parseInt(strInput);
- for (int i = 0; i < intStudents; i++) { //loop repeats for number of students given by user
- //System.out.println(intMarkTotal); //debugging
- do {
- boolAccepted = true; //resetting to stop infinite loop when user enters and invalid grade
- System.out.println("What is Student " + (i + 1) + "'s letter mark?"); // i + 1 since we start at 0
- strGrade = br.readLine();
- if (strGrade.equals("A") || strGrade.equals("a")) {
- intMarkTotal += 80; //if student is getting an A, 80 is added to total marks
- intPassing++; //increases amount of passing students
- }
- else if (strGrade.equals("B") || strGrade.equals("b")) {
- intMarkTotal += 70; //if student is getting a B, 70 is added to total marks
- intPassing++;
- }
- else if (strGrade.equals("C") || strGrade.equals("c")) {
- intMarkTotal += 60; //if student is getting a C, 60 is added to total marks
- intPassing++;
- }
- else if (strGrade.equals("D") || strGrade.equals("d")) {
- intMarkTotal += 50; //if student is getting a D, 50 is added to total marks
- intPassing++;
- }
- else if (strGrade.equals("F") || strGrade.equals("f")) {
- intMarkTotal += 40; //if student is getting an F, 40 is added to total marks
- intFailing++; //increases amount of failing students
- }
- else {
- System.out.println("Error: Invalid grade or other error! Try Again!");
- boolAccepted = false;
- }
- }
- while (boolAccepted == false); //if this occurs it asks the user for the same students grade again
- } // end of for
- intAverage = intMarkTotal / intStudents; //average as a sum of all numbers divided by the number of numbers
- System.out.println("|===============================================|");
- System.out.println("The average mark of the class is " + intAverage + "."); //displaying average
- System.out.println("There are " + intPassing + " student(s) passing the class."); //displaying number of failures
- System.out.println("There are " + intFailing + " student(s) failing the class."); //displaying number of passes
- }
- }
- catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement