Source of Student.java


  1: import java.util.Arrays;

  3: /**
  4:  * A class to hold some very basic information about a student.
  5:  * Made sortable by grade (descending).
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class Student implements Comparable<Student> {

 11:     // ---------- Comparable requires compareTo metho ----------------- //
 12:     /** Sort Students by their percentage grade in descending order */
 13:     public int compareTo(Student other) {
 14:         return other.getAverage() - this.getAverage();
 15:     }

 17:     // ---------- Instance variables (and constants) ------------------ //

 19:     /** The student's A-Number */
 20:     public final String A_NUMBER;
 21:     /** The student's name */
 22:     private String name;
 23:     /** The student's grades (as percentages) */
 24:     private int[] asgnGrades;


 27:     // ---------- Class variables (and constants) --------------------- //

 29:     /** The number of student objects so far */
 30:     private static int numberOfStudents = 0;
 31:     /** Number of assignments graded so far */
 32:     private static int asgnsGraded = 0;

 34:     /** Total number of assignments */
 35:     public static final int NUM_ASGN = 8;
 36:     /** Maximum possible percentage grade */
 37:     public static final int MAX_GRADE = 100;
 38:     /** Minimum possible percentage grade */
 39:     public static final int MIN_GRADE = 0;


 42:     // ---------- Constructors ---------------------------------------- //

 44:     /**
 45:      * Create a Student object.
 46:      *
 47:      * @param initialName      the requested name
 48:      */
 49:     public Student(String initialName) {
 50:         // count this student we're creating
 51:         ++numberOfStudents;

 53:         // assign next A-Number to this Student
 54:         A_NUMBER = String.format("A%08d", numberOfStudents);

 56:         // set personal data
 57:         name = initialName;

 59:         // set academic data
 60:         asgnGrades = new int[NUM_ASGN];
 61:     }


 64:     // ---------- Getters --------------------------------------------- //

 66:     /**
 67:      * Get the value of A_NUMBER
 68:      *
 69:      * @return the value of A_NUMBER
 70:      */
 71:     public String getANumber() {
 72:         return A_NUMBER;
 73:     }

 75:     /**
 76:      * Get the value of name
 77:      *
 78:      * @return the value of name
 79:      */
 80:     public String getName() {
 81:         return this.name;
 82:     }

 84:     /**
 85:      * Get the assignment grades
 86:      *
 87:      * @return the array of graded assignment scores
 88:      */
 89:     public int[] getAsgnGrades() {
 90:         return Arrays.copyOf(asgnGrades, asgnsGraded);
 91:     }

 93:     /**
 94:      * Get one assignment grade
 95:      *
 96:      * @return the grade for the given assignment, or zero if it doesn't exist
 97:      */
 98:     public int getAsgnGrade(int asgnNum) {
 99:         // if it's a valid assignment number AND it's been released
100:         if (isValidAsgnNum(asgnNum) && asgnNum <= asgnsGraded) {
101:             return asgnGrades[asgnNum - 1];
102:         } else {
103:             return 0;
104:         }
105:     }

107:     /**
108:      * get letter grade for this student
109:      *
110:      * @return  the letter grade equivalent to this student's percentage grade
111:      */
112:     public String getLetterGrade() {
113:         return letterGradeFor(getAverage());
114:     }

116:     /**
117:      * Calculate and return this student's average assignment grade.
118:      *
119:      * @return the rounded average of this student's graded assignments
120:      *         or 0 if no assignments have been graded.
121:      */
122:     public int getAverage() {
123:         int sum = 0;
124:         for (int i = 0; i < asgnsGraded; ++i) {
125:             sum += asgnGrades[i];
126:         }
127:         if (asgnsGraded == 0) {
128:             return 0;
129:         } else {
130:             return (int)Math.round((double)sum / (double)asgnsGraded);
131:         }
132:     }

134:     /**
135:      * Get the student's grade on a single assignment.
136:      * Note: assignments are numbered starting at 1.
137:      *
138:      * @param asgnNum   the number of an assignment (1 .. NUM_ASGN)
139:      * @return  this student's grade on that assignment
140:      */
141:     public int getGrade(int asgnNum) {
142:         if (isValidAsgnNum(asgnNum)) {
143:             return asgnGrades[asgnNum - 1];
144:         }
145:         return 0;
146:     }


149:     // ---------- Setters --------------------------------------------- //

151:     /**
152:      * Set the value of name
153:      *
154:      * @param newName new value of name
155:      */
156:     public void setName(String newName) {
157:         this.name = newName;
158:     }

160:     /**
161:      * Set the assignment grades
162:      *
163:      * @param newGrades  array with suitable grades
164:      */
165:     public void setAsgnGrades(int[] newGrades) {
166:         if (areValidGrades(newGrades)) {
167:             asgnGrades = Arrays.copyOf(newGrades, NUM_ASGN);
168:         }
169:     }

171:     /**
172:      * Set one assignment's grade
173:      *
174:      * @param asgnNum   the number of the assignment (1-based)
175:      * @param newGrade  the grade for that assignment
176:      */
177:     public void setAsgnGrade(int asgnNum, int newGrade) {
178:         if (isValidAsgnNum(asgnNum) && isValidGrade(newGrade)) {
179:             asgnGrades[asgnNum - 1] = newGrade;
180:         }
181:     }


184:     // ---------- Other observer methods ------------------------------ //

186:     /**
187:      * Print a brief summary of the student's data.
188:      */
189:     public void printStudentRecord() {
190:         System.out.println("\n\tNumber: " + this.A_NUMBER);
191:         System.out.println("\tName:   " + this.name);
192:         System.out.println("\tGrade:  " + this.getAverage());
193:         System.out.println("\tLetter: " + this.getLetterGrade());
194:         System.out.println();
195:     }

197:     /**
198:      * Create a String to represent this Student.
199:      * This version uses their name and A-number.
200:      *
201:      * @return  a String representing this Student
202:      */
203:     @Override
204:     public String toString() {
205:         return this.name + " (" + this.A_NUMBER + ")";
206:     }


209:     // ---------- Class methods --------------------------------------- //

211:     /**
212:      * Return whether a given grade is in the valid range.
213:      *
214:      * @param g     the grade to check for validity
215:      * @return  whether g is in the correct range.
216:      */
217:     public static boolean isValidGrade(int g) {
218:         return (MIN_GRADE <= g && g <= MAX_GRADE);
219:     }

221:     /**
222:      * Return whether a list of grades is in the valid range.
223:      *
224:      * @param gg    the list of grades to check for validity
225:      * @return  whether gg is of a suitable length with all values
226:      *          in the correct range.
227:      */
228:     public static boolean areValidGrades(int[] gg) {
229:         if (gg.length > NUM_ASGN) {
230:             return false;
231:         }
232:         for (int i = 0; i < gg.length; ++i) {
233:             if (!isValidGrade(gg[i])) {
234:                 return false;
235:             }
236:         }
237:         return true;
238:     }

240:     /**
241:      * Return whether an assignment number is in the valid range
242:      *
243:      * @param asgnNum   the supposed assignment number
244:      * @return  whether asgnNum is in the range 1..NUM_ASGN
245:      */
246:     public static boolean isValidAsgnNum(int asgnNum) {
247:         return 1 <= asgnNum && asgnNum <= NUM_ASGN;
248:     }

250:     /**
251:      * Return the letter grade corresponding to the given grade.
252:      *
253:      * @param g     the percentage grade to be converted to a letter grade.
254:      * @return  the letter grade corresponding to g.
255:      */
256:     public static String letterGradeFor(int g) {
257:         if (g < 50) {
258:             return "F";
259:         } else if (g < 60) {
260:             return "D";
261:         } else if (g < 70) {
262:             return "C";
263:         } else if (g < 80) {
264:             return "B";
265:         } else {
266:             return "A";
267:         }
268:     }

270:     /**
271:      * Set the number of graded assignments.
272:      *
273:      * @param released  the number of the latest assignment released
274:      */
275:     public static void releaseAssignment(int released) {
276:         if (isValidAsgnNum(released)) {
277:             asgnsGraded = released;
278:         }
279:     }

281: }