SOLVED: Student information for Springfield State University is stored in a text file named p02-students.txt

30.00 $

Category:

Description

5/5 - (3 votes)

Your program shall meet these requirements.

1. Student information for Springfield State University is stored in a text file named p02-students.txt. There is one student record per line, where the format of a student record for an on-campus student is: C id last-name first-name residency program-fee credits where: ‘C’ Identifies the student as an on-campus student. id The student identifier number. A string of 13 digits. last-name The student’s last name. A contiguous string of characters. first-name The student’s first name. A contiguous string of characters. residency ‘R’ if the student is a resident, ‘N’ if the student is a non-resident. program-fee A program fee, which may be zero. credits The number of credit hours for which the student is enrolled. The format of a student record for an online student is: O id last-name first-name tech-fee credits where ‘O’ identifies the student as an online student, and id, last-name, first-name, and credits are the same as for an on-campus student. The tech-fee field is ‘T’ if the student is to be assessed the technology fee or ‘-‘ if the student is not assessed the technology fee.

2. The program shall read the contents of p02-students.txt and calculate the tuition for each student.

3. The program shall write the tuition results to an output file named p02-tuition.txt formatted thusly: id last-name first-name tuition id last-name first-name tuition

The records in the output file shall be sorted in ascending order by id.

5. If the input file p02-students.txt cannot be opened for reading (because it does not exist) then display an error message on the output window and immediately terminate the program, e.g.,

5.1 Main Class A template for Main is included in the zip archive. The Main class shall contain the main() method which shall instantiate an object of the Main class and call run() on that object. Complete the code by reading the comments and implementing the pseudocode.

5.2 TuitionConstants Class The complete TuitionConstants class is included in the zip archive. This class simply declares some public static constants that are used in other classes.

5.3 Sorter Class We shall discuss sorting later in the course, so this code may not make perfect sense at this time. However, I have provided all of it for you. The Sorter class contains a public method insertionSort() that can be called to sort a list of ArrayList. When sorting Students we need to be able to compare one Student A to another Student B to determine if A is less than or greater than B. Since we are sorting by student id, we have the abstract Student class implement the Comparable interface and we define Student A to be less than Student B if the mId field of A is less than the mId field of B.

This is how we sort the ArrayList list by student id. java.lang.Comparable is a generic interface (it requires a type parameter T to be specified when the interface is implemented) in the Java Class Library that declares one method: int compareTo(T obj) where T represents a class type and obj is an object of the class T. The method returns a negative integer if this T (the object on which the method is invoked) is less than obj, zero if this T and obj are equal, or a positive integer if this T is greater than obj. To make Student implement the Comparable interface, we write: public abstract class Student implements Comparable { … } Since Student implements Comparable, whenever compareTo() is called in Sorter.keepMoving() to compare two objects, either OnCampusStudent.compareTo() or OnlineStudent.compareTo() will be called. Student Class The Student class is an abstract class that implements the java.lang.Comparable interface (see 5.3 Sorter Class): public abstract class Student implements Comparable { … } A Student object contains five instance variables: mCredits Number of credit hours the student is enrolled for. mFname The student’s first name. mId The student’s id number. mLname The student’s last name. mTuititon The student’s computed tuition. Most of the Student instance methods should be straightforward to implement so we will only mention the two that are not so obvious: +calcTuition(): void An abstract method that is implemented by subclasses of Student. Abstract methods do not have to be implements, and this one is not. +compareTo(pStudent: Student): int < Implements the compareTo() method of the Comparable interface. Returns -1 if the mId instance variable of this Student is less than the mId instance variable of pStudent. Returns 0 if they are equal (should not happen because id numbers are unique). Returns 1 if the mId instance variable of this Student is greater than the mId instance variable of pStudent. The code is for compareTo() is: return getId().compareTo(pStudent.getId()); OnCampusStudent Class The OnCampusStudent class is a direct subclass of Student. It adds new instance variables that are specific to on-campus students: mResident True if the OnCampusStudent is a resident, false for non-resident. mProgramFee Certain OnCampusStudent’s pay an additional program fee. This value may be 0. The OnCampusStudent instance methods are mostly straightforward to implement so we shall only discuss two of them. +OnCampusStudent(pId: String, pFname: String, pLname: String): < Must call the superclass constructor passing pId, pFname, and pLname as parameters. calcTuition(): void < Must implement the rules described in Section 3 Background. Note that we cannot directly access the mTuition instance variable of an OnCampus Student because it is declared as private in Student. So how do we write to mTuition? By calling the protected setTuition() method that is inherited from Student. The pseudocode for calcTuition() is: Override Method calcTuititon() Returns Nothing Declare double variable t If getResidency() returns true Then t = TuitionConstants.ONCAMP_RES_BASE Else t = TuitionConstants.ONCAMP_NONRES_BASE End if t = t + getProgramFee(); If getCredits() TuitionConstants.MAX_CREDITS Then t = t + (getCredits() – TuitionConstants.MAX_CREDITS) × TuitionConstants.ONCAMP_ADD_CREDITS End if Call setTuition(t) End Method calcTuition() OnlineStudent Class The OnlineStudent class is a direct subclass of Student. It adds a new instance variable that is specific to online students: mTechFee Certain OnlineStudent’s pay an additional technology fee. This instance variable will be true if the technology fee applies and false if it does not. The OnlineStudent instance methods are mostly straightforward to implement so we shall only discuss two of them. +OnlineStudent(pId: String, pFname: String, pLname: String): < Must call the superclass constructor passing pId, pFname, and pLname as parameters. +calcTuition(): void < Must implement the rules described in Section 3 Background. Note that we cannot directly access the mTuition instance variable of an OnlineStudent because it is declared as private in Student. So how do we write to mTuition? By calling the protected setTuition() method that is inherited from Student. The pseudocode for calcTuition() is: Override Method calcTuititon() Returns Nothing Declare double variable t = getCredits() × TuitionConstants.ONLINE_CREDIT_RATE If getTechFee() returns true Then t = t + TuitionConstants.ONLINE_TECH_FEE End if Call setTuition(t) End Method calcTuition()

  • StudentTuitionCalculator.zip