Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

'''' Accessor method return a value from a class's

attribute without changing it '''

'''employee ID, employee name, employee worked hours and employee pay rate,'''

'''You need to create

accessors methods for employee ID, employee name, employee worked hours and

employee pay rate'''

'''b) Create a method get_total_pay that calculates and returns the total pay of an employee.

Total pay = worked hours x pay rate'''

'''Create an __str__ method representing an employee data in this format: Name: Thair

Dalain, Worked hours: 30, Pay rate: $100, Total pay: $300'''

class Employee:

def __init__(self, ID, name, worked_hours, pay_rate):

self.ID=ID

self.name=name

self.worked_hours=worked_hours

self.pay_rate=pay_rate

def get_ID(self):

return self.ID

def get_name(self):
return self.name

def get_worked_hours(self):

return self.worked_hours

def get_pay_rate(self):

return self.pay_rate

def get_total_pay(self):

return self.worked_hours*self.pay_rate

def __str__(self):

return 'name:'+self.name+', worked hours:'+str(self.worked_hours)+' , pay rate:


$'+str(self.pay_rate)+', total pay:'+str(self.get_total_pay())

'''d) In your main() function, instantiate the employee class for two different employees

with some dummy data and print out those instances using print() calls '''

def main():

E1=Employee(1, 'sahil thapa', 20, 30)

E2=Employee(2, 'param', 25, 40)

print(E1)

print(E2)

main()
Q2

''' Book objects

a) Create a Book class that holds these attributes: ISBN, title, author, publish year, book

status. Since books can have a different status, the book status should be a list of strings,

for example ['Available', 'Reserved', 'Borrowed', 'Damaged'].

b) Create accessor methods for all attributes and create a mutator to update the book title.
c) Create another program and import the Book class to it. This program creates one or

more book objects with any sample data and then prints out those instances using

accessor methods.

'''

'''ISBN, title, author, publish year, bookstatus.'''

'''book status vlues: 'Available', 'Reserved', 'Borrowed', 'Damaged' '''

class book:

def __init__(self, ISBN, title, author, year, status):

self.ISBN = ISBN

self.title = title

self.author =author

self.year = year

self.status = status

def get__ISBN(self):

return self.ISBN

def get__tittle(self):

return self.tittle

def get__author(self):

return self.author

def get__year(self):

return self.year

def get__status(self):

return self.status
def set_title(self,T2):

self.title = T2

B1 = Book (23567, "My Time", "SAHIL", 2017, "Reserved")

B2 = Book (23588, "GOLDEN", "AHIRA", 2015, "Damaged")

B2.set_title("java")

print (B2.tittle)

You might also like