/**
* A Better Student class: we add the following methods
* student1.equals(student2)
* student1.compareTo(student2)
*/
public class Student2
{
/* The following three variables are called "Instance Variables" */
private String fname;
private String lname;
private int stuID;
/* Remember static variables ? They have no reference....sometimes
* referred to as "Class Variables"
*/
private static int count=0;
/**
* We provide two constructors. The first is called a "Default Constructor"
*/
public Student2()
{count++;
fname="Not Given";
lname="Not given";
stuID=0;
}
public Student2(String f,String l, int ID)
{ fname=f;
lname=l;
stuID=ID;
count++;
}
/* You should (almost always) provide a toString method for output */
public String toString()
{ String temp;
temp="First Name: "+fname+'\n';
temp+="Last Name: "+lname+'\n';
temp+="ID: "+stuID+'\n';
return temp;
}
/* The following is sometimes referred to as an "accessor method",
* that is, it retrieves the value of an instance variable
*/
public String getFirst()
{ return fname;
}
public String getLast()
{ return lname;}
public int getID()
{ return stuID;}
/* The following is referred to as a "Mutator", that is, it changes the value
* of an instance variable
*/
public void changeFirst(String newName)
{ fname=newName;
}
/*
* Study the method below. Was it necessary to write getFirst() and getLast()
* why not getID() ??
*/
public boolean equals(Student2 s2)
{ return (
stuID==s2.stuID &&
lname.equals(s2.getLast()) &&
fname.equals(s2.getFirst())
);
}
/* if s1s2 return a positive number
We'll compare the iD number !
*/
public int compareTo(Student2 s2)
{ return (stuID-s2.stuID);}
/* Ahhhh....explain this one */
public static int getCount()
{ return count;}
}