type.lib
Class Student

java.lang.Object
  extended by type.lib.Student
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<java.lang.String>
Direct Known Subclasses:
CEstudent, ITstudent, SEstudent

public class Student
extends java.lang.Object
implements java.io.Serializable, java.lang.Iterable<java.lang.String>

Provide services to maintain information about a computer science and engineering (CSE) student and his or her academic record. The record is very concise consisting of the course number (4 digits) of each CSE course the student has taken and the letter grade (A,B,C,D,E,F) that was obtained in it. The record does not include non-CSE courses, does not keep track of when the course was taken, and keeps only the last grade if a course is taken more than once; i.e. there is only one grade per course.

Version:
7.0 - Summer 2010
Author:
H. Roumani, roumani@cse.yorku.ca
See Also:
Serialized Form

Field Summary
protected static java.util.Random rng
          The random number generator used by the getRandom() method.
 
Constructor Summary
Student(java.lang.String id, java.lang.String name)
          Create a CSE student having the given ID and name, and an empty (fresh) record.
 
Method Summary
 boolean equals(java.lang.Object other)
          Test the equality of students.
 java.lang.String getCourseGrade(java.lang.String course)
          Determine the grade of this student in the given course.
 java.lang.String getFirstCourse()
          Deprecated. As of version 4.0, replaced with iterator() Determine the first course in the record of this student.
 double getGpa()
          Determine the grade-point-average (GPA) of this student.
 java.lang.String getID()
          Determine the ID of this student.
 java.lang.String getName()
          Determine the name of this student.
 java.lang.String getNextCourse()
          Deprecated. As of version 4.0, replaced with iterator() Determine the next course in the record of this student. It is assumed a prior invocation of getFirstCourse was made and that it produced a non-null return. In order to browse through all courses taken, start by invoking getFirstCourse to get the first course number or null (if the student has not taken any course yet). If you do get a non-null return, keep invoking getNextCourse to get all the other courses in the record until a null is returned, indicating no more courses.
static Student getRandom()
          Create a randomly chosen student.
static Student getRandom(boolean same)
          Create a random student having a random ID, a randomly chosen four-letter name, and a random academic record.
 int hashCode()
          Compute a hash code for this student.
 java.util.Iterator<java.lang.String> iterator()
          Determine an Iterator over the course numbers in the record of this student.
 boolean setCourseGrade(java.lang.String course, java.lang.String grade)
          Update the student's record by indicating in it that the student has obtained the given grade in the given course.
static void setSeed(long seed)
          Change the seed of the random sequence returned by getRandom().
 java.lang.String toString()
          Return a string representation of this student.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

rng

protected static java.util.Random rng
The random number generator used by the getRandom() method.

Constructor Detail

Student

public Student(java.lang.String id,
               java.lang.String name)
Create a CSE student having the given ID and name, and an empty (fresh) record.

Parameters:
id - a 9-digit ID# of the student
name - the name of the student.
Throws:
java.lang.RuntimeException - if the given ID is not a 9-digit string.
Method Detail

getID

public java.lang.String getID()
Determine the ID of this student.

Returns:
the ID of this student.

getName

public java.lang.String getName()
Determine the name of this student.

Returns:
the name of this student.

iterator

public java.util.Iterator<java.lang.String> iterator()
Determine an Iterator over the course numbers in the record of this student. The iterator does not support the remove operation.

Specified by:
iterator in interface java.lang.Iterable<java.lang.String>
Returns:
an Iterator over the courses.

getFirstCourse

@Deprecated
public java.lang.String getFirstCourse()
Deprecated. As of version 4.0, replaced with iterator() Determine the first course in the record of this student.

Returns:
the first course number (4 digits) in this student's record. If the record is empty, null is returned.

getNextCourse

@Deprecated
public java.lang.String getNextCourse()
Deprecated. As of version 4.0, replaced with iterator() Determine the next course in the record of this student. It is assumed a prior invocation of getFirstCourse was made and that it produced a non-null return. In order to browse through all courses taken, start by invoking getFirstCourse to get the first course number or null (if the student has not taken any course yet). If you do get a non-null return, keep invoking getNextCourse to get all the other courses in the record until a null is returned, indicating no more courses.

Returns:
the next course number (4 digits) in this student's record. If no more courses are present in the record, null is returned.

getCourseGrade

public java.lang.String getCourseGrade(java.lang.String course)
Determine the grade of this student in the given course.

Parameters:
course - the given 4-digit course number.
Returns:
the grade that this student has achieved in the given course. If the given course does not appear in the student's record, null is returned.

setCourseGrade

public boolean setCourseGrade(java.lang.String course,
                              java.lang.String grade)
Update the student's record by indicating in it that the student has obtained the given grade in the given course. Note that if the given course number is already present in the student's record then the given grade overwrites the existing one, thereby keeping only the grade of the latest attempt. In either case, the given letter grade is converted to upper case before updating the record.

Parameters:
course - the given 4-digit course number.
grade - the given 1-letter grade.
Returns:
true if the record was updated as requested and return false otherwise. The record will not be updated if the given course number is not made up of exactly 4 digits or if the given letter grade is not one of the letters (A,B,C,D,E,F) in capital or small-case.

equals

public boolean equals(java.lang.Object other)
Test the equality of students. An object is considered equal to this one if it is indeed a student object with the same ID number as this one.

Overrides:
equals in class java.lang.Object
Parameters:
other - a reference to the object to test equality with.
Returns:
true if other is not null and it points to an object that is equal (as defined above) to this object, and false otherwise.

hashCode

public int hashCode()
Compute a hash code for this student.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.

getGpa

public double getGpa()
Determine the grade-point-average (GPA) of this student. The GPA is computed assuming all courses have equal weights and that A has 5 points and F has zero.

Returns:
the grade-point-average of this student.

toString

public java.lang.String toString()
Return a string representation of this student.

Overrides:
toString in class java.lang.Object
Returns:
the string "CSE student: " followed by the student's name.

getRandom

public static Student getRandom()
Create a randomly chosen student. This is a convenience method whose invocation is equivalent to invoking getRandom(false).


getRandom

public static Student getRandom(boolean same)
Create a random student having a random ID, a randomly chosen four-letter name, and a random academic record. The record consists of at least one and at most 25 courses having random course numbers and random letter grades.
The returned student is a CSE student (class Student) 40% of the times, a computer engineering student (class CEstudent) 20% of the times, a software engineering student (class SEstudent) 20% of the times, and an information technology student (class ITstudent) 20% of the times.
This method was added for pedagogical reasons.

Parameters:
same - a flag that determines if a new student is to be generated (if false) or if a reference to the last-generated student is to be returned (if true). You should normally set it to false to get a different student per invocation.
Returns:
a randomly chosen student.

setSeed

public static void setSeed(long seed)
Change the seed of the random sequence returned by getRandom(). This method is meant to be used by a test harness so that the same sequence can be generated for the app being tested and its oracle.

Parameters:
seed - the initial seed for random number generator.


Java by Abstraction: A Client-View Approach