Question
A company needs a computer program to store data about its employees.
Part of the program is being written using object‑oriented programming.
The class Employee stores data about the employees. Each employee has an employee number, a job title and hourly pay rate. The class will also store the amount they are paid each week over a
52‑week year in a 1D array.
Employee
HourlyPay : REAL stores the amount each employee gets paid each hour
EmployeeNumber : STRING stores the employee’s unique number
JobTitle : STRING stores the employee’s job title
PayYear2022 : ARRAY[0:51] OF REAL stores the amount the employee has been paid each week
Constructor() initialises HourlyPay, EmployeeNumber and
JobTitle from the values passed as parameters initialises all 52 elements in PayYear2022 to 0.0
GetEmployeeNumber() returns the employee number
SetPay() takes the week number and number of hours worked that week as parameters calculates and stores the pay for that week in
PayYear2022
GetTotalPay() returns the total of all the values in PayYear2022
(a) (i) Write program code to declare the class Employee.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language appropriate constructor.
If you are writing program code in Python, include attribute declarations using comments.
Save your program as Question3_J2023.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[5]
(ii) The method GetEmployeeNumber() returns the employee number.
Write program code for the method GetEmployeeNumber().
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[2]
(iii) The method SetPay():
• takes a week number and the number of hours worked that week as parameters
• calculates the pay for that week by multiplying the hourly pay by the number of hours worked that week
• stores the calculated pay in the appropriate index for that week in PayYear2022.
Write program code for the method SetPay().
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[3]
(iv) The method GetTotalPay() returns the total of all the values in PayYear2022.
Write program code for the method GetTotalPay().
Save your program.
Copy and paste the program code into part 3(a)(iv) in the evidence document.
[2]
(b) The child class Manager inherits from the parent class Employee.
A manager gets a bonus. This bonus value is a percentage, for example 10.0%.
When calculating the pay, the number of hours the manager worked that week is increased by the bonus value.
Manager stores the bonus value, for example 10.0 represents a
BonusValue : REAL
10.0% increase
Constructor() takes bonus value, hourly pay, employee number and job title as parameters initialises BonusValue to its parameter value
SetPay() takes the week number and number of hours worked as parameters increases the number of hours worked by the bonus value calls the SetPay() method from the parent class
(i) Write program code to declare the class Manager.
You only need to declare the class and its constructor. Do not declare any other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(b)(i) in the evidence document.
[4]
(ii) The Manager method SetPay() overrides the method from the parent class and:
• takes the week number and number of hours worked as parameters
• increases the number of hours worked by the bonus value
• calls SetPay() from the parent class.
Write program code for the method SetPay().
Save your program.
Copy and paste the program code into part 3(b)(ii) in the evidence document.
[3]
(c) The main program has a global 1D array, EmployeeArray, to store data about eight employees. Each employee is stored as an object of type Employee.
The file Employees.txt stores data about the employees, in the order:
• hourly pay
• employee number
• bonus value (where included)
• job title.
Only employees who are managers have a bonus value saved. For example:
• The first employee is a Junior Developer, with employee number 12452 and an hourly pay of $15.22. This employee does not have a bonus value.
• The third employee is an Interface Manager, with employee number 02586 and an hourly pay of $22.50. This employee has a bonus value of 5.25%.
Write the main program to:
• declare the array to store data about 8 employees
• read in the data from the file for each employee
• instantiate each employee as either Employee (if the employee does not have a bonus value) or Manager (if the employee has a bonus value).
Save your program.
Copy and paste the program code into part 3(c) in the evidence document.
[7]
(d) The file HoursWeek1.txt stores the number of hours each employee has worked in week 1, in the order:
• employee number
• number of hours worked.
For example, the first set of data is for employee 21548 who has worked 50.0 hours.
The procedure EnterHours():
• reads in the values from the file
• finds the location of each employee in EmployeeArray
• calls the method SetPay() for each employee.
Write program code for EnterHours().
Save your program.
Copy and paste the program code into part 3(d) in the evidence document.
[4]
(e) (i) The main program needs to call EnterHours() and use the method GetTotalPay() to output the employee number and total pay for each of the eight employees.
Amend the main program to perform these tasks.
Save your program.
Copy and paste the program code into part 3(e)(i) in the evidence document.
[2]
(ii) Test your program.
Take a screenshot of the output.
Save your program.
Copy and paste the screenshot into part 3(e)(ii) in the evidence document.
[1]
Mark scheme
3(a)(i) For X = 0 To 51
PayYear2022(X) = 0.00
Next
End Sub
End Class
Python
class Employee:
#self.__HourlyPay single
#self.__EmployeeNumber string
#self.__JobTitle string
def __init__(self, EmpNumP, PayP, JobP):
self.__HourlyPay = PayP
self.__EmployeeNumber = EmpNumP
self.__JobTitle = JobP
self.__PayYear2022 = []#array 52 elements single
for x in range(0, 52):
self.__PayYear2022.append(0.00)
3(a)(ii) 1 mark each 2
Get method header (and end) with no parameters …
… returning employee number (without overriding)
Example program code:
Java
public String GetEmployeeNumber(){
return EmployeeNumber;
}
VB.NET
Public Function GetEmployeeNumber()
Return EmployeeNumber
End Function
Python
def GetEmployeeNumber(self):
return self.__EmployeeNumber
3(a)(iii) 1 mark each 3
Method header (and close) with two parameters (week number and number of hours)
Calculates pay as number of hours (parameter) * HourlyPay (attribute)
… stores result in correct index in PayYear2022
Example program code:
Java
public void SetPay(Integer WeekNumber, Double Hours){
PayYear2022[WeekNumber - 1] = Hours * HourlyPay;
}
VB.NET
Overridable Sub SetPay(WeekNumber, Hours)
PayYear2022(WeekNumber - 1) = Hours * HourlyPay
End Sub
Python
def SetPay(self, WeekNumber, Hours):
self.__PayYear2022[WeekNumber-1] = Hours * self.__HourlyPay
3(a)(iv) 1 mark each 2
Method header (and close) and returning calculated total (ignore parameters, allow return of any reasonable attempt
at calculation)
Totalling all elements in PayYear2022
Example program code:
Java
public Double GetTotalPay(){
Double TotalPay = 0.0;
for(Integer X = 0; X < 52; X++){
TotalPay = TotalPay + PayYear2022[X];
}
return TotalPay;
}
VB.NET
Public Function GetTotalPay()
Dim TotalPay As Single = 0
For X = 0 To 51
TotalPay = TotalPay + PayYear2022(X)
Next
Return TotalPay
End Function
Python
def GetTotalPay(self):
TotalPay = 0
for X in range (0, 52):
TotalPay = TotalPay + self.__PayYear2022[X]
return TotalPay
3(b)(i) 1 mark each 4
Class Manager header (and end) inheriting from Employee
Constructor within class (and end) taking 4 parameters…
…calling parent class constructor with 3 values from parameters
Declaring BonusValue (real) and assigning parameter to it within constructor
Example program code:
Java
class Manager extends Employee{
private Double BonusValue;
public Manager(String EmpNumP, Double PayP, String JobP, Double BonusP){
super(EmpNumP, PayP, JobP);
BonusValue = BonusP;
}
}
VB.NET
Class Manager
Inherits Employee
Private BonusValue As Single
Sub New(EmpNumP As String, PayP As Single, JobP As String, BonusP As Single)
MyBase.New(EmpNumP, PayP, JobP)
BonusValue = BonusP
End Sub
End Class
3(b)(i) Python
class Manager(Employee):
#BonusValue single
def __init__(self, EmpNumP, PayP, JobP, BonusP):
super().__init__(EmpNumP, PayP, JobP)
self.__BonusValue = BonusP
3(b)(ii) 1 mark each 3
Method SetPay header (and end) taking 2 parameters
Calculating hours (from parameter) * bonus as a percentage
Overriding / calling parent SetPay with week number from parameter and updated hours as parameters
Example program code:
Java
public void SetPay(Integer WeekNumber, Double Hours){
super.SetPay(WeekNumber, Hours * ((BonusValue / 100) + 1));
}
VB.NET
Overrides Sub SetPay(WeekNumber, Hours)
MyBase.SetPay(WeekNumber, Hours * ((BonusValue / 100) + 1))
End Sub
Alternative VB.NET:
Overloads Sub SetPay(WeekNumber, Hours)
SetPay(WeekNumber, Hours * ((BonusValue / 100) + 1))
End Sub
Python
def SetPay(self, WeekNumber, Hours):
Hours = Hours * (1 + self.__BonusValue / 100)
super().SetPay(WeekNumber, Hours)
3(c) 1 mark each to max 7 7
Opening file Employees.txt to read and closing file in an appropriate place
Exception handling with appropriate output for opening the file
Looping to EOF / 8 times
(Attempting to) Read in all lines from the file for each employee
For each employee:
Instantiating and storing an object of type Manager (not Employee) when bonus is included...
…with correct read in values
(otherwise) instantiating and storing an object of type Employee …
.. with correct read in values
Example program code:
Java
public static void main(String args[]){
Double Pay = 0.0;
String ID = "";
Double Bonus = 0.00;
String Title = "";
Integer NumberEmployees = 0;
String Temp = "";
String TextFile = "Employees.txt";
try{
FileReader f = new FileReader(TextFile);
BufferedReader Reader = new BufferedReader(f);
for(Integer X = 0; X < 8; X++){
Bonus = 0.00;
try{
Pay = Double.parseDouble(Reader.readLine());
ID = Reader.readLine();
Temp = Reader.readLine();
3(c) try{
Bonus = Double.parseDouble(Temp);
Title = Reader.readLine();
EmployeeArray[NumberEmployees] = new Manager(ID, Pay, Title, Bonus);
}catch(NumberFormatException e){
Title = Temp;
EmployeeArray[NumberEmployees] = new Employee(ID, Pay, Title);
} NumberEmployees++;
} catch(IOException ex){
}
} try{
Reader.close();
}catch(IOException ex){}
}catch(FileNotFoundException ex){
System.out.println("No file found");
}}
VB.NET
Dim Pay As Single
Dim ID As String
Dim Bonus As Single
Dim Title As String
Dim NumberEmployees As Integer = 0
Dim Temp As String
try
Dim TextFile As String = "Employees.txt"
Dim FileReader As New System.IO.StreamReader(TextFile)
For x = 0 To 7
Pay = CSng(FileReader.ReadLine())
ID = FileReader.ReadLine
Temp = FileReader.ReadLine
If Single.TryParse(Temp, Bonus) Then
Bonus = Temp
Title = FileReader.ReadLine()
EmployeeArray(NumberEmployees) = New Manager(ID, Pay, Title, Bonus)
3(c) Else
Title = Temp
EmployeeArray(NumberEmployees) = New Employee(ID, Pay, Title)
End If
NumberEmployees += 1
Next
FileReader.Close()
Catch ex As Exception
Console.WriteLine("Invalid file")
End Try
Python
#main
Pay = 0.00
ID = ""
Bonus = 0.00
Title = ""
Temp = ""
try:
TextFile = "Employees.txt"
File = open(TextFile, 'r')
for x in range(0, 8):
Pay = float(File.readline())
ID = File.readline()
Temp = File.readline()
try:
Bonus = float(Temp)
Title = File.readline()
EmployeeArray.append(Manager(ID, Pay, Title, Bonus))
except:
Title = Temp
EmployeeArray.append(Employee(ID, Pay, Title))
3(c) File.close()
except IOError:
print("Could not find file")
3(d) 1 mark each to max 4 4
Procedure header EnterHours() (ignore parameters) and opening file to read and closing file in appropriate place
Exception handling with appropriate output for opening file
Looping to EOF/8 times and reading in each line
Searching array for employee number …
… using GetEmployeeNumber()
…calling SetPay() with the number of hours and week number 1 as parameters, for that employee in the array
Example program code:
Java
public static void EnterHours(){
String TextFile = "HoursWeek1.txt";
String EmpID = "";
try{
FileReader f = new FileReader(TextFile);
BufferedReader Reader = new BufferedReader(f);
for(Integer X = 0; X < 8; X++){
try{
EmpID = Reader.readLine();
for(Integer Y = 0; Y < 8; Y++){
if(Employees[Y].GetEmployeeNumber().equals(EmpID)){
Employees[Y].SetPay(1, Double.parseDouble(Reader.readLine()));
}
}
} catch(IOException ex){
}
}
try{
Reader.close();
}catch(IOException ex){}
}catch(FileNotFoundException e){
System.out.println("File not found");
}
}
3(d) VB.NET
Sub EnterHours()
try
Dim TextFile As String = "HoursWeek1.txt"
Dim FileReader As New System.IO.StreamReader(TextFile)
Dim EmpId As String
For X = 0 To 7
EmpId = FileReader.ReadLine()
For Y = 0 To 7
If Employees(Y).GetEmployeeNumber = EmpId Then
Employees(Y).SetPay(1, CSng(FileReader.ReadLine()))
End If
Next
Next
FileReader.Close()
Catch ex As Exception
Console.WriteLine("Invalid file")
End Try
End Sub
Python
def EnterHours():
try:
TextFile = "HoursWeek1.txt"
File = open(TextFile, 'r')
EmpID = ""
for X in range(0, 8):
EmpID = File.readline()
for Y in range(0, 8):
if Employees[Y].GetEmployeeNumber() == EmpID:
Employees[Y].SetPay(1, float(File.readline()))
except IOError:
print("Could not find file")
3(e)(i) 1 mark each 2
Calling EnterHours() and looping through each employee …
… outputting the employee number and their total pay using GetTotalPay() and GetEmployeeNumber()
Example program code:
Java
EnterHours();
for(Integer X = 0; X < 8; X++){
System.out.println(Employees[Y].GetEmployeeNumber() + " " + Employees[Y].GetTotalPay());
}
VB.NET
EnterHours()
For Y = 0 To 7
Console.WriteLine(Employees(Y).GetEmployeeNumber & " " & Employees(Y).GetTotalPay())
Next
Python
EnterHours()
for(Y in range(0, 8):
print(Employees[Y].GetEmployeeNumber(), " ", Employees[Y].GetTotalPay())
3(e)(ii) 1 mark for screenshot e.g. 1