Page 1 of 1

I have this java programming lab that I started but my code has errors and I just want it corrected so that it is error

Posted: Tue Jul 12, 2022 8:27 am
by answerhappygod
I have this java programming lab that I started but my code haserrors and I just want it corrected so that it is error free andruns correctly. Follow the instructions in the lab and add whatevermethods need to be added in order to get it to run properly. Needasap. I'll give a big thumbs up if done correctly. Thank you.
I Have This Java Programming Lab That I Started But My Code Has Errors And I Just Want It Corrected So That It Is Error 1
I Have This Java Programming Lab That I Started But My Code Has Errors And I Just Want It Corrected So That It Is Error 1 (77.64 KiB) Viewed 38 times
I made the new package named lab within the lab 4 project and aclass within the lab package named Staff. As per the directions ofthe exercise, I copied the method into the Staff class that islisted in the above screenshots but I have many errors. Pleasecorrect the errors for me in my code by adding in the necessaryimport statements and correcting any other errors that aregenerated to get the output like the one in the images.
Here is the code from my program in text form so that you caneasily copy it:
package lab;
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;
import assignment.Score;import assignment.StudentWorker;import assignment.TempWorker;
public class Staff { public intreadFromTextFile(String filenameAndPath) { int countOfBadLines = 0; //try catch block for exceptions try {//start of outer try block //open the text file FileReader fr = new FileReader(filenameAndPath);//Creates a new FileReader, given the name of the file to readfrom. BufferedReader br = new BufferedReader(fr);//Readstext from a character-input stream, buffering characters so as toprovide for the efficient reading
//read the info from the file String aLineFromTheTextFile = br.readLine(); //Readthe first line from the opened test file try {//start of our inner tryblock String []dataForASingleEmployee = aLineFromTheTextFile.split(",");//one lineis one employee's info. Fields are separated by # symbol String name =dataForASingleEmployee[0];//first field is name int id =Integer.parseInt(dataForASingleEmployee[1]); //second field isssn double payRate =Double.parseDouble(dataForASingleEmployee[2]); // third field ispay rate per hour double hoursWorked =Double.parseDouble(dataForASingleEmployee[3]); // fourth field ishours worked for the period //TempWorker only needsthese 4 fields so if we have only 4 fields then this is aTempWorker if(dataForASingleEmployee.length == 4) {//TempWorker TempWorkertemp = new TempWorker(name, id); //create TempWorker object temp.setHoursWorked(hoursWorked); // add hours worked for theperiod temp.setPayRate(payRate); // set pay rate per hour this.addEmployee(temp); // add employee }//end if //StudentWorker needs the4 previous fields plus 6 others, so if we have 10 fields then thisis a StudentWorker else if(dataForASingleEmployee.length == 10) {//StudentWorker StudentWorker temp = new StudentWorker(name, id); //createStudentWorker object temp.setHoursWorked(hoursWorked);//add hours worked for theperiod temp.setPayRate(payRate); // set pay rate per hour Strings1Name = dataForASingleEmployee[4]; // get the score name double s1Val= Double.parseDouble(dataForASingleEmployee[5]); //get the scorevalue temp.AddScore(new Score(s1Name, s1Val)); // Create a Scoreobject and add to StudentWorker Strings2Name = dataForASingleEmployee[6];// get the score name double s2Val= Double.parseDouble(dataForASingleEmployee[7]);//get the scorevalue temp.AddScore(new Score(s2Name, s2Val));// Create a Scoreobject and add to StudentWorker Strings3Name = dataForASingleEmployee[8];// get the score name double s3Val= Double.parseDouble(dataForASingleEmployee[9]);//get the scorevalue temp.AddScore(new Score(s3Name, s3Val));// Create a Scoreobject and add to StudentWorker this.addEmployee(temp); // add employee }//end else if else {//we did not haveexactly 4 or 10 fields to consider the line useful for creating aStudentWorker or TempWorker System.out.println("Did not have exactly 4 or 10 fields toconsider the line useful for creating anEmployee"+(++countOfBadLines)); }//end else aLineFromTheTextFile =br.readLine(); //read the next line }//end inner try catch (IndexOutOfBoundsException iobe){ //we had less fields thanexpected on the text line System.out.println("Whoops! We had less fields thanexpected!"); }//end catch //close file br.close(); fr.close(); }//end outer try catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }//end catch return countOfBadLines; }
private voidaddEmployee(Object tempWorker) { // TODOAuto-generated method stub } public static voidmain(String[] args) { Staff staff = new Staff(); staff.readFromTextFile("EmployeeRawData.csv");
System.out.println("Total pay for all employees on staff is$"+staff.calcPayForAllEmployees()+"\n"); System.out.println(staff.toStringOnlyStudentWorkers());
System.out.println(staff.toStringOnlyTempWorkers()); }
}
NOTE: Score, StudentWorker, and TempWorker were former classesthat were used, and I have as imports in this program. If you needthe code for those, please let me know. Thank you.
Exercise 1: ReadFromTextFile   Recall our Staff class had the following methods: - public ArrayList < Employee > getAllEmployees() - public void setAllEmployees(ArrayList < Employee > allEmployees) - public void addEmployee(Employee worker) - public String toStringOnlyStudentWorkers() - public String toStringOnlyTempWorkers() - public double calcPayForAllEmployees() We will add an additional method to read lines of employee data from a text file, create employee objects and add them to the ArrayList in the Staff class. You can use your version of the Staff class or you can use my version provided in this lab. A. Add the following method to the Staff class.


B. Make sure to add the necessary import statements and fix any errors reported. C. Commit and push your code to github. D. Now add a main method so we can test what we have so far. public static void main(String[] args) \{ Staff staff = new Staff() staff.readFromTextFile("EmployeeRawData.csv"); System.out.println("Total pay for all employees on staff is \( \$ "+ \)quot;+ staff.calcPayForAllEmployees ()+"n′′); System.out.println(staff.toStringOnlyStudentWorkers ()) System.out.println(staff.toStringOnlyTempWorkers () ); \} E. Run the program. The output to the console should look something like Total pay for all employees on staff is $900.0 Mary 101 - pay for this period is $900.00 F. Why did it display only one employee?