Source of GetGrades.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to calculate a student's average grade.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class GetGrades {

 11:     public static final int NUM_ASGN = 6;

 13:     public static void main(String[] args) {
 14:         // create variables
 15:         Scanner kbd = new Scanner(System.in);
 16:         int sum;
 17:         double ave;

 19:         // say what we're doing
 20:         System.out.println("\n"
 21:                 + "This program calculates your average grade "
 22:                 + "for " + NUM_ASGN + " assignments.\n");

 24:         // get the grades
 25:         sum = 0;
 26:         for (int asgn = 1; asgn <= NUM_ASGN; ++asgn) {
 27:             int grade;  // don't need the grade outside this loop

 29:             System.out.print("Enter your grade on A #" + asgn + ": ");
 30:             grade = kbd.nextInt();
 31:             kbd.nextLine();
 32:             sum += grade;
 33:         }

 35:         // calculate average
 36:         ave = (double)sum / (double)NUM_ASGN;

 38:         // report average
 39:         System.out.println("\n"
 40:                 + "The average grade is " + ave + ".\n");
 41:     }

 43: }