Source of Undergrad.java


  1: package inheritance;

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

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


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

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

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

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

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

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

 96: }