// StudentPQ.java - A demo of using optional constructor arguments by
// using overloaded constructors. Student objects represent students
// enrolled in a college course and have many proprties, only a few of which
// are shown here. The "PQ" in the name stands for "Production Quality".
// In this class I have added many of the features that a real-world Java
// class should have: standard methods defined for equals, hashCode, and
// toString; get and set methods for properties; a full set of constructors;
// Comparable interface defined, etc. However, in a real PQ Student class,
// we would automatically assign studentID numbers and not pass to constructors.
//
// Written 3/2005 by Wayne Pollock, Tampa Florida USA.
import java.util.Date;
public class StudentPQ implements Comparable
{
private String lastName;
private String firstName;
private String studentID;
private String address;
private String homePhone;
private int level; // 1 = freshman, ...
private Date enrolledDate;
private int GPA; // This is GPA times 100, e.g., 400 == 4.0
// ... // Other properties go here
public String getName () { return firstName + " " + lastName; }
public int getGPA () { return GPA; }
public void setGPA ( int GPA )
{
if ( GPA < 0 || GPA > 400 )
throw new IllegalArgumentException( "GPA of " + GPA
+ " must be between 0 and 400 (0.0 and 4.0)" );
this.GPA = GPA;
}
// Always return a copy of mutable objects:
public Date getEnrolledDate () { return enrolledDate.clone(); }
// Define other get and set methods here...
public String toString ()
{ return "Name: " + firstName + " " + lastName
+ "\tGPA: " + Float.toString( GPA );
}
public boolean equals ( Object obj ) // Case insensitive
{
if ( obj == this ) return true;
if ( ! ( obj instanceof StudentPQ ) ) return false;
StudentPQ s = (StudentPQ) obj;
return studentID.equalsIgnoreCase( s.studentID );
}
public int hashCode () // Case insensitive
{ return studentID.toUpperCase().hashCode(); }
public int compareTo ( Object obj ) // Case insensitive
{
StudentPQ s = (StudentPQ) obj;
return String.CASE_INSENSITIVE_ORDER.compare( studentID, s.studentID );
}
}