Source of Undergrad.java


  1: package inheritance;


  4: /**
  5:  * A class that extends Student.
  6:  * Undergrads have a year-of-study (usually 1 to 4).
  7:  *
  8:  * An example of how we program multiple constructors for a data type class.
  9:  *
 10:  * @author Mark Young (A00000000)
 11:  */
 12: public class Undergrad extends Student {

 14:     // ---------- Class constants ----------------------------------------- //
 15:     public static final int DEFAULT_YEAR = 1;
 16:     public static final String DEFAULT_PROGRAM = "Undecided";
 17:     
 18:     // ---------- Instance variables -------------------------------------- //
 19:     private int year;
 20:     private String program;


 23:     // ---------- Constructors -------------------------------------------- //
 24:     /** 
 25:      * Undergrads need a name, a year, and a program.
 26:      *
 27:      * This is my PRIMARY constructor -- it does all the work of building this
 28:      * Object.
 29:      *
 30:      * @param n the student's name
 31:      * @param y this student's year
 32:      * @param p this student's program
 33:      */
 34:     public Undergrad(String n, int y, String p) {
 35:         super(n);       // PRIMARY constructor calls super(...)
 36:         year = y;
 37:         program = p;
 38:     }

 40:     /**
 41:      * Can create an undergrad with just name and year (program is undecided).
 42:      * 
 43:      * This is a SECONDARY constructor -- it just calls the primary constrctor.
 44:      *
 45:      * @param n the student's name
 46:      * @param y this student's year
 47:      */
 48:     public Undergrad(String n, int y) {
 49:         this(n, y, DEFAULT_PROGRAM);  // SECONDARY constructor calls this(...)
 50:     }

 52:     /**
 53:      * Can create an undergrad with just name and program (year is 1).
 54:      *
 55:      * This is another SECONDARY constructor -- call the primary.
 56:      *
 57:      * @param n the student's name
 58:      * @param p this student's program
 59:      */
 60:     public Undergrad(String n, String p) {
 61:         this(n, DEFAULT_YEAR, p);     // SECONDARY constructor calls this(...)
 62:     }

 64:     /**
 65:      * Can create an undergrad with just a name (year is 1, program is 
 66:      * undecided).
 67:      *
 68:      * Yet another SECONDARY constructor -- but calls one of the other 
 69:      * secondary constructors -- which is OK.
 70:      *
 71:      * @param n the student's name
 72:      */
 73:     public Undergrad(String n) {
 74:         this(n, 1);                   // SECONDARY constructor calls this(...)
 75:     }

 77:     // ---------- Getters and setters ------------------------------------- //
 78:     /** 
 79:      * Get this student's year of study.
 80:      *
 81:      * @return this student's year of study.
 82:      */
 83:     public int getYear() {
 84:         return year;
 85:     }

 87:     /** 
 88:      * Change this student's year of study .
 89:      *
 90:      * @param ny the new year of study for this student.
 91:      */
 92:     public void setYear(int ny) {
 93:         // SHOULD make sure the ny is appropriate -- 1..5, for example
 94:         this.year = ny;
 95:     }

 97: }