Hello there, and welcome back to the Java development course. So last time we went over strings and get some more work to do, your job was to take two strings, combine them and then print the those strings in all uppercase letters. So here's what you should have done. So I have two strings and be assigning just some random values with just some string. Then I have a new string C, which is a plus b. And that just print c.to uppercase and so there's all I get uppercase letters, so we can actually run this right now.
There we go. I get the string and uppercase. So pretty simple homework, nothing too difficult. And yeah, so without further ado, then let's get into the lesson. Alright, so today we're going to be going over the arithmetic operators. So let's create a new class.
Rs, whoops. arithmetic, arithmetic, operators. Raiders, there we go. I can't type today as always There we go arithmetic operators. So in Java, there are a couple basic arithmetic operators. I'm going to create a comment here.
There we go, okay. So there is the plus minus times divided by and what? Okay, so the divided by doesn't really work. There we go divided by, and we have modulus as well. Okay, so those are the basic arithmetic operators in Java. So there are a couple other ones we have plus plus and don't worry, we're going to go over all of them in just a second here and minus minus as well.
Okay, so now let's go ahead and take a look at each one of them. So we're going to create int a equals 12 equals 12. Okay, so first of all, we're going to print do System dot out dot print ln A plus 12. This is gonna print 24. And I can prove it to you There we go 24 Okay, System dot out dot print ln A minus 12, this is going to print zero. And I can prove it to you as well, zero, there we go.
We also have System dot out dot, print, print, whoops, print ln A divided by 12. This is going to print one. So I mean, this is just division, I mean, at this point, these are just very basic operators there used to understand them. And then System dot out dot print ln A times 12, which is going to print what's 144. All right, and there we go. 144.
So as you can see, there's just the basic operators we also have, of course, the percent sign so system, dot out dot print ln, this is called the modulus operator. So what this does it It does division. So in this case, divide 12 by seven, and then it's going to print the remainder of whatever's left. So in this case, it's going to divide a by 12 by seven, seven goes into 12 once, and then there's a remainder of five. So this is going to print five. Let's go ahead and run this.
There we go, we get five. So just to give you a better idea of how exactly this works, we can do 10 divided by three. And this case, it's going to give us one, since three goes into 10, three times and there's a remainder of one. So three times three is nine. And so there's a remainder of one and so we get one. All right, so that is the modulus operator a percent, seven, five, okay, so that is the modulus operator.
Now, there are a couple more operators that we need to go over. So you're only speaking What if we have an integer a right so we have an integer A, which is equal to 12. So what if we now want to A to B equal to 13. Well, what we would do is we would have to do int a is equal to a plus one. So I'm sorry, no, you don't need to specify the name. If you're 30.
You know, the variable type if it's already created, see a is equal to a plus one. Now, that was a problem, right? So A is equal to a plus one, that's quite a bit of code. So you would think that you could just do a plus one, and then that would automatically add it to a and make a 13. But it doesn't really work that way, we have to do a plus one. And so I'm sorry, no, eight equals a plus one.
And so we say that A is equal to A which is 12 plus one which is 13. And so that's how we, you know, add one to eight. But we have another operator called the increment operator, which is just a plus plus, just like that, and what that will do is just add one to eight. So now If we print a we will get there we go 13. So we do a plus plus a is 1212 plus one is 13. And then we print a 13.
There we go. And same thing with the decrement operator. So instead of doing a plus plus, we could just do a minus minus, and now we get 11 instead of 12. That just, you know, takes away one Alright, so let me go to actually comment these as well. This is going to be what is it gonna be 11. Okay, and that's, that's going to be 11.
And then this is going to be 13. There we go. Okay, um, so that is how we work with variables. And so that is generally all of the earth arithmetic operators that there are in Java. Okay, so now we're going to go over a very simple program, we're going to write it together. That's gonna take actually take some input from the user ones, I'm gonna uncomment all these out.
And we're actually going to go over how we can actually get input from the user and then go over how exactly that works. All right. So, again, we're gonna write a program, which will take two numbers from the user. And print those two numbers added, multiplied, subtracted, and divided. So how are we going to do this? Well, we're going to go ahead and first of all, we're going to learn how to get input from the user.
Well, we have something called the scanner class, scanner class. We create us a scanner by just doing scanner and then scanner Yeah. And then giving it a name just like any other variable scanner, SC and then doing new scanner. There we go. Okay, and now we'll get an error in Java. Because we need to import scanner.
So Eclipse has this handy function, not function feature where you can just click Import scanner and it will import it for you. And if we go up here we have import java util scanner. So what does this import mean? Well, Java is actually a very big language. So if you actually take a look at its total size, it's around, I don't know, maybe a couple, a couple hundred megabytes or so. And we don't really need that entire language for our application.
So a lot of what we'll be writing doesn't need a lot of the libraries built in libraries that Java has. And so in results, we don't it only comes with like the bare bones stuff. In this case, scanner isn't part of that barebone stuff in result, we just have to import it. So importing just means that We are going to be using this class, which is import java. That's a package dot util, that's an inner package. And then scanner.
That is the actual class that we are importing. Okay, so here we are just creating a new scanner. So scanner is just like any other variable. It's a complex variable. Or remember we went or string string as a complex variable scanner is a complex variable as well. So scanner, and then we just variable type, variable name, and then we do new scanner.
So this is a little bit of a different than what we usually do. In our case, we usually just do a equals one. But in this case, this is actually called calling the constructor or initializing a variable. We'll go over exactly what constructors are. And we'll actually be creating our very own constructors inside our very own classes later on in this course, but for now, just understand this is just how we declare complex variables. We can have to do the same exact thing with string we can do string G equals new string here.
There you go. And as you can see it's a that's that's also a way we could do it. Okay, so inside our scanner, we're going to put where we're going to draw our user input from. So we can try it from a file from a server, and so on and so on. In our case, we're actually going to call it from a, from system.in. Just about in is actually the console.
So you know, when we print something to the screen, we do System dot out dot print ln, well, system.in is just the console, but you know, taking variables inside. Okay, so that is a scanner, if you run an application now, nothing happens because you don't actually get anything from the scanner to get something from the scanner, we have to do int and then assign as your some value. So g equals SC dot, next int. There we go. And now if we run our application, as you can see, it doesn't actually stop. It's waiting for us to input something.
So let's input five and then press enter. There we go. So now it is it you know, it stops terminate the program. So we can actually just do System dot out dot print ln Jeep g plus one. There we go. So now if we run this, we'll put five, and we get six back.
So that is essentially how we use a scanner in Java. Now we're going to use this to actually create a replication. So we're going to get the first variable. So we're going to get two integers, which we are going to then subtract, divide, multiply and add together, and then return the results of all four of those operations. So let's go out and do it together. We're actually I think this I think together, we're just going to do multiplication and subtraction, no addition and subtraction.
And then for homework, you're going to do multiplication and division as well. Okay, so we get our first variable in G equals to C dot next int and then int g one Let's actually call our variables just some something meaningful. And first value, first Val, and then second Val equals SC dot next int. There we go. Remember, we don't actually have to just get in, we can actually, there's a lot of different things that we can get Next, we have int, we have line, which is a string, we have byte. If you remember, data types, we have short.
We have big decimals, which is a completely different story altogether. We have doubles, Boolean floats, and so on, and so on. So there's a lot of different things that we can actually get in this case, so we're just going to get next. All right, there we go. And so now we have to actually create our values. So we're going to have it added and subtracted int added equals and then int and subtracted, equals, there we go.
So it added it we're gonna have first Val Plus, second. There we go. And then subtracted is going to be first Val, minus, second Val. And there we go. Now we're going to print system, system dot out. dot print ln.
We're going to print added. And then we're going to print. subtracted. I think I misspelled track. No, I did it. Okay, there we go.
All right. So now if we were an application, as you can see, it will go ahead and wait for us to input the value. So we're going to print, we're going to input four, actually five. And there we go. So we get nine because four plus five is nine, and then four minus five is negative one. All right.
So there we go. We've already ran a pretty useful program. So we utilised a lot of what we went over in this course. And in this lesson. Okay, so without further ado, then let's send you some homework. Alright.
So from homework, your job is to take our program that we wrote together, and then add in the multiplication division and modulus operator as well. So it's pretty simple. It should take you maximum 10 to 15 minutes at most, if you really understand the concept. Okay, one thing to understand though, when we're actually going to be going to be printing this, there's going to be a lot of different numbers. So what's really good is if you can have system out print ln, and then we can do first Val, and then plus. And then we can do plus, plus, second Val, plus, equals and then plus added.
So let me go and do that as well for the subtracted one as well. So just change this to minus. So this way, we'll get we actually go to do it. Let's run it Hey data. Okay, there we go ran. Let me see exactly what's happening here.
There we go. Okay, now it's ran. Now let's do five and then six. And there we go. So we get five plus six equals 11. And then five minus six equals negative one.
There we go. That's also something you can do just so that there's less confusion and it just looks a little bit better. Alright, so without further ado, then I wish you luck and I'll see you next time.