Please help with this project, I\'ve been completely stuck on it for 6 days now.
ID: 3879089 • Letter: P
Question
Please help with this project, I've been completely stuck on it for 6 days now. There are 3 Java Files Provided... 1. Main.java, 2. Sorter.java and 3. TuitionConstants.java
BACKGROUND:
At Springfield State U there are two classes of students: on-campus students and online students. On-campus students are categorized as residents (R) or nonresidents (N) depending on whether they reside within the state in which Springfield exists or they reside in a different state. The base tuition for on-campus students is $5500 for residents and $12,200 for non-residents. Some on-campus students, enrolled in certain pre-professional programs, are charged an additional program fee which varies depending on the program. An on-campus students may enroll for up to 18 credit hours at the base rate but for each credit hour exceeding 18, they pay an additional fee of $350 for each credit hour over 18.
Online students are neither residents nor non-residents. Rather, their tuition is computed as the number of credit hours for which they are enrolled multiplied by the online credit hour rate which is $875 per credit hours. Furthermore, some online students enrolled in certain degree programs pay an online technology fee of $125 per semester.
4 Software Requirements
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:
in course schedule info
'C'
id last-name first-name residency program-fee credits
Identifies the student as an on-campus student.
The student identifier number. A string of 13 digits.
The student's last name. A contiguous string of characters.
The student's first name. A contiguous string of characters.
'R' if the student is a resident, 'N' if the student is a non-resident. A program fee, which may be zero.
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.
Here is an example p02-students.txt file:
Sample p02-students.txt
C 8230123345450 Flintstone Fred R 0 12 C 3873472785863 Simpson Lisa N 750 18 C 4834324308675 Jetson George R 0 20
O 1384349045225 Szyslak Moe - 6
O 5627238253456 Flanders Ned T 3
The program shall read the contents of p02-students.txt and calculate the tuition for each student.
The program shall write the tuition results to an output file named p02-tuition.txt formatted thusly:
where tuition is the computed tuition for the student. The tuition shall be displayed with two digits after the decimal point. For example:
Sample p02-tuition.txt
1384349045225 Szyslak Moe 5250.00 3873472785863 Simpson Lisa 12950.00 4834324308675 Jetson George 6200.00 5627238253456 Flanders Ned 2750.00 8230123345450 Flintstone Fred 5500.00
The records in the output file shall be sorted in ascending order by id.
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.,
run program
5 Software Design
Refer to the UML class diagram in Section 5.7. Your program shall implement this design.
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<Student>. 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 <Student> 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<Student> list by student id.
java.lang.Comparable<T> 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:
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:
Since Student implements Comparable<Student>, whenever compareTo() is called in Sorter.keepMoving() to compare two objects, either OnCampusStudent.compareTo() or OnlineStudent.compareTo() will be called.
5.4 Student Class
The Student class is an abstract class that implements the java.lang.Comparable interface (see 5.3 Sorter Class):
}
A Student object contains five instance variables:
mCredits mFname mId mLname mTuititon
Number of credit hours the student is enrolled for. The student's first name.
The student's id number.
The student's last name.
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:
An abstract method that is implemented by subclasses of Student. Abstract methods do not have to be implements, and this one is not.
Implements the compareTo() method of the Comparable<Student> 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:
5.5 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): <<ctor>>
Must call the superclass constructor passing pId, pFname, and pLname as parameters.
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:
If getCredits() > TuitionConstants.MAX_CREDITS Then
t = t + (getCredits() - TuitionConstants.MAX_CREDITS) × TuitionConstants.ONCAMP_ADD_CREDITS
End if
5.6 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.
Must call the superclass constructor passing pId, pFname, and pLname as parameters.
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:
5.7 UML Class Diagram
The UML class diagram shown below was created using UMLet. See the zip archive for the UMLet file. We have these relationships:
6 Additional Project Requirements
Format your code neatly. Use proper indentation and spacing. Study the examples in the book and the examples the instructor presents in the lectures and posts on the course website.
Put a comment header block at the top of each method formatted thusly:
/**
* A brief description of what the method does. */
Put a comment header block at the top of each source code file formatted thusly:
//******************************************************************************************************** // CLASS: classname (classname.java)
//
// DESCRIPTION
// A description of the contents of this file.
//
// COURSE AND PROJECT INFO
// CSE205 Object Oriented Programming and Data Structures, semester and year // Project Number: project-number
//
// AUTHOR
// your-name (your-email-addr) //********************************************************************************************************
----------------------------------------------------------------------
MAIN.JAVA
//**************************************************************************************************************
// CLASS: Main
//
// DESCRIPTION
// The Main class for Project 2.
//
// AUTHOR
// Kevin R. Burger (burgerk@asu.edu)
// Computer Science & Engineering
// School of Computing, Informatics, and Decision Systems Engineering
// Fulton Schools of Engineering
// Arizona State University, Tempe, AZ 85287-8809
// Web: http://www.devlang.com
//**************************************************************************************************************
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
/**
* Instantiate a Main object and call run() on the object.
*/
public static void main(String[] args) {
???
}
/**
* Calculates the tuition for each student. Write an enhanced for loop that iterates over each Student in
* pStudentList. For each Student, call calcTuition() on that Student. Note: this is a polymorphic method
* call.
*
* PSEUDOCODE
* EnhancedFor each student in pStudentList Do
* student.calcTuition()
* End EnhancedFor
*/
private void calcTuition(ArrayList<Student> pStudentList) {
???
}
/**
* Reads the student information from "p02-students.txt" and returns the list of students as an ArrayList
* <Student> object.
*
* PSEUDOCODE
* Declare and create an ArrayList<Student> object named studentList.
* Open "p02-students.txt" for reading using a Scanner object named in.
* While in.hasNext() returns true Do
* String studentType <- read next string from in
* If studentType is "C" Then
* studentList.add(readOnCampusStudent(in))
* Else
* studentList.add(readOnlineStudent(in))
* End If
* End While
* Close the scanner
* Return studentList
*/
private ArrayList<Student> readFile() throws FileNotFoundException {
???
}
/**
* Reads the information for an on-campus student.
*
* PSEUDOCODE
* Declare String object id and assign pIn.next() to id
* Declare String object named lname and assign pIn.next() to lname
* Declare String object named fname and assign pIn.next() to fname
* Declare and create an OnCampusStudent object. Pass id, fname, and lname as params to ctor.
* Declare String object named res and assign pIn.next() to res
* Declare double variable named fee and assign pIn.nextDouble() to fee
* Declare int variable named credits and assign pIn.nextInt() to credits
* If res.equals("R") Then
* Call setResidency(true) on student
* Else
* Call setResidency(false) on student
* End If
* Call setProgramFee(fee) on student
* Call setCredits(credits) on student
* Return student
*/
private OnCampusStudent readOnCampusStudent(Scanner pIn) {
???
}
/**
* Reads the information for an online student.
*
* PSEUDOCODE
* Declare String object id and assign pIn.next() to id
* Declare String object named lname and assign pIn.next() to lname
* Declare String object named fname and assign pIn.next() to fname
* Declare and create an OnlineStudent object. Pass id, fname, lname as params to the ctor.,
* Declare String object named fee and assign pIn.next() to fee
* Declare int variable named credits and assign pIn.nextInt() to credits
* If fee.equals("T")) Then
* Call setTechFee(true) on student
* Else
* Call setTechFee(false) on student
* End If
* Call setCredits(credits) on student
* Return student
*/
private OnlineStudent readOnlineStudent(Scanner pIn) {
???
}
/**
* Calls other methods to implement the sw requirements.
*
* PSEUDOCODE
* Declare ArrayList<Student> object named studentList
* try
* studentList = readFile()
* calcTuition(studentList)
* Call Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING) to sort the list
* writeFile(studentList)
* catch FileNotFoundException
* Print "Sorry, could not open 'p02-students.txt' for reading. Stopping."
* Call System.exit(-1)
*/
private void run() {
???
}
/**
* Writes the output file to "p02-tuition.txt" per the software requirements.
*
* PSEUDOCODE
* Declare and create a PrintWriter object named out. Open "p02-tuition.txt" for writing.
* EnhancedFor each student in pStudentList Do
* out.print(student id + " " + student last name + " " + student first name)
* out.printf("%.2f%n" student tuition)
* End EnhancedFor
* Close the output file
*/
private void writeFile(ArrayList<Student> pStudentList) throws FileNotFoundException {
???
}
}
----------------------------------------------------------
SORTER.JAVA
//**************************************************************************************************************
// CLASS: Sorter
//
// DESCRIPTION
// Implements the insertion sort algorithm to sort an ArrayList<> of Students.
//
// AUTHOR
// Kevin R. Burger (burgerk@asu.edu)
// Computer Science & Engineering Program
// Fulton Schools of Engineering
// Arizona State University, Tempe, AZ 85287-8809
// http:www.devlang.com
//**************************************************************************************************************
package tuition;
import java.util.ArrayList;
public class Sorter {
public static final int SORT_ASCENDING = 0;
public static final int SORT_DESCENDING = 1;
/**
* Sorts pList into ascending (pOrder = SORT_ASCENDING) or descending (pOrder = SORT_DESCENDING) order
* using the insertion sort algorithm.
*/
public static void insertionSort(ArrayList<Student> pList, int pOrder) {
for (int i = 1; i < pList.size(); ++i) {
for (int j = i; keepMoving(pList, j, pOrder); --j) {
swap(pList, j, j - 1);
}
}
}
/**
* Returns true if we need to continue moving the element at pIndex until it reaches its proper location.
*/
private static boolean keepMoving(ArrayList<Student> pList, int pIndex, int pOrder) {
if (pIndex < 1) return false;
Student after = pList.get(pIndex);
Student before = pList.get(pIndex - 1);
return (pOrder == SORT_ASCENDING) ? after.compareTo(before) < 0 : after.compareTo(before) > 0;
}
/**
* Swaps the elements in pList at pIndex1 and pIndex2.
*/
private static void swap(ArrayList<Student> pList, int pIndex1, int pIndex2) {
Student temp = pList.get(pIndex1);
pList.set(pIndex1, pList.get(pIndex2));
pList.set(pIndex2, temp);
}
}
-----------------------------------------------------------------------
TUITIONCONSTANTS.JAVA
//**************************************************************************************************************
// CLASS: TuitionConstants
//
// DESCRIPTION
// Constants that are used in calculating the tuition for on-campus and online students. Use these constants
// in the OnCampusStudent and OnlineStudent classes.
//
// AUTHOR
// Kevin R. Burger (burgerk@asu.edu)
// Computer Science & Engineering
// School of Computing, Informatics, and Decision Systems Engineering
// Fulton Schools of Engineering
// Arizona State University, Tempe, AZ 85287-8809
// Web: http://www.devlang.com
//**************************************************************************************************************
package tuition;
public class TuitionConstants {
public static final int> public static final int MAX_CREDITS = 18;
public static final int> public static final int> public static final int> public static final int>
}
Main Sorter --|-calcTuition(pStudentList: ArrayList): void uses -readFile ArrayList --readOnCampusStudentipln: Scanner): OnCampusStudent ± se ti t( List ArrayList Student List, Order int) id II-readOnlineStudent(pln: Scanner): OnlineStudent IIrun(): void I-writeFile(pStudentList: ArrayList): void Student -mCredits: int mFname: String -mld: String -mLname String .mTuition : double +Student(pld:String, pFname: String, pLname: String): «ctor» +calcTuition): void +compareTo(pStudent: Student): int «override» +getCredits(): int +getFirstName): String +getld): string +getLastName): String +getTuitition(): double +setCredits(pCredits: int): void +setFname(pFname: String): void +setld(pld: String): void +setLname(pLname: String): void #setTuition(pTuition: double): void «interface» Comparable compareTo(pStudent: Student): int OnlineStudent OnCampusStudent mTechFee: boolean +OnlineStudentipld:String, pFname: String, pLname: String): «ctor»I +calcTuition): void «override» +getTechFee(): boolean +setTechFee(pTechFee: boolean): void -mResident: boolean |--> -mi g ramFee: double | +OnCampusStudent(pld:String, pFname: String, pLname: String): «ctor» +calcTuition(): void «override» +getProgramFee): double +getResidency): boolean -+setProgramFee(pProgramFee: double): void +setResidency (pResident: boolean): void TuitionConstantsExplanation / Answer
Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
/**
* Instantiate a Main object and call run() on the object.
*/
public static void main(String[] args) {
try {
run();
} catch (FileNotFoundException e) {
System.out.println("Sorry, could not open 'p02-students.txt' for reading. Stopping.");
e.printStackTrace();
System.exit(0);
}
}
/**
* Calculates the tuition for each student. Write an enhanced for loop that iterates over each Student in
* pStudentList. For each Student, call calcTuition() on that Student. Note: this is a polymorphic method
* call.
*
* PSEUDOCODE
* EnhancedFor each student in pStudentList Do
* student.calcTuition()
* End EnhancedFor
*/
private static void calcTuition(List<Student> pStudentList) {
for (Student s : pStudentList) {
s.calcTuition();
}
}
/**
* Reads the student information from "p02-students.txt" and returns the list of students as an ArrayList
* <Student> object.
*
* PSEUDOCODE
* Declare and create an ArrayList<Student> object named studentList.
* Open "p02-students.txt" for reading using a Scanner object named in.
* While in.hasNext() returns true Do
* String studentType <- read next string from in
* If studentType is "C" Then
* studentList.add(readOnCampusStudent(in))
* Else
* studentList.add(readOnlineStudentW(in))
* End If
* End While
* Close the scanner
* Return studentList
*/
private static List<Student> readFile() throws FileNotFoundException {
List<Student> studentList = new ArrayList<>();
Scanner in = new Scanner(new File("students.txt"));
while(in.hasNextLine()) {
String studentType = in.next();
if(studentType.equals("C")) {
studentList.add(readOnCampusStudent(in));
} else {
studentList.add(readOnlineStudent(in));
}
}
in.close();
return studentList;
}
private static OnCampusStudent readOnCampusStudent(Scanner pIn) {
String id = pIn.next();
String lName = pIn.next();
String fName = pIn.next();
String res = pIn.next();
double fee = pIn.nextDouble();
int credits = pIn.nextInt();
OnCampusStudent ocStudent = new OnCampusStudent(id, lName, fName);
if (res.equals("R")){
ocStudent.setResidency(true);
} else {
ocStudent.setResidency(false);
}
ocStudent.setProgramFee(fee);
ocStudent.setCredits(credits);
return ocStudent;
}
private static OnlineStudent readOnlineStudent(Scanner pIn) {
String id = pIn.next();
String lName = pIn.next();
String fName = pIn.next();
String fee = pIn.next();
int credits = pIn.nextInt();
OnlineStudent olStudent = new OnlineStudent(id, lName, fName);
if (fee.equals("T")){
olStudent.setTechFee(true);
} else {
olStudent.setTechFee(false);
}
olStudent.setCredits(credits);
return olStudent;
}
private static void run() throws FileNotFoundException {
List<Student> studentList = new ArrayList<>();
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
}
/**
* Writes the output file to "p02-tuition.txt" per the software requirements.
*
* PSEUDOCODE
* Declare and create a PrintWriter object named out. Open "p02-tuition.txt" for writing.
* EnhancedFor each student in pStudentList Do
* out.print(student id + " " + student last name + " " + student first name)
* out.printf("%.2f%n" student tuition)
* End EnhancedFor
* Close the output file
*/
private static void writeFile(List<Student> pStudentList) throws FileNotFoundException {
PrintWriter out = new PrintWriter(new File("tuition.txt"));
for (Student s : pStudentList) {
double tuition = s.getTution();
out.print(s.getID() + " " + s.getLname() + " " + s.getFname() + " ");
out.printf("%.2f%n", tuition);
}
out.close();
}
}
Student.java
public abstract class Student implements Comparable<Student> {
private int mCredits;
private String mFname;
private String mID;
private String mLname;
private double mTution;
public Student (String pID, String pLName, String pFName) {
this.mFname = pFName;
this.mLname =pLName;
this.mID = pID;
}
public void calcTuition(){
//WILL BE OVERRIDDEN BY CHILDREN
};
public int getCredits() {
return mCredits;
}
public void setCredits(int pCredits) {
this.mCredits = pCredits;
}
public String getFname() {
return mFname;
}
public void setFname(String pFname) {
this.mFname = pFname;
}
public String getID() {
return mID;
}
public void setID(String pID) {
this.mID = pID;
}
public String getLname() {
return mLname;
}
public void setLname(String pLname) {
this.mLname = pLname;
}
public double getTution() {
return mTution;
}
protected void setTution(double pTution) {
this.mTution = pTution;
}
}
Sorter.java
import java.util.*;
public class Sorter {
public static final int SORT_ASCENDING = 0;
public static final int SORT_DESCENDING = 1;
/**
* Sorts pList into ascending (pOrder = SORT_ASCENDING) or descending (pOrder = SORT_DESCENDING) order
* using the insertion sort algorithm.
*/
public static void insertionSort(List<Student> pList, int pOrder) {
for (int i = 1; i < pList.size(); ++i) {
for (int j = i; keepMoving(pList, j, pOrder); --j) {
swap(pList, j, j - 1);
}
}
}
/**
* Returns true if we need to continue moving the element at pIndex until it reaches its proper location.
*/
private static boolean keepMoving(List<Student> pList, int pIndex, int pOrder) {
if (pIndex < 1) return false;
Student after = pList.get(pIndex);
Student before = pList.get(pIndex - 1);
return (pOrder == SORT_ASCENDING) ? after.compareTo(before) < 0 : after.compareTo(before) > 0;
}
/**
* Swaps the elements in pList at pIndex1 and pIndex2.
*/
private static void swap(List<Student> pList, int pIndex1, int pIndex2) {
Student temp = pList.get(pIndex1);
pList.set(pIndex1, pList.get(pIndex2));
pList.set(pIndex2, temp);
}
}
OnlineStudent.java
public class OnlineStudent extends Student {
private boolean mTechFee;
public OnlineStudent(String pID, String pFName, String pLName) {
super(pID, pFName, pLName);
}
@Override
public int compareTo(Student student) {
return 0;
}
@Override
public void calcTuition() {
double t = getCredits() * TuitionConstants.ONLINE_CREDIT_BASE;
if (this.getTechFee() == true) {
t = t + TuitionConstants.ONLINE_TECH_FEE;
}
this.setTution(t);
}
public boolean getTechFee() {
return mTechFee;
}
public void setTechFee(boolean mTechFee) {
this.mTechFee = mTechFee;
}
}
TuitionConstants.java
public class TuitionConstants {
public static final int> public static final int> public static final int> public static final int ONCAMP_RES_BASE = 5500;
public static final int> public static final int ONLINE_TECH_FEE = 125;
}
OnCampusStudent.java
public class OnCampusStudent extends Student {
private boolean mResident;
private double mProgramFee;
public OnCampusStudent(String pID, String pFName, String pLName) {
super(pID, pFName, pLName);
}
@Override
public void calcTuition() {
double t;
if(this.getResidency() == true) {
t = TuitionConstants.ONCAMP_RES_BASE;
} else {
t = TuitionConstants.ONCAMP_NONRES_BASE;
}
t = t + getProgramFee();
if (this.getCredits() > TuitionConstants.ONCAMP_MAX_CREDITS ){
t = t + (getCredits() - TuitionConstants.ONCAMP_MAX_CREDITS) * TuitionConstants.ONCAMP_ADD_CREDITS;
}
this.setTution(t);
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return 0;
}
public boolean getResidency() {
return mResident;
}
public void setResidency(boolean pResident) {
this.mResident = pResident;
}
public double getProgramFee() {
return mProgramFee;
}
public void setProgramFee(double pProgramFee) {
this.mProgramFee = pProgramFee;
}
}
students.txt
C 8230123345450 Flintstone Fred R 0 12
C 3873472785863 Simpson Lisa N 750 18
C 4834324308675 Jetson George R 0 20
O 1384349045225 Szyslak Moe - 6
O 5627238253456 Flanders Ned T 3
tuition.txt
8230123345450 Flintstone Fred 5500.00
3873472785863 Simpson Lisa 12950.00
4834324308675 Jetson George 6200.00
1384349045225 Szyslak Moe 5250.00
5627238253456 Flanders Ned 2750.00