Student.java
Download Student.java
1: // Student.java - A demo of using optional constructor arguments by
2: // using overloaded constructors. Student objects represent students
3: // enrolled in a college course and have many attributes not shown.
4: // This is sometimes called the "telescoping constructor pattern".
5: // (Validity checking of arguments and other details omitted for clarity.)
6: // Note, this same technique can be used for methods with optional
7: arguments as well.
8: //
9: // Written 2/2013 by Wayne Pollock, Tampa Florida USA.
10:
11: class Student
12: {
13: private static int nextStudentID;
14:
15: static {
16: // Typically fetched from a DB at application (JVM) startup:
17: // ...
18: nextStudentID = ...;
19: }
20:
21: // Add a shutdown hook to save the current value of nextStudentID
22: // to the DB when the application (the JVM) exits:
23: // ...
24:
25: private int studentID;
26: private String lastName;
27: private String firstName;
28: private String address;
29: private String homePhone;
30: private int level; // 1 = freshman, ...
31: private Date enrolled;
32:
33: // The gender really should be an enum! The version here, using
34: // a char, masked an error for several years, with one of the
35: // constructors taking an "int", intending that to be "level":
36:
37: private char gender; // 'M' or 'F', 'U' = unknown
38: // ...
39:
40: public Student ( String lastName )
41: { this( lastName, "" ); }
42:
43: public Student (String lastName, String firstName )
44: { this( lastName, firstName, "Unknown", "", 1, new Date(), 'U' ); }
45:
46: public Student (String lastName, String firstName, String address )
47: { this( lastName, firstName, address, "", 1, new Date(), 'U' ); }
48:
49: // This constructor takes an int, but a char "promotes" to an int, so
50: // the code "new Student ("Last", First", 'M')" calls this constructor,
51: // rather than cause a compiler error. The correct fix is to use enums
52: // for the gender field, but as enums haven't been covered yet, I will
53: // fix the bug by adding another constructor that takes a char:
54:
55: public Student (String lastName, String firstName, int level )
56: { this( lastName, firstName, "Unknown", "", level, new Date(), 'U' ); }
57:
58: public Student (String lastName, String firstName, char gender )
59: { this( lastName, firstName, "Unknown", "", 1, new Date(), gender ); }
60:
61: //////////////////////////////////////////////////////////////////////
62: // Repeat for all allowed combinations of optional arguments! Note //
63: // not all combinations are possible (e.g., last, first, phone). //
64: // (How many constructors will there be?) //
65: //////////////////////////////////////////////////////////////////////
66:
67: // This is the main constructor (which does all the real work),
68: // and which is ultimately invoked by all the other constructors:
69: public Student (String lastName, String firstName, String address,
70: String homePhone, int level, Date enrolled, char gender )
71: {
72: this.studentID = nextStudentID; ++nextStudentID;
73: this.lastName = lastName;
74: this.firstName = firstName;
75: this.address = address;
76: this.homePhone = homePhone;
77: this.level = level;
78: this.enrolled = enrolled;
79: this.gender = gender;
80: }
81:
82: // public Student methods go here:
83: // ...
84: }
85:
86:
87: // Sample use (good luck figuring out which combinations and the
88: // order of arguments to use):
89:
90: class StudentTest
91: {
92: public static void main ( String [] args ) {
93: Student s1 = new Student( "Doe" );
94: Student s2 = new Student( "Piffl", "Hymie", 'M' );
95: // ...
96: }
97: }