Lab 11d – Reading Students from a File

Inside your lab11 directory, create a file named Lab11d.java.  Inside Lab11d.java write a program that behaves as follows.


    1. Create an instance of the Scanner class that can read from students.txt.
    2. Change the delimiters of the Scanner so that it returns tokens that are separated by commas and newline characters.
    3. Read the first integer in the students.txt and store the integer in memory.
    4. Create an array of Student elements named arr whose length is equal to the integer previously read from student.txt.
    5. While there is data to be read from the file
      • Read in a student’s ID, last name, and first name and store them in local variables.
      • Create an instance of the Student class using the data read from the file.
      • Add the newly created instance to arr.
    6. Use a for-loop to iterate over the instances of the Student class that are in arr. 
      • For each instance, print to the screen the String that is returned by the instance’s toString method.

Compile, debug, test, and push.

Lab 11b – Test the Student Class

Inside your lab11 directory, create a file named Lab11b.java. Inside Lab11b.java write a program that runs as follows.


    1. Ask the user to enter a first name, last name and student ID.
    2. Read in the data from the keyboard and store the data in local variables.
    3. Create an instance of the Student class using the data read from the keyboard.
    4. Print to the screen the String that is returned by the instance’s toString method.
    5. Print to the screen the student’s last name using the instance’s getter method.
    6. Print to the screen the student’s first name using the instance’s getter method.
    7. Print to the screen the student’s ID using the instance’s getter method.
    8. Set the student’s last name to “Bunny” using the instance’s setter method.
    9. Set the student’s first name to “Bugs” using the instance’s setter method.
    10. Set the student’s ID to 3000 using the  instance’s setter method.
    11. Print to the screen the String that is returned by the instance’s toString method.
    12. Set the student’s ID to 0 using the instance’s setter method.
    13. Print to the screen the string that is returned by the instance’s toString method.
    14. Create an instance of the Student class for a student named “Joe Black” and whose student id is 1357.
    15. Use the equals method to print to the screen “Yep, equal” if the two instances of the Student class in your program are considered equal; otherwise print “not equal“.
    16. Create a third instance of the Student class for a student named “Miko Marra” whose student id is 1357.
    17. Use the equals method to print to the screen “Yep, equal” if the last two instances of the Student class in your program are considered equal; otherwise print “not equal“.

Compile, debug, test, push.

Lab 11a – Student Entity

Log into cs.bridgewater.edu.  Create a directory named lab11 in the labs directory inside your repository.  Inside the lab11 directory create a file named Student.java.


Inside the Student.java file create a class that models a student.  The Student class should have the following:

    1. Private fields
        • a field named firstName that holds a String.
        • a field named lastName that holds a String.
        • a field named studentID that holds an integer.
    2. A constructor
      • The constructor sets the firstName, lastName and studentID fields using the data passed into the constructor.
        1. If the student ID value passed to the constructor is less than or equal to zero the studentID field is set to -1.
        2. If the first name or last name values that are passed to the constructor are empty strings or null, the corresponding fields are set to “invalid“.
    3. Getters and setter for each field.
      • The setters implement the same input validation as the constructor.
    4. toString method that overrides the Object class’ toString method which returns a String containing the student’s id,  last name, and first name (in that order) separated by commas.
    5. An equals method that overrides the Object class’ equals method which returns true if the reference passed into the method is a Student and has the same studentID value as the studentID value of the object on which it is called; otherwise returns false.

Compile, debug, then push.