what's going on everybody it's Bucky and welcome to your very first java tutorial now in this
order before we begin writing any programs we first need to download something called
the JDK and for all you new Java developers out there this is the program that lets you
actually write Java and actually compile it and compile it just means a transferring it
from code you can understand the code the computer can understand so just follow me
and by the end you'll be able to write Java code so the first thing I need you to do is
go ahead and open up your internet browser right here and once you have that open I'm
going to use fire fact Firefox go ahead and go to the website Java dot sun.com and this
is gonna give you give you Javas main page what you want to look for is the download
link and this is gonna give you a bunch of different Java editions to download you're
gonna want to go to the Java SE link and this stands for a Java standard edition so go ahead
and click that and what we're looking for is something called the JDK in this stands
for Java development kit so we can write in a well compiled Java programs now we just
want the top one which is a standard one on this EE stands for Enterprise Edition this
and that beans is something else we don't want that so just go ahead and find the standard
basic Edition and click download and once you click that it's replicant bring its a
new page let's see and yep and it's gonna say Windows which we want English which we
want go ahead and agree and press continue I already have this downloaded so I'm not
going to download it again though just be stupid but once you download it is gonna give
you an executable file now um your executable file is going to look something like one of
these this is something else I downloaded but it's gonna give you a file like this called
e x e once you get that file go ahead and double click this and that's going to start
the wizard to download the JDK now it also might give you a file on your desktop right
here like put it somewhere right here if it's right there Jan just double click that and
once you do a Wizards are gonna pop up and you're probably gonna have to click like next
40 times and press I agree I agree to some stuff next next but once that's all done then
you're gonna have the Java compiler downloaded now let me show you guys what the Java compiler
is if you go to your Start menu then press CMD and press Enter your command prompt is
going to come up now what this allows you to do is have access to your java compiler
and again like I said before to duck the Java compiler allows you to transfer code that
you can understand to code the computer can understand and that's what we just downloaded
so let's test it out Java C press Enter nothing's working and nothing's working because the
Java compiler it has it downloaded but whenever you try to use it it doesn't know where it
is so let me show you guys how to do that and again pay very close attention to this
hopefully you downloaded it with all the default settings if you didn't then while you're still
gonna be able to see what's going on but go ahead and in your Start menu click on computer
now go ahead and click the local disk witch or whatever your main hard drive is then go
ahead and click program files now you should have a folder called Java go ahead and click
that now go ahead and click the Virgin JDK if you don't see this that means you didn't
download it right so go ahead and click that JDK the next thing in last thing I want you
guys to do is click on the thing that says bin right here and remember you follow all
these instructions very carefully next right click on any of these things right here just
click on your topmost run remember right click it and click properties now this is where
you're going to tell Java to look for your compiler so it says location go ahead and
copy all of location right clicking be it it should say something like C program files
Java your version and say been the last thing make sure you get that bin that's very important
if you're not if you don't see it then do this tutorial again and do exactly as I say
anyways you want to see that bin so once you have this golden code that's what we're looking
for go ahead and X out of that now that's where you the compiler is so how do we tell
your computer to look there well go ahead and go ahead to click on my computer again
from your Start menu and click properties remember you want to right click it and click
properties all these are very important go ahead and click advanced system settings and
probably a little message is going to pop up if my computer is not freezing and it is
so my computer properties advanced system settings there we go continue and you're gonna
see something in your system properties called environment and variables go ahead and click
that and pay attention what I'm gonna do here what we want to do is create a new user variable
so go ahead and click a new now the variable name for this you have to write pass PA th
and the variable value is going to right where that compiler is remember that link with a
bin so that's why it's really important that you get that link now go ahead and click OK
and bam click OK on this ok and you now have your compiler setup and ready to run so go
ahead and go back to your command line and in command line go ahead and type Java sea
and unlike before we now have a bunch of crap that pops up and this crap means that our
compiler is ready and set and ready to go so now with that being said we know that we
are now able to write Java programs so in the next tutorial I'll show you guys how to
do that but for now just follow this tutorial that's how you install a Java on your computer
so you can actually write Java programs if you didn't follow those instructions exactly
like I said you're not going to be able to write Java programs on your computer so make
sure follow this tutorial very carefully do exactly as I did I know it's kind of a pain
in the butt but it'll be worth it in the long run so um thank you guys for watching make
sure you get a job in salt if you have any questions or I wasn't clear on anything just
send me a message or leave a comment and hopefully I'll get back to you so again thank you guys
for watching hopefully you have Java installed and in next tutorial I'll show you guys how
to write our first Java program so thank you don't forget to subscribe to my channel and
I'll see you in the next tutorial what's up everyone its Bucky welcome to your second
Java tutorial and in this tutorial I'm gonna be teaching you how to run a Java program
now I'm not gonna be teaching a lot of the syntax about what's in a Java program before
we do that we actually needed to know how to run one so pretty much you're just gonna
be copying me write a simple program and then you're gonna be learning how to run it so
I'm go ahead and open any editor I'm gonna open notepad plus plus just because it's my
favorite one and uh go ahead and the first thing you want to do is go to file save as
and we want to save this and remember whatever location I already made a file on my right
off my hard drive in there I have a folder called test and I'm gonna save this as let's
save it as YouTube YouTube da and of course that Java is very important and don't forget
where you're saving this this is also important so now we have a file called anything dot
job but mine's called YouTube Java now I'm pretty much just going to show you a simple
hello world program again in this story I'm not gonna showing you like how to write Java
I'm just be showing you how to run it and then the next couple tutorials I'll show you
what everything means in it but for um beginners just follow me the first thing you need to
write is a class now a class in Java pretty much in Java everything begins with a class
and if you don't have a class you can't do anything so you can't do anything until you
create a class so I'm gonna go ahead and make a class called apples and in that class we're
gonna have something called a method now again you don't really need to know any of this
stuff I'm just making a simple program I'm just talking you along you'll understand what
all this means later but for now just stick with me now in your class you're gonna have
something called a method now a method is pretty much a list of things to do and this
method is going to be inside the class now the first thing your computer does is whenever
it runs a Java program and looks for a method called main so let's go ahead and create that
and you did this by clicking public static void main and all of these actually mean something
but uh again don't worry about it for now I promise you're understand then in your parameter
type of string args arg s and put empty square brackets and uh i don't know if i can copy
this code in my description or not but if I can't I'll put it in my description but
I got a feeling YouTube's not gonna let me put code in for security reasons next after
in your method you're gonna want to have to put um a statement or instruction about what
you want your computer to do so go ahead and write system period out period print Ln and
what this does is pretty much print a line on your computer now in your parameters go
ahead and write whatever you want hello YouTube now go ahead and save this and you can go
ahead and find a HelloWorld program from somewhere on the internet again I just uh talk to you
guys through that real quick I promise uh uh I'm not it this isn't even part of the
tutorial the tutorial is just to show you how to run this so I'll be teaching you about
all this means later so go ahead and this is where the tutorial really begins go ahead
in the command prompt and the first thing we need to do before we run this program is
actually compile it now when we compile a program it pretty much takes what we written
and changes it into something called a class file now your computer can't read any files
called Java it can only read dot class so that's a compiler job to take that dot Java
and change into something dot class that your computer can understand so right now what
we need to do is find this file and go find it you use something called CD backslash and
what this does is take you to the very beginning of your hard drive and this is just a quick
tutorial if you never use command prompt for once you're in your C Drive press dir and
press Enter this is gonna give you a list that's everything in your C Drive as you can
see one my directories or folders is called test so let's go ahead and press CD which
means change directory to test now we are in the test so again from your C Drive press
CD test now press dir and see what's in this and let's see YouTube Java so how do we compile
this YouTube Java well once you're in your test go ahead and press Java C which means
Java compiler and then press YouTube dot Java and this means I use the compiler on YouTube
Java and then go ahead and press enter and what your computer does is now if you press
dir we now have a apples dot class so what it does is take that Java file and change
it into a class file depending on whatever your name your class remember I named mine
apples so now that we have a class file a file that the computer can understand we can
actually run this program so let's go ahead and type Java and that means run this program
and then type in the name of your class which is apples and now when you press ENTER it
says hello YouTube so that's a quick tutorial on how to run simple Java programs and again
I can't stress this enough don't worry if you didn't get that HelloWorld program that's
not even the tutorial I wanted to teach you guys today the tutorial was learn how to run
these simple Java programs from the command line and in the next tutorial I'm gonna be
clearing a bunch of things up so just practice running a simple program I suggest you just
go ahead and copy a HelloWorld program from the internet somewhere and once you do just
practice running it and if you have any questions you can just send me a message on youtube
or leave a comment and I'll try to get back to you but thank you guys for watching hopefully
now you know how to run a simple program even though you don't know what it means yet so
again thank you guys for watching make sure to subscribe to my channel and I'll see you
next tutorial what's up everyone it's Bucky and welcome to your third java tutorial now
in this story only showing you guys how to download something called a IDE where we're
going to be writing our Java code so again there's one more last thing I need you guys
download I promise and then we're actually going to be start program in Java so I'm in
the less it's order I showed you guys how to create a program in any notepad or any
text editor and how you can run it from your command line but the only problem with this
and it's kind of a big problem whenever you have program like this in a regular text editor
and say you have like a thousand line program whenever you try to compile it and run it
if something is wrong with it you have no idea what's wrong and you don't even know
where to look therefore it's sometimes useful to get a better editor just for Java made
specifically so that's why we need to down something download something called I de and
I'm going to show you guys how to do that right now go ahead and open up your internet
browser and go ahead and click eclipse.org in eclipse is pretty much a special text editor
for only Java so once this loads go ahead and on the main page you're going to see something
called downloads go ahead and click downloads now this is going to give you a couple on
downloads to click from what you want is either the first one or the second one I got the
first one just because it includes a little more and go ahead and if you're on Windows
go ahead and click Windows and what this is going to do is it's going to download um an
Eclipse IDE and that's pretty much arm pretty much the text editor for Java I'll show you
guys what it is in a little bit but where it's going to be is if you go to computer
and then go to whatever wherever your downloads are you're going to have something called
a zip folder what you need to do is extract you're going to have a folder like this right
here we need to do is right-click it and press extract all then once you click extract all
it's going give you a little wizard once you extract all that information you're going
to get a regular folder like this this is pretty much like a compressed stuff and get
all the information that you need to extract it that's what you do with any zip folder
by the way now once you have your regular folder you can double click it and now your
Eclipse program is in this and it's the little one that says application with a little Eclipse
logo next to it and what I did is I right clicked it and I created a shortcut or you
can press send a desktop and this gives you instead of having to go through your computer
hard drive you have a nice little shortcut on your computer right here where you can
just click it and start writing Java programs so again it's just a basic download I just
want to show you guys walk you through a little bit but once you do all that you have the
Eclipse program on your computer so double click that and your Eclipse program starts
running just select a default workspace anyone is fine and what this program does is allow
you to edit code so in another cool thing about this it has a compiler built right into
it so we can just run our code right from the same program you don't have to mess around
in the command line so what we need to do first is go ahead and press file new project
or Java project doesn't matter one go ahead and press a Java project now you need a project
name I'm going to name my Bucky and go ahead and press next and then go ahead and press
finish you probably could press finish but now you have a project named Bucky and it
just puts everything into a project so you can if you're working on like a different
Java thing for a different company but for here we're only going to be working with one
project so we only needed to do that once next you have a project called Bucky on the
left hand side of your screen and go ahead and press this little arrow and it D collapses
or expands this little pain and you're going to see something called a JRE system library
and a source we want to work with the source file so go ahead and right click this and
press new class since we want to create if you remember everything is in class in Java
so go ahead and click this and you got a neighbor class something go ahead and I'm going to
name mine apples and go ahead and press finish now as you can see it already starts writing
the code for us public class now I'm not going to be teaching you how to write code in this
tutorial I already have a class named apples from last tutorial so I'm just pretty much
I just wanted to eat you guys set up an eclipse in this tutorial so um let me go ahead and
paste a program I already wrote it's the same program I wrote in the last tutorial and now
once you have a program I'm just going to show you guys how to run it and then next
story I'm showing you how to write a thing how to run programs in Eclipse what you do
is press this little circle with a triangle in it and that says run and if you go ahead
and press that what your program does is just go ahead and run as java application go ahead
and click OK ok and down here it just runs your program so that saves you a ton of time
from saving it finding the path going in command line this is about eight thousand times easier
than using a command line so again follow those instructions and I want to show you
guys one more reason why this is useful in the command line in notepad if we had an error
then it would just mess up in a wouldn't tell us what's wrong in this one say instead a
system where you wrote cysts something like that that isn't a valid code so whenever we
try to run this and press ok it says error error exists and then down here it says all
right where does it exist on line three right here so you go one two three here's where
your error is BAM you know exactly where is and what you need to fix so again eclipse
is a really powerful tool and that's the last thing I wanted to teach you guys and now we
are finally ready to learn the java language so now that you have done all that make sure
to check out my next tutorial or i'm going to be teaching you all this crap finally means
and finally how to write the Java of language in syntax so again I want to thank you guys
for watching I thank you guys for sticking with me running all of these programs and
getting java set up I know it's pain in the butt but thank you guys and again make sure
to subscribe my channel thanks for watching I'll see you next tutorial what is going on
everybody welcome to your fourth java tutorial and in this story we're going to be writing
a basic hello world program but before we get started let me point out to you guys one
thing now I told you guys at the point of downloading Eclipse was check for errors and
whenever you get those error messages it's going to say what line that the error is on
so we can fix it but the thing with eclipse and it's odd that it comes default like this
by default it comes without showing the line number so before you get started let me show
you guys how to do one thing that I know a bunch of you guys are going to send me messages
ask me how to do in your window tab at the top go ahead and click on preferences and
then your preferences tab is going to pop up go ahead and expand your general editors
text editors and once you click that it's going to have something that says show line
number go ahead and click show line number and go ahead and click apply now if you guys
could guess what this did it wouldn't surprise you show the line number right there now we
have our line number so whenever we get errors which we're bound to get we're going to know
where to go and we're fix them now as promised let me get to the basic Java program in which
you've all been waiting for now in Eclipse once you create a class I already starts writing
some of the code for you and it creates a class now again I know I told you guys unless
Stora but let me stress why a class is important in Java everything begins with the class so
if you don't have a class then you don't have Java or excuse me Java I don't know what the
heck you have if you don't have a class but you need one in Java so again everything we're
going to be writing in this story is going to be inside your class semicolons so make
sure you're not typing out here or anything like that in I'm going to be telling you guys
exactly what class is later but for right now all you need to know is to type inside
it so just bear with me here now inside your class the first thing we're going to make
is something called a method now what method is is basically a recipe on how you want to
run your program now the first thing your computer does when it runs a Java program
is it looks for a method called main and if it doesn't find the method main it has no
idea where to start so in your computer in Java built-in is the instructions to your
computer all right the first thing I want you to do is look for main and start there
and from there I'm going to tell you what to do so since that's case the first thing
we need build is a method called main so let's go ahead and do that so before I even start
with that I wanted to you guys what the basic layout of a method is before we can even start
building main now I told you guys method is pretty much like a recipe so let's go ahead
and pretend we're writing a method now say we have a method called bake cake what this
would be is the method header right here again the method header is pretty much um a substitute
for the method and name so bake cake is the name of the method and this is also called
the method header now inside your method or inside your recipe if I will go and continue
that analogy is all the steps that you need to take bake that cake so for example one
would be mixed batter and the next one would be put in oven and you probably have some
other stuff ever at eat the eff and cake sorry for cussing but again this is called the method
header in the instruction of how you want to do things is called the method body and
all this together the head in the body that's called the method who would have guessed it
so now that we know pretty much how to name a method and how to put statements inside
a method and what they are let's go ahead and write a basic method and again I told
you guys all about main before so let's go ahead and do it go ahead and write public
static why is it political it up oh this is oblique public static void main and then in
your main put parentheses input string Arg s and 10 put square brackets now again all
you have to pay attention is this is your main method right here all this stuff I'm
going it's important and I'm going to be telling you what it means but I don't have time in
this tutorial to tell you what it means so again this is um what I named it bake cake
this is all this is and I know that you see a bunch of different keywords and trust me
just bear with me you don't need to know them right now so just copy as I do and as I say
is that even saying I don't know but just follow on now that we have a name of our method
we need to give it some instructions and all we want this method do is print something
out on the screen so let's go ahead and write system dot or period what I write period right
there oh that's kind of American system dot out dot print line and after this you want
to go ahead and add parentheses and write hello youtube and i know i spelled YouTube
wrong but who cares now right semicolon now let me tell you guys what I just did print
line is a built-in method and what this is going to do is print a line out on your computer
now if you're saying that you're probably asking all right how come I needed to write
system out then if this knows how to print a line well print line can actually is pretty
versatile method it can actually print line to a database or print line to your network
or print line to other things but right now we want to just print it to our system so
again I'm pretty much just telling you how to work with eclipse right now so just bear
with me right here now once we have our method built we want to go ahead and run this so
go ahead and click this little button on the right hand side of your screen and it's going
to run the program go ahead and you get this click OK and at the bottom right here you
get something called hello you I too and again that's what oppressed so that is how you build
a basic method using of course your class what you need your method header in your method
body and again your method header in your method body together create something called
a method who woulda guessed it so that's your first tutorial on how to create a hello YouTube
or hello UI tube program and I know you you guys don't know what everything means right
now but I don't have time to go through every one of these things every one of these things
these keywords right here actually takes an entire tutorial to describe so um just bear
with me just stick with me for now and now that you guys know how to create a basic program
I can begin teaching you the more complex stuff and yes Java is a entirely completely
complex program so just follow along with me for now type this out and once you got
your program working you're ready to go on in the next tutorial so I want to thank you
guys for watching this story make sure to subscribe to my channel if you have any questions
to send me a message but if not just move on in my next tutorial so again thank you
guys make sure to subscribe and I'll see you next time what's up guys is Bucky welcome
to your fifth java tutorial and in this story I'm going to talking to you guys about something
called a variable now what variable in Java is is pretty much like a placeholder for something
else now if you remember from math class back in like seventh grade or whatever whenever
you had a variable and it was usually named something like X you could have it set to
a value like 73 and then whenever you use X in a program or a calculation or something
X will take the value of 73 and that's pretty much the same exact thing a variable does
in Java you assign something called a variable name to value then whenever you use the variable
people know that it's not really the variable but actually stands for that value so let's
go ahead and make one of these in Java now to make one of these in Java the first thing
that you need to do is tell Java what kind of variable you're going to be working with
and there's a bunch of different kinds of variables you can work with in Java you can
use integers like 3 you can use integers with decimal points like 3.14 you can use letters
like s you can use strings of characters like Bucky and so once you have that decided you
need to have a way to tell Java what kind of variable you're working with so it doesn't
get confused so we're we're going to be working with today is something called a double now
what a double is is it's a little bit above it's a keyword that tells Java all right you're
going to be working with a number but this number can have a decimal point as well so
um after you tell Java what kind of variable you want to work with you got to give it a
name so let's go ahead and say we're going to be working with a variable called double
and we're going to name it tuna now after you have a variable called tuna you need to
give a value so go ahead and write 2 none which is a variable name and then go ahead
and press equals now what equals is here is something called the assignment operator and
what it does is assign a value to tuna so what that we are we're going to assign it
let's go ahead and write five point 28 my favorite number five point 28 so here's what
we did so far we told Java that we're going to be working with a variable called tuna
and then it's going to be a double meaning that has um numbers on both the left and right
hand side of the decimal point again an integer is only a whole number like eight ten negative
four and a double has a fraction like five point two eight eight point one eight so again
we told that tuna is double it has a fraction next we said all right what's the value of
tuna well here's the name here's the assignment operator and here's the value tuna equals
five point two eight now we have a variable that has the value of five point two eight
that we can throw into any Java program so let's go ahead and do that and how we do that
is just like we did before just print it out on a screen so let's go ahead and erase system
on the type of write system dot out dot print and in your parameters instead of typing a
string of text like this is a strength all we have to do is type the word or the name
of the variable such as tuna and that would print tuna out on the screen don't believe
me let me show you guys let's go ahead and run this and as you can see down in our little
run box it has the variable five point two eight so that is how you write variables out
on the screen using Java in another cool thing that you can do with these variables is unless
order I'll show you guys how to write strings of text on the screen you can also combine
these variables with strings of text so let's say we want to write a system got out print
line and get out of here Hoss and in our print line we're going to say I want then go ahead
and press space actually this brings me to another point which I will say right now there's
two different things system out and print and system out and print line so let me show
you guys what those do right now system dot out dot print line and let's go ahead so what's
this thing right now I want five point two eight what do you want five point two eight
movies I want five point two eight movies makes no sense but just deal with it now let
me show you guys what this does and I'll show you guys why it does it let's go ahead and
run this and it says okay we printed three things but only is on one line I want five
point two eight movies and let me show you guys why this is there's two different print
statements that you guys learn so far a system out print what you see in the first two lines
and a system out print line what you see in the next line well a print statement does
a basic print with no line is it says are a Java we want to print this but we want to
stay on the same line and it says all right so just stay on the same line stay on the
same line and then after this move to the next line so after you have print line what
it would do is move the cursor to the next line so if you printed anything after this
it would show up on the next line don't believe me let me show you guys system.out.print and
it can even have a basic print stay memory here and let's have apples let me go ahead
and run this and as you can see this one moves to the new line so that's the difference between
the print statement in the print line statement any time you use a print statement it stays
on the same line that you're printing on such as here so this whole thing's on the entire
line as soon as you have a print line statement one does is when it gets the end of printing
it it moves to a new line so that's the difference between a print statement and a print line
statement and again that's your quick tutorial on how to use variables again this is called
declaring a variable this is called assigning a variable value again this is the name of
the variable and this is the value this is the assignment operator right here and anytime
you want to use a variable to print out on your screen just write system.out.print and
then write the name of the variable right there and it'll print out on your screen so
again that's a quick tutorial on variables and how to use variables in Java so thank
you guys for watching make sure to subscribe to my channel I don't forget to check out
my next tutorial and again thank you and I'll see you next time what's up guys welcome to
your sixth java tutorial and in this tutorial I'm gonna be showing you a real quick way
to get input from a user so one way that we can get input from a user is to use something
that's already built into Java and this is called a scanner and what the scanner does
is take information that the user types in from their keyboard and stores it in a variable
so let's go ahead and get the scanner first and how we get it or tell Java where we need
to use it is we need to import it so go ahead and write import on your first line Java jav
au TI l dot scanner and this is how you're going to be inputting excuse-me importing
stuff from now on so I'm just get used to that syntax if you don't know exactly what
this means it's pretty much just getting the scanner and telling Java we're going to need
it so of course in our class we need that public method again so in our class body go
ahead and type public static void main and in your arguments type string Arg s with a
square brackets what's good good enough now in your body of your main we need to actually
get programming so we need to make a variable that's going to hold the input from the keyboard
before you use it so to do this we need to go ahead and write something out scanner SCA
and NER this means that we're going to use the scanner next you need to give your variable
a name and I'm going to name mine Bucky and then you need to set it equal to whatever
they enter from the keyboard so to do this you need follow-on new scanner and in your
parentheses right here a man my semicolon first you need to type system dot in so here's
what's going in right here or excuse me going on we told Java we need to use the scanner
which is a way to get information from the keyboard we have a variable it's a scanning
variable called Bucky and the Bucky variable is equal to whatever and formation that's
in the keyboard and system n stands for system input and the only input we have in the computer
right here is the keyboard so this pretty much is short for whatever we type in for
the keyboard hold it in our Bucky variable and we're going to tell you what to do with
it right now so now we have input whatever that's going to be from the keyboard and it's
going to be stored in our Bucky variable what we want to do now is just print it out on
the screen so you remember from the last tutorial how to do that system.out.print line and in
your parenthesis you need to type pay attention the first thing you need to type is the name
of your variable and that's going to be on whatever you made up here again this Bucky
you don't have to write Bucky of course you can write anything you want now press Bucky
period next line and what this is going to do is allow you to pause and wait until you
get input from here so again let me run this program and I'll show you guys what it does
go ahead and press ok and now down here is going to say a now press ENTER and it's going
to output hey now so remember it had a blank cursor said hey now once you press Enter it
just repeated it again and I'll show you guys what happened right there again one last time
we imported this scanner if we didn't import it we can never use it since Java doesn't
really like to just gather everything it only uses the things it needs it's very um I don't
know Noba like it's only takes what its needs very courageous a programmable age but anyways
what we did in our main function these are the only two lines you really need to look
for we made a variable called Bucky and it's a scanner variable we said we're making a
new scanner variable that's going to be equal to whatever the user inputted in this example
hey now now hey now equals Bucky or Bucky equals hey now so what do you want so anytime
you print out Bucky is going to print out hey now so instead of system.out.print line
with text in here we actually change that text for Bucky next line and again what that
did is pretty much take that hey now variable and put it in here and that's why it printed
out hey now so that's your quick tutorial on how to use the scanner to set it equal
to variable and then use that variable to output on your screen using the print line
method so that's your basics on how to gather input from the keyboard and we're going to
need this input to use in functions later on say we want to make a mortgage calculator
or just a shopping cart that's how we do that so again I practice this just copy the source
code as I do remember you don't need a name a name your function Bucky so go ahead and
practice this and thank you guys for watching hopefully check out my next tutorial don't
forget to subscribe to my channel and thank you and I'll see you next time what's up guys
welcome to your seventh java tutorial and in this story I'll be showing you guys how
to make a basic calculator using variables and other things that we already learned so
far so in your last order I showed you guys how to get input from a user using something
called a scanner and we're going to need that again and I already imported it so the first
thing that we're going to want to do is go ahead and make a scanner variable so let's
go ahead and write scanner and I'm going to name mine Bucky again and again I'm going
to set it equal to new scanner and also I want to put system in so it knows where to
get input from so or a system dot n and that's all you need to do for now the next thing
we're going to need is three variables we need one variable to hold the first number
one variable to hold the second number and our less variable is going to hold the answer
so you can they're going to be doubles by the way so you can go ahead and declare them
by writing double F num for first num go to the next line alright double s known for second
num or here's the neat little trick if you have many variables of the same type and by
type I mean double or int or something like that since you have many double variables
you can write them all on the same line it's a nice little shortcut so write s num and
answer you can name your variable as anything you want I just usually have shorter variables
since I don't feel like type and Hall up so the first thing we want to do on a character
on our excuse me calculator is just print a line of text that says enter first number
so the user knows what to do so go ahead and you know how to write this system dot out
dot print line print line and your parameters just go ahead and write enter first num and
then put a little a semicolon right there if you want and now we can go once we have
that all this is going to do is put a line of text enter first num on the screen now
what we want to do is have a way to enter a number and have it stored and F num so go
ahead and press F num equals Bucky remember this is our scanner variable and right next
double and that brings me to another point now in our last tutorial I showed you guys
something called next line and it was right here and that was for Strings each data type
in Java you need to give it its own method since you need to tell what you're going to
be inputting in there so it knows how to save it for example if this was strings you're
going to input in there like Hello you're going to put next line but since we're inputting
a number you're going to want next double because double is the type of number so now
once we enter this number and it can have decimal points it's going to be stored in
the variable F numb and we're going to need that later so now we have to do this exact
same thing for the second number so let's go ahead and copy this line since I'm too
lazy to type it where's the copy they are at us and next we want to print enter second
number and now we just need to do the same thing go ahead and copy this and after that
prompt pops up you're gonna there we go you're going to want to store this variable in s
num so the first number you're going to type is in be stored in the variable F num and
the second number you type is going to be stored in the variable s num this stands for
first number and second number now what we want to do to get that answer that we have
those variables is just add them together so go ahead and write answer equals and you're
going to want to write F num plus s num and now we have whatever number that F num and
s num is going to be stored and answer now it's stored in there but we need a way to
output it to the screen to see what it was so let's go ahead and write system dot out
the print line and in your parameters just write answer this is going to do is output
answer whatever it was to the screen so go ahead and run this program and hopefully I
didn't get any errors and here's our program right here enter first number let's do 30
4.6 move my cursor out of the way press enter enter second number um fifty four point two
press Enter it's going to give us our answer eighty eight point eight zero zero zero one
and this is actually something that I want to talk to you guys about and I'll be talking
to you guys about it in the next tutorial well probably not in the next one but um this
brings me to a good point which we don't have time to talk about in this tutorial but I
will get to shortly so basically what I did and our first number entered it and our second
number enter it and then gave us an answer pretty much and again to walk you through
this one last time what I did is import the scanner so I could use the java scanner and
then i wrote everything in my main function because that's where the computer looks first
the first thing i did was write a variable for a scanner just so we could use it next
i made three variables that were double which means numbers first number second number answer
the first thing i did was have a little prompt that should set a enter first number just
so I knew what was going on for the user next I use that little scanner variable and nextdouble
method to have whatever I typed in the keyboard to be stored in the first number then I just
had something says enter second number and then I had it have the user input something
else and wherever they input at that time is stored and s num and then I just outputted
um whatever those two were added together the sum of that and I just printed it out
online screen so that's a very basic calculator and it had one flaw in it and that was giving
us many decimal points and I'll show you why that is in I don't know what tutorial but
it's going to be coming up pretty soon whenever I deal with numbers but again that's a basic
calculator that's how you build it using a new method called nextdouble that we learn
and also how to combine everything we learned so far and build a function a functioning
program so again practice this get good with it make sure you understand everything we
did up to this point and once you do you're ready to move on to my next tutorial so thank
you guys for watching hopefully I see you next time and don't forget to subscribe to
my channel so thank you and I'll see you then sup everyone its Bucky welcome to your eighth
java tutorial in this tutorial I'm going to be talking about a new type of data and that's
called an int and what it is is pretty much not like double before where you had numbers
like three point five four or you could use fractions or decimal points with int it stands
for integer and integers are whole numbers like 6 or 75 or 76 and you can only use whole
numbers without a fraction and no decimal point so another thing at one point now is
whenever you use int like to defy divide and multiply and add and subtract whenever you
use two intz to do these operations your answer is also an int so even if your number comes
uneven like 11/4 that doesn't divide evenly but when you try to do it on your computer
with an int you get an int as a result so let me show you guys with edit let's go ahead
and make three variables girls boys and people and let's go ahead and set these to some values
let's go ahead and we'll start out with showing you all of the math operators girls equals
six boys equals three and people equals girls plus boys now I showed you guys how to use
this math operator and the last one the plus sign to get a simple sum so now we can go
ahead and the sum of girls plus boys is now stored in people so I've put system dot out
print line and in our parameter go ahead and put what is it people and now if we go ahead
and run this we get nine since six plus nine excuse me 6 plus 3 is 9 and so that is how
you can use addition with Java but addition isn't the only thing you can use you can use
minus or subtraction to get the difference which 6 minus 3 is 3 you can also do some
other cool ones in the symbol for multiply is actually the asterisk which is that little
symbol above the eight so whenever you want to multiply make sure to use that as you can
see six times three is eighteen divided is a special one now whenever you do this you're
going to get two but let me show you guys something where if you divide it uneven say
you wanted to divide eleven by three let's go ahead and see what our result is three
well we all know that 11 divided by three doesn't equal three what it equals is three
point something nine divided by three actually equals three so what exactly is going on here
well Java said all right you want to work with intz well it's our whole number so whenever
you ask the question in intz i'm going to give you an int so if you want to get um this
number exact you need to use double so go ahead and press ok and there's your answer
three point six six so that's difference between in and double it gives you whole numbers and
double gives you um like a fraction of a number or numbers with the decimal points so anyways
there's one more thing that I can help you out with when using instead of I'd and that's
something called the modulus now we all know that 3 goes into 11 3 times in that will give
me 9 but you have two things left over whenever you do that and that's where the modulus comes
in the modulus pretty means alright when you divide by a number by a number you get a whole
number aka an int but you also have some left over so let's go ahead and run this and see
what we get it says 2 in the modulus if you remember from math class this is another way
of finding the remainder so again when you divide by 11 by 3 you get 9 I mean excuse
me you get 3 which 3 times 3 is 9 but you have 2 left over so that's where that 2 comes
from whatever's left over is the modulus so that's how you can use the modulus along with
int to find the exact value so again let me give you guys one more example this one might
be easier if you have 7/3 you're going to get the answer to but you're going to have
one left over so if you divided by two such is this you're going to get the answer to
but if you have the modulus you're going to get to answer one since you have one left
over right there and that believe it or not is going to be useful in a bunch of things
that you won't even expect like when we're finding random numbers and stuff that I'm
going to be teaching you later but for now I just wanted to show you guys what the int
variable type was and also how to use different math operators so uh thank you guys for watching
practice this little um I don't know what this program is girls and boys and people
counters I guess and once you get good at all the math operators you're ready to move
on to my next tutorial so again thank you guys for watching make sure to subscribe and
I'll see you next time what's up guys welcome to your ninth - Java tutorial and this is
where I'm going talking to you guys about something called assignment in increment operators
now these are neat little tricks to have when you're working with a Java they're going to
save you a bunch of time and help you out with numbers a little bit so let's go ahead
and make two variables I make one call tuna and set that equal to five - whoa and make
another one called bass in case we need it I don't know if we are beset equal to 18 now
what the increment variable does is allows you to add one very quickly to the variable
and you can do this by pressing plus plus and then the name of variable so right now
by pressing plus plus tuna we change tuna which was five to six now let me show you
guys just so we can I have something you have to take my word for it print line and go ahead
and put a tune in there and go ahead and run this baby and as you can see what was five
now equals six so that's how you can use plus plus to quickly change variables value by
adding one now another thing that you can do is instead of putting it on and separate
you can throw this right into the system print line method and as you can see you still get
six now this is called pre incrementing and what this does is it changes the value of
the variable before it uses it but I want to show you guys something else whenever you
use post incrementing what this is going to do is it's going to use this variable as five
and then it's going to change it to six so let me go ahead and press ok and as you can
see once we do this it gets five and not six so what this did is it changed variable but
it didn't do it until after it already used it so if we were to print it out again it
would get six so let's go ahead and do that save me some time by copying this baby or
get pasted there so now that here's what's going to happen this is going to change the
variable six but it's not going to do it until it after it already outputs it is five and
now once at six is going to show six right here so may show that as you can see five
and then it changed it to six and then we just outputted the regular variable and this
doesn't only work with a addition you can also use this with subtraction by pressing
- - you can change it by subtracting one and getting four and let's see what else there
is what else is there um you can also do something else and this is called an assignment operator
so now we can know how to use the increment operator using two plus signs and also using
two minus signs to subtract one you can also use the assignment operator let me get some
space right here to do it if you want to do more than one and instead of changing value
of tuna and say you wanted to add five to tuna sit you a platoon ax equals tuna plus
five and what this would do is take that number five add 5 to it and give you the value of
10 which I will show you right here 10 now this is a pretty simple way it takes the new
variable and sets it equal to the value of the old variable plus the new number but there's
the easier way you can do this what you can do in sub tune equals 2 plus 5 you can do
tuna plus equals let's get a new number 8 and what this does is take the new variable
you want to use tuna and just a date to it so it's a lot easier way than typing all that
so now we should get the number 13 and we do just like that so that's called um I think
it's called assignment operator but I'm not exactly sure it's how you can use + equals
to add this number to the old variable to get brand new very and also just like anything
else you can use this with subtraction to subtract a from even with multiplication to
multiply 8 and if you do this you should get the number 40 and you do so that's how you
can use basic increment operators to increment one very quickly using plus plus and minus
minus for subtraction and also assignment operators so you can assign a value to a new
variable very quickly without having a press tuna equals tuna times a or something like
that so those are just some nice little shortcuts that you can use so get familiar with them
because if you do it's going to save you a bunch of time and if you don't then you're
going to waste a bunch of time so thank you guys for watching my Java tutorial make sure
to subscribe to my channel and again thank you for watching now see you next time
sup guys welcome to your tenth job tutorial in this tutorial I'm going to be getting to
the actual good stuff in Java so in this tutorial we'll be showing you guys what if statement
is and how to use it to make a decision there's going to be a lot going on the story so pay
attention it's going to be sweet what if statement is is pretty much how your computer makes
decisions you're going to give it a test such as am i a boy or my girl and your computer
looks at this and if it says your boy it does one block of code but if it says you're a
girl it does another block of code for example you could build a program if you're a boy
take you to a boy website but if you're a girl take you to a girl website or something
like that so what we need to do here is build a if statement and to do that go ahead and
write if now after that you have something in parentheses now what in is in your parentheses
is called the condition but what the condition actually means condition is just like the
technical term what it is is actually a test whatever you want to test you're going to
have two choices for now say we have a variable called um let's just name it test and let's
make it int int test is equal to 6 so now we have a variable that we can test so let's
go ahead and test this and let's go ahead and say if test is equal to 9 and this brings
me to another point you're testing operators there um let's see probably six different
ways I've taught my head that you can test the first thing you can do is to see if something
is exactly equal to something else so this means if six is exactly equal to nine remember
you don't use one equal sign because when you use one equal sign that means you're assigning
it to value when you're testing it you use two because you don't want test the equal
nine you just want to test of eight is equal to not so that test if the variable called
test is equal to the value nine if you want to test if it's not equal you need explanation
point equal sign and this means not equal and of course as expected you got your less
than signs is six less than nine is six greater than nine is 6 less than or equal to nine
or a sixth-grader or equal to nine and those are all self-explanatory I'm not going to
show you guys exactly what those means but for now I'm going to use the most basic one
and what we're going to be testing right now is if the variable called test is exactly
equal to nine which it isn't now I told you guys that you have a test and this test can
either go it is or isn't and that's where your computer makes a decision if it is then
after your if statement you have something called a statement your first statement and
this is also called the body now your first one is you need to tell your computer what
it's going to do if the test is true if test is equal to nine then we want you to system.out.print
line that's right yes just write something simple now the first thing you do is tell
your computer what to do if test is equal to nine and this what it does if it is equal
to nine but as you can see test isn't equal to nine so what do we want to do now well
if that isn't true then we give you one more option and that's the else and let's just
go ahead and copy this copy and paste where are you and let's go ahead and write this
is else so let me run this free eyes and I'll show you guys what happens press ok and our
final result is this is else so let me show you guys clear things up how if-statement
works how if a statement works is you write if and then you have a variable so it pretty
much is exact same thing as saying all right if test is equal to 9 I want you to print
yes but if it's not equal to 9 I want you to print this is else so we're going to have
the test one thing if the test is positive have this else if the test is negative have
this but say we had something else like if test was less than 9 which is true well since
this test is true is going to do the first option and not the second option again it
can't do both of them it can only do one or the other so let's go ahead and run this and
it's going to say this time yes so that is how you can use different testing operators
such as equals equals less than greater than less than equal to and that's how you use
a simple if statement to give your computer two choices again it's just like saying if
this test is true do this first thing else do the second thing and don't forget to use
your curly braces to do the body of the first thing and then you're out statement and then
your curly braces to do the body of your second choice and I'll be chaotic excuse me I'll
be clearing this up for you and next couple tutorials but I hope this is pretty self explanatory
just play around with it for now it's the best thing to do to learn so just copy this
throw in some new variables and soon enough you'll get the hang of it so thank you guys
for watching that's the basic if statement how to tell your computer to make a simple
decision so again thank you guys for watching make sure subscribe my channel and don't forget
to check out my next Java tutorial so I'll see you then sup guys welcome to your 11th
java tutorial and this story i'ma teach you guys how to use multiple tests in the Java
program now in the last tutorial I showed you guys how to test the one thing such as
if um let's say apples was less than 3 then do something well there's actually a way to
in your if condition test many tests in the same condition so let me go ahead and demonstrate
there to do demonstrate this point let's go ahead and make a simple dating website so
we need two variables and boy and Inc girl and let's go ahead boy set them to 18 nice
young book as myself and drill setter equaled a 68 shipped me an old cougar so let's go
ahead and make a simple um if statement and in the test condition as I told you guys before
we learned how to test one thing so if boy is greater than 10 we would execute a certain
block of code so let's go ahead and do that system.out.print line and if he's older than
10 wilbur i you can enter since we can grant information else if he's less than 10 then
what do we want to do um let's just copy this save us some time copy and paste there we
go um you are too young so let's go ahead and run this real quick make sure everything
is working and since boy is greater than 10 it says you can enter so that's what I taught
you guys last time how to do one test in an if statement but there's a way where you can
do multiple tests in the same condition now say you wanted to make sure that the boy is
greater than 10 and the girl is less than um 60 since we don't want any senior citizens
on our site no offense but this this is a site for younger people only so what we would
do is use something called the double ampersand in this stands for and and whenever you use
this that means that either test on either side of this has to be true for this to be
true so for example if boy is greater than 10 and girl is less than 60 which is false
you want to print you can enter else print you are young and again here's what and means
whenever you have this little double ampersand and that's a little symbol above the 7 on
your keyboard this test has to be true and this test has to be true for this piece of
code to execute if either one of these are false or if they're both false then this code
is never going to execute so let's go ahead and change this to you can not enter so if
the boy is greater than 10 which is true and the girl is less than 60 which is false 68
print this you can enter so it's not going to print that it's going to print this you
cannot enter so let's go ahead and run this and as you can see you cannot enter so let's
go ahead and what if we changes to the girl was within the age limits 40 and we ran that
baby again it would now say you can enter so that is how you use something called multiple
test in the and sign to make sure that two tests are true so you can execute a certain
block of code but there's also another neat thing besides the and and that's called the
or and to do that you use two little pipe signs and that's the key above your Enter
when you press shift it's a I think it's called a pipes like a pipe or pipe line or something
but here's what this means to little pipes this means or in what or means is either this
has to be true or this has to be true only one of these have to be true to execute this
first statement if they're both false then it's going to just skip that and go to this
so let's go ahead and in another point in an or statement this one has to be true or
this one has to be true or they both can be true such as right now so let's go ahead and
change this to girl equals 99 so only this one is true but this one is false using the
or thing you can enter such show up should show up so let's go ahead and we ran it and
it said you can enter and let me show you guys what happens if both of them are true
um it should do the same thing girl he goes forty-four run it again it says you can enter
but again the only way that this is not going to work is if both of them are false so let's
say if the boy was eight and the girl was 99 it would say you cannot enter so again
to recap one last time if you want to do multiple tests in Java there's two things you can use
in and sign and what this does is make sure both the tests on either side are true and
if it's not true it does the else and you can also use the or sign and unlike the and
where both of them has to be true the or sign make sure only one of them has to be true
but if both of them are true then oh well it still works so that is how you can use
a multiple test in your java program using the double ampersand in double double pipes
to test either an and or so if you have any questions or if this is confusing at all then
I you can just send me a message and hopefully I can clear things up for you but I try to
make it real self-explanatory for area so that's all for this tutorial I hope you learned
how to do multiple tests and make a simple dating program so thank you guys for watching
and don't forget to subscribe to my channel and I will see you next tutorial some guys
is bulky welcome to twelfth Java tutorial and in this thoron we teach you guys about
something called a switch statement now what a switch statement does is test one variable
and depending on the out depending on the value of that variable pretty much that gives
you multiple choices so before we learn the switch statement let me show you what we had
to do with our knowledge so far say we let's make like a program that tells you what kids
do at a certain age so say we have a program that has variable called age and the kids
age is three right now what we would have to do right now is make it if statement for
each age and put if age is equal to one and then we would have to put something like um
system then we would put out and stuff like that and it will just get pain in the butt
and then we would have after one would put if age is equal to two is equal to two and
then do your stuff down here as you can see we don't want to type all that stuff for each
number so I wish there was the easier way just to test age for a bunch of different
things well actually you don't have to write ten different if statements if you want to
test age ten times you can only write something called a switch statement once and it will
give you many different options so instead of writing ten different if statements to
see what a kid does from ages one to ten let me show you guys what to do what you do is
write the word is switch and what switch does is find the variable value in the parameters
so what variable do we want to test we want to test age so after this you just go ahead
and write curly braces and give yourself a nice little switch body now instead of writing
on a bunch of if statements all you would have to do is right there called cases now
what case means is if the value of age is equal to one what do we want to do and after
case one go ahead and put a regular : and I don't know if you guys can see this but
this is a regular : after the one and not a semicolon so in the case of one um let's
go ahead and system.out.print line so what can they do it one arm you can curl and then
after this you want to add something called break now what break does is it means alright
if this is true in age equals one then we have no reason to go to the rest of the program
so you can just exit and I'll be talking about that more later but for now just make sure
you have it so you can just repeat this for as many options as you want so in the case
of two what do we want to happen let's just copy this copy save yourself some time um
you can talk so in the case of two if H equals two what do you want to do you want to print
you can talk and then break and let's go ahead and make one more in the case of three again
using lazy you don't feel like typing it all in the carry case of three you can uh get
in trouble and I know I spell trouble wrong but just deal with it so here's what you do
you put switch and then put the variable you want to test the value of then you put the
values that are possible after the word case now after in the case body what you do is
write re if H is equal to one here's what I want you to do if H is equal to two here's
what I want you to do if H is equal to three here's what I want you to do and the last
thing you have in every switch statement is something called a default and to do that
you just write default and what this does is if none of those values are true it does
a default statement so let me scroll up a little bit so you can see and again you don't
need a value for this like default for because it already knows all right if everything else
is false this is where your and do so that's why you don't need a value so let's go ahead
and all right actually I'm gonna go ahead and copy this since I'm lazy copy if default
let's go ahead and write um I don't know how old your I don't know how old you are and
of course we have a break at the end of this as well if I can get it right break so let's
go ahead and run this with age equal to three and let me show you guys what it does it outputs
you can get in tolerable and it's supposed to be trouble but I typed it wrong so and
let me recap well let me show you guys what default does so if age isn't equal to one
two or three say Asia is equal to seven and I run this it says I don't know how old you
are so let me recap one last time what this switch statement does a switch statement takes
the variable whatever is in its condition and test it so then it looks through the cases
to see what value that variable is equal to if it's equal to the value one it prints this
if not it doesn't do it at all then it looks if it's equal to the value 2 if it is it prints
this if not then it skips this as well then it looks if it's equal to the variable 3 if
it is it prints this if not again it skips all of these and then if it doesn't hit any
one of these then it goes default and what default means is I ran through all these options
and none of them worked so by default I got to do something this can't be just a waste
of a program so by default I want you I want to output I don't know how old you are so
that is how you can use the switch statement to test a single variable for many different
values and this will save you a bunch of time and keep you from writing a bunch of different
if statements and it trust me it's a whole lot easier than writing a bunch of if statements
so that's your basic on the switch statement um it will save you a bunch of time so thank
you guys for watching and hopefully you learn a little something copy this play around with
it and pretty soon you'll be expert it's a switch statement so again thank you guys for
watching and don't forget to subscribe to my channel and I'll see you next tutorial
sup guys welcome to your 13th drop tutorial and in this story I'm going to show you guys
how to do something called a while loop now what while loop does is it allows you to execute
a certain block of code multiple times but only write it one time so pretty much it saves
you a bunch of time writing a code and let me show you what the syntax is for this the
first thing that we need is a variable and I'm going to name my encounter and I'm going
to set it equal to let's go ahead and set it equal to zero to begin with now the syntax
for a while loop is just the word awhile and this means begin your wild loop and what your
while loop is going to do is whatever is in the body of your while loop or curly braces
it's going to execute this code and let's go ahead and say system.out.print line and
let's go ahead and just print the variable counter nothing special here so what this
is going to do is it's going to execute this line of code multiple times so you're saying
alright how many times is it going to execute well that is up to whatever you put in the
condition so let's write while counter is less than 10 we want you to output counter
so right now here's what would happen it says all right what's counter zero print counter
what's counter zero print counter so this loop will kind of go forever so we need a
way to end the loop somehow so we need a way to change counter so let's go ahead and use
the incrementing operator that we learned less time and just put counter plus plus and
let me run this real quick and show you guys what it does run okay and in our little run
box we get 0 1 2 3 4 5 6 7 8 9 just what we wanted now let me show you guys one more time
what this does we had a counter variable to begin and it started it is 0 the first time
we told Wow is all right while this counter is less than 10 we want you just to write
whatever number it is on the screen so the first time it looped it was zero so outputted
0 and then it added one to it so then it was one so now it looked at it again is counter
less than ten yup let me put that on screen now it's two is counter less than ten yup
and it did that again and again until it got to ten and when it got to ten is 10 less than
ten nope so I'm just going to not do anything so that's how you use loops to execute a block
of code many times without having to write it over and over again if we didn't have this
we would have to write um print our prints they met 10 times and as you can see this
saves you a bunch of time so again the syntax for a while loop is wild and then put your
test condition in um what are these called parentheses and then put whatever you want
to execute in curly braces right here and whatever you execute is going to do that however
many times you say so that's your quick tutorial on loops on the sawaal loop there are different
kinds of loops where I'm going to be probably going over in the next tutorial but this is
your basic while loop so thank you guys for watching I hope you test this out a little
bit and play around with it and you'll be expert in no time so again thank you guys
for watching don't forget to subscribe to my channel and I will see you next tutorial
so guys is Bucky welcome to your 14th jump tutorial and this story I'm going to teach
you guys how to use multiple classes now so far I've taught you guys how to build everything
in one simple class such as this but usually in a like 99% in time if you're working for
like a company or building a bigger application or program any time you want to build something
that's bigger than something really simple you need to know how to use more classes or
multiple classes and then you can use those all in your main class or let me just show
you guys how this works so to get another class hopefully you're using Eclipse go ahead
over to your source file right click it and go to new class now go ahead and give this
a name like tuna or something like that and you'll see why this is useful later on so
now we have as you can see from our tabs we move this over we have apples class and the
tuna class now it started out writing our tuna class for us it says public class tuna
now anytime you see public it pretty much means that this class is going to be available
to the public or anything else can use it so let's go ahead and now that we have a class
before I show you guys how to use it let me show go ahead and write a little method in
here now let's go ahead and write a simple method in this other new tuna class that we
just built type public void and again public means anyone can use it void means that it's
going to do something this method is going to do something but it's not going to return
any data so then go a name ahead and name your method I'm going to name my simple message
since I'm just going to be printing out a simple message and that message is going to
be system.out.print line and let's go ahead and add something like this is another class
so now we have another class and this is called public class tuna and our method is public
avoid and don't forget to put a simple message in here somewhere you can put anything you
want and now if you see if we go ahead and try to run this try to run it okay nothing
happens right here and this is because I told you guys anytime you run a Java program it
looks for the first thing that says main and runs it from there as you can see you don't
have main anywhere in here so it doesn't really know where to look first so in order to use
this class we need to go ahead and use it in main so here is our main function or method
right here and it's in the main class one remember this is the main one that we use
before it the other one is the new one that we built so let me give you guys a little
background whenever you use a method in another class say we want to use this method simple
message and it's in the tuna class what we need to do is go ahead and create an object
of that class so the first thing we do is put the class name which is tuna next we need
an object name and you can name this anything you want I'm going to name my tuna object
and what this object is gonna allow you to do is use all the stuff that's in this class
again if you didn't create an object and you had like five classes and what I know what
class you're talking about so that's why we need an object so we're saying are a we have
this object because we want to use something from the tuna class and of course you need
to set it equal to something and you always said equal to new in the class name with empty
parameters for now so now we have an object called tuna object that we can use to get
anything we want from this tuna class that we just build so let's go ahead and use it
what you do to use this is go ahead and put the object name which is tuna object spell
it right tuna object and then after the object name what you do is add a dot separator and
after that dot separator you use the method name so what was our method name uhm simple
message so simple message empty parameters and let me tell you guys why you use a dot
separator in the method name because this class even though we built one method right
here it can have as many methods as one since it can have a method name simple message simple
message to dimple message um bacon and cheese but it says all right we have this object
we want to use in tuna class and we want to use this method so if there are other methods
we want to use this one so that is how you use a specific method in a specific class
by creating a class or excuse me an object of that class so let's go ahead and unlike
before when we run this program we get the message that we just built this is another
class and so this is how you can use different classes in your main program or your main
method so this is a way that if you own like a software company you can say alright Tommy
we need you to build one class to do this John we need to build another class to do
this Stephanie we needed to build another class to do this and then you is the head
programmer would put all those classes together and it's just a way that different programmers
can combined all of their work without having to work on the same file so this makes it
a lot easier again that's how you use a different class such as this an outside class and call
it into your main method or function so that's your a quick tutorial on using multiple classes
follow this tutorial exactly and play around with it and soon enough there will be expert
at it so again thank you guys for watching make sure to subscribe to my channel and make
sure to check out my next tutorial sup guys is Bucky welcome to your 15th Jeff tutorial
and in this tutorial I'm going to be teaching you guys how to use methods with parameters
now in this tutorial we're also going to be building a custom function where we're going
to let the user enter their name in the outside class and it's going to call it in the main
class in just say like hello to whatever your name is or something so if that sounds complicated
then I'll follow along and I promise I'll make it easier it's probably easier just to
see then maybe even say so follow-on so again we have our main appleís class with our main
function or method in here and we have our outside toona class with the method that we
call so let's go ahead and clear everything out of your public class tuna if you have
anything in there because we're going to be adding a new method so go ahead and type as
I do public void and we want our simple message again and instead of just adding empty parameters
this time we needed to take an argument in our parameters and let's go ahead and make
a string variable and call it name so again anytime you have something in your parameters
is called an argument in an argument or extra parameter data is pretty much any extra information
that the method needs in order to work so let me show you guys what this method actually
does and you'll see why it needs extra data what it does is just system.out.print line
it just prints on the screen hello and then it prints the name so if you don't have a
name it will print hello and nothing so again an argument is any extra information that
the program needs to run so again what this is going to do is print hello and then your
name so before we use it we need to do a couple stuff to our main function of course to enter
our name we need to import the scanner or else we have no way of importing a data to
Java so go ahead and import java.util.scanner and this is going to allow us to use a scanner
so we can give information to our program next we need to make a scanner object so let's
go ahead scanner name an input new scanner and I believe that I tell you guys how to
do this in the last tutorial so you should know what I'm doing system in and that's that
for that line that makes that we can use the scanner Mexico ahead and do what we did last
time and create a tune object so we can use this object to get the information from the
tune class we just need that empty parameters next let's go ahead and print system.out.print
line spell alright and we just want to write a prompt to the user so it knows what to do
enter your name here so this is pretty much the only thing that's going to go on right
now we now have text on the screen that says enter your name here now we need the next
thing they enter to be stored in the variable name so let's go ahead and make string name
so the variable name is equal to input next line and I told you guys how to input integers
using the scanners anytime you want to input a string use this next line method so again
anything that the user enters is going to be stored in the variable name so now what
it's going to do is look at name and say hello name so if you're entered Bucky it's going
to say alright Bucky equals name so hello Bucky so now before I do that we need to actually
use that object to call the method so tuna object and go ahead and put your dot separator
simple message with your name parameters and again unlike last time where you don't use
anything at all you have to put the argument that it takes in here and this says again
that this isn't just a method that you don't need any X information for we're going to
give you this extra information for something because we want you to do something with it
so go ahead and let's run this program and then I'll show you guys what does later enter
your name here um bucky roberts and then it says hello bucky roberts and let me talk you
guys through this one last time we imported the scanner because that's how we uh how Java
allows us to input stuff in the computer like i typed in bucky roberts without this I wouldn't
be able to type in anything next in our main function we set our a we want to put this
this just allows you to use the scanner after you imported it we next made an object because
without object we can't use anything in this class as I taught you in the last tutorial
next we just put a prompt on the screen we learned this like in the first tutorial just
showed what we need to do next we put all right we have a variable called name we want
this to be equal to anything we type in B and that's so if you have different users
that are using this program doesn't just type the same message you can use it for a bunch
of different users no matter what they type in for their name next we set our write in
the class tuna use a method called simple message that takes an argument of name so
it's going to go look at all the classes find the one called tuna and find the method that's
called simple message and and do whatever it says to do and what this method says to
do is say alright take a variable called name which we already had we just gave it to it
and then just print out hello whatever the name is so it says alright hello Bucky Roberts
so that is how you use a method with a parameter and that's what parameters do is just the
extra bit of information that you might need to run a program so again that's your quick
tutorial on how to use methods with parameters in a different class so thank you guys for
watching and don't forget to subscribe to my channel for all my latest videos and also
check out my contests so again thank you and hope to see you in my next tutorial what's
up guys is Bucky welcome to your 16th Java tutorial and in this story I'm going to be
teaching you guys how to use multiple methods and also something called instance variables
when working with classes so let's go ahead and open up our tuna class and clear anything
out if you have anything in there and let's go ahead and the first thing I want to do
is make a new variable called private string and just put a girl name because we're going
to be making a program in the store l2 output the name of our first girlfriend so the first
thing you probably notice is instead of public I put private and this is because when you're
working with classes right now we have two types of variables public and private public
variables any class can use that variable but when you use a private variable only the
methods inside this class can use it so the methods that we're about to build can manipulate
and change and also access to this variable if we were in another class like this one
over here it couldn't use this variable in this class so private means private inside
this class only the methods can use it inside this class so let's go ahead and make some
methods the first method that I want to make is public void set name and what this is going
to do is set the name of whatever we put into it into the variable girl name so of course
we need a parameter string name so now we have two variables name and girl name so let's
go ahead and add some instruction for this method we're going to put the girl name is
equal to name that's all that's all we need for this method and what this is going to
do this whenever we use this method in our main program whatever we put in for the argument
for this method all it's going to do is make sure girl name is equal to name so it's pretty
much just putting girl name equal to whatever you typed in next let's go ahead and make
a method to access that so let's go ahead and put public string and this is the return
type let me show you guys in a second again we know public means access anywhere the second
thing after this is a return type and whatever your method does it may or may not return
something this method is going to return a string so that's why we need to put string
and if your method returns nothing go ahead and put void so now let's go ahead and our
method body let's go put return girl name that's all all it does is a return girl name
which was made up here next let's go ahead and make one last method totally avoid saying
and this is going to be our final output to the user so all we need to put here is system.out.print
F and this is a new type of print if I didn't teach you before what it does is take something
like your make sure I was felling everything right your first girlfriend was percent s
and whenever we use percent asset it needs to know what string no I want to put in place
of this well we have a perfectly fine string right here in our get name so let's go ahead
and I won't copy that I won't be too lazy get name so it's going to say your first girlfriend
was in whatever girl name is as defined up here that's what it's going to output you'll
see what it does later but for now let's go over your apples one make sure you import
your scanner and make a tune object so we can access that class and now let's go ahead
and just put a basic prompt on the screen so we can tell the user what to do system.out.print
line and what are we going to print I just put print something like enter a name of first
girlfriend here just any prompt will do just so we're not lost so the user actually knows
what to do and after that go ahead and we need to make a temporary variable so we need
to put string temp you can name yours whatever you want I just name my template fun input
and this is where we use our scanner variable the next line with empty parameters and again
whatever the user types in as their answer it's going to be stored in the variable temp
so that's what this line does this is just a prompt this takes whatever they enter and
stores it in the variable temp right there next we need to do something with this so
let's go ahead and use our tuna object and we need that to access set name and of course
the parameters for this is going to be that temporary variable temp so what it's going
to do is take the name that they entered and set it equal to girl name right here it's
going to take it which is name set it equal to girl name now we need to do one last thing
now that we've got that name equal to girl name all we need to do is do tune object--
saying with empty parameters and what this does is use the tuna object saying what is
this you may ask the saying just outputs on the screen arm your first girlfriend was and
it's going to be whatever they typed in for the name so let's go ahead and run this see
if I got any errors and I don't so let's go ahead and enter name of the first girlfriend
mine was Kelsey go ahead and press Enter and it says your first GF was Kelsey stands for
girlfriend she was babe so let me tell you guys what this does one last time import scanner
we know what all this stuff does and your name what this does is allow us to enter a
name and it's going to store it in that variable called temp and what the next line does right
here is say alright in your class tuna take that variable temp and use it in the setname
method so now it takes that variable name temp which was Kelsey and stores it as girl
name so now girl name equals Kelsey so it says all right now what well next I want you
to just use the saying method in the class tuna so it says all right we know that girl
name is equal to Kelsey now so now we can use that saying what you told us to do right
here and just your first girlfriend was Kelsey and again if you don't know what printf does
then check out my C tutorials and I go over this this is the common print function in
the language C also you can use it in Java but this is just another way to print so don't
let it confuse you so that is how you use um instance variables and multiple methods
so again you can use multiple methods in the same class and don't forget whenever you have
a variable that's outside a method again it's outside all these but it's inside this entire
class it's called an instance variable so that's where I get that name from so I hope
you enjoyed watching this tutorial I hope you understand at least a little bit of it
so thank you guys for watching make sure to check out my next story I promise I'll be
clearing this up for you don't forget to subscribe to my channel and I will see you next tutorial
so guys bulky welcome to your 17th java tutorial and this story I'm going to show you guys
about something called a constructor now what a constructor does is it allows you to initialize
variables as soon as you create an object so for example whenever you created this variable
right here as soon as you created your object let's go ahead and do that right now tuna
let's make them call tuna object like before equals new tuna and of course we had empty
brackets as soon as we did that and try to use our object such as tuna object saying
and what we do it if we ran this it said your first girlfriend is null and this would be
because whenever we try to use girl name it didn't have a value yet well what a constructor
allows you to do is give this variable of value as soon as you create your object so
let me show you guys how to make a constructor and then I'll talk you guys through it one
more time exactly what does now how you make a constructor is pretty much exactly how you
make a method the only difference is the methods name has to be the exact same thing as the
class name so let's go ahead and write public tuna and as you can see since we're working
in the class called tuna a constructor method has to also be called tuna so let's go ahead
and what I want this to do is assign a value to this string variable girl name so let's
go ahead and write string name of course and I'm going to have this do the exact same thing
is this set named it so girl name equals name the only difference is instead of having to
call the setname method we can just do it all as soon as we create our object so let's
go ahead and now we have a constructor object that we're going to well let me just show
you guys what it does so instead of before whenever we use empty parameters right here
we can use those parameters to pass an argument such as Kelsey now let's go ahead and run
this and it says your first girlfriend was Kelsey so that is a really a simple way of
how you can use a constructor let me talk you guys through this one more time and then
I'll show you guys another neat little trick and if you want to initialize a variable before
or excuse me as soon as you create your object such as this all you have to do is create
a method and name it the exact same thing is your class and all you have to do is put
whatever you want to initialize in here and then whenever you create your object instead
of leave them those parameter parameters empty just put your argument in there and that will
go to your constructor and initialize all those variables for you so if you're saying
RA Y is building a constructor even remotely useful why can't I just leave those empty
like before and use this little method right here later on well say you had more than one
object so say you had another object called tuna object 2 and another thing I want to
point out I don't know if I told you guys this before but every time you have an object
it has its own set of variables so for example say you have something in this one called
Nicole I wasn't named my second girlfriend appropriately named to an object - and say
you have a tuna object to this variable that is what was it called girl name this is an
entirely different variable than this these two objects they can't see each other's variables
so that's the kind of beauty of creating object that gets his own set of variables so now
this object can have output saying and now you can also have a saying for tuna object
to not 21 saying and let's see what this one does right here it says your first girlfriend
was Kelsey your first girlfriend was Nicole so let me got give you guys will run down
one last time the first thing you did is create a constructor and again you did that by naming
a method the exact same thing is a class what this constructor allowed you to do is allowed
you as soon as you created an object and we created two of them right here it took that
value and passed it in a girl name and from there you can you just use a girl name anytime
you want to use that object so it use girl name which is Kelsey and tuna object and girl
name which was Nicole and tuna object to and pass the end of the method saying for both
so again that's not as important as how to create an object or excuse me a constructor
and again one last time what a constructor does is it allows you to initial eyes variables
as soon as an object is created so that's your real quick tutorial on how to use a constructor
and what it does and will be coming across that later on and it'll be get easier and
easier as I'll show you guys how to use it in a more useful application but for now I
just want to show you guys the basics so thank you guys for watching make sure to subscribe
to my channel make sure to check out all my contests and I guess I'll see you next tutorial
what's up guys welcome to your 18th java tutorial in this story I'm going to be going over some
called nested-if statements and also else if statements so I gave you guys a quick heads-up
on whether if an else statement was in a couple of tutorials ago but I'm going to give you
a refresher or a quick in case for God and then I'm going to be getting into some more
stuff that we're going to be needing later so go ahead and make an integer you can have
it whatever you want I'm going to name mine age and set it equal to something like actually
set equal to 60 next let me remind you guys what if statement does what if statement does
is it tests a condition and if that condition is true so age is less than 50 it does a certain
thing so let's say if age was less than 50 which is not why I had to print out on the
screen let's make sure I got this type to right for a go any farther um if it ages less
than 50 then you're pretty young so you are young so it allows you to make a decision
if a condition is true and we also went over the else and what this does is a gives us
a alternative unless that condition is false so let's copy that because we're lazy and
it's put you are old let's so go ahead and see what this does right here it outputs on
the screen you are old so what this did real quick I'm not going to get an in detail because
the told you later it looked at this variable called age and tested if it was less than
50 if it was it was going to print this you are young but it wasn't so printing this and
again this is how you can give java a way of making many choices oryx or just one choice
in this condition but what I wanted to talk to you guys about in this story is not the
simple if-else statement it's something called a nested if-else statement or a nested if
statement and what it is is this instead of just having one action if it's true or one
action if it's false say if it was false you wanted it to perform another decision well
actually what you can do is take this entire if-else statement and plug it in one of these
things right here so say the age is 60 right here if the age is less than 50 then it's
going to do this well the age wasn't less than 50 so it's going to do whatever is in
the else statement so let's go ahead and make another decision in it all right we already
know that the age is greater than 50 so let's go ahead all right if in that case age is
greater than 75 oh what do we want to do we'll just print out you are really old so if age
is greater than 75 we want to print out you are really old else in that case age must
be greater than 50 but less than 75 what are we going to print out we'll pronounce something
nice firm don't worry you aren't really that old with much typos because that's what they
wanted so let's go ahead and run this and when we do it says you are old don't worry
you aren't really that old and let me show you guys what happened right here this entire
thing is your main if-else this is what's going to happen if it's less than 50 and this
is what's going to happen if it's greater than 50 now if it's greater than 50 what did
it which it is the first thing it's going to do is print out this statement you are
old and then it's going to say all right what else do I do if it's greater than 50 oh I
got a whole other decision I got to make so aside from this entire decision I got to make
another decision is my age greater than 75 no at 60 well then I'll go to this one don't
worry you aren't really that old so again that's how you can embed an entire if-else
statement inside another if-else statement and you could have just as easily put this
in the first one by I decided to put this in else because that's where our decision
led so that's your quick tutorial on how to make if or excuse me what this is called is
actually nesting when you have one if-else statement inside another one it's called nesting
if statements or nesting if-else statements so that's your quick tutorial on how to do
that in the next store I'm going to show you guys how to use something called elsif so
make sure to subscribe them looking forward to it and I'll see you then so classes pucky
welcome to your 19th tutorial in Java and in this story I'm gonna be going over something
called an else--if statement you know what else if statement is is pretty much a way
of giving Java many options so in our last tutorial I showed you guys how to build a
if statement such as this one where we had a condition which if age was greater than
60 then we want you to do something like system.out.print line and copy that so I don't have to keep
typing it so if it's over 60 say you are a senior citizen let me type it right there
we go so if statement allows you to do give it one option we also have something called
else which gives it another option say else if it's the less than 60 you are a young buck
and whenever you run this make sure we don't got any errors it says you are a young buck
so again a simple if statement what this allows you to do is give it one option and an alternative
option but there's a way where you can do more than just two options and you do this
by in the middle of if and else you add something called a else if and what this is is pretty
much a third option so let's say all right if age is greater than 60 print this actually
just put greater equal two else if age is greater or equal to 50 want you to print you
are in your 50s and we can have as many of these else if statements as we want so unlike
last time where we only had one alternative it tests to see if age is equal or where it
stands in many different tests so if it's greater than 60 do this and if it's not greater
than 60 then it must be less than 60 so else if it's less than greater than 50 do this
and we can do this all the way down actually let's go ahead and just copy that save us
some time copy and paste and it changes to 40 and 40 s so let me uh put 45 and I'll show
you guys why this does this go ahead and put run and it says you are in your 40s so here's
what it did it can only do one of these things so here's what it does the first thing it
does and remember our age is 45 that says all right is age greater or equal to 60 nope
it's less so I'm not doing this is age greater or equal to 50 nope it's not so I'm not doing
this is age greater or equal to 40 yes it is greater than 40 so I am doing this you
are in your 40s so that's why it output it you are in your 40s and again whenever you
use the if statement as soon as it finds this answer it exits the whole thing so it can
only do this this this or this and since it did this one since age was greater than 40
there was no need to go to the elf's because I already found its answer so that's how you
can give a variable many tests using it is excuse me if else if else statement again
the if is the main test and the else if statements are alternative test if these previous tests
are false so it's pretty much is just a way to give it a bunch of different options when
testing the same variable or you can even test different variables if you want to be
dumb but um that's your basic tutorial on an elf's if statement and how to use it in
java programming again it's called else--if and it goes in between the if and else to
give it more options so in the next couple of tutorials I'll probably be clearing this
up later and showing you guys why it's actually useful in applications but for now we just
wanted to show you guys the basics of it so thank you guys for watching make sure to subscribe
to my channel and don't forget to check out all my other videos so again thank you and
I'll see you next tutorial sup guys is spunky welcome to your 20th Jeff tutorial and this
story I'm gonna be talking to you guys about something called a conditional operator now
what conditional operators are is pretty much another way of using an if-else statement
but it's really compact and it's really small so go ahead and create a variable called age
and set it equal to 21 or set it equal to what everyone it's not going to matter what
a conditional operator is is let me just show you make a simple print statement system out
print line and in this print line this um operator is so small that we can write the
entire thing as an argument and what it is is this the first thing you need to do is
give a test so if age is greater than 50 after you give it a test you add a question mark
and what this means is treat this as a boolean expression so it's gonna see if age is greater
than 50 is it well now you have to give it two options if this is true you're gonna give
it it's gonna execute the first option if it's false it's gonna execute the second option
so since this is already in a print statement all we have to do is add strings of text so
if it's greater than 50 put you are old and how you separate the true from the false you
add a colon not a semicolon that's a regular colon and if it's false let's put you are
young so let me go ahead and run this and let me show you guys what it does it says
you are young in the run box and let me show you guys why this is don't mind any of this
that's not what we're learning today what it does is we asked it a simple question is
age greater than 50 well you only have two choices yes or no so then after you do this
after the question mark you need to give it what happens if this is true if H is greater
than 50 and this is true we want you to do this if it's false then and it's less than
50 then we want you to do this so again ask you a question this is gonna come true or
false if it's true do this if it's false do this in that order and don't forget this question
mark and also the colon right here and remember it isn't a semicolon so that's your real quick
tutorial on how to use a conditional operator and again this is um sometimes can be substituted
for if-else statement but I'm usually gonna be writing just basic if-else statements I
just wanted to show you guys in case you come across this you actually know what it is in
aren't loss so that's it for this tutorial thank you guys for watching make sure to check
out my next tutorial and make sure to subscribe to my channel so again thank you guys and
I'll see you next time what's up guys welcome to your 21st tutorial and in this tutorial
I'm going to be building a simple application to average a bunch of numbers together now
we already learn all the basic tools but we haven't really seen how to combine them all
and build a simple program so that's what I'm going to be teaching you guys in this
story again what I'm going to be doing is inputting ten numbers into the program and
it's going to give me the average so the first thing we need to input numbers is the scanner
so let's go ahead and import it again that's dropping UT il scanner and this allows us
to import the scanner but we also need to create an object so we can use it after we
imported it so let's go ahead and make scanner input you can name it anything you want a
new scanner with a parameter of system in and don't forget our system in is the keyboard
so that's why we have that now that we have a scanner we can use we can begin making the
variables we need for this program so let's go ahead and make int total equal to zero
and this is going to be the total of all the numbers let's say we're inputting grades say
we're like a teacher in importing grades so next we need another one called int grade
and this is going to be the grade we enter we have a next one called an average and this
is going to be after everything is done we're going to average them all together so we need
a variable for that and we also need the last one that's encounter and we're going to set
that equal to zero because we want to make sure our counter starts at zero and I'll show
you what that counter variable is right here when we're building our while loop the counter
variable is just on the counter for the while loop so you're going to say while counter
is less than 10 so this while loop is going to execute ten times on but what do we want
to do ten times well since we're importing or inputting ten numbers we're going to want
to put grade equals input next excuse me you need aa census object next int with empty
parameters and what this does is this it allows whatever number you input be stored in the
variable grade so say you inputted a three grade is now going to be equal to three but
that's not very useful unless we can add it to the total so we can average it later later
so now we have total equals total plus grade and what this does is take that grade and
add it to the total of whatever it was before and if you're saying all right how come you
don't just put total equals grade because if you had um like 30 in this before it will
now be equal to three and you want to add it to that 30 so it would be equal to 33 so
that's what that does now you just need to do spell that right counter plus plus and
this will increment okay you're cuckoo counter each time so it actually goes through the
loop so here's what we have going on right now we inputted a grade we added that grade
to total whatever it was before and then we added one so we can actually get out of this
loop now we have a while loop what we need to do is find the average of the total so
let's go ahead and write average equal total over ten and we know that it's ten because
that's how many numbers we entered and now that we have our average calculated let's
just output it on the screen so system.out.print line and let's put something like your average
is plus average so let's go ahead and run this and see how many errors I got and if
nothing pops up in the run box then you know you're good to go so let's go ahead and enter
ten numbers one two three four five six seven eight nine and ten and it says your average
is five which it should be one through five average I mean excuse me 1 through 10 average
is 10 so let me show you guys one last time how this simple program works this and this
is just a scanner what you should know allows you to input numbers you now have four variables
the total holds the sum of all the grades the grade allows you to import numbers individually
average is going to be used later to average them all together and this is just a counter
variable for the wallet which you should know what it does next you put input next in it's
going to be temporarily stored in grade and it's only temporarily because you change it
with each loop this total keeps a running tally of all the grades you entered so total
equals the old total plus the new grade so this gives you a new total each time that
keeps adding and adding up after you have ten grades entered you take the average of
the total which is the sum of all the grades and divided by ten and after all of that you
just give the user a nice little prompt the average is and it gives you the average so
again this is how you can build a simple program using the tools we have thus far and again
you can add some if statements in here if you want says if grade is less than four do
this if grade is higher than five to that bump we'll get to that later I want to teaching
you some more advanced stuff before that so again that's how you build a basic program
I know you guys were it should build something so there you go thank you guys for watching
don't forget to subscribe to my channel and make sure to check out all my other tutorials
and for all my latest Java programming all that good stuff so again thank you guys don't
forget to subscribe and I'll see you next tutorial sup guys is Bucky and welcome to
your twenty second Java tutorial and in this story I'm going to be talking to you guys
about for loop now the less couple tutorials I've been going over a while loops and this
was a different loop but a for loop is something totally new so let me show you guys what the
syntax for this is go ahead and type four and as you can see it pops up in purple and
that's why you know it's a built-in variable or excuse me a built-in keyword and the for
loop takes three arguments or three statements inside it the first one is where you want
the loop to start the second one's where you want the loop to end and the third one is
how much you want to increment it by so if you want to step one at a time or three at
a time so let me show you guys you need three you need to use the variable three times so
the first statement you want to initialize a counter variable and let's go ahead and
set it equal to one and this is because we want our loop to start at the number one the
next one is the same thing you did in a while loop it's the loop condition so let's go ahead
and put counter less than or equal to ten so now our loop starts at one and it's going
to go until it's less than or equal to ten so it's going to go until ten so next thing
on we want to say how much do we want our counter to increment by well we just want
to go one two three four five six seven eight nine ten so we want to increment it one at
a time so it's going to start at one as long as it's less than or equal to ten it's going
to keep going and we want you to go one at a time so that's the three parameters your
for loop takes so now let's go ahead and write something in our for loop let's just write
system.out.print line and we'll just print the counter counter now let's go ahead and
run this see if we got any errors and we don't and as you can see down in our run box one
two three four five six seven eight nine ten and again this happened because it started
at 1:00 our Cameron variable the next thing it did is ran until it was greater or equal
excuse me less than or equal to ten once it got to eleven it says all right and my greater
or excuse me in my lesson or equal to ten no so I'm not going to do this loop anymore
and then I incremented it one at a time you can also increment it like something like
three at a time by pressing plus equals three remember we learned that in a one the previous
tutorials now once you change the increment instead of stepping by one it steps by three
at a time one four seven ten the next one is thirteen which is outside our condition
so it proves false but again we can change the starting point say we wanted to start
it at six and go until let's say twenty one so now it starts at six and goes to 21 and
steps by threes so let's go ahead and run this and as you can see 6 9 12 15 18 21 and
then it stops so that is how you use the basics of the for loop again it takes three um statements
or parameters the first one is where you want to start the second ones where you want to
end or it's going to run as long as this condition is true and the third one is how much you
want to increment by so this is a variation um there's also the while loop and the for
loop we learned this far there's also a couple other loops that I'm going to be teaching
you probably in the next couple tutorials but for now play along with the prytt what's
it called play around there we go not play along with it play around with a for loop
until you get good at it and once you do you're ready to move on to the next tutorial so again
thank you guys for watching make sure to subscribe and I'll see you next time sup guys welcome
to your twenty-third tutorial and this tutorial we're going to be building a simple program
so now that we learn what the for-loop was I'm going to be building a program to calculate
something called compound interest and this is pretty much like interest if you put your
money in like a savings account or something you it will allow you to see how much money
you have in future well I'm going to be using it for is to see how many subscribers that
I'm going to have in the future so right now if you're watching this tutorial and I'm making
this one I have about 10,000 subscribers and I figured out that I gained one percent of
my subscribers each day so with that information I'm going to be able to calculate how much
subscribers or how many I'm going to have in future so the formula for compound interest
is this a which stands for the amount and this is going to be what we're solving for
the future amount equals P which is the principal which is 10,000 times 1 plus R and again R
equals the rates and all of this is ^ where the power of N and n is the number of years
so we want to solve for a and what we need to do is find P which is the principal 10,000
times 1 plus 1% to the power of n and this the number of years so if this sounds really
complicated let me show you guys how this using a for loop and you will be substantially
impressed I promise you so we need 3 variables a double amount and we don't know what this
is yet so we're just going to leave it set to nothing double principal in again we're
using double because we want to use what's it called decimal points we can't use int
because these aren't whole numbers we need to use decimal points a double principle we
know this is 10,000 since that's how many subscribers I have to begin with and don't
put the common there 10,000 so we have the amount the principle and I told you guys the
last thing they need is the rate so double rate equals point zero one since I'm gaining
subscribers at one percent a day so now that we have the amount that which we're solving
for we have the principal the value that I'm starting with and the rate 1% a day we can
go ahead and make a for loop to build this program so go ahead and all this for loop
is going to do is say how long do you want to run it for well let's go ahead int and
day we want to start with the first day and we want to go to day 20 we can go to whatever
we want just but less than or equal to 20 and put day plus plus and all this says is
start on day 1 give me all the information from day 1 to day 20 that's all this does
now let's get the good stuff we want to solve for amount so that's what we're going to be
setting equal to since we don't know what it is yet but we do know what the formula
is for it it's principal and make sure you type these right principal and again you need
the time to spy something to the power in this calls for an outside method that's math
pow now again you can see right here that it takes two arguments the first argument
what's in the parentheses and the second arguments the exponent so again let's go ahead and add
those arguments right here the first thing what's in the parentheses is 1 plus rate and
what do you want to give the exponent year or let's see what it was day actually so now
we have 1 plus rate which is a amount equals principal which is 10,000 times 1 plus 0.01
to the day and that day is going to loop so now all we have to do is print this out on
the screen system.out.print line and let's go ahead and print a year actually day again
whenever you use like a compound interest and stuff you usually use a year but uh no
one really uses day unless you're you're looking at YouTube subscribers so that's what I keep
typing year plus and add a couple spaces amount there you go so let's go ahead and run this
and I'll show you guys what it does so now we have a list down here that says how many
subscribers I'm going to have each day so on the end of the first day or after one day
I'm going to have um 10,000 100 subscribers that sounds about right that's 1% and as you
can see at the end of 10 days from now I'm going to have 1146 subscribers and a couple
half-half subscribers here and at the end of 20 days I'm going to have over 12,000 subscribers
so that is how you can build a program to calculate something like investments or your
mortgage payments or something like that again let me show you guys how this works one last
time as I always do we needed three principal I mean excuse me principles we needed three
variables because that's how many variables are in this formula you need to figure out
the amount which we don't know yet but we do know the principal which is the starting
point and the rate we want to set that amount what you don't know yet it equals the principal
times 1 plus the rate which is 0.01 times the day and this sort of the for loop comes
in handy because this goes 1 2 3 4 all the way to 20 and next we just output it on the
screen so again um if you're watching this tutorial within Google on what the formula
for compound interest is in you'll be able to follow along easier it's kind of hard seeing
in this unless you're looking at the formula but that's pretty much how you calculate compound
interest so um thank you guys for watching my tutorial play around this and I promise
after you do one time you'll be expert at it so again thank you guys for watching I
hope enjoyed building a simple application I don't forget to subscribe to my channel
and I'll see you next tutorial so guys welcome to your 24th java tutorial and in this tutorial
i'm going to be teaching you the do-while loop in Java now what the while loop did if
you remember is ahead a test condition and whenever that test condition was true executed
the loop body but what the do-while loop does is execute the body before even gets the test
condition so you're guaranteed to execute the body at least once before you even check
to see if you should be doing it or not so the first thing we need to in order for me
to show you guys how to do this is a variable just make a variable called counter or something
and set it equal to zero next let me show you guys the syntax for the do-while loop
go ahead and write du and then write something to do in your body of your loop I'm just going
to put system.out.print line and I'm just going to print that variable counter but I'm
going to spell it right and next I'm just going to increment the variable counter so
this loop actually ends and now after you're writing your loop body then you write the
while condition so you want to do this while your variable counter is less than or equal
to 10 so let's go ahead and show you guys why this works as you can see in the run box
it goes zero one two three four five six seven eight nine ten oops so we had a counter that
was initialized to zero and it did print it out while the counter is less than ten if
we're saying all right this looks an awfully lot Allah Allah wait a minute looks awfully
lot alike there we go like the while loop so if you're wondering why is this even different
let me show you guys it only does this loop while the counter is less than or equal to
ten so what if our counter was initialized to fifteen or something like that well what
it would do since it doesn't check the condition until after the loop is it executes this first
so it would print out fifteen and then it would see our a I'm only supposed to do this
while it's less than or equal to and so I'm not going to do it anymore so let me go ahead
and run this and as you can see in our run box we get 15 so that is the difference between
a while loop and the do loop anytime you want to do something at least once you use a do
loop anytime you want to test it first and only run it if the test is true that's when
you use a while loop but again a do-while loop guarantees you at least one run of your
loop in a while loop only runs it when the test is true so that is how you use a basic
do combined with a while loop to form a do-while loop that's what it's called so practice this
and after a while you get good with it in I'll show you why it's useful later on this
rail wasn't really a useful application but this does have many uses in Java program and
so I'll show you why when we build applications later on but for now just get familiar with
the syntax with it and once you are familiar with it you're ready to move on to my next
tutorial so thank you guys for watching don't forget to subscribe to my channel and I'll
see you next time what's up guys is Bucky welcome to your 25th java tutorial and in
this tutorial we're going to be going over different methods in the math class now let's
see if it's probably like tutorials ago when I built a simple compound interest calculator
we use one of the methods called power by using pal and this allows us to use the power
function but or our excuse me methods they're called in Java but in this one I'm going to
be teaching you about other useful methods so the first thing we need to do is go ahead
and make a print line now on the screen so print line and make it take an empty parameter
for now and in this parameter I'm going to be putting all the methods of the math class
so go ahead and type math I'll put your dot separator and then the first one why say first
first one I want to teach you guys about is called the absolute value and that's abs and
inside your parameter it takes one number now what the absolute value does is take any
number negative or not and give you the absolute value of it so if you have 26.7 and you go
ahead and run this it would output twenty six point seven this is pretty much if you
have a positive 26 it would give you 26 if you have negative 26 it give you 26 how much
away from zero that this is so that's what abs does absolute value the next thing I want
to teach you about is seal and this stands for sealing any number you give it it's going
to round it up so if you give it a 7.4 when you run it it's going to round it to eight
so even if it is really supposed to round down the seven when you use seal it always
rounds it up now since it has seal there's always something called floor again this takes
a number say you have seven point eight it always rounds it down so again seal always
rounds up the number floor always rounds down the number so your floor if you have seven
point eight is going to be seven now that we learn absolute value seal and floor let
me teach you about something outs max now this is the first one we're in the less three
we learned how to make a method that takes only one parameter but max is the first one
that takes multiple parameters in the single method so what max does is you give two numbers
and it gives you the maximum of those numbers so say I have two parameters 8.6 and separate
them with a comma in 5.2 whenever you run this it gives you 8.6 because that's the maximum
of the two numbers so that's how you can get two numbers and find the maximum of those
and again whenever you have a method like that you also have the opposite and this one
is called men what min does is take the lesser of the two numbers so let's run this and the
lesser of eight point six and five point two is five point two right down there so now
we learn absolute value steel floor max min again our power function which takes power
and say you want something like five to the third power you would use five for your first
argument and three for your next one again when you run this this was the same one we
used in our compound interest calculator it gets 125 since 5 times 5 times 5 is 125 and
the last built-in method I want to teach about in the math class is something called square
root and sq RT that's what stands for L only takes one parameter such as nine and it finds
the square root of that number so when you run this in the square root of nine is three
outputs three so that's how you use um your built-in methods from your math class again
you want to notice that you don't need to create an object like you do when you're making
your own classes this is built-in so that's how you use math with your dot separator and
then the method name and then put however many arguments takes I just showed you guys
let's see absolute value zero for max min pow square root I just showed you guys the
seven probably most common ones and those are the ones we're going to be working with
from now on so I just want to show you guys what those do because we're going to be using
them and I don't want you guys to get lost so play around to those throw them in your
built-in methods and you can make some pretty cool stuff of those so for now I just want
to show you guys that so thank you guys for watching make sure to subscribe in my channel
and I will see you next tutorial what's going on everyone welcome to your 26th java tutorial
and in this tutorial i'm going to be building a random number generator we're going to be
importing that from a class and i'm going to be showing you how can use random number
generators to you guess that generate random numbers so let's go ahead and pretend we were
building a program so we could roll a dice and whatever the outcome of dice it just outputted
on our screen so whenever we want to use random numbers we need to import something like we
did scanner but this one is called a random so go ahead put import java.util in the name
for this is random well a smoker's cough or something so let's go ahead and import random
and now just like the scanner we need to make an object for it so I'm going to name my dice
you can name yours whatever you want and of course that's equal to nu naught R atom random
with empty parameters so now that we got our dice object we can use random numbers so the
other thing we need is a temporary variable called number it doesn't need to be called
number but I'm going to call it number and this is going to store the number that outputs
on the dice so for example it's going to store 1 2 6 depending on what number you roll so
I just don't want to roll the dice once I want to roll it 10 times so for that I'm going
to need a for loop so let's go ahead and put forward in counter let's start it at 1 let's
say well counter is less than or equal to 10 less than or equal to 10 and let's increment
it by 1 so now we have a loop that's executing 10 times so now we got to give some instruction
well since the number doesn't have a value yet let's go ahead and give it a value now
what you would usually do is use your object like this number equals dice and then put
your dot separator next in because you're getting an integer and then type in how many
options you want it to have and since a dice has options 1 to 6 I'm going to go and +6
right there now this would work right here and let me show you guys what this does let's
just go ahead and system.out.print line and this is a going to work fine so you can pay
attention to this number plus and we don't need that other one right there so look good
let's go enough let's run it and see what it does it says 3 2 3 4 5 0 1 1 2 1 those
look like a random numbers but the problem is all of these random numbers if you couldn't
tell they go from 0 to 5 and why is that well I'm about to show you guys right now whenever
you use the sarandon number generator and you give it 6 options the options that it
takes by default are 0 through 5 if you use 6 again that's 6 numbers but when it includes
0 it only goes 0 1 2 3 4 5 so what if instead of 0 through 5 is your 6 options you wanted
1 through 6 which we do want since it's the dice and the dice doesn't have 0 well what
will you do in this case is simply put 1 plus and then use our dice object to call this
and what this does is it still uses the number 0 through 5 in this but adds 1 to them so
what it does is give the illusion that we're getting the numbers 1 to 6 1 through 6 excuse
me so now let's run this again and we'll see what we get as you can see there's no zeroes
and you get 4 1 6 2 2 4 3 1 1 2 and all of these numbers are in the range of 1 to 6 and
that's what we want so again if you want to do this all you have to do is list however
many numbers you want in your range and if you put 6 by default it's going to give you
0 through 5 since all computer counting starts at 0 but if we want the numbers 1 to 6 we
have to add this little 1 addition sign and that takes 0 through 5 and adds 1 to them
or increments um or pushes them all over by one so instead of 0 through 5 you get 1 to
6 so that's your quick tutorial on how to import on the random class how to make a random
object called dice and how to use that dice object tag to a go fetch random numbers for
you and also how to add one to give yourself kind of a starting point so that's how in
slot machines or any program that uses random numbers that they make that and we'll be doing
some more complex things with this but for now I just wanted to give you guys the core
basics of a random number generator and that's this so thank you guys for watching copy this
code and play around with it and soon enough there will be experts at it I promise so again
thank you guys for watching don't forget to subscribe to my channel and I'll see you next
tutorial what's up YouTube welcome to your 27th tutorial and this tutorial is on arrays
now what an array is is pretty much a variable that can store many values in it so um it's
useful when you want to have a variable that has a bunch of related information and the
thing about an array is that it can only store values of the same data type so if you want
to store a string and an integer in the same array you can't do that so the easiest way
to explain what an array is is just to build it and show you guys how it works and what
it does so I'm going to do just that the syntax for creating an array is simply first thing
if the type in is what type of array you want to create and I want to create an integer
array to hold integers so go ahead and press int the next thing you do is name your array
and I'm going to name mine Bucky so it doesn't get confused the next thing you do is put
square brackets and this is right between the P in the backslash on your keyboard and
this is just so the square brackets tell Java that you're going to be working with an array
if you did this then it would just be a regular variable but when you put the square brackets
in Java knows all right you're about to make an array the next thing you do is set it equal
to new since it's a new array in again or whatever type of data you're going to store
and square brackets the only thing different is inside the second square brackets you need
to put a number in there and this number indicates how many values or how many variables you're
going to store in your single array so I'm going to store ten variables or ten values
and if you don't know exactly what's going on I'll show you right now so now we created
array and it's named Bucky and I told you guys that you can store ten values in this
single array name Bucky interesting all right how do I do that well since we created a variable
or excuse me an array named Bucky we have something called indexes for that array and
each index allows us to store a separate value and those indexes begin with zero and go all
the way to nine and again your computer is kind of weird because even though you made
ten elements doesn't start one and then go to ten and actually start zero to nine because
your computer loves to start counting at zero for some reason so our zero with element and
Bucky which is our first element we can set it equal to any value let's set it equal to
87 so you don't keep it fused next you go ahead and make your next index or your next
element of your array and set this equal to some other value I'll set it in quote 543
and you do this all the way until you get to the last one which is nine let me just
leave a couple out because I don't feel like typing them all and now once you did all of
this and you set all your elements of your array equal to value you can use those elements
in your array and I'll show you how to use them let's just go ahead and make a system
out print statement print line might as well all you can do in order to use this array
instead of giving the variable name you just give the name of the array and type whatever
index you want to use so if I used Bucky 1 to print out what I do is say alright what's
the value for Bucky 1 543 let's go ahead and run this and look at our run screen 543 so
that's how you can use an array and again an array is pretty much a single variable
that can hold multiple values depending on what you typed so for example we made this
array able to hold ten value so it was able to hold ten things I only made three right
here because I was too lazy but you can make zero through nine which will give you ten
and again how you use it just read the name of your array and then write this little subscript
let's go to index inside and that's how you reference it just like your reference a regular
variable now if we say in our write an array wouldn't be really useful if we need type
every single variable over and over again well an array comes in handy let me get rid
of this stuff right here one of the neat things about an array is you don't have to type ten
variables when making your rate there's another way that you can do this and let's just go
ahead and get rid of everything let's tighten this up a little bit so she said another thing
you can do is make something called an array initializer and what this does is it initializes
all your arrays without having to type the array name subscript value array one subskribe
array two sub subscribe gets hard to even say so here's how you make an array initializer
go ahead and type what type of array you want Bucky and again don't forget the square bracket
so it knows we're working with array but instead of typing new int n what you can do is just
simply add those little curly bracket things and go ahead and begin typing numbers in here
two four five seven nine and what this does is instead of having to tell on your array
how many values will you want to store in it it automatically counts how many values
you put in and assigns a scut excuse me a subscript to each one of those so instead
of putting Bucky 0 equals to Bucky one equals four Bucky two equals five Bucky three equal
seven Bucky four equals nine it automatically does that for you in this entire statement
so now whenever we use Bucky and say we wanted to use two again it would be Bucky zero Bucky
1 Bucky - so Bucky 2 would be equal to five so let's see if we got it right run it and
as you can see it outputs the number five so again this is a different way that you
can create an array using an array initializer I don't even know what the first thing is
called I guess it's just called initializing or creating an array or something but this
is how most people do it in create an array initializer and then you don't have to go
through the process of typing book e 0 equals yada yada yada you don't have to do that a
million times all you have to do is type it in a nice little list and it automatically
does all the work for you and again whenever you want to use one of your values or variables
in your array just write the name of the array in the number and again the number is called
the subscript or index I'll usually call it the index or just number so that is your quick
tutorial on arrays and how to create an array in order also how to make an array initializer
so I hope you enjoyed it practice this a little bit in the next couple tutorials I'll show
you how to use arrays and programs and how they're useful and why they're useful in applications
so thank you guys for watching don't forget to check out my next tutorial and I'll see
you then so guys welcome to your 28th java tutorial and in this tutorial I'm going to
be creating just a basic array table to show you guys visually the index and value of each
array so let's go ahead and just make a header for our table using a system.out.print line
statement and I'm just going to put index and then put a tab and backslash T is just
a tab it's like five spaces or something I didn't feel like typing five spaces because
I'm lazy just put backslash T then put some value let me show you guys what this is real
quick this gives you this index of value and that's the header for our table so now we
have a header I can go ahead and list the index and value of each array but before we
do that we need an array of course so I'm going to make integer array called Bucky and
I'm going to use that a ray initializer that we use last time and go ahead and add like
five or six values or something at like 30 to 18 the 54 and 2 it doesn't really matter
how many values you add as you'll see why later on but go ahead and make an array and
put some values in there doesn't really matter what they are or what how many you have next
let's go ahead and make a for loop to loop through each index of our array so let's go
ahead and press 4 and we want it to start let's make a counter variable before I keep
talking we want it to start at 0 because our array our first index of our array is 0 even
though it might have 6 elements in it or 5 the first one is the 0 with index and you'll
see why this is later on but and then the next thing you want to do is give it how long
do you want to go well as long as it's less than however you however many are in your
array and this brings me to another point every time you make an array your computer
knows how many objects are in that array and it stores it in something called the name
of your array dot length and the only thing that's going to change in this is the first
one whatever you named your array dot length is would be in this case 1 2 3 4 5 so counter
is less than 5 but for rate changes you don't have to change this so anytime you put your
array name dot length that gives you the length of your rate it's built into Java it's pretty
cool function and now let's just make it increment by 1 and now that we have our for loop looping
through five times beginning is zero we can begin to use this so let's go ahead and make
a print statement out print line Die type of right hopefully and the first thing we
want to output is the counter and what the counter is going to resemble is our index
it starts at 0 and it goes to 4 0 1 2 3 4 and this is going to resemble the index so
this counter has the exact same value is the index next let's go ahead and just put a tab
because I'm too lazy to put a bunch of spaces and now our last value we want to enter and
this is going to be under the value tab of our table we want to enter the value of the
array so Bucky and in here we have the index which is going to be counter so let me run
this and let me show you guys what this does hopefully I don't have any errors and I don't
so here's what it does as you can see we have built a nice little table right here that
shows us what index of our array is and what the value is so Bucky 0 equals 32 1 12 and
so on as you can see Bucky 0 equals 32 1 12 to 18 354 4 2 and that's what this table did
down here there you go now let me show you guys or explain you guys one last time why
this works the first thing we need to do was create a header this is just so to make it
look pretty pretty much the next thing we did was create an array initializer and I
tell you guys how to do this in the last tutorial the next thing we did was make a for loop
and key here is you need to start is 0 and this is because your first index starts at
0 so that's where we want this to start as you can see right here index starts to 0 after
that what you did is enter a maximum for how far you want your counter go and your maximum
depends on how big your array is and you can get this by the special link thing I told
you about and of course you want to go go through it one at a time after that you just
output it on the screen counter which was the index which was cycled 0 through 4 this
thing will just give you a bunch of spaces in between and Bucky and then the counter
inside it or the subscript inside it and whenever you did this a reference say you reference
Bucky 1 it will give you this value Bucky to this value Bucky 3 Bucky 4 Bucky's zero
so that is how you build basic table to it pretty much is just a nice visual so you can
see the indexes and values of your arrays so that's pretty much all that does and I
hope you understood that at least a little bit so thank you guys for watching don't forget
to subscribe to my channel and hopefully I'll see you next tutorial what's up guys is Bucky
welcome to your 29th Jeff tutorial and I'm going to make a real quick tutorial this time
by a request I'm going to show you guys how the sum all the elements in your array or
to use all the elements in your array in any calculation so let's go ahead and in order
to do that the first thing we need is an array so I'm going to name mine Bucky as usual um
I'm going to make the array initializer so you don't need to add the number it will do
it for you say you wanted to average the ages of people go ahead and write however many
elements you want in your array it doesn't matter I'm going to have let's see 1 2 3 4
5 people these would be their ages or anything um once you have an array and I just use the
object initializer or see the array initializer go ahead and make a variable called a sum
or some variable you can use to keep your total of all your elements and and make sure
to set it equal to zero because that's what you want to start out is the next thing we
need to do was like last tutorial we need to make a for loop and this is going to loop
through every element in the array so of course let's go ahead and make a counter variable
and since the first index of every array is 0 we want to start at 0 and then just do the
little array length so what's this Bucky length and this will give you the length of this
array that's the trick I told you guys how to do last time and then go ahead and increment
it by 1 after this and you don't really need these curly brackets since it's only one line
but you want to take the sum and you want to add um the previous number to it so let's
go ahead and do that and what this is going to do is take whatever the value is and add
it to the sum so its first it's going to take 21 add to 0 16 added 21 86 added that 21 added
that 3 added to that and it's going to go through and store all of these values add
together in the variable sum so then once we have all that all we have to do is output
it on the screen so let's go ahead and put system.out.print line or whatever print you
feel like using and then let's go ahead and write the sum of these modules felt like that
numbers is and now let's go ahead and write our some variable and this should be it hopefully
it works there we go and let's go ahead and run this and if it works I actually forgot
to put space right there but the sum of these numbers is 147 right there and again it displayed
it like that because I forgot to put a space right there and if I do it gives us our correct
the sum of these numbers is 147 so that again um to recap one last time go ahead and do
your housekeeping like this what you do if you want to use all these numbers in calculation
then again you don't have to get to sum you can multiply them together or anything like
that just take a variable and just loop through your entire array using your for loop that
I told you last time and use your little neat little math trick that I told you before to
in this right here this will loop through each element in your array and get the value
of it so that is how you use this neat little summing of the elements of an array trick
to get the sum or any other calculation you want when looking at your array so thank you
guys for watching hopefully now you know how to sum all the elements in your right I hope
to see you next time and don't forget to subscribe to my channel what's up everyone is Bucky
welcome to your xxx Java tutorial and in this tutorial I'm going to show you guys how to
build a program where you can take array elements and store them as counters now we already
build a little random number generator we roll the dice and we store those numbers but
in this program I'm going to make it a lot easier and show you guys how to do it with
an array so we're going to be building a dice rolling it like a thousand times probably
in storing how often each number pops up so of course we need a random number generator
so let's go ahead and import java.util random and now once we imported that we can use it
so let's go ahead and put random I'm going to call my object Rand and set it put two
new random and we don't need any ammeter rat and now let's go ahead and make a frequency
array so just name it freak or whatever not that kind of freak this kind and just set
equal to new int seven now if you're saying alright there's only six sides on the die
why am I having seven elements in my array well we're creating seven elements because
remember the indexes go from zero to six zero one two three four five six since zeros include
that seven elements in we're going to have an array index of zero and one to six that
American use we aren't going to use six I mean excuse me we aren't going to use zero
but we need one two six so that's why we need seven elements so we can get one two six if
we only created six then it would go from zero to five so now we can go ahead and make
a for loop to roll the die a thousand times so let's go ahead and put four and just put
in roll equals one go start at roll number one and put like roll if you want to roll
a thousand just go put ahead and put thousand and of course go ahead and increment them
by one and again this just um loops this a thousand times so you should know that but
wait I want to teach you guys is this anytime that rope up so actually let me go ahead and
write the line of code first and then you can see exactly what's going on go ahead and
take your frequency array and go ahead you want to add one two something you want to
go ahead and put your random number generator in this and ours was named R and dot next
int in of course we want let's see six now let me show you guys what this does this is
the most important piece of code in this entire program and let me break it down from the
inside out this is going to give you the numbers 0 through 5 as you should already know what
this one does is shift it all over so you get the numbers 1 to 6 instead of 0 through
5 so if you're saying all right what's this then what this does is add 1 each time that
this hits that number so for example um frequency 1 2 3 4 5 6 are all set to 0 to begin with
if this rolls in on the first roll it gets a 3 then freak 3 is going to equal 1 if it
rolls again in has a 3 freak 3 is going to equal 2 if it hits again and again again 3
3 is going to equal 4 or 5 and 6 and it's going to keep count of all of the indexes
and every time that it hits that number it's going to add 1 to that index so let me go
ahead and well you'll see what this does so the first thing I'm going to add is a header
so you guys actually know what's going on let's go ahead and print line and let's just
print something like face the Nats head frequency I don't know if I spelled it right but it'll
do Pig that will do for and the last thing we need to build is just something to output
our data so the only thing we have outputted right now is a header that says face frequency
next we want to make a table to show you guys face here is frequency so let's go ahead and
again we need to start at 1 or remember the first number on the dice is 1 so face equals
1 and face let's just have it less than freak dot length remember I showed you guys that
and a couple tutorials ago and just increment it by 1 and now what do I want to do in this
loop remember this loop is going to go 1 to 6 and it's basically just going to make a
sees me a table that has the list 1 to 6 so I want to put system.out.print line not pront
line can't get it print line there we go finally and let's go ahead and go face because that's
going to be the first variable we want output is just the face this is 1 2 3 4 5 and 6 the
next thing we want output is just a tab to give some spaces and then after that we want
to output the value of each array index so put your array name you're right there and
put your indexes right there so let me run this program make sure I don't have any errors
and I'll show you guys what we got we got a table that says face and frequency and again
we got that just by adding this face frequency and then we have your array index number and
your array value index number value index number value in again if you're wondering
RA I know right here we created array with 7 and this equals 6 well actually your array
index is start zero but we just wanted out put on the table one because we don't need
zero for anything we could have added zero right there and when you ran it it'll give
us this it would give us six to one and it would give us zero but again since um this
dice then it doesn't have a zero we didn't use it so that's why zero equals zero so now
you can see um the number one rolled 160 times the number two rolled 158 times and after
rolling the dice a thousand times here's the distribution of all that so again let me talk
to you guys one last time and show you guys exactly what we did imported random number
generator right here you guys know how to do that we made an array called frequency
and we set it equal to seven elements in that array what we did right here was make a random
number generator to roll the numbers 0 through 5 and we move that over so it rolled 1 to
6 any time I rolled one of those numbers it took and added one to that index or that value
in that array next we just made a header right here and right here what we did is we just
made a basic table we started it from zero which we actually started at a 1 by just show
the eyes we went all the way to 6 and this just made a 1 to 6 and then we just outputted
the value so again we outputted the dice number which was the index number and the value so
that is how you create a basic um using elements and storing them as counters and array so
it's kind of confusing but just play around with it and trust me you'll get the hang of
it so thank you guys for watching my xxx Java tutorial there's going to be a lot more to
come so keep an eye out don't forget to subscribe to my channel and don't forget to check out
all my latest contests so again thank you and hopefully I will see you next tutorial
so that's lucky welcome to your let's say thirty first draft tutorial and in this tutorial
I'm going to be talking to you guys about something called enhanced for statement now
in the last couple of tutorials I showed you guys how to use a counter controlled for statement
to add up all the values in an array or just sum them all together well actually Java made
a very easier way to do this and they named it an enhanced for statement so let me show
you guys how to do it first thing we need is an array and I'm going to name mine Bucky
as usual I'm going to use my array initializer and just set it to the equal to like four
five six seven they're real good enough now let's go ahead and we need another variable
to hold the sum of the um array and you can name a sum or total total whatever you want
and now let's get the good stuff the Enhanced for statement now this is um kind of like
a for loop but it's different in a couple ways it's different instead of taking three
options it only takes two different things that it needs in the parameters or parentheses
the first thing it needs is the type of data and an identifier now this is kind of like
a counter variable in your regular for loops but it's kind of different this is of course
the kind of Daddy it takes no surprise in the X what the X is is it's going to store
the values in the array as if slooping through your array so the first time it loops through
it's going to store three then four then five and six and seven and you just need a variable
to hold all those values so that's why this is called the type and this is called the
identifier next after that the only other thing it takes is the name of the array now
if we're saying alright why do I have to name this right in case you have more than one
array in your program one name Bucky one name tunafish one name dogwood it knows what array
you're talking about so now that we have our for loop our enhanced for loop or for statement
with our type identifier name we can go ahead and do something with it so let's just go
ahead and take that total and set it equal to X and what's just going to do is it loops
through is add these numbers to total each time it loops so the first time it's going
to add 3 to 0 then 43 then 5 to 7 and it's just going to keep adding up till it gets
the sum and eventually total is going to hold the sum of all those numbers so now that total
poses the sum holds the sum of all those numbers let's go ahead and just print out system out
one print line there you go total now let's go ahead and just print it out make sure we
don't have any errors and we get the value 25 so let me show you guys what this did one
more time we went in we made an array called Bucky using an array initializer we then went
and made another variable named total instead of equal to 0 and this would hold the sum
of our array next we made this special enhanced four statement the first argument it takes
is the type and identify what variable you want to store the array values in next you
need to say what array you were working with what array you want to loop through and now
you just did anything with those values and we just decided to add them all to total to
get the sum lastly we just printed it out so that's how you use enhance for statement
and remember this is a force theme and that's specialized to loop through all the elements
in your array so you can do something with them so that's that forest soil thank you
guys for watching don't forget to subscribe to my channel and hopefully I'll see you next
tutorial what is going on everybody welcome to your 30 second Java tutorial and in this
tutorial I'm gonna be teaching you guys how to pass a race in two methods now I tell you
guys how to make basic methods and I also taught you guys how to use simple variables
and methods well a race can be a little tricky so that's what I'm going to teach you guys
how to do this time so before we start passing arrays into methods we actually need in array
so let's go ahead and make one again called Bucky and just make your array initializer
and put 3 4 5 6 7 might as well and that's that for now so now that we have an array
we actually need to build a method to do something to this so here's your main method right now
so go outside your main method and again make sure you're inside your class by outside your
main method and go ahead and type public static void and then name your method I'm just gonna
name my change because I'm gonna want to change my array now what you need here is the type
and your local like how we built a variable last time you wanted to do the same thing
but instead of having just a variable name you want to put those square brackets over
it and that says that all right we're gonna be building the method here and it's gonna
take a parameter of one array so make sure you name a variable which is actually an array
and add those square brackets and that's how you tell that you're giving me putting an
array in here next go ahead and let's add something so we can put in our method that
we just built so in this method what do we want to do to change our array let's just
say we want to take all the values in our array and add 5 to them just add something
real simple so I can show you guys so just add a basic for loop to loop through put int
counter make sure I spell it right set it equal to zero because that's the first index
counter is less than X length and remember that's built-in thing that Java knows and
then just add one and let's just put X counter and plus equal five and what this does right
here is it takes um all the indexes in the array and adds five to each value of them
so again you should know what this does already don't forget to put it in a separate method
aside from your main one so now that we got a separate method built and again the most
important thing probably in this tutorial is in this parameter in this method make sure
to have an array instead of just a regular variable so now back in our main method what
we wanted to do is just call the method so how you do this is put the method name and
in your parentheses go ahead and write the array name so mine is Bucky and again since
Java already knows how long it is you don't have to put how many elements like four or
five all you have to put is the name of the array since this is Bucky with this many elements
just put Bucky and it knows our matically how many elements is supposed to pass into
your right so now our array is changed so we made an array we changed it so now we just
got printed out on the screen so we can see it so let's use that enhanced for-loop that
we talked about in the last Oriole and once I get my parentheses my again this is called
Bucky and let's just put system.out.print line and we'll print Y so that should work
I believe so let's go ahead and run this and as you can see the original array was three
four five six seven and our new array is eight nine 11:12 so it added five to the array which
we wanted to do so let me show you guys one last time what this does we made an array
like we did a million times called Bucky three four five six seven we next said alright take
that array Bucky and pass it into the method change or says alright where's the method
change oh it's down here so I took that array now what am I supposed to do to it Oh take
each of the values in add five - alright I did that now let's come back here alright
what do you want me to do next well just print it all out on the screen and we know how to
do that from last time so make an array change it print it out on screen and again the syntax
is a little different than making a regular method so pay attention especially that you
don't need a number right here and also that you need to define your array right here so
aside from that it's pretty simple so pay attention to that play around with it and
get good at it and then once you're good you can move on to my next tutorial so thank you
guys for watching I hope you learn at least some of this don't forget to subscribe to
my channel and I'll see you next time what's up guys welcome to your 33rd tutorial and
in this story I'm going teaching you guys about multi-dimensional arrays now people
make multi-dimensional arrays whenever they want to do something like make a table in
Java or make something where you need coordinates so again or not again for the first time the
syntax for making a multi-dimensional array is your array type which is in it in this
case first array this is just the name of the array and instead of just one square bracket
you actually add to the first one is for the row and the second one is for the column so
how you create this is this and this of course is a multi dimension array initializer and
this is the easiest way to create multi-dimensional arrays so you pretty much have your main array
right here once that I'm just filling it with number four five six you actually fill them
with other curly brackets where you can put something like a nine ten eleven and then
anytime you want to go to a new rope just press comma and new curly brackets and put
something like twelve thirteen fourteen fifteen so what this would mean right here is you
have an array with your first row being eight nine ten eleven in your second row being twelve
thirteen fourteen fifteen so whenever these you want to call an element in your array
the first row of this the first call is zero so this would be first to raise zero one first
array zero to like this first array zero one that's why I'm talking about when I say first
of all one and this will point to this and anytime you want to call something on your
second row its first array one zero first array 1 1 4 3 1 2 1 3 so that's how you call
elements in a multi-dimensional array but what if you said alright I want to build a
table but I want it to have different column linked can I do that sure Ken so int second
array let's go ahead and create that and instead of in your array having all the same row links
what you do is this may be in your first one you would put something like 30 31 32 33 and
then in your second row you would have something like 43 and then in your third row you have
something like 4 or 5 6 so what this would do is create a table with 3 rows 1 2 3 in
the first row the columns would be 30 31 32 33 in the second row um it would only have
one column 43 and in the third row down it would have three columns four five and six
so again how you would call or use the elements in this array anytime you want to use this
one the first number you would put a zero and then put the number of the elements so
zero zero don't move that 0 0 0 1 0 2 0 3 this one won't only have you would be able
to call 1 0 and this one need to be called 2 0 2 1 2 2 so that's how you call elements
in a multi-dimensional array and actually in the next tutorial I'm going to be building
a program to output this so you can see visually what I mean in how you create a table with
rows and columns and once you see it it's going to make complete sense to you and it's
going to be really easy so that is the basics of a multi-dimensional array again in the
next tutorial I'm going to show you guys visually how to do this so it'll make a lot more sense
to you so thank you guys for watching please check out my next tutorial and I will see
you then what's up guys welcome to your 34th tutorial in this tutorial I'm going to show
you guys how to create a visual table with our multi-dimensional arrays that we made
in our last tutorial so let's go ahead and let's just go ahead and make a whole new method
to do this since it'll be a lot easier and go ahead and write public static spell it
right void main and actually it's not main give it a name like display or something since
it's displaying your red and in your parameters since is going to be taking in a multi-dimensional
array just have it take one parameter in a multi-dimensional array easy enough now in
the body of this display method we're going to need to write two loops one loop is going
to loop through the rows and one loop is going to loop through the columns and actually the
UM loop for the column is going to be inside the loop for the rows so go ahead and write
your first for loop and put in a row or something so you can see that's looping through the
rows and just set that equal to zero and then put Rho is less than X length or whatever
you use up here and then just put incrementing by one and now inside this for loop we want
another loop to loop through the columns so let's put four and put int column pull u n
n there we go equals zero since we want to start at the 0th index then you want it to
be let's see column is less than X because that's what we passed into it row and then
we want just the length of that so what this would do is loop through on the exact length
no more no less that's a neat little trick so anytime you're making a for loop to do
this inside instead of just X length because you don't want the length of that you want
the length of the row so that's how you do this so and then just put increment by one
CEO Lu men can't spell column there we go and this is your syntax for your inside loop
set equal to zero have it go until the end of the row and just increment it by one so
now for the body of text we need for this is we just want it to input or excuse me output
the rows on the screen so system.out.print make a simple print statement not a print
line because we don't we all want to have it in a rows and we don't want a new line
every time where that would be a new row so let's go ahead and just print X row column
and what this is going to do take the index of row which is going to start at 0 and this
is going to start at 0 every time and this is going to print out our rows and so let's
just add a little spacer in between a new tab and this would just give us some space
between our numbers so now that we have another row let's go ahead and just make a new line
so let's put system.out.print line so here is the end I forgot a semicolon right there
so here's what this method does the first argument it takes is the row number and the
second argument it takes is the column number it loops through in and what this does right
here is it just prints a row and what this does is when you get to the end of the row
it's going to move down a new line so you print a new row if I didn't have this it would
print everything all on one row and I don't want that so that's why I added this so now
we have a couple arrays and we have a method to print out those erased let's make sure
I got my curly brackets and right there we go should be good so now I have a race a method
to print out those are race now let's actually call that method so all we would do let's
say I make a print statement so we see what's going on print line and just put something
like this is the first array and to call this what you would do is simply put display since
this is the name of our method and you want to pass it an array so let's go ahead put
first array and again you don't need I spell array with 3 R's yes I did and again you don't
need to put any number of elements since it already knows so next let's just do our other
one system.out.print line and then just write this is the second array and then just display
that to not the Splatt display second array there we go should be good so now let me run
this make sure it works and I'll show you guys and take note in my first array there
are two rows the first row has 8 9 10 11 second row has 12 13 14 15 and then my second row
I have three rows with all different column lengths so now if we look at this visually
in my first array what sex ed firts array spelled that wrong has 8 9 10 11 12 13 14
15 in this row and again that's this in the first row and this in the second row and in
my second array let's see what I got 30 31 32 33 4 values in the first row this one in
the second row and 3 in the last row so again that's 4 1 and then 3 so that's how you print
out visually on your table in a multi-dimensional array so let me explain you guys one last
time how this works and hopefully this will clear some things up we went and we had ahead
and made some multi-dimensional arrays using our two square brackets and kind of putting
arrays in an array that's what multi-dimensional array is this calls the function or the method
excuse me and what the method is or how it works is this um this is just housekeeping
stuff we wanted to pass a multi-dimensional array so we make a multi-dimensional array
we just named X so we can use it in our method so the first thing we do this entire thing
is a for statement right here and this is loops through the rows or the first ones and
inside that you need to loop through the columns or the second one and then we went in head
and just printed it out with spaces in between and once we got to the end of a row we just
printed a whole new line so that is how you do that and again what we did right here was
called making a visual table using a multi-dimensional array so again just copy exactly as I did
and once you type it for yourself you'll understand a lot better but for now that's all I got
for you for this tutorial in the next tutorial um I'll probably do something a little bit
easier but just as useful so thank you guys for watching I hope you subscribe to my channel
and don't forget to check out my next tutorial sup guys welcome to your 35th java tutorial
in this tutorial I'm going to show you guys how to make a variable length argument list
now I show you guys how to build methods using multi-dimensional arrays and also I've built
basic methods but each time you did you needed to tell it what arguments it took but sometimes
you want to build a method and you don't know how many arguments took so for example say
you want to build a method to average a bunch of numbers well maybe you don't know how many
numbers you want to average first so you don't know how many arguments to give it well and
this is where I'm going to show you guys how to make a method that takes however many arguments
you want so let's go ahead and go it a method outside our main method let's build a public
static int sense is going to be returning injures average and I just named average you
can name it whatever you want now go ahead and it's going to take an argument of integers
but you don't know how many so anytime you don't know how many what you do is put in
the lips and that's three dots and then put the name of the variable you want to use and
instead of putting in x and y and z you just put three dots in what three dots means is
all right I'm going to be throwing numbers in here but I don't know if I'm going to throw
two numbers five numbers or 500 numbers so dot dot means just do it and I'm not going
to tell you how many numbers so now that we have that that's a new thing I wanted to show
you in this tutorial dot dot means you don't know how many arguments is going to take let's
go ahead and build a simple function let's just total all these numbers so it starts
at zero since we don't have any numbers yet let's make an enhanced for-loop set our variable
equal to X and it takes numbers in it and let's just add all those numbers to the total
so total plus equals X so it's going to loop through all those numbers and add them all
to the total so now after that we just need to return the average so returned total over
numbers the length so what this does if you through five numbers in here it's going to
return total divided by five if you only threw three numbers in here it's going to return
that total divided by three so number what should actually be numbers is what you passed
in within the length of it is how many values so that's how that works so this is our method
we just thought now we need a way to output on the screen so go back to your main function
so you can run this program and just write system out mu my mouse print line and let's
just print something like um average and then in here in your average method you can type
however many numbers you want 4356 76 and eight and let me go ahead and run this and
show you guys the average of this is 45 so that took um four arguments or four numbers
right there but let's say we wanted to add more 6576 to and thirty-one let's go ahead
and run this does it still work yep interestingly enough the average is only forty four for
this interesting so this is what this does anytime you want to build a method this is
the meat of the story all right here anytime you want to build a method that you don't
know how many arguments it's going to take so for example we pass like for the first
time in eight the second time what you want to do is put what type it is the ellipse and
then you just give yourself a variable that you can work with in your method so again
that ellipse right there means I don't know how many it is so you don't have to make it
over and over again so again we just built the method right there and called it in our
main function right there so that's your quick tutorial on how to make a variable length
argument list in methods that you build so you don't have to write um a new method each
time one and enter a different number of are you um so practice that and once you play
with it a couple times you'll get good at it so thank you guys for watching don't forget
subscribe to my channel and don't forget I'm going to have this video able for free download
so make sure you do that if you want so thank you guys for watching now see you next tutorial
sup guys welcome to your 36th Java tutorial and in this tutorial I'm gonna show you guys
how to build a time class now what time class is is it's pretty much a separate class that
has a bunch of different time functions and I'm going to allow the users to enter three
numbers one for the hour minute and seconds and it's going to convert that in military
time and regular time and we're going to be needing this information for a next couple
tutorial so make sure you build this with me so first let's go ahead and our tuna class
right here make sure you're outside your main class and I'm going to go ahead and build
three variables and they're going to be private variables so not private private same thing
right and make sure they're integer variables one for our and another one for a minute and
another one for a second so just copy that because I'm lazy and turn this one a minute
and turn this one a second so now we've got three private variables but now they have
no value so what we want to do is give them a value and we're going to do that by letting
the user input the numbers so let's go ahead and make a method to do this so public void
since it's not going to return anything it just named a set time or you name anything
you want but this is also going to take three arguments in H for our and M for minutes and
in s for second so it's going to take three arguments but we just don't want to let them
enter any number they want since the hour has to be between 0 and 24 the minute has
to be between 0 and 60 since that's how many minutes are an hour and the seconds also have
to be between 0 and 60 so what we're going to do is check this data before we enter it
so let's plan our is equal to and instead of just H let's go ahead and check it so let's
go ahead and write let's just do this Wow would you look at that if our is less than
or equal to zero and you have to make a hand because it needs meat to conditions and my
ampersand is on seven I put a bunch of stickers on my keyboard for After Effects so now I
can't see them as symbols I have to look at my laptop for it so and now if our is less
than 24 so if it's between zero what the heck just happened there I've caps lock on our
sum no just a funky little thing all right now our is less than 24 there we go a mess
up that time so if these two conditions are met and the question mark are these two conditions
met then our if not zero and why is this little green bar right here there we go must've been
something with my screen recorder so here's what we're seeing right now if our is greater
than zero and it's less than 24 then you can go ahead and use that number if it's not then
go ahead and by default just put zero so we don't mess up our whole program just because
they entered a wrong number so now we need to do this for min and second so let's go
ahead and change minutes and changes the m and change this to M in this to M and then
go to guess what we're going to do next second it changes the s and this s and this test
and make sure this hms matches whatever you put up here and now don't forget to do this
instead of 24 go ahead and change these to 60 I know you got to change all this it probably
would have been faster just to type it by hand but well we didn't so too bad so now
that we got them to enter a time and what this is going to do when we call this method
right here they're going to enter three numbers is arguments so what we want to do is repair
must set the time right there but we have no way to display it so the first way I'm
going to display it is through military time so and that's the time where you don't you
can use like 12 to 24 so you'll see what it is later so public string since it's going
to return a string since it's going to display something obviously to make sure I had to
spell military right and it's going to take no arguments so I'm just going to display
and in the body I'm going to return string format and remember our format takes two arguments
oh the added appointment for us how kind delete those kind but not kind enough would you fill
in the rest of it for us it's going to take and remember in here you need to put percent
and where's my % above the 502 d : % o 2 D : % o 2 D make sure I got that right and what
this is going to do is display the first one in two decimal places and then add a colon
display the second number two decimal places then add column to display two seconds at
two decimal places and after that let's just go ahead and add what we want to put in our
minutes second and now that they're set right here we can display them down here so now
just add a colon after that and now we built everything we need to do so now we can go
to our main apples method right here or Apple's class excuse me and we can begin building
our objects since this was the tuna class we need two tuna object for that so tuna tuna
object and remember you need a object so you can use the crap inside the class so equal
new tuna and first let's just go ahead and even though we didn't set a time yet we can
go ahead and display the military time just see what it has for default so system out
and let's just go ahead and print line and put tuna object - or was it to military there
we go to military and so we didn't set a time yet so whenever we run this make sure I don't
have any X's anywhere it should give us all zeros by default so yeah save those and look
that by default we get all zeroes right here so now now that I lost my screen recorder
oh man hopefully it's somewhere in there whenever I run this program my screen recorder goes
behind my java so I have no idea what's going on right now oh oh well anyways that's really
annoying but anyway so let's go ahead and set the time now so tuna hopefully you guys
can see this object and now we can go ahead and press set time and of course this is going
to take three arguments and let's go ahead and press something like 13 for hour and 27
for minutes and 6 for seconds and now once you run this system out well we can just go
ahead and copy this since real lazy copy once after we set it we can go ahead and run this
again and hopefully if I don't have any errors we can go ahead and see right here the first
one was zero that was before we set the time and the second one when we set the time right
here 13:27 6 what that did is converted all to military time and now I can see my screen
recorder but still behind so now do my screen recorder is messing up got to remember fix
that somehow so now let me show you guys what we did in our set time 13 27 and 6 we weren't
right here passed these in right here 13:27 and 6 I checked if 13 was between this it
was which sect if 27 was between this and 6 was between this since it was it used all
the values and set them in an hour minute and second and that was when we put print
to military pretty much a display method it pretty much just put them in this format and
displayed them out to us as you can see here so in the next tutorial I'm gonna be showing
you how to use not military time regular American time or I don't know what it's called just
regular time I guess so um thank you for watching in the next tutorial you definitely want to
watch because we're going to need that information probably for next couple few tutorials so
again thank you guys for watching don't forget to subscribe it and now see you next time
what's up guys welcome to your 37 java tutorial and in this tutorial I'm going to show you
guys how to use our time class to convert time to a regular string instead of military
time since you know not everyone uses military time some people just use normal time like
1:30 p.m. so let's just go ahead and I'm going to show you guys how to do that so we first
need to make another method and I'm going to name mine public but I'm going to spell
it right eventually there we go right there you got a buddy public string and let's just
name it two strings just a normal string you can name it like two normal or something if
you want but you know what I don't so I'm going to name mine to string and now we got
a new method bill and what we wanted to do here is just return a basic formatting string
so let's go ahead and return string just like last time move my cursor earlier format I'm
I'm not typing to right today and now once you do that it's going to let us format the
string how we want it so the first thing that we need to do is make sure I don't get anything
mess up string format looks good first thing we need to do is give the format we want to
put our time in so for the first one for the hour we just want percent d for the minute
we want percent o to D so percent zero to D and this is because the minute always has
two decimal places for the second it's the same one percent o to D or zero to D excuse
me and for the AM or PM we just want to string since they're just letters so now for the
hour we need to do something weird we need to check if it's zero or 12 then put 12 if
not then put the whoa you'll see what we do so in here go ahead and in your well just
do as I type if our is exactly equal to 0 or if our is exactly equal to 12 then what
do we want to do well let's just go ahead and put 12 for that if not else if it isn't
then just go ahead and put our modulus 12 let me go slide this over a little bit maybe
that will be easier for you guys so what we're doing here is saying all right if that if
they put 0 or 12 for the hour then go ahead and put 12 if they put anything else then
divided by 12 and give our remainder so if it's 13 put 1 if it's 8 by 8 if it's 14 put
2 simple enough that the other ones are a lot easier I promise for a minute we just
need to go ahead and type minute since we don't need to do anything special with that
second just go ahead and type second since minutes and seconds don't have a a.m. or p.m.
only hours do so now it brings us to my last point we need to check whether it's a.m. or
p.m. they're talking about and there we go right there so how do we check if it's a.m.
or p.m. well we need to test the hour first of all and if our is less than 12 then what
do we want to do we want to go ahead and insert a.m. else if it's not let's go ahead and insert
p.m. so that's how we check for a.m. or p.m. right there so now we got our basic two string
and we got a little error right there and I forgot parentheses somewhere right here
I'll find it I promise I will I think I did crap this darn screen recorder is in the way
let me pause this off here we go this is the one I forgot right here again when you're
doing your hour make sure this one's enclosed right here and make sure the entire thing
is enclosed right there but anyways hopefully I don't have any more errors now that we got
our basic string to string method built we can go ahead and call it in our main function
so let's just go ahead and right here we called the military one and we can go ahead and copy
this and we're not going to use it again I'm just too lazy to type to string so the first
one is going to call the military time the second one is going to call the string one
that we just built and by default this is zero and by default this is 12 and if you're
saying alright why isn't this zero and why is it 12 well I'll show you right here and
right here where we put if our is zero or 12 and then put 12 and it was zero so put
12 since when you're looking at regular time there's only 12 o'clock then it goes right
to one o'clock there is no zero o'clock I don't know if you guys knew that or not but
uh yeah drop that in your notebooks there's actually no zero o'clock so now let's go ahead
and quit messing around this crap and do what we came here for we want to be able to set
the time so let's go ahead and put tuna actually probably want to put tuna object and let's
go ahead and use our set time function and we want to editor enter three parameters the
hour and we'll go ahead and change that to 13 which should be one o'clock in regular
time and then for minutes we'll put like 27 and then for seconds we'll pull like six or
something then go ahead and add a colon and now we can go ahead and output these two again
and why type them again if you can just copy and paste them the lazy way that's right that's
what I'm talking about and now let's just go ahead and well let's just go ahead and
output it simple as that so here's what we got here is our defaults that we got from
last time before he set the time and then we set the time in military time it was 13
o'clock on him 1,300 hours 27 6 seconds in regular time is 1 o'clock or 127 and 6 seconds
p.m. so that is how you use here let me recap this one more time what we did is refer much
as output the defaults on screen then we went ahead and called set time passing the parameters
13 27 and 6 so duck took 13 hours and converted it to hours was the next one 27 and stored
it for minutes and 6 and stored it for seconds outputted that in our military format outputted
that in our regular string format so that is your basics of your time class right there
and make sure that you are copy all this and follow along with me because in the next tutorials
I'm going to be teaching you a new stuff not about time but we're going to be using this
code right here so make sure you copy all this and make sure your programs are working
and if they aren't then leave a comment on this video and I'll try to answer it for you
so thank you guys for watching don't forget to subscribe I hope you learned a little something
but not too much and I will see you next time sup guys welcome to your 38th java tutorial
and in this tutorial I'm going to show you guys how to control access to different variables
in other classes and also what the this keyword means in Java so let's first of all talk about
access to these private variables right here now what private means is that these variables
are private meaning that only these methods have access to it only these methods in the
same class this tuna class can use these variables so what if we try to use these private variables
in another class like our apples one well let's go ahead and try to do that right now
first of all we already made a tuna object to use all the stuff in that class but let's
go ahead and try to use something like our right here if we go ahead and try to use our
and change it to like another number we get this little um error right here in a freer
to run this would get a compilation error so I don't want to do that so what it says
is the filled tuna hour is not visible and that is because instead of public it is private
again meaning that only these methods right here can use it so if we want to change that
and put it so these variables can be used in any class outside we need to change that
to public and now you can see my little X went away so now I can use the class outside
to change the variables in another class so let's go ahead and change it back to private
and make sure you spell it right that's kind of important and go ahead and delete this
and that brings me to another point what the this keyword is in Java so what if you're
saying that all right I got three variables in this class name hour minute and second
but also in my constructor in or if this doesn't even matter it can be your constructor or
just another function or excuse me method I'm in talking about C++ for too long changes
the hour minute and second so now probably need to change these two don't you think minute
and second so what if we were to say that these variables in this method inside this
class we're the exact same as these variables right here so in other words if the constructor
or any other method uses names or variables identical to instance variables then what
when you run the program what's it going to do well let's go ahead and see right now um
I guess we don't need this anymore but we'll keep it anyways let's go ahead and run this
and as you can see we get one two three one two three and then we set the time which doesn't
matter anymore then we get one two three one two three so my point is that is this anytime
you have the same variables and a method or constructor or the same thing um you Java
looks to these variables the local variables instead of these right here so retain all
right that's good at all and by the way you should never name these variables the same
as this this is just a foreknowledge but um anyways so what if we want to use that these
variables of four or five and six instead well if the method contains a local variable
with the same name as the field names right here then the method is always going to look
at the local variables instead it's always going to look at these instead and that is
there is only one way to kind of overpower this and that is the use of this reference
to refer to these variables instead so for example you know we had one two three one
two three one two three one two three well if we use a little keyword called this instead
then it's going to refer to these variables so this our this and this second and this
pretty much means this method right here that we're talking about so anytime you use this
it says all right don't use all these use this right here and I'm sure that's a tone
when he was creating this use this one right here I'm sure he said that so now if we go
in our main apples program and go ahead and run it we now get one two three one two three
before oh four five six four five six so that is using these local variables instead of
these private ones right here so again any time you want to use the variables in your
method instead of the variables outside your method if they're named the same thing you
need to use the this keyword and that says alright use these ones right here or use this
one's not here that doesn't even make sense but you know what I mean use them locally
instead of far apart so again it's easier just to see and play around with it and once
you do you're going to understand what this means Wow it's like a little joke and anyways
I hope you understand at least a little bit in the next tutorials I promise I'll be clearing
the software is kind of confusing right now but um keep an eye out for my next tutorials
don't forget subscribe and I'll see you next time what's up guys welcome to your 39th job
tutorial and in this story I'll be talking about overloaded constructors now you guys
already know what a constructor is it's pretty much a method with the same name as that class
and whenever you create an object it automatically calls that method or does that method so I
told you guys how to build a constructor and I told you guys that you can build only one
constructor with the same name as the class but I didn't tell you guys that you can build
multiple constructors in depending how many arguments you have it picks what constructor
to use for example if you called that object with one argument it would have a constructor
for that if you call it that um object with three arguments it would have a constructor
that took three arguments so let's go ahead and do this right now so we first need our
three variables private int hour and then we need that same thing let me put a semicolon
instead of colon we need that same variable with minute and second minutes and second
now let's go ahead and build our first constructor and we do this by putting public tuna since
tuna is the name of the class and I'm not going to have any arguments for this one we're
going to do one with zero arguments one two and three so when you put one with zero arguments
what we want to do is eventually we want to give it three arguments so what will you do
is put this keyword and what this does was is pretty much invoked the constructor again
but give it three arguments this time using 0 0 and 0 so now we need to build another
constructor so public actually we can probably just copy this copy Relays E and paste public
tuna but instead of zero arguments this one if they pass in one argument what are we going
to do well if they pass and one number then let's just assume it's the hour so we put
this hour so if they passed in object five it's going to be five o'clock than zero zero
so now let's build another one and this one's going to have two arguments this one's going
to have an hour and minute so int hour and minute right there so now what we want to
do is invoke hour and minute and again what this is going to do is call this constructor
again but pass it three arguments right here so now I have person all right if we call
this constructor again it's going to look through this one see where is there's three
arguments that look through this one see where do these three arguments and look through
this one see where the story arguments and that's why I'm getting X right here it says
the constructor tuna in it is not to find because we keep calling a constructor with
three arguments what we didn't build one yet so let's go ahead and build one right now
let's go ahead and put public tuna and I could have copied that but in our in minute int
second and if you watch all my X's just disappeared so you see there now up and then once we finally
build on it disappears so now that we have a constructor with three arguments we can
go ahead and build something what it does in fact I'm going to build another I'm going
to have it call another method right here that sets the time to our minutes and seconds
and we're actually going to be building this and the next tutorial actually we can prep
a builder right now we have enough time but let me explain you first what this one does
if a user was to create an object with zero arguments it would give it three arguments
by default so it can go down here to this constructor if a user was to enter an object
with two arguments it would go to this one it would use the first number is the hour
the second one is minute and the third one is zero and then it would call this constructor
again with three arguments and the only one with three arguments is this in our in minute
is second and then what this constructor does is set the time in a set time you're saying
all right where the F is that method well we're going to vote it right now so take it
easy let's go public void set time and as the arguments again what we want to do is
take three parameters an H into m per minute and into s for seconds now in the body scroll
up a little bit there we go not bad and the body what do we want to have to set the time
well actually we're going to be building three methods for this as well and I know it gets
confusing but set our and this is going to take an hour set minutes and this is going
to take a minute and set a second and this is going to take a second parameter and now
once you close this it says the method set minute is undefined and we're actually going
to build three I mean let's see six more methods I think we're going to build methods for setting
each of the times and why do we need a new method for set our new method for cent minute
and set second well we can't just have any hour they type in um we need to build a method
for it like last time where we set the hour we had to check if those between 0 and 23
or 24 and set minute we need to make sure this is between 0 and 60 and also for set
seconds and we're also going to make make another method for a get method I mean I not
get method for get our get minute and get seconds and that's just going to return everything
but we're going to be doing that next tutorial but if you don't still don't know why I'm
having set separate methods for these it's so only these methods can access the hour
and down here as I said in last time we don't want any other class that have access to this
information right here so that's why we need to build a separate set and get methods for
each hour in um like hour minute and second but anyways that was supposed to be for next
tutorial that was just a little jump ahead the meet of this tutorial that I want to tell
you is right here um let's see yep these are all constructors right here one with zero
arguments one with one two and three and I just want to tell you that you can build a
different constructors depending on how many arguments you have that way you don't really
need the user to enter one specific set of information it varies and then depending on
how much information they entered it does a certain task so if you don't understand
this I promise in the next on either one or two tutorials this will make all sense once
we finally build our program and you can see what's going on but thank you guys for watching
make sure to write this code right now and again like I said by the end of these tutorials
you're going to understand why we did it and how this all works but for now thank you guys
for watching my tutorial on overload constructors don't forget to subscribe to my channel it
is the best channel ever again thank you guys for watching and I'll see you next time sup
guys welcome to your 40th java tutorial and in this tutorial we're going to be building
the set our set minute and set seconds methods now we already got the data from the data
that we need in the last story so hopefully you watch that because you need this code
anyways but if you did and typed everything up then that's good you're good to go so let's
go ahead and build first the set our function or excuse me method the same thing as function
pretty much I just spent on around C++ all day so anyways public void since it doesn't
return anything and sets our and what arguments is take in H it takes just the hour since
again we're only setting an hour nothing else now in this what we want to do is check if
the data is between 0 and 24 and if it is it's valid if not we're just going to set
it to default like zero or something and by or something I mean 0 so let's take that hour
and first let's make a simple if statement is the hour greater or equal to 0 and where's
my add 7 right there again I put stickers on my keyboard so it's kind of annoying and
is the hour less than 24 you're going to say all right if that is true then you can go
ahead and use the hour else if it's not true go ahead and put 0 so again this is a conditional
expression or um it's pretty much the same as an if statement what this is going to do
is check if the Hauer is between 0 and pretty much make sure that hours greater than 0 and
less than 24 and if it is use it if not use 0 so now do the same thing for minute and
second except when we use this um we want to change our minutes and seconds and we also
want to change this minutes and seconds and we also want to change this minutes and don't
use a capital and second and also on changes minutes minutes second second and instead
of having let me check the sub data real quick in minute minute minimum in it instead of
having 24 you want to change these to 60 since with minutes and seconds it doesn't need to
be between 0 to 24 it needs to be between 0 and 60 so that's why we do that so now that
we've got our hour minute and second built what's this a right here to get methods second
hour and type 2 that's good we're good to go hour minute second what we need to do this
is what Oh saner here sets minutes and set second so then now that we got our check for
hours our check for minute and check for second what we can do now is write get methods just
to get them since we write a set method to change data if need be if it's bad and we
need a get method to pretty much when we're retrieving our data um this is what we need
this method for so let's go ahead and write public int and the first one we want is get
our so get our and this is going to take no parameters and what it's going to do is this
is the easiest thing you'll do all day get returned hour right there so what this is
going to do is pretty much take this and return it so let's copy this and keep up with me
it cuz it's going to be a little too easy get minutes and this one's going to return
a minute and get second and this is going to return a second so now that we have three
set statements in three get methods I mean set methods get methods we need one more method
and I pray this is the last one the last method that we need is pretty much just a formatting
method and this pretty much just spits it all out or display method so like last time
I'm going to put public string since this is going to return a string and after string
I'm just going to name it output like two two military times it'll do military time
military time is a lot easier than normal time and this isn't going to take any parameter
since you're just going to return string so what we want to return is let's go ahead org
string format and again it tells us we need two arguments in the first argument is let
me get rid of this as annoys me the first argument is how we want to format so we want
to format percent zero to D percent zero to D : percent zero to D so percent zero to D
percent is your T % zero to D and this means the hour the minute in the second is each
going to have two decimal places so for the first one for the hour it says what number
do you want to use well we want to use whatever number was returned in our get our method
right there for our second one for our minutes we want to use whatever was returned and our
get minute method right there and for our last one of course get second right there
so now these functions let me double check this make sure it's good these functions are
good to go we built all the methods that we need and again whenever I say functions I
really mean methods I'm just uh just another language those methods are all we need so
now we can go in our main apples class right here and we can begin building our objects
now again whenever we're going to go ahead and create four different objects because
we had four different constructors and actually I'm going to be doing that in the next tutorial
so as you can see we have one constructor two one two three four so in the next tutorial
I'm going to be putting four different objects each to use these constructors in four different
ways and then I'm going to be summing up and telling you how this whole program works and
actually what the heck we just did and then you're going to understand that awesomely
so thank you guys for watching don't forget to subscribe and now see you next tutorial
what is going on people welcome to your let's see fifty first Java tutorial I believe and
in this tutorial we're going to be talking about GUI some more and unlike last time where
we built a GUI that looks the same on every single operating system like if you ran on
a Windows operating system and a Mac it will look the exact same what we want to do is
we want to build an interface that when you run it on a Windows machine that it inherits
all the look in the feel of a Windows program and when you run on Mac machine that uses
all the buttons in two bars and title bars and windows as a Mac so the last one we built
in the last tutorial was simple for like you know a really easy one but we're going to
be going in some complex and it's just a better GUI system of building interfaces so that's
what I'm going to be teaching you today so the first thing that we're going to want to
do again we're going to be working with two classes the apples main class with our main
method in it and a subclass or another class called tuna right here so in your tuna class
or not the one without your main argument the first thing we're going to do is import
a bunch of stuff to use so the first thing we're going to import is go ahead and type
import java.awt dot flow layout and make sure you're capitalized like I am and what this
is going to do is it's going to import pretty much how things are placed here that's how
I can say it what this is going to do is pretty much give me the default layout managing it's
going to say alright I'm going to put this here when I build my next item put it to the
right of it when I build my next item put it to the right of it and once you run out
of space just move to the next line so this is pretty much the layout of our thing and
we don't want to have to program an entire layout so that's why we're importing that
the next thing we're going to want to import is go into import Java X don't forget that
X and we're going to import swing dot j-frame in there we go what jframe is is it pretty
much gives you all the basic windows features like the title bar the minimize the maximize
the X to close and it pretty much just gives you the basic window so we don't want to we
want to use what we have already so that's why we're importing that and the last thing
we're going to import in this tutorial is the J label and this is where we're going
to be building in this pretty much a label is pretty much just a line of text the N it
also lets you do simple images but let's go ahead and import Java X again swing the J
label make sure it's capitalized just like that so again this is gonna there sorry about
that I had to take a little break someone knocked on my door but what J label does is
it allows you to output text and images on the screen so that's why we need to import
that so now we can go ahead and start programming this stuff right here the first thing we want
to do is use inheritance like we did before public class tuna extends ext NDS J frame
so we need this tuna class right here to inherit all the stuff from J frame and again like
I said J frame gives you all the basic windows features so now we have all of the title bars
the minimize the maximize the X button that's what that does now that it's in here it inherited
all those qualities let's go ahead and the first thing we're going to want to do is build
a variable which is pretty much going to be our item so let's make it private and let's
just go ahead J label that's what type it is and just name it like item 1 or something
like that and this is pretty much it's going to be the item that we're adding I mean you
need to name it something so why not item 1 and now we're going to build a constructor
so go ahead and type public tuna or whatever your class name is and give it empty it's
not going to take any parameters and the first line in our constructor and I spell - nor
on the first line in our constructor is the title of the window so let's go ahead and
how you do that is you put super and the only argument it takes is the title so this was
going to be your in title bar though the title bar let's just go ahead and put that so that
is how you add a title to your window the next thing we're going to want to do is set
the layout which is pretty much the default layout so go ahead and put set layout and
all of these methods unlike super and set layout these are imported whenever we imported
everything so set layout and as a parameter for this let's just go ahead and put new flow
layout right there and what this does is pretty much give us our default layout so this adds
a title and this gives us default layout so now let's go ahead and start building the
actual item that we wanted to build so again we already named item one so that's item one
equal to new jlabel and remember remember labels are pretty much just text so this is
how we put text on the screen and the argument for it is what you want to help put on screen
this is a sentence make sure you spell it wrong and now we pretty much just have an
item that says this is a sentence all we have is text on the screen now now we can add some
functionality to our item one thing we can do is make a little scroll box pop up when
you hover over it in that scroll box is actually called a tool tip text so we can take that
item one that we just built and set tool tip text like that and now our argument is what's
going to appear when we hover over it so what do we want to appear this is going to show
up on hover or something like that so now we built a line of text on the screen and
now when you hover over it this thing is going to pop up and we use that during the set tooltip
text method little tongue twister and now lastly the last thing we want to do is add
the item to the window we made an item we have a window but we didn't add the item in
the window so that's easy all you have to do is add item one so again don't forget it
just doesn't automatically add it for you have to actually add it why they did that
I don't know but that's what they did so now we have an item in pretty much our tuna class
is done now go over to your main apples class right here and we can go ahead and start building
on the object for that or calling our toon class so the first thing we're going to need
to do is import one thing right here import Java X dot swing make sure it's pillar right
dot J frame and again like I said before this pretty much gives you all the basic windows
features so now let's go ahead and start building an object for tuna tuna I'm just going to
name my object bucky equals new tuna or whatever your class's not going to take any arguments
and now you need to do a couple things does this object which is pretty much the window
with a line of text in it what we want to do is set and this is really important set
default close operation right there and what we need to put in here is how we want our
program to close so put j-frame dot exit underscore on underscore close all in capitals and what
this does is whenever we hit the X button the program terminates if you don't have this
then your program is going to be messed up whenever you hit the X button is my screen
recorder so we run all right just check it now what we need to do is set eyes for that
object or a window so again put Bucky set sighs let me see how much time I got left
two minutes so this is going to go real quick Bucky set sighs and let's just put it like
275 by 180 or something like that and now the last thing we need to do is Bucky set
set visible and I'm going to do this real fast set true and this pretty much means so
you can see it now let's go ahead and run this and what we got right here is this is
a sentence and when you hover over it it says this is going to show up on hover so again
that's what we built right here this is a sentence put the title bar at the top and
when you hover over it this is going to show up on hover now I only have 30 seconds left
so I can't talk you through how we did this all again but make sure you watch the next
tutorial we're going to be putting more stuff in here and I'm going to be explaining how
we did everything but again like I said I don't have time to do this tutorial because
YouTube only lets you upload 10 minute videos so um thank you guys for watching and I'll
see you next tutorial what is going on people welcome to your 50 second java tutorial and
the store is probably going to be pretty long it's probably going to be a multi-part tutorial
and we're going to be talking about event handlers so what an event handler or event
handling is is in Lesson toriel I showed you guys how to build a simple GUI or GUI well
what an event is is anything that the user can do like move their mouse click a button
press Enter those are called events in computer language now what an event handler is is the
code that responds to your mouse movement or button clicking so the event would be click
a button the event handler would be the code that says ok when you click this I'm on another
box the pop-up that says you just click the button or something like that absolutely the
event is what the user does the event handler is what the programmer makes when they do
that event and this overall process is called event handling so I'll be talking about this
later on but you guys need to know um pretty much I just want to tell you in this program
or tutorial we're going to be building a window on the screen and we're going to be building
some text boxes inside that window and whatever the user enters text and press Enter um the
event handler is going to say alright I'm going to pop up another box so let's just
go ahead and get started if you know I'm talking about then you'll figure it out eventually
ok neat little break now the first thing we need to do is import a bunch of crap that
we're going to use in this tutorial so I'm going to go ahead and type import java.awt
dot flow layout and make sure your egg is not capitalized and go ahead and you can just
copy this right here because we're going to be using something similar to it so we imported
java.awt flow layout the next thing we want to import java.awt events dot action listener
let me make sure I spelled listener right yep and what this does the flow layout pretty
much gives you layout the listener listens or pretty much waits for the user to do something
like press ENTER or something so now the last thing we needed poor import from the AWT is
event come on cursor seriously AWT event action event and these are events which I told you
about last time now we need to import four things from the swing so let's go ahead and
import Java X make sure you have the X swing dot J frame and now we can go ahead and well
might as well copy that because we're going to important three other things copy paste
paste paste we need to import jframe jtextfield and make sure that you got your cap elation
same as mine with a TF capitalize jframe J texture J password field in text field is
somewhere working type text pest password is the same thing but instead of showing the
text it covers it up in Asterix so you can't see and the last thing is option pane so now
we imported everything that we need for this tutorial and these pretty much are all built-in
methods and variables that we're going to use so now what we need to do for our tuna
class is just like before we need to extend or inherit all the stuff from J frame and
what this pretty much does is give us all it pretty much lets us to use a window so
now we created a window like any other program now we can start putting stuff in it so we're
going to put three items plus a password field in it or pretty much for text fields so let's
go ahead and make variables for those let's make our item our first text field first so
private J text field and you need to put jtextfield and you can name anything you want I'm just
going to name my item one and I'm going to make three of these so I'm just going to copy
this because I am extremely lazy item two and I'm three in the lesson sorry about that
head take a little break help my room I find his phone but alright so we created three
text fields right here now let's go ahead and create our last password field and we'll
set that to private as well because I mean why not J password field like that and let's
just uh give this variable oops password field looks good so now I did something oh no semicolon
embarrassing so we created three items plus a password field now let's go ahead and create
our window so let's go ahead and create a constructor for this and since of my class
is named tuna I'm going to have to create a method that's named tuna so I'm just going
ahead and go inside public tuna doesn't take any arguments and whatever I create in this
is pretty much the window so the first thing I need to do or first thing I actually want
to do is add a title to this and if you remember from the last tutorial to do this you just
put super and then in your argument you put the title or whatever you want for the title
not going to get anything fancy because I'm not creative at all so now we're going to
have to set the layout for this which is pretty much how things are placed and we don't want
to have to code a new layout and place everything so we're just going to grab a 1 by default
and how you do this put set layout and if you're wondering where all these methods are
coming from they're imported some are up here all these built-in methods that I'm using
you just can't make set layout and put it in everything you actually have to import
it so set layout takes one argument and the argument for this is new flow layout just
like that and that is a method in itself so make sure you have that up just like that
so now we have a title in a layout to our window so let's go ahead and start adding
those um textfields right there so the first thing we're going to add is the item one and
we're going to set this equal to nu J text field and in the parameters for this set the
length of it is 10 and now we have an item but we didn't add it to the window yet so
let's go ahead and add item 1 and this will add the item to our window so now although
you can't say you pretty much have a blank window with just a little text field on the
screen now we're going to go ahead and do that for the rest of our items so let's go
ahead and put item 2 and set this one equal to and these are going to be a little bit
different just to change things up a little bit new J text field but is your parameters
go ahead for this one and put like enter text here now what this is going to do is give
you a default text inside and it pretty much is like a prompt now let's go ahead and add
this one to the screen item 2 so now we have a pretty much an empty window with two things
on our screen two text fields so now let's go ahead and change our item 3 variable - yeah
you guess the new J text field and for this one we'll go ahead and type all right let
me think of something to do there we go let's go ahead and type we'll make this text on
editable and we'll give it a default value of 20 and now we have a text field that says
uneditable in it and we want to make sure that the user cannot edit this so we're going
to what we're going to do is take that item 3 which is that text field and how you set
the editable Ness is go set editable and what you're going to do for this is put false and
if you put false in that that text field is not going to be editable so the user is going
to be able to change what's on the screen I mean I read what's on the screen but they
aren't going to be able to edit it and now what we want to do is just I'd add item number
3 to that and the last thing we're going to make is our password field just go ahead and
our password field and we're going to set this equal to nu J to get it password field
make sure it's typed everything right and as a default you can give um get out of there
my pass or something and this is going to show up in asterisks on your screen so now
that we have that we can just go ahead and add on the password field or don't want that
so now we have a screen with four things on it we have four different text boxes and each
of em them is a little different I just want to show you guys that I'm changing the parameters
does a little thing different to it and you'll see this later on visually but in the next
tutorial we're actually going to be adding and event and handlers to this that means
when you enter text and press Enter do this or do that but for now I just wanted to get
our window set up and in the next tutorial we're going to start programming this baby
so I cannot wait thank you guys for watching now see you next time what is going on guys
welcome to your 53rd java tutorial and this is pretty much a continuation of the 50 second
one and there's probably going to be a fifty fourth one but i'm just a real quick view
what we did pretty much already if you didn't watch 52 for some reason we just built a window
and put four things in it and that's a that's pretty much it and now what we're going to
do is pretty much add some functionality to this and what we're going to say is all right
when you enter text and one of those text fields and press Enter what we wanted to do
is pop up another box saying what you entered like when a user would type in something and
it says like a verification or something so let's get right to it now what we need to
do is the first thing that we're going to do is build an action listener object so what
this means is we have four text fields waiting on the screen and they're pretty much dumb
and they don't do anything right now what we want them to do is to add some brains to
them and have them waiting on the screen in listening it's called and when you listen
you're pretty much just waiting for an event to happen so right now they just look pretty
they're not even waiting for anything so what we want to say is all right text boxes listen
for something to happen and when that happens we want you to execute a bunch of code so
the first thing that we're going to need to do is we're going to build another class to
execute the code but this class I'm going to build an object for this class before I
even build the class so the class name is going to be the handler and we're going to
get some errors right here but that's fine and I'm just going to name my object handler
and this is going to be equals new handler class and if you're saying all right where
is this handler class we didn't build it yet but I will we promise so actually hold on
new the handler there we go and what this does is it pretty much builds an action listener
object so now we're going to make a new class later on arm that you'll see what this does
later but what we need to do is add this object to each of the items so item one what we need
to do is add action listener and what this takes as an argument is the object we just
created so that's why we need to do that and the object you can see it is called handler
so handler right like that and we need to do this for each object so let's go ahead
and copy this copy and paste paste I'm - I'm three and of course our password which is
password field right there and add add action listener and this is of course going to take
the handler argument again so now we have an object of the class the handler which we
didn't make it but that's what we're going to do right now so once you uh let me make
sure here's jframe here's the one class here's where one class ends or pretty much our constructor
and now here's another thing I'm going to tell you um this pretty much whole thing is
our constructor which is pretty much a method for um this tuna class and you probably think
in our I'm going to build a new class I'm going to build it on the outside of this well
actually we're going to build another class but we're going to build it inside this tuna
class and when you do that when you build a class inside the class the class that's
inside inherits all the crap from the class that's outside so that's what we want to do
so let's go ahead and make this private because it's no need to make it public class and I
hit insert class the handler or whatever you named it right there and now we see we get
a whole bunch of other error messages implements P implements action listener and I spelled
it there we go implements action listener now what we want to do is go ahead and build
body for this and now what we have is our class and why did we need to name the handler
because that's what the object was and it implements action listener and what this means
is pretty much that this is going to be the class to handle the events and you'll see
why later on but I know it's confusing right now but what this action listener class is
is it takes one method there's only one method we're going to be building in this class and
that's the method that's going to be called automatically whenever an event occurs and
to do that type up lick void and I know this doesn't make any sense to you right now it's
weird because you kind of have to build the whole program and see what's going on but
I promise by the end of this you'll see what's going on action and you need to name this
performed and as a argument you need to take action event and then you need to name your
variable something like event like that and now as well let's go ahead and start building
the body for this so what happens in this private class right here is as soon as an
event occurs whatever happens inside here is going to handle the event so let's go ahead
and type some stuff - um pretty much just add a body to our method the first thing we're
going to do is build a string variable and we're just name is string and set it equal
to an empty string and this is going to be the final string the outputs but um you know
we don't need to worry about that now now the next thing we're going to want to do is
make pretty much a huge if statement in you'll see why in a second the first condition we're
going to test if the event and remember this is like an enter or a click and in this case
is going to be if the event is going to be when the Hugh can't talk when the user presses
enter so if the event equals get source and the source pretty much means where it happens
if it's equal to item 1 and what this means is pretty much if they clicked enter on text
field number 1 then what do we want to do well the first thing we're going to want to
do it is take that string variable that is empty right now and we want to set it equal
to string format and what we just use C style formatting and we're going to want to set
this equal to put something like field 1 percent s and put those in quotation marks so what
this is going to output is field 1 with a variable right here and as our variable what
we want to do is press event and this is get action command now this is the last thing
in this tutorial that's going to confuse it what get action command means after I added
my semicolon is get the text from that location so if they type in Bucky and field 1 and press
Enter what this is going to do is say alright your event source or you hit enter at item
number 1 so what am I going to do is put field 1 equals Bucky in you'll see why later on
but for now just follow with me the next thing we're going to do is build 3 else ifs so else
if and just go ahead and copy this copy this paste in event to field - and we'll get the
action command for that now let's go ahead and just copy this one more time copy make
sure my cursor is in the right spot oh cool it's messing up cool I am paste if the vent
get sorts equals from item three then we just want output field three equals that and now
what we want to do lastly is put else--if and let's go ahead and put event get source
all right if this events occurs on what do we name it password field then what do you
want to do set the string while you mess it up pause set the string equal to string format
and and let's just go ahead and do the same thing the format is let's put password field
is percent s and arguments it takes is of course event get actually in command right
there so now make sure we're good go don't have any errors decide from these and what
error begin right here the method add action there is undefined which is a worry about
that later and the last thing we're going to want to do I actually am run out I'm so
I'll do in next tutorial in the next tour we will finish everything up so thank you
guys for watching I'll see you then what is up people welcome to your 54th and hopefully
the last tutorial in this little three part series and before I get started I just want
to tell you guys about one error that I did I spelled listener run like I always do and
just change that to te NER in uh if you have more than third grade education you should
know that but I don't so now we can go ahead and continue with the rest of our tutorial
so pretty much in this handling class or the class that handles on the event what we did
is we took a string variable and we set it equal to an empty string or a string with
nothing in it and then we say alright if they type something in the box number one and press
Enter then we're going to say alright we're going to change that string to field one equals
whatever you typed in if they did it in field2 field3 or the password field that's what we're
going to do so now we all we pretty much did is we changed that string variable so if they
type Bucky for the string variable this pretty much changes your string variable and now
the last thing we need to do in this class is pretty much output it on the screen somehow
so this is the last line so don't worry just type the j option pane and why do we get to
use this because we imported it up here at J option pane and what this pretty much does
is pretty much just a blank window and what we're going to do is show message dialog make
sure I didn't spell that wrong too and it's going to take two parameters the first parameter
it takes is null don't worry about that has to do with positioning and the second one
it takes is your variable you want output in that string so now let me walk you guys
through this one last time we imported a bunch of stuff to use that's not important the second
thing we did is we pretty much built a window I'm using J frame which we did in the last
tutorial you should know what how we did that now here just here we get to the good stuff
what you definitely need in order to handle events is a pretty much an event handling
class that implements action listener and we built that class right here now once we
made that class we can create objects for that class and we created a class or excuse
me an object named handler so anytime that you use this object it's going to refer to
this class right here and after we built this class and we built this object we're going
to go ahead and add that object to each one of these items now we gave each of these items
some functionality so before they were just sitting there and now once you press ENTER
on them they actually do something and what does has to do with this method right here
now inside your class that handles events you need this method and you need to call
it action performed this is a built-in method that has to do with the action listener class
this method gets executed automatically whenever an event occurs so the event has occurred
is enter so whenever they press enter what's going to happen is it's going to create an
empty string it's going to test what you're going to want to change that string to and
then it's going to output it right there so this is all the hard stuff we did it right
here so copy that and do exactly as I did now that we made all the hard stuff we can
go ahead and just execute it like we always do so my main thing is called apples you can
do anything you want the first thing we're going to need to import in the only thing
so don't worry import Java X Java X swing jframe and this just make sure that we can
use Windows in our main method right here so now let's go ahead and start calling our
object so let's go ahead and create a new object called Bucky or whatever and my class
was named tuna you can name it or whatever a year you can name your object whatever you
want but make sure well you should know how to do this right now equals a new object from
the class tuna and the second line we want to put is the closed operation liquid before
so we're going to take that object which is pretty much the thing we just created that
window and set default there we go closed operation didn't it's too lazy to type at
all in the argument that this is going to take solve this crap in my way is jframe da
exit underscore on underscore closed and like before this just means that whenever we hit
X we want to exit the program why why they don't do automatically I don't know but you
need to do yourself the next thing we're going to want to do set the size of the window and
we do this by setting the size and we're just going to set this to something like 350 mm
by 100 like what the heck was that I just had like a piece of skin in my mouth or something
pretty disgusting by the way I got something really disgusting to tell you um next tutorial
or five time at the end of this tutorial but it's a disgusting story that I heard the other
day so I'll tell you then that last thing we need to do is set the visibility so set
visible and you learn how to spell so and we'll just set this to true and now let me
walk you guys through this one last time so now make sure you don't have an errors anywhere
and go ahead and click OK okay okay okay and hopefully this works and now here is what
we built go ahead and type Bucky press Enter it says field 1 equals Bucky enter text here
go ahead and type um Fred 3d field 2 equals free D uneditable look this I'm typing I'm
typing can't edit it so go ahead and press ENTER in that uneditable and this password
on by default if you press ENTER it's my pass but you can go ahead and change it to like
that and then it changes to gf GDS so I'm going to tell you guys through this one last
time how this works um if you don't want to listen to me you can go ahead and watch my
next tutorial or watch some videos of like dogs doing silly things online but for those
of you guys who really want to figure out how this works I'm going to tell you right
now this is all well let's go ahead and just skip the good stuff right here in order to
handle events in Java you need an event handling class in that class needs to implement action
listener and what action listener allows you to do is put that listener on something which
means all right I'm going to wait for an event to happen and once it happens I'm going to
do some code and that once we have this class we can go ahead and create an object and why
do we need to create an object well we need to create an object because this add action
or add action listener method it takes an object as its argument so that's why we needed
to create an object and the object it takes as the argument is how it wants to handle
that event so why can't it just automatically know what event to handle because of this
class right here well sometimes you have different events in different classes so that's why
so now once we have this class that handles the event and the object though we can go
ahead and add some functionality to this so what this method does is get performed automatically
whenever you call this class it's kind of like a constructor but kind of not so and
what this does is take the event is the argument which is in this case the pressing of the
enter button what this does is says alright if event gets source which means where the
thing occurred equals item one if it occurred in text box item number one then what we're
going to do is set the string equal to whatever was inside that text box if it occurred in
item to set the string that was it what was equal to in that text box if it occurred number
three set the string equal to was in that text box for the same thing and now once we
set our string variable of something depending on what was inside that text box we're just
going to output it in the J option pane which was that little check box I mean that little
box at the end that pretty much just said Craig or Bucky or whatever I typed in there
so that is your tutorial on how to handle events I'm sorry is really long but it was
also really necessary so I guys I hope you understand pretty much the basics of what
I did we'll be going over this a little later so if you don't you probably figure it out
but thank you guys for watching thank you for supporting my channel and for all the
awesome comments and some of the not so awesome ones but again I thank all of you guys all
the people who watch my videos so I can't wait to do next tutorial so I'll see you then
what is going on guys welcome to your 55th java tutorial and in this story we're going
to be getting into polymorphism a little bit and also talking about or building something
called a polymorphic array to a demonstrate why polymorphism is actually useful and it's
a pretty confusing topic and trust me they're going to be a lot of tutorials on this so
if you're not quite sure what's going on by the end of this tutorial then I promise you
in a couple of tutorials you'll be a pro on polymorphism so in order to demonstrate polymorphism
we first need to build a computer program that uses inheritance in one way or another
so what I did is I set up and you can probably tell this if you watch my last tutorial I'm
going to have a superclass called food in this tuna class in Popeye class since they're
both types of food they're going to inherit all the methods and variables in this food
class so the food is a superclass in the tuna and Popeye or sub classes I already put extends
food in what extends food does is using a heritance to inherit everything for food but
you should know that already if you all watch my other tutorials just for the people who
just started that's what I'm doing so now let's go ahead and build a basic method called
eat in food so void eat won't take any parameters and what it will do is pretty much go system
print online pretty much system.out.print line and it's going to say take it there exists
some out print line um this food is great and now what tuna and Popeye does anytime
you call the eat method it would say this food is great now that's nice and all but
another thing about polymorphism and I'm just going to do this for demonstration purposes
we can over ate the eat method that if we put it in the tuna class and called it it'll
say this tuna is great now and let's just do that one more time with Popeye this Popeye
is great so even though it's inheriting the eat method from the food class we overwritten
it or overrode it with putting a new eat method inside the tuna and potpie class so now when
you call the eat method in tuna it says this tuna is great and now when you call the eat
method in Popeye it says this potpie is great so now let's get into polymorphism now that
we have a program that uses inheritance what polymorphism is is pretty well first let me
describe to you how we built on reference variables and objects before we did something
like tuna Bucky equals new tuna and we didn't really think about what we were doing we just
did it because that's how we were taught but if you're wondering ah right why do I need
to put tuna right here and tuna right here is well shouldn't I only have to put it once
to know that I'm talking about that class well the thing is well first let me go over
what these are this right here Bucky is the reference variable that means whenever you
refer to the variable Bucky you're fairly you are referring to controlling the stuff
in this class or object which is tuna and this pretty much is the data type so it can
control the tuna data type and this object is of type tuna but another cool thing is
you can do this and this pretty much is the intro to pot pull look look chicka-boom chicka-boom
pond sorry this pretty much is the intro to polymorphism not only is tuna of the tuna
type but tuna is also of the food type so what you can do is some pretty cool things
anything that inherits from the super class which is tuna or excuse me the super class
is food can be assigned to Bucky for example tuna since this inherits from food can be
assigned to Bucky Popeye sense it inherits from food can be assigned to Bucky interests
A&R a that's nice to know I can assign this that and the other thing but why actually
would this be useful in any kind of way well the answer that I will do one thing which
you guys probably need and that's an example the quickest example in the most clear thing
I can do is build something called a polymorphic array and this stores objects of different
classes in pretty much the superclass type so let's go ahead and say we have food and
that's the type and let's go ahead and make an array object named Bucky and we'll set
this equal to new food to so now we have an array and it's called Bucky and it's of the
food type and remember like I said since we have something of the food type it can hold
objects of tuna and potpie so for example Bucky zero can hold objects of new potpie
just like that and we'll set Bucky 1 which is a reference variable and we'll set this
equal to the object of new tuna and you're saying all right well that's still great you
have one variable that can hold an object and another very variable that can hold an
object yeah these variables might be of the food class but I don't see why this is useful
still well just wait a minute and trust me you'll see why in a little bit one reason
for using polymorphism is to use polymorphic arrays and that would be something like this
you loop through this array and in essence what it does is it calls each method of this
class right here so let's go ahead and type for int x equals zero and just put something
like X is less than 2 you can also put array length but you know you don't want to and
do plus plus X and now what we can do is say something like this Bucky X Y dot eat and
what this is going to do is loop through each one of these objects and call the eat method
so it's going to loop through the first object which was assigned Bucky zero Popeye and it's
going to call Popeye dot eat which is say this Popeye is great the next thing it's going
to do is loop through the tuna object which is tuna D and then then it says this tuna
is good so then you save yourself the trouble of making a new object called tuna tuna object
then you go tuna e and then you have to make an object and you have to call each method
and I just got rid of my curly brace right there so what this pretty much does is loop
through each of your objects and calls the eat method for each one don't believe me I'll
run it right here it says this Popeye is great this tuna is great BAM how easy is that so
that is pretty much your basics the polymorphic polymorphic array you can assign different
objects to variables as long as this reference variable is of the superclass type in that
way instead of having to build a new object for each one of your you know having to build
a new object every time you want to use a method from that thing you can build one um
reference variable of the superclass and assign it to objects of the subclasses and just loop
the room with a polymorphic array so how cool is that and I know it's really confusing at
first but once I show you some other examples aside from polymorphic arrays you're going
to understand exactly how to use this how it works and you'll be a pro at it so for
now thank you guys for watching don't forget to subscribe and if you have any questions
just leave me a comment and I'll try to answer them so thank you guys for watching again
and I'll see you next time what is going on guys welcome to your 56th job tutorial and
in this store I'm going to be talking about more about polymorphism in how you can have
polymorphic arguments and also return types and bunch of boring stuff like that but I'll
try to make it a little exciting and maybe I'll tell a funny story maybe I won't I guess
you guys are going to have to wait and see if you've been following my tutorials the
first thing you probably noticed is I made a new class called fatty and we're going to
we need to build this class to demonstrate what fatty can do to food tuna and pot pie
if you haven't guessed he's going to be eating these classes because fatties love food tuna
and Popeye trust me I know firsthand I fan I love them so we made this fatty class now
let's go ahead and make a simple method in here it's going to do public void digest which
pretty much means eat then but we can't have eat tuna because he is already method in here
so we're going to have digest and as a parameter is going to take a food object so let's go
ahead and type food X and now we can go ahead and do something with that object it's tighten
that up a bit that's what she said so now we're going to pass in a food object and when
we pass in the food object we're going to take that object which is now named X and
we're going to call the eat method to that and bam that's all all we're going to do so
here's what we can do right now for example if we had a food object named Bucky we can
pass in the name Bucky and then it's going to say alright whenever we call Bucky II it's
going to say this food is great so when every pass in the food object is going to call that
food method which is eat right here but what you didn't know or maybe you did if you know
Java what you didn't know is anytime you can pass in a food object you can also pass in
objects of a subclass for example you can pass in tuna and potpie so since this object
or excuse me this method has the privilege of accepting a food object is this argument
it can also accept any subclasses such as tuna and potpie so Oh would you say you don't
believe me well let me just go ahead and prove it right here let's go in our main apples
method and let's go ahead and start making some objects let's go ahead and make a fatty
object and we'll name it Bucky because that's me and I am fat we'll say equals new fatty
with no parameters so what we did here is we created a fatty object named Bucky and
let's go ahead and now once we can use this fatty class let's go ahead and give us a way
to use the food objects so let's go ahead and create food will create fo for food objects
equals new food and why do we need to create a food object because first of all put your
semicolons get my wood box so he said and I correct myself up sometimes and why did
we need to create this object for food well because this method takes an argument that's
a food object so that's why so now head now let's go ahead and call this with our fatty
object which is Bucky because I'm fatty we put Bucky eat right there and now let's go
ahead and as our argument remember or excuse me that's digest isn't it and as our argument
for the digest method in our Bucky object it takes one food object so we only have this
object right here fo so let's go ahead and pass that in and see how it works okay run
that baby and it says this food is great wow that food must be great wonder what it is
from a double cheeseburger you fat piece of oh sorry and anyways now you pretty much know
how you can pass in objects into methods but wait Bucky you said that you can pass um subclasses
in as well well I didn't lie anytime you can pass a food object in as a method as an argument
you can also pass the subclasses which are in this case tuna and potpie so for example
we have this fatty digest method that takes a food object is this argument but food also
has the subclasses of Popeye and tuna so let's go ahead and make a food let's limit P Oh
equals new food get out here and BAM just like that so not only can we pass in this
superclass of food object we can also copy this because we're lazy and we can also pass
in this object P Oh which would be the potpie so let's go ahead and run this and it will
be new potpie like that let's run it and see what we got this food is great this potpie
is great so what point am i exactly trying to make well what we did right here is this
we created an object named Bucky so we can use all the stuff in this fatty class right
here and the only thing is is this digest method so you know I don't have a whole lot
to work with here we also created a new food object because this digests method it took
a food object as its argument now after that we created a potpie object now this potpie
object is a subclass of this food superclass so any time this digest method can take a
food argument it can also take a tuna or a potpie argument as well so then I prove that
to you guys because I know I'm a liar and I had to prove it to you guys I called that
Bucky object with digests is the food object is my argument and I also called the Popeye
object as my argument so that is pretty much a way that you can have polymorphic arguments
and you can also have polymorphic return types in the same way so we just want over arguments
right here but you guys can figure out just return any other one if you want so that's
that for this tutorial hopefully you guys learn if didn't then you know I don't know
what else to do watch your ass my sorrows I guess Oh disgusting story I told you guys
that I'll tell you one of my friends she went to a well I don't want to say the name of
the restaurant but she went to a fast-food restaurant before and she got this chicken
sandwich and she bit into it it was like creamy in the inside and she's like oh cool like
a new cheese on the inside must be like like a cheese filling or something like that taste
like blue cheese or something well anyways she ended up getting food poisoning and they
found out that in the center of the chicken it wasn't a filling it was actually pus in
the centre of the chicken so when she bit into it pus burst out and she thought it was
filling and she just ate it anyways and yeah she got food poisoning so if you think that's
disgusting leave me a comment and if you never want to eat a chicken sandwich again then
I don't blame you but it wasn't from McDonald's or Wendy so you guys are still good to eat
there so in any way thanking you guys for watching hopefully you enjoyed my discussing
story in my tutorial so I look forward to teaching you some more so I will see you next
time oh by the way don't please subscribe what is going on people welcome to your 57th
java tutorial and this tutorial is going to be not a lot of coding actually it's going
to be pretty much clearing up a whole bunch of concepts dealing with polymorphism and
trust me you're going to have a huge a better understanding of polymorphism by the end of
this tutorial so I just want to go over um overriding a method or real quick in something
I didn't tell you about we built this superclass called food in two subclasses called tuna
and potpie we called a method and food and we call to eat and we overrode that method
in tuna and potpie each of these had a method that overrode it so one thing I didn't tell
you about overriding a method anytime you override a method you must take the same arguments
as in the superclass method so since this e argument didn't have any arguments in it
it doesn't have like integer this tuna can have like integer argument for this and this
papaya can't do the same whenever you put an argument in here this is called overloading
and that's totally different but again if the food if the main eat method doesn't take
any arguments which it doesn't tuna and pop pi can't take any either and also in that
same path it has to return the same thing too so for example if you returned an integer
in here it would have to excuse me if we return an integer in the superclass it would have
to return an integer in the subclass as well and if you're saying all right why do I have
to do this why do I have to not take any arguments and return the same stuff if I'm overriding
it and in essence doing a whole nother method well it's so that you can have consistency
and that is just so anytime you can call the eat method in any of your other scripts then
it pretty much guarantees that you can use any of the sub classes as well so if all of
these methods take the same arguments and return the same things then they are pretty
much interchangeable and that allows you to use a subclass and substitution anywhere you
can use a superclass so that pretty much then you know what to expect and another thing
I want to talk about anytime you override a method so for example we have this food
method in our superclass and we overrode it with tuna we cannot change the scope of this
so for example this food is public right here we can't put private void II see we're getting
an error already this changes the visibility of it and why would you override it with a
private anyway so you know you just don't want to do that and as I talked about overloaded
method is not the same as override well overloaded method is if you hear anybody talking about
this an overloaded method is a method with the same name but it has different arguments
so for example we can do this in I and not get an error but this is a whole nother complete
method so then over so if someone calls eat with a single argument then this method is
called but for now I just want to tell you guys that overloaded and overridden are two
totally different things so don't get them confused so now that we got that out of the
way in in essence I pretty much just want to tell you that whenever you override a method
you have to take the same arguments and same return types there I said I just said it in
like three minutes the other time so let's go back to our Fatih class right here and
let me refresh your memories we know we can do something like reference have a tuna reference
and set tuna object equal to like new tuna and as you know this is the tuna reference
right here and this is the tuna object and we can do this since tuna is a tuna and we
can also do something like food food object equals new tuna and we can do this because
tuna is a food and those are the things we learn in less orioles but what have we not
seen so far what we haven't seen so far is food food object equals new food huh what
about that well this is a problem for many reasons tuna and Popeye we wouldn't have a
problem with and this is because when we put variables in here like color shape tuna has
a color tuna has a shape Popeye has a color papaya has a shape food is too general it
doesn't really have a color it says what color is food I don't know what shape is food I
don't know and if you say alright I can eat a tuna I can eat a pot pie but I can't eat
a food food is really too general and I know that there might be some exceptions to this
but for the sake of programming there are some classes that are just too general that
you don't want to create objects on in this food class is one example so if we had food
class we want to want to give it any variables like color or shape because food in general
doesn't really have a color or shape so all right that's nice and all I kind of knew that
already Bucky so why are you telling me this I mean I'm not giving you guys a tutorial
to tell you guys that tuna has a color but what I am giving you guys teaching you guys
it says in even though we can't create food objects like right here we need this food
class still for inheritance and polymorphism but when we create this class we want to make
sure other programmers can't create food objects either so how can we make this food class
right here and and how can we kind of bulletproof it to make sure that no one creates an object
and they only use this class for inheritance and polymorphism and stuff like that well
luckily the cool people who invented Java gave us this keyword and it's called abstract
and how you make a class abstract this type a be STR a CT abstract right before the class
and what this does is mean all right you can use this class or right here the class of
food but you can't create any object sermon oh yeah one bet look at this food food object
equals new food object right there oh I did oh look I got an error I guess bucket is right
I can't instantiate the type food and that pretty much means you can't create an object
anymore so if we take that abstract away look at this the air it goes away and now we can
create an object so again what abstract means is that you can't create an object from that
class but you can use that class from for things such as inheritance and polymorphism
and stuff like that and that is so when you have a broad class that you only want to use
for those kind of things then it kind of bulletproof your programming and in the next tutorial
we're going to be going over the two different types of classes abstract we went over in
concrete I'm going to be showing you guys an example of why it is useful and also what
abstract methods are so this next couple tutorials is more of general ideas and then we're going
to be building programs to use these ideas and put them to use use them and put them
to use did I say that right my good enough but thank you guys for watching hopefully
you guys understand that um anytime we override a method you need to take the same arguments
and also the beginning of what an abstract method in excuse me extract class is so again
thank you guys for watching please check out my next tutorial and I will see you next time
oh by the way don't forget subscribe alright guys welcome to your 58th jump tutorial and
in this tutorial I'm going to be talking about the two types of classes and in case you didn't
already know we already talked about the two types of classes I just didn't tell you the
technical definitions of them now you guys already know what abstract class is from the
less toriel might as well put that back there so an abstract class is a class that you can't
create objects from so it's only useful for polymorphism and inheritance and stuff like
that in these other classes that aren't abstract even though they don't have a name people
call these classes concrete and the concrete class is a class that's specific enough where
you can make an object's or excuse me you can make an object from it so even though
it doesn't say concrete that's what people call it just because they have to call it
they just don't want to be like not abstract so again food you wouldn't want to create
something because it's too broad a topic but tuna and pie those are pretty specific aka
concrete so you can create objects from concrete classes but not abstract so again let me demonstrate
this by saying all right whenever we try to create a food object it's too broad and abstract
so it gets an error but if we go ahead and create we can again we can have this food
reference but if we create it from the class like new tuna since this isn't abstract we
don't get um well if I got rid of that then we wouldn't get an error right here because
tuna has no restrictions we can create objects from this now let's get rid of this and move
on to our next topic and so you know what an abstract class is and it pretty much is
useless unless you extend it or its inherited from something and as you can see tuna inherits
food and Popeye inherits fruit if nothing inherited food it will be useless because
you can't create objects from it so that's what an abstract class is but just to make
things more confusing I want to tell you that it's can be abstract - and what exactly is
an abstract method well an abstract method is a method that must be overridden so for
example we created this method in food and since tuna and Popeye inherited food we created
a method eat in tuna and a method eat in pot pie in both of these overwrote food I mean
this wasn't necessary that we created these methods but we did it anyways just for fun
so again an abstract method is a method that must be over red in the sense it must be overwritten
which means you need to create a body for it then this body in the main food class right
here this is unnecessary whenever you're creating an abstract method so let's go ahead and create
an abstract method right here let's go ahead and get it rid of everything in our food class
how would we create an abstract method pretty much the only thing you need to do is add
the head in a semicolon so do public will make it public abstract since it's the abstract
method void eat since it doesn't return anything and it's named eat and now you can see this
is a method but it doesn't have a body and if we're saying alright i never created a
method without a body well since tuna needs to override it pretty much it needs to create
a body for it and popeye needs to create a body for it as well then that is why um in
your declaration right here you don't need a body right here so again an abstract method
in your superclass it doesn't need a body so another thing i want to point out anytime
you have an abstract method and a superclass that entire class needs to be labeled abstract
- right there so you can't have like an abstract method and you can't have this class be not
abstract or else you're going to get an error like that so ever someone's knocking on my
door I think hold on oh I'll let them keep knocking I'll get it after this tutorial and
another thing I want to point out is when we declared this public remember I said I
can't change the scope when we override it so we need to make these public as well so
make that public and if you see that little error right here um multiple markers can't
reduce the visibility that is why anytime you see an error with the word visibility
you probably need to change public to private or private to public or add one or the other
so again like I was saying anytime you have an abstract method you need to make the class
abstract too but here's the thing you can have an abstract class but have abstract or
non abstract methods inside it so again in this is a we're getting here because of something
else here because anytime you have a non abstract method you need a body but anyways I just
wanted to point that out anytime you have an abstract method you needs to be inside
an abstract class but anytime you have a non abstract method it can be inside an abstract
class well you just heard it I don't need to repeat myself so the last thing I need
to point out is let's go ahead and make this method abstract again just like this anytime
you have an abstract method you need to implement it in one or excuse me in any of the subclasses
that extends the superclass now I know that sounds confusing but listen to this all right
this tune the class and this potpie class both inherit from this main food class in
this food class has an abstract method of eat in it that means this tune class and this
potpie subclass they both need to use the eat method or or in other words they both
need to implement or override this eat method if you don't have this eat method then you're
going to get an error in your class just like this type pop I must implement the inherited
abstract method food so again any time you have an abstract method in a superclass then
the subclasses need to override that method and that's like making a promise this food
says all right you want to inherit from me okay I'll let you but I'm telling you I have
this abstract method and if you inherit from me you're going to need to override it and
Poppi's like all right I promise I'll override it since you have an abstract method I promise
if you let me inherit from you I will override it for you so if like the classes could talk
to each other that's kind of the conversation they would be having and for saying all right
if I need to override this method anyways what's the point of even having it in here
well it's pretty much for consistency in some polymorphism and I know that doesn't make
a lot of sense but in the next tutorial I'm going to be building a program to demonstrate
everything I went over in the last two tutorials these last two tutorials were pretty much
the rules and concepts that you have to follow and I couldn't just skip over it because it's
incredibly important for what we're going to be learning in that's next up with tutorials
and this is pretty much a core of polymorphism these rules so in like five tutorials ago
when I called a class or excuse me it was an interface and I implemented it that's what
implemented means implement means pretty much you need to override the abstract methods
so that's enough for this story I probably should go see who's knocking on my door but
thank you guys for watching um don't forget to subscribe I got like phlegm bone up in
my throat but anyways thank you guys for watching don't forget to subscribe and I'll see you
next tutorial what is going on guys welcome to your 59th Jeff tutorial and if you've been
watching last couple tutorials then we pretty much learned all the really basic concepts
of polymorphism and inheritance and stuff like that so in this tutorial we're going
to be putting all our knowledge to use and building a program that pretty much gets a
class in puts objects in an array and it sounds confusing but um you're going to see what
it is eventually but before I get into program and I want to show you guys that I got rid
of all my other classes because I'm going to need a better example and I thought that
this would clearly or better demonstrate so I made a new dog class and I made a new class
called fish in that I made a new class called an animal and since dog is an animal and fish
is an animal then I had them both extends or inherit from the class animal and I didn't
want to do that just because it took time but I pretty much made these classes and I
have nothing inside them so if you want to go ahead and do that you can follow along
with me so then here's a scenario we made these three classes called dog fish and animal
and our boss comes in in he's like alright Bucky I need you to program something for
me I'm like what is he's like I need you to make a new class called mmm well name it dog
list or something like that and in this class I want you to have an array that we can add
dog objects so like I said an array can hold many things again hold numbers again hold
strings it can also hold objects and this comes into use um well we're going to be finding
out why a little later but in this tutorial we're going to be building a new class in
a new array to hold dog objects why because our boss told us to so let's go ahead and
do it the first thing I want you to do is go ahead and under package under your source
right click it and click a new class I know you probably can see it so I will move it
down source new Wow now you really can't see it but anyways if you click a new and class
you get this thing that pops up and just go ahead and put dog list or something like that
and now let me move this back so there we go now we have let's see one two three four
five classes and we have this new dog list class and in this class we're going to be
building an array to hold objects from this dog class and the fact that extends animal
is irrelevant for now so we have this dog list class and our boss wants us to build
an array so let's go ahead and do that first let's go ahead and make a private because
no need to make public dog and we're putting dog and we'll name the array the list and
we'll set this equal to new dog with parameter of five or an array length of five and we
put this dog and dog even though we named the array the list we put dog and dog right
here because that is what objects it's going to be holding it's going to be holding stuff
from the dog class so now that we have an array named main list let's go ahead and the
only other thing we're going to need to work with our Ray is a counting variable so let's
go ahead and name private eye equal zero you can set equal to X if you feel more comfortable
with that boat what's this error saying right here all right we need an int private int
I equals zero so now we have an integer that's just a counting variable that's going to help
us work with our array so now let's go ahead and build a new method to add dog objects
so again or not again we need a method named public void we'll name it add you can name
here's what everyone I'm just going to Emma add because it makes sense and as a parameter
whenever recall this it's going to take a dog object and we'll name it D for now short
and sweet and the first thing we want to do in this method is do a if statement so let's
go ahead and make if and we'll put I which is our counting variable that 0 now is less
than the list dot length and what this pretty much does is check if your array is fuller
or not so if I the counting variable is greater than five then your arrays already full so
it would just I mean the method to run but it wouldn't add another one to array because
this can only take five elements so four seven and we try to add one then it wouldn't do
anything so if it is less than length which means if it still has room in our array what
do you want to do well the first thing that we want to do is put the list and I which
would be 0 now we want to set it equal to D so we're pretty much just going to loop
through the array and every time we call this method add an object to that array element
then just give us a little um system.out.print line on screen something that says like dog
added at index space and we'll pie right there come on come on there we go and we'll just
let's go to right now so now we have just a little something on our screen that we can
see visually because you know we want some feedback here so next we'll just increment
through I so here's what this method is doing it pretty much checks if your rate is full
or not if not it adds that object at that index and then it says just gives us the little
message on the screen and then it goes to the next counter so we don't keep adding it
in the same index in our array so now we built this class just like our boss said that has
an array name the list and it's going to loop through every time you call this method it's
going to add a new object to the array so let's go ahead and do this in our own main
method I named mine apples I don't know why but I did so now let's go ahead and make a
dog list object so let's go ahead and put dog list I'm going to need my diello you can
name your object anything you want and then just put equals new dog list so you should
look the exact same as mine except if you didn't want to name it diello you don't got
a so now we can go ahead and create a dog object because I mean we need one as our parameter
so we create dog D or if you don't have a dog class then whatever you have and we'll
set it equal to new dog object just like that doesn't take any parameters because it doesn't
even have any constructors or anything now what we have to do is pretty much call the
method in our dog list so use that object diello whoa embarrassing diello that's our
method type it and we have a method called add in there remember that's what our method
was called and it takes a parameter of an object a dog object and we have a dog object
right here dog D so let's go ahead and put D as our parameter right there and that's
it pretty much just to run this baby click OK and now if everything goes right it's just
say dog added at index zero and if we added another one I would say dog added it index
1 and we'll go 2 3 4 and it will go all the way and that is because we set this equal
to a length of 5 which means index 0 1 2 3 and 4 so that's your real basic tutorial on
our how to create an array to add objects into it but then what if our boss comes in
and says yeah come in boss hey Bucky I got a little problem what's that boss alright
you know how I told you create a dog list to hold dog elements or excuse me dog objects
well now on the people were working for they said all right I want it to hold fish elements
too so how are we going to fix this problem well that is a problem for the next tutorial
and that's what we're going to be doing and trust me you don't just add another entire
class called fish what we're going to do is making this bigger and better and you know
it's just going to be awesome and you're going to learn a lot in next tutorial so thank you
guys for watching and another thing um my 500th video that I add I have like 491 my
500th video is going be the best video in the biggest surprise for you guys ever so
you do not want to miss this 500 video it's probably being released this weekend but is
going to be incredibly awesome and trust me you guys are definitely going to want to see
it so for now thank you guys for watching don't forget to subscribe and I'll see you
next door what is going on guys welcome to my 60th job tutorial and in the last tutorial
we went ahead and we built a new array and in essence we built a whole new class to create
this array to hold dog objects but then it ended tutorial got a little knock on our door
and our boss was like alright you know how you created that for dogs well actually I
need you to create one for fish as well and you know that's nice and also what can we
do what are our options we can go ahead and create a new fish list and do this all again
or you know then we're going to have if he comes back again and tells us to do it with
dolphins or cats we're going to have to keep doing that again and again and again so instead
of just having to create class after class after class I wish there was way where we
can only create one class and it could hold fish objects and dog objects and any kind
of animal that we want so then when our boss says alright I need you to actually create
one to hold um Birds we're going to make oh you know what already done because we already
built a sufficient and effective program to hold all classes of animals even the ones
that we didn't even know um that were created yet so how would we do that well this dog
list is nice but let's go ahead and delete that right now delete and okay so instead
of just creating a bunch of different classes to hold objects let's go ahead and make one
final thing and we're going to name an animal list so let's go ahead and we can delete everything
out of our main apples or whatever your main our method is and now let me get rid of that
error and now let's go ahead and create a new class by right-clicking source new class
and one go ahead and name this animal list and this is going to be um pretty much let
me run this so X is annoying me there we go and this is pretty much going to be on the
class that has our array too or any type of animal whether it be dog fish if you decide
to create a dolphin class later if you die decide to create a hermit class later it's
going to already on be able to hold that so the first thing that we're going to want to
do is create an array to hold type animals and remember these dog fish since they in
sixth end animals all of these are type animals already so let's go ahead and put private
um animal I didn't yep animal and we'll just name it list equals new animal five so what
this pretty much means is recreated array an array name the list and it can hold five
objects of type animal right there so after let's just go ahead and make our counting
variable perhaps I equals zero and now we can go ahead and build I always do that int
I equals zero and now we can go ahead and build our method and that's public void void
we'll name it add still but we have to spell void or right add and we'll put animal a so
now it takes one parameter which is an animal object and now we'll just do that test again
if what do we name it the list or actually we want to do if I is less than the length
if I is less than the list dot length so it would be five in this case but just in case
you change this then you won't have to change it each time so that's why I'm doing that
so if I is less than the list length what do you want to do let's go ahead and do exact
same thing we did last time the list the index for that which would be I so at first one
I would assign the list zero set it equal to a which is the animal object next let's
just give us a little prompt on the screen like system.out.print line and we'll just
have mo added at index space plus I so this pretty much just gives you a little message
on the screen and the last thing we want to do is increment I because if we don't it's
just going to keep adding to that same index over and over again so each time this runs
it's going to change that counter variable that allows us to move to the next element
in our array so now that we got this animal list it's going to take any animal object
we have and let's go ahead and I'll show you guys that right now so in your main class
whatever you named it I named mine apples still don't know why we can go ahead and create
an animal list object from this animal list we just created so let's go ahead and put
animal list alo you can name it whatever you want new animal list BAM just like that now
let's go go ahead and create a new dog object because remember that method takes a an animal
object as its parameter so go ahead and actually you know what I'll do I'll create a new dog
object and a new fish object so dog will just name it D because it short and sweet with
a new dog just like that going to put fish from our fish class create an object from
here F equals new fish like that now we can go ahead and call the add method in our alo
add first of all we'll add that dog and I will do a ello add and now we'll add that
fish object so now if we go ahead and run this you can see our a animal added at index
one this is the dog object an animal added or excuse me animal Atta in the zero this
is the dog object an animal added in next one that's the fish object right there so
again what we did real quick overview we created an object so we can use the methods from this
animal this class we created two objects of dog and fish which were both animal type and
we go ahead and we just called the add method so it first added that dog element to index
0 and add the element to index 1 and we can call that five times and then that's it then
our array is full if we call it after that then it's just gonna it's going to run the
program but it's not going to add it to our array because it's already full so this is
a much smarter array than we built last time because now if someone wants to create a new
class that was like hermit crab like I said before all they need to do is put extends
animal and it's automatically an animal type so that way we don't have to create a new
array for dog fish hermit crab dolphin gopher watch out beavers we just needed to create
this one class right here and it took care of all those problems so if you think this
array is pretty smart just wait to the next tutorial we're going to build this computer
program even smarter and better than this and if you didn't think that is possible then
trust me just wait to my next tutorial and one other thing and I know you heard me before
if you watched my last tutorial my 500th video is going to be amazing and it's going to benefit
no one but you guys so um a couple you people might already know what that means what I'm
hinting at but trust me it's going to be the best video for you guys ever so thank you
guys for watching don't forget to subscribe and I will see you next tutorial
what's up guys welcome to your 41st jump to tutorial and in this story as promised I'm
going to be building a four different objects and these objects are going to use this tuning
class and depending on how much information they have them they're going to use a different
constructor for each one so let's go ahead and do that right now so let's get some spacing
right there so you can see it good adjust my headphone and bam I'm good to go so in
order to make objects let's go ahead and name this tuna object and we'll set this one equal
to new tuna and this isn't going to take any arguments at all so if we go look in our tuna
class right here and you know that it has no arguments because nothing's in here we
look at which one of these constructors has no arguments well this one has our this one
has a hour in a minute that's two numbers this one has three numbers but this one right
here has no arguments right here so what it's going to do is insert three arguments and
call this constructor again so then it's going to go down here where this one takes zero
zero zero it's going to set the time to zero zero zero and then set the hour to zero in
meant to zero and seconds to zero so now that we have that let's go ahead and build some
more objects but this time let's give them different parameters or different arguments
so let's go ahead and copy this and paste paste paste and one name is tune object two
three and four and we'll give this one to an object two we'll give it one parameter
we'll give it an hour of five we'll give to an object three two parameters we'll give
that an hour five and a minute of 13 and we'll give to an object for an hour of five minute
13 and seconds of like 43 or something so for example this one right here it has two
arguments so it's going to look at this constructor right here is this constructor good for me
no this has zero arguments and I got two so I'm not using you what about can the this
constructor good for me well I got two numbers and this one only lets me insert one so I
ain't using you what about this one well this has three and I only got two so I don't think
so oh man the last one's just right I got to you got to let's use a baby so that pretty
much what happens with these objects that's what's going on right now but now that we
got these objects let's go ahead and actually use them and so let's just go ahead and print
out the time so system.out.print f we'll just do a simple print format and again this takes
two arguments the first argument is how you want to format it and I want to format it
and since we're going to be using on this right here public string to military this
returns a string so we probably want to put a percent string with a new line right there
and actually new line goes this way I believe that would have been embarrassing and so this
one returns a string so now we can use our tuna object and then put something like two
military with no arguments and what this is going to do is is going to return our tuna
object with no arguments right here so let's go ahead and your might as well do the other
ones right now while we got this nice and copied this is tuna object two three and four
now let me run this and make sure I don't have any errors and hopefully it runs right
and bam look at this now the first one was object it was just tuna object and we didn't
enter any parameters the second one we entered a five for the hour and it filled in zero
for the rest the second one we entered a five for hour and 13 per minute and it filled in
the rest of the zero and this last one tuna object for it filled in all of them for us
or actually we did ourselves so let me see if I can position this row since my screen
recorders messing up again and now let me go through one of these and show you guys
exactly what it does not dud by the heck did I say that so anyways let's just pick one
that you can understand easy and how about tune object 3 right here we made an object
that had two parameters right here five and 13 so went to tuna where it creates everything
and it went to our constructors and it says alright which one of these constructors has
two arguments this one has two arguments right here so I'm going to use this one this one
has 0 1 and 3 so I'm going to use this one that has two well what it did was plugged
in what was our numbers 5 and 13 they used the 5 for our the 13 per minute and they used
the default of 0 and it called this constructor again but this time it used its 3 new numbers
so whenever you use the 3 numbers it it uses this constructor right here and what this
constructor does is set time it caused the set time method so now it says all right I'm
going to send you to the set time method so let's scroll down and see what the set time
method tells us to do well it tells us to do three more methods right here set our was
pretty much checking those between 0 and 24 which it was it was 15 I mean 5 set minute
check to between 0 and 60 which it was and set second that was that zero plugged in for
us arm that we didn't have to do anything for that so then we said alright we're good
there so then when we finally called on the two military method what did it do well would
pretty much just said our a we want to use this method for that object right here so
what we're going to do is get that hour which was 5 get that minute which was 13 get that
second which was 0 by default and I'm just going to return them all to you and a nice
pretty looking string format right here so that is why whenever we called this to military
it just outputted the drink right down here 5:13 zero so that is of course a very complicated
way but again you need to know it on how you can use overloaded constructors to let me
scroll down here to make a bunch of different instructors based on how much information
you enter and in that example I just told you since we enter two I pick this constructor
and that is how you can pretty much use different objects with different amounts of information
in it so um this will be clear when would be when pretty much we're building more programs
and this was just an example so it might be a little fuzzy right now but you'll understand
eventually in the next couple tutorials so thank you guys for watching hopefully you
understand at least the basics of overloaded constructors and how they are useful when
creating objects with different argument links so again thank you guys for watching don't
forget subscribe to my channel and I will see you next tutorial what's up people welcome
to your 42nd java tutorial and in this tutorial I'm going to be going over a built-in method
that Java has it's called the two string method and I'm going to show you guys what it is
and why it's useful and we're also going to be building part of a bigger program on this
prep it going to be a two-part tutorial so um pay attention and it's going to be sweet
so the first thing that we're going to need to do is get three variables and they're all
going to be private and they're going to be month day and year so private int month and
just go ahead and copy and paste that take advantage of our laziness 12e control V int
day and private int year so now we have three variables right there so let's go ahead and
do something with them the first thing that we're going to want to do is build this pop
pie again you can see my pot pie class so I'm going to build the pot pie constructor
so go ahead public and just go pop pie and it's going to take three arguments it's going
to take int m per month in 2d for day and Y for year again you can name these anything
you want not important but remember to set your variables which were your private variables
like month equal to whatever you are corresponding letter I just put MD and y for obvious reasons
so that private date variable is going to be set to whatever we pass in for D and that
private Y is going to be set to whatever we passed in for Y so now we got our constructor
but we want our constructor to do one more thing we want it to print out a string so
and we just want it to print out so it can know so we can know that our constructor worked
or didn't work make sure you put an equal sign right there so let's go ahead and system.out.print
F we can just print format and I already started filling them in for us so let's go ahead and
print out a string so let's put um Quan house but the director make sure we spell it wrong
for this is and then we'll go ahead and put percent s and that means string and then go
ahead and put a new line if you feel like it if you don't feel like it you know you
don't got it and now as you can see right here our C style format it takes one string
argument so in our second argument just go ahead and write this and when we write this
what it means is this the key word this what's highlighted right now this is just a reference
to whatever object we just built whenever we call this class so it's going to say alright
then what the heck do you want me to put for a string well that's what we're going to be
going over in this tutorial right now anytime that you need a string representation of an
object Java built this thing called a two string method so let's go ahead and write
public and we're going to be building that right now public string to string and I know
month is most of the time you can call your methods anything you want but you can't you
have to say to string in this case and go ahead and it takes MC parameters and in your
body of your method go ahead and return whatever format you want so return string format and
then go ahead and % go ahead and % WTF does that mean go ahead and put this um I mean
you can write in whatever format you want but just follow on percent d % d % d that's
the common date format and that has the month day and year so the month / day / year / so
this is what this is going to do well let's go ahead and finish our tutorial and then
you can probably see what it's going to do then I'll talk you through it one more time
what we need to do is go ahead and create an object now and I'll call it pop pie and
I'll just call it like something like pots object that pop object is not important and
then whatever new pop pie and I know I should have like proper months and days and years
but I should have four or five six six seems like a good year a long time ago so now that
we go ahead and create this object we should get a nice little string at the end if I didn't
mess anything up and it says the constructor for this is four five six so now let me take
you guys through exactly what I did what I did is create an object using the parameters
four five six so let's go ahead and look what happened when I created that object it said
alright since the first thing I'm going to do is this constructor I'm going to pass the
parameters four five and six and four month was equal to four day was equal to five in
year was equal to six what the constructor also did is say alright every time I create
an object I want you to print out this string but we had in the string variable instead
of an actual string we had a reference to an object and anytime you reference an object
and it needs something in the string format it looks to this method right here what this
method says is anytime you need a string representation of this object you look to me I'm two string
I'm built in Java that's what I'm for so that's why it outputted this right here the constructor
for this is four or five six this is where that format four or five six comes from right
here and in the next tutorial if this seems kind of confusing I'll show you guys a useful
way why to string is actually used and not only you know it might be a little confusing
to understand I mean this is just an example it doesn't really anything but in the next
tutorial I show you guys I'm like one example where we would actually need it and use it
but for now hopefully you get the main idea again the key points is the probably two things
you're going to be confused upon is this I mean what's highlighted right now any time
you use that key word this it's a reference to the current object so it's going to say
all right just reference the curring up the current object so it implies that it needs
a string so since it needs a string it says all right I need a string here says oh I see
that's why you gave me this two string method so anytime I need a string I know where to
look so makes sense and if it doesn't it will in next tutorial but for now thank you guys
for watching hopefully understand the basics of a two string method and don't forget to
name it two string that's very important so again thank you guys for watching and I'll
see you next door what's up guys welcome to your 43rd java tutorial and in this tutorial
we're going to be building the rest of our program and I'm going to be teaching you guys
about something called composition now what composition means is that it means that a
class and this is the one we're going to be building today instead of just the variables
and methods it can also have references to other objects for other classes and it could
uh well I'll show you guys what it means so go ahead in our toon class again in the last
story we built this Popeye class in our tuna class just clear everything out and we're
going to have two um members for now go ahead and type private string and this is a new
one name and that's going to be used just to store my name or something and we're going
to also have a reference to another class the potpie class so go ahead and type private
potpie and type birthday since this program is just going to output my name and the birthday
but it's going to do in cool way so now that we have two members of this class right here
we have a new name variable and we have a reference to a Popeye object or object so
let's go ahead and well let's use it so the first thing we want to do is build our constructor
so this class is named tuna so we have to name our method tuna and that's constructor
what this constructor is going to take is two arguments it's going to first take that
string variable that we have and we'll just name it the name so we don't forget and we
also want a reference to our pot pie date object so let's go ahead and put pot pie and
let's go ahead and write the date or you can write the birthday or something like that
and now in the body of our constructor let's just go ahead and set name set equal to the
name and this go ahead and set birthday that we declare it up here and set it equal to
the dates so now we are in sync we are good to go but what we wanted to do now is go ahead
and make another two string method so let's go ahead and put public string to string and
just like we did in the potpie class and this method it's going to take no arguments and
for the body we want to return a string of course so string format in for our format
I really like when it fills it in for you you know I want to do the work myself and
in our format just go pull my name it is % s and that's going to fill in your name and
then let's go ahead and put my birthday is percent s and here's work is kind of tricky
name birthday so now if you guys can understand the next thing I'm about to say you'll understand
um the next thing I'm about to say sounds easy enough but you understand the key concept
of this tutorial so now whenever we call whenever we need a string representation of this what
we're going to do is this it's going to return my name is and it's going to look right here
to see what it wants to fill in for name well the name was up here and it's going to be
whatever we gave it passed it in but for birthday there isn't a really string name birthday
birthday is just a reference to the date object and again what this means since it's a reference
to an object and not a string any time it needs a string from an object it's going to
say alright anytime you need a string and I'm only object I'm not a string maybe I can
help you out well I'll go to my class and I'll go two two string method and then I can
give you the information that you need so when I put percent s right here you don't
necessarily need to pass in a string object or excuse me a string variable what you can
do is pass in an object instead and whenever it looks in that class it implies through
via the two string method that this is the string you're looking for so again you can
do two things when you're using this you can either pass in a regular string which is just
like a bunch of letters or you can pass in an object and whenever you do pass in an object
it looks into that class and it looks to the two string method of that class so that's
two different ways when you're using the print format that you can do that so now that we
got a two string method which is pretty much the string representation of an object in
the tuna class and we got a string representation of an object in the potpie class I think we're
ready to build some actual objects now put them to use so go over in your main Bucky
or I don't know whatever you named it and let's go ahead and build some more objects
one more to be exact so now that we already build one from Popeye we probably don't even
need that anymore but we'll keep it up for good looks what we want to do is build one
for tuna so tuna tuna ooh got the hiccups tuna object make sure I typed everything or
I got the hiccup in between there and new tuna and now we need to pass in two variables
again this is going to take two parameters because look our constructor takes two parameters
it takes a name and it takes an object so for the name let's go ahead and pass in Greg
and for the object let's go ahead and pass an object that we just built right here so
in order to see what parameters it takes look in your tuna constructor and you can see it
takes a string and it takes an object well our string is Greg that's my real name and
our object is pot object and we just build it up here now everyone knows my real name
oh great so now let's go ahead and just output what we did so system.out.print
line and let's go ahead and just write una object but we have to spell array and we better
change that to Bucky for good looks and now oh let's see it should be good to go so let
me run this see how many errors I got and then I'll talk you through one last time what
we did it says my name is Bucky birthday four or five six you got it so in the first line
in line number three we built a constructor in the second line we built a tuna object
well I guess we built objects and both lines but here's what that you already you guys
already know this one did from last toriel and for the tuna object when we build it in
the constructor we passed in two arguments my name in this object right here it took
the name and set it equal to name and it took the object and set it equal to birthday and
then it says re just return it in a string format well my name is Bucky easy enough my
birthday is this object and it says all right how am I going to turn an object into a string
well anytime I want to turn an object into a string I just go to that class and go to
the two string method so again I'm going to repeat at any time you want to turn an object
in the string it looks to that class in the two string method so that is your quick tutorial
on what the two string method is and also again like I said this is called composition
and that's referring to objects I'm going to show you guys that's referring to objects
in other classes as members so this was composition right here so thank you guys for watching
let's see how much time I got oh just in time so thank you guys for watching make sure to
check out my next tutorial get out of my white house and make sure subscribe my channel because
you know especially I'm lever so I'll see you later sup guys Bucky welcome to your 44th
Jeff tutorial and in this tutorial I'm going to be going over something called enumerations
or enum we learned about them before I think we might not have if we didn't you know no
big deal we're both learning today but the first thing that we're going to do well let
me tell you what enumerations are they're pretty much kind of like classes but they
you're going to use them to declare constants at least in this tutorial you are so let's
go ahead and anytime you want to do this instead of public class tuna you put public enum DNA
um and now you can use this in a specific way so like I said the first thing we're going
to want to do is declare a bunch of constants of the enum type so let's go ahead and make
some constants or these are pretty much going to be like variables that never change so
I'm going to make like three names Bucky and these are in essence objects so let me give
these a few parameters and then you'll see why later on Bucky let's give the first parameter
a description word of the object and I'm going to put nice since you know I'm doing this
story might as well say something good about myself and then let's just go ahead and put
a string about like um how old they are or something I'll make it interesting for the
other two ones now let's think of a next person let's think of a will do constants of my first
girlfriends so my first one was Kelsey and she was she was cutie it was fifth-grade she's
pretty cute and uh let's see when I went out with her I was ten years old and now put a
comma because we're not done with our entire enumeration list and then the next girl went
out with was oh it wasn't really girlfriend but uh we had thing it was Julia she was a
she was a big mistake and I was twelve years old when I went out with her so now we're
done we got three constants let's go ahead and end the statement with a semicolon now
what we're going to do is since we just made objects and again each constant is an object
and it's going to have its own set of variables in this entire enumeration so let's go ahead
and give it two variables that we're going to use later drive it final since we don't
want to change string since this we've just made since we're in quotation marks and put
DSC for description and we'll copy this entire thing copy paste and we have description right
here and what's good name for this year or something year so now we have three objects
which are actually enumerations or column or constants and they each have a set of their
own variables one for description one for year so now what can we do what do we usually
want to do when we create these well the first thing that we're going to want to do is create
an enumeration constructor so let's go ahead and do that by of course typing the name of
your class and then you probably want to play your two variables and put string description
and make sure since this is um we're going to use this make sure this is different than
this and you should know why and then just put string and let's put birthday or something
it isn't their birthday but I don't want to get confused at all I can't put yr something
because they'll just confuse me you know I'm easily confused got something to say about
didn't think so so now let's put DSC or whatever you declare it up here just put equal do description
statement and then go ahead and put birthday year evil birthday and now what do we got
anytime here's what's going to happen with Bucky is going to say all right with the object
Bucky give it two variables a description in a year for the description pass in nice
in this 22 we're going to see his birthday so set your equal to birthday so now we can
work with DSC in year good enough but now that we have a constructor we just pretty
much set these variables equal to where they need to be we have no way of getting the variables
yet so let's go ahead and create two methods that we can get the variables so public string
so this is going to return a string and put get description or get deaths if you're lazy
which I am so it works out and then in this one we want to return the des see that's just
going to return a string of the description easy enough and guess where we're going to
do in this method public string get output get year seems like a good name and this one
is just going to return year loan from of this it's a little too easy so now this is
all we need to do with enumerations again enumerations are constants that are also objects
pretty much and so we needed two variables to represent the two arguments and then once
we have those two variables we can build a constructor and then this constructor allows
us pretty much to set these variables equal to each other and return the information we
wanted so let's go ahead and well let's just go ahead and use the information right now
so what we want to do pretty much in our main class right here with our main method is build
and enhance the for loop so let's go do that right here for and the first parameter you
need is of course the class name in the variable that you want to use in your for loop like
we use um like description right here this is just variable that we just made up this
is going to be the variable that we just made up right here and that's the first like it's
pretty much what you want to call your objects when you loop through them the next argument
is you need an array that you need to loop through and tell you a little bit about this
because this might be kind of confusing anytime you make an enumeration like this Java automatically
makes it takes this constant these constants and makes a built-in array and the array is
called whatever your enumeration is called instance on a my toon it's called tuna and
when you write values like that this is a built-in an array of the constants right here
so for saying are a um I need an array BAM you got an array right there and again you
can't change this keyword values this values is called a static method in again this is
how you get a built-in RA so this is going to loop through each of the constants first
it's going to loop through Bucky and Kelsey then Julia and it's going to assign them to
people well you'll see what does so now we just need to make a print statement so let's
go ahead and put system and why do we need to make a print statement so we can actually
see what's going on system.out.print out and let's just go ahead and make our two arguments
the format is going to be percent s and let's put a tab add some spacing % s @ @ ab % s
and a newline so it's going to write our name our description our age and then it's going
to go to the next person so now we need our arguments the first thing we want is people
because that's what the object is pretty much so in the first one is just going to loop
through our name which is this Bucky in our next argument what we need is to take that
people object and use the method get' why would I name that method people dot get description
and what that is going to do is it's going to return a arm you find my screen recorder
see how much time I'm at I got minute 30 what's going to do is return a string of our description
and this last one people dot get year and what this is going to do is first find you
to get it messed it up for me people to get ear and what this is going to do is return
a string of the year so let's go ahead and make sure I have a semicolon at the end of
that and once again and make sure I have this and let's go ahead and run it so run this
okay and look at my results I got the object the description and the year object description
your object scription yer so what it did again see if I can get it was it loop through the
entire array of people and for the first argument gave us the object name description name and
the year whatever year I signed will so in the next tutorial we're going to be going
over this more but thank you for watching don't forget to subscribe and I will see you
next tutorial so people welcome to your 45th draft tutorial and in this story I'm going
to show you guys how to get a range of constants now this is going to be about eight times
easier than the last tutorial so you know just suck it up pay attention and I try to
make it interesting for you so essentially we need a range of constants and we only have
three right here it's not going to be very exciting first of all and what kind of range
can we give a three so let's add a couple more of my ex-girlfriends to list I had one
girlfriend named Nicole and let's find a description word for her she was Italian and I dated her
when I was I know probably like 13 so I'm going to put 13 and don't want that semicolon
and my next girlfriend after this I would have to say is candy this is when I moved
to my new school she was my first girlfriend and let's say she was um let's put this nice
she was different and um let's see how I was in eighth grade so what's that 14 maybe I'm
wrong but you know we don't have time to think we just have time to do and let's see after
candy go in ninth grade but you know what's the fun in that there's this girl named Erin
I had a huge crush on but I never went out of there but you know what I'm going to add
her to my list because I wish I went out with her so I'm going to put I wish because you
know she's like the one that got away and I had to be like sixteen by the time I had
a crush on her for a long time what could have been Oh anyways so now I have a longer
list of constants right here so what we can do now is go back in our main method right
here and we'll just keep this one right here this is what we did in last tutorial but let's
see let's go ahead and make a new line under this for loop I mean we might as well keep
this for a little it's not hurting anything and in case you're just joining us this for
loop pretty much printed out a list of all the constants so let's go ahead and just print
out a line so we can separate this for loop from our other for loop so system out this
post system right system out print line and let's just go ahead and print something like
um new line new line whoa whoa whoa what's going on up in here alright let's just put
and now for the range the hit caps ox embarrassing constants so now we've pretty much got this
for loop right here and we just got a line saying and now for the range of constants
so well we just want to be able to see it easier so now let's get to the meat of the
tutorial and this is an enhanced for loop to go through the range instead of the entire
thing so the first thing that we're going to need to do is at the beginning of our class
before our class we need to import something we need to import the enum set class so Java
dot utill the enum E and um se T and what this is going to do is it's going to allow
us to use a built-in method called range - well you'll see what it does later I mean especially
learn right just C so let's go ahead and build our enhanced for-loop now just like last time
it's going to take two parameters the first one is what you want to name your things I'm
just going to name those people what you want name your objects now the second one is that
method now we could just plug a regular array in here but we want to have a range of the
array we to start it let's say Kelsey and we want it to end it candy so I mean we can
start and end anywhere so whenever like I said before Java automatically gives you an
array of constants in the order that you did them in but in order to get just a specific
set out of there arranged what you need to do is this e num set period range and the
range is the method and it takes two parameters from is the first one so let's just go ahead
and write tuna which is the class Kelsey and what's going to do is look in tune class and
find this first constant Kelsey and that's going to be your starting point and for – let’s
just put tuna dot candy and let's just go ahead and show so now we have Kelsey to candy
and everything included that's what that does it gives you a whole new array so now once
we have that we can pretty much just copy what we have up here copy thank you for that
and we'll paste it out here and so now instead of an entire array which was two new values
we have a specific set in the array which goes from all the constants from Kelsey to
candy so now let me run this and see what we got that makes no I won't expand it what's
the fun of that so now we have our first array right here that has all the information Bucky
- Erin that was the entire array and then we says our line and then we goes i and now
for the range of constants and we cut out Kelsey Julia and Nicole and candy so using
our enum set class and the method in that called range we were able to provide it with
two arguments we wanted to start and where we wanted to end and this entire thing right
here it gave us a new array so instead of looping through the old array we're able to
loop through this new and improved array to get only a specific set of data so that is
your quick tutorial on my former love life and also on how to loop through a specific
range of an array using this range method right here and again don't forget to import
this class or else you're not going to be able to use it so thank you guys for watching
now you know all about enumerations constants and how to loop the room and all and whatnot
I know how exciting right so in the next tutorial I don't know where we're going to talk about
but I promise it's going to be awesome so you might want to subscribe just a little
hint so thank you for watching and I will see you next tutorial what's up guys welcome
to your 46th Jeff tutorial and in this tutorial I'm going to be going over what static variables
are and how they're used so usually whenever you have a class that has objects each of
the variables um each objects has its own set of variables like first-name and I can
make um an object called Bucky in it would have its own first name Bucky and I could
have another object named Tommy and it would have its own first name of Tommy but sometimes
you want all the objects to share a single variable for example if you're going to make
members of a club and you're going to like have the club count how many members were
in it for example if I was in the club it would be 1 if tommy was in the club it would
be if me and Tommy we're in the club it would be - if me Tommy and Megan we're in the club
it'll be 3 and we each don't need to see that we shall need an updated variable or a separate
variable for 3 since the members in the club is all the same so for example there are some
variables that you just can share and you're saying alright why don't you just write a
separate variable for each object and update it well that sticks a lot of time in space
so you'll see what static is by the end of this tutorial and it's one of the most useful
things that I actually taught you so far so listen up in this story we're going to be
building a club of course and the club is going to be of hot girls that would like to
go out with and yes they will be famous and no I will never go out with them so let's
go ahead and make a couple regular variables first for each object it gets own private
string and let's go ahead and put like first the first name and then go ahead and put private
string and last for last name and now we need to give them a variable that they can share
so go ahead and put private and put static and that static keyword means that every object
shares the same variable so when this one changes it changes the all objects in before
if you change one object's first name not only that object's first name change but when
you change a static variable they change with all objects so if I change this to four then
all objects would see four got it good so static int and we'll have members of our club
and we'll initialize the zero so without building anything we have zero members in our club
so next let's go ahead and make our constructor so public our class name is tuna and it's
going to take two arguments string first name is string last name this is how we're going
to insert members in our club and in our constructor just put first equals first name last equation
to our members variable so after we add our first member members will equal one when we
add our second member members will equal two so next let's just go ahead and system.out.print
format and our format will be something like again you can be creative with this if you
want umm constructor for make sure you spell it wrong % s % s so it will be constructed
for Bucky Roberts and then we'll have members in the club and then put like : % d and then
a new line so we're going to have like constructor for Megan Fox members in the club one constructor
for natalie portman members in the club too easy enough so now we need to put first of
course last and members and I spelled that wrong I mean be ers members members that's
a pretty catchy dude alright now let's go ahead and write some more methods that are
going to come in handy first we need something to get their first name so public actually
let me think about this for this tutorial I'm teaching you is constructors right so
we probably don't even need these yet but we sure do in the next tutorial so let's go
ahead and end in this apples and in our main function right here let's go ahead and make
some members so let's go ahead and make member one tuna because this is into class member
one and we'll set it equal to new to none in the course it takes the arguments again
you can see what arguments it takes by this constructor over here a first name and the
last name so the first name can be megan in the last name of girls I want a date club
can be Fox so now what we should have outputted on the screen is all might as well just do
it constructor for Megan Fox and members in the club one easy enough so let's go ahead
and make another one now tuna member to make another object equals new tuna and we'll have
something like Natalie and then Portman you don't know who she is definitely Google it
and let's go ahead and make one more from tuna member 3 equals new tuna and let's see
let's get us let's get Taylor she can join the club too and Swift she is babe with a
pretty voice hottie with body cutie with booty anyways so we made three different objects
each one well let me run this show you guys that works show myself that it works so we
made three different objects right here one for Megan Fox named member one one for Natalie
Portman named member two and one for Taylor named member three each of them has their
own set of variables let's look at this one natalie portman member number two for every
object they have their own set of variables right here natalie has her own first name
right here and she has her own last name of Portman right here but they all share this
members variable and they cannot like Megan Fox this object she can't see what now these
names are but she can see the members in the club has changed it to and when Taylor Swift
joins the club right here since its static it means all of these objects share the same
static variable so they can all see that the members of the club has changed to three so
that's why static variables are useful since you know sometimes you just need and again
if you didn't have this on a nutrient art right here you would have to have a function
to update all of these each time but you know why update three separate things one two three
and then when a member leaves you have to update three Sep things one two three when
you can just have one variable call it static and update at one time so that's your quick
tutorial on what static is and why it's useful it pretty much instead of having to update
it many times you just call it static and all members or objects can see it so thank
you guys for watching this tutorial if you don't understand you go in next tutorial because
we're going to be going over static some more but like I said thank you for watching don't
forget to subscribe and I'll see you next time so guys welcome to your 47th Java tutorial
and in this tutorial I'm going to be finishing up talking about static members and also I'm
going to be showing you a nice little trick that you can use when accessing static members
so go ahead and in your toon class or not the class was not your main method in it class
without your main method and there we go and let's go ahead and under this constructor
let's go ahead and build three other simple little methods we're just going to have one
to return the first name returning last name and returning member so public string and
let's just name it like get first it's not going to take any parameters and we're just
going to have it return first simple enough and now let's go ahead and make one for last
name public string get last and guess what we're going to have this one to return last
oh you guess that on a pad and now let's have one more and we'll name it um it's a public
static remember this method needs to be static static and gets Emmy and BRS I know I could
figure out how to spell members get members and then what are we going to have this one
do let's just have returned members so now we have three variables up here a private
first private last and a private members which is static this method returns first name this
method returns the last name in this method right here it's going to return the variable
members so now that we have three objects made go back in your main apples class or
whatever you call it let's go ahead and let me show you how each member has its own set
of data so let's just go ahead and for housekeeping sake put system.out.print line and let's just
go ahead and print an empty line right there there because it'll just be easier now let's
just go ahead and copy that because we're going to be using it I didn't know we can
do that did we and let's just go ahead and put what do we want to print on our first
line and it's really our first line because this one's just an empty line well what I
want to show you that is even though they all share that variable called members they
each have their own set of data so let's go ahead and put member 1 which is our object
1 which is Megan Fox and we'll put get first and then let's do this with last copy get
last and was lesson called get members so get members so here's what's going on now
um make sure it's going to print out yep so anytime you call this one it's going to have
Megan Fox and this is unique to its own object but it's going to look at get members and
see that it's static so it's going to share this variable with all the other ones so depending
on how many members are in this then that's what it's going to print so let me go ahead
and run this click OK and we have again we have our other data that up there if you're
wondering oh it went but it says Megan which is unique Fox which is unique in 3 which is
shared among all objects so now let's go ahead and change that to 2 remember to remember
to remember to now let's go ahead and run this and now you can clearly see Natalie which
is unique Portman which is unique and 3 again that 3 and that 3 is because this private
static int static means our this variable is shared between all objects so we don't
need to you know why would you make a separate variable if it like anything how do put this
why would you make a separate variable that is going to be unique that we're going to
have to update in every object if it's the same value no matter what object to this so
I mean you don't need to so we're not gonna so that's where static comes in handy and
now let me show you that trick I was talking about um since the variable static variables
don't change between objects static information is value is available even when you don't
have an object so for example say forget about all these objects right here and we actually
can't just delete them but we'll delete these right here and we can just add in this print
liner really instead of calling a separate object like an object and then your dot separator
and then your method like that what you can do with stack is just put the name of the
class and put your dot separator and then put your method get' members like that now
let me run this and you can see the three right here can you guys see that the three
right there so before we were never able to do this we had to get an object and go to
object anytime we want an access to any of these members or any of these methods right
here but with a static it's a little bit different because it's not going to change from object
to object since no matter how many objects you have the variable is the same so that's
why I Java lets you instead of having to have an object which you can do by the way what
we just did you can also just put the class name and put the static method ahead of that
so again anytime you have a static method that use static variables um you don't have
to have a separate object you can just put the class name and then you can just put the
method and that's how like in arm stuff like math like math functions and math constants
on that's how they do that so that's just a nice little tidbit of information so don't
forget that um static variables are shared among all objects and that you don't have
to access static methods with a specific object you can just do it with a whole class since
it's the same so thank you guys for watching I think I confuse the enough for today but
uh like I said thank you for watching don't forget to subscribe and I will see you next
tutorial what's up YouTube welcome to your 48th draft tutorial and in this tutorial we're
going to be going over instance variables with the final like when you have like public
final or private final int that's where we're going to be going over so unlike static I'm
going to be showing you guys what final means so let's go ahead and in your tuna class right
here and this is the one that's not your main apples isn't has my main method so go into
another one I name my tuna you can name where yours whatever you want as long as it's a
fish that's key it has to be fish or this program won't work I'm just kidding but you
know just a little joke and let's go ahead and the first thing we're going to want to
do is make a variable that's going to be private as well but you got to spell it right that's
key private there we go I know I get eventually into some and what this is going to do is
hold the sum of our numbers that we're about to add up now let's like begin the tutorial
what we want to do now is make variable with the type final so private final and it's going
to be an integer variable and I hit button there embarrassing and let's just name it
number or something and go ahead and put it in caps because it's going to be constant
so we know that the constants aren't caps cook cook there you go cut cut that's how
you can remember it so now the key or what final is is whenever you write final in front
of a variable or constant it means that you can't modify this no matter what so we just
declared it whenever we set it equal to something whatever we set it equal to we can only set
it equal to it once so if we put number equals two then that number is going to be two for
the rest of your life you can't change in Tamara watt but another thing is we declared
it right there but we didn't initialize it which means we didn't set it equal to anything
so we're going to have to do that in our constructor so again you can either do right here or you're
in your constructor so let's go ahead and make a constructor put public tuna and just
put int X it's going to be a temporary variable and then put number equal to X so whatever
we pass in for our argument that's what number is going to be equal to and again don't mess
it up because you can't change this you can't later in your program right all right we're
set it equal to two here but we're going to set equal to fifteen later on or eighteen
or 32 you can't you can only set it equal to something here and it stays like that forever
now let's go ahead and build another method so let's go ahead and write public void make
it called add because what's it going to do it's going to take that sum that we have up
here and it's going to be zero default and let's just add the number to it so again the
sum variable is going to change but this number is always going to be like 10 or whatever
we say this and now let's just build one more method so we can actually see what's going
on public string to string so again you learn a to string method did last time whenever
you need your object in a string format it returns it so let's go ahead and return string
format and idly started filling out for us the format just can be put some like sum is
equal to percent D and then put like a newline for good looks and then for your arguments
just put some so this is going to change because it's the number that stays the same and not
the sum and how do we know that because number variable is final and sum is just private
sum can change so now that we got that look it over and make sure it's the exact same
as mine we can go to our our main class which is apples in this case so go ahead and let's
create an object so tuna tuna object equals new tuna and let's go ahead and put something
like 10 so now well this is going to do since we passed in 10 to our constructor it's going
to take 10 for X put 10 for X right here and set number equal to 10 so now for this object
number equals 10 no matter what we can't change that so anytime we use number like at number
2 some we add 10 to some simple enough so now let's go ahead in well let's go ahead
and make a for loop using this object so let's just put 4 and let's make a simple loop put
like int I equals 0 if I is less than 5 in I plus plus so it loops 5 times 1 through
3 loops 5 times it really doesn't matter what you have inside your for loop this is important
just how to make make something real quick now we can go sit ahead and say what do we
want to do 5 times well let's go ahead and add on let's call this a method 5 times so
the first time it's going to be 10 when it adds it the second not time it's going to
add another 10 to it so it's going to be 20 then it's going to be 30 40 50 simple enough
so let's go ahead and add tuna object and how you use it is add right there so now we
did our method um five times but we can't really see anything because this method right
here it doesn't return anything so we need to return it right here so let's go go ahead
and put system out let's go ahead and let's do a print format and let's just go ahead
for the format we'll put something like let's keep a simple % s and for the arguments let's
just go ahead and put tuna object so let me go ahead and run through this and show you
guys the results sum equals 10 some equals 20 30 40 50 so now let me talk you guys one
last time why this happened make sure hopefully I'm in my screen recorder and good enough
so what we did is we made two variables well let's just go ahead and do this way we made
an object called new tuna or tuna object was the name of our object and we passed the parameters
10 for the constructor so it took that 10 and set number equal to 10 all right simple
enough what's next well we loop through this five times so what did we do five times we
did this add method five times so first four add it took that number ten and added some
so now sum was equal to ten then it just spit out 10 and then the next time on round number
two it took that 10 and added it to 10 then we got 20 so now it spit it out 20 then it
took 10 and added to 20 which was 30 then added it to 30 which is 40 then added it to
40 which is 50 so now that that's all done so that is pretty much your really detailed
tutorial on a really simple explanation that whenever you make a final variable you cannot
change it so for example if we say down here um tuna object number equals 15 look at this
right here we get X right here the filled tuna number is not visible that means that
it can't change pretty much so that's pretty much all in all I could have skipped this
whole tutorial and set our when you're right final instead of a variable it means you can't
change it but I didn't I decided to give you guys the whole spiel instead so thank you
guys for watching and hey if anyone wants to do fantasy football with me um leave a
comment and let me know because I wanted to fantasy football with someone so thank you
guys for watching and don't forget to subscribe and I'll see you next time so guys welcome
to your 49th Java tutorial and in this is order I'm going to be talking about inheritance
now what inheritance is is pretty much inheriting stuff from another class so why is this useful
well say we have two classes right here tuna and potpie and in the tuna class we have a
method like public void we'll just name it eat and let's say armed in this method all
it does is go system.out.print line and then it just print something like I am the eat
method and safe we had a bunch of different little food classes and every single food
class had this method so tuna had it and potpie had it and say we had a bunch of other ones
like chicken liver ham they all had the same exact eat method so then your boss comes in
and tells you all right you know when you wrote the eat method say I'm the eat method
well we actually want you to say I am the e methods with s so now you got to go in every
single different clef class and change that one method and add an S on the end and you're
saying alright I wish instead of having all these copies that I would have to maintain
if I could just have one single copy so that's what exactly inheritance is instead of having
all these copies and different classes that you have to maintain you can only have one
single copy that you have to maintain in each of these classes can inherit that so let's
go ahead and copy this and since tuna and Popeye or both foods I made this new food
class right here and what this food class is going to do is it's going to store all
the things that tuna and Popeye have in common and right now they have this eat method in
common so we can go ahead and get rid of this method right here and get rid of this method
in here and instead of having all these separate methods in all these classes we only have
to maintain this one method in the food class so how exactly does this work and how can
we only have one method to kind of take care of well this is what inheritance is instead
of having a bunch of different methods what this tuna class and Popeye class can do is
it inherits everything the variables and methods from this main food class and whenever we
do this the kind of methods that inherit the stuff those are called the subclasses and
this stuff that they or the class that they inherit from is called the superclass so again
these two tuna and Popeye classes are going to inherit all the stuff that's in the food
class and the food class is called the superclass in the tuna and Popeye class are called the
sub classes so how exactly do we inherit everything well instead of saying inherit from what Java
said is it gives it a new keyword name extends ext BND s and whenever you write extends this
means inherit from so say public class Popeye extends food this means all right in this
class potpie we're going to in hair everything from food so we're going to inherit any variable
that would be in hair in all the methods so let's go ahead and do that same thing from
tuna so public class tuna this class ext inds it extends food so now anything that's in
food is inside Popeye and tuna even though we can't see it that's what we mean so let's
go ahead and let me show you guys that right now so say we go or whatever your main method
is right here let's go ahead and make a object so I'm going to make my tuna tuna object and
let's just go up ahead and put new tuna let's just go ahead and make the same thing with
top pie so pop pie PAH object would be JCT and I'll figure it out eventually new popeye
so now we have a toon object and a potpie object sure I didn't mess anything up so now
even though we have no methods in here they inherited all the methods from food so we
would use those methods just like we would anyone else so this method is called eat don't
remember and it's in pot pie and tuna even though you can't see it so let's go ahead
and put tuna object eat and pop pie or what's called pot object eat and see what happens
and we're getting a error right here oh we forgot to equal sign there we go and now when
you run this we see I am the eat methods and this one came from tuna and this one came
from PAH object so that's how you can have you can hare inherit all the methods from
another class using the extends keyword but what if you're saying alright you know I have
a bunch of different foods and I know we only have two foods right now this is just them
trait but I'm saying alright I have a bunch of different foods like tuna potpie chicken
liver but I have this one class called tuna and I want to inherit all the stuff from food
but I want to change this one method right here well what you can do is override certain
methods and in order to override certain methods you just put the method into the class the
subclass and you recreate it so then if you say all right I am the new method of tuna
so make sure you spell a couple things wrong there and now whenever you override something
you inherit everything from the food class but the methods that you recreate they're
going to override or overpower the methods that you inherited so now if we go ahead and
run this we see this let me get it up for you there we go I am the new method of tuna
and this is the method that we overrode and again the it's the easiest thing in the world
whenever you want to override something all you have to do is pretty much just create
it again and then you don't have to worry about you know well it's pretty much as simple
as that I can't explain it any better but that's how you override something in PI objects
we didn't override that method so that's why I got the default food method and then the
last thing or the last couple things I want to talk about real quick if say tuna extends
pot pie and pot pie extends food then tuna inherits all the stuff from pot pie and food
it's like a little hierarchy so let's go ahead and delete this and now when tuna extends
Popeye and inherits all the stuff from pot pie and food and the last thing I want to
take note of is that only let's go ahead and get rid of one of these objects let's go ahead
and get rid of the pot object only the public methods can be inherited let me see how much
time I got left two minutes only public methods can be inherited so if you have a private
method in the superclass again the superclass is the one that the subclass inherits from
and we want to change that to food so right now tuna is inheriting all the stuff from
food but food has this method in here that's private what private means is all right you
can't inherit this from me in order to inherit it it needs to be public like we did before
but when we put private like this and try to run this right here we get a little error
right here and it says the method eat is not visible so whenever we try to run it I'll
show you the syntax error down here and then pretty much this is saying all right this
isn't visible because food has a private thing that you tried to inherit in news and you
can't it's private it's not public so that's your real quick tutorial on inheritance and
pretty much anytime you want to inherit crap from another class just write extends and
extends is this a real fancy word for tuna inherits from food or tuna inherits all the
methods and variables from food and again well that's it for this tutorial I already
filled your brain with enough so that's your real quick tutorial on inheritance I hope
I made it simple I hope you guys can understand so thank you guys for watching and I will
see you next tutorial what is going on people welcome to your 50 Jeff tutorial and I know
it's been a while since I started making Jeff tutorials but hey we're going to get back
on track and start learning some more Java so actually I had planned out that we're going
to learn polymorphism but I think I'm just going to skip right to GUI is because it's
a lot funner and it's better if we know how to make an interface before we start getting
into some other stuff plus one we have an interface and stuff you can see what everything
does and you're not just typing random code that you're like all right what is this the
matrix because you know it's time we started building an application that the user can
interact with in C so if you don't know what a GUI is or some people call it a GUI it's
a graphical user interface and just like this right here it has title bar at the top menu
dropdowns has a whole different range of buttons and that's what we're going to be making instead
of this right here where it's like matrix style because I mean come on it's 2009 if
you're watching this story on 2009 that is so let's go ahead and get started now the
first thing you need to know is that all gooeys are built from GUI or GUI components that
means there has to be a component for a menu a dialog box scrollbars you have to add each
one of those individually so how do you just go ahead and get these components well actually
Java has built in a class that stores all the components you need you just need to tell
it where to put it and what to do with it and they are created using different methods
in the methods used to create this in the class it's called the J option pane class
now like I said this class is built in the Java so anytime you want to make a button
all you need to do is call it from the class and there are different ways to do this but
this is the easiest way so in order to get all those buttons and toolbars in everything
we first need to import the entire class so let's go ahead and before your evening class
right here just go ahead and put import Java X dot swing dot J option pain and make sure
you type the J in the o capital option paint and just like that and now we're getting a
little error that says on the import Java Japs from pains never used so let's go ahead
and fix that by using some stuff right now so now we imported the class which means we
have access to all the crap in it so let's say we're using something that career the
first thing we're going to want to do is set to variables by the way I'm going to build
program where you enter a number in one box and then click OK you enter a number in another
click ok in the end box the third one shows your answer you're seeing a little bit so
we need to set this equal to string variable and set it equal to first number and I'm just
going to write m then R excuse me FN for first number now what you need to do is called the
class which is J option pane use your period or dot separator in the method for an input
dialog is show input if you can't tell by these hundred pop-up things that pop-up dialog
and it's going to take one parameter right here and this is the message now what this
is is the message or the prompt which you want to tell the user to do so we just want
to put in a string enter first number so now whatever they enter into that first number
is going to be stored in variable FN so now we need a second number so let's go ahead
and copy this press ENTER paste it and let's just set string second number and we'll just
put ekit enter second number right there make sure I got my whatever it's called semicolon
and let's go ahead and move this over here because just because I feel like it now here's
a little thing that you need to know every time they enter something in here show input
dialog method only is able to get a string so we can't add strings strings are pretty
much letters in numbers that it doesn't see as a number so we need to tell Java all right
instead of viewing this as a string we want you to view it as an integer so you can do
some math with it later on so what we need to do is convert these variables to integers
so this has nothing to do with them J option pain this is just Java right here so Java
num or excuse me int num1 equals its Jerk dot parse int and what this does is convert
whatever I type into here in stores is an integer in num1 so I want to put FN and what
this does is take the string which say it's 5 and converts it into the number 5 so now
let's go ahead and do this for num2 num2 we want to take the second number we typed in
and convert it to a variable called number 2 so now the last thing we need to do is just
show the sum so before I even do that let's just add the sum in sum equals o to put num1
plus num2 no spaces and now we got the final answer in the sum variable right here let's
tighten that up a little bit that's what she said and now we can go ahead and display this
now the box that you used to display is something called show message dialog so let's put J
option pain the show message dialog and this is a little different than show input dialog
inputs for inputting messages for displaying now this method takes 4 parameters the first
one is it's a parameter where to position it and if you put null positions at right
in the middle of the screen the second one is what you want to say like for your prompt
but isn't prompt it's just a message so let's put the answer is and let's just put add some
so it's going to be like the answer is 5 or something like that now the third parameter
is what we want to appear on your title bar so let's put like the title or something something
easy like that and the fourth parameter you need is the message that's going to pop up
now I'll go over this later if I got time but for right now just put J option was a
pain then put plain underscore message like that and you don't need to put this last thing
in quotation marks so again well that's pretty much it let's just go ahead and run this program
tire waiting around so let's go ahead click OK run it enter your first number three okay
enter your second number five okay the answer is eight and now I can talk you through this
one more time so now once we go ahead and run this what we're getting is this very first
thing the show input dialog and it says enter your first number and we're going to go ahead
and enter four press ok now once we press ok it stores that four and F n variable right
there and also it allows us to move on to this other show input dialog it doesn't move
on until you complete the show input dialog so under your second number six okay so now
what it did right here is it took that four and convert it to number and took that six
and convert it to a number so that you can add the two sums right here and lastly it
took that show message dialog which if I can get it back right here and it says that know
what main position in the center of our screen the answer is some right here and again that
third parameter was the title which appeared on the title bar and that last one you have
the option of putting icons on your um GUI or user interface that we built but we're
not going to put like ons on here now mostly could because they're stupid um if you want
to have something like an error you can put X on there a warning you put it like a I don't
know they're stupid but we'll probably put them in next time because I'm stupid too so
but that's a real quick tutorial on how to use J option pain to make a simple graphical
user interface aka GUI so in the next tutorial we're going to be building our gooeys and
atom some more function ask more cool buttons and stuff like that so thank you guys for
watching don't forget to subscribe and I will see you next tutorial
what's going on guys welcome to your sixty first job tutorial and this tutorial is probably
going to be the last tutorial for now on polymorphism oh so I flew so in the last couple tutorials
I told you guys about the concepts of polymorphism and I showed you guys how to do some pretty
cool stuff of them but we never really put it into a program and we never really got
to see why this was really useful so in this program I'm going to show you pretty much
the core heart of polymorphism and what you can use it for so let's go ahead and in this
program I'm going to make a method in each one in these classes that's just going to
say I don't know I'm going to have like a line for fish that says make a noise I'm going
to have a line for dog that says make a noise and by line I mean method and also one for
animal as well so let's go ahead and make the same method and animal fish and not dog
and I probably should have made an animal that makes a noise besides fish but you know
fish don't really make noise but that's not important you'll see what this tutorial is
about so let's go ahead and make a method in here for an mo and we'll put public void
noise and since animals don't really make a noise let's just have something on the screen
because this method is actually never going to be called but I just want to make a method
to show you guys it's not going to be called so system.out.print line gotta learn to type
print line and we'll say like animals don't make noise and this is pretty much a point
of inheritance and why we have this um class right here because fish and dog are each since
they extend this they can both override this method right here so let's go ahead and copy
this in the fish one let's go ahead and put the same method in here again we don't change
your name of it it's just the noise one but it's coming from the fish class now and what
kind of noise does a fish make put like blurp slurp that's what noise I'll make if I was
a fish so you know it's good enough dog one should be a little easier we'll put a method
in dog called boys as well and will make this one go grow not rust rough right like that
so now we have this method called noise and animal and we also have a method called noise
and fish even though fish don't really make noise but this fish says glib slurp and we
have one in dog called noise called a ruff so we have this main method and animal and
it gets overwritten when you call it in dog and fish so now um that we have three methods
named the same thing let me show you guys the core heart of polymorphism one of the
main aspects of polymorphism is that it you can put objects in wood an array like we did
last time but we didn't really do anything with it we just added them into the array
the cool thing is that you can put objects in an array and you can loop through that
array and call the same method for the objects the thing is you can automatically call a
noise for dog and when you call noise for dog ruff prints out and when you call noise
for fish glare slurp comes out and if we had a burden you call noise for bird it'll be
chirp chirp if we had a class called cow and the math Conroy's oh do Moo so it's pretty
cool in the sense that you can loop through an array of objects and call the same method
arm the same method name but each method since it's in a different class it does something
a little different and this is my virus protection right here so I will renew that later get
on my way get get so let me go ahead and show you guys how to do this go ahead and make
an animal type object which is actually going to be an array so go ahead and write animal
and one name in the list and we'll set equal to new animal too and again the concept if
we have an array of type animal then not only can we put animal objects in it but also fish
and dogs and say extend animal and that's one of the concepts I tell you guys before
now we can actually put it to use into something useful so now we go ahead I mean we need this
is going to be a rate of objects of two objects so actually this is three but let's go ahead
and make a dog object and the fish object to use it I can go ahead and change it well
it's good so dog D equals new dog with empty parameter since then I have a constructor
or nothing so fish F equals new fish no parameters for that either so now we have an empty array
in two objects D for dog and F for fish so let's go ahead and use those right now we'll
just uh do this the easy way the visual way so at index zero which is the first element
let's just go ahead and set that equal to D for dog and at the list and then index 1
which is the second element set the equal to F for fish now let me get this out way
this is annoying so now we have an array and we have two elements in it and again if you
watch the last tutorial you can see that you can populate this array using add method but
this is just a really easy method method for demonstration the next thing I'm going to
do is write one of these enhance for loops and in case you forgot want to enhance for
loop was it pretty much is a special loop to loop through an array so first you put
animal and then you give a new variable what's going to use X it's kind of like the counting
variable in for loop and then you write the array name so now it's going to loop through
the array the list and every time it loops it's going to treat the index as X so we can
do things like this X which is the object now since we put objects in the array so this
would be substituted for D the first loop and F the second loop and then you should
be X dot noise and what this is going to do in essence it's going to loop through the
entire array and it automatically knows the length of it so you don't got to worry about
counting variables or anything the first thing it's going to do is say alright what's the
first element in the right it's a dog so I'm going to put dog dot noise and I should have
ruff print allen screen alright done with that now next element in the array it's a
fish element so I'm going to but fish noise and the fish noise is glib slurp and then
I'm done and that's it so let me go ahead and run this to all those then you see ruff
glare slurp so that like I said is pretty much the core essence of polymorphism in the
sense that you can automatically create an array of objects and loop through that array
and call each method with the same name in that object and you don't have to create a
bunch of objects you don't have to be like D dog F fish this is a real quick way if you
have like 100 or excuse me and 100 what I keep saying in hundred a hundred objects so
you don't have to call a hundred lines of code you can just make this and it loops through
so in the next couple of tutorials I don't feel like doing any more polymorphism because
kind of annoying ant but I needed to teach you guys with like implements and abstract
classes were so that's why I did this because in the next tutorials we're going to be getting
the gooeys more and some awesome visual stuff so thank you guys for watching this tutorial
don't forget to subscribe the next tutorials are going to be awesome so again thank you
guys and I'll see you next time what is going on people welcome to your 60 second java tutorial
and in this tutorial we're going to be getting back to what everyone loves most and that
is gooeys so as you can see I already created a new class called GUI and I already went
ahead and imported everything and you should all know what this stuff means for my last
tutorials I just didn't feel like um having to type it all again you guys didn't want
to watch me type it anyway so I went ahead and imported everything we need to import
we'll figure out what these do later so if you watch my other tutorials you're already
know what to do but anyways go ahead and make a new class called GUI and have it extends
jframe and I'll leave that up if you guys want to copy I guess and what extends mean
is it pretty much inherits everything from jframe and we're going to be making buttons
in this tutorial in jframe has some built in stuff for us to create buttons and the
first thing we're going to need to do is create two variables so go ahead and put private
j button and we'll create two buttons first one will be a regular one and then we'll go
ahead and create another one J button and we'll just make this one a custom one one
of them is just going to be a regular button in other one if we spoke private right it's
going to be a button with our own images on it and I'll show you where to put the images
and how to do all that good stuff so I'm going to go ahead and minimize this so hopefully
you guys have that copy down because it well just because it's annoying me so the first
thing we're going to need to do is build a constructor so go ahead and put public and
just go ahead and put GUI or whatever you name your class right here and we need to
build a constructor because whenever we create an object from this class we want this window
to automatically pop up we don't want to have to call a method for it to pop up so go ahead
and add no parameters and in your constructor body this is where we're going to be doing
the meat of the programming the first thing we're going to want to do is add a title now
this super right here this super keyword is how you call methods from the superclass so
since we're extending jframe this is the jframe constructor so go ahead in the constructor
for the jframe is pretty much the title so go ahead and write the title so um you know
you guys didn't need to know that all you guys need to know is in order to add a title
to your window just put super the title I just want to tell you guys um if you don't
know what the super does it allows you access to your super class so you know a little bit
of information for everyone so now that we have a title on our window we can go ahead
and make the layout for the buttons we do this set layout and you guys should know this
still from the last tutorial when you're working on gooeys and we'll just go ahead and put
new flow layout just like that and now this just gives us a default layout so we don't
have to code it ourselves and now let's go ahead and add our first button to our screen
so we'll take that variable we made I am named my reg you can name as your wood you can name
yours whatever it was new J button in this J button method is a method to add a button
and it takes one parameter for here and it's pretty much the text that's going to be on
the button I'm just going to name it a reg button so now if we go ahead and add this
to the screen we now have a blank screen that has a title of the title on the title bar
and we have a button that says reg button on it well that's pretty good pretty neat
but we can do better let's go ahead and make a button using our own custom picture in it
and when you roll over it you get another picture so the first thing I want to show
you guys is that I have two pictures already um right here and how did I get these pictures
here so I can use first of all I want to tell you guys when you create your buttons make
them like 40 by 40 and make sure you save them as ping I tried to save them as gif or
JPEG and ping is the only one that like gave me so it could work all the time it Java supports
like a gif and JPEG and all the regular image file but for some reason only pin was working
on my computer maybe it's vista thing maybe it's not but once you create these images
here's where you put them go ahead to my computer and you need to find the files you're working
on these class files mind the default workspace are insi users Greg and then is in a workspace
and go ahead and go to let's see mine is in Bucky source and then this is where all your
class files are like apples that GUI one we've just created put them in the same directory
is that so then you don't have to write a bunch of image paths you can just write the
file name in you're good go so your workspace might be a little different and if you don't
know where your workspace is as soon as you open eclipse it tells you where your workspace
is so just copy that find it and that is where you want to put your ping files right there
so now that you got your ping files in your same directory is your class we can go ahead
and uh well make some fancy buttons so let's go ahead and how you do that is in your icon
class and you have to name it icon go ahead and give it an object name such as B and what
we need to do is put new image icon and this is built-in so you can't change any of this
let's go ahead and get class which is this class and then put get resource and this resource
is going to be the file and the resource is going to be well I named mine be ping and
what this does is pretty much take that image and save it an object named B so now we actually
have two images or at least I do have one named B and one named X so let's go ahead
and create a new one by pasting that in icon X is equal to new image X dot ping if you're
wondering all right what's this beat-up ping an x-up ping they're actually just two images
I made in Photoshop earlier so you guys won't have them on your computer but I just want
needed a little something to test on so that's what I did so now that I have an image named
B and an image named X I can go ahead and change those in the buttons so what I'm going
to do is take that custom button remember I named a custom out here and do custom and
for the value I'm going to set equal to a set equal to nu J button and for my parameters
it first takes the name what's on the bun and I'm going to name a custom and for the
second parameter this is optional parameter you can have the image you want to appear
on the button as well so I'm going to have B and this is pretty much just a little graphic
it's a 40 by 40 graphic and it's like well I guess you guys are going to see and another
cool thing that we can do with it if we said all right on that on the button we just created
there's an image okay I got that but what are you going to do with this X image right
here well that X image is going to be the rollover for it so whenever we do whenever
we want to create a rollover what we do is type the name of that button right there and
you need to put set roll over icon right there actually set rollover I selected around one
embarrassing and the argument for this is of course the image you want to roll over
and now let's just add the custom button to the screen so now we got two buttons on the
screen right here we have a regular button and we have a custom button so in the next
tutorial we're going to be creating a class too so when we click the button it's going
to handle the event and we're also going to be um you know that's it we're going to be
seeing these buttons in action so and I'll also go over everything I did in case I kind
of confuse the by gone too fast but for now my tutorials getting too long so thank you
guys for watching make sure you check out the next tutorial where we see these buttons
in action so again thank you don't forget to subscribe and see you next time what's
up guys welcome to your 63rd Java tutorial and in the 60 second Java tutorial we made
two buns and put them on the screen one right here and one right here so now in this story
where we're going to be doing is building um some kind of handling events so when we
click the button something actually happens so we're actually going to be building a whole
new class called well we'll name it something like handler class but for now we're just
going to be creating the object for it so let's go ahead and put handler class and we're
probably going to be getting an error right there and we'll name a handler object equals
new handler class and why we getting error because we didn't create a class named Henry
alert class yet but trust me we will so now let's go ahead and add some action listeners
so Reg which is the name of the first one we'll just go ahead and add action listener
right there and for the parameter it takes the object handler so that's why we needed
to create a class before or excuse me an object before we created that class Handler and let's
go ahead and just add the same thing to our other bun which is named custom there we go
right there and what this pretty much does add action listener is pretty much the beginning
of adding a pretty much a handler for our buttons so anyways we are let's see I think
that's everything we needed to put in the constructor so this whole thing right here
is your constructor right here so we want to build another class called handler class
but let's build it outside of the constructor so and your parentheses and outside of your
constructor now let's add some space right there let's go ahead and type private class
handler class and we need to name it handler class because we name this Hanover Clause
right here if you name something different up here create it a create the same done here
and yes we are putting a class inside this main class we are allowed to put classes inside
classes so you know just letting you know we can do that why because we just did and
we'll have this implements there we go did I spell there implements action listener and
when a class implements another class what does that mean when a class implements another
class like we're implementing did I spell that right I was wondering I was getting line
when we have a class in the implements action listener what does implement means I had a
couple of questions asked me on the other tutorial what implement means in a nutshell
is we're saying to action listener all right we want to use your class in your method an
action listener says all right if you implement me I'll let you use my methods but you have
to over write every single method in my class and we're saying alright we're going to inherit
air all your classes from you but we promise we're going to overwrite it and it's a little
more complex than that but in a nutshell that's basically what it means so now any method
that's in the actionlistener class we're going to have to overwrite it and create one or
so lucky for us there's only one method in the actionlistener class and this is called
action performed so lucky for us we only have to create one method and it's going to overwrite
this action performed method so let's go ahead and do that right now we'll set it equal to
public void action performed just like this and as a parameter it takes action event event
so that's all we have to do and in the method body what we write in here is what we want
to happen when one of these buttons are clicked so since we added the functionality of add
action listener class to handler it pretty much whenever we click this button this happens
and that's where we're going to create right here to functionality for it so whenever we
create regular this is going to happen whenever we create custom this is going to happen so
enough talking let's just go ahead and do it what we want to happen is we want J option
pane to pop up which is pretty much a separate window and we want to show message dialog
and I didn't feel like typing it because I was lazy so I just did that and this takes
a couple of parameters itself and that is null and that's just positioning and let's
just go ahead put string format put some text on the screen first one will be nothing fancy
just % s and the second argument will pretty much be event and you'll see what this does
later on myself press for you get action command just like that and now I just got to make
sure I don't have any errors and now with that we don't we can go ahead and we can call
this main pretty much class so go ahead to where your main method is for the whole entire
program because that's where we're going to be doing now as you can see you need to import
Java X swing jframe and this pretty much lets you use windows i already imported it because
i didn't want to have to forget so now we can go ahead and make a GUI object so GUI
or whatever you name this class I'm going to name my GUI object geo for GUI object equals
new GUI doesn't take any parameters in the constructor and now just put go set default
close operation and you definitely need to have this or else your program is going to
be able to close and then you need put in here J frame dot exit on close so make sure
you have that it's pretty important if not that I spell something wrong is oh here we
go is undefined for the type of GUI set default close per patient there we go spelling its
key kids it's key definitely need learning that skill in school unlike me so the next
thing I want to do is pretty much set the size for our window do we do this with a set
size billon method and you can set it to anything you want I'm going to set mine by 300 by 200
and the last thing is you need to make sure it's a visible so go dot set visible and it's
boolean value just set equal to true if you set it equal to false and won't show up and
we do want to show up so let's go ahead and run this right here and here's what we see
we see a regular button right here that when we click it it says reg button and we see
a custom button that when we hover over it the roll over changes and when you click it
it says custom okay regular bun custom wow this is an awesome program we should definitely
market this try to sell it to Microsoft or someone because they could use this I mean
look this flawless no bugs or anything that's amazing so now let me see time I left have
left on my tutorial two minutes I'll give you guys a quick little walkthrough of how
this works again I'll just give you guys the meat of the tutorial we created two variables
and those we're going to be the store or buttons next thing in our constructor we added a title
to our window we gave our window a layout which is pretty much default layout we added
a regular button as you can see here and then we made two images and we store them in objects
B and X and then we pretty much in added another button a custom button with the image B by
default and when we roll it over it we got the image X and we added it to the screen
lastly we created a new class which pretty much was the to handle the events when we
clicked our button we set our right for this regular bun we want to have an event when
we click it and for this custom button we wanted to have an event when you cooked it
so they just weren't pretty much playing on buttons just sitting there do nothing and
then since we said our right we want some functionality we actually had to make a class
to add some functionality to it in this event get action command this pretty much just gave
you arm the name of the button so that's pretty much all that did um real basic stuff here
and the next tutorial we're going to be going over some other stuff for gooeys as well we're
probably going to be focusing on seong-gu ease and event handling for a little bit since
you know it's pretty much one of the coolest things in job what we can do so thank you
guys for watching this tutorial don't forget to subscribe and I'll see you next tutorial
what's up guys welcome to your 64th java tutorial and in this tutorial i will be teaching you
guys how to add checkboxes to your GUI and yes it's going to be awesome so pay attention
so I already got my imports right there if you want to copy those go ahead but I'm going
to go ahead and minimize that right now so if we need a copy and pause your screen but
if not see you later they're annoying me so now I already made this class called GUI and
it extends jframe like before but I got rid of everything in the class so now I can go
ahead and start well pretty much making everything again so in this program we're going to build
three variables one's going to be a textbox with a sentence in it and we're going to have
two check boxes underneath one that says bold and one that says italicize and whenever we
could click bold it's going to bold the text and whenever we click italics it's going to
italicize the text so we need three private variables so name private J text field and
this is going to hold the text and we're going to name this TF for text field and we need
two more private J check box make sure ispotter eight don't want to spelt like the Czech Republic
check box and we'll just name this like bold box because this one's going to be the one
in bold and private and it's going to be another check box why did I just not copy and paste
that and this is going to be like a talaq box or something like that so now we have
three variables that are going to hold the text field the bold box in the check box once
the text field to our check boxes so now let's go ahead and well I guess we can go ahead
and build our constructor now so public GUI and doesn't take any parameters of course
and let's just go ahead and first thing add the title of course super just put like the
title and the next thing we need to layout set layout and or up just a future heads-up
Ruby actually going over how to make custom layout in and a couple tutorials but for now
we'll just go with the default because you know works pretty good so new flow layout
will give us the default layout and now we can go ahead and start adding stuff to the
screen so that text field we want to set equal to nu J text field and as our parameters the
first one is just right this is a sentence or something like that with the size of 20
and that pretty much gives us a text field in the TF variable and now let's just go ahead
and we can do stuff to that text field now the first thing that we want to do is pretty
much set font to it so let's just go ahead and set font like just make it actually do
this to put new font there we go and as your parameters put serif and don't put anything
else because Java only accepts um a couple weird one so font plain and plain means like
not not bold and not italicize or anything in the size of 14 so that's how you set the
font you set the font with new font method and it takes serif and make sure it's safe
because I know for a fact that serif no matter what version of Java or what platform you're
running on it's going to show up so make sure you do it serif just like that and now that
we got a text box with it says this is a sentence we pretty much have all the text now all we
have to do is add it to the screen so add TF I mean it would be a waste if we created
all that and we didn't even add the screen so now we got that text field right there
now we can go ahead and make our two little check boxes to add the screen underneath it
so let's go ahead and make bold box or whatever you name your variable and let's go ahead
and set this equal to nu J check box just like that and the parameters it takes any
time you make a check box there's going to be a little thing to the right-hand side of
it or else you're not going to know what the heck it does so it's like a little prompt
to tell you what this tech box does or a check box does I'm just going to say bold because
I mean you don't need any more instructions than that and let's go ahead and do the skin
with the italic box so copy this because we're lazy and paste it and italic box I think that's
what I named it yep and put italics or italicize or something like that and now let's go ahead
and add both of these this screen add full box to the screen and add we scroll down a
little bit there we go right there add italic box to the screen there we go so now we have
pretty much three things on the screen a text field and two check boxes underneath it the
thing is they're just chillin on the screen right now not really doing anything so what
we need to do is add some functionality to it so just like before we're going to create
a new class called handler class and we're going to name the object Handler and set it
equal to new handler class just like that and even though we didn't create that class
yet we're going to use it later on so now let's go ahead and add an item listener for
it so bold I'll go to in bold box dot add item whoa whoa whoa I did not mean it like
that add item listener and what are we going to add it takes an object as its parameter
so let's just add handler so now let's do the same thing actually copy and paste in
it Susan time copy and paste now and do the same thing right here with italics so this
pretty much now that we have added an action or excuse me an item listener we have a listener
waiting and what this does is pretty much waits for something to happen to bold box
or italic box and the only thing that can happen is you can click it so now something
is going to happen and it's going to wait to be clicked and when it gets clicked it's
going to recognize that it gets clicked so now that we are able to recognize one that
gets clicked let's go ahead and make something happen when it gets clicked I mean can recognize
it so might as well so the first thing we need to do is create a private handler class
so private whoa whoa whoa not right there right here private I cannot type today handler
class and implements I spell that right item listener like that and you don't want in for
our body upper class just like this actually in the next tutorial I'm going to show you
guys what goes in the body and it's pretty um different than less tutorial so you guys
definitely want to pay attention to list but another thing that I want to point out before
I forget whenever you put a class inside another class and if you're saying alright Bucky how
come you just don't put this class outside right here and make twos totally different
classes how come you need an inter class to go inside this whoa if you put a class inside
another class then this class this inner class it can see all of the variables and the methods
in the outer class so if you were to make two separate classes either on the outside
of this or in a totally different file then it's not going to be able to see the variables
and methods and since we want to make this class use some of the methods and variables
in here that's why we need to put this class inside the bigger class right here so that
is why I put this handler class inside this GUI class or GUI class but again and the next
story I'm going to show you guys add some functionality to tell you guys when you click
this check box here's what happens when cook the other one here's what happens when you
click them both here's what happens to the text but for now that's it for this tutorial
I'm running out of time so thank you guys for watching and I will see you next time
what's up guys welcome to your 65th Java tutorial and in this tutorial we're going to be building
a new class to handle the events when both of our check boxes are checked so let's go
ahead and make a new class called handler class to do just this we'll go private class
name it handler class there we go get eventually implements I got a type so EMP implements
item listener there we go now what this does pretty much is a class that implements item
listener and what implements mean is we get to use all the methods from item listener
but we're making a promised item listener item listener says all right here here's what
would happen if they're having a conversation item listener BIC all right you want to inherit
from me fine you can but promise if you implement from me you have to overwrite each one of
my classes so look at this it says the type hand handler class must implement the inherited
abstract method so there's abstract methods in item listener that we need to overwrite
pretty much lucky for us there's only one method in item listener not very big class
and this method is called item state changed so let's go ahead and overwrite this favorite
right now so public void item state change right like that make sure you spell it right
if not you're going to have some problems and the parameter it takes here is the item
event event object so what's going on pretty much is this every time you click one of these
checkboxes essentially attach them to item listener something calls an item event occurs
or pretty much an event occurs now what we can do with this event is add some functionality
to it and it can be handled by an item listener object which we created so now whenever since
this item listener is listened linked to this bold box whenever an item event occurs it's
going to run whatever is in here so let's go ahead and make some code to put in here
first of all we're going to want font name it fun instead of equal to null and this is
pretty much just going to store the font that we're going to change so what we want to do
is say all right if you click the bold box we want to bold if you click the italic box
we want the font italics if you click the bold box and the italic box we want to bold
and italics if nothing's clicked just have plain font so let's go ahead and ferd right
if bold box dot and the thingy rate is is selected just like that and this is built
in method to show if it's checked or not so if bold box is selected and let me find it
and it's out what a name italic box dot is selected then what do we want to do well let's
go ahead and change that font equal to new font and we're going to be doing a lot of
copy and pasting after this we want to change it to new font first of all we want to make
it serif just like before I want to take that font and make it bold and actually we need
that all in caps I forgot and we also want to take that font make it italic and then
we want the size to be 14 so get on my way house so what this means if you press the
bold box and the italic box if both of them are checked then we want to change the font
to bold and italics so what other options do we have here whoa we have else if we can
also have an option that if only the bolt box is selected so let me tighten that up
so actually we can just go ahead and copy this little too easy so else if only the bold
box is selected then what do we want to do we want to take that font and set it equal
to new font and we probably copy this my equal serve bold copy what did I copy there we go
too easy so if only the bulb box selected then font equals serif bold and then the size
of 14 so now let's go ahead and make another else if for only if the italics is selected
so copy we're getting real lazy here but really efficient how awesome is that italics or is
it named italic box there we go it's selected then we want to change the font to font italic
so now we only have one other option if it runs through and sees that if bold is selected
in italic is selected nope if both selected note if italics is selected nope else this
only else is going to occur if nothing is selected so what do you want to do want to
do the font and set it equal to new font and I guess I won't copy and paste it this time
first its serif and the rest is font up plain and of course we want to size 14s though so
now well looks like we're good to go and the last thing we have to do now that we actually
change that font we actually need to set the font so in our text field we need to set font
equal to that font that we just changed so this pretty much says alright depending on
what you selected I'm going to style the font a certain way and then at the end want to
test for all the styles it says alright I'm pretty much just going to set it on the screen
right now so now I have this from the last tutorial which you guys can see it pretty
much just makes a new window on the screen puts a function on the window so you can actually
close it size for that window and makes that window visible so now if we run this and click
OK we go ahead and we got this thing right here and I promise you to change that size
but this is good now you see when you click Bowl with the sentence bolts when you click
italics it italicizes to if you unclick bold just as only italics and not bold and if you
uncheck them both it's back to normal now let me give you quiet guys a real quick walk
through one more time of how this works ah take a deep breath you guys ready good we
pretty much made three variables on screen to hold the text field the bold box and the
italic box in our constructor we set the title in the layout nothing new we pretty much added
text field bold box and italic box to the screen and then we created a handler class
to handle these events now again anytime you click one of these boxes something called
an item event occurs now this can be handled by something called an item listener object
so that's why we needed to link this object to this class then once we do that we can
handle the events using a method called item stay changed now this automatically runs well
just like I told you before and what we did pretty much is we pretty much had a variable
called font to stylize the font right here here here and here and then we pretty much
once we found our style depending on what was checked we set it to the screen and then
we just ran our program made a window good to go so that's your quick tutorial on these
are called state boxes because they maintain a state like when you click them right here
I'll show you why when you click them they stay like that so that's called maintaining
a state so that's it for this story thank you guys for watching and next tutorial we'll
be going over more gooeys and we'll be eventually making this box pretty effing sweet right
now it doesn't do a whole lot but you know it will oh and another thing that if you follow
my channel you know that I give away prizes sometime I am waiting for prizes to come in
the mail now and it will be the best giveaway I had so far so just a little tip for you
guys who actually stuck through this tutorial and watched all nice little surprised don't
tell anyone else so trust me when I get my prizes it should be like next week or something
they're going to be the best giveaways ever so you know something to look forward to but
for now thank you guys for watching don't forget to subscribe and I'll see you next
tutorial what is up guys welcome to your 66th Java tutorial and in this tutorial we're going
to be making a GUI with radio buttons on it and what a radio button is it's kind of like
a check box but the only difference is it you can only have one radio button selected
at a time so if you have 4 radio buttons and they're side to side if one selected and then
you click another one the other one automatically Dee selects and they actually got their name
from the older radios I guess I don't even know if I was alive during this time but evidently
the old radios when you press the one button all the other buttons would pop out so you
can only have one button select at a time so that's your real quick tutorial on what
we're doing and also a little tutorial on old-fashioned automobiles so you know the
Saiga 2 for one so let's go ahead and start making this right here we're going to have
let's see like 10 variable variables or something to start out the first variable we need is
something called a J text field and this is just the same one from less storoe and I'll
just name this TF like last time and this is pretty much going to be the field that
stores a text if you couldn't tell and next we're going to have a private a font variable
and we're going to have four types of fun we're going to have plain font we're going
to have bold font and we'll name that be F best friend and we will have another font
called italic font and you can't write if like this because if is a key word so you
got to write ITF that might be confusing but eh we'll get through it and then lastly we're
going to have a font that's both bold and italic so I'm going to put bold italic font
vif bit it's pretty cool-looking variable biff i got named more stuff biff next we're
going to have 4 radio buttons and these are the buttons I was talking about so let's go
private and there's J radio button just like that and I'm going to name my first bun plain
button and when you click this button it's going to give you that plain font right there
so now let me copy this because I'm lazy and make four more or excuse me three more next
I'm going to have a bold button BB next I'm going to have an italic button and I'm named
IB and next I'm going to have a bold italic button name bib so now this plane gives you
plain file bold font italic font bold and italic font when you click these buttons and
the last variable we're going to be using in this tutorial it's actually a group variable
so let's go ahead and write private button group and you can name it anything you want
I'm going to name my group for simplicity and what this group is going to do this is
pretty much going to establish a relationship so that only one button can be selected at
a time if we didn't have them related they know pretty much before separate buttons on
your screen each acting independently we wanted me each be part of a family so that way they
can know when each other's clicked in each other's on clicks or select it or deselect
it if you prefer that so now enough for the variables let's go ahead and start making
our constructor public GUI won't take any parameters and let's go ahead and start making
the body the first thing that we're going to want to add in our constructor is of course
the title like always and I will name mine the title pretty cool huh and the next thing
I want is the layout so I'm going to put set layout and of course in here since we don't
know anything else new new small flow layout just like that looks good so now we got a
title and a layout on our window what else can we do to this well let's go ahead and
add that text field that I talked to you guys about and I named it TF if you guys forgot
and I'm going to set it equal to Nu J text field and this field needs to be capitalized
and for my parameters I'm going to first have what's in the text field so I'm pub lucky
is awesome and hots that's what I you know that's what girls usually say to me walking
down the street but anyways and 25:4 sighs and now we just need to add this text field
to the screen because that we definitely want that to show up so now we got a title a layout
in the text field just chillin on our screen so what else can we add well we probably want
to put those buttons on there so I'm going to take that PB which is that plain button
I'm going to set it equal to nu J J radio button just like that and the parameters for
a button is first what do you want appear to the right of the button I'm just going
to have plain since it's plain font and I'm going to set this equal to true now hold on
and let me H my ear Oh feels good oh I get in there now the parameters it takes for true
and false that are pretty much true is checked and false is unchecked so if we're stabbing
a relationship we can only have one true at a time so the first thing we want to do is
set one of them to true and later on it's going to do automatically for us when you
click it but for now let's just go ahead and copy this and the rest of them are going to
be false by the way since we only want one checked at a time because that's kind of the
concept of radio button the next bold button is going to be equal to nu J radio button
we'll put this equal to bold and we'll put this equal to false now after let's go ahead
and copy this one make a life a little bit easier so now we have two buttons created
now let's go ahead and what do we name this other one italic button for IB purpose should
named it a little more descriptive but and we'll name this italic and this will be the
text chillin next to the button now set this equal to false - and our less button is named
bib bold and italic button so we'll just have like bold and italic sitting next to it so
now that we created these four button objects we can go ahead and add the screen using ad
and PB this should make this so you can add multiple ones at one time but I don't think
they do so add BB add IB and add what else we got here BIP BIP BIP BIP there that's enough
that so now we pretty much created four buttons and we actually added them to the screen so
that they showed up now like I said before the last variable we had it was this button
group right here so that's what we're going to be doing right here right now we're going
to be setting an object just called group and we're going to be setting equal to new
button group like that and this doesn't take any parameters for a constructor and what
this group is going to do is anytime you add an element to the group it's going to add
it pretty much to the group pretty confusing huh so let's go ahead and group add and the
first thing we're going to want to add is the PB which is the plain button so now this
group has one button in it so let's go ahead and add another one group add dot b b now
the group has two buttons in it think you see where this is going group dot add I be
sorry has be in there for us pretty cool and group dot add and first one excuse me the
last one bib so now our family has four buttons in it and we were wondering all right how
come we needed a group them we need to group them so that they know when each other is
checked and when each other is not checked since we group these they automatically by
default know that when one of these is checked the rest of us has to turn off so that way
only one button can be checked at a time it's pretty cool functionality that they you know
built that in for us and we don't have to talk to each other for us so the last thing
I'm probably going to be doing in this tutorial is just setting the font which is up here
P FBF ITF and bi F this is pretty much the fonts that I'm going to set and I need these
fonts to pass in later so the first thing the plain font is going to happen when you
click that plain button and it's going to be equal to new font and in your parameters
for font first type Seraph and lastly font the plane and give it like a size of 14 or
something looks good and now let's go ahead and do this for the other one so there's for
me let me think plain font bold font italic font so plain for bold font italic font bold
attack bar so plain font bold font italic font and vif right yup looks good to me so
now this is going to be font dot bold for the bold italic fonts going to be italic and
the bold and italic fonts going to be fun dot bold plus font daunt italic just like
that so now we got our four fonts on the screen well not yet but um that's it for this tutorial
so I'm going to catch up with next tutorial don't forget to watch it don't forget to subscribe
it's going to be awesome it's going to be amazing so thank you guys for watching and
I'll see you next tutorial we were we will finish this program what's going on everyone
welcome to 67th java tutorial and in this tutorial we're gonna be finishing this program
demonstrating je radio buttons so where we left off on the last tutorial we pretty much
made four different kinds of fonts and now what we need to do is pretty much take that
text field and we need to set it equal to a font by default and we just need to set
it equal to a font by default so in the program runs for the first time and knows what font
to use so I'm gonna set equal to plain font because I'm just a plain old guy you can set
equal to different fun if you want but I ain't gonna so now what we need to do is pretty
much create an add item listener for each button so we can take that plain button first
and add item listener just like that fun fun fun fun there we go got clear on my throat
and the parameter this add item listener takes is gonna be this get out of my way new handler
class and for the constructor we're gonna pass in the plain font now what happens is
this an add item listener pretty much shows there and waits for something to happen to
that item which is in this case the plain button now whenever it does happen what do
we want the object because this is object in here and what do we want it to do whoa
for the handler class is gonna be handling it or it's pretty much the code that's gonna
happen if this button is clicked we need to pass in plain font to the constructor and
let me go ahead and add a comment so this wait for event to happen and this what this
doesn't pass and font object to constructor and we didn't even create a class or constructor
yet so this might be confusing but I spoke constructor run see this this thing if I click
it it's not only a platform but it also is a spellchecker so since I spoke constructor
on it corrected it for me how cool is that compiler and a spell checker double banger
so now let's go ahead and add this for well the rest of them copy this and put that's
for a bold button italic button and bold and italic button so bold 1c italic button is
IB and bold and italic button of course we need to change these to bold font and I said
italic font was ITF and the bold and italic font was that so now we pretty much got our
constructor of our main class built now we can go ahead and we're giving this error message
because handler class is not a class yet so let's change that right now let's go ahead
and make a handed request and we're gonna put this inside the other class so let's go
ahead and make private class hand or lower a little alert class right there no my sound
didn't mess up I actually just said that implements item listener and you guys know what implements
means from the last tutorials so inside my handler class the first thing I'm gonna want
to do is create a variable since uh you know we're just gonna need one it's gonna be a
font variable and it set it equal to the name of font because I mean makes sense right the
next thing I'm going to want to do is create that constructor that we used up here now
as you can see that constructor takes a font object as its argument so we got to make sure
we have a foreign objects as argument when it created it so let's go ahead and make public
and alert class and let's go ahead and put like font and just name it F or something
so now plus I'm lucky TF so now in our constructor for this and make sure yep looks right to
me let's go ahead and just add font and set equal to F now let's see what error I'm getting
oh I must inherit the abstract method item listener items state changed so since we use
item listener it has a method in it cut item state change that we need to use so let me
add a comment to here and what this does is the font object gets variable fun this is
pretty much just changing the object we pass in to the variable font so now that we implemented
from item listener we need to use that method called item state change so let's go ahead
and make a new method called public boy doesn't return anything item state changed and if
I spelled that right we should be good so now this argument takes the item event in
which name it events and for this in the body let's just go ahead and have something like
text font and we'll set it equal to set font for that font so now if I do this my error
should go away but they're not so I'm gonna positive figure out what's wrong okay guys
I found my error and it was really embarrassing actually he's worth constructor of handler
class up a handler class and I spelled it wrong so you definitely need to put handler
class and spoil your constructors right pretty embarrassing if he asked me so you know not
gonna let it happen again I promise so now that we got all that changed let's go ahead
and well I'll pretty much oh I had a comment for this for ease of reading I will put sits
the font to the font object that was passed in looks good so well I'm gonna go ahead and
run this then I'll talk you guys through it so I'm just gonna use my seen object from
last oriole if you didn't watch my less sorrow then pause the video right now and copy that
and I'm just gonna go ahead and run this and hopefully it works so now we got Bucky is
awesome as huh uh-huh thank you and it's plain by default like we said before and now you
can see these radio buttons since they're part of a group then whenever we click one
then the rest uncheck and that is because they are part of a group and they're able
to see each other or know each other is doing and that's some built-in um methods that allow
you to do that so that's pretty cool right there so let me talk you guys one last time
through how we did this well let's see let's just get to the hard stuff we made variables
we added a we added all the stuff this screen right here BAM here's the confusing stuff
aside from adding all the stuff to the screen here's what we did we pretty much need to
add add item listener to each button so that something can sit on those buttons and wait
for it to be click or unclick now add item listener this built-in method it takes this
handler class object as a parameter so what this is is pretty much saying alright we're
gonna be waiting for something happen and as soon as it clicks then I'm gonna call this
class right here and that's the code that's going to happen when this event happens so
now that we told it all right we want you to wait for something to happen and when it
happens do this we said all right what do we want to do well we created a new handler
class and the constructor for that class is called an alert class of course and it takes
a fog object so let's go ahead and look at that constructor right now the constructor
all it does is it gets that font which was plain font and sets it equal to the name font
or the variable font and why do we need to set it equal to this variable so we can use
it later on to set the font so pretty much we took that font set it equal to the word
font and then in the item state change which pretty much is the event that happens when
it's clicked it takes that font and sets it equal to plain font bold fonts out font or
plain and bold font or simply bold and italic font so now that we have a constructor in
our item state changed we're good to go and we were able to run the program so that's
your real quick tutorial of radio buttons and the last thing that you guys don't want
to forget don't forget to group your buttons right here set equal to new button group objects
in this group allows your buttons to be one big family and allows them to see what each
other is doing and this lets them know when each other's is selected or deselected so
that's that for this tutorial hope you guys learned a little something not too much of
course so thank you guys for watching don't forget subscribe don't forget to check out
all my contests and I will see you next time what is going on guys welcome to your 68th
draft tutorial and in this story I'm going to tell you guys how to make something called
J combo boxes now well Jake combo boxes it's pretty much just a drop down list but that's
what Java named it so you know they had to be different so let's go ahead and enough
of the small talk let's get started making it and I just want to tell you guys what this
combo box is going to do is it's going to give you a list of pictures to drop down in
your drop down list and depending on what picture you select is what's going to appear
on the screen so that's a real quick overview of the program so we need two variables one
is going to be the J combo box which is going to be the drop down list itself and I'm just
going to name my box you can name your list if you want but again I'm going to name em
box and the next thing we need is the picture to appear on the right hand side of it so
I'm going to make it private J label in the picture is our name J label in Java and I'm
going to name my variable picture I mean why not it's nice and easy now the next thing
we need to create is actually two arrays now you see here I already have two images be
paying in X topping and these are pretty much just an image that says be an image that says
X with a little graphics on it so we need to create two arrays the first array is going
to store the file name and the second array is going to store the picture itself so let's
go ahead and put grab it static we don't want it to change string in remember this is going
to store the path and the path is just a string and I'm going to name it file a name and we'll
set it equal to B duck ping and make sure you're put in quotes since it's a string so
that's the first element of the array and the second one is X dot ping make sure I got
PNG PNG in so now we have an array called file name and it has two elements in it beat-up
ping and x stopping simple enough now let's go ahead and add it or excuse me create a
new array that is an icon array and we do it like that and we'll name this one picks
and this is going to store the files there themselves so let's go ahead and as the first
element what we need to do is put new image icon and for the parameter of image icon put
get class and it's going to be this class itself it's going to select and get resource
and the resource for this is going to be the parameter it takes as a name and for the name
we can write beat-up ping but let's go ahead and write file a name with the index of zero
so our first element in this array is the file name index of zero so let's see one to
make sure I got my parentheses there lot of parentheses here yep that's right so now let's
go ahead and just copy this so the first element in this pix array is pretty much this B picture
itself this is just a file name this is the actual image so let's go ahead and add the
second array by pasting that in the second element is file name one remember arrays start
at zero and then the next one is 1 so now we can go ahead and go to our next line so
now we got two arrays one stores the file names and one stores the pictures and we're
actually going to be using the file names later so that's why I just didn't put the
file names right here you'll see why in a little bit so now that we got all our variables
in arrays we can go ahead and start building the constructor like we always do so public
my class is named GUI and let's go ahead and add a body of that constructor and the first
thing we're going to want to add as always is the title and I'm just going to write the
title not fancy and set layout su there we go in the layout for this since we don't learn
layouts yet is new flow layout just like that so now we have a title in a layout on our
window what else can we do the first thing we're going to want to do is create that drop-down
box with the file names is the list options so I'm going to take that box variable that
I created up here and set it equal to nu J combo box if I can spell and the parameter
it takes here is an array of options and since we want our options to be beeping and X ping
so the user knows what's going on go ahead and write file a name in here and what this
will do is automatically put all of these options in a list for us how cool is that
so now let's go ahead and get my cursor set up and go the next line and what can we do
after this well we have this box this drop-down box just chilling here right now what we want
to do is add some functionality to it and we do this by adding adding an item listener
so actually I'm going to do this in next tutorial since it's going to take a while I know I'm
going to run out of time in this tutorial but an overview right now we created a box
we created variable for the picture that we want to appear on the right side of the drop-down
box we created an array of file names and array of images of my two images we added
a title set the layout for our window and we added a box to the window then in the next
tutorial we're actually going to be adding some events to this box so when you click
one of the options something actually happens but for now that's it for this tutorial thank
you guys for watching don't forget to subscribe and I'll see you next door what is going on
everyone welcome to your 69th java tutorial I've been making a lot of Java tutorials I
just noticed and in this tutorial I'm gonna show you guys how to add some functionality
to the box so what we need to do to add functionality is add an item listener and one item listener
does is pretty much this chills there and waits for you to click something and when
it clicks it it pretty much just well tells it to do something so let's go ahead and take
that box because that's what we want to add an event to and we'll go ahead and I'd add
item listener there we go now as you know an item listener usually takes an event handler
object as its parameter well I'm gonna tell you guys how to do something real quick here
instead of having a new object like handler and having it passed into the handler class
what you can do is if you go ahead and there we go just like that instead of having an
object and creating a whole another outside class somewhere else you can actually put
something called an anonymous inner class in here and what this does is it actually
implements the item listener class so here's how you do that go ahead and type a new item
listen or just like that and this is your declaration for an anonymous class that implements
item listener so instead of having like before handler class implements item listener outside
and we have to make a whole new class what we can do is just go ahead and stick a class
in here and pass it in for the parameter as add item listener and we bypass that entire
step of having to create a whole new separate class so again since we named a none anonymous
class that implements item listener we need to use that method and that's called item
state changed as you can see right there so what this class has to have in there is only
one method that's know let's go ahead and create it right now item listener has let's
go ahead and make it public void item state change I gotta make sure I spell that right
and did I oh yeah nice now what we have to do is of course it takes in item event event
just like that and as our body for this we just go ahead and write whatever we want to
happen when we selecting one of the options and here's how you can do that go ahead and
make a if statement in this if statement determines what option you selected so if the events
get state change this pretty much if you click a button we want to set it equal to or excuse
me we want to test if it's equal to item event dot selected so what this pretty much means
all right did you select a button is equal to what was this pretty much means what was
selected just a nutshell so what will select it first of all whatever you select do we
want to take that picture and remember this is just the picture that appears on your screen
we want to set icon which pretty much means change the image to and the parameter says
what icon do you want changed to well we have an array called pix and what we have the thing
is we have two elements or two images in this one so what when do we select well go ahead
and type box dot get selected index with empty parameters just like that make sure typed
everything right parenthesis bam bam now make sure the method gets state change is undefined
type and oh here we go hold on a second all right guys in all of my excitedness I did
another stupid ear I actually put get state change instead of changed quite embarrassing
make sure you type that right but now pretty much that's all we needed to do so what this
does is it makes anonymous inner class and this is the class it's and in this class the
only thing we needed to do is since the implement and item listener we needed to add an item
state changed method since that was the only method in item listener and then we pretty
much just said our right while item did you select or what option did you select in the
drop-down list and change the picture to that icon or item so now we can go ahead and get
out of add item listener and we can pretty much just add all this this screen so add
that box to the screen and remember that box is just the drop-down box and now we want
to take that picture and add it to the screen but by default let's go ahead instead of equal
to nu J label and we'll do pix 0 and this pretty much just gives a default value so
you know something appears before you choose any options and lastly let's just go ahead
and add that picture to the screen just like that and we shouldn't get any air should it
and now let's go ahead and just run this and I'll show you guys what we get by default
again like I said we have B on the screen which is at the end X is 0 then when we select
X our picture changes the X we can go ahead and select B X we can do this all day if we
want really but no time for that we need to overview our program so what we did is pretty
much let me get the good stuff we made a box variable a picture variable just a hold box
and picture later on we made an array of file names and we made an array of images named
pics and then we went and go ahead and our constructor which is pretty much making the
window we had a title editor layout we set that box equal to new jcombobox and it took
the file names is the options so again if I go ahead and run this this these options
right here are the default for file name so that's why that was able to run and the only
confusing part which we did new is this instead of having a new object in a new class we just
went ahead and stuck the handle class inside the add item listener and that was actually
an anonymous inner class and what we needed to do is we needed to overwrite this method
item state changed because that was the only method in item listener and we overrode it
with a nice little function right here and then we went had to added the box at a screen
then we went ahead and added the picture this screen and just gave a default setting and
we were good to go so again here is our awesome little program right here flawless no bugs
no anything so that's all you get for this tutorial thank you guys for watching in the
next tutorial we're gonna be going over something else cool you can do with gooeys and well
that's it so again thank you guys for watching don't forget to subscribe and I will see you
next door what's going on guys welcome to your 770 a--the there we go Geoff tutorial
and this is ro we're going to be going over something called Jay list now what Jay lists
is is it's pretty much a list for your GUI and when you click on one of the options we
can make an event happen such as in this case switching the background color so let's go
ahead and get started if you can't tell I imported one more thing here so make sure
you have all of your imports right here if you need them pause the screen but you're
probably don't so I'm just going to go ahead and minimize those right there and now we're
good to go the first thing we're going to need to do is make a variable for that Jay
list so let's go ahead and make private Jay list because that's what type it is I'm going
to name my list for simplicity now after this what program we're going to be making is we're
going to have a list of colors and when you click on one of those colors it changes the
background so we're going to have two arrays one for the color name and one for the actual
colors himself that the computer can recognize so one that we can recognize and one the computer
can recognize so let's go ahead and make our first array just the color names that we can
recognize so it's going to be private static string it's just going to be the names of
the colors and just name it color names and we'll set it equal to let's go have a color
like black blue red and white I have four colors inner lists can have more if you want
but I'm just going to have these basic ones and make sure to put them all in quotation
marks like this and then and your line with a semicolon BAM you're good to go so I have
four color names in our first array called color names now the second thing I want to
do is build an array of colors that the computer can understand so go ahead grab it static
again I'm going to name this array color or excuse me it's going to be of the color type
and I'm going to name it colors and in this list we're going to put the colors in the
way that Java sees them and that's this color dot black all in cat so that's how Java sees
colors or recognizes them it's from the color class and then you write the name of the color
all in caps and our next one's blue so color the blue all in caps and our next one's red
so color dot thread all the caps and color the white so now we should be good to go looks
like America in any airs and now let's go ahead and we have our three variables pretty
much list coloring names and colors we can go ahead and build our constructor or window
so go ahead and go to your instructor and it's not going to take any arguments just
go ahead and do some housekeeping stuff add the title and where you at curse oh got the
hiccups and embarrassing and next thing you want to do is set the layout and just going
to set this to normal flow layout by putting new flow layout I wonder why they capitalized
the F in that one pretty weird and don't forget your empty parameters so now I have a title
in the layout now we can go ahead and start messing with our list the first thing we want
to do with our list variable is actually create something and what we need to do is create
a new j list and the parameter J list takes is an array of what you want the person to
see and we want the person see the color names is the options so let's go ahead and just
write color names in here um you don't need it in like quotation marks or anything like
that so now we have a list and it pretty much is just a list of color names what we want
to do now is set how many rows are pretty much the height of the list so go ahead and
take that list and put set visible Arial row count right there to easy and what this is
is how many options the user can see by default now if you put like they can only see four
but there's like ten items in the list a little scroll bar is going to appear on the right
but since we only have four and we put four then it should fit just perfect um the next
thing you want to do is go list set o it's called selection mode there we go right there
set selection mode and for the perimeter it takes is this some lists allow you to select
one thing or another thing and some lists allow you to select multiple items the one
we want to use is we only want to be able to have a single selection at a time so we're
going to have as a parameter list selection model dot single underscore selection just
like this and now instead of like cooking control and selecting many things or shift
the user can only select one thing in the ERR list at the time so now the last thing
we need to do since we got our list all made is just add it to the window or well yeah
that's all we need to add it to the window so add new j scroll pane and of course we
want to add our list to here and now we added our list to the window pretty much so in the
next tutorial we're going to be adding some functionality to this list and it's a little
different than the other tutorials I'm only teaching you something even newer and easier
in the next tutorial but for now thank you guys for watching don't forget to subscribe
and don't forget to check out the next tutorial so I'll see you then what is going on everyone
hey guys doing welcome to 70 first job tutorial and in this tutorial we're gonna be adding
some functionality to this list we just made on screen so let's go ahead and inside her
constructor so let's go ahead down a couple lines and what we want to do is add a selection
listener to this list so what we want to do is put list dot add list well look at that
already has it for us add list selection listener and then go ahead delete this because we're
not gonna need it and we're gonna make just like last sort of you saw another anonymous
inner class so go ahead and in your first line of the parameters for add list selection
listener what we wanted to do is make a new anonymous class and remember this is just
an easier way of handing it handling so put new list and then see you listen selection
listener just like that it's gonna take no parameters and we should say must implement
yep must implement method value changed so this is pretty much says whatever classroom
aid implements list selection listener it's just a easier way of bypassing it the thing
is we need to make a method in here called value changed that's what it's telling us
right now so in like orders to fulfill our duties of implementing it so let's go ahead
and make public void value changed in all this is pretty much what you need every time
you make a J list so let's go ahead and fir the parameters for this is list selection
event and go ahead and type event and now we can go ahead and start doing the stuff
that we want to do so pretty much aside from this all of this stuff is pretty much built
in Java and you need this every time it's none of your own custom stuff so it's just
the easier way of adding some event handlers to your program so now that we got all this
housekeeping stuff done let's get into the good stuff what do we want to do when a user
clicks one of the colors well what we do want to do in people language is select that color
and change the background to that color so actually there's three different layers on
our window the first there's a background there's a content pane and there's something
called I think it's called a glass pane now the content pane is on top of the background
so they have a set up a really weird you need to actually access the background through
the content pane in order to change background so the first thing you need to do is get content
pane like that and the second thing you need to do is finally we get to our background
so let's go ahead and set background just like that and I spout that around background
that's a little bit better and there we go my hair goes away and so what do we want to
set the background to well in our parameters for here let me just end this statement and
our parameters for here we want to set it to one of these colors from the color list
color black color blue color red color right so the first thing we're gonna nuke you need
to do is add color and empty square brackets and now what we want to appear in here is
one of the numbers one through four so how can we get the numbers one through four depending
on what they selected remember in our list was the array or excuse me was the array color
names and our list was named list so let's go ahead and put list dot get selected index
so it's automatically gonna know whenever we do not want to put that there let's see
so it's automatically gonna know that in our list that we just created and why am I getting
a line under color because it actually colors there we go so when whenever it clicks our
list in by it I mean we it's automatically gonna know if we click the first second third
or fourth item our list it's gonna get that number and it's gonna say alright if you click
the third item in our list I'm gonna pass three in four here so color is three equals
let's see colors zero actually one two three so it's gonna be red so that is how it happens
like that and remember to set the background you need to get the content pane and get set
background I don't know why they did it this way instead of just you know set the background
but they did it this way and don't forget there's actually three layers the content
pane background and I think it's called like a glass pane or something something weird
so that's that for this story and let's actually run this program and see if it works so I
see by default it's nothing because we didn't send it to default value now when you click
black blue white red blue red black white and any order then that's pretty much just
what happens so one more time and let me walk you guys through that the first thing we did
is import all our stuff make sure you have those it's key trust me and let's see let's
get to the important stuff we made a list which is a variable further list we made the
color names which is what we saw on the screen and we had a correspondent to the colors so
that Java can understand it using the special colored black next we made a constructor with
a titled layout yada yada yada we made a list and we set it equal to J list color names
which were the options that the user can see set the visible row count which was four options
by default then we made a single selection so you can only select one at a time and we
made a J scroll pane which gave us the scroll window if necessary next we added all this
stuff which is pretty much the default stuff you have to add in Java and finally we added
this little line right here we went to the content pane so we could get to the background
and then depending on what index they clicked we corresponding it to list of colors that
we made up here so one is black twos blue threes red four is white so that is how we
did that and that's how we made an awesome little program and look at this how awesome
is that it seriously red white and blue didn't even notice it black where does that come
in but anyways thank you guys for watching don't forget subscribe and I will see you
next tutorial what is going on guys welcome to your 72nd Java tutorial and in this story
I'm going to be teaching guys had build a multiple selection list and in the last tutorial
you guys learn how to build a list with a single selection or in other words you can
only select one item at a time but in this tutorial I'm going to be building a program
where there's a list on the left where the bunch of items and there's a button in the
middle and when you select a bunch of items on the left and click the button it moves
it to the right so it's going to be a pretty cool tutorial and actually the cool thing
about this tutorial is all the hard stuff we need to do is already built into Java so
I just need to teach you the methods so let's go ahead and get started make sure you have
all your imports right there I'm going to go ahead and minimize that and now let's go
ahead and start making some variables we need to lists and a button to start out so private
Jay list in the first list I'm just going to name left list since this is going to be
on the left hand side and I'm going to make another one identical to this but I'm going
to change can't keep talking I'm going to name this right list since this is the list
that's going to be on the right hand side so now we've made two variables for left list
and right list and they're both two J lists so now let's go ahead and just make a button
private J button and I'm just going to name my move button because that's what it's going
to do it's going to move this stuff from the left to the right it's actually going to copy
it but you know what's the difference now of course we need an array or pretty much
items to go in our list and I'm just going to have it private static and I'm going to
have a string because you know she's going to be names and I'm going to have foods you
can have anything you want it really doesn't matter but foods sounds something I can it'll
be interesting so let's go ahead and type the foods I like which is of course bacon
and let's see what else wings had some last night they're amazing bone sucking sauce look
it up ham and let's get like two more beef I don't really really like beef all that much
but I like the name of beef and that's the important thing and the last thing of course
more bacon and let's go ahead and end this so now we have an array called food and actually
name that foods because I mean it's foods bacon wings ham beef and more bacon now make
sure I got that and so now we have the four variables we need for this tutorial left list
right list the button in the array so now let's go ahead and start well I guess we can
go ahead and start building our constructor so public GUI of course it doesn't take any
arguments and let's go ahead and add a title first called super just name it title and
make a layout next so set layout and of course nothing new here we're going to get new flow
layout with empty parameters and now let's go ahead and start editing that list on the
left the first thing we need to do is populate it with the foods so it's new jlist and any
time you use a j list it takes the argument of an array or in other words what items do
you want to put in it so we want to put those foods in it so let's go ahead and put foods
in it BAM a little too easy the next thing we're going to want to do to left list is
if I spell it right is add the visible row count so set visible row count and make sure
BAM look at that how easy is that so how many of these items do you want to see by default
and I want to see three items by default so now that our list is five items but we can
only see three what we need to do is add something called a scroll pane that we're going to add
later on right now left list dot set selection mode right there too lazy to type it and go
ahead and and this statement and our selection mode unlike before it's going to be list selection
model not Mosel model dot right there multiple selection and what this means is unlike last
time where you can only select them one thing at a time with multiple interval selection
using your ctrl + Shift keys on your keyboard you can select multiple items at a time using
this so now the last thing that we need to do to our arm left list is add it to the screen
and also like I just told you guys before we need to add a new J scroll pane right here
and what this allows you to do if we add the left list eventually is it allows you to have
a list bigger than default viewing and it pretty much just put a scrollbar on the list
that's what I'm trying to say it pretty much puts a scrollbar on your list so now we did
our left list so now all we need to do now is make our move button and write lists so
go ahead and click move button and set these equal to nu J button and make sure you have
a space in between there and the parameters frist is the text you want to appear on the
button so I'm just going to have move and a little arrow just like that so you know
that we're moving from the left to the right so now we can go ahead and add action listen
or listener of other words functionality to this move button so move button dot add action
listener awesome and of course this is going to have an inner class because it's a lot
easier so inside your add action listener parameter just go ahead and make a whole new
class called new not boo new action listener just like that and this class of course doesn't
take any parameters and inside your new action listener inner class we want to have one method
and if you can see by our air it's called action performed so let's go ahead and add
public void action performed make sure I spelled it right PR fo r m e D looks good and as your
parameters for this just right action event event BAM this is a little too easy that's
the only problem with it now we can finally get to the good stuff of what we want to do
again this is all default stuff that you need that's how did they just made the program
but now we get the custom stuff what we want to actually do well the first thing we want
to do when we click the move button is build our write list we've already built that left
list but the right list depends on what we select in left list so go ahead and type write
list da and the first thing we need to do is set the list data or set the things we
want in the list so go ahead and set list data and what this does is it takes an array
is its argument just like let's see just like J list took an array as its argument set list
that is also going to take an array that's its argument to set the list data so how do
we get an array of selected values from a left list well this is another cool thing
just go ahead and type the left list dot get selected values right there how awesome is
that get selected values and what left less dot gets selected values those is whenever
you select stuff on the left-hand side of your screen or in the left list it makes an
array of those selected values and that array is going to be passed into set list data and
essentially is going to create a list on the right-hand side depending on what you selected
on the left hand side how cool is that we don't have to make a new method for setlist
data we don't have to make a new method for get selected values everything's already built
in method so that is you know it's just awesome so now that we made this button and we added
some functionality to it let's go ahead and do one last thing and of course we need to
add it to the screen so let's just go ahead and click add move button and now our move
button is added to the screen so in the next tutorial we're going to be finishing up this
program and actually on building a write list and you'll see what I mean in the next tutorial
so for now I guess I'll see you next time thank you guys for watching don't forget to
subscribe and I will see you soon what is going on guys welcome to 73rd Java tutorial
and in this tutorial we're going to be finishing up our program and actually running it so
in the last story we created a list on the Left we created the move button we added some
functionality to it so now we go ahead and only thing we have to do is create the list
on the right so let's go ahead and make right list and just go ahead and like set it equal
to nu J list and we don't need any parameters for it because by default we want nothing
in it so that's why I don't need any parameters for it right now you can if you want but I
mean it'll be kind of useful and hold on let me H my ear feels good right in there Oh disgusting
so now what we need to do is since we have nothing in our list by default then it's not
even gonna appear on the screen by default since I mean it doesn't have anything in it
so what we need to do is set a default height and width for it pretty much so let's go ahead
and see what are we put up here for 3 ok what we need to do is first take that write list
and to make sure it appears on the screen by default what we need to do first is get
out of my way set visible row count and I'm gonna set this equal to 3 because our other
one is set to 3 n I just want some consistency so next I'm gonna take that write list and
we need to set fixed cell height and width so set fixed so let's do with first I mean
only makes sense it will set a width of like a hundred and do the same thing for height
right list set fixed crap my default thing height and we'll set this equal to like 15
or something like that and this should be enough to hold her values if it's not we'll
go back and tweak a little bit but oh well and the last thing we have to do aside from
this is take that right list and we need to set the selection mode like before it so set
selection probably can copy this from up there but too lazy to set the selection mode make
sure to enter right get selection mode and what this is into list selection model dot
and we're gonna write Wow how convenient is that multiple interval selection so then once
I type a write list and type this right what this is going to do is allow you to select
the multiple items from the right-hand side and I know you guys can't see that multiple
interval selection so just like that now that we have our right list made the only thing
we need to do is add it to the screen so let's go ahead and add new j scroll pane so we want
a scroll ball urn this one to write list so now we have everything added to our screen
and our program is basically built right here so let's go ahead and in our main I already
have this stuff from the last Oriels let's go ahead and run this and see what we got
well I resize my window a little weird but we can always change that by setting the size
right here but I'm too lazy for now so as you can see the title is tighter title and
now unlike before where we can select single things if I hold down the shift key on my
keyboard look at this click and now I get a multiple selection or if I hold down ctrl
on my keyboard I get that selection where you can select like every other one or if
you want to deselect something just click it again and now if you move it you get bacon
more bacon or if we want bacon in ham I'm going to click bacon hold down shift click
ham and move and we get bacon wings in ham and of course following everything let's go
ahead and just select that move and now we got everything right there so that is pretty
much the program of using a multiple interval selection or or in other words multiple selection
lists and one quick time how this program works we've pretty much made variables right
here in an array we took that array and passed it into left list so left list will have something
to display we set the visible row count we made it so you can select multiple things
and we also made a scroll bar on the left list then we got to the move button we did
all this default stuff on he also did this cool stuff setlist data of the right list
automatically depending on what you select in the left list that's why whenever we create
click move it first looked what we selected in the left list and depending on what we
selected that's what I put in the right list so this is how you do that using setlist data
in the get selected values method we added that button to the screen and then what we
did is we made a new write list that was empty by default and since it was empty by default
it wouldn't display if we didn't give it a height in width and now we made that list
so you could select multiple things in it even though you didn't really need to and
last but not least we added the right to the screen so everything was added to the screen
and it was good to go and it was good to run so that is our final program using multiple
selection lists I hope you guys enjoyed it I got the hiccups now so thank you guys for
watching don't forget to subscribe and I will see you next tutorial what is going on everyone
welcome to your 74th Java tutorial and in this tutorial we're going to be going over
something called Mouse event handling now we already went over stuff like Liz then what
happens or what we want the program to do if the user clicks a button or something but
we didn't go over what they would do if they like press the mouse or click it or if they
drag the mouse and this program we can respond to Mouse events that the user does just like
I just talked about like they click it or if they move it and stuff like that so let's
go ahead and get started and then well once I'm done with this program you're understand
how to respond to users Mouse events so the first thing we need to do is import a bunch
of stuff as you can see I got rid of everything for my other tutorial so let's go ahead and
import java.awt dot let's go ahead and just import all of them and this is going to import
stuff like colors border layouts and well stuff like that next we want to import java.awt
event in all of those and these are pretty much our Mouse events and lastly what we want
to import is import Java X dot swing dot all of those so now we got all the imports we
need I believe if not we'll import them later but I'm pretty sure that's all so after this
let's just go ahead and get rid of that ticker and make two variables we're going to have
two things in our window so we're going to have a private variable jpanel and actually
that needs to be jpanel and just name this like mouse there we go I'll get it eventually
Mouse panel and this is gonna pretty much be the area where you can like click and move
your cursor around so it's pretty much just going to be a big white box and we're going
to have another variable to hold another piece of our window and that's J label and just
set this equal to like status bar so here's what our window is going to look our window
is going to look like it's going to have a big empty space and that's going to be the
called the Mouse panel and this is where we can test out all our mouse events like click
a drag release and we're going to have a status bar at the bottom and when we click we want
the status bar to say you clicked when we drag we want to say have the status bar say
you are dragging but you know we got program all that so let's go ahead and get to it so
after we got our two variables we can go ahead and make our constructor which pretty much
going to construct our window so name it public whatever you name your class I named my GUI
doesn't take any parameters because its constructor I mean I mean come on seriously we're not
going to pass any constructors or parameters into it and then we did super to of course
change the title of it because that is the constructor of the superclass jframe changes
the title um in other words just put super and put title whatever you want now we pretty
much got a window with a title bar on it not very exciting so let's go ahead and change
that mouse panel and actually make it something and we will set this equal to nu J panel so
now we actually set it equal to something so we can use it now so now let's do something
else with it something a little more exciting take that mouse panel and let's go ahead and
set the background color using the built in method set back around and you put note the
output color and is the arguments it takes a color so go ahead put color white so we're
going to put this out white default but don't worry we're going to be changing it to something
more exciting later on and the last thing we need to do is of course add this to the
screen so go ahead and add mouse panel but we don't want to do that if you can notice
we didn't have a flow layout like we did last time so for this layout go ahead and put boarder
layout dot Center and this is just going to make it appear in the center of the screen
and fill up all the empty space and we'll be going over layouts later on so you prize
you guys probably don't know what this means yet but trust me you will in a little bit
so now we have on our window which is this we have a title which is the title bar and
we have pretty much a background not very exciting so let's go ahead and add that status
bar we talked about right here so go ahead and type in status bar and we will set this
I spell that right all these underlines it like I spelled it wrong or something and we
will set this equal to Nu J label remember don't get this confused J panel J label your
status bar is the J label and just set it to um and just put default because that's
what's going to happen by default so now we have a status bar that says default by default
how clever is that so now let's just go ahead and add it to the screen what we're doing
we'll be changing it later with the Mouse events but for now we just want to add it
to the screen get done with status bar and for this order layout south and this is means
for real simply it's going to appear at the bottom of the screen so it's a little more
than that when you learned about layouts but for now trust me just means meal screen fill
up on this all the empty space appear at the bottom the screen simple enough and if you
couldn't guess it has north south east west and center so anyways let's talk about something
a little more useful in order to use our mouse events like mouseclicked mousepressed we need
to make a new handling class to handle these events so I'm going to make um I'm going to
make a class declaration right now even though we didn't make the class yet so in the future
we're going to make a cat class called handler class and I'm going to make my object called
like handler something something easy and of course we're going to set it equal to new
handler class I'll make sure I don't type anything around there so now there we go make
sure they're spelled the same that's how I check if you're using eclipse and if you like
click one thing it will highlight everything else yeah I mean just a neat little tip because
if you're like all right why am I getting air in here and not here just click it once
oh it's not how I there's spell different BAM so anyways what we want to do is we're
going to add two listeners because we're going to be implementing from two different classes
there are two different types of ways or there are pretty much two different types of mouse
events the first set of mouse events is like anything that you can do when the mouse is
still and these include press clicked release and there are two other ones that are kind
of funky called entered and exit which is enter the space and exit the space but the
next class we're going to implement from is what you can do while the mouse is moving
and you can of course move the mouse or if you hold down the mouse is called dragging
the mouse so let's go ahead and add two listeners or a hat right here right here I meant sound
like a what's that guy from politics yeah whatever he is that's why I just sounded like
so Mouse panel dot add Mouse listener how easy is that handler so of course add Mouse
listener weights and it takes an object as its parameter so that's why we passed in handler
and do it again Mouse panelled add always called Mouse motion listener and of course
this takes handler too so now we're getting error here because we didn't build the class
yet which we're going to do in the next tutorial but now let me go over this real quick we
imported everything we need because we needed some stuff we extended J frame which allowed
us to built windows we made two things in our windows a panel and status bar then we
set title of our window and then then we pretty much just set the color or the background
added it to the window we made a status bar added that to the window at the bottom and
then we made this new handler class with the object handler and we need to make an object
because these two built-in methods that wait for Mouse events they take an object as their
parameters so in the next tutorial we're actually going to be going over some of these Mouse
events such as what happens when you click the mouse and we're going to be adding some
functionality to it anyway if we have time we'll probably run it but we might not so
thank you guys for watching now cannot wait to build this next class in the next tutorial
so again thanks you thank you don't forget to subscribe and I'll see you next time what
is going on guys welcome to your 75th job tutorial and in this tutorial we're gonna
be going over how to handle those Mouse events so in the first tutorial we pretty much we
built a window so let's go ahead and go outside our constructor right here and we want to
make a new class and let's go ahead and make it make it private class and you got to name
it whatever you name it up here so I named mine handler class and then what we want to
do is implement from two other classes so implements Mouse listener and it also implements
Mouse motion listener so hopefully those errors go away and they did so now what implement
means is we have to use every method that was in the class Mouse listener and there
are five methods in this and Mouse motion listener and there are two methods in this
so what Java did is they put methods inside Mouse listener and Mouse motion listener to
automatically detect Mouse events so whenever you move your mouse it's gonna be registered
in an object called an event so let's go ahead and I'll just start building these methods
and you'll see exactly what happens so the first methods we need to implement are those
five methods for mouse listener and these are mouse press mouseclicked Mouse released
Mouse entered and mouse exited and I'm gonna tell you guys what these do when I go through
them but for now let's go ahead and build our third one first one so public void Mouse
clicked and what this does it is whenever you click down the mouse and what this means
and I know you guys forever saying are AI know what click means when you press and release
the mouse really click really quick Wow embarrassing that is a click now a mouse press and a mouse
release are two different things so you know maybe guys then no equipment after all but
this is what Java defines a click as so and all these parameters are gonna take something
called this Mouse event event so you might as well just go ahead and copy that and copy
that like that because we're going to be using it a lot later on so now whatever we type
in here is finally what we want to happen when the mouse gets clicked so let's go ahead
and take that status bar and we want to change the text on it so status bar dot set text
and what do we wanted to set the text equal to well let's just go ahead and put let's
put string format and in our format parameters it takes two parameters itself and the first
one is clicked at percent D comma percent D so it's going to say clicked at two coordinates
in Java also has two coordinates based on this event right here why do you have this
event because it registers the event like when you click it decides where it clicked
and you can get to this the locations by clicking event get X and also but guys probably can't
see this it's a little better and also event dot get Y with empty parameters so now whenever
you click this you get the x-coordinate in the y-coordinate of where the event happened
and you guys can see this later on but for now let's go to the next event the next one's
will be a lot easier I just wanted to demonstrate that you can get the x-coordinate and get
the y-coordinate and one of them but and the rest of the methods I'm gonna make them real
simple so we made something that happens when the mouse is clicked but we need to implement
the rest of the functionality and the second one that we need to implement is public void
Mouse released and this is what happens when you release that mouse button so actually
let's go the second one is Mouse pressed that people usually do so let's go ahead and mouse
pressed is what happens when you press down that mouse button so go ahead and throw your
mouse event event in there and for the body let's just go ahead and put something like
status bar set text this is gonna change the text on our status bar to like you pressed
down the mouse so now this is what happens that or excuse me the status bar is going
to pop up this text whenever we press down the mouse and now we can get the public void
Mouse released and this is the event of when the mouse releases or after you press it down
you let go of the button so this is all easier see with our program actually so let's just
go ahead and copy this now because we are too lazy and it's going to say mouse release
so you release the button so now we have three methods that we implement it what happens
when you click the mouse what happens when you press and release the mouse and believe
it or not these are actually three separate things I know you think press and release
is the same thing as clicking but trust me to Java it isn't so now in this class there
are two other methods that we need to implement public void Mouse entered and what this is
gonna take is of course our mouse event event first of all I can't forget that taste that
baby right there that's what I'm talking about and what Mouse entered means is this is the
event when the mouse enters the area in our area is of course um where are you mouse panel
right here so this is what's going to happen whenever the mouse enters the mouse panel
so remember up here we put it at white by default so now let's go ahead and change that
actually the first thing we can do is click status bar set the text to something like
you know you enter the area not bad and now let's go ahead and change the color of our
mouse panel so mouse panel dot sets background not bad ground set background and what do
we want to set it to when the mouse enters um just put something noticeable colored thread
or something like that I mean it's pretty noticeable come on so of course you're wondering
all right you have a mouse entered method something tells me that you're probably going
to have a mouse exited method as well well if you guess that then you would be correct
so public void Mouse exited make sure I didn't spell excited I do that sometimes because
I myself get excited about this method sometimes so you know it happens so go ahead and type
mouse event event nothing new so what this happens whenever the mouse exits the window
pretty much in essence so the first thing we want to do is cyst status bar set text
and put the mouse has left the window BAM pretty much make sure you spell the wrong
make sure you put he instead because I'm too lazy to fix it although but easily could just
fix it right there anyways enough of that so now let's take that mouse panel and whenever
the mouse is in the window it's gonna be read so whenever we exit the window it let's go
ahead and set set background we'll just set this to I guess white again I mean why not
make sure we don't got any errors and we did it so all of these five methods right here
these are the mouse listener events but again we implemented for mouse listener and mouse
motion listener lucky for us Mouse listener has five methods which I already did and Mouse
motion listener which is on the that the methods our mouse dragged and mouse moved it only
has two so this one's gonna be a lot easier so in the next tutorial we're gonna finish
up building this program and all we have to do is implement these two methods Mouse motion
listener which are like I said dragged and move and once we do that we can finally run
our program and see what it does and it's gonna be awesome it's gonna be amazing but
for now thank you guys for sticking with me thank you for learning Java with me don't
forget to subscribe to my channel and I will see you next tutorial what is going on everyone
welcome to your 76th Java tutorial and in this story we're going to be finishing up
that program that lets us do Mouse events and well that's pretty much it so in case
you can't tell right here we implemented from two different interfaces which are pretty
much just a class in essence the mouse listener and we did the five methods necessary for
that but we implemented from this other one to Mouse motion listener and these are pretty
much like you can see the motions of the mouse so when you move the mouse like this or click
and hold down and drag that's what registers these events so these are pretty useful for
a good drawing programs and stuff like that so let's go ahead and implement the methods
right now so public I'm like a little comment um these are mouse motion events so not just
the typical mouse arm or they call a mouse listener or whatever so the first method we
need to make is called mouse drag so public avoid Mouse dragged I always heard it was
drugged like Dig Dug Drake dragged anyways just talk to myself here so let's go ahead
and put mouse event events nothing new here and for the body what do we want to do when
they drag the mouse well let's go ahead and there's nothing really we can do except change
the status bar dot set text and we'll set it equal to something like put that back come
on Hoss you are dragging the mouse how original is that I know it's most creative thing I
can't come up with but hey I'm not that creative that's what my therapist told me so I'm just
going to go with that so after that what we need to do is have mouse move and what mouse
move does is called when you move the mouse without clicking any buttons just like I'm
moving the right here and I'm not click any buttons so mouse drag is when you hold down
the button but public void amounts moved is not holding down any buttons so let's go ahead
to Mouse event event and in our body what do you want to put status bar dot set text
and go ahead and just put something like you move the mouse so now we got MOU SEO of course
that's how you spell Mouse so now we got all of our methods that we needed to implement
since we implemented from Mouse listener and Mouse motion listener so now we can go ahead
and run this program and I've down Lee doesn't see one of these things that's boater on Haan
DL er CLA SSH Andi oh there we go make sure your capitalisation does matter it's case
sensitive in Java as you just saw so now let's go ahead and run this program right here and
I'll show you guys what happens if we don't got any errors it'll pop up trust me so here
is our window by default it has the title bar at the top and here's the status bar down
here and here's our mouse panel and so the first thing we see is well let me go through
each of these methods look at the status bar down here I'm going to press down or hold
down the mouse button and says you press down the mouse button now when I release it it
says I clicked at that coordinate because when you press down your release it real quick
it's a click server saying all right if I press down and release it that's a click how
do I get this to the release method well if you click it and drag it and let it go that's
the one that says you release the button so if you click down and release in the same
spot it's a click if you click down and drag it and release it it says you release the
button so now what do we have we have mousepressed mouseclicked mouse released we also have mouse
entered as you can see as I'm entering it it's changing a red is I'm exiting it the
mouse has left the window it's changing white and lastly I have you move the mouse and if
I hold down my button look I'm clicking down the button and dragging it says you are dragging
the mouse so now let me talk through each of these methods you guys should know what
all this means how to create the window by now if you've been watching my tutorials and
let me talk through each one of these remember well I might as well just run that again so
I can bring it up bring it right there so remember when it said mouse clicked it set
the text equal to the two coordinates % d % D which was event get X and get Y and I
know you guys can't see get Y but trust me so look at this click at 10/10 65 click that
30 46 so that is pretty much what it means in for your information the X is left to right
and the Y is top to bottom it starts 0-0 right here and it increases the most right here
it look 279 141 so that is what click thus the mouse pressed is pretty much taking it
right here and when you press it down that's why we were able to get this text you press
down the mouse mouse released let me make sure I can't move my mouse you release the
button so that's why we're able when we let go of our mouse button and of course when
we enter the area it not only says you enter the area for a short bit of time but also
changes the color to red look at this non anandhan oh I entered I'm not I didn't come
in yet oh I just entered again it's red so now what else did we do exit is white it's
the same thing and the last one is dragged which is holding your mouse button down that
way we were able to set the status bar too you are dragging the mouse and moving it without
holding it down you move the mouse look at that so now what do we know how learn in Java
in this little program that took quite a little ways to build we learn how to respond to the
users Mouse events so now we know how to respond to your keyboard events using scanner and
the other stuff we will enter a nice tutorial and also their Mouse events right now so we
can pretty much start building for some pretty cool programs using you know graphics and
stuff like that but for the next tutorial I'm probably going to be teaching you about
layouts how to make super cool windows with buttons right where you want them and you
know more stuff like that but for now thank you guys for watching my mouse event handling
tutorial I hope you learned a little something of course not too much because I mean come
on why would you want to learn a lot I mean learning not that fun so again thank you guys
for watching don't forget subscribe to my channel so glad your subscribers so glad thank
you for your views don't forget subscribe again and I will see you next door what is
going on guys welcome to your seventy seventh Java tutorial and in this tutorial we're gonna
be learning about something called adapter classes now in the last tutorial when we implemented
from the interface like Mouse listener and Mouse motion listener we had to implement
seven different methods and those are kind of time-consuming in sometimes you don't always
want your program to detect all of those different methods sometimes you only want your program
to be able to tell when the user click something instead of drag release move you only need
one of those so what Java did is in order to kind of save you some time they made something
called an adapter class and what an adapter class does is it already implements an interface
and it overrides it with a method body that is pretty much a blank body or pretty much
a body that does nothing any we're saying all right what's the point of that well in
that way when you inherit from an adapter class you only have to override the methods
that you need so it's pretty cool in the sense that you don't have to override all of those
seven time-consuming methods like we did before you only have to override the ones that you're
going to use in the rest whenever you do them they just don't do anything at all just like
we wanted them to so let me go ahead and show you guys what I'm talking about gonna make
two variables for our program again get all your imports right here and the first thing
they're both gonna be private variables I'm gonna put string in details and what this
program is gonna do is it's pretty much just gonna say how many times well you'll see what
it does but pretty much this details is gonna tell what's going to be displayed in the status
bar so that's all you need to know for now private j label and just put status bar or
whatever you named it last time and now we got two private variables the status bar in
the details is pretty much going to be the text that's going to appear in your status
bar so now we can go ahead and build our constructor which is like gooey it's not gonna take any
parameters and for the body let's go ahead and add a title super title since you should
always have a title get their real name it toodle I mean why not might as well next we
can go ahead and build our status bar which is the only thing we're really gonna want
up here in the window so wow I really miss that up status bar and set equal to Nu J label
and just have the text by default but this is default it's just gonna be the first thing
that appears um after we start doing the stuff this is gonna disappear but now let's go ahead
and add this to the window we'll pad status bar and of course go ahead and put like borderlayout
dot south and now there we go it'll appear at the bottom of the screen if you put south
we'll be going over that later and now we need to add of course the mouse listener just
like that add mouse listener in the parameter it takes here is an object of the class we're
gonna build and we're gonna be building a new class called new mouse class and don't
forget your empty parameters for that so make sure we didn't miss oh I know we're getting
an error because we didn't build the class yet so let's go ahead and create a class called
Mouse class and with this Mouse class we'll just go ahead and throw private class Mouse
class extends so we need to build a mouse class because that's what the parameter it
took in in our class we're gonna pretty much define how to handle these Mouse events and
we want it to extend Mouse adapter in this adapter class that we're extending from this
is the class that is built in a Java already so this can be custom whatever you named but
this has to mean Mouse adapter you have to type it just like that and again this adapter
class is gonna allow you to overwrite only the methods that you need to overwrite so
let's go ahead and first the only thing we're not gonna want to be working with is public
void mouse clicked that's the only thing we care about when they click we don't care when
they drag or anything else like that so put mouse event event oh my god can I not type
I mean seriously Mouse event event and let's go ahead and type what do we want to happen
when they click the mouse well let's go ahead and take those details right up here that
we're gonna have and put details set it equal to string dot format and let's go ahead and
put something it takes two arguments but you clicked percent D so it's gonna say you clicked
percent D and for present D what we want to do is take that event which is pretty much
means when we click and it put get click count just like that BAM already filled it in for
us so it's gonna say you clicked five six 18 whatever doesn't fill in in the end yet
so now we're gonna have to make a if statement and we're gonna have three parameters to finish
this detail variable so if event and here's another kind of confusing thing that is maida
down and ever saying all right what the heck is mated down and what's it mean well whenever
you're building a Java program you don't really know what kind of Mouse the user is using
there's three kind of main Mouse's there's a one-button Mouse like on the Mac a two button
which is like old classic or a three button Mouse in the third button is that scroll wheel
so in order to distinguish the types of buttons that they have you just can't assume that
they're using all your users are using the same one so if you ever use the Mac then how
they make up for this rice mount I mean the right mouse button is they have something
called meta which is called the command or Apple key that they can hold down and when
they click the mouse that simulates the right mouse button or they can hold the Alt key
and when they click the Alt key and click their mouse it simulates what is it that all
is the center mouse button so that is how you can tell I mean for now when we click
the right mouse button it's gonna work perfectly but this pretty much is just for people who
use special like one button or two button mouse is if you have a third three button
Mouse then this is pretty much what does so if meta button is down or is meta down take
your details and I know I'm probably just going to explain it a lot easier just by showing
you what the program is while ads at the end of that details variable so you clicked five
it will say for example and then go ahead and what do we want append alright there we
go with rights mouse button so if meta is down what does mean if they click the right
mouse button or if they hold down that meta key is going to say with the right mouse button
else if what else do you want the test event is alt down just like that let's go ahead
and first put that back and details plus equals with sensor mouse button and the cool thing
about this else by default you don't have to test anything because you know it's just
a regular click down with left mouse button so that's how you distinguish between the
right the center in the left Mouse one for different types of mice and I spelled details
wrong so and now I spelled it right so that's all we have to do so now we have a variable
called details and it's a string it starts with you clicked and says a number and says
with the right mouse button either wideout by either why don't I just put with embarrassing
and then I pretty much this is the beginning and one of these is the end of the string
and now all we have to do is add this to the screen so put status bar set text and for
the text we're just going to write that details variable so the status bar is going to set
equal to the details variable now we can go ahead and run it and I can show you guys what
it does so says this is default what you put that by default and I'm gonna go ahead and
click my left mouse button now look click 1 2 3 4 5 and then if you move it and click
your center look what happens Center center clicking with my center or in other words
my scroll wheel and then go ahead and click here with your right and if you go ahead and
move this then it's actually a new event so if you want to make your things go up you've
got to stay in the same place um if you're moving it then actually is a move event so
that's why it resets but that's your basics of how to distinguish between the right the
center and left and also how to use a mouse adapter class 2 only overwrite the methods
that you want again we only over wrote one method and we then have to overwrite all seven
for our program to work so that's the power of a mouse adapter class but for now thank
you guys for watching don't forget to subscribe and I'll see you next time what is going on
everybody is Bucky and I am kind of sick right now so if I sound a little stuffy or I blow
my nose there in this tutorial then I guess you guys are just going to have to deal with
it but I'm going to teach you some Java in the mean time so let's go ahead and get started
actually we took a break from gooeys for the next couple toriel's I'm going to be teaching
you something about files now you can create and modify files in Java just like most other
programming languages if you guys are saying all right what exactly is a file useful for
I'll give you guys the quick 1/2 of the main thing it's useful for so far we created computer
programs that use like variables and stuff like that like store the number 5 and X or
store like the name Bucky and um Y or something like that but once they end their program
then all of these variables and methods are all gone so if we want to like keep it in
a database then we actually need to like keep it in a text file so that way when they run
the program again then all their information is there and they don't you know it's not
just like a program that you can run once and it forgets about it that way it can store
your information on the computer then when you run your program again we can automatically
load the values and stuff and get them again so what we going over we're actually building
a program later on but for now let me get typing some code before I lose you guys so
let's go ahead and import the file class so Java dot IO dot file or you can import everything
well that's good enough and what the file class does is this is real simple it doesn't
create files or anything all this class does is give you info about files like whether
it exists um has a couple things like length the path the parent stuff like that right
now what we're going to be doing is testing whether a file exists on our computer and
this is useful for things like if you're creating a program to make a file for the first time
if it already exists you don't want to create one again or else is going to delete it so
you want to see if it's there first and then if it's there you can migrate to it if it's
not there then you can create it so simple enough what you do is type the file class
we'll name X you can name it whatever you want and then type new file and what this
does is the constructor takes the path of the file so I actually know that I have a
file in C and you actually have to do this capitalize your director whatever it is and
if you do well I'll show you guys test is in the directory and I have a file named Greg
dot txt right there so normally this would work and that's a valid of path name but the
thing is you know when we're working with strings that that little backslash is the
escape character so this is whenever you put this this is going to try to escape this T
and put a tab in G and stuff like this so what we need to do when we're working with
path names and windows is actually need to put two backslashes like this and when we
do this it views this is only one backslash because remember whenever we do backslash
with a string then it's going to try to escape it because that's just how windows is so actually
whenever you do path names do two backslashes if you're working with windows if you're working
with Linux I think is a front slash bar I'm not sure so now now that we have this file
that is stored in the variable X we can do a couple things with what we're going to be
doing this tutorial is simply check if it exists so along with many of the built-in
methods we can do if X well we just did BX and we'll just shortcut it right there if
X exists and if we hover over this it says test whether the file or directory pretty
much exit exists so if Greg dot txt exists then what do we want to do let's go stuffy
nose system.out.print line and we can print something like we can actually have X dot
getname and that'll be pretty cool and we'll just add something like exist so it will be
like Greg dot txt exists or if it doesn't while we'll insert a file that doesn't exist
later so I can tell you guys what it does and system.out.print line and we'll say this
thing doesn't exist and make sure you almost type everything wrong so which I did so now
we can go ahead and run this program and see what we get it says Greg txt exists and I
probably should put a space right there but I didn't and what is going on is this if you
look at my computer right here and you go to see test and this is a C and the test is
right here a file named Greg dot txt does exist but what if you had a file name in here
like um like that see that's what I could think of off the top of my head if you run
this and click OK it's going to say this thing doesn't exist so that is how you can pretty
much look for files in their existence using the file name and the exist properties in
I also taught you another one the get name pretty much to get the name of it and that's
a quick cute little tutorial and let's see if there's anything else I didn't cover and
that's pretty much it for this tutorial and in the next story I'm actually going to show
you guys alright we already test if it existed or not if a file doesn't exist then how do
we create one I mean of course we can go to our computer right here and create a file
like this but we want to create one in Java program in language so that's why I'm going
to be teaching you in the next tutorial so for now thank you guys for watching study
this and again review one more time you create an object from the file class and is this
constructor it takes the path the name of the file and remember when you're working
with strings you got to escape the back then you test whether it exists if it does exist
run load code to do anything and if it doesn't exist then you usually in here you want to
write code to create the file but for now I just want to let write a little prompt saying
this thing doesn't exist so thank you guys for watching I hope you learned a little something
don't forget to subscribe and now we'll see you next time what is going on everyone welcome
to your 79th java tutorial and in this story I'm going to show you guys a real quick way
on how to create files using Java so pretty much if you don't have a file created and
we could check for a file using what we learned in the last tutorial then we usually want
to run a program like I'm going to be teaching it today or in this tutorial to create a file
so the first thing we need to do is import Java dot util and just go ahead and import
everything from there and now let's go ahead and the first thing we need to do is build
a formatter variable so we need to make a final varial variable format sur type X and
you can name it whatever you I just named everything X because I'm too lazy to type
anything that's more than one letter but I'm going to name an X and what this is going
to do a formatter variable pretty much outputs strings to a file and it's pretty much like
printf but instead of printing to the screen your print a file so this pretty much outputs
a string and let's go ahead and I didn't teach you guys except I didn't teach you guys about
exception handling but I'm going to have to do some right now I'll give you guys the quick
one to you pretty much in exception handling you write try and then you're right what do
you want to try and what we want to try is this we want to set X equal to new formatter
like that in this is pretty much the same thing as instantiated the variable and same
line but we want to try to do it down here in as the constructor for this is pretty much
the same things put final formatter x equals new formatter and again as a constructor it
takes the path of the file so what file do we want to output to well I'm going to make
a file call like Fred dot txt and here's the thing if there is no file to output to then
automatically creates a file on your computer for you so that is how you can create files
on your computer on your computer using formatter so again let's go ahead and now that we created
a file we want something to show up in the command prompt tell us that we did it so system.out.print
line and let's go ahead and put you created a file so now if we try this and it's successful
then this stuff is going to happen however if we get an error then and again I'll be
going over this try and catch you later so don't worry too much if you don't extend it
understand it however if you get an error in this an error and exception is the exact
same thing they just had to name it exception because they had to be different if you get
an error then what do you want to happen system.out.print line you got in air so this is pretty much
your own way of making custom errors so you know in if we had an error before then our
program would just start running and we're get a bunch of error messages on our command
prompt but this is a way if we you get an error running your program that your program
doesn't just automatically shut down and you can actually handle those errors aka exceptions
why they name exceptions I don't know it's annoying but they did it so I know I didn't
teach you guys about try and catch it but it's pretty much try this code if you get
an error do this BAM you just learned it in like half a second so what we're going to
do is let me show you guys right here on my computer if I search in here and I search
for Fred I don't have anything on my computer named Fred or anything like this but now we're
going to run this program right here and it says you crested a file and I mean to put
created but I did it okay make fun of me big deal so did we create a file I mean all we're
getting is this command prompt right here I don't believe you created a file Bucky well
let's go ahead in this computer here in search again and now let's search for Fred and make
sure you only have one day and bam look at this we now have a file created if we click
on the properties is created see users Gregg workspace Bucky and that is pretty much right
here and if you're working with Eclipse you can highlight Bucky click refresh and under
your jRD systems library you get that Fred text and I know I just talked real fast and
you guys didn't really comprehend that probably but I should probably slow it down pretty
much what I'm saying when you create a file you created it in whatever directory your
program ran from so if you have this Java program right in your hard drive then it's
going to create this text file right on your hard drive if you run this Java program from
a folder called um like apples or something then it's going to create this text file in
the folder called apples and you can also make an absolute path like this and you know
well you guys know how to make absolute paths from let pass for my last tutorial to create
that file where you want it like in a folder or something but again to recap one more time
we use this final format or X variable and what this did is pretty much stored this formatter
well it just pretty much created a variable and again the formatter is pretty much a way
of putting strings into a file or pretty much writing stuff in a file so right now our file
is empty but we're going to be putting stuff in it later and again if it couldn't find
the file then it created it and that's why it created it and again if we made an error
then Java makes us handle it in some way so that's why I need to teach you guys about
try and catch um you can catch different types of errors like if a user tries to divide by
0 or if they are supposed to input a number and they actually input letters or something
then those are the kind of errors you usually want to catch and you usually want to send
a message back hate it in put a number no letter but for now we'll just output you got
an error so that is your real quick tutorial on how to create files we're actually be clearing
this up and no makes sense when we create a program but and also your real quick tutorial
on exception handling try and catch so thank you guys for watching don't forget to subscribe
if you're a little confused don't worry I'll clear things up for you in Mexico tutorials
but again thank you don't forget subscribe and I'll see you guys next time what is going
on guys it is Bucky and welcome to your 80th Java tutorial in this story I'm going to teach
you guys how to add data to your files so in the last story we pretty much learn how
to create an empty file not that useful so let's go ahead and put something in it in
this tutorial and hold on one second I got to itching right here I felt good I got it
all right so let's go ahead and import a couple things we need to import Java dot IO and let's
go ahead and import everything from there and as you can see I made a separate class
outside my main class and I just named it create file because that's where we're going
to be doing creating files that's why I named did that so next let's go ahead and put Java
dot Lang we need some crap from in there and lastly let's go ahead and import Java dot
util I'm not exactly sure what we're going to be building in this tutorial but this should
cover everything so without further ado let me think what do I want to do first well the
first thing we need to do is do a private formatter variable and I'm going to name on
X as usual and what this is going to do is hold a formatter object and let's go ahead
and make a method to pretty much create and open a file so let's go ahead and put public
void and we'll just name it Open File you name it whatever you want it's a doesn't like
any parameters because they're just going to open a file now in this method what we
want to do we got to have our try and catch block like always so let's go ahead and try
and set X equal to new for Matt's or object in go ahead and write a file name um make
sure you write one that you don't already have a file or else you're going to overwrite
it so I don't have a file called like Chinese text you probably don't either so you can
use that so go ahead and try and set this file equal to X if you can't let's just go
ahead and catch an exception catch exception II and this pretty much means if you get an
error what do you want to do let's do system.out.print line just put you have an air air no error
there we go alright so now when we ever recall this method open file it's pretty much just
going to set X equal to this file it's pretty much just going to give us a file so we can
use so now we have a file that we can use so what we want to do to that well we want
to add a record to it so let's go ahead and put public void add records and let's go ahead
and this is going to take any parameters either and adding records is the easiest thing in
the world all you need to do is put the file you want to add to which is X because remember
we set equal to Chinese dot txt and what you do is the method is format and this pretty
much is like printf for files and now again how familiar is this the format and the arguments
so for the format let's let's insert three strings the first one say we're making like
a user account the first one will be the ID number so we'll put like 20 it's my favorite
number and the second one will be my first name which is Bucky and the third piece of
data will be Roberts which is my last name so I looks good right there so now what this
is going to do is insert three elements in the same line it's going to say 20 Bucky Roberts
it's going to insert that in our file whenever we call this method add records and the last
thing that we want to do is write a method to close the file so go ahead and write public
void close file or name it whatever you want but that's pretty easy to remember and why
do we need to put oh I would show you guys what the thing is X close and closes built-in
methods that's why we can use this and that's why we need to include all these so we can
use this close method but now let me tell you guys why we need to close this file it's
pretty much like a housekeeping thing whenever we exit our program windows usually and I
say usually closes our file from writing so nothing else can write in it but sometimes
remember every operating system is different sometimes depending on your program it can
leave the file open it can lead to some errors so you always want to close your files it's
good programming you always want to do it I mean you can probably get away with not
doing it but if you ever get job as a Java programmer there are going to be pio to you
if you forget to close your files it's probably going to cause some errors later down the
road so we're pretty much recap talking about something different now we're pretty much
got three methods in here we need to open the file we need to add some records into
it and then we need to close the file because we're done with it we don't want anyone else
messing with it so go ahead and in your main or whatever your main method is just go ahead
and well we don't need to include anything but what we do need is to create an object
and I named my class create file so create firearms can name it like G or something new
great file doesn't take any parameters we didn't make a constructor and remember we
have three methods you go open file right there G would rename it add records right
there and so now we open the file we added the records and now what we need to do is
close that file because we're done with it and now once you go ahead and run this program
nothing happens but what it did is created a file on our computer so if we highlight
source are actually Bucky and put file refresh it's pretty much going to refresh our computer
so now you see we get this Chinese text file that we just created and the cool thing about
eclipse is is actually a text editor as well so double click this file and I'll show you
what's in your file and that is 20 Bucky Roberts this one is just the line number don't mind
that that eyes is actually in the file so in this text file we have 20 Bucky Roberts
so now let's recap one last time what we did in create file that Java is pretty much the
meat of our program we created a method to create a file and that was open file right
here and we needed a try-catch because of Java and we said all right we're going to
look for Chinese text that file we want to write on if we don't have it already then
create a new one so we can write in it and that's what X new formatter does so now we
have a Chinese text file so we can write text in it so what do we want to write in it well
we just wanted to add a record we gave myself like an ID number Bucky Roberts and if we
had to make like a system of like I had to make like a library for school or they have
like credit card numbers or anything where you need a ID number you'd add a record after
record after record and you called this method a bunch of times and I just moved that and
then that's how you get like a database on your computer and then after that you just
close your file so you can't mess with that anymore and that's pretty much it so uh thank
you guys for watching I hope you learned a little something and hopefully I got this
so you guys can see it but oh well I'm done with Orion noise so anyways thank you guys
as it just said don't forget to check out my giveaways and don't forget to subscribe
so I'll see you next time what is going on guys welcome to your 81st Jeff torial and
we are finally going to learn how to read stuff from a file so as you can already see
I created this Chinese dot txt file and I added three more records to it so the first
one is Bucky the second one is the ID number and then a name ID number name ID number name
so it has four records and a record is pretty much um a row of data so now let's go ahead
and figure out all right we learned how to open the file we learned how to write this
stuff on this file but we don't know how to access it from a Java program so that's what
I'm going to be teaching you today and the thing I didn't tell you is before we use scanner
that scanner object to read from our keyboard using system in but we can actually use scanner
to read from a file as well so let's go ahead and first make a private scanner variable
so I'm going to name mine scan or X the cursor out of the way and now let's go ahead and
just make a method to open the file first so public void open file and let's go ahead
and put try to open the file so we're going to set X and again here's where we're going
to put a new scanner and instead of putting a system in here which would be the keyboard
just put new file and put that Chinese dot text I spell it right yep and now instead
of reading from your keyboard it's actually going to read from this Chinese text file
right here so that's a neat little trick that you can do and if we get any errors let's
just go ahead and catch those errors exceptions e and now what do we want to do let's go ahead
and write system.out.print line and let's go ahead and print something like good nods
fine file looks good enough so now we have a method to open the file easy enough so what
method do we need after this well this tutorial is all about reading files so let's go ahead
and make a method to read the data from that file so public void just name it like read
a file or something like that and what we want to do whooping cough the first thing
we want to do is create a while loop and go something like this while I'll put your file
name which is X and then we one-putt has next and what this is going to do it's a built
in method it's going to make a while loop and it's going to keep going until it reaches
the end of the file so it's going to loop through this and has next pretty much means
if you hover over it on another input so as long as there's something there it's going
to continue as soon as there's nothing there and it reaches the end of file this is going
to be false and this while loop is going to break out so that's why we can work right
while X has next it pretty much means while the file X has something in it so what we
want to do is you see each of these records has an ID number a first name and a last name
so we're going to want three variables to hold those string a and let's go ahead and
copy that actually we're going to put string a equals X dot next and what this means is
pretty much it's going to do this well let me finish writing all my variables that will
tell you guys what does string a equals next and this is going to be B and C what it's
going to do is this it's going to loop through the very first thing it sees and it's going
to assign 20 equal to a then it's going to assign Buckey equal to B and Roberts equal
to C and then we're we're going to put after this is we're just going to go ahead and print
those out so system.out.print actually printf will be better so let's go ahead and print
just three string variables so percent s space percent s space percent s in those variables
of course are a B and C so now what it's going to do is after it gets those three it's going
to print it out in might want to put a new line there and then it's going to go all right
next 60 a Joe is B in Delta C now I'm going to print all those out and it's going to keep
printing those out until it gets to the end of the file which is right here and then it's
going to break out that while loop so that's actually the only thing we need in this read
file method right there so now we just need that one last method and what this is going
to do is close the file so public void close file just like that and all we want this to
do is close that file X and now looks like we got everything check for errors and now
we can go ahead and start putting it in our main so let's go ahead and put read file I
didn't intrude probably yep R equals new read file don't take any parameters and now let's
go ahead and put our open file r dot read file and r dot close file right there so now
what this is going to do is call open file to open it read the stuff from it and close
it when we're done with it so now let's go ahead and run this and we'll see what we got
we got pretty much exactly what is in our text file xx Bucky Roberts it just printed
all this out it stored this in a B C printed it out then it got three new variables store
this in a b c print out ABC ABC and then it got to the end of the file and a broke out
so again let me make sure that's in there and i'll tell you guys how this worked one
last time we made a new scanner variable x because remember we can use scanner to not
only read from our keyboard also read from files we then made a method to open the file
right here in which how we did that is we pretty much set this scanner and said we're
reading from this file Chinese dot txt which is right here and we pretty much set the file
equal to X this error is a handling because we programmed it awesomely and we don't need
air handling because we don't have any errors and now we made a method to read the file
or pretty much read the data in the file and how we did this is we pretty much set variables
equal to each one of these items or words or strings choose your word and then when
we got three we printed it out on the line and then we got a new line got three more
printed it out three more print it out and it does that until it reaches the end of the
file using the hasnext function and last but not least we close the file because you know
it's just good programming so that is that for how to read a file in Java um that's it
I mean nothing else to say except thank you guys for watching don't forget to subscribe
and I will see you next time what is going on guys welcome to your 82nd Java tutorial
and in this tutorial I will be teaching you guys about exception handling now what exception
handling is pretty much a way to handle errors now an error and an exception are the exact
same things so pretty much why would we want to handle an air well when the user does something
they're not supposed to do we don't want to get in error or else the program which is
shut down and it will stop running we want a way to do something like if they like divide
by zero or click something you're not supposed to click then we don't want the program to
shut down and we want to be able to tell them they're wrong and tell them to fix their mistake
so let's go ahead and I'm going to use scanner so I'm going to go ahead and import Java util
dot I'll just import everything because I'm lazy and now let's just go ahead and let's
begin making our system so scanner I'm going to name it input equals new scanner and as
parameters type system not in and now we have an input variable called scanner so what do
we want to do well let's just go ahead and make a little program 2/2 number so system.out.print
line and go ahead and print like just give them from like enter first num and now for
the first number in to number one equals input dot next int BAM and now whatever they type
for the first number is going to be the let's see numerator and now let's go ahead and copy
this duplicate it and go ahead and set this equal to num2 and give them a new prompt and
our second number and now let's go ahead and make a new sum equal to num one divided by
num2 so now we got a little program if we go ahead and print out the sum so system.out.print
line and go ahead and print the sum and we'll run this and test it make sure it's working
so as I say number one oh here we go so now let's go ahead and test this real quick and
see if it works okay and our first number 12 and our second number three and it gives
us an answer or four so this works fine but if we run it again and we go 12 in for a denominator
we give a zero remember you can't divide by zero it gives you an exception like this so
this pretty much shuts down our whole program and what if we build a huge program for like
analyzing stock market and that was awesome and then some idiot tried to divide by zero
and the whole thing just shut down well that wouldn't be good so we need a way to kind
of handle these errors until the user R right don't do that but we're not going to shut
down program we're actually going to fix your problem well in order to handle exceptions
you need to learn something called try and catch what try and catch means is it pretty
much like pretty much just like it is you try a bit of code and if you have an error
you're instead of shutting down your program you're going to catch that error and do something
with it so here's how you do this go ahead and put try and that's a key word in in your
curly brackets fill in the code that you want to try so I'm going to try this so in my try
block right here I'm going to write the code that I want to try so I'm going to go ahead
and try this and what this tribe means and what this code block means if you have an
error anywhere in here what we want to do is catch this error so go ahead and write
catch after this and there always needs to be a catch after a try and there can be something
called finally - but for now remember there always needs to be a catch after a try in
for catch what do we want to catch we're going to want to catch all errors and in order to
catch all air errors remember they're called exceptions type DX cep tion ii and why do
you need to write this there's actually different types of errors like arithmetic err I think
one is called like input error or something like that but the like if they make a math
error or if they click something wrong they're different types of errors but if you want
to catch all of them this means if they do anything wrong at all this is just the broad
exception class right here so let's just go ahead and catch that in the e is pretty much
if you want to print out or something like that but if they have any errors what do we
want to do let's type system.out.print line we'll just type something like um you can't
do that and let me fix this so what this pretty much means is try this if you have an error
do this if not if you try this and it works good then this is never going to run at all
so let me show you that I'm going to try this and I'm going to enter 12 3 BAM for this catch
right here never runs it all but if I run this again and I get an error and it says
12 0 it says you can't do that in my rest of my program if I had any more code down
here it would continue to run normally so what if I actually wanted to build a program
so if they entered the wrong thing then I could pretty much have them do it again over
and over again until they enter the right thing well let's go ahead and this is really
simple but it's x equals 1 so I'm going to pretty much have a do-while loop and I'm going
to do this do the pretty much the whole trying catch so catch this and want to catch that
too I want to do the whole trying catch while X that we just made that's equal to 1 is equal
to 1 so I'm going to do that over and over again while this is equal one but the thing
is if it continues to execute successfully and I'm going to set this equal the X equals
two so as soon as it runs successfully I'm going to set this X equal to two and it says
all right that's the point in time when I stop so here is a way where we can keep running
this program over and over again until we get the right input so if you had a really
dumb user the program would look something like this run sample all right and our first
number 12 um 0 oh you can't do that try again ok and our first number again let me expand
this a little bit arm 43 of 0 oh nope you can't do that enter first number again and
it's going to keep trying and trying so let's go ahead and do this right so put like 40
and 10 BAM 4 so that is how we can keep kind of prompting the user to do it correctly and
let me review this one more time how I did this is I pretty much put all right what do
I want to do over and over again well I want to try this code and if this code runs successfully
I'm going to change this variable but if this code does not run successfully then I'm going
to print out an error and this never even runs since you get an error in this in this
and when it tries to divide the sum this is where the error happens right here on this
line so this X never gets set equal to 2 so that is why X is still equal to 1 and that
is why the do-while loop is able to run over and over again but as soon as this whole thing
okay good good good good good good BAM we finally made all the way X equal 2 and then
it looks all right is X equal to none X equal to 1 nope all right then we can finally exit
this bad boy so that's pretty much your basics of track catch - pretty much I mean it's pretty
simple try this if you get an error do this I mean how easy is that um there's actually
a little bit more to it and I'll probably be covering the next tutorial but for now
thank you guys for watching don't forget to subscribe and I'll see you next time what
is going on everybody is bucking welcome to your 83rd drop tutorial now we're actually
going to be getting back with gooeys and learning about layouts again after a short break learning
about files and exception handling and stuff like that so in this story I will be teaching
guys about something called flow layout and I know we used before but this time I really
want to show you guys the details up so let's go ahead and I'm going to make a program that
has some buttons on the screen and depending on what button you click depends on what the
full layout is set as so let's go ahead and make fruit three variables they're all going
to be private so private J bun and I'll just name this like left button and go ahead and
copy that and we're going to name one CB and RB in this gonna stand for center button and
right button so I change this to CB this is going to be Center button and this is going
to be the right button right there so now that we have three buttons or at least three
variables what we need is a flow layout and a container object so go ahead and make a
private flow layout and you can neighbor is anything you want I'm going to name and layout
because it's easy to remember and also we need a private container object and I'm going
to container just like that make sure I spelled it right and looks good enough so now we can
go ahead and build our constructor and my classes name layout so I'm going to name public
layout for my method and this will be my constructor so again first thing we do since we're extending
jframe we can go ahead and build our title using super and I'm just going to name the
title because I'm that original and next we need to go ahead and set our layout object
that we created right up here with flow layout will set layout equal to new flow layout and
this pretty much means we want our layout to go from when we put things in there like
buttons we want to be range from left to right kind of like Microsoft Word would do it so
now that we made our new flow layout and we're actually going to be changing this depending
on what on WordPress but for now that's good so now after this we need to set our container
object equal to something soap container and set this equal to get content pane which pretty
much means get the bulk of your window and this is pretty much just so it knows word
put the stuff so now let's go ahead and set that layout so we'll set the layout equal
to layout and this pretty much means layout equal set layout new flow layout so I mean
simple enough now let's go ahead in we can finally do stuff with our buttons this is
pretty much creating a window now let's add some stuff in it so first thing we need is
equal left button set the equal to nu J button and give it something to put in the button
and what goes in your parameters for nu J button is the text that's going to appear
on the button so my but I have a button on the screen now that says left on it so now
let's go ahead and add that to the screen I just kidding I didn't have it oh but now
I do have it on my screen so as I just added it so now we said it gave us some content
and we added it to our screen next let's add some functionality to this so we do this by
l.b dot add action and look at that it already made it for us add action listener now we
can either write an object in here or to make things easier let's use an anonymous inner
class and you see said method arm well it doesn't give us yet but it needs to use a
new action listener and this is the class and this is again anonymous inner class if
I change that and now if you highlight this it says it must implement the method action
listener action performed so if it told us to do it let's just do it we're easily influenced
so let's just go ahead and put something like well this better method first before we start
thinking about that public void action performed and for our parameter type action event event
and now we can go ahead and add some functionality to this and what do we want to happen whenever
we click this left button finally after all of this adding we finally get to the meet
right here the body of our action perform method what do we want to happen when we click
the button well well first of all we want to change that layout since we click the left
button let's go ahead and set the alignment and got a nice sort of shortcut right there
and what you set it to is flow layout dot left and what this is going to do is take
your entire layout for your window not just this but not just this button but the entire
layout and change everything to left alignment so by default it's going to be in the center
of the screen and when you click this it's going to be on the left so next the only thing
we need to do is put layout that we just change but layout container which is pretty much
our container and go ahead and pass container in this and this pretty much means rearrange
everything depending on what our layout is so now we did this for the left now we can
just go ahead and do this two more times and make it real easy to copy it so I'm actually
going to add a comment so I don't get confused just pull left stuff in here in here so now
let's go ahead and copy everything included in that comment and put Center stuff in here
and change this the center button add center button to the screen add action listener for
center button and change the flow layout to Center which is the default but again we need
to do that now we need to do it for the right so let's put right stuff in here and add the
right button to the screen add an action listener for the right bond again center button and
a right button were a lot quicker so and not that easy house and now let's go ahead and
run this program so what I tell you guys what I did after I run it I know you guys are probably
getting antsy so let's go ahead and run this baby and here's what I got whoa forgot to
change my text on my button quite embarrassing so this is left stuff and I put left-center
again don't forget when you create your buttons to do that embarrassing so let's go ahead
and now oh my God look at this program here is my window that I just created right here
and as you can see it doesn't matter if it resizes it or not it automatically rearranges
from left to right and if you run out of realist acts under each other so now if you see if
I click the left it changes it to left right changes the right and center chooses the center
and you can see even if we do this then we get right left center so it doesn't really
matter how big your window is it just knows the flow layout depending on the size um where
do I want stuff arranged so that's pretty much the basics of flow layout and one more
time let's see how much time I have on my tutorial about two minutes so let me go ahead
and walk you guys through this we created three variables for the buttons and we actually
created a flow layout and container object because we needed them this is just housekeeping
stuff for creating a window in your constructor and let's just go over one of these we added
pretty much some we set the left button variable equal to a button since you know why not then
we added that button to the screen and then all of this pretty much is just saying all
right what do you want to happen when you click this button and what did we want to
happen we wanted to change your layout and then we want to just reset the container using
that so and then we just did it for the center and right button and here's my main in case
you forgot I included a Java X swing because that's the window and well you can see all
that you guys already know what this does already hopefully if you watch my tutorials
so that is that for this tutorial thank you guys for watching don't forget to subscribe
and I will see you next time oh by the way we're going to be creating game soon so uh
you know just thought I'd let you guys know but anyways thank you guys for watching and
uh don't forget subscribe what is going on guys it's Bucky and welcome to your 84th Jeff
tutorial and this story I'm going to be going over graphics because well my ultimate goal
of these tutorials is pretty much make computer games and we can't make a computer game without
sweet graphics so I'm finally getting into graphics I decided to abandon flowlayout and
stuff for now because you know this is just much more exciting if we need any other layouts
we'll go back to them but for now I want to start making some sweet graphics on the screen
so let's go ahead and do that as you can see I made a new class called peach because well
it'll be quite honest just because I like peaches and it needs to extend jpanel because
we're going to be using that so don't forget to have all your imports now let's go ahead
and get started with making graphics on the screen now in order to make graphics on the
screen we need a method called paint component and this paint component method is used to
draw graphics on the screen so let's go ahead and put public void paint component and for
a parameter it takes an object from the graphics class so graphics and I'll just name a G you
can name it whatever variable you want in this parameter right here this G this is going
to be used to like set the color and we manipulate this object depending on what we want to draw
but don't forget that you need this method called paint component this is built-in method
um that's inherited and this is the method used to create all graphics on the screen
so now that we have this method we can automatically start creating graphics so let's go ahead
and get to it the first thing we need to do is just one other housekeeping thing we need
to put super paint component in just fill in G right there and what this is going to
do is it's going to call the super classes remember this is how you call super classes
methods we want to call the super classes pink component method and you just need to
do this because there's stuff in the superclass that we need to use just for housekeeping
stuff and pass in your parameter or whatever it was here and that's the only kind of confusing
thing that might not be self-explanatory so don't forget that so now let's go ahead and
get started the first thing we want to do is set the background color for our screen
so go ahead and write this which means this object set background and bam look at that
now is a parameter for your background color you just take a color now Java already has
built-in constants of colors which is pretty much all your basic colors but for now in
order to get constant just put color dot white because we just want a plain white background
so now we pretty much have on our screen a white background not very exciting but we're
going to change that soon in order to draw objects on the screen you first need to tell
your object or your graphic object what color you want to use to draw with so let's go ahead
and put G set color and is our parameter just like set background it takes a color constant
now let's go ahead and set our color to color blue and make sure blue is in all caps and
that means constant in Java so now that we have a color for our graphics object we can
begin drawing a rectangle on our screen so the first thing I'm going to tell you guys
is how to draw a rectangle and to do that it's fill ret and what this is going to do
is it's going to give you four parameters and this is going to draw a rectangle a solid
rectangle on the screen so the first parameter it takes is the X parameter and Y now this
top left corner of your window is zero zero in from left to right this is the X from left
to right and Y is up and down so let's go ahead and make the coordinates x and y and
you should know this if you ever took a math class in your life so at 25 25 which would
be somewhere around here 25 over and 25 down that is where we want the top-left of the
rectangle to be now how why do we want the rectangle to be let's just go ahead and put
100 pixels wide by 30 pixels high so now we got our color and our coordinates of our rectangle
right here so now we just have a blue rectangle chillin on our screen if I want to tell you
guys something else you can do next we can go ahead and put set color and we can set
the color again or pretty much reset the color and if we want to have a custom color that's
not a constant like our own RGB values and RGB means red green and blue just go ahead
and put new color and as our parameters you type in the RGB value you want so let's put
like 190 commas 81 comma 215 make sure none of these values go above 255 and this pretty
much means all right how much red do we want it 190 red 81 green and 215 blue what color
this is I'm not sure but I know that it's going to be some weird color but again if
you want custom colors then that's how you create it what I usually do is I go in Photoshop
and mess around with the RGB value but you know that's not important right now so now
let's go ahead and put this rectangle or draw rectangle Shoji fill repped and again we have
to fill in all our parameters so where do we want to we can't have it start in the same
place so let's have it like start at 25 over but we'll have this one go 65 down since this
one ends at 55 again this is the Y and this is the height so this will give us 10 pixels
of padding and you can mess around with this on your own and we'll make it the same width
and height for you no it's just nice to be constant get it constant I just made it up
and it's not even a joke really but anyways now we have two rectangles chilling on the
screen so the last thing I want to show you guys is that not only rectangles but you can
actually draw text on the screen so let's go ahead and change our color one more time
just because we don't want everything the same color on the screen it's going to be
boring set color we'll just change this color to like red or something using color dot red
is our constant and now let's just go ahead and this is how you draw strings or text on
the screen G draw string how easy is that now your iterator is pretty much what do you
want the string to say and this is some text and make sure you spell text wrong or else
it won't work I'm just kidding and now where do you want to appear well let's make a x.25
and let's see 120 that should be below everything so now we have a rectangle right here a rectangle
under it and some text under that so now I already did this all I forgot I did this all
but pretty much make a new frame with your title set the close operation don't forget
to do that and then make object of your class whatever you name to add a peach class so
I named P add it to the frame set the size of your window and make sure you set the visible
to true so it actually appears and now if we go ahead and run this program you see that
we created two rectangles that were filled in because we use the method foe rectangle
and again if I do this you can see that the coordinates for the first one the blue one
were 25 over 25 down this is a hundred wide and 30 pixels tall and next we set the color
again and we give it our custom color evidently this is some kind of purple color and this
has RGB value of 190 81 215 and this is 25 over 65 down and that's your top left point
right there 100 wide and 30 tall and next we just pretty much made some text on the
screen and this text started at 25 over and 128 pixels down that's where your text started
and then you pretty much just draw drew a string on the screen using the current color
of your G object so that is pretty much the basics of how do you make simple rectangles
of course there's more stuff we can do but for now just sticking with the basics in the
next story I'm going to show you guys how to draw some sweet graphics and uh well they're
not gonna be that sweet but they're going to be a little better in this one so thank
you guys for watching now don't you subscribe and hopefully I'll see you next tutorial what
is going on guys welcome to your 85th Jeff tutorial and in this tutorial I'm going to
be teaching you guys about something called je color chooser now we're going to be building
a program to make a box pop-up and allow the user to pick their own colors and we're going
to set this color equal to a variable and just going to be doing some cool stuff of
it but this is a really useful application and we're going to be using this a lot from
now on so you know it's going to be good so what we're going to have pretty much on the
screen as a button and when they click the button they're going to be able to choose
your color so let's go ahead and make a private variable refer J button and now just name
it be for button since I'm lazy and next we need a color variable so private color let's
just name it color color variable name the color and we'll set it equal to the constant
way if I can type takes a little bit I'll get there eventually so now we have this color
variable set to white and the last thing we need is pretty much a panel so private J panel
and panel we'll just name it that for simplicity so we have three variables so let's start
doing some stuff with it um the first thing we're going to need to do is build our constructor
so name it public peach or whatever your class is and it doesn't take any parameters because
this constructor no parameters now let's go ahead and add a title and the title I cannot
type today I apologize for that although I'm not really sorry suckers and now that we have
a title let's go ahead and make our panel so take that panel variable you just made
and set it equal to new J panel and again this doesn't take any parameters either so
now we have a panel on the screen pretty much well we didn't add it yet but you know we
got it and next on our panel we want to set the background and this is pretty much the
background of our window our panel so next we need to set a background color for it so
set back BAM look at that too easy to have to type anything next we want to set the back
ground color and we're just going to set it equal to color and what this means is we're
going to set the background equal to white since our color had the value of color dot
white so now we pretty much got a panel chilling on our screen so now let's go ahead and add
our button to the screen so B equals new J button doesn't take let's go ahead and for
the text on our button let's just put choose of color because when they click the button
they're going to need to choose a color so now we can add some functionality to do this
so B dot add action listener right there and is our arguments we're just going to add an
anonymous inner class and let's go ahead and just put something like new action I was sure
the way they'd have to do this every time new action listener and again this action
listener must implement the method action performed and then after this we'll finally
get to the good stuff so public void action performed we did this enough hopefully action
event event and now let's get to the meat of this story finally so whenever we click
this button what do we want to happen well the first thing we want to do is we want to
change that color variable but instead of changing it to this just like a regular constant
or something we're going to let the user decide what they want to change it to so we need
to put something called J color chooser and what this allows is the user to choose your
color so just put show dialog right there and this dialog takes three parameters the
first parameter is the positioning just put a null and if you put a null it's going to
appear in the center of your screen the second parameter is when you click this button um
a new box is going to pop up so what do you want the title of that color chooser to be
just pick your color and last the last parameter is the initial color that's selected and you'll
see it's hard to explain because you can't see the box but just go ahead and put color
right here so whatever the color was set as before then that's what we want to have so
let's go ahead and change that and let me move this over just like that and now we're
good to go but the one thing we have to watch out for is if they don't choose a color at
all so if we give them an option choose a bunch of colors and they don't choose anything
we're going to write all right if the color that they choose is equal to nothing and this
is going to happen if they either hit X or cancel what do you want to do well we just
want to take that color and set it equal to whatever color it was before or let's just
go ahead and set it equal to white so if they don't choose anything then they got white
so that's your fault if you didn't choose anything you're getting white and last but
not least we need to change the color of our background so again the background is pretty
much the panel and set back around just like that and what do we want to change it to whatever
color they chose so now outside our action listener let's go ahead and add everything
to screen panel in I know I guys didn't teach you guys about border layout but border layout
dot center this means the panel is going to appear in some of the center of the screen
next let's add our button and that was named be in border layout in let's just make this
appear in south this means the button appears at the bottom of the screen set see you're
getting like tutorials for one in here set size of the window that's 425 by 150 and once
I run this program you guys are going to see what's going on set set visible and we're
just going to set this to true so it appears now let's run this so I can actually explain
it a lot better and again don't forget to call your object and set default close operation
set okay now we have this program that says title choose a color again this box is white
by default so now if I go to my code right here we're going to go ahead and press choose
a color now this is going to pop up so we go ahead and choose a color like blue or something
and press ok and now our background has turned blue and if we click it again choose a color
change it to yellow and click OK now our background goes to yellow and last but not least if you
choose it and hit something like X or cancel it's white by default so let me go ahead and
I'll explain this if I can get my code to set up right there what this pretty much means
you know what all this means this is a good stuff we change that color variable according
the whatever they wanted to change it do and again we use that using J color chooser show
dialog this is positioning this is the title of that window right here in again pick your
color see that title pick your color right there and someone's calling me I'm not going
to answer it and then this color is when you choose a color it's what's selected by default
so if you click red ok when you do it again Pink's going to be or Red's going to be selected
by default and next what this pretty much does is whenever you pick a color it's going
to store that value in the return value for color so if you chose black now this color
is going to be set to the color black and so that's pretty much what that does J color
chooser so then you test it if they hit X or cancel pretty much if they didn't select
any color if they didn't give them white but if they did then we want to set that background
equal to the color that they chose since that color variable will now equal whatever value
was returned and then we pretty much just added the panel in the center of the screen
right here we added the button to the bottom we made the size of our window we set as visible
so you can see it and then we called it so that's your real quick tutorial on how to
use J color chooser in essence whatever you pick right here is going to be stored in this
very man that's all you need to know so thank you guys for watching we're going to be using
this in our couple tutorials from now on so make sure you learn it and again one more
time thank you guys for watching don't forget to subscribe and I will see you next time
what's going on guys welcome to your 86th Jeff tutorial and in this store I'm going
to teaching guys how to draw some more shapes including like ovals and 3d rectangles but
you know when I say 3d wait till you see this you are not going to be impressed at all it's
pretty pathetic but you know it they had a thing called 3d rectangles so I had to figure
out how to draw it so let's go ahead in you know let's go ahead and get started public
and make sure you extend jpanel not jframe I think we might have done that last time
but go ahead and get your public void paint component again just like that and for a parameter
graphics G and now with our paint component we can actually paint things on the screen
so let's go ahead and well I guess we first need to call the super dot paint component
just like that in G parameter already filled in for us to call the super classes paint
component method and next let's go ahead and set background color might as well make it
white I'm going to make mine while you can make yours whatever you want set back around
and put color dot watch watch just like dodge toggle on corner and now let's go ahead and
the first thing I guess I can teach you guys how to do is draw a basic align on the screen
so G dot set color just like that and we'll set it to something like blue color dot I
mean it's one of my favorite color so moonlight in case you were wondering and now let's go
ahead and put G dot draw line and see what we got for parameters look at X Y and x/y
now you know that in order to draw a line you need two points on the screen this first
line this x and y is going to be a coordinates for the first point this x and y is going
to be coordinates for your second point in java will automatically draw a line from point
A to point B so let's go ahead and put 1025 for the first point which should be somewhere
10 over 25 down and for our second point is put like two hundred and forty five so it's
going to be a downhill align look something like this and now we got a line on screen
so what else can we draw um G dot set color we'll set this one to color dot red because
you know got to mix it up a little bit and now let's go ahead and put draw or excuse
me G dot draw correct and what this means is unlike last time when we drew a solid rectangle
using fill rect this draws a an empty rectangle and it takes the same parameters as before
let's go ahead and put ten over fifty five down it will be one hundred wide and it will
be 30 pixels tall and now let's go ahead and I'm going to teach you guys how to make ovals
now now in well let's go ahead and do this first set color what color you want to make
it guys great you said all right color dot green so we're going to have a green oval
g dot we'll make this one solid fill oval and anytime you have fill and draw they're
pretty much interchangeable depending on personal preference now this fill oval parameters it
takes the exact same parameters as the rectangle but what it's going to automatically do is
round your corners for you so if we want to make an oval that's the same size as this
let's go ahead and move it down a little bit so we'll do ten let's see fifty eighty five
so we'll put it in ninety five that should give us ten pixels padding and we'll make
it a hundred wide by 30 tall now this is going to be the same size is this rectangle right
here but instead of a rectangle it's going to be rounded off at the corners aka and oval
so now we got a line a rectangle in and over in an oval not over E I apologize so the last
thing I'm going to teach you guys is something called a 3d rectangle and what this does is
is pretty much a rectangle that's filled in but it has a little on bevel on one of the
sides to give it a slight appearance that it's like pushed out a little bit so puts
G dot set color and what do you want to change the co2 let's pick good one color dot let's
see that was kind of good on orange BAM that's what I'm looking for orange and now let's
go ahead and put G dot fill vamp 3d rectangle just like this now our parameters this is
a little too easy let's put it 10 over 160 down that's positioning the width can be 100
just like everything else the height make this one a little bigger 50 now what this
raised means since this is 3d rectangle it has the option of being raised pretty much
means do you want to give it 3d look or not and of course we do true because 3d is awesome
you can also put false to give it not 3d look but we want this baby looking 3d and you guys
are about to be disappointed because this doesn't give you nearly as much 3d Z like
but if you go ahead and run this program I already fell - I'm on parameters if you guys
need to do this just go ahead and pause but I did this about 80 times before so you guys
should not do that go ahead and as you can see we drew line from point A to point B we
also figured out how to draw a rectangle let me go ahead and get my good code up here our
rectangle using draw rectangle instead of fill rectangle again fill rectangle would
fill this all in with a red we also filled an oval in using green using the same parameters
as our rectangle and last but not least let's scroll down here we also made this 3d um rectangle
using fill 3 direct now again I don't even know if you guys can tell on this tutorial
because you to probably take some quality away but there's a little line right here
along the edge and it's a little darker than normal and this is a little than normal on
this edge and what this does is give you guys the 3d effect now again it's not like 3d like
you're making a 3d button in 3ds Max or something it's just a little subtle button like you'd
see on a like a very old webpage but you know it's a it's subtle but it's there so that's
how you make 3d and I'm moving my fingers like went quoting right now but that's how
you do 3d rectangles and that's what it means basically so we've learned how to draw a bunch
of new stuff on the screen lines rectangles overs and 3d rectangles it was awesome and
so you know it's been fun but that's all the time I have for this tutorial so don't forget
to subscribe and I'll see you next time what is going on everybody welcome to your 87th
in the last tutorial in Java programming whatever I named it and this is the last tutorial in
Java programming because after this I'm going to start a new series and it's going to be
like intermediate Java programming because I mean we can only continue this beginner
series for so long there's so much a job to go over so this 87th video is the last in
this series but after this there's going to be like 50 more intermediate videos hopefully
if I ever find the time and also there after that it's going to be more expert I just can't
have like 200 videos in a playlist or else it'll seem kind of weird so that's why this
is going to be a last video in this series but trust me we're not done Java yet and aside
from this my hard core Java followers are probably like alright what happened to Java
game development I thought we were really like halfway through that well we're going
to be continuing Java game development but in the meantime we need to go over some basics
because we're going to be using that in our game programming so if you're wondering and
worried about all right I want to get back to game development trust me guys we will
build games in Java but first we need some more basics so what we're going to be going
over in this tutorial is a couple things about strings and I know we talked about strings
before but we never really talked about all the things that we can do with strings so
let's go ahead and make a couple more strings right now I'm going to name mine a B and C
and I'll set a equal to apples I said a lot of things people equal to apples I just notice
that string B set this equal to Bucky and string C set this equal to Bucky but making
sure it's all in caps and spell it right it's crucial so now we have string a B and C all
right nothing we've never seen before now we already know a couple cool things that
we can do like system out print line and we can get like the length of string using a
dot length and let me just test this to make sure it works good and now there we go and
now we can see six right here so I mean we already learned a few things like how we can
get the length using calling the length from a string class but we can also do a couple
more things and the first thing I want to teach you guys how to do is compare strings
now it's a little different than you guys might think so let's go ahead and make it
if statement and what you would usually do is if a is equal to this in whatever you know
whatever comparison you want using two equal signs and this is how we learn to compare
things before whenever we compare strings what we need to do is we need to use the equals
method so here's what you do put a which is apples if this equals and then in your equals
you go ahead and write what you want it to equal to if a equals apples what you does
what do you want to do just go ahead and put system like let's go ahead and just copy this
smell print line it equals apples put that in a string so let's run this and see what
we get after we get rid of that there we go run it and see what we get in it says an error
because I didn't use these two things yet B or C so that's why we're getting that it
equals apples BAM so this worked and if you're wondering all right you just told me to do
that but why can't I use the two equal sign well here's the background for you guys who
are just so curious when you use the two equal signs it how do I say this if two objects
are identical which means they have the same contents but there are two separate objects
themselves then it's going to be false and this happens a lot in strings and what happened
in this case but it's just it just can cause a lot of confusion so with the two equal sign
just remember if you try to compare with two equal signs you kept getting an error that
means that you have I mean they could be the same object they go could be both set to apples
but if they aren't the same object then it's going to be false so that's why you always
need to use the equals method and that's why they built this method for only strings if
you're wondering all right why isn't this in any other class because no other class
causes problems what the equals method does is it compares the contents of it and that's
what you always want to do when you're comparing strings so I mean BAM that's that and why
did I have these two little things right here well we can go ahead and compare these by
just doing a little quick copy-paste and we'll put like if B equals C this is just a demonstrate
that you can't that two strings without the same casing is different so what do we want
to write the keys match right there so if string B equals string C which are both Buckey
let's see what we get run and it says Lucky's match nope we didn't get a Bucky's match so
let's go ahead and now we can see that all right this Bucky does not equal this Bucky
but what if we're writing the computer program and we want them to like type their name or
something and we don't care about casing and how can we compare two strings with kind of
forgetting about casing what we need to do is this put equals ignore case and now let's
see what we get and now look at this it says Bucky's match so again what I'm trying to
demonstrate is anytime you want to compare two strings and you don't want to have to
worry about case then what you do is put B equals ignore case and it's going to compare
your two strings just like before but it's going to take the case of it and pretty much
forget about it say just the contents of Bucky equals Bucky and matter what casing as you
can clearly see so that's that for this tutorial and again like I said when you're moving on
from this tutorial don't look 488 the video there's not going to be one you are now ready
to go to intermediate Java programming and I'm going to be building the first tutorial
of that next is going to be a continuation of strings so with that being said I just
want to thank you guys you've finally made it through you've learned so much Java in
this series and I cannot wait to see you next time so don't forget to subscribe and I'll
see you then.