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

CSE111 Lab Assignment 4

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

CSE111 Lab Assignment 4

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

Course Title: Programming Language II

Course Code: CSE 111


Lab Assignment no: 4
Question 1
Suppose your little sibling wants your help to check his math homework. He is done with
his homework but wants you to see if all his results are correct. Since the student with
all correct results gets 3 stars. However, you want your brother to check this on his own.
So, you design a calculator for him in python. You could have given your scientific
calculator but you wanted to give him a basic calculator and also wanted to see if you
can even design one.

Subtasks:

1. Create a class called Calculator.


2. Your class shall have 1 constructor and 4 methods, namely add, subtract,
multiply and divide.
3. Now, create an object of your class. After creating an object, it should print “Let’s
Calculate!”
4. Then take 3 inputs from the user: first value, operator, second value
5. Now based on the given operator, call the required method and print the result.

Sample Input:
1
+
2
Sample Output:
Let’s Calculate!
Value 1: 1
Operator: +
Value 2: 2
Result: 3
Question 2

Write a class called Customer with the required constructor and methods to get the
following output.

Subtasks:
1. Create a class called Customer.
2. Create the required constructor.
3. Create a method called greet that works if no arguments are passed or if one
argument is passed. (Hint: You may need to use the keyword NONE)
4. Create a method called purchase that can take as many arguments as the user
wants to give.

[You are not allowed to change the code below]

# Write your codes for subtasks 1-4 here. OUTPUT:


Hello!
customer_1 = Customer("Sam") Sam, you purchased 3 item(s):
customer_1.greet() chips
customer_1.purchase("chips", "chocolate", "orange juice") chocolate
print("-----------------------------") orange juice
customer_2 = Customer("David") -----------------------------
customer_2.greet("David") Hello David!
customer_2.purchase("orange juice") David, you purchased 1 item(s):
orange juice
Question 3

The Giant Panda Protection and Research Center in the Sichuan province of southwest
China, actually employs a category of workers known as panda nannies. The primary
responsibility is to play with adorable panda cubs and name them, determine gender,
keep track of their age and hours they sleep. So being a programmer panda nanny, you
will create a code that will do all these works for you.

1. Create a class named Panda and also write the constructor.


2. Access the instance attributes and print them in the given format.
3. Call instance methods to keep track of their daily hours of sleep.
4. Suppose consulting with other panda nannies you have set some criteria based
on which you will make their diet plans. The criteria are:
** Mixed Veggies for pandas having 3 to 5 hours (included) of sleep daily.
** Eggplant & Tofu for pandas having 6 to 8 hours (included) of sleep daily.
** Broccoli Chicken for pandas having 9 to 11 hours (included) of sleep daily.
** Lastly if no arguments are passed then just give it bamboo leaves.
Now handle this problem modifying the method designed to keep track of their daily
hours of sleep and determine diet plan using method overloading.

[You are not allowed to change the code below]

#Write your code here for subtasks 1-4. OUTPUT:


Kunfu is a Male Panda Bear who is 5 years
panda1 = Panda("Kunfu","Male", 5)
old
panda2=Panda("Pan Pan","Female",3)
Pan Pan is a Female Panda Bear who is 3
panda3=Panda("Ming Ming","Female",8)
years old
Ming Ming is a Female Panda Bear who is 8
print("{} is a {} Panda Bear who is {} years
years old
old".format(panda1.name,panda1.gender,panda1.age))
===========================
Pan Pan sleeps 10 hours daily and should
print("{} is a {} Panda Bear who is {} years
have Broccoli Chicken
old".format(panda2.name,panda2.gender,panda2.age))
Kunfu sleeps 4 hours daily and should have
Mixed Veggies
print("{} is a {} Panda Bear who is {} years
Ming Ming's duration is unknown thus should
old".format(panda3.name,panda3.gender,panda3.age))
have only bamboo leaves
print("===========================")
print(panda2.sleep(10))
print(panda1.sleep(4))
print(panda3.sleep())
Question 4

Analyze the given code below to write Cat class to get the output as shown.
Hints:
• Remember, the constructor is a special method. Here, you have to deal with
constructor overloading which is similar to method overloading.
• You may need to use the keyword None
• Your class should have 2 variables
[You are not allowed to change the code below]

#Write your code here OUTPUT


White cat is sitting
c1 = Cat() Black cat is sitting
c2 = Cat("Black") Brown cat is jumping
c3 = Cat("Brown", "jumping") Red cat is purring
c4 = Cat("Red", "purring") Blue cat is sitting
c1.printCat() Purple cat is jumping
c2.printCat()
c3.printCat()
c4.printCat()
c1.changeColor("Blue")
c3.changeColor("Purple")
c1.printCat()
c3.printCat()

Question 5
Implement the design of the Student class so that the following output is produced:

Driver Code Output

# Write your code here --------------------------------


s1 = Student() Hello default student
s1.quizcalc(10) Your average quiz score is 3.3333333333333335
print('--------------------------------') --------------------------------
s1.printdetail() Hello Harry
s2 = Student('Harry') Your average quiz score is 6.0
s2.quizcalc(10,8) --------------------------------
print('--------------------------------') Hello Hermione
s2.printdetail() Your average quiz score is 9.666666666666666
s3 = Student('Hermione')
s3.quizcalc(10,9,10)
print('--------------------------------')
s3.printdetail()
Question 6
Design a “Vehicle” class. A vehicle assumes that the whole world is a 2-dimensional
graph paper. It maintains its x and y coordinates (both are integers). Any new object
created of the Vehicle class will always start at the coordinates (0,0).

It must have methods to move up, down, left, right and a print_position() method for
printing the current coordinate.

Note: All moves are 1 step. That means a single call to any move method changes the
value of either x or y or both by 1.

[You are not allowed to change the code below]

# Write your class here OUTPUT


(0,0)
car = Vehicle() (0,1)
car.print_position() (-1,1)
car.moveUp() (-1,0)
car.print_position()
car.moveLeft()
car.print_position()
car.moveDown()
car.print_position()
car.moveRight()
Question 7

Design the Programmer class such a way so that the following code provides the
expected output.

Hint:
o Write the constructor with appropriate printing and multiple arguments.
o Write the addExp() method with appropriate printing and argument.
o Write the prinDetails() method

[You are not allowed to change the code below]

# Write your code here. OUTPUT:


p1 = Programmer("Ethen Hunt", "Java", 10) Horray! A new programmer is born
p1.printDetails() Name: Ethen Hunt
print('--------------------------') Language: Java
p2 = Programmer("James Bond", "C++", 7) Experience: 10 years.
p2.printDetails() --------------------------
print('--------------------------') Horray! A new programmer is born
p3 = Programmer("Jon Snow", "Python", 4) Name: James Bond
p3.printDetails() Language: C++
p3.addExp(5) Experience: 7 years.
p3.printDetails() --------------------------
Horray! A new programmer is born
Name: Jon Snow
Language: Python
Experience: 4 years.
Updating experience of Jon Snow
Name: Jon Snow
Language: Python
Experience: 9 years.
Question 8

Design the Student class such a way so that the following code provides the expected
output.
Hint:
• Write the constructor with appropriate default value for arguments.
• Write the dailyEffort() method with appropriate argument.
• Write the prinDetails() method. For printing suggestions check the following
instructions.
➢ If hour <= 2 print 'Suggestion: Should give more effort!'
➢ If hour <= 4 print 'Suggestion: Keep up the good work!'
➢ Else print 'Suggestion: Excellent! Now motivate others.'

[You are not allowed to change the code below]

# Write your code here. OUTPUT:


harry = Student('Harry Potter', 123) Name: Harry Potter
harry.dailyEffort(3) ID: 123
harry.printDetails() Department: CSE
print('========================') Daily Effort: 3 hour(s)
john = Student("John Wick", 456, "BBA") Suggestion: Keep up the good work!
john.dailyEffort(2) ========================
john.printDetails() Name: John Wick
print('========================') ID: 456
naruto = Student("Naruto Uzumaki", 777, "Ninja") Department: BBA
naruto.dailyEffort(6) Daily Effort: 2 hour(s)
naruto.printDetails() Suggestion: Should give more effort!
========================
Name: Naruto Uzumaki
ID: 777
Department: Ninja
Daily Effort: 6 hour(s)
Suggestion: Excellent! Now motivate others.
Question 9

Implement the design of the Patient class so that the following output is produced:

[You are not allowed to change the code below]

# Write your code here. OUTPUT:


p1 = Patient(“Thomas”, 23) =========================
p1.add_Symptom(“Headache”) Name: Thomas
p2 = Patient(“Carol”, 20) Age: 23
p2.add_Symptom(“Vomiting”, “Coughing”) Symptoms: Headache
p3 = Patient(“Mike”, 25) =========================
p3.add_Symptom(“Fever”, “Headache”, “Coughing”) Name: Carol
print("=========================") Age: 20
p1.printPatientDetail() Symptoms: Vomiting, Coughing
print("=========================") =========================
p2.printPatientDetail() Name: Mike
print("=========================") Age: 25
p3.printPatientDetail() Symptoms: Fever, Headache, Coughing
print("=========================") =========================
Question 10

Implement the design of the Avengers class so that the following output is produced:

[You are not allowed to change the code below]

# Write your code here. OUTPUT:


a1 = Avengers('Captain America', 'Bucky Barnes') =========================
a1.super_powers('Stamina', 'Slowed ageing') Name: Captain America
a2 = Avengers('Doctor Strange', 'Ancient One') Partner: Bucky Barnes
a2.super_powers('Mastery of magic') Super powers: Stamina, Slowed ageing
a3 = Avengers('Iron Man', 'War Machine') =========================
a3.super_powers('Genius level intellect', 'Scientist ') Name: Doctor Strange
print("=========================") Partner: Ancient One
a1.printAvengersDetail() Super powers: Mastery of magic
print("=========================") =========================
a2.printAvengersDetail() Name: Iron Man
print("=========================") Partner: War Machine
a3.printAvengersDetail() Super powers: Genius level intellect,
print("=========================") Scientist
=========================
Question 11

Design the Shinobi class such a way so that the following code provides the expected
output.
Hint:
• Write the constructor with appropriate default value for arguments. Set the initial
salary and mission to 0.
• Write the changeRank() method with appropriate argument.
• Write the calSalary() method with appropriate argument. Check the following
suggestions
➢ Update the number of mission from the given argument.
➢ If rank == 'Genin' then salary = #mission * 50
➢ If rank == 'Chunin' then salary = #mission * 100
➢ else salary = #mission * 500
• Write the printInfo() method with appropriate printing.

[You are not allowed to change the code below]


# Write your code here. OUTPUT:
naruto = Shinobi("Naruto", "Genin") Name: Naruto
naruto.calSalary(5) Rank: Genin
naruto.printInfo() Number of mission: 5
print('====================') Salary: 250
shikamaru = Shinobi('Shikamaru', "Genin") ====================
shikamaru.printInfo() Name: Shikamaru
shikamaru.changeRank("Chunin") Rank: Genin
shikamaru.calSalary(10) Number of mission: 0
shikamaru.printInfo() Salary: 0
print('====================') Name: Shikamaru
neiji = Shinobi("Neiji", "Jonin") Rank: Chunin
neiji.calSalary(5) Number of mission: 10
neiji.printInfo() Salary: 1000
====================
Name: Neiji
Rank: Jonin
Number of mission: 5
Salary: 2500
Question 12

Design the ParcelKoro class such a way so that the following code provides the expected
output.

Hint: total_fee = (total_weight * 20) + location_charge.

Note: For the method calculate fee: if the delivery location is not given, the
location_charge will be 50 taka or else 100 taka. Also, while calculating total fee, if the
product weight is 0 the total_fee should also be 0.

Assume only these 3 ways you can create an object of a class.

[You are not allowed to change the code below]

# Write your code here. OUTPUT:


print("**********************") **********************
p1 = ParcelKoro() Customer Name: No name set
p1.calculateFee() Product Weight: 0
Total fee: 0
p1.printDetails()
**********************
print("**********************")
Customer Name: Bob The Builder
p2 = ParcelKoro('Bob The Builder') Product Weight: 0
p2.calculateFee() Total fee: 0
p2.printDetails() ----------------------------
print("----------------------------") Customer Name: Bob The Builder
Product Weight: 15
p2.product_weight = 15
Total fee: 350
p2.calculateFee()
**********************
p2.printDetails( Customer Name: Dora The Explorer
print("**********************") Product Weight: 10
p3 = ParcelKoro('Dora The Explorer', 10) Total fee: 300
p3.calculateFee('Dhanmondi')
p3.printDetails()
Question 13

Implement the design of the Batsman class so that the following output is produced:

Hint: Batting strike rate (s/r) = runsScored / ballsFaced x 100.

Driver Code Output

# Write your code here Name: New Batsman


b1 = Batsman(6101, 7380) Runs Scored: 6101 , Balls Faced: 7380
b1.printCareerStatistics() ============================
print("============================") Name: Liton Das
b2 = Batsman("Liton Das", 678, 773) Runs Scored: 678 , Balls Faced: 773
b2.printCareerStatistics() ----------------------------
print("----------------------------") 87.71021992238033
print(b2.battingStrikeRate()) ============================
print("============================") Name: Shakib Al Hasan
b1.setName("Shakib Al Hasan") Runs Scored: 6101 , Balls Faced: 7380
b1.printCareerStatistics() ----------------------------
print("----------------------------") 82.66937669376694
print(b1.battingStrikeRate())

Question 14

Implement the design of the EPL_Team class so that the following output is produced:

Driver Code Output

# Write your code here ===================


manu = EPL_Team('Manchester United', 'Glory Glory Man United') Name: Manchester United
chelsea = EPL_Team('Chelsea') Song: Glory Glory Man United
print('===================') Total No of title: 0
##################
print(manu.showClubInfo())
Name: Manchester United
print('##################') Song: Glory Glory Man United
manu.increaseTitle() Total No of title: 1
print(manu.showClubInfo()) ===================
print('===================') Name: Chelsea
print(chelsea.showClubInfo()) Song: No Slogan
chelsea.changeSong('Keep the blue flag flying high') Total No of title: 0
Name: Chelsea
print(chelsea.showClubInfo())
Song: Keep the blue flag flying high
Total No of title: 0
Question 15
Implement the design of the Account class so that the following output is produced:

Driver Code Output


# Write your code here Default Account
0.0
a1 = Account() ------------------------
Oliver
print(a1.details())
10000.0
print("------------------------") ------------------------
Liam
a1.name = "Oliver"
0.0
a1.balance = 10000.0 ------------------------
Noah
print(a1.details())
400.0
print("------------------------") ------------------------
Sorry, Withdraw unsuccessful! The account
a2 = Account("Liam")
balance after deducting withdraw amount is
print(a2.details()) equal to or less than minimum.
------------------------
print("------------------------")
Sorry, Withdraw unsuccessful! The account
a3 = Account("Noah",400) balance after deducting withdraw amount is
equal to or less than minimum.
print(a3.details())
------------------------
print("------------------------") Withdraw successful! New balance is: 3071.0
a1.withdraw(6930);
print("------------------------")
a2.withdraw(600);
print("------------------------")
a1.withdraw(6929)
Question 16
Implement the design of the Author class so that the following output is produced:

Driver Code Output


# Write your code here Author Name: Humayun Ahmed
--------
auth1 = Author('Humayun Ahmed') List of Books:
Deyal
auth1.addBooks('Deyal', 'Megher Opor Bari')
Megher Opor Bari
auth1.printDetails() ===================
Default
print(‘===================’)
===================
auth2 = Author() Author Name: Mario Puzo
--------
print(auth2.name)
List of Books:
auth2.changeName('Mario Puzo') The Godfather
Omerta
auth2.addBooks('The Godfather', 'Omerta', 'The Sicilian')
The Sicilian
print(‘===================’) ===================
Author Name: Paolo Coelho
auth2.printDetails()
--------
print(‘===================’) List of Books:
The Alchemist
auth3 = Author('Paolo Coelho', 'The Alchemist', 'The Fifth Mountain')
The Fifth Mountain
auth3.printDetails()
Practice Task (17 - 22) Ungraded

Question 17

Design a Student class so that the following output is produced upon executing the
following code

Driver Code Output


# Write your code here Student name and department need to be set
=========================
# Do not change the following lines of code. Department for Carol needs to be set
s1 = Student() =========================
print(“=========================”) Jon is from EEE department
s2 = Student(“Carol”) =========================
print(“=========================”) ###########################
s3 = Student(“Jon”, “EEE”) Name: Bob
print(“=========================”) Department: CSE
s1.update_name(“Bob”) Bob enrolled in 3 course(s):
s1.update_department(“CSE”) CSE110, MAT110, ENG091
s2.update_department(“BBA”) =========================
s1.enroll(“CSE110”, “MAT110”, “ENG091”) Name: Carol
s2.enroll(“BUS101”) Department: BBA
s3.enroll(““MAT110”, “PHY111”) Carol enrolled in 1 course(s):
print(“###########################”) BUS101
s1.printDetail() =========================
print(“=========================”) Name: Jon
s2.printDetail() Department: EEE
print(“=========================”) Jon enrolled in 2 course(s):
s3.printDetail() MAT110, PHY111
Question 18

Design a Student class so that the following output is produced upon executing the
following code:
[Hint: Each course has 3.0 credit hours. You must take at least 9.0 and at most 12.0
credit hours]

Driver Code Output


# Write your code here ##########################
Name: Alice
# Do not change the following lines of code. ID: 20103012
s1 = Student(“Alice”,“20103012”,“CSE”) Department: CSE
s2 = Student(“Bob”, “18301254”,“EEE”) ##########################
Name: Bob
s3 = Student(“Carol”, “17101238”,“CSE”) ID: 18301254
print(“##########################”) Department: EEE
print(s1.details()) ##########################
Alice, you have taken 9.0 credits.
print(“##########################”)
List of courses: CSE110, MAT110, PHY111
print(s2.details()) Status: Ok
print(“##########################”) ##########################
Bob, you have taken 6.0 credits.
s1.advise(“CSE110”, “MAT110”, “PHY111”)
List of courses: BUS101, MAT120
print(“##########################”) Status: You have to take at least 1 more course.
s2.advise(“BUS101”, “MAT120”) ##########################
Carol, you have taken 15.0 credits.
print(“##########################”)
List of courses: MAT110, PHY111, ENG102,
s3.advise(“MAT110”, “PHY111”, “ENG102”, CSE111, CSE230
“CSE111”, “CSE230”) Status: You have to drop at least 1 course.
Question 19

Write the Hotel class with the required methods to give the following output as shown.

Driver Code Output


# Write your code here Staff With ID 1 is added
=================================
# Do not change the following lines of code. Staff ID: 1
Name: Adam
h = Hotel("Lakeshore")
Age: 26
h.addStuff( "Adam", 26) Phone no.: 000
=================================
print("=================================")
Guest With ID 1 is created
print(h.getStuffById(1)) =================================
Guest ID: 1
print("=================================")
Name: Carol
h.addGuest(“Carol”,35,”123”) Age: 35
Phone no.: 123
print("=================================")
=================================
print(h.getGuestById(1)) Guest With ID 2 is created
=================================
print("=================================")
Guest ID: 2
h.addGuest("Diana", 32, “431”) Name: Dianal
Age: 32
print("=================================")
Phone no.: 431
print(h.getGuestById(2)) =================================
All Staffs:
print("=================================")
Number of Staff: 1
h.allStaffs() Staff ID: 1 Name: Adam Age: 26 Phone no: 000
=================================
print("=================================")
All Guest:
h.allGuest() Number of Guest: 2
Guest ID: 1 Name: Carol Age: 35 Phone no.: 123
Guest ID: 2 Name: Dianal Age: 32 Phone no.: 431
Question 20

Write the Author class with the required methods to give the following outputs as
shown.

Driver Code Output


# Write your code here =================================
A book can not be added without author name
# Do not change the following lines of code. =================================
a1 = Author() Number of Book(s): 1
print("=================================") Author Name: Anna Kavan
Science Fiction: Ice
a1.addBook(“Ice”, “Science Fiction”)
=================================
print("=================================") =================================
a1.setName(“Anna Kavan”) Number of Book(s): 2
a1.addBook(“Ice”, “Science Fiction”) Author Name: Humayun Ahmed
a1.printDetail() Science Fiction: Onnobhubon
print("=================================") Horror: Megher Upor Bari
a2 = Author(“Humayun Ahmed”) =================================
Number of Book(s): 3
a2.addBook(“Onnobhubon”, “Science Fiction”)
Author Name: Humayun Ahmed
a2.addBook(“Megher Upor Bari”, “Horror”) Science Fiction: Onnobhubon, Ireena
print(=================================") Horror: Megher Upor Bari
a2.printDetail() =================================
a2.addBook(“Ireena”, “Science Fiction”)
print("=================================")
a2.printDetail()
print("=================================")
Question 21
Implement the design of the Hospital, Doctor and Patient class so that the following
output is produced:

Driver Code Output


# Write your code here =================================
Doctor's ID: 1d
# Do not change the following lines of code. Name: Samar Kumar
h = Hospital("Evercare") Speciality: Neurologist
d1 = Doctor("1d","Doctor", "Samar Kumar", "Neurologist") =================================
=================================
h.addDoctor(d1)
Patient's ID: 1p
print("=================================") Name: Kashem Ahmed
print(h.getDoctorByID("1d")) Age: 35
print("=================================") Phone no.: 12345
p1 = Patient("1p","Patient", "Kashem Ahmed", 35, 12345) =================================
h.addPatient(p1) =================================
print("=================================") Patient's ID: 2p
Name: Tanina Haque
print(h.getPatientByID("1p"))
Age: 26
print("=================================") Phone no.: 33456
p2 = Patient ("2p","Patient", "Tanina Haque", 26, 33456) =================================
h.addPatient(p2) All Doctors:
print("=================================") Number of Doctors: 1
print(h.getPatientByID("2p")) {'1d': ['Samar Kumar', 'Neurologist']}
print("=================================") All Patients:
Number of Patients: 2
h.allDoctors()
{'1p': ['Kashem Ahmed', 35, 12345], '2p':
h.allPatients() ['Tanina
Haque', 26, 33456]}
Question 22

Design the Vaccine and Person class so that the following expected output is
generated.
[N.B: Students will get vaccines on a priority basis. So, age for students doesn’t matter]

Driver Code Output


# Write your code here =================================
1st dose done for Bob
astra = Vaccine("AstraZeneca", "UK", 60) =================================
Name: Bob Age: 21 Type: Student
modr = Vaccine("Moderna", "UK", 30)
Vaccine name: AstraZeneca
sin = Vaccine("Sinopharm", "China", 30) 1st dose: Given
p1 = Person("Bob", 21, "Student") 2nd dose: Please come after 60 days
print("=================================") =================================
p1.pushVaccine(astra) Sorry Bob, you can’t take 2 different vaccines
print("=================================") =================================
p1.showDetail() 2nd dose done for Bob
=================================
print("=================================")
Name: Bob Age: 21 Type: Student
p1.pushVaccine(sin, "2nd Dose") Vaccine name: AstraZeneca
print("=================================") 1st dose: Given
p1.pushVaccine(astra, "2nd Dose") 2nd dose: Given
print("=================================") =================================
p1.showDetail() =================================
print("=================================") Sorry Carol, Minimum age for taking vaccines is
25 years now.
p2 = Person("Carol", 23, "Actor")
=================================
print("=================================") =================================
p2.pushVaccine(sin) 1st dose done for David
print("=================================") =================================
p3 = Person("David", 34) Name: David Age: 34 Type: General Citizen
print("=================================") Vaccine name: Moderna
1st dose: Given
p3.pushVaccine(modr)
2nd dose: Please come after 30 days
print("=================================") =================================
p3.showDetail() 2nd dose done for David
print("=================================")
p3.pushVaccine(modr, "2nd Dose")

You might also like