Java for Everybody: Basic Programming concepts Part 2! | Hadi Youness | Skillshare

Playback Speed


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

Java for Everybody: Basic Programming concepts Part 2!

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

44 Lessons (6h 38m)
    • 1. Introduction

      0:52
    • 2. Material

      1:02
    • 3. Inheritance

      9:30
    • 4. Super Reference

      5:58
    • 5. Overriding Methods

      5:27
    • 6. Class Hierarchies

      4:22
    • 7. Visibility

      9:27
    • 8. Polymorphism

      5:14
    • 9. Polymorphism Example part 1

      8:43
    • 10. Polymorphism Example part 2

      10:52
    • 11. Polymorphism Example part 3

      10:43
    • 12. Recursion

      8:38
    • 13. Example: Sum using Recursion

      6:54
    • 14. Recursion: Maze part 1

      9:52
    • 15. Recursion: Maze part 2

      14:00
    • 16. Recursion: Towers Of Hanoi

      12:14
    • 17. Insertion Sort

      10:50
    • 18. Selection Sort

      9:38
    • 19. Bubble Sort

      5:52
    • 20. Merge Sort

      13:57
    • 21. Quick Sort

      11:21
    • 22. Linear and Binary Search

      10:20
    • 23. Jump Search

      10:09
    • 24. Interpolation Search

      6:54
    • 25. Exponential Search

      7:39
    • 26. Extra Problem: Inheritance

      13:54
    • 27. Fibonacci Sequence

      10:13
    • 28. Big Integer Class

      5:01
    • 29. Extra problems: Recursion

      12:45
    • 30. Sorting Array of Strings

      6:02
    • 31. XML

      8:13
    • 32. Process Local XML

      14:00
    • 33. Process Local XML 2

      8:08
    • 34. Process XML from browser

      13:58
    • 35. Process XML from browser 2

      4:10
    • 36. Catalogue Example

      14:59
    • 37. Catalogue Example 2

      6:23
    • 38. Company Example

      14:55
    • 39. JSON

      14:58
    • 40. Wordpress Example

      10:42
    • 41. Glossary Example

      11:52
    • 42. Quiz Example

      12:21
    • 43. Project

      4:17
    • 44. Recap

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

Community Generated

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

86

Students

2

Projects

About This Class

This class covers some of the most common algorithms and concepts. Some material is already mentioned in the first class, however, in this class we aim to explain it further and dive deeper into each topic. 

we will cover inheritance and polymorphism, recursion, searching and sorting algorithms, and finally solve some extra problems to learn how and when to use each concept, method and algorithm. 

In addition to that, we are going to learn how to retrieve data from XML and JSON and how to work with them in our program. 

It is recommended to start with the previous two classes to be able to do well in this class. 

I hope you enjoy it!

Meet Your Teacher

Teacher Profile Image

Hadi Youness

Computer Engineer

Teacher

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

Class Ratings

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

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

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.

Transcripts

1. Introduction : Coding can be very challenging and yet extremely enjoyable At the same time. Hello everyone. I'm Hadi units and this is Programming for Everybody. Now, if you know nothing about programming, I think you should start with the two previous classes. And the first one we sold Java and Eclipse and learn about some basic concepts such as datatypes, decision-making, loops, and much more, but all in a general way. And in the second class with a deeper to these concepts from creating classes and objects, do using arrays and ArrayLists. Now, in this class, we'll explore some old and new concepts. Some of them are already mentioned in the first class that here we get to the bottom of them. With that being said, you can start and I hope you enjoy it. 2. Material : As usual, we'll start with our material. So what are we going to cover? First of all, we'll talk about inheritance, and then we'll introduce polymorphism and how to use it via inheritance. And then we'll introduce a new concept in programming, recursion and how to use it. After that will move out to sorting algorithms. So if we had, if we have an array or an array list of integers, strings, characters, we'll learn how to solve them. And then we'll talk about searching algorithms. So if we have a specific integer, for example, how to find it in a specific array. And lastly, we'll talk about Big Integer class. After that will solve some problems on each topic as usual. And lastly, we have our project. So this is it. And let's start. 3. Inheritance: First of all, we start with inheritance, fundamental technique for organizing and creating classes. It is a simple but powerful idea that implements is the way we design object oriented software. You can say that a class is to an object what a blueprint is to house. Many houses can be created from the same blueprint. They are essentially the same house in different locations with different people living in them. Now, suppose you want to house that is similar to another, but with some different or additional features. You want to start with the same basic blueprint, but modify it to suit new, slightly different needs. Many housing developments are creating this way. The houses in the development Have the same core layout, but they have unique features. For instance, they might all be split-level homes with the same basic groom configuration, but some have a fireplace or full basement, while others do not. Now, via inheritance, the new class automatically contains the variables and methods in the original class, and then they leave the class as needed. The programmer can add new variables and methods to the derived class or modify the inherited ones. The original class that is used to derive a new one is called the band class, superclass or base class. So here is the parent of Pictionary and the glass. The derived class is called a child class or subclass. And Java uses the reserved word extends to indicate that the new class is being derived from an existing class. Now, the process of inheritance should establish an is a relationship between two classes. So we have an is a relationship between book and dictionary. That is, the child class should be a more specific version of the parent class. For example, dictionary is a book, but not all books are dictionaries. Now, if such statement doesn't make sense, then this relationship is probably not an appropriate use of inheritance. So now lets demonstrate the use of an inherited method. For example, we have words, we have here our main method. And dictionary will have three methods. Computer issue, set definitions, get definitions, and a variable integer. And lastly, our parent class, the book. We have a variable n and two methods set pages and get peaches. Let's go ahead and create our three classes. We have Cook and V8 dictionary. And lastly, word. Glasses. So now let us start with book. So this is the parent class of derived class to use it to demonstrate inheritance. So we have vegetable and integers, let's say is protected and then will explain it later. And you pages equal to 1500 pages. And we have two methods. First of all, set page, which is pagers, give it a value of integer as a parameter, num, and that the pages equal to this value. So now instead the beaches and yet beaches, to return the value of pages. We just return pages. Now, what is the protected modified? As we've seen, the visibility modifiers are used to control access to the members of a class. This effect extends to the process of inheritance as well. So private methods and variables of the parent class cannot be referenced in the child class or through an object of the child class. And if we declare a variable with public visibility for that so that the derived class can reference it. We violate the principle of encapsulation. Therefore, Java provides a third visibility modifiers protected. So we're using protected. We can have access to this variable and our child class, dictionary. Now, it's good to dictionary. And here we have our dictionary class. So this class, dictionary, which is a book, they're derived from the book class. So let's save this classmen dictionary. We have three methods and a variable. Let's use private. Modify this definition to the equal to 2005500 for example. But we have 52,500 definitions. Now, let's create our first method to compute the JHU issue. Now, as we said, we can use and the book, because we said them as protected. And compute the ratios, we simply return a W definitions divided by the issue. I'm sorry, over how many pages, ages and issue we extended from work. And now we have both rest method. Now, method is set definitions. So as usual, nations with numb. And we said that clinicians to be equal to nine. And lastly, we have the get definitions to return how many definitions there is an integer def definition without and simply return definitions. So now these are our methods and let's go ahead and use them in our world-class. So he outward class. So this is our main method and our object dictionary in emitted W. Now let's work with it. So we have some method. Let's print the number of pages. So number of pages. And you're going to use get beaches. We have yet to finishing skip pages. And if we go ahead and run this code, get number of pages, one hundred and five hundred. And let's also print the number of different definitions. We'd say definitions and definitions. So we have 52,500 definitions. And lastly, let's compute them by simply definitions. So no how many definitions that is in the page and use W.com and print it out. We get definitions. Thirty-five definition. Now, remember we also have set Pages and set definition. So for example, if we want to change the pages, simply use W dot set. H is for example, ten and nations for quality. And if we print the Definitions, page one more time, another number which is equal to four. So this is it for this video. See you in the next one. 4. Super Reference : Now we also have a reference that we should be familiar with. This, the super reference. So the reserved word Super can be used in a class to refer to its parent class. So we can have access to a parent's member using the super reference. Now, the word super refers to depends on the class in which it is used. To. Now, lets demonstrate the use of superior reference will stay. With these examples. We have our world-class routes, we have our main method, and we have the dictionary, and we extended book. And lastly we have our class. Now, suppose we have in our book a constructor. So that set a constructor here, saying that public sector have the same name as glass. And let's give it a parameter of integers, which is number of bridges. And inside this constructor, reset the pages to be equal to the beaches given by the constructor. And now this is our constructor into book class. Now let's create another constructor into dictionary class. So we simply created outlet dictionary and we'll give it the value two pages and definitions. Let's change this to the So let's first of all the definitions equal to definition. And and so now I can invoke the super, the parent's constructor using the super reference. All right, super and give it the value of religious. And now we use the super. So the Dictionary constructor takes two integer values as parameters. We have pages and definitions representing a number of pages and a number of definitions in the book. So because the book class already has a constructor that performs the work to set up the path of the dictionary that were inherited. We rely on deck constructor to do that work. So instead of saying that pages equal to the, this basis, we simply rely on this constructor of the class, saying super. So it will go to the constructor into book class and said pages to be equal to this pages. Now, a charged constructor is responsible for calling its parent's constructor. Generally, the first-line of constructor should use the super reference call to the constructor of the pair. And if no such code exists, Java will automatically make a call to super At the beginning of the constructor. So this rule ensures that apparently initializes its variables before the child class constructor begins to execute. Using the super reference to invoke advance constructor can be done only the child constructor. And if included, it must be the first line of the constructor. So the super reference can also be used to reference other variables and methods defined in the parent class. And we use this technique data. Moving on to multiple inheritance, Java's approach to inheritance is called single inheritance. This term means that the derived class can have only one parent. Some object oriented languages allow a child class to have multiple bands. This approach is called multiple inheritance and is occasionally useful for describing objects that are in-between two categories of classes. For example, suppose we had a class and the class truck. And we wanted to create a new class called pickup truck. A pickup truck is somewhat like a car and somewhat like a truck. With single inheritance, we must decide whether it is a better, whether it is better to derive the new classroom guy by truck with multiple inheritance, it can be derived from both, as shown in this figure. Multiple inheritance works well in some situations, but it comes with a price when it, both truck and car have methods with the same name. Which method? Pickup truck inherit? The answer to this question is complex, and it depends on the rules of the language that supports multiple inheritance. The designers of the Java language explicitly decided not to support multiple inheritance. Instead, we can rely on inheritance and interfaces to provide the best features of multiple inheritance without the added complexity. Although a Java class can be derived from only one parent class, can implement multiple interfaces. Therefore, we can interact with a particular class in the specific ways with inheritance and interfaces, while inheriting the core information from one parent class and using interfaces. And we'll talk about interfaces later. So this is it for the super and multiple inheritance. And the next video, we'll learn how a child class can override the parents definition of an inherited method. 5. Overriding Methods : Moving on to overriding methods. When a child class defines a method with the same name and signature as a method in the parent class, we say that the child's virgin overrides the parent's version in favor of its own. The need for overriding occurs often in inheritance situations. So let's go ahead and demonstrate the use of an overridden method. Creates a new package, name it over. Right? And, and this package will create three classes. The first one we'll name it message. The second one will be at twice. And lastly, we'll create a class called thought. So we have three classes. And the message. Here we have our main method and we'll work it later on. But for now, we will connect vice and thought. So at the third is depend class and the advice class is the child extend. And let's create the same method. The same method and the two classes. So in thought, would create a public voice message with no parameter, simply print. This is the message from the parent. And the advice class would have the same method. Public, public message. With no parameter, it simply prints. This is a message from the class. And we'll use the super, super.message to explicitly invoke the parents virgin. Now, let's go ahead and use you have here an error undefined for the time. You need to save this and everything properly. And now let's go to our message. Let's use two objects. One for advice, you have advise, a new advice, and the same thing for the last name and the new. Now, we'll use the same method in the two pluses. So we say a.me and the message that's swap them, use first of all, that key and then print a line. So let's see what will happen. So we printed out that this is a message from the parent class and then aligned. So this is our output from D, that message. And then this is the output from a, that message. In a message will get, this is a message from the child class. And then since we used here, the Super, we invoked what? The message in the third class we have here. This is a message from the parent class, so we'll print out a message from the parent class. Now, a method can be defined with the final modifier. And then jet child Cass cannot override a final method. This technique is used to ensure that the derived class uses a particular definition of a method. And now we'll talk about shadowing variables. Although not recommended for inside class, to declare a variable with the same name as one that is inherited from the parent. Now, there is a difference between a declaring a variable and simply giving an inherited by a particular value. For example, if we have an integer variable here in the third class, we can give it a value in the advice class, but it is not recommended to redeclare it as, for example, a string or as a double. Now, this declaration creates confusion, and this confusion causes problems and serves no useful purpose. So array declaration of a particular variable name could change its style, but that is, and usually unnecessary and general shadowing variables should be avoided. So this is it for shadowing variables and overriding methods. See you in the next video. 6. Class Hierarchies: Now we'll talk about class hierarchies. Child cast derived from one parent can be the parent of its own childcare. So multiple classes can be derived from a single parent. Therefore, inheritance relationships often develop into class hierarchies. So here, this diagram shows us a class hierarchy that includes the inheritance relationship between mammal and host class, for example, bird and parrot, animal and birth. There is no limit to the number of children a class can have, or the number of levels to which the class hierarchy can extend to children the same parent are called siblings. So here, horse and bad are siblings. Although sibling shed the characteristics passed on by the common parent, they are not related by inheritance because one is not used to derive the other. In S hierarchies, features should be kept as high in the hierarchy as reasonably possible. That way, the only characteristic explicitly establish child cast are those that make the class distinct from its parent and from its siblings. So for example, if we have method in animal, let's say it. So all animals eat. So we'll keep it in the animal class. However, not all animals fly. So this method fly, we can created and the bird class. So this way also facilitates maintenance activities because when changes are made permanent, they are automatically reflected in the descendants. Always remember to maintain the is a relationship when building class hierarchies is no single best hierarchy organization for all situations. The decisions you make when you are designing a class hierarchy, restrict and guide more detailed design decisions and implementation options. So you must take them very carefully. Moving on to the object class in Java, all classes are derived ultimately from the object class of a class. Definition doesn't use the extends clause to derive itself explicitly from another class. And that class is automatically derived from the object class by default. So when we say in, for example, let's create a class. And let's name it. Thing will have a class called thing. This representation is equivalent to saying extends object. Because all classes are derived some object on public methods of an object are inherited by API Java class. They can be invoked through any object created and any Java program. The Object class is defined in the java.lang package of the Java's standard class library. So we have some methods in this object class, as it turned out, been using object methods while often in our examples, the toString method, for example, is defined in the object class. So the staining method can be called on any object. And this is why when we define a toString method and a class, we are actually overriding an inherited definition. The definition for toString that is provided by the object class. We also have another method that we view several times before. That equals it returns true if the object is an alias of the specified object. Classes often override the inherited definition of the.equals method in favor of a more appropriate definition. For instance, the String class overrides equals, so that it returns true only if both strings contain the same characters in the same order. So this is it for class hierarchies. See you in the next video. 7. Visibility: As we discussed earlier, all variables and methods, even private members that are defined in a parent class are inherited by the child clubs. They exist for an object of the derived class, even though they can't be referenced directly, they can be referenced indirectly. So let's look at an example that demonstrates this situation. Let's go ahead and create a new package, and let's name it. Visibility. And inside this package will have three classes. As usual. Let's create a class called food analyzer. And here we have our main method. And another class. Let's name it. For example, food item, Alaska's example. We'll name it beta. So now we have three classes. Put analyzer, food item and pizza. So the foot item is dependent glass and pizza extends. Foot is create some methods. So NPDES will create, for example, let's create a constructor. Simply public pizza, give it a value of GM and simply called the super method and put item that could create data. So this, for example, this super We'll take two parameters, the g that we get from the pizza method. And for example, let's say eight. And now we'll create the method in the food item so we understand it better. Now, here in the food item is create three integers. As create private galleries. 30 grams. Let's get it to nine. And another private. But grams. And lastly, create a protected. Now let's create our constructed and it will take two parameters. Let's name it food item. And you take two parameters. One number of petagrams, grams, and the second one is number of serving, serving. So now when we called the Super here, we actually called the constructor. And the food item class, it will take two parameters, so we have two parameters. Now. Let's go ahead and use it. We have fat grams and so banks. So just assigned fat grams equal to number of grams we already got from the user. And the same thing for the servings will number of servings. Now let's create some methods. For example, to return the calories, fat grams, multiplied by the calories per plant. And another method to compute the calories per serving, simply turn names, galleries, servings, and we simply calories and divided by the Serving. So we'll return galleries to to turn. Terrorists will have the method calories. We simply call it using batteries and divide it by the number of servings we have the number of servings in serving. And this is our calories per serving method. Now, let's go ahead and use this method and our main class. So we'll create a pizza. Pizza. Let's name it special pizza with a value of 275. And let's print out the calories per serving. Calories. That is per serving. And we'll get special calories per serving and run the code will get calories per serving, 309. Now, the pizza class is derived from the food item class, but it adds no special functionality or data. That's constructor calls the constructor of food item using the super reference, asserting that they are eight servings of pizza. The pizza object here called special, is in the main method and used to invoke the method calories per serving, which is defined as a public method of food item. Now, here our message should be private. And what we did here is that we could calories per serving, which is a public method. Then this calories per serving calls calories. Calories is a private method and galleries called fat grams and tellers per gram, and they are also private. So what we trying to say here that even though the pizza class cannot explicitly reference calories, calories per grams, and five grams. They are available for use in directly. And the pizza object needs cap. Bizarre object cannot be used tenfold the calories method, but it can call a method that can. So the foot item object was never created or needed. Now, since, for example, calories per gram is, we're absolutely sure that it is equal to nine. We can simply add here private, final and the chair. So what we're saying here is that this variable is finite and we cannot change it. Now, the other uses of the final modifier involved inheritance and can have significant influence and software design, especially the final modifier, can be used, curtail the ability is related to inheritance. So earlier we mentioned that the method can be declared as final, which means it cannot be overridden in any classes that extend that the one it is. The final method is often used to insist that the particular functionality be used in all tried classes. Now, the final modifier can also be applied to an entire class. Final class cannot be extended at all. So we can simply say that public class item. And now we'll have addressee saying that the type pizza cannot subclass the final class put item since it is finite and you cannot extend. So giving this declaration, this class cannot be used in the extends clause of an asset class. The compiler will generate an error message in such a case. Using the final modifier to restrict inheritance abilities is a key design decision and should be done in situations in which a child class could possibly be used to change functionality that you, as a designer, the civically wanted to be handled in a certain way. This issue comes up again and the discussion of polymorphism, and we'll discuss it later. So see you in the next video. 8. Polymorphism: Now we discuss polymorphism and other fundamental principle of object oriented software. Often the type of a reference variable matches the class of the object to which prefers Exactly. So when we're saying, for example, one equals new string. And say, now, what we did here is the type of reference variable is the same as the class object, which is now the term polymorphism then be defined as having many forms. A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. The specific method invoked through a polymorphic reference can change from one invocation to the next. So suppose we have a method called duet. And if there's a reference object here is polymorphic, it can refer to different types of objects at different times. So if that line could discord, runs and then loop, or it is a method that is called more than once. That line of code could call a different version of the duet method. Each time it is invoked, we can create a polymorphic reference in Java in two ways, using inheritance and using interfaces. Let's look at each in turn, will start with Vodafone polymorphism via inheritance. So when we declare a reference variable using a particular class name can be used to refer to any object of that class. In addition, can also refer to any object of any class that is related. And it's declared type by inheritance. So for example, let's try this here we have our main method is create some classes here. For example, creating a class called mammal, another class creature. And the last class hosts. So we have three classes. The parent class, guess the creature, and it has a child class system Emile, it should extend. And this is the main mail, it should extend the creature. And and lastly, we have our course, which is a child class of domain, and it should extend it aswell. So now we have three glasses learned. Together with inheritance. Now, the main reference can be used to refer to any object of the class holds. So for example, if we say, let's name it H. So this is our object horse. And for example, we, for example, we can say equal to edge and to work properly. So this relationship works throughout the class hierarchy. So if the mammal class were derived from a cascode creature here, we can also say that Ranger, For example, let's limit c equal to nu. And that will work properly. And you can also sign into codes. Since Horus is the child of maybe. Now the reference variable SC can be polymorphic because at any point in time it can refer to an Animal object, creature to enable to the host. So suppose that all three of these classes have a method called move that is implemented in different ways and when this line is executed. So if creature currently refers to a creature object, if c refers to a creature object, the move method of the creature class is invoked. Likewise, if c guaranteed refers to a mammal object, the mainland version of MOOC is invoked. And finally, if C is currently country referring to a horse object, the horse, that the method in horse class will be invoked. So this is a general idea about polymorphism. And the next video, we'll do an example to understand it better. 9. Polymorphism Example part 1: Now let's take a look at another situation. So consider this class hierarchy. The classes in this hierarchy represent various types of employees that might be employed at a particular company. So let's explore an example that uses this hierarchy to pay a set of employees of various types. So first of all, we have the firm class contains in Maine, diverted creates a staff of employees and invokes the payday method to pay them call. The program. Output includes information about each employee and how much each is bad. If anything. This firm, we have the staff. It maintains an array of objects that represent individual employees of various kinds. So the area is declared to have to hold staff member references, but is actually filled with objects created from several other classes such as the employee volunteer, executive. And these classes are all descendants of the staff member class. And so the assignment are valid. The established array is filled with polymorphic references. So here in this staff member, we can access volunteer employee executive, undoubtedly. Polymorphic way. Now, each class has its specific characteristics. So all workers need to have name, address, and phone. But if a person is opponent td, it doesn't have to have a social security number or a pay rate. And this is where the volunteer is different from employee. And the same thing for executive and hourly. So executive have, has a bonus and hourly has worked out. So let's go ahead and create our classes. To start with. Let's create our package. So let's finish with an example, example polymorphism, and we'll create our class. We have here our main method. Let's go and create another class. Create the staff member would name it stuff. So we have our second class. Then. We also have let's go and see what. We have our staff, we have staff members. Create staff member class. Then we have volunteered employee Scott, this is the potentate in class. And then we also have executors, and these are the last two classes, executive. And lastly. So now that we have created all our classes, let's assign them together. So we have the staff member, volunteer, and employee that extend the staff member. So volunteered right here extends staff members. And the same thing with employee staff member. And as we can see in this class hierarchy, we have executive and child classes of employee. It's gonna go here and extend them from. And the same thing with extends. And let's start with staff member. As we can see here, we have a staff member, three variables, name, address, and phone, and to method's toString. And let's go ahead and create them. And ourselves. Give them a protected reference name, address. And we'll have our three variables. And now we can create our constructor to setup the staff member using the specified inflammation. So we have these three information to create. Our constructed these information as usual. And the three strings in the address bar. And simply assign them to look at variables. And for n equal to p. So this is our constructor. Now let's create our two methods. We have toString, toString, and it will print the name, address, and phone. Go ahead and create this. I called three, which we have name plus, plus. Let's create a new line by simply you line by simply typing n. So this is our Name. And on the second line, we would address. Address. And the address. The last line, phone number. So this is the phone number. And now we need to add a new line. So this is our toString method, and finally we return the result. Now, the last method we have in this class is the Dublin bay methods. Methods doubled. Every employee has a different payment method, will let the derived classes to define a method for each type of employee. So this method will not have any body. So this is why it will be abstract. And we should also assign the class to be abstract also. Since we have an abstract method. So this is our staff member class, and we continue with the other classes into next Purdue. 10. Polymorphism Example part 2: Now, since we're done with our staff member class, will move to our volunteer. So we have an adder here saying that the volunteer must implement the Inherited abstract method. Staff member. Now will inherited. But before, let's go to our class hierarchy and see we have an volunteer didn't just have a double B method. So let's go back and create our constructor. We have constructor volunteer, twill take as usual, three strengths. Spank and a address has to be performed. And our super constructor and give these three values. So now created our constructor. Let's create our method, which is B. And as a volunteer will not pay anything. We just returned 0. So this is our volunteer class. Now let's move on to our employee class represents a general paid employee. In this class. We have our constructor as usual, but let's go back to our class hierarchy. We can see that we have two new variables, social security number and bade aid. We have two methods to string and pay. So first of all, let's create our two variables. Protected string. We have the social security number, and another protected with a double type eight. Now again, create unconstructed public. And the parameter now we have five variables instead of three. We have the name, address, and phone. And we'll add social security number and period. Let's go ahead and into these five investing and for name, for the address, the phone, and ask for Social Security number. And lastly, a double standard played. And what we're going to do is to assign the three variables as usual to the super, using the super reference to the constructor class. And now we're done with the name, address, and phone. They are automatically. They went to the staff member and they are assigned here. And now would work with our social security number. Social security number, we'll give it a value of S. And the rate we have baited with a value of rate. Now, let's work with our methods. We have two methods that toString that we need to modify and the rate that debate. First of all, let's go and modify the two strands. Who's tank method? And if we use super D2 strain will get the toString method, what we store it here. So we get name, address, and phone number. So let's go ahead and use a tiered. We have. Let's create a slang called result and use the super to call the toString method and staff member supertype. To. Now we have all the information and the staff member as numbered address and foreign in here. And we'll add to our social security number by simply saying social security number with a value of social security number. And then return the result. To. Now, we're done with this method. Let's go ahead and work with our bay method, which is public and would return a value of theta. Now, this is our employee. Lets go and work with our executive. And now let's go back to our class hierarchy and we see that executive have bonus, the w variable, bonus and two methods, our bonus and they would go to the executive and a new private double bonus. Let's create our constructed now public executive and give you some values we have as usual, the name and the address is found, then the social security number, name address. And finally, there are eight. And as usual, we'll call the super, Give it a name. Address BY phone as social and raise. The bonus here will be equal to 0. So we set bonus equal to 0. Now this is our constructor. Let's create our two methods. We have awoid bonus, public, poet bonus. If you take a parameter of a tablet, Let's name it coronas, and we'll assign bonus to this box. And the last method is pay will compute and return for an executive, which is the regular employee payment plus the onetime bonus. So we get our method. And this method, let's create a payment, which is super good. So we'll compute the pay date as usual from the employee and plus the bonus and then return. So this is our executive class. Let's go now to our class. And our class we'd have a new variable, hours worked and three methods at ours and to string. So here, first of all, we create our private variable, worked hours. And let's create our constructor as usual. And we have a, B, Jang. And finally, and we'll assign these four variables, five variables to the super AND a, B, S, and R. And give the work hours the value of 0. Here we don't have a method called add hours to add our Avoid method, public hours and hours. And it will assign wild hours. So whatever we have, invert hours, we add these hours to it. And another method is the debate for this employee. So in this method would return. And we have a double payment. And payment is paid. We have to pay rate times how many hours they simply work. So we have our work hours. And then after that, every time we pay, employee will automatically reset to 0. Then we return into payment. So this is our payment. And lastly we have our toString method is public string. And we can just take the result from the tank and then modify it by adding that current hours. How many hours damper currently work. So we add a new line. And then we'll add, let's say current hours and hours, worked hours. Then return the results. We finished with our parent and children classes, staff member, volunteer, executive, and avidly, the next video we'll talk about staff and for classes. 11. Polymorphism Example part 3: Now we still have two classes, firm class and the staff class. Let's work with the staff class. Inside this class will create a list of some staff members. So private staff member, as Adam and his name is, stuff, gets create a constructor to set up a telescope staff members by simply with no parameters and will give this stuff list value. It is the new stuff. So we have eight elements in this list and and have some elements. Stuff list, position 0, executive for example. It, the string name. And the address will be 1-2-3. Mainline phone number, for example, let's say minus 0.51111. And this Social Security number, it is, for example, the 123456789. And lastly, the rate is, for example, let's say 2423.07.07. The element at position one, employee and employee. And as usual, for example, as usual, let's say for example, 0.01. Social Security number 12345451234. And lastly, iterate, say one hundred and two hundred forty six. 0.15. And we do the same with the others just to not to copy them. And so this is the second 17567. And let's change Haiti for example. Let's say, let's keep it as an employee at the second one. And then the one after it gets broken as a teacher. And the other one has enough, so adjust the and make it size six. Now, let's modify this name, this employee. And by 55 to phone number and 99. And lastly iterate for example, 3,246. And the same here. And we have the 999. Same thing heated. Let's change the numbers and the rate as usual. For example, let's set it to 1010. And lastly, we have to vote and t is known. And it was just the name, address, and phone number without these two additional. So let's go ahead and delete them. And this is our first one, and this is our second volunteers. Just change here. P5. And yeah, this 123 main line. And he is. So these are, these are our members and this list. Now we still have two methods. One and executive, a word poisonous and gathered and ours. And let's use them. And our staff members, our staff member. Now, we need to work with executive. So we simply write executive. And to say to the program that we need an executive staff member. We're specifying what the staff list when you start to put it in parenthesis and upward bonus, and that's 500s for example. And the same thing. And we will have staff listed three. And now we have the staff, the payday method to pay it or the members. So after creating the constructor will create a public void payday. And we have an amount to pay, so we'll save it and it doubled the amount. And first of all, let's print every staff list. Every member in the staff list by printing. And then if the amount is equal to 0, then it is surely a volunteer. Then we'll just print tanks. Otherwise, would print the amount that we're going to pay. And the amount is equal to every established. With the method gives the amount is equal to 0. Then just thanks. It is a volunteer just thanking him. Otherwise, when I'm out. Plus the amount. Now we have our method in the staff. Let's go ahead and use it in our firm. So he recreate the staff object. S is the new stuff and use a method. And let's run the code, see what will happen. So here we have first of all, our first. Let's go back and print the line between F3, less every member in the list and this list, just to, to be more clearer. So after printing every, every amount, print a new line. And now inside the photo and run the code one more time, we get the same output, but with a line to divide every member in this list. So first of all, we have some address, phone number and the amount, and then we have the same thing. And lastly, we have to volunteers. And known and Karla, since they are volunteer, will not print the amount because they are not getting paid. So we just print. Thanks. So I think that this example demonstrates the use of polymorphism we have established. And every time staff member, every time it will print different things since every time to work differently. So this is it for polymorphism. See you in the next video. 12. Recursion: We've talked now about recursion. Recursion is a powerful programming technique that provides an elegant solutions to certain problems. We've seen many times in previous examples that one method can call another method to accomplish a goal. What we haven't seen yet, however, is that a method can call itself. Recursion is a programming technique in which a method calls itself in order to fulfill its purpose. But before we get into the details of how we use recursion in a program, we need to explore the general concept of recursion. The ability to think recursively is essential to being able to use recursion as a programming technique. In general, recursion is the process of defining something in terms of itself. For example, let's suppose we wanted to formally define a list of one or more numbers separated by commas. Such a list can be defined recursively as either a number or as a number, followed by a comma, followed by a list. So we can say that we want a number followed by a comma. And then so these are two examples. We have a number and then a comma, and then a list. No matter how long the list is, the recursive definition describes it. A list of one element, such as this example, is defined completely by the first non-recursive part of the definition for any list longer than one element that it goes if part of the definition is used as many times as necessary until the last element is reached. What we're saying here is that we have a number and then followed by a comma, and then we have a list. And then the same thing goes for 88 is a number, followed by a comma, then followed by that list. And 40 is the number comma, and then a list of one element. And finally, we have a number. Now, the definition of list contains one option that is a cursive and one option that is not. The part of the definition that is not recursive is called the base case. If all options had the recursive component, that recursion would never end. For example, if the definition of lists were simply a numbered followed by a comma, followed by list. No list could ever end. This problem is called infinite recursion, dissimilar to Glenn infinite loop, except that the loop occurs in the definition itself. So now let's go ahead and talk about recursion in mathematics. For example, let's suppose the n factorial. So let's say and factorial. So the value referred to as n factorial is defined for any positive integer n as the product of all integers between one and n inclusive. So if we say n factorial is equal to three times two times. One is six. Now, the same thing if we say five factorial is five times four times three times two, all the way to one. Mathematical formulas are often expressed recursively. And definition of N factorial can be extra expressed as. And factorial is equal to n times n minus one. Here we have factorial four and bigger. So as long as n is greater than one, n factorial is equal to n times n minus one factorial. And the same thing goes for this. And so on. Now, the base case of this definition is one factorial. One factorial is equal to one. All other values of n factorial defined recursively as N times the value n minus one factorial. So using this definition, when we saying 50 factorial, it is equal to 50 times 49 factorial. And 49 factorial is equal to 49 times 48 factorial and so on. And this process continues until we get to the base case of one. Because n factorial is defined on the positive integers, this definition is complete and will always conclude with the base case. Now, let's work with bacteria and programming. First of all, we should create a matter method to return an integer. Let's name it factorial and the twill take an integer as payment. And now, as we said, the base case is when n is equal to one. So we'll say if m is equal to one, we'll just return one. Otherwise, if n is not equal to one, we just returned and multiplied by the factorial of n minus one. Now, to perform this operation, we just call the method one more time with parameter of n minus1. Now let's go ahead and use it. For example, let's say that n is equal to four. And this method and, and run the code, we get 24. So what happened here is we, we performed factorial with a parameter of four. So it goes to this method. We have for it is not equal to one. So we have four times the factorial n minus one, which is three. So four times factorial three. So we have four. Strike here, four times something. Now factorial of three, it goes to the method one more time. We have three times factorial two. So we have three times something. And the same thing, one more time, two times factorial 12 times factorial one. So the last time we entered the method, we have n is equal to one, so just return one. And this is it, it is four times three times two times one. So we'll print this four times 312 times 224, and we'll get 24. Now, we might face something. For example, if we enter a negative value, let's say minus seven, we'll get an error. Because we didn't deal with this. We just have the positive number and multiply it by the number below it. And we have the base case, which is when n is equal to one can fix it by saying while n is less than or equal to one, return one, so we get one. And if we don't want to get one, for example, if we want to get minus1, we can say that if n is less than equal to one, return minus1. And, and then we move this from here and run the code, we'll get minus one. So this is a small example and recursion. And in the next videos, we'll explore it more. 13. Example: Sum using Recursion: Let's now use another simple mathematical operation to demonstrate the concept of recursive programming. Consider the process of summing the values between one and n inclusive, where n is any positive integer. The sum of the values from one to n can be expressed as and plus the sum of the values from one to n minus one. So we can say that sum of n is equal to n plus and minus one. So let us keep this here as comment. And let's start with our method. Let's name it. Some public static returns an integer with the value and, and our base case being deaf and is equal to one. Return one otherwise would be turned the number and plus sum of n minus1. So as we said here, we should return some of n is equal to n. This is n plus the sum of n minus one. And this is our method and let's use it here. Print out some of averages. In this case four. We get four plus three plus two plus one equal to ten. Now, the base case in the summation example is when n is equal to one, at which point no further recursive calls are made. The recursion begins to fall back into the earlier versions of the sum method, returning the appropriate value each time. Each return value contributes to the computation of the Sun at the higher level. Without a base case, infinite recursion would result. The sum function different initial values of non end and until the processing becomes familiar. So in this figure illustrates the recursive calls when the main method invokes some to determine the sum of the integers from one to four. Each box represents a copy of the method as it is in. Invocations are shown as solid lines here and returns as dotted lines. The return value result in is shown at each step. The recursive path is followed completely until the base case is when sum is equal to number here, values equal to one. And after that, the base is now reached. So the cause then began to return the result up through the chain. So when n is equal to one, we begin to return their results. First of all, we return one. Then we have two plus 133 plus three is equal to six, will return 66 plus 410 would return ten. Now, of course, there's a non-recursive solution to the summation problem would just explored. One way to compute the sum of the numbers between one and a number and inclusive in iterative manner is, for example, we'll use the for loop for one to n inclusive, so equal to n and sum plus equal to. And first of all, we have an integer, sum equal to 0. And then we print out some plus sum, and here we need to add. So sum is equal to 0 plus one plus two plus three, plus five plus four. And when i is equal to four, the exit, the for loop and print sum. And here I added a print line here. Now, if we run the code, we'll get some is ten. Now, of course here we can say that i is equal to one. And we get the same thing. Or can set r is equal to one and start with i equal to two. And of course we get the same result. Of course, the solution is certainly more straightforward than the recursive version. We use the summation problem to demonstrate recursion because it is simple, not because you would use recursion to solve it under normal conditions. Recursion has the overhead of multiple method invocations and in this case presents a more complicated solution. Then it's iterative counterpart. The programmer must learn when to use recursion and when not to use it. Determining which approach is best depends on the problem being solved. All problems can be solved in an iterative manner. But in some cases, the iterative version is much more complicated for some problems and allows us to create relatively short, elegant programs. Finally, we'll talk about direct and indirect recursion. Dialectic corrosion occurs when a method invokes itself, such as when some goals, some indirect recursion occurs when a method invokes another method, eventually resulting in the original method being invoked again. For example, the method m1 invokes a method M2 and M2 invokes method m1. We can say that m1 is indirectly recursive. The amount of indirection could be several levels deep. As when m1 in books, m2, which invokes M3, which invokes M4. And finally, this method and for invokes m1. And direct recursion requires all of the same attention to have cases that direct recursion requires. Furthermore, in direct recursion can be more difficult to taste because as the intervening method calls, therefore, extract is one ended when designing or evaluating in directly recursive methods. And showed that the indirection is truly necessary and clearly explained as you want. 14. Recursion: Maze part 1: After using recursion for some simple programs, now use it with more complex ones. Let's go ahead and create a new package. Name it programs. And we create two classes. Two fabulous. That means we have the first one is named maze search. Here we have our main method. And the second one, we'll name it maze. Now it's toggling amaze involves a great deal of trial and error, following a path, backtracking when you cannot go further and trying other untried options. Such activities often are handled nicely using recursion. We have our two classes. This is our driver class, may search. We have a main method here and our maze where we'll create our recursive method. So here, first of all, let's write MAs and we'll use the two-dimensional added. Will never used it before, like an array, but a two-dimensional one. So we'll use a two-dimensional array of integers to represent this maze. And the goal is to move from the top left corner to the bottom right corner and write it down and then we'll get back to you. So this is our maze. Initially, a one indicates a clear path and a 0 indicates a black path. As the maze is solved, these array elements are changed to other values to indicate attempted paths and ultimately successful path throughout the maze. So we'd have two values. One will give them a reference final, and one is, let's limit. So if we try a path, then the value will be 31 over the past Less naming path. And we'll give it a value of seven. Now, the first method we're going to create is the toString method to print this grid, this maze. And to go ahead and we'll use the public. Here we have strain. Strain and he would return. A maze domains will create as a string. So first of all, we have our strength and stamina. And this result. First of all, let's print a line and print a line here. And we just print this. So we'll create two for loops nested for loops. One for the rows and the second for the columns. So here we have the Great dot length. And the other one insight is that length. So if we say great I that length. So we'll take every row and, and we'll see the length of the stroke. So he will have 131313. So the length of the outer for loop as eight and the inner for loop is 13. And now we'll go ahead and store them in the result. We store the grid at i and j and add double quotations here. And we've done, after that, we'll just print aligned. So with that here and you line. And it's going to add these up. And then we should pretend the results. So let's go and try it out. An object means let's use the toString method and printed out the code. Look at our maze. So this is our mains. Now, as we said, one indicates a path and 0 indicates a block. But now, to know if this position is valid or not, I will create a method, let's call it pilot. Before creating our because if method. So we'll go ahead and create a new method. Let's name it. And it will return true if this position is valid and false. If not. So, take two elements. And finally, we have Boolean result. First of all, it is false. And now we check if the cell is in the bounds of the matrix. So check if rho is greater than or equal to 0. And if rho is less than or equal to that length. And of course, the same thing for column. If current is greater than or equal to 0 and column is less than or equal to rho dot length. So this is the case. Then we check now if the cell is not blocked and not previously tied. So if that grid row equal grid row and column is equal to one, then this is a pilot move, would never tried it before and it is not blocked. So if it's not equal to 0 or it's not equal to three, then we go ahead and we'll get the result. We change the results from false to true. And then after that, we returned the result, whatever that result is, true or false, would return it. Now. So this is now a method to check if this is a valid move. So what we're saying here is, for example, let's check this position. So this is at position 012 and approach 0. So first of all, this, if you want to access this position, we can write cred, row and column. It is at column 3012. I'm sorry to hear. So at position 02, we have this 012, we have this position, that 22 here, 03, 0s and so on. So this method valid, first of all, we'll take the two parameters, row and column. And first of all, it will check that the cell, it is the bounds of the matrix. So if rho is greater than 0 and lower than the length, so if it is between 08 and same here, it will check if the current is between 0 and the length 13. After that, if this is the case, then we go ahead and check the number and discrete. So we'll access, read joe Cannon in this case 12. And we'll see here we have 12012012 here. So this is 12. If it is equal to one, then we'll change the result to true. In this case, we have one, so we'll change it to true and it will return result as true. Now we have all the tools and we tend to start with our recursive method. So we'll start with it in the next video. 15. Recursion: Maze part 2: We still have our last method, which is our recursive method. And the only valid moves through the maze are in the full priority direct directions down, dried up and left. Not diagonal moves are allowed. So in this example, the maze is eight rows by three columns. Although the code is designed to handle amazed of emphasize. But for now, let's work with 813. So let's think this through recursively. The ms can be traversed successfully if we can traverse it position 00 to the position 712. So in this case at position 00, we need to work with it, so we'll check if it is a valid position. If it is valid, it is equal to one. Then we have four directions. We have 01100 minus one and minus 10, minus 10100 minus one and not valid. So we still have two valid moves, which is 0110. And we'll go with 01 for example. And we'll check if it is valid. It is valid. And, and now we'll find ourselves in the same position as we were in the first one. So we also have four directions, up, down, right, and left. And we need to check between them. So the same thing occurs every time we move in this maze. Now, let's create our method. Let's name it Avars. And this method is Boolean. Return true, pretend travels the maze and false otherwise. Public boolean. And to start with 0-0 scholar 22 parameters, intro and incarnate and would work with the body here. First of all, we need our boolean. Let's name it done. First of all, it will be equal to false. And at the end of the method, we should return a Boolean. So we'll return done. Now. We need to check the position we are in is valid. So we'll use the valid method. We created a leverage. So if valid, we'll give it two parameters, row and column. And if this is the case, we'll work here. After checking that this is a valid position. Now will change it from one to three since we tried it. So we'll change the grid row and column from one to three since we find it here. So we can say that This has been tried. Now, there's only one case where the mains will be solved and we return true. So it is when rho is equal to the length of the grid minus1. So rho equal to that length minus one, and m is equal to red row minus one. And then we'll change done from false to true. Otherwise, we need to check. So here we are saying that if we are in the position 712 in this case, since dot length minus one is eight minus 17. And quid pro dot length when S1 is 13 minus 1 12th. So if we are at position 712, then the means is solved. W1 equal to, and then we'll return done and we're done. Otherwise we need to check with not in this position yet. We need to check the down by using w1 equal to traverse down. We'll add row one. So if we are at pro 66 plus one, we know across seven row one with the same column. And the same thing is not done. Or we can say if w1 is not equal to, is equal to false. If we still not done. Same thing. We try write row, column plus one equal to rho minus one. The same thing. Try ro minus1, row and column minus one. So this is it. And now we'll explain it. But before dun, dun and dislocation is part of the finite, but then we need to change it from three or one or whatever to seven, which is equal to. So this is what we did here is we created a method called the traverse, which is a Boolean method. It returns a boolean value that indicates whether a solution was found. First, the method determines whether a move to the specified row and column is valid. So a move is considered valid, as we said before, if it stays within the boundaries and if decade contains a one in that location indicating that a move in that direction is not blocked. So the initial call to traverse passes in the upper left location, 0-0 will start with 00. And if the move is valid, that Vint entry is changed from one to three. If it is valid, then will change the grid entry from one to three. And then so these marks that delegation as visited so that later we don't retrace our steps. And this method then determines whether the maze has been completed by having reached the bottom right location. So if we have reached the bottom-right occasion, then would change done from false to true. And our maze has ended then. And otherwise we have some steps to follow. So in this method, we try out different paths. So for example, we can start by U1 and then go down one. And he were blocked by three zeros. And actually there is three, there are three possibilities of the base case for this problem that will terminate any particular because of the first one if an invalid move, because the movie is out of bounds. So for example, here, if we get to here, we have one and the left position is out of bound. So it will terminate the program. And other one is an invalid move because the moon has been tried before. So for example, if we go here 21 and then go back, then I don't know, maybe go try left and then go back to the two down. We tried it before, so it will not be valid. Lastly, a move that arrives at the final location. So after going through the maze and reaching the bottom right here, one, then we have sought the maze and it will terminate our path. Now, currentLocation is at the bottom right corner. We searched for a solution in each of the primary directions if necessary. First, we looked at me, looked down by recursively calling the traverse method and passing and the new location. So the logic of the terrorist method starts all over again. Using the new position. A solution is either ultimately found by first attempting to move down from the current location or it's not found. So for example, if we start with 00 and we have here 01, research down first, first, we have 010, excuse me. So here we are at position 1-0 would try all the logic again. And we'll check first of all down, we have nothing, we have 0. Then it is not valid. Then we'll try, right? 0, we cannot try right? Since it's not valid, then we'll try up. Now is equal to three, then it's not valid also. Then finally would try left, which is equal, which is out of bound. So this is not valid. Then we'll return false and go back to here. After that, what we'll do is to try derived. We have here in the right position. It is 01. So here we are at location 01. First of all, we'll start by looking down. It is 0 invited. So we'll go back here and look, right, we have one, it is valid. So we go to this position x. So now we are at this position, will do the same logic all over again. We start by down, we have one, we find the path. The same thing with one. We go and search down. We have 0 invalid, go with right. The same thing here. Until we reach either a blocked move. If I added one and continue until we reach the bottom right corner, step-by-step until reaching the bottom right corner. But for now, we'll go to the search and we have, this method is booleans. So we create a Boolean variable, name and maize. Two equal to M. Traverse would start with 00. So if this is equal to true, if maze, we can say equally good, leave it empty. Data at the same. And if this is a case, just print was successfully. Otherwise, we'll print there is no possible path. And finally, we'll print out the maze. Run more time. Go ahead and run that code, will get the maze was successfully traversed. And here we have our path 777 down Seventh, down until we reach here than up seven, all the way down, and then up until reaching the last dime and the bottom right corner. So this is it for the maze problem, problem. And in the next video we'll talk about Tower of Hanoi. 16. Recursion: Towers Of Hanoi: And this example will serve the Towers of Hanoi puzzle. The Towers of Hanoi puzzle was invented in the 18 eighties by French mathematician, has become a favorite among computer scientists because it's an illusion is an excellent demonstration of recursive elegance. The peasant consists of three upright bags and a set of disks with holes in the middle so that the slide onto the backs. Each disk has a different diameter. Initially, all the disks are stacked on one peg in order of size, such that the large, the largest disk is on the bottom, as shown in this figure. And the goal of this puzzle is to move all the disks from the original peg to the destination PEC. But this is the original bank. The destination pack is this one. So we can use the extra Peg as a temporary place to put disks. But you must obey the following three rules. First, we cannot place a larger disk on top of a smaller disk. For example, we cannot take this desk, place it here, and then take the other one and place it on top. Second, We can move only one disk at a time. And lastly, old desk must be on some peg except for the desk in transit between packs. So we cannot take today from the towers. These rules implies that you must move smaller disk out of the way in order to move the largest disk from 1 to another. So here we have an example step-by-step solution for the towers of Hanoi puzzle using three desk. So in order to ultimately move all three disks from the first peg to the third peg, you first have to get to the point where the smaller to this out of the way and the second bank so that the largest disk can move from the first peg to the third peg. Here we have the original configuration. So first of all, we move the first, this the smallest disk to the last Peck. And then after that we move the second one to the, to here, to the second peg. And then we'll take back this smoke that the smallest disk and put it on top of the second disc. And now we can move the last disk from here to the destination. And then we move the first disk to the first bag. Move the second disk, did the last bag, and then move the first disk to the last page. So we have seven moves. And this example, while using three. Let's use this idea to form a general strategy. So to move a stack of n disks from the original peg to the destination peg. First of all, we have here n disks. What we did is that we moved and minus1 to disk from this peg to the second peg. So what you should do is to move the top most n minus one disks from the original peg to the extra peg here. Then move the largest disk from the original peg to the destination Peg, as we did here. And finally, move the n minus one disks from the extra peg to the destination pack and use recursion early. So the step to move n minus one disks after the way is the same problem all over again. So when we have here and minus1 desks here, so we need to move them back here. We can perform the same steps as usual. And after getting to this point, we can then perform the same step. All other account. So now let's go to this program and create a new package. Our gathering. Here we have our main method. And the second class, we name it Tower of annoyed. And now, first of all, we set up the puzzle with a specified number of disc. So we create our constructor towers. And then it will take an integer, how many disks? So name it. And we have, of course, I would local variable total desks. And what we'll do is to equal to n. And now we need to solve this program, this button. So we'll use two methods here. The first method is to move the specified number of disks from one tower to another by moving a subculture of n minus one disk to the way. So as we said, what you do is make it private. We'll name it move tower. And now today the number of disk as parameter and the starting and the ending, and the extra one. Now, as we said, if we only have one desk left, we just move it from here to the last Peck. And so this is our base case. If number of this equal to one, then we just move it. So let's create the method that will move the disks will instead of writing it every time here. So we have private void. Let's name it, move one disk. Today, two integers is starting from where we are going to move it to two. And until it will simply print that move one disk from start to the end. Now, so if the number of disk is equal to one, then move one disk from the start to act. Otherwise. What will do is what we said here. So we need to move n minus one disks from the first peg to the extra peg and then move n minus one. Move that last from the first day starting peg to the ending peg here, as we can see here. And then move these back to the first peg. So as we said, we need to move. The first step is to move n minus1 from start to extra. So we'll perform this move tower. We have n minus one here, so a number of minus one. And we start with start. And we moved them to extra. And the excerpt bag would be the ending Peck. After that, we need to move one disk. Now we're here. So what we should do is to move this disk from the first bag to the last peck. So we perform this operation by simply calling the method that you created here. Move one death from start to end. And lastly, we should move these disks to the ending pet. So we'll create, moved our number of death from the extra, the extra here to the end. So here we have and, and start. And now this method is said. Now, we can create a public method just to use this method easily without entering the start and end extra, rename it to sold. And it will take total disks as parameter, an integer. And two simply called moved our method with a number of this, which is the total. This bag is the peg number one, the end is three, and the extra one is two. And now let's go ahead and run this code and our main method. So we'll create an object Tower of Hanoi and T, the new towers of Hanoi. And let's use it with 34 desk. And here we have soil. And this code will get following lines. And the same thing. For example, let's say ten. This will get so many lines to move all the disks from the first peg to the last one. Now, think of the iterative version. How would look like? And if it is as simple as this recursive version. So this is it for the tower of Hanoi problem. And see you in the next video. 17. Insertion Sort: Consider you have a list of integers such as this list. And you want them to be sorted from lowest to highest. Hey, you can use salting. And there are actually multiple sorting algorithms and we'll talk about some of them. So inside by insertion sorting. Insertion sort is a simple sorting algorithm that works similar to the way you saw playing cards in your hands. The array is virtually split into a sorted and unsorted part. Values from the unsorted part are picked and placed at the current position in the sorted part. Two here we can start with this example. Let's suppose we have this list, and this list will be divided into two parts, a sorted and unsorted part. So first of all, I will solve it by two, will be the first element only. So this is our first list and this is the second part. Now, four is already sorted since it is only one element, there is only one element in this list. And you don't have to do anything. There is no element to the left of that is greater than four, then we need to add three. So we have 43. We compare three with four. So four is greater than three. So we should swap them and we'll get 34. Now, the sorted part of the list is 34 and the unsorted part is whatever there is here. And we'll move on to the next element. We have to compare two to 44 is greater than two, then we swap them. And the same thing we compare two with 33 is greater than two also. So swap them one more time, we'll get 234. And now this is the sorted part, and this is the unsorted part, and so on until reaching the last element of the list. And then we'll be done. So the general idea is to compare every element with all the elements to the left. Swap. Every time we find an element to the left greater than the element we are using. So let's go ahead and write the code. You go and create a class. And first of all, let's create our integer array, our list of integers. And for example, we have five elements in this array. Let me store them directly to 5110. So this is our array and let's create our Method. But let's name it insertion. And to take parameter an array, let's name it at work here. So first of all, we have the for loop to pass through all the elements from 0 to listed length minus one. Zeros are already sorted, so we don't have to start with 0. So we start with one. So our for loop result with one and ends a dot length minus one. Now we'll create an integer. Let's name ID key, and it will be our element in this list. So we'll be comparing a of i. So here it is three to 1012 and so on. And we sold it and an integer called game. And we'll have another integer will be j, i minus one to start by minus1 and go back until reaching 0. And what we're doing here is that we're starting this number, for example, that the state does case. The third line, third key in an integer. And we thought this number to an integer called key. And then we stored the position we're recovering to start, which is the position i minus one, j i minus one. So we're going to side with this position and going back until reaching 0. So we'll create a while loop. While j is greater than 0 and the key is less than a of J, we need to swap. So to saying here is that y j is less, is greater than 0. So here in this case j is equal to one. So good. And the second condition, while key, which is two, is less than a of J, in this case four. So we need to swap them. We swap. And then one more time, we check the conditions. We have j greater than or equal to 0. In this case, j is equal to 0. So that condition is valid. And two in this case, we have here too and here four, so 23, so three is greater than two, then this also valid. We need to swap one more time. And then we finish with this for loop since j will be greater than 0. And in this case, so every time we execute this for loop, j will be decremented by one. And here we need to swap this, create an integer called tamp. And as usual, that swap them will take a of j and storage here, and ten and a of j. A of j plus one in this case. And finally, we'll take a j plus one and give it the value of w. So here we swap them. And now we said, let's go back and print out. First of all, let's print the array as it is before sorting. So print at a and some space. Then perform this method with an a. And then that's printed one more time. For let's print the light from the code and time and will get first of all, the unsalted added and then we get the sorted one, since we used the insertion method we created here. Now we can see how this changed every time. So let's print out the array to create another for loop after each change. After executing the loop. And print out a J2, in this case, some space. And and then, and here we need to print an island. Before executing this loop and run the code will get this. So first of all, we have 32510. We start with i equal to one. So I equal to onRestart, t equal to a of i, j is equal to a of 12, and j is equal to 0 in this case, which is i minus1 0. Then we have a while loop that will run only one time since J is equal to 0 and then read recommended. So the condition here, it will not be valid for another execution. So we need to swap them since the two conditions are satisfied. The same thing with five. We compare five to now. Five with 25 is greater than two, then this condition is not satisfied. Will not enter the for loop with simply though and increment I by one. Now we have number one. The same thing here. We'll compare this one with every element and swap the two numbers. So for example, 15 will swap them. And then 13, we swapped them one more time. And lastly, 12, swap them. And lastly we'll have Dan. Dan is greater than all the other elements. So it will not enter the loop, the while loop, and we'll be done with the two loops, the inner and outer loop. And we can print, print telling you look out and a new array out. So this is it for insertion sorting. We'll explore more sorting algorithms in the next videos. 18. Selection Sort : Let's move on now to another sorting algorithm is the selection. The selection sort algorithm sorts an array by repeatedly finding the minimum element from the unsorted part and putting it at the beginning. Now we're considering ascending order. We also have the descending order in which we can find the maximum element and store it at the beginning. And as the insertion sort, we have two parts. The one that was already sorted and the unsorted part. And every iteration of selection sort, the minimum element from the unsorted sub-array is picked up and moved to the sorted subarray. So let's use an example. We'll use the same list here. And let's delete this. We have this method, delete these. And we'll keep this method work here. But before, let's run this code and use this list. Now. But we're going to do in this sorting algorithm is to find the minimum every time and store it at the unsorted the sorted part. So first of all, we will find the minimum and this list here we have one as the minimum, will take this 11 and swap it with this number. Whatever this number is, we'll swap it with one. So here we have one now. And so let me write it here. So we have 1253 and that the next step is to find the minimum and this list. So the minimum is true, and we need to start it at this position. So we're okay since the minimum is two. So the next step would be the same as the first one. Sense, we don't need to change anything. Now, we will look at this list. We have 5310 and we find the minimum here. The minimum is three. We need to store it at the first element. So we need to swap three with five to the next. That is, to swap them. So we'll get 123510 and glass scheme compared these two numbers as usual, Find the minimum between them and store it at the first element in this list. And since ten is greater than five, then we don't need to do anything. And this is our final list. So now let's go ahead and write it as a code. Public static, void, selection parameter of an array, integer. And in this case, for example, first of all, we need to store the length, the length, length to use it in the for loop. So here we have the length of this list. Beside our for loop, we can say array.length or both, the same. And now we need to find the minimum element in the unsorted part. So the unsorted part should be starting with i plus one. So every time we finish with I will compare and find the minimum in the part of i plus one at the length. And here we have n minus one. Since we don't have to end at the last element, we can simply add and here. And the inner for loop would compare the two last element. So in this case, let's store the minimum as i equal to i. And now we should find the minimum and the unsorted part. So we start with i plus one and the angled array, array.length or and the same Maxwell simplicity. And now we'll compare every element to find the minimum. So first of all, we considered that the minimum is at index i. The index and an integer called minimum. Firstly, I is equal to 0. So we considered that the minimum number in this list is at index 0 to go and look at index 0. So we'll find three and it's not the minimum. So we need to compare this number with all the others. And that's what we're going to do to compare a j. In this case, j is the list from i plus one from here to the end. So if i of j is less than array of men, then we need to swap the index and men, it will be actually the next. So what we're saying here is men would be equal to, and let's work with this example to understand it better. So first of all, we'll start with i equal to 0, i equal to 0. We store n minimum 0. Now we are looking at this number and j will be equal to 0 plus one is one who sat with one and, and with 1234. So we'll end with four. We'll compare array of one. F of one is less than array of men, I of men. Remember, many xn equal to 0 and J, in this case it's equal to one, will go and compare T2, which is adding one with three, just array of minimum, array of 0. F. Two is less than three, then minimum now is not this one anymore. It's not at index 0 anymore. It is at index j, in this case at index one. So we'll give minimum and nu value of one. And the same thing as before. Now, J is equal to two. Would compare j, I, a of j and a of two. We have I2 here, five with any of the new minimum, which is one. So you should compare now as five with 25 is greater than two, then nothing will happen. Then we go to here, we compared array of three, which is one with L2. So one is lower than T2. So we need to give the minimum and new value, the value of one. And finally, we have here at which index, the minimum value as after finding the minimum using this for loop, we need to swap this minimum with the element at the sorted list where we should sort them. So what you should do is as usual, we'll take a minimum consulted in an integer called damped than. We change whatever there is minimum with our eye. And lastly, give this i index ID array at index i, the value of n. So here we swap them and let's go back and use this method here. So selection and the arrays, array, and the same thing. We'll print them and we'll get 123510. So I think that selection sort is easier than the insertion. So in terms of understanding, it is basically just finding the minimum in the inner for loop and then swapping it with the first element here. But here. And this is it for selection, so it see you in the next video. 19. Bubble Sort: The third sorting algorithm we are going to talk about is bubblesort. Bubblesort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in groundwater. So consider this list of five elements. First of all, we compare the first two elements and swap them since five is greater than one. And then we do the same thing for number 235 is greater than four, then we should swap. So this is the nest now. And the same thing for element number. 345 is greater than two, she should swap. And lastly we have 58. And since they are in the correct order, eight is greater than five, then we don't swap. Now, we can see that this list is not sorted yet, since we have 1424 is greater than two. So what you should do is repeat the same steps over and over again until we reach the sorted list. The worst-case scenario, if we have the here an element, for example, that say 0. So this 0 should be swapped 12345 times. We have six elements in this list. And the worst case scenario is to swap this 05 times. So if we have a list of n elements, we should swap n minus one times. So we can perform this operation n minus one times, and eventually you will get this sorted list. So let's go ahead and write this code to create another method. Or it bubble with a parameter name ID array as usual and would work here. So first of all, we have a for loop. As we said, from 0 to n minus one, we can say array.length. And inside this for loop we have another for loop to swap every two elements. If the wrong code for we compare every two adjacent elements, a of j is a of j is greater than a of j plus one. Then we'll just swap them. Let's create an integer tamp. Now here, RAM spring, and the same thing here. And if we consider that temp equal to array dot a j and a j, whatever there is at j plus one. And then if j plus1, the value salt, and this is our method and I'm sorry, we will have n minus one and n minus one. Let's use this bubble and use it here, and print the last one more time. And we'll get 325110. This is before eating and after salting 123510. Now, our code can be improved by doing a simple task. So for example, here, we swapped out the elements here. First time. And then the second time, we only needed to swap 42. And this list is sorted now. But in our example, we're obliged to pass through all the list and minus1 times. So one thing we can improve is f. And we got to a point where our inner for loop do not perform any task, then we can equate because we have no elements to swap. So we can do this task using a Boolean. So let's name it swat. First of all, it is, should be and not give it the value here will give it the value inside our outer for loop. Swap two will be first the equal to false in this case. And if, if we only threw up at least one time, then after going out from this inner loop will check swap. If swapped equal to false. Then we did not perform any swapping here. So we can exit the for loop since our list is sorted now. So we can break. Otherwise will continuous swapping. And we run the code, we get the same result as before. But now it is much simpler and it will not take as much time as before. So this is it for bubblesort, the simplest sorting algorithm between all of them. And see you in the next video. 20. Merge Sort: Moving on to another sorting algorithm, merge sort. Merge sort is a divide and conquer algorithm. So let me go ahead and create a new class. Subclass, created insertion name it merged. And he will discuss merge sort. First of all, let's consider a small list of four elements and then discuss a bigger list. For, for example, let's consider a list with four elements, 2431. So at, we're going to do is to divide this list into two list. The first one we'll have 24, will contain two elements, and the second one will have the last two elements, 31. Then we will sort each list alone. So 24 are already sorted, so we don't need to do anything, just type them. And here we have 31, we need to swap them. So this is our second sorted lists. And then after that we need to merge them. Since they are sorted. So the first element should be the smallest. So we'll compare the first element in the two lists. Here we have 21. So one is exploited and would write one. And then we'll compare two with 32 is smaller than two, and then three with four. Same thing, pride v and then still have the last element in the first list, since four. And then we're done. Now, let's use a bigger list. We have in this list seven elements. We divide this lesson to toilets. And the first list will be four elements and the second three elements. Then we do the same thing with this list divided two to 22, the same thing with the other one too. And we still have only one element, so we'll divide it to one and we'll do the same thing. And this list, we have two elements, would divide it to one element and every list. And then we'll have 1234567 list. Each list contains only one element. After that, we need to merge them. We start with the two elements here. We have twenty seven and thirty eight. Twenty seven is smaller than you'd write 2738. Same thing here, 343982. And then after that, we need to merge the two lists here, as we did in the previous example. So first of all, we have three, then we have twenty seven, thirty eight, forty three. The same thing here. And we finally have our finite list final salted. The idea of merge sort is quite simple. It requires to call the method would more than 1s, two until we reach. List of one element, then recreating sorted lists as shown here. Let's go ahead and write it now. So to complete the Merge Sort, need to write two methods. The first method could be thought, the main function that sorts the array using another method. So let's write it. Public void. Let's name it public static, void. And it will accept an integer and no index and high index. They represent where should we start? And then if low is less than the hard, then we can continue. Otherwise, we don't need to do anything. It means that low is greater or equal to high. In this case, we only have one element in the list and we don't need to cite it. So we're working on if low is less than high. So what we should do here is to the middle index equal to low divided by two. And then what we'll do is to sort and divide the list into two lists. And then, so the left part alone and then so the Right but so can do that will enter our At a, we have with low and then sort the right but at a middle plus one. And after that, we need to merge the sorted halves. So we'll call a method limit merge. And it will take as a parameters left, low and high. And let's go ahead and create our merge method. So we have here can make it private, private, static, void, merged and as usual method. And now we need to find the sizes of the two subarrays and to be merged. So the first type is limit n one equal to minus one. And the second one is N2 equal to hi minus. Now we have the two sizes. New lists. So we name it list one. Or we can emit left and right. So we have left the size and right. This second size. After that, you need to copy the data from this original array to our two arrays. Now, we do that. We simply use a for loop with the boundary of un1 and copy all the data from right to left. So left i equal to i. And another for loop to copy the data of the right part of the array to our list called right. In this case, we'll start with an array of middle plus one and plus j. And now we copied the data here. Plus, I am sorry. And this is how we copy our data. Now, we need to merge them together. So what you should do is to create a while loop to copy all the data we stored and left and right copied back to our original update. So as we said in this example, after going to this phase, we need to store them back in the original list. So here we'll compare the two elements and then we'll start them and the original array and the same thing here. We compare these elements together and we'll get our sorted list. So let's go back to our code and write a while loop. And the condition of this while loop that we still have elements in both left and right. So here let's create integers i equal to 0, and the same thing for J, 0. And let's create an integer and name to name it, which is equal to o. Now, while i is less than n, one, which is the size of left and J is less than. And two will be working on the slope. Now first of all, we'll compare left with right of j, of i is less than j, then we store it. This component and the original list. So a will be equal to left. And then we increment i. Since we're done with this, for example, let's go back here. What we have done in this case is we compared left of i here, 27 with three. F, 27 is less than three. We should store it here. Now, in this case, 27 is greater than three, then we should store three in this case. So otherwise, we should store it. And at any k derived component and increment j by one. And in both cases, we should implement k Since it will be filled either way. Now, after finishing this while loop, we might have some elements left in any list. So to fill in the original one, we should complete whatever there is left in our two lists. That would create a while loop. While i is less than N1. The N1 if i is equal to N1. And we broke out of this while loop because of i equal to one, then this while loop will not work since it would be already equal line one. So if this is the case, we should only buy whatever there is in the left part and increment, increment k. And the same thing with n2 if j is less than and to do the same exact thing, to write increment j and k. So now we're done with our merge function. And let's go ahead and use it. And I will main method. So lets go back. But before let's check our boundaries. Here we have left and right. And here we should start with a node. Since we are not sat with zeros, sat with whatever our index here is. And now let's go back and documented here. Our main method. This creates a delay equal to, in this case, four to 718 and use it now, use the sort with a 0 length minus one. And then use a for loop to print out our elements. And discord 12478. So this is it for merge. So it See you next video. 21. Quick Sort: Like merge sort, quicksort is a divide and conquer algorithm. It takes an element as Pivot and partitions the array around the pivot. There are many different versions of quicksort that big pivot and different ways. We can pick the pivot as the first element. First element, random element, or the media. I'd explained what pivot is in a moment. First, let's write a list. So consider we have a list contains 101819. And so this is our list. Now we choose element as a pivot. So let's go ahead and choose, for example, the last element and separate them to understand it. And it's going to have to the same thing here. So here we have this list and this is our pivot. So right down here. And here we have the first element. Now we need two arrows, 2.2 to two positions at this list. The first, we start with the first element from the left and the last element before the pivot. So here we have our first, lets say this is the first element and this is the last. Now, what will do is to compare the first element if it is greater than the pivot, than we need to swap it, we need to end up with a budget that is lower than the pivot and the other part should be greater than the pivot. So to do that, first, ten is greater than the pivot. Now, then will move, will move to 80. Here we have 80. So now we're at 8050. So 80 is ready to be swapped. Now we'll look at 5050. Is 50 greater than the pivot? No, then we can swap it. So now we swap 50 with 80. Here will have 80, and here we have 15. Now we change the positions of these arrows. We have this arrow at 42nd 30. Now, the same thing will do the same thing here. We have 13, is 30 lower than the pivot? Yes. Then we don't need to swap. It will go to another go to 90. And now we'll compare 90 with 40. 90 greater than the pivot? Yes. Then we need to swap it. Is 40 lower than the pivot? Yes. Then we need to swap these two elements, will have 90 here. And the, now the two arrows, let's name them to be able to see what will happen. We have low and high. Now before swapping, these were the possessions, low and high. Low at position 0123 and high position for. Now, after swapping the two elements, we need to increment by one and decrement high by one. So I will be at position, at this position and low will be at this position. And whenever low is equal or higher than high, we can know that we're done here. Since lobe has passed high. Now, the last thing we should do is to swap this element with the pivot. So you will have 17 And at the pivot 90. Now we can see that all the elements smaller than 70 and all the elements here are great and 70. So this is the idea of the QuickSort. We can perform this same exact algorithm to this list. We can choose 40 as the pivot and work accordingly. And the same thing goes here. And we let recursion do the work for us. This is the general idea and we'll use the recursion to be able to implement it more than once has emerged. So we have two methods here. First method will be private, integer. Let's rename it partition. It will take the parameters and emit. And low than this method, where we take the last element as the pivot. The pivot element at its correct position in the sorted array. And cases all smaller, smaller elements than the pivot to the left and greater to the right. So let's go ahead and start with this method. First of all, we have our pivot is created. Now the vector is equal to the last element in this list. And we have the index of smaller element. It is a sname i, which is equal to minus1. And this case, we start with our for loop. We start by low. All the way to. Now, we need to check if the current element is smaller than the pivot. So if j is less in this case, and we need to increment i. One, swap and array j. So let's swap it. And to a. Then equal to, sorry, a equals i, equal to j. And finally, back to G. So now we swapped the two elements. After finishing with this word loop, we need to swap the pivot with at a i plus one. So in this case, create another time and swap the two elements. As we said. Here, we have the pivot is at location a. And then giving the tan two. Now we swapped the two metadata to elements and then we will simply return plus one. So this is our method, the partition method. This method took the last element as Pivot, place the pivot element at its correct position in the sorted at a, and places all smaller to the left and greater to derived. Now, the other method is the main function that implements that quicksort. And let's name it public static, void. So that it would take three parameters as usual, and low and high. First of all, we'll check if flow is not greater than high. We can work otherwise will not work because it will not make sense. And we'll have, we'll create an integer, let us name it by, by is that partitioning and depth. That will, where we'll use this method we created here. So pi would use partition at the low. Now, after getting the index by, now, we should sort the two, abase the left part, Right? But so we'll use the same method one more time with no other way to Pi minus one. And the same thing by plus one other way to write. And then we're done with this method. You can use it. And our main method. So we create an array, for example, steam it, and with some values 426173. And we'll call the sort method 0 and updated length minus1. Then create a for loop and print out the elements of this list. So as usual, with some space here, and let's go ahead and run. The code will get 1234567. So this is the array is sorted array after performing this QuickSort. So this is it for Quicksort. See you in the next video. 22. Linear and Binary Search: Sometimes we want to check, put an element from the list. And here we can use searching algorithms. We have several sessions algorithms and we'll start with linear search. So let's go ahead and create a class name. And then the main method, let's create our array. So this is our array. And let's suppose you want to search for the number to an integer, number equal to two. Now, this linear search is suppose to go through all the list and check if the element in this list is equal to two. Return the position. Otherwise, if no elements matches this number, we should return minus one. So let's go ahead and create our method. And it will take that list and an integer number. So what we're going to do is to compare this number with every element in this list. So use a for loop to go through all the animals in the list. And number two position. Then we should return this specific position. Otherwise. So the method does not work. We should return an integer. And otherwise, if we best through all the list and we didn't return any integer, then we didn't find the number is specified number minus one. And if we use the t at n number and print it out, we'll get then decks of the number two. Now here, 22. Let's change the number, for example, two to six. So here we found the number six at index three in this list. And the same thing if we change it, for example, to nine and print it out, we'll get four and x four. Now, we tried another number that is not in the list, we get minus one. So this is how search works. It's very simple, but it takes. Much time. We'll talk about time complexity later on for every algorithm. Now, let's move on to another searching algorithm is the binary search. Here, we should have a sorted array or list, and you'll know why in a moment. So the idea of binary search is to search a sorted array by repeatedly dividing the search interval in half to begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrowed the interval to the lower half, otherwise, narrow it. The upper half. Repeatedly check until the value is found of the interval or the interval is empty. So let's look at an example. Here we have this list. First of all, we take the middle element. Here. In this case it's 16. And now we want to search for 23. And our array is sorted, as we said. So we check if 23 is greater than 16, then we take this half. Otherwise, who should take this half? Now, since it's greater than 16, then we'll ignore all the elements here because we're absolutely sure that 23 is created then all of them, since they are sorted. And we need to check now for this part. And we take the middle of this part and do the same here. F2 and F3 is greater than 56. No, it's not. Then we need to take the left. But, and he began use the middle as 23. And we found 23 to return the index five, in this case. For now. And our method. Let's create a new method, binary search. And it will take an array as usual. And the low index X. So X is the element to session four in this list. And now we need to find the middle and depth. And to do that, let's go back and see in this example, here we have low and high to what we did is two, subtract nine minus 0 over to 4.5 because its integer, it will take four. And the same thing here, who took 59 minus 54 over two, which is two plus the low index plus five equal to seven, fundamentalist seven. And the same thing we did here. And let's go back and set the condition that if high is less than the load, then less than or equal to the node than we've done. Otherwise, we should work. So here is greater than the load will work. Otherwise, when we turn, we shouldn't return minus1. Now, let's find our middle, as we said, is equal to low plus high minus low of a two. So this is our metal. If metal element is equal to our x is equal to two assertion. And then we'll just return. Otherwise, we should check, as we said, let's go back here. In this case we have 2323 is greater than 16. Then we'll work with this. But we'll do the same exact stamps and this part, otherwise, we should do it steps on this bug. So to do that, we simply compare at a to x. F at a is greater than x, we should return BinarySearch. This is the same, but low, and we need to change how to middle minus1 goes. We're absolutely sure that old middle and all the rest of the elements on the right side greater than S, then the not x otherwise would return. And binary search to search the left side. So we start with the right side, I'm sorry, the metal plus one all the way to high and the same number x. So this is it. Let's go back and implemented here. And our main method, we're going to print out binary search at a 0 and minus one and the number of searching for numbers. Let's go ahead and run the code. We'll get minus 12, for example, six. And we'll get. So sex is at position three. And this is a binary search. Binary search is much faster than linear search. Since every time we are going through the list, we're cutting it in half. However, in linear search, we are obliged to go through all the elements in the list. We still have some searching algorithms and we'll talk about them in the next videos. 23. Jump Search: Like binary search, jumps is a searching algorithm for sorting arrays. The basic idea is to check fewer elements than the linear search by jumping ahead by fixed tabs or skipping some elements instead of searching all the elements in the list. So for example, consider we have this list. We have 16 elements here. Let's suppose that we're searching for number 55. So the jump search will find the value of 50 using some steps. First, let's consider that the block size to be jumped as for since 16, square root of 164. So first of all, it will jump from index 0 to index four. So it will jump to 01234. Jump to this element compared to 5535 is still greater than three. Then we'll jump one more time to any one. Same thing here, 21 is less than 55, then we need to jump, will jump to 144. And then we can see that 144 is greater than 55. So we'll go back to 21 and perform a linear search from 21244 until we find our element to number 55. We usually use the square root of length as the block size to be jumped. Because in the worst case scenario, this is the best step size to take. So let's start with our code. Jump to the main ij's of integer and x that we are going to search in this list. And here, first of all, we need to store the length of the day. We need to choose our stack. And as we said, we'll take the square root of n using the mass. And this mass square root, for example, suppose we have 17 elements, give US 14 certain numbers. So after taking the square root of and we formatted using Math.floor. And then since we're storing it in an integer to converges to ENT. And if the element exists, then we need to find a block where the element is present. So let's go back to our example. And he 55 is between 2144. So we need to find these two elements. And we already have an integers. We create another entity or let's call it, for example, previous, liquid to 0. So at the beginning, previous is equal to 0, so it is at position 0 and step is at position four. And if the element is not found in this interval, then we should give previous the value of four. So previous is now at position four and we need to add four to the step. So step would be at position eight and continue until we find our element in this interval and our interval. So to do that, we need to create a while loop and set the wild loop as at a is less than x. Now, we might get to a point where if we keep adding four, the step, we might have stepped greater than n, then we cannot access at a half-step. So instead of accessing array of step that says a radar minimum between step and, and. So every time we execute this loop, we need to change previous to the new value. And the same for step create to add. Whatever we have here. So it added. And then we'll get previous is greater than or equal to. And then we're finished. We didn't find the animal who simply return minus1. And he we should change to integer. Now. So what we're saying here in this while loop, let's use it at this example. First of all, we have previous equal to 0 and step equal for possession. For. Now. We go through this while loop. First of all, we'll check array of minimum between step and then step is surely less than n. In this case, step is equal to four. So math at a of four, which is three. In this case, we'll check if three is less than x. Yes, then we'll continue executing this while loop will change the values. Now, previous is equal to four and step is equal to eight. And then we'll check if we pass the boundaries. If previous is greater than or equal to n, Then we passed the boundaries and we done, we didn't find any number that matches acts. So now we're at position four. And position eight. The same thing. We compare this 21 with 5521 is less than 55 and we need to execute the while loop one more time previous is now at position eight. So this is position for this position aid. And step is at position 12. In this case, we have 144. So compare one hundred forty four, fifty five and fifty five years less than a 144, then will exit the loop. Having previous the value of eight and step the value of 12, then we have our interval and 55 is in this interval. Now, after exiting the while loop, we need to perform a linear search for x and y and another while loop. So while we sat with the previous, now since the previous is at position eight and step is at position a hundred and forty four and fifty five, which showed that receiver is indistinct third interval, then we'll start with 21 and continue. So wide carry at previous is less than x, then we'll increment by one. And if we got to a point where previous is equal to either step, equal to 12 over n, So equal to minimum between the two integers, either stamp. And then we need to break or return minus1 can simply return minus one. In this case, since we didn't find our number. And then we check if we found the element. So if array previous is equal to x, then we return this position and return minus one. If we die, we didn't find it. So this is here. We have an adder cannot converge from n Boolean, we have a missing equal. So this is it, this is our function. And let's go ahead and choose it here. So we'll print out chunk and we'll search for 55. So let's take this and put them in our array. So this is our array and it will return ten. So 55 is at position ten. So these first two lines were from the past functions now this hour than our position where 55 is at this list. So this is it for jumps. See you in the next video. 24. Interpolation Search: And other searching algorithm as interpolation search. Interpolation search works better than binary search. Because binary search always check on a middle element basis. But interpretation search may go to different locations according to the value of P to be searched. So for example, if we want to search for the number three and this list, if we use binary search, will go and check in the middle. So either 1321034, so this is the middle of the list. However, if we use interpolation search will go to the value that is closer to our number using a specific formula and we'll talk about it later. So he three is closer to 0 than it is closer to 610. So our formula will lead us to a number between these. So the same idea as binary search, but instead of having a middle element, we will having a position that will change according to our elements. So let's go ahead and create our method public. Let's name it interpolation. And as usually to take an array of elements and the size of the element, as well as the t or let say x. Now, we need to set our low and high and low equal to 0 and will be and minus1. Now we'll enter our while loop. Low is less than or equal to i. Otherwise we don't need to work anymore because we didn't find our element. So it's the same as we did in binary search. And we need to add some conditions. While our element x is less than or equal to, our low is greater than or equal, I'm sorry, and is less than or equal to our element. So as long as these conditions are satisfied, we can work here. Now, whenever we get to a point where our low is equal to our high index, then this means that we have done so either we find the element or not. So we'll check if I add the same since they are equal, is equal to our x. And this is the case return low, otherwise, return minus1. And after checking this condition, now we can work, can create our formula that same position that will jump. As we did in our binary search, we created a position called metal element. Every time we go to the middle element now, we create another integer called position. And the formula is as follows. This is how we compute the interpolation. And a of high minus low. Then we multiply it with I would x minus a of load. And now we check if a ray at this position is equal to our element, then we just return our position. Otherwise, we'll check that a at this position is less than our element. Then we need to change our low from low to high position plus one. The same thing as we did and in binary search, but instead of position, we used method otherwise will be position minus one. So otherwise, if the we have at a position is greater than x, then du would be equal to position minus1. And after finishing this condition and everything, the while loop, we can return minus one if we don't find the integer. And now let's go back and use it here. So I print out interpolation. We have the a and B and x will be the number. So for example, let's suppose for a search for b. And let's go ahead and run. Our code. Will get for SO three is at position 401234. This change this number to 89 and we'll get position 11. So 89 is at position 11. And the last thing we're going to check if we enter a number that's not in this lesson, 900, we get minus one. So this is it for interpolation search. See you in the next video. 25. Exponential Search: The last searching algorithm we're going to talk about is exponential search. Exponential search involves two steps. First, we need to find a range where the element is present. And then we'll do binary search in the strange. So let's consider this list. We have 16 elements and we need to find, for example, 89. So what we're going to do is, first of all, consider if our number is equal to the first element in this list. If this is the case, then we return 0, so it is at position 0. Otherwise, we check all the other elements. We start with i equal to one and with doublet I equal to two, then i equal 24816 and so on. And let's go ahead and implemented to better understand this. Go here and you have public static, and let's name it exponential. As usual, take an array of integers and, and, and the value that we're going to search, we name it x here. First of all, as we said, we need to check if at index 0, if the value is at index 0, then we simply return 0. Otherwise, we need to find the range for the binary search by repeated doubling. So we'll define an integer with the value of one. And we enter the loop. While i is less than n, the length of the array. And add a, i is less than our, less than or equal to our number. This loop will be executed. So we'll simply multiply i by two. So every time we enter this loop, we multiply i by two. So let's see here in this example, when we can exit this loop. For example, if we want to search for the number 13, first of all, we check if 0 is equal to 13. No, it's not. Then we define i equal to one and enter this loop. I is equal to one, will check. While I at a, at i is less than or equal to x, one is less than or equal to 13. Yes, then we multiply i by. So now we have I equal to two, and we'll go to our next element. Here we have also one, it's less than 13 and i is less than n. Then we multiply i by 21 more time. Now we have two times 24201234. Now, here we check the is less than 13. So we can multiply one more time to four times 28. So now we're 5678, we're at 21. Now. We'll check 21 is less than 13. No, it's not. Then. We exit the loop with i equal to eight. Now we have I equal to eight. And to get our interval, we have i equal to eight and i equal to four, which is eight divided by two. So after finding our interval here, we simply use binary search. And I work at a. And here we have i divided by two. This is our interval and minimum between i and one, i and n. Since it might be, it might be that i is greater than n and we cannot work outside our boundaries. And here we have our integer x. And since we need to return the type, so we simply turn binary search. And then we're done. Let's go ahead and use it here. So we'll go ahead and print out exponential at array.length and w. We're going to search, for example, 13. And the code will get seven. So 13 is at position seven. So let's make it better, nicer. And exponential. Let's store it as exponential and an integer. As intimate result is equal to this exponential. If the result is greater than 0, then we print out the element is present at index. And we print out the index. Otherwise, we print out that element is not present. And Ray and clustering the code will get element is at index seven. Now we have a shortcut in Java that you can use. So instead of writing all of these, we can simply print out one of the two lines. So we need to set here the condition if result is less than 0, this is the case. We can print out. Element is not present. And the other statement would be element is present, index. And we print out the index. So let's go ahead and see what would have been here. Let's run the code and we get element is present and index seven. So this shortcut, First of all, NDA System.out.print method. We set the condition your result is less than 0. Then automatically this method will print the first statement. Otherwise, it will print whatever there is after the two points here. So we asked them if there is act is less than 0, yes, print this. Otherwise. Print this. This is very helpful if we don't want to complicate things and we need a very simple form of printing. So this is for searching algorithms. See you in the next video. 26. Extra Problem: Inheritance: Now that we're done with our material, let's go ahead and solve some extra problems. And you start with inheritance. Will create the bank account. So I already created a package called it BankAccount. Inside this package, we'll have our parent class, name it, bank account. And we have two types of accounts. We have a saving account and a checking account. So let's go ahead and create two new classes. The first one we name it checking, and the other will be savings account. And of course we need to create our main method. This is our diver method, and we create our main method. Now, the two classes, bank account and just same thing extends bank account. Now let's go back to our bank account and create some methods here. First of all, we have a variable. It is our balance. So let's referred to it as protected w. And let's create a constructor. And then we'll use our methods. So bank account balance. And say that balance to balance. Now, Jaffa allows us to have the same name here. And in this case, we need to do is to refer to this local variable. We need to use that. And now we referring to this one outside and this balance is the one in the parameter. Now, let's create our method. We have the gut balance public. Www2 get balance will simply return. Another method, for example, to withdraw void with a specific number. We would go specific and simply subtract this amount from dependence. And another method if we want to add a specific amount. So we simply added to the balance. Now let's work on our savings account. So in descending account we have an interest. So first of all, let's create our constructor savings account. And we got the support from our parent class with our balance. And to add the interests, we need to have a rate. So let's create a protected vinyl and naming. And we said the rate at 0.04. Now let's create our method to our interests, public void interest, and will simply use the method to create it in our bank account deposit and to add our interest, which is date times our balance. Now, let's move on to our checking account. And our checking account. First of all, let's create our constructor and then talk about the methods. So in our constructed as you live, public checking, account balance. And we'll call the super and penance. Now, every time we withdraw or add an amount to our account, we have a feed that we need to pay. So let's create some integers, some variables outside. To. The first is just to keep count of how many times we used our account either to withdraw, to out at to add a specific amount. So protected and section count. The other one is first of all, we have to free times began withdraw or add an amount for free for two times. So the first two times, we withdraw at its full free. So let's create our finite integer and name it 32. And the last one will be turbulent. And this is the field that you need to pay after passing the 23 times. So let's name it v equal to 0.25. Now, we need to set I will transaction count at 0 equal to 0. So you use go and deposit method. We need to override them. So here we have our deposit and withdraw. So they already called the super dot width row. We don't need to do these steps here. We just need to keep track of our transaction count. So every time we enter this method, we just increment transaction count by one. And the same thing here. Now, these are our method and the last method we're going to create is the method to deduct the fees. So let's name it is. And this method will simply check if transaction count is greater than two. Then we need to add some fees. Otherwise, we just reset the counter. We set the transaction fee, that transaction count to 0. Transaction downtown is greater than antigen outside, we name it three is greater than three. Then we should get the w. Let's name it. Fee. And it would be equal to transaction 23 times. And multiplied by v. We named it outside. So let's suppose here we have FDI. Now, after getting this variable will just withdraw it from L. So these are our method. Now let's create two string method for our bank account class. Here. We have public String toString and we'll simply print with turn balance. Our balance is our balance. In our checking account. We'll call, we'll call super. So let's create a string and call it result, which will store our strength. And then a line and print out our counts. So here we can add our transaction count and just return result now and our saving account. Let's override the toString method. So we'd have to override it. And the string would be equal to super dot toString. And Add to this result, align and return the result. And then we'll go to our main method and create a checking account and savings account. Checking account, let's name it, see it. Violence could be 1000. And saving account S equal to new savings to banish would be 10 thousand. Let's use some of the methods. So c dot with Joe would go 150. And then this word without saving account as dot with TO 2003. After that. Let's use as. Then go ahead and print out checking account with our C. And let's print out the line. Then saving account. And we print out s with a line. And then we'll use our method and see, see, deduct and print it out one more time. And let's go ahead and conduct a checking account balance, saving account balance with count rate. And you can see that the balance here is the same since even though we use data, but count is equal to two. So let's use and the method one more time. For example C, deposit any amounts, for example 20. And we'll see that even though we deposit and we added 20, our Vanessa's now a 169.735, we deducted 0.250 p. So this is it for this example to you in the next one. 27. Fibonacci Sequence: Now we're going to talk about Fibonacci sequence and to solve it using three methods. First of all, the Fibonacci sequence is the series of numbers in which the next number is found by adding the two numbers before it. So let me go ahead and write it here. The first two numbers are 01, then we add 0 plus 111 plus 121232 plus 35, and so on, 8132134 and so on. So this is the idea of this sequence. Now, let's go ahead and solve it recursively, iteratively and using the Big Integer class. We'll talk about the Big Integer class later. So first of all, let's solve it iteratively. So let's create a method, private static that's predetermined along, for example. And let's name it, felt IT. That will take an integer and the position. And what we should return is the number at the specified position. So for example, if we entered five, we should return 01235, we should return three. If we enter seven, we should return eight, and so on. Now, as we said, first of all, let's create an integer and give it a value. And the value at 100. And we need to add the previous element is equal to 0 at first, h equal to one, and the previous previous, previous, previous xi is equal to 0. Now, we create a for loop with, we start with i equal to two and less than or equal to n. And now, every time we enter this loop, we add to the solution previous, previous, previous. Now we need to change our two values here. So previous previous will take the value of previous, previous, previous, previous. Previous will have with the value of the solution. Now, we just return this number after the for loop. So let's go ahead and create our main method. Here. We have our main method and use this. So we print out Fibonacci iteratively duration. And we'll just print out the ICTY with the value. And let's ask the user to enter the value. So scanner scan, scan. Please enter a number, and we'll store that number next n. And we'll give it as a parameter here. Now, if we go ahead and run this code, will get, please enter a number. Suppose we enter five, get the Fibonacci iteration 568, and so on. And for example, we get 55. Now, let's use recursion. So let's go ahead and create another method, method, private Karen to give it a value. And as we said, we need to create our base case. Base case if n is equal to either one or 0, we should end. Otherwise, we should return Fibonacci recursion of n minus one plus Fibonacci recursion of N minus two. So the last two elements. And let's go ahead and run this code. Use it in our main method. So we print out the Nazi. Because plus. And run this code. We get a number, for example, sex, Fibonacci iteration aid, and the same result as we use the recursion method. Now, the last method will use as big integer. So we'll use Big Integer class. So this class is generally used for mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of a 100 contains a 158 digits. So we can store it in any primitive data type available. We can store it and this big integer and use the methods and discrete integer plus. So now let's create our method using MC integer will be a private static with the value of quick integer. Usually we return belonged or integer or string, now returning Big Integer and just name. And the value of n. As usual. Now, the same, I would do the same as we did in our iteration method. So. First of all, we need to store the solution. The value of big integer, let's name it. Solution equal to big integer dot 0. So this is how we store 0. And the solution. We don't say Big Integer solution, solution equal to 0. We say big integers. Now, the same thing for previous, previous, previous. So big integer, previous equal to big integer. But one and Big Integer previous, previous, big integer, 0. Now we start with our for loop and we start with two and end with n equal to n. And solution will be previous, previous, previous, previous, previous, previous. And then previous previous now would be equal to our previous and previous solution. And then we will simply return solution. So this is it. And now let's print out System.out.print integer parameter number. Let's run the code. And we'll get 556, we get eight. Now for example, if we use a large number such as 10 thousand, we can see that because it will take some time. And if we use a 100 thousand concede that it generates an error. However, if we only use this Big Integer class, and let's enter this number. You can see that 20 dead this sequence. And it is a very large, Not really, not be able to store it in any primitive datatype. So this is why we use the Big Integer class. Now, this is it for Fibonacci Sequence. We used three methods and we learned a new class about a new class, the Big Integer class. 28. Big Integer Class: Now let's talk about Big Integer class. We already talked about it and Fibonacci, but he will explode it more. So this class is available for us in Java to math class. And first of all, let's create our big integer, captured this favorite Big Integer one equal to one. So now we are giving this object a value of 1. First of all, let's compute the factorial of code for example. So let's name it. And we create our for loop two, the start from 12 all the way to. And every time we pass through this for loop, we multiply two big integer. We have a method, big integer dot multiply and multiply it by big integer value of i. And then let's go ahead and print out the area of plus big integer. Run the code. You get factorial of 24. Now let's use other methods. We have. For example, let's create another object, Big Integer, big integer, two. New big integer with the value of a 197. Now, let's use it confirmed method and get the minimum between the two objects. So let's print out I think the big T2. And now we get the minimum between the two values. We get 24. And if we use the Macs big integer, integer to get a 197. Now, for example, if we want to get the next prime number greater than 197. So we may use this method. So let's print out the next prime number. Next to a 197 is, and use big Institute to next probably prime. And we get the next prime and it is a 199. Now, we still have lots of methods and we use some of them. For example, we have the ad. So if we say big integers, the integer two, and let's start it in big integer name. Call it edition equal to this, and then print out edition will get to a 121. So the rest method we're going to use the toString. So this method will return the decimal string representation of the big integer. So we can store it in a string. Let's call it equal to integer, one to string. And so we already have n here, so we need to choose a number cruncher name, template name. Now, if we print out name, will get 24 and name dot charAt 0, for example, will get too. So we converted this big integer to a string and now we can manipulate its characters. So this is it for Big Integer class. We don't use it that much when dealing with small integers. And we still have lots of methods. They are so useful and we can use them in our code. So you can explore them on your own. And now see you in the next video. 29. Extra problems: Recursion: Now let's go ahead and solve some problems using recursion. So the first problem we're going to solve is to change old x's and a string, too wise to consider. We have an example code X. Then we need to change it to owed by the same thing. For example, x x, x x. We need to change it to y, y i by light. And wouldn't do it that without using loops. So let's create our method, public static. And it should they turn staying this name, change x, y, and it will take a stand. That's now we need to pass through the animals, the characters in the string. And first of all, we need to check if this thing, our base case is if the string is of length 0, then we're done. We don't have any character. So we check SDR that length. If SDR that time equals 0, then we simply return an empty string. Otherwise, we need to work. Now, the first thing we need to do is to check if the first character in our string is x. If this is the case. If SDR is at z is equal to x, then we need to do something. And we need to return X and converted to y. So we should return y plus. Now, after finishing without first character, we need to do the same thing for the rest of the characters. Now, in this example, let's say, let's say we have x, x, x x. So what we should do here is to compare this to x. If this is the case, then we need to convert it to i. Now we have Y and something. Now, after finishing with the first character, we need to do the same thing with the rest of the characters. So we need to enter this method one more time and check if this character is X. Now we need to convert it to, so we'll let the recursion to do it. We simply call it the method itself with a new string, which is substring from one till the end of the string. Now, when we get to the point where we have only one element in the string, then this will generate an error. Because when the string has only one element, one character, and we're saying that we need all the elements after the first character. So to deal with that, we need to set the condition STR, that length is equal to one. Then we should simply return. Why? Otherwise, we should return y plus quota method one more time. Now, this is for standard chart at 0 equal to x. Otherwise, we need to do another task. So if SDR dot length equal to one, we simply return STR at 0. Otherwise, we return stdio.h I that quote the method one more time. And yeah, that substring, substring from one end of the string. And now we need to convert it to a string. So we simply add string here. And now we're done with this method. Let's go ahead and use it in our main method. This is our main method. And let's ask the user to provide us with the string, please and stored it. A string called name. Then use this method and print out the result. So change x, y with our name. Let's run the code this into a string. For example, x x x x, y, y, y, y. And if we don't have any x, here, for example, we get high. Now let's move on to another simple example. The precomputed, the power of n. For example, if we have 32, then this is, as we are saying, that we need, that we want t to the power of two over three to the power of 45 and so on. So it is very straightforward. Let's create our method. And it will return a teacher to take two integers, and, and, and. And. And I would base case, if n is equal to and 0, then simply return one. Otherwise, we should return our base times our base with an minus1, since we multiply base. And let's go ahead and use it here. Let's, for example, consider n equal to five and n equal to three. And print out with base. And then let's run the code. And we'll get, you get a 125. Now, let's move on to another example. And this example, we'll use integers or an array of integers. So suppose we have this list, one to 20. So here we have two and then followed by 20, which is two times ten. And let's take another example, 14315 and eat the same thing. We have three multiplied by ten, we get. So three is followed by 13. Now, our code should return true if we have two elements and if we multiply the first by ten, then we get the second. So here we return true here. And for example, 12345 should return. Let's go ahead and write our code. Public static should return a Boolean. And let's name it. For example, multiple. And it will take a value. Integers has a limit at a and the number. And this number is the index that we're going to use the pass through our array. And now let's start. So first of all, if then dx is equal to the array.length minus1, then we passed all the array and we didn't find any two elements that matches our condition. And in this case, we should return false. So this is our base case. Otherwise, we should work. Now, if index is less than a day, length minus one, then we can work. In this case, we'll check that if array at index timestamp is equal to array at index plus one, then we should, we should return true. Otherwise, we need to call the same method again with and increment the index. So multiple had a and index plus one. Now, this is not the case, then we just simply return. So let's see what we did here in this case. First of all, we get, for example, let's use this array. And at first it will be 0. So n dx is not equal to that length minus one. So we enter this block and here we'll check if index is less than array.length one is one. This is the case. Then we should check if array index times ten. So if one times ten equal to ten is equal to four, no, then we should call the method one more time. And now our index is incremented by one. So here our index would be four and then which also is four times ten equal to three. Now, than our index will be at position, at this position. And we do the same thing. Three times ten is equal to 30. Now our condition is satisfied. Then we don't need to do this method again. So we'll just return true. Now in this case, for example, if we use this list, then we not find any, then any element that matches our condition here. So we'll just continue to enter the same method until index will be, eventually will be at array.length minus1 and we'll return false. So this is our method and let's go ahead and use it in our main method. So let me just comment these. And let's print multiple and create an equal to, for example, 12355310. And in this case, that is used this method with this at a and 0. Let's see what will happen. Will get true. Now if we delete 15 will get flows. So this is it. Some simple examples on recursion. See you in the next video. 30. Sorting Array of Strings: Now we go back to our sorting algorithms. The oddity tried them with arrays of integers. Now, we will use arrays of strings. So here, a main method. Let's create an array of strings. Scale it names, and it would be equal to some names. For example, Alix and press. So this is I would list. Now let's go ahead and use the insertion. So let's name it public static, void, insertion of arrays, arrays of strings, called them names. So it is the same as we did for integers with some variation. So we start with our for loop at i equal to one. And we need to define our strength is equal to names at i and j would be equal to i minus one. Now will enter our while loop. While j is greater or equal to 0. And we'll use a method available for us. And it's String class in Java. This decomposed to, we'll compare the key to another string. In this case, it was named at j. If it is less than 0, then we need to swap them. We need to create a string termed as usual, give it the value of names, names of j plus one, and names of j plus one. We take the value of damping. Now. So this is our method and let's go ahead and use it in our main method. And search and with names. And then the for loop to print our elements. We print aimed plus space. We'll get Alex, Chris, and James so they're sorted now. So we used, compared to this method is available in the String class. And we use that. We have the same method here. But instead of comparing with this sign, we used the compare method in the String class and Java. Now let's use selection sort. So let's go ahead and copy it without writing it one more time. Selection sort, and we'll modify it here. So let's go ahead and modify it. Now. You take a strength. And here we set minimum is equal to I will. Okay? And if we need to use the compareTo method, the two strings. And then finally, we have a string array that compared to gay and minimum, it should be less than 0. And then we're done. Let's go back here and use it. So instead of using insertion, let's print insertion sort. And could perform selection are names and print them out. Now we already sorted names, so let's create another at a. Let's call it name. Two will be equal to names and use it here. Selection so that here. Let's go ahead and print them out. We'll get, we just need to print a line here. And the code one more time to get insertion. So it, this is our output and selection, so it would get the same output. So this is, this, this video is just to show you that you can use these sorting algorithms not only for integers, but for strength. And you can use them for characters as well. So you can go ahead and tighten out using characters instead of strings or integers. And not only the sorting technique, but you can also use the searching algorithms with strings and characters to this is it for sorting and searching algorithms? See you in the next video. 31. XML: Hello and welcome back. And in this video we're going to talk about XML and how to extract data from XML using Java. Now, before we do that, we are going to talk about XML in general. We're not going to get into so much detail. However, we need to know what is XML and how we can use it. So first of all, XML stands for Extensible Markup language. This means that it is computer language which uses markup and is capable of being extended. So what is markup? Markup are the notations and symbols that are used to correct in making text and indicate how tax should be displayed. For example, whenever we use the symbols, the closing. And so these symbols are the markup symbols. Now, extensible means that it is capable of being stretched or extended. So in information technology, extensible describes program on programming language that is designed so that users and developers can expand, add to its capabilities. So in this XML language, we can create our own tags. There is no predefined tags in it. And let us just see an example about this XML. And this is an example, as you can see, we can read it and we can understand the information we have here. So first of all, we have a company, and inside this company we have staff and another staff member. As we can see, we have an ID for the first one hundred ten hundred one, and for the second 12 thousand. Now, inside the staff you have some information about dispersion. So we have the firstName, lastName, nickname, and salary. And the same thing for the second member. And let's talk about the syntax of XML. Let's take for instance, this example. So we have this first, firstName and we have low. As we can see here, we have two tags, the start tag and the attack. So whenever we want to write, the antilog will simply add this character right here. And inside tags, we can simply write our text. Now, this thing is called one element. So this is one element of our code or our text. Now, xml is case-sensitive. So whenever we write a start tag, we need to drive the n-type, the same exact thing. So we can just write here capital F. This will not work since XML is like Java, is case sensitive. Now the same thing apply here, lies here, so we can try it. The capital F, firstname here and followed by firstName. So we can see that we have. And so the element type firstname must be terminated by the matching end tag. As you can see. So it is a sensitive and it is important to remember this since our code will depend on it. Now, another thing is that elements should not overlap. So let's suppose I have firstName and let me just copy this and paste it here. And move this to the end. As you can see, it will generate an error saying that the element type lastName must be terminated by the matching end tag lastname. As you can see here, we have last name here and the last name ending tag is after the firstName. So XML checks if this tag matches this one, everything is good. However, in this case, lastName is not like this, it's not the FirstName. And to generate an error. However, if I just based here, nothing will happen since everything is in order. We have the last name, and this matches the opening tag or the starting tag, and we have the firstname here. Now let me just move them back as before. And let's move on to the structure of elements in XML documents. So first of all, we have the company and inside this company we have staffs. And we can call this company the parent element, and the staff is the child. Now, another thing is the Declaration, and this declaration is optional and it indicates the number of version of XML in use. And in this case, this is divergent 1.00. And whenever we use this declaration, it must be at the top. Not even whitespace or command should become before it. So we can just simply add some white space. Here are some comments here. We need to add it. The first-line first thing in our document. And this declaration is also case-sensitive, so it can try XML capitals, as you can see. Now let's move on to attributes. And an attribute specifies a single property for an element. And it consists of a name and a value separated by an equals sign. So as we can see here, we have our staff element. And in the staff we have some elements here, and these are the child. However, we can see that we have an ID, and this ID is an attribute of stuff. As we can see, this ID, this is the attribute name, and this 101 is the attribute value. And it is only retained within the start tag. And as you can see, it is after the name without any comma or whitespace or semi, semicolon, so on. So we have this name equal and devalue followed. After that. Then we close our startup. Now we might have more than one attribute, but we can't have two values for one attribute in the same start that. So again, say 10000 plus something. This is wrong. However, we can have two attributes. Let's suppose I have a number, and in this case, this number is three. Let me just write it. And as you can see here, there is no error and it works fine. So this is basically, this is a general idea about XML, how we can read data from these documents. And in the next video, we're going to create our class, our program, so that we can extract this data using Java. But that being said, this is the end of this video and see you in the next one. 32. Process Local XML: And let's go back to our process local. And this is our Java program here we're going to write our program. So first of all, we need to create our main method. And of course we are going to throw any exceptions that might occur here. And let's just, we can. So first of all, we need to take this file. So this is staffed dot XML and it is stored here as we can see. Now, first thing we're going to do is to create a file and this file, name it File and New File staff that XML. So now our file is here. Of course we need to import it. And as before, we simply import java.io dot file. Now that we have our file staff dot xml, how we can extract data from it. So we start with something that is called document. And if I just avoided, it is in Java acts that XML parsers package. And this defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. So we need to extract data from maximum. We are going to use this document, build a factory. And now that we imported, let me just name it DVF. And if I just write it one more time, Factory. And let me see what methods are available for us. We have the new instance method. And as we can see, this new instance, it is a static method that creates a new factory instance. And this method uses the following. We can see that it uses document builder factory and Java that XML parsers, that document builder factory. And we simply use this method. And now we have our DVF objectives. It is documented in the factory. Now, let me just use this DBF and see what is, what are the methods here. And as we can see, if we scroll down, we can see that we have so many methods. However, we are interested in this new document builder. And this new document builder creates a new instance of our document builder factory. Now, let me just use it. And here I'm going to define document builder. Of course, I'm going to import it. So it will be imported from Java X dot xml dot parsers just as the document builder factory and let me name it. Db will be equal to this. Now. After creating this document will be factory and document builder. I am going to pass our file and this document, so document that we name in document d. And inside this document we are going to use Db dot. As you can see, we have so many methods as well. We are interested in this bus. And as you can see, bias, it parses the content of the given file as an XML document. So this is what we are going to do. We are going to bars our file. And finally, we have our file parsed. And of course we are going to import it. And we are going to import it from org dot three, WC dot dm. And now we're done with passing our file. We have all the data and this D document. Now, let's go back to our document and we'll notice that we have staff. So we are interested in staff here, stuff one and stuff too. So let me go back here. And if I add d dot, we can see that we have some methods. However, if we want to take the elements of these documents, we can simply use the get elements by tag name. And in this case, we can use this method, getElementsByTagName. And we can specify the name to staff. And of course, we can store it in an ordered list. Let me just create our node list. And of course, imported from AHRQ dot W3C dot down. And let me name it Staff list. So now we have our staff list. We have the staff stuff one and staff to stored and unordered list. As we can see. Now, how do we access them? Will simply use nodes. So we have unordered list. How do you access a node list using an odd? Now and let me create a nod and by name ID staff naught. And of course, the element inside this node, let me name it staff ELT as elements. And of course, we are going to import them from as usual. And let me just scroll down. We have here all the three WC DOM that knowledge. Now that we have our element, node and node list, we can start with our extracting data. Now, first of all, we are going to use something that we have used before, and that is the StringBuilder. And inside the string will do. Let us, we, we are just going to define it. And now we have our StringBuilder. And of course we are going to append how many staff member we have. So we are going to speed that up band there. And we are going to take stat list and see how many staff members. So we simply use third guideline. So there are some, some lengthier and staff members. And of course, we are going to come to a new line. And we can use this. And now we're done with our length. Now, let me adjust, extract data from our document. So the first thing we are going to do is to pass through the elements inside this stat list, I'm sorry, denote inside the staff list. And to do that, we are going to create the for loop, so forth. As usual, suddenly at r equal to 0 and ending at staffed list dot length. That length. And the first thing we are going to do is to take this staff node and extract one node from the staff list. So how do we do that StatPlus dot? We can see we have some methods here. However, we are going to take this item, this method item, and as you can see, it returns a node so that i, we are going to extract this item and store it. And now we are going to append to this node, to the StringBuilder, the number. So sba dot append. We are going to append that this is tag number and followed by I plus one, since we're starting at 0, however, we don't want to start at 0. So the staff number 12 and so on. Now, after that, we are going to check if this staff node is an element. And as you can see here, we can see that this stuff as an element. And let me go back. So how do you do that? So f, Note that gap. As we can see, we have some methods and we are interested and yet node type, we are going to get denoted. And Aeschylus's equal to note that element node. So if this is an element, we are going to take this and store it in an element, as you can see. So we created here an element. We are simply saying that if this node is an element, store it and ELT. And how to convert two element and compared the staff node into an element. So this is the case. We can convert it, nothing will happen. It will simply converted from node two element. After that, we are going to create an attribute list. And this is how we can extract some attributes from XML. So as you can see, we have our ID. We can extract it using this method. So the first thing we're going to do is to create a named node map. And of course imported acid that we are going to just initialize it. So I will name it attribute and of course, staff element. So this is our element. And we simply get, get these attributes from this element. After that, we are going to create our node. And this is an attribute node. And here we're going to extract our elements from this node, from this attribute list. So we might have more than one element or one more than one attribute here. So we're going to create a for loop and pass through all the attributes inside. However, it happens that we only have one element or one attribute in this case. And outcomes should work correctly for other examples. So we're going to create this for loop. And in this case, we are going to get the length of this attribute list. And in this case, I'll, attribute node will be equal to the attribute list as before that or that item this case and j. So this is it. Now we have our attribute. We can simply append to this StringBuilder. So as speeded up band. And we are going to append the attribute node. And we can get the node name using the get method. And I'll simply write equal. And of course, I want to get the value. So attribute nodes that get node value. And of course let me just jump align. And we've finished this. So until now we just created our node list, node an element. We took this document and store it and created the staff list. So here we have the staff. As you can see, we have two staff, which is, which has the id 101, and we start with the ID 2001. Now that we have our staff and the staff list with just extract them one-by-one using this for loop. And inside this for loop, we are going to take the item of the staff list storied and staff node. And then this staff node is an element, will simply go to convert it to an element, and then just extract the attributes. And in this case we only have one attribute. That is the idea. However, we might have more than one. So this is why we created our second for loop, and this is the inner for loop. And in this case we are going to just append the attributes we have and we still have to extract the elements. So we have first-last nickname and salary, and we're going to extract them in the next video. So see you then. 33. Process Local XML 2: Now we still have to extract the information of the staff. So we have the first class nickname and salary. And in this case, we can simply use the staff element we created and extract the data from here. So we're going to use sba dot append. And first of all, we are going to extract the FirstName. And then after that, we are going to use staff element and get elements by tag name. So the element is the first name. And of course, the item is 0. And we are going to convert it into something that is readable or stank. So we can use beget, taxed context content. And in this case, it will return the node as a string. And of course, let me store it MS drain first_name. And then I'll simply just used this firstName. So I'm going to append this firstName. And let me just jump into another line. And I'm going to do the same thing for the other. So we have the last name. And in this case, let me just copy this. Basically it here. And this is the last name. I'm going to extract the elements by tag name and this is last. And of course the same thing here, item at 0 and get text context content. And of course I'm going to upended so lastname and jump. Now the second or the third information we go to extract is the, as you can see, nickname. And finally we have the salary. So we're going to append nickname. And of course, string, let me copy it. We have here our nickname and getElementsByTagName name. And finally, we are going to appended so named jump. So the last thing is to add band. We have in the salary. And in this case, we are going to store it and restraint also. So string salary equal to, sorry, let me just copy it. So salary here. And get elements by that beam, salary. And finally, we'll simply appended into our information and to our StringBuilder, salary plus jump. And in this case, let me just, just about what we did here. So first of all, we have our element and inside this element we have some other child elements. So we have our staff that is stored in our, as we can see, staff element. Now to extract the data from this, we used getElementsByTagName. However, remember that whenever we use this get, getElementsByTagName, We are going to get a node list. So he restored them in an ordered list. We could have done the same thing here. However, since we only have one item, we can simply use each item method and extract the first item that is at index 0. And then we are going to convert it into a string using the get text content. So we could have done it another way. So we can just create a node list and let's name it. And of course we can use staff element that get elements by tag name. And of course we can use the FirstName. And so this is our node list. Then we can use and add that item 0 or P8, create our for loop and so on. So we can do the same exact thing as before. However, we already know that our staff document is consisting of first-last nickname at salary, so we only have one of each one of these. So we can simply use this method. So now that we have our good, let me just go ahead and done it. So run as Java application that because you get a tear. And nothing happened just because we didn't print out our StringBuilder. So here we have our first for loop ends here. So System.out.print. Run this code one more time. We are going to get our data. So the first thing we did is as we can see that we just zoom in a little bit. And as you can see here, the first thing we did is to print out how many staff members we have. So we have there are two staff members here. Then we moved on to our staff and extracted some data. So of course we appended. So staff number one here, I'm sorry, I couldn't have just jumped and dying. And let me run this one more time. We're going to get this. So staff one to as the ID 101. So we printed out from this. Now, since we have only one idea, we could have used this at 0 and remove this for loop. Firing squad One more time, I'll get the same result as before. However, if we have more than one attribute, this will not work. It will just simply return for us this line. Now let me just go back here. After that, we just print out the firstName, lastName, nickname, and salary, and do the exact same thing for the second staff member. So we have staff to ID 2001, first-last nickname and salary. Now, this is a quick and simple example about XML and how to extract data from XML documents. In the next videos, we are going to dig deeper into this XML document extracting kind of thing and see you then. 34. Process XML from browser: Hello and welcome back. And in this video we're still covering XML. However, this term would not processing local XML. We are going to go to Google and extract some data from a website. So let's suppose we have, let's go to Google and BBC News. And as you can see, if we go here, so these are the news. And of course, we can simply, if we want to extract data from XML, we can simply write RSS dot xml. And this will be direct us to a page, and this is the RSS feed from the BBC News. So our program should take these news and just extract them and stored them either by printing them into the council or stored them in a file. And we'll see what's going to happen. So let me just create a new file. Will simply a class, I'm sorry, when name it. Bbc News. Now this is our class. And inside this class, as usual, we are going to use the main method and throw exception. Now we'll start with our class. So first of all, this is the URL, and we'll simply store it in a URL. Of course important. But the net that u r, l, then I name it you. And, and in this case, if I want to create this URL, I can simply create a new URL and copy the link from here and here. So now we have our URL. Now how we can use it, we can use the input stream. And in this case, let me name it, I guess. And I'll use URL dot. We have the append stream, I'm sorry, append stream. And this will generate a InputStream. And of course I should import it from java.io, that input stream. Now that we have our data, our XML, we can simply start with Baden document builder factory as before, imported DBF equal to document the factory. And the new instance. Then we are going to create our document builder. Same thing as before. Document DB equals DBF dot and followed by New Document builder. Then we are going to create our document and limit the argument this time. And DB that box. We're going to pass the input stream we just created. Now of course we need to employ these. So import from octet three WC, input from Java, excellent XML parsers. Now we have our setup. We can simply start with extracting data. So the first thing we are going to do is to create the StringBuilder to store data. So StringBuilder speed equal to new StringBuilder. And in this case, I will add some strength here since I can use the SP dot append, however, I may use this parameter, so I will start them in HTML. So let me just create the head entitled to our HTML file. So HTML. And inside this head we have the title. In this case, it is BBC News. And of course I should close the title, then. Close the head and followed by the body. I should happen now, the body. Now this is our StringBuilder. Let us start with our XML. So the first thing we are going to do is as before, BAD node list. And in this case, I will name it items list. Of course, I am going to use the document that get elements by tag name and this name is item. So now let me just import this. And everything is. So why did I write item here? If I go back to our BBC News and right-click, we have view page source. And this is our XML document. As you can see, we have the title, description, link, image, and so on. However, we are interested in the items. And these items are either item, title and description. So we have this title and of course in description, then we have another item with title and description also. So we are interested in the items. As you can see, we have so many items and we are going to extract all of them. So let us go back here and work with our item as before, node, item, nodes and element item, ELT. And of course. Import these two from all the three WC that dumb. Now, after creating our node, last node and element, we are going to create our for loop. And the boundary is the document or document.getElementById. Sorry, we can simply use the item list we created. So item list but get length, right? Unless that get length. And now we can work with our for loop. So inside the square loop we are going to take one note at a time, so we're going to store it in the item node we created. So item naught will be equal to items, list item at index i. Now, after this, we are going to check if this is an element. Now. And the process local. Here. We used this as, as you can see here, we checked if staff node to get node types. So we use the get method. And if this is equal to element node, now we're going to use another method. And we can simply use this item node is instance an element if this is the case. So instance of means that this item note is instance of this element, I'm sorry here, instance like this. And so if this item node is an instance of element, we are going to enter this statement. So this is the case as before, we are going to converted into element a story, the story in item ELT. And how we do, do we do that? We'll simply write element and followed by item.name. Now that we have our item element, we can simply store it and our StringBuilder. However, we are creating HTML. So let us use the, as you can see, we have the title and the description. So is thought the title in H2 and the description in a paragraph. So how do you do that as v dot append H2. So here we have edge two. And of course, after that, we are going to use item element that get elements by tag name. And this name is the title. As we said before. If we go back here, we have inside the item, the title and the description. So we're going to get the title as before, the item at 0 that get textContent. And then I am going to close this tag. So I will simply use close to. Now. After that, I am going to simply use the paragraph tag to create the description. So sba dot append. And inside this, I'm going to create our paragraph tag, followed by item elements that get elements by tag name. And in this case we have description. And as usual, that item at 0. Then the text content. Of course I'm going to close it as well. So here we close that. And now we have our data here. Finally, after finishing from ds, let me just modify this. So here we have our for loop. After finishing from this while loop, we need to close our body and HTML. So I simply append here, speed that, upend our close body. Then close HTML. Now we're done with appending and we are set. So if I go ahead and print out no and speed, we run this. And as we can see, we have here so many words. However, I just got them in one line because I didn't store them in clients here. So after every description or every title, I'm going to jump aligned. And of course, and if I round this one more time, we are going to get these. Now. Why did I stored them like this? Why did I create an HTML? So let us just get this data and store them in an HTM L phi. So if I go here before creating our document, Linda factory document builder and document, we can create our file that we are going to store this data. And so after input stream, I am simply going to create our file. Let's name it news, and it will be equal to nu. In this case, I will name it used that SDM. So HTM stands for HTML. Now that we've created our file, I need to oppose it, of course, from java.io. After that, I am going to employ the FileWriter. We learned about this file writer before. So we are going to impose phyla file writer. And after that, we are going to create a file writer. And inside this file news, then we are going to create the buffered writer, BW. And of course, new buffered writer with the parameter of f w has defied writer. Then finally, we are going to create the print writer, and this will use this to extract, to write data into our file. So Brin writer, PW to be equal to new print writer and Vw as a parameter. And now we set. 35. Process XML from browser 2: Now, after setting our file in which we are going to write our XML data in, we are going to use it here after appending the ending tag of the body and HTML, we're going to use the BW, that brand, Sb. Then we are going to close it a p-type due to the close. And now if I run this code, we can see that we have here inside our project, we have news that SDM, so this is our HTML file. Now, if we click on it, we are going to get the title and the description of all of the news. So we have the first one title description than the second title description, and so on. So we have all of the news here and our BBC News. By now. This is something that you can do how now this is changeable. So every time we click, so if I run this code, now, I'm going to get this news that HTML. However, if I run this tomorrow, I am going to get different titles and descriptions. It depends on the time Iran discord. Since then use change everyday. Now we can add something here, and this is to link this news that HTML to the browser. So one way to do that is to use the desktop. And we have a method that is called gad desktop. And inside this method we can browse and we're going to brass the news. However, we need to convert it to URI. So used to URI. Now, if I run this code, it will redirect me into browser. And inside this browser, I'm going to get these titles and descriptions. So this is the news of BBC. I just take them from the XML file or a document, then studied them in my own file as HDMI. Now, of course, I might use this from here or generate a browser using this method right here. And let me explain just a little bit about MDM. And for those who don't know. In HTML, we have predefined tags. For instance, edge two is heading two, and B stands for paragraph. So whenever we want to create a new HTML file, first thing we're going to do is to create the tag HTML, then head, then the title. Of course we are going to write a tighter then close the title followed by the head. After that, we are going to write our body. So we append this body tag. We wrote everything we want here from H2 and paragraph. Then after finishing, we just close the body and close the HTML. And now we our SDM magnified. Then we stored it in the file. We created newest HDR. And of course, we just browse it using this method right here. And this is it for this exercise. We can use this program whenever we want to check the news. We can just simply go to Eclipse and run this code. And we are going to get the news directly. And this is it for this video. I hope you enjoy this program and see you the next one. 36. Catalogue Example : Now let's solve another problem. And here we have a catalogue. So this is our file Catalog dot x m n. So this is our document. And inside this document we have catalog. And inside this catalog we have CDs. So as we can see, in every CD we have the title, the artist name, country, company, price and yield. Now what we're going to do here is that we are going to go over all of these CDs. So we have so many CDs and regard to extract the cheapest CD and the oldest one. So we're going to extract two CDs. One that is the cheapest one, and the second is the oldest when all of these cities. So as we can see here, we have 1971, we scroll down, we'll find 19881987 and so on. So let's go ahead and create our last here. So here we have our sum L, And I'll create our class by name it catalogue. And let's start as usual. So the first thing we are going to do is to create our main method. Of course, throw the exception. And then we can start with output. Now we have our file here, so will simply create a file import, imported from java.io. And let's limit phi equal to phi. And inside this file we have all filename catalog that eczema. And then after that, we can start with creating our document builder factory. So document vendor Factory imported from Java extra the XML. And I limit DVF as usual and document the factory, that new instance. Then we are going to create our document builder. Let's name DB. And important of course. So dB it will be equal to DBF, that new document builder. And finally, let's create our document. And this is our document and we need to import it from three WC that down. Now we have our document and we passed our file returns, start with our extracting data. So we have denote list as usual imported from here. And let's name it, note or an L. So this refers to node list. And we also have a node, and we're going to name it node with a lowercase n. And finally we have our element as usual imported and I limit ELT. Now let's go back to our catalog dot XML document and let's see what we are going to do. So we are going to print out the artist's name and the title of the CV, followed by the year in case we are going to extract the CD with the oldest year and followed by the price whenever we want to print out the city with the cheapest price. So we are going to create an ArrayList of each and every one of these elements. So we are going to create an ArrayList for title, Test and price. And gear will simply end grow, and ignore the country and the company in this case. So the first thing we are going to do is to create an ArrayList imported. And we are going to take it as a string. And so this is the name. And of course, initializing a new array list. And let me copy this and paste it. So we have four ArrayList. And in this case, one for the name. We said that we need one for the title and one price. And the last one is for the title. And this is for price. And finally, this is for the. Now, after creating the array lists, we can begin with our code. So the first thing we are going to do is, as usual, start with a for loop to pass through all of the elements or all of the CDs in this case. So far. And of course, before doing that, we are going to indicate that we need all of the CDs. How do we do that? We'll simply assigned to denote list db dot, get elements by tag name. And this is a CD. Now we have all of the CDs inside our and l, and we can start extracting them one by one. So the boundary here is an L dot get length. So the length of our node list. Now we can start by assigning to denote an item at i and check if this node is an instance of an element. So f node, instance of element. If this is decays, then we are going to precede with R code. And in this case we are going to convert this node into an element and store it in ELT, so element. And here we're going to convert the node we have. And finally, we can get the data now. So inside this node we have a CD, and in each city we have the title, artist, country, company, price and year. So we are going to just take this title, for example, and store it in our ArrayList. That is title. So how do we do that? We're going to take title that pad and we're going to add. Elt dot get elements by tag name. And inside this tag, we are going to indicate that we need the title. And of course, we need the first item in this title. And this is at item 0 and get the extra content. Now that we have our title, we can do the exact same thing for all of the other elements. So let me copy and paste it three times. And now we have our as we, as we said before, we have the title, we have the name. And we also have the Bryce here. And let me just modify the US. So here we have the, we said title and we said the artist and price. And finally the year. Now, let me be just one thing. Why did we add this item at 0? Remember that whenever we use getElementsByTagName, this will generate, generate for us a node list. And in this case, we can clearly see that we only have one element inside this node list, and this is the title. And in this case, we can simply extract the first element. Since we have only one element, we can extract the item at index 0. This is the first element, and this would work here. Now we have our data and they are stored in the ArrayList. We then proceed now with our code. So after extracting all of these data from Catalog dot xml, and now we can use them outside our for loop since they are stored in the array lets list right here. So now, let us begin with our comparison. Now we said that we want the city with the lowest price. So we're going to take price and compare each and every price here with the next one. And whenever we find the minimum, we are going to just print out the title, the artist price, and then proceed with our oldest city. So first of all, let us begin with the prize. So let me just create an integer and this will be the rise minimum price. So price. And in this case, I'll assign an integer that max value, that is a constant holding the maximum value and n can have. So since we are going to check for a minimum, it is better to start with a very large integer. Now, after that, we are going to pass through all of the elements here and see which is the element with the lowest price. So let's create a for loop and it will start with i equal to 0 and equal to Bryce dot size. Now we're going to pass through this and check if men. Price is greater than the minimum price here. So price that get at i, f, this is decays. Then let me just start the index here. So index of the minimum to be equal to 0 at first. Now, we going to check for this price ArrayList. And if we find any element that is smaller than this minimum, we are going to assign this minimum to be equal to the element we just found. So how do you do that? So, men price should be equal to price, but get at i and we're going to store, buy. So index will be equal to i. Now we have our index and our minimum here. Let me just see what is. So here we have, so we have here the price that is a strength. As we said, we created an array list of string and our mid-price is an integer. So how do you deal with that? We can simply convert this string into an integer by using the Integer.parseInt end. And in this case, this is the parameter. And the same thing will apply here. So integer that bus and and here we have capital M. And now we get, so after exiting this whole loop, we're going to have our minimum and we are going to have the index. So since we need to print out the title, artist, and price, we can simply print out the element of title at index. This index. And the same thing for artists and pricing. So we can print out here, save that the CD with the lowest price and get the price that got at index. And we can simply say that the CD with the lowest price of the price is created by and save the name so is created by. And we simply going to add. So here. Let me just create a new string here. And we're going to say that it is created by the artist. And this is the name.gov. And we'll simply say, I will end it and the name of the city and the name. So the name of the CD and title. And here I have index. So now let me just, let me just, just this year we have an array list, name, title, price, and we used the price. We used, I'm sorry, I need to add a plus sign and we get, and if we run this code, we're going to get a number format exception. So here in our catalog, we had some, as you can see in our catalog, that XML we have the price as double and we used integer. So one way to deal with that is to simply use double. And then we are going to double. And same thing applies here. Double.parsedouble. And we've got, now, after finishing this, we simply just going to run this code one more time. And we got the CD with the lowest price of 7.20 is created by simply read and the name of the city is picture book. Now if we go back to our catalog and search through all of the CDC, we can find that this information is correct. We still have the year and we'll do it in the next video. 37. Catalogue Example 2: Our next task is to find the CD with the oldest or the lowest tier. So let's go back to our code. And as we did here, we're going to apply the same exact thing, however. Now with our God, I'm sorry, yielded a set of price. So let me just copy this and paste it one more time here. And change this. So minimum year and x two. And of course, we need to change these also here. So we are going to check for our here. And you've got gas. But instead of passing it into a double, we're going to pass it into an integer. So Integer.parseInt. Also the same thing here, Integer.parseInt. And the final thing we're going to do is to change index two and year. Now we're good. We can actually give us, since our price here are tests entitled are, have the same exact length. So we don't need to really change it, however, we just modify it just so we can understand what we're going to do here. Now that we have our minimum and the index, we can simply write also as we did here. So let me just copy this one more time and paste it. So the CD with we can say actually the oldest city we can say is created. And and by, we can simply remove. So we're going to get the oldest city is created, then the year by the name and the name of the city is, and titles that get. So here we need to change the price to the year. And we are good. If I run this code one more time, I'm going to get the CD with the OS price. This is the first print statement recreated here. Now the second print statement is the oldest city is created in 1985 by simply read and the name of the CD is picture book. Now, notice here that they are the same, so something is strong. As if we go back here, we can simply notice that we use the index of the first one. However, we need the index two and run this code one more time. The oldest city is created in 1968 by Otis Redding and the name of the city is the dock of debate. Now, we have our information, we extracted them from our catalog, that XML. And if we go back to our document and search through all of these CDs here, we can simply we can simplify that all of these information are accurate and correct. So the lowest price is 7.20 and the oldest one is 1968. And this is basically for this example. If we want to modify some of these, we can simply list all of the companies. So one way to do that, we already have ArrayLists. And in this case we can create a new array list just for the company name. So ArrayList, company with two new ArrayList. And actually, if we specify the type here, we don't have to specify it one more time here in the ending tag. So we can simply remove this and it will work correctly. So now we have our company. Let us add the information of the company inside this ArrayList. So company that, that we're going to add element that get elements by tag name. And the tag name is company. And then that item at 0 and convert them to a string using the get textContent. Now that we have our company names, we can simply create a for loop and pass through all of the company names. As we said, we can use the company size and print out company. And let me just add here company i plus 12 points and then I simply print out the company at index i. And before this we can simply companies. And now if we run this code one more time, we're going to get all of these companies. So we just now we have the company's company one, Columbia, company three, RCA, BMG, CBS, and so on. So here is a list of all of the companies inside this catalog, but XML. Now. So whenever we have an XML document, now we can modify it. We can take the inflammation, extract whatever we want, and use them however we want. And this is it basically for this example. See you in the next video. 38. Company Example: Now let's look at another example. And in this case we have an XML file that is company. And inside this company we have some employees. As you can see, we have the first employee, we have the firstName, lastName, and the salary. So we have gross and net inside the salary. Then we have the second employee, and so on. We have four employees. Now let's go and create our class, and let's name it company. So this is our class. As usual, we start with our creating our main method, throwing the exception. So Rose exception. Then we can start with creating our file. So 55 to a new file and poor defiled. And by name is company. That XML. Now that we have our fine, we can start with creating our document Builder Factory. Imported DBF equal to document, build the factory. And in this case, the new incense as usual. Then let's move on to creating our document builder. So bolted db equal to DBF that you document filter. And finally, we can create our document imported from o to three WC.com. As usual, esteemed Document DB, that bars our file. Now we have our document, we can start with extracting our data. So the first thing we are going to do is as usual, create our node list and L. Then of course we need to import it. Then after getting our node list, we can create our node import. Don't forget to import it from Ogden PWC dot-com and not from that sun.org and so on. So this is where you have Bartlett and the element imported also ELT. Now we can move on. So the first thing we are going to do is to create the node list and l. And in this side, this node list document that get elements by tag, name. And we are going to get, if we go back to our company, we're going to get the employee. And in this case, we have employee. And now inside this node list, we have the information of the employee such as firstName, lastName, and salary. Now let's move on. So our loop will start at 0 and end with and yet length. Now, we are going to assign the node two. At item i, then of course we're going to check if this node instance of element, this is the case. We can proceed with our code. And now, if this node is an element, we can simply converted so element and followed by denote. Now, let's go back to our company document and we can see that we have first, last century. And in this case, we need to create an array list to just store these data inside our arrays. And in this case, you can create them here. So array list, and this list is a string. And in this case let's name it firstName. And of course array list. And the second one is the lastname. String also last name equal to mu Array List. And define an array list will be, we have actually two, as you can see here, we have one that is an integer and the second double. So let's create them as integer and double. So array list of integers. And in this case, I need to use the wrapper class here, sorry. And the name of this is gross and simply initialize it. And the last array list will be of type double. In this case, it is the net salary and initialize it as usual. Now we have our array list, and now we can store our data inside these ArrayLists. Now let's go back to our element. And inside this element we can store the values. So first name, but add, we're going to add the LT, but getElementsByTagName and the type name is firstname. And after getting this firstName, we are going to get the first item. So that item, I'm sorry. Here, we need to just modify something to just add the extra parentheses here. And after finishing From this, we can simply let me just actually here, we just add in something and it is not necessary. So item at 0 and that get text content. And we've got now the second is the lastname. So lastname, but add. And inside that get elements by tag, name, last, name, that item. And of course closed that item at 0 as usual that get text content. Now, after finishing with the first and last name, we need to move on to integer and double. We have gross. And now to create our Actually to add our element into the array list of integers, we need to have it as an integer. So whenever we need to, we want to add an integer here in our growth. You can simply say gloss that ad. And first thing we're going to do is use the Integer.parseInt method. And inside this method we can simply use the plt.plot element. And in this case we need to get growth and the item at 0 and I get text content. So what did we do here? We just got the elements by tag name grass, got the first element and converted into a string and then store it and an integer after converting it to an integer also. Now the last thing is the net dot. And as usual, double dot parse. And we can simply use ELT but get element as before, and that item at 0 that get textContent. Now, I think we got, if we go back here and find that we have class net, first name and last name. Now, we have all of our data here. We can work with them as we want. So our goal here is to find the employee with the lowest, lowest salary and with the highest salary. So we need to find to employees. And to do that, that has created, after finishing from this for loop, let's create. So integer max it will get, it will have an integer that meant value. And the minimum will take integer that max value. And these two integers, Max and meant by responsible for finding the highest and lowest gross salary. So let's go ahead and start with our code here. We have integer min and max. And we're going to pass to the grass elements in the grass list. And how do you do that? We simply assigned to grasp size and start. So let me just actually create two and the indices outside, so low equal to 0 and high also equal to 0. Now f, our grass that get at i is less than hour minimum. We need to change our minimum. So minimum will be grass dot get at i and our low will be equal to this i. And of course we need to check for the maximum. So f grass dot at i is greater than the maximum. We're going to assign max to be equal to grass grasp get at i. And same thing will be equal to I. Now, after finishing from this, we're going to get our low and high. We can simply print out after finishing editing this for loop, we can print out our employees. So to do that would simply use the System.out.print method. And we're going to print firstName and first_name.gov at low. Then we're going to add space than the lastname, but get low also. And we can add the employee with the lowest gross salary. And we can specify it. And of course, the cross but gad at low. Now we're going to copy paste this same exact aligned here. However, now we need to print out for the high lastName act I also as the employee with the highest salary of, and of course print out grass dot get high. Now if I run this code and let's see what we are going to get. We're going to get Stephen Colbert is the employee with the lowest gross salary of 340. Cristiano Ronaldo is the employee with the highest gross salary of 300 thousand. Now if we go back here, we can simply notice, since we only have four employees, you can simply notice that this is 600. If we go down 400,300 thousand, so this is the highest and 340 is the lowest. And these information are correct, as you can see here. Now, another thing to do is to just compare the net salary instead of the gross salary will be the exact same. However, we are now dealing with doubles. So instead of creating our integers ij, the minimum and maximum, they will be doubles as double and compared them than finding the low and high as usual, then simply printing out the first lastname. And of course he here we need to change from grass to net two actually, let me just do it quickly. So here we need to have double, double Max and then we're going to compare the net. Of course here, we also need to compare the in that and of course changed. And here also so. And finally net. And after finishing from here, we need to simply modified here also. And with the lowest net salary and highest net salary. If I run this code one more time, I'm going to that Stephen Colbert is the employee with the lowest net salary of 327. And Cristiano Ronaldo is the employee with the highest net salary of 1881000. So if I go back here and let me just check one more time. Here we have the net salary is 378. We have 440. And we can see that this is the lowest one. And the highest is 288 thousand. Now, let me just change the net salary. Hurry here to be equal to 44. And in this case, if I underscored one more time, we're going to get Richard Saul is the employee with the lowest net salary of 44. And we can notice that this is now the lowest net salary instead of this 1227. And this is it actually for this example. Now, we learned the basics of how to extract data from XML using Java. Now, whenever we have an XML document, we can apply the same exact thing as we did here. And we are going to get our data. We can store them in ArrayList, can store them in a file or an IRA or anything we want. So this is at basically and see you in the next video. 39. JSON: Now let's move on to JSON. And first of all, we will learn what is JSON and how do we read data from this type of file? So as we can see here, we have the menu that JSON document. And JSON stands for JavaScript Object Notation. So just let me go ahead and try to down here. So it stands for Java script object notation. And it is a self-describing and easy to understand. So as you can see here, we have Menu and inside this menu we have the id value and pop up. Inside pop-up, we have these different values up and down, and up and document. So let us go ahead and dig deeper and do the syntax of JSON. First thing we are going to do is to take one object at a time. So let's suppose I took this. We can see that we have the value here or a type, and it is followed by a value. So as we can see, we have the name, and in this case it is the ID. And it is in double quotations, as we can see, and followed by the column and then the value of this NAPE, so ID file. And then we have also the name. In this case it is, it is called value and the value of this as file with capital F. Now, everything in curly braces is actually an object. So whenever we define curly braces and write some code here or some lines, these are one object. We can assume that this is an object. Now, whenever we write square brackets, this indicates that we have an array. As we can see, this contains three items, so this array contains three items. So this is object one, object two, and object three. Now let's move on and create our Java program so we can extract this data or information from our JSON file. So first of all, in order to use a cent and use the methods available for us in JSON, we have a library, entities, a specific library, and external actually does not built in Java, so we need to download it. So let's go ahead and go to Google and download.com. And here we have download Jason that jar. I'll leave a link or a lived the whole library and the description. Now we have our JSON. Library. So let me go back here to our code. And of course, we need to extract it from our downloads. So after having our library downloaded, you can go to our project, and this case, this project one, right-click belt path and configure build path. Here we have our libraries and we can see that we don't have any path, any added libraries. So I'll just add external jars. And let me go to desktop. Inside desktop we have deep jar files. This is JSON, so that we just added apply and close, and now we set so we can now use JSON. Let me just create a new package specifically for JSON. And in this case, let us create our first class and we'll name it menu. So now we have our class menu and our File menu, that JSON. So let us start with this file. Let's extract some data from here. The first thing we're going to do is, as usual, create our main method and of course through the exception. So whenever we throw an exception and types, type throws exception, this means that we are going to throw any exceptions that might occur here. And of course we don't mean the adders or cern, we just mean maybe file exception or anything like that. So file equal to new file. And in this case we have menu that JSON. And of course we need to import it as usual from Java to the IO. Now we can start with our jason. So the first thing we are going to import the file input stream. So file input stream and imported, let's name it T, o. And in this case, or maybe I can name it FIOS as file input stream and created as usual initializer. So input stream, and it will take the file as a parameter. Then this will be followed by the JSON token. And let me just write it. And then we talk about it. So let me import it. And as you can see here, let me just type Tokyo enter equal to new JSON Falconer. And in this case, let me just take it as an input. Stream. So this is what we want and inside this building to write FIS. So this is defined input stream. And now that we have our jason toner, We can actually read data from the stock inner. So let me go back to our menu, JSON. And he, as you can see, let me just delete this. And we already said that whenever we have curly braces, This is an object. Whenever we have square brackets, this is an array. So here we can see that this is an object and this is one object. How do we deal with that? We can simply create a JSON object. So JSON object, and this will name it, will name it object that will be equal to new JSON object, and it will take the whole token URL as a parameter. This is how we can extract data from our JSON file if we have one object. Now, let we started with this. Now we have our whole object inside this object. Now after that, let us see what, what we already have here also. Now we can see that we have another object, and this time we have the name that is menu. So how do we deal with that? If we want to extract this data from here, we can simply create another JSON object, since it is also an object. And in this case, let's name it. For example, i. And of course, this will be equal to objects. And we're going to use, Sorry, I have object. And if I JSON, we have that get JSON object. So this method will get us the object and with the name menu. Now, we also have, let me go back and as you can see here, we have the ID, we have the value, and we have the pop-up. So let me just go back here and name it. This one will be the ID. And of course let me just create also adjacent object for the value and the popup. Now we got now let us extract some data. The first thing we are going to extract, let me go back to our menu, that JSON. And we can clearly see that we have the ID and we have the value that is defined. Now, if I want to extract the value of this ID, I can simply print out id dot, get, string, and decay that is PID. Now, as I said before, I could have just created this JSON object as o and use it. To extract the data. So I can remove this. And here I can write o. And now if I go ahead and run this, I'm going to get phi. Now if i want, let we just go back to Menu that JSON. If I want this value, this capital F five, I can simply return GetString. And in this case I'm going to return the value and run. We're going to get a file and file. Now, as we can see here, we have our pop-up and inside this popup We have one object. And inside this object we have the menu item that contains an array of objects. So till now we have this object and this menu item as an array. So let me just go back here and create what we said. So we need a JSON object and we'll call it As we said. Let me go back and one of them is pop up and the second one is, sorry, let me just delete this. And your Burke. So here, one of them is proverb and the other is menu item. So JSON object, pop up and Jason per day. And this is menu item. Now, after creating these, we can simply take this pop-up and Dave, the object of this pop-up, how do we do that? We can simply write that we need pop-up to be equal to dot. Get JSON object. We're going to get the JSON object in which we have pop-up. After that, we are going to take the array of menu item inside pop-up. Menu item will be equal to bump up the get JSON array. And we need the Menu item. And now we're done. We have our menu item. We can create a for loop starting at 0 and the menu item that length. So menu item that length. And inside this, we can simply get the value of the menu item. So let me just create a JSON object for devalue. I can actually create it outside. So JSON object, value and value will be equal to menu item dot get JSON object. We're going to get every object here. So this is the first, second, third object. So at index i, then simply printed out. We're going to print out the value to get strength of this value. Then it's space than value that getString of unplug. And now regard, so we have devalue and on-click. We're going to get both values. And now if I run this, we're going to get file, file as before, new create, new document up and up and document, close, close document. So as we can see here, we just extracted these values from here. And this is it actually, this is a general idea about JSON and how we can extract data from JSON using Java. Now in the next couple of videos, we are going to dig deeper and to JSON. And we're going to get much more complex examples and learn how we can deal with each and every one of them. And we're going to see that they are the same. However, we need to understand them and deal with them carefully. And this is it for this video. See you in the next example. 40. Wordpress Example: Now let us solve another Jason example. This is wordpress dot JSON. And this simple example, let me go ahead and create another class in our JSON package statement, word press. As usual, start with creating our main method, throwing the exception, then starting with our file, of course, importing it. Then this is wordpress dot JSON. Now that created our file, we can create our JSON token. And of course we're going to start with our file. Input stream. May metafile new screen with the file as a parameter. After that, we are going to create the JSON token are important. So this is all that JSON to the library we created earlier. And of course, let's name a documentary that will be equal to new jason token and with FIOS as parameter. Now let's go back to our WordPress dot JSON and see what we have here. So the first thing we noticed is WordPress dot JSON starts with an array. As we can see here, we have the square brackets. And to deal with that, we are going to create an ad a year. So if I go back to our menu.html and see. And so we're going to notice that whenever we created the JSON object, this is an object since it started with the curly braces indicating that this is an object. However, here we have the square brackets. We need to create an array. And to create this, we can simply create this JSON. And let's name it. At a, it will be equal to new JSON at a token URL as a parameter. So as you can notice, JSON or dealing with JSON in Java is quite simple. It doesn't take a lot of time, and it is straightforward. So he recreated our array. Now let's create some objects. So I just named them object one and object two. So object, object one, object two. Now, of course, let me go back and let's see, here we have. For example, let's just print out the ID and the date. And let's see what do we have here? We also have the categories and the tags. So let us print. We said that we're going to print ID, date and categories and tags. As you can notice here, categories and tags are arrays and ID and date objects. So here we have object one, object two, and here we have JSON object. We didn't not import it. And we have a case as we said. So we have the categories and we also have the tags, I think. So. Yeah, basically categories and tags. Now, let's go ahead and create our object transfer object one will be equal to a dot get JSON object. We have this master method. And as we can see, we need to add an index here. So if I add index 0, this will give us the first object. And inside this array, as we can see, this is the first object. This is all of this is the first object. So here we have, we are opening the curly braces and he will closing it. And as we can see, this is only one object, so all the data here are inside this single object. Now let's go back. And if I go ahead and print, Let me just print the ID. If I want the ID, you can simply use. Let me just indicate that this is the idea and use object one. As we said, this is object one dot. We can get end. And using this method, we are going to get an integer. And we entered the id as a parameter. And now if I run this, I'm going to get the idea, and this is 157538. And this is exactly the idea. Now, if I want to extract also the date, I can simply write that the date is. And use objects weren't yet. Let me go back. So this is a string, so that get string and the parameter is a string of date. Now let's run. We're going to get the ID is and the date is 2017, and of course the time. So this is basically for extracting objects from an array. And here we are using the getInt and getString methods since we know what we're going to extract regard to extract the ID and date. And this is a helpful method in this case. Now let's go scroll down here we have our categories and tags. So let's create our category first. So the first thing we're going to do is let me. Just go here and category will be equal to, as we said, object one dot get JSON array. In this case, we're going to get the LDA and whatever we have the categories. And now we created our day. If we want to create the tags also tags object one, dot get JSON adds a and b. We have tags. Now since we know that we only have one element inside this ID, I can simply use, I can simply print it out actually, so categories. And I can use the dot get method and we're going to get the integer at 0. After that, we are going to print out the dance. And in this case, we know that we have more than one tag. So we're going to create a for loop starting at 0 and ending the length. Sorry, tags that length. And this is basically I can now print it out. So tags that get end at i space. Now if I run this, I am going to get the ID is, the date is, and categories we have 6132 and then followed by the tags, we have two dogs, 17, 986 to 98. And this is a general idea about this example that we go back here and see what we have left. So for example, we have, so these are the same as before. This is modified or you can use it as ID or a date. However, let's use this. Here we have the Guide and inside guide I have bended. So we know that whenever we use curly braces, this indicates that we have an object inside. So we have object one that has created a JSON object, since we have an another object in this case that we just name it, rendered. And rendered will be equal to object one dot get JSON object. And in this case, the adjacent object is the guide. And let me just try it. Guide. And after that, I can simply use a printed out. So rendered equal to rendered that get, let me go back here and rendered. We can use the GetString string and the key in this case is rendered. And now if I run this and let me first. So here we got rendered and the value of rendered. So this is it basically. And this is how we can use JSON and extract data from Jason and use them and our Java program. Now in the next video, we are going to take a look at another example. And it is a little bit more complicated, but the same idea or the same concepts still apply to every adjacent document. So see you then. 41. Glossary Example: Hello and welcome back to you Jason example. And in this case we have glossary that JSON. And this is our file. And we can see that this example is a little bit more complicated than the others, since we have some objects that are nested or within other objects, as we can see here, we have the list. Inside the glass list, we have an object, and this object is inside the last div, and glass div is inside glossary and so on. So now let's create our JSON or Java file. And this is our class. Let's name it glossary. And let's start as usual made and throws the exception. Then start with our phi, phi equal to o and glossary that JSON. And now we can create our JSON token. And this is equal to nu Jason. And I'm sorry. The we need to create our file input stream. Totally forgotten. And input file input stream. And then after that, we give this Jason talk, this FIS as a parameter. And after that we can start with our JSON code. So the first thing we can notice here is actually creating the JSON token or is that n, our glossary, we have an object. So we start with creating the object. First thing is JSon object. And in this case, let's name it the object equal to new JSON object. And it will take the document. After that. We can create our other objects so we can see that we have the glossary object. Then we have the cluster object, glass list object plus N3 and gloss deaf. So let us just create them here, all of them at once. So we have a glossary. Then the loss, followed by the glass, then the lawless and tree, and finally the glass, deaf. And so these are our objects. And our goal is to print out some of the elements here of some of the data. And we need to print out this at a. So let me just create this array and JSON. A day. And plus c also. So this is basically now we can start with extracting data. So the first thing we are going to extract is the title. As we can see here we have the title. So this is after creating the object. First thing we're going to do is to take glossary to be equal to the first thing, the object and USD to get JSON object. And we got to get a glossary. And after that, after getting this grocery glossary weekend, simply print out the title, the title as and glossary. Of course. Glossary. And get, we are going to get the string with the key title. And if I run this, let's check. This is the title as example glossary. If we go back here, the title is actually example x, so this is correct. Now after creating this object, we can notice that gloss live is another object. So we can use glossary and extract the objects from glossary. So we have plus f. In this case, it will be equal to Glossary dot. Get JSON object. And in this case we have, let me go back and try it correctly. We have glass death. And this is actually glass div. Now we have our object plus live. If for example, for one extra, extract the title. In this case, I can simply print out the second title as and the GetString and title. Done. We're going to get the second title is S, and this is the case here. Now we can also notice that we have another object here that is lossless. So we'll do the exact same thing here. So plus L2 will be equal to the dot get JSON object. And we have plus. And let me just prints something from glass list. Actually we don't have anything inside. Let me just check. Here. We need to create another object that is for the gloss entry. And in this case, glass. And three will be equal to glass list that get JSON object plus N3. And after that, let's go back here. We can see that inside glass and two, we have ID and in glass term, let's print them just to make sure that we are on the right track. So we got to print. The ID and use LAS and tree dot get. And let's go back here to check the id is a strength of get string. And this is the idea. Now run. We're going to get ID as g, m, l. And this is correct. And the other thing is, let me just use it here. And we need to extract bluster. So turn and use glass and one more time. But GetString as usual, since this is a string. And last year round, we're going to get GlusterFS, Standard Generalized Markup language. And this is also correct. Now we got to a point where we are extracting data actually from this phase. Now, we can actually see that blast death is another object. So we already have here glass death as an object. And we can simply do the exact same thing as before. So glass, the will be equal to plus and treat that get JSON object and in this case glass. And let's just extract something from you. Let me see what's wrong. And change type from, I'm sorry, here we need to create an object, get JSON object. And now we're good. Inside a glass nerve. We have the para. So let me just print out as and plus the getString. And in this case, we are simply going to print out whatever we have Empire. Run this code. We're going to get a meta markup language used to create markup languages such as notebook. And this is also correct. The last thing we are going to do is to use the array we created earlier and print out g, m, l XML here. So we already have this. So this is glossy also, it is an update and let me use it here. So glass C also, and in this case, it will be equal to a plus the get JSON at a. And in this case, this is plus C also. And let's create a for loop to pass through all the elements inside the array, starting with i equal to 0 and ending with glass. Also that length. And of course we can print out plus C also. And, and we're going to get the string at index i. And let me just print out here just to know what we are gone. And so this is a plus c. Also at a. And of course, let me adjust this and some whitespace run one more time. We're going to get glossy also at a GM L and XML. And this is basically, this is considered as a complicated JSON file for extracting data and Java. And it is the same idea actually as before. We just need to follow the road recreated. So whenever we see curly braces, we create an object. Whenever we see a square brackets, we create an at a and so on. So now we extracted some of the data from this file, and this is it basically for this video. In the next video we are going to do one more example. So see you then. 42. Quiz Example: Now let's move on to the last Jason example. And in this case, we have a quiz that JSON file. First of all, let me just create our class. So this is our Java class as usual. And we have quiz, sorry, it just refactored key name and finish. And as usual, create a main method, the exception. And then create the file. So if phi equal to buy and who is the adjacent, then VHD file input stream, stream, and then create the JSON token. And in this case, let's name a token equal to new JSON token a and phi S as parameter. Now if you go back here, we cannot say that this is an object. So the first thing we're going to do is to create the object to JSON object. Object equal to new JSON object. I'm sorry, here we can simply use detox center here. And we're good. So now we have our object. This is our main object. And if we go back one more time, you can see that we have this. This is the first object or the first element inside the object, and this is the second one. So how do you deal with that? We can simply create a quiz object. And inside this quiz object, we are going to take from the main object to get JSON object and we need the quiz. Then we have also, as we said, we have the export. And of course inside the quest we have question one and question one And the math. So inside the quiz we have spoil and math. And in this case we have question one and sport and question one and question two, m. So we have spurred and q one, q two. And of course, now that we have our JSON objects we can start with is equal to the get JSON object. We're going to get the JSON object with sport. And After that, we can get, so now we're here and the sport. And inside sport we have Q1, that is also an object. So Q1 will be equal to spot the get JSON object and Q1. Now we are inside Q1, We have a question and options and followed by the answer. So let me just dive or print out the question. So as we said question as d first. So we can simply write the question as. And then we can write for a huge Q1 that get sprang and question. But now if I run this, I'm going to get the question. That is, the question is which one is correct team name in the NBA. Now after that, I can simply also create an addict since these options inside this array. So Jason, and let me name it options equal to Q1. You get JSON array. And we have options. After that, we can create our for loop starting at index i equal to 0 and ending options that length. Now we can simply print out all of the options. So we have option i plus one and followed by the option itself. So plus option that get string and at index by run this just to check. We're going to get, because in S, which one is correct him name in NBA, we're going to get default options after that. Now we can print out the answer. In this case, the answer will be simply answer as and followed by the answer itself. So Q1, that gets drank. Announcer. And now if I run this code one more time, we're going to get the answer as Huston rocket. Now after that, we also have the question one and question two, n, the math. So let's go ahead and create our math object. Mass will be equal to, as we said, was the get JSON object. And in this case we have math. And after that, we can go back to the math. We can see that inside math we have two objects, that is Q1 and Q2. So we can simply create Q1 will be equal to a math dot, get JSON object as Q1. And of course, q to be equal to the same thing adjacent at Q2. After that, we can work with each question at a time. So the first thing we're going to print as, the first question as and then followed by the Q1 that get, sorry it get string. And we're going to get the question itself. After that, we are going to store in and add a lot of options. So options, we'll be, I'm sorry, here we are going to write Options Q1 dot get JSON array at Options. And then after creating this at a, we can simply create our for loop as before, starting at 0 and add options that length than printing out each option. So options that GetString that index i that we're going to print out the answer. So the answer as and of course followed by P1 dot GetString uncertain. So now the fire and discard going to get the following. So the question is, which one is correct him name in NBA? This is the first part. Second part the question as five plus seven equal, we have 10111213 and the answer is 12. And in this case, we still have the question too and math. So I can simply write the second question. And to be Q2 that GetString question. And in this case, after writing the question, we can also use the same option. And it will be equal to Q2, the get JSON, add a. And of course, options that you need to create before loop as before. And this same boundaries. Options that length. Then print out all of the options that gets drank. And after that, we can write the answer or print out the answer. So the answer is and print out q2 dot get answer. After that. Let me just run this code one more time. So this is for the first quiz. And then we have the math. The first question is five plus seven. So the answer is 12. The second question is 12 minus eight, the answer is four. So just to make them clear, we can simply divide this. After finishing from this one, we can simply print out some equal. And of course, we can skip a line here. And after that. And after the first question, we can also scale aligned run. We're going to get this. So this is the first question. So the question is which one? As usual? And then we have the math girls and this case we have two questions. So this is the first question and followed by the second question as we can see here. So this is basically for JSON. We learned how we can extract data and how to use them in our Java code. Written always told these data and add days less or anything we want. If for example, we just got them as a string and print them out directly. However, we could have just then this and store it in a string, then do whatever we want with it. We may print it out and we may modify it. Or we may just get some integers, add them together and get the average of something, for example. And this is this JSON file or document, works exactly as any other file. We can do whatever we want with the data once we extracted and correct way. And this is basically for JSON. With that being said, this is the end of this bloom. See you in the next one. 43. Project: So our project is to create an account for either student or instructor. This is our main method and let you see it and you need to create the account class student and instructor as children of this parent class account. Now, let's go ahead and see what this class do. So if we go ahead and run the code and we get student or instructor. So of course we are students. And to ask us to enter our first and last name as the major computer. And G. And I should enter my courses. For example, say I'm taking CoA to one, English 101 and physics. To go to. Now, my account is created, had unions student, my ID is one major computer engineering courses. And to ask us to try again. Try again. Type yes. Let's suppose we are instructed Now. First and last name. So Alex, the department, let's see. So the courses I'm teaching, let's say two. And so this account is also created Alexei instructor, department finance courses teaching these and without an idea because the instructor doesn't have 90. Now, if I press enter by mistake, I'll get invalid input. If I want to try again, for example, try again. Lowercases. Suppose a student first and last. For example, a major. Suppose electrical engineering and courses to a one ELE D03. And this account is also created and the idea is now to, since we are to students now, deontology. And lastly, if for example we type by mistake, will get invalid input to try again one more time. So if we don't want to try again, simply type no. And this is the end of the program. Now, your student and instructor. First of all, we started with student. We have a class and we have some methods here. First of all, you need to create a constructor using an object Student. And then we need to set the major and set courses and instructor class. We have the constructor to take just two parameters. Here, we have three parameters, we have the ID and we have two methods, set courses and set department. And we also have else if this is an invalid input and while we enter something different than yes or no, we keep asking the program and we keep asking the user to try again. So this is it for the project. And I hope you enjoy it and see you in the next video. 44. Recap : So this is the end of the class. First of all, we talked about inheritance and polymorphism. Then we moved on to recursion and some, solved some problems in this topic. Then we talked about sorting and searching algorithms. And of course, at the end we solved some problems on every topic. And we have our project. So this is it for this class. The next class, we'll talk about XML and JSON parsing. We learned how to parse JSON and XML using Java. And then we talk about stacks and queues and use them in our St. Louis and doubly-linked list. And finally, we'll talk about collections class and how to use, do use it in Java. I hope this class was beneficial. And see you in the next one.