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(); kbd.nextLine();
49: myStudents = new Student[numStu];
51: // create student objects
52: System.out.println("What are their numbers and names? ");
53: for (int s = 0; s < numStu; s++) {
54: System.out.print("Enter a student's A-Number: ");
55: number = kbd.nextLine();
56: System.out.print("Enter their name: ");
57: name = kbd.nextLine();
58: myStudents[s] = new Student(number, name);
59: }
60: pause();
62: return myStudents;
63: }
65: /**
66: * Read grades for all students in the class list.
67: *
68: * @param myStudents the class list
69: */
70: private static void readGrades(Student[] myStudents) {
71: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
72: System.out.println("Enter grades for assignment #" + a + ".");
73: pause();
74: for (int s = 0; s < myStudents.length; ++s) {
75: int g;
76: System.out.print("Enter " + myStudents[s].getName()
77: + "'s grade for A#" + a + ": ");
78: g = kbd.nextInt();
79: kbd.nextLine();
80: while (!Student.isValidGrade(g)) {
81: System.out.print("Please try again: ");
82: g = kbd.nextInt();
83: kbd.nextLine();
84: }
85: myStudents[s].setAsgnGrade(a, g);
86: }
87: pause();
88: }
89: }
91: /**
92: * Print a full report on the students' grades, including each assignment
93: * grade, overall grade, and class average.
94: *
95: * @param myStudents the class list
96: */
97: private static void printGradesReport(Student[] myStudents) {
98: int sum;
99: double avg;
101: // print table header
102: System.out.printf("%-10s%-20s%4s%4s",
103: "ANumber", "Name", "Pct", "Let");
104: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
105: System.out.printf("%4s", String.format("A%02d", a));
106: }
107: System.out.println();
108: System.out.printf("%-10s%-20s%4s%4s",
109: "-------", "----", "---", "---");
110: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
111: System.out.printf("%4s", "---");
112: }
113: System.out.println();
115: // print table body
116: for (int s = 0; s < myStudents.length; ++s) {
117: System.out.printf("%-10s%-20s%4d%4s",
118: myStudents[s].getANumber(),
119: myStudents[s].getName(),
120: myStudents[s].getPctGrade(),
121: myStudents[s].getLetterGrade());
122: for (int a = 1; a <= Student.NUM_ASGN; ++a) {
123: System.out.printf("%4d", myStudents[s].getAsgnGrade(a));
124: }
125: System.out.println();
126: }
127: pause();
129: // get class average
130: sum = 0;
131: for (int s = 0; s < myStudents.length; ++s) {
132: sum += myStudents[s].getPctGrade();
133: }
134: avg = (double)sum / (double)myStudents.length;
135: System.out.printf("\n"
136: + "Class average: %4.1f\n\n",
137: avg);
138: pause();
139: }
140: private static void pause() {
141: System.out.print("\n...press enter...");
142: kbd.nextLine();
143: System.out.println();
144: }
146: }