public class Student
1: import java.util.Arrays;
2: import java.util.Comparator;
4: /**
5: * A class to hold some very basic information about a student.
6: * Made sortable by various Comparable objects.
7: * NOTE: this version of Student does not implement Comparable<Student>.
8: * All sorting must be done using one of the provided Comparators
9: * or by using another user-defined Comparator.
10: *
11: * @author Mark Young (A00000000)
12: */
13: public class Student {
15: // ---------- Comparators available ------------------------------- //
16: public static final Comparator<Student> BY_NAME
17: = (Student one, Student other) -> {
18: return one.getName().compareTo(other.getName());
19: };
20: public static final Comparator<Student> BY_GRADE
21: = (Student one, Student other) -> {
22: return other.getAverage() - one.getAverage();
23: };
24: public static final Comparator<Student> BY_ANUMBER
25: = (Student one, Student other) -> {
26: return one.A_NUMBER.compareTo(other.A_NUMBER);
27: };
29: // ---------- Instance variables (and constants) ------------------ //
31: /** The student's A-Number */
32: public final String A_NUMBER;
33: /** The student's name */
34: private String name;
35: /** The student's grades (as percentages) */
36: private int[] asgnGrades;
39: // ---------- Class variables (and constants) --------------------- //
41: /** The number of student objects so far */
42: private static int numberOfStudents = 0;
43: /** Number of assignments graded so far */
44: private static int asgnsGraded = 0;
46: /** Total number of assignments */
47: public static final int NUM_ASGN = 8;
48: /** Maximum possible percentage grade */
49: public static final int MAX_GRADE = 100;
50: /** Minimum possible percentage grade */
51: public static final int MIN_GRADE = 0;
54: // ---------- Constructors ---------------------------------------- //
56: /**
57: * Create a Student object.
58: *
59: * @param initialName the requested name
60: */
61: public Student(String initialName) {
62: // count this student we're creating
63: ++numberOfStudents;
65: // assign next A-Number to this Student
66: A_NUMBER = String.format("A%08d", numberOfStudents);
68: // set personal data
69: name = initialName;
71: // set academic data
72: asgnGrades = new int[NUM_ASGN];
73: }
76: // ---------- Getters --------------------------------------------- //
78: /**
79: * Get the value of A_NUMBER
80: *
81: * @return the value of A_NUMBER
82: */
83: public String getANumber() {
84: return A_NUMBER;
85: }
87: /**
88: * Get the value of name
89: *
90: * @return the value of name
91: */
92: public String getName() {
93: return this.name;
94: }
96: /**
97: * Get the assignment grades
98: *
99: * @return the array of graded assignment scores
100: */
101: public int[] getAsgnGrades() {
102: return Arrays.copyOf(asgnGrades, asgnsGraded);
103: }
105: /**
106: * Get one assignment grade
107: *
108: * @return the grade for the given assignment, or zero if it doesn't exist
109: */
110: public int getAsgnGrade(int asgnNum) {
111: // if it's a valid assignment number AND it's been released
112: if (!isValidAsgnNum(asgnNum)) {
113: throw new NoSuchAssignmentException(asgnNum);
114: }
115: if (asgnNum > asgnsGraded) {
116: return 0;
117: }
118: return asgnGrades[asgnNum - 1];
119: }
121: /**
122: * get letter grade for this student
123: *
124: * @return the letter grade equivalent to this student's percentage grade
125: */
126: public String getLetterGrade() {
127: return letterGradeFor(getAverage());
128: }
130: /**
131: * Calculate and return this student's average assignment grade.
132: *
133: * @return the rounded average of this student's graded assignments
134: * or 0 if no assignments have been graded.
135: */
136: public int getAverage() {
137: int sum = 0;
138: for (int i = 0; i < asgnsGraded; ++i) {
139: sum += asgnGrades[i];
140: }
141: if (asgnsGraded == 0) {
142: return 0;
143: } else {
144: return (int)Math.round((double)sum / (double)asgnsGraded);
145: }
146: }
148: /**
149: * Get the student's grade on a single assignment.
150: * Note: assignments are numbered starting at 1.
151: *
152: * @param asgnNum the number of an assignment (1 .. NUM_ASGN)
153: * @return this student's grade on that assignment
154: */
155: public int getGrade(int asgnNum) {
156: if (!isValidAsgnNum(asgnNum)) {
157: throw new NoSuchAssignmentException(asgnNum);
158: }
159: return asgnGrades[asgnNum - 1];
160: }
163: // ---------- Setters --------------------------------------------- //
165: /**
166: * Set the value of name
167: *
168: * @param newName new value of name
169: */
170: public void setName(String newName) {
171: this.name = newName;
172: }
174: /**
175: * Set the assignment grades
176: *
177: * @param newGrades array with suitable grades
178: */
179: public void setAsgnGrades(int[] newGrades) {
180: if (!areValidGrades(newGrades)) {
181: throw new InvalidGradeException(Arrays.toString(newGrades));
182: }
183: asgnGrades = Arrays.copyOf(newGrades, NUM_ASGN);
184: }
186: /**
187: * Set one assignment's grade
188: *
189: * @param asgnNum the number of the assignment (1-based)
190: * @param newGrade the grade for that assignment
191: */
192: public void setAsgnGrade(int asgnNum, int newGrade) {
193: if (!isValidAsgnNum(asgnNum)) {
194: throw new NoSuchAssignmentException(asgnNum);
195: }
196: if (!isValidGrade(newGrade)) {
197: throw new InvalidGradeException(newGrade);
198: }
199: asgnGrades[asgnNum - 1] = newGrade;
200: }
203: // ---------- Other observer methods ------------------------------ //
205: /**
206: * Print a brief summary of the student's data.
207: */
208: public void printReport() {
209: System.out.println("\nNumber: " + this.A_NUMBER);
210: System.out.println("Name: " + this.name);
211: System.out.println("Grade: " + this.getAverage());
212: System.out.println("Letter: " + this.getLetterGrade());
213: System.out.print("\t");
214: for (int a = 1; a <= asgnsGraded; ++a) {
215: System.out.printf("%6s", String.format("A%02d", a));
216: }
217: System.out.println();
218: System.out.print("\t");
219: for (int a = 1; a <= asgnsGraded; ++a) {
220: System.out.printf("%6d", this.getAsgnGrade(a));
221: }
222: System.out.println();
223: System.out.println();
224: }
226: /**
227: * Create a String to represent this Student.
228: * This version uses their name and A-number.
229: *
230: * @return a String representing this Student
231: */
232: @Override
233: public String toString() {
234: return this.name + " (" + this.A_NUMBER + ")";
235: }
238: // ---------- Class methods --------------------------------------- //
240: /**
241: * Return whether a given grade is in the valid range.
242: *
243: * @param g the grade to check for validity
244: * @return whether g is in the correct range.
245: */
246: public static boolean isValidGrade(int g) {
247: return (MIN_GRADE <= g && g <= MAX_GRADE);
248: }
250: /**
251: * Return whether a list of grades is in the valid range.
252: *
253: * @param gg the list of grades to check for validity
254: * @return whether gg is of a suitable length with all values
255: * in the correct range.
256: */
257: public static boolean areValidGrades(int[] gg) {
258: if (gg.length > NUM_ASGN) {
259: return false;
260: }
261: for (int i = 0; i < gg.length; ++i) {
262: if (!isValidGrade(gg[i])) {
263: return false;
264: }
265: }
266: return true;
267: }
269: /**
270: * Return whether an assignment number is in the valid range
271: *
272: * @param asgnNum the supposed assignment number
273: * @return whether asgnNum is in the range 1..NUM_ASGN
274: */
275: public static boolean isValidAsgnNum(int asgnNum) {
276: return 1 <= asgnNum && asgnNum <= NUM_ASGN;
277: }
279: /**
280: * Return the letter grade corresponding to the given grade.
281: *
282: * @param g the percentage grade to be converted to a letter grade.
283: * @return the letter grade corresponding to g.
284: */
285: public static String letterGradeFor(int g) {
286: if (g < 50) {
287: return "F";
288: } else if (g < 60) {
289: return "D";
290: } else if (g < 70) {
291: return "C";
292: } else if (g < 80) {
293: return "B";
294: } else {
295: return "A";
296: }
297: }
299: /**
300: * Set the number of graded assignments.
301: *
302: * @param released the number of the latest assignment released
303: */
304: public static void releaseAssignment(int released) {
305: if (!isValidAsgnNum(released)) {
306: throw new NoSuchAssignmentException(released);
307: }
308: asgnsGraded = released;
309: }
311: }