Exercise 1:
class Ball():
def move(self):
print(self," moving.")
myBall = Ball()
hisBall = Ball()
hisBall.size = "Large"
myBall.color = "Red"
myBall.move()
print("Color of the ball is :",myBall.color)
print("Size of hisBall is:",hisBall.size)
hisBall.move()
Exercise 2:
class Ball():
def __init__(self,color,state):
self.color = color
self.state = state
def move(self):
if self.state == "Still":
self.state = "Moving"
myBall = Ball("Red","Still")
print("The color of the ball is:",myBall.color,"status:",myBall.state)
myBall.move()
print("The color of ball is:",myBall.color,"status:",myBall.state)
Exercise 3:
class Ball():
def __init__(self,color,state):
self.color = color
self.state = state
def move(self):
if self.state == "Still":
self.state = "Moving"
def __str__(self):
msg = "The color of the ball is:"+self.color+"status:"+self.state
return msg
myBall = Ball("Red","Still")
print(myBall)
Homework:
class Student():
def __init__(self,name,grade):
self.name = name
self.grade = grade
def __str__(self):
msg = "Name: "+self.name+" Grade:"+self.grade
return msg
def study_hard(self):
if self.grade == "Qualified":
self.grade = "Excellent"
stu1 = Student("David Smith","Qualified")
print(stu1)
stu1.study_hard()
print(stu1)
[…] 43 Glance at Functions| |____Unit 44 Advanced functions| |____Unit 45 Use of functions| |____Unit 46 Object-oriented basics| |____Unit 47 Object-oriented Example Exercises| |____Unit 48 Student Information Management […]