Python 7 Levels, L5, Unit 46, Object Oriented Basics

Powered By EmbedPress

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)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts