0% found this document useful (0 votes)
6 views

CS115_Lab07

The document outlines a lab guide for CS 115 focusing on classes and inheritance in Python. It details the creation of an Employee class with specific data members and methods, including a subclass for Manager with additional functionalities. The guide also includes instructions for a script to read employee data from a file, manage duplicates, sort employees, and calculate salaries.

Uploaded by

mirzutlawlieya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS115_Lab07

The document outlines a lab guide for CS 115 focusing on classes and inheritance in Python. It details the creation of an Employee class with specific data members and methods, including a subclass for Manager with additional functionalities. The guide also includes instructions for a script to read employee data from a file, manage duplicates, sort employees, and calculate salaries.

Uploaded by

mirzutlawlieya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 115 - Introduction to Programming in Python

Lab Guide 07
Lab Objectives: Classes and Inheritance
a) Create a class, Employee, with the following data members and methods. Note all data
members and class variables should be private (__).

Data Members:
● tax_rate: class variable, same for all Employees, which is set to 30%.
● emp_name: string name of the Employee.
● id_num: identification number of the Employee (int).
● wage: the wage of the Employee (float).

Methods:
● __init__():
○ takes an employee name, id number and wage, and using the set
methods, sets the attributes to the given values.
● Get methods for all data members.
● Set methods for all data members implemented according to the following:
○ Employee name must consist of only alphabetic or space characters. If
there is any other character, such invalid characters should be removed.
See the sample run for an example.
○ Id number should be converted to an integer value. If the id number
cannot be converted to an integer, set to the default, 11111111.
○ Wage must be a positive value. If the value is negative, set the wage to
zero.
● calculate_salary(): returns the salary, which is the wage minus the tax
(using tax_rate value)
● __lt__(): an Employee is less than another if their surname is less. If the
surnames are equal, compare the alphabetic first names. Note: You may
assume that all Employees have at least one name, and one surname. The
surname will always be after the final space in the name.
● __eq__() : Employee objects are equal if their id numbers are equal.
● __repr__(): returns a string representation of an Employee object. See the
sample run for formatting.

b) Create a subclass of Employee, Manager, with the following data members and
methods. Note all data members should be private (__).

Data Members:
● bonus: the bonus of the Manager (float).
Methods:
● __init()__: takes the employee name, id number, wage, and bonus.
Calls the Employee init, to initialize the inherited data members, and sets
the bonus to the value passed as a parameter.
● Get method for bonus.
● calculate_salary(): returns the salary of the manager. Managers
make 10% extra salary in addition to the bonus than a regular employee.
They also pay tax at the same rate.
● __repr()__: returns a string representation of a Manager object. The
method should call the Employee __repr__ to get the Employee data, and
append the Manager data, formatted as shown in the sample run.

c) Write a script Lab07.py with the following functions:


● read_employees(): takes a string filename and reads the file with the given name,
and returns a list of Employes created using data from the file. If there is an
employee with the same id in the list of employees, new employee should not be
added. See the sample run for an example duplicate employee id.

● The script should do the following:


o Get a list of Employees from the file employees.txt, using the function
defined above.
o Sort the list of Employees.
o Display the sorted list of Employees.
o Display the calculated salary of all Employees.
o Display the average calculated salary for Managers.

Sample Run:
Duplicate employee id:
Name: Kemal Oran Employee ID: 87261 Wage: 200000.0
not added
Duplicate employee id:
Name: Veli Sucu Employee ID: 72314 Wage: 350000.0
not added

Sorted List:
[
Name: Ali Aksu Employee ID: 87134 Wage: 280000.0,
Name: Canan Aksu Employee ID: 11111111 Wage: 250000.0,
Name: Hakan Arslan Employee ID: 918236 Wage: 300000.0 Bonus: 600000.0,
Name: Ekrem Bal Employee ID: 87261 Wage: 300000.0,
Name: Zehra Esra Zengin Employee ID: 72314 Wage: 350000.0,
Name: Zeynep Eda Zengin Employee ID: 81214 Wage: 250000.0 Bonus: 500000.0]

Salary of all employees:


Ali Aksu salary after tax: 196000.0
Canan Aksu salary after tax: 175000.0
Hakan Arslan salary after tax: 651000.0
Ekrem Bal salary after tax: 210000.0
Zehra Esra Zengin salary after tax: 245000.0
Zeynep Eda Zengin salary after tax: 542500.0

Average Salary for Managers: 596750.0

You might also like