Source of Student.java


  2: /**
  3:  * A class to hold some very basic information about a student.
  4:  * A static method translates percent grades to letter grades, and a
  5:  * getter method retrieves a "virtual property" -- the student's letter
  6:  * grade, which is based on their percentage grade.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  * @version 2.1 (2017-03-13)
 10:  */
 11: public class Student {

 13:     // ---------- Instance Variables ---------------------------------- //
 14:     /** The student's A-Number */
 15:     public final String aNumber;
 16:     /** The student's name */
 17:     private String name;
 18:     /** The student's grade (as a percentage) */
 19:     private int pctGrade;

 21:     // Class information -- common to all Students //

 23:     // ---------- Class Constants ------------------------------------- //
 24:     /** Maximum possible percentage grade */
 25:     public static final int MAX_GRADE = 100;
 26:     /** Minimum possible percentage grade */
 27:     public static final int MIN_GRADE = 0;

 29:     // ---------- Constructors ---------------------------------------- //
 30:     /**
 31:      * Create a Student object.
 32:      *
 33:      * @param reqNumber the requested A-number
 34:      * @param reqName   the requested name
 35:      * @param reqGrade  the requested percentage grade (must be 0..100)
 36:      */
 37:     public Student(String reqNumber, String reqName, int reqGrade) {
 38:         aNumber = reqNumber;
 39:         name = reqName;
 40:         if (Student.isValidGrade(reqGrade)) {
 41:             pctGrade = reqGrade;
 42:         } else {
 43:             System.out.println("Illegal grade: " + reqGrade);
 44:             pctGrade = 0;
 45:         }
 46:     }

 48:     /**
 49:      * Create a Student object with an initial grade of 0%.
 50:      *
 51:      * @param n     the requested name
 52:      */
 53:     public Student(String reqNumber, String reqName) {
 54:         // get the OTHER constructor to do it:
 55:         //  *it* needs a requested percentage grade, too.
 56:         //  so we give it the requests we got (number and name)
 57:         //  and the Students typically start with no grades)
 58:         this(reqNumber, reqName, 0);
 59:     }

 61:     // ---------- Getters --------------------------------------------- //
 62:     /**
 63:      * Get the value of aNumber
 64:      *
 65:      * @return the value of aNumber
 66:      */
 67:     public String getANumber() {
 68:         return aNumber;
 69:     }

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

 80:     /**
 81:      * Get the value of pctGrade
 82:      *
 83:      * @return the value of pctGrade
 84:      */
 85:     public int getPctGrade() {
 86:         return pctGrade;
 87:     }

 89:     /**
 90:      * get letter grade for this student
 91:      *
 92:      * @return  the letter grade equivalent to this student's percentage grade
 93:      * @since 2.0
 94:      */
 95:     public String getLetterGrade() {
 96:         return Student.letterGradeFor(this.pctGrade);
 97:     }

 99:     // ---------- Setters --------------------------------------------- //
100:     /**
101:      * Set the value of name
102:      *
103:      * @param name new value of name
104:      */
105:     public void setName(String name) {
106:         this.name = name;
107:     }

109:     /**
110:      * Set the value of pctGrade
111:      *
112:      * @param pctGrade  new value of pctGrade (must be 0..100)
113:      */
114:     public void setPctGrade(int pctGrade) {
115:         if (Student.isValidGrade(pctGrade)) {
116:             this.pctGrade = pctGrade;
117:         }
118:     }

120:     // ---------- Miscellaneous Object Methods ------------------------ //
121:     /**
122:      * Print a brief summary of the student's data.
123:      */
124:     public void printRecord() {
125:         System.out.println("\tNumber: " + aNumber);
126:         System.out.println("\tName:   " + name);
127:         System.out.println("\tGrade:  " + pctGrade);
128:         System.out.println("\tLetter: " + this.getLetterGrade());
129:     }

131:     /**
132:      * Create a String to represent this Student.
133:      * This version uses their name and A-number.
134:      *
135:      * @return  a String representing this Student
136:      */
137:     @Override
138:     public String toString() {
139:         return name + " (" + aNumber + ")";
140:     }

142:     // ---------- Class Methods --------------------------------------- //
143:     /**
144:      * Return whether a given grade is in the valid range.
145:      *
146:      * @param g     the grade to check for validity
147:      * @return  whether g is in the correct range.
148:      * @since 2.0
149:      */
150:     public static boolean isValidGrade(int g) {
151:         return (MIN_GRADE <= g && g <= MAX_GRADE);
152:     }

154:     /**
155:      * Return the letter grade corresponding to the given grade.
156:      *
157:      * @param g     the percentage grade to be converted to a letter grade.
158:      * @return  the letter grade corresponding to g.
159:      * @since 2.0
160:      */
161:     public static String letterGradeFor(int g) {
162:         if (g < 50) {
163:             return "F";
164:         } else if (g < 60) {
165:             return "D";
166:         } else if (g < 63) {
167:             return "C-";
168:         } else if (g < 67) {
169:             return "C";
170:         } else if (g < 70) {
171:             return "C+";
172:         } else if (g < 73) {
173:             return "B-";
174:         } else if (g < 77) {
175:             return "B";
176:         } else if (g < 80) {
177:             return "B+";
178:         } else if (g < 85) {
179:             return "A-";
180:         } else if (g < 90) {
181:             return "A";
182:         } else {
183:             return "A+";
184:         }
185:     }

187: }