Source of Student.java


  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 Compaable<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:                 one.getName().compareTo(other.getName());

 20:     public static final Comparator<Student> BY_GRADE =
 21:             (Student one, Student other) -> 
 22:                 other.getAverage() - one.getAverage();

 24:     public static final Comparator<Student> BY_ANUMBER =
 25:             (Student one, Student other) -> 
 26:                 one.A_NUMBER.compareTo(other.A_NUMBER);

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

 30:     /** The student's A-Number */
 31:     public final String A_NUMBER;
 32:     /** The student's name */
 33:     private String name;
 34:     /** The student's grades (as percentages) */
 35:     private int[] asgnGrades;


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

 40:     /** The number of student objects so far */
 41:     private static int numberOfStudents = 0;
 42:     /** Number of assignments graded so far */
 43:     private static int asgnsGraded = 0;

 45:     /** Total number of assignments */
 46:     public static final int NUM_ASGN = 8;
 47:     /** Maximum possible percentage grade */
 48:     public static final int MAX_GRADE = 100;
 49:     /** Minimum possible percentage grade */
 50:     public static final int MIN_GRADE = 0;


 53:     // ---------- Constructors ---------------------------------------- //

 55:     /**
 56:      * Create a Student object.
 57:      *
 58:      * @param initialName      the requested name
 59:      */
 60:     public Student(String initialName) {
 61:         // count this student we're creating
 62:         ++numberOfStudents;

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

 67:         // set personal data
 68:         name = initialName;

 70:         // set academic data
 71:         asgnGrades = new int[NUM_ASGN];
 72:     }


 75:     // ---------- Getters --------------------------------------------- //

 77:     /**
 78:      * Get the value of A_NUMBER
 79:      *
 80:      * @return the value of A_NUMBER
 81:      */
 82:     public String getANumber() {
 83:         return A_NUMBER;
 84:     }

 86:     /**
 87:      * Get the value of name
 88:      *
 89:      * @return the value of name
 90:      */
 91:     public String getName() {
 92:         return this.name;
 93:     }

 95:     /**
 96:      * Get the assignment grades
 97:      *
 98:      * @return the array of graded assignment scores
 99:      */
100:     public int[] getAsgnGrades() {
101:         return Arrays.copyOf(asgnGrades, asgnsGraded);
102:     }

104:     /**
105:      * Get one assignment grade
106:      *
107:      * @return the grade for the given assignment, or zero if it doesn't exist
108:      */
109:     public int getAsgnGrade(int asgnNum) {
110:         // if it's a valid assignment number AND it's been released
111:         if (isValidAsgnNum(asgnNum) && asgnNum <= asgnsGraded) {
112:             return asgnGrades[asgnNum - 1];
113:         } else {
114:             return 0;
115:         }
116:     }

118:     /**
119:      * get letter grade for this student
120:      *
121:      * @return  the letter grade equivalent to this student's percentage grade
122:      */
123:     public String getLetterGrade() {
124:         return letterGradeFor(getAverage());
125:     }

127:     /**
128:      * Calculate and return this student's average assignment grade.
129:      *
130:      * @return the rounded average of this student's graded assignments
131:      *         or 0 if no assignments have been graded.
132:      */
133:     public int getAverage() {
134:         int sum = 0;
135:         for (int i = 0; i < asgnsGraded; ++i) {
136:             sum += asgnGrades[i];
137:         }
138:         if (asgnsGraded == 0) {
139:             return 0;
140:         } else {
141:             return (int)Math.round((double)sum / (double)asgnsGraded);
142:         }
143:     }

145:     /**
146:      * Get the student's grade on a single assignment.
147:      * Note: assignments are numbered starting at 1.
148:      *
149:      * @param asgnNum   the number of an assignment (1 .. NUM_ASGN)
150:      * @return  this student's grade on that assignment
151:      */
152:     public int getGrade(int asgnNum) {
153:         if (isValidAsgnNum(asgnNum)) {
154:             return asgnGrades[asgnNum - 1];
155:         }
156:         return 0;
157:     }


160:     // ---------- Setters --------------------------------------------- //

162:     /**
163:      * Set the value of name
164:      *
165:      * @param newName new value of name
166:      */
167:     public void setName(String newName) {
168:         this.name = newName;
169:     }

171:     /**
172:      * Set the assignment grades
173:      *
174:      * @param newGrades  array with suitable grades
175:      */
176:     public void setAsgnGrades(int[] newGrades) {
177:         if (areValidGrades(newGrades)) {
178:             asgnGrades = Arrays.copyOf(newGrades, NUM_ASGN);
179:         }
180:     }

182:     /**
183:      * Set one assignment's grade
184:      *
185:      * @param asgnNum   the number of the assignment (1-based)
186:      * @param newGrade  the grade for that assignment
187:      */
188:     public void setAsgnGrade(int asgnNum, int newGrade) {
189:         if (isValidAsgnNum(asgnNum) && isValidGrade(newGrade)) {
190:             asgnGrades[asgnNum - 1] = newGrade;
191:         }
192:     }


195:     // ---------- Other observer methods ------------------------------ //

197:     /**
198:      * Print a brief summary of the student's data.
199:      */
200:     public void printStudentRecord() {
201:         System.out.println("\n\tNumber: " + this.A_NUMBER);
202:         System.out.println("\tName:   " + this.name);
203:         System.out.println("\tGrade:  " + this.getAverage());
204:         System.out.println("\tLetter: " + this.getLetterGrade());
205:         System.out.println();
206:     }

208:     /**
209:      * Create a String to represent this Student.
210:      * This version uses their name and A-number.
211:      *
212:      * @return  a String representing this Student
213:      */
214:     @Override
215:     public String toString() {
216:         return this.name + " (" + this.A_NUMBER + ")";
217:     }


220:     // ---------- Class methods --------------------------------------- //

222:     /**
223:      * Return whether a given grade is in the valid range.
224:      *
225:      * @param g     the grade to check for validity
226:      * @return  whether g is in the correct range.
227:      */
228:     public static boolean isValidGrade(int g) {
229:         return (MIN_GRADE <= g && g <= MAX_GRADE);
230:     }

232:     /**
233:      * Return whether a list of grades is in the valid range.
234:      *
235:      * @param gg    the list of grades to check for validity
236:      * @return  whether gg is of a suitable length with all values
237:      *          in the correct range.
238:      */
239:     public static boolean areValidGrades(int[] gg) {
240:         if (gg.length > NUM_ASGN) {
241:             return false;
242:         }
243:         for (int i = 0; i < gg.length; ++i) {
244:             if (!isValidGrade(gg[i])) {
245:                 return false;
246:             }
247:         }
248:         return true;
249:     }

251:     /**
252:      * Return whether an assignment number is in the valid range
253:      *
254:      * @param asgnNum   the supposed assignment number
255:      * @return  whether asgnNum is in the range 1..NUM_ASGN
256:      */
257:     public static boolean isValidAsgnNum(int asgnNum) {
258:         return 1 <= asgnNum && asgnNum <= NUM_ASGN;
259:     }

261:     /**
262:      * Return the letter grade corresponding to the given grade.
263:      *
264:      * @param g     the percentage grade to be converted to a letter grade.
265:      * @return  the letter grade corresponding to g.
266:      */
267:     public static String letterGradeFor(int g) {
268:         if (g < 50) {
269:             return "F";
270:         } else if (g < 60) {
271:             return "D";
272:         } else if (g < 70) {
273:             return "C";
274:         } else if (g < 80) {
275:             return "B";
276:         } else {
277:             return "A";
278:         }
279:     }

281:     /**
282:      * Set the number of graded assignments.
283:      *
284:      * @param released  the number of the latest assignment released
285:      */
286:     public static void releaseAssignment(int released) {
287:         if (isValidAsgnNum(released)) {
288:             asgnsGraded = released;
289:         }
290:     }

292: }