*Please Note* - all presentations and code challenges+solutions are available to subscribing members. You've had your introduction to Classes and it's now time to start creating them. In this session we will be looking at creating two different types of classes. A planet class (based on the shape circle) and a student class based on an entirely different scenario, and provide a more abstract way of looking at how to create classes. Watch the video below and then go on to attempt the challenges presented in the code
""" Task: Create a Circle Class (based on the same tempate)... 1. Create circle 3 and 4, green and blue respectively and position them in different positions on the screen 2. use the scaleSize feature to draw white circles inside them! """ from turtle import* class Planet: #self is how we refer to things in the clas from within itself. .self is the first parameter in any function defined inside a class #to access functions and variables inside the class, their name must be preceded with self and a full-stop (e.g. self.variable_name) def __init__(self,x,y,radius): self.radius=radius #circles need a radius in order to be defined self.x=x self.y=y canvas = Screen() canvas.setup(800,800) turtle = Turtle() def circumference(self): return 2*3.1415*self.radius def scaleSize(self,scale): self.radius=self.radius*scale def draw(self,colour): self.turtle.goto(self.x,self.y) #go to (x,y+radius) self.turtle.color(colour) self.turtle.dot(self.radius) #dot takes the circle diameter as a parameter (size) #Specify width and height below e.g. 100,200 planet1=Planet(-200,-100,200) planet1.draw("red") print("Circumference *check the maths!* is:",planet1.circumference()) planet1.scaleSize(0.5) planet1.draw("yellow") planet2=Planet(300,300,100) planet2.draw("black")
#What this program does: check to see if a student has met their target (e.g. if test score > 90) #Task=======Add the attributes and methods to perform the following: #Log Student attendance (out of 100). Create method to find out if the student is a model student #IF the student has both attendance AND test_score >=90, then return "Model Student" #ELSE return 'Not quite a model student!" class Student: def __init__(self,name,test_score): #this init method is the first method to be invoked when you create an object #ensure you initialise all your attributes in the init method self.name=name self.test_score=test_score #=======creating a method to see if the student has met their target def hasAchievedTarget(self): #the self is a default parameter that the methods of your class accept, in order to use the attributes of your class if self.test_score>=90: print("Target met, Well done!") else: print("Target not achieved") #To access all of these attributes and methods, we need to create instances or objects of the class #Here we are also creating instance attribtues, Jonathan and 99 as his test score student1=Student("Jonathan",21) #the process of creating an object is called instantiation. print(student1.name) print(student1.test_score) student1.hasAchievedTarget()
The following code provides you with a Stack Class including a list (that is the stack itself), and methods that a stack uses such as push, pop, etc. Use this Stack class to produce a program to reverse a word. e.g. if the input is APPLE, then ELPPA should be returned. You need to use the methods in the stack class to achieve this.
#Define a Stack as a class with OOP implementation #Stack is LAST IN, FIRST OUT (unfair!) LIFO class Stack: #start with this part which is like a constructor def __init__(self): self.items= #one attribute called self.items and start this as an empty stack #method needed to push items on to the stack def push(self,item): self.items.append(item) #items is the attribute, and we append a new item def pop(self): return self.items.pop() def size(self): return len(self.items) #returns the length of the list def report(self): return self.items #returns the elements contained in the stack def isEmpty(self): if self.items==: #test to see if items is equal to empty list, return True return True else: return False #take in a word and reverse it word=input("Enter a word:")
Download Python 3 here: