View the step-by-step solution to:

make class called Payroll in your TMA03_Project_Q4_Sol project.



         make  class called Payroll in your TMA03_Project_Q4_Sol project.

  • i.Add a line of code to the Payroll class to declare an instance variable programmers, to reference a list of Programmer objects.
  • Next add a line of code to the Payroll constructor to assign a new instance of ArrayList to programmers.
  • (2 marks)
  • ii.In the Payroll class, add a method with the header private int computePay(Programmer p).
  • The method should return the programmer's grade multiplied by the number of hours they worked. For example, if the programmer's grade is 2, and the total hours they worked is 6, the method should return 12.
  • (2 marks)
  • iii.In the Payroll class public instance method called readEmployeeData() that takes no arguments and returns no value. The method will need to read from the file employees.txt, which can be found in the project folder for this question, and which contains the name, grade and hours worked for each of the programmers in comma-separated format, as explained at the beginning of this question.
  • The code for the method should do the following:
  • • Use a suitable OUFileChooser method to allow the user of the program to locate the file to be read.
  • • Construct a buffered file reader object to read the filename the user identified.
  • • After each line of the file is read, use a Scanner object to extract the programmer's data from it.
  • • For each line in the file a new Programmer object should be created and its instance variables set as follows:
  • - name should be set using the first token from the data for the current employee, when constructing the programmer object.
  • - grade should be set based on the next value from the line.
  • - hours should be set to the total hours worked for the current employee.
  • - To set the pay, compute the total pay the programmer is due using the method computePay() you wrote in part (ii). Use this value to set the programmer's pay.
  • • Finally, the instance of Programmer you've created and initialised, as above, should be added to the programmers list.
  • In order to gain full marks you will need to ensure that the input file is properly closed by your method and also that it handles possible exceptions sensibly, e.g. if the user chose the wrong file to be read.
  • You can test your code by executing:
  • Payroll proll = new Payroll();
  • proll.readEmployeeData();
  • in the OUWorkspace.
  • Having executed the method, you can use the inspector to inspect proll and check that its instance variable programmers references a list of the Programmers created from the data in the text file, with correct names, pay grades and pay.
  • (10 marks)
  • iv.create toString() method for the Programmer class, to return a string describing each programmer, as follows:
  • Sleepy Servlet (Grade 1, ID 1) Hours 351 Pay 351
  • The string contains the programmer's name, grade, ID (in parentheses), hours worked and pay. You will test this method in the next part.
  • (2 marks)
  • v.add public method for the Payroll class called showPayroll() that takes no arguments and returns no value. The method should iterate over programmers, and print out the string representation of each programmer.
  • (2 marks)

c.To add list of highest earning coders, the software mine wants the list of programmers organised in order of their pay from lowest to highest and they would like this information written to an output file called "payrollResults.txt".

  • i.So that you can sort programmers by pay, modify the Programmer class so that it implements the Comparable interface. you will need to modify the class header and add compareTo() method that will allow instances of Programmer to be sorted by ascending order of the value stored by their pay instance variable.
  • (3 marks)
  • ii.Now write the writePayroll() method for Payroll. The method should be public, take no arguments, and return no value.
  • The method should sort the receiver's list of programmers from smallest to largest pay. The resulting sorted programmer list should be written to the text file "payrollResults.txt", which should be saved in the project folder for this question. The program user will need to identify the appropriate location to store the file. The output for each programmer to be written to the file is its string representation as produced by toString().
  • Test this method so that you create the file "payrollResults.txt" in the required location. The resulting file should contain lines similar to the following, sorted by order of increasing pay:
  • Grumpy Gopher (Grade 2, ID 2) Hours 123 Pay 246
  • Doc Datamine (Grade 1, ID 4) Hours 312 Pay 312
  • Sleepy Servlet (Grade 1, ID 1) Hours 351 Pay 351


My code ( not sure if is right)

public class Payroll

{

  // instance variables - replace the example below with your own

   private ArrayList<Programmer> programmers;

  /**

   * Constructor for objects of class Payroll

   */

  public Payroll()

  {

     // initialise instance variables

    programmers = new ArrayList<Programmer>();

    

  }

   

  private int computePay(Programmer p)

  {

     int grade = p.getGrade();

     int hours = p.getHours();

     return grade * hours;

        

  }

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question