hey welcome to draft Academy my name is
Mike in this tutorial I want to talk to
you about working with numbers in Java
numbers are extremely common datatype
and when you're working your java
programs a lot of times you're gonna
want to work with numbers so I'm gonna
show you all the different things you
can do with numbers we're gonna talk
about how different types of numbers
interact and I'll show you some awesome
math methods that we can use to perform
more complex math operations on the
numbers in our programs so let's get
started here's the basics I'm if I just
wanted to print out a number on to the
screen I could just like type in any
number inside of this system dot print
line statement and it'll just get
printed out onto the screen but let's
say that I wanted to do something a
little bit more complicated what if I
wanted to add two numbers together well
if you want to add two numbers all you
have to do is just type out the numbers
that you want to add and the operator
that we want to use so I could say like
3 plus 4 and now instead of just
literally printing out 3 plus 4 job is
gonna go ahead and execute that
operation for us and it's gonna print
out the sum of 4 and 3 so you can see
here we get 7 so in addition to using a
plus sign we could use a minus sign that
I'll subtract the numbers and you also
see that Java has no problem with
negative numbers so if I take 3 minus 4
we're able to get negative 1 and it'll
display just like that so we can use
subtraction we could also use division
and division is just going to be this
forward slash and then we can also use
multiplication so let's go ahead and
divide these numbers let's take 3/4 and
you'll see that we get 0 now here's the
first distinction that I want to make in
Java there's two different types of
numbers
there's integers and then there's
decimal numbers and there's a couple
different data types that we can use for
decimals but let's just say that they're
doubles because double is the most
common datatype for decimal numbers
because I'm dividing two integers in
other words 3 is an integer in 4 is an
integer I'm getting back an answer
that's also an integer so 0 in
an integer now normally 3/4 would give
us a decimal answer but since I'm
dividing the integer 3 by the integer 4
I'm only going to get an integer answer
back if I was to come over here and say
three point zero divided by four point
zero now I should get a decimal answer
back so you'll see now we get zero point
seven five which is the actual answer so
whenever you're doing math inside of
your java files you want to always make
sure that you know what data type you're
using if you're using integers you want
to make sure that you're okay with
getting an integer answer if you're
using doubles then you want to make sure
that you're okay with getting a decimal
answer so those are just some basic math
operations that we can do there's one
more I want to show you it's called the
modulus operator so I could say 10 mod 3
and that's just this percent sign right
here what this is gonna do is it's gonna
take 10 it's gonna divide it by 3 and
it's gonna spit out the remainder that
we get so 10 divided by 3 is 3 with a
remainder of 1
and so we should now get a 1 over here
on the screen so in certain
circumstances you're gonna want to get
the remainder of a couple numbers
getting divided and you can use the
modulus operator to do that so in
addition to just printing out numbers we
can also store them in variables and I'm
gonna create a couple different
variables we'll create an integer and
we'll just call it my int and we'll just
set it equal to 3 and then we'll also
create a double call of my double and
we'll just set it equal to 5.5 and I
could just come down here and use my
integers to do my math so I could say
for example 5 plus my int and basically
what this is gonna do is it's gonna say
5 plus 3 so we'll end up getting 8 and
you can see we get 8 over there here's a
question though what happens if I add a
integer and a double together so let's
say if I wanted to add my double and my
aunt we're actually gonna get back a
double answer so anytime you perform
math using a double and an integer
you're always going to get
to answer as a double so you see here
we're gonna get back eight point five
instead of just a normal eight so you
can use doubles and you can use ants to
do your math and I want to show you guys
some other math operations that we can
use now there's a lot of more complex
math operations that we might want to be
able to use and Java has this thing
called the math class and the math class
is basically just a huge collection of
methods that we can use to perform more
complex math on our numbers and don't
really worry too much about what a class
is we're gonna get into classes later in
the course but a method is basically
just a chunk of code that's gonna
perform a specific operation so there's
a lot of these methods for different
math calculations and different math
operations that we can use to do them in
our programs so the first one I want to
show you is gonna be a method that allow
us to get the absolute value of a number
so all we want to do to access these
math methods is just say math and that
needs to have a capital m and then I'm
gonna access the method that I want to
use so I can say math dot abs and abs is
a method which will basically give us
the absolute value of a number so inside
of these parentheses I actually need to
give this a number so I'm gonna go ahead
and pass in my double and what this will
do is it'll give us the absolute value
of the number in here so I'm gonna make
this a negative five point five so the
absolute value of negative five point
five is just gonna be normal five point
five so that's allowing us to use
absolute value we can also use one for
power so I could say P o W here and I'm
actually going to use the in so say my
int so I can pass in the number that I
want to take to a specific power and
then I can pass in the power that I want
to take it to so if I said three
basically what we're saying here is we
want to take three to the power of three
so we want to get three cubed and over
here on the screen C we get 27 because
27 is
three cubed right three times three is
nine nine times three is 27 so you can
use this POW method in order to like use
Exponential's we can also do the
opposite of this so we could say s QR T
and s QR T stands for square root so I'm
just gonna give this a number why don't
we give this like 36 so s QR T of 36 the
square root of 36 is going to be six and
we'll just get that back over here
there's also some other useful functions
another one I want to show you guys is
called min and min will basically take
in two numbers and it'll tell you which
one is the smallest one so if I passed
in like two and six this min method is
going to return the smaller of the two
numbers so let's run the program you can
see it returns two so in a situation
where we had two numbers and we didn't
know which one was smaller so for
example I could say 2 and over here I
could say my int maybe I don't know what
number is stored inside of my int so
this number this min method could tell
us which one is smaller you can also use
max so max of 2 in my--in is just gonna
return 3 because my in has a value of 3
which is bigger than 2 so that's the max
function there's a couple more I want to
show you guys another one we can use is
round and round is basically gonna take
a decimal number and it's gonna round it
so I could pass in my double and this
will round my double using like normal
rounding rules so let's run this you'll
see we get negative 5 so it's actually
rounded the number down and you know you
put any number you want in here so for
example I could put like five point nine
and Java is gonna go ahead and round it
up another useful method is called
random and basically what this is gonna
do is it's gonna spit out a random
number between zero point zero