Transcript for:
Comprehensive Overview of Java Programming Course

[Music] hello my name is danielov and welcome to the Java programming course let's see first what this course will cover first of all we will see some concepts of the Java and then how to store data in variables and types and how to read data from the user and how to print data to the user and then how to do simple calculations the programs are just like reading books so you read from top to down from left to right and if this is not okay for your program then you can make conditions that controls which part will be running and which part will not be running and if you want to repeat a part then you can use loops and we will see how to use some tips and tricks in the net beans how to debug the program and what debug means how to use the code templates which are abbreviations of the statements that we will use we will reveal the secrets of basic algorithms so how to sum up elements in the series how to count elements that are appropriate to a condition how to determine minimum and maximum of the series and how to search inside and then the arrays they are multiple elements together and we will see how we can use one dimensional and how we can use two dimensional arrays they are just like the spreadsheets and then the functions and structured programming functions are very similar to the Genies we can assign tasks to Genies and then invoke and they will do what we want them to do and based on the Genies with a herd of Genies we can create a whole program and they are very useful for constructing the program and structuring the program since the Java is an object-oriented programming language we will see what classes objects are and how to use the inheritance for creating new classes out of the original ones and what the polymorphism is we will see how to handle dates and times there are two methods one old and one new the new one introduced in the Java 8 so we will see both of them and then how to sort elements and why it is useful and what the benefits of the Sorting are there is an interesting part of the Java this is the generics and we will see what the secrets of the type parameters are we will see what the collections framework is they are classes that are useful for storing objects in different ways we will see what the list the set and the map are and then if something goes wrong then the Java will create and throw exceptions and we have to catch it and return back to normal State how to do this and what this is in the background of the file handling there are the streams what the streams are how to use them and what the properties are properties are for using the inil and if a variable can contain only a specific set of values then we can use enams an example is the days Monday Tuesday Wednesday so if we have only these values then it is worth using enams for that and last but not least when um we would like to do something maybe someone else has created a solution for we don't have to reinvent the wheel so we will see how to use other people's work in our programs during the course we will write a lot of exercises because programming is just like martial arts you cannot not learn it from books you can learn it by exercises so we will have smaller exercises that demonstrate the different aspects of the programming language and we will write a simple computer game which is a role playing game in which you are the hero the idea is based on the fighting fantasy the fighting Fantasy game books are created by Steve Jackson and aan Livingston and uh while the normal books are read from the chapter one to the last chapter in the fighting fantasy books you can decide in each chapter where to go what to do an important thing in these Game books are the dice we will use dice to create the different attributes of the character and we are using dice to fight with the monsters just like any other fantasy words the word of the fighting fantasy is very colorful it contains a lot of different monsters in our program we will simulate the fighting with these monsters until death let's review what we will do in this video first of all some basic concepts about the Java and then we will install our working and learning environment the net beins and then we will write the hello word project this is the very first program you write when you start learning a program language and then I introduce the variables and types which is a concept for storing data and then we learn how to read in and write data for the user and how to do simple calculations we will learn the conditions and based on these conditions one part of the program will run and the other part of the program will not run and last but not least we start the you are the HERO program we will write the first part of it as you might know the Java is a very popular programming language if you read statistics on the internet all agree that the Java is in the top three most popular programming language and some of them say that Java is the most popular programming language and you might hear the expression 3 billion devices run Java and Java is a platform independent programming language that means you can write your code once and you can run it anywhere and there are a lot of well-paying jobs this might be very interesting for you because there is a great demand for Java developers and you can have a very good and well paying job if you learn Java and it's widely usable if we consider the Java platform as a tree the tree is based on the basic computer skills that is uh folders operating systems running programs on the basic computer skills there is the programming in Java after learning programming Java you can learn basic Java tools and after that you have three options you can be an a mobile application developer you can be a desktop application developer or you might go to a huge company and develop Enterprise applications inside the mobile applications a very big part is the Android 60% of the market is the Android there is another technology Java me it was quite popular before the Android but now it's less important nowadays the second option is the desktop application development you can develop games you might know the Minecraft it's a quite popular game nowadays and it is completely written in Java or you can develop software tools for example net beans and Eclipse are written in Java we will use net beans to learn Java programming and if you are a researcher it might be interesting for you that there are a lot of applications that are developed by the Java in the research field the third option is is that you go to a huge company as a developer and it might be interesting that 90% of the Fortune 500 companies use Java if you go to a huge company you might develop e-commerce applications or banking applications or huge backend services in this course we will cover the first two topics the programming in Java and basic Java tools as I said the Java is a platform independent programming language what does it mean let's suppose that you as an English speaker person you travel to Europe and as you might know Europe is a multicultural continent there are a lot of languages so let's suppose that if you look at a pretty girl you want to tell her that she's a pretty girl she won't understand you so what can you do in this situation you can translate your message with a device you use your mobile phone and you use your translator on your mobile phone so you type in what you want to tell the girl and it translates to in this case Italian and the girl understands you this is the compilation when your message will be transformed to another version that is understandable for the other party in the computer field you are the user who wants to do something and the computer won't understand you unless you translate your message to the computer this is the compiling what other options exist in this case the other option is to interpret the message the situation is the same and uh you hire someone who speaks English and Italian so you tell her your message and she interprets your message and the girl will understand so as you might see this is a slower method of communicating with someone in the computer field if you would like to tell the computer something then you can use another program that translates your message on the Fly and this is the interpretation okay let's see the Java style Java is a platform independent programming language what does this mean let's consider the situation you are traveling through Europe and uh you want to talk to every single pretty girl you introduced so her and her in this case you might use your mobile phone to compile your message to a common language in this case we will use the Esperanto language which was intended to be used as a Common Language throughout the word without any success so in this case we compile our message to Esperanto and we hire some other persons who will understand this Esperanto and the native languages and we show the interpreters this message and she will translate the message for the pretor and the pryors will understand the message so in this case in the Java terminal ol the output of the translator the output of your mobile phone is called the bite code and the interpreters that interpret your message your translated message are called virtual machines if you start dealing with the Java programming you might hear a lot of abbreviations Expressions just like G ID GD a gvm net beans and eclipse what are they and what do you need for starting programming in the core of the Java is the Java virtual machine in our analogy that was the girl who was hired to translate our compiled message from espiranto to the native language this Java virtual machine is the part of the Java runtime environment the gr this software is needed for you if you would like to show your program to your mom to your spouse or to your kid you have to install this software to their computers if you want to make Java programs you need the Java development kit in our analogy this was the mobile phone with the translator the Java development kit contains the Java runtime environment this you have to install to your computer but using this part is not so comfortable it's more comfortable if you use integrated development environments for example net beans and Eclipse they are very popular and because the net beans is easier to use for beginners in this course we will use the net beans okay let's see how to install the net beans and the jdk first start a browser and uh in a Google search let's search for GDK net beans 8 8 is the version and the first one is uh the netb plus GDK bundle this is what we will need and in the download page first accept the license agreement and then these links will be clickable there are different operating systems and different bit numbers x86 is the 32-bit version and x64 is the 64-bit version which one do you need I suppose that you know which operating system you are using I use currently windows so the question for me is which which version I should download and install how to figure out let's right click on the computer and in the properties you will see the system type 32-bit operating system that means for me only the 32bit version will be appropriate if you see here 64-bit operating system you might choose whichever you want but I suggest the 64 version so I download the bundle okay so when the download is finished let's click on the installation kit and uh let's start it and then the installation kit will configure itself and the program will start the installation procedure is a simple one I suggest you to install the junit system it will be usable for class testing and uh it will ask us which directory we should U install the jdk this default is appropriate so click on next and then it asks where to install the netb ID and the jdk is uh configured next and then after everything is set up we can click on the install button okay so after finishing the installation the netb installer ask us if we would like to contribute to the net beans projects by providing the anonymous usage data you can leave it like this or you can switch it off if you want to I switch it off so I think it is finished so the net bin ID is uh on the desktop let's start it okay so after installing the net beans ID let's run the [Music] program you will see a window very much like this the start page is not necessary for anymore so click on this checkbox and switch it off and uh close it and after that we will have a window split into two parts this left part will contain our projects one project is simply a program we can have a lot of programs next to each other so we will create now a new program that is we create a new project so click on the file new project and in the appearing dialogue select the Java and Java application and click on next and here let's type the hello word since we will write our very first application an application that print out the hello ver text this is a very common first program in every environment so make sure that this create new class will remain switch on and then click on finish okay as you can see here in the projects window there will be a hello word application and this will contain everything that is necessary for compiling our project and uh the net beans will open up the source file and this will contain the code that we would like to run in the source code there are gray areas these are commands they are for us not for the computer they do simply nothing they are just remarks for us to know what happened here and uh there is a package declaration here this package declaration shows which package this source code is in a package is simply a container for our source code files we might have hundreds or thousands class files source files so if we package up two smaller parts then we might have a better View and uh the net beans created as a class everything in Java will be contained in a class and uh there is a main function I will explain the class and the function later we have to know that our program has to be written into the main function so let's remove this comment here and uh let's type in system. out do println and into quotation marks let's write hello world this is our very first program and let's run it let's click on this green triangle this will run our project it will compile and Save autom automatically when we click it and after a while a new sub window will appear and this will be the output of our very first program so congratulations so the very first concept we have to get to know is the concept of variables imagine a wardrobe if you open up your wardrobe you find your valuable things in the different places the variables are very similar to these places you can put your important things to these places so simply a variable is a place to put your important thing what is an important thing for a program is the data and you can have different type of data you can have a number you can have a character you can have a text and uh you can put them into variables but as you don't put your shirts into the place of t-shirts and vice versa and you don't put your underwear in the place of t-shirts you won't put your text into the place of numbers and you don't put your numbers into the place of text so in the case of variables we Define another concept this is type the type represents what kind of data you want to store what kind of variables and types are there in Java if we consider all the types we can divide it into two parts one is primitive type very simple data and the classes which is a little bit more complex data let's examine the Primitive types first if you take the Primitive types there are the numbers so all the numbers can be here and uh there are characters in Boolean so a character can mean a letter a sign a space so everything which is one one character and the Boolean can contain true or false numbers are not one single type but numbers are different so there are integral numbers just like 1 2 3 1,000 and so on and there are real numbers 1.2 2.3 and so on among the integral types there are many types and uh one is smaller can contain fewer numbers and there are bigger types so the smallest one is the bite it can contain only tiny numbers short is bigger int is bigger than the short and the long is the biggest and um among the real types there are two the float float is the smaller type and the double is the bigger type most of the cases we will use the int and the double okay let's make an exercise it is called value replacing exercise the exercise is about replacing the value of two variables let's take this simple code inside the main function we create an INT variable the name will be first and the type is int and the value is seven so we would like to store the seven in the variable first and the type is int and we create a second variable the name is second the type is int and the value is 19 and after some code this is a comment we will print out the result system out the print Lan and uh first will be a text and uh then we will print out the value of the first in this case this is seven and then we will print out the second and the value of second so in the quotation mark this will be printed character by character and outside the quotation marks this will be the name of the variable and the value will be printed out okay and the exercise is about to replace the value of two variables without writing any more numbers and without changing the existing lines only write new parts here but before solving this one let's see how it works let's create a second project the name will be replace and make sure that the net beans will create the main class click on finish so first of all let's create a variable the type is int and the name will be first and we can assign a value to to the variable in this case seven so type name and value and let's create a second variable type is int name is second and the value will be 19 okay what can we do with these variables first task is to print out to the screen so first we print out a string first and then we print out the value of the first outside the quotation mark and then we begin a further quotation mark this will be second equals and then outside the quotation mark we will print out the value of the second variable and the semicolon at the end okay let's see what it does so let's start and it will print out as we can see the thing inside the quotation mark will be printed out character by character and then the other first this black one will be replaced by the value and then the second character by character and then the second will be substituted with the value of the second let's make another exercise let's consider two glasses one with red wine and one with white wine and the task is to replace the content how to do that without mixing up the vines the idea is to have a third glass so you will pour the white wine to the third glass and the second glass will be empty so you can pour the red vne to The Empty Glass the second one and you can pull back the white wine to the first class and now the task is solved how can we use this experience okay so let's create a third variable just like the third glass and we put the content of the second into the third one and then the second will be empty and we put the content of the first to the second [Music] variable and then the first is empty so we can put the value of the third into the first one remark for The Emptiness of the variables I consider them to be empty just like the glasses but in real it they won't become empty when you assign their values to another variable after the assign both variable will contain exactly the same [Music] value let's see how it works and they are replaced so congratulations the next EX exer will be the temperature exercise we will write a program that reads in an integer value which is a Celsius value and we will calculate and print it in Fahrenheit grade the formula is here in the bottom right corner what we don't know to solve this problem first we don't know how to read in a number from the user and the second one is uh how to make calculations okay let's see how it works okay so let's create the solution of the temperature exercise so first of all let's create a new project Java category Java project next and [Music] let's create temperature project make sure that it will create the main class so first first of all we will print out something for the user that uh please give in the Celsius [Music] value and how to read in a number for reading in a number we have to create a tool this is written by the Java developers and this is called scanner java. utl do scanner I will name it to SC and we create a new Java utl scanner you don't have to understand this line This is a magic line we have to write this into the program if you want to read in a number and just just one remark here you write here java.util device and we programmers are lazy so we don't want to write things twice so I would be happy if I could remove this java. utl but in this case the Java compiler doesn't recognize the scanner uh this is because the previous java. utl was the name of the package the scanner is in we have here hello word and and uh temperature packages and the scanners package is the java. utl and um we can reach tools from any other packages in the Java environment but uh you have to specify the whole package name with the class name but we are lazy so we don't want to to write that here how to solve the problem which appears here with an import Line This import line is a new line here import java. utl do scanner and that means that every time I mention the scanner it will understand that this is the Java do utl do scanner okay so we created a scanner with this magic line and this magic line will be used to read in a number so first of all we create a variable this will be Celsius and the value will be sc. next Double that means we read in a double value and we put this double value into the Celsius variable and after that we have to calculate the fahrenheit value by using our formula so to store the result of the calculation we create a Fahrenheit variable and we use the formula [Music] 9 divided by 5times Celsius plus 32 I will explain this .0 later um and after calculating the fahrenheit system out print [Music] Ln Fahrenheit is Fahrenheit okay so let's run this program it asks us the Celsius value so for example 20° C is 68 in fight so it works so let's go back to this point O if we write only nine 9 is an integer and 9.0 is a double so in this case we are using always double values Celsius is double Fahrenheit is double so we add double values into the formula okay and uh one more remark what if we are using integers here okay let's see what happens if we are using integers and we will give in the same value Celsius 20 okay let's rerun the project 20 so it's different it was 68 and now it's 52 why is there a difference between the two the explanation for that is uh that the calculation is uh different when you use doubles or integers especially the division so if there is a division then if you use doubles then the result is double so in this case 1.8 and if you use ins then it will be rounded down to one so if you calculate the result of this expression then 20 will be multiplied by 1 this is 20 and plus 32 is 52 and uh in the other case if you use double values so 9.0 and 5.0 then the result of this sub expression will be 1.8 and this will be multiplied by the Celsius value which will be 36 and you add 32 so this will be 68 okay so always remember if you use integers in the division then the result will be rounded down the next exercise will be in connection with the temperatures as well in this program we will read in a temperature value again in Celsius and we will print out if the water is boiling or not at the specified temperature so what we don't know is how to run a part of the program based on the temperature value okay so for solving the boiling exercise we will create a new project like before boiling exercise okay so first of all we have to read in a number number which is a temperature value so it's very similar to the previous exercise so first of all we print out something for the user give in the temperature and then we again create a scanner with this magic line and we again import this scanner so a new import line is appearing and in this case let's use integers I abbreviate the temperature as Temp and uh we will read in an integer number the scanner is a tool and it provides us a lot of possibilities not only next Double but we have uh next int next short Next Bite and we have functions for every types that I mentioned so in this case we will use the next int and uh after reading in the temperature we have to decide what to print out if the temperature exceeds 100° C then the water is boiling and if it's below 100 Celsius then it is not boiling so the key word for that is the if and inside the if we will write a condition the condition is something that has a true or false value that can be decided if it's true or false so in this case if the temperature exceeds the 100 exceeds means equals or greater than [Music] 100 and this condition goes to parenthesis and after that in curly brackets the program statements the program commands that will be executed when the condition is true so in this case this is very simple we just print out that uh the water is boiling okay and what should we print out when the water is not boiling because in this case if you run the program run the project so in this case if the temperature exceeds the 100° C then this writes that the water is boiling and what if not let's rerun the program and type in 88 and it prints out nothing this is not always good sometimes it would be better if we print out something when the condition is not true so in this case we can use the lse keyword and curly brackets and inside the curly brackets of the lse you can write a statement to print out something else the water is not boiling so let's rerun the project so when the temp temperature exceeds 100 then it prints out the water is boiling and if I rerun the project and it doesn't exceed the 100 then it prints out the water is not boiling our next exercise will be the boiling and freezing exercise this will be very similar to the previous exercise but in the this case we will have three options not only boiling and not boiling but boiling freezing and normal State what we don't know since we have more than two options we have to join the ifs and create three options out of these two okay so for the boiling and freezing exercise we will copy the boiling exercise so right click on the project boiling and select the copy and here provide the name boiling and freezing this will be the name of the exercise and click on copy so a new project is created boiling and freezing with the same content so we have two boiling do Java one in the boiling project and one in the boiling and freezing project this is easy to mix up so it's better if we rename the boiling do Java this can be done by right click on the name of the Java file and refactor and rename or you can use the control+ r rename boiling and freezing and click on refactor so this will rename the file and this will rename the class itself but the content remain the same okay so back to the exercise it's very similar to The Boiling exercise but uh when the water is not boiling so the water is boiling is done but if the water is not boiling then we have to divide it in into two parts when it is freezing and when it is in normal state so instead of just printing water is not boiling we will split up this option by an if so if the temperature is uh below Z or equals to zero then that means the water is freezing the water is freezing and the other option can be done by an lse and in this option we are writing that the water is in normal [Music] State okay so we have an if with an else and in in the else Branch there is an other if else so if we have a lot of branches then this can be a very huge structure so a little bit simplification in those cases when the lse contains only one instruction then you can remove these curly brackets and and this IFL statement as a whole is one instruction so let's remove these curly brackets these yellows and um let's remove some some tabs so I will remove this tab and this tab this tab this and this so in this case the logic is the same but it's easier to read so we have a branch with temperature exceeds 100 we have a branch which uh less than or equals to zero and we have a branch that is everything else okay let's try it out so one branch is when the temperature exceeds 100 the then the water is boiling we didn't touch it so it works the same let's rerun it if it's below zero then the water is freezing and if it's in normal state for example 56 then the water is in normal State what did we do in the boiling and freezing exercise with these ifs and as ifs and eles let's imagine a sausage the sausage represents all the values from minus infinity to plus infinity and with the first if we cut one part of this area just like if we cut the first part of the sausage this was the first if and in the lse branch we handle the rest of the sausage the rest of the area and in the lse part we cut it into two parts the if of the else is the second part of the sausage and the last else will be the rest of the sausage and the sausage is eaten some might think in the boiling and freezing exercise that we have three separate branches each with an if and without an else so an if when the temperature exceeds 100 and another branch which is separated from the first if when the temperature is below zero or equal to zero and then a third branch which is executed when the temperature is uh between Z and 100 but this expression is not understood by the Java because this is not a not a complete condition complete condition is this one and complete condition is this part it simply doesn't understand it understands if we write two separate but complete condition and join with something in this case we join with end so if the temperature is greater than zero and the temperature is below 100 then the water is in normal State we saw in the exercise this end operator and this might be strange for you this end means that if we have two conditions the resulting condition is true when both of the conditions are true we have to two more logic operators one is or that means if one of the conditions is true or both of the conditions are true then the resulting condition will be true and we have the negate which makes true out of the false and makes false out of the true the next exercise is the fighting exercise which is the first part of the you are the HERO program the goal of this exercise is to sum up everything we learned in this video we will write a program in which you can fight with a monster so in the program first we read in the attributes of you and the monster and then we will play one round of the fight what will be the fighting rules there will be some attributes for you and the monster the attack points determines how strong you are in attacking the other party defense points how good you are in defending yourself the damage how many damage you can cause to the other party and the life points how healthy you are if it goes to zero then you are dead one round of the fight first of all we choose who will be the attacker this is a simple coin TOS if heads one of the party attacks the other if Tails then the other party attacks and then we roll two dice and add to the attacker's attack points and we compare it with its enemies defense points if the attacker's result is bigger than the defender then the attack is successful and therefore the enemy's life points will be decreased by the damage otherwise the attack is not successful so what we don't know is how to generate random numbers if we know how to generate random numbers then we are ready to fight okay let's create the solution of the fighting exercise first of all we create a Java project Java and Java application and then the name will be fighting in the first part of the program we will read in all the attributes of U and the monster because we are reading numbers then we create a scanner this is the tool for reading in numbers and as always we import the scanner and [Music] then we read in the attack points of the monster the variable name will be monster attack and we read in from the user then reading the defense points the first part of the monster monster defense will be the variable [Music] name reading from the user then the damage of the monster this will be monster [Music] damage and five finally the Live points of the monster we be read from the user and then your attributes we be read in after reading all of the attributes we start the fighting round the first thing that we have to decide is who will be the attacker for this we will do a Coos in the programs for that we will use random number generator and this random number generator will generate us a number in this case if we would have to decide which part is starts we need not a number but a Boolean value so if true is generated then one of the party will attack if false is generated then the other party attacks so let's create the random number generator this is the random number generator and this is very similar to the scanner this is a tool we have to create it with new and we have to import the java. utl random and with this random number generator we create a Boolean value this Boolean value will determine which party will attack we will use the next Boolean we create the next Boolean value randomly and uh after that if the attacker is true then that means that you will attack you attack and what you need is to roll two dice therefore we generate another random value in this case a random number so generator. next in if you write here a six that means this function will return a random value between 0 and five inclusively six cannot be generated so if we have this number from 0 to 5 then we add one to this 0 to 5 number and then we get 1 to six this is a normal die so I add one to the value so this is one die and we need two dies so we generate another number from from 0 to five and we add one more so this is two dice and according to rules the attack value will be calculated by by your attack points plus the two dice but before we go on let's print the values that was rolled the rolled values and your attack value and then we decide if your attack was successful or not if the attack value is greater than the monsters defense that means your attack was successful and therefore you damage the monster so the Monsters life will be decreased by your damage so we print out the remaining life of the monster if the condition is not true then your attack was not successful let's print it out unfortunately okay so if you were the attacker then this is your attack otherwise the monster attacks let's print it out okay and that's all let's run the program and let's type in some values the attack point point of the monster will be five defense points 10 damage of the monster three life points of the monster will be 40 your attack points 10 your defense points 25 your damage six and your life points 40 and after that you attacked the r values was six so therefore your attack value was 16 which is greater than the defense points of the monster therefore the attack was successful and therefore you damaged the monster and the monster's remaining life points was 34 you successfully hit the monster but it is still alive to kill it we still need to learn a lot so let's see what's in the next video one of them is to choose one from many this is the switch statement and for repeating things just like fighting rounds we learn the V the do vile and the for Loop and we will learn two things to control the loops break and continue and finally in the next video we will learn how to put one Loop in the other and we will continue developing our game