Source of Undergrad.java


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

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


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

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

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

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

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

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

 94: }