0% found this document useful (0 votes)
9 views10 pages

Lab-03 - OOP (Classes and Objects) Classwork

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Lab-03 - OOP (Classes and Objects) Classwork

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course Code: CSE111

Course Title: Programming Language II

Classwork No: 03

Topic: OOP(Classes and objects)

Number of tasks: 5
Task 1
You have been hired by a bank to develop cutting-edge banking software. As part of your task,
(i) Design a class called BankAccount that represents a bank account that produces the expected
output.

# Driver code
account1 = BankAccount("Bilbo", "Savings")
print("=====================================")
print(f"User Name: {account1.user_name}")
print(f"Balance: {[Link]}")
print(f"Account Type:", account1.account_type)
print("=====================================")
account2 = BankAccount("Frodo", "Business")
print(f"User Name: {account2.user_name}")
print(f"Balance: {[Link]}")
print(f"Account Type: {account2.account_type}")
print("=====================================")

Output:
=====================================
User Name: Bilbo
Balance: 1.0
Account Type: Savings
=====================================
User Name: Frodo
Balance: 1.0
Account Type: Business
=====================================

(ii) Now change the balance of account1 to 15.75 taka and account2 to 700.50 taka. Finally, print
the updated details with their respective user names.

Output:
New account balance of Bilbo is 15.75
New account balance of Frodo is 700.5
Task 2
You are the proud owner of a mangoficent mango orchard where you have different varieties of
mango trees such as Fazlee, Langda, Harivanga, Himsagar, etc. But this year, demand for
Gopalbhog and Amrapali is too high. To cater to this unsatisfied demand in the future, you have
decided to plant these two varieties in your orchard.

(i) Now, based on the given driver code, you need to design a MangoTree class.
Initially, when you plant a tree, it will have a height of 1 meter and the number of mangoes will
be 0.

# Driver code
mangoTree1= MangoTree("Gopalbhog")
# Display the details of the mango tree
print("=====================================")
print("Mango Tree Details:")
print(f"Variety: {[Link]}")
print(f"Height: {[Link]} meter(s)")
print(f"Number of mangoes on the tree: {mangoTree1.number_of_mangoes}")
print("=====================================")
mangoTree2= MangoTree("Amrapali")
# Display the details of the mango tree
print("Mango Tree Details:")
print(f"Variety: {[Link]}")
print(f"Height: {[Link]} meter(s)")
print(f"Number of mangoes on the tree: {mangoTree2.number_of_mangoes}")
print("=====================================")

Output:
=====================================
Mango Tree Details:
Variety: Gopalbhog
Height: 1 meter(s)
Number of mangoes on the tree: 0
=====================================
Mango Tree Details:
Variety: Amrapali
Height: 1 meter(s)
Number of mangoes on the tree: 0
=====================================
(ii) Suppose 5 years have passed and these small trees have grown larger and started to bear fruit.
● A mango tree roughly grows 3 meters in one year.
● Amrapali bears 15 mangoes per meter and Gopalbhog bears 10 mangoes per meter.
Now, update the height and number_of_mangoes of both mangoTree1 and mangoTree2,
respectively, in the driver code using the formula mentioned above. And print the updated
details.

Output:
Updated details after 5 years:
=====================================
Variety: Gopalbhog
Height: 16 meter(s)
Number of mangoes on the tree: 160
=====================================
Variety: Amrapali
Height: 16 meter(s)
Number of mangoes on the tree: 240
=====================================

Task 3
Design the class “Contacts” so that the code produces the expected output. You are not allowed
to change the given driver code below.

# Driver code
names = ["Emergency", "Father", "Bestie"]
numbers = ["999", "01xx23", "01xx87", "01xx65", "01xx43"]

m1 = Contacts(names, numbers)
print("Saved Contacts:", [Link])
print("---------------------------------------------")

[Link]("Mother")
[Link]()

m2 = Contacts(names, numbers)
print("Saved Contacts:", [Link])
Output:
Contacts cannot be saved. Length Mismatch!
Saved Contacts: {}
---------------------------------------------
Contacts saved successfully.
Saved Contacts: {'Emergency': '999', 'Father': '01xx23', 'Bestie': '01xx87', 'Mother': '01xx65'}

Subtasks:
Suppose, after running the above code, you again write the following line:
m1 = Contacts(names, numbers)
1. What will happen after executing this line?
2. Since m1 was already created in the 3rd line of the driver code, will the newly created m1
have the same reference now? Explain by writing/modifying a few lines of code.
[Hint: Store the previously created m1 in a temporary variable and compare it with the
new one.]

Task 4
Design the class “Student” so that the code produces the expected output. You are not allowed
to change the given driver code below.
[Hint:
● If a student's CGPA is less than 2.00 then s/he is under probation. Probation students can
not take less than 1 course and more than 2 courses; otherwise, his/her advising request
will be denied.
● If a student's CGPA is greater than or equal to 2.00, the student will fall under the regular
category. Regular students must take at least 3 courses and at most 5 courses, or else
his/her advising request will be denied.
● If a student’s advising request is denied, that means his/her currently taken courses are
not finalized. So, the value should be set to 0.]

# Driver code
s1 = Student("Clark", 3.45, 4)
print(f"Name: {[Link]}\nCGPA: {[Link]}\nCourses Taken: {s1.courses_taken}")
print(f"Student Status: {s1.student_status}\nAdvising Status: {s1.advising_status}")
print("--------------------------------------------------------------------------------")
s2 = Student("Barry", 1.93, 2)
print(f"Name: {[Link]}")
print(f"Student Status: {s2.student_status}\nAdvising Status: {s2.advising_status}")
print("--------------------------------------------------------------------------------")
s3 = Student("Diana", 2.91, 2)
print(f"Advising Status: {s3.advising_status}\nCourses Taken: {s3.courses_taken}")
print("--------------------------------------------------------------------------------")
s4 = Student("Bruce", 1.52, 5)
print(f"Advising Status: {s4.advising_status}\nCourses Taken: {s4.courses_taken}")

Output:
All the best, Clark, for the upcoming semester.
Name: Clark
CGPA: 3.45
Courses Taken: 4
Student Status: Regular
Advising Status: Approved
--------------------------------------------------------------------------------
Study hard this time, Barry.
Name: Barry
Student Status: Probation
Advising Status: Approved
--------------------------------------------------------------------------------
Hello Diana, You are a regular student and have to take between 3 to 5 courses.
Advising Status: Denied
Courses Taken: 0
--------------------------------------------------------------------------------
Sorry, Bruce, you are on probation and cannot take more than 2 courses.
Advising Status: Denied
Courses Taken: 0

Task 5:
Suppose you are hired by the authority of a library that only keeps antique books. They want you
to build a management system to keep track of the books. They have given an instruction
manual:
1. Only books that are antique enough are kept in the library. [hint: analyze the driver code and
the output to find the minimum age of a book to be considered antique.]
2. Library has 3 floors. Floor 0,1 and 2. The comparatively antique books are stored on the
higher floors. Books that were published less than 200 years ago are stored on floor 0, published
at least 200 years ago are stored on floor 1, and published at least 400 years ago are stored on
floor 2.
Now, design a class called ‘Book’ using a parameterized constructor so that after executing the
driver code, the desired result shown in the output section will be printed.
Hint: You can use the following lines of code to find out the current year.

import datetime #Import this module at the top of your code.


today = [Link]() #This will give you today’s date.
year = [Link] #Extracting the year from today’s date.

#Driver Code
book1= Book('The Act', 'Ferguson', 1924)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book1.year_of_publication}.")
print(f"This book is {[Link]}")
print("-------------------------------------")
book2= Book('Flame', 'Nolan', 1932)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book2.year_of_publication}.")
print(f"This book is {[Link]}")
print("-------------------------------------")
book3= Book('Norms', 'Alfred', 1832)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book3.year_of_publication}.")
print(f"This book is {[Link]}")
print("-------------------------------------")
book4= Book('Apex', 'Samson', 1923)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book4.year_of_publication}.")
print(f"This book is {[Link]}")
print("-------------------------------------")
book5= Book('Habitat', 'Eden', 1723)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book5.year_of_publication}.")
print(f"This book is {[Link]}")
print("-------------------------------------")
book6= Book('Apocalypto', 'Menez', 1603)
print(f"{[Link]} wrote the book '{[Link]}'.")
print(f"This book was published in {book6.year_of_publication}.")
print(f"This book is {[Link]}")

Output:
Checking the book.
Ferguson wrote the book 'The Act'.
This book was published in 1924.
This book is Rejected. The book is not antique enough.
-------------------------------------
Checking the book.
Nolan wrote the book 'Flame'.
This book was published in 1932.
This book is Rejected. The book is not antique enough.
-------------------------------------
Checking the book.
Alfred wrote the book 'Norms'.
This book was published in 1832.
This book is Accepted. The book is stored on floor: 0.
-------------------------------------
Checking the book.
Samson wrote the book 'Apex'.
This book was published in 1923.
This book is Accepted. The book is stored on floor: 0.
-------------------------------------
Checking the book.
Eden wrote the book 'Habitat'.
This book was published in 1723.
This book is Accepted. The book is stored on floor: 1.
-------------------------------------
Checking the book.
Menez wrote the book 'Apocalypto'.
This book was published in 1603.
This book is Accepted. The book is stored on floor: 2.
TASK 6

Write the output of the following code:

1 class Human: Output


2 def __init__(self):
3 [Link] = 0
4 [Link] = 0.0
5
6 h1 = Human()
7 h2 = Human()
8 [Link] = 21
9 [Link] = 5.5
10 print([Link])
11 print([Link])
12 [Link] = [Link] - 3
13 print([Link])
14 [Link] = [Link]
15 [Link] += [Link]
16 print([Link])
17 h2 = h1
18 print([Link])
19 print([Link])
20 [Link] += [Link]
21 [Link] += [Link]
22 print([Link])
23 print([Link])
24 [Link] += [Link]
25 [Link] = [Link]
26 print([Link])
TASK 7
1 class Student: Output
2 def __init__(self):
3 [Link] = None
4 [Link] = 0.0
5 s1 = Student()
6 s2 = Student()
7 s3 = None
8 [Link] = "Student One"
9 [Link] = 2.3
10 s3 = s1
11 [Link] = "Student Two"
12 [Link] = [Link] + 1
13 [Link] = "New Student"
14 print([Link])
15 print([Link])
16 print([Link])
17 print([Link])
18 print([Link])
19 print([Link])
20 s3 = s2
21 [Link] = "old student"
22 [Link] = "older student"
23 [Link] = "oldest student"
24 [Link] = [Link] - [Link] + 4.5
25 print([Link])
26 print([Link])
27 print([Link])
28 print([Link])
29 print([Link])
30 print([Link])

You might also like