Homework 7 The objective of this homework is to give you experience using inheri
ID: 3806575 • Letter: H
Question
Homework 7
The objective of this homework is to give you experience using inheritance with Ruby. You will refactor the software you generated for Homework 4 in C++ by implementing the same functions using Ruby.
For this assignment, you will use the base class Employee, which was implemented with the files Employee.h and Employee.cpp.
Each employee object has a user id; a first and last name; a middle initial; and a department code (integer). Your implementation must now add two derived classes. The first one will be for a salaried employee, which will require a monthly salary variable. You will need member functions including a constructor, set and get salary functions, a salary calculation function, and a print function. For versatility, you might include a variable that specifies what fraction of time the person worked and use the fraction in the salary calculation. Hint: set the fraction default to one. For consistency, name your salaried employee class as SalariedEmployee
Your second class should represent an hourly worker. In this case you will need to store the hours worked and the hourly rate. You should also include provisions for overtime hours, which will be paid at 1.5 times the regular hourly rate. Hint: set the default overtime hours to 0. You will need similar functions as the salaried employee to set and get variables, as well as to calculate salary. Name your hourly employee class as HourlyEmployee.
Include all of the class definitions and the test code in one ruby file. Your test code must instantiate at least two of each type of worker to test the classes for proper operation. I recommend a full-time and half-time salaried worker and two hourly worker test cases, one of which earns overtime.
Execute your test program and copy the outputs to a text file to demonstrate proper execution. What to submit: Please submit a copy of your ruby source code file and a text file(s) that includes execution output that demonstrates proper operation.
EMPLOYEE.H
Explanation / Answer
class Employee
def initialize(id, last, first, initial, dept)
@myIdNum = id;
@myLastName = last;
@myFirstName = first;
@myMiddleInitial = initial;
@myDeptCode = dept;
end
def getIdNum
@myIdNum
end
def setIdNum(id)
@myIdNum = id;
end
def getLastName
@myLastName
end
def setLastName(last)
@myLastName = last;
end
def getFirstName
@myFirstName
end
def setFirstName(first)
@myFirstName = first;
end
def getMiddleInitial
@myMiddleInitial
end
def setMiddleInitial(middle)
@myMiddleInitial = middle;
end
def getDeptCode
@myDeptCode
end
def getDeptCode(deptCode)
@myDeptCode = deptCode;
end
def print_employee
puts "(EmpID No=#@myIdNum, LastName=#@myLastName, FirstName=#@myFirstName, MiddleInitial=#@myMiddleInitial, DeptCode=#@myDeptCode)";
end
end
class SalariedEmployee < Employee
def initialize(id, last, first, initial, dept, salary)
super(id, last, first, initial, dept);
@monthlySalary = salary;
@workFraction = 1;
end
def getWorkFraction
@workFraction;
end
def setWorkFraction(workFraction)
@workFraction = workFraction;
end
def calculateSalary()
@workFraction * @monthlySalary;
end
def print_employee
@netSalary = calculateSalary;
puts "(EmpID No=#@myIdNum, LastName=#@myLastName, FirstName=#@myFirstName, MiddleInitial=#@myMiddleInitial, DeptCode=#@myDeptCode, Net Salary=#@netSalary)";
end
end
class HourlyEmployee < Employee
def initialize(id, last, first, initial, dept, hours, rate)
super(id, last, first, initial, dept);
@regularHours = hours;
@hourlyRate = rate;
@overTimeHours = 0;
end
def getRegularHours
@regularHours;
end
def setRegularHours(hours)
@regularHours = hours;
end
def getOverTimeHours
@overTimeHours;
end
def setOverTimeHours(hours)
@overTimeHours = hours;
end
def getHourlyRate
@hourlyRate;
end
def setHourlyRate(rate)
@hourlyRate = rate;
end
def calculateSalary()
(@regularHours * @hourlyRate) + (@overTimeHours * @hourlyRate * 1.5);
end
def print_employee
@netSalary = calculateSalary;
puts "(EmpID No=#@myIdNum, LastName=#@myLastName, FirstName=#@myFirstName, MiddleInitial=#@myMiddleInitial, DeptCode=#@myDeptCode, Net Salary=#@netSalary)";
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.