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: // Updated 3/2021: Replaced mutable Date with immutable LocalDate.
11:
12: import java.time.*;
13:
14: class Student
15: {
16: private static int nextStudentID;
17:
18: static {
19: // Typically fetched from a DB at application (JVM) startup:
20: // nextStudentID = ...;
21: nextStudentID = 1; // For demo purposes
22: }
23:
24: // Add a shutdown hook to save the current value of nextStudentID
25: // to the DB when the application (the JVM) exits:
26: // ...
27:
28: // Add a shutdown hook to save the current value of nextStudentID
29: // to the DB when the application (the JVM) exits:
30: // ...
31:
32: private int studentID;
33: private String lastName;
34: private String firstName;
35: private String address;
36: private String homePhone;
37: private int level; // 1 = freshman, ... Should be an enum!
38: private LocalDate enrolled;
39:
40: // ...
41:
42: public Student ( String lastName )
43: { this( lastName, "" ); }
44:
45: public Student (String lastName, String firstName )
46: { this( lastName, firstName, "Unknown", "", 1, LocalDate.now() ); }
47:
48: public Student (String lastName, String firstName, String address )
49: { this( lastName, firstName, address, "", 1, LocalDate.now() ); }
50:
51: // This constructor takes an int, but a char "promotes" to an int, so
52: // the code "new Student ("Last", First", 'M')" calls this constructor,
53: // rather than cause a compiler error. The correct fix is to use enums
54: // for the gender field, but as enums haven't been covered yet, I will
55: // fix the bug by adding another constructor that takes a char:
56:
57: public Student (String lastName, String firstName, int level )
58: { this( lastName, firstName, "Unknown", "", level, LocalDate.now() ); }
59:
60: //////////////////////////////////////////////////////////////////////
61: // Repeat for all allowed combinations of optional arguments! Note //
62: // not all combinations are possible (e.g., last, first, phone). //
63: // (How many constructors will there be?) //
64: //////////////////////////////////////////////////////////////////////
65:
66: // This is the main constructor (which does all the real work),
67: // and which is ultimately invoked by all the other constructors:
68: public Student (String lastName, String firstName, String address,
69: String homePhone, int level, LocalDate enrolled )
70: {
71: this.studentID = nextStudentID; ++nextStudentID;
72: this.lastName = lastName;
73: this.firstName = firstName;
74: this.address = address;
75: this.homePhone = homePhone;
76: this.level = level;
77: this.enrolled = enrolled;
78: }
79:
80: // public Student methods go here:
81: // ...
82: }
83:
84:
85: // Sample use (good luck figuring out which combinations and the
86: // order of arguments to use):
87:
88: class StudentTest
89: {
90: public static void main ( String [] args ) {
91: Student s1 = new Student( "Doe" );
92: Student s2 = new Student( "Piffl", "Hymie", 1 );
93: // ...
94: }
95: }