Source of Student.java


  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 implements Comparable<Student> {

 10:     // ---------- Comparable requires compareTo metho ----------------- //
 11:     public int compareTo(Student other) {
 12:         return this.name.compareTo(other.name);
 13:     }

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

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


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

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

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


 40:     // ---------- Constructors ---------------------------------------- //

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

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

 54:         // set personal data
 55:         name = initialName;

 57:         // set academic data
 58:         asgnGrades = new int[NUM_ASGN];
 59:     }


 62:     // ---------- Getters --------------------------------------------- //

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

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

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

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

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

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

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


147:     // ---------- Setters --------------------------------------------- //

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

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

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


182:     // ---------- Other observer methods ------------------------------ //

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

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


207:     // ---------- Class methods --------------------------------------- //

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

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

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

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

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

279: }