public class TestStudent
2: import java.util.Scanner;
4: public class TestStudent {
6: public static final Scanner KBD = new Scanner(System.in);
8: public static void main(String[] args) {
9: Student[] myStudents;
11: // introduce yourself
12: printIntroduction();
14: // get size of class
15: myStudents = getClassList();
17: // read students' grades
18: readGrades(myStudents);
20: // print out a table of grades
21: printGradesReport(myStudents);
22: }
24: /**
25: * Introduce this program
26: */
27: private static void printIntroduction() {
28: System.out.println("\n\n"
29: + "CSCI1226 Grading Program\n"
30: + "------------------------\n\n"
31: + "Enter the names and grades for all your students "
32: + "and their assignments,\n"
33: + "and I'll show you their final grades.\n");
34: pause();
35: }
37: /**
38: * Get the class list from the user -- names and student IDs.
39: *
40: * @return the list of students -- no grades assigned
41: */
42: private static Student[] getClassList() {
43: int numStu;
44: Student[] myStudents;
45: String number, name;
47: System.out.print("How many students are in your class? ");
48: numStu = KBD.nextInt();
49: KBD.nextLine();
50: myStudents = new Student[numStu];
52: // create student objects
53: System.out.println("What are their numbers and names? ");
54: for (int s = 0; s < numStu; s++) {
55: System.out.print("Enter a student's A-Number: ");
56: number = KBD.nextLine();
57: System.out.print("Enter their name: ");
58: name = KBD.nextLine();
59: myStudents[s] = new Student(number, name);
60: }
61: pause();
63: return myStudents;
64: }
66: /**
67: * Read grades for all students in the class list.
68: *
69: * @param myStudents the class list
70: */
71: private static void readGrades(Student[] myStudents) {
72: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
73: System.out.println("Enter grades for assignment #" + a + ".");
74: pause();
75: for (int s = 0; s < myStudents.length; ++s) {
76: int g;
77: System.out.print("Enter " + myStudents[s].getName()
78: + "'s grade for A#" + a + ": ");
79: g = KBD.nextInt();
80: KBD.nextLine();
81: while (!Student.isValidGrade(g)) {
82: System.out.print("Please try again: ");
83: g = KBD.nextInt();
84: KBD.nextLine();
85: }
86: myStudents[s].setAsgnGrade(a, g);
87: }
88: pause();
89: }
90: }
92: /**
93: * Print a full report on the students' grades, including each assignment
94: * grade, overall grade, and class average.
95: *
96: * @param myStudents the class list
97: */
98: private static void printGradesReport(Student[] myStudents) {
99: int sum;
100: double avg;
102: // print table header
103: System.out.printf("%-10s%-20s%4s%4s",
104: "ANumber", "Name", "Pct", "Let");
105: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
106: System.out.printf("%4s", String.format("A%02d", a));
107: }
108: System.out.println();
109: System.out.printf("%-10s%-20s%4s%4s",
110: "-------", "----", "---", "---");
111: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
112: System.out.printf("%4s", "---");
113: }
114: System.out.println();
116: // print table body
117: for (int s = 0; s < myStudents.length; ++s) {
118: System.out.printf("%-10s%-20s%4d %-2s",
119: myStudents[s].getANumber(),
120: myStudents[s].getName(),
121: myStudents[s].getPctGrade(),
122: myStudents[s].getLetterGrade());
123: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
124: System.out.printf("%4d", myStudents[s].getAsgnGrade(a));
125: }
126: System.out.println();
127: }
128: pause();
130: // get class average
131: sum = 0;
132: for (int s = 0; s < myStudents.length; ++s) {
133: sum += myStudents[s].getPctGrade();
134: }
135: avg = (double)sum / (double)myStudents.length;
136: System.out.printf("\n"
137: + "Class average: %4.1f\n\n",
138: avg);
139: pause();
140: }
142: /**
143: * Prompt the user and wait for them to press the enter key.
144: */
145: private static void pause() {
146: System.out.print("\n...press enter...");
147: KBD.nextLine();
148: System.out.println();
149: }
151: }