Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a decently sized project to complete, but I\'m having a really hard time

ID: 3744413 • Letter: I

Question

I have a decently sized project to complete, but I'm having a really hard time completing it. I would greatly appreciate any help on this. I have some code that I've gotten started with, posted below.

CODE I HAVE SO FAR:

MAIN

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) {
        String id = pIn.next();
        String lname = pIn.next();
        String fname = pIn.next();
        OnCampusStudent student = new OnCampusStudent(id, fname, lname);
        String res = pIn.next();
        double fee = pIn.nextDouble();
        int credits = pIn.nextInt();
        student.setResidency(res.equals("R"));
        student.setProgramFee(fee);
        student.setCredits(credits);
        return student;
    }

    /**
     * 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() {
        ArrayList<Student> studentList;
        try {
            studentList = readFile();
            calcTuition(studentList);
            Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
            writeFile(studentList);
        } catch (FileNotFoundException pExcept) {
            System.out.println("Sorry, could not open 'p02-students.txt' for reading. Stopping.");
            System.exit(-1);
        }
    }

    /**
     * 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

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);
    }

}

STUDENT

public abstract class Student implements Comparable<Student> {

    private int mCredits;
    private String mFname;
    private String mId;
    private String mLname;
    private double mTuition;

    /**
     * Creates a Student object and initializes the mId, mFname, and mLname instance variables.
     */
    public Student(String pId, String pFname, String pLname) {
        setId(pId);
        setFirstName(pFname);
        setLastName(pLname);
    }

    /**
     * calcTuition() is to be implemented by subclasses of Student.
     */
    public abstract void calcTuition();

    /**
     * The Comparable interface declares one method compareTo() that must be implemented. This
     * method is called from Sorter.keepMoving() to compare the mId fields of two Students.
     *
     * Compares the mId fields of this Student and pStudent. Returns -1 if this Student's mId
     * field is less than pStudent's mId field. Returns 0 if this Student's mId field is equal to
     * pStudent's mId field. Returns 1 if this Student's mId field is greater than pStudent's mId
     * field. The compareTo
     */
    @Override
    public int compareTo(Student pStudent) {
        return getId().compareTo(pStudent.getId());
    }

    /**
     * Accessor method for mCredits.
     */
    public int getCredits() {
        return mCredits;
    }

    /**
     * Accessor method for mFname.
     */
    public String getFirstName() {
        return mFname;
    }

    /**
     * Accessor method for mId.
     */
    public String getId() {
        return mId;
    }

    /**
     * Accessor method for mLname.
     */
    public String getLastName() {
        return mLname;
    }

    /**
     * Accessor method for mTuition.
     */
    public double getTuition() {
        return mTuition;
    }

    /**
     * Mutator method for mCredits.
     */
    public void setCredits(int pCredits) {
        mCredits = pCredits;
    }

    /**
     * Mutator method for mFname.
     */
    public void setFirstName(String pFname) {
        mFname = pFname;
    }

    /**
     * Mutator method for mId.
     */
    public void setId(String pId) {
        mId = pId;
    }

    /**
     * Mutator method for mLname.
     */
    public void setLastName(String pLname) {
        mLname = pLname;
    }

    /**
     * Mutator method for mTuition.
     */
    protected void setTuition(double pTuition) {
        mTuition = pTuition;
    }

}

TUITION CONSTANTS

public class TuitionConstants {

    public static final int>     public static final int MAX_CREDITS         = 18;
    public static final int>     public static final int ONCAMP_RES_BASE     = 5500;
    public static final int>     public static final int ONLINE_TECH_FEE     = 125;

}

Explanation / Answer

Note: I've changed your implementation of compareTo in Student.java class as I found it to wrong for the given requirements. Other than that I haven't changed any of your existing code.

Not including TuitionConstants.java and Sorter.java as they do not have any changes.

Main.java

package springfieldstateuniversity;

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) {

new Main().run();

}

/**

* 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) {

for(Student student : pStudentList) {

student.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(readOnlineStudent(in))

* End If

* End While

* Close the scanner

* Return studentList

*/

private ArrayList<Student> readFile() throws FileNotFoundException {

ArrayList<Student> studentList = new ArrayList<>();

Scanner in = new Scanner(new File("p02-students.txt"));

  

while(in.hasNext()) {

String studentType = in.next();

if(studentType.equals("C")) {

studentList.add(readOnCampusStudent(in));

}

else {

studentList.add(readOnlineStudent(in));

}

}

in.close();

return studentList;

}

  

/**

* 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) {

String id = pIn.next();

String lname = pIn.next();

String fname = pIn.next();

OnCampusStudent student = new OnCampusStudent(id, fname, lname);

String res = pIn.next();

double fee = pIn.nextDouble();

int credits = pIn.nextInt();

student.setResidency(res.equals("R"));

student.setProgramFee(fee);

student.setCredits(credits);

return student;

}

/**

* 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) {

String id = pIn.next();

String lname = pIn.next();

String fname = pIn.next();

OnlineStudent OnlineStudent(id, fname, lname);

String fee = pIn.next();

int credits = pIn.nextInt();

if(fee.equals("T")) {

onlineStudent.setTechFee(true);

}

else {

onlineStudent.setTechFee(false);

}

onlineStudent.setCredits(credits);

return onlineStudent;

}

  

/**

* 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() {

ArrayList<Student> studentList;

try {

studentList = readFile();

calcTuition(studentList);

Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);

writeFile(studentList);

} catch (FileNotFoundException pExcept) {

System.out.println("Sorry, could not open 'p02-students.txt' for reading. Stopping.");

System.exit(-1);

}

}

/**

* 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 {

PrintWriter out = new PrintWriter(new File("p02-tuiton.txt"));

  

for(Student student : pStudentList) {

out.print(student.getId() + " " + student.getLastName() + " " + student.getFirstName());

out.printf("%.2f%n", Double.toString(student.getTuition()));

}

out.close();

}

}

  

Student.java

package springfieldstateuniversity;

public abstract class Student implements Comparable<Student> {

private int mCredits;

private String mFname;

private String mId;

private String mLname;

private double mTuition;

/**

* Creates a Student object and initializes the mId, mFname, and mLname instance variables.

*/

public Student(String pId, String pFname, String pLname) {

setId(pId);

setFirstName(pFname);

setLastName(pLname);

}

/**

* calcTuition() is to be implemented by subclasses of Student.

*/

public abstract void calcTuition();

/**

* The Comparable interface declares one method compareTo() that must be implemented. This

* method is called from Sorter.keepMoving() to compare the mId fields of two Students.

*

* Compares the mId fields of this Student and pStudent. Returns -1 if this Student's mId

* field is less than pStudent's mId field. Returns 0 if this Student's mId field is equal to

* pStudent's mId field. Returns 1 if this Student's mId field is greater than pStudent's mId

* field. The compareTo

*/

@Override

public int compareTo(Student pStudent) {

// Your implementation.

// return getId().compareTo(pStudent.getId());

  

// My implementation.

// I think your implemenation of comparision is incorrect given the above requirements. Please feel free to revert.

int currentStudentId = Integer.parseInt(this.getId());

int studentIdToBeCompared = Integer.parseInt(pStudent.getId());

if(currentStudentId < studentIdToBeCompared) return -1;

else if(currentStudentId > studentIdToBeCompared) return 1;

else return 0;

  

}

/**

* Accessor method for mCredits.

*/

public int getCredits() {

return mCredits;

}

/**

* Accessor method for mFname.

*/

public String getFirstName() {

return mFname;

}

/**

* Accessor method for mId.

*/

public String getId() {

return mId;

}

/**

* Accessor method for mLname.

*/

public String getLastName() {

return mLname;

}

/**

* Accessor method for mTuition.

*/

public double getTuition() {

return mTuition;

}

/**

* Mutator method for mCredits.

*/

public void setCredits(int pCredits) {

mCredits = pCredits;

}

/**

* Mutator method for mFname.

*/

public void setFirstName(String pFname) {

mFname = pFname;

}

/**

* Mutator method for mId.

*/

public void setId(String pId) {

mId = pId;

}

/**

* Mutator method for mLname.

*/

public void setLastName(String pLname) {

mLname = pLname;

}

/**

* Mutator method for mTuition.

*/

protected void setTuition(double pTuition) {

mTuition = pTuition;

}

}

OnlineStudent.java

package springfieldstateuniversity;

public class OnlineStudent extends Student {

public OnlineStudent(String pId, String pFname, String pLname) {

super(pId, pFname, pLname);

}

boolean mTechFee;

public boolean getTechFee() {

return mTechFee;

}

public void setTechFee(boolean pTechFee) {

mTechFee = pTechFee;

}

@Override

public void calcTuition() {

double t = this.getCredits() * TuitionConstants.ONLINE_CREDIT_RATE;

if(this.getTechFee()) {

t = t + TuitionConstants.ONLINE_TECH_FEE;

}

this.setTuition(t);

}

}

OnCampusStudent.java

package springfieldstateuniversity;

class OnCampusStudent extends Student {

boolean mResident;

double mProgramFee = 0;

OnCampusStudent(String pId, String pFname, String pLastname) {

super(pId, pFname, pLastname);

}

public boolean getResidency() {

return mResident;

}

public void setResidency(boolean mResident) {

this.mResident = mResident;

}

public double getProgramFee() {

return mProgramFee;

}

public void setProgramFee(double mProgramFee) {

this.mProgramFee = mProgramFee;

}

@Override

public void calcTuition() {

double t;

if(this.getResidency()) {

t = TuitionConstants.ONCAMP_RES_BASE;

}

else {

t = TuitionConstants.ONCAMP_NONRES_BASE;

}

t = t + this.getProgramFee();

if(this.getCredits() > TuitionConstants.MAX_CREDITS) {

t = t + (this.getCredits() - TuitionConstants.MAX_CREDITS) * TuitionConstants.ONCAMP_ADD_CREDITS;

}

this.setTuition(t);

}

}