I am really stuck on this JAVA Program. The code and project is listed below. I
ID: 667947 • Letter: I
Question
I am really stuck on this JAVA Program. The code and project is listed below. I have 4 seperate classes under my "tuition" package: Main, Student, Sorter, TuitionConstants. Whenever I run main with all of these files in the same package through netbeams I get the error ...
xception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at tuition.Main.readFile(Main.java:35)
at tuition.Main.run(Main.java:90)
at tuition.Main.main(Main.java:14)
I desperately need some help. Thanks to anyone who has some input! Cheers
/// MAIN ////////
************************
package tuition;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.run();
}
private void calcTuition(ArrayList pStudentList) {
for (Student s : pStudentList) {
s.calcTuition();
}
}
/**
* Read class reads in the file as an array list while initiating the file
* not found exception and adds info to studentList object
*/
private ArrayList readFile() throws FileNotFoundException {
ArrayList<Student> studentList = new ArrayList<Student>(); //Creating an ArrayList object named studentList
File file = new File("p02-students.txt"); //Creating a new file instance named file
try {
Scanner in = new Scanner(file);
while (in.hasNext()) {
String studentType = in.next();
if (studentType.equals("C")) {
studentList.add(readOnCampusStudent(in));
} else {
studentList.add(readOnlineStudent(in));
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return studentList;
}
private OnCampusStudent readOnCampusStudent(Scanner pIn) {
String id = pIn.next();
String lname = pIn.next();
String fname = pIn.next();
Student OnCampusStudent(id, fname, lname);
String res = pIn.next();
double fee = pIn.nextDouble();
int credits = pIn.nextInt();
if (res.equals("R")) {
onCampusStudent.setResidency(true);
} else {
onCampusStudent.setResidency(false);
}
onCampusStudent.setProgramFee(fee);
onCampusStudent.setCredits(credits);
return onCampusStudent;
}
private OnlineStudent readOnlineStudent(Scanner pIn) {
String id = pIn.next();
String lname = pIn.next();
String fname = pIn.next();
Student 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;
}
/**
* run class takes the information in the p02-students.txt file puts it into
* an array format and runs it through the sorter class which formats it
* into descending or ascending order finally we store this info into
* studentList. If file fails to be accessed we catch it and print a failure
*/
private void run() {
ArrayList<Sorter> studentList = new ArrayList<Sorter>();
try {
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
} catch (FileNotFoundException e) {
System.out.println("Sorry, could not open 'p02-students.txt' for reading.");
System.exit(-1);
}
}
private void writeFile(ArrayList pStudentList) throws FileNotFoundException {
File file = new File("p02-tuition.txt");
PrintWriter out = new PrintWriter(file);
for (Student s : pStudentList) {
out.print(s.getId() + " " + s.getLName() + " " + s.getFName());
out.printf("%.2f%n", s.getTuition());
}
printWriter.close();
}
}
/// STUDENT //////
*****************************
package tuition;
public abstract class Student implements Comparable<Student> {
private int mCredits;
private String mFname;
private String mLname;
private int mId;
private double mTuition;
//constructor of Student class
public Student(String pFname, String pLname) {
mFname = pFname;
mLname = pLname;
}
//Implement this method in the subclass
//of student class
public abstract void calcTuition();
//Set credits of student
public void setCredits(int pCredits) {
mCredits = pCredits;
}
//Returns the credits of student
public int getCredits() {
return mCredits;
}
//Set first name
public void setFname(String pFname) {
mFname = pFname;
}
//Returns first name
public String getFirstName() {
return mFname;
}
//Set id
public void setid(int pId) {
mId = pId;
}
//Retunrns id
public int getid() {
return mId;
}
//Set last name
public void setLname(String pLname) {
mLname = pLname;
}
//Returns last name
public String getLastName() {
return mLname;
}
//Returns the tuition fee
public void setTuition(double pTuition) {
mTuition = pTuition;
}
//Returns the tuition fee
public double getTuition() {
return mTuition;
}
/*Override the method compareTo that takes the Student object and
*compares the getid of the object and return 0 if the both id's are
*equal.
*returns -1 if the getid is less than pStudnet id
*returns 1 if the getid is greater than pStudnet id
* */
@Override
public int compareTo(Student pStudnet) {
if (getid() < pStudnet.getid()) {
return -1;
} else if (getid() > pStudnet.getid()) {
return 1;
} else {
return 0;
}
}
}
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().run();
}
public Main()
{
}
private void calcTuition(ArrayList<Student> pStudentList)
{
for (Student s : pStudentList) {
s.calcTuition();
}
}
private ArrayList<Student> readFile() throws FileNotFoundException {
ArrayList<Student> studentList = new ArrayList<Student>();
info.add(type, id, lname, fname, resstat, prfee, credits);
String fName = "p02-students.txt";
Scanner in = new Scanner(new File(fName));
String studentType:
while (in.hasNext())
{
studentList.add(
if (in.next() == "C")
{
studentList.add(readOnCampusStudent(in));
}
studentList.add(readOnlineStudent(in));
in.nextLine()
}
in.close();
return studentList;
}
}
private OnCampusStudent readOnCampusStudent(Scanner pIn) {
String id, lname, fname;
id = pIn.next();
lname = pIn.next();
fname = pIn.next();
private OnCampusStudent(String pId, String pLname, String pFname)
{
String res;
Double fee;
Int credits;
res = pIn.next();
fee = pIn.nextDouble();
credits = pIn.nextInt();
if (res == "C")
{
residency= (true)(in));
}
studentList.add(readOnlineStudent(in));
in.nextLine();
}
}
private OnlineStudent readOnlineStudent(Scanner pIn) {
String id, lname, fname;
id = pIn.next();
lname = pIn.next();
fname = pIn.next();
}
private void run() {
ArrayList<Sorter> studentList = new ArrayList<Sorter>();
try {
studentList = readFile();
calcTuition(studentList);
Sorter.insertionSort(studentList, Sorter.SORT_ASCENDING);
writeFile(studentList);
}
catch (FileNotFoundException e) {
System.out.println("Sorry, could not open 'p02-students.txt' for reading.");
System.exit(-1);
}
}
private void writeFile(ArrayList<Student> pStudentList) throws FileNotFoundException {
File file = new File("p02-tuition.txt");
PrintWriter out = new PrintWriter(file);
for (Student s : pStudentList)
{
out.print(s.getId() + " " + s.getLName() + " " + s.getFName());
out.printf("%.2f%n", s.getTuition());
}
printWriter.close();
}
}
package tuition;
public abstract class Student implements Comparable<Student> {
private int mCredits;
private String mFname;
private String mLname;
private int mId;
private double mTuition;
//constructor of Student class
public Student(String pFname, String pLname) {
mFname = pFname;
mLname = pLname;
}
//Implement this method in the subclass
//of student class
public abstract void calcTuition();
//Set credits of student
public void setCredits(int pCredits) {
mCredits = pCredits;
}
//Returns the credits of student
public int getCredits() {
return mCredits;
}
//Set first name
public void setFname(String pFname) {
mFname = pFname;
}
//Returns first name
public String getFirstName() {
return mFname;
}
//Set id
public void setid(int pId) {
mId = pId;
}
//Retunrns id
public int getid() {
return mId;
}
//Set last name
public void setLname(String pLname) {
mLname = pLname;
}
//Returns last name
public String getLastName() {
return mLname;
}
//Returns the tuition fee
public void setTuition(double pTuition) {
mTuition = pTuition;
}
//Returns the tuition fee
public double getTuition() {
return mTuition;
}
@Override
public int compareTo(Student pStudnet) {
if (getid() < pStudnet.getid()) {
return -1;
} else if (getid() > pStudnet.getid()) {
return 1;
} else {
return 0;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.