public class Student
1: import java.util.Arrays;
3: /**
4: * A class to hold some very basic information about a student.
5: *
6: * @author Mark Young (A00000000)
7: */
8: public class Student {
10: // ---------- Instance variables (and constants) ------------------ //
12: /** The student's A-Number */
13: public final String A_NUMBER;
14: /** The student's name */
15: private String name;
16: /** The student's grades (as percentages) */
17: private int[] asgnGrades;
20: // ---------- Class variables (and constants) --------------------- //
22: /** The number of student objects so far */
23: private static int numberOfStudents = 0;
24: /** Number of assignments graded so far */
25: private static int asgnsGraded = 0;
27: /** Total number of assignments */
28: public static final int NUM_ASGN = 8;
29: /** Maximum possible percentage grade */
30: public static final int MAX_GRADE = 100;
31: /** Minimum possible percentage grade */
32: public static final int MIN_GRADE = 0;
35: // ---------- Constructors ---------------------------------------- //
37: /**
38: * Create a Student object.
39: *
40: * @param initialName the requested name
41: */
42: public Student(String initialName) {
43: // count this student we're creating
44: ++numberOfStudents;
46: // assign next A-Number to this Student
47: A_NUMBER = String.format("A%08d", numberOfStudents);
49: // set personal data
50: name = initialName;
52: // set academic data
53: asgnGrades = new int[NUM_ASGN];
54: }
57: // ---------- Getters --------------------------------------------- //
59: /**
60: * Get the value of A_NUMBER
61: *
62: * @return the value of A_NUMBER
63: */
64: public String getANumber() {
65: return A_NUMBER;
66: }
68: /**
69: * Get the value of name
70: *
71: * @return the value of name
72: */
73: public String getName() {
74: return this.name;
75: }
77: /**
78: * Get the assignment grades
79: *
80: * @return the array of graded assignment scores
81: */
82: public int[] getAsgnGrades() {
83: return Arrays.copyOf(asgnGrades, asgnsGraded);
84: }
86: /**
87: * Get one assignment grade
88: *
89: * @return the grade for the given assignment, or zero if it doesn't exist
90: */
91: public int getAsgnGrade(int asgnNum) {
92: // if it's a valid assignment number AND it's been released
93: if (isValidAsgnNum(asgnNum) && asgnNum <= asgnsGraded) {
94: return asgnGrades[asgnNum - 1];
95: } else {
96: return 0;
97: }
98: }
100: /**
101: * get letter grade for this student
102: *
103: * @return the letter grade equivalent to this student's percentage grade
104: */
105: public String getLetterGrade() {
106: return letterGradeFor(getPctGrade());
107: }
109: /**
110: * Calculate and return this student's average assignment grade.
111: *
112: * @return the rounded average of this student's graded assignments
113: * or 0 if no assignments have been graded.
114: */
115: public int getPctGrade() {
116: int sum = 0;
117: for (int i = 0; i < asgnsGraded; ++i) {
118: sum += asgnGrades[i];
119: }
120: if (asgnsGraded == 0) {
121: return 0;
122: } else {
123: return (int)Math.round((double)sum / (double)asgnsGraded);
124: }
125: }
127: /**
128: * Get the student's grade on a single assignment.
129: * Note: assignments are numbered starting at 1.
130: *
131: * @param asgnNum the number of an assignment (1 .. NUM_ASGN)
132: * @return this student's grade on that assignment
133: */
134: public int getGrade(int asgnNum) {
135: if (isValidAsgnNum(asgnNum)) {
136: return asgnGrades[asgnNum - 1];
137: }
138: return 0;
139: }
142: // ---------- Setters --------------------------------------------- //
144: /**
145: * Set the value of name
146: *
147: * @param newName new value of name
148: */
149: public void setName(String newName) {
150: this.name = newName;
151: }
153: /**
154: * Set the assignment grades
155: *
156: * @param newGrades array with suitable grades
157: */
158: public void setAsgnGrades(int[] newGrades) {
159: if (areValidGrades(newGrades)) {
160: asgnGrades = Arrays.copyOf(newGrades, NUM_ASGN);
161: }
162: }
164: /**
165: * Set one assignment's grade
166: *
167: * @param asgnNum the number of the assignment (1-based)
168: * @param newGrade the grade for that assignment
169: */
170: public void setAsgnGrade(int asgnNum, int newGrade) {
171: if (isValidAsgnNum(asgnNum) && isValidGrade(newGrade)) {
172: asgnGrades[asgnNum - 1] = newGrade;
173: }
174: }
177: // ---------- Other observer methods ------------------------------ //
179: /**
180: * Print a brief summary of the student's data.
181: */
182: public void printStudentRecord() {
183: System.out.println("Number: " + this.A_NUMBER);
184: System.out.println("Name: " + this.name);
185: System.out.println("Grade: " + this.getPctGrade());
186: System.out.println("Letter: " + this.getLetterGrade());
187: }
189: /**
190: * Create a String to represent this Student.
191: * This version uses their name and A-number.
192: *
193: * @return a String representing this Student
194: */
195: @Override
196: public String toString() {
197: return this.name + " (" + this.A_NUMBER + ")";
198: }
201: // ---------- Class methods --------------------------------------- //
203: /**
204: * Return whether a given grade is in the valid range.
205: *
206: * @param g the grade to check for validity
207: * @return whether g is in the correct range.
208: */
209: public static boolean isValidGrade(int g) {
210: return (MIN_GRADE <= g && g <= MAX_GRADE);
211: }
213: /**
214: * Return whether a list of grades is in the valid range.
215: *
216: * @param gg the list of grades to check for validity
217: * @return whether gg is of a suitable length with all values
218: * in the correct range.
219: */
220: public static boolean areValidGrades(int[] gg) {
221: if (gg.length > NUM_ASGN) {
222: return false;
223: }
224: for (int i = 0; i < gg.length; ++i) {
225: if (!isValidGrade(gg[i])) {
226: return false;
227: }
228: }
229: return true;
230: }
232: /**
233: * Return whether an assignment number is in the valid range
234: *
235: * @param asgnNum the supposed assignment number
236: * @return whether asgnNum is in the range 1..NUM_ASGN
237: */
238: public static boolean isValidAsgnNum(int asgnNum) {
239: return 1 <= asgnNum && asgnNum <= NUM_ASGN;
240: }
242: /**
243: * Return the letter grade corresponding to the given grade.
244: *
245: * @param g the percentage grade to be converted to a letter grade.
246: * @return the letter grade corresponding to g.
247: */
248: public static String letterGradeFor(int g) {
249: if (g < 50) {
250: return "F";
251: } else if (g < 60) {
252: return "D";
253: } else if (g < 70) {
254: return "C";
255: } else if (g < 80) {
256: return "B";
257: } else {
258: return "A";
259: }
260: }
262: /**
263: * Set the number of graded assignments.
264: *
265: * @param released the number of the latest assignment released
266: */
267: public static void releaseAssignment(int released) {
268: if (isValidAsgnNum(released)) {
269: asgnsGraded = released;
270: }
271: }
273: }