video

Lesson video

In progress...

Loading...

Hey yah, welcome to the last lesson of Object Oriented Programming.

I'm Mac your computing teacher for this unit.

In today's lesson, you're going to explore a programme made using OOP.

You're going to investigate what kind of classes are in there, and then you can make some modifications.

This will be the culmination of everything we've been learning about Object Oriented Programming.

And I really hope you enjoy this look at a real piece of software that might be made using Object Oriented Programming.

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

And I'd also like you to make sure you've got a repl.

it account, so that's R-E-P-L-.

-I-T.

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

So make sure you've done that before you start today's lesson.

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

Finally, and you know it already.

I have my water here, I want you to make sure that you have some refreshments before you start today's lesson.

If you'd like to pause the video here, head off get everything you need, come back when you're done and we'll get started.

There's not too many objectives for today's lesson.

You're going to explore programme written with Object Oriented Programming using the paradigm.

And then you're going to explain the key concepts of Object Oriented Programming that we've been learning throughout this unit.

Let's get started.

The focus for today's lesson is going to be a piece of library tracker software.

Imagine your school library wants to use a new piece of software to keep track of the library books and which students or teachers have them.

The prototype for this project has been provided for you and it uses OOP, and as I said, all of the exploration in this lesson is going to be focused on this piece of software.

Before we get started I want you to make sure you have the example open.

So you'll find it by following the short link on the screen right now, if you open that up you'll see there as a repl.

it for you.

And before you do anything else, make sure that you've forked it, the way to fork it is by clicking on the name in the top left-hand corner, and then click fork at the bottom.

That means you've got your own copy for when you make modifications later, and we're not going to interfere with the example for all the other students.

So make sure you've done that.

Pause the video here, make sure you're already, you're looking at the example project and then come on back and I'll tell you your first task.

Cool, welcome back, you'll pause again straight away, but thank you for doing that.

First thing I'd like you to do, is I'd like to use the questions on the worksheet to explore the example programme, identifying some of the OOP concepts that have been used, and just getting to grips with what is already in there.

Head up to your worksheet, follow the questions I'm going to load up my library tracker software, and then we'll go through the answers when we get back.

Right, here's the library tracker software.

Hopefully you found everything you needed to, but let's just have a look.

So the first thing to note is that there are four files in this programme.

So we've got main dot.

py, book.

py, library.

py and user.

py.

The first question I asked you, was how many books are already in the programme.

And so hopefully you looked in this main.

py file here, and then you found that there were two books, right? So there's faults here, which is "The Fault in Our Stars" by John Green.

And then there's "Hunger Games", which is of course "The Hunger Games" by Suzanne Collins.

I also asked you what the names of the users were that were already in here.

Again, hopefully you were able to identify that these are the users right here, right? With the helpful comment, and their names are Jane Doe and Jakob Mertens.

Finally, I asked you which variable you would use to access the library.

Again, hopefully you were able to identify that you want this school underscore library variable here, because it's an instance of the library class.

Next, I asked you to have a look at all of the classes that are already in this programme.

So let's do that quickly.

Now I can see that they're going to be in my external files, right? So let's have a look.

We've got a book class, right.

Which has a few different attributes.

It's got title, genre, author, and current holder, right? So some information about the book and then presumably this is going to be who has the book, right? Hopefully from the names you're able to get that.

And then we've got our getters and setters just like you're used to, and we've got two methods on the book.

So we've got rent out, which takes a user as an argument and obviously self like all methods in Object Oriented Programming.

And this will probably give the book to whichever user you assign it, right? So it's going to change that current holder variable.

And it's also going to assign something on the user.

Next, let's have a look at the library.

So library, right, is another class.

And it's only got two attributes, it's got users and books.

Now, hopefully from your programming lessons, you'd notice that this is a dictionary and this is a list, right? So there's going to be collections of things, and don't worry about it, you just needed to identify what the attributes were.

Next, we've got some add getters and setters, right? So add book and get book inside of these lists.

And then we've also got a method called list books, author.

And if you have a look, what this is going to do, is it will list each book with underneath the author.

So at the moment, I believe that it's called right at the end here.

So let's just run it and we can see what it does.

So let's see here, it says John Green "The Fault In Our Stars", Suzanne Collins, "The Hunger Games".

Fine, so that's what that method does.

One last class to have a look at, so hopefully you noticed there were three.

is the user, and they have the attributes name, email, and history, and also current book.

So when we use that rent out method on the book class here, where it says rent out, we're probably going to be looking at this current book attribute.

So we can check whether a book has a current holder or user has as a current book.

Again, getters and setters very standard.

And we've also got a return book method, right.

Which could actually just be a, I was not quite so we got a getter and a setter.

And then this is to change, add something to the history, so the history is probably all the books that they have already looked at.

And then we're also just going to erase the current book variable.

So it looks like, so we've got nothing again.

Awesome, I'm going to head back to the slide so that you can get your first task, but hopefully you were able to identify all of those classes and attributes and some of the methods.

And now this has a bit more complicated than what you're used to seeing, but it's just using the exact same concepts that you're used to, and that you've used over the past five lessons.

Let's head back to those slides.

Now that you've identified some of the classes, attributes and methods that are involved in this library project, I'd like you to use the project and add a few things for yourself.

You're going to use your worksheet for this task.

And I want you to add two new books of your choice.

It could be a favourite books, books you've seen one time, they can be comics, they can be picture books, they could be a non-fiction whatever you like, two new books of your choice.

I'd like to you to add yourself as a user, so you can use a user construction and add your details in there.

Don't worry, they're not going to go anywhere, so there's no data privacy concerns.

And then I'd also like you to check test, the checking out functionality.

So you know that rent out method, I want you to use that to give one of your new books to you as a user.

Head to your worksheet, it's got step by step instructions for what you need to do, when you come back, we'll have a look at my version of the library tracker software.

So you can see how it's done.

See you in a sec.

Right, now, I'm going to show you how you complete these explorer tasks.

So the first one was to add two new books to the library.

So let's do that now.

I'm going to add one called, one of my favourite books of "Name of the Wind" by Patrick Rothfuss.

So we'll add that, and I'm going to use the book constructor.

Now, hopefully you were able to just use these as an example and just copy that, but put your own information in, right? So, "The Name of the Wind', it's a fantasy book, this is the genre.

It's by Patrick Rothfuss.

So there's my first book, and then my second book, I'm going to add one that I read recently which is called "The Way of Kings".

Can you tell that I'm really into fancy book? That's okay as I said, it's up to you, these are my books you can have whichever one you want, so here, we're going to add another book and it's called "The Way of Kings".

And this one is another fantasy book.

Our library is going to be full of fantasy, and this one is by Brandon Sanderson.

So there we go, I've added two new books to the library projects, one called "The Name of the Wind" and one called "The Way of Kings".

Next, I asked you to add two new, oh, sorry, one last thing, you can see here, that not only do we have to create the books, we also needed to add the books to the library.

So I'm just going to do that as well.

Now, copy and paste is your friend here, I'm actually going to use the auto complete function.

So dot add book, "Name of the Wind", perfect, and then there's also add one for "Way of Kings".

There you go, so I've created my two books, I've also added them to the library.

Hopefully you were able to do that too.

Now, so the same thing, but I'm going to add myself as a user.

I'm going to say, Mac = user, but I need to give my name, and my name is Mac Bowley.

And then I also need to give an email.

Now I'm going to give an example email, I'm just going to say, example.

com, actually let's do example.

ca for Canada, 'cause I was actually born in Canada, so we'll do that.

All right, so I've added myself as a user, and I'm also going to add myself to the library here, dot add user.

So now we've added my two books out in the library, I have added me, and I've added me to the library as well.

So now let's just do one last thing.

Let's say, "Name of the Wind" dot rent out Mac.

So this should rent out the book, "The Name of the Wind" to me.

Let's have a look.

Cool, okay, so I've got an error here.

It says you have no attribute rent out, so all I'm going to to do to fix this, So I'm going to go into my book, what have we got? Oh, I see, so here I haven't got the underscore, but it needs an underscore.

So let's get back here and we'll fix this issue.

So I'm just going to say, underscore so "The Name of the Wind" rent out with an underscore to me, hopefully this should work this time.

There we go, look, I've added my two new books here.

And then if I say, Mac get current book.

Again, let's go back in so, what is the name of it? So, oh, I need more underscores.

So let's use the proper method name.

So get current book.

So again, so I've got book object.

Let's do dot get title.

We should see that "The Name of the Wind" comes out.

Use a proper name, there we go.

As you can see, I did encounter some errors as I was going through that, it's been a while since I wrote this programme as an example for you, but I was able to check through the classes, to work out what the names of the methods were that I needed.

So I can pause the video here, make sure that you've done all of these things.

I'm going to go back to main.

py, you've added two new books, and then you've also done this rent out.

That as well, that might've been a bit trickier, resume the video when you're done, I'm going to head back to the slides and you'll get your next task.

Right, now that you've gotten used to how using the library tracker software, I want you to add something to it, when you use some of the stuff that we covered in the previous lesson about inheritance.

Your next task is to add a new subclass of the book class.

So this library now wants to branch out and have two different types of books.

It's going to have fiction and non-fiction, so you can choose if you'd like to implement the fiction class or the nonfiction class, both of them should be a subclass of the book classes already in there.

I'd like you to add two new attributes to your new subclass.

And I want you to create getters and setters for those new attributes.

You head up to your worksheet, you've got all you need over there, step-by-step instructions to complete this task.

So make sure you choose either fiction or non-fiction.

Pause the video now, have a go at doing that, and I'll see you back at my piece of library tracker software watching my subclass so you can compare.

Here I am back at my project, so let's have a look.

So obviously I'm going to create the new subclass inside of this book.

py file, same place that my book class was declared.

So let's have a look.

Now, here is my new subclass.

So it was called a non-fiction, that was the one that I chose, you could have done fiction if you wanted to.

And then it's derived from the class book.

So again, we're going to use the inheritance stuff that we covered in lesson four.

I then declare my new constructor and you'll notice that I've chosen two new attributes, one called topic and one called year.

So what is the book about.

And also when was the book published, that's a bit more relevant for nonfiction books because it might debut the information that's in them, things like that.

So those were two important attributes.

So I've used my super keyword there to initialise the book class, and given it the title, the genre and the author.

And I've also, done, assigned my new attributes for topic and year.

So I've said self the topic, it was topic and self the year, it was year.

And I also created some getters and setters for each of my two new attributes.

Right, so I've done everything I was supposed to do.

I created a new subclass, I chose fiction, I gave it two new attributes, and I created getters and setters for them.

Now I also asked you to test your new subclass out, and let's do that bit together.

So the first thing I need to do is make sure that my new subclass is imported.

So I need to say find book and from the book file, and pull book and nonfiction, and then let's add a new book.

I think I'm going to do one called "Atomic Habits", which is a really good book about building habits and how you can make change, that it's buying an exercise routine.

Something I really enjoy this nonfiction book that I read recently.

So then I'll use the nonfiction keyword to activate the constructor, and then need to get it, the title, is called "The Atomic Habits".

It's a self help book I suppose that's okay, on prime of that, so "Atomic Habits", self help and then the author is James Clear, so his name.

And then I also need to give it the two new things.

So the topic is Habit Building.

It was released in 2017.

So I've added my new book.

I've given it all the things I need.

It's got the same as these books up here, except it's got my two new attributes as well.

I'm then going to add it to the library, so I'll say school library dot add book.

"Atomic Habits".

That should be in the library now, and if we look, if I run this programme we should see it pop up, there we go.

And then the final thing I asked you to do, was to rent it out, so let's rent that out to Jane Doe.

Remember, I need to underscore that.

And then we're going to say to Jane.

And then just to check if it's works, I'm going to print jane get current book.

It's not how it was written.

Try use the auto complete feature.

Dot get title.

Hopefully, let's just double check that.

So get current book, yep and then get title, perfect.

So my test is all ready, so let's run it and hopefully we should see that this prints out, and then I should also get to see "Atomic Habits" pop up.

Perfect, 'cause that shows you what Jane's got.

That's it all done, hopefully now, if you want to pause the video here and do these same tests on yours, just make sure yours works, remember first you need to import it at the top, and then you should be able to create your new book, add it to the library and test it out.

Right, let's head back to the slides and we're just going to do a little recap of everything we've covered so far in this OOP unit, I'll see you there in a sec.

Well done, that was quite complicated library projects, you did really well to get to grips with it and hopefully make a subclass of your own.

If you didn't do the whole thing, that's absolutely fine.

The fact that you were even able to recognise some of it, is the amazing bit of progress that you've made since the start of this unit.

Let's just recap some of those terms before I leave you, so that they're fresh in your mind and you can hold them with you.

Object Oriented Programming is a way of creating programmes using classes, and classes are collections of data in the form of attributes and behaviours in the form of methods, all bundled together as one.

When we want to use these classes in our programmes, we create objects, which are manipulatable versions of classes, that we can pull into our programmes and then access that attributes and methods.

So a class is like a blueprint, and then we create objects from that blueprint that we can manipulate in our programmes.

And those are called objects.

Class and objects hold both information and behaviours.

So attributes are the data that an object holds, it's variables, and methods are and objects actions, the things it can do, they are the functions defined inside of a class.

So data is attributes, functions are methods.

Finally, we discussed one of the principles of OOP inheritance, which allows you to derive one class from another.

The original class is called the superclass, and the new class is called the subclass.

And when you do this, the subclass inherits all of the attributes and methods that the superclass had originally, and remember it was a really powerful thing to avoid you repeating code, to allow you to organise your programmes into hierarchies, and also to be able to use the same methods on two quite different classes If they're inherited from one superclass.

That's it, well done for making it all the way to the end of Object Oriented Programming.

I've had a great time teaching you this lesson, and I hope you've had a great time learning about OOP and advancing your programming skills just that little bit more.

The last thing I'd like to ask you to do is both, to make sure you take the exit quiz on your way out, it's going to ask you some questions about this library programme, I think it will be good for cementing your learning.

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

I'd love to see all of the creations you've made through this unit, so share them with us if you would like to.

Anyway, that's all from me.

I hope you guys have a great rest of your term day, month, wherever you are right now.

I'll see you soon.

Happy learning.