public class Student
2: import java.util.Arrays;
4: /**
5: * A class to hold some very basic information about a student. Added an array
6: * of grades (assignment grades) to replace the one percentage grade of earlier
7: * versions.
8: *
9: * @author Mark Young (A00000000)
10: */
11: public class Student {
13: // ---------- Class Variables/Constants ---------------------------- //
14: public static final int NUM_ASGN = 8;
15: public static final int NUM_DROPS = 2;
16: public static final int MAX_PCT_GRADE = 100;
17: public static final int INVALID_GRADE = -1;
19: // ---------- Instance Variables/Constants ------------------------- //
20: public final String aNumber;
21: private String name;
22: private int[] asgnGrades;
23:
24: // ---------- Constructor ------------------------------------------ //
25: /**
26: * Create a Student object.
27: *
28: * @param number the requested number
29: * @param name the requested name
30: */
31: public Student(String reqNumber, String reqName) {
32: aNumber = reqNumber;
33: name = reqName;
34: asgnGrades = new int[NUM_ASGN];
35: // array starts off all zeroes, which is what we want,
36: // so no need to set each assignments[i]
37: }
39:
40: // ---------- Getters ---------------------------------------------- //
41: /**
42: * Get the value of aNumber
43: *
44: * @return the value of aNumber
45: */
46: public String getANumber() {
47: return aNumber;
48: }
49:
50: /**
51: * Get the value of name
52: *
53: * @return the value of name
54: */
55: public String getName() {
56: return name;
57: }
59: /**
60: * Get the student's percentage grade. Current version is based on their
61: * assignment grade.
62: *
63: * @return this Student's course percent grade
64: */
65: public int getPctGrade() {
66: // TODO: add labs and test grades
67: return getAsgnsGrade();
68: }
70: /**
71: * get letter grade for this student
72: *
73: * @return the letter grade equivalent to this student's percentage grade
74: */
75: public String getLetterGrade() {
76: return Student.letterGradeFor(this.getPctGrade());
77: }
79: /**
80: * Get student's grade on a particular assignment.
81: *
82: * @param a the assignment requested
83: * @return the grade on the requested assignment OR INVALID_GRADE if there
84: * is no such assignment
85: */
86: public int getAsgnGrade(int a) {
87: if (inAsgnRange(a)) {
88: return asgnGrades[a - 1];
89: } else {
90: return INVALID_GRADE;
91: }
92: }
94: /**
95: * Get the student's overall assignment grade -- currently just the average
96: * of all assignment grades.
97: *
98: * @return the overall assignment grade for this student
99: */
100: public int getAsgnsGrade() {
101: int[] drops = getSmallest(asgnGrades, NUM_DROPS);
102: int sum = sumArray(asgnGrades) - sumArray(drops);
103: int numCounted = NUM_ASGN - NUM_DROPS;
105: return (int)Math.round((double)sum / numCounted);
106: }
108: /**
109: * Return a copy of this Student's assignment grades array.
110: *
111: * @param this Student's grades in an array
112: */
113: public int[] getAsgnGrades() {
114: return Arrays.copyOf(asgnGrades, NUM_ASGN);
115: }
117: // ---------- Setters ---------------------------------------------- //
118: /**
119: * Set the value of name
120: *
121: * @param name new value of name
122: */
123: public void setName(String name) {
124: this.name = name;
125: }
127: /**
128: * Set the value of an assignment grade.
129: *
130: * @param a number of assignment this grade is for
131: * @param g new assignment grade (must be 0..100)
132: */
133: public void setAsgnGrade(int a, int g) {
134: if (inAsgnRange(a) && isValidGrade(g)) {
135: asgnGrades[a - 1] = g;
136: }
137: // otherwise fail without warning(!)
138: }
140: // ---------- Observers -------------------------------------------- //
141: /**
142: * Print a brief summary of the student's data.
143: */
144: public void printRecord() {
145: System.out.println("\tNumber: " + aNumber);
146: System.out.println("\tName: " + name);
147: System.out.println("\tGrade: " + getPctGrade());
148: System.out.println("\tLetter: " + this.getLetterGrade());
149: }
151: /**
152: * Create a String to represent this Student. This version uses their name
153: * and A-number.
154: *
155: * @return a String representing this Student
156: */
157: @Override
158: public String toString() {
159: return name + " (" + aNumber + ")";
160: }
162: // ---------- Class Methods ---------------------------------------- //
163: /**
164: * Return whether a given grade is in the valid range.
165: *
166: * @param g the grade to check for validity
167: * @return whether g is in the correct range.
168: * @since 2.0
169: */
170: public static boolean isValidGrade(int g) {
171: return (0 <= g && g <= MAX_PCT_GRADE);
172: }
174: /**
175: * Return whether a given number is a valid assignment number.
176: *
177: * @param a the number to test for validity
178: * @return whether a is in the correct range
179: * @since 3.0
180: */
181: public static boolean inAsgnRange(int a) {
182: return 1 <= a && a <= NUM_ASGN;
183: }
185: /**
186: * Return the letter grade corresponding to the given grade.
187: *
188: * @param g the percentage grade to be converted to a letter grade.
189: * @return the letter grade corresponding to g.
190: * @since 2.0
191: */
192: public static String letterGradeFor(int g) {
193: if (g < 50) {
194: return "F";
195: } else if (g < 60) {
196: return "D";
197: } else if (g < 63) {
198: return "C-";
199: } else if (g < 67) {
200: return "C";
201: } else if (g < 70) {
202: return "C+";
203: } else if (g < 73) {
204: return "B-";
205: } else if (g < 77) {
206: return "B";
207: } else if (g < 80) {
208: return "B+";
209: } else if (g < 85) {
210: return "A-";
211: } else if (g < 90) {
212: return "A";
213: } else {
214: return "A+";
215: }
216: }
218: /**
219: * Sum the elements of an array.
220: *
221: * @param arr the array to sum the elements of
222: * @return the sum of the elements of arr
223: */
224: private static int sumArray(int[] arr) {
225: int sum = 0;
226: for (int i = 0; i < arr.length; ++i) {
227: sum += arr[i];
228: }
229: return sum;
230: }
232: /**
233: * Return an array containing the lowest values from an array.
234: *
235: * @param arr the array to find the lowest values in
236: * @param n the number of lowest values to find
237: * @return an array containing the n smallest values of arr
238: */
239: private static int[] getSmallest(int[] arr, int n) {
240: int[] copy = Arrays.copyOf(arr, arr.length);
241: Arrays.sort(copy);
242: return Arrays.copyOfRange(copy, 0, n);
243: }
245: }