video

Lesson video

In progress...

Loading...

Hey ya.

Welcome back to lesson three of object-oriented programming.

I'm Mac, your computing teacher for this unit.

In today's lesson we're going to take all the learning you've been doing about classes and objects and you're going to apply it to create a class all of your own.

For today's lesson, you're going to need a notepad and a pen, so you can take notes and answer questions while you're learning.

You also are going to need a Repl.

it account.

So that's R-E-P-L.

it.

And to get an account, you're going to have to ask your parent or carer, so make sure that you've signed up and you've got an account before you start today's lesson.

As per usual, I've got my water here, and I want you to make sure that you've got some refreshments throughout the lesson, if you need it.

I'd also like you to remove all distractions from around you, and this includes your mobile phone.

If you want to pause the video here, go get everything you need, remove all those distractions, get yourself some water, come on back when you're done and we'll get started.

In today's lesson, you're going to create a class.

We're going to define the use of the self parameter in object-oriented programming.

You're going to create a method inside of a class, and you'll also access attributes using getters and setters.

A lot of this we covered in lesson two.

So if you haven't done that lesson yet, make sure you go back and do it first.

But today you're going to be applying this stuff all on your own.

Let's get into it.

The very first thing we're going to do is we're just going to take a moment to recap some of the terms that we've used throughout this unit so far.

Object-oriented programming is full of new terms for you to learn and try to understand.

And one of the best ways to do that is to repeatedly go over them and test your knowledge.

So that's what we're going to do right now.

On the left-hand side here, you can see I've got some terms, some things that we've discussed so far about object-oriented programming, and on the right-hand side, I have some definitions.

What I'd like you to do is I want you to pause the video here, and I want you to match the term to the definition.

But instead of just drawing a line like you normally would, I want you to write it out in a full sentence.

So something like, "A method is a", and then write the description out.

This will help you remember it for later on.

If you struggle with handwriting, feel free to type it out.

The process of processing the words and putting them on a page or typing them out is what's important.

So, pause the video here, have a go at that, come back when you're done and we'll go through it.

Right, welcome back.

Now, hopefully, those terms are starting to sink in a little bit.

Let's just make sure that you've got the right term matched up with the right definition.

And then those are going to be great revision tools for you later.

So the first, a method is a associated action of an object, right? So methods are the associated actions of an object.

An instance is every new object that's created from the same blueprint.

So you'd say something like, "I created a new instance of an object from a class", okay? So if you hear me use the word instance or anybody else, while you're talking about object-oriented programming, that's what they're referring to.

Every new object is created from the same blueprint.

Next, we have a constructor, which is a special method used to create new objects from a class.

So in order to create an instance, we need to use a constructor.

A constructor, special method, it creates a new object from a class.

And finally, yes, the attributes are the individual properties of an object.

Cool, so hopefully you got all those definitions matched up.

If you got any of them wrong, pause the video here, make sure that they're right in your notes.

'Cause like I said, you're going to need to go over them later.

Resume the video when you're done and you're happy, and we'll keep going.

Sweet.

We're going to move on now to planning a new class.

In lesson two, I gave you a design for a class, and I walked you through how to make it.

In this lesson, we're going to design the class together and then you're going to make it on your own.

So I'm stepping back a little bit about how much help I'm giving you.

This is so that you can apply the things that you've already done on your own to help cement your learning.

For today's lesson, you're going to be stepping into the role of a game programmer.

You've been selected to programme for the upcoming puzzle game Monster Quest.

Very exciting.

Your first job is to create a programme for managing the monsters in the game.

And you're going to need to use object-oriented programming to do it.

So we're going to create a Monster class, and you're going to use OOP to do it.

Let's have a look at how that's done.

We have a couple of requirements for a Monster class, right? This is to give you an idea of what you need to make.

The first requirement is that every monster has a name, some health points and a line of dialogue.

Something that they're going to say to the player, right? Dialogue just means words or something you'll say.

Each monster must be able to take damage and speak to the player, right? So it needs to have a name, health points and a line of dialogue, and it needs to be able to take damage and speak to the player.

Now, remember those definitions, 'cause you should get a clue about what we're going to need to make.

And I can pause the video here, before I go through what we're going to make.

I want to give you a chance to think about it on your own and apply your own ideas.

So pause the video here, head over to your worksheet.

And there are a few tasks on there for you to complete.

So start thinking about your class.

Awesome, welcome back.

Hopefully, you've enjoyed thinking a little bit about how you're going to meet those requirements.

Let's go through it.

So the first requirement was that each monster must have a name, some health points and a line of dialogue.

So what attributes are we going to need to meet those requirements? Well, we'll need one for each of those properties, right? We're going to need a name.

We're going to need a line of dialogue.

I've called mine speech.

You can call yours something else, dialogue, talking, anything at all.

Mine's going to be called speech.

So just keep an eye out for that.

And then we also need some health points, right? HP.

Cool, let's think about these a little bit deeper though.

'Cause we can get our programme, our planning heads on right now.

So the next question I asked you was what type of data are each of those attributes going to be? Now, the type of data they're going to be will change how we're going to interact with it inside of our programme.

So it's important to think about it before you start typing things out.

So what type of data will the name be? Well, name will be a string.

Speech will also be a string, right? Both of those things are just words.

You know, a name will be a single word.

Speech might be a few words or put together, and then we have the health, which will be an integer, right? It's going to be a number.

We're going to go for whole numbers.

You could do something like a float, if you wanted to, but let's keep it simple and just do integers for now.

Finally, I'd like you to pause the video here, and I want you to draw the table I've got on the left-hand side here.

You can see at the top, we've got the title for our class.

It's going to be Monster.

Notice how I've got a capital M there, following the convention for class names.

And in the top part, I want you to write out each of these attributes, and then a colon, and then I want you to write what type of data they're going to be.

So you can see here, I've done name already, right? So name, and it's going to be a string.

Now, in your notes, draw out the table and make sure you add those other two attributes as well.

Pause the video, play when you're ready, and we'll keep going.

Sweet, let's keep going.

Just an example.

This is called a class diagram.

We'll go through that in a minute, but this is what the start of your class diagram should look like.

So you've got all of those attributes and the data types right there, ready for you to plan your class.

Now, I want you to pause the video again, and you're going to go back to your worksheet and think a little bit about the methods that you're going to need for your monster class.

Resume when you're done, and we'll go through it.

Welcome back.

Let's have a look at these methods.

So the second requirement was that each monster must be able to take damage and speak to the player.

And I asked you what actions or methods your monster class would need to be able to use to meet this requirement.

Now, I think they're going to need two methods.

The first one I'm going to call take damage, okay? It's quite on the nose, but at least it's really obvious what that method is supposed to do.

And then a second method called speak, right? Hitting the nail on the head, but those are going to be our two actions.

I also asked you to think a little bit about how they will work.

So let's have a look at the first one.

So, take damage, let me just move my camera quickly.

I know I'm not really supposed to do this in the middle of a video, but it's okay.

I'm going to move myself down here for the minute, just so you can see what's going on up there.

So take damage.

The monster's health is going to go down, right? That's what taking damage means.

And for speak, we're going to print the line of dialogue, that attribute that we set earlier.

I also wanted you to think a little bit about the parameters that you're going to need, 'cause all methods will need parameters.

Now, hopefully you put the self parameter in both methods, right? 'Cause every single method in object-oriented programming needs the self parameter.

It's the way that an object knows which of the many instances of an object it is.

Take damage is also going to need a damage parameter.

It needs to know how much the monster's health should go down by.

So let's go back to our class diagram.

Now what I'd like you to do is add these two methods into this class diagram in the bottom box there, including the parameters.

You don't need to put the explanation.

That is just something to keep in your head, but I'd like you to add those in there and then resume the video when you're done.

Welcome back, I've moved myself back up to the corner here, 'cause I want you to be able to see what's going on down there.

Now, let's have a look at what our UML class diagram should look like.

So we should've added those two methods in the bottom there, including the parameters.

Now, just remember that self parameter is very important.

This class diagram is part of UML, which stands for Unified Modelling Language.

And it's a way of using diagrams to create software.

It's another convention, so you could give this to any programmer around the world.

And as long as they knew the UML guidelines, they would be able to figure out what you were making and how it might fit into a larger programme.

It's just a nice way to help you plan your class.

Now, what I'd like you to do now, before we move on, is on your notepad, I want you to take this class diagram and I want you to use your example of the pet from last lesson, in lesson two, you made a Pet class.

And I want you to draw a rough plan for your new class for monster.

I want you to include constructors and the getters and setters you might need, just a really rough plan in your notepad.

You don't need to write full Python statements, even if it's just structured English, that's absolutely fine.

It's just good to think about these things before we jump into code.

Pause the video now.

I'm going to open up the pet example really quickly, and then we will tell you what you need to do for the next activity.

Right, hopefully, you were able to write out that quick plan in your notes.

If you weren't able to do the whole thing, don't worry.

We're going to step through it.

I'd also recommend, if you did lesson two, I'd recommend you opening up your pet example.

You can see mine here.

Now, we made this in the last lesson, and I guided you through the process.

And while you're making your class today, I'm going to give you specific parts of this class to use as a worked example.

That means that you're going to use it to create your new class.

So you're going to know that you need to make a constructor and then you can use the Pet class to see how you did it here, right? So I can see this is what a constructor might look like.

Okay, what do I need to change? What's different about my monster, right? So you're going to work through it like that.

In reality, past you is helping current you out, right? You're using this, your own example, but this time just a little bit more independent.

Cool, let's head back to the slides.

Welcome back.

So, the first task is creating your class and the constructor.

Now, on your worksheet, I've given you the pet constructor as an example, and I've also written out the steps that you need to take.

Now, I need you to create a brand new rappel project for this programme that you're going to make.

So go do that now, and then resume the video when you're done.

And I'll show you my example to work of off.

Right then, let's have a look at my example.

So here's one that I made earlier, but there's something wrong with it.

I'd like you to have a look at the programme and tell me what is wrong? What haven't I done here? And how would you fix it? If you need to pause the video, that's fine.

Pause it here, have a look and see if you can identify what I haven't done or what I've done wrong in this programme.

So you might have spotted that I haven't used the self parameter to set any of the attributes.

If you compared it with your Pet class, you'll see that every time I set an attribute, I have to use that self parameter.

A way to fix it would be just to add that self parameter before each of the attributes, right? So I'm setting self.

name = name, self.

health = health, speech, etc.

And whenever you write a function in a class, you must use the self attribute to assign and access the attributes on an object, sorry, the self parameter to access the attributes on an object.

And self just means this object.

So you could say that this constructor is just saying, "Set name only on this object "to whatever I'm passed through.

"Set health, only on this object "to whatever I'm passed through." This is how you bridge that gap between the blueprint and the individual instances of a class.

Self is very, very important.

We have to remember it, okay? So, self means on this object.

Now, if you want to pause the video again, check your constructor, it looks the same as mine, right? Use this example and your pet example just to double-check your work.

Some of you might have got it already, but let's just be sure before we carry on.

Ready to go? Let's do it.

So the next step in creating our class is creating getters and setters.

Pause the video again here, and I'd like you to head over to your worksheet, where again, I've given you the Pet class as an example, and you can use that to create the getters for your new Monster class.

Head over and do that now, resume the video when you're done, and we'll go through it.

Hey ya, so I'm back in rappel now and I'm going to show you my example.

So you'll see here, I've got my Monster class written out.

Here's the constructor, and I've already done a couple of the getters.

Now, one important thing I just want to highlight here is I've created a new file called monster.

py.

Now, I want you to make sure that you've also got your Monster class in a brand new file called monster.

py.

It'll be important later.

So, at the end of this, after I show and you pause, and just make sure yours in there.

You can just copy the whole thing out, if you need to, press Control + C and then paste into a new file, which you can create with this button up here.

Now, you can see I've done my getName and getHealth.

So let's just add the last of my getters, which is getSpeech.

Make sure I've got that self parameter, remember, very important.

Then we're going to return self.

speech.

Perfect, now let's also do my setters.

So setName.

Now, this time, I'm going to have the self parameter and I'm also going to have a name parameter.

'Cause I want, if I'm setting something, I'm going to be given a value to set it to, right? So then I'm going to say self.

name = name.

And you'll see again, that the line of code here is identical to what we do in the constructor, right? So we're just setting this name parameter on this object only to whatever we're passed through in both places.

Now, I'm going to do the same with health, no, just health, so I need to say setHelath, self, health, right? So two parameters again, and then we'll say self.

health = health.

And then one last setter.

So, setSpeech.

Self and speech.

Right, now let's do self.

speech = speech.

And that finishes off my getters for this Monster class.

Now again, you could call these variables anything you want.

It doesn't have to be health, you could say HP, if you want, it doesn't have to be speech, it could be dialogue.

The point is that we all meet the requirements, and that can be done in a number of different ways.

Now, let's head over to the slides and see what you're doing next.

Right, if you were following along with the worksheet, you should've also done a couple of extra tasks.

First, you should've gone to your main.

py file, then imported your new class on the first line.

As you can see in my example here, I've got from monster import monster.

So that grabs that Monster class from my separate file and brings it into this programme, so I can use it.

You also were supposed to create a monster object using your constructor.

And you can see, I've done that on line three here.

I've created a variable called zombie, gives you some indication of what kind of monster it's going to to be.

And then I've used that Monster with a capital M, keyword to use the constructor, and I've given him a name.

So my zombie is going to be called Dave.

He has 100 health points and his line of dialogue is going to be, "Brainnnnsss", a very zombie-ish thing to say.

So now, taking this as an example, I'd like you to print the name of the monster using your get and set methods.

I want you to print the line of dialogue again only using your get and set methods.

I'd like you to change the health point total to another number.

It can go up or down, it's up to you.

And then I want you to print the new health point total.

So pause the video here, have a go at these explorer tasks and then come on back, and I'll show you how they're done.

How did you get on? Let's have a look at how these explorer tasks are done.

So you can see here on line five, I've used my getName attribute, my method, my getter to print the name.

And then I've used my getSpeech method to print the speech, the line of dialogue that we made.

I then set my zombie's health points to 70 rather than 100.

And then I print the results of that to the console.

So hopefully, you got something similar in your code as well.

Right, we're getting close to the finish line now, we just got a few more things to do.

Now we need to add the methods.

So we discussed two methods earlier, take damage and speak.

So take damage is going to reduce the health points and speak is going to print the line of dialogue, and hopefully, something a bit helpful.

All right, pause the video here again, head over to your worksheet.

I've given you some examples in there from the Pet class to help you, have a go at writing these methods on your own.

You can do it, I believe in you, and then come on back and we'll go through it.

Welcome back.

How was it adding your methods? Let's have a look at how I implemented them.

Remember that it might be different, but it'll give you some idea of how you can achieve those requirements.

So here are my two methods.

So the first one, take damage, takes two parameters.

Like we said earlier, it says self and damage.

So damage is going to be the amount of the monster's health points need to go down.

So you can see that I'm using self.

health, because I'm inside the object.

I don't need to use the getter or setter.

I can just use self.

And then I'm setting that to be itself minus the damage.

So that'll lower it by the amount that I'm handed with as part of this method.

And then as well, I'm printing something useful out.

So the player can see what's happened.

So I'm going to say, "The health is now however much it is." I then made the speak method, which uses the monster's name to give some kind of context to the dialogue.

That's just a little bit of flavour that I like to add.

You guys might have done it in any number of ways, but the point is that the player will be able to see that line of dialogue printed out.

If you want to pause the video here, you can see as well I've catenated it a little bit.

So you can see those dots in the middle.

That just means that's where the getters and setters are.

So that's why the numbers kind of skip up.

If you want to pause the video here and just double-check your methods, make sure they work.

We're going to test them in a minute, but just double-check them now to make sure that they look something like this.

You might have a different kind of flavour, not saying their name or something like that, but that's fine.

Resume the video when you're done, and we're going to the last bit.

Right, one last activity.

We're going to test your monsters next.

So well done, we finished the Monster class now.

It's all where it needs to be.

We've got the methods in there.

We've met all of the requirements that were asked of us.

I've got three last explorer tasks for you.

I'd like you to create three monster objects.

You've already created one.

So you only need to do two more.

I want you to do damage to two of them using your new take damage method.

And I also want you to make the third monster speak, using your speak method, right? So don't use getters and setters this time.

Instead, I want you to use those new methods you just made.

Right, pause the video here, have a go at that, come on back when you're done.

Right, let's have a look at how we do that.

So here you can see I've made three monsters.

I've got a zombie, a werewolf and a vampire, and they all have health points and also a line of dialogue and a name.

I particularly like the vampire's dialogue, "I vant to suck your blood." It made me chuckle, hopefully it makes you chuckle.

I then did damage to the zombie, did damage to the werewolf, and I made the vampire speak.

Now, hopefully you were able to do this too.

Huge congratulations for creating your first class basically on your own.

I didn't really help you out individually with very much of that at all.

So, really well done for getting this far.

That's all from me today.

Thank you so much for persevering with it.

The last thing I'd like you to do is first of all, go take the exit quiz to refresh and recap on the knowledge that you've gained in this lesson.

And also, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter tagging @OakNational and #LearnwithOak.

I would love to see your monsters.

So if you would like to, please do that.

That's all from me today.

Thank you again for joining me for this third lesson about object-oriented programming.

Until next time, happy learning.