Source of Student.java


  2: /**
  3:  * A simplified Student class to demonstrate inheritance from Person.
  4:  * This class has public and private methods.
  5:  * Undergrad and GradStudent extend this class.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class Student extends Person {

 11:     public final String A_NUMBER;
 12:     private int grade;

 14:     public static final int MAX_GRADE = 100;
 15:     private static int numStudents = 0;

 17:     /**
 18:      * Create a Student. A-number generated automatically.
 19:      *
 20:      * @param name this Student's name
 21:      */
 22:     public Student(String name) {
 23:         super(name);   // set my name AS A Person
 24:         grade = 0;
 25:         A_NUMBER = nextANumber();
 26:     }

 28:     /**
 29:      * Change this Student's grade.
 30:      *
 31:      * @param newGrade the new grade
 32:      */
 33:     public void setGrade(int newGrade) {
 34:         if (isValidGrade(newGrade)) {
 35:             this.grade = newGrade;
 36:         }
 37:     }

 39:     /**
 40:      * Return this Student's grade.
 41:      *
 42:      * @return this Student's grade
 43:      */
 44:     public int getGrade() {
 45:         return this.grade;
 46:     }

 48:     /**
 49:      * A method for Undergrad to inherit.
 50:      */
 51:     public void publicStudentMethod() {
 52:         System.out.println("\tin publicStudentMethod for " + this);
 53:         publicPersonMethod();
 54:         privateStudentMethod();
 55:         System.out.println("\tpublicStudentMethod done");
 56:     }

 58:     /**
 59:      * A method NOT to be inherited.
 60:      */
 61:     private void privateStudentMethod() {
 62:         System.out.println("\tin privateStudentMethod for " + this);
 63:     }

 65:     /**
 66:      * Replacement for Person method
 67:      */
 68:     @Override
 69:     public void replacedMethod() {
 70:         System.out.println("\tin replacedMethod for the Student " + this);
 71:     }

 73:     /**
 74:      * Replacement for Person method that incorporates the inherited method
 75:      */
 76:     @Override
 77:     public void revisedMethod() {
 78:         System.out.println("\tin revisedMethod for the Student " + this);
 79:         super.revisedMethod();
 80:         System.out.println("\trevisedMethod for Student done");
 81:     }

 83:     /**
 84:      * Return a String representation of this Student
 85:      *
 86:      * @return a String representing this Student
 87:      */
 88:     @Override
 89:     public String toString() {
 90:         return this.getName() + " (" + this.A_NUMBER + ")";
 91:     }

 93:     /**
 94:      * Check whether the given number is a valid grade.
 95:      * Valid grades are between 0 and the maximum grade (inclusive).
 96:      *
 97:      * @param value the value to check
 98:      */
 99:     private static boolean isValidGrade(int value) {
100:         return 0 <= value && value <= MAX_GRADE;
101:     }

103:     /**
104:      * Return the next available Student number. 
105:      *
106:      * @return a previously unused Student number
107:      */
108:     private static String nextANumber() {
109:         ++numStudents;
110:         return String.format("A%08d", numStudents);
111:     }

113: }