Lab 13
Lab 13
Lab Objectives:
1. Understand and demonstrate compile-time and runtime polymorphism through method
overloading and overriding examples.
2. Explain the purpose and use of abstract classes in object-oriented programming.
3. Learn how inheritance promotes code reusability by inheriting properties and behaviors
from a superclass.
4. Reflect on and clarify the differences between key OOP concepts.
5. Apply theoretical knowledge by using small code snippets to demonstrate concepts.
Getting Started
1. First you each need to have hands on with each problem given. The goal is to learn
together, not just to finish quickly.
2. You will work in pairs. Or a group of 3 if there are an odd number of students.
3. Create a google document or other shared document with the title “Lab13 -
Name1, Name2. Both partners must submit the same document.
4. Follow instructions in this lab document to populate the shared document.
You will submit this document for lab credit.
5. Stuck? Ask your partner, and if you are both stuck, ask for help!
Within the document, whenever you see a prompt, it should look bolded like this:
Example:
At this step, we tried to run the code, and it didn’t work.
If you are expected to submit screenshots, put those at the end of your report.
Offline questions (25 marks):
These following conceptual questions are designed to help you deepen your understanding of
core object-oriented programming concepts such as polymorphism, inheritance, and abstract
classes. Understanding these concepts is important for writing efficient, maintainable code, and
will help you approach complex programming problems. Provide examples that have not
been used in class already. You should be able to provide small code snippets for your
examples to help you explain.
3. Explain the concept of an abstract class, including the types of methods it can have.
Why would you use an abstract class? Provide an example of a situation where an
abstract class would be more appropriate?
4. Describe how inheritance allows one class to inherit the properties and behaviors of
another class. How does inheritance promote code reusability? Provide an example of a
class hierarchy where a subclass inherits fields and methods from a superclass and
explain the key benefit of using inheritance in that scenario.
In this lab, you will be creating an Employee Management System that includes different types of
employees: Full-Time Employees and Part-Time Employees. Your task is to design and
implement an object-oriented system with abstract classes and inheritance to manage employee
details and calculate wages.
Instructions:
1. Create the Abstract Class Employee:
o The Employee class should be an abstract class with the following fields:
§ name (String)
§ employeeID (int)
§ department (String)
o Implement the constructor to initialize these fields.
o The Employee class should have the following abstract methods:
§ calculateAnnualWage(): Method to calculate and return the annual wage.
§ calculateMonthlyWage(): Method to calculate and return the monthly
wage.
§ calculateWorkload(): Method to calculate and return the workload (e.g.,
number of hours worked per month).
§ toString(): Method to return a string representation of the employee details.
2. Create the FullTimeEmployee Class:
o The FullTimeEmployee class extends the Employee class.
o Add a field monthlySalary (double) to store the monthly salary.
o Implement the constructor to initialize these fields.
o Implement the calculateAnnualWage(), calculateMonthlyWage(), and
calculateWorkload() methods:
§ The annual wage is the monthly salary multiplied by 12.
§ The monthly wage is the monthlySalary itself as entered by user.
§ The workload is fixed at 160 hours per month, this can be declared as a final
class field.
o Implement the toString() method to return the employee’s details, including their
monthly salary, annual salary, and workload.
3. Create the PartTimeEmployee Class:
o The PartTimeEmployee class also extends the Employee class.
o Add fields hourlyWage (double) and hoursWorkedPerDay (int) to store the hourly
wage and hours worked per day.
o Implement the constructor to initialize these fields.
o Implement the calculateAnnualWage(), calculateMonthlyWage(), and
calculateWorkload() methods:
§ The annual wage is calculated by multiplying the hourly wage by the
number of hours worked per month (calculated as hours worked per day × 5
days per week × 4 weeks per month × 12 months).
§ The monthly wage is calculated as the hourly wage multiplied by the total
hours worked per month.
§ The workload is the total number of hours worked per month.
o Implement the toString() method to return the employee’s details, including their
hourly wage, monthly salary, annual salary, and workload.
4. Create a EmployeeDemo Class to Test the System:
o This class should allow the user to enter Full-Time and Part-Time as type of
employee
o Use a Scanner object to prompt the user for the following details:
§ For Full-Time Employees:
§ Name, Employee ID, Department, Monthly Salary
§ For Part-Time Employees:
§ Name, Employee ID, Department, Hourly Wage, Hours Worked Per
Day
o Store each employee object in an Arraylist and display their details at the end.
o Provide an option to exit the input loop.
5. Additional Considerations:
o For this task, you do not need to handle errors or perform input validation in
your code. This means that we are assuming all inputs provided by the user
will be valid (e.g., correct data types and no missing fields).
o After receiving input for numeric values, use nextLine() to clear any leftover
newline characters in the input buffer to avoid issues with subsequent nextLine()
calls.
Deliverables:
• Employee.java: Contains the abstract Employee class with abstract methods.
• FullTimeEmployee.java: Contains the full-time employee implementation.
• PartTimeEmployee.java: Contains the part-time employee implementation.
• EmployeeDemo.java: The main class where the program runs, prompting for user input
and displaying employee details.
Sample Output:
Reflective Question (5 marks):
How might your design need to change if the company’s requirements shifted and
employees had to be able to update their own details, such as changing their department or
salary? What additional methods or structures would you need to add to handle this?
2. Subclasses:
- Create subclasses such as MainCourse, SideDish, and Dessert.
- Each subclass should:
- Override the serve() method to provide unique behavior (e.g., serving size, preparation notes,
or presentation style).
- Include any additional fields or methods specific to the type of dish as you wish.
Additional Notes:
• Feel free to include any additional details or creative functionality to make your
Thanksgiving Feast unique
• Use Thanksgiving-themed or add your favorite Thanksgiving dishes or invent creative
ones!
Reflect on how your program captures the spirit of a Thanksgiving meal. Did you choose specific
dishes or add playful details that make the program feel festive? Did playing around with this
program help you understand the concepts of polymorphism and abstraction better?
Before you Leave:
At the end of the report, put the following:
1. Submit your work as yourname_lab13.zip
Check in with you Lab Instructor before you leave