GBC Class

    10:25PM Oct 29, 2024

    Speakers:

    Peter Sigurdson

    Keywords:

    object interaction

    program space

    non-deterministic

    Legend of Zelda

    UML diagram

    inheritance subclassing

    business domain

    polymorphism

    method overloading

    method overriding

    sealed classes

    abstract classes

    exception handling

    runtime errors

    compile time

    Yeah, so we said that there are three reasons for writing a computer program, numerical analysis, number crunching, right if you're doing science research or something, games or simulations which work with objects. And this can happen because, where this is possible, we put objects in our program space, and the objects can interact with each other independently in non deterministic ways according to the business rules and their methods. So this is possible because objects, the objects are program space,

    can interact with each other, Non deterministically according to the rules of their methods. I

    and that allows us to get a much more complicated, a much more complex experience of the program that would be then would be available if we were doing everything in a language like C and we had to explicitly code every single action the program could take. I remember when my kids were quite small, they they really loved this game. Was a Nintendo game called Legend of Zelda. And imagine all of you, I think at some point, have played some video games. So this character can walk around in the world space right at the the space of the program, and you can use the paddle controller to make them go left or right, up or down. You know, they can pick up the magic rock. They can pick up the sword. If they strike the sword against the magic rock, the sword can get another 10 points of magic power. Now, those are like already I've described a number of very complicated possibilities, and those possibilities are all spider webbing out from what the user chooses to do. So it would be impossibly complicated to sort of manage all the different algorithms, or all the different gaming outcomes that could happen if we didn't have objects. So I'm just going to quickly finish this up now, because now everybody is here, we have to move ahead on our lesson our lessons on whatever it's what's wrong with me anyway, we'll get to there in a couple of minutes. But, yeah, our lessons, we're doing interfaces, we're doing exception handling, but first we're doing, oh, we're doing polymorphism. Sorry, that's how I got into this. So

    so yeah, and the third one, of course, is to create which is our main focus here at college, here at school, where we're learning to do this for business, our main focus is to create software systems, programs, class hierarchies, communities of objects, which reflect, which are a delivery of which are an implementation of realization, I guess, in a sense, because they make it Real. The UML diagram that describes the business domain, I uh,

    anyway, we're now going to go on and talk about our, um, our main lesson points tonight. But I think it'll be much easier, because if we had not have sort of detoured and taken a little sidetrack to understand some of this background stuff, then let's say when I when it comes time to explaining what an interface is. For example, if you have a theoretical structure in your imagination sort of position these concepts, then it'll be much easier to grasp, whereas if you don't have this, if you don't have an orientation to what's going on, then you'll be disoriented, and you'll be reduced to just brute force, you know, memorizing the definitions of things, but you won't really have the technical intuition that makes you able to use it effectively in programming, for example, in interface, one of the concepts we're going to learn Tonight in interface, well, you already know, and I think, yeah, Professor Janelle did talk yesterday about inheritance and subclassing. Yeah, what's the purpose of inheritance and subclass?

    So, in other words, it's a way of expressing in the grammar of the computer programming language, just like in natural English or Chinese or Arabic or any language we have grammar, and computer programming is also a language with a grammar. So sub classing is a grammatical formulation which lets us express relationships. So I can say that I have a set of relationships in my business domain, which is involved in my UML diagram. So I have an employee, and an employee can sub class down to a manager or a worker. And a manager could be a line manager or an executive manager, and a worker could be a production worker, warehouse accounting, you know that they can be different kinds of roles in the company. So the purpose of inheritance now we understand is to express it gives us an expressive syntax, an expressive formulation. So another programmer looking at our code will immediately understand what's the purpose of it. And as we write more and more complicated programs, and we have the job of making sure that other programmers later can come in and support what somebody else did if we use and by the way, I'm recording all of this with my with my honorai. I'll print out the dictation later. I'll send it out to some people like to read as well as listen to things. But the purpose of inheritance, and by the way, there's another thing we're going to talk about later, called composition. There's there's two ways to wire up objects so they can talk to each other. There's inheritance, so there's composition. And if you understand that, then you understand when you're making a method call, what's the right way to structure that method call. Or if you're using inheritance, I can inherit, let's say if I'm a type of object called an instructor and I sub class, maybe we have an abstract super class called a perfect instructor, and I sub class down to just like a regular work a day instructor. So that is a sub class, and that's an inheritance relationship, but I don't have to just inherit from a class. What's a class? A class is a information representation which contains data, fields and and methods, it does not contain unique identity. When object contains unique identity, we make an object from a class. Now we go from class to object, we get a unique thing in the world. So a class, in a sense, is like a meta or a sort of a, sort of a spiritual or like a platonic description of something that exists in the world. So anyway, I can make a subclass in my class hierarchy. When I say class hierarchy, that's just a UML diagram. When I'll draw a few pictures on the whiteboard later, but a class hierarchy is just a UML diagram that shows all my classes and how they connect to each other. To get either thing with me, you can connect classes by inheritance or composition. And there's another thing we're going to be studying. After we go and study our polymorphism, we're going to be studying something called interfaces. And interface is a super class, but it does not tell me how to do things. It tells me something needs to get done. It doesn't tell me how to do it. So what's the benefit of that? Well, let's say you're my Asia programming team and you're my European and this is the way things were at IBM, right? We get programming teams all over the place, and you guys need to work with classes that they are going to write, but they haven't written it yet. And you need to make your classes work with classes they're going to write, but they haven't written yet. So there's a way around that. As the information architect, I'm going to tell them that this set of classes has to obey a certain interface, which means it has to deliver certain methods. How are you going to deliver the methods? Well, that sort of depends on the exact executional context of what's going on when the method is delivered. So again, going back to The Legend of Zelda and video games, by the way, I encourage people to sometimes try programming a very simple interactive game, even if you don't have graphics and stuff, just a text console based game, because it is a very good way of drilling your thinking and understanding how to deal with things real time in accordance with, like, a user's unknown at design time. You don't know what the user has been asked for, but you have to accommodate it. So Zelda is walking around carrying their sword, and they, you know, pick up this magic stone, and they like hit the magic stone against the sword, and the sword now has some power, so Zelda can now go under the bridge, and the troll is going to show up and go, Why not letting you go across the bridge? Don't ask me why they always kill dragons and stories. I never thought that I like I've never been bothered by a dragon. I probably liked them if I met them. But apparently in video games, you're constantly killing dragons. So the point is, the Zelda hero might be able to kill the dragon if their sword had the right kind of magic power. Anyway, that's an example of where you might need an interface to specify, then a subclass should deliver a certain method, but how it does that? That's going to be when we're calling it. That's something we'll have to decide downstream anyway. Let's go ahead now. That's probably enough theory. Let's go and start working on our first workbook, which is inheritance. And I'm going to keep this open, and I'm going to go through the the lab work, but I'm going to sort of note a few theory topics here as we're going along. And I'm doing this because the college is sort of created. Was very good, like they do, very nice material. It's great. But I've sort of observed in past terms, sometimes at the end, or when I look at the final exam or the final project, I get the feeling the students may not have gotten enough of a sort of an intuitive understanding of the concepts. And with object oriented programming, there's actually a lot to get. So anyway, you know what? I'm gonna link this directly to your lab book. I'll put it up there in George in the Learn thing later on, but for right now, just so you have it, we'll also link it to your lab book.

    So okay, here we go. Yeah. So this is what is in your maybe a couple of you joined us a minute or two late, but I linked this up to your um Slack. So this link for years and slack, I'll put it in the official learn web page later on. The Office wants us to do that as well here, I'll say, here is some, Yes, object oriented theory concepts to understand tonight's work. I

    right? So if you know how this works, if you just press the refresh button, you're going to go ahead and get the update to this so you know all that.

    So tomorrow morning, by 8am I will post quiz five. It'll be due by 10pm and things that I'm emphasizing on a lot will probably be on your quiz. In fact, I suppose it's not going to hurt anybody if I share out our dictation. Where's, where's my dictation notes. Now, here we go. I'm

    so you could, for example, go and chat with your AI buddy, based on the instructors passionate interests

    list, some likely quiz questions.

    So anyway, this is pretty cool, because it's going to help you. Later on, you can go and have you should take your own notes as well, of course, but that's going to maybe give you a because this is not simple stuff, right? And I do know that I teach it at a slightly more technical level than is perhaps common at college. You guys probably know that there's a certain kind of hierarchy. University is like maybe a little bit more abst, a little bit more abstract, and college is a little bit more sort of hands on vocational but if you're going to get out there and compete for jobs with other people, you got to have this kind of basic, I'm trying to weave in some basic, foundational thinking structures. So if you just sort of take notes and you remember and you train yourself to think about things in a very what's the term? I'm looking for a very structured way. Okay, what is programming, right? Think about that. Maybe that's a quiz question. What is programming, based on the type of discussion we've had here, you could say it is the construction of objects to implement a business domain described in UML, but that's not complicated, right? I think everybody can probably get their fingers around that fairly easily, and then that naturally leads to some other object or some other questions, such as, okay, so programming is the construction of objects. By the way. Here's one very important thing. I was going to say it before, but I don't know why I got off it a little bit. I have to mention this, I have to say I forgot my bag of pens at home. Fortunately, not there's a pen here, but we said that an object is what. We said that an object is, thank you. I love you. A box, simple, a Bora, you could say that it's a dog, because I don't have a complicated brain, right? My brain is not complicated. I'm a simple guy. I think about things in a very simple way. I can understand a box, dog box.

    What do we put in this box.

    In particular, this is something I haven't taught you yet, but we said that one of the major things of programming, one of the major applications of programming, is construct communities of objects and

    which deliver, Which realize, which make it happen, the business domain and

    described in UML, described as a UML diagram, or as a series of UML entities, as a connected topology of UML entities, connected topology of UML entities.

    Now a business object is a UML entity, a business object,

    is an engine, in a sense, which Pro which runs some of the processes of the business, which runs some processes of the business. Like a bank. You walk in there to open a bank account. They take your information, your si n number, and all that, and they put it in the computer. They give you a debit card that's a set of processes. So open a bank account could be a business object. So the thing that we put in this box, which is an object box, thing that we put here is you is business objects.

    So an object in a in a Kotlin or in a JAVA sense of an object, an object is a container into which we put business processes. Everybody repeat after me. That's the reason why programming is so massively effective in a business context to develop business systems. When I come out and say it. I know your first impulse is the sword. And say, Jesus Peter, drink less coffee. You get excited about stupid things, but if you stop and think about it people, you'll realize that's a very profound insight, like why exactly is and I've asked right? I played with people at job interviews. I have friends my IBM days. They run headhunting agencies now, sometimes they buy a day of my time. I'll come in and do interviews on on people that are looking to place in 19 positions and so on. And one of the things, one of my personal pet peeves, and I'm telling you guys, never ask me when I'm teaching something. Never ask me, Is this going to be on the exam? I think that's going to be the destruction of civilization people who are just studying things because they're going to be tested on it. So somebody is like that if they don't have insight into what they're doing. So I play with them and I activate them. Or some, one typical question I might ask a candidate, somebody I'm interviewing is, um, why do you suppose it is that writing software is a valuable thing to do in a business. No means that's such a simple question. It's so foundational, it's so far down the stack, they sort of look at me like they're glitching energy information. Okay, good. What information

    we how do we manage it?

    Literally, those are the kind of noises they make. So what I'm encouraging you to do as a result of the things I'm saying tonight, is train yourself. And it's not a very hard thing to do. Train yourself to reduce into very simple formulations, and then just take a paper and just draw the connection of things with each other. Because if you can do that, that is a very fast, simple way of really learning things down to the bottom. And as we get into the remaining days of our class here and next week, getting in on whenever it is two weeks from now, whenever it is, we're getting into Android, you're going to find this is a very powerful way of visualizing things, because if you don't do this, and the only other alternative is you're going to have to brute force yourself to memorize things, and you don't develop intuition. All right, so let's get this finished. You

    Yes, all right, so here's where we were now. So those are our lecture notes, polymorphism, exception handling and interfaces. So here's our stuff on polymorphism and polymorphism. I

    everything I'm saying is described out here. But yeah, so polymorphism, what's the sort of philosophical meaning of it?

    Objects have methods

    and methods contain implementations of algorithms. You should type this stuff out software. Systems are constructed by connecting objects together.

    And the two ways we can do that, as we figured out, are either by so by composition, we'll talk about later, or by inheritance, if we

    so now, polymorphism belongs to The side of this diagram, which relates to inheritance. I

    Professor Jenelle taught you that inheritance means to sub class. I

    create a class which is a small scale version of another larger class I

    and therefore in the subclass, we're going to have access to all of the methods and All of the data fields of the parent class I'm

    now consider that the way the sub classed object is being used, and the examples of it where we're going to get to the coding in a few minutes, I just want to give you a sort of a generic one size fits all description, and then you can make your own distinctions later on as we go through the code. In general, I

    we don't know, or we may not know. We may be agnostic. We might be without knowledge. We may not know. We won't know at design time how the subclass will be required to behave. Zelda is walking around this enchanted forest has a sword, perhaps the sword against the the the ancient tree, and as a result, the sword now gets some magic power. And the sword can now be used to cut through the the underbrush to get to the other world, or whatever it is. Programmer can't possibly know at design time, all the things that the player controlling the paddles might have been doing with their character, walking left or walking right, tapping the sword against the magic stone or against the Magic Tree, cutting the web between the worlds or cutting footholds and the rock to climb up the mountain. So therefore, when we say that we don't know how that sub classed object is going to be used, we really don't know. It's really time to start writing code. I just want to finish this little description, because some people are big picture thinkers and some people are fine, great. I'm a fine grain thinker, right? My way of doing it is, just show me the code and I'll figure out everything else by itself. But I've gotten feedback in previous terms of teaching, a lot of students have said, well, until you give me a big sort of existential picture of how this thing exists in the world. I can't really process the details. So if you're a fine grain thinker, and you're sort of biting at the chomp to get to the code, it's coming. But just let's in the respect for our brethren who need to see the big picture. Let's take care of them first, how the sub class might be used. So therefore the programmer must, or is responsible for providing implementations, for providing situational context, appropriate situationally, appropriate

    method implementations,

    the subclasses I

    it now that is a very complicated thing to do. It takes years of experience to sort of spider branch out to spider web out all the different ways to do that. That's why programmers get paid a lot of money, because you're not going to get a job, or you're not going to get a well paying job writing for loops and if then statements being able to understand the semantic reality of the business domain, you're encoding the code. That's why we say that as a new graduate, your first responsibility is to determine, decide on a business domain you want to specialize in, telecom, healthcare, government, public services, financial services, FinTech, because it does take about five to 10 years to really understand that business domain well enough to be able to Do that and make that happen. However, the rewards are, those are the people who get four or 500,000 voters a year. Alright, so anyway, let's just get started on our first lab exercise. Now, then we'll go for a break and carry on. It's coming up at about seven o'clock. See, here is the formal description from the textbook, every textbook ever written. See up here. This is what Peter says about polymorphism, what the general world wisdom says is polymorphism means many forms, allows objects to take on multiple forms while sharing common characteristics. Who here understands what this means? I'm putting it here because it's from the textbook. I suppose I'm responsible to put it there. I read crap like that when I was a student. What's wrong with me? I understand that, and now, as somebody who teaches for a living, I realize there's a lot of stuff. Just because something's written in a textbook doesn't necessarily mean you should take it too seriously. What I think happened is that some time back in the early days in the 1970s or something, somebody was tired or bored, or maybe they had a few too many drinks or something, they had to get the textbook to the publisher by midnight. So they just say they wrote that, and then other people throughout history even copying and pasting it. But anyway, that's the this thing about take on multiple forms while sharing common okay, if you say so, my definition is polymorphism means that you're responsible for coming up with a good, meaningful way to provide a method implementation based on the context that you envisage it being used in. And if you don't know how, if you're at work, and you don't know that's when you go and talk to your business analyst, or the person who owns the relationship with the client that you're developing the software for, you can go and ask them and say, Well, I don't understand your business domain well enough tell me, and then you describe the situation, and you get the that that's the job of the business analyst on the programming team, by the way, to answer questions like that, what is the appropriate thing to do in a given situation? All right, so let's go now and find our first lab, which is right here, method number one. So there's two ways of doing polymorphism. There is method overloading and method overriding. However, based on the way some of you guys are looking at me, I'm getting the feelings this would probably be a good time for coffee. So let's go, and then when we come back, I'll stop doing so much talking, and we'll just do coding, and we will find out that when I sub class from a super class, what do I get in the sub class? And data properties, so properties and end UPS very good, and we said that the methods can change according to what's situationally appropriate. There's actually two ways we can change the methods, polymorphism. There's two ways of doing polymorphism, method overwhelming and method overriding. And this is sort of like the cliffhanger you know, when you've watched all season of Star Trek, and then the last episode, you mean the enterprise of the vote, you get destroyed. And that's when they say, you know, come back next season and find out what happened. I'm not going to tell you the end of that, or else you won't come back. Rate. So if you want to find out what overwhelming and overriding are, you have to be here. And let's, let's give it 20 minutes. 20 minutes seems to be a good, comfortable time. I don't know if this elevator is working yet. I hope it is. But anyway, let's resume instead of 20.

    Whoever took my pen give it back. This isn't fun. You.

    Ah, sound my pants. George was hiding it from me back there. I

    What's up

    so in our state, yeah,

    30% of this. So I don't know what, what? What's wrong with the course project?

    I think you would, I think Professor didn't. Because I, first of all, I generally give people that actually, if I can ask you, send me a screenshot or something of this. Because, first of all, I just finished on the assignment, and my head is really spinning like it's very, you know, to figure out, because think about it, when you're marketing to somebody else's work, you

    to understand everybody's unique way of doing it. You have to. So the bottom line is, I might have marked that I don't remember doing it. It's possible that I did. But if I can ask you to send me an email and out, because I think Professor Janelle did that, but if I did it, then I'll in my email answer why I did it. I just because I don't think I was the one who marked that. I think I did the assignment she did. If you did it, yeah, in submission, yeah? Actually, I submitted two files, yeah? So I always look at the I look at the dates, and I look at the later one, yeah, this one. So just I submitted this one, yeah, look at the later date, at the most last one that was uploaded. Yeah, this one, yeah, that's right, yeah, yeah. I always do that. Never Yeah. About that project thing. Send me an email, because, honestly, I just don't remember. Maybe I did mark it. I think Janelle did the projects. I think so. But send me an email if you did sure, like, just doing the message in a select or just, like, set, yeah. Or just, well, if you want slack is good as well. Just send me a screenshot so I see you. Hey guys. I need some coffee. I'll be back in a couple minutes. You

    All right. So hopefully you got something more or less like this. By the way, I updated your lab book so you can go and you can either play with this a little bit by yourself, maybe using the auto code completion feature, you just copy and paste it directly out of your level or updated.

    So vehicle polymorphism demo. I

    mean, that's I lost it. Now, here we go. So we're treating our vehicles here. So we're treating a generic vehicle with its constructor, car with its constructor, and diesel truck with its constructor.

    So giving you a couple of

    minutes to copy that code over and run it and play with it, maybe the debugger. You have any questions, ask me about it in this code, because we're going to go into slightly more complex code formulations, and we will spend more time on them. So here, for example, we're making a mutable listed vehicle, and we remember the formulation we used for generic when we put a list, then we put a data type in angle bracket after the name of the mutable list. That means that it's a generic that means this list will only accept allowing vehicles in it. So here we have a method for adding vehicles. We can start all vehicles. So we're using our collection, right? So we're using because we know that vehicles is a mutable list, therefore we know it has a for each iterator, therefore we can walk over it and the iterator we can call start engine. Now, remember what I said when we were talking about generics here? Maybe I can magnify this up background. We don't see it too good. Want to make an important

    point here. So we said that one

    of the benefits of generics, and I think the example I probably use, because I always use it, is a bookshelf. And on this bookshelf we can only put cookbooks. So here we have a mutable list, and we can only put vehicles. Therefore, when we walk over the mutable list, the one thing that we know is true is every item we pull off the list which we're accessing via the it or iterator variable, right? We didn't define that was something that was handed to us when we chose to use a generic so we walk over every to object

    our

    list, and we know that every object has a method called Stop engine. Why? Because the thing that's allowed in that list is a vehicle, and we know what methods vehicles have. So anyway, everybody got that going, I guess. Let's move on to our next one.

    Now. So here we see examples where certain kinds of vehicles or certain kinds of sub classes will have a method not shared by the other. So an electric car has a charge battery method,

    see, so we put that only in the electric car method. So

    a truck has a load cargo method, which is not something a passenger car you have, so we go and look in truck,

    old cargo.

    Anyway, this is something you really you know, maybe we'll end the class a couple of minutes early tonight. We'll wrap it up at 920 or something, give you time to go home and sleep. Then in the morning you can play with it. Because something like this, you sort of have to play with the code by yourself, ideally, with the debugger me, sort of reading it to you and talking through it is probably not the most you know, beneficial learning strategy here. So here we've made an array of generic vehicle, and then we're iterating over that again. We're starting the engine, and when we run that, let's try running it again. We'll see how the polymorphic behavior of the language is calling the appropriately defined method for each vehicle, All right, so let's run that and observe that. So

    I don't think I'd put the output there. I'll copy this just if somebody wants to see you up and they're studying

    it. Sorry. Yeah. So they're starting all vehicles, for example, generic vehicle, electric motor, diesel engine, all right, let's move to the next one. Now this is the first couple of examples. Were just very simple code mechanics. Now we're getting more into the area of modeling the way a business behaves in software. I didn't put the UML diagram here.

    Well, actually, let me show you something.

    So I'm going to take the code, and I've shared with you in previous classes some of the things we can do with chat GPT, and one of my reasons for doing that is to motivate you and make you believe what I say when I tell you now more than ever, like since I was your age in university, they were telling us about the importance of getting deeply grounded and steeped in a business domain. But now it's more important than ever, because the one thing AI agents are still not good at is gaining human insight into a business domain, but writing code mechanics theory that right? So unlike you know, even five years ago, if you were a decent coding mechanic without an idea of the full that's why I'm emphasizing philosophy and semantics more now than I ever did before. Because if the only thing you can do is work with the mechanics of code, that's no longer going to get you very far. Let's take this code and go over to chat. GPT actually Claude AI is much better for doing this. A lot of you have to pay for that. People. A lot of people probably don't have that. So I'll stick with something that's probably commonly available to all of us,

    and I'll go to Mr. Spock, which is a specially trained AI I've been working with for this stuff for Spock. Where are you? There we go. I'll say, create a UML pass interaction diagram. You guys all used your time wisely last week. I hope power points on UML agile. See some people nodding? Good.

    So anyway, the benefit of doing this, first of all, this is a very simple program, and if you have a pencil and a blank, blank piece of paper, it shouldn't take you much longer to draw out the UML diagram on paper than than to get this to do it. But this is to just illustrate the importance of having insight into the operation of your code. So here is my class interaction diagram, showing me the shared fields between the classes, and another thing I could ask for is show the object interaction diagram I

    anyway, once again, this is a trivially simple example, but imagine if you were doing doing something that had some complexity. Maybe there were dozens of objects, maybe a business domain. I me that you're not familiar with this. This could be quite, quite beneficial. All right, let's go back and keep going now. So let's get our code working for our E commerce system. Join me now and dump this into Kotlin. So but once again, if I was doing this for real, let's say if I was a student and I had to make some kind of an analysis or some kind of an assignment or something on this, I've told you, my preferred way of working is with index cards, those colored index cards you can get at the dollar shop, get a pack for $1 I would write down on each card, I would write down the class its methods and its data fields. And then the nice thing about the cards is it easy to move them around and quickly create a feel for how the classes communicate with each other. In fact, I don't think IntelliJ does this some IDEs, actually, I think, um, think Eclipse does this at least. The last version I checked, did you can put your class hierarchy? This is the class hierarchy, by the way. Remember, we were talking about this concept of class hierarchy and a community of objects connected together by method calls and field calls, uh, a shared field interaction. This is what a class hierarchy is. So let's copy this dump that into your Kotlin. You don't need to make a new project, just replace whatever's there now you

    unlike if we were doing Java, for example, in Java, public class name has to be the same as the name of the five historian Kotlin doesn't really care. So that's why we easily reuse this project. So I'm going to do control, a delete, paste, save, and where is my I'm don't think of the main method here. All right, sorry, guys, I think I didn't copy over this completely. It's gonna jump back to my GitHub for a couple seconds now you by yourself, see if you can get this running, and if you can generate your main method, just in the interest, because I've still got a bunch of stuff I want to do. I still want to get into um, exception handling as well. So I'm just going to jump over to my GitHub to pull down the proper code that I need here. For example, I don't have a way to run right. I'm not seeing my run button. Why? Because I don't have my main global function that does that. So let's go over and Get that I'm

    all right, so I've updated your lab workbook with the main function to get this running. So go over there and replace either just that method or the entire code, and let's observe the operation of this You all

    right, let's walk through trace through the code and see what we're

    doing. You.

    Well, actually, here's, yeah, sorry, here's where I work. I was wondering why I didn't put that so in the exercise below that, I gave it as an exercise to the students to go ahead and generate the main function. Anyway, you've got it now, So

    don't do that. You

    so here we have our base product that everything is super is um, sub class, and from it is telling us a product ID, a name and a price, and it's giving a method called Calculate tax, because that's the same for everything. It's just basically the tax rate applied to the price. And everybody has a display which says our product name and price. So here we have a digital project, sorry, digital product, which is sub classing from product, and it's adding certain things here, and it's overriding, in this case, the tax, I guess, because in this jurisdiction, if something is electronic goods are taxed at a different price rate.

    And because this is a digital product, the delivery is by download. They're also adding a method for downloading the product. Here we have a subscription product.

    And our subscription product, once again, it's introducing some additional factors that aren't relevant to a generic product, such as the monthly price, duration of the subscription. And I guess there there's a different tax, so once again, they're overriding the tax.

    Here's our shopping cart. So class shopping cart, we have a mutable list of product. Now look here. This is actually a very strong example of why generics and mutable lists are quite useful, because we can make our Z No, I just want to say this in the right way. Pay attention here.

    I hope, if I say this in a clear way, the fact that we're using inheritance and subclassing is why we can put different product classes, different product objects in the same mutable list. So the fact that all our products, digital, subscription, physical and so on, so

    are Sub classed through the same what's called ancestor hierarchy and

    so we can say sort of official computer programming terminology. We can say they inherit from the same, from the same, they descend from the same ancestor hierarchy.

    This is what allows us to put different objects into the same generically typed, mutable list. Because some of the things in this list are subscription, some are physical product, some are digital product, but we can put everything in the same collection even though it's a generic because of this nature of the ancestor descent you

    and one of the factors there to bear in mind is because we know that for their from The same ancestor hierarchy, because the highest the original ancestor, which was product, had

    a

    had these methods that we want to work on, such as add item, so anything that was common to everything In the original ancestor can be used here on classes that are in our generically typed list. So let me drop this into your into your notes. I'm

    all right, I'm not going to do this notification exercise here. I would encourage you to do it later. There's another challenge exercise we will spend a few minutes doing, but I also need to get it's eight o'clock. We've got about another two hours, and I also need to cover off at least on handling exceptions. Honestly, handling exceptions. There's not that much to it, but we need to get that done. And then I told you, now we might get started on interfaces, or we might put that off till tomorrow, depending on how this goes. But please do what I'm saying here is, please do this at home. This is a homework exercise for you, and when you get it going, paste your screenshot up in the Slack channel so I can be proud of you. You

    now we've got a couple of advanced topics we're going to talk about in a couple of minutes. Honestly, they're not particularly advanced, but the textbook calls them advanced topics. So who might you but let's go back here and make sure we've covered off on all these factors. Actually, I think we have now. So create a main function. All right, we did that together. I already gave you the main function. You

    e commerce system demonstration. Oh yeah, that is just the output from my code create a new type of product called some Oh, guys, I want you to do it by yourself. Now this should not take you more than about five or six minutes, and if you do get it bonus, and if you don't get it, then when I show you, my solution will sort of stick into your brain better. You kind of tried it by yourself. So I want everybody to try doing this exercise right now. Give you five minutes, and then We'll look at it together. You

    all right, so go and do this now.

    We you get it going, post your screenshot up in Slack. You

    so add a new type of product. So you're going to make something called subscription product. You're going to sub class from product. Actually, there's, there's not very much to this at all. You're going to set your subscription duration, monthly fee, and you're going to make an overridden price calculation or subscription period, and you're going to override your tax anyway, I'm going to stop because here's one big problem with a lecture in a sort of a coding class, if I talk too much, you just sort of Get into the thing of listening to me and the active processing part of your brain doesn't really do its job properly. So I want you to I want to see Everybody posting their solution. Go and do it now. You

    Ah, I see people solutions showing Up in the Slack channel. Great. You.

    All right. Wayne, good job. Nice. Very good I'll give you the rest of you guys like one two more minutes, and we'll move on to our next topic. And I posted my solution in the lab book, so after you've done your best wrestling with it, if you want to take a quick peek At my solutions over

    There. All right. David, good job. Nice. All right, folks, so I'm going to ask you to just take a note to yourself, just drop a little note your Google Calendar for tomorrow morning or something to come back and spend another few minutes playing with this. By the way, the way to get good at this is many small work sessions, not a few big ones, right? I know when I was a student, I used to pride myself on being able to sit down and just push it for four or six or eight hours, and I will later on. That's perhaps not really the best way to do it, because the cognitive psychologists, the people who study learning, say that you do 80% of your learning in the first 20% of your study session and the last 20% of your study session. So if you maybe make more reasonable, I don't know, 30 to 50 minutes or whatever, like, you know, maybe a 45 minute study, and then go and do something else, do housework, clean the dishes, go for a run workout, you know, do a couple of cups, whatever, then come back and do another 45 minutes that the experts tell us is actually a much more time and energy productive way of learning. All right, so let's carry on. So

    we're going to spend a couple of minutes to some advanced topics in polymorphism. To be honest, I don't really know why they're particularly advanced. They're just other examples of polymorphism. However, I'm just going by what the person who wrote the textbook said, and I suppose they're probably a pretty smart person if their textbook was accepted as our course textbook. So I'm going to defer to their judgment and agree that it is kind of advanced. So sealed classes represent restricted class hierarchies where all subtypes must be defined within the same file. They're especially useful for representing limited sets of possibilities. What exactly does that mean? Let's put it this way. There is stuff which is in the language in Kotlin, which I'm sure is important, because somebody's time putting it there. If you're going to ask me for an example of why this is useful, I really give you one, because I've never actually been in a situation where I've had to do something like that, however it's in the language, we should note it. So therefore, let's take a look.

    Maybe I've just led a sheltered life, and maybe, depending if you're programming in certain domains, it might be useful. Maybe at work, you're going to need this stuff sometime. I'm

    right? So

    let's drop this now. So I'll give you a couple of minutes to work by yourself. Maybe you want to keep the file you have if you did not get your previous, you know, self exercise you were working on by yourself. Just save that by name you're going to remember and open a fresh file, and let's put this in it. So I'll give you a couple of minutes to work on getting this done. Yeah, yeah. Sealed has to do with the good, good observation, by the way. So sealed and again, I'm not presenting myself as the world's experts, because I never actually worked with this for sort of you know, because I do contract it work in a number of languages, including Kotlin. I've never worked with sealed classes. But one thing I can think of where it might be beneficial if you had a very large programming team, and depending how you connected up the access in your GitHub, you might give this team access to certain branches in the GitHub repo, and that team access to others, and that team access to others, especially if it might be security sensitive. Maybe you have things like encryption algorithms for online banking or something, and you want to restrict to his access so I can sort of see where there might be beneficial uses of this. I just can't think of a way to make a simple example in the classroom. Yeah. Yeah. Exactly. Yeah. Yeah.

    Yeah, very

    good example. So that's that's a very good way of thinking through it. Great. All right. Anyway, I'll give everybody a couple of minutes to try and get this up and going. We have 123, yeah, we have three, so to speak, advanced examples. And then we're going to do something really fun, which is exception handling, yay. Well, I'll try. I'll try and convert you to my religion, my cult like religion of excitement. Okay. Anyway, I'm just going to be quiet for a couple of minutes, see how far you get going, getting This up And Running You

    What's up?

    So on this thing like I'm just finding when I'm trying to follow along with you, I'm getting lost. So like, this Coda thing, like, I find, like, it's just, like, this huge

    wall of text, yeah, well, that's just the output from the program. So yeah, I

    understand. But like, so anytime you're moving around, if I lose my spot, what ends up happening is, I find it extremely difficult to come back to a spot.

    I'm gonna start annotating it. I'll say, like, point A, point B, and so on. Then you can

    just hold up. I can,

    like, one, like, yeah, sorry about that. That's a good point. Yeah, yeah.

    I just like, so, like, before I was using like, you know, Command F, and I would write the stuff on the screen, but now there's nothing on the screen. So now I'm even trying that. So, like, even, you know, when we're with Janelle, she has, like, the page numbers, and it's like, would constantly be like, page 14, page 15. Oh,

    I can, yeah, that's a good thing. No, there's, there's nothing on the screen now, because I'm going into my own stuff, which might be a little, yeah, I don't know. I mean, I don't really have anything confidential, but I just, I turn it off when I'm

    glad, I'm sure, thank if I, like, lose my spot, yeah, for like, one second, I find it very hard to get back. I'm

    going to start, let's try bookmark, or whatever.

    We're working on a thing, right? Yeah, yeah. Where is it?

    I'm going to book all. I'll put like, a bookmark, and then you can just control your FY control, yeah, find your way. That's

    like, sure. I just Yeah.

    It's a good thing. I hadn't realized that. Thank you for calling out to my attention the

    previous one. Did you post the you said you were going to do yours?

    I will show you what is in the lab workbook. Let me just, where is that? Let me just finish what I'm doing. Then I'll show it to everybody. I'll be with you in one second. I'm actually just right now I'm in the Kotlin help documentation site. I'm trying to chase down George's question about when exactly would be a good way to use it. So since that was a good question, I've been wondering that myself. Let me see if in 30 seconds I can find that great otherwise, I'll come back to it later, and we'll go back and I'll show you what I just let me just see if I can find this here somewhere. Hang on.

    Yeah, I really like the idea now that you mentioned that. And sometimes I have trouble finding my own stuff in long Google Docs as well. Well, trying to think of a good way. And if you guys come up with something better than me. Let me know. We'll try and figure out a way to just tag it, maybe put, like, little annotations, little sort of, not really breadcrumbs, but little phrases, or something we can go and search for, like, lab seal classes, you know, point B or whatever. Yeah, let me. Let me work on that. I'll come up with something, right? Okay,

    there's no like checkpoints on this giant

    that's actually a good name for our bookmarks. We'll do that. Yeah,

    down, and we will document our progress a little bit more, and that's a good thing to be aware of. So that's great. Thank you. You just see what I'm doing here. Now I'm I'm actually having this because I was just getting my thing on sealed classes. I'm not sure where I put it, so Let me go and Find that. So I

    All right, we'll finish talking about sealed classes, and then we'll give it a little 10 minute walking around break. I require everybody you have to leave your wallet and your car keys and everything here, so I know you'll come back. I'm not letting you out of the room with with all your like, knapsack and everything. I don't want to be sitting here by myself. It feels creepy and lonely. Don't abandon me just because I got old. Yeah, pull on the heartstrings. That's a good thing. You

    all right, so

    first of all, somebody was, oh, there's a number of screens and play and so on. And some of the stuff is in Slack. Actually, I don't put any. The only one thing I did put in Slack was this right here to just focus everybody's attention that I wanted them to do the extension to subscription product. So aside from that, there's no instructional material in Slack. So you know, you're not missing anything there. So just relax. And you know, the only thing I put in Slack, really, at the beginning of the past, I just put the URL link to our lab books. Everybody get to it. So that's slack. And now, in terms of what we're working on right now, so with our kind of lab book today, which is the F 24 you know, fall 24 october 29 Kotlin lab book. So I want everybody now, let's go and look here at advanced topics and polymorphism. And to be absolutely certain, latest and greatest, you can just frequently reload the page, and then you can do Control F search advanced topics.

    In fact, I guess maybe periodically I should go back and do that myself to keep everybody lockstep together here, so everybody is now good, right? And if you're not good for among friends, right? You don't, don't be sort of shy or something. Just say, Peter, I don't know where you are. I got lost. And then I'll, I'll go right back to the beginning, and then we'll find our way here. But hopefully everybody is pretty good. Now, they did Control F in our lab book, and they found advanced topics in polymorphism. Then here. So here's sealed classes. So just this is actually something I just ripped out of the documentation site of of Kotlin. I just copied and pasted it over. So sealed class when you need to represent a restricted set of types or classes that are known at compile time. So think about them as like a Noom, right? You know what a Noom is? A Noom is a data type pretty much every language, Kotlin, Java, Python, all of them. And a Noom is a type which gives you a predefined number of options. It's like, if I want to make a traffic light, well, traffic lights can only be green. I never pay attention to traffic lights, by the way, so I'm not sure, but I think most other people it's like green, red and yellow. So I could make it a new which would give me one of those options. So here they're reflecting that sealed classes are similar to a new and I'm going to leave it to you to go and read through it by yourself. Here, it's not really an important it's not actually on the course outline. You won't be tested on it, but because people were asking about it, I will invite you to go and study. Ping me on slack if you have any questions. Honestly, I don't want to spend class time on it, because I I'd rather get ahead and see if we can get going on our exception handling. Or you can just go and, you know, Google search on perplexity, or whatever. You can go and search in the documentation. Seal class represent a sub class of class hierarchies, where all sub types must be defined within the same file. So you can't have some sub classes in another file and import them once again. The benefits of doing that. Can't easily think of a good example. So

    now abstract classes via interfaces. So once again, we're in our same main today's workbook, which is called F, 24 October, 29 kotland. So you could do a Control F and search for abstract classes. You

    US versus interfaces. Now, interfaces is another topic we have to address, and here we are at 830 we might get to interfaces, maybe, maybe not. We'll see how it goes. Now, an abstract class can have state and constructors. So an abstract class basically doesn't have data. An abstract class, well, actually, there's a couple of what I'm basically saying. I don't really want to go into this right now, because this is not really directly related to our main topic we were working on before. It's a kind of an advance. So we'll come back to this later. So we're not going to talk about this right now once again, because I want to make sure I at least cover off on exception handling. These are topics I need to develop more later on. I need to talk about abstract classes after I talk about interfaces, or maybe we'll get to interface. So pretend that's not there for right now. And similarly, generic types with polymorphism, we know what a generic type is, right? So a generic type is when you say that only one type of class is allowed to be put in a a the collection class like an array. And this just talks about the example of how polymorphism works if you sub class a generic type, once again, advanced. We're going to get back to this later. I'm

    so in terms of your quiz tomorrow and so on, if it's something we're not covering, it won't be there. And when we get to a situation for the project or whatever we need it, or maybe if we need it for some of our Android stuff, we'll go and address it at that point. I so anyway, we're now pretty much done and finished with the basis of polymorphism in terms of stuff you need to know about for your, you know, your quiz tomorrow. Let's make a little a summary of key topics. You know, for those people like to see that. So this is going to be way up here, way at the top of our lab book. So summary of topics or of items you need to know, least at this point, there's always more to learn later on, summary of key items you need to know at this point,

    starting polymorphism.

    And what I'm really saying is, in terms of your quiz tomorrow, they say we're not really supposed to get too close to telling students, you know, to study exactly to the quiz. But sometimes people have a little concern about, you know, it'll be pretty easy. You won't have any problems with understanding it. So you should understand this concept now, of abstract of class inheritance, that we can inherit, we can sub class from super classes and

    and we do that

    all of the baggage from the super class gets dragged down to the sub class, so all of The Supers data and methods are available in the subclass i.

    Now of particular import. So in terms of data fields, this is not very interesting, to be honest. If a super class is called a person, and a person has a data attribute called name, and then from person i sub class down to employee or manager. So employee and manager both get the data type of name. There's nothing very interesting about that. So in terms of data fields, don't really have much to say, but in terms of methods, we have to be aware of certain mechanical details of what happens when you sub class a method.

    So I'll just note here, in terms of data attribute or data, field subclassing. There's nothing interesting going on that needs to be talked about.

    But in terms of method subclassing, we do have a little bit to talk about. I

    so we must address the considerations of

    polymorphism.

    And there are two formats of polymorphism, exactly and often in the computer books, you'll see it just abbreviated as dropping that abbreviation in there, so you'll be familiar with it if you see it later. So we have method overloading

    and method overriding.

    Now in terms of method overloading, imagine it's the last day of class. So to celebrate, you know, I bring in pizza for everybody, and I know you guys like how you are, you love to eat pizza. So I'm carrying, like, this big stack of pizza boxes, and it's about to fall over on the floor, because it stacks so hard, I would really definitely be overloaded at that point, because I'd be carrying so many data attributes, which are the pizza boxes. So that must mean that method overloading refers to changes in the signature. So the name, right? The method name has to be the same, because the name defines what the method is. So you, if you go messing around with that, you made another method. So method name is, is the same does

    change,

    however, the method signature doesn't change in method overloading. So

    and in particular, the number and the type of the parameters can change. So in my Add method might have ADD int, a and b, right? So I have two parameters or int, well, an overloaded method. Let's say, for example, I wanted to maybe change that to do I wanted to make a division method, and I wanted to handle real numbers, so I might change that for floats. So the method signature does change. We can change the number of and type of parameters, input parameters to the method.

    And method overriding is when we change the implementation of the algorithm right change what the method is doing.

    I honestly can't think of anything else to say, like in terms of your spotlight items, your review from this lecture, your quiz items tomorrow. Can anybody think of anything based on we've talked about that I forgot? Yeah, that's actually, that's true, but that's for our first lesson on, um, on, on, on polymorphism. That's actually a bit advanced. I'm not going to ask that's a true statement, but because sometimes people have told me at the end of the lesson, they just like a sort of a quick snapshot of the most important stuff. What you're saying is true. I would not put it on a list of the most important things, but it's definitely true. I would say the most important things is that we have overloading and overriding and then it's just study the examples to develop your own intuition on when is the right time to use each one the only thing right there's no amount of talking I can do that's going to get it into your brain. You just need to study and write a lot of code. So study our examples, until you develop an intuitive comfort,

    or develop an intuition

    Yeah, yeah.

    Well, I can put it. I can put it on the quiz tomorrow, if you guys want, like, I'll ask you about runtime and, you know, compile time, typing and so on. Yeah, we can do that because George wants it, yeah. So study. So I'm just wrapping up one more thing here. Study the examples, so you develop an intuition on when is the right time to use either overloading or overriding. Do?

    And in practice at work, that answer is going to come from where, the same place that everything else about your program comes from, which is your UML design at work, you're not going to be designing the algorithm yourself. I mean, unless it's an extremely small company, you and one other person, Jim, it's like a reasonably sized IT team. There is going to be an information architect and a business analyst that interface with the customer. They design the program in UML, and then they give you the UML design and say, called this. So in practice, your UML, and even if you're just by yourself, you're a one woman or a one man organization, one person organization. Maybe you're a computer programming consultant or whatever. You should always, always, always draw the UML diagram and go from there.

    All right, so that's it. Now it's 840 and we need to talk about exceptions. Having said that, exceptions are not particularly difficult, but what I'm going is, do you want to jump into them right now, or do you want to have like, a little, sort of five pound get some water, stretch your legs, whatever you you guys. Tell me what you need, and I'll go by your thing on it. So this is a brand new lab workbook right now I'm going to pick up in Slack, just for now. I'll put it on the Learn page later, so you can go to Slack and access it. So guys, what are we doing? You want a break? Or do you want me to keep going? All right, let's give it a 10 minute break. It's 840 let's give it till 850 because I did something very smart tonight when I got my coffee. I asked them to put an espresso shot into it so I can go to sunrise, right? I'm I'm flying above the clouds now, but you regular mortals who don't have an espresso shot, just walk around. Get some, I don't know, get a Pepsi, get a chocolate bar, whatever you need, and we will resume in 10 minutes at 850 you

    or I mean, if you want to start working ahead, you can jump into the you can jump down and get our next lab workbook, lab book on exception handling. Yeah, we're definitely not going to be starting interfaces tonight, although I have a video on interfaces. I'll share my video out with you. I'm guessing, you know, probably last week, when you were on break, you probably had a couple of video nights. Friday night, you invited your friends over, and you you binge watched all my videos, so you need some new ones now. You

    all right, so I'm now putting in Slack our lab book on and I'll go and see if I can find my video on um interfaces content before I think it's no good idea to do what? No good question. I was just joking. I was just I was just messing you around. No, here's the thing. The reason I would not put that whenever we put something on a test or an accountability, we have to address the whole infrastructure about it. And this question about runtime and compile time, that is actually very deep at the infrastructure of how programming works. So that would be something maybe like in a third year, you know, university course on theory of computer languages, or something we would have to talk a lot about, for example, what does a compile language mean? How does it work? What is an interpretive like that would be, you know, probably at least two or 314, week courses, just by itself, to sort of build up. Yeah, so don't worry, I'm not going to do it. But yeah, just to let you know that would, that would actually be a very deep topic. No, I was just, I was just trying to get your your classmates to, sort of like, give you a couple of giving them these ideas,

    right? Where's the professor's channel? Ah, the professor

    in the US election, honestly, you know what? I'm just thinking, if there was, like, space alien, or somebody you know, some like, you know, from another planet watching this, like, what would they think of humans like this? I don't know, major country. You know, when you look at any of these people, it's like, if you look at a date here, like, you know, with three and four year old children fighting over whose toy it is? Like, these are actually the people who have the nuclear control codes, Jesus. And it's not a matter of one side or the other. They're both exactly the same. All right, what am I looking Oh yeah, I'm looking for interfaces.

    My video, and somebody took it. Give it back. It's in my other channel. See, the problem is, in the early days, I put things in different channels. Now I'm, I'm kind of confused on what's where this might be my Peter Sigurdson, champ,

    oh,

    ah interfaces, here we go. I

    You can watch it

    at home or Something you

    illustrate This. Let's make More.

    Class It came here.

    It's Like So

    all right, it's 8/5, let's carry on.

    Oh, the heart of a vibrant jungle, a unique Kingdom thrives. Seriously, I have 49 subscribers. Do you know how hard I work on these videos? You guys watch them. You don't subscribe. Now, if it's a video about, you know, girls dancing and all this, and you know this and that that'll have 1000s of subscribers. That more interesting than Kotlin interfaces. Somebody got to explain that to me, because I don't get it. But anyway, let's carry on.

    So the video is here. You can, I don't know, watch it tomorrow morning, or something. Exception handling. All right, let's keep going. It was a mistake to give them a break, like with with the exception of you, fought you, uh, you for hardcore guys. Everybody else ran away. I'm disappointed in them. Anyways, their loss. So this lab workbook provides a comprehensive introduction to exception handling and Kotlin, handling in Kotlin drill. So basically, what happens now, assuming you're doing something which might make a mistake and it might have some problem. For example, you create a class which has a division method. But what if somebody tries to say five by zero. Well, dividing something by zero doesn't work out too well, so that might make a runtime error, right? That might crash out your whole program, not good. So exception handling means you can look, you can take code that might potentially lead to some catastrophic result, you know, like, like crashing the system or whatever, and you can capture so instead of letting it proceed to do what's dangerous and unsight stuff, you'll you'll stop it, you'll prevent it from executing, and you'll prevent the program from crashing. Other examples might be, if you're going to connect to a database, or, you know, connect to an API on a network connection. Basically, whenever you're connecting to a remote resource, you should always wrap it up in exception handling, because something might go wrong. Right? The database may not be there. Maybe the password has changed and you didn't update the code. Or, you know, the file system is full, so you try to write to the file, but you can't, because there's no room left on the hard drive. Whenever you're doing something involving a resource that might not be able to deliver the service you need, you should wrap it up in exception handling. So by completing this lab so everybody is together, right? We're right here now at the top of our lab book on exception handling, which is in Slack. Anybody not finding it, let me know. I'll share that. So we're going to understand the purpose of exception handling, different types of exceptions in Kotlin, Master, try, catch, finally, block. So this is exactly 100% the same as Java, if you've done this in Java. Not anything to learn here, and we're going to see how we can create our own exceptions, and we're going to see how we can create defensive programming, which basically just like defense of driving you always assume the other person is an idiot, and you avoid them here. In terms of programming, you assume that if something can go wrong, file system is not available. Passwords been changed, network is down. There's any situation which could go wrong, you assume it's already gone wrong, and you take appropriate precautions. So exceptions are unexpected events that occur during program execution. So here would be the basic structure of a exception handling so you wrap up your code. For example, you know, divide you. You're running a division. You wrap it up here in this try block, and then you catch so if everything goes fine and nothing goes wrong, then it just executes normally. If there is some exception, you can capture it. You can maybe, you know, notify the user that they were doing something that isn't going to work, or take some other course of action. And then you could, optionally, you don't have to have the finally stands up. But if you do have it, then the finally stands it would always work. Would always run, regardless of whether the exception happened or not. So common exception types, no later exception. So if I'm, when would that happen? Well, assuming I'm, you know, trying to retrieve an object from a collection, or I'm trying to, you know, access some sort of, some sort of a data variable, it's not there, that would be a null pointer. Exception, illegal argument. Exception, once again, dividing by zero, you know, might be an example of that index out of bounds. Exception, so suppose I have an Iran array has 10 elements in the array, but I tried to access the 15th element right index out of bounds. Exception, number format. So if I had a number, and I maybe said something like 7.32 or whatever. Well, you know, that's not a valid number, so that would be a number format exception, or IO exception. This is the big one I always run into problems with. So the database is not available, or it's down, or the password has changed, or the network's not available, or the API, you know, is not available, or the passwords not there, or my, you know, whatever, my accounts not working. So when you're trying to read or write resources outside of your own program space, that's inputoutput. So create a simple calculator function that handles division by zero. So once once again, me talking is not going to so I'm just going to shut up for a couple of seconds and let everybody, including me, observe this happening. So let's go and get that going. You

    it alright.

    So the code is running.

    Now, let's go and study and see what It's understand. So it's understand the semantic.

    News, here we

    have our divide function,

    and it does what it's supposed to do, which is, yeah,

    I was in a hurry or something. I was in a hurry or something. I was confusing this with another example, because obviously that's not dividing, that's converting maybe some sort of an integer to double or something. Anyway, we can go and fix that ourselves. We should be able to just

    say, return a slash b. So sorry that Michael class exercise to go and do it by ourselves. Here, I'll say, return a slash b. Oh,

    that could have been it. Yeah, maybe that was it. Now, that's true. Actually, the embarrassing thing is, I also teach about this term, and I should have caught that. You cannot, in general, divide and invite in a lesson four, Div two or something very cool. All right, so, but we're still gonna need to, um, oh yeah, sorry, guys. It's been a long day, and my eyes are a little bit tired. So yeah, that actually, thank you so much for catching that great. So that is doing the job. I don't know. I just didn't see that great. So let's try now, um, you have this in the case of the second one here, we should see the exception answer,

    yeah, so it's just given

    in that case. So here, that's the arithmetic exception. So anyway, everybody got that going? Let's carry on.

    Yeah, yeah. I was just thinking about

    that myself. Let's just go and dig into this a little bit more. So it's capturing the exception, all right, and it's returning zero here, which I don't think it

    should be great. Oh, okay. Well, anyway, let's um instead of saying cannot divide by zero, which we're actually not similar. Yeah, oh, you mean in terms of math. I know I've been following that, believe it or not, I was very close to when I was doing my undergrad. I actually got a degree in math, and then I looked at the kind of pay for people with undergrad degrees in math, I was like, Jesus, this is no. Then I put two more years. I got electrical engineering. But I've actually been following that, yeah. So that's the final that's the final that's the final arbitration that you can divide by zero. Cool daughter, Nikki, was in high school. She's won her PhD now, so she went into the academic thing, but she really math, and we talked about that a lot. So, but anyway, let's play with them. What's gonna happen? First of all, what happens if we get rid of this? Well, a method always has to return something, right? So if we're gonna return something which is a tight double and we don't, let's experiment. Let's hack around and use the debugger so save see I'm actually getting an error right there. That's going to say, Well, you're not returning anything, and you have to, all right, so that's not even a runtime error. That's a compile time error. And here, see if we try printing out our exception E. So here I'll print E, whatever the value of E is, whatever

    the output message of it is. All right, let's try that. I'm well,

    I'm just curious for all to see what happens, because I know what that would do in Java.

    It's been a while since I've messed around Chris, actually, I want to see what it's going to

    do. This is just for my own curiosity. So give us a couple seconds here. So save and run.

    Yeah, expecting

    error. Let's see what happens if we get rid of that.

    So that's our error message. We're getting infinity in that case, way, just absorb it. Make it part of your experience of what's going to happen. Let's go

    on to the next one you

    all right, let's move on to drill two create a function that converts a string input to numbers and performs calculations. So calculate from string. So we're doing our type conversions there. Now you see here, you're not letting the runtime determine the exception. We're explicitly saying by ourselves to throw an arithmetic expression, an arithmetic exception, and there's a bunch of exceptions. And I haven't worked a lot with exceptions in Kotlin, usually, the stuff I do is an Android and we can constrain the inputs the users allow, so you can make sure before you get to this point things are going to work. However, there is a large library of exceptions, and it's a good thing if you're sort of targeting. This is your career path you want to get into. You should go and practice with them anyway. Let's, let's drop that into our program. So starting from getting our calculated string all the way down to our main function, let's see what that's going to run as. So by the way, right now we're on drill two. So if everybody you know, if anybody needs me to help find where we are, or you can just control f yourself to drill two, that's where our thing is. So let's now go over to our

    IntelliJ and just paste it in. Save you. Okay, let's run it

    first from string, so we're giving some numbers and well anyway, let's just run and see what it does. So

    run you this

    case, we were given out numbers. The output was five, so no problems there. The next one. Now we expect, because ABC cannot be converted to a number, probably going to get some sort of illegal format exception, so save and run that.

    Please enter valid numbers.

    You see what's happening here. Now we're looking specifically to capture a number format exception, and if that happens, then we're going to print out this exact message. The alternative, we could, we we could tell the user something like this, although this would probably be a pretty unfriendly message to most users, so probably something more, more sort of like a pleasant experience. So let's see what happens if we just predict the exception that are caught.

    I'm are caught.

    Remember what we said on the first day that Kotlin is based on Java, and a lot of the underpinnings in Kotlin is Java, so they're getting a Java laying number format exception. All right, let's do our third example here. So calculate from string. In this case, we should get something relating to divide by zero,

    right there. So the second number

    is zero. Thrown arithmetic exception with this as the as the message that's going to come out

    with that. So try running that I'm

    so in that case, whatever argument you put there is going to be whatever questions or something. Again, there's not really much theoretical explaining. Even doing this, you just have to mess around with the code and the debugger and play all right. So we did drill one, drill two, multiple Inception types. I'm

    all right? Well, it's pretty much the same as what we saw before, but it'll just take a couple of seconds, not going to spend too much time explaining it now, because we're hopefully getting familiar with the oops. What did I do? I erased it, and we're hopefully Getting pretty familiar with it you

    Yeah, I think

    this is actually pretty much just a variation on our example before not going to spend time because we've already seen, um, we've seen all the stuff for us. Unless anybody has any questions, let's move on to drill three you I create a bank account system with custom exceptions. So here, for example, we're making, do you see how we're doing that we're making an exception called insufficient funds. And that is an exception because we are sub classing from

    exception. Same thing here,

    insufficient funds, invalid amount exception.

    So here, if we withdraw, if they're trying to assess zero,

    then we're throwing an invalid amount exception. If the amount is more than what they have and insufficient funds. So again, I don't think I need to explain that too much. Let's just observe this running. I

    am I getting there's a squiggly, or red squiggly under that, some sort of an error. Oh, maybe yeah,

    no, thank you. Oh yes, yeah, I

    did okay, cool. Thank you. Oh,

    nice. All right, so through this. All

    right, pretty good. So

    consider that if you were kind of doing a more complex program, maybe for your project, you might actually model in your UML capture from when you're interviewing your users or designing your requirements, you would capture some of the common types of things, conditions, and we talked before about defensive programming. So from beginning, you could create exceptions, describe the you know, so if a user or the system a certain kind of that with a customization, right? Very cool. So we did number four. Let's move on to three. Move on to number four. Create a function that safely processes a list of mixed data. Ah, interesting. All right, so let's get this going. Here we have a data class, which is our processed result. Here we have our processed items. Here we're using a generic, but we're saying it can be any here we're making a mutable list of strings, and then we're walking over our items, in our processed items, and for each item. Now here we're extracting out this. So we're walking over a list of 10 things, or whatever you're pulling the rivers, the thing off the top of your collection and your performance procedure. So here you could jump directly ahead and say something like processed items. Dot. You could do that right up here, but you're wrapping a try block around it, and you're catching potential errors, so it's just building up on stuff you've already seen. All right, so let's go and try running that I'm

    save.

    Hello, what did I do? Wrong? Title,

    Serializable list ending said, Really, I did that. Okay, let

    me just take a couple seconds here. I'm

    gonna turn off my screen share. What I want you guys is to play with this by yourself, drop a few debug points, step through with the debugger, and we'll see if we it's already 915 so we've got about another 20 minutes or so. Let's see if we can get through. I'm not even going to start on interfaces today, but if we can make it through our worksheet on on tritons, I think that's that would be pretty cool. So give me a couple seconds to play with This. You.

    Handling in Kotlin, so just press the reload this page button, and you can do a Control F to find back to where we are. Just Control F yourself, drill four, right? So you're going to reload, Ctrl F, drill four. And I just made a cup up here. I just actually looked in the instructors GitHub repo. Apparently, the instructor who did this deliberately introduced certain errors for the students to find and correct as a lab exercise. And maybe I'll use certain little things like that to do. However, for right now, here is the I'm not going to go through my I mean, if you want, you can go and compare what we had before with what the updated changes are. But for right now, given it's already 920 and I want to finish off our sheet, we have another two or three drills. Let's just observe it working. So we're going to create a function that safely processes a list of mixed data. The error we occur because process items function was expecting a parameter of type list, any the compilers and foreign are more specific type for list elements. And here, this was the instructor. I just pulled this stuff out. I think Mohammed was the guy who did that. He was stepping the students through fixing it. But let's just go and get this going now. So we're right here. So once again, you can control f yourself to drill for that's where we are, right there. Here's the correct scene. Something says, here's the corrected version of your code. So starting from data class, you can scroll down. And the way I do it, by the way, if you try to click and drag over a large passage of text, maybe it's just my mouse, it's a little bit it's been dropped too much, or whatever. I don't know, for some reason it it just jumps out, or it doesn't work. So my way of doing it is I click and drag over the first line, then I mouse wheel myself down to the bottom, I hold the shift key and I left click. And that grabs everything very in a very neat way, right? So I'll say Ctrl C, and I will control a, get rid of all of that stuff. Control B, to paste.

    Let's try running it. Save and Run.

    Yeah, yeah,

    exactly the optional thing? Yeah, you can maybe even as an exercise for tomorrow, or whatever. I don't know, depending on I'm not sure what your entity level is like, but I just want to stay focused on the handling. But definitely You're right. So what our classmate is telling us is, because we're working with an exception that can contain null, we have to put the optional symbol in there. So I think if you just dropped a question mark in, it would probably work the way it was before. So that's pretty good, but thinking All right, so let's right, so let's save

    and rest run.

    So here in process, four items successfully, it failed as sort of a Boolean world, right? So these run supported. So anyway, this is just now an example of catching exceptions when you're walking over a collection class, put that output there for people Want to study

    that I'm

    all Right, here we are drill five Network Operation simulation. Create a system that simulates network operations with timeouts and retries. So here we're making a class called Network exception, which is sub classing from exception and timeout exception. So we have a class called Network Service, which is basically set up to simulate the kinds of things that might happen if you're fetching data right? So we're making a fetch data function. So

    and we're calling our fetch data function. Where are we calling our fetch? Oh, right there. We're calling within our main All right, so let's go and run this so we're on drill five now, so you can do a Control F for drill five to get grounded and where we are, and Let's carry on. I'm going to copy from here. I'm

    write no compiler. So let's run

    test. One successful, two would fit failure

    and failures could be a timeout, invalid, Pearl, all right. So very good. Um, again, unless anybody has any specific questions, I'm not really gonna walk about it, because you need to sort of just tomorrow, in a peaceful mind with your cup of tea, whatever you like to drink, just pour over the coat and meditate and cotton play on it you

    all right, here are some practice exercises. Let's see if any of these are of particular sort of interest to us. Create a system that logs different types of exceptions to different destinations. Build a form input validator, Resource Manager, API response handler, exception hierarchy, challenge, exercise,

    create a comprehensive error handling system for our E commerce application from the previous lab. All right, very cool, but I'm not going to get into any of these right now. What I am going to do is, let's just write our little summary notes of what we've learned here, key things you need to know about, and then, with everybody's permission, unless anybody has any questions, if you want us to go and work on on some of those other challenges we can but first of all, let's capture a little summary of what we've learned.

    So we did pretty good, right? We covered everything we were asked to cover. We covered polymorphism and we covered exception handling. Those were the two items on the course outline for tonight. So yeah, I think we've done it all. I feel we've we've earned our our proper position and what we're supposed to be doing. So it's a good general practice. It's a best practice as a programmer,

    incorporated into your personal set of practices to liberally like to make your code sort of centric around exception handling. Always assume that things will go wrong, and if they don't go wrong, then bonus, you're ahead, and if they do, we got the gearing in place to handle it. Plus, what I found myself is when you write your code to be I say this, when you write your code to be robust in terms of handling exceptions, a side effect of that is you write generally better code. So it was a programming practice. Was a personal practice. I should say, am I saying? I don't know what I'm saying here. I wanted to say, make it part of your professional practices as a programmer, you

    make exception handling At

    the center of all the work you do you

    all right, so be liberal wherever it's appropriate to do. So wrap up your code and try catch blocks. If it makes sense. According to the situation, you can put a finally block there as well. Finally is something that will always run whether the exception happened or not. Wrap up your code and try catch finally blocks wherever applicable.

    And be creative in your use of decide to become an expert in terms of creating your own custom exception classes. So study and be aware of the various exception classes you can sub class from. So decide to become an expert, you

    says the side effect of making your program code generally better you

    Did I forget anything very cool, your quiz will be up there by 8am it'll be pretty simple, just three or four questions, you know, a couple of multiple based on the kind of things we've done

    So take off, safe trip home. See you later. Go and do some of those, some of those extra exercises that were in there. And if you get it going, paste it up in the Slack channel for the rest of us to celebrate with you.

    Bye, everybody here you

    now let's see if I can turn this thing off. That's the next challenge.

    I did it

    what's the things still on?

    Stop, stop, off,

    power off,

    hey, I did it. You?

    Bye, everybody. Safe trip. Here, see you later.

    Take care.

    See you on Friday, I guess was my next game. Here night you

    I'm,

    I don't know, some of the people in the class have been kind of voicing their like, frustrations with like, following some of your so I don't know they've kind of like a couple people, like, I just have been talking about it and stuff. It's just like, I do, I do, like, agree with them in some ways. Like, what I brought up today with stuff like, it can be kind of hard to follow, like, for example, like one of the things you on this summary here, like this to, like, applying custom example, custom exceptions. Like, we never even, like, really, wrote any of those. Or, like, really, it was just kind of like a copy and paste a block of clo code and it just, I know it's like, a tough way to, like, learn it. I know, like, yeah, hammer this stuff home the next day and stuff. But I just find that a lot of it is, like, a lot of copy and paste and a lot of, like, not really writing code. Okay.

    Well, that's the week. We definitely switch over to go, Yeah, that's fine. That's not a problem. I cannot make it to be more CO writing. Sure. I don't want

    to speak for everyone, people saying that and kind of, I mean, as you can see, with like, the attendance in the class, like, it's gone down to, like half the class, and like, those up, half of them leave within like, the first, like, hour and a half, which sucks, you know, like, I'm sure you want to be teaching for a class and everything, and it's but it's just, I know people are getting frustrated, and it's, you know, the attention.

    No, no, definitely. I can adjust. We can go over into more of the writing the coach here. That's, yeah,

    that's necessarily.

    We'll give it a shot. We'll we'll do the spin, and we'll see how that code so, so much for showing me

    that. Yeah, thank you. Oh, you bet I'll take care. You.