Arithmetic Operators

Learn the Basic Java Concepts Working with variables and operators
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.