Welcome to Java Programming for Computer Science A students. This is the fastest and easiest way to learn Java. Let's get started. First, let us install the Java Development Kit or JDK and an IDE, that is an Integrated Development Environment, basically a tool to write Java code.
To download Java JDK, go to Google. And search for Java JDK download. Now it doesn't really matter when you are watching this video. Just download the latest Java JDK.
The core Java language doesn't really change. Only new features are added. So always download the latest JDK. Here I am going to select JDK download. And here the exe file.
Let's run the exe and install it. You have to keep clicking next. Next, we need to install the Java IDE.
IDE stands for Integrated Development Environment. It's just a tool to write Java code. There are many popular IDEs in the market.
Eclipse, NetBeans, IntelliJ. It doesn't really matter. All three are easy to use.
If you know one tool, you will be able to use another tool. For this class, I'm going to use the Eclipse IDE. Here to always download the latest IDE, let's click download.
Let's run the exe to install. Here you can install either the Eclipse IDE for Java developers or Eclipse IDE for enterprise Java developers. If you want to create advanced Java applications like Java server faces and things like that, you need to install Eclipse IDE for enterprise Java developers. For this class, since we are going to learn core Java, let's just use Eclipse IDE for Java developers. So here too you can select where you want to install Eclipse and click on install.
To launch Eclipse, you can click on launch over here or you can go to this folder where Eclipse is installed and then launch from there. So if you go drill down that folder, you will see something called as eclipse.exe. You can either create a shortcut for this onto your desktop or just double click over here to open Eclipse.
So here you can select your workspace. A workspace is the place where you want to store your Java code. So I'm going to select the workspace and click on launch.
Let me close this welcome screen. First let us create a Java project. To create a new Java project click on File New Java project.
Let's give it name for this Java project. I'm just going to call it my first Java project. We can keep the rest of them as default.
Let's click next. Here you don't have to create this module info.java file. So let me uncheck this.
This will be required only while packaging our Java projects. Let's click finish. So we now have our Java project ready. Now if you don't want to install the JDK and the IDE, you can simply use an online IDE called Replit which is very famous.
Just type in Replit, click on start coding, login using your Gmail account and over here click on this button, new repl. Select Java and here you can give your program name. So I have a program called First Program and click on Create Ripple. I will explain to you this program and classes later on. But here we are just going to learn how to use this Ripple site.
To run the program, I can click on Run. This usually takes some time to run and you will see the output over here. It took quite some time but the output hello world appeared over here because I had written the text inside the system.out.println. Of course I will teach you all this later on. If you want to add additional programs you can click on the add file over here or if you want to create a folder you can click on add folder.
Let's create another file called add file. I'm going to create another program, employee.java. So make sure that you have.java whenever you're creating a file in Java.
Press enter and here you can create the other programs. Don't worry about what these programs mean. I will teach this later on.
Replit is a great tool. It can do most of the things that an Eclipse IDE can do. Like for example, it can also give you help.
When I say object dot, it will give you help that is the various methods you can call upon. So instead of installing the JDK and Eclipse IDE, you can simply use the Replit online tool. First, let us learn why Java became so popular.
If you take a language like say C++, when you compile C++, it makes use of the operating system resources to create the compiled code. So you cannot take a C++ code compiled on Windows to run on the Linux operating system. Around 25 years back, many people developed their code in Windows but wanted to deploy the code in Linux. Because Linux was considered a better server.
So Java engineers solved this problem by creating another layer. This layer is called the Java Virtual Machine. So if you look at it, there is a JVM or Java Virtual Machine for every operating system and Java code resides on top of the JVM. So once you compile Java code, you can deploy it to any operating system. That is why Java is called a platform independent language.
It is independent of the operating system. The operating system is the platform and Java is independent of this platform. In chapter 2, we are going to learn the Java syntax.
by writing some small Java programs. So now we have a project and if you click here right next to the project you will see that it has a source folder. The SRC means is a source folder where you can store all your Java programs.
Now before writing any Java program let us create something called as a package. A package is nothing but folders in Java. We can right click on the source click new package and create a package like day1.examples and click finish.
Now these packages are nothing but folders in Java. If you want to see the folder structure, you can go to the C drive that learn Java workspace. You can go to the C drive learn Java workspace and you can see that project name first project and under which we see that source folder and under which you will see this day1 folder and under which you will see the examples folder. So you can see that the packages that we created there basically created folders for us in the C drive.
So once we have created a package, let us create a small Java program. So let us right click on a package, click on new class. Now don't worry about what a class is, we will learn that very soon.
But for now, remember, if you want to write any Java program, you need to create something called as a class. And let us call this class my first class. Now remember while creating the class name you need to use a capital letter the class name starts with a capital letter and then the subsequent words will also have to be capital letters for example my M is capital and then first class F is capital and C is capital there should be no space between these particular words it should be one word also click on this public static void me Of course you will learn very soon about the use of all these things but for now just remember to create a class and then select public static void main click finish. Now I want to increase the font size in Eclipse so that everyone can see clearly.
To increase the font size click on windows preferences type in font select colors and font. basic text font click edit and here you can select a size and click OK now before writing any program we have to learn how to write comments comments are useful in describing a particular program for example I want to say that I am planning to learn Java so I want to write a comment so let me delete these things now to write a multi-line comment All you have to do is put this slash sign asterisk press enter. This will be a multi-line comment like this.
Now I can write some kind of a description about this particular program. So here you can see that I have written some kind of a comment. Now this is an example of a multi-line comment. For a single line comment, you need to have two slashes. So here you can see that I have like two slashes and my first statement.
It is a comment. You can write a comment anywhere in a class. It is not going to affect your program.
Now if you want to print something to the console, the syntax is system. now remember the system keyword should start with a capital S and when you put this particular dot you should get some kind of a pop-up this pop-up shows that this particular word you have typed it correctly system.out.println then you open the parenthesis and double quotes and within the double quotes you can type anything you want like say hello world now this is the syntax to print something to the console in java system dot out dot print line and system starts with a capital s and within double quotes you can type in anything you want now you can save this particular program by clicking on the save button here and to run this program that is compile and run this program you can right click here anywhere in the screen click on run as and then click on Java application now you can see that the hello world has been printed here to the console now suppose you want to add two numbers so to add two numbers we first need to declare two numbers that is something like this so here we have declared two numbers like say integer X equal to 10 and integer Y equal to 20 this x and y are called variables. Variables can hold some data.
Now this integer is called the data type that is this integer x can hold any whole number. So your next question may be like what if I want to hold a decimal number? What if I want to hold like a character or a string or something like that like a text?
We will see all different types of data types later on but for now remember that. integer data types can hold any whole number and then we can say something like integer c equal to x plus y let's say integer result equal to x plus y and then we can print result now to print result once again it is system.out.println now there is a shortcut to write system.out.println you can say type s y s o and keep the control key pressed and Press the space bar like this and then you can say result. Now you can save this program and run it, right click, run as java application and you will see that the hello world is printed and the result of 30 is printed.
You can also say result equal to within double quotes and then plus result. So what this will do is it will print out, save it and run it. So here you can see that it has printed out result equal to and then it has concatenated. This plus sign does not add, you cannot add some text and some number, so it has it's called a concatenator.
It concatenates this text and this particular number and then you see the printout result equal to 30. Next let us take a look at the if statement. We can always expand and collapse this package explorer by clicking here. So to learn the if statement, let us do another example. Right click on the package, new class, and then over here, let us type example if. Select public static void main, click finish.
We don't need these statements. Let us delete them. So imagine I have two numbers, two variables, integer x equal to 20 and integer y equal to 20. An if statement goes something like this. you read this as if x is less than y this symbol is for less than to see if the value of x is less than y then I want to print out s y s o control space print x is less than y let us write another if statement if x is equal to y you The way you say equal is you have to say equal to equal to. If x is equal to equal to y, that means if the values of x and y are the same, x is equal to y.
Let's check if x is greater than y. Now let us save this. You can either click here or you can press on control key and keep the control key pressed and then click on the S key. Control S. We'll save it or you can click on this particular save button. Now let us run this, right click, run as Java application.
You will see that x is equal to y has been printed. The reason is x is 20 and y is 20. It will come and check if x is less than y. Well, x is not less than y because 20 and 20 are equal to each other.
Then it will come and check if x is equal to equal to y. If x is equal to equal to y, yes, both of them are equal. then it will print x is equal to y.
It will also go and check if x is greater than y, when x is not greater than y, so it will not print this particular statement. So you just learn the if statement. But of course, if you notice, this is an inefficient way of writing a program.
The reason is, it is checking if x is less than y, then it is checking if it is equal to y, then it is checking if it is greater than y. So it will run all these three statements. You can use an else statement to make it more efficient like this.
You can say if x is less than y, system.rower.println x is less than y, else if x is equal to equal to y, system.rower.println x is equal to y, else if x is greater than y, then system.rower.println x is greater than y. Now the advantage of this is writing a statement like this is like it will come and check if X is less than Y if it is less than Y well it will print it out but in this case it is not less than Y it will come here else if X is equal to equal to Y so since it has matched the condition it will print this and it will come out of it it will not go check else statement because only if this fails it will go to the next else statement So you are saving that fraction of a second for the compiler. So let us save and run this. So you can see that you get the same answer. But then the compiler did not check this particular condition.
And for the last condition, you really don't have to use this if statement. You can simply say like, else system.route.println x must be greater than y. The reason is, so here you can see, if x is less than y, else if x is equal to equal to y, print this, else. Well, I'm sure if it is not less than y and if it is not equal to y, it should be greater than y.
Save and run this. You'll get the same answer. As a new Java programmer, one of the most confusing things is the small issues like when to put the semicolon, when not to put the semicolon, when to use the curly braces and when not to use the curly braces, etc.
Remember, These things you will know by experience. But for a general rule, any complete statement will end with a semicolon. For example, you see that int x equal to 20 is a complete statement. Whereas the if statement is not a complete statement.
I am checking there is going to be some kind of a code. If x is less than y, I want to do something else. So I will not end this with a semicolon.
This is like some kind of a main method. So I cannot end this with a semicolon. Same way, whenever you open a curly brace, you must make sure that you close that particular curly brace. These are called curly braces.
You can always check if a curly brace is closed by clicking on a curly brace and you will see a small square box here on top of the one which is open. Same way, if I click on this, I will see a square box here so that you can make sure that every open curly brace is closed. Same thing goes for if I click on a curly brace over here. the closing curly braces shown and these are called parentheses. You will get more used to these syntaxes as and when we go on.
This system.out.println is a statement on its own. This less than symbol here is called a relational operator. These are relational operators.
You may be wondering what are the other relational operators. Well, there are a few other relational operators. 2 equal to science is equal to An exclamation mark and equal to sign is not equal to.
This is the greater than, less than sign. This is greater than or equal to and less than or equal to. These are used to check if x is greater than or equal to y. Next, let us learn how to do a for loop.
Let us create a class called example for. The for statement syntax goes something like this. For int i equal to 0 semicolon i less than or equal to 10 semicolon i plus plus. all these within parenthesis, then open curly braces, press enter, it will automatically do the closing curly braces, and here let us print out i.
A for statement, you can see that has three parts. The initial value, that is int i equal to 0, the condition, that is loop through this as long as i is less than 10, and then the incrementer or decrementer of y. So the very first time the value of i has been declared as 0. So it will come here and it will check is i less than 10 or less than or equal to 10. Yes 0 is less than or equal to 10. So it will come here and it will print whatever it will execute whatever is there within these curly braces.
So it will print out system.route.println i equal to 0. Then once again it will go into this particular for statement. Now when it goes here for the next time you can see that. It reads this i++, that means the value of i has been incremented by 1. i++ is nothing but i equal to i plus 1. Writing i++ is a short form of writing something like this, i equal to i plus 1. If you want, you can say i equal to i plus 1 right here. So it will start printing out, like 0, 1, 2, 3, it will keep incrementing i, till i's value reaches 10. When it reaches 10, what will happen is, yes, i's value is 10, so 10 is less than or equal to 10. Yes, it is equal to 10. It will print 10, but next time it comes, i's value will be 11, but 11 is not less than or equal to 10, so what will happen is it will come out of the loop. So in this case, it will print from 0 to 10. So let us save this and run this.
Right-click, run as Java application. You can see that I can increment. I'll expand the screen.
You can see that i's value has been printed from 0 to 10. i equal to 0 to 10. If you want to print from 0 to 9, all you have to do is remove the equal to sign and then run this. You can see that i's value has been printed from 0 to 9. You may be wondering like is there any other symbol like this plus plus sign? Is there anything else I should know?
You can either use a plus plus sign which is like will increment the value or you can use a minus minus sign to decrement the value. Let us try using the minus minus sign. Let us put the initial value of i as 10 and we will check we will print till i is greater than 0 and then we will say i minus minus. Let us save this and run this. So here you can see that It started out printing i equal to 10 and then it kept reducing the value of i by 1 till i became 1. Since 1 is greater than 0, it printed it, but 1 minus minus is 0. So once i reached 0, it came out of this loop.
Next, let us learn about the while loop. Let us create a class. So we have a class called example while.
Let us have some integer. x equal to minus 10. The while syntax goes something like this. While x is less than or equal to 0, curly brace, curly braces, print x. So here you have integer x equal to minus 10. While x is less than or equal to 0, well minus 10 is less than or equal to 0, print x and then increment the value of x.
We can increment the value of x by saying x plus plus, which is nothing but x equal to x plus 1. Now let us run this, save. So if you look at the result, x started at minus 10, and then like, is minus 10 less than or equal to 0? Yes, it is less than or equal to 0. So print out x equal to minus 10. Then it will come and increment the value of x by 1. So minus 10 plus 1 is equal to minus 9. So it's minus 9. It will go back here. Is minus 9 less than 0?
Yes, it is. It will print out x equal to minus 9. So it keeps printing out till x reaches the value of 0. Is 0 less than or equal to 0? Yes, it is equal to 0. So it will print out 0. But 0 plus 1 is equal to 1. 1 is not less than 0. so it will come out of the loop. So this is what you call a while loop.
Now there are different ways to increment the value of x. You can either say x plus plus as you have written here or let me comment this and type x equal to x plus 1. This is another way of incrementing the value of x. This will also work the same.
Let us save and run this. You can see that there is no difference. And there is another way of writing this too. When you have something like x equal to x plus 1, you can also write it like this. x plus equal to 1. So this means x equal to x plus 1. Let us run this.
So you can see it's the same answer. So different styles of writing it. So just remember plus equal to means x equal to x.
plus let us change this number to 2 this means x equal to x plus 2 so here you are incrementing the value of x by 2 so you get printouts like minus 10 minus 8 and minus 6 you may be wondering is there any other symbol like this so here you can see that you can either use plus equal to which says x equal to x plus whatever number it is called an addition assignment you can also do minus equal to or multiplication equal to or division equal to and mod equal to. Next let us learn about the do while statement. Let us create a class called example do while. Let us declare some integer, integer x equal to 10. The do while syntax goes something like this.
Do curly brace curly brace and then your while statement comes here. While let us say x greater than 0 and then a semicolon here and inside these curly braces I want to print out x and I will reduce the value of x. So let us run this and see how this works. So here you can notice when I run this it starts printing out x equal to 10 that is x equal to 10 do Then it will come in here and it will print out X equal to 10 X minus minus will reduce the value of 10 by 1 That is 9 it will check is 9 greater than 0. Yes 9 is greater than 0 So it will come back to the loop and it will print out X equal to 9 and then it will come here Reduce the value of 9 by 1 it will come and check if 8 is greater than 0 Yes, it will go back here and print out it so on and so forth. So it will print till X equal to 1 Then X minus minus of 1 is 0 is 0 greater than 0 no it will come out of the loop So this do while is very similar to the while loop do not forget to put the semicolon right here So what is the difference between while loop and do while loop well for a while loop?
You can see that it first checks for the condition it checks if X is less than or equal to 0 and then it goes in principle whereas for do while It will first print the statement, it will do all the work and then it will check for the condition. So the main difference is the do while statement will run at least once. For example, if x is equal to minus 10 here, even though minus 10 is not greater than 0, this statement will still run.
It will still print out x and it will minus x by 1. So when I run this, You can see that I have x equal to minus 10 printed out. So even if this while condition is false, whereas for a while statement, if I make this 10, it will not print this even once. You can see that, well, 10 is not less than 0, so it will not print here. So do while statement executes the block of code at least once. Next, let us learn about AND and OR statements.
Let us create a class called Example AND and OR. Imagine I have two numbers. integer x, integer y. I can declare two variables like this and then I can assign numbers to x equal to 10. Now imagine I want to check if both these numbers are positive numbers, that is x and y are positive numbers.
The way to do it is if x greater than 0 and y greater than 0. curly brace and close curly brace. So the way you read it is x greater than 0 if x greater than 0 and these two ampersand signs is referred to as and. If x is greater than 0 and y is greater than 0 then print both numbers are positive.
Else If, check if at least one of them is positive number. That is, if x is greater than 0 or y is greater than 0. The way to write it is, the R symbol is the two pipe symbol. You can find this pipe symbol right above your enter key. You have to press the shift and the key above your enter key. So you read this as, else if x is greater than 0 or y is greater than 0. At least one number is positive.
Yes. Well, I don't have to check if both of them are not greater than 0 and at least one of them is not greater than 0, that means both must be less than 0, that is both numbers are negative. So let's save it, Ctrl S and run it, right click run as Java application. So here you can see that at least one number is positive as printed because x is 10 and it is a positive number.
So this is the AND and OR statement. Now this will work even if you have like a single ampersand and a single pipe symbol. This will work the same.
So what is the difference between this single ampersand and double ampersand is? This double ampersand is called a shorthand. Now what is a shorthand is? Imagine I have x as minus 10 and y as positive 10. Now it will come here and will check if x is greater than 0. Well, is x greater than 0? No, it is not greater than 0. So what happens is this.
If this condition fails, a shorthand will not even bother to look at the other condition because both the condition must be true for and to work. And this x must be greater than 0 and y must be greater than 0. But if you have like two ampersand sign, it will know that it is useless to go look here at this particular condition here. The reason is, well, x fails, so it doesn't matter if y is positive or not. If you have like one ampersand symbol, what happens is it will check if x greater than zero. Well, no, it is not greater than zero, but even then it will go and check if y is greater than zero.
You may be wondering then why use the single ampersand at all. Well the single ampersand is used for bit operations which I think you can read up on your own at a later stage. Next let us take a look at the switch case statement. So let us create a new class. So imagine I have a variable like j equal to 2. The switch case statement is written like this switch within parenthesis j curly braces close query braces then you create various cases like this case zero colon system.dot.println value is zero and then say break create another case so here you can see that i have created various cases like i have a int j equal to 2 and then i have a switch j and then i have like case 0 which will print out value is 0 and then there is a break case 1 value is 1 and so on and then at the end i have something called as default and it prints out no value and then break and then i close the particular curly braces over here this curly braces i close it here now let us save and run this so when you run this you get the value as to value is two is printed so you can see how the switch case works when the compiler reaches this particular point like switch g Then it will look for a case which matches the value 2. So since this particular case matches the value 2, it will execute all the code in this particular block of code before the break is hit.
And then it will reach this break and it will come out of this particular switch statement. If you do not have this break statement, Then what will happen is it will fall down to case 3 and it will print out values 3 also. So it will print out values 2 and values 3. I can save and run this. So here you can see that if I run this now, values 2 is printed and then values 3 is printed because I had removed the break statement. Now suppose there is some value like say 5 and we don't have any of the matching cases.
In this case, I have... no case 5 so in those cases the default case will be executed that is no value would be printed since Java 7 you can also use strings in switch case statement before Java 7 you cannot use string string is used to hold any kind of a text so let us modify this to use strings string J holds the text as 2. So now let us say case 0 1 2 and you can see that I have also changed all the cases to like strings. Now if I run this you can see that values 2 is printed. So you can also use string in these switch case statements. Next let us take a look at arrays.
So here I have a new class called example arrays. Now what are arrays? Imagine I want to store a bunch of data.
I can store data like this. So here I'm storing some data that is integer a equal to 10, integer b equal to 20. I'm also declaring like variables like c, d and e and I'm storing c as 30, d as 40, e as 50. Like I can have them in one line, I can have them in multiple lines, it doesn't matter. So I can store a bunch of numbers like this but This doesn't look like an efficient way of storing numbers. I want to store file numbers. This doesn't look like an efficient way.
And I'm likely to run out of variable names too. So another way to do it, maybe I can do the same thing like this. So let me comment out this block of code.
To comment out this block of code, you can just select it, click on source, and then say add block comment. So this whole thing would be like blocked. so here i have like int a1 equal to 10 a2 equal to 20 like this i can declare all the numbers but this still does not look efficient so what is an efficient way of storing large amounts of data uniform data so to do that let me block this code an efficient way of storing large amounts of uniform data is called arrays so if you want to store a large amount of data the best way to store the data is So here I have declared an array of A.
So how you declare an integer array is you say integer square brackets A equal to all the numbers that you want to store in your array. Once you declare something like this what happens is internally Java will create a boxes like this. Since we named the array A it will label the boxes like this.
So here you can see that we create an A array. and it has five numbers so java internally will create five boxes one two three four five and it will label the boxes as a of zero a of one a of two a of three a of four this is read as a of zero a of one and it will put those five numbers in these boxes so this is an array an efficient way of storing a bunch of homogeneous data. So if you want to access some data in this array, all you have to do is try to print out like say a of 2 and a of 4, it will print out 30 and 50. So let us print out something from the array. So here, you can see that I am trying to print out a of 2. And then there's a space and then I'm trying to print out a of 4. Remember, arrays always start at zero. So a of 2 will print out this is a of 0, a of 1, a of 230. then a of 4 is 50. So let us save this and run this.
So you can see that 30 and 50 are being printed out. Now you may ask like what if I want to loop through all the numbers and print out all the numbers in the array. To loop through all the numbers you can use something called as a for each statement or an enhanced for statement.
The way to write a for each statement or enhanced for statement is for Now the data type of the array, whatever the data type of the array, in this case the data type of the array is integer, some temporary variable. colon and then the name of the array a curly braces curly braces and inside this you can print out the temp variable. So what happens in this case is let us save and run this you can see that all the values in the array that is 10 20 30 40 50 have been printed. So how this enhanced for work is the very first time it comes and sees this for int temp colon a it takes a first value of A, the first value of the array A and puts it in temp and then it is printing out temp. The second time it loops through it takes a second value of A and puts it in this temp and then it prints out temp.
So this is an enhanced for statement. Of course it is not required that whenever you declare an array you should immediately give all the numbers in that particular array. You can first declare an array and then you can assign numbers to it like this. So in this case, I have declared an array called x and I'm saying like new int of 5. That means this x array will have 5 numbers.
So immediately java behind the scene will create 5 boxes and label them as x of 0, x of 1 till x of 4. Remember arrays always start at 0. Now since we have just declared the array like here and since that array x does not have any values, By default, it will put the value 0. So here you can see that 0 is the value available. Of course, you can always assign data later like this. So here you can see that I am assigning x of 3 is 25 and x of 0 as 12. So x of 0 will be 12 and x of 3 would be 25. Let us loop through this particular array. I can have some temporary variable.
So here I have some temporary variable temp2. I'm going to loop through this x array. I'm going to print it out.
Let's save and run this. To differentiate between this printout and this printout, I'm just going to have this dash dash dash printed in between. Save and run. So here you can see that it is printing out this other array with 12, 0, 0, 25, and 0. 12, 0, 0, 25, and 0. The zeros are default. if you don't give any values for an integer array.
Of course you can create any type of arrays you can also create string arrays if you want. So here I have a string array called st which has one two and three and I can loop through string arrays. So you can see that it is printing out one two and three. Same way I can create I'm creating an empty string array and inside which so here I'm creating an empty string array inside which I'm placing the text 25 and text 12. You can see that these are not numbers, I have put them within double quotes. So let's save and run this.
So same way here I'm declaring a string array of 5 and I'm putting the text 25 and text 12 in the string array of 3 and string array of 4 and I'm looping through this particular string array. Now the one big difference between declaring a string array and declaring an integer array is I told you if you declare an integer array by default all the values will be zero and then you can assign numbers but for string array the default is null values the default is null values so here so here instead of zeros you're going to have null values and null here and when you save and run this You can see that 12, null, null, 25 and null are being printed. Next, let us take a look at a two-dimensional array.
So here I have a class called example2dimArray. Now arrays need not be one dimension like this, that is you have like one array of numbers. It can be two-dimensional. What does it mean by two-dimensional? Two-dimensional arrays are a kind of a matrix.
So here you have like columns. Column 0, column 1, column 2 and then rows that is column row 0, row 1, row 2 and row 3. Two dimensional arrays you can label them that is here you have like a two dim array of 0,0 that is the 0 row and 0 column. This box is 0 row first column and 0 row second column.
Here it is row 1 and 0 column, row 1 and 1 column and in these boxes you can assign numbers. and this would become a two-dimensional array. So how do you go about declaring a two-dimensional array and assign values?
You can declare a two-dimensional array like this, int, two square brackets, like an array named to dim, and then new int of how many rows do you want and how many columns you want. Here you can see that I want four rows and three columns. So what I did is I named the array as two-dim. And I said four rows and three columns.
Four rows, three columns. Now to assign data to this array, I can assign it like this. So here you can see that I am assigning the second row first column as the number 10. So the second row first column will be assigned the value 10. So like this I can assign data to any of these boxes.
I can also use two for loops. to assign data automatically like this. So here you can see that I have a loop within a loop.
I have a for int i equal to 0, i less than 4, and then i plus plus. And inside this, I have like for int j equal to 0, j less than 3, j plus plus. And now I'm going to say, so here I'm saying like two dimensional of i comma j is equal to temp and the temp value initial value is 10. and then I am incrementing the temp value, that is temp plus equal to 10, that is temp equal to 10 plus 10. So how this works is, the very first time i will be 0, and then j will be 0. So two-dimensional of 0, 0 will be 10, then the temp value will increment by 10, that is temp will be 20. It will go here, and first it will go into the internal room, that is i will still be 0, but j will increment by 1, so it will be 1. So 0,1 will be 20. So you will have data like this.
So 0,0 will be 10, 0,1 will be 20. This is the i and this is the j value. And here it will be like i value will be 0. Next loop j value will be 2 and then the temp would have incremented to 30. So once j reaches 2 and then it increments to 3 it will come out of this loop it will go back to this for loop and i value will now be 1. So now it will once again come back it will come back to this j and j will start at 0 because now i is 1 and there will be a fresh j loop. So here it will be like 2 dimensional of 1 comma 0 and that will be like the temp value.
So it will take the latest temp value. So once that entire loop is done, it will fill up these boxes with these numbers. Suppose you want to see the printout, you can always redo this and here instead of assigning data, let us print it out. So I'm going to print out the two-dimensional of i,j, the 2dim of i,n,j, what we declared here.
So if you run this, you can see that all the numbers are printed from 10 to 120. 10 to 120. Of course, you may feel like I don't want everything to be printed in one line. Can we print it like a matrix? Yes, you can. Remember, the print line will always print in a new line. But you can use the print statement.
The print statement will print in the same line. So what I'm going to do is I'm going to say system.out.print. print the number but then the print statement will keep printing without creating a new line so every time j loop ends i want to print a new line so i'll print an empty line like this system route dot print line and then i just leave it so now let's run this so here you can see that it has printed like a matrix just like what we have drawn 10 20 30 40 50 60 and so on So this did not print in a new line. It printed the two dimensional of 0, 0, then the space that is 10 and then the next time 0 and 1 and then space.
But once the J got over before going back to I, it printed a new line. So it came here and then it started printing 40, 50 and 60. Next, let us take a look at string manipulation. String is used to hold any text.
So for example, I have a string x equal to James Dean. If I want to concatenate string, I can do it like this. So this statement will print hello space and then it will concatenate it with James Dean to print out hello James Dean.
If I want to convert the string into uppercase, I can say x dot to uppercase. Now like this, there are so many different methods. You can see here, when I say x dot, you have all these methods that you can do with.
That is trim. uppercase like you know substring and all those things we will take a look at few of these string methods x dot 2 uppercase will convert that particular string into uppercase so X dot substring of 2 will print everything from the second character now what is the second character remember when you have string X equal to James Dean will internally create an array of characters like this so you have like James Dean each in their own boxes and it will label it as x of 0 to x of 9. So when you say x of substring of 2 it will take from the second character that is x of 2 till the rest of the word. So mes space dean will be printed.
When you say x of substring of 2 comma 7 then what happens is it will print from the second character up to but not including 7. So it will print MES space and then D up to but not including 7. So here you can see that MES space D has been printed. X dot char 3 will print out the character at the third position that is E will be printed. Now sometimes what happens is you may get a number as a string.
For example You have a string age equal to 37, which is basically a string, and then string salary equal to 78,965.83. Now, if you have a number as a string, you cannot manipulate it. That is, if I want to divide age by 2 or if I want to multiply age by 2, I cannot do that. So, how do I convert a string into a number?
Well, we have a concept called wrapper classes. If you want to convert some kind of a string into... an integer this particular age into integer you can do something like this you can say integer dot parse int and then you can pass this particular string so what this will do is this particular class called integer will take the string of age 37 and it will convert it into an integer and it will assign it to a now this you can divide by 2 if you want to So here you can see that I am taking this particular salary which has a decimal number and I am converting it into a double number double dot pass double and then I'm passing the salary and then whatever the double value I get I can multiply with 0.15. I cannot do like divide by 2 here it will give me an error it will say that you cannot divide string by 2 but I can convert that particular string age into an integer and then divided by 2 and I can assign it to a or I can like say add 2 for this.
So now if I run this you can see that I got 39 as the answer because I've added 2 and I'm getting 15 percent of the particular salary that is 11 000. So this is called wrapper classes. There is a wrapper class for every primitive data type. Of course the next question will be like okay we saw integer and you just introduced double to us to hold decimal places and you also showed us a string which holds text.
What are the other data types that Java has? Java has eight primitive data types byte, short, int, long, float, double, character, boolean. Now out of these You will be using mainly integer for holding any kind of a whole number.
An integer can hold a whole number of this range. If you want to hold any decimal values, you can use a double. And for holding true or false, you can use a Boolean. Now string is not a primitive data type, but then use string to hold text values.
That is string x equal to hello old or any text values. You will not be using byte, short and Long you can use long if you have like a really long whole number and for all decimal places You can use double instead of a 32-bit float. So once again, you will mostly use integer to hold a whole number, a number without any decimals, or you can use long, and for using decimal, use double, for text use string, for true or false value use boolean.
Let us do a quick review of all the concepts we have learned till now. Our very first example we learned how to do a print statement to print something to the console you use system.out.println and then you can put anything within double quotes until printed to the console same way we learned how to declare numbers like integer x equal to 10 and then we learned how to add numbers or like multiply or the mod mod is basically how many times does 3 go in 10 3 goes in 10 3 times so the reminder is 1 and the x mod y it will print out the result as 1. next we learned about the if statement We had two numbers x and y. If x is less than y, print out like x is less than y.
Else, if x is equal to equal to y, print out x is equal to y. Else, system.out.println x is greater than y. Then we learned the for statement. A for statement has three parts. First is the initial value, then the condition.
The condition must always be true for this for to be executed. And then the incrementer or decrementer. So in this case, I'm going to print the initial value of i equal to 10. So it will check, is 10 greater than 0?
Yes, it is greater than 0. Then it will go and print out i equal to 10. Then the next time it comes over, it will reduce the value of 10 by 1. i minus minus will decrease the value of 10 by 1. So this one will print from 10 up to 1, but not 0 because it will stop when it comes to 0. Then we saw the concept of while loop. A while loop will first check for the condition and then execute the block of code. So in this case x equal to 10 as long as 10 is less than 0 it will it will execute this. In this case it will not 10 is not less than 0 so it will not execute. So you can see here this will not be executed but if you say As long as 10 is greater than or equal to 0, it will execute and then here you are incrementing the value of x or you can decrement the value of x if you want.
So you can see that it has printed minus 10, it is 0 less than 0 and then you are incrementing by 2. Then we saw the concept of do while. Do while will first execute the block of code and then it will check for condition. So in this case, so here in this case I have x equal to 10, it will execute that.
x equal to 10 and then it will reduce the value and it will keep looping till the value of x is greater than 0. So the advantage of do while is even if the condition is false it will execute this block of code at least once. So that is the difference between while and do while. Next we saw the concept of switch case. We can use like integer, double or string from Java 7 we can use string for the switch case.
Switch case is like you have some kind of a data like string j equal to 2 and then switch j it will look for a case that matches this particular string. So case 2 it matches this particular word 2 so this particular block of code value is 2 will be printed and it will break out of this particular switch case. If you don't have the break it will fall down to the next case and will start printing here. If none of the cases match then the default case will be printed.
Then we saw the concept of AND and OR statement. That is, if you have like two conditions and both the conditions have to be true, that means you can use the AND. That is, if x is greater than 0 and y is greater than 0, then print both numbers are positive.
If either one have to be true, then use the OR statement. That is, x is greater than 0 or y is greater than 0, then print out at least one number is positive. We also saw the concept of arrays. So basically if you have like some kind of a data, same data and you want to store them, you can use an array. So this is an integer array of a, so which will be used to store an array of integers.
It will create boxes and it will put them as a of 0 to a of 4. And you can access those data by saying a of 2 and a of 4. If you want to loop through an array you can use an enhanced for or a for each loop. You can say like the data type. integer, put some temporary variable, colon, the name of the array a, and then you can print out temp.
You can also declare arrays and then assign values to those particular array boxes. If you don't assign some value by default it will take zero. and same way you can loop through this particular array too.
You can also create string arrays if you want. Here you are creating a string array and of course while looping through a string array the data type of this array is string. You put a temporary variable colon the name of the array and then it will print out temp and same thing happens for you can declare a string array and you can loop through those string array.
Of course if you don't assign any value to a particular box by default for string array it will be null. value. You can also create two-dimensional arrays.
Two-dimensional array is a matrix that is it will have four rows and three columns so you have like two-dimensional array and you can assign data to time two-dimensional arrays like this you can say like a row and column and you can assign some data you can use a loop within a loop to assign data or to print a two-dimensional array. Then we also saw the concept of strings. that is how to manipulate strings, how to concatenate strings, use some of the string methods, get the substring and find what are the characters and we saw the concept of wrapper classes that that is if you have some data some number that is as a string that comes to you as a string then you can always convert that into a number so that you can manipulate that number. Now let us try a few practice exercises. Let me create another package.
Right click on source, new package, and then let me create a package called day1.exercises. And here I can create a new class called exercise A. Right click on the package, new class, exercise A, public static environment, click finish. You can view these packages two ways.
Click on this. Click on package presentation. You can either view them in a flat manner, that is it will show like day one dot examples, day one dot exercises, etc. Or you can view them in a hierarchical manner. Click on package presentation and say hierarchical.
So what happens is it will say like day one and within which it will show you all the packages and the methods or classes inside those particular packages. So for this first exercise. Let us take a look at this data.
Imagine I have this data that is salary and tax rate. I want to write a program to calculate the tax. Now up to $15,000 the tax rate is 10 percent. From $15,000 to $40,000 the tax rate is 20 percent and above $40,000 the tax rate is 30 percent. So write a program to calculate the tax.
Imagine we have a salary. a double salary equal to 78,067 8.65 we want to see if this particular salary falls within those particular range so let us have another variable called tax and let us use a series of if statement to calculate the tax so here you can see how we have solved the problem we declared some kind of a variable called tax equal to 0.0 and then we are checking if the salary is less than 15,000 or less than or equal to 15,000 then calculate the tax as salary times 0.10 10 percent if the salary is less than or equal to 40,000 then calculate the salary at 20 percent else calculate the salary it must be if it is not less than 15,000 and if it is not less than 40,000 it should be more than 40,000 so So above 40,000 calculate the salary at 30% and print out the tax. So let us save and run this.
So here in this case it will calculate the salary at 30% of 78,000. Suppose I have like 10,000 as a salary it will calculate it at 10%. You can see that 10% of 1000, 10,000 is 1000. If I say like 20,000 salary it will calculate it at 20%. that is 4000 which is 20% of 20000. So we used a simple EFL statement to calculate the tax.
For our next exercise, let us try to solve this problem. Imagine I have a string array called numbers which basically has numbers but they are present as strings that is you have like 10 but it is within quotes basically it is a string. Imagine I want to find the sum of all these numbers. Well, how do I go about solving it?
Let me first declare a variable in total equal to 0. and let me try to loop through these numbers loop through these string data so we can use an enhanced for loop now i cannot say something like this this will not be acceptable i cannot say total equal to total plus temp because temp is a string so how do i convert this particular 10 into an integer i can use a wrapper class i can say something like this i can say Total equal to total plus integer dot percent of 10. That means I take the very first 10 from this array, which is in temp, convert it to an integer, and then I'll add it to the total. So the next time it comes over, the 20 will be taken into temp, and then you convert the 20 and the 20 plus 10 total will be 30. So on, we can add all the numbers. Of course, a shorter way of writing this is you can simply say total plus.
equal to integer dot percent. And once we are out of the loop, we can print out total equal to total. Of course, don't try to print it within this particular loop because every time it loops through, it will print it again and again. Let us save and run this.
So you can see that the total is 100 has been printed. For our last exercise, imagine I have a string a equal to hello world. I want to print out the word world in lowercase.
Now how do I go about doing that? I can use a print statement and I can say a dot substring of. Now if I want to print out world, I can say like substring of 6. The reason I have to use 6 is h is 0, 1, 2, 3, 4, the space is 5. So everything from 6, I want to print out everything from 6, so I will say a.substring of 6. So when I save this and run it, you will notice that the word will be printed.
But you can see that the W is in capital case. I want everything in lower case. So if I want to convert this whole thing into lower case, all I have to do is put a dot here and then say to lower case.
Now you can save it, Ctrl S and then run it. Now you got the world printed all in lower case. So with this, we end our day one exercises. Next, let us take a look at the concept of class and object. Now, if you have done any kind of programming, like Fortran or COBOL, and you are given a problem like calculate the total pay of an employee by adding the salary and bonus how would you go about doing it?
So we will have something like this. So given a situation where we have the employee salary and bonus we would like add up the salary and bonus and then print the total pay or if you have done some COBOL or FORTRAN we may call a method or a subroutine here. So here if you notice We are taking the input of salary and bonus and then we are calling some kind of a function or subroutine to calculate total pay.
And then this function will calculate the total pay and then we will print the total pay. So this used to be the old way of writing programs. A kind of a top-down approach.
The program starts at the beginning here and then it ends here. And then you run this program again and again for various employees. Like for an employee like Alex, we will take a salary and bonus.
We will call this function which will calculate the total pay and then we will print the total pay for Alex. For Linda, we will run this program again. We will get the salary bonus and then we will call a function to calculate the sum of salary and bonus and we will print the total pay.
But in object-oriented programming, the same program of calculating total pay is written in a different manner. In object-oriented programming, there is first something called as a class. Now don't worry about what a class is.
Now this class will have those two data that is salary and bonus. And then it will have this function or what they call a method which will calculate the total pay by adding the salary and bonus. So here you can see that what happens is there's a class called employee which has like two data that is like salary and bonus. And then it has like. a kind of a method which will add up the saldian bonus and print the total pay now in some other place we will have some kind of a main method Where we will create employees like this.
Like employee Alex, Linda, John. Now if you remember, this syntax is very similar to this. Like integer x, y, z. Like we have integer x, y, z.
We will have employee Alex, Linda, John. Now if you remember, when you say integer x, y, z. That means x can hold an integer.
Y can hold an integer. and Z can hold an integer. That is, you can assign some integer to X, you can assign some integer to Y. Same way, when you look at this, when you say employee Alex, Linda, John, that means Alex can hold an employee, Linda can hold an employee, and John can hold an employee.
Just like X can hold an integer and Y can hold an integer. So what does it mean by holding an employee? Now, when you say int, x that means x can hold an integer and when you say employee alex that means alex can hold an employee that means alex will have his own salary will have his own bonus and will have his own calculate total pay that means you can say alex dot since employee has a salary alex will also have a salary dot salary equal to say ninety thousand Since employee has a bonus, Alex will also have a bonus, 20,000. And since employee has a method called calculate total pay, Alex will have a calculate total pay method.
So what happens now is, when Alex calls this calculate total pay method, it will call this method called calculate total pay and it will add up the salary and bonus. But whose salary and bonus will it add? Since Alex is the person who calls this particular method, it will add up Alex's salary and bonus, that is $90,000 and $20,000, and it will print out over here $110,000.
Same way for Linda, Linda will have her own employee, that is Linda is a type of employee, so Linda will have access to her own salary, her own bonus, and she can call her own calculate total pay. That is Linda.salary and Linda. can call her own calculate total pay. So over here when Linda calls calculate total pay, what happens is it will come here and it will add up salary and bonus.
Whose salary and bonus? Since Linda is the person who called this method, it will add up Linda's salary and bonus and it will print out $120,000. This Alex, Linda and John are what you call objects.
This Alex, Linda and John are what you call objects. So what is an object? So an object can be defined as a copy of a class. So if you notice, Alex is nothing but it has a copy of the class employee. Because employee has salary and employee has bonus, Alex will also have a salary, also will have a bonus and Alex will have its own copy of calculate total pay method.
Since Linda is a type of employee, that means Linda has a copy of employee, that means Linda will have her own salary, her own bonus and her own calculate total pay method. So an object can be defined as copy of a class and some people even call it instance of a class. So if that is the case, what is a class?
A class is a template for an object. A template, the meaning of template, if you remember like if you use MS Word document, you can create new documents from like templates, existing templates, and then you can type in your own letter. So same way, this is nothing but a template.
You create copies of this class employee and then you assign your own data for each and every object. So a class is nothing but a template for an object. Now what does a class usually contain?
This is called data. This double salary and double bonus is called data and this is called a method. So a class usually contains like data and methods. It will have multiple data and multiple methods. So a class contains data and methods.
Now let us look at another example. This time I will have a class called box which will have like two data. Length and width and we shall have a method so you should look at this class box It has two data, length and width, and it has one method called calculateArea, which basically multiplies the length and width, and it prints out that particular area.
So, to make use of this class, I will create objects of this class. So here I have created two objects of this box class, that is UPS and FedEx. And since UPS is a type of box, UPS will have its own length and width, and FedEx will have its own length and width. and ups will have its own calculate area and FedEx will have its own calculate area. So here you can see that ups since it is a type of box ups will have a length and I'm assigning 10 and ups will have its width assigned as 5 and when ups calls calculate area it will multiply the length and width and it will print the area but whose length and width will it call since ups is the one which called this method it will use ups's length and ups's width and it'll print out 50 the same thing when fedex calls calculate area it will use fedex's length and width and it'll print out the area so this ups and fedex are called objects so what are objects objects are nothing but copies of this class each and every object has a fresh copy of this class it has a fresh length fresh width and a fresh character area method and the class has data and methods and it is a template for objects.
So let us try out the two examples that we saw that is the employee class and the box class. Now let us create a package called day2.class and object and in this let us create a new class called employee. you need not check this public static void method.
So in this class employee we are going to have like a salary bonus and a calculate total pay method. So now if you see I have this class called employee with salary and bonus and then I have this method called calculate total pay which adds up the salary and bonus and prints out total pay equal to the total pay. It is giving me some kind of an error. Now To remove this particular error, you have to put this void in front of this method. Now, you may be asking me, what is this void?
We will learn more about this void and return type later on. But for now, remember, this particular method is printing out something right here. It is not returning this any value.
This method is not returning any value. So void means that this method is not sending any value anywhere else. it is just printing it out right here. Of course you'll see more uses of void later on.
Let us save this class and let us create another class in the same package and we'll call this like test employee and here let us select public static void main. In this main method we are going to create objects of employee. Now to create objects of employee you can either do something like this. So to create objects of employee you can either do something like this employee Alex Linda John and then we can initialize each and every object. This is a requirement you have to do this or You can say employee Alex equal to new employee.
So this new employee is required. You are initializing the object. So once you have created this employee objects Alex, Linda and John, you can assign the salary and bonus for each and every object and call the calculate total pay. You can say Alex dot. and here you will immediately see salary bonus.
So that means this Alex object as a copy of salary, a fresh copy of salary that you can assign. Alex.bonus equal to Alex.calculateTotalPay. So when you say Alex. these things must come.
Now when you run this, save it and run this particular main method, right click run as Java application, you will see that the total pay of 110,000 is printed. Same thing goes for Linda. So Linda has her own salary, her own bonus, and Linda can call her own calculate total pay.
And when you save and run it, you'll get this Linda's total pay printed right here. So let us try out the other example, the box class, and create box objects. I'm going to right click on this package, new class.
So in this box class, I have like a length, width, and a calculate area method, which basically multiplies the length and width, and then prints out area equal to area. And to test this box class, I'm going to create a test box class with the public static void main method. I'm creating two objects of box, UPS and FredX, and I'm going to assign the length, width, and I'm going to call the calculate area method. So when you run this box class, this main method in the box class, right click, run as Java application. You will see that the area is 50 for this ups and area is 42 for this FedEx.
Now a few conventions while creating classes. Class name should always start with a capital letter. Now these are conventions not a rule. It will work even if you have a lowercase e here, but then follow the convention so that your code looks neat. For data, data always starts with a lowercase.
Now if you have two words like salary of employee that means the next word will start with the capital letter. So you can have data like this salary employee or something like that then the next word will start with the capital letter but if you have only one word it's all lower case. Same thing goes with the method name you start with the lower case but the subsequent words you will have capital letters like calculate total pay t and p are capital.
So here also if you notice The double total pay, the T is small letter, but the P is capital letter. Also, if you have like lots of classes opened here and you feel like, you know, I want to close the others, you can just right click on the class and say close others. So you'll have only one class open at a time. One of the most frequently asked questions when it comes to object oriented programming is the concept of class and objects. This is especially true for people who come from like COBOL or Fortran background.
Now everyone understands the concept of tables and records and columns and all those things. So in this video, I will explain the concept of class and object using a table, a database table. So imagine you have a table called employee, which has these columns called employee ID, employee name, employee salary, employee bonus and employee total pay.
And this table has a bunch of records like record number 100, employee ID 100, Alex Rod and some salary and some bonus. And then the employee total pay is empty for now. And imagine you have to fill up this employee total pay by adding the employee salary and bonus and the calculated amount should be inserted in employee total pay. Now if you are a database guy, you will go ahead and write a procedure to fill up this employee total pay. by calculating the sum of employee salary and bonus.
So whatever you do in a database, now we are going to learn how to translate it into Java classes and objects. Here you have a table called employee. So what we'll do is we will create something called as a class called employee. I created a package called day6.class and object. So I'm going to create a class called employee.
So when you have a table called employee you have a class called employee. Next you have these columns called employee id, employee name, employee salary, employee bonus and employee total pay. So we will have them as data in our class. Because you have employee id column which is going to be an integer I'm going to say int employee id.
We are going to have a text employee name so it's going to be a string employee name and then we will have employee salary bonus and EMP total pay. Since they are all like numbers which will have decimal places I have put the data type double. So all the columns in a table are represented as data in a class file. Now let us see how to represent these records.
So I have a record called 100 and a record called 200 with these data. How do we represent them? Every record is represented as an object.
Let me create another class. where I will create the objects. So a class called test employee with public static void main. To understand this public static void main and test please look at the previous videos. Now every record in the table is represented as an object.
The way we create an object in Java is like this. You say employee, give some name for the object, equal to new employee. Now we have to give values for that record.
Values like employee ID is 100, Alex Rod and all those things. So the way we give values is, you say, Alex that is the record dot employee ID is equal to the value 100. Alex dot the employee name it's a string so within double quotes you can say something like and Alex dot employee salary and employee bonus according to that particular record. Like this we can create any number of objects or any number of records just like we have it in a table. So for Linda, we can create records like this too. Now in a database table, if we were to fill up this employee total pay, we would use something like a PL SQL procedure or a trigger.
Basically a method inside the database where we can add the employee salary and bonus and then we can fill up this employee total pay. So in Java, how do you write a procedure? In Java, when you want to do something like that, you call that a method.
So you go back to your employee class and let us write a method which will do that calculation. So here I'm going to write that method which says with a name like calculate total pay, which is going to calculate. plus emp bonus so this is how you write a method give some name and inside that you will put the particular logic just like you will do a procedure in a database table now to call this particular method to do this calculation all you have to do is go to this test employee and say alex dot call that particular method calculate total pay and say linda dot calculate total pay.
So now what happens is this when you call alex.calculateTotalPay it will go to this particular method and it will calculate the employee salary and bonus and it will add up the employee salary and bonus and put it in the employee total pay of alex because alex is the object that calls the calculate total pay. So it will do the calculation and it will fill up the answer right here. So if you want to print it out let's use the system route.println alex.emp total pay same way we can print out a dot emp total pay so now when we run this right click run as java application so you can see the result for alex it's hundred thousand three hundred and forty five point seven eight and for linda it is 110 000. so those who are new to programming we have just seen how to translate the concept of a database table into a class and You take the table name as the class name and this employee ID, employee name, all these column names as the data names. Table name as class name, these are the data names.
If you have some calculated value, that will be your methods. If you have some inbuilt procedure, that will be your methods. So you need a method name and put your calculations inside that particular method.
This method returns nothing, void. Of course. If you want to understand void, you have to buy the video, patrickvideos.com.
And we also saw like how to create records. That is, you create a record name, like say employee, Alex equal to new employee. And then you give those parameters that are required for a record, like Alex.employeeID is 100, employee name is AlexRaw and so on.
For the calculated value of total pay, you call that method employeeTotalPay, which will calculate that employeeTotalPay column. So these column names. are called data and these are called methods. So a class has usually data and methods. And this Alex Linda, these records are called objects.
So an object is nothing but a copy of a class because when a class has employee ID name salary bonus the copies also have those data. Just like you have these column names you're going to have data for every column. So an object is nothing but a copy of the class. So I hope that you'll be able to understand the concept of class and object better using this example.
So in object oriented programming the concepts are very similar to database programming. So just like you can have multiple tables you can have multiple class files and just like you can have relationship between these tables that is an employee will belong to one department and a department will have like one to many relationship with employees, a department will have many employees. So like this, you can create relationship between class files. Next, let us look at the concept of a method returning a value.
We saw in this particular method called calculate area, we are returning void that means this method is not returning anything. It is printing the area right Now if I go to the main method and imagine I want to fetch the area of UPS and fetch the area of FedEx and I want to add up both the areas. How will I go about adding the area of UPS and area of FedEx?
I need to access the area of UPS but the area of UPS is printed here but I need to access this particular value in this main method. So to access this particular area value in this main method, I need to return this particular area to the main method. So here, even though I am printing area equal to area right here to the console, I can return the area to the main method like this.
I can say return area. When I say return area, that means this method is now going to return the area back. to the main method and when you say return area you should make sure that instead of void you will have to put the data type of the return variable so area is of type integer so you will have to put the data type of area which is an integer so this calculate area is going to return an integer which is nothing but the area of this particular box so when you return this particular area I have to catch hold of it Somewhere so here I will declare something like int area1 equal to I would declare something like int area1 equal to ups.calculateArea.
So when ups calls calculateArea and the area is calculated that is 10 times 5 it is printed area equal to 50 but at the same time the 50 is returned to the main method. and it is stored in this int area1. Same way I'll have another variable called int area2 equal to FedEx.calculateArea.
So when FedEx calls calculateArea it multiplies 6 times 7 and area equal to 42 is printed and then the 42 is returned back to the main method and stored in area2. Now I have access to both area 1 and area 2 right here in the main method. So I can say total of UPS and FedEx is equal to area 1 plus area 2. I'm adding this area 1 and area 2 within the parenthesis so that it just does not concatenate the area 1 and area 2 to this particular text. So this will add up the area 1 and area 2. So it will add up 50 and 42 and that is 92. And it will print out this particular statement.
So you can see the advantages of returning a value I want to catch hold of the area of UPS and area of FedEx in this main method so that I can calculate the total Area of both the boxes. I cannot do it here because when this is called either UPS or FedEx is calling it So I have to catch it somewhere else. So I will do it right here in area 1 equal to whatever the result of this calculate area and area 2 whatever the result of this calculate area and I can add these two and print the answers.
So let us save everything and run this. So here you can see that the area 1 is for UPS and then area 2 is for FedEx, this area 2 FedEx and then when it comes here it takes this area 1 and area 2 adds them up and prints out 92. Now we saw how to return values from a method. So the next we will see If we can pass values to a method, that is, can I have some value passed to this method? Yes, I do have length and width right here, but this length and width is called a class level data. But the question is, if I can pass some values to the method, imagine I have something like integer x, and I want to use that x to, like say, multiply this length times width times x.
So I can't say something like integer x and to this calculate area I can pass the value x. That is I can save here and in the test box I can pass some value like say 4, 2. So now what happens is when ups calls calculate area it will pass this number 4 as x here. So the 4 will come here length times width times 4. 200 will be printed.
Same thing happens for this fedex.calculateArea. 2 is passed to x and then it prints out like length times width times x that is x will be 2. So let us run this and you can see that area equal to 200 area equal to 84 and then this is the total area. So you this way you can pass values to a method if you want to pass some values to a method. This integer x is called passing argument. arguments.
Now sometimes you will see something like this. You will see like integer length, integer width. I'm going to remove the x.
So now what happens is this calculate area is requesting two numbers. It is requesting two integers. That is one length and one width.
So imagine I'm going to give this length and width. So if it is request to numbers, you have to give two numbers. So I'm going to say like 4, 3. And same way I'm going to say like 2, 5. So now let us run this and see.
I'm going to right click, run as Java application. So suddenly you'll notice something. It has taken 4 and 3. It has passed 4 to length. and 3 for width and it has calculated the 4 and 3 and it is printing the area as 12. Same way it has taken the length and width as 2 and 5 and it is calculating the length and width as 10. So what happened to the other numbers? What about 5 and 10 and 6 and 7, the length and width?
Remember, when you pass some arguments, the passing arguments takes precedence class level data when you say ups dot length this is your setting the class level data but when you pass some data and if both of them match that is both of them clash like this have the same name that means it will take the passing arguments so it will take the length and width of this calculate area you can always check which length and width it is picking up By double clicking on this length you will see that immediately this length is highlighted and you double click on this width, this width is highlighted. So the next question is what if I want to use the class level data? What if I want to use the 5 and 10 and ignore the 4 and 3?
You can if you want get the class level data and ignore the local data by using the keyword this. When you say this.length. And this dot width, that means it will take the class level data, that is whatever is assigned here, instead of taking the local data. So now if you highlight length, you can see that this length is highlighted and this width is highlighted. Now let us run this, go back to test box and run this.
You can see how it has gone back to 5 and 10 and gone back to that 6 and 7, ignoring this local data. So we learned how to pass data to methods and if you have a clash between passing arguments and class level data we learned like how to access passing arguments or how to access the class level data. Next let us look at the concept of constructors. I'm going to create another package in the source day2.constructors.
In this package I'm going to create a class called smallbox And in this small box, I'm going to have some length and width. And let me have a method called calculateArea. So I have this class called small box with length and width and then I have a method called calculate area which is going to print out area equal to length times width. So I'm going to test this box. So let us create another class.
Here I can create an object of small box. Now I have an object of small box. I can assign values like length and width.
Now imagine without assigning values like length and width if I call calculate area what will happen. Without assigning any values of length and width, if I call calculateArea, it will take the default value of length and width. The default of integer is 0. So it will take 0 times 0 and print out 0. It printed out area equal to 0. But imagine I want like some kind of a default value of like say 5 and 6. By default, all boxes are of length 5 and with 6. I want to specify some default values.
If that is the case, I can use something called as a constructor. So what is a constructor? A constructor is a method that has the same name as the class.
It is executed when an object is created. It is used to set default values. So let us look at this. A constructor is a method that has the same name as the class. That means, let me create a method.
So this method has the same name as a class. A constructor does not return anything, including void. You cannot even put void in front of this.
So here, in this constructor, a method which has the same name as a class, I'm going to declare the length and width as this. I'm going to say the class level data, that is this.length, Equal to Phi I'm also going to have a print statement called constructor fired I'm going to have a print statement called constructor fired so that I will know when exactly is this method executed now Save this go back to that test box and run this particular method now here You can see that the area is 30. So what exactly happened here? We had a method which has the same name as the class.
This is called a constructor. It's a method that has the same name as the class. When did this method execute? It is executed when an object is created.
Where exactly is an object created? An object is created right here. You are saying like small box ups or small box object equal to new small box. So when this line is executed, the constructor is automatically executed. If you have a method that has the same name as the class.
It automatically executes it. So in this method, you are setting the length as 5 and width as 6, and you are saying constructor fired. So in this particular line, the length and width of this object is set, and the constructor fired is executed.
So when you say object.calculateArea, when the object calls calculateArea, it says area equal to whatever the length and width that was set, it will be executed. The use of a constructor, is in setting some default values. I want to allow people to create objects, but if they don't give the length and width, I want these default values of 5 and 6 assigned.
You can have like constructors that accept values too, like this. You can have this constructor small box which accepts length and width, and whatever length and width comes in, I can say this.length, that is the class level length, is equal to whatever length comes in. And this dot width, that is the class level width, the class level width is whatever the width that comes in. So to set this small box, to call this constructor, imagine I create another object.
Now imagine I want to call this constructor. This constructor requires a length and width to be given. So if I simply create an object, say, Small box ups equal to new small box if I don't give any values by default it will call this Empty constructor that is a constructor which requires no values, but if I want to call this constructor That means I have to give a length and width so here I can give something like 3, 4 so when I give 3 comma 4 The 3 is passed to length and 4 is passed to width the 3 and 4 are set and when ups calls calculate area It will take the 3 and 4 as the length and width because the 3 was set as the class level length and 4 as set as 4 was set as class level width. So 3, 4 tool will be printed here. So let us go back to test box, right click run as Java application.
Remember to always save your classes before executing and always execute from your main method. Do not try to execute from a class which has like data and methods. You should execute from the class which has the main method and always save after making every modification.
So here you have this object calling calculateArea and printing out 30 using the default 5 and 6, and this object calling the other constructor, which takes in a length and width, and it calculates the area as 12. So a constructor is a method that has the same name as the class. It is executed when an object is created, It is used to set default values, usually to set default values, and it does not return anything. Remember, do not put void in front of these methods.