A8 ‣ Iterating Lists and Writing Files

Create a directory in your repository named a8 and include all of the files for this assignment in the a8 directory. Place all files within the a8 package.

  1. Write an Interface named Person.
    1. The interface should require access methods for a first name (String) and last name (String).
  1. Write an enumeration named Major that includes instances for the following majors: Undecided, Computer Science, Math, and Physics. Use 4 character names for the enumerations.
  1. Write an exception class named IllFormedStudent.
    1. The class should have a constructor that takes a String as an argument and saves the string in a field named reason.
    2. The class has a toString() method that returns the string “Ill formed student: “ concatenated with the reason.
  1. Write a class named Student that implements the Person interface.
    1. The Student class should have a SSN field (int) along with an access method for the field.
    2. The Student class should include a major field along with access and modifier methods for the field.
    3. The class should set all of its fields in the constructor using arguments passed to the constructor.
    4. The constructor should throw an IllFormedStudent exception if the first name or last name are null or if the ssn is zero.  If the major is null, it should be set to Undecided.
    5. The class should override equals, hashCode using the student’s SSN as the only significant field.
    6. The class should override toString.
  1. Write a class named TransferStudent that is a subclass of Student.
    1. A transfer student has field that specifies the number of credits transferred in (int) and include an access modifier for the field.
    2. The constructor should set all of the class fields using the values of the arguments passed into the constructor. Note: for this problem, it is fine to have a constructor with 5 parameters.
    3. If an IllFormedStudent exception is caught, the constructor should throw the exception to the caller.
    4. The class should override toString.
  1. Write a class named Driver.
    1. The class should have a method named writeStudents that has two parameters. The first parameter holds a file name (String) and the second parameter holds a List of TransferStudent objects. The method should use an Iterator to get each element in the list. For each element in the list, the method should write to the file specified in the first argument, a separate line containing a comma separated list of field values for the following fields: first name, last name, SSN, major and number of credits transferring in.
    2. When the program runs, it creates two objects of type TransferStudent for the following students, adds them to an ArrayList and writes them to a file named “students.txt” by calling writeStudents.
      1. John Doe is undecided with 24 credits transferring in.  His SSN is 123-45-6789.
      2. Kim Brown is a Computer Science major with 27 credits transferring in. Her SSN is 987-65-4321.
    3. If an IllFormedTransferStudent exception is thrown, the driver should print to the console “Error: ” followed by the reason string.

 

© 2017 – 2019, Eric. All rights reserved.