Вы находитесь на странице: 1из 3

Introduction to Software Development

Seminar Week 11
Create a word document and answer the following questions. Where there is
coding involved, take a screenshot of the code along with the output and
attach it to the word document below each question.

Tasks:
1. a) Copy and paste the following code into your Python editor. Check
that the code runs and produces the output you expect.
class Student:
def init (self, name, age, course, ID):
self.name = name
self.age = age
self.course = course
self.ID = ID
def print_details(self):
print("Name: " + self.name)
print("Age: " + str(self.age))
print("Course: " + self.course)
print("Student ID: " + self.ID)

student1 = Student("Bob", 20, "Computer Science",


"1000121")
student2 = Student("Alice", 21, "Computer
Science", "1000475")
student3 = Student("Jane", 18, "Information
Technology", "1000823")

student1.print_details()
student2.print_details()
student3.print_details()

b) At the end of the file, add code to prompt the user to enter details for
a new student. Using these details, create a new object of class Student,
and call the print_details function to display the details of the new
student.

Page 1 of 3
Example output:

2. a) In a new Python program, copy and paste the following code:


class BankAccount:
def init (self, account_number, balance):
self.account_number = account_number
self.balance = balance

def print_balance(self):
print("Account balance for account " +
self.account_number + " is: " +
str(self.balance))

account1 = BankAccount("123456789", 1000.00)


account1.print_balance()

Check that the code runs and produces the output you expect. Create
two more BankAccount objects and call the print_balance function to
display the balance of these accounts.
Example output:

b) Add another attribute to the BankAccount class, to store the name of


the account holder. Modify the constructor function as necessary, and
modify the print_balance function so that it also prints out the account
holder’s name. Add a new function to the class, called print_name,
which prints the account number and the name of the account holder.
At the end of the program, call this function on the objects you have
created, to show the names of the account holders.
Example output:

Page 2 of 3
c) Add two new functions to the BankAccount class, one called
withdraw and one called deposit. The withdraw function should take
one parameter, and should deduct the amount passed in the parameter
from the account balance. The deposit function should also take one
parameter, and should add the amount passed in the parameter to the
account balance. At the end of your program, call both functions on the
objects you have created. Then call the print_balance function to show
the updated account balances.
Example output:

Page 3 of 3

Вам также может понравиться