Java for Everybody: Basic Programming Concepts. | Hadi Youness | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Java for Everybody: Basic Programming Concepts.

teacher avatar Hadi Youness, Computer Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.



    • 2.



    • 3.

      Create Objects


    • 4.



    • 5.

      String Class


    • 6.

      Random Class


    • 7.

      Math Class


    • 8.

      Math Class: Application


    • 9.

      Number Format Class


    • 10.

      Decimal Format Class


    • 11.

      Printf Method


    • 12.

      Wrapper Classes


    • 13.

      Anatomy Of A Class


    • 14.

      Boolean Expression


    • 15.

      If, if-else


    • 16.

      While Loop


    • 17.



    • 18.

      For Loop


    • 19.

      Break And Continue Statements


    • 20.



    • 21.



    • 22.



    • 23.

      Array as Object


    • 24.



    • 25.

      Try and Catch Block


    • 26.

      Try and Catch: Application


    • 27.

      Exception Propagation


    • 28.

      Checked And Unchecked


    • 29.

      Checked Exception


    • 30.

      Create an Exception


    • 31.

      Array: Application


    • 32.

      ArrayList: Application


    • 33.

      If or Switch?


    • 34.



    • 35.



  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

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





About This Class

Java is one of the most popular programming languages, and it widely used by top companies. 

This class aim to teach basics concepts in Java and programming in the simplest possible ways. If you are completely new to programming and Java, We recommend you start with the first class and then get back to this one. This class is a bigger version of the first one, however, most concepts are reviewed in this class. 

We are going to cover:

-Classes and Objects. 

- Decision making


-Arrays and ArrayLists

- Exception

And Finally we'll solve some examples to link everything we learned together.

Meet Your Teacher

Teacher Profile Image

Hadi Youness

Computer Engineer


Hello, I'm Hadi. I am studying Computer Engineering at the Lebanese American University (LAU). I like to share my knowledge with everybody and I believe that teaching is a perfect way to understand anything since you must be well informed about something to be able to teach it in the simplest possible ways!

See full profile

Level: All Levels

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

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


1. Introduction: Programming is the process of designing and building a program to accomplish a specific task can be very complicated, unenjoyable, and boring if you do not understand that well since the very beginning. But once you understand it, you start to see the problems you face as challenges that you need to pass. Saudi seeing more enjoyable, challenging, and entertaining at the same time. All goes back to the way you think about a specific problem and whether you understand the concepts of programming and love. I designed this class, especially for beginners. I sat down until it about how I would like someone to teach programming. And I ended up creating these lectures. If you don't know anything about Java or programming, I recommend you start with the first class I created. However, you can still do well in this class with some extra work. Would use Eclipse and you can find how to install Java, and that lives in the first class. Now, let's get started. 2. Material: Before we start outlast, let's have a quick look at what we going to cover. First of all, recite by creating objects. And then we'll, there are to aliases, defining what are aliases. Then we'll go to classes. You have string fandom in math classes. We discussed them and use the methods available for us in each and every class. After that, we'll go to number format and decimal format classes and also use the methods here. Then we define print f method. And how to use it is another print method other than print and print L N. Then we go to wrapper classes and learn how to use them and how to use their methods also. Lastly, we'll learn about anatomy of a class. Then we jump to decision-making. We have Boolean expressions, if statements, switch case statement. After that we'll go to loops. We have while and for loops. Then we'll define break and continue statements. And will know what is an iterator. And we'll define the methods available for us here. Then we go to arrays. We have idealist and I work with ArrayList. And then we'll jump to Adi and use areas primitive datatype and areas objects. And lastly, we cover exceptions, uncaught exceptions, and how to handle any type of exception using try and catch block. And then we learn about exception propagation and how to create an exception. At the end, we do some applications on arrays and ArrayLists and when to choose FS, switch case statements. And then we have our project. 3. Create Objects: And our first-class who presented an overview of object oriented concepts, including the basic relationship between classes and objects. Then, in addition to discussing primitive data, who provided some examples of using objects for the services they provide. This class explores these ideas further. And previous examples we used the print method. As you mentioned before, the print l m method is a service provided by the system to the out object. To be more precise, the identifier out is an object variable is stored in the system class. It has been predefined and setup for us as part of the Java's standard class library, we can simply use it. Now, let's carefully examined the idea of creating an object. Let's consider the following two declarations and saying name. The first declaration creates a variable that holds an integer value, as we've seen many times before. Now the second declaration creates a string variable that holds a reference to a String object. An object variable doesn't hold an object itself towards the address of an object. So now these two variables don't contain any data. They look like this. We have an empty box and string name, also, empty box. As we pointed out and Chapter two, it is always important to make sure that the variable is initialized before using it. For most, in most situations, the compiler will issue a narrow if you attempt to use a variable before initializing it. Now we also said that object variable can also be set to land. So we can say string name equals. Now. Now let's give these two variables, some values. Let's say int num equal ten and name equal eight. So the act of creating an object using the new operator is called instantiation. So you can use the new operator to instantiate the object. After the new operator creates the object, we should call the constructor. So the constructor is invoked to help, to help set it up. Initially. We can think of the constructor as a special method that has the same name as the class. Now, let's go ahead and print num and see what happened. Then. And Alex a. So we have here an empty line. Does we print, print out line here? Now, since an object reference variable holds the address of an object, it can be thought of as a pointer. So it looks like this. We have numb inside the box ten because this is a primitive data type. And name, which is a string, doesn't contain Alex, contain a pointer to the location in memory where Alex exists. So we again show here the actual ad, just a number, but it doesn't matter because it is irrelevant. What is important is that the variable refers to a particular object. Now after an object has been instantiated, you can use the dot operator that's used now. To invoke the method length. You can say name dot length. The length. The purpose of the length method of the String class is determined and returns the length of the string. Now in this case, let's create a new integer, count and count. But first, let's count the letters in this string. You have a 1234567. So if we printed out, can see seven. No. Even though strength is not the primitive datatype, character strings are so fundamental and so often used in Java that Java defines string literals delimited by double quotation marks. So instead of saying string name equals new string, we can simply say name one year at x, a and semicolon. Now, even, even though this string is not primitive data type, we can use it as we use a primitive one like this is the shortcut notation of this one. And whenever a string literal appears, this one string object is created automatically, so we don't need to say new String. String objects. Explicit use of these new and calling the constructor can be eliminated. And most probably in most cases we will use the simplified syntax. This one. 4. Aliases : Because an object reference variables, source, and address, a programmer must be careful when managing objects. First, let's review the effect of assignment on primitive values. Suppose we have two integer variables, num1 and num2. Num one initialize 25 and numb two is initialized to ten. And let's say num to numb one. And the following assignment, copy of the value that is stored in num one is stored in num to the original value of 1210 is num. T2 is overwritten by the value of the variables num1 and num2 str refer to different locations in memory, and both of those location now contains the value five. Now, let's consider another declaration. Let's say we have name one, which is strained, M1 is equal to hello and name two equal to How are you. So initially the references name one and name to refer to different string objects. We have hello and how are you? Now suppose we say name equals name on the assignment works the same as the integer assignment. Copy of the value of name one is stored in name2. But remember object variables hold the address of an object and it is the address that gets conflict. So we have here an address and we copy it to name two. Now, originally the two references referred to different different objects after the assignment, both name one and name to contain the same atlas and therefore refer to the same object. The name one and name to reference variables are now aliases of each other because they are two names that refer to the same object. References to the object or originally referred, referenced by name2 are now gone. So how are you is now going? One important implication of aliases is that when we use one reference to change an object, it is also changed for the other reference because there is really only one object. So we have only one Hello here. So Alice's can produce undesirable effects unless they are managed carefully. Now, all interaction with an object occurs through a reference variables. So we can use an object only if we have a reference to it. Now, we don't have a reference to how are you. So when all references to an object are lost, that object can no longer, longer, longer contribute to the program. The program can no longer invoke its methods or use its variables. At this point, the object is called garbage because it serves no useful purpose. Java performs automatic garbage collection when the last deference to an object is lost. Like in this case, we have no reference to how're you occasionally guarantee scenes. The Java environment executes a method that collects all the objects might for garbage collection and determines their memory to the system for future use. So the programmer does not have to worry about explicitly reclaiming the memory that has become garbage. 5. String Class: Let's now examine the String class in more detail. Once a string object is created, its value cannot be changed, nor can any of its characters. Thus, we say that distinct object is immutable. However, several methods in the string class return new string objects that are easily the result of modifying the original strings value. So some distinct methods refer to the index of a particular character. For example, we have string name equals hello. How are you? Now, a character in a string can be specified by its position or index. So the index of the first character is 0, the second one is 123, and so on. Now, we'll go ahead and use some of the methods available for us and the string class. So firstly, let's create some strength is create string M1, M2, M3, and M4. First of all, let's use the concat method. We say M1 equals name dot concat. So as we see, the method concat concatenates the specified string to the end of the string. So if we have, for example, the string called Dame contains hello, how are you? And that's a name. Let's add Alex for example. And print it out. We can see that we'll get hello, how are you LF. Another method would be to uppercase. We have m2 equals name. To uppercase. It would convert all of the characters in the strings to uppercase. So if we print it out, we'll get the same line, but an uppercase. Hello, how are you? Now if we want to add Alex, we just use the new string called M1. So here we have Hello, how are you, Alex. Third method is replace. So we can replace any character in the string with another one. So for example, 400 replace the character e. So let's use the string name, but replace, we replace the old character, which is E, with the new character which is x. And printed, will get the same line, but Every is replaced by x. Now, lastly, we have the substring method. This method, let's use it on the name string substring. So we, as we can see, we have two.substring methods. The first one begins with the character at the specified index and extends to the end of the string. However, the second one only extends to the specified index minus one. So let's use the first method. We will specify the index four and print it out. Now, we print M four, will get o. How're you know, if we want to use for now, just take o to how we just add here, one from four to ten. And we'll get o to l. So these are some methods in the string class. There are lots of other methods that you can search for it. You can easily learn about them by pressing the dot operator and seeing all the available methods with the definitions. 6. Random Class: The need for random numbers occurs frequently when writing software. Games often use a random number to represent the roll of a die or the shuffle, a deck of cards. A flight simulator may use random numbers to determine how often assimilated fly has in giant travel. Also, the program designed to help high school students prepare for the SAT. They use random numbers to choose the next question to ask. The Random class is part of the Java that util class. Now let's go ahead and explore some of the methods available for us and this random class. So first of all, you need to create our constructor, tandem. Let's name it generator equals. And let's import it from the java.util class. So now we have our constructor called generator. We can use it to invoke our methods. So first of all, let's create an integer called P_1 and use our constructor. And the next method, this is the first method we're going to use. This method. Next, n can be called with no parameters, or we can pass it a single integer value. For the version with no parameters generates a random number across the entire range of integer values, including negative numbers. So now we go ahead and print it out. We'll get positive number, a negative number, and so on. Now, usually we need a random number within a more specific range. So for instance, to simulate the roll of a die, we might want a random number in the range of one to six. The next method returns a value that is in the range from 0 to one less than its parameter. So if we type here, six will get a random number between 06 minus one, which is five. Now we need a random number between 16. So to fix that, we simply add one here. So what we saying is we need the generator to generate a random number between 05 and add one to this number. Whatever this number is, just add one to it. Now, we can also, for example, generate a number between minus 109. Let's say two equal generator. But next int. And let's use a perimeter of 20 in this case, and minus ten. So now we have a random number between minus 109. If we go ahead and print this out, we'll get minus 101 more time minus 33. Now, the second method we're going to use as the next float method. It returns a random number between 01. So let's go ahead and define a float, float f generator. That next clock. And print it out. We get 0.98. We can get 0.0.0, but we cannot get one. So it is between 0, inclusive and exclusive. Now we can use it also to generate a number, for example, between 06. To do that, we simply multiply it by six and we get this number, 0.933.89 and so on. So these are the methods that we can use in the random class. We can manipulate them and use them as V1. 7. Math Class: The Math class provides a large number of basic mathematical functions that are often helpful in making calculations. It is defined in the java.lang package of the Java's standard class library. All the methods in the math class are static, which means they can be invoked to the name of the class in which they are defined without having to instantiate an object of the class first. So we don't have free to do what we've done with the random class. We don't have to instantiate the constructor. Now here we have some of the methods available for us in the math class. First of all, we have ABS, which means absolute two-thirds that the absolute value of the specific number. So for example, we have an integer, gold, one equal minus five. Let's use the absolute method. You use it, you simply type math.pi. Absolutely. And specific number which is one. And the code will get five, which is the absolute value of minus five. Moving on to the a cosine and a sine and a danger, they return the arc cosine, arc sine in the arctangent of a number, as the name indicate. Now we also have cosine, sine and tangent. But we need to understand that these angles are measured in radians. Moving on to seal and flow now, sealed returns the ceiling of a number, floor returns a flawed open number. So for example, if we have two with a value of 2.5 is used, the two methods, mass two and mass fluid. To quit on the code will get 3 and 2.0. now we also have the exponential which returns devalue e raised to a specific power. For example, we use this method that exponential to the power of three will get 20.08. Now we discuss the power and then the random methods. Power as as the name indicates, it returns the value of a specific number raised to the specific power. So for example, if we want to perform to the power three, we simply type math.pi two to the power of three. Now we get 8. We also have the random method. It returns a random number between 0 inclusive and 1.00 exclusive. We perform it. We just like math dot random and will get a random number every time we run that code. Lastly, we have the square root method. It returns the square root of a number which must be positive. So for example, if we want the square root of five, will get 2.23. Square root of four, we'll get 2. So these are some of the methods available for us and the math class. In the next video, we'll apply them in a specific code and we'll learn how and when to use them. 8. Math Class: Application: In this video, we'll create a program, we call it the quadratic program. And it uses the math class to compute the roots of a quadratic equation. So the quadratic equation has the general form of a ax squared plus bx c. So our program needs to read the values that to present the coefficient in a quadratic equation, a, b, and c. And then evaluates the quadratic formula to determine the roots of the equation. So the quadratic formula is for x plus equal to minus b plus square root of b squared minus four ac over 2A. And the same thing for x minus, but with this mine sign. So let's divide our formula into smaller tasks to first of all, let's finish with this b squared minus four ac and then continue in our formula. First of all, we need to create some integer is a, B, and C. The one. Now we need to ask the user to give us some inputs. So first of all, we import the scanner data from java.util system that n. And now we can use this anode will ask the user to enter the coefficient of the x squared, the coefficient of x squared, which is a. So we stored it in. And then ask them again, coefficient of the x, NB. And lastly, we should ask them to enter the constant and we'll store it in c. Now, let's perform this step, b squared minus four ac and store it in D. So we say d equal, we have Math.pow, b to the power of b squared minus four times a times C. So now we have this, but let's go ahead and continue the formula. So our first one should be equal to minus b plus square root of E over two times a. Now the same thing for root two, but with the minus square root. A minus B minus, but square root. Sorry. But that square root of two times eight. And then we'll print the two roots. One plus root one. Same thing to two plus two. So let's go ahead and use it. Enter 34. So we get one minus 0.66, and so on. Do my cold minus 2.0. so now if we try them out on the calculator will get the same exact numbers. So this is how we use some of the methods in the math class. And see you in the next video. 9. Number Format Class: In this video, we'll talk about formatting output. The number formatter class and the decimal format class are used to format information so that it looks appropriate when printed or displayed. They are both part of the Java's standard class library, and they are defined in the Java dot txt package. So in this video, we'll talk about the number for a math class. This class provides generic formatting capabilities for numbers. You don't instantiate a number formatting object by using the new operator. Instead, you request an object from one of these static methods that you invoked through the class name itself. So here we have some of the methods of the number format class. First of all, we have format which returns a string containing the specified number formatted according to the object pattern. We also have number format gap currency instance. So this method returns a number format object that two presents a currency format for the current local. Lastly, we have the get percentage instance, which returns a number format object that represents a percentage format for the local, for the current local. So now lets demonstrate the use of the number format class to format output. So let's try to go and code that calculates the final price of a purchased item using values entered by the user. So here we have our main method. Let's define a double tax rate equal to 0.05 for example. So we need to have a quantity and some doubles. Our first cost. Let's name it. Subtotal. Ducks, total cost and price. So we need to ask the user to give us some data. So we use the scanner. Scanners scan equals new scanner system. But now we'll use the number format. To apply the number format, we simply type number format F1. And as we said, we don't need to use the new operator, just type number four on my dot. And here we have all the methods. We use, the currency and stance. And number four, on the phone, my F2 equal number format, dot get percentage. And so first of all, we need to ask the user to enter the quantity. So we'll ask quantity and stored it and the integer called quantity. Scandalous next, and then ask them to enter. And so the unit price. Next, wwe. Now to have our subtotal of sub total cost, which is equal to one to t. Two. We simply multiplied the tax rate by the subtotal, so tax. Multiply that sub. And lastly, our total cost is the sub-total plus tax, our tax. But here we have a subtotal and then total cost. So now let's use the number format here. First of all, let's print our subtotal is what we'd say. Subtotal. It turned out the free run this code and the quantity 53 would get 15. So if we want to use the number format and simply type F1 and enter the sum total in the parameter that's printed out, we get 15 pounds. Now. Let's print our tax and our tax rate. So I will is our tax, but we need to format it also. So we use a foreign tax at a rate of we also need to format the rate, but we formatted using F2 to get the percentage now. So it should be 0.05% F2 dot format and that tax rate. And now let's enter five. B, get subtotal 15 pounds, that's 0.75 pounds at 5% rate. So instead of saying 0.05, we will get 5%. Now, lastly, let's print our total costs and we'll get 15.75. So this is it put the number format. And the next video we'll talk about the decimal format. 10. Decimal Format Class: Now we'll talk about the decimal format class. Unlike the number four multicasts, the decimal 400 class is instantiated in the traditional way using the new operator. Its constructor, takes a string that represents the pattern that will guide the formatting process. So here we have some of the methods. First of all, we have the constructor that creates a new decimal format object with the specified pattern. So we'll discuss this pattern now. And we also have this tank format that returns a string containing the specified number formatted according to the current pattern. Just like the non-performance class. So now let's try to code and use the decimal format class net. So first of all, we need to ask the user to type some data. So we'll use this kind of task sky system that n, as usual. Let's define an integer, integer name point and ask the user to enter a number and store it in one. So now, let's multiply this one by pie. So we'll take this one, multiply it by math. In the math class will have a method which is math dot py. This method returns a double value that is closer than any other two pi. So we'd have doubled. Let's use here w and printed out from the code number for, for example, get this large number. So now we want to use that decimal format class. In this case, we simply define it decimal format. Let's name it d. We import it using Jaffa texts package the equal. Now we'll use the new operator, decimal format and we need to add some parameters. So various symbols are used to represent particular formatting guidelines. So for example, this pattern indicates that at least one number should be printed to the left and atleast three numbers should be printed to the right. So here we have 12, then we'll print 12. But here we have only three, then we should only print 566 to the right. So now, if we use it on this number, we can use that format. And it takes one parameter and print it out. We get 12.56. So this is it for the decimal format class. See you the next one. 11. Printf Method: In addition to print and print a land, a system class has another output method called print f, which allows the user to print a formatted string containing data values. The first parameter to the method represents the format string and the remaining parameters specify the values that are inserted into the formatting string. So now to understand it, let's use it. Suppose we have an integer called ID equals 12345 and this string. So first of all, let's use it and then I'll explain it. So we have a t percent S IT a name for now is printed out, will get ID 1-2-3-4-5 and name. The first parameter specifies the format of the output and includes literal characters that clip at the output values. Here we have in the double quotation, ID and name. And he, and we also have the five percentage, five D. This indicates that the corresponding numeric value, which is the ID, should be printed in a field of five characters. So we have five characters. And also the patent percentage as matches the strain parameter. The values of id and name are inserted into the string, producing results such as this. So now this print f method was inserted and added to Java to measure a similar function used in programs written in the C programming language. So it makes it easier for a programmer to translate or migrate an existing C program into Java. However, this method is not particularly clean object oriented solution to the program, to the problem of formatting output. So we'll avoid using it here. So this is it for the method, which is the print f method. I just wanted you guys to have an idea about it and see you in the next videos. 12. Wrapper Classes : As we've discussed previously, Java represents data by using primitive types such as n_, double, char and boolean. In addition to classes and objects, having two categories of data to manage primitive values and object references can present a challenge in some circumstances. So sometimes we need to wrap a primitive value into an object. So here we can use the wrapper classes. Wrapper class represents a particular primitive type. For instance, the Integer class represents a simple integer value. So for example, when we say ten to one, so here we have an integer which is a primitive data type. So for example, we can say integers to integers times. Once this declaration are performed. That too is an object and represents the integer 40 as an object. Can be used wherever an object is needed in a program better than a primitive type. So we can present then as a primitive type or as an object. So for each primitive type, there is, there exists a corresponding wrapper class in the Java Class Library. Or wrapper classes are defined in the java.lang package. So he will have some of them for four byte by byte capital B shortly have short with capital S, and so on. Now, let's see some of the methods of the Integer class we have. First of all, the constructor creates a new integer objects storing the specified value as we did in the example. We also have the byte value, double value, and so on. They return the value of this integer as the corresponding primitive type. Now, one of the most important method, it is a static method, which is the past and that returns the end corresponding to the value stored in the specified string. So if we have a string that contains a specific number, we can convert this into an integer using this class. So let's go ahead and try it out. For example, if we have a string called number equal to a 100, let's create an integer called equal number plus two. So now you can see that it generates an anode saying that cannot convert from string to. And so what we can do here is to create an integer called a, for example, and use the integer class integer. And let's pass the string call number. Now we can have. The number and add it to two. And everything works well at now. And the code will get hunted to. The Java wrapper class. Classes often contained static content that are helpful as well. For example, we also have the integer in the Integer class, two constants, men value at max value that told the smallest and largest end values the spectrum. So for example, if we want to use them, we can simply create an integer called men. Would use them digit class. Then value will get the minimum value of the integer, integer that max value. Go ahead and print them out. We'll get these two numbers. So this is the min and max value. Now, the last thing we'll discuss in this class is autoboxing. Autoboxing is the automatic conversion between a primitive value and the corresponding wrapper object. For example, let's consider an integer object. We have an integer object one and primitive type integer num one equal. So if we say object one equal one. So here it automatically creates an integer object to instead of creating of saying object one equals new object and give it the value of ten. We can say object one equal num one, and automatically do that. Another type of conversion is this, the reverse conversion called unboxing, also occurs automatically when needed. So for example, if we have object to an integer num two, let's give this integer value. An integer value. Then. Now, if we say num to object to, what we're doing here is automatically extracting the integer value here. So extracting ten and give it to num two. Assignment between primitive types and object types are generally incompatible. The ability to adopt a toolbox occurs only between primitive types and corresponding wrapper classes. So we cannot use the Integer object integer class with another primitive type. So for example, we cannot use integer with double, double with integer because this would cause a compile time error. So this is a photo wrapper classes. See you in the next class. 13. Anatomy Of A Class : In all of our previous examples, we've written a single class containing a single main method. These classes represent small but complete programs. And these programs of an instantiated objects using predefined classes from the Java Class Library. And use those to objects for the services they provide. Those predefined classes are part of the program too. But we never really concern ourselves with them other than to know how to interact with them. We simply trust them to provide the services they promise. So now let's create our own class. So I already created a class and named it, died. Now let's fill it. And so first of all, we need to create a constructor. To do that, we simply public and the name of our class, NonEmpty parameter, and this is our constructor. Now, let's have some integers outside. We have private and base value, reduce it now. And private matters, for example, which is equal to six, would use data into methods. So now we have our constructor. Lets save that face value is equal to one whenever we create a die object. Now, let's create some methods. One method we might think of is to run the dye and returns the result. So and public method, which returns an integer. So public and the name of the method which is roll. And now we tend to work in this method. So now we need to change the face value from one to a random number between 16. So we can do, as we learned previously. Value, we used them mastered random, math, dot random to generate a number between 01 and multiply it by six. So we have a number between 0.995 and add to it one. So now we have an error saying it cannot convert from W to N is simply integer here. And now we said, finally, we return the face value from the method. So what we're saying here is that we need a random number between 06 and then we'll have a random number between 05 because we convert it to end and then add one to it. So we'll have from one to six. So this is the wrong method, is create another method. Sometimes we need to set the face value. So it will create a method called Set phase value which doesn't return any type, will be void, public void, set, base value. In this case, we need to have the value we need to set. And we'd set it by simply. Same face value equal the value we are getting. Another method would be to get the face value. So as we did, we need to get an integer. So the type of the method is n. It returns an integer value with no parameters and simply return based by. So now we have our methods and our main class. So this is our main does. First of all, we need to create an object. So we create an object called tai chi one equal. Now we have an object because die. Let's use some of the methods available for us here. So first of all, we can say, now this method returns an integer, so we need to store it in an example and one by one to call it. Now, we go ahead and print this out. We'll get 623 and so on. Let's use the sad face value. We can say dy one, set face value and set it to five. And if we go ahead and print this out, prone one, that face value, sorry, di1, that get feedback, you will get the value of five in this case. So now when important method and it will be useful for us in some cases, is the toString method. So in this case we can go and created here, simply create a public string, and let's name it to string. It returns the result as a string. So we will convert the result from integer to string by using the integer to string method. So we have an integer which is the face value. We convert it to a string and return this prank. So usually it is a good idea to define the string method for most classes because we'll use it a lot. So now we can simply print out die alone and we'll get the number five. So here we have our old methods that we've created. We use them in our main method. And this is it for this class. See you and the next one. 14. Boolean Expression : All programming languages have statements that allow you to make decisions to determine what to do next. Some of those statements allow you to repeat a certain activity multiple times. For now, we focus on Boolean expressions. The order in which statements are executed in a running program is called the flow of control. Unless otherwise specified, the execution of a program proceeds in a linear fashion. That there's a running program slash at the first programming statement and moves down one statement at a time until the program is complete. So within a given method, we can alter the flow of control through that code by using certain types of programming statements. We have conditional statements. Conditional statement is sometimes called a selection statement because it allows us to choose which statement will be executed next. And the conditional statements we have f, fs, and switch statements. We'll explore them later in the next videos. But for now, we'll focus on Boolean expression. Each decision is based on a Boolean expression, also known as a condition, which is an expression that evaluates to either true or false. So for example, if we have an integer count equal to 20, Let's set the condition. If the count is greater than 200, creative than 25, Grant down. So if we go ahead and run the code, nothing would happen. Now if we have count to 30 and the code will get 30, that condition in this statement, doubt is greater than 25. That expression evaluates to a Boolean, true or false. So the result, either the value stored in count is created than 25 or it's not. So if it is greater than, we'll print count. And if it's not, we can skip it and move on to whatever call it follows it. They need to make decisions like this comes up all the time in programming situations, for example, the cost of life insurance might be dependent on whether this insured person is a smoker with a personal smoke, recalculate that cost using a particular formula. And if not, we calculated using another. Now we also have blue or repetition statement. It allows us to execute the programming statement over and over again. Like a conditional, a look is based on a Boolean expression that determines how many times the statement is executed. For example, suppose we wanted to calculate the grade point average of every student in the class. The calculation is the same for each student, does just performed on different data. So we would set up a loop that is the calculation for each student until there are no more students to process. For. For example, we can feed forward loop. Suppose we only have two students. So we set the condition i is less than two. We ask the user to enter the grade. Of course. First of all, we need to import the scanner scan new system. And then we can store and a double its name as w one equals x dot will then ask the user to give us a second box and store it at double-quotes. To. Now we have the two gates. We can simply add them and sorted them in. And the double called add one plus two, then divide them in a double called d divided by two. Now we have, our average is simply print it. So let's go ahead and try it out. We'll enter fixed 0.64.56. We have an average of 50 or 5554.58 for the first student. Now for the second one will be 78.9 T 9.87, we get an average of 64.85. So this is it for Boolean expression. See you in the next video. 15. If, if-else: In previous videos, we've used the if statement. An if statement consists of the reserved word if, followed by a Boolean expression, and then a statement. The condition is enclosed in parentheses and must evaluate to true or false. So as we did before, let's suppose we have an integer call down the value of them. Let's set the if statement is less than 20 and go ahead and print it out, run the code, then you get ten. Now, sometimes you want to do one thing if a condition is true and another thing, if the condition is false. We can add an else as close to the if statement. If else statement. For example, in this case, if count is less than 20 printed out, otherwise else, Hello. In this case, we'll get ten. However, if count is equal to a 100, for example, we get Hello. Now, sometimes we may want to do one more than, more than one thing as the result of evaluating a Boolean expression. So in Java, we can replace any single statement with a statement. A block statement is a collection of statements enclosed in braces. We've used these braces many times in previous examples to enclose method and class definitions. So for example, if we achieve, now, let's set the count to ten. We can see that at Princeton and then this statement, so this line is not part of the S clothes. To make it as close, we'd need to add curly braces here. And now if we run the code, it will not appear if count is equal to a 100 in this case, and the code will get these two lines. Now sometimes the statement executed as the result of an if statement could be another if statement. This situation is called an asteroid F. It allows us to make another decision after determining the results of a previous decision. Let's go ahead and write the code that takes three integers from the user and find the minimum between them. So let's delete this and put this kind of system that now we ask the user to enter the integers and stored them. Three variables called num1, num2 next and same. But the second and the third, click Next. And now we have our data. Let's start with that with statements. First of all, we'll compare the two numbers. The first two numbers, num1 and num2. If num one is less than num to then compare this number with number three. This is the case. Then the minimum between these three numbers is num one since it is less than numb two and less than three. So the minimum, let's create a minimum, set it to 0. And if the minimum equal to num one. If this is not the case, then the minimum is number three. Now finished with this statement, we go back to the bigger one. We said that if num one is less than numb two, we perform this. Otherwise. We have now num two is less than num one. That's compared with number three. This is the case minimum. Otherwise, minimum is equal to three. So now, if we go ahead and print the minimum, we'll get into the integers. For example, a 100899800450. We'll get a 100 because it is them minimum. So this is it for the nested if. In the next example, in the next videos, we talk about loops or repetition statements. 16. While Loop: As we discussed in the previous videos, in repetition statement for loop allows us to execute another statement multiple times. In this video, we'll talk about why statement. The while statement is a loop that evaluates a Boolean condition just as an if statement does, and executes a statement if the condition is true. However, unlike the if statement, after the body is executed, the condition is evaluated. Again. If it is still true, the body is executed again. This repetition continues until the condition becomes false. Then we continue with whatever follows it. For example, let's create a wild loop. Let's say we have an integer called i equal to one. While count is less than or equal to five. This number out and increment by one. Now if we run, the code will get 12345. So now Let's compute the average of a set of values entered by the user. For example. First of all, we need to scan as visual system. And now our sum is equal to 0. I would value and count will have an average. We can create a cater. Now, let's ask the user to enter an integer. And if you wanted to quit, you can simply, let's sort it and next end. Now, why this value is not equal to 0. This loop will be executed. So first of all, we'll increment our count to count how many times the user enter an integer. And then we'll add this value to the sun every time and print the sum so far. And after that, we'll ask the user if she wants to quit. If he wants to quit, then they should provide us with 0. So 0 is not 0, then this loop will continue. So we'll start it in fairly equal standard text. Now, if he entered 0, then we'll check value is equal to 0, will go back to here, and whatever follows it. If the value is not equal to 0, then we repeat these statements. Now, we have a while loop. Let's print a line and then if count equal to 0, so if the user didn't provide us with any value, then we'll print no values. Otherwise. The average. So let's compute the average. We have a variable called average is equal to sum over how many times we need to convert it into W. And this is our average printed out. Now let's find the code. So we have, for example, 53816 if you want to quit by 0. And this is the average. Now, we can format it using the decimal format. So in this case, let's use it. Let's name it F equal decimal format. In this case, let's format I want only three after the decimal and print the average formatted. And now here we go. In this rented out. So 30, the average is 4.6. Sorry, there's an integer 5913, the average is nine. So this is it for the while loop. And see you next video. 17. Switch-case : Another conditional statement in Java is called the switch statement, which causes the executing program to follow one of several paths based on a single value. Similar logic could be constructed with multiple statements, but the switch statement usually makes code easier to read. This switch statement evaluates an expression to determine a value and then matches that value with one of several possible cases. Each case as statements associated with it. After evaluating the expression, controlled jumps to this statement associated with the first case that matches the value. So now, let's go ahead and demonstrate the use of a switch statement. So let's write the code that feeds upgrade from the user and prints comments accordingly. So first of all, we have an integer. And let's use the standard system. And ask the user. Grade eight. Let's print a line. Is. Over. Now we have stood in the kitchen. And let's go ahead and start our first of all, I would write switch and grade, which is now our cases. So if this is equal to ten, then we print out case nine. Degree is equal to nine. We'll print out average. We'll print above average, below average. And finally, the default case. And which now of course we need to add a break statement for each case. So let's go ahead and copy this and paste it each and every case. And now we're done with the switch. So let's go ahead and use it. So we'll enter that over time. Then. Suppose eight, the grade is baba. Now let's try it again. Using the grade of four. We get the grade is not passing. So if no case value matches that of the expression, execution continues with the optional default case, indicated by the reserved word default, as we did here. So if no default case exist, no statements in the switch statement are executed. So if we don't have any default case, then will not print anything. Let's try it out. Given take data for a grade as and doesn't print anything. So it is often a good idea to include the default case, even if you don't expect it to be executed. So we'll leave it for now. So this is our default case, and this is our code. So yeah, this is it for the switch statement. See you in the next video. 18. For Loop: The wine statement is good to use when you don't initially know how many times you want to execute the loop. But the first statement is another repetition statement that is particularly well suited for executing the body of a loop. A specific number of times that can be determined before the loop is executed. So for example, let's suppose we have a for loop or that set i equal to one and less than equal to five. If we run the code, we get 12345. So this code prints the numbers one through five using the for loop, just as we did using other loop statements in previous examples. Now, the header of the for loop contain three but separated by semicolons. Before the loop bigger ends. The first part is the header is executed, so this is the first part, is executed only once. And then the second part of the header, which is the Boolean expression, is also executed. After that, we execute the loop body. And then finally, we'll execute the last part of feather loop of the for loop, and we'd repeat until this condition is not satisfied anymore. Now, lets demonstrate the use of a for loop. For example, let's write the code that takes from the user to input one number and element and multiply the number by two every time until it reaches the limit. So in this case, we'll use the scanner as usual. And you ask the user to provide us with two numbers, the number, and store it to integers. And now we can use them. So we'll create our for loop. We set i is equal to our number at first. And while i is less than or equal to the limit, this loop will keep running and I will multiply it every time by to get out. Now, if we provide the this program 76, we get 481632646464128, which is over the limit, then it will not print it out. Now, let's print them at the same line. Clear idea, what's going on. So we have four. 248163264. Now for example, if we don't want to have any number between 1535. So in this case, we'll use if statements inside the for loop. So let's go ahead and set this f statement. If the number which is i is greater than 15 and at the same time is less than 35. Continue. And if we go ahead and run this code, or we could only get 4864. Lastly, we'll discuss the nested for loops, which is a loop, a for loop inside another one. So let's demonstrate the use of this nested loop by printing a triangle shape using characters. So in this case, that's just two rows equal to ten. And create our counter for loop has changed to o is less than rows. And o plus plus. So this is our first for loop. Now inside this for loop recreate another for loop. This loop just print line. Now the inner for loop to print the stars. So everytime print the number of steps and a specific number of sides for, for example, the first time that's set equal to one. So the first time we print only one star. And go ahead and start here. And then explained what's happening. So here we have our triangle. So basically what we're saying here is that the outer for loop will execute ten times. So we have rho equal to one until row equal to ten. So we have ten times, so ten lines. And the inner loop will print a specific number of stars. So at the first line will print only one star, since we have star equal to one and star is less than or equal to o. So we only have one. Now when the rho is incremented by one, so rho equal to two in this case will print two stars, and so on until we reach the last slide. So this is it for the forward loop and the four and the nested for loops. See you in the next video. 19. Break And Continue Statements: Java includes two statements that affect the processing of conditionals and loops. And this video will talk about break and continue statements. When a break statement is executed, the flow of execution Tange was immediately to the statement after the one governing that current flow. For example, if the break statement is executed within the body of a loop, the execution of the loop is stopped and the statement following the loop is executed. Next, it breaks out of the loop. So now, considering we have an integer called count equal to 0. And let's create a while loop. An infinite loop because could keep running forever since this drew and never turn into false. So let's increment count and print it out. And set a condition down is equal equal to five. Then break out of this loop. Let's run the code and see what will happen to break out a count equal to five. So we printed out which act count is equal to five to five, then break here. And so now we get five. And then at the end. Now, this break statement is never necessary in a loop. And equivalent loop can always be written without it. You can and should avoid using the break statement in the loop. So if you want to set this condition, can easily set it here. So we can say is less or equal, equal to, less than or equal to five. And we get the same result here, 12345, followed by the end. Now we also have the continue statement. It has a related effect on loop processing. The continue statement is similar to a break, but the loop condition is evaluated again and the loop body is executed again. If the condition is still true. Like the big statement, the continue statement can always be avoided in a loop and for the same reasons it should be. So now let's go ahead and use this statement after incrementing down. Let's suppose down is equal to three, then continue and see what happens. So we have 1245. So number three is skipped. So what this continue. So this is continue here. We are saying that if count is equal to three, continue. So we jumped from this loop again to check count is equal to three in this case incremented, so now it is equal to four. Then check this condition is not true. Jump, print four and so on. So this is it for the break and continue statements. See you in the next video. 20. Iterators: In this video, we'll talk about iterators. And iterator is an object that has methods that allow you to process a collection of items one at a time. That is an iterator. Let you step through each item and interact with it as needed. There are several classes in the Java standard class library that defined iterator objects. One of these classes is the Scanner class, a class we've used several times in previous examples to help us read data from the user. We have the has next method of the scanner class that returns true, but there is another input token to process. We also have, as we've seen previously, the next method that returns the next input token as a string. So now let's go ahead and demonstrate the use of scanner to read file input and pass it using alternative delimited. First of all, we create our file. So we created the file, it has two lines, googled and Linux. Now let's create a string that is phi. This phi lines string. Let's call it URL for example. Now we'll create our scandals than let's create the file, read the file, and you can use later. Now we have our files can be defined using this equals new scanner. Now instead of thing, System.out n will be provided with the name of our class, in this case, five dxdy. Now, of course we need to import the file and threw an exception. The exception. Now I'll centric and read this file. So we have the file scan, but has next method. So this method tells us if there is an x line. If there is an x line, we can keep running this while loop and store it and you scan that next line. So now we have our first line here. Let's print it out and see what happens. Print out the URL. So we have the first line, then, the second line. Now sometimes we need to divide a same line into parts. So for example, we can use Telemachus in this case. Let's say you need to divide this into three parts. This is the first part, slash, this is the second part, and then this is the third part. So we can use here delimited. Now we'd have this string. We can use the URL scan. She's a scanner and read this chain along. As we said, we can use that URL and give it the value of u at L. And now we can read it. Let's use the perimeter. We can say simply a US. Us can use kilometer and specify our delimiter in this case, which is the slash. And let's go ahead and print f report using the URL scan that hasNext as we did for the phi and this bug out. The next scholar done that code will have, because the first line we only have one part, so we print it out one more time. However, in the second line, we have three parts, so we print the whole line first, and then we print each part after that. So this is it for the scanner, how to read from the file and how to use the delimiter. See you in the next videos. 21. ArrayList: Now that we have a loop and our arsenal of programming statements, let's introduce a very useful class for managing a setup objects. The ArrayList class. This class is part of the java.util package of the Java's standard class library. An ArrayList object sorts a list of objects and allows you to refer to each one of them by an integer index value. We will often use loops to scanning through the objects to in this list and deal with them in one way or another. Now, we can create an ArrayList without specific specifying the type of elements and which case the ArrayList source object preferences. So this is usually not a good idea because the point of being able to commit to storing particular type and a given ArrayList object, that's the compiler, help you to check that only the appropriate types of objects are being sought in the object. So if we created an analyst without specifying the type, and we only want integers in compiler will not have any problem if we enter some strings. So to deal with this, we simply specify the type that we need only integers. In this case, if we provide this array list with a string, it will compile. The compiler will give us an atom. So let us now go ahead and see some of the methods available for us. We have the constructor that creates the empty list. We also have two methods. One is Boolean and second is void. The first one it inserts the specified object to the end of the list. And the second one specified specified object into the list at a specified index. We also have cleared that removes all elements from the list. He moved that removes the element at a specified index, gets, gets the element is empty returns this list contains no elements. And finally, the size returns the number of elements in this list. Now let's try this class. First of all, we need to create our constructor list, arraylist, and that will set. Let's add some elements. For example, John, Alex, and Chris. So now we have an outer list, three elements. For example, if you want to know the location of Alex in this list, we simply print out the index. Alex. This is one method we can use and will print this at index one. So John at index 012. Now suppose we want to get a name and we know it is at index one. Simply print B, whatever. The index one at index one. So we have islands, footprint, Alex. Let's print the list to see books. So we have done Alex and Chris. Now, we use the add and added these names to the end of the list. So we have John and then Alex and then Chris. Suppose we need to add, for example, Chris. For example, let's poll to between John and Alex. So to do that we simply use the add, specify the index at index one and parenthesis out. We get John, Paul, Alex, and Chris. Now suppose we want to know the size of the list. B. We get the size, which is in this case. For. Lastly, we talk about how to remove elements from the list. We have two methods. The first one, to clear the list by removing all the elements. Let's use it and prints. Now we notice that it is an empty list. The other method is to remove every element alone. In this case, let's create a while loop and chemo every element alone and print it out. So let's use the is empty method to check if the list is empty. When the list is not empty. We'll print every element and move it at the same time by using the remove method B dot remove the first element in the list at index 0. Now, we need to remove this clear because we'll not have the name Snow, John, Paul, Alex, and Chris. So if we go ahead and print the list, we find that this empty now. So this is it for the ID list. This class is so helpful and to store data and use them whenever we want to. So this is it for this video and see you in the next point. 22. Array: An array is a simple but powerful programming language construct used to group and organize data from writing a program that manages a large amount of information, such as a list of a 100 names. It's not practical to declare separate variables for each piece of data. As solves this problem by letting us stickler one variable that can hold multiple individually accessible values. The array is a list of values. Each value is stored at specific numbered position in the array. The number corresponding to each position is called Index. In Java, I, indexes always begin at 0. Therefore, the value stored at index five is actually the sixth value in the array. So now let's learn how to declare and use arrays, for example, and describe it. And then we'll explain. And numbers equal u. And, and let's give it a value of n. So what we're saying here is that we need an array of ten elements. In Java, arrays are objects, so we can create them as we create objects using the new operator. So the variable numbers is declared to be an array of integers whose type sweetened as nt. So all values stored in an array have the same type. For example, we can create an array that can hold integers, or an array that can hold strings, but not an array that can hold both integers and strings together. Now, let's demonstrate the basic array declaration and use. For example, let's create an array, fills it with various integer values. When the phi one value then append values. To do that. And then use come now. And that's the limit elements 15 elements, that a closed loop equal to 0. I is less than list.length, which is filament. For example. If we go ahead and print it out, I will get 0101 foot. So what we're doing here is that referring the list starting at index 0, index 15, minus1, which is 14. So fitting it with multiples of ten. So we have 01020 and so on. Now, let's change the value in this array. For example, let's change the 30. So 30 is at index 0123. So to do that, at the index of three to 555, and then competence to print it out one more time. And let's say we get, let's print the line between the value at index three is now changed from 13 to 555. The index operator performs automatic bounds checking, which ensures that the index is changed for the array being referenced. So whenever a reference to an array element is made, the index must be greater than or equal to 0 and less than the size of the array. For example, here we have an array of elements. And I chose to change the third element at index three, which is the fourth element. So for example, i minus one, we get IndexOutOfBoundsException. And the same thing would occur until 15, for example. You get the same thing, that array index out of bound exception. Let's treat a single list of numbers from the user, stored them in an array, and then print them in the opposite order. So for example, let's delete this and create our scanner and ask the user to enter the size is size. And so now we have the size of g at a. And let's create our array. And let's name it. We have the size. Now we have to fill it. And so we're asking the user to enter the elements. Element. So now we have our array went in the opposite order. We start by the last element and go back to the first one. So to do that, we create a for loop. But it will not start at r equal to 0. To start at i equal to the length of the array minus one, which is the last element. So length minus one and n two n. When i is greater than or equal to 0 and will document instead of incrementing the elements space. And it's run the code size that is, for example, by eight x 17. And we get 4176312, split them in the same line, and I will get 54321. Now, we can use an initializer to instantiate an array and provide the initial values for the elements of the array. There's essentially the same idea as initializing a variable, a primitive data type, and its declaration, except that unary requires several values. So the items and an initializer list are separated by commas and delimited by braces. So when an initializer list is used to, the new operator is not used. And the size of the array is determined by the number of items. And this initializer, for example, let's say we need an array. Scores, races 54424312. And so this is our array printed out. This the same as the other one that I will have. The same, 54431215. And now we've used arrays with primitive data types. So in the next video, we'll use as objects. 23. Array as Object: In the previous examples, we use earnings to sort of primitive data types such as integers, characters, and strings. Arrays can also store references to objects as elements. Fairly complex information, information management structures can be created using only arrays and other objects. So now lets demonstrate the use of an array as an object. First of all, we have the main class and I created it, create class that we'll use now. In this class, we'll have two variable data types. Drain. You have a name and an integer lower bound. So what we're aiming to do is to create a great class that takes a great and the lower bound, for example, grade of a and the lower bound is 90. And then use the methods available in this class. And our main method, you think arrays. First of all, let's create the constructor here. And it will take the two parameters, and integer c for example. And I would name the value of V, and I would lower bound the value of C. Now, our constructor as some methods. So first of all, we have the two string to print out. As we did previously in politics tend to strain it simply. I would name plus the lower bound. Now, we have to private variables, string and integer. So we may want to modify them. So recreate a setter and a getter methods to them to be able to use them in the main method. First of all, let's create the Center for the name, public, boyd, disagreement, name, and a string. Let's call it created. This method will only set the name of our class here. So in this case, to get the public, we need to get the name which is a string. So the type of string and David getName, just the name. Now in Java, we have an automatic generator that generates the setters and getters for every variable. So here we have only two variables, we can create them. But if we have, for example, 20 variables to be a good idea to create each and everyone of them. So let's, for example, use the automatic generator, right-click source, generate getters and setters. In this case we have lower bound and name, will choose both of them and generate. And it will automatically generate all the setters and getters. Getname, setName gets lower bound and set lower bound. Now let's use this class and the methods to create an area of great objects and print them. So let's save this and go back to our main method. We create an array of, let's name it and open the curly braces. Now, remember, this is not the primitive datatype. Discrete is an object, and we said that it should take two parameters, a string and an integer. So to create the first value is 19. And other will be the same thing. And let's suppose 0. Scott. Now we said this, and now we have our grades and print them simply. This is a, let's find the good grades, a, B, 70 and so on. So this is it for arrays as object. See you in the next video. 24. Exception: As we've discussed briefly, problems that arise in a Java program may generate exceptions or errors. An exception is an object that defines an unusual situation. It is drawn by a program on the runtime environment and can become an handled appropriately if desired. And the other hand, an error is similar to an exception, except that an owner generally represents an unrecoverable situation and should not be cut. Java has a predefined set of exceptions and errors may occur during the execution of a program. So some situation that goes exceptions to be thrown. For example, attempting to divide by 0 and index an array index that is out of bounds a specified for file that could not be found. So these are just a few examples. There are dozens of others that address very specific situations. Now, we have several options when it comes to dealing with exceptions. And programs can be designed to process an exception in one of three ways. It can, first of all, not handled the exception at all. Secondly, handle the exception when it occurs. Or lastly, handled the exception at another point in the program. We explored each of these approaches in the following videos. For now, we talk about uncaught exceptions. So if your program does not handled the exception at all, it will terminate normally and produce a message that describes what exception occurred and where it was produced. Information associated with an exception is often helpful in tracking down that cause auto crop. For, for example, let's look at arithmetic exception. For example, this exception occurs when we attempt to divide by 0. So let's say we have an integer x equal to ten, integer y equal to 0. And let's try to divide x, y. As you can see, the program shows this exception, which is arithmetic exception, and it occurs when an invalid arithmetic operation is attempted. In this case, the program attempts to divide by 0 because there is no code in the program to handle this exception explicitly. It terminates when the exception occurs, printing specific information about the exception. So now let's go ahead and, and learn how to read this exception. So the first line of the exception output indicates which Excel exception was thrown and provide some information about why it was thrown. So we have the arithmetic exception, and the reason is because we divided by 0. The remaining lines are the coals stack trace to indicate where the exception occurred. In this case, there's only one line and that goes back trace, but there may be severed and depending on where the exception originated. So here we have only one line, main.js at line five. So it is a terrifying when we attempted to divide then by 0. Now the call stack trace information is also available by calling methods of the exception class that is being thrown. The method get message, for example, returns a string explaining the reason the exception was thrown. And the method print stack trace, prints the call stack trace, and we talk about them later in the next videos. So this is for not handling the exception at all. In the next video we'll talk about trying to catch statement. 25. Try and Catch Block: Let's now examine how we catch and handle an exception when it is thrown. The try-catch statement identifies a block of statements that may throw an exception. A catch clause which follows a try block, defines how a particular kind of exception is hand. So the try block can have several catch clauses associated with each catch clause is called an exception handler. So when you have a try statement and it is executed, this makes statements in the try block are executed if no exceptions are thrown during the execution of the try block, processing continues without going to the catch clauses. The situation is the normal execution flow and should occur most of the time. Now, sometimes an exception might be thrown. So at this point, during the execution of the try block, that control is immediately transferred to the appropriate handler. So control transfers to the first catch clause whose exception class corresponds to the exception was thrown. So now let's take an example on how to use a try and catch block. Suppose we have and number equal to. And let's take this. Now. As the user is your ID and your number together. So we previously know that DID consists only of two numbers. So we should take the number next. Now we have our number and the first two characters in this number. The ID number and other characters are the name of the user. So let's use the substring to take this number and store it in an integer called NB equal and converted from string two integer, integer. That too. And we have bus and we'll use the plus end on this substring of the number, numbers. But substring, we know that it is only the first two characters are from 0 to two. And go ahead and print it out. Let's suppose that our ID is I11, Addie, we get 11. So for example, if by mistake the user typed one, we get number format exception. So let's deal with it using the current catch block. First of all, we try to do this. If something happened, such an exception, we simply catch it using the catch block. And for example, put exceptionless name. In this case, if we run the code, we have had 11 were good and valid input since you don't have to number one had the same an invalid input because we catch the exception and we execute the code into catch. So in the next video, we'll do another example that demonstrate the use of a catch block. 26. Try and Catch: Application: Now let's demonstrate the use of a try catch block and using analytic example. And this example would ask the user to enter some goods and divide them to valid and banned. Now, this is sampled, good. We have this quote. What we're going to extract from this code is the phone numbers in the metal and this character. So to extract them from the user's input, will use for the phone numbers, the substring method to extract from 0123, the third index, 4567. So we get these four numbers. And to extract the letter will use the method to extract whatever letter is on index at index nine. Now, first of all, let's use this garners system that n, as usual. And ask the user to enter product code. And if you wanna just entered 3x two. And we'll store this number and a string. Let's call it code equal. Equal to. Next line. Now we have our good as defined and digits that we'll use in our loop. First of all, we need to extract that character, so it a character called w1 and need to extract the number. So we need the integer d. And you also need to divide these codes and to valid and band so many counters to do that. Define a valid initialized to 00 as well. For now they are equal to 0. Now we start the while loop to examine each and every code the user provide us. So I would loop will execute until the code is equal to 3x two while the code is not equal to x, x x. Then continue running while loop. Now, let's extract our character and store it. And so I would character is at index nine from this, from And next time we'll get the character. And also, for example, as we did in the previous example with the four numbers here. So we'll use that substring. From three to seven, you get the phone numbers. And lastly, if d is greater than 2 thousand. So we said in some conditions to know if this code is not band, its band. So in this case, the code is meant if the d is greater than 2 thousand and the zone character equal equal two, then plus, plus. And here. Every code is valid if we can extract this information from them. So now if we go ahead and find a good band, violet will get centered this code. And it will get, first of all, we forgot to ask the user to provide us with another input. Good, who started the code? Let's go ahead and run this code and will get Van j1 and pilot one. Go ahead and try one more time. Change this to d. And we get benzene. Since r is not equal, this one is not equal to i, is equal to t. So benzene and pilot plant. Now perhaps by mistake, we provide it with this number. For example, an exception will occur, which is number format exception. So in this case, we need to handle this exception. Now, we also have another exception that might occur. For example, if the code is not long enough, we have string index out of bounds since we cannot extract something snot here. Now let's fix these two exceptions by using the triad guts clock. First of all, and our while loop, we try to do this. So this is outgoing, will try to do it until something happened, we catch the exceptions. First of all, we have an exception called string. Index out of bound exception is needed. And it should print the integer code length is not enough. That's printed. Length not enough. Another exception might occur, which is as we said, number format exception, exception that the, the number extracted from that goal. And finally, let's take these two lines. So here we have our good. And let's try it out. For example. This one more time, but let's change that to t In this case. And try another 1.3 letters, get integer code tactic is not enough. For example, if we have this code, but with the t in the middle, the programmers hated the number extracted from the code is not numeric. So we handled both exceptions very well using try and catch block, the string index out of bound and the number format exception. Finally, let's quit. And we'll have one band and two valid. So this is how we can deal with exceptions using time and catch pluck to you in the next video. 27. Exception Propagation: If an exception is not God and handled where it occurs, control is immediately return to the method that invoked the method that produced this exception. We can design our software so that the exception is caught and handled. At this Alto 11. If it isn't caught, the control returns to the method that called it. This process is called propagating the exception. For now, let's demonstrate exception propagation. First of all, we have our main method and we created a class called methods to create some methods here. And three methods. First, mess that method will be void. Let's name it 11-1. And first of all, this as level. And then try to call the second method. And if something happened, catch the exception. And let's print the line. After we print the message of this exception message, message. You can get the message and then we'll call the stack trace, then the line and then the call stack trace them by simply print step case. So this is what the catch luck to. Now, let's sprint after that. One. Here, level one. And now another method, for example, to this method is only coding level three. So let's print 11 to beginning and ending. And finally, we'll create our last method. Let's call it level three level. And this method will simply k two integers, x equals ten, y equals 0 to have an arithmetic exception, Let's divide them. But first, sprint. Beginning. And hearing too. I've leveraged to ending. And after that, you divide. Divide x by y will have an arithmetic exception. Print out s. Now we have our three methods. Let's call a method in our main method. And we have a syntax error. We need to add a semicolon. And notice that we go to a main method and method level one. So first of all, we need to create an object called a method from the method class. Let's name it. D equals new methods. And let's call Level one. Lets first printed program beginning broke down. And after that, that this is the end Program. Now, if you go ahead and run the code, will have this is Program beginning level one, level two, level three, the message factories. And finally Level one. And this is program ending. So now let's see what happened here. Starting with our main method, we printed that this is the program beginning. So we can see it here. And then recall the method called level one. So went to our method's class. This is level one and the method class. And the level one method recalled level two. So this is level two method in which we have this is level two beginning. So we, as we can see, and the level one we have this is level one beginning this, it is here. And then we call W2. So we have transferred to level two. And level two. We have level two beginning this here, and then we call Level three method and go to level three. Here, an exception occurs. So here we have, this is level three, and then we have the exception because we divided by 0, this arithmetic exception. So now we didn't can catch or handle the exception and method level three. So we are transferred back to level two. Now we are in level two, the same thing. We did not catch it here. So we are also transferred back to level one. Here we can catch it. We used the catch block to catch the exception. So we'll print out the message and then that call stack trace to print them out. Now we can continue normally. So we then print, this is level one ending. So as we can see, we print the message and then cause that trace. After that, we can see this is level one ending. And we'll go back to our main method and print this is pro companding. So we did not print any of these two lines because the program is stopped here. And then it is stopped one more time here and got transferred back to level one. So this is a propagating the exception to you in the next video. 28. Checked And Unchecked: The classes that defined various exceptions are related by inheritance, creating a class hierarchy as shown in this figure. The throwable class is the parent of both the adder and exception classes. Many types of exceptions are derived from the exception class, as we can see. And these classes also have many children. These high level classes are defined in the java.lang package. Many child classes that define specific exceptions are part of several other packages. Inheritance relationships can span package boundaries. Now, we encountered some of the exceptions earlier, such as arithmetic exception, IndexOutOfBoundsException. So now let's take a look at string out of bound exception, for example. So go ahead and create a screen name equal to hello. And we try to access the fifth index named by. And then we run the code, see what happen. What will happen, string index out of bounds, exception. Exceptions are divided into checked and unchecked exceptions. So a checked exception must either be cut by a method or it must be listed. And that throws clause of any method that may throw but propagated. So a throws clause is appended to the header of a method definition to formally acknowledge that the method would throw or propagate a particular exception if it occurs. An unchecked exception requires no throws clause. The only unchecked exceptions in Java are objects of types, runtime exception, or any of its descendants. So here in this tree, this is the unchecked exception, runtime exception. We have arithmetic exception, index out of bounds, null pointer, and so many others. We already handled unchecked exceptions such as arithmetic and index out of bound exceptions. To the next video, we'll handle checked exception and then we'll create our own exception. See you in the next video. 29. Checked Exception: Let's now learn how to handle the checked exceptions. So we use the input output exception. And in the first class, we learned how to extract some data from a specific file. So in this video, while we're handling the checked exceptions, we'll learn how to print some data into a file. To do that, we need to import the print writer print fighters from, lastName at GW. And we'll use the new print guide and give it the name of our class. For example, let's name it text that dxdy. And now if we run this code and then refresh our project, we find we have a new file name test for dx t. Now, let's print some data into this file. So for example, let's print ten lines and each line, let's print some random numbers between 1099. So we'll use the atom class and the ureter and quoted from java.util. Now we have our random and print them lines. We need to get a full loop consisting of n lines. And another for loop to print some random numbers inside each of the lines. So it is also bounded by n. Now, let's, let's create an integer value outside and give this value between ten to 99. So we do that, we simply add 90. And so what we're saying here is that we need a number between 089 plus ten, which makes it from ten to 99. Now we have a local variable, so we'll initialize it outside. And we print it into this phi. We named it PW is used the method print and print whatever is outside with white space. And then after we finish, we'll print the line. And lastly, we'll close despite outside. So let's print. Output. File has been created and declared the name, which is test.txt. So let's create the strength outside the train. Test and enter name and use it. One more time here, run the code. So we have print, we have created the output file that's Refresh and then check the test. But dxdy will find some random numbers. And they're all negative because here we have a minus sign that's from the code one more time. And could check. So now we have random numbers between 1099. What we did here is that we used nested for loops, compute random values, and drag them to the output file. After other values are printed, we closed the file. For output files must be closed explicitly to ensure that the data is retained. In general, it is good practice to close all file streams explicitly when they are no longer needed. Now, let's treat whatever we have an hour file. So for example, in this case we can use the file. Reader will limit f, w and important and give it a parameter, the name of the file, and use the buffer BW, FW. Now we already know that I will find 210 lines. So let's create a for loop consisting of ten executions. And inside V8, our inner for loop. We also have ten integers and now let's print them. So we create an integer called read to read every integer by using BWA and print them out. So we ran some space and then print line and we'll get whatever that is. And so every time they will change. And for example, let's set a condition that we don't want any number greater than 50. So in this case, Let's say if R0 is greater than 15, print out 0. Otherwise, print the number. In this case, if we run the code, we can find that every number that is greater than 50 is now 0, however, in the test file numbers. So this is it for reading the file and handling the exception by throwing it at the main method. We can also throw the IOException, since we know that this is an IOException input output five, and of course I'm quoted and it will work properly. But since IOException is a subclass of the exception class, then you can simply throw the exception. See you in the next video. 30. Create an Exception: We have lots of exceptions in the Java Class Library. However, sometimes we might need something that is not available for us in this library. So Java allows us to create our own exceptions by deriving a new class from exception when one of its key sentence. So the class we choose as dependent, depends on what situation or condition the new exception represents. So let's go ahead and create a class, call it. For example, let's call it out of range exception. We'll go to our Package, create class out of range exception. And the confidence. So here we have our class and let's set up the exception object with the political message. So public out of range exception and give it a string called message. Because the super, as we did our first message, let's save it. But first of all, we need to stands derived extends exception to extend exception from any exception class. And then we said we can work with this algebraic exception as we want in our main class. In this case. Let's go back to our main method and use the scanner to the user. As usual system that now we can read from the user. And let's create our exception. We create an object, let's name it. For example, problem. And new range exception with a message. Let's specify the input value is out of range. Now will set some conditions on our input. So first of all, ask the user to enter an integer between 015 and then store it. And then let's check if this value is less than ten or it is greater than 15, then we'll throw an exception saying that template is out of range, is greater than 15. Then we can throw this exception that we called problem. And then we'll print. And Our main method. So if the value is less than or greater than 50, then this code will then would never reach this statement. So we never print and of our main method. So for example, let's check it out. Let's print type 20. So we have a main method. However, if we type, for example five, we have an exception out of range exception and put fat it is out. This is the message we specified. For example, sometimes if we want to have different messages for different inputs. So for example, if the user provide us, provides us with a number, blows down, we need to print the specific management message and we print another one if the value is created pendant 50. And this case, we can remove this and let's set the condition. If the value is less than ten, then we'll throw out of range exception and give it a message. So the message is value is less than ten. And if the value is greater than 15, then we throw an exception. Thrown out of range, exception. That's give it. The message value is greater than 15. And now we have an error since we forgot to type new and a new exception. And now let's and the code see what will happen. So if we enter it by algebra, exception value is less than ten and valid 70. Same thing out of range exception, exception, but different. Message value is greater than 50. And we can also ask the user to enter is minimum and maximum. So we can ask and to minimum and maximum n two variables called men. Now, we can ask again to enter an integer between his minimum and maximum so that between and would finish this slide. And then we'll open one more time. The quotation and maximum marks. Some space here. Now we need to fix value. This is less than the minimum and the maximum. So to fix these messages, close minimum, ten of them, one more time. And the same thing, close up in it. And let's run the code. And to a minimum and maximum ten, sorry 1000. The main method, I take 10000, we have an exception value is less than ten, invited. 5017. Let's provide a program with an 18. We get out of range. Exception value is created at 1070 invited. So this is how we can deal, how to create our own exception, and how we can use it in our main method. 31. Array: Application: Until now, we've covered so many concepts in programming in Java. So in the next videos, we'll do some application and some of them will excite it by arrays. So first of all, we write a Java program to some values for this specific add-in. Let's start by creating the array a and with a size of ten. And let's fill it in with some data from the user. And this kind of class and the user inputs. And then let's add all the elements and an integer called some, define it outside to 0 and that all the elements in this array. And then print it out. We say that this is now one. Then we get this sum is 50. This is simple program. Now we can modify it to get the average. So for example, we simply divide the Sun, the average, double sum over ten. And we'll get under code, we get 234, and so on. The sum is 253 and the average is 25. Now, let's move to some more complex, complex codes. So for example, let's find the minimum and maximum value of an array. So we have our array and let's now define two integers called them min and max. And men use the methods and the integer, but give it a maximum value. And the maximum we'll give it a minimum value. And as we're going through, if a is less than 0, then the new minimum is this element. And if a is greater than the maximum, maximum is also this element. After that, we just ran the minimum and the maximum max. Let's go ahead and run the code. We provide one by two and the minimum is 0 and the maximum is nine. Now, the reason why we use integer, that max value and integer min value is to make sure that minimum and maximum are modified in this for loop. Because for example, if we set minimum equal to 0 and give the programs for values. Now the minimum is 00 is not. And this array, since this condition is never satisfied and minimum will always be 0. So this is why we said our minimum at a very high value. Using the integer that max to get the highest, the maximum value and then can have. So this is outgo to find the minimum and maximum. Now, that's right. Another, another call to reverse an array of integer values. Let's delete this. We have our, now, let's reverse it. First of all, let's start and another array to be able to work with it. So we'll create another elegant statement, a2. And at the same size then. And let's fill it then. A2 psi equal to a of i. Now we have another a contains the same elements and add A1. Now we need to fill with whatever a2, but by defacing it, so create a for loop. And as usual, I think at 0 and ending at a dot length and fitting a of i by whatever is at A2 of the last element. And, and going back, so we define a counter outside. Let's name it down equal to that length minus one. So this is after it is executed, will decrement count to get the second value from the right. And then let's print it out. A, I. See what would happen. If you got one. For example, it's tragic. And that 1098765 all the way to one, would write code to find the common elements between two arrays. To do that, we first have an array called a limit A1 and A1. Let's create another at eight, a2 equals integer of size five, for example. And that's with some values. A2 equals can extend. Now. Now we have to address and that filled in with some elements. Now we need to compare every element in the array with every element in the array to. So we need to have nested for loops. First of all, we create our outer for loop, which is foot at a one. We go through all the elements in A1 through this loop. And the second for loop for array to go through all the elements in the second array through this for loop. Condition F one equal to k to j. Then we have a common element, will print this element. So we can print a1, a2, Jay, just printed, and A1 subspace. Then Let's say, for example, it's become an elements. And see how many elements we have in common. Run the code. First of all, 12345678910. The second one, we have 1203040. And so we should have two common elements, 81 and the code will have common elements are 18. The last program tonight is as trying to find all pairs of elements in an array whose sum is equal to a specified number. So let's go ahead and delete these. Now, we have an array of ten elements. Let's ask the user to enter specified number. And then an integer called as scan that Next. And now we have our number. So we're going to check each element with another element and compare it with another element. And check if these two elements have a sum of s, Then we're going to print them out. So what we're going to do is, first of all, take one and compare it with every other element, add them together. If the sum is equal to the specified number, then we print them out. To do that, we simply create a for loop such with zeros and a one dot length minus one. And this for loop, we're going to start with whatever the index is plus one. So I will enter for Luke is i plus one, and ending with a1 dot length, as usual. And f a of I plus a of j equal to S. Put them in parentheses. If this is the case, then print out a plus a. So before we print them out. And we have here, sorry, it is A1. And let's go find the code. For example, was 102015 or 1213. Twenty one hundred thirty zero fifty five and to a specified number, for example, 301020219300. I hope that this gives you an idea about how useful and helpful. And these are simple and quick exercises on arrays. So see you in the next video. 32. ArrayList: Application: Moving on to editors. For example, let us try to call it that search for an element. And first of all, create our list. Just impose it from java.util integer. Let's use the new operator integer. And now we have our list of integers and copy it at different numbers. Then the team 34357. So to find an element in the list, we have two methods. First method is to use lists. Then we'd get the index of a specific number. For example, 34. For example. T equals listed index of printed out. And get off three. Now, if the number doesn't exist, for example, 353 thousand and print it out, we get minus one. Another method we'll be using the Food Loop. And this also simple. We'll go through all the list, every element in this list, and check if this number is at index i equal to 34. Print out. Go ahead and run the code would get also three. Let's swap two elements in this list. First of all, let's print it. And we have 330, thirty four, thirty five, thirty seven. Now, let's delete this and we're swapping two elements. For example, let's ask the user to enter two positions. So if you would like to swap, then we 3410 is at index 134 is at index three. So we will provide us with 13 is two positions to swap. And. We stored them in position one and position two. And now this is how we swap two elements. First of all, we need to store the first element and a new integer that's called temp equal list. The first element two, just as position one, we can remove it from the list and the position to displace. So to do that we simply remove, remove whatever is at index at position 110, add at position one. A new element, which is the same element at position two to, for example, can use listed get whatever it is at position two. Now here we have position one. So if we go ahead now and run this query, and then the last one more time, we'll get 13, you get 33530. Thirty four, thirty five, thirty seven. Now, this is not what we expected. What we wanted is to swap with 34. And to make it clear, let's print the list after, remove the element here, split list, and see what will happen. Then we'll print it. Now we can see what happened here. So at first, 34 is, was at position 30123. Now after removing that, 1034 is at position two. So in this case, we cannot add after position one a position, a new position, since it has changed. So the fixit will simply remove this. And before removing the animals from the list, we define a new element, a new integer called for example, the DOM to the later and list the position. And then at the theater, now we can see that T 30, thirty five, thirty five, thirty seven. And we have to 34. Now. Now to delete this 34, we'll go ahead and use the list.pop, remove and remove whatever is at index. Position 21 does time. Now we have 1334303537. Now the last thing we're going to do is to add the ten that we removed earlier at the second position. So we simply use this put add at position two. And what we're going to add is that print the list. Let's check it out. Provide the program one N3, 33430102537. Let's go ahead and delete all these print methods and keep. The last one. Took 13. We swapped the element at position one with the element at position 324, for example. We swap the position to do with the item at position four. Now, these are simple chords and programs, and you can do so many things. Battery list. See you in the next video. 33. If or Switch?: When to use if and when to use switch case statement. First of all, any treatment we're using one of these statements and also be written using the other. So they are equivalent. But one statement is faster than the other under some condition. For example, if you only have two conditions, let's say you have an integer value of n and use the same example we've used earlier. If count is less than 21, print two, and now we've got one. For example, we can say that if a company is equal to ten and, and otherwise print two, and we'd also get one. So we can write the exact code using switch statement. So let's go ahead and use the switch, give it a value of count. And this is the system that allowed one rate and the default case to print to. And so this switch case is the same as the statement, but we can see that to give us more time to write. So in this particular example, the if-else statement is easier to write that switch case. However, if we have many cases, for example, let's say we need to determine a grade and comments accordingly. So suppose we have this. It's equal to scan. And the next, and that the first letter to store it in. Now, for example, set the condition f. This character is equal to a. Friend. Print one. L is equal to B to F. Same thing here. B, C, D, and E 2345. Now if we go ahead and run the code, let's suppose we entered CY will get three. Let's use the switch case statement to write the same exact code and see that it looks so much better. So if you switch that into a, print out one, and the same thing. And a, B, C, D, and E. And we have great copy and paste. And so now we go ahead and run the code. You get three. But now the smart ys here, it is easier to read since I can clearly see each case and see what will happen in each and every case. At the end of the day, you can use a misstatement, but sometimes the obliged to use one statement over the other. For example, when we had an attitude count equal to ten, and we said that if count is less than 20 prints something and otherwise, then another thing. So now we'll not use which case because we not go and study each and every case when k 0123 till 20210 to infinity. So this is not practical. We'll simply use f statement in this case. And this is how we can choose between the two statements. See you in the next video. 34. Project: Now this is a sample of our project. So our project is to create a word counter program. This program should create data from a file. So we have our file, we have some random graphs. And then we should ask the user to enter a word and compare this word with every word in the paragraphs from the file and count how many times this word has occurred in this paragraph. So for example, here we asking enter a word, let's say hello. We have 0. For example, Tie Gan, Yes. We have two hours. Yes, For example, a 19. And if by mistake retyped Q, invalid input asks the user to try again you until one of the two choices, yes or no. Yes. We're going to try again, always. One node. This is the program should print the end. Now, one thing we can improve as to generate an exception if no word matches the one entered by the user. For example, we can create the exception and use it if the counter state 0 after the loop is executed, otherwise, we print the counter. So in this case, as for example, type boolean, instead of printing 0, we should generate an exception and then ask the user if you want to try again. And in case of a number, will simply print the number alone. This is it. This is simple project in which you'll use most of what you have learned. So good luck. 35. Recap : Now let's have a quick recap of what we covered earlier. So first of all, we talked about classes and objects, classes such as random Math, String class. And we learned how to create objects and use them and use their methods. Then we talked about decision-making. We introduced boolean expressions, if else and switch statements, as well as break and continue statements. Then who worked with loops for and while loops? After that, we defined array lists and arrays and worked with them and use their methods. And lastly, we talked about exception, and we learned how to handle the exception using try and catch block, and how to create our own exception. After that, we did some applications on ArrayLists, arrays and when to choose f, fs switch case statements. And then we did our project. So this is it. Hope you enjoyed this class. See you the next one.