video

Lesson video

In progress...

Loading...

Heya.

Welcome to the object-oriented programming unit.

My name is Mark and I'm going to be your computing teacher for this unit.

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, but you'll also need a repl.

it account.

To get an account, you're going to need to ask your parent or carer's permission.

So make sure you do that before starting this lesson.

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

One last thing, I've got some water here and I want you to make sure that you've got some refreshment if you need it throughout the lesson.

If you want to pause the video now and go get all the things that you need, come back and resume the video and we'll get started.

In today's lesson you're going to describe the role of conventions in programming, and how they make programmes both easier to write and easier to read.

You're then going to recall that you already use a programming paradigm in the Python programmes you will have already written.

We'll then going to define object-oriented programming which is a new paradigm and will be the focus of this unit.

Finally, you're going to identify a class and an object inside of a programme.

These are two fundamental building blocks of object-oriented programming.

So, let's get started.

To start off with, you're going to have a look at the conventions you already know and use.

So I'm going to show you a programme.

I want you to see how you could improve it.

And here's the programme for you.

Imagine it's been written by a fellow Computer Science student.

They've written this programme to achieve a specific purpose and your job is to read the programme and try and work out what will happen when it's executed.

Pause the video now, have a read through, and make some notes about what you think will happen when this programme is run.

Hey there, welcome back.

Hopefully you've realised that this programme generates a random multiplication question and then ask the user to guess the answer.

It'll then tell the user whether the answer was right or wrong.

And you've probably worked that out.

It might have taken you a little while and it could have been easier, couldn't it? There's a key convention that the writer of this programme has not followed, and that's to give variables relevant names to the things that that their for.

A, B, C, and D are not very useful variable names, and it made it quite hard for you as a reader to immediately tell what it did.

I imagine you have to kind of trace through it to work out what was going on.

If the writer had given them relevant names to start with, that whole process would have been a lot easier.

For example, they could have called the first two inputs A and B num1, and num2, right? Because they're two numbers.

The result of the multiplication could have been given the name result, right? It's really clear what that's for.

And then the answer that the user gives shouldn't be D, it should also be answer.

So here you can see an improved version of this programme that follows the convention of giving variables relevant names.

Now, there are loads of conventions in programming and that's what we're going to focus on for the next activity.

To clarify, a convention is an agreed upon practise and it is the way that things are usually done.

Now, conventions on specific to programming.

They're used in a variety of disciplines and they help people work together and collaborate effectively.

And you're going to have a little look at how they do that.

First, let's have a look at this mathematical equation.

You've got it written down here and I'd like you to pause the video again, and I'd like you to answer these questions.

How would you work out the answer to this mathematical equation, this question? Specifically, which operations, the plus, the multiply, or the divide would you do first? Pause the video here, have a go, write a couple notes on it so just so you know when we come back to discuss.

See you in a sec.

Welcome back again.

Now, looking at this equation, some of you probably immediately went to the bid mass convention that you're taught in maths.

Now this is another example of a convention in a separate disciplines programming.

And this is just to show you that it's not an individual thing inside of programming.

Conventions are used across the board to make it easier for people to collaborate and work together.

What do you think? Why is this convention useful? So BIDMAS tells us which order the operation should go in.

And so we divide first because the D stands for divide, then we'd multiply, which is the M, and then we would do the addition last.

Why is this useful? Why is it useful to have a convention that tells us which order to do things in? Well, it's useful because it doesn't matter who I give that equation to.

As long as they know the convention of BIDMAS will arrive exactly the same answer.

So this convention is shared so that everybody can understand what they should do with a mathematical equation.

And it means that I can share this equation, for example, with somebody across the world who I've never met before and they will still know how to work out the answer and how to replicate the answer that I got.

Now, I want you to pause the video here and head over to your worksheet.

We're going to look at programming convention, specifically now, and there are two programmes on your worksheet that are not following a convention.

Your job is to find out which convention they're not following and make some suggestions on how you would improve the programme.

So pause the video now, head over to the worksheet, and then come on back when you've done that.

Welcome back.

Right, let's have a look then.

So this was the very first programme that I showed you and this is breaking a very important convention, and hopefully some of you spotted it straight away.

Now, typically in a Python programme and in many other languages too, if we need to use modules which are extra bits of code we need to import in, we should import them at the top.

This makes it a bit easier to read and interpret the programme.

I want you to think, who is this convention for? Is it for the reader or for the writer? You can pause the video and have a think about it, make some notes, then we'll resume.

In reality, this convention is for both the reader and the writer.

For a reader it makes it easy to spot which modules are needed for a specific programme.

Now, these two, in this example, are built into Python.

But you might also have modules if somebody needs to download and instal.

And having them at the top of the programme, people know where to look so that they can go prepare before running your piece of code.

For the writer, it's good to know where you need to put your modules when you're writing a programme.

And if you needed to add a new module it's good to group them all together.

So really it's for both.

For both the writer and the reader, it has benefits.

Here's the second programme.

Now this one, the convention might not have been quite so obvious, but hopefully you noticed that there's a lot of repetition in this programme that isn't necessary.

The writer hasn't used a loop and the whole programme really is just a repeated set of steps, which we know as programmers should be in a loop.

So, here's a much better version of this programme.

It's better because it's easier to read and it's better because, to be honest, it'd be easier to write.

So is this convention for the reader or for the writer? Can you pause the video again if you'd like to have a think about it and then resume and we'll carry on.

Right.

Again, this convention is for both.

It's easier to read.

The first one was a bit, you know, it took a while.

Even though it's a really simple programme, I might have taken a while to understand what was going on.

And it's easier to write too.

It's much shorter.

It takes me much less time.

So this is another convention that benefits both the reader and the writer, and hopefully you're noticing a pattern here.

So, we've spoken a little bit about conventions and there benefits for readers and for writers.

So what I'd like you to do is head over to your worksheet and there's a task on there for you to complete a couple sentences starters which explain the benefits of following a convention by the writer and a reader.

This is quite an important point so I just want to make sure you have some notes on it.

So pause the video here and then come back when you're done.

Welcome back, let's have a look.

So, the first I sentence I told you to complete was conventions in programming help the write of a programme because they give us guidelines to follow to write and structure better programmes.

Welcome back.

Now, let's have a look at the answers to those two sentence starters.

So, conventions in programming help the writer of a programme because they give us guidelines to follow to write and structure better programmes.

And really that's what we want to do, right? Especially if we're learning programming.

We want to learn how to kind of write and structure our programmes in a way that's readable and also easy for us to edit or debug And conventions in programming help a reader to interpret programme making it easier to locate specific parts, but also to interpret the programme as a whole.

Hopefully you got something along those lines.

Now, some conventions are universal to all programmes and programming languages.

Things like giving variables relevant names and importing modules at the top are quite common across a bunch of different programming languages.

Some of those are more language specific.

For example in Python, you always declare all functions at the top of your programme regardless of where you're going to use them.

This isn't the case in lots of other languages, but in Python we do that because, again, it allows a reader or us to find the functions much easier.

Now, the reason we're talking about conventions now is that this whole unit is going to teach you a new way to write programmes and you're going to have to follow specific rules and guidelines to fit within that.

And I just wanted you to know that you already do things like this.

You already use conventions when you're writing programmes.

And so what we're going to cover in this unit is nothing new or scary.

It's just a different version of a practise that you already have.

Next, we're going to have a look at programming paradigms In this unit, as I've already said, you're going to be learning object-oriented programming.

I'm going to call it OOP going forward.

But just know whenever I say OOP, I mean object-oriented programming.

I'm just saving myself some breath.

Now, it is a programming paradigm which is a specific way to write programmes.

The conventions that we've just looked at inform how we write individual parts of a programme.

Maybe a single line, maybe a section, an if statement, or a loop.

So it's very specific, it's targeted.

Conventions help us write better programmes in small chunks.

Programming paradigms give us a set of guidelines on how to write entire programmes, and they have rules that we need to follow.

Paradigm might be a new word to some of you, but all it means is a typical example or a pattern of something, a pattern or a model.

Essentially, it's quite a fancy way of saying, a way to do things.

An example of a paradigm might be us right now.

You're taking a lesson online and I'm teaching a lesson online.

This is not the usual paradigm of how education works.

Usually you're in a classroom in front of the teacher and I'm on the board and I can teach one single class.

We are now stepping out of our usual paradigm and into a new one where I deliver lessons online and you watch them, right? So that's just another application.

Don't worry, it just means a way of doing things.

Even if you don't know it, you already use a programming paradigm.

It's called procedural programming, and you will have definitely used it in Python in some of your earlier programming classes.

Procedural programming makes uses of subroutines to reuse code blocks.

Procedures are subroutines that perform a specific task and then return to the main programme.

But there are also functions which perform a specific task and then return to the programme, but they also return data.

So that's the difference, procedures and functions.

Procedures just do something, functions return something.

Now, let's have a little activity, let's apply this.

So if you have a look at this code on the right-hand side here, I want you to tell me where is the subroutine inside of this? And is it a function or a procedure? Feel free to pause the video and have a think about it, make some notes Right.

Well, inside that code input is a subroutine and specifically it's a function, because it returns whatever the user has input to the main programme.

So even in that short simple piece of code, there's a procedural programming paradigm being used.

Now, both functions and procedures are huge part of this paradigm.

And the key idea is that repeated steps can be bundled together and then inserted into your programmes when they are needed, instead of having to explicitly write every new part of a programme, right? Now, just to highlight this and I'd like you to pause and take a note of this about procedural programming because it's going to be important and we'll compare it later.

So data is stored in the main programme and then passed to subroutines.

And sometimes new values are returned based on what we input to the subroutine and also the process that goes on behind the scenes.

This is the key idea about procedural programming.

So as I said, pause, just take a note of that 'cause it'd be important for later.

Now, I'm going to get you into the action again and I want you to have a go at implementing procedural programming, but we're going to do a bit of a code comprehension exercise again.

Now this is where you'll need your Replit account.

So if you want to head over to your worksheet, there's a link on there for you.

Follow the link and I want you to have another go at reading and interpreting what this programme does.

I'm going to load up on my computer and then when you get back we'll go through it.

Hey, welcome back.

I've moved to this bottom corner just so we can see what's going on in the code above.

Hopefully, you were able to read and interpret this programme and make some predictions, and then test them by running the programme.

We're just going to walk through it a little bit just so you understand what's going on.

And so when we move on and we try to improve this programme, you know what we're trying to achieve.

So this programme will ask you to input three book titles.

After you've input the book title, it's going to capitalise it.

Right, that's online five, just over here.

It's going to capitalise it.

And it's then going to work out where inside of the list, it should place your book.

Now, it does that three times and it sorts them in alphabetical order.

So if I run this, we'll just have one go through.

So let's see, the first book, is called "Harry Potter," second book is called "The Northern Light," Philip Pullman.

And then should we do "The Lion, the Witch, and the Wardrobe?" Another classic.

And what we're left with is a list of books that's sorted into alphabetical order.

Now, there are some functions and some procedures inside of this code, but it's not really following the procedural programming paradigm to its fullest.

Now, what I want you to do is I want you to head back over to the worksheet, and there's a new version of this programme that does follow the procedural programming paradigm.

And I want you to see what's different about the programme, how it's been improved and whether anything has actually changed about the execution.

So head over there, do the activities on the worksheet, and then come back and we'll have a look at the new version of the programme.

Heya, welcome back.

Let's have a look at this new version of the programme and talk through some of the questions I asked you to answer.

So the first question was, has the operation, the execution of this programme, been changed in any way by updating it to follow the procedural programming paradigm? And the answer is no, it hasn't.

It's exactly the same.

All we've done is taken those repeating blocks of code and put them into our procedure so that we can call it when we need it.

And this is following the paradigm, right? It's all about keeping the data in the main programme and then you have subroutines, in this case a procedure, that take that data and do something with it.

So if I run this programme, we'll put some books in.

So let's start with "Harry Potter" again, and then were going to do some slightly different ones this time.

So let's do "Alex Rider," and then let's also do "Goosebumps".

That's now been sorted into alphabetical order, right? "Alex Rider" first, "Goosebumps," and then "Harry Potter." So the operation is exactly the same.

It's not changed in any way.

But there are some benefits to this type of programme.

As a reader, it's much easier to interpret what's going on because you only need to look in one place, inside of the sort procedure.

All of the functionality is essentially held in there.

Everything else is just taking input and then passing that data to this new procedure that we've read.

So as a reader, you only need to look in one place to see what's going on.

Now as a writer, this also has benefits because everything, again, is contained in one place.

Now you might have multiple functions, but even then if you want to change something about the way that your programme works, say you wanted to make the books sort in the other direction, so going from lowest in the alphabet to highest which is ascending, it's what that's called, we just need to change it in one place, just up here.

So we change that to be that way instead.

So it's now a greater than symbol instead of a less than symbol.

And now if I run my programme, we should see that it changes what happens.

So let's do the same books again to "Harry Potter," "Alex Rider," and then "Goosebumps." Now it's sorted in the opposite direction.

So we're starting at the lowest and moving up to the highest.

And that was just one change.

In the old programme I would have had to go through and change that in every single place where it occurred.

So hopefully, this has given you an insight into why following a paradigm and particular why following the procedural programme paradigm is beneficial.

And I hope you're going to take these lessons and apply them to this OOP paradigm that we're going to look at next.

Next, we're going to have a look at object-oriented programming, the focus for this unit.

Object-oriented programming is another paradigm and actually builds off of procedural programming.

So the things that you know and you've already used about procedural programming will come in use when we're looking at OOP.

Now, it's not a brand new thing.

The very first language to include OOP principles is called Simula, and that was released all the way back in 1967 And now a number of different modern languages allow you to use OOP or are built entirely around OOP.

You remember that in procedural programming, we said that the data is stored in the main programme and then it's passed to subroutines which do something with it, and either pass it back or just return to the main programme to continue executing.

In OOP, the data and the subroutines are stored together in a structure called an object, and these are created from classes.

Now these are two brand new terms that you will learn in this unit.

The OOP paradigm is focused on objects which represents the parts of a computer programme.

We can create objects to represent real physical things like human being or maybe rooms in a house, or they could be small parts of a larger programme.

For example, a widget on a mobile app that shows you the weather.

That's like a small part of the larger programme that's running, but that itself could be an object and a class.

You could even use object oriented programming to create enemies in a video game.

You have one class.

Here you can see it's called enemy at the top of the diagram.

And this would detail what information each enemy can hold and the behaviours that it has.

You would then create individual objects based off of this class to be the actual enemies in the game, and they would all have slightly different attributes.

They would have different values, but the framework would be exactly the same because it's based on that class.

Right, I'm going to kind chuck you into the deep end a little bit here, but I think you will be able to pick out some parts of object-oriented programming without me really explaining them to you yet.

So, I'd like you to pause the video here and have a look at this programme.

And I want you to tell me what you think the class is called and what you think the objects are called.

Yeah, pause the video public, have a look, and then come on back and we'll go through it.

Right.

Now, I know this is all new to you, but hopefully you were able to make some kind of educated guesses there.

The class in this example is called House.

We import the class so that it can be used to create objects.

Classes usually you have capital letters at the start of their names.

This is another convention so it's easy for us to identify.

The objects inside this piece of code are called my_house and pm_house.

These are actually the variables that stole the object.

Now we can use those variable names to access the different pieces of information, the subroutines, that are on the objects, defined by that class and then stored on the objects.

Now this is just a little taste of OOP.

In the coming lessons, you and I are going to explore this paradigm further and you'll even write your own class and create your own objects.

But for today, that's it.

You've completed lesson one of object-oriented programming.

Thank you so much for joining me.

I do hope you enjoy this look at the way that we write programmes.

And I hope you're looking forward to learning a bit more about OOP in coming lessons.

The last thing for me is to say, if you'd like to, please ask your parent or carer to share your work on Instagram, Facebook, or Twitter tagging @OakNational and #LearnwithOak.

We would love to see what you're up to.

Right, that's all from me.

I'll see you in lesson two.

Have a great day.