Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question concerns a Java program to set up and maintain information about programmers who work for the Milton Software Mine. Details about the programmers

This question concerns a Java program to set up and maintain information about programmers who work for the Milton Software Mine.

Details about the programmers names, their pay grades and the hours they have worked over the last six months are recorded in the file employees.txt, in the format:

Firstname Secondname,paygrade,hours,hours,hours,hours,hours,hours

The employees names are separated by a space; the pay grade is an integer (1, 2 or 3) and then there are six integer figures representing hours worked in each of six months. The items are separated by commas, and there are no spaces around the commas. There is a line like this in the file employees.txt for each employee.

Launch BlueJ and open the project TMA03_Project_Q4 in the TMA03Download folder you unzipped earlier, then immediately save the project as TMA03_Project_Q4_Sol in the same folder.

a.Open the Programmer class, which contains declarations for instance variables, a partially completed constructor, and some getter and setter methods. The purpose of each instance variable is indicated in the comments accompanying the declarations. The instance variable name is initialised by the constructor. Programmer ID numbers are to be allocated on a sequential basis as each Programmer object is created: the first Programmer whose details are read from the file should have the number 1, the second 2, the third 3, and so on.i.The ID to be allocated to the next new instance of Programmer is to be held in a private static variable of the Programmer class called nextId. Add a single line of code to the Programmer class to declare this class variable and initialise it to 1.

(1 mark)

ii.Add code to the constructor to set the id instance variable of a newly created Programmer object to the value of the class variable nextId. Then increment the value held by nextId by 1, so that it will hold the number of the next Programmer object to be created.

Test your code by creating several instances of Programmer in the OUWorkspace and inspecting them. You should find that your Programmer objects all have the same state except for their id instance variables, which should have the values 1, 2, 3 and so on. Note that we have provided test code for you in the README.TXT file for this project.

(1 mark)

b.Create a 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 programmers grade multiplied by the number of hours they worked. For example, if the programmers grade is 2, and the total hours they worked is 6, the method should return 12.

(2 marks)

iii.In the Payroll class write a 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 programmers 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 programmers pay.

?Finally, the instance of Programmer youve 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.Write a 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 programmers name, grade, ID (in parentheses), hours worked and pay. You will test this method in the next part.

(2 marks)

v.Write a 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 create a 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. To do this you will need to modify the class header and write a 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 receivers 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

Copy the output to your Solution Document.

(7 marks)

**********employee.txt*****************

Sleepy Servlet,1,11,22,55,66,23,34 Grumpy Gopher,2,23,56,67,54,12,111 Sneezy Sandbox,3,34,54,67,78,34,12 Doc Datamine,1,34,65,89,23,78,23 Bashful Bandwidth,2,54,78,90,45,34,97 Dopey Desktop,3,34,65,78,89,98,34,78 Happy Hyperlink,3,34,54,67,87,88,78 Proud Packet,2,54,65,67,89,34,65,87 Luxuria Laptop,1,54,65,67,89,98,56 Slothful Screenshot,1,65,78,89,89,34,23 Avaricious Algorithm,2,45,65,67,87,89,89 Irate Iteration,2,54,56,67,87,89,65 Gluttonous Gui,1,54,65,67,87,89,89 Envious Ethernet,3,67,78,89,89,87,45

**********Programmer class***************

/** * Class Programmer - Models a programmer * * @author M250 Course Team * @version Version 1 */ public class Programmer { private int id; // programmer's id private String name; // programmer's name private int grade; // Determines pay; 1, 2 or 3 private int pay; // total pay in gold coins private int hours; // total hours worked

/** * Constructor for objects of class programmer */ public Programmer(String aName) { super(); this.name = aName;

}

public int getId() { return this.id; }

public String getName() { return name; }

public int getGrade() { return grade; }

public void setGrade(int aGrade) { if (aGrade >= 1 && aGrade <= 3) { this.grade = aGrade; } else { throw new IllegalArgumentException("The grade " + aGrade + " does not exist"); } }

public int getPay() { return pay; }

public void setPay(int pay) { this.pay = pay; }

public int getHours() { return hours; }

public void setHours(int hours) { this.hours = hours; } }

*******************README.txt****************

//(a)(ii) checking if programmer id's are being incremented Programmer p = new Programmer(); Programmer q = new Programmer(); Programmer r = new Programmer(); System.out.println(p.getID()); System.out.println(q.getID()); System.out.println(r.getID());

//(b)(iv) testing the toString() method - these objects //have default values set for their instance variables System.out.println(p); System.out.println(q); System.out.println(r);

//(c) Test reading the employee data file, //displaying the payroll (using toString of Programmer) //and writing the payroll to the file payrollResults.txt Payroll pr = new Payroll(); pr.readEmployeeData(); System.out.println("Results.... "); pr.showPayroll(); pr.writePayroll();

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions