2021 Kotlin Crash Course for Beginners | Kristaps Cerminieks | Skillshare

2021 Kotlin Crash Course for Beginners

Kristaps Cerminieks, Lead QA Engineer

2021 Kotlin Crash Course for Beginners

Kristaps Cerminieks, Lead QA Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
13 Lessons (3h 3m)
    • 1. Introduction

    • 2. Download & Install IDE

    • 3. Variables

    • 4. Arrays, Array lists

    • 5. If statements

    • 6. For loops, While loops, Case

    • 7. Functions

    • 8. Nullability handling

    • 9. Classes

    • 10. Data classes

    • 11. Debugging with IDE

    • 12. Code commenting & Kotlin Style Guide

    • 13. Final challange - CLI app

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

This is a crash course about Kotlin programming language. Since 2016 Kotlin is the preferred language for Android mobile app development, which means that this relatively new language is becoming more and more popular. Kotlin is similar to Java, but at the same time, it's more concise and fixes what's known as Javas Billion Dollar Mistake, if You are interested join in! 

In this crash course we will check out:

  • variables;
  • arrays;
  • for/while loops;
  • if statements;
  • data classes;
  • functions;
  • propper code commenting;
  • nullability handling;
  • code debugging with the help of IDE

And at the end of it, together We will create a trivial CLI app from scratch


Meet Your Teacher

Teacher Profile Image

Kristaps Cerminieks

Lead QA Engineer


QA Engineer with experience working on different scale international projects. Experience in manual testing, team leading, and automated testing for various client apps. Preferred OOP language - Kotlin. Preferred automation framework - Espresso.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction: Hello, Hello everyone who has opened this video and want to earn what Katlyn. Let me introduce myself. My name is Chris stops. I come from Latvia. I'm Quality Assurance Engineer with four years of experience in manual testing, automation testing, and QA team leaving. And this course is about coffin programming language and it's targeted for juniors, or in other words, for people who haven't written a line of code and Katlyn or in any other language for that matter. So let's start with the prerequisites. There aren't that many. As I said, this is for juniors. So basically two things will be needed. Mac, Linux or Windows machine on which we can actually use to write our code. And we will use Intel injury ID Community Edition, which is free. Don't want a news. So yeah, that's basically it. You don't have to have any knowledge about object oriented programming or Katlyn, I will try to draw you are getting from ground 0. So you get the understanding of how this language and object oriented programming works. So yeah, the plan is fairly straightforward. We will install Intel, the j, and then we will go through multiple parts in our videos to understand the fundamentals of Katlyn. It's going to be variables, constants, race, classes, not the classes and methods reading userInput, outputting information to user using CLI or Command Line Interface or terminal. And we will talk about, and we will test nobility handling how Katlyn handled nobility. We will use and we'll try to take advantage of Portland style guide. And also we will understand what the best practices in commenting your code. So I Katlyn, I hump your screenshots from Katlyn official webpage. And this can be divided in three parts. First of all, it's concise. It's much more easier to read, much more easier to write, and much more easier to understand if we compare it to Java, for instance, here is an example of how we can create another class. It's okay if you don't understand the meaning of it at the moment. But the main idea is that in order to derive the same amount, in order to create the same data class in Java, you would have to write much longer and much more difficult code. If we compare it to Katlyn, that's what's important on this bolt moment. Once we will start to go through the coating itself, you'll, you'll understand more. Second thing is safe. It's, Katlyn is really focused to avoid all these null pointer exceptions. Also, if you know about them, that's fine. We will talk about this in depth later on. Basically. When apps are written in Java programming language, often enough, there were cases when APP crashes. That's what the end user sees that app crashes. I think most of you have experienced this, both for iOS and Android tour up and just quits unexpectedly. In most cases, this was placed on an Android caused because of no pointer exceptions. And it's also referred as the $1 billion mistake. But also about this, we will speak more in depth later on. And third thing is inter operable, which means that everything that's written in Java, and we can write and Katlyn as well. If your project is written in Java, you can start, you can start migrating to Katlyn. Even if you don't have the resources to do it in one release cycle, for instance, you can do it in the term of half a year or a year because you can have Katlyn files and classes and Java files and glasses. And they will work with each other because Scotland runs on Java Virtual Machine mastery, they read coffin code and we want to compel the next cue that it's being translated to Java and then it's being executed. Which also means that with cotton making news in Java libraries. So Gotham explained most of it, but still she was tough. As I mentioned, it's object oriented programming language. It was announced in 2011, which means nine years ago. But at 2016, inputs declared as the official Android programming language for Android mobile apps. And it's also an appropriate language. It's much more safer language than Java unwinding what's going on in the $1 billion mistake, as I mentioned before, much more easier to code and runs on Java virtual machine. Which helps a lot for this migration though, different programming language. So that's about it in short. What we will do in this course. And yeah, let's jump to the next video where we will install or IntelliJ IDE, and we will start to write some code. So thanks for watching and let's meet in the next video. 2. Download & Install IDE: Hey guys, and welcome to the first official lesson. After introduction. And in this lesson we will download and install or IDE or text editor. So as I mentioned, we will use IntelliJ ID. It's a paid product, but they have this Community Edition which is free for use. And it's really, really fantastic ID and it's really easy to use. And you will experience that yourself. So, yeah, let's just don't want the intelligence IntelliJ, Joseph, Google, Norman intelligence. And the first and the first link should be it, yeah, check out that JetBrains.com. So once it's open, use your operating system. In my cases, it's Mac and just choose the community edition and pause the video and wait until the installer is downloaded as it's quite large file. I already did it, so we don't have to wait. So I will just open it up. I will drag it to my applications folder. Shouldn't belong. Awesome. Now we can inject this, we can delete this. And now either we our Applications folder or using Spotlight search, we can open up our ID. And we pure right IntelliJ will find it immediately. So let's click on it. Let's click open. You can choose to enable notifications are not enabled them. So this is how it should look. Let's just create a new project. From left hand side, let's choose Java and then Kaufman slash JVM, which stands for Java virtual machine. Here for project as decay or software development kit. I read that it should install it as well. But if you don't see it or you'll see some kind of error here. You don't want to hear. Just by choosing the version. I. You can choose 15, the same one. I have. An vendor shouldn't be oracle. Just know on that and shouldn't be straightforward. Also, in the course description, I left some links so you don't want that VMM by herself, excuse me. And also installation guide should be there as well. But yeah, if everything is fine, then just leave it as it is and select Java and Coffin JVM. But next, now we need to find a way where find where we will store our project. I have created a folder just right here. So first let's decide in which folder we want. Save it. I will say it here. I'll click open. And the project name can be the same as the folder, my problem Junior. And we can press on finish. So the initial opening could be a bit longer as ID needs to index our project, but shouldn't be longer than a half a minute. And it looks like it's done. So on the left-hand side, if you don't see this palette, you can press on this project button and it will open it up. And here we can see the hierarchy of our project. And let's go ahead and in source folder, click, make, right-click and click new Katlyn file class. Here, please choose file and name it main with a capital letter and just press Enter. And in this file we will write our code except for the final task we will do together. So now to see if everything works, let's just create a simple function. Let's call it Main. So the keyword to declare a function is keyword fun. So right, that I will zoom in a bit and let's call it main. Functions are named with the lowercase letter and then CamelCase, but you will use, we will see that in the future. We want pass in any arguments and just open up this block and write the following print line. And let's bring the cliche Hello world. And you should see this grey, a round button and just click on it. Click on Run. It should take few seconds. But after that we should see our string HelloWorld being displayed in terminal. So it's compiling. The first compile is the longest one up towards going to be much shorter. And as you can see, it's showing her hello world. So our ID is set up and we can run our code, compiler code, which is great. So in next lesson we will start by taking a look at variables. So see you in the next one. 3. Variables: Hello and welcome back. In previous lesson we setup or IDE, and now we can start to code. So we will divide these lessons in functions so we can look back on them and using this project as a reference, we will add comments to understand what's going on and what's not. So lets just John Bennett. So first of all, let's create a new function. And we will call it wars. And yes, so let's add a comment to this function as well. So we know what exactly it does. Defining and using law. Cool. So before we start, talk about how we can declare them and change their volume, we have to talk about types. So we will speak about the most popular ones. There are a lot of them actually, but we'll speak more, some of them. So let's start with the first one, and it's on integer. And the keyword for integer is word, int. And it's hall number. And an example would be, let's say number seven. Next is gonna be with name K14. This is the same. And it's a decimal number. For instance, 3.14. The third one, it's gonna be string. We can declare it with the keyword string. And it's basically a text. And an example would be this text, which is the best. And the last one we will look at it is logical operator. Boolean. Keyword for Boolean is the same and Bolin. And it's logical. So it can be either true or false. Okay? So let's start by looking. So let's call this one declaring variables that can change the value. And the keyword for this. Is sorry, dipole. So to declare a variable, we have to start the keyword. After that, we define the name of the variable. So let's call. And we helped write the names both for variables and functions and M in camel case. So let's call this one. This is in afternoon, name follows the equal sign. We give this variable a value and this will be an integer. And that's why we can give it a value of whole number, for example seven. And it's straightforward for anything else. For example, this is normal. And we can give it a value of 3.14. Same for string. This is shrinking. Volume being best. For instance. Last, this is sorry or Bolen. And we can give it a value of true. So this is how we declare variables which can change the volume. Also in Katlyn and we can specify what kind of body will this will be by adding a column and keyword of the data type. For instance, in this case, it wouldn't be int. But I'm Katlyn is quite clever and it, and the id is graying out or keyword because it's redundant. If we hover over it, we can see that explicitly given type is redundant here. And that's because we have given already a value and a coffin. By looking at the order volume, can determine what type of variable this is going to be. So this can be room removed cases when we can use this. And it's actually usefulness is if we declare a variable without any volume. So let's declare a new variable. We'll call it mind. Let's call it my EMR. And we will give it type in danger. And that's beyond any volume. So in these cases we can and should use this. So we specifically specify what kind of data type this variable is going to be. Cool, and that's about money balls which can change the volume. Next, let's look at sorting. Typos on time. So declaring variables that can not change value, keyword for that as well. And it's exactly the same how we will define by their bowl, which can change its volume. Only difference is that the keyword is well, so this is int and just call it well equals sign and give it the volume. And exactly the same applies to everything else. So let's not write the same again, because I think the idea is clear. No. Another type of variable is a constant. And we cannot define constants and functions. So let's jump out of the function block. And that's the common here as well. Keep you declare variables as constants and keyword for that. So rain is constant, volume Constable. So to actually declare this, we can use, we can see that the over these injustice constant volume, now the naming for constants has to be in all caps. So my columns for instance. And then we can give it a value like this. The difference between constant while unwell or that are really, they're almost the same. The main difference is that the constants are initialized in compile time and volumes are initialized in the runtime. So they are really, really simple. Let's copy this one in our function so we have a reference for it. But let's comment this Fanon because it will result in an error as we cannot put constant values and functions. And now let's look at how we change the values and do what we can change the audio saw changing values. Okay? So we can easily change the volume of more type of variable. For instance, we can write this is end which we declared here, can see that it's now shown for us and we can simply add no value, whatever we like. And this will work. Also, we can add. Something to a variable and its value. For instance, imagine we want to add as library 12, add together variables value with another, with another number. So how we can do it? We can call or minimal followed by equal sign. And then we can write our variable again. And we can add plus sign and volume. So now we will add 0.86 to the value of our variable. And as you can see, the ID is showing us an info, which means that we can optimize this code and we definitely can. And we can see that replaceable with operator assignment. And if we press on it, we can do it. Just as easy barreling plus equal the width. Also for logical operators, we can give the same Or another way will that doesn't matter. Opposite volume. For instance, lets call or this is Boolean. And let's just say that we want this Boolean out to be the opposite. So we can write, we could write false. Because here we define true. But if we will change the value here, the pulse, then you'll have to change the value of here to true, which is not the best practice. So what we can do is actually call our volleyball again, what Emily again. And at the beginning of it. And we can add exclamation mark, which means the opposite. And now if our initial volume on Boolean was false, and that afterwards is going to be true. And we can just easily change this to true. And then this one afterwards is going to be false. So let's just check what we have done and if it actually works. So I suggest to print line takes initial volume of int. And in order to add a variable in our string which we are printing out, and we can simply add the dollar sign and write the name for what it will. So this isn't, for instance. So I would suggest and just copy this two times. Let's change at initial volume of double. And right here let's call double variable. And here the check Boolean. And let's try, let's change it to o n. Okay? So this whole print out or initial values. So it should print out 7.143 and true. And let's bring out the same variables after we did some changes to it, see if they actually work. And let's just change the word initial so don't get confused. Okay? And in order to print it out, we need to call our function so we can delete this land coal. And the way how we call functions is just by writing their names like this. So let's try it. Once, double-check. So initial value of integer is seven, which is true. Initial volume of double is 3.14, which is also true, and the initial value of Boolean is true. That's correct. Let's check out what's happening afterwards. So it changed the integer value tonight. And it's shown here, which is correct. The value of double now is four because we added 0.86, the war initial volume, which was 3.14, true. And Boolean is opposite of the initial volume. If initially o is true, the opposite is false. So this is how we declare variables and mol molecules. This is how we can change the values of it. And well, we are added. We can check out what will happen. If you will try to change a value to a variable which cannot be changed. So much just call or this is into L. And let's try to give it an undervalued like five. When we do it, we see this error. And if we hover over it, we can see that the model cannot be reassigned. And we can interchange or initially o to one. So it's changeable form, we just delete this one out. And also we can try to change, let's say we want to try to change this symbol to a string. This one your enables value to string. So it's called, what's called this is Boolean. And let's give it a string value. And we're also seeing an error which says type mismatch required boolean found string. So we can only add bold in values for boolean variable. Which makes sense. So, yeah, this is it about variables become checked out data types, we can have checked out how declared variables were changing all yours and variables, which values cannot be changed as well as constants. How we can add and change the values of them. And yes, that's it for this lesson. See you in the next one. 4. Arrays, Array lists: And we'll come back in another of our lessons. So in this lesson we will talk about arrays. So we can minimize this function here. And we can create new function. And let's call it arrays function. And let semi-colon. So it's clear. The planning and the planning and the thing using raise. Okay, so what are arrays? Arrays are basically a list of values. For instance, let's imagine. We would have to hold info, information somewhere with a list of all participants in some kind of lottery. For instance. We could define h member's name in a variable, but it wouldn't be tedious and it put take low time. Instead of that, we can draw the pint one array. And in this array we can on and all these values in all the names of the people which are participating in this lottery. Hope this example makes sense. So there are two things in Katlyn. There are arrays and there are lists. And difference between them is fairly minimal on res basically from fixed allocated memory. But ArrayLists have dynamic memory allocation. And let's check examples of both of them. So let's start with simple array declarations. Let's call it array declaration. We can write down for cells that array's fixed. Sorry, typos everywhere. Graze has fixed allocate memory. Okay? So we can define array using also the keyword in trios were runnables bar. And let's call this one array a. And if we want to add volume goes immediately to our array, we can write array of. And here we can add values like 123456789. Like this. Also, we can define an array with no elements inside of them. Similarly to how we define variables with no value. So let's call this array b, which is equal. And I'm sorry. If we define a variable without any values in it, initially, we need to specify the type of the values inside our array. So we write array with a capital letter. We can use this. Then here we need to specify what type is going to be integer, string, double, Boolean, and et cetera. So let's form this instance. Just type string equals empty array. Just like this. That's how we define arrays. Let's speak mode, re, least declaration. And also it's run-down for ourselves that array lists have, sorry, dynamic memory allocation. So all ArrayLists and by default are immutable, which means that we cannot edit them. So if we want to the final RLS which can be trained in future, we need to do the following. So let's start with the keyword what we type in, array. Police would give you this name. And we write mutable list off. So here we can define the type. Let's call the integer. And we can choose and or not the band values. If we choose to add values, then this time declaration is redundant. Because again, just like with variables, we give already some values, can determine what kind of type this is. So this is n. So we can remove this. Next. We can also define empty ArrayList list B, which just like for arrays, meaning that define that it's our array list, meaning to the point of type string. And now we don't write empty array, but we type array list. That's it. So this is an empty ArrayList B be which will hold in future only string values. Okay? And if we want to define immutable list arraylist, sorry, let's call this array list C. It's basically list. We can give also the same principle. We can give it a type, a type for the elements inside what's called Boolean. But if we actually write in some values, then this is redundant and we can delete this. Okay? So this is about the curation. Now let's speak about editing. It. Bought anything array values. So let's take our array a. And if we want to edit any of the value we have to give, we have to tell which element in array we want to edit. So we need to pass index. One thing to note, and what's really important in array is, is it starts with 0. So in this case, this value one has index 00, CO2 has index of one, and et cetera. So if we want to change the volume for first index, we pass in 0. And we send the new volume at say, nine. And this is how we ended existing array values. We can also add volume to store away. And how it's done is also similar to simple variables. What we checked in a previous lesson. We can call our array a and the news operator. This. To add new volume two or array. It expects a integer, so we have to send the integer and let's send 100. The same applies to arrive be indifference being that minute pass string because it's expecting a string. Let's give it first text. Now speaking about the ArrayList, it's really similar. So if we want to edit array list, we can call ArrayList. I'm sorry, that's in this one. We can specify the index 312. And it, let's just edit this time that last one to say, you know, 123 and the third one, and give it a value of 89 for instance. And if we want to add values, it's InDesign. I'm been differently. Let's use ArrayList B. Now if we want to add a value, we have to use dot notation. And and you can see we can add element of type string because analysis be expects only string. And we can pass in our string, whatever the case. Now, we will try to edit. If you will try to edit immutable ArrayLists like a rarely seen. Let's call it. Let's pass in an index. And let's show, let's say false because it's true already. We will get an error. And if we hover over it, we can see that if we want to edit it, we have to change the ArrayList C, two mutable list. Because if we declared it like this, it's immutable, we cannot change it. So we can just comment this one out. Now, let's look how we can iterate through ArrayLists. And this is really simple. A lot of programming languages you have to write four cycle for low sodium, which is, it's not that difficult, but if we compare it to the methods that Cotner has, then it seems difficult. So let's see actions with arrays and array lists. So on each array we can call something ArrayList personally. Let's call array a. And on this, we can call this super, super cool method called for each. So what this does, this we'll iterate through every element of array a. And in each of this cycle, we can determine what we want to do with this element. As you can see here, it's the way how to refer to that element in that specific cycle is using keyword it. And we can already see that it's an integer because we call it on our array a, which has only integer values. So what we can do just showcase how porch we can in each cycle, print line. And we can pass it, it. So in it, it should go through all array and print out the value. It's cool enough that we can edit text so it's more clear. Iterating through volume. And we can add a dollar, sign it. I can delete this one. Fixed MyType Oregon. And we can actually test this out. Let's and delete this one. And let's call our function arrays. And let's run it. Okay? And you can see that it iterated through everything. And also what you can see is that we edited some of the values in array. Like here, we change the first value to 99. We can see that it's actually done here. And the same applies that we added a new value. So original, originally we had integers TO up until nine. But we ended up actually for array pay on under volume, which is a 100. And you can see it here as well. So this political exactly the same applies for analysts. We can just write out at least for H and do the same. But the thing I really want to show you, which is super cool as well, is a method called for-each index_set. So let's use for instance, ArrayList C. So I list si por each dixon. So, sorry, wrong. For each index. Sorry, this one. Exactly. Yes. So remember. So what do we do here? Here is how we will refer to the index of that specific ArrayList element. And this is going to be the value. We can change it. We can say index and we can say value. And here in this code block, we can also decide what, what we are going to do when this elements. So also form demonstrated purposes. We can print line. We can see we can write currently, sorry, Lee will be at index. And let's pass n index reference. And its value is and pass in the value. Okay? Now let's comment this one out. So it's easier for us to see what's printed out here. And let's run it again. And we can see, if we check out what we declared is that currently we are at index 0 and its value is true, which is true. Currently we are at index one and its value is false. It's also true. And at index two become value which is true, which is also true. And this is how we work with ArrayLists and arrays. How we can declare them, added them, add them, and you will know how to use arrays a lot and programming. And this is the most used methods for race. And it's fairly simple. It's really simple actually, and it's really powerful as you can see. So I encourage you to use these as much as you can. And also we can check out some errors which IDE is drawing us. If we hover over bar, we can see that variable is modified and can be declared immutably using well, so they can change it to a wall. And the same applies for RL is b, sorry. And let's see. Now if you would like to change values like we did before, we will see an error. Possibly. So this is how we work with arrays, and this is it for this lesson. And I will see you in the next one. 5. If statements: Hello guys and welcome back to another lesson. In this lesson we are going to check out if statements. So let's start by creating a new function. Let's call this fun if statements. Okay? And let's just common here as well, IP usually doom, go. So what can we do with if statements? We can basically check if some value. For instance, if we are talking about numbers, great example would be to check if number is equal to something and if it is, we do one thing and if it isn't with 100 thing. So my proposal would be to create an if statement to check if number is odd or even. So let's start by creating a variable which will be our number. Let's call it just number. And let's give it a value of two. Okay? So the way how we can check if division by a number leaves something behind it is with this percentage operator. And we will integrate this in our if statement. So let's start with keyword IP, opened up brackets. And here we can write that if our number after division by two equals 0, then we do something. And if it doesn't, we do something else. So what could we do if the number is actually even we couldn't print line. So it's much more understandable. Print line, number. Let's pass in or variable like we usually do. Lumber is even. Okay, great. So if number will be evenly, will print out this string else, if our number is odd, Let's print line number. Or number is odd. Okay? And let's check if this works. Let's change our, let's edit our main function and delayed and delayed arrays function. And let's call if statements function, like we usually do. And let's try to run it and see what happens. So with number tow, it should display that number two is even. And that's what is happening. Number two is even. We change our number two, number three, and run this again. We can see that number three is hard, which is correct. Now, we can make more advanced if else statements and let's try to do it. Let's try to check if number is, let's, let's check if number is positive and only if it's positive, we will check if it's odd or even. And if it's a negative number, we won't do this check. So there are multiple ways to do it. And I will show you one way because I want to show and you can pass in multiple parameters and one if statement, there are other ways how to do it, which could be potentially much more shorter, but let's do this this way. So if number divided by two leaves nothing, and we can add to and symbols, which means that there is an understatement we want this number to meet. So we can add, and if number is larger than 0 and we can edit our print two number or number is even and positive. Else. Now we can actually add another if else statement and we can just write else if open brackets. Now we can copy this part of code and paste in here. And here, we want to check if number is not. If, sorry if I'm not slept after division is not equal to 0 and the number is above 0. We want to print line that number is odd and positive. And the last part is to check, to check, but the last code block will execute only if our number is smaller than 0. So we can write print line number. Let's pass in. Our number is negative. So what will happen here is that a port number will divide by two and will be positive. We go print out this line. Offspring. If something of this doesn't meet up. If number is, for instance, negative or number can be divided by deal without any leftover. And we are moving forward to next check. Which checks if division by two leave something behind. And if number is positive, again, if most of these passes, we print out this line. But if something of this fails, and then we move forward. So let's start by checking number three. Let's run it. And technically it should print out the number three is odd and positive. And yep, Number three is odd and positive. Let's change it to number two and run it again. Now it should say number two is even and positive and does exactly the same. And let's add a minus sign indicating that this number is negative. And let's try again and see what happens. Now we can see that number two, number minus two is negative, which is correct. So that's basically it about if statements. We can add more LC shapes if we want to and if it's necessary. But usually if you end up adding a lot of else-if statements, you can use case of operator and revoke particle this one later on. So stay tune. But the, and this is how we use if-else statements. Also a quick note here you can add instead of and we can add also r. So this will pass if number can be the wind, but to or if number is above 0. Instead of tracking that both of these statements meet requirements. But in this case, we need to use and to sew on coal works as we expect. So thanks for tuning in and I will meet you in the next lesson. 6. For loops, While loops, Case: Welcome back. In this lesson, we will check out for loops, while loops, and when cases. So let's just jump in it. First, let's create new function. We will call it four. Whoops, sorry. And let's hear. Awesome. So for loops, we use if we want to iterate through a specific range. So for example, let's write code which iterates through range appears, and calculates how many long years were in this range and how many short years were in this range? To check if a year is along here, we can divide it by four. An epic divides that is along here. So let's do it. Let's create the first variable, and let's call it start here. And let's say 2005 for instance. On Southern under button, another variable for the end year. And let's say 2010. Now, we will need our two variables. We can add it because we will need to save somewhere our counters. So let's create a variable and let's call it long year contract. Let's leave it on 0 obviously. And also do the same for short year counter. And let's say it in 0 as well. So now it's time to write a for loop. In order to deal with, we need to write keyword for open up brackets. First thing made lumen into specify how will going to name our iterator and how we will reference to it. I usually call it an HIE. We need to add keyboard n. And now we want to specify our range and we haven't declared to enable Soviet Naval, We are going to use them and we will just write, start here, two dots. And here. And here comes in the logic. And we will use if statements may learn about if settlements in the last lesson. So it shouldn't, there shouldn't be any problems of using it. So let's create the if statement. Let's say that if our iterator year can be divided by four and nothing else is left, then we are going to in Greece or long here counter. And if you remember from our valuable lesson, we can do it easily like this. But in cases when we cannot divide it by four, we want to increase or short year counter. So let's call shortly or counter and exactly the same. So that's Ed basically. Now, we can print ourselves results. So in a given range, the wear. And let's call our Columbia variable counter, contrarian, long year counter, long years. And let's call our short year Condor. Short year counter. Short years. Cool. Now let's try it, run it and save torques, meaning modify or main function. Again, let's delete if statements, and let's call our for loops function. And let's show. So in given range there will wear one long years and five short years, which makes sense. And our code is working as expected. So this is how we can use for loops. Now, let's look at the while loops. So while loops are kind of similar to for loops. But the main difference is that in a while loops, we do something as long as some condition is met or not met. So for instance, for example, let's create, let's write code which we'll ask user to enter a password. And if password is less than eight characters, user is presented with a message that password has to be at least eight characters long. And we will ask user to enter password again. And user or how to enter password for as long as he enters a valid password reaches at least eight characters. So let's define our control variable. Let's call it pass for password requirement. So the neck met, so password requirement met and by default is going to be false. And now we write our while loop. The keyword is while. I'm sorry, even into my bad, we need to create a function. Sorry. Let's create a function for while loops. While loops. And let's have the comment here was always read. Now let's paste our code from outside and placed in KID grade. Now, while password requirement met is equal to false. And you will see that ID actually tells us that we can shorten this. And if you remember from our previous lessons and then we actually can, you believe it. Like this, it will mean, well, password requirement met equals true. But if we add an exclamation mark at the beginning, it will mean while password requirement met is false, do something. And what are we going to do? We will print to user text. Please enter password. Then we are going to read the user's input in order to do it. And we need to use keywords, read line. So between need to assign a user's input to a variable so we don't lose it. So let's create a variable. Let's call it user input, which will be equal to read line. Now we have to do our check. And we'll use if statements. Again. If user input dot linked me use this one as well in previous lessons. If users Andrew password is shorter than eight characters, we want to print line and say that password has to be at least eight characters or chars. And that's it. And also the animal may have to add something else. So sorry, something is wrong here. We will call, we will speak about save calls. I later on. But let's just add two input Sunni. Let's just add two exclamation marks after user input. I will explain in future future lessons where we need to do it. So if user input is less than eight characters be print line password has to be at least eight characters long. But if users entered password is at least a choice as long. We want to print line and say password valid. And what's important here is to change or variable pass require a so password requirement meant or for short bus requirement, we have to change to true. So it breaks the loop. If, if we won't do it, then the slope will be continuous and it will never stop. So let's try planet. Let's modify our main function. Let's do a pore loops, and let's call while loops. And let's run our app. So we are asked to enter password and we can actually modify a bit. So it's, so it looks a bit better. Let's add a colon and a space. So there is space when you actually write it in. And let's run it again. Let's enter password which is shorter than HR actors, 1-2-3-4-5 for instance. And let's press enter. And we are reader, that message password has to be at least character alone. Let's try again and also add two short password. And we see the same. Now let's add a password which is actually h characters long, and press enter. And we can see that password is valid and the app executed successfully. So this is how we can use while loops. It's easy, it's simple and it's super useful. And you will also use these a lot in future. Okay, and the next, and the last topic in this lesson, we'll be when cases. So let's create a new function that's called when cases. And comments as always, when cases. So we use when cases, when we expect multiple inputs or multiple values for a variable, let's call it through that. And we want to execute different action for each, for each value. We could use if statements as well. But if we want, if we like, if we expect then different inputs and each input does something else. We will have, we will have to write a lot of if statements and its really long code and it's really tedious. So here is where when cases come in. So for instance, let's create a simple function that brains message. Prints one message if our boolean variable is true and prints and different message for Boolean violin is a boolean. Variable is false. So let's make a value or variable. And let's say answer, sorry, an answer equals true. And now we call When keyword. And here we want to pass in the actual variable which we are going to check against different values. So in this case it's our answered variable. And then we open up in bracket o. And actually id is showing us the beacon inland this. And let's do it for shorter code. Let's undo it. It's never mind this one. Because we will change this value or for testing purposes. But actually we can inline, but it's gonna be much difficult for you to understand. So let's just leave it as it is for now. So when answerer and now we write our cases. So if our answer is true, then be indicated with this arrow. Then, then we want to do some action. For instance, let's print line what Abel is true. Now in case when the variable is false, let's print out a message saying what Abel is false. Also, if we have larger, more code to write in each case, I will just cut this out. We can and these parentheses and paste in that code here. For instance, if we want to add something else, if you want to do something else, like print and underline for instance, and say anything. So let's run this main function as well. Let's call or when cases. So in this case we are variable is true. So it should print variable is true, which actually happens. Let's try to change it to false, and let's run it again, sorry. So the bumming, let's run it again. And now we can see that the variable is false by n. Ok, for Booleans there are only two cases, but for instance, if this is a numeric value like 0, then we can actually do a lot more. For instance, if it's one, do one thing, if it's to do other things, if it's three and do different things. So this is really powerful and this is also really commonly used and really helpful. So thanks for tuning in, and I will see you in next lesson. 7. Functions: Welcome back. In this lesson, we will talk about functions. As you may already observed, we are using functions since the beginning, but now we will take a deeper dive into them. So let's start by looking at a simple function, solids creative and your function. And let's call it, let's call it simple function. And we won't pass any arguments when we don't return anything. And this function, we'll just print line. This is a simple function. And now let's add the common heroes on the planning simple function. I'll just call it. So how we call them may write the name of the function and we add prints here. And we run it. Let's see. Okay, so this is a simple function. Everything is pretty straightforward and easy to understand and believe. So, but that's not what I want to focus on so much. I want to focus on more with functions which actually takes in some arguments or unordered horns values because those are the most popular ones. And usually when you write functions, you expect some info to come into the function. So I'm gonna pull to go out in the pension. So we can minimize this one and we can add new function, let's call it function with drugs or arguments. So if we want to take in some arguments, we need to add them in here. And first we need to add the name of the argument. So let's make it simple as well. Let's ask for users age, and then we'll just display what the user has already written for us. So let's ask for age is the name of the variable or the argument. And we need to specify what type, what data type is going to be. If we're speaking about the age, that most probably is going to be integer. Okay? And now we can simply write print line. Your age is. And then we can call a runnable age. Simple as that. So let's, and let's call this function. Let's just edit our main function, right? Function with arguments. And here you can see that we need to pass something in. If we want, then we won't be able to compile our code. So you can see here that non-value passed for the pedometer age. So we can now pass or perimeter of h. I will pass in mind. And you can see that id automatically shows what kind of argument we're passing in, which is super helpful and much more easier to understand. Now let's run it and see how it works. And yeah, your ages 23. Okay. And we can add more variables to it. We can say, let's bring it out not only users age, but for instance, user's name as well. So we can specify the variable or the argument name as name. And we can say that or name will be string. Great. Now, we can see that we are seeing this error here because the function requires two arguments which we have passed only one. So to fix that, we need to add comma and we can add, in my case it's crosstabs. Okay? And now let's modify our function to print out both arguments. Let's say your name is, and we'll pass in our name argument. And you are. Let's pass in all age, years old. Okay. And let's run it. You can see your name is Chris tabs and your 23 years old symbol. Now, let's look at the case when we have, when we function actually returns something. So let's minimize this one and let's actually add a comment which we forgot to do. And finding function with arguments. So it's clear. And now let's create a new function. Let's name it function with two. Okay? And let's imagine that this function is not requiring any incoming arguments, but this function will return something. And when we declare a function, we need to specify what type of data type of return we return. So how do we do with outer parentheses? We add col colon, and then we write the. So let's just say that we want to return a string. And let's open up our function. So you will see immediately that we have this red ID is showing us a narrower already. And id is showing us error because we haven't defined that this function will return something, but for now it doesn't. So if we hover over even see a return expression required in a function with a block body. So we need to return something. What could we do? Ok, let's make this simple. In order to return something from function, we need to use keyboard return. So I'll just write return and sorry, and a total. And let's just return some kind of string. And let's just return also name for instance. Okay, easing. So now we can call our function function with return. You don't have to pass anything in. And let's run it. And as you can see, nothing is printed out. And that's because we are not saving what our function is returning. We are not doing anything with the value that this function is returning. So we can do the following. We can create a variable with Val and nine, this user's name. And when we assign a value to it, we just pass in this function. And what will happen? What will be that? When we initialize this variable, this function will be called. And this function will return string crystal as simple as that. But if it will run, run it now we won't see anything here because we're not printing anything. So in order for us to see what's actually returned, let's print it just for just so we can understand how it works and we can add in or valuable user's name. And now let's run it and see it returns crystal maps and prints out. Actually, we can make this shorter. We don't do any of this variable. We can just pass our function into here. And we can delete our malleable and they can run it again. Oh sorry, pressed on the Run button. It's in bug mode. Let's run this. And we can see disruptions, crystals. Cool. So you can add a comment here. Function with coal. And now let's look at a function which takes in arguments and also returns something. So let's create a function, function with parks. And, and let's create a function to which we pass in current year, our birth here. And this function will return our age. Okay, so let's start with birth year. And this will be integer. And also current year, which also will be an integer. And we will return users age. So this whole we also integer and we can open up our function. Now. We need to do or logical operations here, which are fairly simple and it can create a variable. Call it users. Use rage, which will be calculated by this mathematical expression. Now we can write return. It was an age. And you will see that id is also screaming at us and showing an error because we can actually do this much more simple. You can see that variable used only in following return and shouldn't be inlined. And if we press in line variable, we can see that we can simply return, that. We can simply return our mathematical expression. Ok, so let's call this one. And also, as we are returning something, you'll have to print it. So let just call on function with RX and return so we can hover over and you can see what this function requires. Function acquires grittier and we'll add mine. And function also requires current here, which is 20-20. Awesome. Let's run it and let's check if it works. It shouldn't own 23. And it does. That's awesome. Now. But we can also do, is that we can specify in a function, this is called, this is basically a function constructor. We can specify the default, a default, default value for a argument. So now the current year is 2020. So we can actually say that current here is 20-20. And now what will happen if we will pass in a second attribute? It will use this one. So let's add 2021 for instance. But if we, if we are not passing anything in, how we don't see any errors. And because by default, functional u with this value. So if it will run this again, the result should be the same. 23. Now, if we add if decide do I and the second argument, and that's a year, 2021. So we can see the difference and run this again. We can, we will see that all default value is already done and the result is 24. So let me just add Common here. Function with arguments and return. And that's about it. These are functions and I will see you in our next lesson. 8. Nullability handling: Hello guys and welcome back. In this lesson, we will talk about null ability. So nobility is one of the things why a coffin programming language, languages so popular and so desirable because it basically fixes the largest problem java had. And this is also referred as the $1 billion mistake. And what it means, let's dig deeper and find out what this actually is and what we can do with it. So to do it, let's create a new function. Let's call it mobility. Let's minimize this one. Comment saying handling nobility. Ok, so in Java, we were able to define a variable as null. And we were able to compile this code and run it. Now, as you can see, we can define our ANOVA table. But if we want to use it, we won't be able to simply use this variable, for instance, let's say as defining a variable length of bar, which will equal my variable dot length. Sorry, let's specify a type for this so it knows what to expect. And now we will, we will, we will be able to call this one. Now, as you can see, we see an error here that know cannot be MOU of no known type nano type string. So yeah, in Java we were able to define a variable like this and then write this line of code and we were able to compile this code. The problem is that if we will call this variable, if we will call any method to this variable. In runtime, is going to cause an error. And for end-users, this error looks like app crashes. I think I'll neo, having experienced that, you are using an app and just quits unexpectedly. So in most cases, this was caused by mobility issues. And the person who developed Java said himself that this was a $1 billion mistake he made because this actually cost at all the companies which developed publications in Java portion. So Katlyn is here to fix it and we will check out how Katlyn attempts to fix this. So yeah, let's delete this one out. It was just for presentation purposes. And let's leave our mandible here. But as you can see, we cannot just initialize it like this. We need to add something. And in order to define a variable with value and all, we have to add a question mark after it, which means that this variable can be null. Okay? And there are four cases how we can handle nobility build in. The first case is safe goal. Let's write it down in the comments. And we will use question mark and up for it. And this basically means that use this. This if you don't, sorry, I don't mind that string. No. Can be returned. One this means so let's just write a print line so we can see how this works. So we can write that, for instance, my string length is, now let's call our variable myVar dot string. Sorting memorable dot length. Or we need to use braces for this. My variable buckling. And you will see an error here. Because there can be cases when my work is know and if myVar is no, we cannot get the length of it. So the way how to fix this is to add a question mark here. And what will happen is that if our variable will actually contain, will actually be a string, this, we will, this will return the length of it. But if this variable will be null, then galling it, anything on it will return a string null. And we can see how this works. So let's leave automatable. Know what's maybe change it to string. I'll just add QWERTY. And let's modify our main function. And now we want to call mobility function. So let's run it and let's check it out. What will happen. So it's compiling now. Okay? So we get my string length is six, which is correct. Now, let's change it to know and run it again. We can see that my string length is known. So we can use this safeguard L when we don't mind that we can be able be return, that string will be returned. So this is the first way, sorry how to handle nobility. Second thing is quite similar, but there is an additional function on data to it. It's saved call with lead. And what it means is that something will be done only if our variable is not null. So we can write that. It will execute LED block only if in case, in our case it's myVar is not null. And what does it mean? So we have our war and the way how to let, lets imagine that. For instance, you have user foreign and user have some optional fields. And after the user fills in all the forms, we print out what user, how entered. And as some fields are optional, user can lead them and empty. And if there are empty that they are null. So we want to print only something. And we want, sorry, we want to print only those form inputs which are actually filled. So how we can do it. So we can still call or what's Sorry, what's yeah, we can call it a valuable. We also add a question mark quite like before and dot. And then we add a keyword to let me open up and brackets. And here we will specify what we want to do if our variable is not null. And also in here, if you want to reference to our variable, we use a keyword. It, as you can see, are the is showing us. So we can print line saying that our variable is not null. It's, and then we can call our variable. And as I just mentioned, we will refer to it as it. And let's run it. So let's change it back to a string. Let's just leave it outside and different strings. So it's been confusing. And let's run it. Sorry. Let's comment this line out so it's easier to understand and Lattes around. So we can see our variable is not know its quality and its correct. And now if we will add null, if it will change it the null and run again. This Auditor nothing because at this point it stops and skips this block as our valuable is known. Okay, now let's talk about next option we have. Let's comment this out. The third option, we can use something called Elvis operator. Elvis operator. And it's also something similar. It's like an something similar to if else statement. So I will, so it's easier to understand what's actually happening. I will write the same function twice. I one will be using if statement and the second will be using Elvis operator. So let's create a variable and let's call it just come under water, whatever it does matter. And we will say that if Sony, if my Guatemala, my table is not equal to now, we do one thing. So we can say that R, another variable will have the length of Mayawati, for instance. But if, but if R one is null, we wanted to do something else, for instance, and we can just return minus five or minus one, doesn't matter. And let's just print an underwater. Okay. Awesome. So now our variable is now. So let's see what will happen in stony minus1 as expected. And if we change it to string and run it again, it should return four grid. But there is an easier way how to write this and much, much, much shorter and this is using are always operator. So basically, let's add an underwriter variable. Let's call it another one, du. And this basically will be the representation of this. So what we wanna do is we want to call my war. So in case when it's not, No, we want to return the length of it. But if it is null, we want to return minus1. And it's showing us an error. Y. Also forgot meaning to Al question one kid as well. So like so this one line of code will do exactly the same thing. What one does these five lines of code. And we can change what's in. Basically, here is the positive scenario when our variable is not more. And here is the negative scenario when automatable is null. So let's print out now this variable and let's run it. So we have string. Let's run it. It should return four. And does. So if you will change it to null and it returns minus one. So this is Elvis operator and also can be used to manage mobility issues. And the last one, let's just delete this. So it's not confusing. And deletes some empty lines. So it's clear how Elvis operator works. And last one is non null association operator. So in order to demonstrate this, let's add another print line and say something similar. Length. Length of our variable is. And now let's call our variable my bar. We need to add brackets, sorry, some brackets. So myVar dot length, but you won't get an error. So usually we added a question mark here. But in this case we add two exclamation marks here. And what will happen? We're basically telling Katlyn that we are completely sure that this variable at this point will have a value and it's not going to be no. So palettes and comment out this one. And let's save it. Sorry, I changed it to string. And let's run it. So now it should return four again. Yes, length of four available is four grid. And now, if we will change this back to being null. And Miranda again, we see this beautiful red struct raise exception intra-domain, no lability. So this is, this is what happened in Java often. And in order to get something like this in Scotland, we need to specifically tell that we definitely know that this variable will have a value. So this is how Katlyn managers, and this is how potent managers, nobility and all the developers which can come from Java really appreciate this and are happy that this has been implemented. So that's why I wanted to tell you guys about it. Maybe at this point, it doesn't seem like a big deal between venule. Start to write code more often and more complicated. You will understand that this is actually super helpful. So thanks for tuning in, and I will see you in next lesson. 9. Classes: And welcome back. In this lesson, we will talk about classes. And classes are really important in object-oriented programming. So let's take a few minutes here to check out what our glasses and how we can use them. For those who don't know class as basically are like a templates for something. Let's imagine, we, let's imagine a car. Car could be a class. Car has its own properties and functions. Properties can be like we'll size or how big is the engine, or how many seeds there are. And proper SRE and functions can be alike, drive or stay or switch gears and something like that. So I hope this helps to understand what our classes just think of them as templates. Templates which recreate if we now that will need multiple instances of something similar. So let's just start by writing some code and hopefully it will be much more easier to understand them. So let's start by creating a new function as we always do. And this will be let just call. And sorry, we need to create not function but the class. So we can do it in Katlyn files so we can just created here usually clauses are on it all on its own, like file, but for testing purposes we will just add them here. So if we want to create a class, we need to use the keyword class. And then we just need to add the name and four functions. We write names with the lowercase letter than with classes, it's uppercase letters. So let's just create a class car. This would be a simple glass with simple method. There won't be any perimeters or the initial constructors. And just create like this. Once also under comment as we always do. So it's much more easier for us to understand what we are doing here and just call this one simple glass with simple method, with simple mental. Ok. And here we can create a new variable. Let's create private model because we won't change it. And let's name it car Mondo. And we can choose an a moment. I will choose, for instance, lambda. And now we will create a function and me grit function inside the class like we usually do. And let's call this function output out. So they output, sorry, output. This function will take any parameters, but what we will do, we will simply print line with text. My soldering, my car model is, and here we can call our variable car model. And let's check out how we can call this class and how they can call our function. So let's scroll to the top. Let's delete mobility. And let's try to call our class. In order to call our class, it's super simple and it's basically the same as we call it. Functions be just enter name, taught and you can see ID is already showing us, but we need not only car color function. So we write car, we open and close brackets and then we can see all the functions it has and it has only one which is output card. So we can autocomplete, autocomplete that one. And we can run. And just as expected, it's printing out my corn model Islamic rate. So now let's create a new class. And let's create a bit primary constructor. Primary, primary constructor basically means that we taken some values. So let's add a comment here. Consoling, twerking. So glass with primary constructor, aka fundamentals. So we use keyword class. Again. Now it's a bit differently. Let's call it my car. And we will take in name, which will be string. Okay? So this is our primary constructor. And it's basically the same as for functions. So now we can call, make an assign, the input a name to our local variable. This can also actually be private. Yeah, if we add keyword private, it will mean that this variable will be accessible only in this class, only in this block. It won't be accessible for anyone anywhere else. And yeah, let's add a new function which will have, which will be named the same as the previous one. So let's call it function car. And now we can write print line. Mondo is, and we can call our local variable. Okay? So it's basically the same as here. But the only difference is that we are hard coding or car model. Here we allow the user to enter its own. So I'll try to run it and change this to my car as the, the function is the same, but we can see that it asks us for pedometer and says that normally you passed for bolometer name. That's as it should be. And we can pass our cloud model. And we can run the test again nonetheless, but the function, the main function. And you can see that we are outputted string. We have given here. Okay, so let's think till now it's pretty straightforward. Classes are really similar to functions. Now, let's add, let's create a class with init block, okay? Solids and the comment as usual. Subclass. It will still have primary constructor. So class with primary instructor and block. Ok. So let's create a new class and just give it a different name so it doesn't overlap. So like my private car. In this case, we'll ask two parameters. One will be one will be the string for Michel. Sorry. Second rule, below the age. So, okay, let's make a bomb is my car. My car. Or better? Let's add here. And it's gonna be an integer because we needed fall. I'm number. Okay, so what's in a block? To create a new block, we need to write just a keyboard in it. Press tab. So this block will execute immediately when new instance or four class is created. So this is for something. So basically here we add something that we want to do immediately when this class is initialized? Navy hovered over, I guess, just an error because we haven't added anything in yet. So let's just bring something else. For instance. Print line. The same as usual. My car model is and we can just call my car name. Ok. We'll fix that later on. So this is something that will be called initially. Now, we can add additional functions, store it. For instance, function output, car H. And here we will simply print line. And we will say that my car is. And then we'll call our variable, but with these brackets because we need to make calculations here. So we enter what materials now and we'll, we'll subtract my car here. Okay, so Let's try this out. So to see how in, in block works, let's just, let's just create a variable. And this variable will have the type or class because we can set type for variable as our class. So we can say that my car equals and we can say that it's my private car. And we can pass in the name of the car and the year of the car. Okay? So now technically it should output or block which tribe and see. So yeah, as you observe, usually we have to call a function, or we should only have to call like a class in order for something to happen. But as this is in a block, this happens every time we initialize our glass or create a variable with type for class. So now that this is done, we can use our variable, my new car, and we can call this function output car age. We don't have to pass anything and because we have passed everything in this initial, initialization beforehand. So let's just run it. It should output the same in block n. It should output how on your car. And as you can see, it, does that. Also, maybe you observed that beforehand we just created these input arguments, just like we do for functions. And this actually required me to create a new value inside of our class. Because if I tried to use name argument, it won't let me. And show it will show unresolved reference name. So we have to declare new variables and we have to assign the input data to these variables. Here, we can actually, I'm passing keyword well or water. And in this case it can be also a private. And this also can be private. And now we don't have to what? Sorry. So yeah. And so now we don't have to make new variables inside our class. We can just call them here because they are actually initialized in here in the constructor. And maybe we would want to access, for instance, my car name. And we can do it, do it by calling automatable. And as I said, glasses again, how both functions and parameters. And if we add dot and tried to call my car name, we can do it because it doesn't exist. It doesn't exist in this context because it's private. So if we remove private, and now it actually exists and we can call it. And if we do call it, it should display. Also label do nothing because we're not printing it out. Let's bring this one out. Print line. Lunches put this in, and let's just run it. You can see how the classes are super powerful and they are used a lot that you cannot program without them. They're super powerful and, but this is just a brief look on how we can define them. What are the ways, how to define them and what they can actually do with them. I hoped you enjoyed this lesson and I will see you in the next one. 10. Data classes: In this lesson, we will talk about data classes. So in previous lesson we checked out what our classes and how to use them. And I wanted to make a separate video about data classes because they are basically the same, but they are not. They are mostly focus on storing, If we can call it like that. So let's start by adding comment class. And that class is created a super similarly to the way how we create a simple class. With the difference being that we need to add Aneesh additional keyword. And you can see it here. It's data. So we add datum and we add class. And now we set properties. And the best example would be imagine if you don't have to create, say, a program that holds your employee information. So each employee has name, surname, age, let's say bird. So you know when to give employees presence or how many kids the employee has. So you can send like Christmas gift to your employee kids and so on and so forth. So and really nice way how to hold all this information is using data class. And just for an example, if we like one this data class, then each time we have a new employee be just called this class. It's like a constructor. So like template and we just fill in all the information. So for instance, let's call it, start with ID. Id. It's gonna be integer. So we can uniquely identify these. I'm persons. Second would be name, string. Third, volume. Certainly. Lastname, shrink. Next. Kids. This will be also an int. And sorry, we forgot to add the name and just call this one employee. So we found that the class employee, it will have ID, will have name, surname, and kids. We don't have to add brackets if we don't want to do anything with this class. So, and let's just check out how we can use it actually. So we can live on this. Let's say we have a new employee. So let's make a variable. So let's call this one John. We have to do it. So John John will be the same as our employees. So we call employee. We opened on brackets and we start to fill in information. So ID, John in this case is the first person we are actually saving. So ID will be one. Or if we are programmers than let's start with our developers at 0. The name John. Then certainly in Manchester, how it's actually spawn, sorry, but yeah, let's imagine this is it's certainly and amount of kids, let's say three. And now we have created john as on data class with all these properties. And what does that mean? We can do a lot of stuff actually with this, and it's super simple to work with this. So we can print line, we can print line that new employee and that width. And then we can call our new John. And then we can call dot, and we can output what we want to get out of this employee. So in this case we want to output ID, so we just write ID. So we need to add these brackets against its work. It works. Okay, so now let's bring out a new line. New ie has. And now we can call our knew John variable. Something's not right. Also, no need to add dollar sender, John, that dot kids and kids. And now if we print it and see new employee under the new employee has three kids. So this is super powerful. And in most cases, you will create this data class in encase employee. And you will have a some kind of array with an HRA will have the type employee because it's not a class. So each time you would like to add a new user or solid new employee, you would just put an extra record into, into the array and you'll fill in all the fields that we have put in here. And then, and then in case like. And then you can print out all your employees in alphabetical order or print out only employees which has kids using some additional methods. But this is super useful and this is super easy to use in Java to create a data class. It's actually, there's a lot of boilerplate code that we will have to write, but in Katlyn it's super-easy. And also additionally, we wrote here, well, we can write one as well. So if you would like to allow to edit some values, I think it makes sense to allow edit kids because kids, amount of kids can change. The same. Actually apply for surname because for instance, someone gets married, so Lan gets changed name actually as well. But for ID, we just leave it as read-only because we don't want to mess with IDs. And we can also add like a default, default values. And if we don't, if we don't. And these volume, when we initialize new employee, it's going to be default. So for instance, we can say that kids by default will be 0. And if we remove kids, we won't have any errors. And now if we run the class again, sorry if we run our main function again, which creates data class to print out that employee has 0 kids. And that's actually true because by default is 0 and we haven't set any other value. If we do set it back. And we run this function again, it will, it will say tree because it will override the default value. You can see the swam that's basically at about data classes. Hope you enjoyed this one and I will see you in next lesson. 11. Debugging with IDE: In this lesson, we will talk about debugging, which is really important and super-useful. It changes a bit from ID to ID, but fundamentally it's the same. So I want to show you how you can debug your code to understand what's going on if something is not working as you expect and you can find why this happens without debugging it. So in order to visually represent this was create a new function. Let's call it the budding. And let's add comments, so on. Okay. So here I will just assign a bunch of variables. So value a is going to be 108. Value B is gonna be one hundred and five. Twenty three. So value c is going to be a power, a dot power, which means power. And we'll just say two times and then divided by b. Okay? So value d is going to be false. A Boolean value e is going to be opposite of d. So it's going to be a Boolean as well. So there's gonna be a string, value j. It's going to be a string, but it also will include while your app. So my name is, and as always, we can call our variables here. And let's add value. H is going to be array of and we'll just pass in 123456 should be enough. And value e is going to be an element of array h at the index of file. Okay, cool. So let's imagine this is just to represent there is no real meaning to this code that I want to go through it yourself with you guys and explain how the debugging works and what we can actually see when we do bug in the code. So in order to debug code, we need to add what's called a breakpoint. And you can add by pressing here. And this red circle, it will mean that on this line I will stop and the bugging will start. So. Once we have added breakpoint, we can go back to our main class. We can delete on this. We can call our mental debugging. But in this case, let's press on the bug, not run because it will run. This breakpoint 1B is going to be ignored basically. So let's hit the bug. Lets wait a minute. Okay, so we can see that our program has stopped here. And we can press this button to go line by line further on. So let's just press Step Over. And this line has executed. And now in memory we have say on this value. And it's with the value we assign to it. And you can see it here as well. Let's go deeper. Sorry for further. Now we have said memory or about value b with the value we have given. Now it's time to calculate the value of C. As you can see, as we are using variable a and b, it's showing us what our actual values of both of these variables. And if we step over, it will calculate that C is actually 96 and a bunch of numbers. So let's continue. So d is false. So E should be true. And if we step over, we can see that e is true. And also as we are using the, these showing here as well. Let's go. F now is a string with while your crystal and volume g should be a string with value. My name is Crystal, and this is true. And if we go further, we will initialize new array and we have done it. And Volume II shouldn't be value from array h at index five. So it's 012345, it should be six. And if we step over, we can see that i is six. And we can see all this info here as well, everything that's saved in memory. And this is super easy way how to debug code. Well, this is a simple representation, but I think you'll get the idea. When you will have longer functions. You'll see you can use this to debug, and this is super-easy. Also, let's run the debugging, again. Stopping Iran. Also. Now we are just checking in the level of or function debugging. We can actually dig deeper. We can press on step into. Okay, this is easy, but yeah, we don't have a function. So imagine we call a function here. If we will press step into it, will actually go to that function. And then we can see what happens in that specific function. In this case, we're just assigning values. That's why it's not going any deeper. But if we would call a function here for instance, then would. So this is how we can debug code. I strongly recommend you using it because you will have times and it's not gonna be rarely that something is not working and it's really hard to understand why. So just feel free to and this red dot here and run the bug mode and see what happens step by step in order to define the issue and fix it as soon as possible. Thank you for watching and I will see you in the next one. 12. Code commenting & Kotlin Style Guide: We are slowly going towards finishing this course. And I wanted to talk about code commenting and Paul formatting, which is also really important. Not only if your work alone, but only, but the more important actually if you work with bunch of other people with the same codebase, let's imagine you are in depth theme. I'd like ten people working on the same app and you are touching the same codebase. They are some fundamental rules to take into consideration and one of them is called commenting. So actually we did comment our code using these comments and we didn't just, so it's really easy to distinguish the difference different things we checked. But let's check out how, what's the correct away? How do comment functions? So, and this, I believe a place for classes as well. So let's add a function called addition. And let's pass in two parameters. So number a, which will be an integer, and number B, which will be an integer as well. And let's return integer as well. So this function will be super straightforward and simple. All we do is run the write the keyword return. And let's just add numbers a and b. Ok? So in order to appropriately common function, we may need to use this green column here. And the way to do it, at least until the DJ is AD portal slash two stars and then press enter and it will auto completed. Now, each time we add this kind of comment above a function, it's attached to that function and I will describe how it works later on. So up until now we didn't comment on what exactly we are doing. But now let's take it to the next level. So let's describe what this function takes as arguments. So we can write the x argument. And now in square brackets we can say number a. And you can see that it actually auto-completes and it's showing us two numbers which are in the constructor function. So takes argument number a and number B. And the next line we can describe what it returns. So we need to use at return and we write return sum of past numbers. Okay? So first of all, why should we use the square brackets? As you can see, it automatically changes the color of them, which means that they are linked together. Which means that if I refactor in this variable, for instance, we're pressing left clicking, right-clicking, sorry. Right-clicking Refactor Rename, change this to number one. It will automatically update the comments as well. So it will be, we will write it without the square brackets. We would have to manually change it here as well. And the same applies here if we press or we can do it actually from here as well, or from here as well, I believe Refactor Rename and yeah, we can do it here. Number two, sweep. The second thing why this is important is if we actually call this function from our main method or function, and you hover over it. Then at the bottom we can actually see the comment we wrote. So it takes argument number one and number two returns sum of past numbers. So this is really, really useful, okay, in this case, we have only one file and we know what we just wrote. But in case if you're working with multiple people and you are using other people, classes or functions, you may not know what they actually do. So having this kind of comment makes it much more easier to understand what's actually happening. And not even if you're working with multiple people, if you have a large code base and you're the only developer, it will be very likely that absolutely pure months or years even you will forget what you wrote and what was the intention of a function or a class. But by adding this comment, when you write your code, you can quickly see what this does, both from here and just by deep linking to the function itself and just reading the common here. And the second type of comment is just a simple comment. And we're having two forward slashes. And you can explain what's happening below. So just summing. I'm not showing that to you in a world. Okay? Yeah, let's just solve returns sum of numbers. Okay? So in this case it's kind of redundant because it's really, really easy function and you should never comment this easy, this trivial functions. But this is just for showing how it works. So in case when you have like some weird function or you think that maybe it's some other developer won't immediately understand what's happening here. You can add this simple comment explaining what's happening below. And that's basically how, how you use comments and the fundamental rules about commenting code. And this is also a best practice to comment your code. The second thing is Katlyn style guide. So it's also really important to how consistent code. And when we write code, we may forget to add space like I forgot to add here. Or we, for instance, if we don't use autocomplete and we'd read ourselves, we don't add space here. And what we can do actually before we are pushing our code, we can reform the file automatically. First thing we need to do is go to IntelliJ I settings, press on preferences. Then we should go to editor, code, style, find. I'm a language in which we are developing. In our case, it's Katlyn. And here we can either set it up manually, how the Tom sizes in dense and so on, so forth. Or we can set from and use Katlyn style guide. In my case, it's already applied, but in your case, you should press on cotton style guide. Press on, Apply and okay. And then when that's done, you can introduce shortcut, which is Option Command L. On the code menu, you can select the Report that code. And you will see that the code was reformed and actually 11 lines were reformatted. And you can see that all the spacings are now here and there are no unnecessary spaces and the books cleaner. And actually in most cases, in real projects, before pushing codes in Git, which is version control system. And there are some automatic jumps implemented that they will reform and the whole code without your, like, without you asking for it. But it's still really important to know these things because it's also a best practice. We're consistent code without formatting problems. And this is super easy by just set up your custom style guide and use shortcut, which was Command Option Command L. Option command L. Or we can use for the file to reform the whole file. So that's basically it about commenting and cotton style guide. This is not really about programming. It's more like best practices and why you should use them. And hope you understood and hope you liked it. And I'll see you in next lesson. 13. Final challange - CLI app: Hello guys and welcome. In our last lesson. In this lesson, we will create CLI application. Cli means command-line interface. And the idea is here to just put together not everything, but most of the, most of the stuff which we touched and dusted and checked out. And just to give you guys some somebody vision of how it actually looks to create something from scratch. If we, if we can call it like that. As this is ClO, ClO application is going to be quite easy because it's running via command line interface. So it just accepting input and outputting input. We don't have any UI elements or stop. But still, let's try to make the short. And let's start by creating a new project. So we can click on file. We can click on New Project. Oh, not dishonest, but as the first time, let's select Java and then Katlyn. And let's press on next. And now let's find a place where to save it. I will create a new folder on my desktop. And let's just call it final task. Kid finish. You can choose this window. Okay, so let's start. As always. We first need to create our main cotton fall. So in source folder, let's create Cotton file class. Let's choose file, and let's call it main. And here we can already great men function. Okay, cool. So in this CLF program, I'm planning to use, I'm not a glass simple class. Outputting in for the users and receiving input from users. And let's start by creating input handler function, which will basically agreed the user ask user for dot-dot-dot and all that. If dot dot is valid and if yes, it will return us the data but which user entered? So the application will be super-simple, will ask user for its name, his name hers nine. And we'll ask. To guess number from 0 to nine. And then we'll create a function which generates random number from 0 to nine. And if user predicted the guests the same number, user has one. But if user having guest number than user has lost. So it's super simple, super trivial. And it's not that fun game actually as well, but it's great for, for learning purposes. So let's start with the function and let's call it input handler. So input handler won't receive anything because input handlers job is to give us information. But we want to receive something back from input handler. We will have properties of user, properties which user gave us. But I would like to store that in data class. So first we need to define a data class. So we can actually set the output of the function are data class. So let's go in and let's click on our source folder. Let's click on new cotton fiber glass. Let's choose class, and let's call this class player. And hit enter. And forgot to zoom in. So this is a class player. We need a data class, so we just add the keyword dot the claws and the beginning. We can remove these curly brace. Brackets, braces natural how they are call. And here you'll need to create the template of our player. So first things first will have the last kilos and for its name. So first thing we can write Maribo name, which is going to be a string. And let's form on this phone properly. Stood like this. So it's much more easier to read. Second property we need from user is going to be addiction or predicted number. So this is going to be an integer. And then we will generate random number and we will, we will display if user one or not. I'm planning to display this in a table format. So they can sort, store all this inner 0.1 dot plus and then just output user, his name is predicted number actual nonlinear and user one or not. So we can finish this class with new variable, random number. And this is going to be int as well. And we are going to set it by default to 0. And this is because we will get these two parameters at the beginning when we initialize this class. But random number and an next next parameter will get later. So in order for us to do that, we need to give them. And up old values. So random number will be 0 and last user, one is going to be a Boolean. And for now let's set it to false. Okay? Pretty straightforward. Let's say if we don't have any link that ours are we how will wanting with dots? Because our Player class is not used yet. On the jump back to the main, main file. And now we can say that this function will return us layer. Okay? So give me a cyclic may understand. But what we can start. So yeah, let's add a comment to this function as well as it is by the best principles. So this function on the user and dysfunctional read users input. It will validate the user's input. And it will return us Player, class, wit users, dot-dot users enter and delta. Because it's not going to be completely finished with. Awesome. So let's start. First, we'll name the variables where to store the temporary name and temporary prediction. Because we all need to while they're at it and we can put it in our class unless we know that this, this user's input dot is mode. So let's add a comment. Save them for our a input. And let's create a variable name. It's going to be string sorting. And for now it's null. As you may remember, we cannot just set something like this as a minivan or a question mark. And the same will do for them. But addiction. This is going to be, it's technically integer and but we are going to set it for now as string. And because we read it as string, a string, we will all day string and when necessary we will convert it to integer. So for now, this is going to be string as well and it's gonna be No. So let's add a question mark at the end. Now the idea how I usually all that input is that I create a while loop. And while all the conditions which I want are not met, user is repeatedly asked to input collect data. So we'll need some control and valuable. So that's how the common here. And not the motivation. Malleable. Cool and dot dot valid equals false. And now we can actually start our while loop. So while dot-dot valid equals false, or as you may remember, we can write it differently, chosen by adding exclamation mark at the beginning. And here we will on block will start. So we'll have a lot of code here. So be prepared for this. So first is that we need to inform our user what this is. So let's print line. And let's write, this is our lottery. If you guess number. And let's add the range here. You will win. Okay? Simple. Now after that we'll start to ask user for input Soviet alike and some kind of line, line. This solver can divide what's like an formula to a message in watts and input. Awesome. Now we label asks for a user's name. So what's right? In this case, we will write print, not print line. And I will show you a lighter one. So print pulleys and your name. Ok. And now it's time for us to read what user entered on weekend us our quality bowl here so we can crop it. So temp name will equal readline. Next, we want to ask user for its prediction. So we write print again. And let's write in blase. And third, your diction. And we all read, will read that. We will assign what we read to our prediction variable. Sort of thing. It's not the correct one. The line. Okay, cool. So now it's time for us to all that user's input. I think we should check first fall if user inputted anything, because user can just escape, press enter and it will enter nothing. And afterwards we could check if the range is correct if user has actually entered number from 0 to nine. So let's write our first if statement. So that's held up small common here. Validate that input is not. Mt. And let's right, so if name is equal to nothing, or we try this in previous lessons as well. Or if then prediction equals to nothing, what will we do? So first of all, we want to inform the user that it has to enter require data. So let's write, please enter. At acquired. Now we should also add some kind of divider. So it's easy to understand when the program starts to execute again and ask again for users input. And now what we have, do we have to break this while. So it jumped back to the beginning and we can do it by adding keyword continue. So if users enter a name or prediction is going to be empty user or get this message. And at this point, we will jump back to here and we will tell you that this is a lottery. Please enter your name, please enter your prediction. But incase if user has actually entered something here and it's not empty, we continue. So, right, and under f. But let's start with comment. Validate that. Dick tion in range. So let's write if. So. Yeah, so if temporary prediction, and now we have to, if temporary prediction is more than nine. Remember nine is the highest possible value, or temporary prediction is less than 0. Then do something. First of all, we need to fix this. So as I mentioned, we are defined then prediction a string. So we can compare string to integer. So we need to convert string to integer, and that's actually super easy. We can just call a method to int. And we can do the same here. Second thing is we need to be sure that it's not know, and we will be sure that it's not null because we have all if check here so it can go through if it's empty. So if you remember from our knowability lesson, we can add two exclamation marks and here we actually don't have to add it because we say here that it's definitely non-zero. Okay, and in case when the users enter a number is not between 09, we need to print something out as well. So in this case we will print error message, please, and take action in the given range. And we can just copy this one. And here as well we'll add continue because if something is this validation fails, we need to start all over again. But if this passes, if user has entered named, Djoser has entered our prediction. And if that prediction is between 09, we write else. And we set our controller y double dot valid to true because we want this to end. I want this law to end because we got and we can move forward. So once this is done, once this is done, we need to return, return players class. So I'll uncomment the TOR user data in bilayer class, and let's run it, let's try it. Return constraint player to indicate that it's a class that you, the first one, which is rude. If we open brackets, we will see what we need to pass. We need to pass name and predict a number. And all we do is we type a temporary name. Here. We also have to add exclamation marks because cotton won't allow us to proceed. So we definitely know that this, this won't be No. So let's add two exclamation marks. And the second thing is temporary prediction. And the same applies here, two exclamation marks. And also we need to convert this to integer. Because if we look at our Player Datta class, Riyadh expecting any danger. Okay, so let's just dive to int. Okay, cool. So our ultimate, so our input handler is basically ready. Or excuse me, we needed to ride this outside of wild slope. This would be a horrible mistake. Yes. So be, be careful with this. So we need to write this outside our while loop because actually here we return the data and I have a typo. So yes, in this code block, recheck if all the info is correct and if not, we repeat the flow. And as long as the user enters a valid model data. But once user enters while a date that we want to break this and we want to return information. Cool. So now, now we need to create a class and where we will pass this info and a pole, it will generate a random number. And it's going to compare the number and if our user one or not. So let's create a new class in our source folder, Portland file class that's chose glass. And let's just call it a lottery. Okay? So we so min, so water, they will need some arguments. So to be able to use them without assigning to different variable. Var keyword here. Let's call this odd player. And it's how would the type of player or a four data class. This class won't return anything per se, but functions in it will. So first we need a function that generates random number. So before we read function, we write the common four function. So remember slash two stars and we write that Jordan random number from 0 to nine. Okay? Let's write the function. Generate random number. We won't pass anything in because we know the range. We can just hard audit in this case, but we need to return something in it's going to be an engaging. And actually do get random number from covenants really using. And let's just run right return. First we need to specify the range and we already know the range. So we're entered the start of range, then two dots, which means until and then end up range. And to get random integer from this range, we just need to call, sorry, we just need to call random. And that's basically it. And the second function will actually compare if users enter number is the same as a random generated number and it will fill in the rest of the info here. It will fill in this random number. And then it will compare random number with predicted number. And if they are equal is going to be true and they are not equal, it's going to be false. So now let's write new comment for our next function, which is going to call, let's call it debugging determined if one or something. So what it does is compares. Prediction with a random number and returns. Turns layer class with four info. Cool. So let's write our function. Function determine the term min if one on they can anything because we can access our Player variable from anywhere, but it will return player. So first, we need to update our plan daughter glass with the random number which we got from the generator. So we just call our Player variable. Then we find random number and it's going to be equal to r generate random number function. Because generate random number function returns an integer from a range of 0 to nine. And when that's done, we need to write an if statement. So if our player random number equals R player predicted number, then our player dot user one equals true. And that's basically it. And we could write else. Or player, user one equals false, but that's kinda redundant because by default we have false. So if our player or players random number equals two, our players predicted number or players property user one will be true. So now, once this is done, and now actually we have to, we have to set it to false or renowned. Okay, let's try by not setting it to false. So all we have to do now after this check is return our player. So now let's jump back to our main function. So what do we need to do here? We need to pass or lottery class or Datta class instance with our users input and inform. It may sound harsh, but it's actually really easy. So let's create new malleable variables for him. Let's call it current player. So current player will be equal to, let's call lottery class, solid lottery class. So lottery class asks us for our player with type player. Now we can call our input handler. Because input handler returns as player. And now we need from this class function determine F1. Because the Durman F1 will return the full info. So it will return a variable with type, player with falling, fall, boat inputted data and calculate the dot. So now we have, you've got users input o that the user's input. We determined if user one or not last step is to outputting fall. So for that, we could create an output handler function just like we did for the input. So let's start by a. Sorry. What's the problem here? So k is odd. So this function will take actually variable which will be our player. And it will output results to you that it won't return us anything. So now let's run the function. Put handler, sorry, insulin correctly hold right output handler. So we'll pass in or player. With type player. We won't return anything. So you don't have to add anything. Let's add a divider. We can actually copy it from here. Great. And now I want to print it out in a beautiful way. So just follow my lead because we have the option to print out a bit done and given paddings. So just tried system out. Print F. As you can see, we can pass in a specific format. So just follow my lead and type in the following. Percentage minus sign, then. Small S percentage sine 15, small percentage sign 20, S, percentage sign minus 15 S backward slash. What may find that. And this and this will determine the format. How wide will be it's going to output like in table. So we're specifying how many row, how many rows will have and how wide they will be. And now we have to set our row headings. So first is going to be named. Second is going to be prediction. Term is going to be a random number. And finally, user one. And let's add a question mark here. Okay, so we have our titles for our table. Now, we have to use, we can basically copy everything because we will use the same bad things. And now we can write the following. Or player dot name, comma R, player dot prediction, coma or player dot, sorry, random number, comma R player dot user1. And don't forget to close bracket. And that's our opt handler. Now, here we need to pass in info for output handler. And the way we can do it is simply by calling o output handler function and passing in layer. We could actually just based This one here as well. And we don't want to use this volume, but it's gonna be harder for you guys to understand. So let's just leave it as it is. So let's try to run it and see if that actually works. Usually it doesn't the first-time, usually something is missing. Okay? Solids, SRA, lname, and prediction of our prediction. Okay, so Chris stops prediction seven random number five, user one false. Okay, coal. Looks like positive scenario is working. Let's try to, Let's try not entering values that just leave them empty. Enter, enter. Okay, so please enter not required. Okay, it looks like it works well and we are asked to enter again. So this time I will enter my name, but I won't enter the prediction. So please enter data required. This folder should looks like it's working as expected. So this time I will enter my name, Chris stops and I will enter a prediction. But it's going to be in a wallet. There's going to be out of our range, so I will add 15. Okay, please enter prediction in given range, okay, and this seems to be working fine. So let's again crystals, my prediction is one. Okay? So I predicted one random number three and I didn't want because it wasn't the same. So it works. But one thing we forgot to do was actually to Ford Madonna code. So let's just click reformed code. You can see nine lines are formatted. Let's go up a lot today class and the form of code. You can see that it can shake wise function. Oh yeah, sorry. Functions in classes could be private. We are calling this function, so this one cannot be private, but this one can be. So I'll just add private function here. And we already formed cold, I guess, yes. And let's just report them, this one. And here everything is fine. Okay, let's check if it actually works correctly. If I, if the enter prediction and random number is equal, and to do that, I will cut this out and I will say return five always. And I will run the, our program again. And I will enter file to see if that actually works. As expected minerals are enters correct volume and one wins. So five, so prediction, random number, User One, True. That's awesome. So it works as expected. So I can put this back. So yeah, and this was a bit longer lecture and this was super, super easy CLI Program. I just wanted to show you guys how it looks when you develop a pen to end. I really hope you like this course and this lesson. And feel free to leave me feedback both positive and negative. And I am kinda newbie here in all these teaching environments on your feedback would be appreciated. I will try to link all the resources in my classroom. And if you have any questions, feel free to let me know. But nevertheless, thank you for attending. I really hope this was useful for you guys and maybe let's see you in another course in the future. Thank you guys for attending and feminized name.