Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello internet and welcome to my new java video tutorial in this one tutorial you're basically going to get a 1,000 page book on java crammed into one video underneath the video you will find a table of contents you can click on anything and jump throughout the video to all the different topics covered and in the description under the video you will find all of the code as well as a transcript of this entire video and I have a lot to do so let's get into it okay so I decided to use the Eclipse IDE because it's free and job works the same on everything so use anything you would like and what we're gonna do here first just come in and say that I want to create a new Java project and I'm just gonna call this Java tuts and that's fine and you can see I'm using Java 11 or I think you can see it that's what that says right there and I'm gonna come down here and click on next this is all perfectly fine and I'm gonna click on finish under the module name I'm just gonna call this Java Tut and go and click on create and everything is all set up here so I'm just going to close that guy and I'm gonna come over here and create a class and a class is basically I'm gonna get much more detail basically a class is just going to be a model of a real world system or object but like I said I'll get more into that later so I'm gonna go and right-click on that in the source folder and I'm gonna say I want to create a new class come over here and the package isn't normally gonna be something like a website or something unique so that you don't have any conflicts with anything so I'm just gonna call this calm new think-tank cuz that's my website I'm gonna come down here and say yes I want to create a main function inside of this class and then I'm gonna go and say that I went to the name of this class to be hello world and I'm gonna click on finish and that's going to be created for me this right here is a comment to forward slashes gives you a comments and if you would want to do a multi-line comment you just do a forward slash and a star and then start typing your comments inside of there you can see up here is the package that I set up and basically the class where all of your code will reside is going to be between this curly brace and this closing curly brace and execution of your program is going to start inside of the main function by default public just means that anyone or any code that has access to your class and this is your class name is going to be able to run this static means and there'll be more on this later in the tutorial it basically means that it is a function that belongs to the class like I said before we create objects and such and some functions are going to be specific to the objects we create and some will just be based off of the class if it says static it'll be class but like I said more on that later don't worry about it if it doesn't make any sense void means that this function does not return anything after it executes and string basically means that if we run our program on the command line and we're terminal that it can receive a string array just a whole bunch of different words or whatever you would want to pass into it and they would be stored inside of this function called args okay a lot of complicated stuff here that I will explain much later in much more detail now I'm going to be using a whole bunch of borrowed libraries of functions and if you would like to import some of them so one of the ones I'm going to use is a Java util and if I want to import all of them I put a star at the end of it and this is going to be used for array functions array lists iterators and a whole bunch of other different things I'm also going to be using something called int stream that is going to be used to generate ranges so if I want to go and get those functions that have been pre-made for me I can do that so I'm go to stream and int stream and for now that's all I'm gonna bring in there now down inside of my class I'm going to want to be able to receive input from a user and so I'm gonna bark this as static and this is a scanner objects it's going to be allowed to input information from the keyboard say new scanner and if I want to get information from the keyboard the Java way of referring to that is system in if you would like to make a constant variable that value is never gonna change you're going to start that off with the word final and then double is what this guy is going to be and I'm just gonna call this short pi is equal to 3.14159 okay so once you set that value it cannot be changed and that is called a constant okay so now what we're going to do is fulfill the whole hello world thing that we want to do here and if you want to output information into your console you're gonna type in system out followed by print line and what print line means is that it wants to print out everything that is between the double quotes and then add a new line at the end of it and there you go that's all you really need you don't need all this stuff up here to have a Java program but that's what you need to do hello world and then if you want to use Eclipse what you're gonna want to do is click over here there's a little downward arrow and you're going to go to run as java application and you're gonna see hello world comes up over there from then onwards you can just hit the little play button and it'll work so there you go now you know how to do a little world now what I want to do is talk about a variables now a variable must start with a letter and then have letters numbers underscores or dollar signs inside of them and to create a variable what I want to do here is create an integer which means that it's a number that does not have any decimal places and so let's create one and all your statements have to be ended with semicolons these guys are right there you're also going to be able to create multiple different variables at once just by going int and then v2 and v3 or whatever you'd like so you can have multiple different variables on the same line and it's just the basics all variables and now what I want to do is talk about datatypes now java requires that every variable have a defined datatype and basically what datatype means is how much space we are setting aside in memory to store the values we want to store okay so you can see all the primitive here there's byte short character boolean int float double and long and like I said they are all going to be able to store a minimum value as well as a maximum value and how we go and figure out what the maximum value or the minimum value of a byte for example is is to just follow up the datatypes name in uppercase letters and then followed by max value or min value and then if we run that you're going to see all of the minimums and maximums for all of those different data types on the right side of your screen to get more into the specifics a boolean is either going to have a value of true or false and you can't use 0 or anything else like you would use in other languages and so let's come in and create one and we'll say happy and we'll just mark that as true characters are only going to be able to store single characters and whenever you define them you are going to have to surround them with single quotes just as I did right there you're also however going to be able to store inside of character types things like new lines and tabs and back spaces and form feeds and returns as well as double quotes as long as you go and do a backslash or single quotes or back slashes in that situation floating-point numbers are going to be able to store decimals and they are going to have a precision up to six decimals as you're going to see so it's come in and do something like f num is equal to and just make sure if you want it to actually be a float that you end it with an uppercase F then what we'd be able to do is to output some information onto our screen and here I'm going to show you the precision of a float so I'm going to say float like this and then if you want to concatenate or join different types of data you just put a plus sign inside of there and I'll form those together you can also continue to the next line without any problem and then I can do something like f num plus F num2 and you're going to see that this messes up whenever we run it and see it six points of precision as you can see it messes up on the seventh place if you decide that you need more precision than a float provides to you you can come in and use a double instead so we could do something like double numb and we could go and get these same values and basically a double is going to have a precision up to fifteen decimal places and you don't put an F on the end of it of course and if we go and put that inside of there and then run it you will see that a double is much more precise than a float and you could come in here and tell you the double of course all right you're also going to be able to use scientific notation whenever you're creating your values so let's go double and thousand for example 1e + 3 and then we come in and print that value out and you can see that it works you're also going to be able to use Long's which are very large numbers as you saw previously the maximum size for those and sometimes when you're working with very large values it helps to separate things so you can use underscores for example like this as well okay so there is a rundown of the different data types are available and much more will be covered with them as the tutorial continues and now I want to talk about casting now by casting all that I am referring to is converting from one data type to another and by default you can always convert from smaller types to larger types so let's say we create an integer here I'm gonna call this small int is equal to 10 I can then go and get a long and go small long is equal to small whoops int like that and everything works without any issues however if you are going to be converting from integers and doubles and from large to small you are going to have to do that in a different way so let's go and create a double and I'll call this C double is equal to a 1 point 2 3 4 now if I would want to convert this into an integer I can and let's just call this C int is equal to all I need to do is put what I want to convert it into inside of parentheses like that and or no issues that will automatically convert it of course it will lose all of the decimal information however and if I go and output that you will see that that is indeed true and you just get a value of one if you are going to be creating a long go big long like this is equal to and you can go and put in whatever value would like inside of here and let's end that with an L to represent that's a long you can then come in and go int and be int equal to and know that you're probably going to get some weird results let's go and do it anyway just to show you what happens so let's convert a long into an integer and you can see that you do get a really weird value all right if you would want to be working with strings we can go and create a string just a series of characters that's all a string is and you'd be able to convert to a string by using a wrapper class so let's go favorite number is equal to double and two string so the two string works with any of the other data types to convert them into strings and then you can throw in whatever you would like inside of here and it's automatically going to work and perform that conversion and also you would be able to convert strings into all of the different data types by just typing in and byte for example and then following that up with parse byte boolean parse boolean and the same exact format as I'll show you right here so let's go comment that out and let's create an integer and I'll go string int is equal to and in this situation we will go integer and parse int and then throw our string inside of it and it's going to convert it from the string format into the format that we want which is an integer and we can do this but it's not really gonna matter either way okay so there you go and that is how you cast from one data type into another multiple different data types and now I want to talk about some math functions now of course you have all the basic types you can add you can subtract multiply divide this is modulus which gives you the remainder of a division and if we go and run that you can see exactly how the output works other things to be aware of is that math done on integers defaults to the integer output and doubles return doubles so if we would do something like this and 5/4 is equal to and then + and we'll throw in a double so we'll go five point zero divided by four point zero and close that off and run it you're going to see that we get double values here because we perform this operation using doubles there's different shortcuts in regards to performing operations so for example if we have Inc me followed by plus plus that is going to be equivalent so if we went Inc me equal to Inc me plus one as I'll show you here in a second so let's go and create Inc me and give it a value of zero to start off then what we'd be able to do is go down so I'm going to show you something that might throw you off because sometimes it's throws people off if we would come in here and do this and add this in and go Inc me plus plus I'm gonna actually show you the results and then afterwards I'm going to explain what happened so here we'll go Inc me get the plus plus off of there and instead put it in the front now what do you think it's going to provide you for output well if we run it you're gonna see that zero comes out and you think that doesn't make sense well what actually happens whenever you put the addition on the right side it goes and outputs whatever the current value of Inc me is which is zero then in increments down here in contrast when you put the plus plus in front it goes in ads now the value of Inc me is 1 so it becomes two and then it outputs so just something to refer to or something to be aware of in regards to that and the other shortcut that is available to you is if you wanted to increment the value of Inc me by 10 you could just go plus equal to 10 and that would increment that value and of course that would be exactly the same as if you would come in and go Inc me is equal to Inc me plus ten all right so just a couple little shortcuts and now I'm gonna show you a ton of math functions these guys are all going to be built in so you can get absolute value ceiling floor round and so forth let's go and run it so you can actually see what's going on and there is look at all those different guys so absolute value ceiling floor round max men exponents and all of the other different mathematical calculations you can perform using Java and there's gonna be all kinds of other cool things we can do with math as you'll see in a bit and we're also going to be able to come in here and create random numbers so let's go and create or define our minimum value that we want to work with or which is going to be equal to five so let's say I want to generate a random number that goes from five up to say twenty this is exactly how I do that so I can go int and random number is equal to I'm gonna go minimum number plus and then I want this to remain as an integer so I'm going to say int and that's gonna convert it into an integer and then I'll say math and the random function is gonna help us out here with generating this and let's say I want to go to the next line I can and then I'm gonna follow that up with the max number minus the minimum number plus one and now we come in here and see it so I'm gonna say R and plus and random number to see our output and if we run it you're gonna see at 11 and each time we were on it we're going to get completely different random values just as we were hoping for so that was a whole bunch of information about math now I want to talk about strings now strings are going to be objects or what are referred to as reference types and they're gonna have a whole bunch of built-in methods and you're going to see a whole bunch of them here in a second so let's go and create a string and let's just have this be Derrick now like I said before you're going to be able to combine strings with the plus sign as you already have done so we'll do something like whole name is equal to and I can go and throw the variable version of it inside of there if I'd like and then go and put the rest of my name and there we go we were able to combine those two different strings you can also come in and change the value of your variables if I didn't mention that before and let's say we want to join a string to another string we could do that is my name and there we go so you can use that shortcut for things other than just doing math now our conversion is going to be automatic whenever you are using primitives of any type so say doctors dog is equal to and K and then go nine like that and that's automatically going to work and each string that you work with is going to have each of the characters inside of it or they're going to have indexes or addresses or whatever you want to refer to them and how you refer to the first character is index 0 and then 1 2 3 4 and so on and if you want to actually get the character that is at a specific position like the 0 position you just type in character at and then run it and you're gonna see the d comes back you're also going to be able to search through strings to see if they contain other different strings so for example I can come in and I can go W name dot and see if it contains the other string I'm searching for here which is going to be Derrick and it'll come back is true if it does find a match for that string you're then going to be able to get to the index of that specific match as well just by going in and referring to the string once again and then you will say index of and then type in what you're specifically searching for and you can see it came back you're going to be able to get the number of characters that you have inside of each of your strings just by typing in length followed by your parentheses those are very important and one thing that's important is whenever you are trying to compare strings as you're gonna see later equals that represents equals is often used whenever you're comparing numbers and such you should never however do that with strings but you should instead use equals so for example let's come in and let's do something like dog equals cat and then we'll get our result from that so let's go and output this and how you should do string comparisons is to for example type in dog and then follow that up with equals and then inside of here what you are trying to compare it to which in this situation would be cat and as if you do that you're gonna see it comes back as false there's other ways that you can compare your different strings up and one thing I want to make sure I cover is if you want to compare two different strings you want to ignore case you would instead just go equals like this it's not gonna matter in this situation but we can go ignore case and that will allow us to compare different strings and ignore the case there's other ways to compare your characters as well let's say that I would come in here and go W name and compare two and then follow that up with something like ABC I could do that and basically the way this works is if this gives us a result of 0 that means that the strings are the same if it gives us a value of negative 1 that means that the string comes before and then anything above that is going to mean that the string comes after which is how we get 3 as a value like that now we're also going to be able to come in and replace any different matches that we have so let's come in and we'll go W name and replace and let's change this what we're searching for and what we want to replace is Derrick in this situation and we want to change that to Bob instead that's how easy it is to change those strings we're also going to be able to get a string using the indexes that I've been referring to and how you do that let's say we come in and go W name and sub string and say we want to start at the zero index through v do so and run it and you can see it just grabs my name out of there we're also going to be able to turn strings into arrays which we are going to cover more later on and what I'm going to do here is I'm actually going to use what's called an enhanced for-loop which I will cover more later on just allows you to iterate or slowly go character by character through a string for example so I'm gonna go string X and I'll cover this in more detail later if we want to convert a string into an array we would just go W name and then follow that up with split and then we would go and put a space inside here and that's basically saying everywhere there is a space inside of the string we want to convert the words before and after the space into things that go into there all right an array is just a whole bunch of little boxes that you store stuff in okay so what we're gonna be able to do with this enhanced for-loop is a neat little trick what we're going to be able to run and output all of that information you can see that it did that Derrick baños is my name it went and took each of those individual words and stuck them into their own separate boxes inside of them right other things that we can do that I'm not gonna cover is there's also going to be a trim function that's going to delete whitespace at the beginning at the end of the string you can do upper case like this to convert every single character in the string to uppercase and you could also come in and do the same thing with lowercase alrighty so there's a whole bunch of things on strings and a whole bunch more coming later and next I want to talk about string builders and buffers now if you are going to be making many string changes you may prefer to use a string builder for reasons every single time you created a string it creates a new place in memory and it can cause a lot of havoc so making a whole bunch of changes use a string builder instead of a regular old string and the only difference between a string builder and a string buffer is a string buffer is used whenever you're using threads I'll cover threads later on in the tutorial so let's go and create a string builder and how you do it is just go string builder like that and equal to new string builder and then you can come in and do something like I'm a string builder and of course you're going to be able to put this on a separate line if you'd like so let's just come down here and do so it looks a little bit nicer alright so there you go you just created a stringbuilder you're going to be able to come in and get the number of characters that you have inside of it just by coming in and doing length just like we did when we are working with strings you're going to be able to get the size that is set aside for your string builder because it gives you a certain size actually larger than what you requested whenever you created it and how you get that is you just come in and do capacity I'll run this in a second you're also going to be able to append a primitive or a string to your string builder just by using a pens and let's just do something like yay and there it is you're going to be able to insert at a specific index if you'd like so let's go SB and inserts and we'll say that we want to insert at the 6th index the word big and you could do that let's run it just to see what that is there it is I'm a big string builder yay see how that works you could go in replace at a specific index so let's do replace and we'll say from 6 through 9 and what we want to put inside there instead is wig I don't work you're going to be able to extract sub strings out of this so let's go and do something like SB & sub string and we'll go 6 through 10 and make sure that you keep the name that's exactly the same otherwise it obviously won't work you can also come in and delete characters at an index so we could do something like delete and 6 through 10 and then close that off and we can run it and see all our different results just like with strings with the exact same function name you're going to be able to come in and get a character at a specific index so we could go character at and we could do 4 and then finally we can get an index for our string that we type in so we could go SB and index off and then throw in a specific string and it'll give us that result as well if we run it you can see that all that works ok so there's a run of string builders and string buffers like I said just use a string buffer instead that obviously it's like this FF if you're using string or if you're using threads and now I want to talk about arrays oh right so basically an array is just boxes in memory that allow you to assign a name to multiple values instead of just one value so let's go and let's create an integer I ran that's exactly how you do it and let's say that we would like to set aside ten spaces for our numbers so we'll just go int and then say ten and that's what it does it sets aside ten boxes to store ten integer values we can go and assign them using indexes just like this so let's say we want to put one in the very very first box in the array that's how that's done we can also come in and fill the array with a value so we can say arrays and fill and you just put in what the array name is and what value you would like to fill every single little spot with and that's gonna put a two in all those spots also going to be able to get values by referring to indexes so let's go and get that you're going to be able to come in and get your array size just by following that up with the word length as you can see we're reusing a lot of things you're going to be able to create and add values at the same time so let's go and go string and a2 is equal to and we will throw in lycanthrope strings inside of here so we'll say one and two like that just make sure of course if you call this a string array you have to put strings in it if you call it an integer array you can't put doubles in it okay just the rule you're going to be able to generate an array so let's go and go int and let's call this one two ten and I'm going to use the integer stream which I'm going to cover in much more detail later so you just go range closed and I want to generate values from 1 to 10 and so out of this array and then I need to convert them into arrays so there we are from the integer stream you're going to be able to use enhanced for-loop that I talked about before and how this works is it's going to cycle through every single value in the array temporarily storing them into this variable that we call X then you're going to put in whatever the name of your array is one to ten and since this is a single line statement that we want to work with here you can put that all on one line you don't need to surround it with curly brackets and the world will be fine with that and there you can see I cycled through all of them and print them all out on our screen you're also going to be able to find values you're gonna type in arrays and binary search and you're going to type in whatever the array is that you want to search through and I'm specifically searching for a value of nine inside of that array and it's going to tell me if indeed that is found or not or it's gonna tell me what index have found it in that situation which is eight they're also going to be able to create what are called multi-dimensional arrays so this is just a raise inside of rays and to create one you just come in here and put these two brackets if you want to do a just two of those so we'll go equal to new and then inside of it you have to say how many different spaces to set up inside of those guys so let's just go like that and there you go you just created a multi-dimensional right you're also going to be able to create and initialize at the same time and one thing that's kind of confusing is sometimes people wonder exactly what this looks like and you know what the multi-dimensional array looks like basically what we're saying here is how many down or how many rows we want to set aside like matrices with you know mathematics and this number tells me how many acrost so let's go and let's create and initialize at the same time I'm gonna go string and let's say that I want to create a multi-dimensional array I'm gonna call it a four and I'm going to initialize it at the same time and how you do that is just multiple curly brackets so we'll go 0 0 & 1 & 0 and then we're going to close 1 curly bracket and then create another one so let's go like this get that to line up a little bit better and then we can say 0 1 & 1 1 one one like that and then we close off both of the curly brackets at the same time there you go you went and created and initialized it at the same time now I got a stray curly bracket here and we come in here and we'll actually print these values so that we wanted to print using an index for our a4 let's say we wanted to get the first row as well as well this is actually a second because remember it starts at zero so the second row second column value is what we're looking for and we can run it so you can see that that indeed is 11 which is this guy right here let's go and create a three dimensional so let's go string and a five and if we want to have three dimensions you just go and do exactly that and then if we want to go and put different values inside of there one two three four working with three dimensional is that you're going to have three of those curly brackets and I just went ahead and created that and if we want to go and output a specific index on the screen we can just go a five and we'll do something like two and three and 0 and whenever we're working with three dimensional arrays remember I said before the first value that we have here is basically going to be our rows meaning this guy is rows and the columns and this would be like pages so you think of this as a three dimensional object and if we run it you'll see it we got 320 to show up there and I'll be doing all kinds of other cooler things with arrays as the tutorial continues let's go and I'll show you how to copy one array into another world another array so we'll go like this and we will initialize let's go a six like that is equal to and we'll just something like one two and three and then we'll go and create another array so this is gonna be a seven is equal to and if you want to copy one array into another array you just go raise and copy of and the array that you want to copy from and then how many values you want to copy specifically you're going to also be able to come in and compare arrays so we'll go raise and you're going to use equals once again and compare a six with a seven and also you're going to be able to sort arrays so let's go and get a eight and let's initialize an array that does not go in order so we can see that we can get the computer to do the heavy work for us and how you do that is you call the sort function by passing it the array that you want to sort and then we can come in and go and convert an array into a string that we can output just by going arrays to string and pass in a eight and if we run it you can see that all works all right so there is a whole bunch of things on arrays more coming up later and now I want to talk about array lists okay so people use array list because they resize and provide for easy insertion and deletion versus a regular and array and how you would create one as you go array list and string this is if you want a string array list and you can obviously put anything you want inside of them and let's say that I want to have 20 spaces set aside whenever I very first start them off this is how you would do that and then just put the 20 there at the end to define how big you want it to be now if you want to go and add values to an array list you just go add and you can put something like su you can also generate an ArrayList and here I'll show you a way to do it so we can go ray list and this time I want to generate an integer array list note the upper case I that you have there and then we could come in and go array lists and then I will generate one so I'll go arrays and follow that up with AZ list and then I can just throw whatever values inside of there that I would like and then I want to of course make sure that I close this off with a semicolon at the end so there is how we generate that and we can come in and use our enhanced for-loop to go and print out all of the different values and like I said previously everything will temporarily be stored inside of X as we cycle through it and because it's just one line we can just in here and put this all on one line and then we can end that with a semicolon and I'll put that information and you can see there it is on the screen and you can also note I didn't put print line and that's the reason why everything showed up on one line instead of two we can of course come in and get different values out of here as well this time I will use print line and if you want to get an individual value based off of the index for it let's say we want to get the second value out of there you can of course do that you can add a value at a specific index just by coming in and saying set and let's go one and let's throw a five inside of it you're also going to be able to come in and remove values by typing in remove and let's go and say we want to delete what is it the three index you would also be able to clear the entire array by going al two followed with clear that would delete everything and then let's talk about iterators basically an iterator is going to allow you to cycle through collections like array lists and all the other collections that we have seen and will see and to create one you just go iterator and I'm just gonna call this IT is equal to a l2 and iterator and there you go now you have an iterator and to use it to cycle I'm gonna cover loops here in a second of course you can always jump around within the playlist and how you call for each of the individual values in the ArrayList is has next and then we command and output each of those individual values so we can just go it and next and as long as there is a value to output this loop will continue looping forever and then it'll output all those different values ok so brief overview of some things you can do with ArrayList I actually prefer linked lists and that's what I'm gonna cover next now linked lists are basically used or best used whenever you find you have to make many changes in the middle of your list I have an entire tutorial on how they're created but basically the way they work is that each item or link in the list has only a reference to the value before and the value after it and how you create one as you come in and go a linked list and let's say you want to work with integers and let's just create one here equal to new and then linked list again and then of course integer and then you can just say whatever you want inside of it just end it at the end there and then if you want to come in and add a value you can just do add and throw one and value inside of there you can also come in and put multiple difference statements as long as you have semicolons on them all in one list I don't know if I said that before or not so let's just go and throw a couple values inside of there you would also be able to add a whole list of items just by going add all and then using arrays as lists and then throw a whole bunch of different values inside of there you would be able to add to the front by going add first and let's just throw zero inside of there you would also be able to use add last but I'm not gonna do that you can do it on your own you can check if a value is inside of a list by just listing that and then following it up with contains and whatever value you are looking for you can get the index for a match just by typing in index of and the four or whatever the value is you can replace values by saying set and zero and two for example and I'll output all of this all at one time you can get values just by going like this and then following that up with get and let's say we want to go and get what's in the zero index I'll just run that just you can see what that looks like and there you go you can also call get first and get last to get the first and last item you can remove items just by using remove and then follow that up with the one there and you'd also be able to do clear which is gonna remove all values and also be able to come in and get the number of items that you have inside of it just by calling size and you would be able to convert it into an array let's just go and create an object array which is the most basic form we'll go to array and of course I can use the enhanced for-loop here to output all the different values that are in the linked list alright so there you go rundown of linked lists and now I'd like to talk about user input well now I'm gonna show you how to use the scanner to get some user input and let's just go and system out and we'll do something like enter name and if you don't want it to give you a newline get rid of L and of course now what we need to do is worry about what data type we are going to be working with so what I'm gonna do here is I'm gonna check if the user actually entered a string so I'm gonna use the scanner object up there and I'm gonna go has next line which is what you use if you are going to be receiving a string input and make sure that this is uppercase and what you would do is instead of line line represents strings if you want to check for any of the other different data types you type things like shorts or integer or whatever the different data types are then if we come in here and we did get a string what I can do is say username is going to be equal to and if I want to get that string that they entered I go next long and then I can get it and of course just replace the line with things like shorts and integer to get them to work with the other different data types and then I can go and do an enhanced hello world so I can just go like this and then follow that off with a username and if we run it it'll say and her name and I can come in here and say Debbie and it'll say hello Debbie alright so cool stuff you can also go and use dialog boxes so let's go and open a dialog box I'll call this job name is equal to and then we can go down to the next line and then what I want to do is go J option pane and show input dialog and I can put my message inside of here which is going to be enter name and then I can go system out and do a hello world thing again just by going GOP name and it's giving me a underline little error here inside of Eclipse you just put this here and it'll give you the option to import J option pain import J option pain Java axe swing just do that problem goes away and we can save it and run it and her name and I'll say sue hello sue and you can see here is the dialog box and I can go derrick and click on that and hello Derrick shows up alright so two different ways to use user input now I want to talk about conditionals now if you want to perform different operations based off of conditions you are going to use relational operators and that is just a fancy name for things you've seen before which is equal to not equal to greater than less than greater than or equal to and less than or equal to and then you can use multiple different comparisons using what are called logical operators and those are going to be things like knots which is going to give you the opposite of whatever you give it so if you say you go not true it's gonna give you false and not false it gives you equals true and then you also have and and you also have or so I'm just gonna give you a whole bunch of examples that show you exactly how those work so let's go and create an integer called age and let's say the age is 12 what I'm gonna do now is I'm gonna say if the age is greater than or equal to 5 and whoops and we also have an age that is less than or equal to 6 well in that situation the curly brackets are very important as are the parentheses I am going to output go to kindergarten see both of those need to be true otherwise this will not print now if you would like to have another if statement inside of here what you could do is say else yeah and then you're gonna do another condition like this so you can stack those inside of there so we can go like this and in this situation I'm gonna say 7 and I'm gonna say 13 and then the message is going to be different of course so I'll say middle school and then we just keep on going so let's go get this guy right here go to the next line else if and then I can change this to 14 and then I can change this to 18 and then I'm going to have one more and that is going to be just a simple closing of that curly bracket and then I will list my default and that is going to be stay home so I'll just grab that guy throw that inside of there and I'll say stay home and you'll see how this all ends like that and if I run it go to middle school comes back all right so I'm gonna do a couple more examples here let's do I'll show you exactly what the or operator does let's just go like that is equal to and then close that off and then add on what we're doing so we can just go true and then or all suppose that guy and there you can see that that comes back as true as long as one of the values is indeed true we can also come in and I'll show you what not does so not true is gonna be equal to and then we can just do not true and like I said it's going to provide false just the opposite of whatever you are doing more examples you also have something called the ternary operator which returns one value if the condition is true or the second value if it is not so I can say something like boolean can votes and can vote will get the first value that I'm going to define here if the age is greater than or equal to 18 and you just follow that up with a question mark and we're gonna give it a value of true if that's true otherwise we will give it a value of false and of course you could give it the value of if this was a integer you could give it a value of 1 or 0 or whatever you'd like doesn't have to be true or false and we can come in and I'll put that right like that there's also the switch statement which is often used when you have a very limited number of options so let's go and create string Lang is equal to and France this is going to provide different output depending upon the language I define so I'm gonna say switch throw the thing that we're going to be searching for value wise curly brackets of course and then we're going to put in our different values so I could do something like chill a and if you want to check for more than one you just end that like that and then go and put another case on there and we could do Cuba and then we could put our condition inside of it which is basically going to be hola which I think I pronounced that correctly all right and then to end checking anything else we put break if we don't put break inside of there it's going to check the next value inside of it and then ultimately print out what is in default so you normally are going to want to put a break statement inside of there and here we could go Bonjour and break again or Japan with konnichiwa or if none of those end up being true we can come in and do a default and then out put on our screen hello so there we go and if we run that you're going to see that Bonjour comes back just as we were expecting alright so there's a whole bunch of different ways of handling conditionals and now I want to talk about looping okay so Java like most programming languages is going to have a for loop and what you do is you assign a starting value and then you will continue continue looping as long as this condition is true and then you have to define how much you want to change the value of I each time through the loop and you could then come in here and just print that value and it's going to do basically what you would expect while loops are going to continue to execute as long as the condition is true so we can just come in and go WL is equal to zero you're gonna have to put your value outside of the while loop like that instead of inside with your for loop and we can go and say that we want to continue cycling through the code between the curly brackets as long as that is true so let's say we will only want to print even numbers we can say if and like that and anytime you do modulus of two and it comes back as zero you know you have an even value because there's no remainder whenever you divide by two with an even value and we can go in and print that value and don't forget to increment the value after you do that change this to uppercase and then if you want to skip executing code that comes after the curly brackets you'd go and put continued in there and it jumps back up here and starts executing from the beginning so that's what continued does and then we could throw another condition in here and we could say something like if WL is ever greater than or equal to 10 in that situation we want to stop looping all together and if you ever want to stop looping all together you type in break and that's the end of that and then of course don't forget to increment the value of WL and if we run it you can see only the odd or even values up to 10 or print it out on the screen so that is the while loop now let's talk about the do-while loop which is basically going to definitely execute at least once let's do something here a little bit fun let's go and put in secret number and say that the value is going to be equal to you tell me 0 through 10 how about seven all right so then I'm gonna put a guess inside of here which is going to be equal to zero and then I'm going to go do and we're gonna continue executing as long as the user does not enter in the value that we want them to enter in so we'll say guess and we can go and put this on separate lawn or get rid of the newline I mean then we can say if and check with the scanner to see if they entered an integer or not and then we'll have guess is equal to SC next int and then what we do is put the WoW part at the end of the do-while loop so we'll just come right here and we'll say while secret number is not equal to guess and then like that and then we know also that once they get out of this loop that they actually guessed the secret numbers we can put it down there alright and the way this operates is we have guests and we can go five and it's gonna continue asking for guesses until I have seven and then you guessed it prints out there's a couple more ways you can loop I of course I've already talked about the enhanced four I'm going to show you a couple other different ways but now I want to talk about methods now methods are basically going to allow you to avoid duplicate code and help you organize and let's go and you have to create them outside of your main function so I'm just gonna create a whole bunch of them here so let's create one that is going to receive to values and return a sum now I'm gonna type public inside of here and static int get some and then I will list the two values along with the datatype of the two values that this function or method is going to be receiving and basically public methods are going to be able to be executed by any program that knows of your class you could also come in here and mark this as private if you do that that means that these functions or methods are only going to be available inside of the body of the class and if you go and mark it as protected that means that the methods are going to be available to your class as well as any subclasses that you create and I'll cover more on subclasses in a little bit static like I said before means that it belongs to the class and not to any objects of the class again more on object oriented programming later it means that's going to return an integer so let's finish this up by just going return and then adding these two values together and returning them now down inside of main we can actually call our function so let's go and say something like five plus four is equal to and then just call our function to get that sum by passing in five and four and run it and you can see we get our answer okay so simple method will get more complicated now one thing that is very important to understand is that all data that is going to be passed to a method is going to be passed by value so changes in the method have no effect outside of your method so let's go and create an integer call this C num for change number it's give it a value of zero then I'm gonna call a function that I'm gonna create here in a second called change me and then I'm going to output the change here just to demonstrate this so change number or value is going to be equal to a change number so we're going to be passing change number into change me which we're going to create right now so once again this is going to be public and static it's not going to return a value so I'm gonna put void inside of there and change me it's going to receive the change number variable and it is going to change the value or try to change the value that is but as you see whenever we run it it still has a value of zero so what happened here is zero was passed in however a new variable was created that is only inside of the change me method once the changed me method ends right here this variable no longer exists so I passed a value of zero we changed the value to ten but it has no effect on this even though the names are the same they are completely different variables now let's say you would like to be able to provide a variable number of parameters into your function so we're gonna handle that what you would do is go public static this is gonna return an integer and we'll create the new improved get some two and it is going to receive integers however it's going to receive an unknown number of integers and it will still be able to handle it what it's basically doing is receiving a array we can now go in sum is equal to zero and then create our for loop here which is gonna cycle through that array that we created and it's going to say sum plus equal to whatever the value of x is after it cycles through that entire array it is then going to return sum and pretty cool stuff now what we'll be able to do is go and send in any number of variables and it will just work so let's just have this be sum and we will say get some two and we'll say whatever we want to do so that's a nice way to be able to handle any number of variables pretty cool stuff now let's say you wanted to return multiple different values how would you do that well let's go and let's create it once again public and static and I'm gonna do get next to which is gonna receive a value and then return the next two values that come after it so it's going to receive an integer and then it is going to create an integer array and that's going to store two values int and two and then what we can do is say values in the first index is going to be equal to whatever the value was that post passed in and then we can say once again values and the second index it's gonna be equal to X plus two and then to return a array is no big deal just go like that and there you go all right so we have that all set up whoops make sure you come in here though and say that we want to be returning a integer all right and also just so you know you don't need public hearing get rid of it and everything will still work out perfectly fine and then once again down inside of our main function I'm gonna create an integer array and let's just call this mult VA is equal to and then I can say get next to and then pass in a value of five for example and then I can go in and output that so we can just go int X because those are integers inside of the array that we're working with and malt multiple values and then let's go and outputs each one of those different values that are in there and that is how we're able to return multiple values from a method another thing we can do is we can actually receive multiple values of different types and how you do that is you can create a list using object and every single thing inside of Java comes from this object object and object class where we want to call it so we can work with it so we can say something like get random list and we can get a whole bunch of different things and I'll create that a second and then we can come in and output the random list whoops just by doing this get rid of that guy and there it is and now we will create random list or get random list so again get random list and then we put the return type for it wisten which is list and it has objects inside of it and we can go get random and don't forget to put in the return type close that off and then within the curly brackets I'm just going to go define a whole bunch of things so string name is equal to Derrick and there we go and then I can go arrays as list and put name and age inside of it and return those values and if we come in and we execute it make sure you spell this right print them the list now I got it and get rid of the OM on there all right so everything's fine now and you can see that it returned that list of random different datatypes items now I'm gonna do something neat we are going to go and create a recursive function that is going to call itself so let's go and calculate factorials so this is going to return an int and let's just call it factorial and the what we want to get the factorial of now whenever you're creating a function that actually calls itself you need to somewhere inside of it have a condition where we will no longer call the function and that is this guy right here so we're gonna say if num is ever equal to one in that situation we are going to return a value of one all right so that handled that condition else what we're gonna do is we're going to call the function over and over again so we'll go result is going to be equal to num times and factorial num minus one and you'll understand this at the end so don't worry about it if this is a little bit confusing if you never heard of a factorial before don't worry about it I'll completely explain it in a second all right so that's all we need to calculate a factorial now what we can do is down inside of main type in something like fact four is equal to and then call our function so factorial and four and I'll put that on to the screen and if we run it you're going to see we get a value of 24 and the way that works is I'm gonna walk you through the whole process whenever we first enter into our function we are going to provide it a value of four which is right here let's get rid of that so we provide a value of four which comes inside of here is the number equal to one of course not we come down here we go and have four times factorial 4 minus 1 which gives us a value of 3 so we call the factorial function again with 3 which is sky right here we take the value down to two and then we have this we jump down here two times factorial one in this situation has a value of one it returns a value of one which gives this or a value of two which jumps up here which gives a value of six that six jumps up here and gives us our final results so that's how recursive functions work and that is how we would calculate factorials and I'm gonna do a lot more with really funky functions here later on closures and such but I want to get one more demonstration in which we pass an array into a method so let's go nomes is equal to 1 2 & 3 close that off and then we're going to have a function let's copy this again which is going to print out a sum so sum is equal to and we'll call it get some 3 and numbers and then let's create get some 3 and how you define that you are getting an array is this is going to return an int as you go get some 3 for example and you go int and put the brackets inside of there and numbers sometimes it makes sense to put the length inside of there as well and then we can go int sum is equal to 0 and then we can in cycle through this guy integers of course and this will be gnomes and then we can go and have some equal to each of those individual values of X and then return the sum and if we run it you can see that we got the value of 6 there so there you go that's a ton of information at least to start off more on methods in a little bit but now I want to talk about enumerated types all right so sometimes it makes sense to have custom types that have a limited number of options so let's say and you have to put them outside of the main function we need to define them so I'm gonna go and I'm going to create a public enumerated type and I'm gonna call it day and it's just to keep this brief I'm just gonna put a couple days inside of here so I'm gonna go Monday Tuesday and Wednesday all right so there are our different days now down inside of main I can actually define my day my custom data type and I can something do something like favorite day is equal to day and Monday and then let's go and output that favourite day and is and then just go favorites today you can see that it's automatically going to work is Monday alright so just a very very very brief explanation of enumerated types and now I want to talk about exception handling now exception handling is going to be used to catch errors that could crash your program so you're basically just creating them to protect your code and what you do whenever you want to try to block one of those errors is you're gonna surround problematic code with a try block which is what that is and inside of it we could go and do something like int and bad int is equal to and do a division by zero and then if you do have that you're going to want to catch that potential problem and how you do that is with catch and the thing we want to block here is the arithmetic exception and we'll give it a value of e^x and we're going to use that e^x if we do have this problem to come in here and output a message so we could do something like can't divide by zero inside of here or we could go and get other messages so we could do something like get an automatically generated message such as a get message is going to provide for that specific exception or error that we had and we can go and also print this out and stream from string format which we'll see what that looks like here in a second and close that off alright so that's going to catch a very specific exception if you would like to catch all exceptions and this is normally not good to do but people do it anyway this will catch any exception and provide a error message so we'll go e^x gets message again and then you will also have the opportunity to clean up after any of this code has been executed you could do things like closed files or databases or other cleanup things inside of the finally block and then you could just put something in here like clean up you'll see later on when we talk about 5io how that would be used alright and if we go and run it you can see can't divide by zero comes on here and then it prints out a longer message and then clean up also comes on here but you could also throw custom error messages if you would like and what we will do is actually just get rid of that guy right there and instead go throw new you could check for some problematic thing there that there is no exception for and you could go exception and bad stuff and this is automatically going to work as well and if we run it you'll see bad stuff comes up there and this guy right here actually clocked at that one alright so brief overview of exception handling and now since Java is an object oriented programming language let's do some cool object-oriented programming examples alright so now what I want to do is talk about object-oriented programming so what we're gonna do is we're gonna come over here and we're gonna create warriors that are going to fight to the death so our first thing I want to do is create a class and I'm going to not have a main function inside of it and the name of my class is going to be warrior because that is what I'm trying to model so I'm gonna come down click on finish and here we have our bare-bones warrior class now with object-oriented programming it's very simple what we are trying to do is every single real world object has attributes such as height and weight and capabilities such as having the ability to eat and run for example in object-oriented programming we store attributes in fields and we model capabilities using methods so one we're just gonna come in here we're gonna list some attributes a real world warrior would have so we're first gonna come in and mark this as protected and what this means is it is code or it is a field that is only going to be accessible inside of our class that we're creating and also subclasses will be able to have it if we mark this as private subclasses would not have access to this thing and that thing is going to be a man we're going to say the default name for warriors is just simply warrior then we're going to say other things are going to be public and such as our health our health is going to be very important for a warrior so we are going to save it public means that this information we will be available anywhere so also we're going to have an attack amount that our warrior can do and also we are going to have a block amount that our warrior will be able to do all right so there we go we have our attributes for a warrior now what we need to do is to initialize or set up a warrior whenever it is created you do that with a thing called a constructor and a constructor has the same name as your class and a constructor can be so simple as just that it basically whenever a new warrior object is called this warrior constructor function will be called now we could also come in and set attributes which are a little bit more useful so whenever a warrior is created we can in say that we want to accept a name and give a data type that we want to accept health and have that be inside of there we can also say that we want to receive a attack maximum amount that the warrior can attack with and also a maximum block amount so I have this big block max and there we are and now we have that set now if we want to refer to these attributes for a warrior and we do not have a warrior object created how do we do that we have no way of referencing it well we have something called this that is just a way of referencing a object that we do not have a name for what we're gonna do is we're gonna create setters and getters for this named guy here so I am just going to create this first and set the name well why don't I create the getters and setters also and the reason we do this is because we do not have direct access to name so what we need to do is provide methods that are going to allow us to provide access so we're going to say that we want to provide a string get name and whenever this is called it is going to just return the you of name now the reason we use getters and setters is let's say that we did not want to just return the name that we would maybe want to return the name plus something else well we could provide special outputs for example if we had something like wait we might want to return kilograms or pounds or something added on to whatever the value of weight would be signs also let's say that we would want to come in here and provide a way to set the name like we did up there we can do so and the reason we do this is let's say we don't want the name to contain numbers well if we could go and check as you're gonna see with regular expressions later on in a tutorial we could check to see if it's actually the name contained a number and we could say no we will not accept that name and then not set it okay so that's why we have getters and setters and things like that all right so now what we can do is we can go and set all the other attributes such as health which is going to be to whatever the health they sent in and we can do the same thing for attack maximum amounts which is going to be whatever the attack max was that they sent and also made that an uppercase K all right fixed that and then we could also have a block max which is gonna be whatever they sent in for that okay so now our warriors have a way of being initialized now what we needed to do is to model the capabilities of a warrior now a warrior is going to be able to attack as well as block so we are going to name it that way and it's going to return an INT which is going to represent the attack amounts and inside of here I'll just use a random number generator to provide just a random attack amount and how we did I covered randoms before so I'm gonna say that I want to return one plus and I only want to work with integers in here just because it makes it simple call math.random and then we will multiply that times the attack maximum amount minus 1 if you want to know the details of how the random function works you can just click inside the table of contents for this tutorial and jump directly and see all the details alright so there we go so we have that and we're going to do the same thing for our ability to block so we just want to keep this whole entire thing as simple as humanly possible so that it's completely understandable and as you're gonna see I'm going to break up a whole bunch of this whole entire thing into multiple different files so it'll be very very easy to work with and then we can come in here and do this and then we change this to block max instead and everything else is exactly the same so that's all we have we have a warrior who has a name a health attack mount block amounts we initialize it and it has the ability to attack and block and that's it that's all we need to do for now so now what we want to do is add more capabilities so that the warriors can actually battle and again we are simulating a real world scenario and the scenario we're simulating is a battle so we will come over here and we will create a new class again it's not going to have a main function and it is going to be called battle because that's what we are simulating and click on finish and now we have that so inside of our battle class this is basically a utility class and so in that situation it makes sense that all of the methods inside of it should be static and like I said previously static methods and fields should be used whenever it doesn't make sense for a real world object to be able to perform some things or when values should be shared by multiple objects or in this situation where we have basically a utility class that's going to simulate a battle between multiple warriors so what do we want to do we want to have a situation in which we start our fight and it is not going to return anything so we will say start fight and it is going to be passed a warrior and which is gonna be warrior one and it's also going to receive another warrior that we are going to refer to as warrior two we are going to use a sleep function which is reserved for threads and such so I am also going to come in here and add in an exception but I'll just do that in a second so basically what I want to do here is I want to loop giving each character a chance to attack and then block I will simulate that by putting in true inside of here and then I want to create a function that is going to provide the attack result so we will go and static again breaking up everything into very very small pieces of code that are very very understandable so get attack result this again is going to receive a warrior and I'm going to call this warrior a this is going to represent the attacking warrior and then I am also going to have another warrior passed inside of it and this is going to be warrior P and that's going to be representing a warrior that is blocking first thing I want to do is get our warriors attack as well as our blocks so a warrior attacking attack amount is going to be equal to whatever warrior a is and call the attack function in the warrior class that we created also I'm gonna have warrior be the block amount and I'm just going to refer back to that and we can get that by going where your be and calling the block function so everything is very much based around trying to simulate reality what I need to do then is subtract the block amount from the attack to find out the overall damage so I'll go damage to warrior b is going to be equal to warrior a attack amount and - the warrior b block amount so pretty simple straightforward stuff i'm then going to check if the damage is going to be greater than zero before I go and try to decrease it so I'll say if damage to warrior B is greater than zero well in that situation I will go warrior B who's you know blocking health is going to be equal to where B health - the damage to warrior B and is that all right so like this and that's gonna be there okay so got that all set up else if it isn't well then I'm just going to say damage to warrior B is equal to alright so simple stuff so we got that all out of the way what do we want to do now well I want to print some information about what happened during the attack and what I want to do here is use the print F formatting function to provide the more nice-looking output so what I'm gonna do is go system and out followed by print F and I want to have a string here so what I'm gonna do is put % s and whatever a the string variable that we're gonna have here is going to be placed directly where that percent s is and deals and then I'm going to add on to this let's go to the next line so I'll say plus and then I'm going to put a integer and integers are represented with percent D I'll provide a list of all the different types of variables you can have here I then want to put a new line inside of there and then what I want to put right here in my output is going to be the warrior a's name and i call the get name function that i created to get that and then what I want to place here in our output is going to be warrior B's name so I'm like a warrior B and call get name on that as well and then I need to put the damage amount and the damage amount is going to be damage to warrior be the warrior that is blocking and there it is and like I said printf is going to be used for formatted output anywhere you want to place a string you put a percent s integers percent D floats at percent F if you want to say limit the output to two decimal places you put point two in between the percent and the F and you have characters scientific notation dates and Bolin's all right so you can play around with that to get more acquainted with printf I'm gonna go and use printf again however so let's come in here and get this so after I print that out now what I want to do is print out the health changes so like this and I will go percent s has percent D this is an integer health and then let's say we want to put in two new line just to separate our output code so it's easier to see because there's gonna be a whole bunch of things just flying all over the screen whenever we do this so now what I need to do is get the blocking characters name or warriors name and then follow that up with the blocking characters health so we got both of those things and there we are so we have the attack information and all that now what I want to do is throw a sleep period which means I want to pause the output for 15 hundred milliseconds and to do that I just come in and I'll do more on threads here in a minute so that you know what a thread is don't worry about it it's not something I don't cover and this just requires that I come in here and add in a ability to handle an exception so I will say add throws declaration and I can just do that to the method itself and you could say that a wins right there and then we can come down here and continue adding in our additional code now what I want to do is I want to check if the health has fallen below zero or not because remember start fight is going to be calling this function and it needs to know when the while loop needs to terminate so I will come down here and figure that out so I will say if the blocking warriors health is less than or equal to zero then I want to end this whole entire thing so say system and well I have that safe don't I yes all right so I will come in and go that has died and the other warrior is and we will go and add that in victorious so like this and I put a space in there right is victorious so and then we can throw in a new line and then we can go and add in this additional information we're just going warrior block is and get the name because we know that Warrior has died we have to put the percent signs in there because we have to call the getter on that and then we will go and get the attacking warrior and I'll put their name showing that we were able to have a victor in our little combat here in that situation what I'm gonna do is I'm just going to say return game over and I can show you how they do logic in regards to this and otherwise else I'm going to say return and I could actually put anything in this but I'm just gonna say fight again and there it is alright so that's how the whole entire handling of attacks works and now what I just need to do is iterate and have the Warriors fight against each other and how are we going to do that well basically I'm just gonna say if get attack result this guy right here I'm going to call it in multiple times so gets attack result and I'm gonna pass in warrior 1 and warrior 2 into this and then I'm gonna check to see if it is equal to or equals game over meaning that they fought to the death and everything's done and then in that situation I'm just going to output some information here whoops and that information is just gonna be game over I could put in you know the game over that actually came passing in there but whatever that's fine and then now that I got that I can then say break to jump out of my loop and this is also giving me an error because of the thread sleep down inside of here so I can just come in here like this and then say throw add throws declaration and there that goes and that's going to protect that as well alright so I have to give both warriors an opportunity to kill each other so I'm just gonna copy this and then I'll change this into a 2 and this into a 1 so both warriors now will continually attack each other until they die all right so I got the battle all set up so now what I want to do is have a completely different class setup that is going to run all of this so once again come over into source and right-click and new class and I'm just gonna call this warrior game and in this situation I'm gonna want a main function so I'm gonna say warrior game and yes I want main and everything else here is fine and let's click on finish all right so we got this and then everything is going to be taking place inside of our main function so what do I want to do well I want to create a war so just go warrior and let's say it's Thor and to create this warrior I just go warrior and then I pass in the name and then I just have to decide what I want the health and so forth to be let's say I just make it 800 and then the attack amount Thor is pretty strong 130 and the block amounts going to be 40 and I'm gonna do the same thing for Loki and then have them attack each other and then I'm gonna do all kinds of other things where I give Loki more abilities and all this other stuff so I'm gonna say Loki and there and change this to Loki and let's say that Loki's attack amount is less than Thor's and there we are and then how much does it take to come in here and actually have them fight each other I can just go battle start fight and pass in Thor and Loki or whoever I would want to pass into this and it's going to just automatically work and it also wants me to handle that exception you normally wouldn't do this stuff with threads but I'm doing it just so that I can make it look better on the screen so add throws declaration and that's okay and we'll run it and there we are and you can see our output as Thor attacks Loki and then Loki attacks lore and on and on and on and this will continue until probably more than likely four kills Loki so what we're going to need to do now is go in here and actually give Loki more abilities so he has a better chance of defeating the four and there you can see is the final output Loki has died and Thor is victorious so Loki is kind of a sneaky little guy so why don't we go in and let's give him more capability and how we're gonna do that is by creating a subclass that is called dodge warrior so we're gonna come over here and we're going to create dodge warrior so just this right-click and class and this is going to be a Dodge word which is gonna give Loki the ability to dodge so we'll just go dodge Warrior and no main again and let's click on finish and let's add some capabilities first thing we want to do is we would like to get all the attributes and capability in warrior so we can do that just by going extends warrior and that's all we need to do and now all of a sudden everything that's in warrior is also in dodge warrior so what are we going to need to add in regards to attributes well I am going to say that I want to figure out the chance that Loki is going to be able to dodge an attack from four and I'm gonna call that dodge percent and I'm also going to want to randomize that so let's go and get Rand as he puts a new random all right so we have that set and let's just go in here and get that library import random and there it is so now what do we need to do well we need to initialize our dodge Warrior so we'll just go public dodge Warrior and this is the constructor and it also is going to receive a string for the name it's going to get health just like before it's going to get a attack amount all right attack max it's going to receive a block max and it is also going to get the added capability of how likely it is that Loki or our dodge warrior is going to be able to dodge now it'd be great if we could just have of our warrior class constructor handle all this for us like it does there and we can send how we do that is to call the warrior constructor we just say super because it's the superclass and we pass in all the things that it can initialize for us and then we handle the things that it can initialize so we'll say block Max and there we go and the warrior constructor will handle that and then we just need to handle the things that are not in warrior inside of here so dodge percent is equal to dodge percent and there we are and now what we'd like to do is go and change the things in warrior that we want to change and the only thing we want to change is the block so this is going to overwrite what is inside a warrior the block ability there's block so we're overriding that in dodge warrior and adding additional capabilities so we're gonna say double chance is going to be equal to random and next double because we want to get a double and then we will say our chance is less than or equal to dodge percent well in that situation we are going to print out on our screen and we're gonna use print F again and we will whoops those and we're going to say that we were able to dodge dodged the attack throw in some new lines and saw out of there and then after that I can go and put in the name for my warrior so go and call get name again which of course is gonna be there and then this is going to return a block amount that is just extremely high just to make sure that no damage is done to our warrior that just dodged so return 10,000 and otherwise I'm gonna say else and I'm going to return exactly what it was in block before so return this guy right here let's just copy that and paste that inside of there so there we are we got that all set up and now we have a warrior that is able to randomly dodge so how is this going to impact our warrior game let's jump back over there so how this is gonna impact it is we have Loki right here all we need to do to add this additional Dodge ability is change it from a warrior into a dodge warrior we can still keep this as the warrior name and treat it as a warrior which is super awesome and the only thing we have to do here is add in the chance that Loki can dodge so we're going to say that Loki has a 25% chance of dodging let's see if we gave Loki enough abilities to be able to survive an attack with Thor and here we go and you can see Loki dodged the attack so there it is being used and is being quite useful well as you can see even though we gave the Loki the ability to dodge he still was killed by Thor so what can we do if we want to go and give an object a capability that is not already inside of the warrior class for example one thing you do is use an interface now basically interfaces are empty classes and they define the methods you must use but none of the code and classes can only inherit one class as we've seen so far when we subclass warrior into creating the dodge warrior but they can in herre numerous interfaces and what we're gonna do is we'll use an interface to define whether a warrior can or can't teleport and we will add this capability without making many changes to the classes that we're trying to affect and how we would do this is creating an interface called teleports so we'll come in and go to source and new and we will create an interface this time and we'll come in here and the interface name is going to be teleports so we'll give Loki the ability to teleport away to save himself all right so there is our interface and all we're gonna do is we're gonna need to say that it is going to receive or it is going to return a string and teleport is going to be this guy after we do that directly because the interface basically doesn't do anything I'm gonna throw two classes inside of here even though I could put them in separate files and I'm gonna say one of them is called can teleport and how we go and implement the teleports interface is just by going implements teleports and what this guy is going to do is we're going to override the teleport function so we'll say public string teleport and I'll show you a more traditional way of using interfaces I'm using what's called composition here which is kind of an advanced technique but after you see the example I think it'll make sense so we'll say teleports away if it has the ability to teleport is what it's going to return and if it can't teleport well then we're going to do something different so we'll just go can't teleport implements teleports again we'll put teleport inside of here and we'll say return and we'll say fails at teleporting and this is going to allow us to add capabilities of those classes without really changing much which is awesome so what do we need to change here to get this to work well inside of warrior I'm actually going to add a couple different things but basically if I want to add in the ability to teleport what I'm going to do here is use what is called an instance variable that is a subclass of the teleports interface and what this is going to allow me to do is at runtime subclasses are going to be able to define whether they can teleport or not so I need to do code-wise is go public teleports I don't need to extend this or do anything with the interface and I'm gonna say teleport typed which is gonna be whether they have the ability to teleport or not and that's all I needed add there and then at the bottom of this I'm going to add in our different capability so which is going to be handling the teleporting ability and also allowing them to dynamically change the teleporting capability and very little code is being used here so all I need to do is go strength teleport and then I can go return teleport type and call the teleporting function inside of there and then if I want to dynamically allow them to change it I can say public void set teleport ability and fellow ports new teleport misses all composition like I said previously and then I'll show you a more you know easy to understands way of using interfaces and new port type and that will allow them to change that and this should be teleports and new teleport type okay so we got that all set up and that's all we needed to do to change that to add that capability inside of there what do we need to do inside of our other ones well we could go into our dodge warrior as well and right after our dodge percent we could add in teleport type is equal to new can teleport and we'll say that it by default the Dodgers are going to all be able to teleport unless we decide to change that and that's all we needed to do add that one line of code and everything works so we're going to be able to chain or need to change anything else well in warrior game we're gonna be able to come in here and I'm not gonna do it in the middle of the battle let's just keep this simple let's say that we want to test if Loki is going to be able to teleport away well we can just do print line and then we can do something like Loki and then go and call to see if Loki is going to teleport away to safety tell port that and then let's also come in and Goins give take away Loki's teleporting ability by going teleport ability and then to change it to take his ability away we just pass in can't teleport they can't teleport class and there we are and it's automatically going to take away his ability and then we can go and print this out alright just to prove that that all works and you can see Loki still died and for your homework what would be an awesome thing to do is to go in and use the teleporting ability inside of the dodge warrior class and do like a percentage chance that Loki can or can't teleport successfully if his health gets below a certain amount all right so good homework and you could see that Loki teleports away and then we changed his ability of teleport and then he failed now if you wanted to see and this can be additional homework a more traditional type of interface here is a pizza interface and you can see how we were able to implement pizza and then come in and add in the description as well as the cost so that would be a more traditional interface in the way that they are used you can go and make that on your own and test that out for homework and then you also have things called abstract classes and crash bowls here being used as an example basically the way that they work they're not that different from interfaces and they are basically going to be used whenever you don't require all methods to be implemented and you would also like to provide for the option for methods to actually have code as you can see right here you crashed actually has code that it has inside of it so if you want to have some of your methods go and for you no actually I've code in them use an abstract class if you want none of your methods to do anything then use an interface alright so there is a rundown of interfaces abstract classes numerous different things in regards to object-oriented programming and now what I want to talk about is streams ok so now what we're gonna do is talk about streams I'm gonna create another class because everything's getting kind of busy here so let's just go class and then let's go and call it job tuts too and let's get a main function inside of here and click on finish and there we go alright so basic these streams are going to allow you to group groups of objects and so you can perform aggregate operations on them and I'll show you exactly what I mean by that so let's first off come in here and let's create a list using a stream so this is going to be integers and I'm just gonna call it one two ten and it's equal to and then I'm gonna use the int stream and range closed which is going to give me zero through ten and it's ranged close I do that all the time then I just put in my 1 and my 10 then after that I'm gonna type in boxed and after I get all this done I'll explain what all these things do and then collect and collectors to list so that's a lot of stuff going on there but I'll go through the whole entire thing and explain it step by step okay so first I'm gonna come over here to the list and import the java.util.scanner gaba util module we have here and there it is now basically int streams are going to be used with ants and I have to also import that library so let's go into it and it's right here in Java util stream so if I want to create an integer stream I'm gonna use int stream range closed is going to generate a list from this starting point one and through the ending point which is going to be ten boxed is going to return a list that's boxed to an integer collect is going to process the list elements into a container and that container is going to be a list container and I can come in here and that is range closed and then I need to get my collectors class and as I do this you can see them showing up alright so a lot of imports now what I'm going to do is use math and it's going to perform an operation on each value that we have so I'm gonna go and I'm just gonna copy this guy and you'll see how cool this is I'm gonna call this squares and that's going to be equal to 1 to 10 and stream and then what I can do is run map what map does it's going to take each one of those values inside of the list that I provide for it and in this situation I'm going to multiply them to look towards each other and then save them into a list again and then collect and I need two collectors and once again turn them back into a list so there we are and then close that off now I can use my enhanced for-loop to output this new list that we have and I can go and get X and I'm working with a different function here so what I'm gonna do is come up here and I'm gonna go down to run configurations and I'm specifically gonna get Java Tut too and then come down here and click on run and you can see if I run it then it goes and multiplies all those individual values times themselves and outputs that okay so we can go and perform the same exact calculation on each item in a list now what I'm going to do is show you a filter which is actually going to eliminate values based off of a condition let's come in and let's get this guy and let's just copy the whole thing paste that there and I'm gonna rename this guy even so what we're going to do is just keep our even values then after stream I'm going to type in filter X which is going to represent each item in our list and then our conditions gonna say if X modulus two is equal to zero well then I know that it is a even value so I want to put it into my list so collectors and then once again two lists and a semicolon now what we can do is go in the cycle and print that out once again except this is evens of course and now you can see we just kept the even value so that's the difference between map and filter now let's say that we would want to go and limit our stream so glint stream and I'm gonna call this limit to five is going to be equal to inch stream I'm going to generate a bigger one you can also use range I'm going to say one through ten however I want to limit to just the first value you can use the limit with any of these other little guys if you just want to limit the amount of output that you get and we can also go and get this int stream we have and instead go for each and list what we want to use for output which in this situation would be print line and there you can see we just got five values even though ten were generated we can also use reduce to multiply all the values in a list or perform an operation over and over again on each of these guys so in-stream and range and let's go one two five and then call reduce on it and we're gonna start off with value of one and then we're going to take each value thereafter and multiply those values times each other and you just go x and y so it's taking out two values at a time and performing operations on both of them instead of you know the other way we were doing it and then we can come in here and output that and you can see whenever we go and multiply them all times each other very very powerful tool you'd also be able to go and get an int stream and convert it into a double stream so int string and let's go and generate one through five and then use map to double and you just take one from one list and put it into the other list another thing that's very useful is we can generate different statistics so I'm gonna go create in summary statistics and I'm gonna call this I stats is equal to int stream and generate a range one through 10 and then summary statistics and you'll see what we'll be able to use this for we can come in and get a average by going high stats followed by get average we're going to be able to sum them just putting get some ins out of there get minimum and also get maximum and there you can say are our answers alright so a very quick run-through of how we can use streams and map and filter and reduce I'm gonna do a little bit more with those as the tutorial continues but now I want to talk about regular expressions now lambda expressions are basically functions that can be passed as if they are objects so what I want to do is I want to create a list here this is an ArrayList just as I created them whenever I was talking about streams and such and this is basically going to be an ArrayList with values 1 2 3 4 & 5 so let's say I would like to multiply each of those values by - well what I'm gonna do is I'm gonna go one two five and I can go and call for each and I can go and output onto the screen those values multiplied times two for example just grab X and multiply it times two and run it and you can see that that's exactly what I was able to do now these are gonna get more complicated let's say that I would like to cycle through this list and only outputs even values can do that as well again you're gonna use basically the same format except here what I'm gonna do is put in a condition and you can put curly brackets inside of here and that conditions going to be the modulus of two if that is equal to zero in that situation I want to print it out otherwise I do not want to print it out and you can put this directly inside of here inside of curly brackets and close that off neat stuff and there you can see that I only printed out the even values all right so pretty understandable especially if you saw the part with streams and maps and filters and so forth so let's do something really cool let's generate for Bonacci numbers so I'm going to create a list and integer list and I am going to call it fib equal to new and linked list and this guy's gonna perform magic all right so to generate Fibonacci numbers I am going to create a stream I am going to iterate over it new int array and I'll explain exactly what I'm doing here second I'm right so limit this just give me one second just keep everything straight okay so exactly what is going on here is that iterate is going to create what is called an infinite stream starting with zero through one as we define right here then what it's going to do is it's going to generate the next value that's going to go into this by adding the previous two which is how we did right here so it's gonna start off with zero through one and then proceed onwards and onwards and this guy can literally be infinite what we're doing here is saying we want to limit our results to just ten map is going to store results into our what it will eventually be our list and then collect is going to process the list elements into a container and then we're gonna use for each doubt put all of them okay and you can see here if I run it that it goes and generates those ten values and I can go and increase this like that and it will go and get bigger and bigger and bigger basically until it reaches the point where it is bigger than the actual data type that we are working okay so pretty cool stuff and multiple examples with lambda expressions and now I want to talk about file i/o and what we're gonna do first is I'm going to show you how to work with files and directories and so forth and I'm gonna use a file object which is a file or a directory and to create one you just say file and new file and define whatever you want to call it I'm going to call this file one log and what we're gonna do first to try to create a file on our hard drive and there could be potential errors so we're gonna surround it with a try block and one say if F 1 create new file then go into output that the file was created and now that has been created we can go and do different things so we can come in and do something like rename it so rename - and just go new file and let's call it f 1 backup log and then after that we can come in and delete the file so let's delete the old one anyway so that's how we can create and rename and delete files else well we know we have some sort of problem with the file not being created so let's I'll put that file not created and of course let's go and put an equal sign so out of there and the error that we're going to catch here is going to be an i/o exception have that be e and then we can print out any exception that would occur now what we want to do is create a file object and tie it to a directory to show you what we can do with directories so file again new file and I'm just going to use the directory that I'm currently in that I'm currently programming in and if you're on Windows you'll have to switch the direction of that and now what I'm gonna do go and check if this is a directory or not and if it is a directory let's say I'd like to get a list of all the files and stick them into a array all stored as files that I can perform different operations on and to do that you just go list files and then we can go and output each of the file names pass that inside of there and to get the name you just go get name now a stream is going to be a sequence of characters and character streams are strings basically and binary streams are bytes of data from primitive types so what I want to do now is create a file for writing text too so I'll to do that again just go file f2 new file provide a name for it so let's call this f2 and text o let's go and output this first and you can see that it output at all the different files and directories that I have inside of the directory ok so now we can go and write in this now again we're going to be working with files so that means that we are potentially going to create an error so what I'm going to use is the print writer to write text to a file and I'm going to show you two different ways of doing this so print writer is going to be equal to and I can say new printwriter so I'm going to write to a file and then I'm going to go in and read from a file and if you're wondering what I'm using here I am actually using the java io libraries so inside of print writer I will then have let's go to the next line new buffered writer and basically what print writer does is it formats the data that you're going to be writing while the buffered writer is going to save data until it is time to write so that its rights at all at once instead of trying to write constantly while you're performing this action and then we're going to go new file writer which is actually going to write the characters and then you put your file inside of there and if you would put true inside of there that would actually append information but I don't want to do that in this situation and by a pen demean like add to the end of the file versus overwriting what's already there and now that I have the printwriter setup I can write text directly to it just by referencing it and I go print writer and print line and then the text that I want to show in there so I'll say this is sample text and then I can close my file and then of course I'm going to open it again and read the text from it and this is gonna potentially throw the same error that we had before so let's sort of that in there and that'll handle that now let's go and read information from a file so again I'm just gonna use F two again is equal to new file and F two you could give it a new name if you'd like and then once again I'm going to be trying to open this so it's a pro potential problem so let's go throw try blocks inside of there and what I want to do here is read the data one line at a time so I you can also you don't need to put stack these like this some people like it some people don't I'm gonna show you the other way of doing it so this is a bufferedreader and it reads data one line at a time so let's just call this V R is equal to new buffered reader and inside of here we can say new file reader and the specific file that we're going to be reading from once again I'm gonna go and get the Java IO buffered reader library that's where I'm pulling information from or that's where the library is I'm using then I can come in and read the line that I want let's just call this text is equal to buffered reader and read line and then I can cycle through and stop whenever a null is reached and null is going to be the end of our file so not equal to null as long as they're stuffed they'll read we will read it and go and outputs each of the lines I only have one line though and go and read the next line if there is a next line but we know there's not going to be for homework you can go in there and throw in more text if you'd like and then after we're done with that of course we're going to want to close our file and again it's going to throw the same exception if we have a problem so there we are and you're going to see that we were able to both write as well as read text from a file okay so that's how to read and write text to a file now let's talk about how to write binary data alright so we'll create another file and let's call this f3 no no changes in regards to text versus binary data that you're gonna be writing all right so we have our file created and we're working with files and another thing we need is file output stream let's call this fos and this is just going to connect to the file to write our raw or raw bytes of information writing information so that's gonna be require a try block and let's just initialize this object and pass in the file that we're gonna be working with again if you would put comma and true after this it would append versus overwriting the file buffered outputs stream is going to allow us to write in bulk instead of trying to write individual parts individually and you're gonna pass the file output stream inside of there now if you want to be able to write primitives to a stream you're gonna need the data output stream and you need to import this library again you're gonna use Java IO once again and to this you pass in the buffered output stream and now we create the data we want to write to it so we can go and write strings to it and we can write anything we want basically say an integer and a double so a whole bunch of different things and how you write specifically is let's say you want to write a string to do that you're gonna use different functions to write different data types so if you want to write a string you use write UTF if you want to write an integer you're gonna use write int and if you want to write a double guess what you're right write double and there it is and then of course you want to close your file after you are done writing information to it and this potentially is gonna throw the same error that we saw previously now if you want to read that information from that stream that you just created you can go and go new file and get the same guy again except this time we're gonna use a data input stream to read and so we use file input stream again we're gonna use a try block and let's go and get this cat right here so that everything works paste that inside of there now we'll go file input stream is gonna be equal to and initialize this file input stream and pass in the file we want to work with we want to add buffering once again buffered input stream I can just come in here and just change this to a star just to get rid of that hassle sure to did that earlier do the same thing with this okay everything still works good and then you just pass the file input stream into that and then there's multiple different methods you need to use to be able to access those that primitive data that you wrote to your file and the data input stream is going to provide you with those methods and you pass in the buffered input stream for that and then we can just go and output that information just like any other type of information using print line data input stream and then if you want to read a string you just go read UTF and you're gonna do pretty much exactly like we wrote the information so this is going to be read int and then do another one and this is going to be read double then of course you're going to want to close your file following put stream and close and that should do it and there you can see that we wrote as well as read data from our file all right so there's a whole bunch of things in regards to working with files and directories and reading and writing multiple different types of data in Java now I want to talk about generics oh right so generics basically allow you to use any type any object type and perform an operation on it so let's go and I'm going to create two different completely different types this is gonna be a string array and let's go and define that and let's have it be one and two just to keep it simple and I'm gonna do the same exact operation on two different types of data so we have a string array and we're going to have an integer array and we'll just go and define those values inside of there and then we're going to actually do this inside of a method and if you want to work with an unknown data type we can just go public static and what you're gonna do is define your type inside of angle bracelets using an uppercase letter this is actually not going to return anything it's just gonna print this information out on the screen again we don't know what we're getting we're just know we're getting some type of an array and then we can use our enhanced for-loop again just put in there it'll work with anything he basically represents any type of data and then we can say that we want to output that data and it's just that simple now what we can do is go print stuff and pass in our different files and know that they will work our different you know completely different arrays such that and there is our integer array and boom they both work okay so cool stuff could also come in here and accept an array list that could contain just about anything so let's go public static void you can do anything I think I've taught you enough Java at this point that you'll be able to print just about anything so this is gonna print any type of ArrayList and how you define that is by putting a question mark inside of there and then once again we can go copy this and we can use objects here to represent any type of object and just change this to a L and it'll cycle through and print out exactly what we have there you could also well I'm here I might as well just go and create a generic class to show you how that would work so I'll come down here and go class my generic and it'll work with any type as well again T it's going to have a some random type that's going to be stored in value and we could go public and void we can set the value again by not defining the datatype and know that it's going to work and store that value inside of here and then likewise we could provide that value back to the user again whoops it's gonna provide it you can just throw T inside of there and as long as the datatypes the same it will work and return value okay so there's another one and now let's go and demonstrate that so back up here create a ArrayList of course work for anything yeah one is equal to new ArrayList and then let's go and generate one so use arrays and as list and one two and three and four and then you can pass it inside of there and know that it will work let's print Al and you can see it worked now what we're gonna do is create a generic custom class so my generic and like I said you can put any type in there you'd like just call this my GI equal to new my generic again it's going to be an integer then I'll be able to go my GI and set our value to ten and we can output that information just by referencing get value so my GI and get value and then we can copy this completely change the data its life and it'll still work paste that there change it into a string instead of an integer of course change this into string these have to match up set value we can just change this the dog or whatever we'd like and then this is going to be let's make this my GS for string and we don't even have to change get value that'll still work and if we do that you're gonna see we're able to work with both data types in our generic class so there you go rundown of how to work with generics and I think I taught you enough you can do pretty much anything else you'd like to do with them now I want to talk about threads so basically a thread is a block of code that is expected to execute while other blocks of code execute and let's go and first let's create a function that's going to contain that block of code what you're gonna do is you're gonna come down here and you're gonna create a new class I'm going to show you a couple different ways of working with threads and we can go implements runnable and that's gonna give you an error because it needs you to implement the run method inside of it add unimplemented methods and then this is going to be the code this is going to be the code that's going to execute so what we want to do here is let's turn on a couple different things like let's print the number of active threads so just blocks of code running at the same time as other blocks of code so active threads just so you can see how many there are and to get to the number of active threads you just go thread and active count close that and what else do we want to do well whenever the thread is first started let's say we want to output that you know the thread has started so start thread and then thread current thread and get name and we'll define a name here in a second then what we want to do is we want to actually pause execution for a certain period of time you saw sleep previously and let's put 3,000 milliseconds inside of there you have to surround that with a try block and what we do is we catch the interrupted exception and print out the any type of error that we would have there and then after we are done sleeping we want to print that our thread has ended so we'll just come in here and just say end thread and it's going to output the name as well okay so that's all we need we just need a something inside of the run method that does something to treat this as a thread now what we can do up inside of main is go and create a thread call it t1 is equal to new and thread and create a new my thread object and then give it a name so that will have something to output and we'll just call this thread 1 and then we're gonna do the same thing for another threat call this t2 and change this to thread 2 and it's create another one what the heck it's go and create a third one and thread 3 of course and thread 3 and then we can go and run them so we can go t1 and on the each one of them and you will see how they work together to get their jobs done go and run it and you can see active thread listed is 4 and you might say why is that we only have 3 threads well main is actually a thread as well and you can see here that we go and we start at thread 3 start a thread 1 start a thread 2 and we went and closed them in the same order and if we run it again you're gonna see that the threads may start and stop in different ways perfectly normal there are ways to stop that as well and one thing that we can do is let's have the third thread actually wait for the first to finish before we actually start running how would we do that well we got both of these guys running we could then come in and go try and we're going to have our interrupted exception inside of this as well paste that there and then inside of our try block we can join this to the first thread and then try to run our third thread and you can see in this situation that the third thread does not start execution until after the first thread has actually ended so another thing we can do and we can do more there's more ways of locking things up here so that the threads behave themselves let's say we would like to simulate a bank account that is only going to allow one customer at a time to go in and withdraw money so let's create a customer class call it name and let's go and just provide the customers name just for tracking purposes mainly here and this name is gonna be whatever the name is you pass in okay so we got that customer that's created now let's go and simulate a bank account by going bank accounts and then inside of this do some more complicated things I'm also going to show you how Singleton's work so you can only have one bank account at a time so just to cover even more information inside of this tutorial so we're going to have a balance and let's say the balance is a hundred dollars or a hundred whatever so static and we're going to have a customer all right so what are we going to be able to do here this is basically how you're gonna set up a singleton so that there is only ever one bank account what we're gonna do is go public static bank account it's going to return a bank account even though it is inside of a bank account so what we're gonna do is we're going to say get bank account is what's going to be called to create new bank accounts objects and you're gonna pass a customer inside of here and we're gonna say if account is equal to null then we know that there is not currently a bank accounts there has not been an account been initialized and that's the lowercase a so in that circumstance what we want to do is we want to go and create a new bank account so this is the only way a bank account ever gets created is if one has never been created before then what we do is we say bank account customer and assign our customer and then we return our account and there we are so that's how to make sure that you only ever have one bank account or one object created for each class it's called a singleton hopefully if you ever have any questions just leave them in the comments and I will answer those so now what we want to do is get our balance because that's the whole purpose of this is to make sure we don't over draw accounts by having multiple customers go in and withdrawing large amounts at the same time so that's gonna return our balance and then what this guy's going to do is actually withdraw money from the account and if you want to protect it so that only one thread can never execute this code at a time you put synchronized inside of there it's not gonna return anything and then you know that you will only ever have the ability for one customer to ever withdraw information at a time so have this be withdrawal and the amount of money they want to take out of the account then we're going to go try and we're going to say if the balance is greater than or equal to the balance that they want to withdraw well in that situation it's okay for us to output that information or withdraw that amount so we will say customer name plus and then we're going to go and get some information requested a certain amount of money that we deemed is okay and then we're gonna put the thread to sleep and this is how let's say for a pair time that's you know just in there to basically show that we are protecting our balance and then we are going to subtract the balance from the account and then we will output a new message and that's going to be customer name plus tried that's gonna be received the amount that they asked for and then else if they asked for more money than is actually in the account well we got a problem there and let's catch this exception let's just use a junk exception that's gonna catch everything so we'll go catch exception II and I'll catch every exception and go and print an error message else if they tried to take more money out than is available inside of it we're gonna throw it or we're gonna print a message saying and you know that's not cool we can't do that and we'll just say try to exceed balance and then let's go and also output the current balance after all of this is done with this and we'll go Koerner in its balance and all the sun and whatever the balance is and it's throw in another print line just to have separation with our code and now what we'll do is put all of this together and using runnable so that we can go and have our threads run this different code I'm gonna call this thread test and this is going to extend thread and implement runnable okay so what we'll need to do here is set up our constructor so thread test and we're gonna receive a customer object and then just to sign that so customer is equal to whatever the customer is and I'm gonna go and put a customer inside of here so customer that we go alright so there's our constructor and then we are going to need to have the run function so public void run and then cycle through and get accounts and try to take money out and then sleep as we run through this so I is equal to zero well I is less than four just randomly trying to take money out of the account we're gonna get a try let's go and just use our regular old catch-all for our exception we're using this for sleep that's why we're surrounding it with the catch block there all right so we got bank account and let's go and create a bank account but remember there can only ever be one bank account and to curate it you just need to create or pass in a customer object now what we're gonna do is try to withdraw money from our account and we're gonna try to withdraw ten dollars each time so eventually we're gonna have problems because when they have $100 in it and then we have threads sleep and let's just have that be a thousand just to slow down everything and everything else there should be cool all right so got that saved now let's go and use it to test out our whole entire system and see if we can over draw the account now let's go and create a couple different threads and have them all try to take money out so thread test is equal to t t1 is equal to new thread test and remember we have to pass a customer into it that's the only requirements so we'll have Sam go inside of there and then let's create a couple more of those and we have to start our threads of course so t1 and start you know okay create a couple threads and see if we were able to protect our account from having multiple or from being overdrawn so I'll change this three we'll change this from Sam into sue and we'll change this into SID that's all we need to do let's run it and I had a little bit of an error and that's because I didn't change this to two and change this to three got that saved and I'll test it and you can see right here Sid's trying to get money he gets it the balance goes down and you can see how all of the customers are taking out ten dollars at a time eventually we will run out of money and you can see that they try to exceed the balance but our system protected it okay so multiple different ways we can use threads and now I want to talk about databases okay so I'm gonna be using MySQL for connecting to a database and I'm gonna be able I'm gonna be using the connector to provide for the easiest way for you to set this up to test everything so what you're gonna want to do is go to dev MySQL column downloads connector J and specifically what you're gonna want to do is under select operating system come down here where it says platform-independent and download that then what you're gonna want to do also you're gonna need to install MySQL I'm not gonna go through that I'm sure you'll have no problem doing it but I'm specifically using MySQL 5.7 not the newest version 5.8 or like Oracle likes to call it MySQL eight I'm doing that only because of security issues and so forth use 5.7 especially if you're a beginner with databases and Java and so forth it'll save you a ton of time and you're not giving up on anything so I'm using five point seven then what I have here on the screen is everything and I in the description underneath the video I have a link to all this you can copy and paste this after you download MySQL is how you setup root then this is how you change the password for root this right here is going to create a database for our students that we will be using I'm gonna say that we're using the database names students this is going to be how we are going to initialize everything I'm not gonna get into what all these commands mean I have a MySQL tutorial you can watch that but you can just copy and paste all this in this is how you insert information into each of the pieces and regards to different student names this is how you create a new MySQL system administrator this is going to be the password this is how you grant privileges to give this new user the ability to use your new database you created and then here you can also see the permissions you provide okay so they're all in one page you can pause or you can copy and paste what is in the description otherwise now I'm gonna jump over and show you how to access this database and make changes okay so here we are I trust you have a database set up exactly as I had on the previous screen now to access this database you're going to use the connection like this and you go and import this but what you're going to want to do first just get the connector so how you do that as you come over here to your project I have mine called Java Tut and you're gonna right-click on it and then you're gonna come down here to where it says build path if you are inside of Eclipse and then you are going to configure your build path so there we are then we can come in and go to class path make sure you have libraries selected right here you're gonna click on add external jars this guy pops up and this is the file that I told you to download mysql connector for java and there it is and click on open and there it is and apply and close you may need to restart java but everything should work fine now what we're gonna do is with connection come over here and you're specifically gonna want to java SQL this guy right here and click on that and now we're ready to rock and roll and access our database so what we're gonna do here is inside of try anytime you're accessing the filesystem you don't want to use a try block what we're gonna do is we're gonna go class for name and comm dot my SQL CJ dot JDBC dot driver now what you want to do is provide the URL that you're going to assign to this so this is going to be so that you can access the student database and that is my SQL localhost and students to be specific string you're going to have the user defined and that's going to be the s DB admin as I showed you previously to set up we're also going to have another string which is going to be whatever that administrators password was which I have set for Turtledove you're going to now go in and be able to issue queries to your database just as if you were doing it in any other way so to do that you go get connection and you pass in the URL the user and the password and this is going to be calling as equal to driver manager get connection and this is going to want us to use job ask you well again let's just come in here and save ourselves some time and put a star here there we go now we're gonna have everything we want there we go this next guy is going to send Cory's to the database for our results con and create statement and then we need to actually create the query we are going to use for getting information so I'm gonna say that insert I'm actually going to show you how to insert data into the database insert into students and then we will go first name coma last name Street these are the names of the different fields in our database and student ID so those are all the different things that we are going to reference in our database or be entering information into it anyway then we say values and then we put in whatever specific information we want and this would work really well as a method but whatever and then inside here we will go and inside of quotes put Shelley for the first name and those are single quotes and then we're going to put Johnson and then we will put three two one main and then we'll put an all inside of there because the student ID has automatically generated and there we go so this is a query that is going to insert a new student into our database and how we execute our query is to just go s and execute update and pass in our quarry alright so we were able to enter new information how do we get results in between the two of these you're basically going to be able to access and get anything you could ever want out of your database select first name last name and Street that's the information I want to get out of the database and from and the database that we're working with or this is actually the table we're working with so that's from student then we can view and use result sets to cycle through our results and how we do that is we execute our query once again and of course we have to pass our quarry in then we need to go and get the specific information as we cycle through our results so this is going to be different depending upon on what type of data we're pulling out so go and get a string out ere actually all these are going to be strings get string and first name is the name of that data we're gonna pull out and then we're gonna do that for all the other data pieces of data in the database that we're pulling out of there so also last name and also Street okay so we got all that out of there now what we can do is we can safely close our database of course outside of our while loop and if you wonder what the other things are you have getstring you're also going to get boolean get date get double get int and get long I'm gonna take that out alright so we got our while loop we cycled through everything we got our results now we need to close our connection to our database and then we need to catch any issues that we have and the potential issues are class not found exception and the other exception that we could have is going to be an SQL exception if there was something wrong with our Cori and I think everything there looks like it's working really well let's run it see if it works oops got an error what's the error and it says on line 28 is where I have my problem and I see where the air is I forgot to put a comma right there so let's save that try it again and it worked and accidentally had Shelly Johnson multiple times that's why there's multiple Shelly Johnson's all right so there you go guys that is about as much information on Java as I could possibly put into one video believe it or not I was actually going to also cover Java FX Java Web Start servlets and server pages but they thought this tutorial be so long but if you guys tell me you want to see that I'll make a supplement to this video and I will make it as quick as possible so please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 213,651
Rating: undefined out of 5
Keywords: Java Tutorial, Java, Java Programming, Learn Java
Id: n-xAqcBCws4
Channel Id: undefined
Length: 138min 43sec (8323 seconds)
Published: Fri Feb 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.