Java Full Course for free ☕ — Transcript
Full transcript
- 0:00hey how's it going everybody in this
- 0:02video I'm going to teach you everything
- 0:03you need to know to start coding with
- 0:05Java this series even contains about 15
- 0:08Hands-On projects that we're going to
- 0:09code together our final project will be
- 0:12a functioning alarm clock that plays
- 0:14music of your choice if you've never
- 0:16coded anything in your life don't worry
- 0:18we're going to start from the very
- 0:19beginning I encourage you to sit back
- 0:22relax and enjoy the
- 0:26show I don't like boring introductions
- 0:28so we're just going to Jump Right In to
- 0:30start coding with Java you'll need two
- 0:32things a Java development kit also known
- 0:35as a jdk a jdk contains a compiler a
- 0:38compiler will compile your source code
- 0:40your written code into bite code which
- 0:42can run on a machine you'll also need an
- 0:45integrated development environment IDE
- 0:48an IDE is a fancy term for a workspace
- 0:51in which we can write code because
- 0:53sometimes notepad just doesn't cut it
- 0:55we'll get started by downloading a jdk a
- 0:58Java development kit okay here's how to
- 1:01download the jdk the Java development
- 1:03kit you can easily just Google or do a
- 1:05search for jdk download we're going to
- 1:08use the one by Oracle so we'll click on
- 1:10this link I'll also post the link in the
- 1:13description of this video if you would
- 1:15like to access it that way we will
- 1:17download the latest jdk version as of
- 1:20the filming of this video we're on jdk
- 1:2323 we're going to look for our operating
- 1:26system I'm running Windows I will
- 1:29download the 64- bit installer by
- 1:31clicking this
- 1:32link then we just have to wait for it to
- 1:37download the download is complete we're
- 1:39going to open this
- 1:41executable select next
- 1:49next our jdk has been successfully
- 1:52installed we can close it and that's all
- 1:54we got to do for the
- 1:56jdk now we need to install an IDE an
- 1:59integrated development
- 2:01environment one I recommend is intellig
- 2:05you can easily just Google or do a
- 2:07search for intellig
- 2:09download we'll select the top
- 2:12link now if you're not brought to this
- 2:14page let's say you're at a different
- 2:17page go to the top right where it says
- 2:20get intellig click it and you should be
- 2:23brought to a download page this version
- 2:25is a 30-day trial but if you scroll down
- 2:28there is a free version this is the
- 2:30Community Edition because I don't like
- 2:32to pay for things select the correct
- 2:35download for your operating system I'm
- 2:36running Windows I will download the
- 2:41executable once the download is complete
- 2:43we will open this executable for
- 2:46intellig select
- 2:49next choose a destination folder I'll
- 2:52keep it as is Click
- 2:55next you can create a desktop shortcut I
- 2:58think I will just for convenience
- 3:01now this is important too you can update
- 3:04the path variable it basically tells
- 3:06your operating system where to find the
- 3:08executable files this is useful if
- 3:10you're going to be running Java code
- 3:12from command prompt or terminal but we
- 3:14won't be doing that in this series you
- 3:16can always come back later and update
- 3:18this but we'll just select next and
- 3:24install all right and we can finish we
- 3:27might as well start intellig right away
- 3:29I'm going to check this check boox to
- 3:30run intellig and
- 3:34finish we should now be at a welcome
- 3:36screen for intellig we need to create a
- 3:38project select new project I'll name my
- 3:42project my first
- 3:45project select the latest version of
- 3:48your jdk I have jdk version 23 there is
- 3:52a checkbox and it's most likely going to
- 3:54be checked that says add sample code
- 3:57just as a beginner I recommend
- 3:59unchecking this it'll autogenerate some
- 4:01sample code for us but we're going to do
- 4:03that ourselves I'll show you how and
- 4:06then
- 4:09create within our project folder
- 4:11navigate to your Source folder it has a
- 4:14name of
- 4:15SRC we need to create a main Java file
- 4:18go to the top toolbar go to file new
- 4:21Java class we'll name this class main
- 4:25it's going to contain the main body of
- 4:26our code then select class
- 4:31now the font is probably going to be
- 4:33really
- 4:34small here's how to change
- 4:37that go to the top toolbar go to
- 4:41settings go to
- 4:44editor
- 4:45General and you can check this checkbox
- 4:48for change font size with Control Plus
- 4:50Mouse wheel apply okay now you should be
- 4:55able to zoom in if you hold control and
- 4:57scroll your mouse wheel now we can
- 5:00actually read something in Java in order
- 5:03to get some code to run you need a main
- 5:05method within our class of main there's
- 5:07a set of curly braces we'll type the
- 5:11following public static void main add a
- 5:17set of parentheses then a set of curly
- 5:19braces within the set of parentheses
- 5:21you're going to type the following
- 5:24string straight brackets
- 5:27args like you're a pirate
- 5:30you need this method in order for your
- 5:32program to run over the course of this
- 5:34series we'll learn what each of these
- 5:36keywords means but as a beginner think
- 5:39of it as a magic spell that you have to
- 5:40say in order to get your program to run
- 5:43to run your program you're going to
- 5:44click the screen Arrow or you can hold
- 5:47shift plus
- 5:51F10 if we have written the main method
- 5:54successfully we should have no
- 5:56errors it says processed finished with
- 5:59exit code zero that means everything is
- 6:01running fine if I were to delete this
- 6:03main method we can't actually run the
- 6:07program so be sure that that method is
- 6:09in to Output some text to the console
- 6:12window we're going to type system with a
- 6:15capital s. out. print add a set of
- 6:19parentheses then a
- 6:21semicolon in Java you'll end most of
- 6:24your statements with a semicolon it's
- 6:26kind of like a period at the end of a
- 6:28sentence in the English language
- 6:30within the set of parentheses we'll add
- 6:32a set of double quotes whatever we would
- 6:35like to Output we're going to type
- 6:37within the set of double quotes think of
- 6:39a food you like I'll type I like
- 6:43pizza and then we can run this and this
- 6:45should be the
- 6:47output again I'm going to go to the
- 6:48Green Arrow or you can hold shift
- 6:52F10 and a console window should pop up
- 6:56that says I like pizza or whatever your
- 6:59favorite food is whatever you typed
- 7:03here let's add another line of
- 7:05code again type
- 7:10system.out.print end it with the
- 7:13semicolon within quotes which we type I
- 7:16like pizza it's really
- 7:20good let's run it
- 7:23again all right here's our output I like
- 7:27pizza it's really good
- 7:30now these are both on the same line to
- 7:32move your output down to the next line
- 7:35you can use print Ln meaning print
- 7:39line now it should look something like
- 7:42this I like pizza it's really
- 7:47good if you type back sln that's an
- 7:51escape sequence for a new line and that
- 7:53works too besides print line I like
- 7:57pizza it's really good
- 8:00let's add another line of text
- 8:04system.out.print or print
- 8:07line I like pizza it's really good buy
- 8:11me
- 8:12pizza and I'll add a new line
- 8:17character I like pizza it's really good
- 8:20buy me
- 8:22pizza or again you could use print
- 8:26line so print Ln for print
- 8:31line and this will do the same
- 8:35thing now you can write a comment
- 8:38comments aren't displayed as output to
- 8:40write a comment you use two forward
- 8:43slashes and the text should be gray out
- 8:46comments are good as notes for yourself
- 8:48or for other developers let's add a
- 8:50comment that this is my first Java
- 8:56program when we run this this won't
- 8:58display us out output it's really just
- 9:01used as notes we still have our output
- 9:04it hasn't
- 9:06changed you can even add a multi-line
- 9:08comment with a forward slash then an
- 9:12asterisk to end a multi-line comment you
- 9:15need an asterisk then a forward slash
- 9:18I'll write this is a
- 9:23multi- line
- 9:26comment this won't output either
- 9:31we don't see either of these
- 9:33comments this is specific for intellig
- 9:36but you can change the console font and
- 9:37the colors I kind of find this hard to
- 9:40read to do that we're going to go to the
- 9:42top menu under file go to
- 9:45settings under color scheme go to
- 9:48console font here you can change the
- 9:50font if you would like or the colors
- 9:52I'll change the colors I'll make the
- 9:54output bright green so it's easier to
- 9:57read I'll go to console
- 9:59standard output and select a new color
- 10:03I'll select something
- 10:04green apply and I think that looks
- 10:07pretty good it's more readable that way
- 10:10I'll also change the system output
- 10:13too I'll make it kind of gray out that's
- 10:18pretty
- 10:18good yeah and there's our output I like
- 10:22pizza it's really good buy me
- 10:25pizza now a shortcut to type system. out
- 10:27at print line if you're using intellig
- 10:30is to type s o u t then hit tab that's
- 10:34going to autogenerate a print line
- 10:35statement for you I didn't realize this
- 10:37trick until a little bit later so you
- 10:39don't always need to type out this
- 10:41statement you can just autogenerate it
- 10:43all right everybody and that is your
- 10:44first Java program your homework in the
- 10:47comment section is to post three print
- 10:49line statements maybe type A poem or
- 10:51some song lyrics or something and well
- 10:54everybody that is your first Java
- 10:55program
- 10:59all right everybody so we are moving on
- 11:01to variables in Java probably back in
- 11:04elementary school or middle school you
- 11:06learned about variables in algebra a
- 11:08variable was a representation of a
- 11:11number well in programming we have
- 11:13variables too a variable is a reusable
- 11:16container for a value a variable behaves
- 11:19as if it was the value it contains but
- 11:22variables can contain more than just
- 11:24numbers they can contain characters and
- 11:27even entire words there's different data
- 11:29types a whole number would be an integer
- 11:32a number that includes a decimal would
- 11:34be a double there's two categories of
- 11:36variables we'll discuss primitive and
- 11:39reference primitive variables are simple
- 11:42values like numbers they're stored
- 11:45directly in memory usually in a location
- 11:48known as the stack reference variables
- 11:52actually hold memory addresses on the
- 11:54stack they point to a location in a
- 11:57different area known as the Heap think
- 11:59of it this way if I give you a primitive
- 12:02variable that would be the same as me
- 12:04handing you
- 12:05$10 but if I give you a reference
- 12:08variable a memory address that would be
- 12:11the same as me giving you an IOU that
- 12:13says I owe you $10 but the actual money
- 12:16I owe you is at the bank that might be a
- 12:19beginner's way of thinking of the
- 12:20difference between a primitive and
- 12:22reference data type primitive I'm giving
- 12:24you $10 a reference data type I'm giving
- 12:27you an IOU that I owe you $10 we'll
- 12:31discuss the different data types we'll
- 12:32begin with integers an INT short for
- 12:36integer is a primitive data type I'll
- 12:39show you how we can create an integer
- 12:41variable in Java there's two steps to
- 12:44creating a variable the first is
- 12:46declaration the second is assignment
- 12:50let's say we would like to create a
- 12:51variable that stores a whole number an
- 12:53integer with the first step of
- 12:55Declaration we're going to first type
- 12:57the data type of what we would like to
- 12:59store exactly so for integers we will
- 13:02type int then we should think of a
- 13:04unique variable name we don't want to
- 13:06say X or something similar that would
- 13:09make sense in basic algebra but in
- 13:12programming you want your variables to
- 13:13be descriptive like shut up in
- 13:17programming you want your variables to
- 13:18be descriptive so let's say we are
- 13:20working with let's say in age end this
- 13:24line with a semicolon so this is
- 13:27declaration step one we are creating a
- 13:30variable named age if I was to print my
- 13:33variable of age
- 13:36system.out.print
- 13:38line let's print age and see what it
- 13:44is variable age might not have been
- 13:48initialized we're using a variable but
- 13:50we've never given it a value we've never
- 13:52completed step two of
- 13:54assignment so we can set age equal to a
- 13:57value so on my YouTube channel the
- 14:00average age of my viewers is
- 14:02approximately 21 or so so let's set age
- 14:05to be 21 and then we'll print
- 14:09it so that outputs 21 if I were to
- 14:13change this to
- 14:1530 then my variable age would behave as
- 14:19if it was the number 30 so those are
- 14:22integers they're whole numbers let's say
- 14:24that I'm
- 14:2530.5 let's see what happens exactly you
- 14:28can see that intellig is already giving
- 14:30us a warning that we're working with a
- 14:32different data type known as doubles so
- 14:35if I were to run this we get an error
- 14:38message we have incompatible types from
- 14:42double to in if we declare a variable as
- 14:45an integer they can only store whole
- 14:49integers let's think of a few more
- 14:51examples another example of an integer
- 14:53could be a year so let's say the year is
- 14:572025
- 14:59then we'll print our
- 15:02year
- 15:042025 or maybe a quantity int
- 15:09quantity equals
- 15:111 then we'll print our
- 15:14quantity one so in working with
- 15:17quantities they're whole numbers usually
- 15:20right we wouldn't have let's say half a
- 15:22product 1.5 I would like 1.5 Nintendo
- 15:26switches that wouldn't make sense right
- 15:28they would would be whole numbers like
- 15:30one or
- 15:33two now you can output a variable with a
- 15:36string of text as
- 15:38well let's say the year
- 15:43is then I'll add plus a variable year
- 15:48make sure that the variable is not
- 15:49within
- 15:51quotes So currently this says the year
- 15:54is
- 15:552025 you do have to pay attention to the
- 15:57spacing so after the word is I'm going
- 16:00to add a space to separate is and the
- 16:03variable
- 16:04year the year is
- 16:072025 now if our variable was within
- 16:11quotes we would literally be printing
- 16:14the word year the year is year so don't
- 16:19do that unless it's
- 16:21intentional let's go on to the next data
- 16:23type the next data type is double double
- 16:27is a number but it can contain a decimal
- 16:29portion unlike int ins are whole
- 16:33numbers so this time we will create a
- 16:35variable type of double what are some
- 16:38examples of a variable that could be a
- 16:40double let's say a price there could be
- 16:43dollars and cents
- 16:48$19.99 or perhaps a GPA a grade point
- 16:51average double GPA equal
- 16:573.5 or a temperature
- 17:00double temperature
- 17:04equals
- 17:0812.5 all right let's output some of
- 17:10these let's do
- 17:13Price
- 17:15system.out.print
- 17:18Line let's output the
- 17:23price
- 17:2419.99 let's add a unit of currency I'll
- 17:27pick American dollars
- 17:31so Dollar Sign Plus price
- 17:35$19.99 if you were to assign a variable
- 17:38that's a double data type but store an
- 17:40integer here's what would
- 17:44happen we would just add 0 Z to the end
- 17:47it's still a double so those are doubles
- 17:51they're numbers that can contain a
- 17:52decimal portion unlike
- 17:55integers okay then the next data type we
- 17:58have is charart
- 17:59meaning character Char pronounced like
- 18:02the word
- 18:04Charizard so with chars what are a few
- 18:07examples of single characters we could
- 18:09use let's say a grade like a letter
- 18:13grade with chars characters we enclose
- 18:16them within single quotes let's say we
- 18:19have the letter A that's the grade we
- 18:22got on the last test or
- 18:24something or maybe a symbol
- 18:29maybe I'll put an exclamation point or
- 18:32something or currency Char currency
- 18:37equals pick a unit of currency I'll pick
- 18:41dollars okay then let's output some of
- 18:43these
- 18:45system.out.print
- 18:48line let's print our
- 18:53grade which is an
- 18:55A or our symbol
- 18:59an exclamation point or our
- 19:03currency which is American
- 19:06dollars so then we have something called
- 19:09booleans booleans are either true or
- 19:13false we will declare this variable as a
- 19:17Boolean let's check to see if somebody
- 19:20is a student our variable name will be
- 19:22is student and this equals true or false
- 19:27so if I am a student I would say true
- 19:31now a common naming convention in Java
- 19:33is called camel case naming convention
- 19:36if your variable name takes up two words
- 19:39such as is and student when you combine
- 19:43them the first letter of the first word
- 19:45should be lowercase then any word after
- 19:48the first letter is going to be
- 19:50uppercase so is student and I will set
- 19:54that to be true maybe something is for
- 19:57sale or not Boolean for sale equals
- 20:02false it's not for sale is somebody
- 20:06online Boolean is online now I will set
- 20:11that to be true these are a few examples
- 20:14of Boolean
- 20:16variables now you could output them
- 20:18directly using system.out.print line
- 20:22Let's test it system.out.print line
- 20:26let's output is student
- 20:29are we a student that is true is
- 20:32something for sale for
- 20:35sale that is false now with Boolean
- 20:39variables we don't typically output them
- 20:41directly to the console we tend to use
- 20:43them more internally within a
- 20:46program one place in which you might see
- 20:48them is with the use of if statements
- 20:51and this is a future topic we'll revisit
- 20:53it later but I want to demonstrate the
- 20:55usefulness of booleans so this is an if
- 20:58statement we can check some condition
- 21:01parenthesis then curly
- 21:03braces if our condition is true if is
- 21:08student double equals true but we could
- 21:12simplify this
- 21:13to if is student if this variable
- 21:18contains true which it does we can
- 21:20execute some code
- 21:23system.out.print
- 21:25line we will print the following
- 21:28you are a
- 21:31student if this Boolean variable
- 21:34contains true we will print the
- 21:37following you are a
- 21:40student or we could add else an else
- 21:43Clause else let's
- 21:46print you are not a
- 21:50student So currently we're still a
- 21:53student but if this Boolean variable
- 21:55contained a value of false we would
- 21:58perform form the else
- 22:00statement you are not a student so we'll
- 22:04cover if statements in a future topic
- 22:06you don't necessarily need to know how
- 22:08these work right now but that's one of
- 22:10the uses of booleans they can only
- 22:12contain true or
- 22:15false these are a few of the basic
- 22:17primitive data types for beginners int
- 22:20for integers they contain whole numbers
- 22:23doubles are numbers but they can contain
- 22:25a decimal chars meaning characters are
- 22:28single characters and booleans which are
- 22:31either true or false these are a few of
- 22:34the beginner primitive data types but
- 22:37there's still a lot more than this
- 22:39there's floats there's long doubles all
- 22:41sorts of different data types but you
- 22:43don't need to know those as a beginner I
- 22:45would just stick to these four for now
- 22:47so then we have the reference data types
- 22:50A few of which include strings arrays
- 22:52and objects in this topic we're only
- 22:55going to discuss strings a series of
- 22:57characters phas and objects are really
- 22:59complex we'll revisit these later so
- 23:02we're going to create a string a series
- 23:05of
- 23:05characters so the data type is going to
- 23:08be string an example of a series of
- 23:11characters could be a name what's your
- 23:13name with strings we enclose them within
- 23:16double quotes chars single characters
- 23:20are single quotes it's really easy to
- 23:23mix the two up you might do it a few
- 23:25times as a beginner so type in your name
- 23:28in including a space I'll use my YouTube
- 23:30channel
- 23:32name and then we will output our name
- 23:36system.out.print Line Print your
- 23:41name and your first and last name should
- 23:44appear now we could use this variable of
- 23:47name within another string this is known
- 23:49as string
- 23:50concatenation so let's
- 23:52say
- 23:55hello plus our name
- 23:59hello bro code or whatever your name is
- 24:03what are a few other examples of strings
- 24:04we could
- 24:06write let's add string food
- 24:10equals again within a set of double
- 24:13quotes add your favorite food mine is
- 24:17pizza then let's type again
- 24:20system.out.print
- 24:23line your favorite food is
- 24:29plus the variable
- 24:31food your favorite food is
- 24:38pizza what about an email string email
- 24:42equals make up an email fake1
- 24:46[email protected]
- 24:50strings they can contain numbers but we
- 24:54treat them more as characters rather
- 24:56than actual numbers that we can use for
- 24:58any sort of math or
- 25:00arithmetic so let's print our email your
- 25:04email
- 25:05is plus
- 25:10email your email is fake1 [email protected]
- 25:15or what about a
- 25:18car string
- 25:20car think of a car that you like I will
- 25:23pick a Mustang that's another example of
- 25:26a string or a color
- 25:29string color equals
- 25:33red so strings are a series of
- 25:36characters they have a reference data
- 25:38type you may have also noticed that the
- 25:41font color of strings is different from
- 25:43the primitive data types too of booleans
- 25:45chars doubles and Ins at least within my
- 25:48workspace the font color is orange and
- 25:51with strings the font color of these
- 25:53data types is white now we're going to
- 25:56cover a few exercises
- 25:58so let's start with our name
- 26:01system.out.print line we will print
- 26:06hello plus our name variable it's a
- 26:10string hello your first and last
- 26:15name let's print our age
- 26:18system.out.print
- 26:20line u
- 26:23r we're going to insert our variable age
- 26:26then add another string another string
- 26:28literal you are age years old again do
- 26:33pay attention to the
- 26:35spacing you are 30 years
- 26:39old let's use my
- 26:42GPA that's a
- 26:45double system.out.print
- 26:51line your GPA
- 26:54is then we will insert our GPA
- 26:59your GPA is
- 27:023.5 okay let's use our Char of
- 27:08grade
- 27:09system.out.print
- 27:13line your average letter
- 27:17grade
- 27:21is our variable
- 27:25grade your average letter grade is is a
- 27:29now within a single print line statement
- 27:31we're going to combine multiple
- 27:33variables let's say we're going to
- 27:35Output our
- 27:37color a
- 27:39year and our
- 27:41car all within one print statement
- 27:46system.out.print
- 27:48line let's say we're choosing a car your
- 27:52choice is a We'll add our color variable
- 27:59plus the
- 28:01year plus our car let's see what happens
- 28:04exactly if we attempt
- 28:07this so we've combined all three
- 28:10variables red 2025 and our car of
- 28:13Mustang so we're going to add some space
- 28:16characters between each to separate them
- 28:19so plus a space character add that after
- 28:22the
- 28:23color after the
- 28:26year we'll add a space between color and
- 28:29year and year and
- 28:32car so your choice is a red 2025
- 28:38Mustang let's display the
- 28:41currency and a
- 28:44price
- 28:47system.out.print
- 28:50line the price
- 28:55is plus currency
- 28:58I picked American
- 29:00dollars plus
- 29:04price the price is
- 29:07$9.99 uh for a car let's up that that's
- 29:10way too cheap that's too good to be true
- 29:13let's say
- 29:15$9,999
- 29:1999 that's more
- 29:21realistic now we're going to use this
- 29:24Boolean variable of for
- 29:26sale if
- 29:29for sale if this is true then we're
- 29:33going to
- 29:39print there
- 29:42is
- 29:44a plus
- 29:47Car Plus the text of for
- 29:52sale else we will print something else
- 29:55I'll just copy this
- 29:59let's
- 30:01say
- 30:04the plus Car Plus is not for sale for
- 30:11sale
- 30:12currently that's set to false the
- 30:15Mustang is not for sale but if this were
- 30:20true there is a Mustang for sale for
- 30:24sale is set to true all right everybody
- 30:27so those are variables in Java a
- 30:30variable is a reusable container for a
- 30:33value a variable behaves as if it was
- 30:36the value that it contains there's two
- 30:39different categories of variables
- 30:41primitive and reference primitive data
- 30:43types are simple values like integers
- 30:47floats single characters and
- 30:51booleans they're stored directly in
- 30:53memory usually in a location known as
- 30:56the stack reference variables such as
- 30:59strings which are a series of characters
- 31:02like your name or an email they're more
- 31:05complicated we store a memory address
- 31:08that points to a location in the Heap
- 31:11and your homework assignment is in the
- 31:13comment section post five variables a
- 31:16string an integer a double A Char and a
- 31:20Boolean and if you don't do your
- 31:22homework you're going to make me sad and
- 31:24well everybody that is an introduction
- 31:26to variables in
- 31:29Java hello again friends so today I'm
- 31:32going to show you how we can accept user
- 31:34input in Java we will need the help of
- 31:37what is called a scanner a scanner is an
- 31:40object that allows us to accept user
- 31:42input in Java however in order to use a
- 31:44scanner we actually need to import it
- 31:47from the certain package called
- 31:48utilities or util at the top of our Java
- 31:51file we're going to write the following
- 31:54statement import Java do
- 31:58util this is a package do scanner
- 32:03scanner is a class so make sure you have
- 32:06this import at the top before we get
- 32:07started to sum up programming we
- 32:10typically accept input process it then
- 32:13produce output so that's why accepting
- 32:15user input is important we would like to
- 32:17do something with the input so to accept
- 32:21user input we're going to create a
- 32:23scanner object so type this scanner
- 32:27that's the name of the class
- 32:28class scanner that's going to be the
- 32:31name of the object we'll be working with
- 32:34do pay attention to the
- 32:35capitalization our scanner object is all
- 32:38lowercase characters whereas in the
- 32:40first scanner has an uppercase
- 32:42s scanner scanner equals new scanner add
- 32:47a set of parenthesis then a semicolon
- 32:50within the set of parentheses we're
- 32:52going to type
- 32:54system.in our scanner object can read
- 32:57user input however it is good practice
- 33:00if you create a scanner object at the
- 33:02end of your program when you're done
- 33:04we're going to take our scanner object
- 33:07and use the close
- 33:10method so scanner. close parenthesis
- 33:14semicolon if you don't close your
- 33:16scanner it can lead to unexpected
- 33:19behavior for example let's say that we
- 33:21read a file well after we're done
- 33:23reading the file we'll want to close the
- 33:25file when we open something we typically
- 33:28want to close
- 33:29it we're going to create a prompt within
- 33:32our console that says enter your name
- 33:34then a user will be able to type in
- 33:36their name we'll need a user prompt to
- 33:38work with so let's say system.out.print
- 33:42Line enter your
- 33:48name after receiving this prompt a user
- 33:51is going to be able to type in their
- 33:53name we're going to use our scanner
- 33:55object scanner
- 33:58next line parentheses semicolon so a
- 34:03user is going to be able to type in a
- 34:05string of text so let's type in our full
- 34:07name then hit
- 34:08enter however with this user input we're
- 34:11not doing anything with it so let's
- 34:14assign it to a
- 34:15variable so with our line of scanner.
- 34:19next line we will declare a variable and
- 34:22assign it string name equals scanner.
- 34:26nextline we have declaration and
- 34:30assignment those two
- 34:32steps and then once we have our name
- 34:35let's output it system.out.print line
- 34:39let's say hello plus
- 34:44name so enter your name type in your
- 34:47full name hit enter and the output
- 34:50should be hello whatever your first and
- 34:53last name is now if you prefer we can
- 34:56put the input on the same line as the
- 34:59prompt we're going to use print instead
- 35:02of print Line Print line will add a new
- 35:04line character to the
- 35:06end so let's stick with
- 35:09print so the input will be on the same
- 35:12line as the prompt if you prefer it that
- 35:15way so the next line method of our
- 35:18scanner object it reads A String of
- 35:20characters including any spaces if you
- 35:23don't want any spaces you can use next
- 35:29so if I were to type in my full name
- 35:31with the first and last name separated
- 35:33with the
- 35:34space we only get the first name next
- 35:38doesn't read any
- 35:40spaces so use what you would prefer I
- 35:43typically just use next
- 35:45line to read an integer there's a
- 35:48different method called Next int this
- 35:51time let's create a prompt of enter your
- 35:55age I'll use print instead of print line
- 35:59we will assign a variable of age int age
- 36:03equals use our scanner
- 36:06object use the built-in next int
- 36:10method then once we have our integer of
- 36:13age let's output it system.out.print
- 36:17line u
- 36:20r plus our variable age plus years old
- 36:30enter your name type in your name enter
- 36:32your age oops I'm going to add a colon
- 36:36and a space after my prompt just because
- 36:38I think it'll look
- 36:41better okay enter your full
- 36:44name enter your age hello your name you
- 36:49are whatever your age is years old so
- 36:53that's how to accept an integer if you
- 36:56were to type in a floating point number
- 36:58or a double anything that includes a
- 37:00decimal now check this out our variable
- 37:04of age should be an integer if we type
- 37:06in something that contains a decimal
- 37:08like a floating Point number or a double
- 37:10let's say that I'm
- 37:1325.9 well we get an error here it says
- 37:16we have an error at line 12 where we
- 37:18accept an integer so if you need a
- 37:22double there's a next Double method of
- 37:26scanners so this time let's ask for a
- 37:30GPA what is your GPA your grade point
- 37:35average we will assign a variable that's
- 37:37going to be of the double data type
- 37:40double GPA equals again use our
- 37:44scanner use the next Double
- 37:48method and then let's output
- 37:52it system.out.print
- 37:55line your GPA is plus our variable
- 38:02GPA so again let's go through the
- 38:04prompts type in your name type in your
- 38:08age what is your GPA uh let's use print
- 38:11instead of print
- 38:15Line enter your name enter your age what
- 38:20is your GPA let's say my GPA is 2.1 I'm
- 38:24just barely passing hey C get degrees
- 38:28right your GPA is the GPA variable so
- 38:33that's how to accept the double use the
- 38:35next Double method of scanners now for
- 38:38some reason if you need a Boolean
- 38:40booleans are either true or false here's
- 38:43how we can accept
- 38:45that system.out.print line let's ask are
- 38:49you a
- 38:51student then I'll add a prompt for
- 38:54true or false so the US user knows to
- 38:58type in one of these instead of yes or
- 39:00no so we will create a Boolean variable
- 39:04of is student equals use our scanner
- 39:09object use the next Boolean
- 39:13method and then let's output
- 39:16it system.out.print
- 39:19line let's say student colon space plus
- 39:24is student I think an if statement would
- 39:26be better we'll cover that in a
- 39:29moment again go through the prompts type
- 39:31in your name type in your age type in a
- 39:35GPA are you a student true or false
- 39:39let's say that's true student
- 39:43true and again I'm going to use print
- 39:45instead of print
- 39:47line not necessary but I think it looks
- 39:49better let's use an if statement instead
- 39:52when checking is student is true or not
- 39:55so if is student again if statements
- 39:58will be a future lesson we'll cover
- 40:00pretty soon if is student if that's true
- 40:04then we will
- 40:06print let's print you are
- 40:11enrolled as a
- 40:14student else we will print something
- 40:16else if that's not true if it's false
- 40:20you
- 40:23are
- 40:25not enrolled
- 40:32okay enter your name enter your age
- 40:35enter a GPA are you a student let's say
- 40:39that it's
- 40:40false okay we have our name our age our
- 40:44GPA and then we're outputting you are
- 40:47not enrolled you are not enrolled in
- 40:49classes because we said
- 40:51false now if we put true
- 41:00well then we get the output you are
- 41:02enrolled as a student that's how to
- 41:05accept user input of different data
- 41:07types A few of which include strings
- 41:10integers doubles and
- 41:12booleans now there's one common issue
- 41:15when accepting an integer or double than
- 41:18accepting a string let me
- 41:24demonstrate so we have our scanner
- 41:27let's say we ask for a user's
- 41:30age enter your
- 41:33age and I'll make this a print statement
- 41:36instead of print
- 41:37line int age equals use our scanner use
- 41:41the next int
- 41:44method and then let's ask what the
- 41:47user's favorite color is enter your
- 41:51favorite
- 41:53color we will store that within a
- 41:55variable named color and it's going to
- 41:57be of the string data type string color
- 41:59equals use our scanner object use the in
- 42:03next line
- 42:05method then we'll output the
- 42:08following system.out.print line you are
- 42:13Plus
- 42:15Age years
- 42:18old as well
- 42:20as you like the
- 42:24color plus our variable color
- 42:28here's what
- 42:35happens enter your age 25 hit
- 42:39enter enter your favorite color you are
- 42:4225 years old you like the color so
- 42:45what's going on here exactly so when we
- 42:48type in a number for example 25 then hit
- 42:51enter within the input buffer there's
- 42:54still a new line character because we
- 42:56hit enter
- 42:57so the next line method is picking up
- 42:59that new line character and using that
- 43:02as the
- 43:03input so we need to get rid of that new
- 43:05line character this is a common problem
- 43:08that you might see in Java if you accept
- 43:10an integer or a double then accept a
- 43:13string of text so one way in which we
- 43:15can clear the input buffer to get rid of
- 43:18that new line character is after
- 43:20accepting an integer or a double we can
- 43:22use our scanner and call the next line
- 43:26method but don't assign it to anything
- 43:29that should get rid of that new line
- 43:30character in the input buffer if that
- 43:32problem comes up so let's try this again
- 43:35enter an age enter a color you are 25
- 43:39years old you like the color red so if
- 43:43you encounter that problem I would just
- 43:45call the next line method of your
- 43:47scanner object to clear the input buffer
- 43:50now we're going to cover an
- 43:52exercise in this exercise we're going to
- 43:55calculate the area of a rectangle
- 43:57what I like to do when creating a
- 43:59project is declare all of my variables
- 44:01at the top again make sure that you're
- 44:04importing the scanner class I will
- 44:06create a double variable of width I'll
- 44:11set that to be
- 44:12zero and a double variable of height
- 44:16also
- 44:18zero and double
- 44:21area I will set that to be
- 44:24zero we're going to accept some user
- 44:26input
- 44:27and reassign width and
- 44:30height so we need a scanner object to
- 44:33accept user input scanner scanner equals
- 44:36new scanner parentheses semicolon within
- 44:40the parenthesis we will type
- 44:44system.in and again since we're opening
- 44:46a scanner it's good practice to close it
- 44:49I sometimes forget to do that scanner.
- 44:52close we'll create a prompt to tell the
- 44:55user to enter in a width we can use
- 44:58system.out.print line or print for
- 45:01prompts I like to use
- 45:03print enter the
- 45:07width we will take our width we've
- 45:10already declared our width once we don't
- 45:11need to type the data type again we can
- 45:14reuse it width equals use our scanner
- 45:19object use the an next Double method
- 45:22because we're accepting a double a width
- 45:27let's do this with height I'll just copy
- 45:29these two lines then change width to
- 45:38height all right let's test it
- 45:42currently let's make up some numbers 3.1
- 45:45and
- 45:464.2 all right once we have the width and
- 45:49the height we have to calculate the area
- 45:53so we're going to take our area variable
- 45:56equals
- 45:57our width times our
- 46:02height and then let's output
- 46:05it system.out.print line the area
- 46:10is plus our variable
- 46:14area so let's say 3.2 and
- 46:194.3 the area is
- 46:27after area We'll add centimeters you
- 46:30could do centimet squared if you prefer
- 46:33or you could even add a superscript of
- 46:35two so if you're on Windows make sure
- 46:37num lock is on hold alt then type 0178
- 46:42on the
- 46:43numpad if you would like a superscript
- 46:45of two you know because we're working
- 46:48with
- 46:49areas so let's say
- 46:525.3 and
- 46:556.4 the area is is
- 47:27we're going to create a game of Mad Libs
- 47:29Mad Libs is a game where you have a
- 47:31story and a user fills in different
- 47:33words the result is that it gives you a
- 47:35story that's really silly or doesn't
- 47:37necessarily make sense at times but
- 47:39that's what we'll be doing in this video
- 47:41it will help us practice accepting user
- 47:43input since we're accepting user input
- 47:45we'll need to import the scanner class
- 47:47at the top of our Java file import
- 47:52java.util.scanner
- 47:54a scanner is going to help us accept
- 47:57user input we'll declare that and assign
- 48:00it scanner scanner equals new scanner
- 48:04parenthesis
- 48:05semicolon then within the parenthesis we
- 48:08will type
- 48:11system.in when writing a program I like
- 48:13to declare all of my variables first all
- 48:16these variables are going to be of the
- 48:17string data type we will need an
- 48:20adjective you're going to get an English
- 48:22lesson today
- 48:24too adjective one an adjective describes
- 48:28something like Fast slow cheap
- 48:32expensive we'll need a noun string noun
- 48:37one a noun is a person place or thing
- 48:41we'll need another
- 48:43adjective adjective
- 48:45two we'll need a verb a verb is an
- 48:49action like sleeping or running verb one
- 48:54then another adjective a again an
- 48:57adjective describe something all right
- 49:00so let's work on our
- 49:02story we'll have four
- 49:10lines so for the first line we'll say
- 49:14today I went to
- 49:18a we'll insert an adjective adjective
- 49:21one we're going to describe a zoo that
- 49:24we went to
- 49:27we haven't asked for user input yet
- 49:29we're getting a warning that this
- 49:31variable has not been initialized that's
- 49:34okay we'll take care of that
- 49:36soon so today I went to a a description
- 49:40an adjective
- 49:42zoo in an
- 49:47exhibit I saw
- 49:50a plus a noun a noun is a person place
- 49:55or thing like a gorilla or
- 49:59harambe then we'll add some punctuation
- 50:02a
- 50:03period we'll say noun
- 50:08one
- 50:12was plus adjective
- 50:18two
- 50:21and our verb verb one then add some
- 50:25punctuation
- 50:30so we could say that harambe was big and
- 50:35sleeping so a verb is an
- 50:40action I
- 50:43was plus adjective
- 50:46three then add some
- 50:48punctuation all right now we just need
- 50:50to accept user input so let's do that
- 50:54after assigning the variables we'll need
- 50:56five print
- 51:06statements we will prompt the user to
- 51:09enter an
- 51:11adjective enter an adjective I'll give a
- 51:14hint that an adjective is a
- 51:16description we're describing
- 51:21something enter a
- 51:25noun enter a
- 51:29noun I'll give a hint that we are
- 51:31looking for
- 51:33a animal or
- 51:40person we'll need another adjective we
- 51:43can just copy this
- 51:44line then we need a
- 51:46verb enter a verb and we want it in
- 51:50present tense so ending with ing like
- 51:58running I'll give a hint that a verb is
- 52:01an
- 52:02action and then another
- 52:05adjective all right now we just need to
- 52:07assign these variables we've already
- 52:09declared
- 52:10them adjective one equals take our
- 52:14scanner use the next line
- 52:19method and really we can just copy this
- 52:22line paste it and then change what we're
- 52:24assigning it to
- 52:27we're accepting strings for each of
- 52:29these lines we have adjective one noun
- 52:33one adjective
- 52:352 verb one and adjective
- 52:393 all right now the last thing that I'm
- 52:41forgetting since we opened up a scanner
- 52:44it is good practice to close it at the
- 52:46end of our file if we don't it can lead
- 52:48to unexpected Behavior if we leave any
- 52:50open
- 52:51resources scanner. close all right we
- 52:55are ready to run this
- 52:57I'm going to prefix a new line character
- 52:59before displaying our
- 53:01story just so that it's not so close to
- 53:03the prompts when outputting
- 53:05it enter an adjective a
- 53:09description I'm going to say
- 53:13suspicious enter a noun so I like making
- 53:17fun of Mark Zuckerberg I'm going to
- 53:19write the name of a person Mark
- 53:24Zuckerberg enter an adjective
- 53:27smelly enter a verb ending with
- 53:30ing
- 53:32screaming enter an adjective
- 53:36happy and here's our Mad Libs game today
- 53:39I went to a suspicious zoo in an exhibit
- 53:43I saw a Mark
- 53:44Zuckerberg Mark Zuckerberg was smelly
- 53:47and screaming I was happy so for your
- 53:51Mad Libs game post your results in the
- 53:53comment section because I would like to
- 53:55read them I want to see what you you
- 53:56guys wrote or if you wrote a different
- 53:58story post that too and well everybody
- 54:01that is a simple Mad Libs game that we
- 54:04can write using
- 54:07Java all right everybody so we got a
- 54:10really important topic to discuss today
- 54:12although I can be a little dry and that
- 54:14is the use of different arithmetic
- 54:15operators in Java when it comes to
- 54:17programming you got to math sometimes
- 54:20it's a pretty important topic so let's
- 54:22start with some basic addition in this
- 54:24demonstration we will have three
- 54:25variables X will equal 10 y will equal 2
- 54:31and Z will store the result we'll
- 54:34declare it but not yet assign it so for
- 54:36basic addition we'll store our result
- 54:39within z z equals X for addition you can
- 54:42use the plus sign
- 54:45Y what is 10 + 2 then we'll display
- 54:49it we're storing the result within
- 54:53Z which gives us 12
- 54:58then we have
- 54:59subtraction z = x -
- 55:03y 10 - 2 is
- 55:078
- 55:09multiplication for multiplication we're
- 55:12going to use an asterisk 10 *
- 55:152 is
- 55:1820 for division we use a forward slash z
- 55:22= x / y 10 /
- 55:282 is
- 55:305 there's also the modulus operator
- 55:32which is a percent sign that would give
- 55:35you the remainder of any
- 55:36division Z is going to store the
- 55:39remainder this time x modulus y so
- 55:44modulus is a percent sign 10 divides by
- 55:47two evenly so the remainder is going to
- 55:49be
- 55:51zero but if we divided 10 by three that
- 55:55doesn't divide evenly
- 55:5710 modulus 3 gives us a remainder of one
- 56:01the modulus operator is really useful if
- 56:03you need to determine if a number is
- 56:05even or not or when working with time
- 56:07you need to see if something is
- 56:09divisible by 60 seconds or 60
- 56:13minutes we'll get more into that later
- 56:15but you should at least know what the
- 56:16modulus operator
- 56:18is now we have something called
- 56:20augmented assignment operators instead
- 56:23of storing the result within Z this time
- 56:25we're going to reassign the result to
- 56:27variable X so let's say we would like to
- 56:30add 10 and 3 together but store it
- 56:33within X well we could say x = x + y
- 56:38this would technically
- 56:41work so 10 + 3 is 13 but there is a
- 56:46shortcut though that we could use and
- 56:48that is by using augmented assignment
- 56:50operators we'll condense some of these
- 56:53steps instead of saying x = x + y we
- 56:56simply say x + = y and that does the
- 57:01same
- 57:02thing 10 + 3 is
- 57:0513 there's also
- 57:08subtraction x = X -
- 57:13y 10 - 3 is 7 but we could condense it
- 57:17to be x - equal
- 57:21y that gives us
- 57:247 or multiplication x = x * y 10 * 3 is
- 57:3130 but we can condense it to be x * =
- 57:37y that is also
- 57:4030 for division x = x /
- 57:46y that gives us three now one thing to
- 57:49pay attention to with division all right
- 57:51so 10 / 3 should be 3.33 repeating right
- 57:55well we're using something called
- 57:57integer division we're not able to store
- 57:59the decimal portion because we're
- 58:01working with whole integers so that's
- 58:03something to pay attention to when
- 58:04you're using integer Division if we were
- 58:07working with
- 58:09doubles then we would retain that
- 58:11decimal portion so do pay attention to
- 58:14that we'll discuss that in another video
- 58:16when it comes to typ casting for the
- 58:18augmented assignment version that would
- 58:20be x / = Y and let's change that to be 2
- 58:26so 10 / 2 is
- 58:295 then we have the modulus operator x =
- 58:34x modulus y 10 divides by 2
- 58:39evenly X will be zero the augmented
- 58:43assignment version of this would be X
- 58:45modulus equals
- 58:47y and that would also be 0o but if y
- 58:51were
- 58:52three then that would be one now we have
- 58:55increment and decrement operators you
- 58:58see this a lot with looping structures
- 59:00let's say that x equal 1 this time
- 59:04instead of saying x = x + 1 which would
- 59:08give us 2 or even the augmented
- 59:11assignment inversion which would be X+ =
- 59:161 well there's even a shortcut for this
- 59:19if we're only incrementing by one and
- 59:22that is
- 59:24x++ we're incrementing X by
- 59:27one you tend to see this in a lot of
- 59:29looping structures but that's a topic
- 59:31for another day each time I increment X
- 59:34by
- 59:351 it's going to be retained so now x = 3
- 59:40x ++ again well now X is
- 59:444 now there's also the decrement
- 59:47operator x = X - one of course that's
- 59:52going to be zero this
- 59:54time so to decrement you can use x -
- 59:59minus so now X is zero let's do it again
- 1:00:03X
- 1:00:04-- now X is -1 x
- 1:00:08-- X is
- 1:00:11-2 so those are the increment and
- 1:00:14decrement operators you'll see these
- 1:00:16again when we reach the topic on Loops
- 1:00:19the last thing I need to discuss today
- 1:00:21is the order of operations and there's
- 1:00:23an acronym PEMDAS it's parentheses
- 1:00:26exponents multiplication division
- 1:00:29addition then subtraction in that order
- 1:00:32you may hear the phrase to remember this
- 1:00:34please excuse my dear Aunt Sally but I
- 1:00:37like the phrase please excuse my dope
- 1:00:40ass swag that's my version that I like
- 1:00:42to use let's say we have the equation
- 1:00:45double result equals 3 + 4 time
- 1:00:52parentheses 7 - 5 divided by
- 1:00:572.0 I'm dividing by 2.0 because right
- 1:01:01now we're using integer division which
- 1:01:03we previously talked about so I'm going
- 1:01:05to use 2.0 so we retain that decimal
- 1:01:07portion again that's a topic for another
- 1:01:10day let's display the
- 1:01:13result so I don't know what the result
- 1:01:15is but it is 7
- 1:01:207.0 in what order do we solve this
- 1:01:22equation exactly well we can use that
- 1:01:24acronym of pendos
- 1:01:26from the left to the right we solve
- 1:01:28anything within parentheses first so
- 1:01:30going from the left to the right we skip
- 1:01:33over any addition multiplication we're
- 1:01:35looking for parentheses which we have
- 1:01:37right here if you enclose part of an
- 1:01:39equation within parentheses you would
- 1:01:41solve that first 7 - 5 is 2 there's no
- 1:01:46more
- 1:01:48parentheses then we check any exponents
- 1:01:51there are no
- 1:01:52exponents
- 1:01:54multiplication well we have
- 1:01:56multiplication right here we would solve
- 1:01:57that next 4 * 2 is 8 there's no more
- 1:02:02multiplication then we have
- 1:02:04division we have 8 / 2 which gives us
- 1:02:084 then any addition 3 + 4 is 7 and that
- 1:02:14was our original result and then you
- 1:02:16would do any subtraction last so that is
- 1:02:18the order of operations when it comes to
- 1:02:20arithmetic operators all right everybody
- 1:02:23so that is some basic arithmetic and
- 1:02:25different operators you can use in
- 1:02:28Java hey everybody so in this video
- 1:02:31we're going to create a shopping cart
- 1:02:33program a user is going to be able to
- 1:02:34type in an item set a price for each and
- 1:02:37then ask for a quantity the output is
- 1:02:40that it's going to give you a total
- 1:02:41based on the price and how much you're
- 1:02:43buying this would be a good exercise for
- 1:02:45us to get us more comfortable with
- 1:02:46accepting user input all right let's get
- 1:02:48started everybody we'll need the help of
- 1:02:50a scanner the scanner allows us to
- 1:02:53accept user input we'll need to import
- 1:02:55that at the at the top so be sure that
- 1:02:56you have this line of code import
- 1:03:00java.util.scanner
- 1:03:03we'll create a scanner object scanner
- 1:03:07scanner equals new scanner parentheses
- 1:03:11semicolon type
- 1:03:14system.in then if we open a scanner it's
- 1:03:17good practice to close it when we're
- 1:03:19done with it scanner. close if you leave
- 1:03:23resources open it can lead to unexpected
- 1:03:25behavior when creating a project I like
- 1:03:28to declare all of my variables at the
- 1:03:30top let's say we have an item name that
- 1:03:33data type is going to be a string string
- 1:03:36item what are we buying
- 1:03:38exactly we need a price that would be a
- 1:03:41double because there might be dollars
- 1:03:44and cents a quantity how many of
- 1:03:48something are we buying int quantity
- 1:03:51pick a unit of currency this could be a
- 1:03:54character char
- 1:03:56currency I'm going to initialize this
- 1:03:59right away American
- 1:04:01dollars and then a total we're going to
- 1:04:04multiply the price by the quantity to
- 1:04:07give a
- 1:04:08total double total now if you would like
- 1:04:12you can initialize some of these right
- 1:04:14away and assign them for example let's
- 1:04:16say we are at a pizza
- 1:04:19restaurant I could set the item to be
- 1:04:21pizza right
- 1:04:22away or within this program we could
- 1:04:25fill it in then let's create some
- 1:04:28prompts I'll use
- 1:04:33system.out.print what item would you
- 1:04:36like to
- 1:04:38buy then let's accept user input we're
- 1:04:42going to assign our string of item equal
- 1:04:44to use our scanner object and use the in
- 1:04:48next line
- 1:04:50method then let's just print it to
- 1:04:53verify that it's working
- 1:04:58what item would you like to buy maybe
- 1:05:00this time I am buying a hamburger that
- 1:05:03will output hamburger we know that we
- 1:05:06can get user input so let's delete this
- 1:05:08line I was just testing it let's ask for
- 1:05:12a
- 1:05:13price you could set an initial
- 1:05:16price if you would
- 1:05:19like or we could ask for one depending
- 1:05:21on how you want to write this
- 1:05:23program so let's system. about the
- 1:05:28print what is the price for
- 1:05:34each if we're working with a price a
- 1:05:37double we'll use the next Double method
- 1:05:40we're going to assign our price equal to
- 1:05:44use our scanner. next
- 1:05:48Double and then let's test it by
- 1:05:50outputting the
- 1:05:54price okay let's say I'm buying a pizza
- 1:05:57this time what is the price for each
- 1:06:00$4.99 and that outputs
- 1:06:03$4.99 so it is a good idea every once in
- 1:06:06a while to test your code just to be
- 1:06:09sure that it all works as you go
- 1:06:11along then we'll need a
- 1:06:16quantity let's ask how many would you
- 1:06:22like we will assign our quantity
- 1:06:24variable
- 1:06:26equals
- 1:06:28scanner. next int since we're working
- 1:06:31with whole numbers this
- 1:06:33time then let's test our quantity to see
- 1:06:36if it
- 1:06:38works I don't know maybe I'm buying a
- 1:06:40hot dog this time what is the price per
- 1:06:42each
- 1:06:43$4.99 how many would you like I would
- 1:06:46like 12 hot dogs
- 1:06:4912 and I'm just going to make sure I'm
- 1:06:51using print instead of print line just
- 1:06:53because I think it looks better
- 1:06:57now we'll create a total we don't need
- 1:06:59user input for that we're going to use
- 1:07:02the variables that we've already had
- 1:07:04after assigning them total equals the
- 1:07:08price times the
- 1:07:13quantity and then I will just test the
- 1:07:19total maybe I'm buying a pizza or
- 1:07:22pizzas each is $4.99
- 1:07:26and I buying three of
- 1:07:28them
- 1:07:3144.97 instead let's output the
- 1:07:36following I'm going to use a new line
- 1:07:38character just to separate our output
- 1:07:40from the
- 1:07:41prompts you have
- 1:07:45bought plus our variable of
- 1:07:51quantity plus a space character plus our
- 1:07:55variable
- 1:07:57item but it might be more than one so
- 1:07:59let's say/ s it might be pizza or
- 1:08:05pizzas then we will display the price
- 1:08:08including the unit of
- 1:08:11currency your total
- 1:08:15is I'll add our unit of currency it's a
- 1:08:19character I picked American dollars but
- 1:08:22pick whatever you would like plus our
- 1:08:25total
- 1:08:26that we have
- 1:08:28calculated all right let's see the
- 1:08:31result what item would you like to buy I
- 1:08:33would like to buy a pizza what is the
- 1:08:36price for each
- 1:08:38$4.99 how many would you like I would
- 1:08:41like eight pizzas maybe it's for a party
- 1:08:44you have bought eight pizzas your total
- 1:08:47is
- 1:08:51$19.92 all right everybody so that is a
- 1:08:54shopping cart program that you can write
- 1:08:55as an exercise using
- 1:08:59Java hello everybody today we're going
- 1:09:01to discuss if statements in Java an if
- 1:09:04statement performs a block of code if
- 1:09:07its condition is true we've discussed
- 1:09:10how Boolean variables can be used within
- 1:09:12if statements we may be a little
- 1:09:14familiar with them already in this
- 1:09:16demonstration we will have a variable of
- 1:09:19age set it equal to be some
- 1:09:22age age will be an integer I would like
- 1:09:25to check if my variable of age is
- 1:09:27greater than or equal to 18 if so we can
- 1:09:30print a message that you are an adult to
- 1:09:33write an if statement will type if
- 1:09:35parentheses curly braces within the set
- 1:09:38of curly braces we can check a condition
- 1:09:40to see if it's true so let's check to
- 1:09:43see if age is greater than or equal to
- 1:09:5018 if that's true we'll print the
- 1:09:53following you are an
- 1:09:59adult that would result in US outputting
- 1:10:02you are an
- 1:10:04adult now if this condition was false if
- 1:10:08it's not true let's say that my age is
- 1:10:1212 well then we skip over the if
- 1:10:15statement we check if this condition is
- 1:10:17true if it is we perform this code if
- 1:10:20not we skip it alternatively we could
- 1:10:24add an else clause
- 1:10:26this would be another statement we would
- 1:10:28add else if this is false then do
- 1:10:31something else so let's output the
- 1:10:34following you are a
- 1:10:38child since the age is 12 we would
- 1:10:42output you are a
- 1:10:46child there's also an else if Clause so
- 1:10:49we have one of two
- 1:10:51possibilities do this or do that now we
- 1:10:54could add something called else if
- 1:10:57Clause let's check else
- 1:11:00if age is less than
- 1:11:04zero if this statement is
- 1:11:07true then let's say you haven't been
- 1:11:12born
- 1:11:14yet now we're doing one of three
- 1:11:18things but it all depends on these
- 1:11:21conditions our else Clause is kind of
- 1:11:24like the default if none of the above
- 1:11:26conditions are
- 1:11:27true so let's test this my age variable
- 1:11:31is set to
- 1:11:3219 this would display you are an
- 1:11:35adult if my age variable was -1 we would
- 1:11:40display you haven't been born
- 1:11:42yet this statement was false so we go
- 1:11:45down to the next
- 1:11:47one this is true NE 1 is less than zero
- 1:11:51so we do
- 1:11:52this or if I was 10
- 1:11:56well then these top two conditions are
- 1:11:58both false so we do whatever is within
- 1:12:00the lse
- 1:12:01statement you can add as many else if
- 1:12:04Clauses as you would
- 1:12:05like so here's another
- 1:12:09one else
- 1:12:12if our variable age is equal to zero
- 1:12:17exactly double equals is the comparison
- 1:12:20operator we're checking if a variable or
- 1:12:23a value is equal to another value or
- 1:12:27variable you don't want to use single
- 1:12:29equals because that's the assignment
- 1:12:31operator Java thinks we're trying to
- 1:12:33assign a variable when making a
- 1:12:36comparison you use double
- 1:12:38equals is our variable age equal to
- 1:12:42zero then we'll print something else
- 1:12:45okay we're going to print you are a
- 1:12:50baby I will set variable H to be zero
- 1:12:55you are a baby because our variable age
- 1:12:59is equal to zero
- 1:13:02exactly let's add another L
- 1:13:05statement there's something I want to
- 1:13:07demonstrate let's check to
- 1:13:10see if age is greater than or equal to
- 1:13:1665 then we will print you are a
- 1:13:22senior I'll set age to be 70
- 1:13:26here's the
- 1:13:27result you are an adult so why did we
- 1:13:30print this statement rather than this
- 1:13:33one well with these if statements we
- 1:13:36check them from the top down this
- 1:13:39condition is technically true as well as
- 1:13:41this one 70 is greater than or equal to
- 1:13:4418 so we execute this code and skip over
- 1:13:48the rest even though this one is also
- 1:13:50true you do have to pay attention to the
- 1:13:52positioning of your if and else if
- 1:13:54statements in this situation it would be
- 1:13:56better if we were to check to see if
- 1:13:58somebody's age was greater than or equal
- 1:14:00to 65 first so let's turn this into the
- 1:14:03if
- 1:14:08statement and then where we check if age
- 1:14:10is greater than or equal to 18 we will
- 1:14:12use an else if Clause instead now this
- 1:14:15should
- 1:14:17work you are a
- 1:14:20senior but if the age was 50
- 1:14:26well then you are an adult now we're
- 1:14:28going to modify this program
- 1:14:31slightly this time we will ask for user
- 1:14:33input we'll need the help of a scanner
- 1:14:36at the top of our Java file we're going
- 1:14:38to import
- 1:14:41java.util dothe class of
- 1:14:44scanner we'll need to create a scanner
- 1:14:46object scanner scanner equals new
- 1:14:51scanner within the set of parentheses
- 1:14:53type system.in
- 1:14:56then when we open a scanner it is a good
- 1:14:58idea to close it when we're done with
- 1:15:00it just so we don't leave any resources
- 1:15:04open with our variable of age we're
- 1:15:07going to create a prompt to have a user
- 1:15:08enter in their age I'm going to declare
- 1:15:11a variable of age but we'll assign it
- 1:15:13later let's create a
- 1:15:16prompt enter your
- 1:15:18age I'll use print instead of print
- 1:15:22line and then we will assign age equal
- 1:15:26to scanner. next
- 1:15:30int okay let's try this
- 1:15:33again and to your age let's say that I'm
- 1:15:3721 you are an
- 1:15:40adult what if I was 99 you are a
- 1:15:46senior I am negative 1 years old you
- 1:15:49haven't been born
- 1:15:52yet I am zero exactly you are a
- 1:15:56baby or maybe I'm 10 you are a
- 1:16:01child so with if statements we check a
- 1:16:04condition if it's true we do it we do
- 1:16:07this code if not we don't otherwise we
- 1:16:10can check another condition with else if
- 1:16:13and if none of these conditions are true
- 1:16:15we can perform whatever is within an
- 1:16:17else statement else acts like the
- 1:16:19default code if none of the above
- 1:16:21conditions are true now this time we're
- 1:16:23going to work with strings
- 1:16:26we're going to create a variable of name
- 1:16:28and it's going to be a string we'll
- 1:16:30declare it but not yet assign
- 1:16:33it we will prompt the user to enter in
- 1:16:36their name I will use print instead of
- 1:16:38print
- 1:16:39Line enter your
- 1:16:43name we will assign our variable of name
- 1:16:47equal to scanner. next line to get a
- 1:16:52line of text
- 1:16:55this group of if else if and else
- 1:16:58statements let's set this to be group
- 1:17:01two we're going to create another
- 1:17:05set this will be group one for the
- 1:17:09name when somebody enters in their name
- 1:17:12they could skip the prompt like this
- 1:17:15enter your name I'm just going to hit
- 1:17:17enter to skip
- 1:17:19it we will check if our name variable is
- 1:17:22empty here's how
- 1:17:26if we'll check if our name is empty
- 1:17:29there's a string method for this we
- 1:17:30haven't discussed string Methods yet
- 1:17:33instead of saying name is equal to an
- 1:17:35empty space there is a built-in method
- 1:17:38of strings and that is to take our
- 1:17:39variable name dot is
- 1:17:44empty this is another way to check to
- 1:17:46see if a string is empty this will be
- 1:17:48our condition is empty will give you a
- 1:17:51Boolean of true or false
- 1:17:55so let's print the following if our name
- 1:17:57variable is
- 1:17:58empty let's say you didn't enter your
- 1:18:05name else if our name is not empty that
- 1:18:09means somebody typed in their name let's
- 1:18:11say hello plus name and I'll add an
- 1:18:16exclamation
- 1:18:18point okay let's try this
- 1:18:22again enter your name if I hit enter
- 1:18:25we'll perform then whatever is within
- 1:18:26the if statement then enter an age you
- 1:18:30didn't enter your name you are an adult
- 1:18:33this time let's type in our name our
- 1:18:34first
- 1:18:37name hello whatever your name is you are
- 1:18:41an
- 1:18:41adult this time we have two groups of if
- 1:18:44else statements group one is for the
- 1:18:47name group two is for the user's age
- 1:18:50this time let's create a
- 1:18:52Boolean Boolean is
- 1:18:57student then we'll create a
- 1:19:00prompt let's ask are you a student but
- 1:19:04we need a value of true or
- 1:19:08false cuz booleans can only be true or
- 1:19:11false
- 1:19:13normally is student equals use our
- 1:19:16scanner to get some user input next
- 1:19:22Boolean now we'll create another group
- 1:19:24of if and lse if
- 1:19:26statements group
- 1:19:29three we need an if
- 1:19:34statement so with booleans we could say
- 1:19:37is student is equal to true but there is
- 1:19:40a way to simplify this a
- 1:19:42shortcut you can just say is student it
- 1:19:46holds a direct value of true or false we
- 1:19:49don't necessarily need to write like a
- 1:19:51whole condition
- 1:19:54so if we're a student we'll
- 1:19:57output you are a
- 1:20:00student else if a student is
- 1:20:04false then we'll output you are not a
- 1:20:09student we have three groups of if else
- 1:20:12statements group three is for is
- 1:20:16student group two is for the
- 1:20:20age group one is for the
- 1:20:23name just to make this more fun I'm
- 1:20:26going to add some emojis I think it
- 1:20:28would make it more interesting but you
- 1:20:30don't have to so if somebody doesn't
- 1:20:32enter their name I'm going to add a
- 1:20:34pouting
- 1:20:35face if somebody enters their name I
- 1:20:38will add a smiley
- 1:20:40face if somebody is a senior maybe we
- 1:20:44can find an emoji for a
- 1:20:48grandpa let's find one for an
- 1:20:52adult you haven't been born yet uh we
- 1:20:55could do like a spirit or
- 1:21:00something you are a
- 1:21:04baby you are a
- 1:21:10child you are a student we could do a
- 1:21:17school you are not a student we could do
- 1:21:20an
- 1:21:21office all right so this is our final
- 1:21:24program
- 1:21:26again we have three groups of if
- 1:21:27statements this time one for the name
- 1:21:30one for the age and one for is student
- 1:21:34and then for the prompt for are you a
- 1:21:35student I'm going to use print instead
- 1:21:37of print
- 1:21:39line here's the result enter your name
- 1:21:43enter your age are you a
- 1:21:49student hello your name you are an adult
- 1:21:52you are not a student
- 1:21:55let's try some of the other statements
- 1:21:56I'm not going to enter a name for the
- 1:21:59age let's say that I'm
- 1:22:01ne1 are you a student
- 1:22:04true you didn't enter your name you
- 1:22:07haven't been born yet you are a
- 1:22:09student so those are if statements
- 1:22:12everybody they perform a block of code
- 1:22:15if its condition is true check a
- 1:22:18condition if it's true do this if not
- 1:22:21you can do something else and well
- 1:22:23everybody that summar mizes if
- 1:22:25statements in
- 1:22:27Java hey everybody in this video I'm
- 1:22:30going to show you how we can work with
- 1:22:31the random class and generate some
- 1:22:33random numbers let's begin to work with
- 1:22:35random numbers we'll need to import the
- 1:22:37random class at the top of our Java file
- 1:22:40we will import java. u.
- 1:22:45random much like what we do with the
- 1:22:47scanner class we're going to follow a
- 1:22:49similar pattern for creating a random
- 1:22:51object much like a scanner object we
- 1:22:54will type the name of the class random
- 1:22:57the name of the random object random
- 1:23:00equals new
- 1:23:02random so random random equals new
- 1:23:07random let's say we have an integer an
- 1:23:10integer variable of number I'm going to
- 1:23:13assign our number variable equal to and
- 1:23:17here's where we'll generate a random
- 1:23:18number we will take our random object
- 1:23:21then we will use the next int method
- 1:23:25this will generate a random number
- 1:23:27however there's one issue with this I'm
- 1:23:30going to print our
- 1:23:32number the number is going to be very
- 1:23:36large so the random number that's
- 1:23:39generated it's going to be within a
- 1:23:41range between just about -2 billion and
- 1:23:44positive2 billion I'm guessing you don't
- 1:23:46want a number that big So within the
- 1:23:49next integer method we can set some
- 1:23:52bounds if I would like a random number
- 1:23:54between 1 and six like I'm rolling a
- 1:23:56dice or die I would say 1 comma 6 this
- 1:24:01is actually going to give us a random
- 1:24:02number between 1 and
- 1:24:07five the first number is inclusive the
- 1:24:11second number is exclusive no matter how
- 1:24:14many times I rerolled this die I'm never
- 1:24:16going to get
- 1:24:20six since this number is exclusive I'm
- 1:24:23going to inrease this to be seven that
- 1:24:26will give us 1 through
- 1:24:32six and there's six let's say this time
- 1:24:35you would like three random numbers
- 1:24:37you're rolling three dice let's say we
- 1:24:39have number one number two and number
- 1:24:43three all
- 1:24:45integers we'll assign number one with a
- 1:24:48random integer and we'll do this with
- 1:24:50numbers two and three as well number one
- 1:24:53number two number number
- 1:24:55three then we will print them number one
- 1:24:59number two number
- 1:25:02three 2
- 1:25:0546 23 4
- 1:25:08225 good enough for random numbers
- 1:25:11between 1 and 100 the second number is
- 1:25:13going to be 101 because it's
- 1:25:17exclusive so now we're generating three
- 1:25:20random numbers between 1 and 100
- 1:25:30not only can you generate random numbers
- 1:25:32you can Generate random
- 1:25:37doubles so let's set this back to be
- 1:25:42number let's change the data type and
- 1:25:44number to be
- 1:25:49double we're going to use the next
- 1:25:52Double method
- 1:25:55this is going to generate a random
- 1:25:57number between zero and
- 1:25:59one just the decimal portion
- 1:26:08really you can also generate a Boolean
- 1:26:11this might be good in a situation where
- 1:26:13you need to flip a coin let's say we
- 1:26:16have a Boolean of is
- 1:26:19heads when we flip a coin is it heads
- 1:26:25random. nextext
- 1:26:28Boolean then we will display if the coin
- 1:26:31we flip is heads or not is it heads or
- 1:26:34it's Tails that's true it is
- 1:26:37heads now it's false it's
- 1:26:40Tails we could even use an if
- 1:26:44statement if is heads if our coin flip
- 1:26:48is on
- 1:26:49heads let's
- 1:26:51print heads
- 1:26:54else we will
- 1:26:57print
- 1:27:00Tails so this will simulate us flipping
- 1:27:03a
- 1:27:09coin we're getting a lot of
- 1:27:18Tails all right everybody that is how
- 1:27:20you can Generate random numbers and use
- 1:27:22the random module in Java
- 1:27:28hey everybody in this video we're going
- 1:27:30to cover some useful math related
- 1:27:32constants and methods that you may be
- 1:27:34interested in as a beginner then at the
- 1:27:35end of this topic we'll cover a few
- 1:27:37exercises let's begin if at any time you
- 1:27:40need the value of pi there's a built-in
- 1:27:42constant of the math class so I'm going
- 1:27:45to print the
- 1:27:46following math with a capital M Pi all
- 1:27:51capital so that would give you the value
- 1:27:553.14 and some change so instead of
- 1:27:58creating a variable for pi it's always
- 1:28:00available to you within the math class
- 1:28:02you just have to access it there's also
- 1:28:07E math. capital
- 1:28:12E this is the exponential constant also
- 1:28:15known as ul's number you may never use
- 1:28:18this but it's available to you in case
- 1:28:20you do so again access the math class
- 1:28:23Dot to name the
- 1:28:25constant this time let's create a double
- 1:28:28variable named result here's a few
- 1:28:30methods I'm going to reassign result
- 1:28:33equal to access math dot to raise a base
- 1:28:38to a given power you can use the pal
- 1:28:41method let's raise 2 to the^ of 3 then
- 1:28:45print
- 1:28:46it let's print our variable result 2 ^
- 1:28:51of 3 would be 8
- 1:28:55and again I'm using a double that's why
- 1:28:57we have the decimal portion 2^ of 4
- 1:29:00would
- 1:29:01be 16 2 the^ of
- 1:29:055 is
- 1:29:0732 po meaning power you can raise a base
- 1:29:11to a given power these can be values or
- 1:29:14variables there's the absolute value
- 1:29:17method math.abs for absolute value
- 1:29:22what's the absolute value
- 1:29:27of5 that would be five positive 5
- 1:29:30basically speaking the absolute value
- 1:29:32method gives you that number but
- 1:29:34positive if it's negative think of it as
- 1:29:36the distance away from zero if that
- 1:29:39helps for square root there's a square
- 1:29:42root method result equals
- 1:29:45math.sqrt
- 1:29:46what is the square root of 9 I did make
- 1:29:50a spelling mistake here if you're
- 1:29:52accessing the math class make sure it's
- 1:29:54with a capital m so the square root of 9
- 1:29:58would be
- 1:30:023 we can also round a
- 1:30:05number again access the math class use
- 1:30:08the round
- 1:30:12method let's round
- 1:30:143.14 round will round a number to the
- 1:30:17nearest whole
- 1:30:19integer again we're working with the
- 1:30:21double though so that gives us
- 1:30:263.0 to always round up there's the
- 1:30:30ceiling
- 1:30:32method math. c
- 1:30:35l again I accidentally used lowercase M
- 1:30:38it's capital M let's round 3.14 up to
- 1:30:42the
- 1:30:43ceiling so 3.14 rounded up is four to
- 1:30:49always round down you can use the floor
- 1:30:51method
- 1:30:54math.
- 1:30:56floor so this time let's round 3.99 down
- 1:31:01normally it should round up
- 1:31:03right so 3.99 rounded down is
- 1:31:07three you can find the max between two
- 1:31:10values or variables
- 1:31:14math. Max method we'll have two numbers
- 1:31:18which are comma separated 10 and 20 what
- 1:31:21is the max between these two values or
- 1:31:23variables
- 1:31:25the greater number is 20 there's also
- 1:31:29Min I'll just copy this cuz I'm lazy
- 1:31:32replace Max with Min what is the minimum
- 1:31:35value between these two
- 1:31:37values that would be
- 1:31:3910 so those are a few useful math
- 1:31:42methods and constants that you may be
- 1:31:44interested in we're going to cover some
- 1:31:46math related exercises if you would like
- 1:31:48additional practice
- 1:31:54in this first exercise we're going to
- 1:31:56find the hypotenuse of a right triangle
- 1:31:59you can follow this formula C the
- 1:32:02hypotenuse equals the square root of a^
- 1:32:052 one of the sides plus b^2 the other
- 1:32:08side we're going to accept some user
- 1:32:11input we'll need a scanner we need to
- 1:32:14import the scanner class import
- 1:32:17java.util do
- 1:32:20scanner then we'll create a scanner
- 1:32:22object scan scanner scanner equals new
- 1:32:28scanner then place system.in within the
- 1:32:31set of
- 1:32:32parentheses then when we open a scanner
- 1:32:34it is a good idea to close it so we
- 1:32:36don't leave any resources open scanner.
- 1:32:39close we'll prompt the user to enter in
- 1:32:42the length of side A and B let's declare
- 1:32:45those as variables double a double b
- 1:32:50we'll also declare Double C that will
- 1:32:52store the hypotenuse
- 1:32:54Double
- 1:32:56C we'll need two
- 1:32:58prompts I will use print instead of
- 1:33:00print
- 1:33:01Line enter the length of side
- 1:33:09a with inside a within our variable of a
- 1:33:14we'll assign it to scanner. next
- 1:33:19Double let's do this with side B we can
- 1:33:22copy these two lines paste them replace
- 1:33:25a with
- 1:33:28B that is lowercase
- 1:33:31b let's do a test run just to be sure
- 1:33:33that these two work
- 1:33:36temporarily I'm going to print a then
- 1:33:44B enter the length as side a 3 B will be
- 1:33:48four these work I just wanted to test
- 1:33:52that it is a good idea to test your code
- 1:33:54as you're writing it then we have to
- 1:33:57follow this
- 1:33:59equation C equals math. square root of
- 1:34:03a^2 +
- 1:34:05b^2 there is a square root method within
- 1:34:08the math class but we have to raise side
- 1:34:11a to the power of two plus b to the^ of
- 1:34:14two here's how we can do that so within
- 1:34:17the square root method we're going to
- 1:34:19raise a to the power of two first math
- 1:34:24po to raise a base to a given power
- 1:34:27raise a comma to the power of
- 1:34:31two plus again math.pow to raise a base
- 1:34:36to a given power B to the power of
- 1:34:40two and that's it so let's display
- 1:34:43whatever C is the
- 1:34:47hypotenuse
- 1:34:49the
- 1:34:52hypotenuse I'll write that this is C
- 1:34:55side
- 1:34:58C is the
- 1:35:02following plus
- 1:35:07C if side a is three side B is four side
- 1:35:12C should be five which it is if you
- 1:35:15would prefer we can add a unit of
- 1:35:17measurement such as centimet or inches
- 1:35:20so after side C add cm for centimet or I
- 1:35:25for inches whatever you
- 1:35:28prefer so let's try that again side a
- 1:35:31will be four side B will be
- 1:35:34five and side C is this really long
- 1:35:37number we haven't talked about the print
- 1:35:39F statement yet using print F you can
- 1:35:41display a given amount of digits but we
- 1:35:43do have centimeters at the end which is
- 1:35:44what we were looking for so let's try
- 1:35:47three and four and that gives us 5
- 1:35:51cm all right let's cover another
- 1:35:55exercise given the radius of a circle or
- 1:35:58a sphere we'll return the circumference
- 1:36:01the area then the volume that's if we're
- 1:36:04working with the sphere given the radius
- 1:36:07so be sure that we're importing our
- 1:36:08scanner at the top and we'll create a
- 1:36:10scanner object scanner scanner equals
- 1:36:14new
- 1:36:17scanner system.in then be sure to close
- 1:36:20your
- 1:36:22scanner scanner .
- 1:36:25close we'll prompt the user for a
- 1:36:28radius because we need a radius to work
- 1:36:31with we'll create a variable it will be
- 1:36:33a double named radius we'll declare it
- 1:36:36but not yet assign
- 1:36:37it we'll prompt the user using print
- 1:36:40instead of print
- 1:36:42Line enter the
- 1:36:45radius then we will assign our radius
- 1:36:48variable radius
- 1:36:50equals scanner.
- 1:36:53next
- 1:36:56Double once we have a radius we have to
- 1:36:58follow these formulas so we need a
- 1:37:00circumference area and volume let's
- 1:37:03declare those at the top of our program
- 1:37:05but assign them later double
- 1:37:09circumference I think I spelled that
- 1:37:13right double
- 1:37:15area double
- 1:37:18volume we will calculate the
- 1:37:20circumference
- 1:37:24here's the
- 1:37:25Formula 2 times we need pi to work with
- 1:37:30instead of typing
- 1:37:333.14159 so on and so forth we can access
- 1:37:36that from the math class
- 1:37:38math.pi Pi is always available to
- 1:37:42us then multiply this by the
- 1:37:46radius let's perform a test
- 1:37:49run I will display the circumference
- 1:37:54the
- 1:37:55circumference
- 1:37:57is plus our variable of
- 1:38:00circumference then I'll add a unit of
- 1:38:02measurement afterwards centimeters is
- 1:38:04good or whatever you
- 1:38:07prefer enter the radius I'll enter in
- 1:38:10five the circumference is 31.4 CM in the
- 1:38:14future using print F we can limit the
- 1:38:17amount of digits that are displayed I'll
- 1:38:19show you how to do that later on in this
- 1:38:21video then we need the
- 1:38:25area area
- 1:38:27equals here's the formula again we need
- 1:38:31Pi Pi is always available to us math. pi
- 1:38:35times our radius to the^ of two so we
- 1:38:39need the power method of math
- 1:38:42math.pow raise the radius to the power
- 1:38:45of
- 1:38:47two and I'll just copy this line because
- 1:38:49I'm
- 1:38:51lazy the area is our variable area now
- 1:38:56when working with an area the unit of
- 1:38:59measurement is going to be squared so we
- 1:39:01can raise it to the power of two or you
- 1:39:04can use superscript to if you're on
- 1:39:06Windows make sure numlock is on hold alt
- 1:39:10then on the numpad type
- 1:39:120178 for a superscript of two if you
- 1:39:15would like to use that
- 1:39:16instead so now what is the
- 1:39:20area enter the radius 5
- 1:39:23the area is 78.53982
- 1:39:57doubles times math. pi and again make
- 1:40:01sure the m is
- 1:40:04capital times our radius to the^ of
- 1:40:103 math. power method raise our radius to
- 1:40:15the power of
- 1:40:19three then display the volume the volume
- 1:40:24is our variable volume cm
- 1:40:28cubed for a superscript of three make
- 1:40:31sure numb lock is on hold alt then type
- 1:40:350179 that's if you're on
- 1:40:37Windows all right let's print the
- 1:40:41volume again my radius is
- 1:40:44five then we have the
- 1:40:47circumference the area and the
- 1:40:50volume and the unit of measurement is
- 1:40:52cenm cubed now we haven't talked about
- 1:40:55the print F statement we could display
- 1:40:58just a few digits here's
- 1:41:00how again this is a future topic replace
- 1:41:04print line with print
- 1:41:09F instead of adding plus our variable
- 1:41:12name we're going to add a
- 1:41:15placeholder
- 1:41:17percent F to display a double but if you
- 1:41:21would like one digit between the percent
- 1:41:22and F type .1 so let's do this with area
- 1:41:27and
- 1:41:28volume replace the plus with a
- 1:41:34comma then instead of adding plus centim
- 1:41:37after our
- 1:41:38placeholder add
- 1:41:40CM then cm
- 1:41:43squar and cm
- 1:41:46cubed then we can get rid of the portion
- 1:41:48where we add the centimeters to the end
- 1:41:57so let's say the radius is five then we
- 1:42:00should add a new line character to the
- 1:42:01end of each of
- 1:42:12these that looks much
- 1:42:14better we're only displaying one digit
- 1:42:17after the decimal again don't worry
- 1:42:19about this this is a future topic the
- 1:42:21print F statement but if you would only
- 1:42:23like to display one digit that's how you
- 1:42:25can do that again we'll cover this later
- 1:42:29all right everybody so those are a few
- 1:42:30math related constants and methods that
- 1:42:33you may be interested in as a
- 1:42:37beginner what's up everybody in this
- 1:42:39video I'm going to discuss the print F
- 1:42:41statement in Java print f is a method
- 1:42:44used to format output we already do have
- 1:42:46a little bit of experience using print F
- 1:42:49in the last few topics involving math
- 1:42:51we'll get more in depth with the print F
- 1:42:52stat in this topic print f is an
- 1:42:55alternative to print and print line
- 1:42:57wherever we would like to insert a
- 1:42:59variable we can add a placeholder of a
- 1:43:01percent sign then add one of a few
- 1:43:03characters following it to format the
- 1:43:05output in this demonstration let's
- 1:43:07create a few variables let's create a
- 1:43:09string of name I'll assign some name
- 1:43:13I'll pick SpongeBob because many people
- 1:43:15know who SpongeBob is we'll need a
- 1:43:18character let's say character first
- 1:43:21letter equals
- 1:43:24s int age equals
- 1:43:2830 and a double double
- 1:43:32height equals
- 1:43:3660.5 and a Boolean Boolean is
- 1:43:40employed I will set that to be true
- 1:43:43SpongeBob is employed at the K crusty
- 1:43:45Krab so we have some variables they're
- 1:43:48all different data types we can insert
- 1:43:50them into print F statements when we
- 1:43:52output string literal here's a
- 1:43:57demonstration instead of using print
- 1:43:59line I'm going to use print
- 1:44:02F we will display the
- 1:44:05following
- 1:44:06hello then I would like to insert this
- 1:44:08variable of name we're going to use a
- 1:44:10percent sign as a
- 1:44:13placeholder percent then we need a
- 1:44:15specifier our character what is the data
- 1:44:18type of the variable we're trying to
- 1:44:20insert if you're trying to insert a
- 1:44:22string you're going to use a lowercase
- 1:44:25s so after this word of hello we're
- 1:44:28adding a placeholder then inserting a
- 1:44:30string at this location so instead of
- 1:44:32adding plus the variable
- 1:44:36name we're going to comma separate any
- 1:44:38variables we're inserting so comma name
- 1:44:42here's the
- 1:44:44output hello our variable name which
- 1:44:47contains
- 1:44:49SpongeBob if we need to insert a
- 1:44:51character that would be lower case C so
- 1:44:55let's use
- 1:44:56system.out.print
- 1:44:59F I don't know let's say your name
- 1:45:03starts with a we're going to insert a
- 1:45:06variable we need a placeholder that
- 1:45:08perent sign lower case C for a Char
- 1:45:13variable then comma separate any
- 1:45:16variables comma our variable of first
- 1:45:21letter so here's an important thing too
- 1:45:24with a print F statement we have to
- 1:45:26manually insert any new line characters
- 1:45:28we'll end each print F statement with a
- 1:45:30new line
- 1:45:31character so after our format specifier
- 1:45:35We'll add back sln to insert a new
- 1:45:41line that's better otherwise the output
- 1:45:43is going to be all on the same
- 1:45:45line so that's how to insert a character
- 1:45:48percent C for
- 1:45:50chars then for an integer we use
- 1:45:55D so print
- 1:45:59f u r we're going to insert a variable
- 1:46:04we use D for
- 1:46:07integers you are our variable age years
- 1:46:12old than a new line character then we
- 1:46:15will insert our variable
- 1:46:20age you are 30 years old
- 1:46:24and in the middle is our variable of
- 1:46:27age for a double that would be
- 1:46:32f
- 1:46:34system.out.print
- 1:46:38f you are add a placeholder f for
- 1:46:43floating Point number think of that as a
- 1:46:47double inches tall or you can pick
- 1:46:50centimeters up to you
- 1:46:53then we will insert our variable of
- 1:46:59height you are
- 1:47:0160.5 then a bunch of zeros inches tall
- 1:47:05we can limit the amount of digits that
- 1:47:07are displayed after the decimal we'll
- 1:47:09have to set a Precision but I'll explain
- 1:47:11that
- 1:47:13later then a Boolean all we need is the
- 1:47:16letter
- 1:47:17b
- 1:47:19system.out.print
- 1:47:21f let's say
- 1:47:24employed add a
- 1:47:26placeholder B then a new line
- 1:47:31character we will display our variable
- 1:47:34of is
- 1:47:37employed employed then our variable of
- 1:47:40true now you can insert multiple
- 1:47:43variables within the same line here's
- 1:47:46how we'll use
- 1:47:48system.out.print
- 1:47:50f we're going to inst insert our
- 1:47:53variable of name
- 1:47:55SpongeBob for a string the specifier
- 1:47:58character is going to be lowercase s for
- 1:48:00a string so string SpongeBob
- 1:48:04is we're inserting an integer we need
- 1:48:07percent
- 1:48:08D years old we'll comma separate all
- 1:48:13variables we're inserting we have a
- 1:48:15string followed by an integer so that
- 1:48:18would be name comma age we're inserting
- 1:48:21two variables of this time SpongeBob is
- 1:48:2530 years
- 1:48:27old so those are specifier characters
- 1:48:31add a percent sign a placeholder then
- 1:48:33follow this placeholder with a specifier
- 1:48:35character depending on what the data
- 1:48:37type is s for Strings C for
- 1:48:41chars D for integers f for doubles or
- 1:48:45other floating Point numbers and B for
- 1:48:48booleans it might take a little bit of
- 1:48:50time to remember
- 1:48:51these now we'll cover Precision with
- 1:48:54setting the Precision we can limit the
- 1:48:57amount of digits that display after a
- 1:48:59decimal let's create three different
- 1:49:01prices and these will be of the double
- 1:49:03data type price 1 equals
- 1:49:08$9.99 price
- 1:49:10two I'm just making up numbers here
- 1:49:1510.15 and double price 3
- 1:49:21equals 5
- 1:49:24$41 we're going to use print F to
- 1:49:27display these
- 1:49:29prices
- 1:49:31system.out.print F we're going to insert
- 1:49:34we'll use that percent placeholder then
- 1:49:36use f to display a double f for floating
- 1:49:39Point
- 1:49:41number comma our variable of price one
- 1:49:45then we'll do this with price two and
- 1:49:46price
- 1:49:47three price one price two price three
- 1:49:53and here are the prices oh and we have
- 1:49:55to add a new line character too because
- 1:49:57they're all in the same line I forgot to
- 1:50:00do that so back sln back
- 1:50:03sln back
- 1:50:06sln that's better naturally when we're
- 1:50:10using print F to display a floating
- 1:50:12Point number such as a double normally
- 1:50:14it displays six digits after the decimal
- 1:50:17point but we would like to limit that
- 1:50:20here's how we're going to set the
- 1:50:22Precision
- 1:50:24to display one digit after the decimal
- 1:50:26between the percent sign and the F Type
- 1:50:30.1 to display one
- 1:50:36digit so we have
- 1:50:3910.0 this will automatically round the
- 1:50:41output so we have 10.0 100.2 and
- 1:50:4654.0 for two digits that would be 02
- 1:50:54999
- 1:50:5610.15 and
- 1:50:5954.1 or we could do
- 1:51:02three
- 1:51:053F then we have three digits after the
- 1:51:10decimal so that is precision it's useful
- 1:51:13for displaying prices then we have Flags
- 1:51:16next let's set the Precision to be 0.2
- 1:51:22here's different flags that we can add
- 1:51:24by adding a plus after the percent sign
- 1:51:27we can output a plus for any positive
- 1:51:37numbers positive 999 positive 10.15
- 1:51:4354.1 is still
- 1:51:46negative to display a plus before any
- 1:51:49positive numbers just add a plus sign
- 1:51:53let's increase these
- 1:51:55prices let's add three zeros to the end
- 1:51:58of
- 1:52:04each if you add the flag of a comma it's
- 1:52:07a comma grouping separator for any
- 1:52:09thousand's place after the percent sign
- 1:52:12add a
- 1:52:13comma at any thousand's place we have a
- 1:52:17comma grouping separator again this is
- 1:52:20useful for displaying prices
- 1:52:24by adding a single left parentheses any
- 1:52:27negative numbers are enclosed in a set
- 1:52:29of parentheses so let's revert these
- 1:52:31prices
- 1:52:33back after the percent sign add a left
- 1:52:39parentheses so we only have one negative
- 1:52:42price-
- 1:52:4754.1 and then we have a space character
- 1:52:50this will display a minus if negative
- 1:52:52space space if positive we have two
- 1:52:54positive numbers in one negative one so
- 1:52:58by using a space
- 1:53:03character any positive numbers are
- 1:53:06preceded with a space 54.1 is negative
- 1:53:09so we do not have that space it's good
- 1:53:11for aligning
- 1:53:13numbers the last specifier we have to
- 1:53:15talk about is
- 1:53:17width we're going to create some ID
- 1:53:19numbers these will be integers int id1 =
- 1:53:261 int id2 =
- 1:53:3123 in
- 1:53:33ID3 equal
- 1:53:37456 and int id4 equals
- 1:53:457890 these integers have a varying
- 1:53:48number of digits 1 2 3 4 by setting the
- 1:53:53width we can align them here's how again
- 1:53:56we're going to use print F we're
- 1:53:59displaying an integer so we need percent
- 1:54:01D we will Begin by displaying
- 1:54:05id1 then let's do this with 2 three and
- 1:54:07four as
- 1:54:09well 2 3
- 1:54:144 and again I am missing that new line
- 1:54:17character new line new line
- 1:54:22new
- 1:54:23line new
- 1:54:26line that's better so we have our four
- 1:54:31IDs by adding a zero after the percent
- 1:54:34sign we can zero pad some of these
- 1:54:42values but Java is giving us a warning
- 1:54:45by how many characters do we want to pad
- 1:54:47these numbers with
- 1:54:49zero let's say four we have one variable
- 1:54:53that has four digits we'll set it to be
- 1:54:55four to be the maximum so
- 1:54:5904 and here we are these ID numbers are
- 1:55:02all now zero
- 1:55:04padded and they align vertically which
- 1:55:07is kind of
- 1:55:09nice so to add some zero padding follow
- 1:55:12our placeholder with zero then the
- 1:55:15amount of digits to Zer pad in this case
- 1:55:18four by setting a positive number we can
- 1:55:21WR justify these variables
- 1:55:23so let's replace zero with just
- 1:55:27four rather than having that zero
- 1:55:29padding we just have some spaces if you
- 1:55:31would prefer it that
- 1:55:33way or with a negative number we can
- 1:55:36left justify these
- 1:55:38values so let's set four to be instead
- 1:55:42-4 now these values are left Justified
- 1:55:45and we still have those spaces
- 1:55:47afterwards except for the last variable
- 1:55:49because it's already four digits
- 1:55:52all right everybody so that is the print
- 1:55:54F statement it's a method used to format
- 1:55:56output you add a placeholder then one of
- 1:55:59a few characters afterwards there is
- 1:56:01Flags width precision and specifier
- 1:56:03characters depending on the data type of
- 1:56:05what you want to display we'll have more
- 1:56:07practice with this in the future and
- 1:56:09well everybody that is print F in
- 1:56:13Java hey everybody this video serves as
- 1:56:16a project we're going to create a
- 1:56:17compound interest calculator using Java
- 1:56:20if you put money in a bank account or
- 1:56:22the stock market using the given formula
- 1:56:24we can determine what our final amount
- 1:56:25is going to be plus interest after a
- 1:56:27given amount of time this project serves
- 1:56:29as an exercise using Java let's begin
- 1:56:32all right let's get started everybody
- 1:56:34the first thing we'll need is a scanner
- 1:56:35we'll need to import it at the top of
- 1:56:37our Java file import input
- 1:56:41import
- 1:56:44java.util.scanner
- 1:56:48we need to create a scanner object again
- 1:56:51scanner scanner equals new
- 1:56:54scanner then within the set of
- 1:56:56parentheses type
- 1:56:58system.in when you open a scanner it is
- 1:57:01good practice to close the scanner when
- 1:57:03you're done with it so you don't leave
- 1:57:04any resources
- 1:57:06open after creating our scanner here the
- 1:57:09different variables we'll need double
- 1:57:13principal this will store the principal
- 1:57:16amount what's our original investment an
- 1:57:19interest rate double rate
- 1:57:23an integer for times compounded how
- 1:57:27often does our interest compound like
- 1:57:30yearly quarterly
- 1:57:32monthly then we need an amount of time
- 1:57:34such as years into
- 1:57:38years and the final amount
- 1:57:42double
- 1:57:44amount now we have to get some user
- 1:57:46input we'll need some prompts first we
- 1:57:49need the principal amount all use
- 1:57:55system.out.print enter the principal I
- 1:57:58never spell principal right enter the
- 1:58:01principal
- 1:58:05amount we are working with the double so
- 1:58:08we need the next Double method of our
- 1:58:10scanner principal equals our
- 1:58:15scanner do next
- 1:58:20Double then we need the interest rate
- 1:58:23this is going to be a
- 1:58:26double
- 1:58:29system.out.print enter the interest
- 1:58:34rate I'll add within our prompt that
- 1:58:36this is in a percentage form like 5% or
- 1:58:40so the number five instead of
- 1:58:4405 so then we have our interest
- 1:58:47rate so we need our scanner do next
- 1:58:53double so let's say somebody types in
- 1:58:56five for
- 1:58:575% we would like the rate to be 05
- 1:59:01rather than 5 after accepting the user
- 1:59:03input let's divide that number by 100 or
- 1:59:07let's say somebody types in seven we
- 1:59:09don't want the rate to be seven we want
- 1:59:10it to be
- 1:59:1207 that's why we're dividing by
- 1:59:15100 we'll fill in the times compounded
- 1:59:19variable we're going to use print
- 1:59:24enter the number of
- 1:59:28times
- 1:59:30compounded per
- 1:59:32year so this could be yearly that would
- 1:59:35be one quarterly four or monthly would
- 1:59:38be 12 in most cases it's probably going
- 1:59:40to be one for yearly times
- 1:59:43compounded equals use our
- 1:59:48scanner
- 1:59:50next int cuz we're accepting an
- 1:59:54integer then we need the amount of times
- 1:59:56our money is going to be
- 2:00:00invested enter the number of
- 2:00:05years then we need to assign our
- 2:00:08variable years years equals
- 2:00:12scanner. next int because we're working
- 2:00:15with an integer then we need to
- 2:00:17calculate the total amount here's the
- 2:00:19formula
- 2:00:23amount
- 2:00:24equals we need our principal
- 2:00:29amount times access the math class use
- 2:00:33the power
- 2:00:35method raay is 1 plus the interest
- 2:00:41rate divided by the variable times
- 2:00:47compounded so this is going to be the
- 2:00:50base we Comm separate the base and the
- 2:00:53power we are raising this part of our
- 2:00:56equation to the
- 2:00:58power of times
- 2:01:02compounded times the number of
- 2:01:06years and that's our
- 2:01:08formula it's a little confusing but it
- 2:01:10should
- 2:01:12work then we will display the
- 2:01:15amount system.out.print
- 2:01:19line the amount
- 2:01:25after plus years
- 2:01:30plus
- 2:01:31is pick a unit of currency I'll pick
- 2:01:34American
- 2:01:36dollars plus
- 2:01:39amount let's test
- 2:01:42this so let's say we have $10,000 or so
- 2:01:46and we're putting it in the
- 2:01:48bank what is the interest rate
- 2:01:52maybe
- 2:01:533% so I'm going to type three enter the
- 2:01:57amount of times compounded per year I'll
- 2:01:59go with
- 2:02:01one enter the number of years
- 2:02:04one so we should have 10,300 in our
- 2:02:09bank which we do
- 2:02:1210,300 if you would like you can use a
- 2:02:15print F statement to format the
- 2:02:18output so if you're familiar with that
- 2:02:20let's replace print line with print
- 2:02:24F instead of concatenating these
- 2:02:26variables we'll add some placeholders
- 2:02:34instead so to display an integer we need
- 2:02:37percent D within print
- 2:02:40F
- 2:02:42years is then to display a double we
- 2:02:45need percent F then we'll list our
- 2:02:48arguments
- 2:02:50years comma
- 2:02:55amount so to display two decimals within
- 2:02:58a double between the percent and the F
- 2:03:01we're going to type 2 to display two
- 2:03:04digits after the
- 2:03:05decimal we'll proceed this placeholder
- 2:03:08with the unit of currency let's do
- 2:03:09American dollars okay let's see the
- 2:03:11total
- 2:03:12amount enter the principal amount
- 2:03:16$110,000 enter the interest rate 2%
- 2:03:20enter the number of times compounded per
- 2:03:21year this time let's do quarterly so
- 2:03:24four because there's four quarters in a
- 2:03:26year enter the number of years let's do
- 2:03:29two
- 2:03:31actually and here's the amount in our
- 2:03:33bank account after 2 years
- 2:03:39$1,477 all right everybody so that is a
- 2:03:42compound interest calculator you can
- 2:03:43write using
- 2:03:46Java what is going on everybody in this
- 2:03:49video I'm going to explain nested if
- 2:03:50statements in Java
- 2:03:52in Java it is possible to have if
- 2:03:54statements within other if statements
- 2:03:57kind of like
- 2:03:58this after checking some condition you
- 2:04:01could follow with another condition if
- 2:04:03the first condition is true you can add
- 2:04:06if
- 2:04:07statements L if
- 2:04:09statements and or else
- 2:04:12statements really depends on the program
- 2:04:14that you're trying to write so what
- 2:04:15we're going to do in this demonstration
- 2:04:17is have two Boolean
- 2:04:19variables a Boolean of is
- 2:04:23student let's say that we're selling
- 2:04:25movie tickets if somebody's a student
- 2:04:28they get a 10% discount on their movie
- 2:04:30ticket we also have a senior discount
- 2:04:33Boolean is
- 2:04:36senior if somebody is 65 or older they
- 2:04:39get a 20% senior
- 2:04:41discount or if they're a student and a
- 2:04:44senior they get a combined total of 30%
- 2:04:47off their ticket
- 2:04:49price let's say we have a double of
- 2:04:53price the price of a movie ticket will
- 2:04:55be $9.99 let's say that's actually kind
- 2:04:59of cheap
- 2:05:00nowadays we'll write an if statement to
- 2:05:03check to see if somebody is a student if
- 2:05:06is student is equal to true but since
- 2:05:09this is a Boolean variable we don't need
- 2:05:11the equals true portion we can just say
- 2:05:14if is student I will go ahead and set
- 2:05:17this to be true right away so if
- 2:05:20somebody is a student they get a student
- 2:05:22discount else they
- 2:05:27won't so if a student is true let's
- 2:05:30output the
- 2:05:32following you get a
- 2:05:36student discount of
- 2:05:4010% then we will take our
- 2:05:44price price equals price time
- 2:05:490.9 that will give you a 10% discount
- 2:05:51account you could shorten this using the
- 2:05:54augmented assignment operator price
- 2:05:56times equals
- 2:05:580.9 that will set the price to be
- 2:06:0090% or a 10% discount so to say if
- 2:06:04somebody's not a student they're going
- 2:06:05to pay full price price times equals 1
- 2:06:10that really doesn't do anything but it
- 2:06:11might help with this
- 2:06:14visualization then we'll output the
- 2:06:18following the price of a ticket
- 2:06:24is pick unit of currency then I'll add
- 2:06:27our price let's perform a test
- 2:06:31run currently we're a student you get a
- 2:06:35discount of 10% the price of a ticket is
- 2:06:40$8.99 to limit the amount of digits that
- 2:06:43display let's use a printf statement
- 2:06:46we'll use print F instead of print line
- 2:06:49after the dollar sign I will add a
- 2:06:51format specifier we're going to display
- 2:06:54a double we'll use f for a floating
- 2:06:56Point
- 2:06:57number comma price to display two digits
- 2:07:01after the decimal use 0
- 2:07:032 now we can display a
- 2:07:06price you get a student discount of 10%
- 2:07:09the price of a ticket is
- 2:07:12$8.99 now if we weren't a student I'll
- 2:07:15set this to be
- 2:07:18false we don't get that discount the
- 2:07:21price of a ticket is $9.99 what it was
- 2:07:25originally now what if somebody is a
- 2:07:27senior and a student within our if
- 2:07:31statement we'll write a nested if
- 2:07:34statement as well as an else
- 2:07:37statement if is senior they get a
- 2:07:40student discount and a senior
- 2:07:43discount let's cut these two lines
- 2:07:46within the if statement and stick them
- 2:07:49within the nested else statement
- 2:07:52if somebody is a senior and a
- 2:07:55student you get a senior
- 2:08:00discount of
- 2:08:0420% and a student discount of
- 2:08:0810% so let's take our original
- 2:08:11price times equals
- 2:08:180.7 and an additional 10% off the orig
- 2:08:24so let's say is senior is true for
- 2:08:28now okay else if they're not a student
- 2:08:32but they are a
- 2:08:35senior we'll check that with a nested if
- 2:08:37statement within the else
- 2:08:40statement if is senior they get that
- 2:08:44senior
- 2:08:46discount but it's only 20% instead of 30
- 2:08:50because they're not also as
- 2:08:53student price times equals
- 2:08:580.8 you could add
- 2:09:00else price times equals
- 2:09:041 that's if somebody is not a student
- 2:09:08and not a senior but you don't
- 2:09:11necessarily need the sell
- 2:09:12statement all right let's perform a test
- 2:09:17run let's say that we're not a student
- 2:09:19and not a senior
- 2:09:24the price of a ticket is
- 2:09:25$9.99 what if we were a student but not
- 2:09:28a
- 2:09:30senior you get a student discount of 10%
- 2:09:33the price of a ticket is
- 2:09:36$8.99 what if we're not a student but we
- 2:09:39are a
- 2:09:42senior you get a senior discount of 20%
- 2:09:46the price of a ticket is
- 2:09:48$7.99 what if we're both a student and a
- 2:09:53senior you get a senior discount of 20%
- 2:09:57you get a student discount of 10% the
- 2:10:00price of a ticket is
- 2:10:02$6.99 that's how nesa if statements can
- 2:10:05be useful you could check another
- 2:10:07condition after checking a condition
- 2:10:09already so that's just one example of
- 2:10:12nesa if statements and well everybody
- 2:10:14those are Nesta if statements in
- 2:10:19Java hey yeah so today I'm going to show
- 2:10:22you a few useful string Methods you may
- 2:10:24be interested in as a beginner using
- 2:10:26Java in this demonstration let's create
- 2:10:28a variable of name the data type is
- 2:10:30going to be a string type in your full
- 2:10:32name including a
- 2:10:34space I'll just use my YouTube channel
- 2:10:36name if at any time you need the length
- 2:10:39of a string you can use the length
- 2:10:40method I'll store that as an integer int
- 2:10:44length equals to access these string
- 2:10:47Methods you need a string or a variable
- 2:10:51that is a string
- 2:10:53dot then we have one of a few methods if
- 2:10:56you need the length you can use the
- 2:10:57length
- 2:10:59method and then let's print
- 2:11:03it print the length how many characters
- 2:11:07are in the string
- 2:11:09eight 1 2 3 4 5 6 7 8 this could be
- 2:11:14useful let's say somebody is setting a
- 2:11:16password and you have a maximum size of
- 2:11:1812 characters well you can check the
- 2:11:19length to see if it's under 12
- 2:11:21characters so that is the length
- 2:11:24method there's Char at that will give
- 2:11:27you the character at a specified index
- 2:11:29we'll be given a single character we'll
- 2:11:31store that as a variable Char letter
- 2:11:35equals take our string of name use the
- 2:11:39Char at
- 2:11:42method within the Char at method we'll
- 2:11:45list an index zero is the index of the
- 2:11:47first character because computers like
- 2:11:49to start at zero what what is the
- 2:11:51character at index
- 2:11:53zero well that letter is B at index
- 2:11:59one that would be R and
- 2:12:02two would be
- 2:12:04o that is the charat method it returns a
- 2:12:08character at a given index within a
- 2:12:10string we also have the capability of
- 2:12:12finding a letter at a given
- 2:12:14index let's say we have a variable of
- 2:12:17index take our name use the index of
- 2:12:23method find the first occurrence of an
- 2:12:27O so in my example that should be 2 0 1
- 2:12:322 that is the first index of this
- 2:12:35character o what about a
- 2:12:39space well that would be three 0 1 2
- 2:12:463 there's also last
- 2:12:49index last index equals take our string
- 2:12:53of
- 2:12:54name then use the last index of
- 2:13:00method what is the last index of an O
- 2:13:03because index of gives you the first
- 2:13:05index last gives you well the last
- 2:13:09one what is the last index of O where is
- 2:13:12it exactly
- 2:13:15five 0 1 2 3 4 5
- 2:13:21we can take a string and make all the
- 2:13:23characters uppercase all reassign that
- 2:13:25to our name variable so we're going to
- 2:13:28take our name use the two uppercase
- 2:13:32method then print our name after
- 2:13:35reassigning
- 2:13:36it now all the letters are
- 2:13:39uppercase there's also two
- 2:13:42lowercase we'll replace upper with
- 2:13:46lower and I should probably turn this
- 2:13:49into a comment
- 2:13:52now all the characters are
- 2:13:55lowercase we can trim any white space
- 2:13:57before and after a string let's add a
- 2:13:59few spaces before and after then print
- 2:14:02the
- 2:14:03name we can eliminate all this white
- 2:14:06space we'll use the trim method for that
- 2:14:09we'll reassign name equals name and use
- 2:14:13the trim
- 2:14:15method now while that white space is
- 2:14:19gone we can replace a character with
- 2:14:22another
- 2:14:23one name equals name we will replace one
- 2:14:28character with another let's replace any
- 2:14:31o's with A's for your own name feel free
- 2:14:35to change
- 2:14:38these so what is my name
- 2:14:42now
- 2:14:44broade there are methods that return
- 2:14:46Boolean values to check to see if a
- 2:14:49string is empty you can use the is empty
- 2:14:52method this would return a Boolean to
- 2:14:55demonstrate I'm going to put this within
- 2:14:57a print
- 2:15:01statement so my string is not empty
- 2:15:04that's false but if it was it would
- 2:15:08return true but I have to turn these
- 2:15:10lines into
- 2:15:12comments that would return true my name
- 2:15:15is empty this could be useful within an
- 2:15:18if statement
- 2:15:22if name do is empty
- 2:15:26method if our name is empty we will
- 2:15:28print your name is
- 2:15:32empty else we will
- 2:15:36print
- 2:15:37hello plus
- 2:15:39name currently my name is
- 2:15:42empty your name is
- 2:15:44empty this method returns true but if it
- 2:15:48wasn't
- 2:15:51it would return
- 2:15:52false hello whatever your name
- 2:15:58is I'll use a multi-line comment just to
- 2:16:01comment all this
- 2:16:06out we can check to see if our string
- 2:16:09contains a character that would return a
- 2:16:13Boolean let's check to see if our
- 2:16:17name contains any spaces
- 2:16:23if that's
- 2:16:27true your name
- 2:16:31contains a
- 2:16:34space else will print your
- 2:16:39name doesn't
- 2:16:42contain any
- 2:16:44spaces so my name does contain a
- 2:16:48space that would return true your name
- 2:16:51contains a
- 2:16:52space if we're working with let's say a
- 2:16:55username usernames typically don't
- 2:16:57contain
- 2:16:58spaces your name doesn't contain any
- 2:17:01spaces there is a method to check to see
- 2:17:04if two strings are
- 2:17:05equal again I'm going to use a
- 2:17:07multi-line comment just to comment this
- 2:17:15out if our
- 2:17:18name equals
- 2:17:22using the equals method we can check to
- 2:17:24see if two strings are equal the given
- 2:17:26characters within them let's see if our
- 2:17:29name equals
- 2:17:33password this actually would be more
- 2:17:35appropriate if we were working with the
- 2:17:36string named password if our name if the
- 2:17:40string of name is equal to this string
- 2:17:43if they have the same characters exactly
- 2:17:45then we'll print your name can't be
- 2:17:50password
- 2:17:53else we will
- 2:17:56print
- 2:17:57hello plus
- 2:18:00name I will set my name to be
- 2:18:05password your name can't be password but
- 2:18:08if it
- 2:18:12wasn't well then this returns false
- 2:18:15these two strings are not equal and we
- 2:18:18execute the L statement hello your name
- 2:18:21now the equals method doesn't account
- 2:18:23for case sensitivity so let's say our
- 2:18:26password has a capital
- 2:18:31P well then this method returns false
- 2:18:34hello password to ignore case
- 2:18:37sensitivity you can use equals ignore
- 2:18:42case then that would return true your
- 2:18:45name can't be password all right
- 2:18:48everybody those are a few useful string
- 2:18:50Methods you may be interested in as a
- 2:18:52beginner using
- 2:18:54Java all right everybody in this video
- 2:18:57I'm going to explain how the substring
- 2:18:58method Works in Java substring is a
- 2:19:01method that's used to extract a portion
- 2:19:04of a string strings have a built-in
- 2:19:06method of substring within the substring
- 2:19:08method you can list one of two indices
- 2:19:11this method will create a new string
- 2:19:13based on the positioning of the indices
- 2:19:15the start and the end I can best
- 2:19:18demonstrate this with us creating an
- 2:19:20email SCE ER program so what we'll do in
- 2:19:22this demonstration is create a string of
- 2:19:27email type in your own email I don't
- 2:19:29want to give you guys my actual email
- 2:19:31because some of you guys are weird no
- 2:19:33offense my email will be bro1
- 2:19:37[email protected]
- 2:19:41our variable of email is a string
- 2:19:44strings have a built-in method of
- 2:19:46substring we can create a new string
- 2:19:48from portions of the original string
- 2:19:51here's
- 2:19:52how let's say I would like the first six
- 2:19:56letters but depending on your own email
- 2:19:58it may be different the substring method
- 2:20:00will return a new string so let's create
- 2:20:03a new variable the data type of string
- 2:20:06of usern
- 2:20:07name equals to create a substring we
- 2:20:11will take the original string use the
- 2:20:13substring
- 2:20:15method then list one of two possible
- 2:20:17indexes or indices the first index will
- 2:20:21be zero at what position would we like
- 2:20:23this substring to end I would like to
- 2:20:26end right before this at sign so that
- 2:20:28would be 0 1 2 3 4 5
- 2:20:326 the second index is exclusive so we're
- 2:20:36creating a new string of username by
- 2:20:39using the substring method of our
- 2:20:41original string of email then let's
- 2:20:43print it to test it we will display our
- 2:20:47username and now we have a new string
- 2:20:50mine is bro 1 123 but depending on what
- 2:20:53you wrote for your email it may be
- 2:20:54different now I would like a new
- 2:20:56substring of every character after the
- 2:20:59at sign this will be a variable named
- 2:21:02domain what is the domain of our email
- 2:21:05again we're taking our original string
- 2:21:08using the substring method then list one
- 2:21:11of two possible indices so again let's
- 2:21:14count this is index zero because in
- 2:21:16programming we tend to start with zero 1
- 2:21:192 3 4 5 6
- 2:21:247 so 7 8 9 10 11 12 13 14 15
- 2:21:3316 then we will display our
- 2:21:37domain so my domain is
- 2:21:41gmail.com within your substring method
- 2:21:43if you have a starting index but you
- 2:21:45would like all the characters that come
- 2:21:47after all the way till the end you don't
- 2:21:49necessarily need an ending index X we
- 2:21:51could just say seven that would work too
- 2:21:56gmail.com there's one issue with how we
- 2:21:58wrote this program what if somebody has
- 2:22:00a different length of an email there's a
- 2:22:02greater or lesser number of characters
- 2:22:05so let's say I have a different
- 2:22:11email this program doesn't work as
- 2:22:14intended it's not flexible so my domain
- 2:22:17is [email protected]
- 2:22:19I could could modify the indices of the
- 2:22:22substring method so 0 1 2 3 4 5 6
- 2:22:3178 then the domain would be nine because
- 2:22:33I want everything after the at
- 2:22:36sign and then my username let's check
- 2:22:40that so this would be my username bro
- 2:22:43code one to make this program more
- 2:22:46flexible instead of manually entering
- 2:22:48the indices we can determine this number
- 2:22:50should be with another string method
- 2:22:53we're going to use the index of method
- 2:22:55in place of a number we're going to find
- 2:22:57the at character let's replace our
- 2:23:00second
- 2:23:01index with email our string dot using
- 2:23:06the index of method then we will find
- 2:23:09the index of our at character we're
- 2:23:12going to replace this number with email.
- 2:23:15index of and find the at sign so now
- 2:23:19let's try printing our
- 2:23:22username so here's my
- 2:23:25username then let's print the
- 2:23:29domain currently we have that at sign
- 2:23:32the index of method returns a number
- 2:23:35which we're using as the index it's
- 2:23:37returning the index of this at sign I'm
- 2:23:40going to increase it by one so we get
- 2:23:42everything after the at sign so plus
- 2:23:46one and now the at sign is removed
- 2:23:50even better yet let's accept some user
- 2:23:54input we'll need a scanner import
- 2:23:58java.util do scanner we'll create a
- 2:24:01scanner
- 2:24:02object scanner scanner equals new
- 2:24:08scanner then we will need
- 2:24:11system.in then when we open a scanner it
- 2:24:14is a good idea to close it when we're
- 2:24:15done with it I sometimes forget
- 2:24:20we'll create a prompt for a user to type
- 2:24:22in their
- 2:24:24email enter your email I'll use print
- 2:24:28instead of print line I'm going to
- 2:24:31declare my variables after our scanner
- 2:24:34that's just how I like to arrange things
- 2:24:36we'll declare our email but not yet
- 2:24:38assign
- 2:24:39it same thing applies with our
- 2:24:42username and our
- 2:24:45domain we've already declared these
- 2:24:47variables we don't need to do that again
- 2:24:53instead of manually assigning our email
- 2:24:55we're going to use our scanner to accept
- 2:24:57user input scanner. nextt
- 2:25:02line let's display our username and our
- 2:25:10domain okay enter your email I'll make
- 2:25:12up something bro1 [email protected]
- 2:25:17here's my username
- 2:25:20and the
- 2:25:21domain we can take this a step further
- 2:25:24too we can check to see if our email is
- 2:25:26valid we'll use some of the string
- 2:25:28Methods we learned about in the last
- 2:25:30video so after accepting our email we're
- 2:25:34going to write an if
- 2:25:35statement if our email contains the at
- 2:25:43character our email is only valid if we
- 2:25:45have this at character if it is
- 2:25:50then we'll perform all this code so
- 2:25:51let's cut it then stick it within the if
- 2:25:54statement else we will do something
- 2:25:58else let's print emails must contain
- 2:26:05at let's type in an email that's not
- 2:26:08valid I won't include the at
- 2:26:12sign emails must contain the at
- 2:26:16character let's try that
- 2:26:18again fake
- 2:26:21guy one at maybe they're using Yahoo
- 2:26:26yahoo.com so my username would be fake
- 2:26:29guy one the domain is
- 2:26:32yahoo.com all right everybody so that is
- 2:26:34the substring method and how to create
- 2:26:36an email slicer program the substring
- 2:26:39method is used to extract a portion of a
- 2:26:41string you can list one of two possible
- 2:26:43indices within the substring method a
- 2:26:45starting index and an ending index we
- 2:26:48use the substring method on an email
- 2:26:50then created two new substrings one of
- 2:26:52username and the other of domain and
- 2:26:54well everybody that is the substring
- 2:26:56method in
- 2:26:59Java hey everybody in this video we're
- 2:27:01going to create a weight conversion
- 2:27:03program to convert a weight from pounds
- 2:27:05to kilogram or vice versa it will help
- 2:27:07us become more familiar with if
- 2:27:09statements so let's get started okay
- 2:27:11let's get started everybody what might
- 2:27:13be helpful to you as a beginner when
- 2:27:14creating a project is to use comments to
- 2:27:17describe what you want to do at each
- 2:27:18part of your program this is also known
- 2:27:20as pseudo code for example the first
- 2:27:22thing we'll do is declare our variables
- 2:27:25declare
- 2:27:28variables then we'll create a welcome
- 2:27:33message then a prompt for user
- 2:27:36input prompt for user input or
- 2:27:43Choice we'll say option
- 2:27:46one convert lbs meaning pounds to to kgs
- 2:27:52kilog option two convert kgs to
- 2:27:58LBS if somebody doesn't select option
- 2:28:01one or two we'll use an else statement
- 2:28:03else print not a valid
- 2:28:08choice so here's a rough outline of our
- 2:28:10program and what it's going to do we'll
- 2:28:12begin at the top we'll declare any
- 2:28:14variables that we'll need so we will
- 2:28:16need a scanner we'll create a scanner
- 2:28:18object scanner scanner equals new
- 2:28:21scanner then type system.in within set
- 2:28:24of parentheses if you hover over the
- 2:28:27word scanner you can import it
- 2:28:29automatically import
- 2:28:31java.util.scanner or otherwise you can
- 2:28:34type it
- 2:28:35in what are some other variables we'll
- 2:28:38need double weight for the original
- 2:28:41weight double new
- 2:28:44weight that's after we convert the
- 2:28:46weight we'll need to store it and int
- 2:28:49choice
- 2:28:50a user is going to type in one to
- 2:28:52convert pounds to kilograms or two to
- 2:28:55convert kilograms to
- 2:28:57pounds we have our variables now we need
- 2:29:00a welcome
- 2:29:02message I'll use a few print
- 2:29:04statements let's say weight
- 2:29:08conversion
- 2:29:15program one C and
- 2:29:18space this means opt one convert lbs
- 2:29:22meaning pounds to kgs
- 2:29:26kilog or Choice
- 2:29:282 to colon space convert kilogram to
- 2:29:34pounds
- 2:29:36lbs then we'll prompt for a user's
- 2:29:41Choice I'll use print instead of print
- 2:29:45line choose an option
- 2:29:51then we'll need to store that choice
- 2:29:53variable Choice equals use our scanner
- 2:29:56to accept user input we're accepting an
- 2:29:59integer we'll use next
- 2:30:03int let's do a test run and I will print
- 2:30:06our choice
- 2:30:10temporarily weight conversion program
- 2:30:12one to convert pounds to kilograms or
- 2:30:15two to convert kilograms to pounds I'll
- 2:30:18type in one we get one
- 2:30:21or
- 2:30:22two then we output two we can delete
- 2:30:26this print line statement we were just
- 2:30:27checking to see if it
- 2:30:29works okay option one if somebody
- 2:30:32selects option one we have to convert
- 2:30:34pounds to
- 2:30:35kilograms well we'll need to check to
- 2:30:37see if choice is one we can use an if
- 2:30:40statement for
- 2:30:42that if choice is equal to one be sure
- 2:30:46you use double equals that is the
- 2:30:48comparison operator we're checking if
- 2:30:51these two values are equal if you use a
- 2:30:53single equals that is the assignment
- 2:30:56operator and Java thinks you're trying
- 2:30:58to assign one to choice so double equals
- 2:31:01for
- 2:31:03comparisons we're converting pounds to
- 2:31:05kilograms we'll ask a user to enter the
- 2:31:11weight in
- 2:31:13lbs I'll use print instead of print
- 2:31:18line we we will assign our
- 2:31:22weight equals use our scanner then use
- 2:31:26the next Double method then we will
- 2:31:29accept a double value so once we have
- 2:31:32our weight we have to convert it to
- 2:31:34kilograms because it's currently in
- 2:31:36pounds we will assign our new weight
- 2:31:40equal to the weight our current weight
- 2:31:43that
- 2:31:44is times
- 2:31:460.45 3592
- 2:31:51then we'll output the new
- 2:31:55weight the
- 2:31:57new weight in
- 2:32:00kgs
- 2:32:02is then we'll add our new
- 2:32:07weight okay let's perform a test
- 2:32:10run we'll type in one to convert pounds
- 2:32:13to kilogram enter the weight in pounds
- 2:32:16what's 150 lb converted to kilog
- 2:32:2168.0
- 2:32:23388 if you would like to limit the
- 2:32:25amount of digits that display after the
- 2:32:27decimal you can use a print F statement
- 2:32:29instead of print line so to do that
- 2:32:32we'll convert this to print
- 2:32:35F where we would like to insert a value
- 2:32:37or variable we'll use percent since
- 2:32:40we're displaying a double we'll use f
- 2:32:44meaning floating Point number to display
- 2:32:46two digits after the decimal we'll use 0
- 2:32:492 or 0.1 to display one digit depending
- 2:32:52on your preference replace the plus with
- 2:32:55a
- 2:32:56comma we're inserting this variable at
- 2:32:59this location this placeholder let's
- 2:33:01perform that
- 2:33:02again choose an option convert pounds to
- 2:33:05kilograms enter the weight in pounds 150
- 2:33:09is good the new weight in kilogram is
- 2:33:1368.4
- 2:33:15kilg now if Choice equals 2 we're going
- 2:33:19to convert kilog to pounds and really we
- 2:33:22can just copy the if statement paste it
- 2:33:25then convert
- 2:33:27it else if Choice equals 2 the user
- 2:33:31wants to convert kilogram to pounds
- 2:33:34enter the weight in
- 2:33:36kilog the new formula is going to be
- 2:33:39weight time
- 2:33:422.24
- 2:33:4462 the new weight in pounds lbs is our
- 2:33:50new
- 2:33:52weight let's try
- 2:33:54it choose an option we will convert
- 2:33:57kilograms to pounds I'm going to press
- 2:33:59two enter the weight in kilogram let's
- 2:34:03say 68
- 2:34:05kg the new weight in pounds is
- 2:34:0914991
- 2:34:11lb what if somebody types in a number
- 2:34:14that's not
- 2:34:15valid so we can get rid of this comment
- 2:34:18is use an else statement to print print
- 2:34:20that the user didn't select a valid
- 2:34:23choice we will follow this with an lse
- 2:34:26statement we will follow else if with an
- 2:34:28else statement else
- 2:34:31print that was not a valid
- 2:34:38choice we have options one or two if I
- 2:34:41type in three we execute the L statement
- 2:34:44that was not a valid
- 2:34:46choice and again like usual I'm
- 2:34:49forgetting to close my scanner because I
- 2:34:51sometimes forget to do that so at the
- 2:34:53end of your program close your scanner
- 2:34:55scanner. close because you don't want to
- 2:34:58leave resources
- 2:34:59open okay let's do one final test run
- 2:35:02let's convert pounds to kilograms what's
- 2:35:06200 lb converted to
- 2:35:08kilograms 90.7
- 2:35:102 all right everybody so that is a
- 2:35:13weight conversion program you can write
- 2:35:15using
- 2:35:16[Music]
- 2:35:18Java hey everybody in this video I'm
- 2:35:20going to show you how we can use the
- 2:35:22tary operator in Java in Java the tary
- 2:35:25operator is a question mark followed by
- 2:35:27one of two possible values here's the
- 2:35:29formula we check a condition then use a
- 2:35:32question mark like we're asking a
- 2:35:33question is this condition true if it is
- 2:35:37we'll return a value if not we return
- 2:35:40something else a different value it's a
- 2:35:43simpler version to an if else statement
- 2:35:45here's a
- 2:35:46demonstration let's say we have an
- 2:35:48integer of a score score will equal 70
- 2:35:52that's technically
- 2:35:54passing using an if statement I will
- 2:35:56check if my score is greater than or
- 2:35:59equal to
- 2:36:0260 if it is then we will print pass else
- 2:36:07we will
- 2:36:10print fail so this does
- 2:36:14work with a score of 70 I have passed if
- 2:36:18it were 55
- 2:36:20well then I fail there's another way in
- 2:36:23which we can write this and that is by
- 2:36:24using the tary
- 2:36:26operator what we're going to do let's
- 2:36:29create a variable a string variable of
- 2:36:33pass or
- 2:36:35fail and we're basically just following
- 2:36:37this formula we have our condition is
- 2:36:41our score greater than or equal to
- 2:36:4560 question mark we're asking a question
- 2:36:49is this condition true if it is let's
- 2:36:52return a string of pass colon think of
- 2:36:56it like
- 2:36:58otherwise will return
- 2:37:02fail and then I will print pass or fail
- 2:37:05to display it so 55 that means we fail
- 2:37:10but if it were
- 2:37:1375 that means we pass by using the tary
- 2:37:16operator it's an alternative to writing
- 2:37:19an if else statement and it can be
- 2:37:21simpler in many cases here's a few other
- 2:37:24examples let's create a variable of
- 2:37:27number set it equal to some
- 2:37:31number using the Turner operator we will
- 2:37:34check if this number is even or
- 2:37:36odd I'll create a string of even or odd
- 2:37:42equals our
- 2:37:44condition we can check if a number is
- 2:37:47even or odd by using the modulus
- 2:37:48operator
- 2:37:50we will take our
- 2:37:52number modulus 2 the modulus operator
- 2:37:56gives you the remainder of any division
- 2:37:58does 3 divide by two evenly if it does
- 2:38:01that would equal
- 2:38:05zero 3 doesn't divide by two evenly
- 2:38:08there's a remainder of one and that does
- 2:38:10not equal
- 2:38:11zero is this number divisible by two
- 2:38:14we'll use the trary operator if it is
- 2:38:18we'll return a string of even otherwise
- 2:38:21we'll return a string of odd is my
- 2:38:24number even or
- 2:38:26odd so three is
- 2:38:29odd but if it were
- 2:38:32four well then it's
- 2:38:35even here's a more practical example
- 2:38:38let's say we're working with a
- 2:38:41time in hours this will be in military
- 2:38:45time hours equals
- 2:38:4813 that would be the the same as 1
- 2:38:51p.m. I will create a string of time of
- 2:38:56day equals we'll check if our hours is
- 2:39:00greater than or equal to 12 if it is
- 2:39:03we'll return PM if not will return
- 2:39:06am hours less than
- 2:39:1012 tary operator is our hours less than
- 2:39:1512 if it is we'll return a.m.
- 2:39:19otherwise will return
- 2:39:22p.m and then we will display the time of
- 2:39:25day 13 hours that's the same as 1:
- 2:39:30p.m. yep the time is 1: p.m. or if the
- 2:39:33hours were 9 that would be
- 2:39:39am all right last example let's say we
- 2:39:42have an income or
- 2:39:45salary income equals $60,000
- 2:39:50let's pretend that you're a software
- 2:39:52engineer in the united states in the
- 2:39:54United States we have different tax
- 2:39:56brackets depending on your income this
- 2:39:58time we will assign a variable of tax
- 2:40:01rate equals we'll check if our
- 2:40:05income is greater than or equal
- 2:40:09to $40,000 for
- 2:40:13$40,000 question mark turnar
- 2:40:16operator if that's true then our tax
- 2:40:19rate is going to be
- 2:40:220.25 otherwise it will be
- 2:40:270.15 so with our income what is the tax
- 2:40:33rate
- 2:40:340.25 but if I changed it to
- 2:40:3730,000 well then our tax rate would be
- 2:40:400.15 for
- 2:40:4315% so that is the tary operator you
- 2:40:46follow this formula you check a
- 2:40:49condition within a set of parentheses
- 2:40:51then use the tary operator like you're
- 2:40:53asking a question if this condition is
- 2:40:56true return this otherwise if it's false
- 2:41:00return something else and then if you
- 2:41:01would like you can assign it to a
- 2:41:03variable and well everybody that is the
- 2:41:05tary operator in
- 2:41:09Java all right everybody in this video
- 2:41:11we're going to create a temperature
- 2:41:13conversion program to convert from
- 2:41:15Celsius to Fahrenheit or Fahrenheit to
- 2:41:17Celsius this is a practice project to
- 2:41:19get us more familiar using the tary
- 2:41:21operator so let's get started we will be
- 2:41:23accepting some user input so let's get
- 2:41:25the scanner out of the way first scanner
- 2:41:28scanner equals new scanner we need a
- 2:41:31scanner to accept user input within the
- 2:41:34set of parentheses type
- 2:41:36system.in and then I will just
- 2:41:37automatically import this class so at
- 2:41:40the top of your Java file you need this
- 2:41:42import import
- 2:41:44java.util.scanner then when you open a
- 2:41:47scanner it is a good idea to close it at
- 2:41:48the end of your file when you're done
- 2:41:50with it because you don't want to leave
- 2:41:52resources open we have our scanner to
- 2:41:54accept user input let's declare some
- 2:41:57variables what will we need we will need
- 2:42:00the original temperature this will be of
- 2:42:02the double data type double temp we'll
- 2:42:06need a new temperature let's say double
- 2:42:09new temp that's after we convert it and
- 2:42:12a unit we could use Char for the unit
- 2:42:15you know the letter F or c for
- 2:42:17Fahrenheit or Celsius but just to keep
- 2:42:20it simple I'm going to use a string
- 2:42:22because there's a built-in method of two
- 2:42:24uppercase we can take the input and make
- 2:42:26it uppercase automatically just to make
- 2:42:28it simple we'll use a string rather than
- 2:42:30a Char for the
- 2:42:32unit okay now we just need to get the
- 2:42:34temperature we'll need a prompt I'll use
- 2:42:37print instead of print line we will ask
- 2:42:40the user to enter the
- 2:42:46temperature we will assign the
- 2:42:48temperature temp equals scanner. next
- 2:42:52Double because we're accepting a value
- 2:42:55we're signing a variable of the double
- 2:42:57data type now we need the unit is it
- 2:43:00going to be Celsius or
- 2:43:02Fahrenheit we'll need another prompt
- 2:43:04I'll use print instead of print
- 2:43:06line we will ask the user convert to
- 2:43:12Celsius or Fahrenheit
- 2:43:20C or
- 2:43:24F let's ass our unit
- 2:43:27variable use our
- 2:43:30scanner then use the next method to get
- 2:43:33the next character we'll use the next
- 2:43:35method to get the next
- 2:43:38character we don't necessarily need next
- 2:43:40line we just want a single
- 2:43:43character so check this out I'm going to
- 2:43:46Output our temp
- 2:43:49and our
- 2:43:57unit what if somebody types in a
- 2:43:59lowercase C or F so let's say the
- 2:44:02temperature is
- 2:44:0475 75° F and we would like to convert it
- 2:44:08to Celsius I'll type lowercase C rather
- 2:44:11than uppercase
- 2:44:14C our unit is currently a lowercase C we
- 2:44:18can convert it to be
- 2:44:20uppercase following the next method of
- 2:44:22our scanner where we assign our unit
- 2:44:24we're going to do something called
- 2:44:25method
- 2:44:26chaining we'll add another dot then use
- 2:44:29the two uppercase method of
- 2:44:33strings let's try that again and see if
- 2:44:35it
- 2:44:36works
- 2:44:3975 I'll type in lower case
- 2:44:44C the two uppercase method will take
- 2:44:46that lowercase C and make it uppercase
- 2:44:50which is what we'll need in this next
- 2:44:51section we can delete these two
- 2:44:55statements now we're going to use the
- 2:44:57tary
- 2:44:58operator how the trary operator works is
- 2:45:02that we check a
- 2:45:03condition followed by a question mark
- 2:45:06like we're asking a question we will
- 2:45:08return some code if that condition is
- 2:45:10true colon meaning else if it's false
- 2:45:14we'll do some other code or return some
- 2:45:16other value we're going to follow this
- 2:45:19formula so what is our condition let's
- 2:45:23check to see if our unit is equal to a c
- 2:45:27take our variable of
- 2:45:29unit use the equals method because
- 2:45:32strings have a built-in method of
- 2:45:34equals does our unit equal in uppercase
- 2:45:38C question mark That's the tary operator
- 2:45:42we're asking a
- 2:45:44question if this is true that means
- 2:45:46we're trying to convert from Fahrenheit
- 2:45:48to Celsius
- 2:45:50the formula to do that would be the
- 2:45:52following we would be executing this
- 2:45:55code since it's
- 2:45:56true take our temp subtract
- 2:46:0032 then multiply 5 /
- 2:46:059 now what if our unit does not equal C
- 2:46:09that means we're trying to convert to
- 2:46:11Fahrenheit we'll need that colon for the
- 2:46:13next part of the
- 2:46:14section now here's the formula to
- 2:46:16convert from Celsius to Fahrenheit
- 2:46:20this will be the false section of the
- 2:46:22trinary
- 2:46:24operator take our temp * 5 /
- 2:46:309 +
- 2:46:3232 with the trinary operator how we've
- 2:46:36written this code is that we're going to
- 2:46:37be returning a double after converting
- 2:46:40it we need to do something with it so
- 2:46:42we're going to assign it to a variable
- 2:46:45our variable of new temp then we can do
- 2:46:47something with it
- 2:46:50once we have our new temperature let's
- 2:46:52display it I'll perform a test run let's
- 2:46:56print our new
- 2:46:58temp let's say it's 100° F and we're
- 2:47:02going to convert to Celsius I'll type in
- 2:47:05see well the temperature in Celsius is
- 2:47:0937.7 repeating we're going to format
- 2:47:12this in a
- 2:47:14moment let's add a degree
- 2:47:16symbol plus a string on Windows to add a
- 2:47:21degree symbol make sure num lock is on
- 2:47:23hold alt then type
- 2:47:280176 plus our unit we're converting to
- 2:47:32let's try that
- 2:47:34again what is 100° fah converted to
- 2:47:39Celsius
- 2:47:4137.7 repeating de
- 2:47:44C to limit the amount of digits that
- 2:47:46display after the decimal we we can use
- 2:47:49a print F statement let's convert our
- 2:47:51print line for the new temp to be print
- 2:47:53F rather than print
- 2:47:55line we'll add a
- 2:47:59placeholder we're displaying a double so
- 2:48:02we need F to display one digit type 0.1
- 2:48:06between the percent sign and the
- 2:48:08F comma new
- 2:48:14temp after our placeholder We'll add our
- 2:48:17degree symbol again I'm holding alt numb
- 2:48:20lock is on I'm typing
- 2:48:230176 then we will display the unit it's
- 2:48:26a string percent s for
- 2:48:29Strings we're inserting a new variable
- 2:48:32we're going to comma separate them then
- 2:48:34add our
- 2:48:35unit and let me just close this Gap
- 2:48:39let's see what the final product
- 2:48:41is let's convert
- 2:48:4430.1 this will be in Celsius to
- 2:48:47Fahrenheit
- 2:48:49and the temperature in Fahrenheit is
- 2:48:5148.7 De
- 2:48:55fahit okay everybody that is a
- 2:48:57temperature conversion program that you
- 2:48:59can write using the tary operator in
- 2:49:04Java why hello there today I got to
- 2:49:07explain something called enhanced
- 2:49:09switches in Java in Java there switches
- 2:49:12and then there's enhanced switches
- 2:49:15enhanced switches are a Java 14 feature
- 2:49:17I would recommend using using enhanced
- 2:49:19switches over standard switches so what
- 2:49:21is a switch exactly a switch is a
- 2:49:24replacement to using many l statements
- 2:49:27here's an example of us using a lot of
- 2:49:29lse if statements in this sample program
- 2:49:32we have a day of the week I set mine to
- 2:49:34be Friday with each of these if
- 2:49:36statements I am checking to see if the
- 2:49:38day equals Monday Tuesday so on and so
- 2:49:42forth currently my day is set to
- 2:49:45Friday this would give me it is a
- 2:49:47weekday
- 2:49:49if I set the day to be
- 2:49:52Saturday well then it is the
- 2:49:55weekend or Sunday let me show you the
- 2:49:59code it is the weekend then I have an
- 2:50:02else statement if none of the above
- 2:50:04conditions are true for example let's
- 2:50:07say day is pizza day which doesn't exist
- 2:50:10but it really
- 2:50:11should Pizza day is not a day this code
- 2:50:15does work but it could be more efficient
- 2:50:18we have a lot of redundancies we're
- 2:50:20using a lot of else if statements that
- 2:50:22all basically do the same
- 2:50:25thing an improvement would be to use an
- 2:50:28enhanced switch let me demonstrate how
- 2:50:31to make
- 2:50:33that so let's set our day to be Monday
- 2:50:37for example to create a switch we will
- 2:50:40type switch at a set of parentheses then
- 2:50:43a set of curly
- 2:50:44braces it's similar to an if statement
- 2:50:47already with within the set of
- 2:50:49parentheses for the switch we're going
- 2:50:51to examine a value or variable we're
- 2:50:53going to examine our string of day we
- 2:50:56will examine this value against any
- 2:50:59matching cases the first case will be a
- 2:51:02string of
- 2:51:04Monday if day is equal to this value a
- 2:51:07string of Monday we'll do
- 2:51:10something we'll use an arrow an arrow is
- 2:51:13the arrow operator in Java think of it
- 2:51:15as meaning do something do this if day
- 2:51:19equals Monday do this code so let's
- 2:51:22print
- 2:51:23something let's print it is a
- 2:51:28weekday and then for fun you don't have
- 2:51:30to but I'm going to add an
- 2:51:33emoji nobody likes Mondays let's be
- 2:51:36honest if day is Monday do this code
- 2:51:40let's add another case let me zoom in a
- 2:51:43little if Case is
- 2:51:47Tuesday Arrow op operator meaning do
- 2:51:49this code we will print we'll print the
- 2:51:53same thing it is a
- 2:51:55weekday we'll do this with the other
- 2:51:57days too I'll just copy this code then
- 2:52:01repurpose it to save some time case
- 2:52:04Wednesday it is a
- 2:52:07weekday case
- 2:52:10Thursday it is a
- 2:52:12weekday case
- 2:52:15Friday it is a weekday now if it's sat
- 2:52:18Saturday that means it's the
- 2:52:20weekend case
- 2:52:23Saturday it is the weekend let's change
- 2:52:28the
- 2:52:32Emoji case Sunday it is the
- 2:52:39weekend let's perform a test run if
- 2:52:42you're using intellig you may have These
- 2:52:44Warnings it's basically saying that
- 2:52:46these sections of code are unreachable
- 2:52:48we're manually setting a variable these
- 2:52:51sections of code are not going to
- 2:52:52execute so I would just ignore that for
- 2:52:55now so let's perform a test
- 2:52:58run my day is Monday day is equal to the
- 2:53:02case of Monday so we do this code if it
- 2:53:06were
- 2:53:10Friday case Friday we do this
- 2:53:14code if Case were Saturday
- 2:53:19well we do this
- 2:53:21code and
- 2:53:25Sunday case Sunday so we do this code it
- 2:53:29is the
- 2:53:30weekend what if we have a day that
- 2:53:32doesn't exist like pizza
- 2:53:37day well we can add a default case
- 2:53:40here's how we'll type the keyword
- 2:53:43default Arrow meaning do
- 2:53:46this we'll print
- 2:53:49our variable
- 2:53:51day plus a string of is not a
- 2:53:57day Pizza day is not a day the default
- 2:54:01case it behaves similarly to your else
- 2:54:04statement if no above conditions are
- 2:54:07true then we will do
- 2:54:09this so what we'll do now is accept some
- 2:54:12user input using a scanner we'll need to
- 2:54:14create a scanner object scanner scanner
- 2:54:18equals new scanner within the set of
- 2:54:21parentheses type
- 2:54:22system.in and then I'm just going to
- 2:54:24automatically import this class at the
- 2:54:26top of my Java file import
- 2:54:29java.util.scanner
- 2:54:31we'll create a prompt for a user to type
- 2:54:34in the
- 2:54:35day enter the day of the week and I will
- 2:54:40use print instead of print line instead
- 2:54:43of manually assigning a variable we'll
- 2:54:45use our scanner scanner. nextt line to
- 2:54:48accept a
- 2:54:51string okay let's try this
- 2:54:54again enter the day of the week it is
- 2:54:57Tuesday it is a weekday let's try that
- 2:55:00again enter the day of the week it is
- 2:55:03Saturday it is the
- 2:55:05weekend and let's make up a
- 2:55:08day uh let's go with taco
- 2:55:11day taco day is not a
- 2:55:15day we could actually improve this widg
- 2:55:18even further intellig is saying we have
- 2:55:20a duplicate branch in our switch we have
- 2:55:23two or more cases that are doing the
- 2:55:25same thing we are outputting it is a
- 2:55:30weekday we could consolidate some of
- 2:55:32these cases here's how the cases of
- 2:55:35Monday Tuesday Wednesday Thursday and
- 2:55:37Friday they all output the same thing so
- 2:55:40we're going to consolidate them I'll
- 2:55:43copy one of these print statements and
- 2:55:45delete all of them
- 2:55:54we can comma separate these cases so
- 2:55:57case Monday
- 2:55:59comma
- 2:56:01Tuesday
- 2:56:04comma
- 2:56:06Wednesday
- 2:56:08comma
- 2:56:10Thursday
- 2:56:13comma
- 2:56:16Friday then Arrow
- 2:56:19if our case is Monday Tuesday Wednesday
- 2:56:21Thursday or Friday then do the following
- 2:56:25we'll print it is a
- 2:56:27weekday now Saturday and Sunday do the
- 2:56:30same thing
- 2:56:30too let's cut one of these
- 2:56:34lines and delete
- 2:56:37them case
- 2:56:39Saturday comma
- 2:56:42Sunday Arrow meaning do this it is the
- 2:56:46weekend then we'll keep keep our default
- 2:56:49case in case there are no matches let's
- 2:56:52try this
- 2:56:53again enter the day of the week it is
- 2:56:58Wednesday it is a
- 2:57:01weekday it is
- 2:57:04Saturday it is the
- 2:57:06weekend it is Sunday it is the
- 2:57:11weekend uh then let's go with uh
- 2:57:13Hamburger
- 2:57:14Day hamburger day is not a day
- 2:57:19so those are enhanced switches they're a
- 2:57:21replacement to using many LF
- 2:57:24statements if you find yourself using a
- 2:57:26lot of lsif statements I would recommend
- 2:57:28using a switch instead but not just any
- 2:57:31switch an enhanced switch which is a
- 2:57:34Java 14 feature that you should know
- 2:57:36about and well everybody those are
- 2:57:38enhanced switches in
- 2:57:41Java what is going on everybody in this
- 2:57:44video we're going to create a simple
- 2:57:46calculator program using what we've
- 2:57:48learned with enhanced switches this is
- 2:57:50meant to be more of a practice project
- 2:57:52let's get started the first thing we'll
- 2:57:54need is a scanner we will create a
- 2:57:56scanner object scanner scanner equals
- 2:57:59new scanner within the set of
- 2:58:01parentheses type
- 2:58:03system.in and then when you open a
- 2:58:05scanner it is a good idea to close it
- 2:58:07when you're done with
- 2:58:09it scanner. close then be sure to import
- 2:58:13the class at the top of your Java file
- 2:58:16make sure you have this import import
- 2:58:20java.util.scanner
- 2:58:21what are the different variables we'll
- 2:58:24need a user is going to type in two
- 2:58:26numbers and an operator such as a plus
- 2:58:29for addition minus for subtraction so on
- 2:58:32and so forth the first variable will be
- 2:58:34up the double data type double num
- 2:58:38one double num two then an operator this
- 2:58:43will be a Char Char operator
- 2:58:49and a result double
- 2:58:53result there's going to be one more
- 2:58:55variable but we'll fill that in later so
- 2:58:58we'll stick with these four for now
- 2:59:00we'll prompt a user to type in the first
- 2:59:02number followed by an operator than the
- 2:59:05second
- 2:59:06number here's how we can write
- 2:59:08that let's ask the user to enter the
- 2:59:12first number and I will use print
- 2:59:16instead of print line
- 2:59:20we will assign num one equal to use our
- 2:59:24scanner to get user
- 2:59:25input next
- 2:59:30Double then we need the operator we will
- 2:59:34use a print
- 2:59:36statement enter n
- 2:59:40operator we'll have Plus for addition
- 2:59:43minus for subtraction an asterisk for
- 2:59:46multiplication a for slash for division
- 2:59:49let's add one more let's add a carrot
- 2:59:53this will raise a base to a given
- 2:59:55power feel free to add more if you would
- 2:59:57like but we'll just stick with these
- 3:00:00five a user is going to enter in a
- 3:00:03character one of these operators
- 3:00:05hopefully we will assign our variable of
- 3:00:08operator equal to
- 3:00:11scanner.
- 3:00:16next the next method will give you a
- 3:00:18string we can method chain the Char at
- 3:00:22method to return a single character when
- 3:00:25a user types in something just give me
- 3:00:27the first character this will also
- 3:00:30convert it to be a character rather than
- 3:00:32keep it a
- 3:00:33string so ideally our operator is going
- 3:00:36to be a plus a minus an asterisk a
- 3:00:39forward slash or a
- 3:00:43carrot then we need num two we can just
- 3:00:46copy these two lines paste them change
- 3:00:49first to be second and num one to be num
- 3:00:53two we have our two numbers and our
- 3:00:58operator we have to determine what the
- 3:01:00operator is we can use an enhanced
- 3:01:03switch for that we will create a
- 3:01:06switch within the set of
- 3:01:08parentheses we are examining our
- 3:01:12operator we're examining our operator
- 3:01:15against any matching cases the first
- 3:01:18case will be a plus sign and these cases
- 3:01:21are going to be within single quotes not
- 3:01:23double quotes because we're working with
- 3:01:26a character not a
- 3:01:27string with our switch if our operator
- 3:01:31is a plus character we're going to write
- 3:01:34an arrow meaning do
- 3:01:36this we will take our variable of
- 3:01:39result set it equal to be num one that's
- 3:01:43the first number plus num two
- 3:01:48that will be the case for addition then
- 3:01:50we have
- 3:01:51subtraction case minus Arrow do this
- 3:01:56assign our result equal to num one minus
- 3:01:59num
- 3:02:012 multiplication would be
- 3:02:04case a character vaster risk arrow take
- 3:02:08our result equals num one * num
- 3:02:15two now for division
- 3:02:17we have a forward
- 3:02:19slash arrow take our result equals num
- 3:02:24one divided by num
- 3:02:28two now with division if somebody
- 3:02:31divides by zero we'll return to this
- 3:02:33case later and make it a little more
- 3:02:35sophisticated where we check to see if
- 3:02:37num 2 is zero I would like for us to get
- 3:02:39a solid foundation first for this
- 3:02:43program so if somebody would like to
- 3:02:45raise a base to a given power we will
- 3:02:47use the
- 3:02:51carrot we will assign our result equal
- 3:02:53to now we're going to use the math
- 3:02:56class the power method of the math class
- 3:02:59we can raise a base to a given power we
- 3:03:01will raise num one to the power of num
- 3:03:06two let's perform a test run just to be
- 3:03:09sure that it
- 3:03:10works I'm going to display our result
- 3:03:13after the
- 3:03:15switch so Java is giving us a warning
- 3:03:18that variable result might not have been
- 3:03:21initialized that's because it's possible
- 3:03:23there's no matching
- 3:03:24cases and we would be keeping our result
- 3:03:27uninitialized so to clear that up when
- 3:03:30we declare our variable we can also
- 3:03:32assign it I'll just set it to be
- 3:03:35zero let's perform a test
- 3:03:40run enter the first number let's do
- 3:03:433.14 enter an operator let's do addition
- 3:03:46and the second number
- 3:03:491.1 the result is
- 3:03:524.24 let's do subtraction
- 3:03:563.14 minus 1.1 which gives us
- 3:04:022.04
- 3:04:03multiplication
- 3:04:063.14 times let's do
- 3:04:08two
- 3:04:126.28 then division 3.14
- 3:04:18divided by
- 3:04:20two that gives us
- 3:04:231.57 then we have power what is
- 3:04:273 to the power of
- 3:04:31three that is
- 3:04:3427 3 * 3 is 9 * 3 is 27 3 ^ of
- 3:04:403 we have one potential issue though
- 3:04:44what if somebody divides by zero here's
- 3:04:47what happens
- 3:04:48what is
- 3:04:503.14 /
- 3:04:530 we get the output of
- 3:04:56infinity here's how we could prevent
- 3:04:59that underneath the case where we do
- 3:05:03division we'll write a few lines rather
- 3:05:05than just one but we're going to enclose
- 3:05:07them within a set of curly
- 3:05:09braces then I'm going to cut this line
- 3:05:12of
- 3:05:13code within this case we'll write an if
- 3:05:16statement we'll
- 3:05:18check
- 3:05:20if num 2 is equal to
- 3:05:24zero that's because we can't normally
- 3:05:26divide by zero if it is then let's print
- 3:05:31cannot divide by
- 3:05:39zero
- 3:05:41else we'll assign our result of num one
- 3:05:44/ by num 2 we'll execute the L statement
- 3:05:48if num 2 doesn't equal zero because if
- 3:05:51it did this would be true and we execute
- 3:05:53this code instead let's try that
- 3:05:57again what is
- 3:06:003.14 /
- 3:06:030 cannot divide by
- 3:06:05zero however we still do display a
- 3:06:08result and that is by using this print
- 3:06:11statement what if somebody types in an
- 3:06:13operator that doesn't exist 3.14
- 3:06:18I'm going to type in pizza for the
- 3:06:21operator type in I don't know
- 3:06:23one well we still display a result so
- 3:06:27let's modify this program let's display
- 3:06:30a result only if we have a valid
- 3:06:33operator because right now we don't
- 3:06:35Pizza is not a valid
- 3:06:40operator let's add a Boolean variable of
- 3:06:44valid
- 3:06:46operation I will set this to be true
- 3:06:48from the
- 3:06:52beginning if somebody attempts to divide
- 3:06:54by zero I'm going to take our valid
- 3:06:57operation variable and set it to be
- 3:06:59false because we do not want to continue
- 3:07:02and display the
- 3:07:05result we're going to add a default case
- 3:07:08the default case will be the
- 3:07:12following so I'm going to write two
- 3:07:14lines of code for the default
- 3:07:16case let's
- 3:07:19output invalid
- 3:07:24operator then we will take our Boolean
- 3:07:26variable of valid operation and set it
- 3:07:29to be
- 3:07:30false so this is what our switch looks
- 3:07:33like feel free to pause the video if you
- 3:07:36would like a moment to look it
- 3:07:38over within our case of division we do
- 3:07:41have a few lines of code same thing
- 3:07:44applies with the default
- 3:07:46case we're only going to print the
- 3:07:49result if our Boolean of valid operation
- 3:07:52is
- 3:07:54true if valid operation is equal to true
- 3:07:59but really we can just shorten this to
- 3:08:01if valid
- 3:08:04operation then we'll print the
- 3:08:07result then we don't necessarily need an
- 3:08:09else
- 3:08:11statement because we're already printing
- 3:08:13either invalid operator or cannot divide
- 3:08:16by zero
- 3:08:18okay let's run this one final
- 3:08:20time
- 3:08:233.14 plus
- 3:08:252.1 is
- 3:08:285.24
- 3:08:313.14
- 3:08:32minus 1.1 is
- 3:08:392.04
- 3:08:423.14 time
- 3:08:452.1 is 6594
- 3:08:494
- 3:08:513.14 okay 3.14 / 0 Let's see what
- 3:08:55happens cannot divide by zero and we do
- 3:08:59not display the result because our
- 3:09:01equation is invalid which is good we
- 3:09:03don't want to display the result we're
- 3:09:05displaying a type of error message
- 3:09:09instead let's raise
- 3:09:124 to the
- 3:09:15power of two which gives us
- 3:09:1816 now let's type in an operator that
- 3:09:21doesn't exist
- 3:09:253.14
- 3:09:26Pizza
- 3:09:2869 invalid operator that's because Pizza
- 3:09:32is not a valid
- 3:09:34operator all right everybody that is a
- 3:09:36calculator program that you can write
- 3:09:38the purpose of this program is to more
- 3:09:40or less help us with enhanced switch
- 3:09:42statements depending on the operator we
- 3:09:44can perform one of a few operations
- 3:09:47we've also learned that your cases can
- 3:09:49take up more than one line if you would
- 3:09:50like and well everybody that is a
- 3:09:53calculator program in
- 3:09:57Java yo yo yo in today's video I'm going
- 3:10:00to explain logical operators in Java
- 3:10:02logical operators they allow us to check
- 3:10:05or modify more than one condition
- 3:10:07there's and or not we'll discuss and
- 3:10:11first which is represented by two Amper
- 3:10:13Sands let's declare a variable of
- 3:10:16temperature temp meaning temperature I
- 3:10:19will set this to be 20 I would like to
- 3:10:22check to see if my temperature Falls
- 3:10:23within a certain range well to do that I
- 3:10:26could use the and logical operator we
- 3:10:29can check more than one
- 3:10:31condition so first I would like to see
- 3:10:33if my
- 3:10:35temp is less than or equal to 30 30°
- 3:10:40C and if the temp is greater than or
- 3:10:45equal to zero
- 3:10:48we have two conditions this time if this
- 3:10:50is true and this is true then do this
- 3:10:54code so let's output the
- 3:10:57following if our temperature Falls
- 3:11:00within this range we'll
- 3:11:03output the weather is good and for fun
- 3:11:07I'll let a smiley face cuz why not okay
- 3:11:11let's see how this works the temperature
- 3:11:14is currently 20 20° C
- 3:11:18the weather is good if the temperature
- 3:11:21were 40 40°
- 3:11:24C well we don't do
- 3:11:26anything this condition evaluated to be
- 3:11:29false but this one is true our
- 3:11:33temperature is greater than or equal to
- 3:11:35zero that is true but this one is false
- 3:11:38and using the and logical operator both
- 3:11:41conditions must be true in order for
- 3:11:43this entire statement to be true
- 3:11:47since only one of them was true where
- 3:11:49temp is greater than or equal to zero we
- 3:11:52don't execute this code the and logical
- 3:11:55operator is used to check more than one
- 3:11:57condition we can add as many as we would
- 3:11:59like to this time let's create a
- 3:12:02variable of is sunny this will be a
- 3:12:05Boolean Boolean is sunny is it sunny
- 3:12:09outside I will set that to be
- 3:12:13true not only are we going to check to
- 3:12:15see if our temperature is within a
- 3:12:16certain range we're also going to check
- 3:12:20and is sunny is this true so now we are
- 3:12:25linking three different conditions all
- 3:12:28three must be true so if all three are
- 3:12:31true we'll outut the following it is
- 3:12:35sunny
- 3:12:42outside let's set is sunny to be false
- 3:12:46and then we are going to set our
- 3:12:47temperature to not
- 3:12:49240
- 3:12:5320 these two conditions are true but
- 3:12:57this one is false so we don't do any of
- 3:12:59it we skip over it with the and logical
- 3:13:02operator all conditions must be
- 3:13:05true if is sunny we're true
- 3:13:09then well then we execute this code the
- 3:13:13temperature Falls within this range this
- 3:13:15is true and this this is true and this
- 3:13:18is true all three conditions are true so
- 3:13:21we execute this code the weather is good
- 3:13:24and it is sunny
- 3:13:26outside we check more than one condition
- 3:13:29and both must be true there's also the
- 3:13:32not logical operator it's a little
- 3:13:34different how it works is that it gives
- 3:13:36you the opposite you can check to see if
- 3:13:38something is not true let's add an elive
- 3:13:43statement we're going to copy these
- 3:13:46three conditions but make one change
- 3:13:49we're going to precede is Sunny with the
- 3:13:51not logical operator represented by an
- 3:13:54exclamation
- 3:13:55point it checks the opposite we are
- 3:13:58checking to see if something is not true
- 3:14:02is our temperature less than or equal to
- 3:14:0430 and greater than or equal to zero and
- 3:14:08is it not
- 3:14:10Sunny so if something is normally false
- 3:14:13using the not logical operator it
- 3:14:15becomes true if it's normally true it
- 3:14:18becomes false it gives you the opposite
- 3:14:20for a Boolean value
- 3:14:23basically if the weather is good but
- 3:14:26it's not Sunny that must mean it's
- 3:14:28cloudy it is cloudy outside then I'll
- 3:14:33add an emoji of a
- 3:14:38cloud the temperature is good but we'll
- 3:14:42set is sunny to be
- 3:14:45false the weather is
- 3:14:48good but it is cloudy
- 3:14:50outside the temperature Falls within
- 3:14:52this
- 3:14:53range and it is not
- 3:14:56Sunny the not logical operator checks to
- 3:15:00see if something is not true then we
- 3:15:03have the or logical operator represented
- 3:15:05by two vertical bars or is similar to
- 3:15:08the and logical operator however only
- 3:15:11one condition needs to be true that are
- 3:15:13linked by the or logical operator with
- 3:15:16and both must be true let's say our
- 3:15:18temperature is really cold it is -10°
- 3:15:22C but it is sunny
- 3:15:26outside we actually don't end up doing
- 3:15:28anything we skip over the if statement
- 3:15:31and the else if statement let's add
- 3:15:33another LF
- 3:15:36statement we're going to check to see if
- 3:15:38our temperature is really hot or really
- 3:15:42cold we will check if our temp is
- 3:15:45greater than 30
- 3:15:47or using two vertical bars temp is less
- 3:15:52than
- 3:15:53zero then we'll
- 3:15:56output the weather is
- 3:16:03bad if our temperature is
- 3:16:07-10 the weather is bad with the or
- 3:16:10logical operator at least one condition
- 3:16:13must be true this condition was false
- 3:16:16but this one was true so we execute this
- 3:16:19code because using the or logical
- 3:16:21operator at least one condition needs to
- 3:16:23be
- 3:16:25true or if the temperature was 35 35°
- 3:16:31C this time this condition was true but
- 3:16:34this one was false so we execute this
- 3:16:38code all right let's go over another
- 3:16:40example we're going to use these logical
- 3:16:43operators to validate a username when
- 3:16:45somebody tries to set a
- 3:16:47username so in this project we'll need a
- 3:16:50scanner scanner scanner equals new
- 3:16:54scanner we're going to type
- 3:16:58system.in then import this
- 3:17:00class then be sure to close your scanner
- 3:17:03at the end of the file scanner.
- 3:17:08close our username has a few
- 3:17:12rules I'll add these as comments
- 3:17:14username must be between 4 through 12
- 3:17:21characters username must
- 3:17:25not
- 3:17:26contain
- 3:17:28spaces or
- 3:17:32underscores we'll create a string
- 3:17:34variable of
- 3:17:36username we'll create a prompt to have a
- 3:17:38user enter in a
- 3:17:41username enter your new
- 3:17:45username I'll use print instead of print
- 3:17:48line we will assign our username using
- 3:17:51the scanner username equals scanner.
- 3:17:55next
- 3:17:59line we'll write a condition to check if
- 3:18:02our username is between 4 to 12
- 3:18:04characters we're going to check if our
- 3:18:07username is under four characters or
- 3:18:10greater than 12 characters we'll check
- 3:18:12that within an if
- 3:18:13statement if our username
- 3:18:17use the length method to return the
- 3:18:19length of this string if the length of
- 3:18:22our username is less than four use the
- 3:18:25or logical operator or our
- 3:18:28username the length of it using the
- 3:18:30length method is greater than 12 that
- 3:18:34means our username is too short or too
- 3:18:36long we'll output the following username
- 3:18:40must be between 4 through 12 characters
- 3:18:47let's perform a test
- 3:18:49run enter your new username I'll type in
- 3:18:52something that's less than four
- 3:18:54characters username must be between 4 to
- 3:18:5712
- 3:18:58characters let's type in a really long
- 3:19:01username I'll just make up something
- 3:19:04username must be between 4 to 12
- 3:19:07characters let's type in something
- 3:19:09actually within that
- 3:19:14range oh and then we need to add an lse
- 3:19:16state statement because I
- 3:19:18forgot else we will
- 3:19:22print
- 3:19:24welcome plus
- 3:19:28username enter your new
- 3:19:32username welcome whatever your username
- 3:19:36is the second part of this assignment is
- 3:19:39that we need to check to make sure that
- 3:19:41there are no spaces or underscores
- 3:19:43within our username after the if
- 3:19:45statement We'll add an L if statement to
- 3:19:47check
- 3:19:49that using the contains method of
- 3:19:52strings we can check if a string
- 3:19:54contains a certain character with our
- 3:19:56username we will use the contains method
- 3:20:00does our username contain any
- 3:20:04spaces or does our
- 3:20:07username
- 3:20:09contains any
- 3:20:11underscores if so we'll output the
- 3:20:15following user usame must
- 3:20:18not contain spaces or
- 3:20:26underscores let's perform another test
- 3:20:28run type in your first and last name
- 3:20:30including a space username must not
- 3:20:33contain spaces or
- 3:20:36underscores this time I will use an
- 3:20:38underscore to separate the first and
- 3:20:40last
- 3:20:41name username must not contain spaces or
- 3:20:44underscores let's type in a usern name
- 3:20:46that follows the
- 3:20:48rules that seems to
- 3:20:50work so that is the or logical operator
- 3:20:54if at least one condition is true then
- 3:20:56the entire statement is true and we
- 3:20:59execute it all right everybody so those
- 3:21:01are logical operators and checks more
- 3:21:04than one condition both must be true or
- 3:21:07checks more than one condition at least
- 3:21:09one needs to be true not not is a little
- 3:21:12different it checks if something is not
- 3:21:15true basically does the inverse and well
- 3:21:18everybody those are logical operators in
- 3:21:23Java what is going on everybody in this
- 3:21:26video I need to explain while Loops in
- 3:21:28Java if I could summarize a while loop
- 3:21:30in one sentence a while loop will repeat
- 3:21:32some code Forever while some condition
- 3:21:36remains true first I'll give you a
- 3:21:37demonstration using an if statement then
- 3:21:39we'll convert it to be a while loop and
- 3:21:41we'll see how it's useful we'll need to
- 3:21:43accept user input in this demonstration
- 3:21:46I will create a scanner I'm pretty sure
- 3:21:48we know how to create one by
- 3:21:50now be assured to import the scanner
- 3:21:52class import java.util.scanner then be
- 3:21:56sure to close your scanner because I
- 3:21:58always forget to do
- 3:21:59that all right we are going to create a
- 3:22:03string of
- 3:22:05name I'll use an if statement to check
- 3:22:07to see if my name is
- 3:22:09empty if name use the is empty method
- 3:22:14now Java wants us to assign this name
- 3:22:16right away okay so I'm going to go ahead
- 3:22:17and initialize it we can initialize it
- 3:22:20with an empty space if our name is empty
- 3:22:23which it will be we will prompt the user
- 3:22:25to enter your name I'll use print
- 3:22:28instead of print line to keep it on the
- 3:22:30same line then we will assign our
- 3:22:33variable of name name equals scanner.
- 3:22:37next
- 3:22:39line after we escape the if statement
- 3:22:42we'll output our name let's say hello
- 3:22:46Plus
- 3:22:47name so this program will work but
- 3:22:51there's one issue what if I don't type
- 3:22:52in anything when I enter my name I'll
- 3:22:54just hit
- 3:22:56enter well we still continue with the
- 3:22:58rest of the program the output is hello
- 3:23:01we don't display a name because we
- 3:23:03didn't type in anything how can we
- 3:23:05prevent people from doing stuff like
- 3:23:06this skipping
- 3:23:08prompts well we could use a while loop
- 3:23:12rather than an if statement replace if
- 3:23:15with while
- 3:23:17what we're doing is while this condition
- 3:23:20remains true continue this code Forever
- 3:23:24Until this condition is no longer true
- 3:23:28let's try that
- 3:23:30again enter your name I'm just going to
- 3:23:32hit enter enter your name no enter your
- 3:23:35name no enter your name no okay I give
- 3:23:39up I'll type in my name then we escape
- 3:23:41the while loop hello your
- 3:23:44name so that's why a while loop Loop is
- 3:23:46useful we can repeat some code possibly
- 3:23:49Forever at times we may not want a user
- 3:23:51to continue without doing something a
- 3:23:54while loop would be really helpful for
- 3:23:55that they would be stuck until this
- 3:23:57condition is no longer true at the end
- 3:24:00of your while loop we go back to the
- 3:24:01beginning to check the condition if the
- 3:24:03condition is no longer true then we
- 3:24:05escape the while loop and move on with
- 3:24:07the rest of the program there's one
- 3:24:09thing you should be cautious of though
- 3:24:11and that is an infinite Loop let me give
- 3:24:12you a
- 3:24:15demonstration with our while loop if we
- 3:24:17have a condition that we can't change
- 3:24:19from within the loop that's called an
- 3:24:21infinite Loop so let's say while the
- 3:24:23number one is equal to 1 even my IDE
- 3:24:27intellig is giving me a warning that 1
- 3:24:30is equal to 1 is not updated inside the
- 3:24:33loop I would not recommend following me
- 3:24:35along for this part your computer might
- 3:24:37freeze up but I'm going to print
- 3:24:40help I'm stuck in a loop
- 3:24:46within this while loop I have no way to
- 3:24:48change it from inside of it so we're
- 3:24:50going to be stuck in this while loop
- 3:24:52forever here's a
- 3:24:55demonstration we're just going to print
- 3:24:57this code
- 3:24:59forever let's check back later one
- 3:25:03eternity later it's still going we're
- 3:25:06stuck in a loop this may happen to you
- 3:25:08if you have some condition that you
- 3:25:10can't change within your while loop
- 3:25:12you'll need some way to update your
- 3:25:13condition
- 3:25:16let me give you another example again
- 3:25:18we'll need our
- 3:25:27scanner we're going to pretend that
- 3:25:29we're playing a game for a user to quit
- 3:25:31they have to press the q key we will
- 3:25:33declare a string of
- 3:25:36response we'll use a while
- 3:25:38loop now for my condition for us to be
- 3:25:41able to escape the while
- 3:25:43loop our response I'm going to use the
- 3:25:46not logical
- 3:25:49operator
- 3:25:50equals a capital Q and this will be a
- 3:25:54string so while our response does not
- 3:25:58equal Q we continue the loop forever in
- 3:26:02order for somebody to escape this game
- 3:26:04they have to press Q to quit Java wants
- 3:26:07me to assign this variable right away
- 3:26:08and initialize it so I'll do so we don't
- 3:26:12want our variable to be empty when we
- 3:26:14use it to check a condition
- 3:26:17then intellig is giving us another
- 3:26:19warning that our response is not updated
- 3:26:21inside the loop if we don't update it
- 3:26:23we'll be stuck in an infinite
- 3:26:25Loop let's pretend that we're playing a
- 3:26:28game you are playing a
- 3:26:35game then we'll
- 3:26:37output
- 3:26:38press Q to quit
- 3:26:46then we will need to update our response
- 3:26:49response equals use our scanner for
- 3:26:52Single Character we can use
- 3:26:55next in case somebody types in a
- 3:26:57lowercase que we'll follow this method
- 3:27:00with the two uppercase
- 3:27:03method if a user was to type in a
- 3:27:05lowercase Q we'll convert it to be an
- 3:27:07uppercase q so that it
- 3:27:10matches once we escape the while loop
- 3:27:13we'll
- 3:27:14print you have have quit the
- 3:27:20game okay let's perform a test
- 3:27:23run you are playing a game press Q to
- 3:27:26quit no
- 3:27:28yes
- 3:27:30okay as long as our response does not
- 3:27:33equal Q we are still playing our
- 3:27:36imaginary
- 3:27:43game in order for us to quit to escape
- 3:27:46the while loop we have to press Q to
- 3:27:48quit I will press q and we escape the
- 3:27:51loop you have quit the
- 3:27:54game while our response using the not
- 3:27:57logical operator while our response does
- 3:28:00not equal Q continue the loop forever
- 3:28:04let's go over another example we'll ask
- 3:28:06a user for their
- 3:28:09age int age I will set this to be zero
- 3:28:12right
- 3:28:14away we'll prompt a user to enter in
- 3:28:16their age enter your
- 3:28:20age we will assign our age
- 3:28:24variable AG equal scanner. next
- 3:28:30int then at the end of this program I
- 3:28:32will
- 3:28:33print you are our variable
- 3:28:38age plus years
- 3:28:43old let's say I would like to prevent
- 3:28:45somebody from typing in a negative
- 3:28:47number like -1 you are ne1 years old
- 3:28:51well we could use a while loop to
- 3:28:52reprompt the user before displaying the
- 3:28:56result so after we assign our age once
- 3:28:59let's use a while
- 3:29:01loop while age is less than zero we'll
- 3:29:07output the
- 3:29:10following your age can't be negative
- 3:29:16then we'll reprompt the user again we
- 3:29:18can just copy these two lines and then
- 3:29:20paste them within the while loop we'll
- 3:29:23ask the user to enter in their age again
- 3:29:25and reassign
- 3:29:27it if we keep on typing negative
- 3:29:30numbers we can't escape the while
- 3:29:33loop we're stuck until we type in
- 3:29:35something that's
- 3:29:39valid you are 21 years old that's a
- 3:29:42valid
- 3:29:43number from the beginning if I type in
- 3:29:46something that's valid we don't enter
- 3:29:48the while loop at all it is possible to
- 3:29:51skip over
- 3:29:52it we check the condition of a while
- 3:29:54loop before entering it if we type in a
- 3:29:58number that's valid let's say ages
- 3:30:0125 we check the condition it's false
- 3:30:04from the beginning so we don't enter the
- 3:30:06while loop at all we skip over it and go
- 3:30:09straight to the end in this program we
- 3:30:12only get caught in the while loop if we
- 3:30:14type in user input that's not
- 3:30:16valid there's a variation of the while
- 3:30:19loop known as a do while loop we do some
- 3:30:21code first and then check the
- 3:30:24condition if I was to convert this code
- 3:30:26to be a do while loop I would cut the
- 3:30:29while part do this code then check the
- 3:30:33condition at the
- 3:30:34end this is how the program would run
- 3:30:37now it's going to be a little bit
- 3:30:41different your age can't be negative
- 3:30:43enter your age I am years old your age
- 3:30:47can't be negative enter your age you are
- 3:30:50whatever your age is years old so with
- 3:30:54the do while loop we do this code first
- 3:30:57and then check the condition at the end
- 3:30:59where it's different compared to a
- 3:31:00standard while loop is that with the
- 3:31:03standard while
- 3:31:04loop you may not enter the while loop at
- 3:31:07all if this condition is false but with
- 3:31:10the do while loop you always do this
- 3:31:13code at least once and then check the
- 3:31:15condition at the end let's go over one
- 3:31:18last example with the do while loop
- 3:31:21we'll prompt a user to type in a number
- 3:31:23that's between a certain range we will
- 3:31:25have int number and I will assign this
- 3:31:28to be zero right
- 3:31:30away let's use a basic while loop
- 3:31:33first
- 3:31:35while our number is less than one we'll
- 3:31:39use the or logical operator to check or
- 3:31:43if our number is greater than 10
- 3:31:46then do
- 3:31:49this prompt the user to enter a
- 3:31:53number between 1 through
- 3:32:0110 then we will assign our variable of
- 3:32:03number equals use our scanner use the
- 3:32:07next int
- 3:32:10method and then at the end we will print
- 3:32:12our
- 3:32:13number you picked
- 3:32:18plus
- 3:32:21number so let's run this our number is
- 3:32:23set to
- 3:32:26zero enter a number between 1 and 10
- 3:32:29let's do negative
- 3:32:301 11 -2 one
- 3:32:36kajillion I think I broke it okay I'll
- 3:32:39type in something that's valid like five
- 3:32:41you picked
- 3:32:44five rather than check the the condition
- 3:32:46from the beginning we could check it at
- 3:32:48the end so let's convert the while loop
- 3:32:51to a do while loop do this code once and
- 3:32:55then check the
- 3:32:59condition so this would work
- 3:33:07similarly so do this code once check the
- 3:33:11condition at the
- 3:33:12end all right everybody so those are
- 3:33:14while Loops they allow you to execute
- 3:33:16some code possibly forever while some
- 3:33:19condition remains true they're really
- 3:33:22useful when accepting user input because
- 3:33:24the user might not type in something
- 3:33:26that's valid and you have to keep on
- 3:33:27repr prompting them that's one good use
- 3:33:30there's two variations of while Loops a
- 3:33:33standard while loop and a do while
- 3:33:37loop it depends if you want to check the
- 3:33:39condition before entering the loop and
- 3:33:41well everybody those are while Loops in
- 3:33:44Java
- 3:33:47hello everybody in today's video we're
- 3:33:50going to create a number guessing game
- 3:33:51using Java let's get started in this
- 3:33:54program we'll be using random numbers
- 3:33:56we'll need to create a random object
- 3:33:59random random equals new
- 3:34:03random then import this class so at the
- 3:34:07top of our Java file import
- 3:34:09java.util random this random object can
- 3:34:13Generate random numbers for us we'll
- 3:34:16also accept user input we'll need a
- 3:34:18scanner scanner scanner equals new
- 3:34:23scanner then within the set of
- 3:34:25parentheses type
- 3:34:26system.in then import this class as well
- 3:34:30import
- 3:34:31java.util.scanner we have a random
- 3:34:34object to Generate random numbers and a
- 3:34:36scanner object to accept user
- 3:34:39input what are the different variables
- 3:34:41we'll need we'll need the following an
- 3:34:45integer to hold our
- 3:34:47guess so let's say we're going to guess
- 3:34:49a random number between 1 and 100 we'll
- 3:34:52use the scanner to accept an integer and
- 3:34:55place it within this variable of
- 3:34:57guess for example the guess might be
- 3:35:0050 we need to keep track of the amount
- 3:35:02of attempts it took a user to get the
- 3:35:04correct number int attempts we'll assign
- 3:35:09these
- 3:35:10later and we'll need a random number int
- 3:35:13random number
- 3:35:16let's go ahead and assign this right
- 3:35:18away to generate a random number we're
- 3:35:20going to use our random
- 3:35:22object use the next int method of a
- 3:35:26random object within this method we can
- 3:35:29list two numbers a range if I would like
- 3:35:32a number between let's say 1 and 10 my
- 3:35:36two numbers would be 1 comma 11 the
- 3:35:39first number is inclusive the second
- 3:35:42number is
- 3:35:43exclusive let's perform a test run of a
- 3:35:45random number I just want to be sure
- 3:35:47that it's generating correctly when
- 3:35:50writing a program it is a good idea to
- 3:35:51test your code every once in a while
- 3:35:53just to be sure that everything is
- 3:35:56working so we should get random numbers
- 3:35:58between 1 and 10 1 10 5 8 good enough a
- 3:36:07random number stores a random number
- 3:36:09after generating
- 3:36:12it let's create a welcome message let's
- 3:36:15say number guessing
- 3:36:19game we will prompt the
- 3:36:23user guess a
- 3:36:25number
- 3:36:28between let's start with 1 through 10
- 3:36:32later on we'll increase it to
- 3:36:38100 now we'll need a while loop but not
- 3:36:41just any while loop we'll create a do
- 3:36:43while loop so do this code once while
- 3:36:48this condition remains true and let me
- 3:36:50zoom in a little
- 3:36:52bit so what's our condition going to be
- 3:36:55we're going to continue playing this
- 3:36:57game while our
- 3:37:00guess does not equal our random number
- 3:37:05we'll keep on playing until we get the
- 3:37:06random number exclamation point equals
- 3:37:09means not equal so if our guess does not
- 3:37:13equal our random number this return
- 3:37:15turns true then we want to keep playing
- 3:37:18while our guess doesn't equal the random
- 3:37:21number within this do while
- 3:37:24loop will prompt the user to enter in
- 3:37:27their guess I'm going to use print line
- 3:37:30instead of print for guess a number
- 3:37:31between 1 and
- 3:37:3310 now we'll prompt the user to enter a
- 3:37:36number enter a guess enter a
- 3:37:40guess for this I'll use print instead of
- 3:37:43print line we will assign Our Guest
- 3:37:48variable equal to use our scanner use
- 3:37:52the next int
- 3:37:56method we're also keeping track the
- 3:37:58amount of attempts it takes a user to
- 3:38:00get the correct
- 3:38:01number so we can increment attempts by
- 3:38:04adding
- 3:38:07Plus+ and let's go ahead and initialize
- 3:38:10attempts we'll set that to be zero
- 3:38:15temporarily outside the while loop I'm
- 3:38:18going to
- 3:38:20print you have
- 3:38:23one my random number right now is only
- 3:38:25between 1 and 10 I'm just going to keep
- 3:38:28guessing until we get the right
- 3:38:33number so I'll just start with one and
- 3:38:35go all the way to
- 3:38:4210 the answer was nine we escaped the
- 3:38:45wild
- 3:38:46Loop so we know that this is working
- 3:38:49currently one thing this program is
- 3:38:51missing is that we'll want to give a
- 3:38:53user a clue if their guess is too low or
- 3:38:57too high because we have no way to
- 3:38:59tell I'm just typing in random numbers
- 3:39:02and I have no clue how close I am to the
- 3:39:04actual number that we need to
- 3:39:07guess let's get rid of this print
- 3:39:10statement we were using it for testing
- 3:39:12purposes within the do while loop we'll
- 3:39:15write an if statement we'll check if our
- 3:39:19guess is less than our random
- 3:39:23number that means we guess too
- 3:39:26low if that's the case we'll output the
- 3:39:30following two low try
- 3:39:39again else
- 3:39:41if our guess is greater than our random
- 3:39:46number that means we guess too
- 3:39:49high too high try
- 3:39:54again if our guess isn't lower than our
- 3:39:56random number and our guess isn't
- 3:39:59greater than our random number that
- 3:40:01means our guess must equal the random
- 3:40:04number so if that's the case we would be
- 3:40:07executing this lse statement because the
- 3:40:09preceding two conditions would be false
- 3:40:12that means we have the right answer the
- 3:40:14right guess so we'll output the
- 3:40:18following
- 3:40:22correct the let me zoom
- 3:40:25out the number
- 3:40:28was plus our random
- 3:40:36number and then we'll display the amount
- 3:40:38of attempts it
- 3:40:40took number of attempts
- 3:40:46plus our variable of attempts after each
- 3:40:49guess we increment it by
- 3:40:52one the last thing I'm forgetting is to
- 3:40:55close my scanner because I always forget
- 3:40:58to close
- 3:40:59it scanner.
- 3:41:02close okay let's perform a test run
- 3:41:06we're only guessing a number between 1
- 3:41:07and 10
- 3:41:09currently I'll guess something right in
- 3:41:11the middle five too low try again the
- 3:41:15random number we have to guess should be
- 3:41:17between 5 and 10 then I'll go with seven
- 3:41:20too low 8 too low nine too low okay it
- 3:41:26must be 10 then correct the number was
- 3:41:2910 number of attempts it took
- 3:41:32five we're going to improve this code
- 3:41:35now what if we would like the numbers 1
- 3:41:37through 100 this time well we could make
- 3:41:40this program a little more flexible
- 3:41:43instead of hardcoding these numbers
- 3:41:45within the next integer method we'll
- 3:41:47Place some variables here instead we
- 3:41:50will create int Min equals whatever you
- 3:41:53want the minimum to be let's say one int
- 3:41:56Max I will set the max to be 100 let's
- 3:42:00replace these two
- 3:42:01numbers the first one will be Min comma
- 3:42:06Max so these variables behave as if they
- 3:42:09were these numbers within this range the
- 3:42:12second number is exclusive so TW include
- 3:42:15100 I'm going to add +
- 3:42:18one give me the numbers between 1 and
- 3:42:22100 within this print statement let's
- 3:42:25use print F
- 3:42:28instead we'll replace these numbers with
- 3:42:31the format specifier a placeholder we
- 3:42:35will display an integer so we need D
- 3:42:38let's do the same thing with 10
- 3:42:43d uh let's do comma Min comma
- 3:42:49Max and then add a new line at the
- 3:42:53end let's do one final
- 3:42:56run so now we're guessing a number
- 3:42:58between one and 100 I'll guess something
- 3:43:01right in the middle too high try again
- 3:43:04our numberers going to be between 1 and
- 3:43:0650 then 25 too high 12 too high 6 too
- 3:43:13high three
- 3:43:16too low between three and six four
- 3:43:20correct the number was four the number
- 3:43:22of attempts it took six
- 3:43:24attempts all right everybody so that is
- 3:43:26a number guessing game that you can
- 3:43:28create using
- 3:43:32Java hi there everybody so in this video
- 3:43:34I'm going to explain four Loops in Java
- 3:43:37a four Loop is similar to a while loop
- 3:43:39however a for Loop executes some code a
- 3:43:43certain amount of times a for Loop is
- 3:43:45different from a while loop a while loop
- 3:43:48could execute an infinite amount of
- 3:43:49times until its condition is no longer
- 3:43:52true so with for Loops we want to do
- 3:43:54something a certain amount of times to
- 3:43:56create a for Loop type four parentheses
- 3:43:59curly braces now within the condition of
- 3:44:02the for Loop there's three statements
- 3:44:05each is separated with a
- 3:44:07semicolon we have statement one
- 3:44:10statement two and statement three the
- 3:44:13first statement is used for ini ization
- 3:44:16we can create a counter to keep track of
- 3:44:18how many times we have iterated this
- 3:44:20Loop basically we're declaring a
- 3:44:22variable a counter this will be int now
- 3:44:26a common practice for a for Loop is to
- 3:44:28create a counter named I meaning index
- 3:44:31in I equal Z or some other number I is
- 3:44:35going to be used as a counter within
- 3:44:38this Loop also known as a loop control
- 3:44:41variable the second statement is a
- 3:44:44condition when do we want to
- 3:44:46stop maybe we would like to execute some
- 3:44:49code 10 times we'll continue this loop
- 3:44:52as long as I is less than
- 3:44:5510 now the third statement the third
- 3:44:58statement is the step we can increment
- 3:45:01our counter of I by One or another
- 3:45:03number or even decrement after each
- 3:45:06iteration let's increase I by One
- 3:45:10i++ let's perform a test run I'm just
- 3:45:13going to print the word
- 3:45:18pizza so we should execute this code 10
- 3:45:21times using this for Loop 1 2 3 4 5 6 7
- 3:45:278 9
- 3:45:2910 so a for Loop is good if you want to
- 3:45:32repeat some code a certain amount of
- 3:45:35times but there's a little bit of setup
- 3:45:37you have to do we'll need some sort of
- 3:45:39index or counter a condition in which
- 3:45:42you want to stop and you'll need to
- 3:45:44update the counter
- 3:45:45one way or another
- 3:45:48initialization condition update we can
- 3:45:52also print this index of I rather than
- 3:45:55printing the word Pizza let's print I to
- 3:45:58see what we're working with during the
- 3:46:00first iteration I is zero after our Loop
- 3:46:04is complete we increment I by One our
- 3:46:07counter so we have 0 1 2 3 4 5 6 7 8 9
- 3:46:13We're looping 10 times
- 3:46:15but we set I to be zero to begin with if
- 3:46:18I were to set I to be 1 and I want to
- 3:46:21iterate 10 times I could say while I is
- 3:46:24less than or equal to
- 3:46:2710 so now we're starting at 1 and
- 3:46:30counting up to
- 3:46:3210 we can even decrement
- 3:46:35to let's set I to be 10 we'll continue
- 3:46:38the loop as long as I is greater than
- 3:46:42zero to decrement we'll set I to be
- 3:46:45minus
- 3:46:48minus now we're starting at 10 and
- 3:46:53decrementing but we're still looping 10
- 3:46:56times let's go back to the
- 3:47:01beginning using the update statement we
- 3:47:04can even increment by a certain number
- 3:47:06instead of incrementing by one let's
- 3:47:08increment by two and see what
- 3:47:11happens so this time we start at one and
- 3:47:15update our counter of I two each time or
- 3:47:19even three i+ equal
- 3:47:233 1 4 7
- 3:47:2710 we can decrement by a given
- 3:47:30number let's set I to be 10 we'll
- 3:47:32continue this loop as long as I is
- 3:47:35greater than zero and we will decrement
- 3:47:38our step we will decrement by 2 i - = 2
- 3:47:45now we're starting at 10 and
- 3:47:47decrementing by two or even three IUS =
- 3:47:543
- 3:47:5710741 for this next example we're going
- 3:48:00to accept user input again we'll need a
- 3:48:10scanner import the scanner class and
- 3:48:13close your scanner at the end
- 3:48:20we'll create a
- 3:48:22prompt enter how many times you want to
- 3:48:31Loop I'll create a variable of Max Max
- 3:48:36will equal use our scanner use the next
- 3:48:40int
- 3:48:41method and now we're going to create a
- 3:48:43loop a for Loop
- 3:48:47again there's three
- 3:48:51statements each separated with a
- 3:48:54semicolon we'll need an index or counter
- 3:48:56to keep track of how many times we have
- 3:48:59looped let's say in I equals
- 3:49:030 for the next statement what's our
- 3:49:06condition we want to continue as long as
- 3:49:08I is less than the max I is less than
- 3:49:13our Max variable
- 3:49:15then increment I by
- 3:49:19one then we'll print I or something
- 3:49:25else let's try this how many times do
- 3:49:28you want to Loop let's Loop five
- 3:49:32times 1 2 3 4
- 3:49:365 again we set I to be zero so that's
- 3:49:39why we're beginning with
- 3:49:42zero if you would rather begin with one
- 3:49:44we can set I to be 1 then the condition
- 3:49:47would be I is less than or equal to Max
- 3:49:51let's try that again let's Loop six
- 3:49:55times 1 2 3 4 5
- 3:49:596 all right now we're going to create a
- 3:50:01mini
- 3:50:02project we'll create a program to
- 3:50:05simulate a
- 3:50:08countdown perhaps we have a variable of
- 3:50:11int
- 3:50:12start let's start at 10 we're counting
- 3:50:15down from
- 3:50:1710 then we'll create a for
- 3:50:19Loop we will set our index of I equal to
- 3:50:23our
- 3:50:24start our start variable behaves as if
- 3:50:27it was the number
- 3:50:2910 we'll continue as long as I is
- 3:50:32greater than zero then we will decrement
- 3:50:35by one with IUS
- 3:50:37minus during each iteration let's just
- 3:50:41print
- 3:50:42I then when we escape the the for Loop
- 3:50:45let's print happy New
- 3:50:49Year kind of like it's a countdown to a
- 3:50:51new year here's what we have
- 3:50:55currently we start at 10 count down to
- 3:50:58one then display happy New Year if you
- 3:51:02would like we can use the thread class
- 3:51:04and have our program sleep for about 1
- 3:51:07second between each
- 3:51:09Loop here's how you can do that after
- 3:51:12printing I this is a little Advan Java
- 3:51:15at this level you don't need to
- 3:51:16understand how this works but we're
- 3:51:18going to use the thread class called The
- 3:51:21Sleep Method then pass in an amount of
- 3:51:24milliseconds in which we would like to
- 3:51:26sleep 1,000
- 3:51:29milliseconds now Java wants us to add
- 3:51:31the
- 3:51:33following Java wants this method of main
- 3:51:36to throw this exception if our thread is
- 3:51:39interrupted this is intermediate Java we
- 3:51:43will need the section of code in order
- 3:51:45for our program to sleep after you
- 3:51:47finish this lesson be sure to remove
- 3:51:49throws interrupted exception we'll no
- 3:51:51longer need it print I then our program
- 3:51:54is going to sleep for 1 second 1,000
- 3:51:57milliseconds all right and here's our
- 3:51:59mini
- 3:52:01project 10 9 8 7 6 5 4 3 2 one Happy New
- 3:52:12Year
- 3:52:14let's modify this program a little bit
- 3:52:17now we'll use a scanner to accept user
- 3:52:18input scanner scanner equals new
- 3:52:22scanner we have already imported the
- 3:52:25class with start we're going to accept
- 3:52:27some user
- 3:52:28input but we'll need a
- 3:52:31prompt how many seconds to countown
- 3:52:37from we will assign start to be use our
- 3:52:41scanner then use the next int method
- 3:52:47all right let's try this
- 3:52:51again let's say I would like to count
- 3:52:54down from
- 3:52:5720 and it looks like it's working I'll
- 3:53:00fast forward the video till we get to
- 3:53:02the
- 3:53:04end 3 2
- 3:53:071 happy New
- 3:53:10Year so those are four Loops we can
- 3:53:13execute some code a certain amount of
- 3:53:16times it's very similar to a while loop
- 3:53:19in fact there's a lot of overlap where
- 3:53:21you could use either a for Loop or a
- 3:53:23while loop use a for Loop if you want to
- 3:53:25do something a limited amount of times
- 3:53:28and well everybody those are four Loops
- 3:53:30in
- 3:53:33Java I have a pretty short video for you
- 3:53:35today we're going to discuss two
- 3:53:37important keywords when it comes to
- 3:53:39Loops break and continue break is used
- 3:53:42to break out of a loop continue skips
- 3:53:45the current iteration of a loop think of
- 3:53:48the break keyword as the stop button on
- 3:53:50a remote continue would be similar to
- 3:53:53skip with break we're stopping with
- 3:53:56continuing we're skipping let me give
- 3:53:58you a
- 3:53:59demonstration let's create a for Loop
- 3:54:01that will iterate 10 times let's say in
- 3:54:05I equals 0 will continue as long as I is
- 3:54:09less than
- 3:54:1010 then increment I by 1 during each
- 3:54:14iteration I'll just print I then add a
- 3:54:19space here's what we
- 3:54:22got we have the numbers 0 through 9 so
- 3:54:27let's say when we reach five I would
- 3:54:29like to break out of the loop we're
- 3:54:32going to stop it well I could add an if
- 3:54:36statement let's
- 3:54:38say if I is equal to 5 then we will use
- 3:54:43the break keyword
- 3:54:45and break out of the loop
- 3:54:47entirely here's what
- 3:54:50happens once we hit five we break out of
- 3:54:53the loop previously we continued all the
- 3:54:56way until
- 3:54:579 now with the continue keyword we don't
- 3:55:00break out of the loop we just skip the
- 3:55:03current iteration like a skip button on
- 3:55:06a remote this time let's use continue
- 3:55:10rather than
- 3:55:11break and we'll see the differences
- 3:55:13between the two
- 3:55:16now since we're using continue the
- 3:55:18number five is missing we skipped that
- 3:55:21specific cycle of the loop and went back
- 3:55:23to the beginning that's why five is
- 3:55:25missing between four and six but we
- 3:55:28still continue to the end when working
- 3:55:30with loops if you ever need to break out
- 3:55:33you can use the break keyword to skip
- 3:55:35the occurrent iteration you can use
- 3:55:37continue and well everybody that is both
- 3:55:40the break and continue keywords in Java
- 3:55:45hello again friends today I'm going to
- 3:55:46explain nested Loops in Java a nested
- 3:55:49Loop is really just a loop inside of
- 3:55:52another loop you often see nested Loops
- 3:55:55within a matrix or matrices or when we
- 3:55:58reach the topic of data structures and
- 3:56:00algorithms they might not come up for a
- 3:56:03while yeah this can be any combination
- 3:56:05of for Loops or while Loops any kind of
- 3:56:08loop inside of another loop let me give
- 3:56:10you a
- 3:56:12demonstration let's say I would like to
- 3:56:14print the numbers 1 through N9 I can do
- 3:56:16that pretty easily with a for Loop so we
- 3:56:19will create a for loop I will set an
- 3:56:22index of I equal to 1 we'll continue
- 3:56:25this loop as long as I is less than or
- 3:56:28equal to 9 then I will increment I by
- 3:56:31One during each iteration I will display
- 3:56:36I so this will print the
- 3:56:39numbers 1- N9 or if you prefer you can
- 3:56:43have it all on the same l line by using
- 3:56:45print instead of print line then it
- 3:56:47would look something like
- 3:56:49this 1 2 3 4 5 6 7 8 9 you can even
- 3:56:52separate them with the space after
- 3:56:55printing our index of I let's print a
- 3:56:57space
- 3:56:59character and now the output looks
- 3:57:01something like
- 3:57:02this what if you would like to do this
- 3:57:05code three times let's say print the
- 3:57:07numbers 1 through 9 three
- 3:57:09times well you could copy and paste this
- 3:57:12for
- 3:57:13Loop then do that two additional
- 3:57:16times so we have the numbers 1 through 9
- 3:57:19three
- 3:57:22times to put each row on a new line can
- 3:57:25use an empty print line
- 3:57:29statement then it would look something
- 3:57:31like this a
- 3:57:34matrix we seem to be repeating our code
- 3:57:36a lot in programming we like to follow
- 3:57:38the dry principle don't repeat yourself
- 3:57:41if you don't have to
- 3:57:44instead why don't we create a for Loop
- 3:57:46and stick this for Loop inside of that
- 3:57:48Loop let's create a for Loop to iterate
- 3:57:52three times I will create an index of I
- 3:57:55equal to 1 we'll continue as long as I
- 3:57:59is less than or equal to three then
- 3:58:01increment I by One all we're going to do
- 3:58:04is take our code including the for Loop
- 3:58:08and stick it within this other for Loop
- 3:58:10do pay attention to the
- 3:58:12indentation but we have we have one
- 3:58:15problem we're reusing this index of I
- 3:58:18you can see that Java is already giving
- 3:58:19us a warning here's what
- 3:58:23happens variable I is already defined in
- 3:58:26method
- 3:58:28main if we have a loop inside of another
- 3:58:30loop we can't use the same
- 3:58:33index a common naming convention for a
- 3:58:36nested Loop is to use an index of J
- 3:58:39rather than
- 3:58:40I because J comes next in the alphabet
- 3:58:43after I
- 3:58:44instead of printing I we're going to
- 3:58:46print
- 3:58:47J let's see how this works and here's
- 3:58:51our Matrix of data we're printing our
- 3:58:53index of J three
- 3:58:58times after escaping the nested for Loop
- 3:59:01we have a print line statement here if I
- 3:59:03were to get rid of
- 3:59:05that we'll print the numbers 1 through 9
- 3:59:08three times all on the same
- 3:59:11line let's put that back in
- 3:59:15nested Loops are really good when you're
- 3:59:16working with a matrix of data nested
- 3:59:19Loops come up pretty often in the topic
- 3:59:21of data structures and algorithms
- 3:59:23because we have to Loop over sets of
- 3:59:25data now we're going to cover a mini
- 3:59:28project we're going to create a matrix
- 3:59:30of a symbol that a user is going to type
- 3:59:33in a user will set the rows and columns
- 3:59:35for this Matrix we'll declare the
- 3:59:37following variables int
- 3:59:40rows int
- 3:59:42columns and and Char
- 3:59:48symbol we'll need a scanner so let's
- 3:59:50declare that we'll need a scanner
- 3:59:53scanner scanner equals new
- 3:59:56scanner within the set of parentheses
- 3:59:59type
- 3:59:59system.in let's be sure to import this
- 4:00:02class of scanner at the top of our Java
- 4:00:04file and close the scanner when we're
- 4:00:07done with it scanner.
- 4:00:11close we're going to accept some user
- 4:00:14input a user is going to specify the
- 4:00:16rows the columns and the symbol we're
- 4:00:18going to
- 4:00:19use let's create some
- 4:00:22prompts enter the
- 4:00:25number of rows I'll use print instead of
- 4:00:29print line we will assign our variable
- 4:00:33of
- 4:00:34rows equal to use our scanner use the
- 4:00:39next int
- 4:00:40method then let's do this with columns
- 4:00:44enter the number of
- 4:00:48columns columns equals scanner.
- 4:00:51nextend and the symbol we're going to
- 4:00:58use enter the symbol to
- 4:01:04use we will assign our symbol equal to
- 4:01:09scanner.
- 4:01:10nextt and we can use the chart at method
- 4:01:16give me the character at index of
- 4:01:19zero let's create a for Loop for the
- 4:01:21columns
- 4:01:24first let me zoom
- 4:01:28in we're going to set in
- 4:01:32I we can set it equal to
- 4:01:34zero we'll continue as long as I is less
- 4:01:38than our columns whatever the user types
- 4:01:41in then increment I by One
- 4:01:44during each
- 4:01:45iteration I'll use print instead of
- 4:01:48print line we will print our
- 4:01:52symbol let's perform a test
- 4:01:55run enter the rows it doesn't matter we
- 4:01:59didn't set up the rows yet uh for the
- 4:02:01columns let's do
- 4:02:03eight enter the symbol to use Let's
- 4:02:08do let's do a dollar sign that'd be
- 4:02:12cool we have the column set set up
- 4:02:14currently we should have eight dollar
- 4:02:16signs 1 2 3 4 5 6 7 8 we need to repeat
- 4:02:22this for Loop for as many rows as we
- 4:02:25have so we're going to stick this Loop
- 4:02:28within another
- 4:02:36loop with our nested Loop let's replace
- 4:02:39our index of I with j
- 4:02:44for the outer for Loop we will create a
- 4:02:46new index of I equal to zero we'll
- 4:02:50continue this as long as I is less than
- 4:02:53the rows the number of rows that a user
- 4:02:55types in then increment I by
- 4:02:59One let's perform another test
- 4:03:02run let's say I would like three rows of
- 4:03:06six and I will use an at
- 4:03:09sign so these are all on the same line
- 4:03:12we should have a total of
- 4:03:1418 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- 4:03:2116 17 18 our inner loop ends after each
- 4:03:25group of six I'm going to print a new
- 4:03:30line we can use an empty print line
- 4:03:33statement our outer for Loop is in
- 4:03:35charge of the rows our inner for Loop is
- 4:03:38in charge of the columns let's see the
- 4:03:40final product I would like five
- 4:03:44rows of three three
- 4:03:47columns I'll use an Amper sand here we
- 4:03:52are we have three columns and five
- 4:03:59rows all right everybody those are
- 4:04:01nested Loops it's really just any kind
- 4:04:03of loop inside of another kind of loop
- 4:04:07it doesn't matter if it's a for Loop or
- 4:04:09a while loop we often use nested loops
- 4:04:12with matrices or when we reach the topic
- 4:04:15of data structures and algorithms they
- 4:04:17might not come up very often but you
- 4:04:19should at least be aware of their
- 4:04:21existence and well everybody those are
- 4:04:23nested Loops in
- 4:04:27Java what is going on everybody in this
- 4:04:29video I'm going to discuss methods in
- 4:04:31Java think of a method as a block of
- 4:04:34reusable code that is executed when you
- 4:04:36call it to call a method you use a set
- 4:04:38of parentheses kind of like it's a pair
- 4:04:41of telephones talking to each other why
- 4:04:43are methods is useful well with the
- 4:04:44method you can write some code once and
- 4:04:47reuse it whenever you want you just have
- 4:04:49to call it let me show you why a method
- 4:04:51is useful so let's say we have a task we
- 4:04:53have to sing a happy birthday song three
- 4:04:56times so without using a method we can
- 4:04:58do something like this we'll create one
- 4:05:00verse of a happy birthday song I'll just
- 4:05:03make something up let's
- 4:05:06say happy birthday to you
- 4:05:14happy birthday dear
- 4:05:19you you are we'll insert somebody's age
- 4:05:23here but let's say you are X years
- 4:05:27old then happy birthday to you
- 4:05:30again then I'll add a new line character
- 4:05:32to the end so when we run this
- 4:05:35code we sing Happy Birthday once if I
- 4:05:39need to sing Happy Birthday let's say
- 4:05:41three times well without using a method
- 4:05:44we could although it's inefficient copy
- 4:05:46this code and paste it to additional
- 4:05:49times then we're singing Happy Birthday
- 4:05:52a total of three times well you know
- 4:05:54this does work but we tend to be
- 4:05:56repeating ourselves a lot in programming
- 4:05:59we like to follow the dry principle
- 4:06:01don't repeat yourself how can we not
- 4:06:03repeat ourselves if we don't have
- 4:06:06to well let's create a method a method
- 4:06:09is just a block of reusable code so to
- 4:06:12create a method in Java outside of the
- 4:06:14main method do pay attention to these
- 4:06:16curly braces we'll create a new method a
- 4:06:19method can return something for now
- 4:06:21we'll type void much like our main
- 4:06:23method and we need a name for this
- 4:06:26method the method should be descriptive
- 4:06:28of what it does let's call it the happy
- 4:06:31birthday method all it does is sing
- 4:06:33happy birthday we need a return type a
- 4:06:36name for the method that's descriptive
- 4:06:38parentheses then curly
- 4:06:41braces let's cut our song of Happy
- 4:06:43Birthday
- 4:06:44birthday and paste it within the happy
- 4:06:46birthday method at any point to use this
- 4:06:49method I have to call it call it by its
- 4:06:52name happy birthday then add a set of
- 4:06:55parentheses so we have one issue in Java
- 4:06:58Java wants this method to be static much
- 4:07:01like the main method that we work within
- 4:07:03so in order for this program to
- 4:07:06work we need to add this keyword of
- 4:07:08static normally you don't need this
- 4:07:10keyword but since we're calling this
- 4:07:12method of happy birthday from another
- 4:07:14method that's static this method also
- 4:07:16needs to be static later on in the
- 4:07:18series we'll discuss what static does
- 4:07:20you'll likely have to include that for
- 4:07:22now all right let's do a test run so we
- 4:07:25are going to sing Happy Birthday once so
- 4:07:28again to call a method type the name of
- 4:07:30the method then a set of parentheses the
- 4:07:32pair of parentheses kind of represent a
- 4:07:34pair of telephones that's how I think of
- 4:07:36it at least so now we should sing Happy
- 4:07:40Birthday now this code is reusable we
- 4:07:43write it once and reuse it whenever we
- 4:07:45want we just have to call it again we
- 4:07:48will call the happy birthday method
- 4:07:52again now we are executing that block of
- 4:07:55code two times or even
- 4:07:59three happy
- 4:08:05birthday one thing you should be aware
- 4:08:07of is that methods are unfamiliar with
- 4:08:10any variables declared within other
- 4:08:12methods so let's say we have two
- 4:08:14variables a string of
- 4:08:18name type in your first name whatever it
- 4:08:21is and an integer of age set it to be
- 4:08:25some
- 4:08:26age if I was to use these variables
- 4:08:29within my happy birthday method well the
- 4:08:31happy birthday method actually doesn't
- 4:08:33know what these
- 4:08:34are so let's attempt to use them and see
- 4:08:37what happens within our happy birthday
- 4:08:39song Let's convert this print statement
- 4:08:41to be a print F statement and we will
- 4:08:44insert our name variable replace U with
- 4:08:48the format specifier and we are
- 4:08:50displaying a string see we already have
- 4:08:53a warning cannot resolve symbol name
- 4:08:56I'll attempt to run this code and we'll
- 4:08:58see what happens
- 4:09:00exactly Java cannot find variable
- 4:09:04name methods are not familiar with
- 4:09:07variables inside of other methods if we
- 4:09:10live in this method of main if it's our
- 4:09:12house so to say
- 4:09:13we can't see what's going on in our
- 4:09:15neighbor's house one way in which we can
- 4:09:17solve this is to pass arguments to this
- 4:09:20method from one method we can send
- 4:09:22information to another method these are
- 4:09:25known as
- 4:09:26arguments so when we sing Happy Birthday
- 4:09:30within the set of parentheses within the
- 4:09:31pair of telephones we can pass a value
- 4:09:34or a variable so let's send our variable
- 4:09:37of name to this method of happy birthday
- 4:09:41anything that you send a method is known
- 4:09:43as an argument but we have one issue
- 4:09:47though our method of happy birthday
- 4:09:50isn't set up to receive arguments if you
- 4:09:53send any values or variables to a method
- 4:09:56via arguments you need a matching set of
- 4:09:59what is called parameters our happy
- 4:10:01birthday method isn't set up to receive
- 4:10:03these arguments we need a matching set
- 4:10:06of parameters to set up some parameters
- 4:10:09we're going to list the data type of
- 4:10:10what we're receiving we're receiving a
- 4:10:12string Val value so the data type is
- 4:10:15going to be string and then we need a
- 4:10:17name for what we're receiving it doesn't
- 4:10:20have to be exactly the same but it might
- 4:10:22be helpful to keep these consistent so
- 4:10:25let's perform a test run after this
- 4:10:27print F statement I'm going to add a new
- 4:10:29line
- 4:10:33character happy birthday to you happy
- 4:10:35birthday dear your variable
- 4:10:38name so if I was to change my name to
- 4:10:41let's say sponge Bob we'll insert this
- 4:10:45value at this location within our
- 4:10:49method happy birthday dear
- 4:10:53SpongeBob let's also pass in our
- 4:10:55variable of age so any arguments you're
- 4:10:57sending to a method you're going to
- 4:10:59comma
- 4:11:01separate however we need a matching set
- 4:11:03of parameters our method isn't set up to
- 4:11:06receive this variable of age let's
- 4:11:08perform another test
- 4:11:10run so we have an error actual inform
- 4:11:13formal argument lists differ in length
- 4:11:16we need a matching set of parameters to
- 4:11:19the arguments we're receiving not only
- 4:11:21do we need a string
- 4:11:23variable we need an integer variable
- 4:11:25because we're receiving an integer so
- 4:11:28let's say int
- 4:11:30age we'll insert our age variable right
- 4:11:33here we're displaying an integer so we
- 4:11:35need percent D this will be a print F
- 4:11:38statement then I will include a new line
- 4:11:41character we are inserting our age
- 4:11:44variable let's say SpongeBob is 30 years
- 4:11:48old all right let's see how this works
- 4:11:52now happy birthday to you happy birthday
- 4:11:55dear SpongeBob you are 30 years old
- 4:11:58happy birthday to
- 4:12:00you so with these parameters you can
- 4:12:03rename them when you receive these
- 4:12:05arguments just be sure that the order
- 4:12:07and their data type is correct for
- 4:12:09example we could rename name as let's
- 4:12:12say birth day
- 4:12:17boy you know this would work too
- 4:12:20SpongeBob is the birthday
- 4:12:23boy the names of your parameters can be
- 4:12:25different from the
- 4:12:29arguments just be sure that you get the
- 4:12:31data type correct and the order in which
- 4:12:34you receive these
- 4:12:35arguments let's replace the name of
- 4:12:37SpongeBob with Patrick Patrick will be
- 4:12:4138 so now now with the happy birthday
- 4:12:44method we're inserting new variables
- 4:12:47happy birthday dear Patrick you are 38
- 4:12:50years
- 4:12:51old with methods they also have the
- 4:12:54capability to return a value so I'm
- 4:12:57going to collapse this happy birthday
- 4:12:59method we won't be working with it
- 4:13:02anymore let's create a method to square
- 4:13:05a number we'll create a method to square
- 4:13:07a number then return the result normally
- 4:13:10if we're not returning anything we would
- 4:13:12type this keyword avoid void but since
- 4:13:15we're returning a double we have to list
- 4:13:17the data type of what we're returning
- 4:13:19we're returning a double then we need a
- 4:13:21name for this method let's say double
- 4:13:27square all we're going to do is use the
- 4:13:30return keyword let me zoom in a
- 4:13:33little let's return a number times
- 4:13:37itself number we're going to receive one
- 4:13:41argument an argument of number we have
- 4:13:43to set up a matching parameter our
- 4:13:46number is going to be a double double
- 4:13:49number let's call the square method I
- 4:13:52will Square the number three and again
- 4:13:55since we're calling a method that's not
- 4:13:57static from another static method we
- 4:13:59need to add this keyword of static in
- 4:14:02order for it to run there'll be more on
- 4:14:03the static modifier in the future so if
- 4:14:06I was to run
- 4:14:09this we get no output we're returning a
- 4:14:12double back to the exact spot in which
- 4:14:14we call it we could assign it to a
- 4:14:16variable or print it
- 4:14:18directly let's say double result equals
- 4:14:22Square the number three then display the
- 4:14:26result so 3^ squar would give us
- 4:14:30nine so when returning something just
- 4:14:33imagine that after the method ends we're
- 4:14:35replacing that method call with whatever
- 4:14:37we're returning in this case nine
- 4:14:40picture it that way and then we're
- 4:14:41assigning nine to our double of
- 4:14:44result or we can just output it directly
- 4:14:46if you would prefer to do it that way
- 4:14:49let's print 3^
- 4:14:53squar and that would also give us
- 4:14:58n now let's create a method to cube
- 4:15:01we're going to return a double double
- 4:15:04Cube we have one
- 4:15:07parameter a double of
- 4:15:10number we're going to return our number
- 4:15:14time number time number that would cube
- 4:15:18a number that we're
- 4:15:19given and again we need to add static
- 4:15:22because we're calling a method from
- 4:15:24another method that's static all right
- 4:15:26this time we will call the cube method
- 4:15:29and return the
- 4:15:31result so 3 cubed is 27 3 * 3 * 3 is
- 4:15:3727 one last
- 4:15:40example we'll create a method to return
- 4:15:43full name after being given a first name
- 4:15:45and a last name we're going to return a
- 4:15:49string we'll call this method get full
- 4:15:52name we'll have two
- 4:15:55parameters a string of first for first
- 4:15:58name and a string of last for last name
- 4:16:02we're going to
- 4:16:04return our first
- 4:16:06name plus a space to separate
- 4:16:09them plus our last name of last
- 4:16:14and again we have to add
- 4:16:16static okay let's declare a string
- 4:16:20variable of full name equals call be get
- 4:16:25full name method then pass in some
- 4:16:28arguments let's pass in
- 4:16:31SpongeBob then for the next argument
- 4:16:35SquarePants and then we'll print the
- 4:16:37full
- 4:16:38name print full name
- 4:16:43our method returns one long string of
- 4:16:46SpongeBob
- 4:16:47SquarePants the return keyword will
- 4:16:50return a value after the method ends it
- 4:16:52will return that value back to the place
- 4:16:55in which you call that method after this
- 4:16:58method of get full name is complete just
- 4:17:00imagine that we're replacing it with one
- 4:17:03long string of SpongeBob
- 4:17:05SquarePants like this and then assigning
- 4:17:08it to this
- 4:17:09variable okay so I lied I have one last
- 4:17:12good example to give you will create a
- 4:17:14method to verify a user's age normally
- 4:17:17in the United States at least you have
- 4:17:19to be 18 or older to sign up for things
- 4:17:22like a credit card we'll create a method
- 4:17:24to check a user's age again we're going
- 4:17:26to use static because we're calling a
- 4:17:28method from another method that's static
- 4:17:31let me zoom in we're going to return a
- 4:17:33Boolean this time the name of the method
- 4:17:37let's name it age
- 4:17:41check and there's going to be one
- 4:17:43parameter and integer of age if we're
- 4:17:47going to do an age verification check we
- 4:17:50could use an if statement so within our
- 4:17:52method we'll write an if statement if
- 4:17:55our age parameter is greater than or
- 4:17:58equal to
- 4:17:5918 then let's return
- 4:18:04true else we're going to return
- 4:18:08false this can be simplified to the
- 4:18:11following
- 4:18:13return AG is greater than or equal to 18
- 4:18:15you could do it either way for a
- 4:18:18beginner it might be easier to read if
- 4:18:20we have separate IFL statements but both
- 4:18:22work the same all right so we now have a
- 4:18:25method to check a user's
- 4:18:28age so now we can use this
- 4:18:31method I will create an integer variable
- 4:18:34of age set it equal to some
- 4:18:37age I'll use an if statement we're
- 4:18:40returning a Boolean value I can use that
- 4:18:43within an if
- 4:18:44statement if H
- 4:18:47check then pass in an argument we will
- 4:18:50pass in our variable of
- 4:18:52age if this is
- 4:18:54true if age is greater than or equal to
- 4:18:5718 you return
- 4:19:00true so let's print the
- 4:19:03following you may sign up maybe we're
- 4:19:07signing up for a credit
- 4:19:08card else let's
- 4:19:11print you must be 18 + 2 sign
- 4:19:17up our ages
- 4:19:2021 you may sign up but if we were
- 4:19:2512 that method returns false you must be
- 4:19:2918 plus to sign
- 4:19:31up so that's a method everybody it's a
- 4:19:34block of reusable code that is executed
- 4:19:37when called again I like to think of the
- 4:19:40parentheses after a method as a pair of
- 4:19:42telones talking to each other to use a
- 4:19:44method you have to call it and well
- 4:19:47everybody those are methods in
- 4:19:51Java hello everybody in this video I'm
- 4:19:53going to explain overloaded methods in
- 4:19:55Java in Java overloaded methods are
- 4:19:58methods that share the same name but
- 4:20:00they have different parameters a
- 4:20:03method's name plus its parameters will
- 4:20:06give you a unique method signature each
- 4:20:09method signature needs to be unique no
- 4:20:11two methods can share the same signature
- 4:20:13but they can share the same name let me
- 4:20:16give you a demonstration let's say we
- 4:20:18have a method it's going to return a
- 4:20:20double the method name will be add we'll
- 4:20:24add two numbers together add is the
- 4:20:27method name and then we'll set up some
- 4:20:29parameters we'll have double A for the
- 4:20:32first number and Double B all we're
- 4:20:36going to do is return a plus b it's a
- 4:20:39simple
- 4:20:41method so I'm I'm going to
- 4:20:44print add the numbers 1 + 2 and then
- 4:20:48print
- 4:20:49it this will give us
- 4:20:52three what if I would like to add a
- 4:20:54third
- 4:20:55number you can see that Java is already
- 4:20:57giving us a
- 4:20:59warning expected two arguments but three
- 4:21:01found so if I were to run this program
- 4:21:04we get an error actual and formal
- 4:21:07argument lists differ in
- 4:21:11length once solution is that I could
- 4:21:13create another method also named ad but
- 4:21:16it has different parameters where we
- 4:21:18accept three numbers so let me just copy
- 4:21:21this method paste it then I'll add
- 4:21:23another parameter for Double C return a
- 4:21:28plus b plus C and that should
- 4:21:31work now the number is six these two
- 4:21:35methods share the same name but they
- 4:21:38have different
- 4:21:39parameters so that's actually legal in
- 4:21:42Java
- 4:21:43a method's name plus its parameters will
- 4:21:46give you a unique method signature no
- 4:21:48two methods can share the same
- 4:21:51signature with our second method let's
- 4:21:53delete the last parameter of Double C
- 4:21:56and attempt to run this and we'll see
- 4:21:58what happens
- 4:22:00exactly well we get an error message
- 4:22:03method add is already defined in class
- 4:22:05main Java is saying that we have two
- 4:22:08methods that share the same name and the
- 4:22:11same exact parameters methods can share
- 4:22:13the same name but they need different
- 4:22:15parameters if that's the case now if I
- 4:22:18would like to add another number like
- 4:22:19four I could create another overloaded
- 4:22:22method also named add but this one will
- 4:22:25have four parameters Double D return a
- 4:22:30plus B+ C+
- 4:22:33D and this works
- 4:22:36too let's go over another
- 4:22:39example let's pretend that we're baking
- 4:22:42a pizza
- 4:22:43we'll have a string variable of
- 4:22:46pizza we'll create a method to bake a
- 4:22:48pizza but we have to pass an ingredients
- 4:22:51we'll declare a method with static we'll
- 4:22:54return a string that's the return type
- 4:22:57the method name will be Bake
- 4:22:59Pizza our parameters are going to be a
- 4:23:02set of
- 4:23:03ingredients for this method we'll just
- 4:23:06accept a string of
- 4:23:10bread we'll just return
- 4:23:14our bread variable plus the word
- 4:23:19pizza so this could be for example a
- 4:23:22thick crust pizza or a flatbread pizza
- 4:23:25so to
- 4:23:26say let's set our pizza variable equal
- 4:23:29to called the bake pizza
- 4:23:32method now this method only has a bread
- 4:23:35setup as a parameter an
- 4:23:38ingredient let's say flat bread for the
- 4:23:42bread and then we'll print our
- 4:23:45pizza system. out. print line our string
- 4:23:48variable of
- 4:23:50pizza here's what we have we have a
- 4:23:53string of flatbread pizza so when baking
- 4:23:56a pizza there's a lot of different
- 4:23:58ingredients let's create another method
- 4:24:00of baked pizza but we have a parameter
- 4:24:03of bread and a parameter of cheese let's
- 4:24:06copy this method paste it but we have to
- 4:24:09change the parameters we're already
- 4:24:11receiving a warning
- 4:24:12we'll have string bread and string
- 4:24:15cheese no pun intended let me zoom in
- 4:24:19now those warnings have went away we
- 4:24:21have two methods with the same name but
- 4:24:23they have different parameters which is
- 4:24:25fine we can do that so this time let's
- 4:24:28return a string of cheese plus a space
- 4:24:34character plus our bread plus the word
- 4:24:37Pizza let's pass in a type of
- 4:24:40cheese Mozzarella
- 4:24:43I probably misspelled that actually I
- 4:24:45spelled it correctly okay now we'll bake
- 4:24:47our
- 4:24:50pizza we have a mozzarella flatbread
- 4:24:53pizza if there's two or more methods
- 4:24:56with the same name we'll end up using
- 4:24:57the method where the parameters match
- 4:25:00we'll create one more overloaded method
- 4:25:02we'll create another baked pizza method
- 4:25:04but this one will have a custom topping
- 4:25:07string
- 4:25:08topping we'll return topping
- 4:25:13plus a space
- 4:25:14character plus everything we had
- 4:25:18previously then we can pass in A new
- 4:25:23ingredient how do you spell pepperoni I
- 4:25:25can never spell
- 4:25:26it
- 4:25:31pepperoni now this time we have a
- 4:25:33pepperoni mozzarella flatbread
- 4:25:36pizza all right everybody so those are
- 4:25:39overloaded methods methods can share the
- 4:25:41same name but they need to have a
- 4:25:43different list of
- 4:25:45parameters a method's name plus its
- 4:25:47parameters will give you a unique method
- 4:25:49signature and no two methods can share
- 4:25:52the same signature and well everybody
- 4:25:54those are overloaded methods in
- 4:25:58Java I got a relatively quick video for
- 4:26:01you today today we're going to discuss
- 4:26:03variable scope in Java variable scope is
- 4:26:06where a variable can be accessed there's
- 4:26:08two levels we'll discuss today local and
- 4:26:11class
- 4:26:12we'll cover local
- 4:26:14first inside of a method if you declare
- 4:26:17a variable it has what is considered a
- 4:26:19local scope in this demonstration let's
- 4:26:22create a variable of x x will equal some
- 4:26:25number such as one and then you know we
- 4:26:27can print whatever X
- 4:26:30is xal
- 4:26:321 a variable declared inside of a method
- 4:26:36has a local scope and I'll just add a
- 4:26:38comment that this is a local variable
- 4:26:42outside of the set of curly braces where
- 4:26:45this variable is declared X isn't going
- 4:26:47to be recognized that's why if we create
- 4:26:49another method we would have to pass it
- 4:26:51as an
- 4:26:52argument let's create another method
- 4:26:55this will be a static method that
- 4:26:57doesn't return anything I'm just going
- 4:26:59to say it's the do something
- 4:27:01method inside of other methods you can
- 4:27:04declare variables that have the same
- 4:27:06name within this other method we're
- 4:27:08going to create an integer variable of X
- 4:27:11but X is going to equal two so Java
- 4:27:14isn't giving us any
- 4:27:16warnings it is legal to have variables
- 4:27:19with the same name as long as they're
- 4:27:21within different methods the moment I
- 4:27:23place this variable within my main
- 4:27:26method Java is giving us a warning
- 4:27:29variable X is already defined in the
- 4:27:31scope this local scope variable X within
- 4:27:35main has a local scope variable X within
- 4:27:38the do something method also has a local
- 4:27:41scope and I'll just add a comment that
- 4:27:43this is a local
- 4:27:45variable if I was to print X within this
- 4:27:49method and if I was to call this
- 4:27:52method what do you think will output one
- 4:27:56or two take a
- 4:27:59guess
- 4:28:00two within the context of our do
- 4:28:03something method xal 2 this method isn't
- 4:28:07aware of the existence of this variable
- 4:28:09within this method of main they each
- 4:28:11have a local scope it's kind of like
- 4:28:14their neighbors they can't see inside of
- 4:28:16each other's house now a class variable
- 4:28:19is declared inside of a class but not
- 4:28:22within any methods you usually see them
- 4:28:24at the top let's create a static
- 4:28:27variable an integer of X where x equals
- 4:28:323 variable X within this class of main
- 4:28:35has a class scope in our analogy of
- 4:28:39houses you could say that this variable
- 4:28:42is out in the street these two methods
- 4:28:44can look out their window and see
- 4:28:46variable X and that it equals
- 4:28:48three if I was to delete these two local
- 4:28:52variables and print X here's the
- 4:28:56result xal
- 4:28:593 or within our main method if I was to
- 4:29:02print
- 4:29:04x x also equals 3 let's say that each
- 4:29:09method is a house they're neighbors you
- 4:29:12you could say that a class variable is
- 4:29:14outside they're walking down the street
- 4:29:17both of these methods can look outside
- 4:29:19their window and see this variable now
- 4:29:22if you had a mix of both local and class
- 4:29:24variables that share the same name let's
- 4:29:27say int xals 1 and this was a local
- 4:29:31variable and within our method of do
- 4:29:34something xal
- 4:29:38two let's call the do something method
- 4:29:43we'll print X within the main method and
- 4:29:46then within the do something
- 4:29:48method what do you think the output is
- 4:29:50going to
- 4:29:51be well we have one and
- 4:29:55two even though there's a third X
- 4:29:57variable within the class scope Java
- 4:30:00likes to use any local variables first
- 4:30:02if they share the same name as any class
- 4:30:05variables within our method of main this
- 4:30:09method thinks xal 1 it's going to use
- 4:30:11theal scope first you'll likely want to
- 4:30:14stick with local variables over class
- 4:30:17variables there are a few situations
- 4:30:19when a class variable would be better
- 4:30:21such as if you have constants where the
- 4:30:23value doesn't change or when working
- 4:30:26with object-oriented programming which
- 4:30:27is a whole another topic so that is
- 4:30:30variable scope in Java anything with the
- 4:30:32local scope is declared inside of a
- 4:30:34method or otherwise within a set of
- 4:30:36curly braces and is only recognized
- 4:30:39within this method other methods aren't
- 4:30:41aware of its existence that's sometimes
- 4:30:43why we pass them as arguments to other
- 4:30:45methods class variables are declared
- 4:30:48inside of a class they are recognized
- 4:30:50throughout the entire class and well
- 4:30:52everybody that is variable scope in
- 4:30:57Java hello everybody in this video we're
- 4:30:59going to create a very simple banking
- 4:31:02program using Java this video is meant
- 4:31:04to be a beginner's project to help us
- 4:31:06understand how methods work so if that
- 4:31:08sounds good to you let's get started as
- 4:31:11a beginner it might be helpful to you to
- 4:31:13break up your project into separate
- 4:31:15steps then tackle this project one step
- 4:31:17at a time I'll add a few comments of the
- 4:31:20different steps our program is going to
- 4:31:21take so first we're going to declare our
- 4:31:25variables I misspelled
- 4:31:29that then we will display a
- 4:31:33menu a user is going to pick a choice
- 4:31:35from this menu depending on what they
- 4:31:37want to do they can make a deposit show
- 4:31:39their balance make a withdrawal so on
- 4:31:41and so forth we will get and
- 4:31:46process users
- 4:31:50Choice we'll create a method of show
- 4:31:53balance in order to display the balance
- 4:31:55to the
- 4:31:56user let me zoom
- 4:31:59in another method to make a
- 4:32:02deposit then a method to withdraw
- 4:32:05withdraw funds we'll just say
- 4:32:08withdraw and then we will create an exit
- 4:32:11message at at the end once the user
- 4:32:15exits so again as a beginner it might be
- 4:32:18helpful to you to break up your program
- 4:32:20into different steps we'll start with
- 4:32:22our step of declare variables here's the
- 4:32:25different variables we'll need we have
- 4:32:27an account our account has a balance
- 4:32:29such as dollars and cents so a double
- 4:32:32would make sense for the balance because
- 4:32:35it includes a decimal portion sents so
- 4:32:38we have our balance we'll need a scanner
- 4:32:40because we're accepting us user input
- 4:32:42scanner scanner equals new
- 4:32:46scanner type system.in import this
- 4:32:50class import java.util.scanner and then
- 4:32:54we are going to close our scanner when
- 4:32:56we're done with it let's take care of
- 4:32:58that now because I'm probably going to
- 4:33:02forget we have our scanner our
- 4:33:05balance we're going to continue this
- 4:33:07program as long as a user doesn't exit
- 4:33:10we'll need a while loop we will declare
- 4:33:12and assign a Boolean variable of is
- 4:33:15running set that to be true continue
- 4:33:19this program while is running is
- 4:33:23true then we will accept a choice from
- 4:33:25the user this will be an integer a user
- 4:33:28is going to pick one to show balance two
- 4:33:31to deposit three to withdraw or four to
- 4:33:34exit the user is going to fill this in
- 4:33:36later using the scanner all right we
- 4:33:38have declared our
- 4:33:40variables now we'll a menu to the
- 4:33:43user I will print the following let's
- 4:33:46say banking
- 4:33:48program now you don't need to do this
- 4:33:51but I like to add some separators using
- 4:33:53a bunch of
- 4:33:55asterisks I just think it makes it look
- 4:34:02cool we will lay out the different
- 4:34:05options we will have option one option
- 4:34:09one will be to show balance
- 4:34:15option two will be to
- 4:34:20deposit option three will be to
- 4:34:27withdraw four will be to
- 4:34:30exit and then I'll add another
- 4:34:33separator again not necessary I think it
- 4:34:36would just look more organized now we
- 4:34:38need to get and process the user's
- 4:34:40choice
- 4:34:43we have a variable of choice we'll need
- 4:34:46to create a
- 4:34:48prompt we will say enter your choice
- 4:34:53then hint one through
- 4:34:564 I'll use print instead of print line
- 4:34:59because I like the user input on the
- 4:35:01same line as the prompt we will assign
- 4:35:04our variable of choice equal to use the
- 4:35:07scanner call the next int method
- 4:35:12ideally a user is going to type in a
- 4:35:14number 1 through 4 to do one of the
- 4:35:16following now we'll process the user's
- 4:35:19choice I think an enhanced switch would
- 4:35:21be great for this we will create a
- 4:35:25switch and examine our variable of
- 4:35:28choice we're examining our choice
- 4:35:30against matching cases if Choice equals
- 4:35:33a case of one we'll write an arrow to do
- 4:35:37something we'll do this we're going to
- 4:35:40call a method to show our B balance
- 4:35:42because one means the user wants to show
- 4:35:44their
- 4:35:45balance we'll fill that in later let's
- 4:35:48write a test message I'll use
- 4:35:50system.out.print LINE let's say show
- 4:35:56balance case two will be for
- 4:36:00deposit we'll just print a test message
- 4:36:03of deposit again we're going to fill
- 4:36:05this method in later and call
- 4:36:10it case three is for
- 4:36:12withdraw case
- 4:36:14three
- 4:36:18withdraw case four is to exit that we
- 4:36:21can actually fill in right away it's
- 4:36:23pretty
- 4:36:25simple we'll take our Boolean variable
- 4:36:27of is
- 4:36:29running and set it to be
- 4:36:32false then we'll add a default case in
- 4:36:35case somebody doesn't type in a number 1
- 4:36:37through
- 4:36:38four we'll output
- 4:36:42invalid
- 4:36:44Choice Let's test numbers 1 through
- 4:36:47three
- 4:36:49first enter a choice I'll pick one
- 4:36:52that's for show balance again we'll be
- 4:36:54calling a method later to show our
- 4:36:57balance two is for deposit we're
- 4:37:02depositing three is for
- 4:37:04withdraw four is to
- 4:37:07exit but it doesn't do anything
- 4:37:09currently
- 4:37:12then any other choice such as
- 4:37:15five prints out invalid
- 4:37:19choice we want this program to continue
- 4:37:21running as long as is running remains
- 4:37:25true so let's enclose our code within a
- 4:37:29loop a while
- 4:37:31loop
- 4:37:33while is running you don't necessarily
- 4:37:37need to say equals true if it's a
- 4:37:39Boolean you can just say the Boolean
- 4:37:40variables name name while is running is
- 4:37:43true then do all this code so I'm going
- 4:37:46to cut our code currently and paste it
- 4:37:49within the wild
- 4:37:51Loop now we can test is
- 4:37:55running one is to show
- 4:37:58balance we show our balance two is to
- 4:38:03deposit we
- 4:38:04deposit three is to
- 4:38:07withdraw withdraw four is to exit and we
- 4:38:11should exit processed finished with exit
- 4:38:14Code Zero we know that that works
- 4:38:17now our next step is to show balance and
- 4:38:21this is going to be a
- 4:38:23method outside of our main method we're
- 4:38:27going to create a new method of show
- 4:38:31balance this has to be a static method
- 4:38:34because our main method is also
- 4:38:38static there will be no return type the
- 4:38:41return type is void the method name will
- 4:38:44be show
- 4:38:46balance there's going to be one
- 4:38:48parameter a double of balance when we
- 4:38:52call this method we have to pass in our
- 4:38:54balance
- 4:38:55variable within this method we're going
- 4:38:58to use a print F
- 4:39:03statement and I'll zoom in a little
- 4:39:06within this print F statement pick a
- 4:39:08unit of currency you would like to use
- 4:39:09for this banking program all use
- 4:39:11American dollars we need a placeholder
- 4:39:14we'll use a percent sign and then F to
- 4:39:17display a double f means floating Point
- 4:39:19number we will display our
- 4:39:23balance and from the beginning I'm
- 4:39:25actually going to set my balance equal
- 4:39:27to zero we're going to be testing
- 4:39:31it then let me zoom out for case one
- 4:39:36we're going to call the show balance
- 4:39:38method but we have to pass in our
- 4:39:40balance variable
- 4:39:44all right let's test
- 4:39:46it enter a choice I'll pick one to show
- 4:39:49our
- 4:39:50balance uh here's what we got currently
- 4:39:530 and a bunch of zeros after the decimal
- 4:39:57we are forgetting that new line
- 4:39:58character too so let's add a new line
- 4:40:00character to the end before I
- 4:40:03forget if you would like to display two
- 4:40:05digits after the decimal for
- 4:40:09example normally when you display a
- 4:40:11floating Point number using print F you
- 4:40:13display six digits after the decimal to
- 4:40:16limit it to two we can add the
- 4:40:18following after our placeholder our
- 4:40:20percent sign we're going to add
- 4:40:2502 and to your choice I will type in one
- 4:40:28to show our balance our balance is $0
- 4:40:31and0 we're displaying two digits after
- 4:40:34the
- 4:40:36decimal let's change our initial balance
- 4:40:38to be $10 and
- 4:40:4499 ENT to your choice I will type in one
- 4:40:47our balance is $10 and 99 I think I'm
- 4:40:51also going to add a separator before
- 4:40:52displaying the balance too to help with
- 4:40:54readability although it's not necessary
- 4:40:57so I'm going to cut one of these lines
- 4:40:59where we display a bunch of
- 4:41:01asterisks and print it right before
- 4:41:03displaying the
- 4:41:06balance let's show our balance and
- 4:41:09here's our balance $10 and 99 and we
- 4:41:12have some of those separators before and
- 4:41:14after which I think looks
- 4:41:16nice our show balance method is complete
- 4:41:19I'm going to collapse this method we
- 4:41:21don't need to code within it
- 4:41:24anymore our next method is to make a
- 4:41:26deposit we'll create a method of
- 4:41:31deposit oh and then be sure to set your
- 4:41:33balance back to
- 4:41:38zero we will create a static method the
- 4:41:41return type is going to be double we
- 4:41:44will declare this method as the deposit
- 4:41:46method there will be no parameters but
- 4:41:49we will be returning something a double
- 4:41:52inside of this method I will create a
- 4:41:54local
- 4:41:55variable the data type is double the
- 4:41:58name will be amount a user is going to
- 4:42:01type in an amount the amount that
- 4:42:03they're depositing using this method
- 4:42:05this method isn't going to work properly
- 4:42:07until we return
- 4:42:09something add return statement
- 4:42:12just temporarily so everything runs fine
- 4:42:14I'm just going to return zero we'll take
- 4:42:17care of this later I just want
- 4:42:18everything to run a compile fine we will
- 4:42:21prompt a user to type in an
- 4:42:23amount I will use print instead of print
- 4:42:25line that's my own preference for
- 4:42:28prompts enter an amount to be
- 4:42:35deposited now here's one issue we're
- 4:42:38accepting user input from a user amount
- 4:42:41equals if I attempt to use our scanner
- 4:42:44scanner. next Double there's an
- 4:42:47issue our method isn't aware what the
- 4:42:50scanner is because we declared the
- 4:42:53scanner within a separate method our
- 4:42:56main method methods can't see inside of
- 4:42:58each other there's a few different
- 4:43:01options one we could create a local
- 4:43:03scanner within this method you know
- 4:43:06scanner scanner equals new scanner and
- 4:43:08all that I don't like the idea of
- 4:43:10creating creting several different
- 4:43:11scanners within this project because
- 4:43:13then we're leaving them all open to
- 4:43:15accept user input so why don't we do
- 4:43:18this let's take our scanner move it from
- 4:43:21this local scope and put it within a
- 4:43:23class scope so let's cut our scanner
- 4:43:26within the main
- 4:43:28method and stick it within the main
- 4:43:32class but it does need to be
- 4:43:34static because some static methods are
- 4:43:37using it this one scanner is accessible
- 4:43:39throughout the whole program now
- 4:43:42and that warning goes away so I think
- 4:43:45this would be better than creating
- 4:43:46several different scanners and leaving
- 4:43:48them all open throughout this program
- 4:43:50we're just going to reuse the same one
- 4:43:53at this point we're going to verify the
- 4:43:54amount that the user types
- 4:43:56in I'll use an if statement first if a
- 4:44:00user types in an amount that's negative
- 4:44:02well they can't make a deposit they
- 4:44:04can't deposit negative money that's
- 4:44:05stupid so we will check if our amount
- 4:44:09variable that the user types in is less
- 4:44:11than zero if that's the case we will
- 4:44:14print the
- 4:44:16following amount can't be
- 4:44:21negative and then we will return
- 4:44:26zero if the user does type in an amount
- 4:44:29that's valid we'll execute an lse
- 4:44:32statement where we're going to return
- 4:44:35let's cut our return statement at the
- 4:44:36end we'll return our amount
- 4:44:41now when we call our method we can
- 4:44:44delete this print line
- 4:44:46statement if a user selects two to make
- 4:44:48a deposit we're going to assign our
- 4:44:51balance
- 4:44:53variable balance equals our balance plus
- 4:44:58call the deposit
- 4:45:02method our deposit method is going to
- 4:45:05return a number an amount if it's valid
- 4:45:08we're going to return it back to the
- 4:45:09place in which we call it it we will add
- 4:45:12this to our current balance and reassign
- 4:45:14it to balance our balance will equal our
- 4:45:17current balance plus the deposit that we
- 4:45:19make we can shorten this too we could
- 4:45:22just say balance plus equals
- 4:45:25deposit okay let's do a test
- 4:45:29run banking program let's show our
- 4:45:32balance our balance is currently zero
- 4:45:35we'll select two to make a
- 4:45:37deposit two enter an amount to be
- 4:45:40deposited
- 4:45:42let's try and break it first let's say1
- 4:45:47million amount can't be negative then
- 4:45:50we're brought back to the
- 4:45:51menu let's try and make a deposit again
- 4:45:54enter an amount to be deposited let's
- 4:45:56say
- 4:45:58$150 and a nickel 5
- 4:46:02cents that seems to have worked let's
- 4:46:05show our balance again and here's our
- 4:46:08New Balance $150
- 4:46:11and 5 cents all right the deposit method
- 4:46:14is
- 4:46:15done we can collapse it we no longer
- 4:46:18need to code within it and now we need a
- 4:46:20withdraw
- 4:46:22method we will declare a new
- 4:46:26method it's a static method we'll be
- 4:46:28returning a
- 4:46:30double the name of this method will be
- 4:46:36withdraw there will be one parameter we
- 4:46:39will pass in a balance double
- 4:46:43balance we do need to return something
- 4:46:45so temporarily just so our program runs
- 4:46:47correctly I'm just going to return zero
- 4:46:50we'll change that
- 4:46:52later within this method I'm going to
- 4:46:54create a local variable of amount double
- 4:46:58amount we will prompt a
- 4:47:03user enter amount to be withdrawn
- 4:47:12we will assign our amount equal to we're
- 4:47:15going to use our scanner and remember
- 4:47:18it's within the class scope so we have
- 4:47:20access to it within this method scanner.
- 4:47:23nextt double to accept a
- 4:47:26double if somebody tries to withdraw
- 4:47:29money that they don't have we can check
- 4:47:32that within an if
- 4:47:33statement if the amount is greater than
- 4:47:37our balance we're passing in our balance
- 4:47:40so we know what what it is if somebody
- 4:47:42tries to withdraw a million dollars but
- 4:47:45they have no money in their bank account
- 4:47:47well they can't do
- 4:47:48that we will
- 4:47:51print
- 4:47:54insufficient
- 4:47:57funds what if they try and withdraw
- 4:47:59negative money we can check that within
- 4:48:01an else if
- 4:48:02statement else if our amount that the
- 4:48:06user types in is less than
- 4:48:09zero will
- 4:48:12output amount can't be
- 4:48:20negative else if everything checks out
- 4:48:23we'll return the amount I will cut our
- 4:48:26current return statement we will return
- 4:48:29the amount that the user types
- 4:48:31in return
- 4:48:33amount So within our if statement return
- 4:48:37zero within our else if statement return
- 4:48:40zero
- 4:48:42when we reach the end we do need to
- 4:48:44return something even if it is
- 4:48:46zero and I'm going to use print instead
- 4:48:49of print
- 4:48:50line so let's go back to our switch
- 4:48:53right here we're going to be returning a
- 4:48:56double we will take our
- 4:48:59balance equals we're assigning it take
- 4:49:03our current balance minus call the
- 4:49:06withdraw
- 4:49:08method but we have to pass in our our
- 4:49:11balance our original
- 4:49:13balance from our current balance we're
- 4:49:16subtracting the amount that we're
- 4:49:18withdrawing we can simplify this
- 4:49:20expression too we could say balance
- 4:49:23minus equals then call the withdraw
- 4:49:27method all right let's do a test
- 4:49:30run first we'll make a
- 4:49:33deposit I will deposit $100 to keep the
- 4:49:36simple I will show our balance we have
- 4:49:39$100
- 4:49:41I will try and withdraw like a kajillion
- 4:49:45dollars insufficient funds we don't have
- 4:49:48a kajillion
- 4:49:50dollar let's withdraw again I will try
- 4:49:53and withdraw negative money $
- 4:49:5842069 amount can't be negative we can't
- 4:50:01withdraw negative money okay let's try
- 4:50:03and withdraw something that's valid
- 4:50:08$501 that seems to have worked let's
- 4:50:11show our balance our balance is now
- 4:50:15$49.99 the last step is to exit and
- 4:50:18display a goodby
- 4:50:21message let's print the
- 4:50:24following thank
- 4:50:26you have a nice
- 4:50:29day just so the user knows that they
- 4:50:31have exited the
- 4:50:32program and for fun I'm going to add
- 4:50:35some text decoration some
- 4:50:38separators not necessary but I think
- 4:50:40it'll look
- 4:50:44nice all right let's run this one last
- 4:50:48time banking program enter your choice I
- 4:50:50will show our balance we have 0 I will
- 4:50:53make a deposit I will deposit
- 4:50:56$11,000 show our balance we have
- 4:50:58$1,000 I will
- 4:51:01withdraw let's withdraw a single penny
- 4:51:050.01 show our balance we now have
- 4:51:08$999.99
- 4:51:10sense I will press for to exit and we
- 4:51:14exit thank you have a nice
- 4:51:17day all right everybody that is a Java
- 4:51:21banking program that you can write as a
- 4:51:25beginner hey there everybody in this
- 4:51:27video we're going to create a dice
- 4:51:29rolling program a user is going to type
- 4:51:31in a number of dice they would like to
- 4:51:32roll based on the roll of each die we
- 4:51:35will displace masy art then at the end
- 4:51:37we will give the user a total the sum of
- 4:51:39all the dice that they have rolled so if
- 4:51:41that sounds good to you let's begin as a
- 4:51:44beginner it might be helpful to you to
- 4:51:46break down a large project into smaller
- 4:51:48steps let's list the order of steps
- 4:51:50we're going to take we will Begin by
- 4:51:52declaring our
- 4:51:54variables declare
- 4:51:57variables then we need to get the number
- 4:52:01of Dice from the
- 4:52:04user after we have the number of dice
- 4:52:07let's check to see if it's greater than
- 4:52:09zero we don't want somebody rolling
- 4:52:11negative dice check if number of
- 4:52:15dice is greater than
- 4:52:19zero we will roll all the
- 4:52:24dice get the
- 4:52:26total or the sum you could say the final
- 4:52:30part of this project we will display the
- 4:52:32asy art of
- 4:52:36dice all right here's a rough outline of
- 4:52:38our project the first step is to declare
- 4:52:41our
- 4:52:42variables we're accepting user input so
- 4:52:44we'll need a scanner scanner scanner
- 4:52:47equals new
- 4:52:49scanner type
- 4:52:51system.in import this class import
- 4:52:56java.util.scanner and then close our
- 4:52:58scanner when we're done with it scanner.
- 4:53:01close we'll be working with random
- 4:53:03numbers we'll need a random object to
- 4:53:05Generate random numbers random random
- 4:53:08equals new random
- 4:53:11import the
- 4:53:13class import java. u. random we'll need
- 4:53:18a number of dice the user is going to
- 4:53:20fill this in int num of dice how many
- 4:53:25dice are we
- 4:53:27rolling and a total int total we'll sum
- 4:53:31up all the dice for a
- 4:53:33total all right now we need to get the
- 4:53:36number of Dice from the user we'll need
- 4:53:38some input but we'll create a prompt
- 4:53:41first enter
- 4:53:44the number of dice to
- 4:53:48roll I'll use print rather than print
- 4:53:51line we will assign our variable of
- 4:53:54number of dice number of dice we will
- 4:53:58use our scanner to accept some user
- 4:54:00input we are looking for an integer
- 4:54:03we'll use the next int method of our
- 4:54:05scanner scanner. next
- 4:54:08in now we need to to check to see if the
- 4:54:11number of dice number of dice is greater
- 4:54:14than
- 4:54:15zero so let's take care of that we'll
- 4:54:18use an if statement to check
- 4:54:20that if num of dice is greater than zero
- 4:54:26then we'll roll some
- 4:54:27dice we'll get to that
- 4:54:30momentarily else we will print the
- 4:54:34following let's say number of
- 4:54:37dice must be greater than
- 4:54:42zero within our if statement let's say
- 4:54:45just
- 4:54:46temporarily you
- 4:54:49roll the dice okay we're doing a test
- 4:54:54run enter the number of dice to roll
- 4:54:57let's say I try and roll zero dice
- 4:55:00number of dice must be greater than zero
- 4:55:04let's try this again let's say I roll
- 4:55:06three
- 4:55:07Dice and then we're just printing a test
- 4:55:10message
- 4:55:10you rolled the dice okay we know that
- 4:55:13that works we can delete this print
- 4:55:15statement we no longer need it we were
- 4:55:17just testing
- 4:55:18it now we actually need to roll all the
- 4:55:21dice we'll take care of that within our
- 4:55:23if statement if the number of dice is
- 4:55:25greater than zero then we will roll the
- 4:55:28dice we're going to create a for Loop
- 4:55:30that will iterate once based on the
- 4:55:32number of dice we have so let's create a
- 4:55:35for
- 4:55:36Loop for in I will be our index in I
- 4:55:41equals z we will continue as long as I
- 4:55:44is less than our number of
- 4:55:46dice then increment our index of I by
- 4:55:49One during each
- 4:55:51cycle within this for loop we're going
- 4:55:53to generate a random number I will
- 4:55:56create a local variable of r r will
- 4:56:00equal now we'll use a random object to
- 4:56:02generate a random number a random number
- 4:56:05between 1 and six we will use the next
- 4:56:08int method of our random object
- 4:56:11we will specify our range 1 through 7
- 4:56:15the first number is inclusive the second
- 4:56:17number is exclusive this will give us a
- 4:56:20random number between 1 and six
- 4:56:22then once we have our roll let's print
- 4:56:26it
- 4:56:27temporarily let's print you rolled colon
- 4:56:33space plus our local variable of Ro
- 4:56:41let's say we would like to roll four
- 4:56:43dice you rolled five you rolled five you
- 4:56:46rolled one you rolled two then we'll add
- 4:56:49these numbers together for a sum or
- 4:56:52total let's take our
- 4:56:55total total equals our total plus our
- 4:57:00roll and this is within the for Loop so
- 4:57:04Java does want us to initialize this
- 4:57:07variable so set total equal to zero when
- 4:57:10you declare it so total equals total
- 4:57:14whatever it currently is in this case
- 4:57:16zero plus roll we can shorten this too
- 4:57:19we could just say total plus equals roll
- 4:57:22we'll use the augmented assignment
- 4:57:24operator for
- 4:57:25addition then when we escape the for
- 4:57:28Loop we will print the
- 4:57:31total let's say total colon space plus
- 4:57:35our variable of total let's do a test
- 4:57:38run I Will Roll three dice we have 3 2 2
- 4:57:44for a total of seven 3 5 7 okay we know
- 4:57:49that that
- 4:57:50works we have rolled all the dice and
- 4:57:52get the
- 4:57:55total now here comes the hard part we're
- 4:57:57going to display some ASI art based on
- 4:58:00the result of each die that is
- 4:58:02rolled so we're going to declare a
- 4:58:04method to take care of that for
- 4:58:07us let's do so outside of the main
- 4:58:09method
- 4:58:11we will create a static method that's
- 4:58:14not going to return anything the return
- 4:58:16type is void let's name this method
- 4:58:20print
- 4:58:21D D is the singular version of the word
- 4:58:25dice we're going to set up one parameter
- 4:58:28int roll what did we roll we're going to
- 4:58:31call this method before we display the
- 4:58:34output of you rolled whatever the RO
- 4:58:37is we will call the method of printing
- 4:58:41die and pass in our
- 4:58:43roll it's going to be a number between 1
- 4:58:46and
- 4:58:47six let's head back to our print die
- 4:58:50method within the print die method we're
- 4:58:53going to create some asky art the asky
- 4:58:55art is going to be made of strings let's
- 4:58:57create a string of dice one equals now
- 4:59:01instead of a standard string that takes
- 4:59:03up one line we're going to create a
- 4:59:05multi-line string we need a triple set
- 4:59:09of double quotes
- 4:59:12anything between these two lines is a
- 4:59:14multi-line string it's within here that
- 4:59:17we're going to create some dice some asy
- 4:59:19art of dice so this will be an art
- 4:59:21lesson today let's start with seven
- 4:59:26dashes on the next line we'll create a
- 4:59:28vertical bar and on the other side we
- 4:59:31need a vertical bar
- 4:59:33too I'm going to shift these dashes over
- 4:59:35by one same thing with the vertical bar
- 4:59:39we need a another set of vertical
- 4:59:41bars another
- 4:59:43set and then we can copy our dashes
- 4:59:47again so we should have a shape like
- 4:59:50this right in the middle we're going to
- 4:59:52add a bullet point the easiest way to do
- 4:59:54this is to pull up the charm map
- 4:59:57application so if you hold windows and
- 5:00:00type R you can look up charm map hit
- 5:00:05enter if you scroll all the way down to
- 5:00:07the bottom and up a little bit
- 5:00:13it there should be a bullet
- 5:00:16point let's select it copy it close
- 5:00:20character
- 5:00:21map and paste that bullet point or
- 5:00:24otherwise you can find it from someplace
- 5:00:26else online let's print it just
- 5:00:29once I'm just going to print dice
- 5:00:33one let's do a test run I'm just going
- 5:00:36to roll one dice and this isn't going to
- 5:00:38be accurate right away
- 5:00:40but we do have an asky art of a
- 5:00:42one-sided
- 5:00:46dice then we need dice two really we can
- 5:00:50copy everything for dice one paste it
- 5:00:53change dice one to dice two and move
- 5:00:56this bullet point
- 5:00:58around so this is Dice two then we need
- 5:01:02dice
- 5:01:03three dice
- 5:01:05three there will be a bullet right in
- 5:01:08the middle
- 5:01:11dice
- 5:01:15four we need the bullets in each
- 5:01:19corner and none in the center that's
- 5:01:21dice
- 5:01:23four dice
- 5:01:27five we'll have a bullet point right in
- 5:01:30the middle along with the corners and
- 5:01:32dice
- 5:01:38six we have two rows of bullet points
- 5:01:42and those are the dice that we
- 5:01:44need now we'll create an enhanced
- 5:01:49switch what are we
- 5:01:52examining so going back to our
- 5:01:54parameters right here we're examining
- 5:01:56the role that we pass in it's going to
- 5:01:59be a number 1 through six we'll examine
- 5:02:01our role against matching
- 5:02:03cases if roll matches a case of one
- 5:02:07we'll do this
- 5:02:10we will print rather than print
- 5:02:13line dice
- 5:02:17one dice one is this image the
- 5:02:24string if roll matches a case of two we
- 5:02:28will print dice
- 5:02:30two if Case is three we will print dice
- 5:02:34three four if Case is four print dice
- 5:02:39four
- 5:02:42five print dice five and
- 5:02:46six print dice
- 5:02:49six and a default case we might as well
- 5:02:52add
- 5:02:54one if there are no matches we'll print
- 5:02:56the
- 5:02:58following we'll say invalid
- 5:03:02roll all right and that's it let's run
- 5:03:06this enter the number of dice to roll
- 5:03:09I'll roll three
- 5:03:11Dice and here's what we got I rolled a
- 5:03:14six a two and a
- 5:03:17six all right everybody so that is a
- 5:03:20dice rolling program that you can make
- 5:03:22using
- 5:03:25Java what is going on everybody in this
- 5:03:28video I'm going to discuss arrays in
- 5:03:30Java an array is a collection of values
- 5:03:33of the same data type as a beginner it
- 5:03:35might be helpful to think of an array as
- 5:03:38a variable that can store more than one
- 5:03:40value it's a little more complicated
- 5:03:42than that but that might be a good way
- 5:03:43to think of it let me give you a
- 5:03:46demonstration suppose we have a string
- 5:03:48variable a fruit pick a fruit I will
- 5:03:52pick an
- 5:03:53apple and then you know we can print our
- 5:03:55variable of
- 5:03:57fruit which would give me an
- 5:04:00Apple well what if I told you that this
- 5:04:03variable can store more than one value
- 5:04:05if we were to turn it into an array
- 5:04:08here's how after the data type of our
- 5:04:10variable add a set of straight brackets
- 5:04:13enclose any values within a set of curly
- 5:04:16braces and there we go we now have an
- 5:04:19array we have an array of
- 5:04:22strings this array can store more than
- 5:04:24one value but they all have to be the
- 5:04:26same data type in this case strings
- 5:04:30let's add some more fruit let's add an
- 5:04:34orange a
- 5:04:36banana and a coconut
- 5:04:40the name of your array should be
- 5:04:42descriptive of what it contains let's
- 5:04:44say fruits instead of
- 5:04:46fruit technically in the English
- 5:04:48language fruit would still be plural but
- 5:04:51by adding an S it's more descriptive
- 5:04:53that it contains more than one value I
- 5:04:55know English is a weird language at
- 5:04:57times if I was to print my array of
- 5:05:00fruits here's the
- 5:05:03result while we get a memory address
- 5:05:06arrays fall under the category of a
- 5:05:08reference data type if we access the
- 5:05:11name of our array we're given a memory
- 5:05:13address each value within an array is
- 5:05:16known as an element to access an element
- 5:05:19we have to indicate an index number
- 5:05:22after our array name let's add a set of
- 5:05:24straight brackets and then an index
- 5:05:27number as is the case with many things
- 5:05:29in programming toxis the first of
- 5:05:32something it usually has an index of
- 5:05:34zero that applies to arrays as well
- 5:05:37print our array of fruits at index of
- 5:05:40zero that's going to give you the first
- 5:05:42element of Apple for the next element
- 5:05:45that would be an index of
- 5:05:47one which is
- 5:05:50orange
- 5:05:52two would be
- 5:05:54banana
- 5:05:56three coconut if we attempt to access an
- 5:05:59element that doesn't exist here's what
- 5:06:03happens we get an exception an exception
- 5:06:06is similar to an error we have an array
- 5:06:09in index out of bounds
- 5:06:11exception this error message appears
- 5:06:13when you try and access an element that
- 5:06:15doesn't exist within an array there is
- 5:06:18no index of four there's only 0 1 2 3 in
- 5:06:22this case now you do have the capability
- 5:06:26to change the value at a given
- 5:06:28index to do so we will take the name of
- 5:06:30our array in this case fruits specify an
- 5:06:34index using the set of straight brackets
- 5:06:37at index of zero let's set that equal to
- 5:06:40a string of pineapple rather than
- 5:06:43Apple and then we'll print it print our
- 5:06:46array of fruits at index of
- 5:06:49zero that would give us a pineapple
- 5:06:52rather than an apple by specifying the
- 5:06:55index you can change any element so
- 5:06:58fruits at index of one is now a
- 5:07:03pineapple if at any time you need the
- 5:07:05length of an array there's a built-in
- 5:07:07property for that let's say say we have
- 5:07:09an integer variable of
- 5:07:12num of fruits meaning number of
- 5:07:16fruits to access the length of an array
- 5:07:19take our array in this case
- 5:07:22fruits use dot which is the axis
- 5:07:25modifier then type
- 5:07:28length give me the length of my array of
- 5:07:32fruits how many elements are within this
- 5:07:34array take a
- 5:07:37guess four
- 5:07:40there's four elements within this array
- 5:07:421 2 3 4 if you ever need the length of
- 5:07:46an array use the length property of your
- 5:07:48array this will return an integer which
- 5:07:51you can assign to a variable if you
- 5:07:53would like if you need to print all of
- 5:07:55the elements of an array you could use a
- 5:07:57for Loop here's
- 5:08:01how well declare for
- 5:08:04Loop we'll create an index of I equal to
- 5:08:07zero
- 5:08:09how many times do we want to iterate
- 5:08:11this Loop let's continue as long as I is
- 5:08:15less than we need to determine the
- 5:08:17length of this array well we can use
- 5:08:19that length property that we discussed
- 5:08:21take our array of fruits access the
- 5:08:24length property this should return the
- 5:08:27number four continue as long as I is
- 5:08:30less than four then we will increment
- 5:08:32our index of I so our for Loop in this
- 5:08:35case is going to iterate four times
- 5:08:39let's perform a test run if I was to
- 5:08:41print my array of fruits at index of
- 5:08:44zero this is what happens
- 5:08:48exactly it's going to print fruits at
- 5:08:50index zero four times apple apple apple
- 5:08:55apple what I need is to print each
- 5:08:58element of this array we could use our
- 5:09:00index V for that so replace zero with i
- 5:09:04i is going to change during each
- 5:09:06iteration of this loop we're increasing
- 5:09:09it by one after we complete each cycle
- 5:09:11of the loop so here's what happens now
- 5:09:15we get all the elements of our array
- 5:09:18Apple orange banana
- 5:09:20coconut this index of I is going to
- 5:09:23increase after each cycle of this Loop
- 5:09:25each
- 5:09:26iteration alternatively rather than
- 5:09:29printing each fruit on a new line we
- 5:09:31could use
- 5:09:32print then after printing each element
- 5:09:35we could add a space
- 5:09:38character that would print each element
- 5:09:40and separate each with a space if you so
- 5:09:44choose there's something called an
- 5:09:46enhanced for Loop an enhanced for Loop
- 5:09:49will cycle once for each element within
- 5:09:51an
- 5:09:52array we'll start with a normal for Loop
- 5:09:55to create an enhanced for Loop within
- 5:09:57the set of parentheses we will list the
- 5:10:00data type of each element of the array
- 5:10:02in this case
- 5:10:05strings now we need a unique identifier
- 5:10:07for each element within the Sur array
- 5:10:10since we're working with an array of
- 5:10:12fruits let's say fruit
- 5:10:14singular then we need a colon then our
- 5:10:17array that we're iterating through in
- 5:10:20this case
- 5:10:22fruits basically we're saying for every
- 5:10:25fruit in my array of fruits do
- 5:10:29this let's print each
- 5:10:32fruit fruit will be the current element
- 5:10:34we're cycling
- 5:10:36through here's what happens we print
- 5:10:39each fruit we have all the elements of
- 5:10:42our array of fruits Apple orange banana
- 5:10:46coconut this is an enhanced for Loop
- 5:10:49also known as a 4 each Loop it
- 5:10:51simplifies iterating through a
- 5:10:53collection such as an
- 5:10:55array if you need to sort your array
- 5:10:58there's a built-in sort method we're
- 5:11:00going to access the class of arrays you
- 5:11:03may need to import it import class
- 5:11:07arrays so make sure you have this import
- 5:11:11import
- 5:11:12java.util.arrays
- 5:11:14this class provides a lot of
- 5:11:16functionality when working with arrays
- 5:11:19there's a built-in sort
- 5:11:21method we're going to pass in our array
- 5:11:24of fruits to this method we'll end up
- 5:11:26sorting the elements in this array
- 5:11:32alphabetically originally the order of
- 5:11:34our elements was Apple orange banana
- 5:11:37coconut but after sorting there they're
- 5:11:39now apple banana coconut orange they're
- 5:11:42all arranged alphabetically or if these
- 5:11:45were numbers they would be arranged in
- 5:11:47ascending
- 5:11:49order there's also a fill method with
- 5:11:51arrays we can fill all the elements of
- 5:11:53an array with a given value again we're
- 5:11:56going to access the arrays class called
- 5:11:59The Fill
- 5:12:00method pass in our array comma the value
- 5:12:04we're going to fill the array with let's
- 5:12:07fill our array with pineapples
- 5:12:11then we'll use a for each Loop to print
- 5:12:13each
- 5:12:14element and here's our new array we have
- 5:12:17filled our array of fruits with the
- 5:12:20value of
- 5:12:21pineapple all right everybody that's an
- 5:12:24introduction to arrays think of an array
- 5:12:26as a variable but it can store more than
- 5:12:28one value and well everybody those are
- 5:12:31arrays in
- 5:12:34Java what is going on everybody in this
- 5:12:36video I'm going to show you how we can
- 5:12:38enter in user input into an array so
- 5:12:40let's get started before assigning
- 5:12:42values into an array our compiler needs
- 5:12:44to know the size of the array beforehand
- 5:12:47we have to first allocate space for the
- 5:12:49array let's begin by creating an array
- 5:12:51of strings we'll create an array of
- 5:12:53strings we'll enter in some food I will
- 5:12:56name this array Foods equals add a set
- 5:12:59of krly braces and then within this
- 5:13:01array you can enter in some values pick
- 5:13:03some food that you like I'll enter in a
- 5:13:06pizza
- 5:13:07taco and and
- 5:13:12hamburger the size of this array is
- 5:13:15three
- 5:13:16elements once you set a size for an
- 5:13:18array normally you can't change it this
- 5:13:20array will have three elements however
- 5:13:23all the elements are already filled with
- 5:13:25values to create an empty array you
- 5:13:27don't want a set of empty curly braces
- 5:13:30then we're creating an array of no
- 5:13:31elements just to demonstrate that I'm
- 5:13:34going to print the length of our array
- 5:13:36Foods foods. length
- 5:13:41this is going to give us
- 5:13:43zero and we can't assign any values even
- 5:13:46if I attempt to let's say Foods at index
- 5:13:50zero equals a string of pizza here's
- 5:13:55what
- 5:13:57happens well we have an exception and
- 5:14:00array index out of bounds exception
- 5:14:03before assigning values we'll want to be
- 5:14:05sure that we're setting a size of the
- 5:14:07array even if it is empty instead of
- 5:14:10assigning a set of values we'll create
- 5:14:12an empty array we'll set our array equal
- 5:14:15to use the new keyword type the data
- 5:14:18type of our array straight brackets and
- 5:14:22within the straight brackets we'll set a
- 5:14:24size a number so let's say three we're
- 5:14:28creating an empty array that has space
- 5:14:30for three elements three values let's
- 5:14:33try this
- 5:14:35again the length of our array is three
- 5:14:39and we can assign the values so let's
- 5:14:41say Foods at index
- 5:14:43one equals a
- 5:14:46taco Foods at index 2 will be a
- 5:14:55hamburger I'll print all the foods in
- 5:14:57this array we can use an enhanced for
- 5:15:00Loop list the data type we're working
- 5:15:02with an array of strings for every food
- 5:15:05in our array of
- 5:15:07foods we will print each
- 5:15:11food that would give us pizza Taco
- 5:15:16hamburger we have declared and
- 5:15:18instantiated an empty array and filled
- 5:15:21in the values
- 5:15:22later let's modify this program so a
- 5:15:25user can type in some values let's
- 5:15:27delete these lines if we're accepting
- 5:15:29user input we will need a scanner we'll
- 5:15:32declare that and instantiate it scanner
- 5:15:34scanner equals new
- 5:15:36scanner within the set of parentheses
- 5:15:39typ
- 5:15:40system.in import the class for
- 5:15:42scanner import java.util.scanner and
- 5:15:46close the scanner when you're done
- 5:15:48because I sometimes forget to do that
- 5:15:50still scanner.
- 5:15:52close so we have an empty array of three
- 5:15:56elements we'll create a for Loop to
- 5:15:59iterate over the length of our array of
- 5:16:02foods so four we'll create a counter of
- 5:16:06in I equals z will continue as long as I
- 5:16:10is less
- 5:16:11than our array of foods length property
- 5:16:15which returns three because there's
- 5:16:17three elements then increment I by One
- 5:16:21during each iteration of this Loop let's
- 5:16:23do the
- 5:16:24following we'll output the following
- 5:16:27prompt enter a
- 5:16:30food I'll use print instead of print
- 5:16:32line because I prefer
- 5:16:34that we'll assign our array of foods at
- 5:16:38index of I I changes during each cycle
- 5:16:42at first it's zero then 1 then two then
- 5:16:45three we'll set that equal to use our
- 5:16:49scanner to accept some user input call
- 5:16:51the next line
- 5:16:54method okay let's perform a test run
- 5:16:57currently we have an array with three
- 5:16:59empty elements we'll cycle this loop
- 5:17:02three times because the length property
- 5:17:04of the array is three then print all the
- 5:17:07elements when we're done
- 5:17:09here's what happens exactly enter a food
- 5:17:13pizza Taco
- 5:17:16hamburger here are the elements within
- 5:17:18our array of food pizza Taco
- 5:17:22hamburger let's say that we change the
- 5:17:25size of the array let's say for this
- 5:17:29time Pizza Taco
- 5:17:32hamburger hot
- 5:17:36dog Pizza Taco hamburger hot dog
- 5:17:40we're going to make a few changes
- 5:17:41instead of setting a static size for the
- 5:17:43array we'll have a user enter in the
- 5:17:46number of food they would like to enter
- 5:17:48we'll replace this number with a
- 5:17:49variable to determine the size what
- 5:17:52we'll do now is we will declare our
- 5:17:55string array of foods but instantiate it
- 5:17:58later once we know the
- 5:18:00size we'll
- 5:18:02output what number of food do you want
- 5:18:10I'll use print instead of print
- 5:18:12line I'll also create an integer
- 5:18:14variable of size I'll declare it but not
- 5:18:17yet fill it in until after our
- 5:18:20prompt let's set the size the size of
- 5:18:23the array that is equal to use our
- 5:18:26scanner and call the next int method we
- 5:18:31are accepting an
- 5:18:32integer once we know the size of the
- 5:18:34array we can finish instantiating it
- 5:18:38let's take our array of foods that we've
- 5:18:40declared but have not yet assigned and
- 5:18:43we'll assign it our array is made up of
- 5:18:45strings we'll use the new keyword new
- 5:18:49list the data type
- 5:18:51string it's an array so we need a set of
- 5:18:53straight
- 5:18:54brackets and instead of a number we're
- 5:18:57going to use our variable the user is
- 5:18:59going to determine what this number is
- 5:19:00going to
- 5:19:02be there's one final step this is
- 5:19:04specific for scanners since we're
- 5:19:06accepting an integer and then we're
- 5:19:08accepting a string for our user input we
- 5:19:11have to clear the input buffer next
- 5:19:13integer isn't going to pick up the new
- 5:19:15line character at the end so just to
- 5:19:18demonstrate
- 5:19:19that let's say we want one food
- 5:19:23item there's still that new line
- 5:19:25character within the input buffer we
- 5:19:26didn't get the opportunity to enter in a
- 5:19:28food we are just going to clear our
- 5:19:30scanner by using scanner. next line and
- 5:19:34that will clear it
- 5:19:36up all right let's try this again
- 5:19:40what number of food do you want I want
- 5:19:43five five food items enter in some food
- 5:19:47pizza
- 5:19:49Taco
- 5:19:51hamburger hot dog and
- 5:19:54Sushi and here's our food pizza Taco
- 5:19:58hamburger hot dog
- 5:20:00sushi in summary to enter values into an
- 5:20:03array you'll likely want to create an
- 5:20:05empty array but you need to know the
- 5:20:07size of the array first
- 5:20:09if the size of the array is
- 5:20:10predetermined you can always set that to
- 5:20:11be a number otherwise if one way or
- 5:20:14another if a user is going to determine
- 5:20:16the size of the array you'll have to get
- 5:20:18that from the user and you can set the
- 5:20:20size of the array to whatever the user
- 5:20:21types in just be sure to do that before
- 5:20:24filling in the values if you would like
- 5:20:26a copy of this code you can look in the
- 5:20:27comment section down below be sure that
- 5:20:29you're looking in the playlist and well
- 5:20:31everybody that is how to enter and user
- 5:20:33data into an array using
- 5:20:37Java what is is going on everybody in
- 5:20:39this video I'm going to show you how we
- 5:20:41can search through the elements of an
- 5:20:42array let's begin we'll need an array to
- 5:20:45work with to keep the simple let's
- 5:20:47create an array of integers we'll create
- 5:20:49an array of integers named numbers
- 5:20:52equals make up some numbers to put
- 5:20:54within this array I'll just type
- 5:20:56whatever comes to
- 5:21:01mind good enough we'll also need a
- 5:21:03Target what number are we searching for
- 5:21:06let's say I'm searching for the number
- 5:21:08number two I will create an integer
- 5:21:11variable of Target set it equal to
- 5:21:15two in data structures and algorithms
- 5:21:18we'll be performing what is known as a
- 5:21:19linear search we're going to use a for
- 5:21:22Loop to iterate through these elements
- 5:21:24and then see if there's a match we'll
- 5:21:27create a for Loop create an index of I I
- 5:21:31will equal zero we'll continue as long
- 5:21:33as I is less than R array of numbers
- 5:21:37access the length property that will
- 5:21:40return the length as a number then
- 5:21:42increment I by
- 5:21:44One I will check using an if
- 5:21:47statement if our
- 5:21:50Target is
- 5:21:52equal to our array at the current index
- 5:21:56of I at first I will be zero and then we
- 5:22:00increment it during each cycle of this
- 5:22:03Loop if our Target equals the current
- 5:22:06element let's print the following
- 5:22:10let's say element found at
- 5:22:15index plus I that's our
- 5:22:19index in case we find the target for
- 5:22:21example two we don't need to search
- 5:22:24through the rest of the array we could
- 5:22:26add what is known as a break statement
- 5:22:28to break out of the
- 5:22:29loop if we find the target break out of
- 5:22:32the loop let's perform a test run we're
- 5:22:36searching for the number two
- 5:22:38and we'll be given the index element
- 5:22:41found at index
- 5:22:432 let's look for
- 5:22:46one one is found at index
- 5:22:50zero four which is near the end is found
- 5:22:54at index 6 0 1 2 3 4 5 6 what if we
- 5:23:01don't find our Target I don't have the
- 5:23:03number seven in
- 5:23:06here well there's no output
- 5:23:10since this if statement is never true
- 5:23:12once we reach the end of the array we
- 5:23:15don't do anything so let's add a Boolean
- 5:23:19variable of is
- 5:23:22found and set it to be
- 5:23:25false we'll check if is found is false
- 5:23:29if that is the case we'll output a
- 5:23:31message that element wasn't found in the
- 5:23:33array so if we do find our Target we'll
- 5:23:37take our Boolean variable of is found
- 5:23:40and set it to be true we have found our
- 5:23:43Target that we're looking
- 5:23:46for if we exit the for Loop and never
- 5:23:48find our Target we can check
- 5:23:52that we'll say is
- 5:23:55found but we'll use the not logical
- 5:23:58operator the not logical operator gives
- 5:24:02you the inverse of a Boolean value or
- 5:24:05variable if is found is false if
- 5:24:08something isn't true if we don't find
- 5:24:10the target then we execute this if
- 5:24:13statement where we will print the
- 5:24:15following let's say something such as
- 5:24:19element not found in the
- 5:24:24array all right we are looking for the
- 5:24:26number seven which does not exist within
- 5:24:29this
- 5:24:30array that would give us this message
- 5:24:33element not found in the array because
- 5:24:36is found was false we could never find
- 5:24:39it within this for
- 5:24:41Loop so we're checking if is found is
- 5:24:44not
- 5:24:46true what if we have an array of strings
- 5:24:49and we're searching for an element well
- 5:24:51instead of using the comparison operator
- 5:24:53we have to use the equals method of
- 5:24:55strings because strings are a reference
- 5:24:57data type so this time let's say we have
- 5:24:59an array of
- 5:25:01strings named fruits think of some
- 5:25:05fruit I will pick an apple
- 5:25:09an
- 5:25:11orange and a
- 5:25:13banana the target this time will be a
- 5:25:19string let's say I'm searching for an
- 5:25:24orange will iterate through our array of
- 5:25:27fruits the condition for the for Loop
- 5:25:30will be I is less than the length of our
- 5:25:32array of fruits now we can't necessarily
- 5:25:36say is our Target equal to
- 5:25:39fruits at index of
- 5:25:41I we're comparing two strings and since
- 5:25:44strings are reference data types we
- 5:25:46would end up comparing the memory
- 5:25:48addresses of two different strings we
- 5:25:50would be saying are these two memory
- 5:25:52addresses the same are they at the same
- 5:25:55location which isn't the case if we're
- 5:25:57comparing the value of strings we'll use
- 5:26:00the equals
- 5:26:01method let's take fruits at index of I
- 5:26:05call the built-in equals method of
- 5:26:07strings and pass in our Target as an
- 5:26:11argument now this should work we are
- 5:26:14searching for an
- 5:26:17orange element found at index one what
- 5:26:21about a
- 5:26:26pineapple element not found in the array
- 5:26:30there was no pineapple let's modify this
- 5:26:33program slightly we'll accept some user
- 5:26:35input but we'll need a scanner
- 5:26:44import the
- 5:26:46class java.util.scanner and close your
- 5:26:49scanner when you're done with it
- 5:26:51scanner.
- 5:26:53close instead of assigning our Target
- 5:26:56we'll accept user input we'll use our
- 5:26:58scanner use the next line
- 5:27:02method and we will also need a
- 5:27:06prompt let's print the following
- 5:27:09enter a fruit to search
- 5:27:16for just to make this a little more
- 5:27:18clean I'm going to move this variable of
- 5:27:20is found to the top
- 5:27:23here I'll declare our string of Target
- 5:27:26but not assign it and then after the
- 5:27:28prompt I'll assign our name of Target
- 5:27:31this is just how I like to arrange
- 5:27:33things but it all works the same now we
- 5:27:36can search for an element after acting
- 5:27:38user input and I will convert this to be
- 5:27:40a print statement instead of print line
- 5:27:43all right let's search for some fruit is
- 5:27:46there a coconut element not found in the
- 5:27:52array perhaps a banana element found at
- 5:27:56index 2 0 1 2 all right everybody so
- 5:28:01that's how to search through an array
- 5:28:03using
- 5:28:06Java hello everybody in this video I'm
- 5:28:08going to explain variable arguments also
- 5:28:11known as VAR args in Java variable
- 5:28:14arguments allow a method to accept a
- 5:28:17varying number of arguments they make
- 5:28:20methods more flexible and there's no
- 5:28:22need for overloaded methods overloaded
- 5:28:25methods are methods that share the same
- 5:28:27name but have different
- 5:28:29parameters in this example I have
- 5:28:31several add methods they each have
- 5:28:33different amounts of parameters if I was
- 5:28:36to add a few numbers together for
- 5:28:38example I'll add 1 + 2 + 3 well then I
- 5:28:43would use this method what if I told you
- 5:28:45instead of creating several different
- 5:28:46methods that basically do the same thing
- 5:28:49we could instead create one method that
- 5:28:51accepts any number of
- 5:28:53arguments we'll only need to declare one
- 5:28:57method we will create one add method
- 5:29:00that accepts a varying amount of
- 5:29:02arguments so let's create a method the
- 5:29:05return type let's say is an integer
- 5:29:08we'll create a method of add our method
- 5:29:11will have one parameter what is the data
- 5:29:14type of the values we're sending this
- 5:29:15method in this case
- 5:29:18integers we're going to follow the data
- 5:29:20type with three dots and
- 5:29:23ellipses how this works is that the Java
- 5:29:26compiler is going to pack the arguments
- 5:29:28we send it into an array we'll need a
- 5:29:31name for this array let's say
- 5:29:35numbers just to demonstrate this
- 5:29:38temporarily I'm going to print my array
- 5:29:40of
- 5:29:43numbers when we call the add method we
- 5:29:46can send any number of arguments for
- 5:29:48example 1 2 3 4 and they're going to be
- 5:29:52packed into an
- 5:29:53array within this method we're now
- 5:29:56working with an array let's perform a
- 5:29:58test
- 5:29:59run let's return void
- 5:30:03actually so we're given a memory address
- 5:30:06of this array now let's actually add
- 5:30:09these numbers
- 5:30:10together we need to cycle through the
- 5:30:12elements of this array we can use a for
- 5:30:15Loop or an enhanced for Loop for that so
- 5:30:17let's return an integer in this
- 5:30:20case let's declare a local variable of
- 5:30:24sum I will set that to be
- 5:30:29zero let's create an enhanced for
- 5:30:32Loop the data type will be int for every
- 5:30:35number in my array of numbers
- 5:30:39let's take our sum and add the current
- 5:30:43number sum plus equals
- 5:30:45number then at the end we will return
- 5:30:48our
- 5:30:50sum and there we go and then we need to
- 5:30:53print the output I'm going to cut this
- 5:30:56and paste it within a print
- 5:30:59statement let's perform a test run the
- 5:31:02sum when we add these four numbers
- 5:31:04together the sum is 10 and we can pass
- 5:31:07in any number of
- 5:31:11arguments the result is now
- 5:31:1421 basically the Java compiler is
- 5:31:16packing all of these arguments into an
- 5:31:19array when we send them to a method then
- 5:31:22within the context of this method we
- 5:31:24would just need to work with this array
- 5:31:26one way or another but it's a lot easier
- 5:31:28than having several different overloaded
- 5:31:30methods okay this time let's create a
- 5:31:32new method we'll create a method of
- 5:31:35average average will find the average
- 5:31:37value of whatever we send it so we will
- 5:31:40create a static method that's going to
- 5:31:42return a double this time the method
- 5:31:45name will be
- 5:31:47average what is the data type of what
- 5:31:50we're sending let's say double then we
- 5:31:52need the ellipses the three dots then a
- 5:31:56unique name for this array let's say
- 5:31:59numbers so to find the average we can
- 5:32:01perform the following
- 5:32:03code let's say we have
- 5:32:06double sum
- 5:32:09I will set that equal to
- 5:32:11zero we will create an enhanced for Loop
- 5:32:15the data type is double for every number
- 5:32:19in my array of numbers do the following
- 5:32:22we will take our
- 5:32:24sum and add the current number sum plus
- 5:32:27equals
- 5:32:29number then at the end we will return
- 5:32:33sum divided by now since we're working
- 5:32:36with an array there is a built-in length
- 5:32:39property numbers. length to find the
- 5:32:43average you take all the numbers add
- 5:32:45them together then divide it by the
- 5:32:47amount of numbers that you
- 5:32:50have let's perform a test
- 5:32:52run I'm going to
- 5:32:55print call the average
- 5:32:57method let's find the average of a few
- 5:33:00numbers I don't know what that's going
- 5:33:02to be so the average of these four
- 5:33:05numbers is 2.5
- 5:33:09I can pass in any number of
- 5:33:12arguments so the average of these
- 5:33:14numbers is four
- 5:33:184.0 you can even pass in no arguments if
- 5:33:21you so
- 5:33:23choose but with this method that we have
- 5:33:25created here's what
- 5:33:27happens we're actually dividing by zero
- 5:33:31because the length of our array is zero
- 5:33:33there's no elements that we're passing
- 5:33:35in in this specific example let's create
- 5:33:38an if
- 5:33:39statement if our array of
- 5:33:42numbers if the length of it we can
- 5:33:45access the length property is equal to
- 5:33:48zero let's return zero instead of
- 5:33:52receiving noted number we're calling the
- 5:33:55average method but passing in no
- 5:33:57arguments this
- 5:33:59time that would give us zero because the
- 5:34:02length of our array is zero because
- 5:34:05we're passing in no arguments I already
- 5:34:07everybody so those are variable
- 5:34:09arguments they allow a method to accept
- 5:34:12a varying number of arguments it makes
- 5:34:14methods more flexible and there's no
- 5:34:16need for overloaded methods the Java
- 5:34:19compiler will pack the arguments into an
- 5:34:21array when setting up the parameters of
- 5:34:23a method declare the data type and add a
- 5:34:26suffix of an ellipsis Three Dots and
- 5:34:30well everybody those are variable
- 5:34:32arguments in
- 5:34:35Java hey there everybody so today I'm
- 5:34:38going to explain 2D arrays in Java also
- 5:34:41known as multi-dimensional arrays a
- 5:34:43two-dimensional array is an array where
- 5:34:46each element is an array it's useful for
- 5:34:49storing a matrix of data let me give you
- 5:34:52an example let's say we have some basic
- 5:34:54one-dimensional arrays I will create a
- 5:34:57string array of
- 5:34:59fruits think of some
- 5:35:02fruit I'll add an
- 5:35:04apple an orange and a
- 5:35:08banana then I'll create another separate
- 5:35:11array let's create a string array of
- 5:35:15vegetables think of some
- 5:35:17vegetables I will add a
- 5:35:20potato an
- 5:35:22onion and a
- 5:35:24carrot let's create one more I will
- 5:35:27create a string array of
- 5:35:31meats I will add
- 5:35:35chicken pork
- 5:35:39beef let's add one more and
- 5:35:44fish these three arrays are all separate
- 5:35:47one-dimensional arrays well what if I
- 5:35:50told you we can make an array made of
- 5:35:52arrays where each element is an array
- 5:35:55here's how we're going to combine all
- 5:35:58these arrays together into a grid or
- 5:36:01Matrix I will create a two-dimensional
- 5:36:04array of strings we will need two sets
- 5:36:07of straight brackets I will name this
- 5:36:09two-dimensional array
- 5:36:15groceries and then we need our curly
- 5:36:17braces this two-dimensional array each
- 5:36:20element within this two-dimensional
- 5:36:21array is going to be an array we have
- 5:36:26fruits
- 5:36:27vegetables and
- 5:36:31meats let's attempt to display these
- 5:36:33elements you can use a for Loop or an
- 5:36:36enhanced for Loop
- 5:36:38within this for Loop what is the data
- 5:36:40type of each element well each element
- 5:36:43is a string
- 5:36:45array so we need to list the data type
- 5:36:47as a string array let's give a nickname
- 5:36:50of each element as
- 5:36:52Foods in our two-dimensional array of
- 5:36:57groceries let me show you what happens
- 5:36:59if I print each element within our
- 5:37:00two-dimensional array of
- 5:37:02groceries I will print each array of
- 5:37:05foods here's what we got
- 5:37:08well we get a bunch of memory addresses
- 5:37:10because arrays are a reference data type
- 5:37:13we're referring to some place in
- 5:37:15memory we'll need to use a nested
- 5:37:21Loop an enhanced for Loop would be good
- 5:37:23for this with each of these arrays what
- 5:37:26is the data type of each element within
- 5:37:28each well we're working with strings so
- 5:37:31the data type is going to be string
- 5:37:34let's give a nickname of each element as
- 5:37:36food
- 5:37:37in our array of
- 5:37:40foods during each cycle of this Loop
- 5:37:43let's print not print line each food and
- 5:37:47then I'll add a
- 5:37:50space here's what we got
- 5:37:54currently we have our first array and
- 5:37:58our second then
- 5:38:00third after finishing each array I'm
- 5:38:03going to add a new line
- 5:38:04character we can use a print line
- 5:38:06statement for that
- 5:38:09here's what we got now what we have is a
- 5:38:13grid or Matrix of data you can say that
- 5:38:16there's
- 5:38:18columns and
- 5:38:22rows each row is a separate
- 5:38:27array now when initializing an array you
- 5:38:30don't necessarily needed names let's
- 5:38:33remove
- 5:38:34fruits copy the elements of this array
- 5:38:37including the curly braces and paste
- 5:38:39them within this two-dimensional array
- 5:38:41just for readability I'm going to place
- 5:38:43each row on a new
- 5:38:46line let's replace vegetables with the
- 5:38:50elements including the curly
- 5:38:53braces same thing applies with
- 5:38:57meats and we can delete all
- 5:39:00this this would work the
- 5:39:03same groceries is a matrix of data a gri
- 5:39:08let's print
- 5:39:10this and that works the
- 5:39:13same if you ever need to access or
- 5:39:15change an element you'll need to use two
- 5:39:17indices for example let's replace apple
- 5:39:21with the pineapple we will take our
- 5:39:24two-dimensional array of groceries and
- 5:39:26list two
- 5:39:28indices the first index applies to the
- 5:39:30row the second index applies to the
- 5:39:33column so we're saying at row 0 column
- 5:39:380 we can change it or access it let's
- 5:39:42say that row 0 column 0 is now a
- 5:39:45pineapple rather than an apple then we
- 5:39:48have pineapple orange
- 5:39:51banana let's replace our potato with
- 5:39:53something
- 5:39:54else that would be Row one column 0 Row
- 5:39:59one column 0 is now
- 5:40:05celery celery onion
- 5:40:10carrot let's replace the carrot with
- 5:40:12celery then that would be Row one column
- 5:40:162 0 1 2 Row one column
- 5:40:222 potato onion
- 5:40:26celery let's replace pork with eggs that
- 5:40:29would be row two 0 1 2 column 1 0 1
- 5:40:37row two column one is now
- 5:40:42eggs we have chicken eggs beef
- 5:40:47fish to access or modify an element
- 5:40:49within a 2d array you need to list two
- 5:40:52indices the first for the row the second
- 5:40:54for the
- 5:40:55column okay now we're going to create a
- 5:40:57mini
- 5:40:59project we'll create a two-dimensional
- 5:41:01array that resembles a telephone number
- 5:41:03pad this two-dimensional array will be
- 5:41:06made up of characters
- 5:41:07the data type is Char and then we have
- 5:41:10two sets of straight brackets let's name
- 5:41:12this two-dimensional array telephone
- 5:41:15equals we have an array of arrays we'll
- 5:41:19have four
- 5:41:24rows and just for readability I'm going
- 5:41:26to place each of these on a new
- 5:41:28line just so it resembles a grid or
- 5:41:33Matrix for the first row we'll have one
- 5:41:39two three and these are all characters
- 5:41:42not strings we're using single quotes
- 5:41:44not double quotes for the second row
- 5:41:47we'll have
- 5:41:48four
- 5:41:505
- 5:41:526 the third row will be
- 5:41:567
- 5:41:588
- 5:42:009 now for the last row we'll have an
- 5:42:03asterisk
- 5:42:05character Zero
- 5:42:09and pound or hashtag as I like to call
- 5:42:13it so here's our Matrix our grid of
- 5:42:17characters we have an array of arrays it
- 5:42:20has rows and
- 5:42:24columns now let's display our data we'll
- 5:42:27need nested Loops for
- 5:42:31this our outer for Loop is going to be
- 5:42:34in charge of the rows what is the data
- 5:42:36type of each element within this
- 5:42:38two-dimensional array well each row is
- 5:42:41an array of characters that's going to
- 5:42:43be the data type of the outer for Loop
- 5:42:46we have an array of characters let's
- 5:42:48give a nickname of each inner array as a
- 5:42:51row in our two-dimensional array of
- 5:42:55telephone for every Row in our telephone
- 5:42:59do this then we'll need a nested
- 5:43:03Loop what is the data type of each
- 5:43:05element within each array their
- 5:43:07characters the data type will be
- 5:43:10Char let's say for every number
- 5:43:15in each array of row we'll print the
- 5:43:19following we'll use print rather than
- 5:43:21print
- 5:43:23line we'll print each number and I'll
- 5:43:26add a space
- 5:43:29character let's do a test
- 5:43:32run after each row I'm going to print a
- 5:43:35new line character
- 5:43:37so when we escape the inner four Loop
- 5:43:40I'll add an empty print line
- 5:43:43statement and here's our telephone we
- 5:43:46have
- 5:43:47rows and
- 5:43:49columns it's a two-dimensional array of
- 5:43:53characters so those are two-dimensional
- 5:43:56arrays they're an array where each
- 5:43:58element is an array it's useful for
- 5:44:00storing a matrix of data we probably
- 5:44:03won't be using 2D arrays too often but
- 5:44:06you should still be familiar with them
- 5:44:08and well everybody those are two
- 5:44:09dimensional arrays in
- 5:44:13Java hello everybody in this video we're
- 5:44:15going to create an interactive quiz game
- 5:44:17using Java for this project you'll
- 5:44:19create an array of your own custom
- 5:44:21questions and a two-dimensional array of
- 5:44:23all the different options for answers
- 5:44:24that we have you'll type in a number 1
- 5:44:26through four depending on your guess
- 5:44:28after all of the questions are answered
- 5:44:31we will display a final score depending
- 5:44:33on the amount of correct guesses that
- 5:44:35you have so let's begin as a beginner it
- 5:44:38might be helpful to you to break down
- 5:44:39your project into different steps as
- 5:44:42comments we'll list all of the different
- 5:44:43steps we need to take to get this
- 5:44:45program up and running the first thing
- 5:44:47that we need is an array of questions
- 5:44:51questions will be an
- 5:44:52array we will also need
- 5:44:56options options will be a
- 5:44:58two-dimensional
- 5:45:00array we will declare our
- 5:45:03variables we'll need a welcome message
- 5:45:09we will list each question using a
- 5:45:11loop I'll just write in parenthesis that
- 5:45:14we will use a loop now within this loop
- 5:45:17we're going to do a few things we will
- 5:45:20list our
- 5:45:23options then get guess from
- 5:45:27user then check our guess to see if it's
- 5:45:32correct at the end of this program we
- 5:45:34will display find fin
- 5:45:40score so here's a rough outline of our
- 5:45:43project we'll fill this project in one
- 5:45:46step at a time let's begin with creating
- 5:45:48an array of questions we'll ask the
- 5:45:51user the data type will be a string
- 5:45:54array of
- 5:45:57questions here's where you're going to
- 5:45:59ask all of your questions in this
- 5:46:01project we'll have five
- 5:46:03questions they're all going to be very
- 5:46:06long strings
- 5:46:10for our first question you don't need to
- 5:46:12type in the same questions that I do
- 5:46:14feel free to come up with your own
- 5:46:16here's a few that I've looked up online
- 5:46:18my first question will be what is the
- 5:46:22main function of a
- 5:46:27router that'll be the first question
- 5:46:30I'll put each of these questions on a
- 5:46:31new line just for
- 5:46:33readability the second question will be
- 5:46:37which part of the
- 5:46:39computer is
- 5:46:43considered the
- 5:46:46brain then I'll go to a new
- 5:46:52line what year was Facebook
- 5:47:02launched who is known as the father of
- 5:47:08computers then for my last
- 5:47:12question what was the
- 5:47:16first programming
- 5:47:20language all right here are the
- 5:47:22questions questions is an array of
- 5:47:24strings a one-dimensional array now
- 5:47:28we'll create a two-dimensional array of
- 5:47:31options we will create a two-dimensional
- 5:47:34array of strings named options
- 5:47:39we have five questions that means we
- 5:47:42need five arrays one array for each
- 5:47:53question I'll Place each of these on a
- 5:47:55new line for
- 5:48:04readability our first array within our
- 5:48:062D array of options will hold all of the
- 5:48:09options for our first question each
- 5:48:12value within this first array is going
- 5:48:13to be strings and I'm going to zoom in a
- 5:48:16little so we can both read it what is
- 5:48:18the main function of a router here's
- 5:48:20where you're going to list all of the
- 5:48:21different possible answers but only one
- 5:48:24of them is going to be correct I'll say
- 5:48:26one. space storing
- 5:48:32files for option two that will be
- 5:48:36encrypting
- 5:48:41data option
- 5:48:44three will be
- 5:48:46for
- 5:48:48directing internet traffic this will be
- 5:48:51the correct answer number three number
- 5:48:55four will
- 5:48:57be managing
- 5:49:01passwords this will be my first array
- 5:49:04all the options for question one now for
- 5:49:07question
- 5:49:13two for question two we'll have four
- 5:49:21options honestly I'm just going to copy
- 5:49:24these and then paste them within each
- 5:49:26array just to speed things up what part
- 5:49:29of the computer is considered the
- 5:49:32brain here's the different options
- 5:49:34option one will be CPU that's going to
- 5:49:37be the correct answer the central
- 5:49:39processing unit two hard
- 5:49:43drive three
- 5:49:46Ram four
- 5:49:50GPU question three what year was
- 5:49:52Facebook
- 5:49:53launched option one will be 2000 option
- 5:49:57two 2004 and that's the correct
- 5:50:01answer
- 5:50:032006 or 2008
- 5:50:07who is known as the father of
- 5:50:09computers let's say Steve
- 5:50:14Jobs Bill
- 5:50:17Gates Allan
- 5:50:21Turing or Charles Babbage and that's the
- 5:50:23correct
- 5:50:29answer what was the first programming
- 5:50:32language either
- 5:50:34cobal C
- 5:50:38for
- 5:50:39Tran or
- 5:50:42assembly the correct answer is actually
- 5:50:44Fortran it's the first commercially
- 5:50:47available programming language one more
- 5:50:49thing we're going to create a
- 5:50:51one-dimensional array what are the
- 5:50:53correct answers we'll need some sort of
- 5:50:55key we could include this with our
- 5:50:57declare variable
- 5:50:59step we will create an integer array of
- 5:51:03answers we will list the correct answers
- 5:51:06as integers the correct answer to
- 5:51:08question one is directing internet
- 5:51:11traffic so we will list three for the
- 5:51:15next question what part of the computer
- 5:51:16is considered the brain that is the CPU
- 5:51:19option
- 5:51:20one Facebook was launched in the year
- 5:51:232004 that's
- 5:51:25two who is the father of computers that
- 5:51:28is Charles
- 5:51:31Babbage that's
- 5:51:33four and the first commercially
- 5:51:35available program ining language is for
- 5:51:37Tran that is three here are the correct
- 5:51:41answers are key there's a few more
- 5:51:43variables we'll need we will create an
- 5:51:46integer variable of score we can set
- 5:51:49that to be zero if we would like and an
- 5:51:52integer of guess guess is going to store
- 5:51:56whatever the user types in 1 through
- 5:51:58four so it's going to be an integer we
- 5:52:01will also need a scanner to accept user
- 5:52:04input scanner scanner equals new scanner
- 5:52:09it doesn't really matter where we
- 5:52:10declare it just as long as we declare it
- 5:52:12before we use it system.in and then we
- 5:52:15will import this
- 5:52:17class import java.util.scanner and then
- 5:52:21close our scanner at the end of our
- 5:52:23program when we're done with it scanner.
- 5:52:28close all right here are the variables
- 5:52:30we'll need including our options and our
- 5:52:33questions our next step is to create a
- 5:52:35welcome message
- 5:52:38let's output the
- 5:52:40following let's say welcome to the Java
- 5:52:45quiz
- 5:52:48game and then I'll add some
- 5:52:50separators a bunch of asterisks because
- 5:52:53I think it'll look cool
- 5:52:59mostly we have our welcome message and
- 5:53:01I'll just perform a test run it's a good
- 5:53:04idea to test your code as you're coding
- 5:53:05it just sure that everything is working
- 5:53:07fine and that is our welcome
- 5:53:11message now we'll Loop through all of
- 5:53:13the questions that we
- 5:53:17have we'll probably want to use a for
- 5:53:21Loop we will create an index of I in I
- 5:53:25equals z we'll continue this loop as
- 5:53:27long as I is less
- 5:53:30than the length of our array of
- 5:53:32questions questions access the length
- 5:53:36property
- 5:53:37which will return a number in this case
- 5:53:39five because there's five questions we
- 5:53:41will increment I by
- 5:53:43one as a test let's print our
- 5:53:48questions at index of I I will start at
- 5:53:52zero so we're returning the first
- 5:53:54question at index
- 5:53:56zero then during each iteration of this
- 5:53:58Loop we will display each question let's
- 5:54:01do a test
- 5:54:03run so here are the five questions
- 5:54:07we're displaying each element of our
- 5:54:09array of
- 5:54:10questions after each question we have to
- 5:54:13list all of the different
- 5:54:16options that's the next
- 5:54:18step we'll display a question then
- 5:54:22create a nested Loop to display all of
- 5:54:24the
- 5:54:26options I'm going to use an enhanced for
- 5:54:29Loop for this what is the data type of
- 5:54:33each value within each array within
- 5:54:35options
- 5:54:37well we're working with
- 5:54:38strings the inner for Loop is going to
- 5:54:40be an enhanced for Loop the data type of
- 5:54:43each value is a
- 5:54:45string string option colon meaning
- 5:54:50in rray of options at index of
- 5:54:55i options is a two-dimensional array if
- 5:54:59we retrieve a value at a given index
- 5:55:01will be given an array display every
- 5:55:04option in each inner array
- 5:55:07so to demonstrate that let's print each
- 5:55:14option so here's what we have after
- 5:55:18printing each question we will Loop
- 5:55:20through and print all of the
- 5:55:26options now before moving on to the next
- 5:55:28question we have to get some user input
- 5:55:31we don't want to display all the
- 5:55:32questions right away just one at a time
- 5:55:36so we will do this within the outer for
- 5:55:38loop after printing all of the options
- 5:55:42this is where we will get the guess from
- 5:55:43the
- 5:55:46user we will create a prompt to enter
- 5:55:50your guests and I will use print instead
- 5:55:53of print line because I like the user
- 5:55:55input on the same
- 5:55:56line we will assign our variable of
- 5:56:00guess guess equal
- 5:56:03scanner. next int the user going to type
- 5:56:06in a number 1 through
- 5:56:094 then we have to check our guess is it
- 5:56:11correct or
- 5:56:14not we can do the
- 5:56:16following we'll use an if statement if
- 5:56:19our guess is equal to our array of
- 5:56:24answers it's a one-dimensional
- 5:56:26array at index of I we're using the
- 5:56:30index of I because we're still within
- 5:56:32our for Loop during the first iteration
- 5:56:35of this Loop I will be zero so is our
- 5:56:38guess equal to answers at index zero if
- 5:56:41that is correct let's print the
- 5:56:43following let's say correct and for fun
- 5:56:47I'll add some text decoration some of
- 5:56:49those
- 5:56:51separators again it's not
- 5:56:54necessary I just think it'll look
- 5:56:58cool else if our guest doesn't equal our
- 5:57:01answer well we can check that with an
- 5:57:03lse statement if we don't have the
- 5:57:05correct answer that means we have the
- 5:57:07wrong answer and I will output
- 5:57:11wrong and I will just space this out a
- 5:57:13little
- 5:57:14bit all right let's do a test
- 5:57:17run for the first question the correct
- 5:57:20answer is three I'll type in three then
- 5:57:22we print
- 5:57:24correct which part of the computer is
- 5:57:26considered the brain I will
- 5:57:27intentionally get this wrong let's say
- 5:57:30the
- 5:57:32GPU
- 5:57:33wrong and I'll guess the others
- 5:57:36wrong
- 5:57:38wrong wrong then we have to calculate a
- 5:57:41final
- 5:57:43score okay then we have to display the
- 5:57:45final score so after we get the correct
- 5:57:48answer we will increment our score by
- 5:57:50one score
- 5:57:52Plus+ then we will display the final
- 5:57:58score your final score is I'll use
- 5:58:03string catnation to keep this simple
- 5:58:06we'll add our score variable plus the
- 5:58:10words out
- 5:58:13of plus we're going to get the length of
- 5:58:17our questions questions is an
- 5:58:20array questions. length
- 5:58:24property I'm just going to add a space
- 5:58:26right before out because I'm forgetting
- 5:58:29that let's run this one last
- 5:58:33time what is the main function of a
- 5:58:35router that that is three directing
- 5:58:36internet traffic which part of the
- 5:58:39computer is considered the brain that is
- 5:58:40the
- 5:58:42CPU what year was Facebook launched I'm
- 5:58:44going to get this one wrong
- 5:58:45intentionally I'm going to say 2000 and
- 5:58:48that is wrong who is known as the father
- 5:58:50of computers that is Charles babage
- 5:58:534 what was the first programming
- 5:58:56language well the first commercially
- 5:58:58available programming language that
- 5:59:00would be three for
- 5:59:01Tran correct your final score is four
- 5:59:05out of five
- 5:59:07all right everybody so that is an
- 5:59:08interactive quiz game that you can
- 5:59:10create using
- 5:59:13Java what is going on everybody in this
- 5:59:16video we're going to create a game of
- 5:59:17rock paper scissors for beginners so
- 5:59:20let's get started as a beginner when
- 5:59:22creating a project it could be helpful
- 5:59:24to you to break down a project into
- 5:59:26smaller steps just as comments I'm going
- 5:59:29to list the different steps that we'll
- 5:59:30need to take so let's say first we will
- 5:59:32declare our
- 5:59:34variables then then get a choice from
- 5:59:38the
- 5:59:40user get random choice for the
- 5:59:46computer check win conditions who
- 5:59:51won ask to play
- 5:59:55again then at the end we will display a
- 5:59:57goodbye
- 6:00:00message it may be helpful to you as a
- 6:00:02beginner to list the different steps
- 6:00:04that your program is going to take just
- 6:00:06for more organization all right let's
- 6:00:08work on step one we will declare our
- 6:00:12variables we'll need a scanner because
- 6:00:14we're accepting user input scanner
- 6:00:16scanner equals new
- 6:00:18scanner within the set of parentheses we
- 6:00:21will type system.
- 6:00:22in import the class for scanners import
- 6:00:26java.util.scanner and then we will close
- 6:00:29our scanner when we're done with it
- 6:00:31scanner. close because we don't want to
- 6:00:33leave any resources open we will be
- 6:00:36generating random numbers we will need
- 6:00:38to create a random object random random
- 6:00:41equals new
- 6:00:43random and then import this class as
- 6:00:46well import
- 6:00:48java.util
- 6:00:50random we're going to create an array of
- 6:00:53choices they will be strings rock paper
- 6:00:56or scissors we will create a string
- 6:00:59array named
- 6:01:01choices equals an array this will be an
- 6:01:06array of
- 6:01:06strings at the first index we'll have
- 6:01:10Rock then
- 6:01:12paper then
- 6:01:16scissors we'll store our choice within a
- 6:01:18variable this will be a string we'll
- 6:01:21name this variable player
- 6:01:25choice I will declare this variable but
- 6:01:27not yet assign it and the computer's
- 6:01:30Choice string computer choice
- 6:01:36we're going to create a while loop that
- 6:01:38will ask the user to play again when
- 6:01:40they reach the end we'll create a string
- 6:01:43variable of play again and I will set
- 6:01:48this to be
- 6:01:50yes basically while play again is yes
- 6:01:54continue the
- 6:01:55game let's work on our next step get the
- 6:01:58choice from the
- 6:02:00user we need to prompt the
- 6:02:02user we'll use a print line statement
- 6:02:05and say
- 6:02:06enter your move I'll add a set of
- 6:02:09parentheses that says rock paper
- 6:02:14scissors just so the user knows what to
- 6:02:16type in I'll use print instead of print
- 6:02:19line because I prefer user input on the
- 6:02:21same
- 6:02:22line once we have our prompt we're going
- 6:02:25to assign our player choice variable
- 6:02:27player choice equals we will use our
- 6:02:30scanner to get some user input we need
- 6:02:33to accept a string so we can use the
- 6:02:35next
- 6:02:36line
- 6:02:37method to accept a line of text in case
- 6:02:41a user types in any uppercase characters
- 6:02:43for example Rock we could follow this
- 6:02:46with the two lowercase method just to
- 6:02:50make it all lowercase whatever the user
- 6:02:52types
- 6:02:53in accept the next line of user input
- 6:02:56then make it lowercase just for
- 6:03:00consistency now we should have our
- 6:03:01player
- 6:03:03choice we need a way to determine to see
- 6:03:05if this user input is valid did the user
- 6:03:08pick rock paper or scissors or something
- 6:03:10else entirely well we could use an if
- 6:03:13statement for
- 6:03:18that we're going to be checking to see
- 6:03:20if our player choice doesn't equal Rock
- 6:03:24and doesn't equal paper and doesn't
- 6:03:27equal scissors so we can write the
- 6:03:29following statement we're going to use
- 6:03:31the not logical operator first not will
- 6:03:35give you the opposite the
- 6:03:38inverse if player choice use the equals
- 6:03:42method pass in a string of rock if the
- 6:03:46Player's Choice doesn't equal Rock then
- 6:03:50we'll use the and logical
- 6:03:53operator and the Player's
- 6:03:56Choice doesn't again I'm using the not
- 6:03:58logical
- 6:04:00operator
- 6:04:03equals
- 6:04:04paper again we're going to use the and
- 6:04:07logical
- 6:04:08operator and just to help with
- 6:04:11readability cuz we're going to have one
- 6:04:13really long line I'm going to place
- 6:04:15these conditions on a new line each this
- 6:04:18will help with
- 6:04:21readability and take our player
- 6:04:25choice not
- 6:04:30equals
- 6:04:33scissors we're linking three conditions
- 6:04:35together
- 6:04:36if our player choice doesn't equal Rock
- 6:04:40and our player choice doesn't equal
- 6:04:43paper and our player choice doesn't
- 6:04:47equal
- 6:04:48scissors if all of these conditions are
- 6:04:51true that means a user didn't type in
- 6:04:53something that's valid so let's do the
- 6:04:55following again I'm within my set of
- 6:04:57curly
- 6:04:59braces we'll output the
- 6:05:01following we'll say something like
- 6:05:04invalid choice
- 6:05:07all right let's perform a test
- 6:05:12run enter your move rock paper scissors
- 6:05:16let's choose Rock nothing happens that's
- 6:05:20good paper nothing
- 6:05:24happens
- 6:05:27scissors nothing happens all right I'm
- 6:05:29going to type in something that's not
- 6:05:31rock paper or scissors uh I choose the
- 6:05:34gun invalid
- 6:05:37Choice that's good that's what we
- 6:05:40want we're picking something that's not
- 6:05:42rock and is not paper and is not
- 6:05:45scissors so we execute this if statement
- 6:05:47if that's the
- 6:05:49case now for the next step we have to
- 6:05:51get a random choice for the
- 6:05:54computer we don't need user input this
- 6:05:56time we will assign our variable of
- 6:05:59computer
- 6:06:01Choice equals access our array of
- 6:06:05choices
- 6:06:07at a given
- 6:06:09index zero would be Rock because it's
- 6:06:12the first value in our
- 6:06:15array one would be paper two would be
- 6:06:18scissors well why don't we use our
- 6:06:20random object random and call the next
- 6:06:23int
- 6:06:24method then pass in
- 6:06:26three this method of a random object
- 6:06:30would return a random number between 0
- 6:06:32and two zero for rock one for p paper
- 6:06:36two for
- 6:06:37scissors then we'll output it using a
- 6:06:39print line
- 6:06:41statement let's say computer Choice col
- 6:06:45in space then add our variable of
- 6:06:48computer
- 6:06:49Choice let's do a test
- 6:06:53run I'll type in rock the computer picks
- 6:06:59paper I will pick paper the computer
- 6:07:02picks
- 6:07:04scissors I will will pick
- 6:07:07scissors the computer picks scissors as
- 6:07:09well all right we know that that
- 6:07:12works using this method of a random
- 6:07:15object we're generating a random number
- 6:07:18as the
- 6:07:19index now here's the tricky part we have
- 6:07:22to check all the win
- 6:07:23conditions I feel like the best way to
- 6:07:25handle this would be to create an if
- 6:07:27statement followed by an else if
- 6:07:30statement followed by an else
- 6:07:33statement within our if statement this
- 6:07:35will be for a tie let's output the
- 6:07:39following it's a
- 6:07:42tie our Eli statement will be used if
- 6:07:45you win we'll
- 6:07:47output you
- 6:07:49win else will be used for you
- 6:07:53lose you
- 6:07:56lose if we have a tie that means the
- 6:07:59Player's Choice is equal to the
- 6:08:01computer's
- 6:08:02Choice that's how you get a tie within
- 6:08:05the of statement we can check the
- 6:08:06following condition if our player choice
- 6:08:10variable use the equals method of
- 6:08:14strings then pass in the variable of
- 6:08:17computer
- 6:08:19choice we're comparing two strings
- 6:08:21the're reference data types so we're
- 6:08:23going to use the equals method you can't
- 6:08:25use double equals because then you're
- 6:08:27comparing memory addresses of two
- 6:08:29different strings we're going to be
- 6:08:31using equals the equals method if these
- 6:08:34two strings are equal will print it's a
- 6:08:36tie now for the wi conditions within
- 6:08:39this LF statement this is where things
- 6:08:41are going to get kind of tricky we're
- 6:08:43going to list all of the different win
- 6:08:45conditions within our LF statement first
- 6:08:48we'll check if the player picks Rock and
- 6:08:50the computer picks scissors if our
- 6:08:52player
- 6:08:53choice use the equals
- 6:08:57method if the player picks Rock a string
- 6:09:01of
- 6:09:03rock and the the computer's
- 6:09:08Choice equals let me zoom out a little
- 6:09:12bit scissors that means we
- 6:09:16win within our condition of our LF
- 6:09:18statement we'll be adding the different
- 6:09:20win conditions but temporarily I'm going
- 6:09:23to add a few else if statements just to
- 6:09:25help me explain this so if we pick Rock
- 6:09:28and the computer pick scissors that
- 6:09:30means we win but we will also win if the
- 6:09:34player
- 6:09:36pick
- 6:09:37paper and the
- 6:09:39computer let's add that right
- 6:09:42here picks rock that means we also
- 6:09:46win and then temporarily I'll add
- 6:09:48another LF
- 6:09:51statement else if the player picks
- 6:09:57scissors and the computer picks
- 6:10:01paper that also means we win
- 6:10:06we could condense all of these LF
- 6:10:08statements into one but we'll do that in
- 6:10:10a moment I just want to be sure that it
- 6:10:12all works first let's do a test run I
- 6:10:16will pick Rock the computer picks
- 6:10:18scissors that means we
- 6:10:21win I will pick paper the computer picks
- 6:10:24paper it's a
- 6:10:26tie I'll keep playing until I lose I
- 6:10:29will pick scissors the computer picks
- 6:10:32paper we win let's try scissors again
- 6:10:36the computer picks Rock we lose we know
- 6:10:39that these different wi conditions are
- 6:10:42working this is fairly readable but if
- 6:10:45you prefer we can condense all of these
- 6:10:47steps down together into one else if
- 6:10:50statement optionally what you could do
- 6:10:53with our first win condition let's
- 6:10:55enclose them within a set of
- 6:10:59parentheses then I will use the or
- 6:11:02logical Operator Let Me zoom in so you
- 6:11:04can see
- 6:11:05it I'm adding or to the
- 6:11:08end
- 6:11:12or let's take the entire condition
- 6:11:14within our LS if statement then paste it
- 6:11:17after the or logical
- 6:11:20operator and these do need to be
- 6:11:22enclosed within a set of
- 6:11:26parentheses and we can delete this LF
- 6:11:30statement We'll add the or logical
- 6:11:32operator to the end
- 6:11:36cut this condition for the next LF
- 6:11:39statement and delete this LF
- 6:11:42statement and paste
- 6:11:45it so here's our LF statement there is a
- 6:11:49lot going on here this should work too
- 6:11:51but if you prefer to keep it the other
- 6:11:53way just for readability feel free to do
- 6:11:55so if you like to condense some of the
- 6:11:57code here's how you could do that so let
- 6:12:00me just be sure that everything's
- 6:12:01working fine
- 6:12:05I will pick Rock computer picks Rock
- 6:12:07it's a
- 6:12:08tie I will pick paper computer picks
- 6:12:11scissors you
- 6:12:13lose I will pick scissors computer picks
- 6:12:16paper you win all right this LF
- 6:12:20statement does work again if you would
- 6:12:22prefer to add more LF statements feel
- 6:12:25free to keep it the other way now we
- 6:12:27need to ask the user if they want to
- 6:12:28play
- 6:12:29again after displaying either it's a tie
- 6:12:32you win or you lose We'll add a prompt I
- 6:12:36will use print instead of print
- 6:12:39line I'll ask play
- 6:12:43again yes slash
- 6:12:47no we have one variable we haven't used
- 6:12:50yet our play again variable and
- 6:12:52currently it's
- 6:12:54yes play again equals use our scanner
- 6:12:58use the next line
- 6:13:00method and I will take the user's input
- 6:13:02and make it lowercase right away to
- 6:13:05lowercase
- 6:13:06method we're going to enclose all of our
- 6:13:09code right before the variables within a
- 6:13:12loop a do while loop so we'll say do all
- 6:13:16this code while some condition remains
- 6:13:20true let's cut all of our
- 6:13:24code cut it and then paste it within
- 6:13:27this do while
- 6:13:31loop so with the do while loop we do all
- 6:13:33of this code once
- 6:13:35and then check the condition at the
- 6:13:37end what's our condition going to be
- 6:13:41we'll continue playing while our play
- 6:13:43again variable equals a string of yes
- 6:13:47play again equals
- 6:13:50yes one more change we're going to
- 6:13:54make if the user picks something that
- 6:13:57isn't valid we're going to skip the
- 6:13:59current iteration of this Loop we can
- 6:14:01use continue for that
- 6:14:05we want to go back to the beginning if a
- 6:14:08user types in something that is
- 6:14:10invalid then the last step is to create
- 6:14:13a goodbye message we're doing this
- 6:14:15outside of the while loop cuz when we're
- 6:14:17outside of the while loop that means
- 6:14:18we're no longer playing let's print the
- 6:14:22following thanks for
- 6:14:25playing all right let's run this one
- 6:14:28last
- 6:14:30time I will pick Rock computer pick
- 6:14:33scissors you win play again again I'm
- 6:14:35going to type yes enter your move I will
- 6:14:38pick paper computer picks rock you win I
- 6:14:41will play again I will pick
- 6:14:45scissors the computer picks Rock You
- 6:14:47Lose I'm going to type no to play again
- 6:14:51and we escape that Doh Loop thanks for
- 6:14:54playing all right everybody so that is a
- 6:14:56game of rock paper scissors you can make
- 6:14:58using
- 6:15:01Java hello everybody in this video we're
- 6:15:04going to create a slot machine program
- 6:15:06using Java a user is going to bet an
- 6:15:08amount for the symbols of our slot
- 6:15:10machine we'll be using emojis if a user
- 6:15:13gets any matching symbols they'll
- 6:15:15receive a payout then at the end we'll
- 6:15:17give a final balance if that sounds good
- 6:15:20to you let's get started all right
- 6:15:23everybody when working on a project it
- 6:15:25might be helpful to you to tackle your
- 6:15:27project one step at a time as comments
- 6:15:29we'll list the different steps that our
- 6:15:31program is going to take this will give
- 6:15:33us a rough outline of our project the
- 6:15:35first thing we'll do is declare our
- 6:15:39variables then display a welcome message
- 6:15:42display welcome
- 6:15:46message we will play if our balance
- 6:15:50meaning the money that we have is
- 6:15:52greater than
- 6:15:54zero if we don't have any money we can't
- 6:15:57play it is a slot machine after all a
- 6:16:00user is going to enter bet amount then
- 6:16:03I'll write a few Subs steps underneath
- 6:16:07here we need to
- 6:16:09verify if the bet is greater than the
- 6:16:12balance you can't bet money that you
- 6:16:15don't
- 6:16:16have then
- 6:16:19verify if bet is greater than zero you
- 6:16:23can't bet negative money so we'll need
- 6:16:25to check
- 6:16:27that then subtract bet from our balance
- 6:16:34once we subtract the BET from the
- 6:16:36balance then we will spin our
- 6:16:39row our row is going to contain emojis
- 6:16:43but we'll need to display it we'll
- 6:16:45create a method to print our row of
- 6:16:49emojis once we display our row we need
- 6:16:52to get a payout if we win something if
- 6:16:55we have any
- 6:16:57matches then ask to play
- 6:17:01again if the user doesn't want to play
- 6:17:04we will display an exit message display
- 6:17:09exit message so here's a rough outline
- 6:17:12of our project we will Begin by
- 6:17:14declaring our
- 6:17:16variables we will be accepting user
- 6:17:18input we'll need a scanner for that
- 6:17:20scanner scanner equals new
- 6:17:23scanner type system.in within the set of
- 6:17:27parentheses import this class import
- 6:17:30java.util.scanner then when we're done
- 6:17:33with our scanner we'll want want to
- 6:17:35close it scanner.
- 6:17:40close we'll need a balance how much
- 6:17:42money do we have I will set that to be
- 6:17:46100 100 as in
- 6:17:48$100 this could also be done with other
- 6:17:50units of currency we'll need an amount
- 6:17:53for our bet int bet how much are we
- 6:17:56betting how much are we
- 6:17:58risking int
- 6:18:00payout how much money do we win we'll be
- 6:18:04adding our pay pay out to our balance if
- 6:18:06we
- 6:18:07win and a string array named
- 6:18:11row our row is going to contain our
- 6:18:14symbols our emojis that will display to
- 6:18:16the output window that's why we're
- 6:18:18creating a string array to store all of
- 6:18:20them here are the variables we'll need
- 6:18:23we'll move on to the next step our next
- 6:18:25step is to display a welcome
- 6:18:28message let's say something like this
- 6:18:30I'll use a print line statement we'll
- 6:18:33say welcome to
- 6:18:35Java
- 6:18:37slots underneath this line we'll add
- 6:18:40another print line statement I will
- 6:18:42print the word symbols and here we'll
- 6:18:45list all the different symbols we would
- 6:18:46like to use within our program I'll be
- 6:18:49using emojis to pull up the Emoji menu
- 6:18:52if you're on Windows hold on the window
- 6:18:53key then hit the semicolon
- 6:18:57button so pick some emojis that you like
- 6:19:00here's a few that I typically see
- 6:19:03cherries watermelon
- 6:19:05lemons bells and
- 6:19:09stars if you would like to stick to a
- 6:19:11certain theme feel free to change
- 6:19:14these but these are a few symbols you
- 6:19:16may see on a slot
- 6:19:18machine and I'm just going to work on
- 6:19:21the spacing a little bit that looks
- 6:19:23pretty
- 6:19:24good just to make this a little more
- 6:19:26fancy I'm going to add a bunch of
- 6:19:28asterisks as separators before and after
- 6:19:31not necessary I just think it looks cool
- 6:19:34and that's a good enough
- 6:19:35reason let's perform a test run just to
- 6:19:38be sure that this looks
- 6:19:41fine I think that looks pretty good all
- 6:19:44right we have our welcome
- 6:19:47message our next step is to play our
- 6:19:49game if our balance is greater than zero
- 6:19:52this can be done with the while
- 6:19:54loop while our balance is greater than
- 6:19:58zero we will continue
- 6:20:01playing then we have to enter the BET
- 6:20:03amount that's the next step
- 6:20:06first we'll display our current
- 6:20:08balance current balance colon
- 6:20:12space pick unit of currency I'll pick
- 6:20:15American
- 6:20:16dollars plus we will display our balance
- 6:20:20variable we will ask a user for some
- 6:20:24input place your bet
- 6:20:27amount I'll use print rather than print
- 6:20:30line because I like the user input on
- 6:20:32the same line we will assign our bet
- 6:20:37variable use our scanner to accept some
- 6:20:40input let's say next int we won't deal
- 6:20:44with cents we'll just bet whole
- 6:20:47dollars then we have a couple substeps
- 6:20:50we have to verify if our bet is greater
- 6:20:53than our
- 6:20:56balance we'll write an if
- 6:20:59statement if bet is greater than our
- 6:21:02balance we'll output the following
- 6:21:05let's say insufficient
- 6:21:10funds we'll want to skip the current
- 6:21:12cycle of this Loop we'll use continue to
- 6:21:15go back to the
- 6:21:17beginning then we'll verify if the bet
- 6:21:19is greater than
- 6:21:22zero we don't want a user to bet
- 6:21:24negative
- 6:21:26money else
- 6:21:30if our bet let's say less than or equal
- 6:21:34to zero we don't want to use our betting
- 6:21:37no
- 6:21:40money bet must be greater than
- 6:21:45zero else we have to subtract the BET
- 6:21:48from the
- 6:21:50balance we could say balance equals
- 6:21:54balance minus bet but we can shorten
- 6:21:57this using the augmented assignment
- 6:21:59operator we can say balance minus equals
- 6:22:03bet Within our LF statement we're going
- 6:22:05to use the continue keyword to skip the
- 6:22:08current iteration of this Loop so
- 6:22:11temporarily within my else statement I'm
- 6:22:13just going to print our balance right
- 6:22:17after just to be sure that it's all
- 6:22:20working let's do a test
- 6:22:23run okay welcome to Java slots current
- 6:22:26balance $100 place your bet amount I
- 6:22:29will bet money that I don't have one1
- 6:22:31kajillion dollars insufficient funds
- 6:22:35current balance $100 place your bet
- 6:22:38amount we can't bet negative money I'll
- 6:22:41bet1 bet must be greater than zero then
- 6:22:44we're brought back to the beginning I
- 6:22:47will bet
- 6:22:48$10 and our new balance is
- 6:22:51$90 all right we know that that
- 6:22:55works we can delete this print line
- 6:22:57statement we no longer need it we were
- 6:22:59just testing something our next step is
- 6:23:02that we're going to spin our row of
- 6:23:03symbols our
- 6:23:06emojis after we subtract our bet from
- 6:23:09our balance it's here that we'll spin
- 6:23:11our Row first I'll print the following
- 6:23:14message let's say the word
- 6:23:18spinning then we will call a method to
- 6:23:20spin our row but we will need to declare
- 6:23:23this method we will do that outside of
- 6:23:26the main
- 6:23:28method this will be a static method the
- 6:23:32return type is going to be an array of
- 6:23:34strings
- 6:23:35string array and the name of the method
- 6:23:38will be spin
- 6:23:41row we'll be creating a few local
- 6:23:44variables within this method but first
- 6:23:46we do need to return
- 6:23:48something because we promised that we're
- 6:23:51going to return a string array in order
- 6:23:53for this program to run a compile fine I
- 6:23:55will add The Following return statement
- 6:23:57return a new empty array of strings
- 6:24:01we'll be replacing this later but we do
- 6:24:03have to return something in order for
- 6:24:05this method to at least work we'll be
- 6:24:07creating a few local variables the first
- 6:24:11is a string
- 6:24:12array named
- 6:24:14symbols equals then within a set of cly
- 6:24:18braces we need our emojis let's go back
- 6:24:21to the
- 6:24:22beginning copy our
- 6:24:25emojis then place them within this array
- 6:24:28they do need to be surrounded with a set
- 6:24:30of double quotes because this is an
- 6:24:32array of strings
- 6:24:34so let's take a moment to take care of
- 6:24:43that all right that is good we will
- 6:24:46declare a string array named row our row
- 6:24:51is going to be empty it's going to be a
- 6:24:53new string array that can hold three
- 6:24:57elements row is an empty array that can
- 6:25:00hold three strings which will be picked
- 6:25:02at random
- 6:25:05we will be generating random numbers we
- 6:25:07need a random object random random
- 6:25:10equals new
- 6:25:12random then import this
- 6:25:15class at the top import java.util
- 6:25:22random temporarily there's something I'm
- 6:25:25going to demonstrate let's display our
- 6:25:28aray of symbols at index of zero that
- 6:25:31should output this first Emoji a cherry
- 6:25:36I'll just bet a dollar doesn't matter
- 6:25:38and we have our
- 6:25:41Cherry one should be our
- 6:25:46watermelon
- 6:25:49watermelon then four should be our
- 6:25:53star
- 6:25:56star we need to choose a random number
- 6:25:59between zero and four when we select our
- 6:26:02array of symbols within the straight
- 6:26:04brackets where we list an index we will
- 6:26:07take our random object call the next int
- 6:26:12method we will generate a random number
- 6:26:15between zero and
- 6:26:17five the first number is inclusive the
- 6:26:20second number is exclusive if your range
- 6:26:22starts with zero you don't technically
- 6:26:24need this first number you could just
- 6:26:25say
- 6:26:26five let's run this a few times just to
- 6:26:29see what
- 6:26:30happens so that time I got a bell
- 6:26:36a
- 6:26:39cherry and a
- 6:26:42watermelon if we have more or less
- 6:26:44emojis we would need to change this
- 6:26:46number why don't we use the length of
- 6:26:49our symbols instead symbols then give me
- 6:26:52the length property of this
- 6:26:55array if we were to add more or less
- 6:26:57symbols our length is going to change
- 6:26:59automatically when we run and compile
- 6:27:01this
- 6:27:02program so we'll do a test run again
- 6:27:05again and that seems to
- 6:27:10work you're getting a random symbol each
- 6:27:13time let's delete the print line
- 6:27:16statement portion of
- 6:27:18this we need to generate three random
- 6:27:21symbols we can use a for Loop for that
- 6:27:24for in I equals 0 we will continue as
- 6:27:29long as I is less than three then
- 6:27:32increment I by One
- 6:27:34we will take our row at index of I in
- 6:27:39the beginning I is going to equal zero
- 6:27:41then one then two then the four Loop
- 6:27:43ends we will assign row at index of I
- 6:27:47equal to cut this line of
- 6:27:51code we're picking a random symbol from
- 6:27:54our array of strings our array of
- 6:27:57emojis at the end we will return our row
- 6:28:01we're returning our array of strings
- 6:28:04just to test it I'm going to Output
- 6:28:07using print row at index
- 6:28:13zero plus row at index
- 6:28:18one plus row at index 2 I just want to
- 6:28:22be sure that we get three random
- 6:28:27symbols there we go we get three random
- 6:28:31symbols we'll format the output later so
- 6:28:33don't
- 6:28:36wor we can delete this statement we were
- 6:28:38just testing
- 6:28:40something now this method is complete we
- 6:28:43can close
- 6:28:45it we're returning a string array back
- 6:28:48to the place in which we called this
- 6:28:49method we can assign this
- 6:28:52array back to the beginning we will
- 6:28:54assign our string array of
- 6:28:58row our row equals call the spin row
- 6:29:02method and this will return a string
- 6:29:05array which we're assigning to row once
- 6:29:08we have our row we're going to print our
- 6:29:10row we'll need to create another method
- 6:29:13for that we will create a static method
- 6:29:16it's not going to return anything the
- 6:29:18return type is void the name will be
- 6:29:21print
- 6:29:23row there will be one parameter we
- 6:29:26accept a string array which we will name
- 6:29:31row after assigning our row we will call
- 6:29:34call the print row
- 6:29:35method but we have to pass in our
- 6:29:38row because we have one parameter we
- 6:29:41have to pass in a string array which we
- 6:29:45are so here's where we'll print and
- 6:29:47display our row of emojis let's output
- 6:29:50the following I will display a space
- 6:29:53character plus now the string class has
- 6:29:56a few useful methods type string with a
- 6:29:59capital s dot call the join method
- 6:30:04for each character within a string we
- 6:30:06can join them together with another
- 6:30:08character I will join them by a space a
- 6:30:12vertical bar then a space comma
- 6:30:16Row for every string in my string array
- 6:30:19of row we're going to join them with the
- 6:30:21vertical bar along with a space before
- 6:30:23and after here's the output
- 6:30:26currently I'll just bet a dollar here's
- 6:30:29what we have we have three
- 6:30:31emojis each separated with the IAL
- 6:30:36bar I'll add some of those separators
- 6:30:38too those
- 6:30:40asterisks 14 Aster risks should be
- 6:30:51enough all right let's take another look
- 6:30:53I'll bet
- 6:30:55$10 that looks pretty good I'm happy
- 6:30:57with that our next step is to get a
- 6:31:00payout if there's any
- 6:31:02matches we can collapse this method we
- 6:31:05no longer need
- 6:31:07it now we need to get our
- 6:31:10payout after printing our row we will
- 6:31:13call a method of get payout which we
- 6:31:15still need to
- 6:31:17declare we'll pass in two things our
- 6:31:20string array of row and our bet how much
- 6:31:24money did we
- 6:31:25bet we need to declare this
- 6:31:28method after the print row method we
- 6:31:31will declare a static method this method
- 6:31:34is going to return an integer our payout
- 6:31:37the method name will be get
- 6:31:40payout we have to set up the parameters
- 6:31:43we're accepting a string
- 6:31:46array we will name the string array
- 6:31:49row and a bet our bet is an integer int
- 6:31:55bet we do need to return something just
- 6:31:58temporarily at the end I'm going to
- 6:32:00return
- 6:32:01zero we need to check to see if we have
- 6:32:04any
- 6:32:05matches what we could do first is check
- 6:32:08to see if all three elements match are
- 6:32:11all these strings the same symbol here's
- 6:32:13how we can do
- 6:32:15that let's write an if
- 6:32:18statement if our
- 6:32:20row at index of zero that's the first
- 6:32:24symbol now you can't use the comparison
- 6:32:27operator for Strings cuz they're
- 6:32:28reference data
- 6:32:30types you would think you could say if
- 6:32:32row at index 0 is equal to row at index
- 6:32:36one but you actually need to use
- 6:32:40equals if row at index
- 6:32:42zero equals row at index
- 6:32:50one if the first two symbols match row
- 6:32:53at index0 and row at index one well then
- 6:32:57the first two symbols match but not only
- 6:32:59that we have to compare row at index one
- 6:33:01and row at index 2 do those match as
- 6:33:04well we're checking to see if all three
- 6:33:06symbols match we will use the and
- 6:33:08logical operator and we can copy this
- 6:33:12check paste it and if row at index one
- 6:33:17equals row at index 2 if all three
- 6:33:20symbols match then we execute this if
- 6:33:23statement why don't we use an enhanced
- 6:33:25switch a switch can return something
- 6:33:28we'll use the return
- 6:33:30keyword create a switch within the
- 6:33:34switch where are we examining let's
- 6:33:36examine one of these symbols we can just
- 6:33:38pick any I'll just pick the first
- 6:33:40one we are examining our row at index
- 6:33:43zero all three symbols are going to be
- 6:33:46the same so it doesn't matter which one
- 6:33:47you pick we'll examine this value this
- 6:33:51Emoji against any matching cases the
- 6:33:54first case will be our first symbol of a
- 6:33:57cherry if these two values match we'll
- 6:34:00do the following we'll write an arrow
- 6:34:04and we're going to be returning
- 6:34:05something let's return our bet times
- 6:34:08three let's
- 6:34:10say we get a payout of three times what
- 6:34:13we bet for our next
- 6:34:16case that was a
- 6:34:19watermelon the payout will be
- 6:34:24four for a
- 6:34:28lemon let's set the payout to be
- 6:34:32five a
- 6:34:39Bell three matching Bells will be worth
- 6:34:4210 three stars let's say is
- 6:34:5020 bet times
- 6:34:5320 We'll add a default case too in case
- 6:34:55there's no
- 6:34:56matches default Arrow return
- 6:35:02zero looks like I'm missing a semicolon
- 6:35:04at the end if all three symbols match
- 6:35:08return our bet times a certain amount
- 6:35:10depending on which symbols
- 6:35:12match the get payout method is going to
- 6:35:15return an
- 6:35:16integer we will assign our variable of
- 6:35:19payout equal to get
- 6:35:23payout if our payout is greater than
- 6:35:28zero we'll do the following that means
- 6:35:30we won something if that's the case
- 6:35:33let's let's output U1 pick a unit of
- 6:35:38currency plus our payout whatever we
- 6:35:42win then we will add that to our balance
- 6:35:45balance equals balance plus our payout
- 6:35:49but we can shorten this using the
- 6:35:51augmented assignment operator for
- 6:35:53addition balance plus equals our
- 6:35:58payout else if we don't get a payout if
- 6:36:01our payout is zero that means we didn't
- 6:36:04win anything we'll
- 6:36:06output sorry you lost this round okay
- 6:36:11let's do a few test runs I'm going to
- 6:36:13keep on running this until we get three
- 6:36:15matching symbols just to be sure that it
- 6:36:17all works I will bet a
- 6:36:20dollar our message is sorry you lost
- 6:36:22this round and the current balance is
- 6:36:24$99 I'll bet another dollar you lost you
- 6:36:29lost you
- 6:36:32lost it's hard to get three matching
- 6:36:35symbols here we go I have three matching
- 6:36:38cherries you won $3 and my current
- 6:36:41balance is
- 6:36:43$86 now if you would like to check to
- 6:36:45see if the first two symbols match or
- 6:36:47the second two you can add the following
- 6:36:51code let's copy our if
- 6:36:53statement paste it change if to be else
- 6:36:57if we'll only be checking the first two
- 6:37:00symbols we can delete the second
- 6:37:03condition
- 6:37:04if the first two symbols match we'll
- 6:37:07give a reduced payout let's say for two
- 6:37:09matching cherries that will be two for
- 6:37:12watermelon that will be three
- 6:37:154 5 and then
- 6:37:1810 if the second symbol matches the
- 6:37:21third
- 6:37:22one we'll write another L
- 6:37:26statement index one and index two will
- 6:37:29give the same
- 6:37:31payout hello this is from the future
- 6:37:34there's just one bug that I need to fix
- 6:37:36the else if statement where we check if
- 6:37:38our second symbol matches the third
- 6:37:41within our switch we have to examine one
- 6:37:43of these two symbols I forgot to change
- 6:37:45that originally both these symbols
- 6:37:48should be the same so it doesn't matter
- 6:37:49which one we pick I'll just pick the
- 6:37:51index at one back to the previous LF
- 6:37:54statement index zero is fine because the
- 6:37:57first two symbols should match I just
- 6:37:59needed to fix that that is all let's
- 6:38:02check to be sure that works I'll bet a
- 6:38:07dollar so my first two symbols match we
- 6:38:10have a reduced payout of $3 but if it
- 6:38:13was three watermelons we would win $4
- 6:38:18instead we can close this get payout
- 6:38:20method we're done with
- 6:38:21it here we'll ask the user to play again
- 6:38:24if they would like to
- 6:38:28continue we'll create a
- 6:38:31prompt do you want to play again I'll
- 6:38:36use print rather than print
- 6:38:39line there's one more variable we'll
- 6:38:42need that I'm
- 6:38:43forgetting let's create a string
- 6:38:46variable of play
- 6:38:54again we will assign play again equal to
- 6:38:58use our scanner call the next line
- 6:39:01method
- 6:39:04we'll want a user to type in either
- 6:39:06capital y or capital N after accepting
- 6:39:10user input we'll make it uppercase using
- 6:39:12the two uppercase
- 6:39:17method using an if statement we'll check
- 6:39:21we'll check if play again doesn't
- 6:39:26equal for comparing strings we use
- 6:39:28equals if play again doesn't equal a
- 6:39:32capital Y then we will break to break
- 6:39:35out of the loop that we're
- 6:39:37in then go to the exit the exit
- 6:39:40message one more thing we need to add
- 6:39:43though I'm going to play a
- 6:39:46bet we get that prompt to play again but
- 6:39:50the program exits after typing in a
- 6:39:53number there's still a new line
- 6:39:55character within the input
- 6:39:57buffer because we're using next int next
- 6:40:00integer we need to clear that input
- 6:40:03buffer
- 6:40:04after this line of codee where we call
- 6:40:06the next int method we can take our
- 6:40:08scanner called the next line method to
- 6:40:11get rid of that new line character and
- 6:40:13that should
- 6:40:15work I'll bet a dollar then we can play
- 6:40:18again if we type in
- 6:40:21y we display our current balance we can
- 6:40:24place another
- 6:40:25bet hey $50 that time I'll type n to not
- 6:40:30play again then we exit
- 6:40:33the last thing we're going to do is
- 6:40:35display an exit
- 6:40:37message we'll take care of that right
- 6:40:39here once we're out of the
- 6:40:41loop let's output the
- 6:40:44following game
- 6:40:47over your final
- 6:40:50balance is pick unit of
- 6:40:54currency then I will add our variable of
- 6:40:57balance okay let's run this one last
- 6:41:00time welcome to Java slots I'll bet a
- 6:41:04dollar sorry you lost would you like to
- 6:41:07play again I will type y for yes current
- 6:41:10balance $99 I will bet a dollar
- 6:41:13again I
- 6:41:15lost I'll just keep playing till I win
- 6:41:18hey I won $10 we have two matching Stars
- 6:41:22I'll play one more time I will bet
- 6:41:25$10 and I won 20 we have two matching
- 6:41:29symbols I will type in no to not play
- 6:41:31again and we get our game over message
- 6:41:34your final balance is
- 6:41:37$117 so we came out ahead all right
- 6:41:40everybody that is a slot machine game
- 6:41:42you can create using
- 6:41:46Java yo we have finally made it to
- 6:41:49object-oriented programming in Java so
- 6:41:52what is an object exactly look around
- 6:41:54wherever you're sitting right now you
- 6:41:56are surrounded by different objects for
- 6:41:59example next to me I have my phone a cup
- 6:42:03of coffee and a microphone in
- 6:42:06programming an object can represent a
- 6:42:08real world entity they can do one of a
- 6:42:11few things objects can hold data and
- 6:42:14they can perform actions the data they
- 6:42:17hold are known as attributes the actions
- 6:42:20that they can perform are known as
- 6:42:22methods objects can have things and they
- 6:42:25can do things some attributes that a
- 6:42:27person may have would be a name their
- 6:42:30age their height and their weight a few
- 6:42:33methods meaning actions that a human
- 6:42:36could do a person well they could eat
- 6:42:38and they could sleep objects are
- 6:42:40reference data types we store the data
- 6:42:43for an object in a location known as the
- 6:42:46Heap in this demonstration we're going
- 6:42:48to be creating some car objects but
- 6:42:50we'll need a Class A Class can serve as
- 6:42:53a blueprint to create objects so what
- 6:42:56we're going to do is create a new Java
- 6:42:59class we will create a class of car
- 6:43:04we have two classes our main class and
- 6:43:07our car
- 6:43:08Class A Class can behave as a blueprint
- 6:43:11for creating objects we'll start by
- 6:43:13listing the different attributes that a
- 6:43:15car may have so what does a car have
- 6:43:18exactly well a car could have a make
- 6:43:21that would be a
- 6:43:22string we can assign it if we would like
- 6:43:25think of a car you'd like or a car you
- 6:43:27drive I'll pick my favorite car the make
- 6:43:30will be Ford another attribute could be
- 6:43:33a
- 6:43:34model and I will pick a
- 6:43:38Mustang cars could have a year that
- 6:43:40would be an integer into Year I'll pick
- 6:43:42the year
- 6:43:442025 a price that could be a double
- 6:43:47double price
- 6:43:49equals let's say
- 6:43:53$58,000
- 6:43:5599 a car's engine could be running that
- 6:43:58could be a Boolean Boolean is running I
- 6:44:02will set that to be true
- 6:44:03or false if the car is turned off things
- 6:44:07that an object has in this case car
- 6:44:09these are known as attributes they are
- 6:44:12things that an object
- 6:44:14has now heading back to our main class
- 6:44:17we will create a car
- 6:44:19object we're already a little familiar
- 6:44:21with working with objects for example a
- 6:44:24scanner is an object to create a scanner
- 6:44:26we would type scanner scanner equals new
- 6:44:32scanner then you type
- 6:44:34system.in or with random objects we
- 6:44:37would type random random equals new
- 6:44:42random Now to create a car object we're
- 6:44:45going to be utilizing this
- 6:44:47class we'll type car then think of a
- 6:44:50name for this car we'll just say car all
- 6:44:52lowercase equals new car we're following
- 6:44:56a similar
- 6:44:57pattern car car equals new
- 6:45:00car we don't need the scanner or the
- 6:45:03random object I was just demonstrating
- 6:45:06something we now have a car object that
- 6:45:08we can use our car object has these
- 6:45:12attributes these
- 6:45:15things if I was to print my car object
- 6:45:19here's what we can
- 6:45:21see well since objects are a reference
- 6:45:24data type if you were to print your car
- 6:45:26directly you get a memory
- 6:45:30address if you need to access one of
- 6:45:32these attributes
- 6:45:34you have to follow the object name with
- 6:45:36a DOT a DOT is known as the dot operator
- 6:45:41it allows you to access things within an
- 6:45:43object if I need the model of my car I
- 6:45:47would take my object name use the dot
- 6:45:50operator followed by the attribute name
- 6:45:53if I was to print the model of my
- 6:45:55car that would output
- 6:45:59Mustang let's print some of the other
- 6:46:01attributes car. make who manufactures
- 6:46:05the
- 6:46:06car
- 6:46:08Ford what is the year of my car car.
- 6:46:14year
- 6:46:182025 what's the price car.
- 6:46:23price
- 6:46:25$58,000 and
- 6:46:2899 is my car running or not car do is
- 6:46:32running
- 6:46:37false the car is not currently running
- 6:46:40you can modify and change these
- 6:46:41attributes too with our car object let's
- 6:46:45access the is running attribute I will
- 6:46:48set that to be true like we're turning
- 6:46:49the car
- 6:46:51on is running is now true because we
- 6:46:55changed the attribute of is
- 6:46:58running not only do objects have the
- 6:47:00capability to hold data meaning
- 6:47:03attributes they can perform actions they
- 6:47:06can have their own methods going back to
- 6:47:08our car class we'll Define a few
- 6:47:11methods what sorts of actions can a car
- 6:47:14take well we could start a car within
- 6:47:17this class we will Define a method we
- 6:47:20don't need that static keyword this time
- 6:47:23we need a return type we'll say void
- 6:47:25we're not returning anything we will
- 6:47:27Define a method of
- 6:47:29start let's do the following let's
- 6:47:32output
- 6:47:33you start the
- 6:47:36engine we'll create another
- 6:47:39method
- 6:47:41void
- 6:47:47stop let's say you stop the
- 6:47:51engine so now with our
- 6:47:54car we can delete these print line
- 6:47:57statements we'll take our car object use
- 6:48:00the dot operator call the start method
- 6:48:03method this should output you start the
- 6:48:07engine or we can stop car.
- 6:48:13stop you start the engine you stop the
- 6:48:16engine not only that Within These
- 6:48:18methods let's change one of these
- 6:48:20variables one of these
- 6:48:22attributes within our start method let's
- 6:48:25take is running set that to be
- 6:48:28true within the stop method we'll take
- 6:48:31is running set that to be
- 6:48:35false going back to our main
- 6:48:38class before starting the car let's
- 6:48:41output car do is
- 6:48:44running after starting the car let's
- 6:48:47output car dot is running and then
- 6:48:50stopping the car will output car dot is
- 6:48:55running the car is currently not running
- 6:48:57that's false you start the engine the
- 6:49:00car is now running that's set to true
- 6:49:02you stop the engine the car is no longer
- 6:49:05running that is
- 6:49:06false let's add two more methods we can
- 6:49:10start we can stop we can drive we'll
- 6:49:13create a method to drive void
- 6:49:19Drive let's output you drive the I'm
- 6:49:24going to use string concatenation we'll
- 6:49:26concatenate the model of the
- 6:49:29car so my car is a Mustang
- 6:49:33we will create a method to
- 6:49:38break
- 6:49:40you break the plus our model variable
- 6:49:45our model
- 6:49:47attribute we can delete these lines of
- 6:49:50code let's drive the car car.
- 6:49:54drive you drive the Mustang and here's
- 6:49:57the model of the car and we can break
- 6:50:00car. break
- 6:50:05you drive the Mustang you break the
- 6:50:08Mustang there's one issue with this
- 6:50:10though with our class of car every car
- 6:50:14that we make has the same attributes and
- 6:50:16methods we can only create 225 Ford
- 6:50:22Mustangs if I was to create another car
- 6:50:25object let's say car car 2
- 6:50:29equals new
- 6:50:31car we'll rename the first car as car
- 6:50:351 and I'll output some of their
- 6:50:38attributes Car
- 6:50:411make Plus a
- 6:50:44space plus car
- 6:50:471 model we'll do this with our second
- 6:50:50car object too Car 2 car 2. make car 2.
- 6:50:56model these cars have the same
- 6:50:59attributes it would be nice if we had a
- 6:51:01way to customize them so that each car
- 6:51:04is unique because currently they're all
- 6:51:06the same they're different cars but they
- 6:51:08have the same attributes and methods
- 6:51:10which is kind of lame that's why in the
- 6:51:12next video we're going to discuss
- 6:51:14Constructors by passing in arguments we
- 6:51:17can create unique objects in summary an
- 6:51:20object is an entity that holds data it
- 6:51:23has attributes and they can perform
- 6:51:26actions they can perform
- 6:51:28methods they have things and they can do
- 6:51:31things and and well everybody those are
- 6:51:34objects in
- 6:51:37Java hey yeah today we're talking about
- 6:51:39Constructors in Java a Constructor is
- 6:51:42just a special method within a class
- 6:51:44it's used to initialize objects by using
- 6:51:47a Constructor we can create objects with
- 6:51:50unique values when you create an object
- 6:51:52you can pass arguments to a Constructor
- 6:51:54to set up the initial values here's a
- 6:51:57demonstration what we're going to do is
- 6:52:00create a new class let's go to file new
- 6:52:04Java
- 6:52:05class let's create a class of students
- 6:52:09this will be the student
- 6:52:11class what are some attributes that a
- 6:52:14student should have they should have a
- 6:52:17name let me zoom in a little so you can
- 6:52:19read it NH AG int
- 6:52:22ag ag GPA meaning grade point
- 6:52:26average let's create a Boolean of is
- 6:52:30enrolled
- 6:52:33that is good enough for now there's one
- 6:52:35issue with this let's say that all
- 6:52:38students that I create using this class
- 6:52:41they're all going to be named
- 6:52:43SpongeBob going back to my Java file
- 6:52:46I'll create a few
- 6:52:47students we type the name of the class
- 6:52:49and name for this object let's say
- 6:52:51student one equals new student the name
- 6:52:55of the class
- 6:52:56again then we'll create a second
- 6:52:59student student two
- 6:53:03I'm going to Output student one's name
- 6:53:06student 1. name as well as student 2's
- 6:53:13name the issue that we have is that all
- 6:53:16students that we create all student
- 6:53:18objects they're all named
- 6:53:21SpongeBob what if we would like to give
- 6:53:24each object a unique name well we can do
- 6:53:27that with the help of a Constructor a
- 6:53:29Constructor is just a special method
- 6:53:31within a class
- 6:53:33to set up a Constructor we type the name
- 6:53:35of the class in this case student you
- 6:53:38need a set of parentheses then a set of
- 6:53:40curly
- 6:53:41braces we automatically call this
- 6:53:45Constructor when we instantiate an
- 6:53:47object of that class when we say new
- 6:53:51then the name of the class
- 6:53:53parenthesis behind the scenes we're
- 6:53:56calling this
- 6:53:57Constructor and we can pass in
- 6:53:59arguments within the set of parentheses
- 6:54:01we can pass in arguments so let's pass
- 6:54:04in what are we missing a name age GPA is
- 6:54:08enrolled as well but we'll take care of
- 6:54:10that momentarily let's pass in a name
- 6:54:13age and
- 6:54:14GPA student one's name will be
- 6:54:19SpongeBob SpongeBob's age will be let's
- 6:54:21say 30 he seems about 30 SpongeBob's GPA
- 6:54:25can be 3.2 that's pretty
- 6:54:29good we have a warning our Constructor
- 6:54:32isn't set up to take these arguments
- 6:54:35going back to our Constructor since
- 6:54:37we're receiving arguments we need a
- 6:54:39matching set of parameters just like any
- 6:54:41other method we have a string for name
- 6:54:44integer of age double GPA we'll take
- 6:54:47care of these three first we have string
- 6:54:50name int age double
- 6:54:55GPA to assign some of these attributes
- 6:54:59we're going to use the this keyword this
- 6:55:02dot the name of the attribute this do
- 6:55:06name equals our parameter of
- 6:55:10name this. AG equals the age that we
- 6:55:15receive this. GPA equals the GPA that we
- 6:55:20receive going back to our Java file we
- 6:55:23can now construct an object if I attempt
- 6:55:26to run this currently we are receiving a
- 6:55:30warning our Constructor expects three
- 6:55:33arguments but we don't pass in any zero
- 6:55:35are found if I were to run
- 6:55:38this we get this error message actual
- 6:55:42and formal argument lists differ in
- 6:55:44length in order for us to create a scent
- 6:55:47object we have to pass in these three
- 6:55:49things a string for the name an integer
- 6:55:52for the age and a double for the
- 6:55:55GPA if we are missing these three we
- 6:55:58can't construct an object but previously
- 6:56:01we could with without the Constructor
- 6:56:03for our second student let's pass in a
- 6:56:06name of Patrick Patrick will be
- 6:56:0934 Patrick's GPA isn't too good it's a
- 6:56:161.5 all right let's test this we're
- 6:56:18printing student one's name and student
- 6:56:202's
- 6:56:22name we get SpongeBob and
- 6:56:25Patrick let's display some of the other
- 6:56:30attributes we have
- 6:56:33name age
- 6:56:36GPA we'll do this with student 2 as well
- 6:56:39student 2.age student 2.
- 6:56:44GPA SpongeBob SpongeBob is 30 years old
- 6:56:48SpongeBob's GPA is
- 6:56:503.2 these are all the unique attributes
- 6:56:53of our student object student
- 6:56:55one student 2's attributes are Patrick
- 6:56:59age is 34 GPA is 1.5
- 6:57:04with the this keyword this refers to the
- 6:57:07object we're currently constructing or
- 6:57:09otherwise working with imagine that when
- 6:57:12we construct student
- 6:57:14one imagine we replace the this keyword
- 6:57:17with the name of the object we're
- 6:57:20assigning student one's name equal to
- 6:57:22the name that we receive same thing
- 6:57:25applies with our age and our
- 6:57:28GPA the same thing would apply with
- 6:57:30student 2 when we construct student 2
- 6:57:34imagine during the process we replace
- 6:57:36this with student
- 6:57:392 this refers to the object we're
- 6:57:41currently working with or
- 6:57:43constructing with these parameter names
- 6:57:46they don't necessarily need to be the
- 6:57:47same as your attribute names just to
- 6:57:50demonstrate let's rename name as a age
- 6:57:53as B GPA as C this.name equals a this.
- 6:57:59age equals B this. GPA equals c
- 6:58:03this should work
- 6:58:05too each object still has their unique
- 6:58:11attributes but I like to keep the
- 6:58:12parameter names the same as the
- 6:58:14attributes it makes the code easier to
- 6:58:16read and understand we haven't assigned
- 6:58:19to this attribute of is
- 6:58:21enrolled each object that we create
- 6:58:23using this Constructor let's assign this
- 6:58:26objects is enrolled attribute equal to
- 6:58:29you don't always necessarily need to
- 6:58:32assign arguments to these attributes for
- 6:58:34example let's just say that when you
- 6:58:36become a student you are enrolled that
- 6:58:39is true we don't necessarily need to
- 6:58:41pass in an argument for that going back
- 6:58:44to our main class let's print take
- 6:58:48student ones is enrolled attribute and
- 6:58:51then print it we'll do the same thing
- 6:58:54with student two
- 6:58:57Patrick when we create a student object
- 6:59:00we're automatically setting is a
- 6:59:02enrolled to be true they are enrolled in
- 6:59:04classes and that applies to Patrick as
- 6:59:07well student to Let's create another
- 6:59:09student object just so that we get the
- 6:59:11hang of this repetition is going to help
- 6:59:13you remember let's create student 3
- 6:59:17student 3 is an object
- 6:59:19new
- 6:59:21student to create a student we have to
- 6:59:23pass in three things a string for the
- 6:59:25name an integer for the age and a double
- 6:59:28for the
- 6:59:30GPA student 3 will be be
- 6:59:33Sandy Sandy is 27 let's say Sandy is
- 6:59:37smart she'll have a 4.0
- 6:59:40GPA and then we'll print student 3's
- 6:59:43attributes student 3. name student 3's
- 6:59:47age student 3's GPA student 3 is
- 6:59:52enrolled our object of student 3 has
- 6:59:55these attributes Sandy 27 4.0 and
- 7:00:00true if if your class has any methods
- 7:00:03for example all students have the method
- 7:00:06to
- 7:00:09study void study I'll just output the
- 7:00:13following let's take our name
- 7:00:16attribute but I'm going to use
- 7:00:19this.name plus the text of is
- 7:00:24studying let's delete these print line
- 7:00:27statements we'll take student one call
- 7:00:30the study method
- 7:00:33then we'll do this with student two and
- 7:00:35student
- 7:00:393 SpongeBob is studying Patrick is
- 7:00:42studying Sandy is
- 7:00:45studying after assigning values to your
- 7:00:48attributes using a Constructor you can
- 7:00:50use them within methods too or change
- 7:00:53them this refers to the object we're
- 7:00:56currently working with if student 3
- 7:00:58meaning Sandy was to call her study
- 7:01:00method imagine we're replacing this with
- 7:01:03student 3 student 3's name
- 7:01:08attribute all right everybody those are
- 7:01:10Constructors they're a special method
- 7:01:12found within a class to initialize
- 7:01:14objects you can pass arguments to a
- 7:01:17Constructor when you initialize them
- 7:01:19they're used to set up the initial
- 7:01:21values when you construct an object a
- 7:01:24Constructor is automatically called when
- 7:01:26you create a new object but you need a
- 7:01:28matching set of arguments if there's any
- 7:01:31parameter setup just like any other
- 7:01:34method by using a Constructor we can
- 7:01:37create objects with unique values and
- 7:01:40well everybody those are Constructors in
- 7:01:44Java what is going on people today I'm
- 7:01:47going to explain overloaded Constructors
- 7:01:50overloaded Constructors allow a class to
- 7:01:52have multiple Constructors with
- 7:01:54different parameter lists much like
- 7:01:56overloaded methods they enable an object
- 7:01:59to be initialized in various ways here's
- 7:02:02a demonstration we are going to create a
- 7:02:04new class let's go to file new Java
- 7:02:08class we will create a class for users
- 7:02:11we will create a user
- 7:02:13class let's say that users have three
- 7:02:17things they will have a string of
- 7:02:21username a string of
- 7:02:24email and an integer in
- 7:02:27age let's say that when we create a user
- 7:02:30object these three fields are optional
- 7:02:34if these three attributes are optional
- 7:02:36we may or may not receive them when we
- 7:02:38construct a user object so let's do this
- 7:02:41let's create a Constructor if somebody
- 7:02:43were to create a user object with just a
- 7:02:46username to create a Constructor we type
- 7:02:49the name of the class in this case
- 7:02:52user we need to set up a parameter for
- 7:02:55our username we will receive a string of
- 7:02:58username we will assign this St user
- 7:03:01usern name equal to the username that we
- 7:03:04receive as an argument let's go back to
- 7:03:07our main Java
- 7:03:10file we will create a new
- 7:03:12user let's say user user one equals new
- 7:03:20user we're receiving a warning
- 7:03:23though in order to create a user object
- 7:03:26we have to pass in a string of
- 7:03:29username let's say that our user is
- 7:03:31going to be SpongeBob because that's the
- 7:03:33first thing that came to mind for
- 7:03:35me we'll use this Constructor where we
- 7:03:37have a username we can also set up the
- 7:03:39default values for our attributes too
- 7:03:42let's say this. email equals a string of
- 7:03:46not
- 7:03:49provided this. AG equals
- 7:03:520 going back to user
- 7:03:55one I will output the
- 7:03:58following user one dot
- 7:04:05username user one's email user 1's age
- 7:04:11here's what we got
- 7:04:13currently username is SpongeBob email is
- 7:04:16not provided age is
- 7:04:19zero these attributes were optional when
- 7:04:23we construct this object we're just
- 7:04:25assigning them to some default values
- 7:04:27what if we create a user that has an
- 7:04:29email well we could create another
- 7:04:31Constructor with the same
- 7:04:33name but we need a different set of
- 7:04:36parameters this is very similar to
- 7:04:39Method overloading you can have methods
- 7:04:41with the same name but they need
- 7:04:43different
- 7:04:45parameters not only do we have a string
- 7:04:47of username we'll have a string of
- 7:04:50email our email attribute equals the
- 7:04:54email that we receive we can use either
- 7:04:57of these Constructors depending on the
- 7:04:58arguments that are passed in
- 7:05:01let's create a second user user user 2
- 7:05:06equals new
- 7:05:09user the username will be
- 7:05:12Patrick Patrick's email is let's say
- 7:05:17p Patrick is probably still using AOL
- 7:05:20for some reason let's say
- 7:05:23aol.com then we'll print user 2's
- 7:05:26attributes user 2. username user two.
- 7:05:29email user 2.age
- 7:05:32here's what we got we have all the
- 7:05:35attributes for user one and user two
- 7:05:38Patrick P star
- 7:05:40aol.com our age is still
- 7:05:42zero we can create different objects
- 7:05:45with a varying number of
- 7:05:47arguments let's create a third
- 7:05:50user this time we'll have a parameter
- 7:05:52for our age
- 7:05:54attribute I'm just going to copy this
- 7:05:55cuz I'm lazy we'll have an INT age we
- 7:06:00will assign our age variable
- 7:06:02with the age argument we
- 7:06:04receive we'll create user
- 7:06:063 user user 3 equals new
- 7:06:14user username will be
- 7:06:18Sandy email will be S
- 7:06:22cheeks
- 7:06:24gmail.com then an age let's say
- 7:06:2727 then I'll print user 3's attributes
- 7:06:31user 3. username email then
- 7:06:36age here is all of sy's attributes
- 7:06:40username email
- 7:06:44age let's set up a Constructor that
- 7:06:46accepts no
- 7:06:50arguments this will be kind of an
- 7:06:52anonymous
- 7:06:54profile this. username will equal guest
- 7:06:58or a guest
- 7:07:00profile this . email
- 7:07:03equals not
- 7:07:08provided this. AG equals
- 7:07:12z now we could create a user object then
- 7:07:15pass in no arguments user user 4 equals
- 7:07:19new user this time we don't have any
- 7:07:23arguments then I'll display user Force
- 7:07:29attributes username
- 7:07:38email
- 7:07:40age here's what we got username guessed
- 7:07:43email not provided age is
- 7:07:46zero in a way by creating an object and
- 7:07:49passing in no arguments you can set up
- 7:07:51some default
- 7:07:53values all right everybody those are
- 7:07:55overloaded Constructors they allow a
- 7:07:57class to have multiple Constructors they
- 7:08:01just need different parameter lists they
- 7:08:03enable objects to be initialized in
- 7:08:05various Ways by passing in a varying
- 7:08:08amount of
- 7:08:09arguments this would be great in a
- 7:08:11situation where some fields are optional
- 7:08:14when creating an object and well
- 7:08:16everybody those are overloaded
- 7:08:18Constructors in
- 7:08:21Java hey everybody in this video I'm
- 7:08:23going to show you how we can create an
- 7:08:25array of objects using Java it's pretty
- 7:08:27useful at times and you should probably
- 7:08:28know how to do it we'll Begin by
- 7:08:30creating a new class let's go to file
- 7:08:32new class we will create a car
- 7:08:36class let's say that cars have the
- 7:08:38following
- 7:08:39attributes we'll just pick two to keep
- 7:08:41it simple a string of model and a string
- 7:08:45of color we'll need a car
- 7:08:48Constructor we will pass in a string of
- 7:08:51model and a string of color we will
- 7:08:55assign this. model equals the model we
- 7:08:59receive this. color equals the color
- 7:09:02that we receive we'll add one method
- 7:09:06void drive all cars can drive well I'll
- 7:09:10put the following using a print line
- 7:09:13statement you drive
- 7:09:17the plus this. color plus a space plus
- 7:09:23this. model okay we are done with the
- 7:09:26car
- 7:09:28class we'll construct a few car objects
- 7:09:31let's start with car one car car 1
- 7:09:34equals a new car we have to pass in a
- 7:09:37model and a
- 7:09:38color think of some cars that you like I
- 7:09:42will pick a red Mustang for one car
- 7:09:44let's create Car 2 Car 2 equals a new
- 7:09:48car I will pick a
- 7:09:52Corvette that is blue one more car car 3
- 7:09:57car car 3 equals a new
- 7:10:00car a yellow
- 7:10:05charger all right we have our three car
- 7:10:07objects we can create an array but for
- 7:10:10an array you have to list the data type
- 7:10:12of what you're storing we're going to be
- 7:10:14storing car objects the data type of the
- 7:10:16array is going to be car objects car
- 7:10:20then for an array we need a set of
- 7:10:21straight
- 7:10:22brackets what is the name of this array
- 7:10:24going to be well I think cars would be
- 7:10:27appropriate cars equals if you're
- 7:10:29creating an empty array you're going to
- 7:10:31use the new keyword list the data type
- 7:10:34of
- 7:10:35car within the straight brackets specify
- 7:10:38the size of this array let's say three
- 7:10:40elements Max or we could go ahead and
- 7:10:43just assign these right away too using a
- 7:10:45set of cly braces let's Place car 1 Car
- 7:10:492 and car 3 within this array we'll
- 7:10:52place these three car objects within
- 7:10:54this array of cars now you could use a
- 7:10:57for Loop to iterate through this array
- 7:10:59here's how we'll start with the standard
- 7:11:02for Loop we'll need an index in I equals
- 7:11:06z we'll continue as long as I is less
- 7:11:09than our cars length property then
- 7:11:12increment I by one we'll access our
- 7:11:15array of cars at index of I which is
- 7:11:18going to increase each time but it
- 7:11:19starts at zero we will call the drive
- 7:11:22method and let's see what
- 7:11:27happens you drive the red Mustang you
- 7:11:29drive the blue Corvette you drive the
- 7:11:31yellow
- 7:11:32charger now an enhanced for Loop would
- 7:11:35probably be more appropriate for
- 7:11:39this if you want to use an enhanced for
- 7:11:41Loop type four parentheses curly
- 7:11:44braces what is the data type of each
- 7:11:47element within this array car objects
- 7:11:50we'll need a nickname for each element
- 7:11:52within this array let's say car colon
- 7:11:55think of colon as in then our array of
- 7:11:59cars for every car object in our array
- 7:12:02of cars do the following we'll take each
- 7:12:05car object call the drive
- 7:12:08method and that's going to do the same
- 7:12:10thing you drive the red Mustang you
- 7:12:12drive the blue Corvette you drive the
- 7:12:14yellow
- 7:12:15charger another thing you can do to when
- 7:12:17creating an array of objects you can
- 7:12:20pass in Anonymous objects rather than
- 7:12:22first instantiating the objects then
- 7:12:24assigning them to the array here's how
- 7:12:26you can do
- 7:12:29that for each element we're going to
- 7:12:31call the car Constructor with the new
- 7:12:34keyword call the car Constructor then
- 7:12:36pass in the details for each car the
- 7:12:38necessary arguments we'll pass in our
- 7:12:42Mustang that's red that's the first
- 7:12:44element I'll just copy it to save some
- 7:12:47time then we're creating a new car but
- 7:12:50this will be a
- 7:12:52Corvette that's
- 7:12:54blue and I'll put each of these on a new
- 7:12:56line just for readability
- 7:13:04and then we have our
- 7:13:05charger that's
- 7:13:09yellow and this will do the same thing
- 7:13:13too we have our three
- 7:13:16cars constructing a new object without
- 7:13:18giving the object a unique identifier
- 7:13:20like car 1 Car 2 or car 3 these are
- 7:13:23known as Anonymous
- 7:13:25objects So within an array you can
- 7:13:28create Anonymous objects if you would
- 7:13:30rather not give each of the car is a
- 7:13:31unique identifier another thing you can
- 7:13:33do too with an array of objects is let's
- 7:13:36change the color of each car using our
- 7:13:38enhanced for Loop car do
- 7:13:41color let's change the color to be black
- 7:13:44for each
- 7:13:45car we'll use another enhanced for Loop
- 7:13:48to display each car car die
- 7:13:53drive you drive the black Mustang you
- 7:13:56drive the black Corvette you drive the
- 7:13:58black
- 7:13:59charger all all right everybody those
- 7:14:01are a few things you can do with an
- 7:14:03array of objects in
- 7:14:07Java hello everybody today I'm going to
- 7:14:09explain the static keyword using Java
- 7:14:12static is a keyword it modifies a
- 7:14:14variable or method so that it belongs to
- 7:14:17the class rather than any specific
- 7:14:19object we see the static keyword within
- 7:14:21the main method when you modify a
- 7:14:23variable or method with the static
- 7:14:25keyword static is used to create utility
- 7:14:28methods or other shared resources let me
- 7:14:31give you a demonstration we're going to
- 7:14:32create a new class let's go to file new
- 7:14:36class we'll create a front class we will
- 7:14:39create some friend objects because
- 7:14:41you're
- 7:14:42lonely let's say that all friends will
- 7:14:44have a name attribute string
- 7:14:47name we need a Constructor it's the same
- 7:14:50as the class name parenthesis curly
- 7:14:52braces when we construct a friend object
- 7:14:54we have to pass in a string of name we
- 7:14:57will assign this object's name attribute
- 7:15:01equal to the name parameter that we
- 7:15:03receive let's construct two friends
- 7:15:06we'll say friend friend one equals new
- 7:15:11friend but we have to pass in a name
- 7:15:14pick a name my default is SpongeBob
- 7:15:17because most people know who SpongeBob
- 7:15:19is if I was to Output friend one's name
- 7:15:23then it should be SpongeBob we just want
- 7:15:25to be sure that it
- 7:15:27works yes it does SpongeBob
- 7:15:31now going back to our friend class let's
- 7:15:34say we would like to keep track of how
- 7:15:35many friend objects we create well that
- 7:15:38could be an attribute let's say we have
- 7:15:40an integer of num of friends meaning
- 7:15:45number of friends whenever we create a
- 7:15:47friend object we will increment number
- 7:15:50of friends by one num of friends
- 7:15:54Plus+ so going back let's output friend
- 7:15:59On's number of friends attribute num of
- 7:16:03friends so this should be one yep num
- 7:16:07friends is one we have one friend but
- 7:16:10what if we create a second friend object
- 7:16:12I'll just copy this line of code paste
- 7:16:14it change friend one to friend two we'll
- 7:16:17pass in a new name let's say
- 7:16:19Patrick we'll print friend One's number
- 7:16:22of friends and friend two's number of
- 7:16:24friends how many friend objects have we
- 7:16:27created we have created two but what's
- 7:16:30the out
- 7:16:31put well it's one for
- 7:16:34both here's why each friend object has
- 7:16:38their own copy of the number of friends
- 7:16:40variable we're incrementing each copy of
- 7:16:43number of friends by one when we create
- 7:16:46the object of friend one we're
- 7:16:48incrementing its attribute of number of
- 7:16:50friends by one same thing applies with
- 7:16:52friend two when we create friend two
- 7:16:54friend 2's number of friends is zero and
- 7:16:57we're incrementing it by one what if we
- 7:16:59would like to keep track of the total
- 7:17:01amount of friends that we created and
- 7:17:02store it within one variable well there
- 7:17:05is a way we can do that and that is with
- 7:17:07the static
- 7:17:08modifier so going back to our friend
- 7:17:10class we're going to precede the data
- 7:17:13type of this attribute with the static
- 7:17:15modifier static int number of friends
- 7:17:19rather than all objects all friend
- 7:17:21objects having their own copy of number
- 7:17:23of friends they're all going to share
- 7:17:25one so going back let's print a number
- 7:17:28of friends now
- 7:17:31it's two we have two total friends if I
- 7:17:34was to create a third friend friend
- 7:17:38three friend three will be
- 7:17:44Squidward and then I'll output friend
- 7:17:463's number of
- 7:17:49friends we have three total friends we
- 7:17:52are receiving a suggestion when
- 7:17:54accessing a static variable or calling a
- 7:17:56static method it's actually best to do
- 7:17:59so through the class itself
- 7:18:01rather than any object created from that
- 7:18:03class instead of saying you know friend
- 7:18:05one then accessing the static member we
- 7:18:08will instead use the name of the class
- 7:18:10in this case friend it's better for
- 7:18:12clarity to access a static member by the
- 7:18:15name of the class itself rather than any
- 7:18:17object because you and other developers
- 7:18:19will know that this is a static
- 7:18:21attribute or method to display the
- 7:18:24static variable we would want to access
- 7:18:26it by the class name then access that
- 7:18:27static attribute how many friends do we
- 7:18:30have now three let's create a fourth
- 7:18:34friend friend four will be Sandy then
- 7:18:40display the number of friends we
- 7:18:42have now we have four friends going back
- 7:18:45to the friend class it's as if all these
- 7:18:48objects are sharing the same variable
- 7:18:51but rather than any one object having
- 7:18:53ownership of this variable rather the
- 7:18:55friend class owns it and all objects
- 7:18:58have access to it methods can also be
- 7:19:01modified by the static
- 7:19:03keyword we will create a static method
- 7:19:06that doesn't return anything the return
- 7:19:08type is void this method will be the
- 7:19:11show Friends method we'll just output
- 7:19:14the following we'll output you
- 7:19:18have
- 7:19:20plus normally when you access an
- 7:19:22attribute you type this dot the name of
- 7:19:24the attribute such as you know name if
- 7:19:27you're working with the static attribute
- 7:19:29you don't need this
- 7:19:31for example this. number of friends you
- 7:19:33don't need this we even have a warning
- 7:19:36here that friends. this cannot be
- 7:19:38referenced from a static context if
- 7:19:40you're working with a static attribute
- 7:19:42you don't need this this refers to the
- 7:19:44object you're currently working with but
- 7:19:47since this attribute belongs to the
- 7:19:48class we don't need this you have your
- 7:19:51static attribute of number of friends
- 7:19:54then the words total friends
- 7:20:02to call a static method type the name of
- 7:20:04the class in this case friend dot the
- 7:20:07name of the method show
- 7:20:12friends you have four total friends
- 7:20:15let's create another friend friend
- 7:20:19five friend five will be
- 7:20:23Gary friend. show friends you have five
- 7:20:27total
- 7:20:28friends another place in which you see
- 7:20:30static members is through the math class
- 7:20:33for example let's say you would like to
- 7:20:34round a number you would type the name
- 7:20:36of the math class math. round and you
- 7:20:39can round a
- 7:20:40number round is a utility method we
- 7:20:44access it through the class name of math
- 7:20:47you don't need to go ahead and create a
- 7:20:48math object such as math math 1 equals
- 7:20:52new math and then access it through this
- 7:20:56object that would be
- 7:20:58silly round is a static method it
- 7:21:01behaves as a utility method we can
- 7:21:03access it through the class name and we
- 7:21:04don't need to create any math objects
- 7:21:06because that's stupid and to prove this
- 7:21:09let's go to the math class I'm going to
- 7:21:11hover over math jump to
- 7:21:16Source here's the math class and let's
- 7:21:19find the round method I'm going to do a
- 7:21:21search for
- 7:21:23it here's the round
- 7:21:25method so round is a method and we have
- 7:21:29that static keyword round is a utility
- 7:21:32method for the math class we can access
- 7:21:35it through the name of the class all
- 7:21:37right everybody so that is the static
- 7:21:39keyword it modifies a variable or method
- 7:21:42so that it belongs to the class rather
- 7:21:44than any specific object it's commonly
- 7:21:47used for utility methods or shared
- 7:21:50resources among objects created from
- 7:21:52that class such as the number of friends
- 7:21:55all those friend objects share the same
- 7:21:57attribute rather than each of them
- 7:21:58having their own copy and and well
- 7:22:00everybody that is the static keyword in
- 7:22:03Java what is going on people today we
- 7:22:06got a very important topic to discuss
- 7:22:08today and that is inheritance
- 7:22:10inheritance is where one class inherits
- 7:22:13the attributes and methods from another
- 7:22:15class much like how a child can inherit
- 7:22:18traits from A parent well in programming
- 7:22:20we could do something similar one class
- 7:22:23a child's class can inherit traits from
- 7:22:25A parent here's an example we're going
- 7:22:28to create a few classes but we'll start
- 7:22:30with a new class of animal let's go to
- 7:22:33new class I will create an animal
- 7:22:38class what do animals have let's say
- 7:22:41that all animals are alive they will
- 7:22:44start by being alive is
- 7:22:47alive let's create a Constructor of
- 7:22:51animal I will set is alive to be true
- 7:22:54when we construct a new animal object
- 7:22:57but not only are all animals alive all
- 7:23:00animals can eat they will have an eat
- 7:23:02method void eat we'll print something
- 7:23:05like the
- 7:23:06following the animal is eating easting
- 7:23:12eating that is good enough for this
- 7:23:14class so what we're going to do is
- 7:23:17create two more classes file new class
- 7:23:21we will create a dog
- 7:23:23class and a cat
- 7:23:29class dog and cats are types of animals
- 7:23:33our animal class will be the parent the
- 7:23:35dog and cat class will be its children
- 7:23:38in order for a class to inherit all the
- 7:23:40attributes and methods from another
- 7:23:42class after the class name you will use
- 7:23:44the extend
- 7:23:46keyword then specify the parent class
- 7:23:49dog extends animal our dog class will
- 7:23:53inherit all these traits these
- 7:23:55attributes and methods from the animal
- 7:23:57class the cat and dog class don't have
- 7:24:00any anything within them let's see if
- 7:24:02they actually do have these attributes
- 7:24:03and methods even though there's nothing
- 7:24:05within them let's create a dog object
- 7:24:08dog dog equals new dog and a cat cat cat
- 7:24:13equals new
- 7:24:16cat let's print our dogs is alive
- 7:24:20variable is my dog alive I sure hope
- 7:24:24so cat do is
- 7:24:28alive look at that they're both
- 7:24:31true they should also be able to eat
- 7:24:34dog. eat method cat. eat
- 7:24:41method the animal is eating the animal
- 7:24:43is
- 7:24:45eating even though these classes of dog
- 7:24:47and cat don't have anything within them
- 7:24:49since they extend to the animal class
- 7:24:52we're specifying that the animal class
- 7:24:54is the parent the cat and dog classes
- 7:24:57are the children of the animal class
- 7:24:59they inherit all these attributes and
- 7:25:01methods from its parent of animal even
- 7:25:04though they don't have anything within
- 7:25:05them this isn't as useful if you only
- 7:25:07have a few classes such as two because
- 7:25:10you know you could just copy and paste
- 7:25:12all this code within
- 7:25:14each and then just change the
- 7:25:18Constructor but imagine if you had
- 7:25:20hundreds of classes hundreds of children
- 7:25:22you'd have to do that for hundreds of
- 7:25:24classes but not only that if you have to
- 7:25:26make any changes to say you know this
- 7:25:29animal is eating
- 7:25:31you'd have to make that change for
- 7:25:32hundreds of classes and we are repeating
- 7:25:34ourselves a lot it'd be a lot easier if
- 7:25:37we can just make that change in one
- 7:25:40place if we have to change the to this
- 7:25:43all of the children classes will inherit
- 7:25:45this updated method we're following the
- 7:25:48dry principle don't repeat yourself
- 7:25:50we're writing the code once and just
- 7:25:52reusing it all the children classes will
- 7:25:56inherit this updated method but not only
- 7:25:59that each child class can have their own
- 7:26:01unique attributes and methods too let's
- 7:26:04say that all dogs will have one life int
- 7:26:08lives equals 1 they will also have their
- 7:26:12own unique speak method void
- 7:26:16speak when our dog uses the speak method
- 7:26:19let's output the following let's say the
- 7:26:21dog goes
- 7:26:25woof then within the cat
- 7:26:28class cats will have have nine
- 7:26:32lives and they can speak but they will
- 7:26:35say something different when they speak
- 7:26:37let's output the cat goes
- 7:26:43meow going
- 7:26:46back let's output our dog objects lives
- 7:26:50how many lives does our dog have same
- 7:26:53thing applies with our cat cat.
- 7:26:57lives our dog has one life our cat has
- 7:27:00nine
- 7:27:01lives they also have their own unique
- 7:27:03speak methods dog
- 7:27:06dope cat
- 7:27:11dope the dog goes woof the cat goes
- 7:27:15meow there's also the concept of
- 7:27:17multi-level inheritance we'll create a
- 7:27:19new class to serve as a sort of
- 7:27:23grandparent a child class will inherit
- 7:27:25from a parent a parent will inherit from
- 7:27:28a grandparent so let's change this
- 7:27:30program around a little bit we're going
- 7:27:32to create a new class file new Java
- 7:27:35class we will create a class of
- 7:27:41organism animals will extend the
- 7:27:45organism
- 7:27:47class let's cut our is aive variable and
- 7:27:50the
- 7:27:52Constructor if you inherit from the
- 7:27:54animal class you will get an eat
- 7:27:57method if you inherit from the organism
- 7:28:00class you are alive and that's it but we
- 7:28:03need to change the
- 7:28:05Constructor animal inherits from
- 7:28:08organism dog and cat inherit from the
- 7:28:10animal class so from the perspective of
- 7:28:13our dog and cat class the organism class
- 7:28:16is you could say a grandparent going
- 7:28:19back to our main Java file let's take a
- 7:28:22look at our is alive variables dog. is
- 7:28:26alive same thing with our cat
- 7:28:30cat dot is
- 7:28:33alive yes our dog is alive and our cat
- 7:28:36is alive let's take this a step
- 7:28:39further let's create a class of plant
- 7:28:42plant is going to inherit from the
- 7:28:44organism class because plants are a type
- 7:28:46of organism but they're not related to
- 7:28:49animals let's create a new class of
- 7:28:58plant let's say that if you're a plant
- 7:29:00you get a method to well plants don't
- 7:29:04eat but they can
- 7:29:05photosynthesize
- 7:29:07photosynthesize I think I spelled that
- 7:29:09right probably not but who
- 7:29:12cares if you're a plant and you
- 7:29:14photosynthesize then the
- 7:29:18plant absorbs
- 7:29:22sunlight hey this is bro from the future
- 7:29:24I forgot to extend the organism class so
- 7:29:28let's take care of that heading back to
- 7:29:31our main Java file let's create a plant
- 7:29:34object plant plant equals new
- 7:29:40plant our plant object should have an is
- 7:29:43alive attribute is alive which it
- 7:29:47inherits from the organism class that's
- 7:29:50true and plants can
- 7:29:52photosynthesize plant
- 7:29:55do photosynthesize
- 7:30:02the plant absorbs
- 7:30:04sunlight it's kind of like we have a
- 7:30:07whole family tree dog and cat inherit
- 7:30:09from animal animal inherits from
- 7:30:11organism and plant inherits from
- 7:30:14organism but animals and plants aren't
- 7:30:17related they're siblings but they're not
- 7:30:20the same going back to our main Java
- 7:30:22file let's see if our dog has a
- 7:30:24photosynthesize method dog.
- 7:30:28photosynthesize
- 7:30:35looks like they can't we do not have a
- 7:30:37photosynthesize method for dogs they do
- 7:30:40not inherit this method because they're
- 7:30:42not related to
- 7:30:44plants all right everybody so that's
- 7:30:46inheritance it's where one class
- 7:30:48inherits the attributes and methods from
- 7:30:50another class A Child class will inherit
- 7:30:53all the attributes and methods from a
- 7:30:55parent class parents can also be
- 7:30:57considered children too and inherit it
- 7:31:00from a let's say grandparent class you
- 7:31:03just have to use the extends keyword and
- 7:31:06well everybody that's inheritance in
- 7:31:08Java yo so let's get started today I got
- 7:31:12to discuss the super keyword super just
- 7:31:15refers to the parent class when using
- 7:31:17inheritance a child class is also known
- 7:31:19as the sub class the parent class is
- 7:31:22also known as the super class super just
- 7:31:25means parent for all intents and
- 7:31:27purposes the super keyword is used
- 7:31:29within Constructors and Method
- 7:31:31overwriting method overwriting is a
- 7:31:33whole another topic that we'll discuss
- 7:31:35we're more focused on Constructors in
- 7:31:36this topic we use the super keyword to
- 7:31:39call the parent Constructor to
- 7:31:41initialize attributes let me give you an
- 7:31:43example we'll be creating a few
- 7:31:45different classes let's go to file new
- 7:31:49class we will Begin by creating a class
- 7:31:52of
- 7:31:53person person will be the parent the
- 7:31:56super
- 7:31:58class let's say that any person object
- 7:32:01has the following attributes they will
- 7:32:03have a string of first meaning first
- 7:32:06name then string last meaning last name
- 7:32:10we'll need a Constructor it's going to
- 7:32:12be the same as the class name Person
- 7:32:15parentheses curly braces when we
- 7:32:17construct a person object we will need
- 7:32:19to pass in the following arguments a
- 7:32:22string for first name first and a string
- 7:32:25for last name last this objects first
- 7:32:30name attribute this. first
- 7:32:33equals the argument that we receive for
- 7:32:36first same thing applies with last this.
- 7:32:40last equals the argument we receive for
- 7:32:43last and then we'll create one method in
- 7:32:46this demonstration this method won't
- 7:32:48return anything we will name the method
- 7:32:51show
- 7:32:53name all we'll do is output the
- 7:32:57following this do first plus a space
- 7:33:02plus this. last okay let's test this
- 7:33:06class
- 7:33:07out person person equals new
- 7:33:12person but we do have to pass in a first
- 7:33:14and last name I've already used
- 7:33:16SpongeBob for too many examples so let's
- 7:33:18mix it up this time I'll pick Harry
- 7:33:20Potter or feel free to pick a different
- 7:33:22franchise that you like so one person
- 7:33:25from Harry Potter is
- 7:33:27Tom Riddle
- 7:33:31we have a person object with this first
- 7:33:33name and this last name first
- 7:33:38last our person is going to have a show
- 7:33:41name method show name then call
- 7:33:45it there we go there's our person
- 7:33:47objects first and last name Tom
- 7:33:51Riddle our person class will be the
- 7:33:54parent class we'll create a few other
- 7:33:56classes to inherit from this person
- 7:33:58class we're going to create a class of
- 7:34:00student that's going to inherit from the
- 7:34:03person class the student class will
- 7:34:05inherit these attributes and
- 7:34:08methods let's create a new class of
- 7:34:13student student
- 7:34:16will extends the parent or super class
- 7:34:21of
- 7:34:22person since student is inheriting from
- 7:34:25person student is going to have a first
- 7:34:27and last name already and a show name
- 7:34:31method what other attributes does a
- 7:34:33student have that a person might not
- 7:34:36well students have a GPA a grade point
- 7:34:38average we'll declare that as an
- 7:34:40attribute double
- 7:34:42GPA we're going to create a student
- 7:34:46Constructor let's say that when we
- 7:34:48create a student we need to pass in a
- 7:34:51string for first name String
- 7:34:54first string
- 7:34:56last and double GPA what's their grade
- 7:35:00point average now rather than assigning
- 7:35:02our first and last name within this
- 7:35:04Constructor such as this. first equals
- 7:35:08first this. last equals
- 7:35:12last and this. GPA equals GPA we are
- 7:35:18receiving a
- 7:35:20warning there is no parameterless
- 7:35:23Constructor available in person we're
- 7:35:25not able to assign these attributes of
- 7:35:27first and last within the Constructor
- 7:35:29for student this is because student
- 7:35:32inherits from the person class since the
- 7:35:34Constructor of our parent requires a
- 7:35:37first and last name we have to pass
- 7:35:40these arguments to its parent of person
- 7:35:42how we can do that going back to the
- 7:35:44child class we're going to be using the
- 7:35:47super keyword any arguments that the
- 7:35:49parent requires we have to send the
- 7:35:52parent these arguments from the child
- 7:35:55Constructor and how we can do that is
- 7:35:57use the super keyword
- 7:36:01and then pass in those arguments of
- 7:36:03first and last and now Java is
- 7:36:07Happy super just refers to the parent
- 7:36:11imagine that we replace the super
- 7:36:13keyword with
- 7:36:15person think of it like that and now
- 7:36:18this should
- 7:36:19work going back to our main file we
- 7:36:23should be able to create a student
- 7:36:24object student student equals new
- 7:36:28student
- 7:36:31we have to pass in a first name first
- 7:36:33name will be
- 7:36:34Harry last name will be
- 7:36:36Potter then we have to pass in a GPA
- 7:36:39because our student Constructor requires
- 7:36:42it Harry Potter has a
- 7:36:453.25 GPA let's say our student object
- 7:36:50should have these attributes this
- 7:36:53method and this attribute from the
- 7:36:56student class of GPA
- 7:36:59Let's test it student. show
- 7:37:03name this student's name is Harry
- 7:37:07Potter and they should have a
- 7:37:10GPA let's print students GPA
- 7:37:15attribute
- 7:37:173.25 but not only that let's create a
- 7:37:19method to display their GPA within the
- 7:37:22student class we will create a method
- 7:37:24that doesn't return anything it will be
- 7:37:27show GPA
- 7:37:30we will output the
- 7:37:31following let's say this.
- 7:37:35first plus a string apostrophe s so
- 7:37:40Harry's
- 7:37:42GPA
- 7:37:45is plus this.
- 7:37:51GPA let's take our student object call
- 7:37:54the show GPA method that we have
- 7:37:57declared
- 7:38:00Harry's GPA is
- 7:38:043.25 let's create one more class we will
- 7:38:06create an employee
- 7:38:09class class
- 7:38:14employee employee will inherit from the
- 7:38:17person
- 7:38:18class employee extends
- 7:38:25person since employees aren't studying
- 7:38:27they're not students they instead get a
- 7:38:30salary we will create an integer
- 7:38:32attribute of
- 7:38:35salary and then we need that Constructor
- 7:38:37for our employee
- 7:38:40object all employees will require the
- 7:38:43following
- 7:38:44arguments a string for first name String
- 7:38:48first a string for last name String last
- 7:38:52and an integer for our employee salary I
- 7:38:56will attempt to assign these attributes
- 7:38:58for our employee again it's not going to
- 7:39:00work but let's try it this. first equals
- 7:39:04first this. last equals
- 7:39:08last this do
- 7:39:10salary equals
- 7:39:13salary again we're receiving that
- 7:39:15warning there is no parameter list
- 7:39:18Constructor available in
- 7:39:20person since employee extends person and
- 7:39:23our person Constructor requires a first
- 7:39:25name and a last name we have to send our
- 7:39:27person Constructor these arguments first
- 7:39:30and last so again going back to our
- 7:39:32employee
- 7:39:33class we will call the Constructor of
- 7:39:36our parent using the super keyword and
- 7:39:39then passing in those arguments first
- 7:39:41and
- 7:39:42last let's create a method within our
- 7:39:44employee class void Show
- 7:39:50salary let's output the following we'll
- 7:39:53say this. first this person's first
- 7:39:57name apostrophe yes we'll create the
- 7:40:01employee of Hagrid their
- 7:40:04salary is pick unit of currency I'll
- 7:40:07pick American dollars plus this do
- 7:40:11salary now we'll need to construct an
- 7:40:13employee
- 7:40:17object
- 7:40:18employee
- 7:40:20employee equals new
- 7:40:23employee we have to pass in a first name
- 7:40:25a last name and a salary in order to
- 7:40:28construct an employee object we're going
- 7:40:30to pick Hagrid Hagrid's first name is
- 7:40:33actually
- 7:40:35rubius last name
- 7:40:40Hagrid Hagrid's salary will be
- 7:40:44$50,000 let's
- 7:40:46say all right let's take our
- 7:40:49employee call its Show salary
- 7:40:55method and there we go rubius ALS also
- 7:40:59known as Hagrid rubi's salary is
- 7:41:03$50,000 in conclusion super refers to
- 7:41:06the parent class student and employee
- 7:41:09inherit from person since the
- 7:41:11Constructor of the parent of person
- 7:41:13requires a first name and a last name
- 7:41:16any children objects that we create we
- 7:41:19have to call the Constructor of the
- 7:41:20parent and pass in those arguments to
- 7:41:23satisfy Java if the parent didn't
- 7:41:25require these
- 7:41:27arguments then you wouldn't need to
- 7:41:34and well everybody that is the super
- 7:41:35keyword in Java what's going on people
- 7:41:39today I'm going to discuss method
- 7:41:40overwriting using Java method
- 7:41:42overwriting is when a subass provides
- 7:41:44its own implementation of a method
- 7:41:46rather than use one that's inherited
- 7:41:47from a parent it allows for code
- 7:41:49reusability and you can give specific
- 7:41:52implementations of a method let me give
- 7:41:54you a demonstration we'll be creating a
- 7:41:55few classes file new class we will Begin
- 7:41:59by creating a class of
- 7:42:02animal then a new
- 7:42:04class of
- 7:42:07dog a
- 7:42:10class of
- 7:42:13cat then finally a class of
- 7:42:18fish dog cat and fish are going to
- 7:42:21extend the animal class after the class
- 7:42:24name type extends
- 7:42:27animal so let's do the this with cat and
- 7:42:32fish the dog cat and fish classes are
- 7:42:35all children of the animal class animal
- 7:42:38class is the parent class there's not a
- 7:42:41lot we have to write with this example
- 7:42:42if a child class inherits from the
- 7:42:44animal parent class they will inherit a
- 7:42:47method to move let's define a method of
- 7:42:50move all we'll say is this animal is
- 7:42:57running we can keep the dog cat and fish
- 7:43:00class is empty for now so going back to
- 7:43:03our main Java file let's construct a dog
- 7:43:06object dog dog equals new dog a cat
- 7:43:09object cat cat equals new cat and a fish
- 7:43:15fish fish equals new
- 7:43:18fish all three of these animal objects
- 7:43:21dog cat and fish should have a move
- 7:43:24method let's take our dog call its move
- 7:43:27method say same thing with the cat and
- 7:43:30the
- 7:43:31fish cat. move method fish. move method
- 7:43:35currently when we run this program we
- 7:43:37get the following
- 7:43:39output our dog is running our cat is
- 7:43:42running and our fish is running but fish
- 7:43:46don't have legs though they can only
- 7:43:49swim the move method that the fish class
- 7:43:52inherits isn't appropriate for that
- 7:43:54class this animal is swimming would be
- 7:43:56more appropriate so why don't we do this
- 7:43:59within the fish class why don't we do
- 7:44:01some method overwriting specifically for
- 7:44:03the fish class we will create a unique
- 7:44:05move method only for this class we will
- 7:44:08Define a new move
- 7:44:11method where we will
- 7:44:14say this animal is
- 7:44:21swimming however it is good practice to
- 7:44:23add this annotation of at override so
- 7:44:27that you and other Developers know that
- 7:44:29this method is being overridden our fish
- 7:44:32class does inherit a move method from
- 7:44:34its parent of animal if you have the
- 7:44:37same named method defined in a child
- 7:44:39class you'll use that one first it has
- 7:44:42precedence going back to our main Java
- 7:44:45file let's call the move method for our
- 7:44:48fish our dog is running our cat is
- 7:44:51running but our fish is swimming within
- 7:44:55the fish class we have overridden the
- 7:44:57move method we have given a specific
- 7:45:00implementation for this method just for
- 7:45:02this one class the cat and dog classes
- 7:45:05will use what's inherited from their
- 7:45:07parent not only is a good practice to
- 7:45:10add this annotation of at override but
- 7:45:12let's say that you misspell this
- 7:45:14overwritten method rather than move
- 7:45:16let's say moves even though it should be
- 7:45:19move we're receiving a warning that this
- 7:45:22method does not override the method from
- 7:45:24its super class it's parent if you
- 7:45:27didn't have this annotation you may not
- 7:45:29notice that you misspelled this method
- 7:45:31and you're not actually overwriting
- 7:45:34it adding the at override annotation
- 7:45:37provides a system of checks and balances
- 7:45:40when overwriting method this error will
- 7:45:42go away once we actually are overwriting
- 7:45:44a
- 7:45:45method all right everybody so that is
- 7:45:47Method overwriting it's when a subass
- 7:45:49provides its own implementation of a
- 7:45:51method rather than use one that's
- 7:45:53already defined it allows for code
- 7:45:55reusability and you can give specific
- 7:45:58implementations of a method we'll be
- 7:45:59seeing method overwriting again with the
- 7:46:01two string method coming up in a future
- 7:46:04topic and well everybody that is method
- 7:46:06overwriting in Java hello everybody
- 7:46:09today I got to explain the two- string
- 7:46:11method using Java the two- string method
- 7:46:13is inherited from the object class
- 7:46:15whenever you create an object behind the
- 7:46:18scenes the object class is a super class
- 7:46:21the two-ring method is used to return a
- 7:46:24string representation of an object if
- 7:46:26you were to Output it directly using
- 7:46:28system.out print line the default
- 7:46:30behavior of the two- string method is
- 7:46:32that it returns a hash code as a unique
- 7:46:34identifier for that object but it can be
- 7:46:36overridden to provide meaningful details
- 7:46:39which is why we've learned about method
- 7:46:40overriding in the last video in this
- 7:46:43demonstration let's create a new class
- 7:46:45file new class we will create a car
- 7:46:49class let's say that all cars should
- 7:46:51have the following attributes a string
- 7:46:54of make who manufactures the car a
- 7:46:57string of model what's the model of the
- 7:46:59car int year for the year and string
- 7:47:03color for the color we'll need a
- 7:47:06Constructor and then we'll assign these
- 7:47:08attributes we have a string of make a
- 7:47:13string of model an INT of year and a
- 7:47:18string of color then we'll assign these
- 7:47:22attributes this St make equals make this
- 7:47:26St model equals model
- 7:47:30this.e equals
- 7:47:33year this. color equals
- 7:47:37color that looks good enough for now
- 7:47:40going back to our main Java file we'll
- 7:47:42construct a car object car car equals
- 7:47:45new car but we have to pass in the
- 7:47:48details for our car Constructor our car
- 7:47:51Constructor requires a make a model a
- 7:47:53year and a color so pick a car that you
- 7:47:56like I like Ford Mustang I'll pick a
- 7:47:59Ford Mustang the make will be Ford Model
- 7:48:03will be
- 7:48:04Mustang I need a year 2025 and a color
- 7:48:08I'll pick the color
- 7:48:10red car is an object if I were to Output
- 7:48:13my car object directly using
- 7:48:15system.out.print line here's what you'll
- 7:48:20see if you output your card directly
- 7:48:23technically you'll be given a hash code
- 7:48:25a unique identifier for that car object
- 7:48:28hashing is kind of an advanced topic
- 7:48:30that I don't want to introduce right now
- 7:48:32but typically with hashing it's a unique
- 7:48:34identifier that uses the object's memory
- 7:48:36address to calculate a hash you don't
- 7:48:38need to know any more than that right
- 7:48:39now but wouldn't it be useful if we were
- 7:48:42to print our car object directly we were
- 7:48:44given the details of the car normally
- 7:48:46what we would have to do is output
- 7:48:49something like this this is if we wanted
- 7:48:51to Output the details of the car we
- 7:48:54could say something such as car. color
- 7:48:58that would would be first plus a space
- 7:49:00character plus car do which should be
- 7:49:03next let's say the year color year then
- 7:49:07make and
- 7:49:09model car. make plus a
- 7:49:13space plus car.
- 7:49:16model this print line statement is
- 7:49:19giving us meaningful details of this car
- 7:49:22object this is the
- 7:49:24output red 2025 Ford Mustang it would be
- 7:49:28nice if we were to print our car object
- 7:49:30directly using print or print line we
- 7:49:33could be given these details instead
- 7:49:35well we can do that with method
- 7:49:36overwriting going to our car
- 7:49:39class we will be overriding a method we
- 7:49:42need the at override
- 7:49:45annotation we do need this keyword of
- 7:49:48public this is a publicly accessible
- 7:49:51method the return type is string the
- 7:49:54name of the method is to string
- 7:50:00everything within this print line
- 7:50:02statement we're going to cut and delete
- 7:50:05this print line
- 7:50:07statement going to our car class within
- 7:50:09the two string method we have to return
- 7:50:12a string because we set the return type
- 7:50:14to be a string we promised that we would
- 7:50:16return a string let's paste all the
- 7:50:19details of that car we'll need to
- 7:50:21replace car with
- 7:50:23this this refers to the object we're
- 7:50:26currently working with that and add a
- 7:50:28semicolon to the end if we were to print
- 7:50:32our car object directly using this print
- 7:50:34line statement or a print statement or
- 7:50:36even printf instead of that hash
- 7:50:40code we're instead given meaningful
- 7:50:42details of that car that we've
- 7:50:44customized the cars color year make and
- 7:50:49model we have overridden the two- string
- 7:50:51method that's normally available to all
- 7:50:55objects let's construct another car
- 7:50:57object let's rename car is car one we'll
- 7:51:00have car car 2 equals new
- 7:51:04car pick some different details for a
- 7:51:06car I will pick a
- 7:51:10Chevrolet
- 7:51:12Corvette the year will be
- 7:51:142026 and the color will be blue I'll
- 7:51:18print car
- 7:51:19one then print Car
- 7:51:242 here's car one and car two Car 2 is a
- 7:51:29blue 2026 Chevrolet
- 7:51:32Corvette all right everybody so that is
- 7:51:34the two-ring method objects inherit from
- 7:51:37the object class the object class does
- 7:51:40have a two string method but it can be
- 7:51:42overridden so that when you print an
- 7:51:44object directly you can display
- 7:51:46meaningful details such as the details
- 7:51:48of that object instead of displaying a
- 7:51:50hash code if you were to print the
- 7:51:52object it's pretty useful at times and
- 7:51:55well everybody that is the two string
- 7:51:57method in Java
- 7:51:59all right everybody it's about time we
- 7:52:00get into abstraction overall abstraction
- 7:52:03is the process of hiding implementation
- 7:52:06details and showing only the essential
- 7:52:08features think of it like this if I'm
- 7:52:10teaching somebody how to drive I'm not
- 7:52:12going to open up the hood of the car and
- 7:52:14explain how the engine works I'm instead
- 7:52:16going to tell the user that the gas
- 7:52:18pedal is to drive and the brake is to
- 7:52:20stop the car abstraction is the process
- 7:52:22of hiding implementation details a user
- 7:52:25just needs to know what the gas pedal is
- 7:52:27and what the brake is we can make
- 7:52:29classes and methods abstract by using
- 7:52:31this abstract keyword this provides a
- 7:52:33few benefits abstract classes can't be
- 7:52:37instantiated directly abstract classes
- 7:52:39can also contain abstract methods which
- 7:52:41must be implemented they can also
- 7:52:43contain concrete methods which are the
- 7:52:46opposite these are real and physical
- 7:52:47methods you could say concrete methods
- 7:52:49are inherited so I know that's a lot let
- 7:52:52me give you an example we're going to be
- 7:52:54creating an abstract class that can't be
- 7:52:56instantiated directly meaning we can't
- 7:52:58create any objects from this class in
- 7:53:01this demonstration we're going to create
- 7:53:02some shape objects shape is going to be
- 7:53:04the parent then we'll have circle
- 7:53:07triangle and rectangle classes as its
- 7:53:09children so we're going to go to new
- 7:53:12Java class we will create a shape
- 7:53:16class then a class for
- 7:53:23Circle a class for triangle
- 7:53:31then a class for
- 7:53:37rectangle our shape class is going to be
- 7:53:40abstract we can't create any shape
- 7:53:42objects before the word of class we will
- 7:53:44add this keyword of abstract we can't
- 7:53:47create any shape objects it's an
- 7:53:49abstract class now circle triangle and
- 7:53:52rectangle are going to extend the shape
- 7:53:54class Circle extends shape circles are
- 7:53:58type of shape same thing applies with
- 7:54:01triangle and
- 7:54:03rectangle let's attempt to create a
- 7:54:05shape object shape shape equals new
- 7:54:11shape and we get an error message shape
- 7:54:14is abstract it cannot be
- 7:54:17instantiated since shape is an abstract
- 7:54:19class we can't create any objects from
- 7:54:22this class it adds a little bit of
- 7:54:23security to our program we don't want
- 7:54:26anybody creating any generic shape
- 7:54:27objects
- 7:54:29we want them to create a certain kind of
- 7:54:30shape a circle a triangle or a rectangle
- 7:54:34but we can create some circles triangles
- 7:54:36and rectangles so let's do so circle
- 7:54:39circle equals new
- 7:54:43circle triangle
- 7:54:47triangle equals new
- 7:54:51triangle
- 7:54:52rectangle
- 7:54:54rectangle equals new rectangle
- 7:55:01we can create circle triangle and
- 7:55:03rectangle objects just not shape because
- 7:55:06it's an abstract
- 7:55:07class that's good though because we
- 7:55:09don't want people creating any shape
- 7:55:11objects it's too generic we want a user
- 7:55:13to pick a certain kind of shape abstract
- 7:55:16classes can contain abstract methods and
- 7:55:19concrete methods an abstract method is a
- 7:55:23method which must be implemented by its
- 7:55:25children within our shape class
- 7:55:28we will define an abstract method use
- 7:55:31the abstract keyword let's list a return
- 7:55:34type of double every child class of the
- 7:55:38shape class needs an area method I'll
- 7:55:41add a comment that this is an abstract
- 7:55:44method
- 7:55:46abstract So within our Circle class we
- 7:55:49get an error
- 7:55:50message Circle must either be declared
- 7:55:53abstract or implement the abstract
- 7:55:55method of area circle is going to
- 7:55:58inherit this abstract method of area
- 7:56:00from its parent of shape we need to
- 7:56:02implement this method so let's do
- 7:56:05so this is going to be an overridden
- 7:56:08method we need The annotation of
- 7:56:11override we need to define a method of
- 7:56:16area but we'll fill it in momentarily
- 7:56:19just for now I'm going to return zero
- 7:56:22and now that error message goes away
- 7:56:24since we inherit from the shape class
- 7:56:26and there's an abstract method of area
- 7:56:28we need to implement this method this
- 7:56:30helps to ensure consistency among the
- 7:56:32children classes you can see that we're
- 7:56:34missing that same method with triangle
- 7:56:36and rectangle so why don't we copy this
- 7:56:38code where we override the area method
- 7:56:41and paste
- 7:56:42it we'll be filling these in
- 7:56:46later going back to our shape class
- 7:56:49abstract classes can contain abstract
- 7:56:51methods and concrete methods which are
- 7:56:54kind of the opposite concrete methods
- 7:56:57are defined and inherited within an
- 7:56:59abstract class let's define a method of
- 7:57:04display this method doesn't return
- 7:57:06anything the return type is void we'll
- 7:57:08just output the
- 7:57:09following this is a shape I'll add a
- 7:57:13comment that this is a concrete method
- 7:57:17in an abstract class a concrete method
- 7:57:19is
- 7:57:20inherited within our circle triangle and
- 7:57:23rectangle classes we don't need to
- 7:57:25implement it we don't need to override
- 7:57:27this method it's
- 7:57:29inherited going back to our main Java
- 7:57:32file if I was to take my circle object
- 7:57:35call the display method we get the
- 7:57:38following
- 7:57:40output this is a shape and that applies
- 7:57:43with triangle and rectangle to triangle.
- 7:57:48display rectangle.
- 7:57:55display this is a shape this is a shape
- 7:57:57this is a a
- 7:58:00shape this concrete method of display
- 7:58:02was inherited from the shape class their
- 7:58:05parent now we'll Define the area methods
- 7:58:08area is an abstract method that means
- 7:58:10that the children classes have to Define
- 7:58:12it to calculate the area of a circle
- 7:58:15we'll need a radius we'll Define that as
- 7:58:16an attribute all circles will have a
- 7:58:19radius attribute Double Radius and we'll
- 7:58:23need a Constructor a circle Constructor
- 7:58:26when we construct a circle object we
- 7:58:29have to pass in a double for the
- 7:58:32radius this. rius equals the radius we
- 7:58:37receive now the formula to calculate the
- 7:58:40area of a circle will be we're going to
- 7:58:42return access the math class access the
- 7:58:46constant of Pi from the math class times
- 7:58:51radius time
- 7:58:55radius that's how to calculate the area
- 7:58:57of a circle
- 7:58:58now for our triangle we need the
- 7:59:02following a double for base a double for
- 7:59:07height we need a
- 7:59:13Constructor parameter 1 will be double
- 7:59:16base parameter 2 will be double
- 7:59:19height this.
- 7:59:22base equals
- 7:59:24base this do height equals height
- 7:59:29for the area we will
- 7:59:32return
- 7:59:350.5 time base time
- 7:59:39height then we have a
- 7:59:44rectangle double
- 7:59:47length double
- 7:59:49width we need our Constructor of
- 7:59:53rectangle parameter 1 will be double
- 7:59:56length parameter 2 will be double
- 8:00:00width this do length equals
- 8:00:04length this. width equals
- 8:00:09width
- 8:00:11return length time width within the area
- 8:00:15method and we're good let's go back to
- 8:00:18our main class we're missing those
- 8:00:20arguments we have to pass them in for
- 8:00:23our Circle object let's say the radius
- 8:00:25is three for the triangle the base and
- 8:00:26height will be 4 and 5 the rectangle the
- 8:00:30length and width will be 6 and
- 8:00:327 we are going to Output using
- 8:00:35system.out.print line take our circle
- 8:00:37called the area
- 8:00:39method then do this with triangle and
- 8:00:47rectangle here's the area of each of our
- 8:00:49shapes our Circle our triangle and a
- 8:00:53rectangle so that's the abstract keyword
- 8:00:55abstraction is the process of hiding
- 8:00:57implementation details from a user and
- 8:01:00showing only the essential features
- 8:01:02that's why we've created a shape class
- 8:01:03that's abstract we can't directly create
- 8:01:06any shape objects we want a user to
- 8:01:08create a certain kind of shape whether
- 8:01:10it's a circle a triangle or a rectangle
- 8:01:12a shape is too generic an abstract class
- 8:01:15can contain abstract methods which need
- 8:01:17to be implemented by the children
- 8:01:19classes or concrete methods which are
- 8:01:21inherited and the children classes will
- 8:01:24have access to it all right everybody so
- 8:01:26that is abstraction and the of the
- 8:01:28abstract keyword in Java all right
- 8:01:31what's going on everybody in this video
- 8:01:32I got to explain interfaces using Java
- 8:01:35an interface is very similar to an
- 8:01:37abstract class in fact there's a lot of
- 8:01:39overlap where you could use either one
- 8:01:41but there's a few key differences an
- 8:01:42interface is a blueprint for a class it
- 8:01:45specifies a set of abstract methods that
- 8:01:47any implementing classes must Define one
- 8:01:50key difference is that by using
- 8:01:52interfaces we can achieve multiple
- 8:01:54inheritance like Behavior normally with
- 8:01:56inheritance a class can only have a
- 8:01:59single parent but by using interfaces a
- 8:02:01class can have multiple parents like two
- 8:02:04three or more so in this demonstration
- 8:02:06we're going to create two interfaces
- 8:02:09let's go to file new Java class rather
- 8:02:13than creating a class we're going to
- 8:02:14create an interface we will create an
- 8:02:17interface of prey and the other for
- 8:02:20predator file new Java class
- 8:02:24interface
- 8:02:26Predator we're going to be declaring
- 8:02:28some methods but not defining them if a
- 8:02:31class inherits from the prey interface
- 8:02:34they will have to finish defining a
- 8:02:36method of flea the return type will be
- 8:02:39void if you're considered prey you need
- 8:02:41a flea method to run away if you're a
- 8:02:45predator you need to define a method to
- 8:02:48hunt cuz you're a predator now we'll be
- 8:02:50creating three classes file new Java
- 8:02:54class the first class will be rabbit
- 8:03:00then fish we'll create a fish
- 8:03:02class and
- 8:03:08Hawk going to our rabbit class rabbits
- 8:03:11are typically considered prey not
- 8:03:14Predators unless you count that one
- 8:03:15rabbit from Monty Python and the Holy
- 8:03:17Grail that's the exception our rabbit
- 8:03:19class is going to
- 8:03:21implements the prey interface we get a
- 8:03:24warning message class rabbit must either
- 8:03:27declare abstract or Implement abstract
- 8:03:30method flee since the rabbit class is
- 8:03:33implementing the prey interface we have
- 8:03:35to finish defining this method that's
- 8:03:38declared within the prey interface it's
- 8:03:40kind of like a contract the prey
- 8:03:42interface is telling the rabbit class
- 8:03:44hey if you're going to implement me you
- 8:03:45need to Define this method a flea we'll
- 8:03:48need to do that in order to create rabid
- 8:03:49objects we're going to be overwriting
- 8:03:52this method using the override
- 8:03:54annotation this method will be flee void
- 8:03:58flee and we'll want to add this axis
- 8:04:01modifier of public it's a publicly
- 8:04:03accessible method in this method we're
- 8:04:06going to Output the following let's say
- 8:04:10the rabbit is running
- 8:04:14away and that error should go away let's
- 8:04:17go back to our main class we'll create a
- 8:04:19rabbit object and test it rabbit rabbit
- 8:04:23equals new rabbit let's take our rabbit
- 8:04:26object have it call the flea
- 8:04:29method and this should
- 8:04:32work yes the rabbit is running away now
- 8:04:36let's go to our Hawk class Hawk will
- 8:04:40implement the Predator
- 8:04:43interface but we have to override this
- 8:04:45method of hunt because with The Predator
- 8:04:48interface it's kind of like we're
- 8:04:50signing a contract if Hawk is going to
- 8:04:52implement the Predator interface then we
- 8:04:55have to finish defining this method we
- 8:04:57will add the at override
- 8:05:00annotation and Define this method and
- 8:05:03then again we do need an axis modifier
- 8:05:06we'll pick
- 8:05:07public let's output the
- 8:05:10following the hawk is
- 8:05:13hunting let's create a hawk
- 8:05:15object Hawk Hawk equals New
- 8:05:21Hawk our rabbit is fleeing let's take
- 8:05:23our Hawk object use the hunt method
- 8:05:29okay we have the rabbit is running away
- 8:05:31the hawk is
- 8:05:33hunting now rabbits don't have a hunt
- 8:05:35method and Hawks don't have a flea
- 8:05:41method now one key difference with
- 8:05:43interfaces compared to abstract classes
- 8:05:46is that you can Implement more than one
- 8:05:48interface so fish they eat smaller fish
- 8:05:51and they flee larger fish they could be
- 8:05:53considered both prey and
- 8:05:55predators we will implement two
- 8:05:58interfaces Implement prey comma Predator
- 8:06:03then we need both of these methods we
- 8:06:04have to finish defining both again we're
- 8:06:07going to use the at override
- 8:06:10annotation we will create a publicly
- 8:06:12accessible method a flea where we will
- 8:06:16print the fish is
- 8:06:19swimming away and we need a hunt method
- 8:06:26too add the at override annotation this
- 8:06:30is a public method of hunt where we will
- 8:06:35print the fish is hunting let's say all
- 8:06:41right let's test it we will create a
- 8:06:43fish object fish fish equals new
- 8:06:48fish our fish can
- 8:06:51flee and our fish can
- 8:06:56hunt the fish is swimming away the fish
- 8:06:59is hunting our fish object implements
- 8:07:02both the prey and Predator interfaces it
- 8:07:04has to Define these two methods so
- 8:07:07that's one key difference with
- 8:07:08interfaces compared to abstract classes
- 8:07:10a class can inherit more than one
- 8:07:12interface normally with inheritance you
- 8:07:15can only have one parent but this is a
- 8:07:17way around that using interfaces fish
- 8:07:19are considered both prey and predators
- 8:07:21all right everybody so that is a quick
- 8:07:23introduction to interfaces an interface
- 8:07:25is a blueprint for a class that's
- 8:07:27specifies a set of abstract methods that
- 8:07:29implementing classes must Define and it
- 8:07:32supports multiple inheritance like
- 8:07:34Behavior there's a lot of overlap where
- 8:07:36you can use abstract classes or
- 8:07:37interfaces you should be familiar with
- 8:07:39both and well everybody that's a quick
- 8:07:41introduction to interfaces using Java so
- 8:07:45yeah it's about time I explain
- 8:07:46polymorphism using Java polymorphism is
- 8:07:49a Greek word poly means many morph means
- 8:07:52shape together you get the concept of
- 8:07:55many shapes or forms it's a general
- 8:07:58programming concept objects can identify
- 8:08:00as other objects objects of different
- 8:08:03types objects can be treated as objects
- 8:08:06of a common super Class A dog identifies
- 8:08:09as a dog but it can also identify as an
- 8:08:11animal it can also identify as an
- 8:08:13organism it can also identify as an
- 8:08:15object it can identify as more than one
- 8:08:18thing in this topic we're going to
- 8:08:19create a few vehicle objects let's
- 8:08:21create a few classes file new Java class
- 8:08:25we will create a super class of vehicle
- 8:08:28iicle this class doesn't have to be
- 8:08:30abstract but let's make it abstract we
- 8:08:32can't create any vehicle
- 8:08:34objects we will declare an abstract
- 8:08:37method that doesn't return anything the
- 8:08:40return type is void if you're a vehicle
- 8:08:42you need a go method so what we're going
- 8:08:45to be doing is creating a few different
- 8:08:47types of vehicles a car bike and boat
- 8:08:50class which will inherit from the class
- 8:08:52of vehicle so let's create these classes
- 8:08:54file new Java class we will create a car
- 8:08:57class that extends the vehicle class but
- 8:09:02there is an abstract method of go that
- 8:09:04we need to
- 8:09:06Define we will use the at override
- 8:09:10annotation the method will be return
- 8:09:13type of void go all we'll do is output
- 8:09:17you drive the car all right let's do
- 8:09:21this with a few other classes I'll just
- 8:09:24copy this method because we'll reuse it
- 8:09:28we'll create a bike class
- 8:09:31bike bike extends
- 8:09:35vehicle and we need that method of go
- 8:09:39you ride the
- 8:09:42bike and a boat class file new Java
- 8:09:45class
- 8:09:48Boat Boat extends
- 8:09:54vehicle you sail the boat
- 8:09:59that is good enough for now going back
- 8:10:01to our main Java file let's create these
- 8:10:04objects starting with our car car car
- 8:10:07equals new
- 8:10:09car bike bike equals new
- 8:10:13bike Boat Boat equals new
- 8:10:18boat each of these objects has a go
- 8:10:21method for example let's have our car
- 8:10:23object use its go method followed by
- 8:10:26bike bike Dogo method and Boat Boat Dogo
- 8:10:33method you drive the car you ride the
- 8:10:35bike you sail the boat so let's say we
- 8:10:38would like to have a
- 8:10:40race we're going to place all of these
- 8:10:42objects within an array our car bike and
- 8:10:45boat objects so what should the data
- 8:10:48type of the array be let's attempt to
- 8:10:51create an array of car objects and place
- 8:10:53our bike and boat objects within that
- 8:10:55array and we'll just see what happens
- 8:10:57exactly for learning purposes I will
- 8:10:59create an array of
- 8:11:01cars the data type is car it's an array
- 8:11:05named cars equals curly braces let's
- 8:11:09stick our car bike and boat objects
- 8:11:12within here we're already getting a few
- 8:11:14error
- 8:11:17messages incompatible types bike cannot
- 8:11:20be converted to car bikes and boats
- 8:11:24don't identify as cars cars identify as
- 8:11:27cars s that's one of the shapes that
- 8:11:29bikes and boats don't have they don't
- 8:11:31identify as cars if this were an array
- 8:11:33of
- 8:11:34bikes bike bikes equals R array of
- 8:11:41objects well then we have another a
- 8:11:43message car cannot be converted to a
- 8:11:46bike and you can assume the same thing
- 8:11:48applies with
- 8:11:50boats cars and bikes don't identify as
- 8:11:53boats what we could do is declare our
- 8:11:56array of what they have in common well
- 8:11:58our car bike and boat they all extend
- 8:12:01the vehicle class you could say they
- 8:12:03also identify as Vehicles we're going to
- 8:12:06change the data type of our array to
- 8:12:07hold Vehicles we have an array of
- 8:12:11vehicle
- 8:12:12objects and then let's change our array
- 8:12:14name to be Vehicles cars identifies cars
- 8:12:18but also Vehicles bikes identifies bikes
- 8:12:21but also Vehicles boats identify as
- 8:12:24boats but also Vehicles that's the one
- 8:12:26thing they have in common they're all
- 8:12:29extending the vehicle super class
- 8:12:31they're also considered Vehicles that's
- 8:12:34another one of their forms they shapes
- 8:12:36for every object within this array I
- 8:12:38would like to have each object use its
- 8:12:40go method like we're
- 8:12:42racing well what we could do is use a
- 8:12:45for each
- 8:12:48Loop what's the data type of the array
- 8:12:51well it's vehicle we have an array of
- 8:12:54vehicles we will give a nickname of each
- 8:12:56vehicle
- 8:12:58as vehicle in my array of
- 8:13:01vehicles for every vehicle in this array
- 8:13:05of vehicles let's take each
- 8:13:07vehicle call the go method take every
- 8:13:10vehicle object call its go method here's
- 8:13:13the result you drive the car you ride
- 8:13:16the bike you sail the
- 8:13:19boat oh and another thing I'm forgetting
- 8:13:22polymorphism can also be achieved
- 8:13:24through using interfaces let's delete
- 8:13:26our vehicle
- 8:13:30class we'll create a new Java class this
- 8:13:34will be an interface a
- 8:13:38vehicle rather than extending the
- 8:13:41vehicle class we will implements the
- 8:13:44vehicle
- 8:13:48interface we will declare a method of go
- 8:13:52that doesn't return
- 8:13:53anything these methods do need an access
- 8:13:56modifier let's just say
- 8:14:04public so polymorphism can be achieved
- 8:14:07this way too through using interfaces if
- 8:14:10you prefer interfaces in summary
- 8:14:12polymorphism means many shapes it's
- 8:14:15where objects can identify as themselves
- 8:14:17but also other objects objects can be
- 8:14:20treated as objects of a common super
- 8:14:22class and well everybody that is an
- 8:14:24introduction to polymorphism using jav
- 8:14:27Ava what's going on people today I got
- 8:14:29to explain a runtime polymorphism also
- 8:14:32known as Dynamic polymorphism how I
- 8:14:34would explain it in a single sentence is
- 8:14:37when the method that gets executed is
- 8:14:39decided at runtime based on the actual
- 8:14:42type of the object in this demonstration
- 8:14:45a user is going to pick a type of animal
- 8:14:47they want as a pet a dog or a cat but
- 8:14:50that's going to be determined at runtime
- 8:14:52after the program is already running
- 8:14:54we'll create a total of three classes
- 8:14:56first we'll create an animal class which
- 8:14:59will be a parent class we will create a
- 8:15:01class of animal we might as well make
- 8:15:04this an abstract class for good practice
- 8:15:07we don't want anybody creating any
- 8:15:08animal objects we will have an abstract
- 8:15:11method the return type is void this
- 8:15:14method will be speak when an animal
- 8:15:17speaks they'll say something
- 8:15:19unique we'll create a dog class file new
- 8:15:23Java class we will create a class of dog
- 8:15:27that
- 8:15:28extends the animal class but we do need
- 8:15:31a speak method we will use method
- 8:15:34overwriting the return type is void with
- 8:15:37our speak
- 8:15:38method we will output the
- 8:15:41following the dog goes woof all right
- 8:15:47then we need a cat class file new Java
- 8:15:50class we will create a class of
- 8:15:54cat cat extends
- 8:15:58animal and we'll just copy the speak
- 8:16:00method just to save a little bit of
- 8:16:03time when the cat speaks we'll output
- 8:16:07the cat goes meow that's good enough for
- 8:16:10this
- 8:16:12example since animal is an abstract
- 8:16:15class we can't create any animal objects
- 8:16:17I'll attempt to do so animal animal
- 8:16:20equals new
- 8:16:23animal and we get an error message
- 8:16:25animal is abstract can be instantiated
- 8:16:28that's good though animals are too
- 8:16:30generic we would rather have a user
- 8:16:32create a certain kind of animal either a
- 8:16:34dog or a cat in this demonstration we're
- 8:16:37going to have a user pick if they want a
- 8:16:39dog or a cat but we don't know which one
- 8:16:41they're going to pick so why don't we do
- 8:16:43this let's declare an animal object but
- 8:16:47not instantiate it we will assign our
- 8:16:49animal equal to a new dog or a new cat
- 8:16:52based on the user input so animal animal
- 8:16:56this will either be animal equals a new
- 8:16:59dog or animal equals a new cat based on
- 8:17:04the user
- 8:17:06input we will declare an object that's
- 8:17:08at least an animal we'll accept some
- 8:17:10user input we'll need a scanner scanner
- 8:17:13scanner equals new
- 8:17:16scanner we will pass in
- 8:17:19system.in and import this class
- 8:17:22java.util.scanner
- 8:17:25we'll need a prompt
- 8:17:31would you like a dog or a
- 8:17:36cat we'll say 1 equals dog 2 equals
- 8:17:43cat I'll use print rather than print
- 8:17:46line because I like the input on the
- 8:17:47same line let's declare a variable of
- 8:17:50choice this will be an integer we'll use
- 8:17:52our scanner called the next int method
- 8:17:58and then we'll do the
- 8:17:59following we'll examine if our choice
- 8:18:03variable is equal to one if it is we'll
- 8:18:06finish instantiating our animal object
- 8:18:09but we'll assign it to be a new dog
- 8:18:12object then we will have our animal use
- 8:18:15its speak
- 8:18:17method so if it's a dog it should go
- 8:18:23woof else if
- 8:18:29choice is equal to two then we will
- 8:18:32instantiate our animal object as a new C
- 8:18:36and have our animal use its speak
- 8:18:42method you could add an else Clause but
- 8:18:45it's not really
- 8:18:46necessary this is good enough for
- 8:18:49now before running the program we don't
- 8:18:52know what kind of animal we're going to
- 8:18:53create it's either going to be a dog or
- 8:18:55a cat
- 8:19:00if I select one we get a dog the dog
- 8:19:02goes
- 8:19:03woof if I select two we get a cat the
- 8:19:06cat goes meow the actual method that we
- 8:19:09call is determined at runtime after the
- 8:19:12program is already
- 8:19:13running before we run the program we
- 8:19:16know that we're going to use a speak
- 8:19:17method we're just not sure which one so
- 8:19:20that's runtime polymorphism it's when
- 8:19:22the method that gets executed is decided
- 8:19:25at runtime based on the actual type of
- 8:19:28object that we create and well everybody
- 8:19:30that is an introduction to runtime
- 8:19:32polymorphism using
- 8:19:34Java all right everybody today I'm going
- 8:19:36to be explaining getter and Setter
- 8:19:38methods using Java getter and Setter
- 8:19:40methods help protect object data and
- 8:19:42they add rules for accessing or
- 8:19:44modifying that data here's a
- 8:19:46demonstration we're going to create a
- 8:19:48class of car file new class we will
- 8:19:52create a car
- 8:19:53Class A few attributes that cars could
- 8:19:55have include a string of model A String
- 8:20:00of color and an INT let's say price then
- 8:20:05we'll need a
- 8:20:08Constructor we'll have three
- 8:20:10parameters a string of model A String of
- 8:20:14color and an inch of price we will
- 8:20:18assign this do
- 8:20:20model equals
- 8:20:22model this do color equals color
- 8:20:27this St price equals price let's
- 8:20:31construct a car
- 8:20:34object car car equals new
- 8:20:38car pick a model a year and a price I've
- 8:20:42already used red Mustangs for too many
- 8:20:44examples I'll pick something else let's
- 8:20:46say a
- 8:20:48charger that is yellow and the price
- 8:20:51will be
- 8:20:52$10,000 but I don't know if that's good
- 8:20:54or not we don't even know the year of
- 8:20:56the car anyways so we have our car
- 8:20:58object and then let's output these
- 8:21:00attributes just to test them car. color
- 8:21:03I'll add a space character to plus car.
- 8:21:07model plus a space
- 8:21:10character plus car. price I'm
- 8:21:13intentionally not adding a unit of
- 8:21:16currency so here's our
- 8:21:19car I have a yellow charger and the
- 8:21:22price is 10,000 10,000 of a unit of
- 8:21:24currency that you choose but that's not
- 8:21:26important
- 8:21:27with our car object its attributes are
- 8:21:30publicly accessible meaning we can view
- 8:21:32and change them
- 8:21:33easily to demonstrate let's say that the
- 8:21:36model of our car is now a
- 8:21:43Corvette we have a yellow Corvette that
- 8:21:46is
- 8:21:46$10,000 we don't want the model of our
- 8:21:49car to magically become a Corvette once
- 8:21:51we assign its model to be charger we
- 8:21:53don't want it to change what we could do
- 8:21:56when to declaring these attributes is
- 8:21:58add this access modifier of private
- 8:22:00preceding the data type private string
- 8:22:03model private string color private and
- 8:22:07price we can't access them and let's
- 8:22:10test that you can see that the font
- 8:22:12color already changed to
- 8:22:15Red I will attempt to print the car's
- 8:22:17color model and
- 8:22:21price color has private access and car
- 8:22:25model has private ACC access in car and
- 8:22:28price has private access in
- 8:22:30car since these attributes are private
- 8:22:33we can't normally access them outside of
- 8:22:35the car class but there is a way around
- 8:22:37that and that is by using getter and
- 8:22:39Setter methods getter methods make a
- 8:22:42field readable Setter methods make a
- 8:22:45field writable use get to read set to
- 8:22:49write we'll set up some getter methods
- 8:22:52first I would like the car's color model
- 8:22:55and price
- 8:22:57here's how we can do that within the car
- 8:22:59class we will create a method we'll
- 8:23:02start with the model the return type
- 8:23:04will be string because our model is a
- 8:23:06string data type we'll create a getter
- 8:23:08method following this naming convention
- 8:23:11get then the name of the attribute
- 8:23:14model all we're going to do is return
- 8:23:18this.
- 8:23:20model and then we'll do this with the
- 8:23:22color and the price and I'll just copy
- 8:23:25this method because I'm I'm feeling a
- 8:23:26little lazy we have get
- 8:23:29model get color return this.
- 8:23:33color and get
- 8:23:37price return this.
- 8:23:39price oh and this returns an integer not
- 8:23:42a string because our price is an integer
- 8:23:46let's go back to our main Java file
- 8:23:48rather than accessing these attributes
- 8:23:51directly because we can't we're going to
- 8:23:54call these getter methods get model if
- 8:23:56you need the model get color if you need
- 8:23:58the color get price if you need the
- 8:24:01price replace car. color with car do get
- 8:24:05color and this is a method car. getet
- 8:24:11model and car. getet price and that
- 8:24:16should
- 8:24:18work yep we can now read those
- 8:24:21attributes we have a yellow charger and
- 8:24:23the price is
- 8:24:2510,000 another thing you can do too with
- 8:24:27getter methods you can add additional
- 8:24:29logic so with our get price method let's
- 8:24:32instead return a string we will return a
- 8:24:36unit of currency I'll pick American
- 8:24:38dollars plus the price let's see how
- 8:24:41that
- 8:24:42changes again we are calling the get
- 8:24:44price
- 8:24:47method there we go yellow charger
- 8:24:52$110,000 getter methods make a field
- 8:24:55readable and you can add additional
- 8:24:57logic when retrieving one of these
- 8:24:59attributes and in this demonstration I
- 8:25:01just added a dollar sign just to keep it
- 8:25:03simple then we have Setter methods
- 8:25:05Setter methods make a field writable
- 8:25:09going to our car class we have model
- 8:25:11color and price I don't want the model
- 8:25:14attribute to be writable once we've
- 8:25:16declared the model of our car we don't
- 8:25:18want to change it our charger can't
- 8:25:20magically become a Corvette even though
- 8:25:22that'd be pretty cool but the color you
- 8:25:24could change you can paint your car and
- 8:25:26the price you can sell your car for a
- 8:25:28different price we'll declare Setter
- 8:25:30methods for our color and price but not
- 8:25:32the model because we don't want this
- 8:25:34attribute to be
- 8:25:35writable so let's scroll down to the
- 8:25:38bottom following a similar pattern we're
- 8:25:40going to create a method the return type
- 8:25:43is void this will be set
- 8:25:47color we have one parameter a string of
- 8:25:52color we will assign this do color equal
- 8:25:56to the new color that we
- 8:25:59receive we'll do this with our price
- 8:26:02too void set
- 8:26:06price we'll have one parameter and int
- 8:26:10of
- 8:26:11price this. price equals price the new
- 8:26:15price that we receive okay let's test
- 8:26:18these
- 8:26:22things I'll attempt to access our color
- 8:26:25directly car. color equals I don't know
- 8:26:29blue that's the first thing that came to
- 8:26:31mind and car. price equals you know what
- 8:26:36the car's on sale it's
- 8:26:38$5,000 well I can't change these we
- 8:26:40still have that red text for color and
- 8:26:44price because the color and price
- 8:26:46attributes are still
- 8:26:48private instead we're going to call
- 8:26:50these Setter
- 8:26:52methods we will set the color and then
- 8:26:55pass in the new
- 8:26:57color rather than assign it directly set
- 8:27:00color pass in a new
- 8:27:03color set price passing a new price
- 8:27:075,000 then using our Getters we should
- 8:27:09be able to print these
- 8:27:12attributes yes we now have a blue
- 8:27:14charger and the price is
- 8:27:17$5,000 we don't have a Setter method set
- 8:27:19up for the model that's good though we
- 8:27:22don't want the model to
- 8:27:24change we only have set
- 8:27:26and set price if I attempt to access a
- 8:27:29set model
- 8:27:31method we shouldn't be able to change
- 8:27:36it because we never declared a set model
- 8:27:38method it doesn't exist but if you did
- 8:27:41want to change the model if you want
- 8:27:43this attribute to be writable well then
- 8:27:46you could add a Setter
- 8:27:47method void set
- 8:27:50model we'll pass in a string for the new
- 8:27:54model this model equals
- 8:27:59model and then you could change
- 8:28:02it we have a blue corvet for $5,000
- 8:28:05which is really
- 8:28:07cheap but you know what I don't want the
- 8:28:09model to be writable just
- 8:28:12readable if you don't want an attribute
- 8:28:14to be writable when you declare it you
- 8:28:16can also add this keyword a final that
- 8:28:18adds an extra security
- 8:28:22measure one last thing we should do with
- 8:28:25the price let's add some additional
- 8:28:27logic so with our price let's do the
- 8:28:29following let's add a
- 8:28:31check if our price is less than zero
- 8:28:36then let's output the
- 8:28:38following price can't be less than
- 8:28:44zero else if everything checks out if
- 8:28:47our price is zero or above then we'll
- 8:28:49assign this do price equals the price
- 8:28:52that we receive as an argument Let's
- 8:28:55test it
- 8:28:56I'll change the color of my car to be
- 8:28:58blue but the price is going to
- 8:29:04be100 we get that message price can't be
- 8:29:07less than zero the color changed but not
- 8:29:10the price it stayed the same at
- 8:29:13$10,000 so those are getter and Setter
- 8:29:15methods they help protect object data
- 8:29:18and add rules for accessing or modifying
- 8:29:20that data getter methods make a field
- 8:29:23readable Setter methods make a a field
- 8:29:26writable depending on your object's
- 8:29:28attributes you may want some to be
- 8:29:30readable or writable or both it's up to
- 8:29:34you and well everybody those are getter
- 8:29:36and Setter methods using Java all right
- 8:29:39let's do this today I got to talk about
- 8:29:41aggregation in programming aggregation
- 8:29:44represents a has a relationship between
- 8:29:47objects one object contains another
- 8:29:49object as part of its structure but the
- 8:29:52contained object or objects can exist
- 8:29:54independently B basically speaking an
- 8:29:57object can contain another object but
- 8:30:00those objects can exist independently
- 8:30:02what we'll do for this demonstration is
- 8:30:04create some book objects then we'll
- 8:30:05create a library object to contain the
- 8:30:08book objects the books and the library
- 8:30:10can exist independently that's
- 8:30:12aggregation the library object will have
- 8:30:15book objects we'll create a new class of
- 8:30:18book file new class
- 8:30:22book and a class for Library file new
- 8:30:26class
- 8:30:29Library let's design our book
- 8:30:31objects let's say that books have a
- 8:30:34string of title and an integer for Pages
- 8:30:38you could include an author too if you
- 8:30:39would like but I don't want this example
- 8:30:41to be too big we'll need a Constructor
- 8:30:43for our book
- 8:30:44objects we'll set up the parameters we
- 8:30:47need a string of title and an integer
- 8:30:50for
- 8:30:51Pages this. tile equals title
- 8:30:57this. Pages equals Pages if you would
- 8:31:00like you can set these attributes to be
- 8:31:02private but that might be overkill for
- 8:31:04this lesson why don't we create a method
- 8:31:06to display the book's info this method
- 8:31:08will return a string we'll Define a
- 8:31:10method to display info we will display
- 8:31:14the info of this book Let's return the
- 8:31:17following let's return this.
- 8:31:21tile plus a space character
- 8:31:26plus this.
- 8:31:29pages and a string of the word
- 8:31:35pages and I think I'm going to enclose
- 8:31:38the number of pages within a set of
- 8:31:40parenthesis you can also use a print F
- 8:31:43statement if that's easier for you but I
- 8:31:45want to try and keep this beginner
- 8:31:46friendly okay that's good enough for the
- 8:31:48book class let's create a few book
- 8:31:51objects we'll start with book one book
- 8:31:54book one equals a new book but to
- 8:31:58construct a book we have to pass in at
- 8:32:00least the following a title and a number
- 8:32:03of pages so I've picked out a few books
- 8:32:06already feel free to choose your own if
- 8:32:08you would rather choose different books
- 8:32:09for the first book that I'll pick I will
- 8:32:11pick the Fellowship of the Ring this
- 8:32:14will be all one
- 8:32:16string and the number of pages at least
- 8:32:19according to wekipedia is
- 8:32:21423 then we'll create another book
- 8:32:24another book object this will be book
- 8:32:27two book two will be the two towers the
- 8:32:32number of pages will be
- 8:32:35352 and then book
- 8:32:38three book three will be the Return of
- 8:32:43the
- 8:32:45King the number of pages will be
- 8:32:48416 all right we have our independent
- 8:32:50book objects then what we'll do is stick
- 8:32:53them all within an array we'll create an
- 8:32:55array of book objects named books equals
- 8:33:00then within a set of curly braces we can
- 8:33:02place our book objects within here book
- 8:33:04one book two book three we have an array
- 8:33:08of book objects before moving on let's
- 8:33:11test the display info method of each of
- 8:33:13these books so we'll need a print
- 8:33:16statement book one. display
- 8:33:21info so we should get book one The
- 8:33:24Fellowship of the Ring 423 Pages I'm
- 8:33:27just going to make one
- 8:33:30change I'll put the right parentheses at
- 8:33:32the
- 8:33:36end that's
- 8:33:38better let's display book
- 8:33:41two the two towers 352
- 8:33:44Pages book
- 8:33:47three the Return of the King 46
- 8:33:52Pages you could also Loop through this
- 8:33:54array using an enhanced for for
- 8:33:57Loop the data type of each element in my
- 8:34:00array is book for every book in my array
- 8:34:03of
- 8:34:04books will print take each book call the
- 8:34:09display info
- 8:34:13method and here's all the books that I
- 8:34:15have in my array The Fellowship of the
- 8:34:18Ring The Two Towers The Return of the
- 8:34:20King and we can delete this for Loop we
- 8:34:23no longer need it
- 8:34:26now we're going to create a library
- 8:34:27object our library object will contain
- 8:34:30our array of book objects because
- 8:34:32libraries contain books going to our
- 8:34:34library
- 8:34:36class what should libraries have let's
- 8:34:39say that our library should have a name
- 8:34:41what's the name of the library maybe a
- 8:34:43year in which the library was
- 8:34:45established in year and libraries should
- 8:34:48contain book objects we will create an
- 8:34:51array of book objects named
- 8:34:54books we'll need to instructor for the
- 8:34:56library
- 8:34:57class we'll need the following a string
- 8:35:00for the name of the
- 8:35:02library an integer for the year of the
- 8:35:04library when was the library built and
- 8:35:08an array of book
- 8:35:10objects this St name equals
- 8:35:13name this doe equals
- 8:35:18year this. books equals the books that
- 8:35:23we receive this array
- 8:35:26all right going back to our main Java
- 8:35:27file this is where aggregation is going
- 8:35:30to come
- 8:35:31in we will create a library
- 8:35:34object
- 8:35:36library
- 8:35:37library equals new
- 8:35:41library but we have to pass in the
- 8:35:43following as arguments to the
- 8:35:45Constructor a name for the library a
- 8:35:48year for the library and an array of
- 8:35:51book
- 8:35:52objects so for the library I'm going to
- 8:35:55say the NYC the New York
- 8:35:58City public
- 8:36:01library after a quick Google search this
- 8:36:04library was built in the year
- 8:36:081897 then we also need an array of book
- 8:36:10objects I will pass in my array of book
- 8:36:13objects named
- 8:36:15books this is
- 8:36:18aggregation our library object has book
- 8:36:22objects within the library class let's
- 8:36:24also create a display info method for
- 8:36:27the library we'll make it a little bit
- 8:36:29different this method won't return
- 8:36:31anything let's say the return type is
- 8:36:34void we'll name this method display info
- 8:36:38we'll display the following information
- 8:36:40about this Library let's output the
- 8:36:44following
- 8:36:45the this.e of the
- 8:36:49library plus a space
- 8:36:51character plus
- 8:36:54this.name you can also also use a printf
- 8:36:56statement if you would prefer to use
- 8:36:58that I'm trying to keep this as beginner
- 8:37:00friendly as possible so let's test
- 8:37:03this we will take our library object
- 8:37:07call it display info
- 8:37:13method the
- 8:37:141897 New York City public
- 8:37:18library then within this method of
- 8:37:20display info for the library let's also
- 8:37:23list all the books that it has this
- 8:37:25Library
- 8:37:26has aggregated a whole bunch of book
- 8:37:28objects so then we can display them or
- 8:37:30use them for something let's output the
- 8:37:33following let's output
- 8:37:37books
- 8:37:39available then we'll use an enhanced for
- 8:37:42Loop the data type of what we're
- 8:37:44iterating through is book objects for
- 8:37:48every book in my array of books take
- 8:37:52each book call the books display in
- 8:37:57method and then we do have to print it
- 8:37:59using a print line statement because
- 8:38:02this method returns a
- 8:38:05string there we go let's test
- 8:38:07it so after calling the display info
- 8:38:10method of the
- 8:38:12library we should display the
- 8:38:14information of the library including its
- 8:38:16books the 1897 New York City public
- 8:38:19library and we display the books
- 8:38:21available which are the array of books
- 8:38:23that we have created
- 8:38:25so that's aggregation it represents a
- 8:38:28has a relationship between objects our
- 8:38:31library object has book objects within
- 8:38:34it there's another similar concept
- 8:38:36called composition with aggregation if
- 8:38:39you were to delete this Library class
- 8:38:41for example I'll delete
- 8:38:45it well these book objects can exist
- 8:38:47independently outside of the library the
- 8:38:50books aren't built into the library so
- 8:38:53they can exist independently which is a
- 8:38:55key compared to composition which is the
- 8:38:57next topic we'll cover and well
- 8:38:59everybody that is aggregation using Java
- 8:39:02hey everybody in this video I'm going to
- 8:39:04explain composition to you in
- 8:39:05programming composition represents a
- 8:39:08part of relationship between objects for
- 8:39:11example an engine is part of a car an
- 8:39:15object can be part of another object
- 8:39:18this allows for complex objects to be
- 8:39:20constructed from smaller objects in this
- 8:39:23demonstration we're going to create a
- 8:39:24car object
- 8:39:26the car object is composed of an engine
- 8:39:28object we will create a class of car and
- 8:39:32a class of
- 8:39:38engine we'll begin with our class of car
- 8:39:41what sorts of attributes should cars
- 8:39:43have let's say a string of
- 8:39:46model an INT of year and an engine
- 8:39:51object engine engine
- 8:39:55we'll need a
- 8:39:57Constructor we'll pass in a string for
- 8:40:00the
- 8:40:00model an INT for the
- 8:40:04year now for the engine we're not going
- 8:40:06to be passing in an engine object let's
- 8:40:08say that when we construct a car object
- 8:40:11we have to pass in a string for the
- 8:40:14engine
- 8:40:15type is it an inline engine is it a V6
- 8:40:19that sort of thing we will assign this
- 8:40:22St model equals the model that we
- 8:40:24receive
- 8:40:26this. year equals the year that we
- 8:40:28receive now with our engine this. engine
- 8:40:33we're going to call the Constructor for
- 8:40:35a new engine
- 8:40:37object and then we can pass in our
- 8:40:39engine
- 8:40:40type but we still however have to set up
- 8:40:43the Constructor for our engine class
- 8:40:46when constructing a car object we will
- 8:40:48also be constructing a new engine object
- 8:40:51so let's go to our engine
- 8:40:53class let's say that engine
- 8:40:55have a
- 8:40:56string of type you could say engine type
- 8:41:02but that would be redundant I would say
- 8:41:04let's just say type then we need a
- 8:41:07Constructor for our engine class we will
- 8:41:11need a string of type to pass
- 8:41:14in this.type equals
- 8:41:19type so going back to our car class when
- 8:41:22we pass in a string for the engine type
- 8:41:25you could say that this is
- 8:41:27type this term is kind of ambiguous
- 8:41:29though because a user is going to think
- 8:41:31that the type is for the car type rather
- 8:41:33than the engine type so when you pass
- 8:41:36arguments your parameter names can be
- 8:41:39different from the argument names that
- 8:41:41you pass in we simply rename them when
- 8:41:43this Constructor receives that argument
- 8:41:46but type refers to the engine type all
- 8:41:49right so this is a basic demonstration
- 8:41:51of composition now we'll construct a car
- 8:41:53object car car equals new
- 8:41:56car and then we have to pass in the
- 8:41:59following a string for the model an inch
- 8:42:01for the year and a string for the engine
- 8:42:04type pick a card that you like this time
- 8:42:06I will pick a
- 8:42:08Corvette the year will be
- 8:42:1120125 and for the engine I will pick a
- 8:42:14V8
- 8:42:16engine then let's see if we have these
- 8:42:19details I'm going to Output my car
- 8:42:22objects model and these attributes I did
- 8:42:26not set to private but you can set them
- 8:42:28to private and use Getters and Setters
- 8:42:30if you would like so we have
- 8:42:34model
- 8:42:38year and we have an engine
- 8:42:43object here's what we have we have our
- 8:42:46model Corvette year 2025 now for the
- 8:42:50engine our engine is an object it's a
- 8:42:52reference data type so if you you output
- 8:42:55it directly you're given a hash ID to
- 8:42:58represent that engine object so if we
- 8:43:00need the type of engine we're going to
- 8:43:03access our engine object then get the
- 8:43:05type so following our engine object we
- 8:43:08will use that access modifier that
- 8:43:11dot then access the type the
- 8:43:16attribute so the engine type is a
- 8:43:19V8 so it's like we have an object that's
- 8:43:22composed with another object our n
- 8:43:25is part of our car you know what let's
- 8:43:28create a method for the engine we'll
- 8:43:30create a start
- 8:43:31method void
- 8:43:34start we'll output the following let's
- 8:43:37say using a print line
- 8:43:39statement you start
- 8:43:42the plus this. type meaning type of the
- 8:43:47engine plus the word engine then going
- 8:43:50to our car class let's create a start
- 8:43:52method for the car class
- 8:43:55void
- 8:43:57start when we start the car we also
- 8:43:59start the engine let's take this.
- 8:44:03engine call it start
- 8:44:07method and then we can output something
- 8:44:09else too if we would like let's
- 8:44:12output the plus this do
- 8:44:17model plus the words is
- 8:44:22running now if we start the car let's
- 8:44:25take our car call it start
- 8:44:28method and we should get the following
- 8:44:31you start the V8 engine the Corvette is
- 8:44:34running now a key difference with
- 8:44:37composition is that if we delete our car
- 8:44:40object that should also delete our
- 8:44:43engine cuz our engine is part of our car
- 8:44:47so to demonstrate that I'm going to
- 8:44:50delete our car object
- 8:44:55and we no longer have access to the
- 8:44:59engine all right everybody that is
- 8:45:01composition it represents a part of
- 8:45:03relationship between objects for example
- 8:45:06an engine is part of a car this allows
- 8:45:09for complex objects to be constructed
- 8:45:12from smaller objects and well everybody
- 8:45:14that is composition using Java well look
- 8:45:18who showed up today I got to talk about
- 8:45:20rapper classes rapper classes allow
- 8:45:22primitive values you know such as as
- 8:45:25integers characters doubles booleans
- 8:45:27those sorts of things rapper classes
- 8:45:29allow these primitive values to be
- 8:45:31treated as objects or basically wrapping
- 8:45:33primitive values within an object kind
- 8:45:36of like a Christmas present generally
- 8:45:38you don't need to wrap Primitives unless
- 8:45:40you need an object for some reason like
- 8:45:42if you're using a collections framework
- 8:45:44in Java and rapper classes also give you
- 8:45:46access to a lot of useful static utility
- 8:45:48methods so let me give you a
- 8:45:50demonstration of rapper
- 8:45:52classes the method of creating rapper
- 8:45:54classes I'm about about to show you is
- 8:45:55actually depreciated but it's going to
- 8:45:57help you visualize how this works
- 8:45:59wrapping A Primitive within an object
- 8:46:02the modern approach is using a technique
- 8:46:04called autoboxing but the method I'm
- 8:46:06about to show you is a good way to
- 8:46:07visualize this whole thing so let's say
- 8:46:09we have an INT of a variable a a equal
- 8:46:15123 we can treat this primitive as an
- 8:46:19object by using a wrapper class here's
- 8:46:21how rather than declaring this variable
- 8:46:24as an INT
- 8:46:25we will use the rapper class of integer
- 8:46:29integer variable a equals a new integer
- 8:46:35and then we're going to pass in that
- 8:46:37number that integer as an argument a is
- 8:46:40an object we're passing in that
- 8:46:42primitive data type that int as an
- 8:46:45argument to the integer class so a is an
- 8:46:48object but it does contain A Primitive
- 8:46:50value again this is depreciated as a
- 8:46:53version 9 so you shouldn't do it this
- 8:46:55way but this is a good way to visualize
- 8:46:57it then if you need a double we'll use
- 8:47:00the wrapper class of double let's say
- 8:47:02variable b equals a new double then
- 8:47:06we'll pass in a double 3.14 is
- 8:47:09good then we have char which is
- 8:47:13character that's the rapper class
- 8:47:15character C equals a new character then
- 8:47:19pass in a character I'll pass in a
- 8:47:22dollar
- 8:47:23sign We'll add one more let's say a
- 8:47:26Boolean we will use the wrapper class of
- 8:47:28Boolean Boolean D equals a new boan then
- 8:47:34pass in a Boolean
- 8:47:35value here's just a few examples these
- 8:47:38are all objects a b c and d by wrapping
- 8:47:42these Primitives within an object that
- 8:47:44allows us to use these primitive values
- 8:47:47within collections Frameworks such as
- 8:47:49array lists which we'll talk about in
- 8:47:50the next topic the modern way to use
- 8:47:53rapper classes is actually just to
- 8:47:55assign them directly to their primitive
- 8:47:56values the class of integer a equals
- 8:48:01123 doubleb equal
- 8:48:043.14 character C equals a dollar sign
- 8:48:08character and then D equals
- 8:48:11true this technique is called
- 8:48:16autoboxing We're directly assigning
- 8:48:18these Primitives into an object using a
- 8:48:21rapper class now this is very similar to
- 8:48:24how we declare and assign strings to
- 8:48:26create a string you would say string
- 8:48:29let's say E equals a string of
- 8:48:32characters such as the word Pizza you
- 8:48:36can see that there's a similar pattern
- 8:48:37we have always created strings this way
- 8:48:40but with autoboxing it's a similar
- 8:48:42process these are all reference data
- 8:48:44types they're a type of
- 8:48:46object there's also a technique called
- 8:48:49unboxing to convert a rapper class back
- 8:48:51to its primitive this is
- 8:48:56unboxing so let's say we would like to
- 8:48:58convert our object of a it's an integer
- 8:49:01object back to its primitive let's say
- 8:49:04int x equals all you got to do is set it
- 8:49:08equal to your object in this case our
- 8:49:11integer object of a this is unboxing
- 8:49:15we're taking a primitive that's wrapped
- 8:49:17in an object and unboxing it we
- 8:49:20unwrapping it and setting it back to its
- 8:49:22primitive if we were unboxing a double
- 8:49:24we would say double some variable equals
- 8:49:29b or chars Char x
- 8:49:33equals our character object of c and
- 8:49:36then Boolean Boolean x equals D again
- 8:49:40this is autoboxing we're wrapping up a
- 8:49:43Christmas present we're wrapping up a
- 8:49:45primitive within an object and then
- 8:49:47unboxing is removing it we're opening
- 8:49:49the Christmas present now these rapper
- 8:49:51classes themselves do have some pretty
- 8:49:53useful utility methods that are static
- 8:49:56let me demonstrate a few you might be
- 8:49:57interested in but there would be too
- 8:49:59many to cover in just this one video if
- 8:50:01you ever need to convert a primitive
- 8:50:03data type into a string there's a two-
- 8:50:05string method of these utility classes
- 8:50:07so let's say we have string
- 8:50:09a string
- 8:50:11B string
- 8:50:13C and string
- 8:50:18D I can convert an integer into a string
- 8:50:22using the integer wrapper class it's ACC
- 8:50:24statically so we type the name of the
- 8:50:26class integer called the two string
- 8:50:29method which is static and then pass it
- 8:50:32an integer like
- 8:50:33123 so let's do this with the
- 8:50:36double we will access the double wrapper
- 8:50:39class called the two string method and
- 8:50:41then pass in a double
- 8:50:453.14 then to convert a character to a
- 8:50:47string we would use the character
- 8:50:49wrapper class character to string and
- 8:50:53then pass in a character
- 8:50:56and then
- 8:50:57Boolean we will access the Boolean
- 8:50:59wrapper class called the two string
- 8:51:02method and then pass in a
- 8:51:05Boolean so strings can be concatenated
- 8:51:08together we should be able to do the
- 8:51:11following let's say string X = A + B + C
- 8:51:17+
- 8:51:18D and then we'll output whatever our
- 8:51:21variable X is
- 8:51:25we get 1 123 3.14 at false it's all one
- 8:51:30long string it's because we use these
- 8:51:32rapper classes to convert these
- 8:51:34primitive data types into Strings if you
- 8:51:37ever need to convert a primitive to a
- 8:51:38string then use that primitive data
- 8:51:40types rapper class and call the two
- 8:51:42string method then you just need to pass
- 8:51:44that value in as an argument then it's
- 8:51:46going to spit out a string for you now
- 8:51:48on the other hand to convert a string to
- 8:51:50a primitive data type there's another
- 8:51:51useful utility method of wrapper classes
- 8:51:55so what we'll do is
- 8:51:56parsing we will have an INT of
- 8:52:00a a double of
- 8:52:03B A Char of
- 8:52:06c and a Boolean of
- 8:52:10D int a will equal we're going to call
- 8:52:14the integer wrapper class call a static
- 8:52:17method of parse int we can convert a
- 8:52:21string to an integer I will convert a
- 8:52:24string of 123 to an
- 8:52:27integer now with doubles you will call
- 8:52:29the double wrapper class and we will
- 8:52:32parse a
- 8:52:35double we'll pass in a string of
- 8:52:393.14 now characters don't have a parse
- 8:52:42method what you could do instead is with
- 8:52:45any string let's say we have a string of
- 8:52:46pizza strings do have built-in methods
- 8:52:49one of which is the Char at
- 8:52:52method return the character the first
- 8:52:55position where index is zero again
- 8:52:57character doesn't have a parse character
- 8:52:59method so you would have to use charart
- 8:53:01at which is unrelated to wrapper
- 8:53:05classes so then with the
- 8:53:07Boolean we would access the Boolean
- 8:53:09wrapper class called the parse Boolean
- 8:53:13method and we can convert a string
- 8:53:16representation of a Boolean such as true
- 8:53:19into an actual Boolean and then let's
- 8:53:21output them now we shouldn't be able to
- 8:53:23add these together much like what we did
- 8:53:25when we used the two string method for
- 8:53:27example string X = A + B + C + D this
- 8:53:34isn't going to work because we're mixing
- 8:53:36and matching different data types and
- 8:53:38then if I was to Output
- 8:53:42X that would give us a compilation error
- 8:53:45to convert a string to its primitive
- 8:53:47data type there is various parse methods
- 8:53:49to handle that within our rapper classes
- 8:53:52it depends on the data type you're
- 8:53:53working with
- 8:53:55there's a couple other miscellaneous
- 8:53:57utility methods within rapper classes
- 8:53:59I'll just cover two more so let's say we
- 8:54:02have a Char of letter pick a
- 8:54:06letter I will pick the letter lowercase
- 8:54:10B I'm going to use a print line
- 8:54:12statement we can check to see if our
- 8:54:14letter actually is a letter we can
- 8:54:17access the character wrapper class call
- 8:54:20the is letter method and then pass in
- 8:54:23our letter
- 8:54:24this returns a Boolean true or false is
- 8:54:27this character a
- 8:54:30letter that is
- 8:54:32true this would be useful for verifying
- 8:54:35user input what if this was a dollar
- 8:54:37sign is this a letter well this returns
- 8:54:41false that is not a
- 8:54:43letter I'll switch this back to
- 8:54:45lowercase b there's also a built-in
- 8:54:48method to check to see if this letter is
- 8:54:50uppercase or not there's a built-in
- 8:54:52utility method of the character rapper
- 8:54:54class class to check to see if this is
- 8:54:56uppercase or
- 8:54:57not
- 8:54:59character is uppercase and then pass in
- 8:55:03our letter is it uppercase currently it
- 8:55:07isn't that is
- 8:55:09false but if it were
- 8:55:13uppercase that returns
- 8:55:15true some of these utility methods of
- 8:55:18the character rapper class would be
- 8:55:20great for verifying user input like a
- 8:55:22username or a password all right
- 8:55:24everybody so those are wrapper classes
- 8:55:26we're basically taking a primitive and
- 8:55:28wrapping it in an object kind of like a
- 8:55:30Christmas present in the next topic
- 8:55:32we're going to be working with array
- 8:55:34lists and array lists only work with
- 8:55:36objects so we need to utilize these
- 8:55:38rapper classes in order to work with
- 8:55:40array lists and wrapper classes do
- 8:55:42provide some useful static utility
- 8:55:44methods that you can always access all
- 8:55:46right everybody and those are wrapper
- 8:55:47classes and in the next video we're
- 8:55:49going to cover array lists all right
- 8:55:52people it's about time we get to array
- 8:55:53lists and Java an array list is a
- 8:55:56resizable array that stores objects you
- 8:55:58can store Primitives if so you'll be
- 8:56:01using autoboxing which we learned about
- 8:56:03in the last topic on rapper classes
- 8:56:05arrays are fixed in size but array lists
- 8:56:08can change they're Dynamic here's how to
- 8:56:11create an array list first we'll follow
- 8:56:13our similar pattern with creating many
- 8:56:15objects in Java array list let's say
- 8:56:19list equals new array list we will need
- 8:56:23to make One Import import java. u.
- 8:56:27arraylist following the type of array
- 8:56:30list we're going to use the diamond
- 8:56:32operator this has to deal with generics
- 8:56:34which is a future topic but for now
- 8:56:37we're going to specify what type of
- 8:56:39object we're going to be storing within
- 8:56:40our array list and you can always use
- 8:56:43the appropriate wrapper class to store
- 8:56:45Primitives for example if you need to
- 8:56:47store integers you can say integer and
- 8:56:50then we do need that diamond operator
- 8:56:51after the second array list
- 8:56:54you don't also need to specify the data
- 8:56:56type within the second Diamond operator
- 8:56:59Java can infer that you're going to
- 8:57:01follow this pattern to create an array
- 8:57:03list this array list can store integers
- 8:57:06you just have to specify that type so
- 8:57:08for example let's take our list and add
- 8:57:10a few
- 8:57:11numbers
- 8:57:15three 1 and
- 8:57:21two and then we can print our list our
- 8:57:25list I will print our
- 8:57:28list and we have three numbers
- 8:57:32312 now if you were storing doubles you
- 8:57:35would set the type to be double and use
- 8:57:37the double wrapper class 3.14 1.99
- 8:57:422.01 I'm just making up numbers
- 8:57:46here there we go so let's store
- 8:57:51strings let's create an array list named
- 8:57:54fruits we will store the names of some
- 8:57:56fruit so the array name is going to be
- 8:57:58fruits and then we will print our array
- 8:58:01list of fruits think of some fruit I
- 8:58:03will add an
- 8:58:06apple an
- 8:58:09orange a
- 8:58:12banana and a coconut that should be good
- 8:58:14enough for this
- 8:58:18example and now we have an array list of
- 8:58:21strings named fruits Apple Orange banana
- 8:58:25coconut to add an element to an array
- 8:58:27list you can use the add method add is a
- 8:58:30built-in method of array lists to remove
- 8:58:32an element you can use the remove method
- 8:58:34then specify an index so let's take
- 8:58:36fruits and remove the element at index
- 8:58:40zero that should get rid of our
- 8:58:44Apple then we have orange banana
- 8:58:47coconut if we were to remove the element
- 8:58:50at index one that should get rid of our
- 8:58:52orange apple banana Coco
- 8:58:55nut there's also the set method take our
- 8:58:58Ray list of fruits called the set method
- 8:59:01add a certain index let's say zero we
- 8:59:04can set that element to be something
- 8:59:05else let's replace apple with
- 8:59:08pineapple replace the element at index
- 8:59:11zero with
- 8:59:13pineapple then we have pineapple orange
- 8:59:15banana coconut or index
- 8:59:19one would be apple pineapple banana
- 8:59:22coconut
- 8:59:24that's the set
- 8:59:25method with an array list to get an
- 8:59:28element at a certain index you can use
- 8:59:29the get method then specify the element
- 8:59:32number get the element at index zero
- 8:59:36that's going to return
- 8:59:38Apple get the element at index one would
- 8:59:41be
- 8:59:42orange
- 8:59:45two
- 8:59:46banana
- 8:59:493
- 8:59:51coconut you can also get the size of an
- 8:59:54array list take your array list call the
- 8:59:57size method that will return the total
- 9:00:00amount of elements within your array
- 9:00:02list so my array list has four
- 9:00:06elements to sort your array list you
- 9:00:09will use the collections framework call
- 9:00:12the sort method and then pass in your
- 9:00:15array list our array list is named
- 9:00:17fruits and then you do need to import
- 9:00:19this
- 9:00:20class java.util doc collections
- 9:00:24after sorting our array list let's print
- 9:00:27it print my array list of
- 9:00:30fruits so these all should be in
- 9:00:32alphabetical order starting with a for
- 9:00:34apple b for banana C for coconut o for
- 9:00:37orange so that's how to sort an array
- 9:00:39list you can use the collections
- 9:00:41framework you could use an enhanced for
- 9:00:44Loop to iterate through all the elements
- 9:00:46of an array list so the data type of my
- 9:00:48aray list is strings we specified that
- 9:00:52type for every fruit in my array list of
- 9:00:56fruits let's print each
- 9:01:02fruit and here they are apple banana
- 9:01:05coconut
- 9:01:06orange all right so now we're going to
- 9:01:08cover an
- 9:01:11exercise we'll create an array and
- 9:01:13accept user input a user will enter in
- 9:01:16all the food that they want within this
- 9:01:17array list so to help us with this
- 9:01:19exercise we will need a scanner scanner
- 9:01:22scanner equals new scanner
- 9:01:25pass in
- 9:01:27system.in import this class and then be
- 9:01:31sure to close your scanner at the end
- 9:01:32because I tend to forget scanner.
- 9:01:36close we'll need an array
- 9:01:39list again we'll follow a similar
- 9:01:41pattern with creating objects array list
- 9:01:45let's say list equals new array
- 9:01:49list using the diamond operator we're
- 9:01:52going to specify the type of what we're
- 9:01:54storing within this array list we'll
- 9:01:56stick with
- 9:01:58strings let's rename our list as Foods
- 9:02:02we'll be storing some food well at least
- 9:02:04the name of
- 9:02:06food we'll create a prompt we'll ask a
- 9:02:09user enter the number of food they would
- 9:02:11like to
- 9:02:12store enter the number of food you would
- 9:02:19like and I'll use print rather than
- 9:02:21print line for the user input
- 9:02:25we'll create a variable of num of food
- 9:02:28to store the number of food that we're
- 9:02:30going to store equals take our scanner
- 9:02:34call the next int method to get the next
- 9:02:36integer from the
- 9:02:38user once we accept an integer we should
- 9:02:41clear the input buffer because there's
- 9:02:42going to be a new line character within
- 9:02:44there take our scanner call the next
- 9:02:47line method to clear it let's do a test
- 9:02:52run and enter the number of food you
- 9:02:54would like I'll just say three and that
- 9:02:57seems to work for now we have the number
- 9:02:59of food we're going to store within this
- 9:03:01array list in my example let's say I
- 9:03:03would like to enter three food items
- 9:03:06well we could use a for Loop to iterate
- 9:03:08three times we will ask your user to
- 9:03:10enter in a food three times then so we
- 9:03:13can use a for Loop for that within our
- 9:03:15for loop we're going to create a counter
- 9:03:17of I meaning index we can set that to be
- 9:03:20zero or one let's stick with one we'll
- 9:03:23continue this for loop as long as I is
- 9:03:26less than or equal to our number of food
- 9:03:30variable if I would like to enter in
- 9:03:32four food items this would be four so we
- 9:03:35should iterate this Loop four times we
- 9:03:38will increment I by One during each
- 9:03:40cycle of this
- 9:03:41Loop during each iteration of this Loop
- 9:03:44we will ask for a food item I'll use
- 9:03:47print rather than print line we will ask
- 9:03:50a user to enter food number
- 9:03:54then I'll add plus our index of I so
- 9:03:57during the first cycle I is going to be
- 9:03:59one then 2 3 four so on and so
- 9:04:03forth then I'll add a colon then a
- 9:04:07space we'll create a local variable of
- 9:04:09food this will be of the string data
- 9:04:11type we will set this equal to use our
- 9:04:14scanner called the next line method to
- 9:04:17get a line of text from the
- 9:04:19user once we have our string of food
- 9:04:21we're going to add it to our array list
- 9:04:23of Foods so take your array list of
- 9:04:26foods call the ad method to add an
- 9:04:29element we will add our string of food
- 9:04:32to our array list of
- 9:04:34foods and then once we're done let's
- 9:04:36print our array list using a print line
- 9:04:39statement we will print our Ray list of
- 9:04:42foods all right let's test
- 9:04:44it enter the number of food you would
- 9:04:47like I'll say four so we should ask for
- 9:04:50four food items enter food number one
- 9:04:53I'll say
- 9:04:54pizza hot
- 9:04:56dog hamburger and then
- 9:05:00Taco and here's my array list of food
- 9:05:03pizza hot dog hamburger
- 9:05:06taco all right everybody those are array
- 9:05:09lists an array list is a resizable array
- 9:05:12that stores objects you can store
- 9:05:14Primitives you're just going to use
- 9:05:16Java's autoboxing feature if that's the
- 9:05:18case once you have your array list
- 9:05:21there's various methods to add remove
- 9:05:23move or set elements and well everybody
- 9:05:26those are array lists in Java today I'm
- 9:05:29going to talk about exceptions using
- 9:05:31Java an exception is an event that
- 9:05:33interrupts the normal flow of a program
- 9:05:36these exceptions pop up if you do things
- 9:05:38like divide by zero you can't find a
- 9:05:41resource for some reason or if there's a
- 9:05:43mismatch with the input type let's say
- 9:05:45the program asks for a number and you
- 9:05:47type in a string of characters these
- 9:05:49sorts of events interrupt your program
- 9:05:51they're called exceptions there's a way
- 9:05:53that we can handle them and that is by
- 9:05:55using a few blocks of code try catch and
- 9:05:59optionally finally so any code that's
- 9:06:02considered dangerous where it might
- 9:06:04interrupt your program you can surround
- 9:06:06with a tri block here's an example so
- 9:06:09let's say I'm going to take the number
- 9:06:12one and divide it by zero mathematically
- 9:06:15speaking you can't divide by
- 9:06:19zero that causes an exception more
- 9:06:22specifically and arithmetic exception
- 9:06:25I'm actually just going to copy this and
- 9:06:26save
- 9:06:27it this code is dangerous it's
- 9:06:30interrupting our program by causing an
- 9:06:33exception we can gracefully handle this
- 9:06:36exception so that it doesn't interrupt
- 9:06:38the normal flow of our program any
- 9:06:41dangerous code we're going to surround
- 9:06:43with a tri block try and a set of curly
- 9:06:50braces there we go however if we have a
- 9:06:53Tri block we also need a catch
- 9:06:56block we will catch any
- 9:06:59exceptions but we're going to list the
- 9:07:01specific kind of exception we're going
- 9:07:03to catch well let's catch that one
- 9:07:05exception that arithmetic
- 9:07:07exception this is the type we're setting
- 9:07:10up a parameter here arithmetic exception
- 9:07:13is the type we will give this exception
- 9:07:15a name of e for a
- 9:07:17nickname if we encounter this exception
- 9:07:20we can take a different course of action
- 9:07:22instead of interrup the program so let's
- 9:07:25do the
- 9:07:25following let's
- 9:07:27output you can't divide by zero idiot so
- 9:07:33if we run this again it shouldn't
- 9:07:36interrupt our
- 9:07:38program we catch this exception and
- 9:07:41output this text
- 9:07:43instead so any code that's dangerous
- 9:07:46where it might cause an exception you
- 9:07:48can surround with the tri block you can
- 9:07:50add more than one catch block to catch
- 9:07:52and handle spefic specific exceptions so
- 9:07:55this time we're going to accept some
- 9:07:56user input we'll need a scanner for now
- 9:07:59I'm going to write the scanner outside
- 9:08:01of the tri Block near the end of this
- 9:08:03video there's a feature called used with
- 9:08:05resources I'll show you how to do that
- 9:08:07later scanner scanner equals new scanner
- 9:08:11type
- 9:08:12system.in and then import this
- 9:08:15class import java.util.scanner
- 9:08:19anytime you accept user input it's
- 9:08:22almost always d dangerous code because a
- 9:08:24user can type in
- 9:08:26anything this time let's prompt a user
- 9:08:30to enter a
- 9:08:32number and I will use
- 9:08:35print let's create a local variable of
- 9:08:38int number equals use our scanner called
- 9:08:42the next int method to accept an integer
- 9:08:46and then we'll just output our
- 9:08:52number and enter a number uh how about
- 9:08:55no I'm going to type in the word Pizza
- 9:08:57instead well we get an exception an
- 9:09:00input mismatch exception Java was
- 9:09:03expecting an integer but we typed in a
- 9:09:05string instead we can handle these
- 9:09:07exceptions too and I'm just going to
- 9:09:09copy the name of this exception because
- 9:09:12we'll reuse it let's catch the following
- 9:09:15let's
- 9:09:17catch any input mismatch
- 9:09:20exceptions this is the type we will give
- 9:09:23this exception a nickname of e so this
- 9:09:26exception we actually do have to
- 9:09:28import import java.util do input
- 9:09:32mismatch exception in case there's a
- 9:09:34mismatch with the data type we're asking
- 9:09:36the user for a number but they type in a
- 9:09:38string Let's help with the following
- 9:09:41let's
- 9:09:42say that wasn't a
- 9:09:45number let's try it
- 9:09:47again enter a number uh I'll type in the
- 9:09:50word I already did pizza let's do Taco
- 9:09:53that wasn't a
- 9:09:55number and our program finished with no
- 9:09:58problems you can catch and handle more
- 9:10:00than one exception there is a catch all
- 9:10:03statement that you could
- 9:10:06add you could catch all exceptions
- 9:10:10exception e using this by itself isn't
- 9:10:13good
- 9:10:14practice you want to let a user know
- 9:10:16exactly what went wrong if all else
- 9:10:19fails and there's an exception that you
- 9:10:21don't anticipate you could just say okay
- 9:10:23something went
- 9:10:26wrong you could technically use this by
- 9:10:31itself enter a number for example I'll
- 9:10:33type in hot dog something went wrong
- 9:10:37this does prevent our program from being
- 9:10:39interrupted it is good practice to let
- 9:10:41the user know what went wrong exactly
- 9:10:44I'm looking at you Microsoft catching
- 9:10:47all exceptions acts as some sort of
- 9:10:49safety net you should only do it at the
- 9:10:51end in case there's something you don't
- 9:10:53anticipate it is better to catch and
- 9:10:55handle specific types of exceptions
- 9:10:58because you can let the user know what
- 9:10:59went wrong
- 9:11:00exactly now there's also the finally
- 9:11:03block this is
- 9:11:05optional finally will always execute
- 9:11:08whether there's an exception or not this
- 9:11:11is where you might clean up any
- 9:11:12resources for example with my scanner I
- 9:11:15forgot to close it we can actually close
- 9:11:17it within the final
- 9:11:19block when we're done with our scanner
- 9:11:22let's close it
- 9:11:24because if we encounter an exception we
- 9:11:26might not close the scanner other times
- 9:11:29finally might be useful is if you open a
- 9:11:31file within a tri
- 9:11:33block then you'll want to close the file
- 9:11:36when you're done with it you can do that
- 9:11:37within the finally block for testing
- 9:11:39purposes I'm just going to add this
- 9:11:43always
- 9:11:45executes all right let's do a test
- 9:11:49run enter a number I'll type in 12 three
- 9:11:53and then we output 123 see that finally
- 9:11:56block does execute this always
- 9:12:00executes if we encounter an exception
- 9:12:03enter a number I will type in
- 9:12:05Taco we get our exception message for
- 9:12:09input mismatch and then that finally
- 9:12:11block does
- 9:12:12execute finally is quite often used for
- 9:12:15cleanup cleaning up your program when
- 9:12:17you're done with it when you open a file
- 9:12:19or resource you want to close it we'll
- 9:12:22have more practice with this on the next
- 9:12:23topic of file handling now there's also
- 9:12:26try with
- 9:12:27resources and actually Java is
- 9:12:29recommending this rather than declaring
- 9:12:32our scanner outside of the tri block we
- 9:12:35can place within a set of parentheses
- 9:12:37and then Java is automatically going to
- 9:12:39close those resources when it's done
- 9:12:40opening them so this works
- 9:12:43too so enter a number
- 9:12:48123 enter a number
- 9:12:50Pizza that wasn't a number
- 9:12:53all right everybody so those are
- 9:12:55exceptions they're events that interrupt
- 9:12:57the normal flow of a program exceptions
- 9:12:59occur when you divide by zero you can't
- 9:13:02locate a resource or there's a mismatch
- 9:13:04of the input type that a user types in
- 9:13:07you'll want to surround any dangerous
- 9:13:08code with a tri block anytime you accept
- 9:13:11user input or try and locate an external
- 9:13:14resource that's almost always dangerous
- 9:13:16code try any dangerous code catch any
- 9:13:19exceptions and optionally you can add
- 9:13:22finally to do any resource cleanup and
- 9:13:25well everybody those are exceptions in
- 9:13:28Java all right everybody in this video
- 9:13:30I'm going to show you how you can write
- 9:13:31a file using Java after doing some
- 9:13:33research I've encountered four popular
- 9:13:36options the first way I'm about to show
- 9:13:37you is the most simple and is what I'll
- 9:13:39be demonstrating in this topic we'll be
- 9:13:41using a file writer object it's good for
- 9:13:43writing small or medium-sized text files
- 9:13:46there's also buffered writer this has
- 9:13:48better performance for large amounts of
- 9:13:50text print writer is best for structure
- 9:13:53data like reports or logs then there's
- 9:13:57file output stream which is best for
- 9:13:59binary files such as images or audio
- 9:14:02files files of that nature in this topic
- 9:14:05I'm going to cover file writer just so
- 9:14:06we get the hang of the basics I might
- 9:14:08make videos on the other writers but I
- 9:14:10haven't decided yet here's how we can
- 9:14:11write a basic file using Java we'll need
- 9:14:13a file writer object file writer I'll
- 9:14:18name this file writer just writer for
- 9:14:20short equals new file
- 9:14:24writer then we need to pass in a file
- 9:14:27path or a file name let's create a file
- 9:14:29named test and include a file extension
- 9:14:32let's say it's a txt file a plain text
- 9:14:34file now you could include an absolute
- 9:14:36file path I'll show you how to do that
- 9:14:38later our plain text file a test is
- 9:14:41going to appear in our source folder
- 9:14:44reading or writing files is kind of
- 9:14:45unpredictable you could consider that
- 9:14:47dangerous code any dangerous code we
- 9:14:49want to enclose within a tri block try
- 9:14:53this dangerous code catch any exceptions
- 9:14:55that might interrupt our
- 9:14:56program with modern Java you can try and
- 9:15:00open up some resource add a set of
- 9:15:02parentheses after try we will try to
- 9:15:05open up a file writer
- 9:15:09object so file writer writer equals new
- 9:15:12file writer then we have our file name
- 9:15:15including the
- 9:15:16extension now we do have to import this
- 9:15:19class import
- 9:15:21java. file writer
- 9:15:24if for some reason we can't write this
- 9:15:25file we might encounter an exception we
- 9:15:28will catch any IO exceptions IO meaning
- 9:15:31input output catch any input output
- 9:15:34exceptions that's the type we will give
- 9:15:37this exception a nickname of
- 9:15:39e then we do have to import this class
- 9:15:42too in order to work with IO exceptions
- 9:15:45java.io doio
- 9:15:47exception in case there's a problem
- 9:15:50writing this file we'll catch this
- 9:15:52exception if it comes up this acts as
- 9:15:54more of a safety net if we encounter
- 9:15:56this exception we can say something
- 9:15:58generic such as could not write file but
- 9:16:04it is best practice to handle specific
- 9:16:06kind of exceptions first momentarily
- 9:16:08we're going to add a catch clause for a
- 9:16:10file not found exception in case we
- 9:16:13can't locate a file path but we'll take
- 9:16:14care of that in a moment within the tri
- 9:16:17Block in order to write a file using
- 9:16:19Java using file writer we will take our
- 9:16:21file writer which we named
- 9:16:24writer called the write method and then
- 9:16:27pass in a
- 9:16:28string let's say I like then pick your
- 9:16:31favorite food as you know my favorite
- 9:16:34food is
- 9:16:35pizza then after writing this file if we
- 9:16:38don't encounter any exceptions we'll
- 9:16:40print a confirmation
- 9:16:42message let's say the
- 9:16:44following file has been
- 9:16:48written all right let's test this
- 9:16:53file has been
- 9:16:56written since we didn't specify a file
- 9:16:58path your file is likely going to be
- 9:17:01within your Source folder and here's
- 9:17:03mine test.txt and it says the following
- 9:17:07I like
- 9:17:08pizza now you could pick a different
- 9:17:10location for example you could use an
- 9:17:13absolute file path so let's say I would
- 9:17:15like to write this file to my desktop so
- 9:17:18what I could do I'm going to go to my
- 9:17:20desktop I'm going to get the file
- 9:17:23location my desktop I'll just right
- 9:17:25click on one of these
- 9:17:26folders go to properties copy this
- 9:17:31location then I'm going to paste that
- 9:17:33file
- 9:17:36path you'll likely need to use double
- 9:17:39forward slashes Java interprets a single
- 9:17:42forward slash as an escape sequence so
- 9:17:44you may need to use double slashes when
- 9:17:47I write this file it should be at this
- 9:17:49location this absolute file path Let's
- 9:17:52test it
- 9:17:53file has been written let's go to my
- 9:17:55desktop and here's that
- 9:17:57file
- 9:17:59test it says I like
- 9:18:05pizza let's say that we get the file
- 9:18:07path wrong instead of desktop I'll
- 9:18:10misspell this as
- 9:18:12deskto run this again we counter that
- 9:18:15exception could not write file that's
- 9:18:19because Java couldn't locate this file
- 9:18:20path I misspelled it now when handling
- 9:18:23exceptions it is best practice to catch
- 9:18:26specific types of exceptions first here
- 9:18:29we're relying solely on our safety net
- 9:18:31that catches all input output exceptions
- 9:18:34another kind of exception that we can
- 9:18:35catch is the
- 9:18:37following file not
- 9:18:41found exception we will give this a
- 9:18:44nickname of e then we do need to import
- 9:18:47this class
- 9:18:49java.io file not found
- 9:18:51exception this this is if we can't
- 9:18:53locate a file or a file path is
- 9:18:56invalid so let's output the
- 9:18:59following could not locate file location
- 9:19:05let's try that again remember that my
- 9:19:08file path is
- 9:19:10misspelled could not locate file
- 9:19:14location let's spell this correctly this
- 9:19:16time I'm going to delete this
- 9:19:19file maybe I'll add another line of text
- 9:19:23I like
- 9:19:24pizza it's really good okay let's try
- 9:19:28that
- 9:19:30again file has been written going to my
- 9:19:33desktop here's that file and it says I
- 9:19:37like pizza it's really
- 9:19:40good for better organization what you
- 9:19:43could do is create a string a file path
- 9:19:47and set that equal to a
- 9:19:50string we're going to cut everything
- 9:19:53within our file
- 9:19:55writer and paste it within the string
- 9:19:58we're going to assign the string of text
- 9:20:00equal to a string variable a file path
- 9:20:03then we'll pass in the string variable
- 9:20:05to the file
- 9:20:07writer and instead of passing in a
- 9:20:09string literal to the right method of a
- 9:20:11file writer for better organization
- 9:20:14let's create a string of let's say text
- 9:20:19content equals and then we'll cut our
- 9:20:28text and then pass in the text
- 9:20:32content let's see I'll add another line
- 9:20:34I like pizza it's really
- 9:20:36good buy me
- 9:20:40pizza okay let's run this
- 9:20:43again file has been written go to our
- 9:20:46test file and here's our test file I
- 9:20:49like pizza it's really good buy me pizza
- 9:20:53if you have a string that takes up
- 9:20:55multiple lines another option is to
- 9:20:57write a multi-line string by using a
- 9:21:00triple set of double quotes So This is
- 9:21:03if you have a lot of
- 9:21:05text this time I'm going to write a
- 9:21:08poem roses are
- 9:21:11red violets are
- 9:21:14blue booty booty
- 9:21:19booty rocking everywhere
- 9:21:23all right so this is a multi-line string
- 9:21:26you use a triple set of double
- 9:21:28quotes that's if you have a lot of text
- 9:21:30to write okay let's test it
- 9:21:33again file has been written let's go to
- 9:21:36our desktop open this file and here's
- 9:21:39our poem roses are red violets are blue
- 9:21:42booty booty booty rocking
- 9:21:45everywhere all right everybody so that
- 9:21:47is how to write a file using Java to
- 9:21:49write small or mediumsized textt files
- 9:21:51you can use a file right writer but you
- 9:21:53will need a try and catch block and well
- 9:21:55everybody that's how to write a file
- 9:21:57using Java hey everybody in this video
- 9:21:59I'm going to show you how we can read a
- 9:22:01file using Java after doing some
- 9:22:03research there's three popular options
- 9:22:06the first and which I'll demonstrate in
- 9:22:08this video is to combine a buffered
- 9:22:10reader and a file reader both together
- 9:22:13this technique is best for reading text
- 9:22:15files line by line buffered reader can't
- 9:22:17read a file by itself you can say that
- 9:22:20it acts as a middleman between the
- 9:22:21program and the file system it helps us
- 9:22:23read files more efficiently and a file
- 9:22:25reader is what actually reads the file
- 9:22:28so we'll be using these two together
- 9:22:29which I will demonstrate momentarily
- 9:22:31another technique is to use a file input
- 9:22:34stream which is best for binary files
- 9:22:36such as images and audio files then
- 9:22:39there's also Random Access file that's
- 9:22:41best for reading or writing specific
- 9:22:43portions of a large file so use this if
- 9:22:46you need to read something specific from
- 9:22:48a big file in today's video we're going
- 9:22:50to combine a buffered reader and a file
- 9:22:52reader together but first we need a file
- 9:22:54to work with pick a location where you
- 9:22:56would like to read this file from for me
- 9:22:59I'm going to create a file on my desktop
- 9:23:01I will create a new text document I'll
- 9:23:05name this
- 9:23:06test and what should our file say think
- 9:23:09of something to write so there's a poem
- 9:23:11that I like to use roses are
- 9:23:14red violets are
- 9:23:16blue booty booty
- 9:23:20booty rocking
- 9:23:25everywhere we will need the name of this
- 9:23:27file including the file extension I'm
- 9:23:29going to right click go to
- 9:23:32properties this file is a plain text
- 9:23:34file a txt file I'm going to copy this
- 9:23:40location I will create a string variable
- 9:23:43named file path equals then within a set
- 9:23:47of quotes cuz we're working with strings
- 9:23:50I will paste that file path and include
- 9:23:52the file name including the extension
- 9:23:55test.txt
- 9:23:57now we're going to be combining a
- 9:23:59buffered reader and a file reader
- 9:24:01together first we will create a buffered
- 9:24:03reader
- 9:24:04object
- 9:24:06buffered reader we'll name this reader
- 9:24:10equals new buffer
- 9:24:14reader we will need to import this
- 9:24:18class import
- 9:24:20java.io buffered reader
- 9:24:23we can't create a buffered reader object
- 9:24:26without first passing in a reader object
- 9:24:29which is why we'll be combining this
- 9:24:30with a file reader object within the
- 9:24:32Constructor we'll be passing in an
- 9:24:34object a new file reader
- 9:24:38object and then we do need to import
- 9:24:40this class
- 9:24:41too import java.io file reader within
- 9:24:46the Constructor of our file reader we're
- 9:24:49going to pass in that file path that
- 9:24:51string
- 9:24:53when reading and writing files you'll
- 9:24:54need try and catch blocks you can see
- 9:24:57that Java is already giving us a warning
- 9:24:59when working with files it's considered
- 9:25:01dangerous code because you don't know if
- 9:25:03you'll actually locate that file or not
- 9:25:05or reading or writing that file might be
- 9:25:06unsuccessful for some reason we will use
- 9:25:09a try and catch block try this dangerous
- 9:25:11code catch any
- 9:25:13exceptions with modern Java you can use
- 9:25:16with
- 9:25:17resources we will add a set of
- 9:25:19parentheses after
- 9:25:20try copy the snippet of code where we
- 9:25:23create a buffered reader
- 9:25:26object and then paste it within that set
- 9:25:28of parentheses remove the semicolon when
- 9:25:30using try with resources you'll
- 9:25:32automatically close this reader when
- 9:25:34you're done with it we're going to catch
- 9:25:36any file not found exceptions within the
- 9:25:39catch
- 9:25:40block so catch file not found exception
- 9:25:44that's the type we will give this
- 9:25:46exception a nickname of e within the
- 9:25:48parameter and we do need to import this
- 9:25:50class as well import java.io file not
- 9:25:55found
- 9:25:56exception if for some reason we can't
- 9:25:58locate this file at this file location
- 9:26:00let's output the
- 9:26:02following could not locate
- 9:26:07file to catch all other input output
- 9:26:10exceptions We'll add another catch block
- 9:26:12this will act as a safety
- 9:26:15net we will catch any
- 9:26:17IO exceptions IO exception we will give
- 9:26:21it a nickname name of
- 9:26:23e this is where we'll say something went
- 9:26:28wrong and import this class as
- 9:26:31well just temporarily within our Tri
- 9:26:33block I'm going to Output that file
- 9:26:38exists so let's test
- 9:26:41it that file
- 9:26:43exists so that file is at this file
- 9:26:46location on my
- 9:26:49desktop for some reason if we can't
- 9:26:51locate that file let's say that I
- 9:26:54misspell desktop as deskto run this
- 9:26:58again well we get that exception that
- 9:27:00file not found exception could not
- 9:27:02locate
- 9:27:04file let's make sure the file path is
- 9:27:07correct now here's how to actually read
- 9:27:09the file within our Tri block let's
- 9:27:12create a local variable of line we'll be
- 9:27:14reading our file line by
- 9:27:20line since we're reading the file line
- 9:27:22by line we'll be using a while
- 9:27:25loop during each cycle of the while loop
- 9:27:28we're going to write the following
- 9:27:29within a set of parentheses we're going
- 9:27:31to set our string of line equal to use
- 9:27:35our buffered reader object call the read
- 9:27:39line method we're taking our reader
- 9:27:42reading the current line this will
- 9:27:44return a string which we're assigning to
- 9:27:46the string variable of line after
- 9:27:49reading one line our reader is going to
- 9:27:51point to the next line however if we run
- 9:27:53out of lines the readline method is
- 9:27:56going to return null so we're going to
- 9:27:58continue reading lines while line does
- 9:28:01not equal a value of null use the reader
- 9:28:05read each line assign it to this
- 9:28:07variable of line if we run out of lines
- 9:28:10if this is null we'll escape the while
- 9:28:12loop during each cycle of the while loop
- 9:28:15we'll output each line it's going to be
- 9:28:17a string and now this should work let's
- 9:28:20test it
- 9:28:24and here's our poem roses are red
- 9:28:26violets are blue booty booty booty
- 9:28:28rocking everywhere so that's one way in
- 9:28:31which you can read a file using Java you
- 9:28:33can combine a buffered reader and a file
- 9:28:35reader it's best for reading text files
- 9:28:38line by line but there's other methods
- 9:28:40you should be aware of too such as file
- 9:28:42input stream and random access file I'm
- 9:28:44not sure if I'll make videos on these
- 9:28:46yet and well everybody that's how to
- 9:28:48read a file using Java hey everybody in
- 9:28:51this video we're going to create an
- 9:28:52audio player using
- 9:28:54[Music]
- 9:28:57Java let's get started all right let's
- 9:29:00get started everybody now in this
- 9:29:01following project what I'm about to show
- 9:29:03you isn't compatible with MP3 files but
- 9:29:06you could easily convert an MP3 file to
- 9:29:08a wave file you can do that online
- 9:29:11otherwise to play an MP3 file you'll
- 9:29:12need an external library or framework
- 9:29:14such as Java FX and I do have another
- 9:29:17video on that if you need some sample of
- 9:29:19music you can always use YouTube's audio
- 9:29:21library these songs are free to use as
- 9:29:23long as they're within the context of
- 9:29:25YouTube you could search for songs find
- 9:29:28one that you like and download
- 9:29:31it this is the song I picked it's
- 9:29:34currently an MP3 file I'll need to
- 9:29:36convert it to a wave file you can easily
- 9:29:38look up an mp3 to wave
- 9:29:41converter select a
- 9:29:47file convert to
- 9:29:50wave convert
- 9:29:54download for convenience I'm going to
- 9:29:57put this on my
- 9:29:59desktop I'll delete the MP3
- 9:30:02file and we now have a wave file to make
- 9:30:05this even more convenient I'll place
- 9:30:07this wave file within my source
- 9:30:11folder so that I don't need to use an
- 9:30:14absolute file path so we're ready to
- 9:30:16begin once we have a wave file we're
- 9:30:18going to create a string for our file
- 9:30:20path
- 9:30:22you can use an absolute file path or
- 9:30:24relative file path since this wave file
- 9:30:27is right next to my main Java file I can
- 9:30:29use a relative file path easily we're
- 9:30:32going to copy the name of this file
- 9:30:34we're going to copy path file name and
- 9:30:39then paste it within the string the song
- 9:30:41that I picked is a caring friend and
- 9:30:43this is a wave file be sure to include
- 9:30:45the extension too once we have our file
- 9:30:47path we'll create a file object but we
- 9:30:51have to pass in our file file path as an
- 9:30:52argument to create a file object file
- 9:30:56file equals new
- 9:30:58file then within the Constructor we will
- 9:31:01pass in our file path to the file
- 9:31:03Constructor then be sure to import this
- 9:31:06class for
- 9:31:07file when handling files it's considered
- 9:31:10dangerous code because accessing files
- 9:31:12can be unpredictable we'll use a try and
- 9:31:14catch
- 9:31:15block try this dangerous code catch any
- 9:31:19exceptions one exception that we may run
- 9:31:21into
- 9:31:22is an IO exception we'll give it a
- 9:31:25nickname of e then be sure to import
- 9:31:28this
- 9:31:29class this catch statement will act as a
- 9:31:31safety net to catch any unexpected IO
- 9:31:34exceptions here we'll output something
- 9:31:37went wrong it is better to handle
- 9:31:40specific kinds of exceptions first we
- 9:31:42will do that momentarily within the tri
- 9:31:45block we will create an audio input
- 9:31:47stream object that's the first step to
- 9:31:49playing audio the type of object is
- 9:31:53audio input
- 9:31:55stream let's name this object just audio
- 9:31:59stream
- 9:32:01equals we will access the audio system
- 9:32:05class we're accessing it statically so
- 9:32:08we type the name of the class call the
- 9:32:11get audio let me zoom out a little get
- 9:32:15audio input stream
- 9:32:20method and then we will we'll pass in
- 9:32:22our file
- 9:32:24object be sure to import this class too
- 9:32:26we'll be doing a lot of importing so get
- 9:32:28used to it now with the get audio input
- 9:32:31stream method we have to add a catch
- 9:32:34clause for any unsupported audio file
- 9:32:37exceptions well we can write a catch
- 9:32:38block for
- 9:32:41that catch the following exception type
- 9:32:44where the type is unsupported audio file
- 9:32:48exception this is if somebody tries to
- 9:32:50use an audio file that's not imported
- 9:32:52not one of the following we will give
- 9:32:54this a nickname of e import this class
- 9:32:58then we'll output the
- 9:33:01following audio file is not
- 9:33:06supported be sure to import the audio
- 9:33:09input stream class
- 9:33:13too now when you open this audio stream
- 9:33:16you do want to close it so in modern
- 9:33:18Java you can use try with resources add
- 9:33:21a of parentheses after try we will cut
- 9:33:24the snippet of
- 9:33:25code and paste it within the set of
- 9:33:28parentheses delete the
- 9:33:31semicolon if you use triy with resources
- 9:33:34you'll automatically close this object
- 9:33:36when you're done with it you don't need
- 9:33:37that finally Clause so now we're going
- 9:33:40to create a clip
- 9:33:42object clip clip equals access the audio
- 9:33:48system class we're doing it
- 9:33:50statically call the get clip method
- 9:33:54import this class for clip in simple
- 9:33:57terms a clip is like a music or Sound
- 9:34:00Player it allows you to load an audio
- 9:34:02file and then play pause stop or reset
- 9:34:04the audio so it gives you some
- 9:34:07controls and we do need a catch clause
- 9:34:09for this too for any line unavailable
- 9:34:13exceptions so let's add that catch
- 9:34:16clause in case we encounter a line
- 9:34:20unavailable exception
- 9:34:22which we will nickname e you might
- 9:34:24encounter this exception if another
- 9:34:26resource is trying to access that audio
- 9:34:28file or if it's unplayable for some
- 9:34:30reason it's
- 9:34:31unavailable we could output something
- 9:34:34such
- 9:34:35as unable to access audio
- 9:34:41resource then import this class as well
- 9:34:45there we
- 9:34:46go once we have our clip it gives us
- 9:34:49some controls but we do have to use it
- 9:34:51to open the audio stream
- 9:34:54object we will take our clip object call
- 9:34:58the open method and then pass in our
- 9:35:01audio stream
- 9:35:03object the clip object which is our
- 9:35:06player is going to open our audio
- 9:35:09stream now we're not going to play it
- 9:35:11quite yet before we actually do play it
- 9:35:13I'm going to add another catch
- 9:35:16Clause we're going to catch any file not
- 9:35:18found exceptions if we can't locate our
- 9:35:20audio file file not
- 9:35:24found exception of
- 9:35:28e will
- 9:35:30output could not
- 9:35:33locate file import this class
- 9:35:38two so we have four catch blocks the
- 9:35:41last one acts as a safety net now let's
- 9:35:44be sure that we can locate this file
- 9:35:45first so
- 9:35:48temporarily let's output no problems
- 9:35:53detected I'm going to run this
- 9:35:56program could not locate
- 9:36:00file if that's the case with our
- 9:36:02relative file path we're going to access
- 9:36:04the source folder then access that audio
- 9:36:09file there it is no problems
- 9:36:13detected if this file is unsupported
- 9:36:15let's say it's MP3 I'll bring back my
- 9:36:18MP3 file move it to my source folder
- 9:36:27then I'll switch this to
- 9:36:29MP3 then we should get an unsupported
- 9:36:31audio file exception if that's the
- 9:36:33case yes audio file is not supported
- 9:36:37because we're trying to use an MP3 file
- 9:36:39so let's switch that back to wave or
- 9:36:42whatever yours was
- 9:36:43originally and delete our MP3 file we
- 9:36:46were just testing it all right now we're
- 9:36:48actually going to play the clip
- 9:36:52now one last thing that I'm going to add
- 9:36:54it's optional I'm going to add a finally
- 9:36:57block once we're done with this program
- 9:36:59we can output the following let's just
- 9:37:01say
- 9:37:03by this will make sense in just a moment
- 9:37:05why I'm doing this there's one problem
- 9:37:07we're going to encounter when we play
- 9:37:08our audio now to actually play the audio
- 9:37:12we're going to take our clip
- 9:37:14object call the start method to start
- 9:37:17the
- 9:37:18audio here's what happens when I run
- 9:37:20this currently
- 9:37:25the music may play for just a brief
- 9:37:26second we print by then exit the program
- 9:37:29right away the issue that we're running
- 9:37:31into is that our program doesn't wait
- 9:37:33for the audio to finish once we start
- 9:37:35the clip we immediately end the
- 9:37:38program so we need some way to keep the
- 9:37:40clip open and there's a bunch of
- 9:37:42different options you could use
- 9:37:43threading within a while loop to check
- 9:37:45to see if the clip is still running but
- 9:37:47I haven't taught that yet so here's
- 9:37:49another option we create a prompt for
- 9:37:52the user to play stop reset or quit the
- 9:37:55audio program the audio will continue
- 9:37:58playing unless the user decides to stop
- 9:38:00or quit the program so we'll need a
- 9:38:02scanner to work with to accept user
- 9:38:04input scanner scanner equals new scanner
- 9:38:09pass in
- 9:38:12system.in import this
- 9:38:15class now if you open a scanner you do
- 9:38:17want to close it when you're done with
- 9:38:19it you can do that within the final
- 9:38:21finally block like this scanner.
- 9:38:24close with modern Java you can use try
- 9:38:28with resources which will automatically
- 9:38:30close any resources and we can do that
- 9:38:32with our scanner too let's cut our
- 9:38:35scanner paste it within the tri block
- 9:38:38now be sure to include the
- 9:38:41semicolon our scanner is going to
- 9:38:43automatically close when we're done with
- 9:38:44this program because we're using try
- 9:38:46with resources you can use try with
- 9:38:49resources if an object implements the
- 9:38:51autoc closable interface scanner and
- 9:38:54audio stream both do clip doesn't so
- 9:38:56we'll keep that within the tri block
- 9:38:59rather than start our clip let's create
- 9:39:01a string variable of
- 9:39:03response we'll create a while
- 9:39:07loop while our
- 9:39:11response
- 9:39:13equals let's say capital Q now we want
- 9:39:16to say while our response doesn't equal
- 9:39:19capital Q we'll use the Notch logical
- 9:39:22operator and to make our while loop
- 9:39:24happy we do have to initialize our
- 9:39:26response we'll set that to be an empty
- 9:39:28string so a user is going to type in a
- 9:39:31response as long as it's not Q meaning
- 9:39:33quit we'll continue the
- 9:39:35program within the while loop we'll give
- 9:39:38a user a few
- 9:39:39options let's output the
- 9:39:42following let's say p equals
- 9:39:46play Let's copy this print line
- 9:39:49statement s equals
- 9:39:53stop R is to
- 9:39:57reset and Q is to
- 9:40:04quit then we will ask a user to enter
- 9:40:08your choice I'll use print rather than
- 9:40:11print line to keep the user input on the
- 9:40:13same
- 9:40:15line we'll take our response which is a
- 9:40:18string set it equal to use our scanner
- 9:40:21get the next character by calling the
- 9:40:23next method in case a user types in a
- 9:40:26lowercase character we can make it
- 9:40:28uppercase by Method chaining the two
- 9:40:31uppercase
- 9:40:32method all right we'll do a test run and
- 9:40:36let's be sure that we start the clip
- 9:40:38just temporarily clip.art method what's
- 9:40:42likely going to happen is that we'll
- 9:40:43start the audio clip and it won't stop
- 9:40:46until we press Q to quit
- 9:40:52so here's the audio hopefully you can
- 9:40:54hear me once I press Q the program's
- 9:40:58going to
- 9:40:59stop it's because the program closes
- 9:41:01down and
- 9:41:03exits now we want to give the user a few
- 9:41:06options after they type in their
- 9:41:08response we can use an enhanced switch
- 9:41:11for this let's also be sure that we
- 9:41:13delete the start method of our
- 9:41:17clip we don't want the audio clip to
- 9:41:19start until the user presses p to play
- 9:41:22we will create a
- 9:41:24switch we will examine our
- 9:41:27response does our response match any
- 9:41:31cases if our response matches a case of
- 9:41:35P we'll write an arrow to do the
- 9:41:37following take our clip then call the
- 9:41:40start
- 9:41:42method if our response matches a case of
- 9:41:45s that means stop take our clip object
- 9:41:49called the stop method
- 9:41:52if our response matches a case of R do
- 9:41:55the following take our clip to reset
- 9:41:58you're going to call the set
- 9:42:01micro
- 9:42:03second
- 9:42:05position then pass in
- 9:42:07zero do pay attention to the
- 9:42:10capitalization s is a
- 9:42:13lowercase if our response matches a case
- 9:42:16of Q take our clip call the close method
- 9:42:20to close the clip
- 9:42:22then if there's no matching cases let's
- 9:42:24output the
- 9:42:25following system.out.print line invalid
- 9:42:31Choice all right and that should be
- 9:42:37everything enter your choice just to be
- 9:42:40sure that the default case is working
- 9:42:42I'm going to type
- 9:42:44Taco invalid Choice we'll press P to
- 9:42:48play
- 9:42:52s to
- 9:42:54stop P to play
- 9:42:57again R to
- 9:43:01reset and Q to
- 9:43:03[Music]
- 9:43:05quit all right everybody that is an
- 9:43:07audio player that you can write using
- 9:43:09Java what's up everybody in this video
- 9:43:12we're going to create a working game of
- 9:43:13hangman using Java a user is going to
- 9:43:16guess certain characters and we'll see
- 9:43:17if there's a match within a secret word
- 9:43:19near the end of this video topic will be
- 9:43:22importing a file of words and one of
- 9:43:24those words will be secretly selected so
- 9:43:26let's get started all right let's get
- 9:43:28started everybody near the end of this
- 9:43:30video we'll have our program choose a
- 9:43:32random word from a list of words but to
- 9:43:34keep this program simple in the
- 9:43:35beginning for a testing purposes let's
- 9:43:38create a string variable of word here
- 9:43:41we'll select a word that we're going to
- 9:43:42use throughout the program just for
- 9:43:44testing pick a word for me I'll say you
- 9:43:46know what let's go with pizza so this
- 9:43:48will be my word so we have our word
- 9:43:51we'll be accepting user input so we need
- 9:43:53a scanner scanner scanner equals new
- 9:43:57scanner pass in
- 9:44:00system.in import the scanner class and
- 9:44:04close your scanner at the end of the
- 9:44:05program scanner.
- 9:44:09close here's where we'll be working with
- 9:44:11array lists we will create an array list
- 9:44:14of characters to do that we type array
- 9:44:17list then specify the type within a set
- 9:44:19of angle brackets we can't use
- 9:44:21Primitives such as Char we'll have to
- 9:44:23use the rapper class of character to
- 9:44:26store characters I will name this array
- 9:44:28list word State equals a
- 9:44:33new array list use the diamond operator
- 9:44:37set of parenthesis semicolon we'll need
- 9:44:40to import this
- 9:44:43class now within the diamond operator
- 9:44:45you don't need to list character again
- 9:44:47even though that does work Java can
- 9:44:49infer what the type is in the beginning
- 9:44:52our word state is going to contain a
- 9:44:54bunch of underscores and we're going to
- 9:44:55fill them in one letter at a time by
- 9:44:57guessing we'll also need a variable to
- 9:45:00keep track of our wrong guesses int
- 9:45:02wrong
- 9:45:03guesses this will initially be zero
- 9:45:07we'll set that equal to be
- 9:45:09zero within our array of word State
- 9:45:12we're going to add an underscore for
- 9:45:14every letter within our word we'll
- 9:45:16create a for Loop that will cycle once
- 9:45:18for every letter in our word here's how
- 9:45:20we can do that
- 9:45:22so we'll start with a basic for Loop
- 9:45:24create an INT of I for index equals 0
- 9:45:29we'll continue as long as I is less than
- 9:45:32take our word and strings have a
- 9:45:34built-in length method return the length
- 9:45:37of this word so in this case it's going
- 9:45:39to be five because there's five letters
- 9:45:41in this word then increment I by One
- 9:45:45during each cycle of this for loop we're
- 9:45:47going to take our array of word State
- 9:45:50and and add a character by using the add
- 9:45:52method we will pass in an underscore an
- 9:45:56underscore character be sure you're
- 9:45:58using single quotes not double quotes
- 9:46:01because these are chars technically not
- 9:46:03strings all right let's do a test run
- 9:46:05I'm going to Output our word state to
- 9:46:08see what we have so we should get 5core
- 9:46:12characters and we do one 2 3 four five p
- 9:46:19i z z a throughout this game by guessing
- 9:46:23letters we'll be filling in this word
- 9:46:24state by replacing the underscores if
- 9:46:26there's a
- 9:46:28match let's change this word just so we
- 9:46:30know that everything's working I will
- 9:46:32pick watermelon because that's the first
- 9:46:34thing that came to mind so watermelon
- 9:46:37has 10
- 9:46:38characters 1 2 3 4 5 6 7 8 9 10 all
- 9:46:44right we know that that works I will
- 9:46:46switch it back to Pizza delete this
- 9:46:49print statement and we can move
- 9:46:51on word state is an array list made up
- 9:46:55of characters currently they're all
- 9:46:57underscores but we'll be replacing each
- 9:46:59with the character if we get the correct
- 9:47:01guess after the for Loop why don't we
- 9:47:03create a welcome
- 9:47:04message let's say welcome to Java
- 9:47:10hangman now you don't need to do this
- 9:47:13but I'm going to add some decorators
- 9:47:15just a bunch of asterisks before and
- 9:47:18after again you don't need to but I
- 9:47:20think it looks
- 9:47:24cool let's make sure everything looks
- 9:47:27okay welcome to Java
- 9:47:32hangman for our hangman we're going to
- 9:47:34display some asky art outside of our
- 9:47:37main method we're going to declare
- 9:47:38another method that's in charge of
- 9:47:40returning asky art based on the number
- 9:47:42of wrong guesses that we have this will
- 9:47:44be a static method it's going to return
- 9:47:47a string we'll name this method get
- 9:47:51hangman
- 9:47:53art we will have one parameter we have
- 9:47:56to pass in how many wrong guesses we
- 9:47:59have then depending on the number of
- 9:48:01wrong guesses we'll return a certain
- 9:48:03image represented as a
- 9:48:06string within our get hangman art method
- 9:48:09we'll be returning a string a string
- 9:48:11representation of our asky art we can
- 9:48:14best do this with the switch now a
- 9:48:16switch can return something we'll use
- 9:48:18the return keyword create a
- 9:48:22switch end it with a semicolon within
- 9:48:25our switch what are we examining we're
- 9:48:28examining the number of wrong guesses
- 9:48:30this will be a number 0 through 6 we'll
- 9:48:33need a case for each of those numbers
- 9:48:35for each of those guesses depending on
- 9:48:37how many wrong guesses the user has
- 9:48:39we'll display one of a few images so
- 9:48:41here we'll be working on our art skills
- 9:48:43today too if we have a matching case
- 9:48:46where wrong guesses matches a case of
- 9:48:48zero that means they have no wrong
- 9:48:50guesses
- 9:48:52case0 Arrow meaning do this will return
- 9:48:55the following in Java you can write a
- 9:48:57multi-line string but you have to use a
- 9:49:00triple set of double quotes and I'm just
- 9:49:02going to line these
- 9:49:04up within this triple set of double
- 9:49:06quotes I'll have three empty lines
- 9:49:08between
- 9:49:10them we won't display anything if the
- 9:49:12number of wrong guesses is
- 9:49:14zero then for case one if a user has one
- 9:49:18wrong
- 9:49:18guess we'll display our man's
- 9:49:22head represented by an
- 9:49:25O if wrong guesses is two we can
- 9:49:29represent that with case two we will
- 9:49:31draw the man's
- 9:49:33body represented by a vertical
- 9:49:36bar case three will be the left
- 9:49:41arm for the left arm we'll use a forward
- 9:49:45slash for case four
- 9:49:50that will be for the right arm we'll use
- 9:49:52a backslash in Java a backslash is
- 9:49:55actually an escape sequence to display a
- 9:49:57backslash we have to use double back
- 9:50:00slashes case five will be for the left
- 9:50:07leg it's kind of like we're assembling
- 9:50:09Exodia if you know the
- 9:50:12reference and then k6 is for the right
- 9:50:16leg we'll need two backs
- 9:50:19slashes we'll want to a default case if
- 9:50:21there's no matching
- 9:50:22cases we'll just return an empty string
- 9:50:25just to make Java happy all right and
- 9:50:28this method is done based on the number
- 9:50:31of wrong guesses we'll return one of a
- 9:50:33few
- 9:50:36strings Let's test it I'm going to use a
- 9:50:40print line
- 9:50:41statement call our get hangman art
- 9:50:43method we'll start with zero zero wrong
- 9:50:47guesses we should end up displaying
- 9:50:49three empty lines
- 9:50:52if wrong guesses is one we should
- 9:50:55display The Hangman's
- 9:50:57head
- 9:51:00two two is for the
- 9:51:03body three is for the left
- 9:51:08arm
- 9:51:11four is for the right
- 9:51:14arm five is for the left leg
- 9:51:20and six is the full
- 9:51:24body that's when we lose when wrong
- 9:51:26guesses is six all right we know that
- 9:51:29the get hangman art method works I will
- 9:51:31collapse it because we're done with it
- 9:51:33to get the art for our hangman we just
- 9:51:35have to call this method then pass in
- 9:51:37the current number of wrong guesses
- 9:51:40let's delete this print line statement
- 9:51:41we no longer need
- 9:51:43it now when we print our word
- 9:51:46State let me show you
- 9:51:50we get this ugly formatting where we
- 9:51:53have a set of straight brackets then all
- 9:51:55the underscores are comma separated we
- 9:51:57can customize that so here's what we'll
- 9:52:00do after our welcome message we'll
- 9:52:02output the
- 9:52:05following we'll output the word word
- 9:52:10colon space I'll use print I want to
- 9:52:14stay on the same line we'll create a for
- 9:52:17Loop an enhanced for Loop
- 9:52:21for every character in our array list of
- 9:52:23word State we're going to display an
- 9:52:25underscore with no
- 9:52:27formatting using an enhanced for Loop we
- 9:52:29have to list the data type of what we're
- 9:52:31cycling through we're cycling through an
- 9:52:33array of characters Char a common
- 9:52:36convention with enhanced for Loops is to
- 9:52:39name each letter C because we can't use
- 9:52:41Char because that's a data
- 9:52:44type and character is kind of long so C
- 9:52:47is short for character for every
- 9:52:49character of C
- 9:52:51in our array list our array list is
- 9:52:54named word
- 9:52:56State we'll do the
- 9:52:58following we will output our character
- 9:53:01of C plus a
- 9:53:04space so then when we output
- 9:53:07this oh let's use print rather than
- 9:53:10print line because we want them all in
- 9:53:12the same
- 9:53:15line that's much better that output
- 9:53:18looks a lot nicer each letter letter is
- 9:53:21an underscore they're all separated with
- 9:53:23a space when we guess each letter we'll
- 9:53:26be flipping these underscores to its
- 9:53:28matching character if they do
- 9:53:30match I'll add an empty print line just
- 9:53:32so we go down to the next
- 9:53:35line okay for the next part we'll have a
- 9:53:38user guess a letter we'll need some user
- 9:53:41input we'll need a
- 9:53:44prompt guess a letter colon space I'll
- 9:53:48use print not print line
- 9:53:51we'll create a variable of guess guess
- 9:53:53will be a
- 9:53:54letter we'll need to accept user input
- 9:53:57we'll need to accept user input using
- 9:53:59our scanner scanner. next next will get
- 9:54:03the next
- 9:54:05character now since we're working with
- 9:54:07lowercase characters we can method chain
- 9:54:10the two lowercase method just to make it
- 9:54:12lowercase if somebody types in an
- 9:54:14uppercase character to lower case
- 9:54:20okay to lowercase method using next the
- 9:54:24user input is going to be a string not a
- 9:54:26character we can easily convert that to
- 9:54:28a character by using the Char at
- 9:54:31method but we have to pass it an index
- 9:54:34we're only accepting a single character
- 9:54:36so we'll use Char at zero we're telling
- 9:54:39Java with the string that the user types
- 9:54:41in give me just the first character and
- 9:54:44convert it to be a Char so guess is
- 9:54:47going to be a single letter it'll be of
- 9:54:49the Char data type
- 9:54:51all right then let's test it I will
- 9:54:53output our
- 9:54:57guess I will guess an uppercase
- 9:55:00P we'll end up outputting a lowercase p
- 9:55:03which is
- 9:55:04good how about a z yep our guess is z if
- 9:55:09I were to type in a full word like
- 9:55:11pineapple we still end up with just the
- 9:55:13first character the first character in
- 9:55:15our
- 9:55:16string which is good though let's delete
- 9:55:19this print line statement we no longer
- 9:55:20need it for this next part we're going
- 9:55:23to check to see if our guess matches any
- 9:55:26letters within our
- 9:55:28word we'll use an if statement for
- 9:55:31that if take our word it's a string in
- 9:55:34this case Pizza we'll use the built-in
- 9:55:37index of
- 9:55:40method and then pass in our
- 9:55:44guess so basically with the index of
- 9:55:47method it's going to return the index
- 9:55:49within the string of the first
- 9:55:51occurrence of the specified
- 9:55:54character for example let's say I guess
- 9:55:56the letter Z well Z is in the word Pizza
- 9:55:59it's at index 2 that's the first
- 9:56:03occurrence if my guess was Z index of is
- 9:56:06going to return the number two because
- 9:56:08that's the first occurrence of Z we will
- 9:56:10check if the first occurrence is greater
- 9:56:13than or equal to zero that means there's
- 9:56:15a matching letter if that's the
- 9:56:17case if there's a matching letter well
- 9:56:20will
- 9:56:21output
- 9:56:22correct guess and then I'm just going to
- 9:56:26add a new line character to the
- 9:56:28end all right let's do a test
- 9:56:31run I will guess the letter Z that's a
- 9:56:35correct
- 9:56:37guess but are there any
- 9:56:39X's there is not so we don't output
- 9:56:43correct
- 9:56:44guess if it's the wrong guess we can
- 9:56:46output using an lse statement
- 9:56:50wrong guess then I'll add a new line
- 9:56:53character to the end let's try that
- 9:56:56again I will guess an X wrong
- 9:57:01guess there are no x's in the word
- 9:57:05Pizza okay so within our if statement if
- 9:57:07we get the correct guess we're going to
- 9:57:09change our word state to reflect
- 9:57:12it that's one less letter that the user
- 9:57:14has to
- 9:57:15guess we're going to cycle through the
- 9:57:17length of our word using a for Loop for
- 9:57:21in I equals 0 we'll continue as long as
- 9:57:25I is less than our words length by
- 9:57:29calling the length method then increment
- 9:57:31I by
- 9:57:33one we have to see where there's a match
- 9:57:35exactly which index within our word
- 9:57:38we'll use an if
- 9:57:39statement we will take our word in this
- 9:57:42case Pizza called the Char at method
- 9:57:46then pass in our index of I during each
- 9:57:49cycle I is going to increment but during
- 9:57:51the first cycle it's going to be zero so
- 9:57:54we're saying take our word in this
- 9:57:56example Pizza give me the first
- 9:57:58character so for pizza that would be the
- 9:58:00letter
- 9:58:02P is that equal to using the comparison
- 9:58:05operator are
- 9:58:07guess so let's say I do guess P the
- 9:58:10first character of my word is P that
- 9:58:13means that they match we're going to
- 9:58:15take our array list of word State and
- 9:58:19we're going to update it by using the
- 9:58:21set
- 9:58:22method we have to pass in an index and a
- 9:58:25new value our
- 9:58:29guess let's test
- 9:58:31it so with the word Pizza are there any
- 9:58:35z's correct guess oh then we have to
- 9:58:38Output it
- 9:58:39too we're going to continue playing as
- 9:58:41long as wrong guesses is less than six
- 9:58:45once wrong guesses is six or greater
- 9:58:47then the game
- 9:58:48ends so so let's cut this portion of
- 9:58:52code cut it don't delete it we'll write
- 9:58:55a while
- 9:58:56loop
- 9:58:58while our wrong guesses variable wrong
- 9:59:02guesses is less than six we'll continue
- 9:59:05playing this game within the while loop
- 9:59:08we'll paste all that code again let's do
- 9:59:11a test
- 9:59:13run guess a letter I'm going to guess Z
- 9:59:17so that should update our word to
- 9:59:19reflect two Z's because my word is pizza
- 9:59:22but if there's no matching
- 9:59:23letters it doesn't get updated because
- 9:59:26there's no matches let me fill this in p
- 9:59:30i ZZ a and we get the full word all
- 9:59:35right that's good so far so
- 9:59:38good so we'll need to keep track of
- 9:59:40wrong guesses within our else statement
- 9:59:43we're going to take our variable of
- 9:59:44wrong guesses incremented by one using
- 9:59:48the increment operator
- 9:59:51then back to the beginning of the loop
- 9:59:53we're going to display our hangman art
- 9:59:55by calling the get hangman art
- 9:59:58method this method will return a string
- 10:00:01so we can put that within a print
- 10:00:03statement let's use print call the get
- 10:00:07hangman art method then pass in the
- 10:00:09number of wrong
- 10:00:11guesses let's do another test
- 10:00:14run we have three empty spaces because
- 10:00:17we haven't displayed any parts of the
- 10:00:18hangman yet I will guess the wrong
- 10:00:20letter X we get the head of the hangman
- 10:00:25I'll guess some more wrong
- 10:00:26letters
- 10:00:28CV b n
- 10:00:34m at the end we'll have to display the
- 10:00:36full hangman when we
- 10:00:38lose let's display the right
- 10:00:40letters p i z
- 10:00:45a okay good what happens when we lose if
- 10:00:49wrong guesses is six or
- 10:00:53greater so outside of the while loop our
- 10:00:56while loop ends right
- 10:00:58here we'll write an if
- 10:01:01statement if our variable of wrong
- 10:01:04guesses is greater than or equal to
- 10:01:07six okay we will again use a print
- 10:01:11statement called the get hangman art
- 10:01:14method and then pass in the number of
- 10:01:17wrong
- 10:01:18guesses we'll also display a game over
- 10:01:22message game
- 10:01:25over then display the
- 10:01:28word the word
- 10:01:31was colon space plus our original word
- 10:01:36of
- 10:01:37pizza let's do a test run
- 10:01:40again I'm going to guess the wrong
- 10:01:47characters all right game over we
- 10:01:49display the the full hangman the word
- 10:01:51was
- 10:01:53pizza I think I'm going to make a few
- 10:01:55changes I'm going to remove these new
- 10:01:57line
- 10:01:58characters it's a little too much space
- 10:02:00I
- 10:02:01think all right we're
- 10:02:03good so how do we win let's add that
- 10:02:07after the for
- 10:02:08Loop if we get a correct guess we'll
- 10:02:12check to see if our word State doesn't
- 10:02:14contain any lowercase
- 10:02:17characters so take our array list of
- 10:02:19word
- 10:02:22State call the contains
- 10:02:25method and then pass in an underscore
- 10:02:28character but we want to check to see if
- 10:02:30our word State doesn't contain any
- 10:02:32lowercase characters we can prefix this
- 10:02:35with the not logical operator an
- 10:02:37exclamation point if our word our word
- 10:02:41State doesn't contain any lowercase
- 10:02:43characters that means that we guessed
- 10:02:45all the letters and we
- 10:02:47win if that's true let's display our
- 10:02:49hangman One Last Time
- 10:02:52system.out.print get hangman art pass in
- 10:02:55the number of wrong guesses and we'll
- 10:02:59display you
- 10:03:02win and display the word
- 10:03:06again the word was colon space plus our
- 10:03:12word then we'll want to break out of
- 10:03:14this Loop cuz we're stuck in a while
- 10:03:16loop we can use a break statement after
- 10:03:18we win to break out of the
- 10:03:22loop all right let's see what we're
- 10:03:24working
- 10:03:27with I'll guess a few wrong letters x c
- 10:03:32v b n all right one more wrong guess and
- 10:03:37I lose game over the word was
- 10:03:40pizza all right I'll actually win this
- 10:03:43time but I'll guess a few wrong
- 10:03:44characters q
- 10:03:47w t
- 10:03:49I'll guess some correct characters a z i
- 10:03:55p you win and we display our current
- 10:03:58score the current state of our hangman
- 10:04:01we display you win the word was
- 10:04:04pizza in this next part of this video
- 10:04:06we're going to create a more advanced
- 10:04:08version of this program by importing a
- 10:04:10file of words a word is going to be
- 10:04:12randomly chosen from a file instead of
- 10:04:15just setting one in the beginning okay
- 10:04:17everybody in this next section we're
- 10:04:19going to create a file that contains all
- 10:04:21the words that we can possibly use for
- 10:04:23this Hangman game within our project
- 10:04:24folder why don't we create a new file
- 10:04:27file new file I'm going to name this
- 10:04:31file words and this will be a txt
- 10:04:34file for all the different possible
- 10:04:36words in this game place within this
- 10:04:38file you can copy and paste them from
- 10:04:40somewhere online if you would like just
- 10:04:42to make reading this file a simple as
- 10:04:44possible I'm going to list all the words
- 10:04:45on a separate line so think of some
- 10:04:47words or like I said copying paste them
- 10:04:50from somewhere I'll just add several
- 10:04:52Apple orange banana
- 10:04:56coconut
- 10:04:58pineapple grape lime lemon that's good
- 10:05:05enough in my project folder I have this
- 10:05:07file words.txt
- 10:05:10temporarily with my game I'm going to
- 10:05:13write a multiline comment and just
- 10:05:15comment all of this out until we're
- 10:05:17ready to bring it back in
- 10:05:23at the top of my Hangman game I will
- 10:05:26create a string named file
- 10:05:28path this will contain either a relative
- 10:05:31file path or an absolute file path since
- 10:05:34this file is in my project folder I just
- 10:05:36have to list the file name words.txt
- 10:05:41we're going to read this file line by
- 10:05:44line we'll store each word in an array
- 10:05:46list we'll create a new array list AR
- 10:05:49array list the data type is Going to Be
- 10:05:52Strings so string I will name this array
- 10:05:56list words equals new array list Diamond
- 10:06:01operator parenthesis
- 10:06:03semicolon now we'll actually need to
- 10:06:05read this file reading files is
- 10:06:08considered dangerous code because it's
- 10:06:10highly likely that something can go
- 10:06:11wrong we will write a try with resources
- 10:06:14block we need that set of parentheses
- 10:06:16after try we will catch any except
- 10:06:21options when you try with resources
- 10:06:23you'll automatically close any resources
- 10:06:25listed within the set of parentheses
- 10:06:27within the set of parentheses we will
- 10:06:29create a buffered reader
- 10:06:31object buffered reader which we will
- 10:06:35name reader for short equals new
- 10:06:38buffered
- 10:06:42reader now a buffered reader by itself
- 10:06:44can't read files we do have to pass in a
- 10:06:47file reader to the Constructor
- 10:06:49new file reader then within our file
- 10:06:53reader Constructor we will pass in our
- 10:06:56file path the location to that
- 10:06:59file now our file reader object wants us
- 10:07:02to catch any file not found
- 10:07:04exceptions we'll place that within our
- 10:07:06catch block file not found exception of
- 10:07:10e then we'll output if we can't find
- 10:07:12this
- 10:07:13file could not find
- 10:07:17file We'll add another catch block just
- 10:07:19as a safety net we'll catch any
- 10:07:23IO
- 10:07:26exception e
- 10:07:28exceptions this is where we'll say
- 10:07:31something went wrong this just acts as a
- 10:07:35safety net if there's something we don't
- 10:07:37anticipate so we have our reader object
- 10:07:40within the tri block we will create a
- 10:07:42local variable it's a string of
- 10:07:46line we will read this file line by line
- 10:07:49using a while
- 10:07:51loop while within a set of parentheses
- 10:07:55assign our line equal to use our reader
- 10:07:59called the read line
- 10:08:02method if we run out of lines this is
- 10:08:05going to return null while the value
- 10:08:08that's returned does not equal null
- 10:08:11we'll continue reading this file reader
- 10:08:14is going to give us each of these words
- 10:08:16line by line we're going to assign them
- 10:08:19to our array list of words take our
- 10:08:21array list of words call the add method
- 10:08:24we will add the current
- 10:08:27line if there's any white space after
- 10:08:30these words this will pick that up and
- 10:08:32our program will consider them to be
- 10:08:34characters we will use the trim method
- 10:08:37after line to trim that white
- 10:08:41space let's perform a test run I'm going
- 10:08:44to Output our array list of words
- 10:08:49yep and here's our words all within an
- 10:08:51array
- 10:08:53list let's delete that line we're going
- 10:08:56to choose one of these words at random
- 10:08:59we'll need a random object random random
- 10:09:02equals new
- 10:09:05random import the
- 10:09:08class here's where we're going to choose
- 10:09:10our word I'm going to copy this from the
- 10:09:12previous
- 10:09:14code and paste
- 10:09:16it rather than setting our word to be
- 10:09:19hard-coded string we're going to take
- 10:09:21our array list of
- 10:09:26words call the get
- 10:09:28method but we have to specify an
- 10:09:31index with our array list return a
- 10:09:34string at a given index that index is
- 10:09:37going to be randomly generated by using
- 10:09:39this random object we will pass in a
- 10:09:41random object call the next int method
- 10:09:45to generate a random integer the range
- 10:09:48of random numbers is going to be
- 10:09:49dependent on the size of our array list
- 10:09:52of
- 10:09:53words take our words call the size
- 10:09:56method so with our file I have let's see
- 10:10:00how many words how many strings 1 2 3 4
- 10:10:035 6 7
- 10:10:058 the size method of words is going to
- 10:10:07return the number eight just imagine
- 10:10:09that I'm replacing this with the number
- 10:10:11eight return a random index between 0
- 10:10:13and 8 that's kind of how that works and
- 10:10:17then let's output our word for testing
- 10:10:18purposes
- 10:10:21what is my
- 10:10:22word this time my word was pineapple but
- 10:10:25it's likely going to change every time
- 10:10:26you run it Apple lime Apple again
- 10:10:32pineapple
- 10:10:34grape it really likes pineapple for some
- 10:10:38reason let's delete this print line
- 10:10:40statement we'll integrate our code back
- 10:10:42into the program again we can delete
- 10:10:44that multi-line
- 10:10:47comment and let's R this program one
- 10:10:50last time we should now have a randomly
- 10:10:52chosen
- 10:10:53word I don't know what it's going to be
- 10:10:56for me it's six letters I'll guess some
- 10:10:58of the vowels a there's one a e i o
- 10:11:06u it's probably orange but I'm going to
- 10:11:09guess some more wrong letters for
- 10:11:11testing
- 10:11:12purposes okay game
- 10:11:15over the word was Orange let's run it
- 10:11:18again
- 10:11:20this one is seven
- 10:11:22letters a e i o u it's
- 10:11:29coconut
- 10:11:31Co n u t you win the word was
- 10:11:36coconut all right everybody that is a
- 10:11:39game of hangman that you can code using
- 10:11:41Java hello there everybody in this video
- 10:11:44I'm going to show you how we can work
- 10:11:45with dates and times using Java this
- 10:11:47serves as more of an introduction we can
- 10:11:49work with local dates times date times
- 10:11:52and UTC timestamps let's get the date
- 10:11:55right now using local date we will
- 10:11:57create an object of local date let's
- 10:12:00name this date for short
- 10:12:03equals access the class of local
- 10:12:06date called the now
- 10:12:09method if I was to Output our date
- 10:12:12object we'll print our date that's going
- 10:12:15to give you the current date whatever it
- 10:12:17is right now I'm filling this video
- 10:12:20December 17th
- 10:12:222024 post the date that you're watching
- 10:12:24this in the comment section down below
- 10:12:26that is local date there's local time to
- 10:12:29get the time let's replace date with
- 10:12:32time local time.
- 10:12:35now and rename this as time cuz that's
- 10:12:38more
- 10:12:39appropriate print the time currently the
- 10:12:42time for me is a little after 9:00 a.m.
- 10:12:469 hours 29 minutes 52 seconds
- 10:12:49and a bunch of
- 10:12:51milliseconds to retrieve both the date
- 10:12:53and the time you can use date time local
- 10:12:57date
- 10:13:00time local dat time.now method print the
- 10:13:04current date
- 10:13:06time and currently for me it is December
- 10:13:0917th 2024 at about 9:30 in the
- 10:13:13morning you can also use UTC time that's
- 10:13:16a little different we will create a
- 10:13:19instant object instant instant equals
- 10:13:24access the class of instant called the
- 10:13:26now
- 10:13:27method and print our instant
- 10:13:30object which will give us the current
- 10:13:33date and time in
- 10:13:34UTC December 17th 2024 in UTC time I
- 10:13:39think that's 300 p.m. it's in military
- 10:13:42time that's how to get either the
- 10:13:44current date time date time or UTC
- 10:13:48timestamp when you output the current
- 10:13:50date and/ or time it's kind of ugly and
- 10:13:52hard to read well we can write a custom
- 10:13:54format to display our date time here's
- 10:14:00how here we'll create a custom format
- 10:14:03for our date time we will need the
- 10:14:05current date
- 10:14:06time local date
- 10:14:09time we will name this date time equals
- 10:14:13access the class of local date time
- 10:14:16called the now method
- 10:14:19then we'll need to create a formatter
- 10:14:21object date time
- 10:14:25formatter we'll name this formatter
- 10:14:28equals date time formatter I'm just
- 10:14:30going to copy it call the of pattern
- 10:14:34method and then we're going to pass on a
- 10:14:37string let me move the cursor so you can
- 10:14:40see this within the string we will write
- 10:14:43pattern letters what is the pattern in
- 10:14:45which we would like to display our date
- 10:14:47and our time if you go to the official
- 10:14:49Java documentation there's an extensive
- 10:14:51list here's a few beginner friendly ones
- 10:14:54for the day type 2DS dash for the month
- 10:14:58two Capital M's for the year Das y y y y
- 10:15:03four
- 10:15:04y's this will display the day the month
- 10:15:08and the year I'm an American so we
- 10:15:10display the month first followed by the
- 10:15:12day so I'm going to switch this around
- 10:15:14but you can keep it as it originally was
- 10:15:16if you prefer so that's how to display
- 10:15:18the date in that
- 10:15:19format month day year now for the time
- 10:15:23you can do the following for hours do
- 10:15:25two Capital H's colon minutes is two
- 10:15:29lowercase M's colon then for seconds
- 10:15:32it's two lowercase s's that is a good
- 10:15:36pattern we will create a new
- 10:15:38string named new date
- 10:15:42time equals take our current date time
- 10:15:45it's an object call the format method
- 10:15:50we are going to pass in our formatter to
- 10:15:53format our date time to this
- 10:15:56pattern once we have our new date time
- 10:15:59we're going to print it let's output our
- 10:16:02new date time and it should be in that
- 10:16:06format yeah that looks pretty good
- 10:16:08December 17th 2024 about 9:00 in the
- 10:16:11morning 9:30 at least for me instead of
- 10:16:15getting the time and date right now we
- 10:16:17can create a custom date time object
- 10:16:20here's how so we'll start with a local
- 10:16:23date local date I'll name this object
- 10:16:27date equals local date we're accessing
- 10:16:32the class statically we'll call the of
- 10:16:34method depending on the date you would
- 10:16:36like to create we'll pass them in as
- 10:16:38arguments let's say I would like to pick
- 10:16:40Christmas of
- 10:16:422024 the first argument corresponds to
- 10:16:44the year I will pick
- 10:16:462024 the next is for the month 12
- 10:16:49meaning
- 10:16:50December then the 25th 25th of December
- 10:16:54I'll type in
- 10:16:5525 all right just to test it I'm going
- 10:16:57to Output it let's print our
- 10:17:02date the year is 2024 December
- 10:17:0825th you can also use date time let's
- 10:17:11switch local date to local date
- 10:17:16time we need a few more arguments ments
- 10:17:19so let's say noon on Christmas Day that
- 10:17:22would be
- 10:17:2312 for the minutes 0o for the seconds
- 10:17:26zero then let's print this again let's
- 10:17:30output our date well technically it
- 10:17:32should be date time but that still makes
- 10:17:36sense here's our date
- 10:17:38time December 25th 2024 at noon 12:00
- 10:17:43p.m. you can also compare dates too
- 10:17:46let's copy the snippet of code paste
- 10:17:50it rename the first date as date one the
- 10:17:53second date as date two for the second
- 10:17:56date let's set that to New Year's Day
- 10:17:582025 the month will be January the 1st
- 10:18:02of
- 10:18:04January at midnight that would be
- 10:18:06zero let's print date one and then date
- 10:18:12two so we have
- 10:18:14Christmas December 25th 2024 and new
- 10:18:18Year's Day January 1st
- 10:18:212025 all right now to compare dates we
- 10:18:24can use the following we'll use an if
- 10:18:27statement let's check if date
- 10:18:30one called the is before method dates
- 10:18:35have a built-in is before method pass in
- 10:18:37date two is date one before date two and
- 10:18:41that is
- 10:18:42true let's output date one plus a string
- 10:18:47of is earlier than plus date 2 let's
- 10:18:53check that our first date Christmas is
- 10:18:57earlier than New Year's our second
- 10:19:02date else
- 10:19:04if date
- 10:19:07one let's just copy this date one is
- 10:19:11after date two then we'll output
- 10:19:14something
- 10:19:15different date one is later than date
- 10:19:19two so let's change date 1 to be January
- 10:19:242nd of
- 10:19:282025 the second of January is later than
- 10:19:32New Year's Day our second
- 10:19:35date you can also check if they're
- 10:19:38equal let's add else
- 10:19:41if let me scroll down date 1 is equal to
- 10:19:48date 2
- 10:19:50and then we'll say that they're
- 10:19:52equal date 1 is equal
- 10:19:57to date
- 10:19:59two let's set these to be the same New
- 10:20:03Year's Day at
- 10:20:06midnight New Year's Day at midnight is
- 10:20:09equal to New Year's Day at midnight this
- 10:20:12could be good for some sort of alarm
- 10:20:13clock checking to see if two date times
- 10:20:15are
- 10:20:17equal all right everybody so that is an
- 10:20:19introduction to working with dates and
- 10:20:21times using
- 10:20:23Java well hello again friends today I
- 10:20:25got to explain Anonymous classes in Java
- 10:20:28so what is an anonymous class it's a
- 10:20:30class that doesn't have a name meaning
- 10:20:32it cannot be reused it's when you want
- 10:20:34to add custom Behavior without having to
- 10:20:36create a new class because it can be
- 10:20:39really inconvenient to create a new
- 10:20:40class just for one object if that one
- 10:20:43object is different from the rest in one
- 10:20:45way or another they're often used for
- 10:20:47onetime uses when utilizing features
- 10:20:49such as timer task the runnable
- 10:20:52interface or callbacks these are more
- 10:20:54advanced Java topics so let me
- 10:20:57demonstrate why an anonymous class would
- 10:20:59be useful let's begin with this example
- 10:21:01where we create a dog class file new
- 10:21:04Java class we will create a class of
- 10:21:08dog let's say that in this dog class
- 10:21:10there's a single method a speak
- 10:21:13method void speak all we'll do is I'll
- 10:21:17put the following
- 10:21:18the dog goes woof and then let's create
- 10:21:23a dog
- 10:21:24object dog dog equals new
- 10:21:28dog we'll take our dog object have it
- 10:21:31use its speak method which will
- 10:21:34output the dog goes wo so what if
- 10:21:38there's a unique kind of dog it's
- 10:21:40different from the rest let's say it's
- 10:21:42scoobydoo now
- 10:21:44Scooby-Doo he doesn't speak dog he
- 10:21:46speaks English if I want Scooby be do to
- 10:21:48say something else I'd have to override
- 10:21:50this method so one option would be to
- 10:21:53create a new class again this is me not
- 10:21:55using Anonymous classes you'll see their
- 10:21:57usefulness in just a moment let's create
- 10:22:00a new Java class let's create a talking
- 10:22:03dog class I know it's kind of a weird
- 10:22:06example so our talking dog class will
- 10:22:09extend the dog class and then we'll
- 10:22:12override the speak
- 10:22:14method
- 10:22:16override void speak then we'll I'll put
- 10:22:20the
- 10:22:21following Scooby Do says what's he
- 10:22:25usually say rut row or something rut row
- 10:22:29I think that's how you spell it but you
- 10:22:31get the idea going back to our main Java
- 10:22:34file I can create a talking dog object
- 10:22:38talking dog talking dog equals new
- 10:22:43talking dog that's a lot to
- 10:22:45type and then I can have my talking dog
- 10:22:49use its speak
- 10:22:53method our dog goes woof and Scooby do
- 10:22:56our talking dog says rutro like uh- oh
- 10:23:00it is a lot of work to create a whole
- 10:23:02new class just for one unique object our
- 10:23:05talking dog of Scooby-Doo is the
- 10:23:07exception so rather than creating an
- 10:23:09entirely new class just for this one
- 10:23:11object just for Scooby-Doo our talking
- 10:23:14dog let's instead create an anonymous
- 10:23:17class we'll take our our talking dog
- 10:23:19class and delete
- 10:23:25it instead of creating a talking dog
- 10:23:28object let's create another dog object
- 10:23:31but it's going to have some unique
- 10:23:32features that set it apart from other
- 10:23:34dogs such as the ability to speak
- 10:23:36English let's rename dog is Dog one
- 10:23:40we'll create another dog named dog 2
- 10:23:43equals new dog add aside a parenthesis
- 10:23:47now to create an anonymous class you're
- 10:23:49going to add a set of curly
- 10:23:51braces then make sure there's that
- 10:23:53semicolon at the end within the set of
- 10:23:55curly braces you can Define any unique
- 10:23:58features or override any methods dog 2
- 10:24:02is going to be different from all other
- 10:24:03dogs with it speak method so let's
- 10:24:06override that method at override void
- 10:24:12speak and then we'll
- 10:24:15say Scooby Do
- 10:24:19says rut
- 10:24:22row dog one is going to speak and dog
- 10:24:29two the dog goes woof Scooby-Doo says
- 10:24:33rut
- 10:24:34R so instead of creating an entirely new
- 10:24:37class just for one dog Scooby-Doo we can
- 10:24:40create a normal dog and just override
- 10:24:42one of the methods using an anonymous
- 10:24:44class so those are Anonymous classes
- 10:24:47it's a class that doesn't have a name
- 10:24:49and it cannot be reused if you need to
- 10:24:51reuse a class you're better off just
- 10:24:53creating a new class it allows you to
- 10:24:55add custom Behavior without having to
- 10:24:57create a new class which is what we did
- 10:24:59with the speak method Anonymous classes
- 10:25:01are often used with timer task which
- 10:25:03we'll talk about in the next video the
- 10:25:05runnable interface and callbacks so it
- 10:25:08is an important topic that you need to
- 10:25:09know going forward at least to create an
- 10:25:12anonymous class after you construct an
- 10:25:14object add a set of curly braces within
- 10:25:16that set of curly braces you can Define
- 10:25:18new behavior and well everybody those
- 10:25:21are Anonymous classes in Java so uh yeah
- 10:25:25I got to talk about timers and timer
- 10:25:27tasks in Java timer is a class that
- 10:25:30schedules tasks at specific times or
- 10:25:33periodically timer is useful for sending
- 10:25:36notifications scheduled updates or
- 10:25:38repetitive actions when you need to
- 10:25:40schedule a task at a specific time you
- 10:25:43can use the timer class now timer task
- 10:25:46is the task that's going to execute cute
- 10:25:48when our timer says so in order to use
- 10:25:51timer task you're going to extend the
- 10:25:54timer task class and then Define your
- 10:25:56task and we're going to be doing this by
- 10:25:58using Anonymous classes which we learned
- 10:26:01about in the last video we'll Begin by
- 10:26:03creating a timer we need a timer to well
- 10:26:05keep track of the time let's say timer
- 10:26:08timer equals new
- 10:26:11timer and then import these classes I'm
- 10:26:14pretty sure you're used to importing
- 10:26:16these by now
- 10:26:18we're going to select the timer from the
- 10:26:20utility
- 10:26:21package all right we have our timer then
- 10:26:25we need a timer task what are we going
- 10:26:27to do when the timer is
- 10:26:31up let's just say task timer task task
- 10:26:35equals new timer task import the
- 10:26:42class all right so here's the
- 10:26:45situation with our timer class we have
- 10:26:47to implement the run method to override
- 10:26:50the run method we could create a child
- 10:26:52class from the timer task class but
- 10:26:55here's a better option we're going to
- 10:26:57create an anonymous class after the set
- 10:26:59of parentheses we will add a set of
- 10:27:01curly braces then add that semicolon to
- 10:27:03the end within this Anonymous class we
- 10:27:06can update or change the behavior of our
- 10:27:08timer task object what we'll do is
- 10:27:11override the run
- 10:27:13method
- 10:27:16override to overrun this method
- 10:27:18this will be public void run it's the
- 10:27:23run
- 10:27:24method when we execute our task what
- 10:27:27would we like to do let's output the
- 10:27:29following let's just say
- 10:27:32hello so our task is to print
- 10:27:36hello but we have to do it when our
- 10:27:38timer says so so they work
- 10:27:41together let's say I would like to say
- 10:27:43hello after 3 seconds to do that we're
- 10:27:46going to take our timer
- 10:27:48call the schedule
- 10:27:51method and then there's two arguments
- 10:27:53we're going to pass in our task what are
- 10:27:56we going to do that's the first argument
- 10:27:59the next argument is the delay after how
- 10:28:02many milliseconds will we execute this
- 10:28:04task I said 3 seconds let's say 3,000
- 10:28:08milliseconds then after a delay of 3
- 10:28:11seconds 3,000 milliseconds do your task
- 10:28:15that we're passing in so after 3 seconds
- 10:28:18will
- 10:28:21output
- 10:28:23hello or if this were 1,000 millisecs it
- 10:28:26would only take a
- 10:28:28second hello you can also schedule at a
- 10:28:32fixed rate our timer can do things
- 10:28:36periodically so let's repeat our task of
- 10:28:38saying hello more than just once so we
- 10:28:41have our delay you know what I'll set
- 10:28:44that to be zero the next argument is for
- 10:28:47the period what is the delay in
- 10:28:49milliseconds between repetitions let's
- 10:28:52say 1,000 milliseconds so 1
- 10:28:55second starting immediately 0
- 10:28:59milliseconds print hello every 1,000
- 10:29:02milliseconds every second let's try
- 10:29:07it hello hello hello hello I think you
- 10:29:12get the idea or you could add a delay
- 10:29:15that's the second argument let's say
- 10:29:17after a delay delay of 3,000
- 10:29:19milliseconds 3 seconds repeat this line
- 10:29:22of code every 1,000 milliseconds 1
- 10:29:25second so now there's going to be a
- 10:29:28delay of 3
- 10:29:29seconds then we output hello every 1
- 10:29:33second now how do you cancel this timer
- 10:29:36once it's started well you can use the
- 10:29:38cancel
- 10:29:40method where should we add that exactly
- 10:29:43well we can add that within our task
- 10:29:45we'll need some sort of stopping
- 10:29:46condition kind of like a wild Loop so
- 10:29:49let's say this I would like to say hello
- 10:29:51three times I'll create a local variable
- 10:29:55of count I'll set that to be
- 10:29:59three intellig wants me to make this
- 10:30:01final you don't have to I'll keep it as
- 10:30:04is so again we are within our Anonymous
- 10:30:07class we're declaring a variable to use
- 10:30:09within the context of our
- 10:30:11task within the run method we'll output
- 10:30:15hello decrement our count by one count
- 10:30:18minus
- 10:30:20minus then using an if statement this is
- 10:30:23how we're going to escape if our count
- 10:30:27is less than
- 10:30:28zero then let's
- 10:30:31say task
- 10:30:35complete and then to cancel the timer we
- 10:30:37will take our timer object called the
- 10:30:40cancel
- 10:30:42method all right let's try it I'll set
- 10:30:46the delay to be zero so it starts
- 10:30:48immediately every 1,000 milliseconds
- 10:30:51print hello whoops it looks like that's
- 10:30:53less than or equal to zero I fixed it
- 10:30:57all right so starting immediately the
- 10:30:59delay is zero every 1,000 milliseconds
- 10:31:02print hello but only do it three times
- 10:31:05and then we cancel the timer to stop
- 10:31:07because we don't want to say hello
- 10:31:08forever and here it
- 10:31:11is 1 2 3 task
- 10:31:16complete all right everybody so that is
- 10:31:18timer and timer task timer is a class
- 10:31:21that schedules tasks at specific times
- 10:31:24or periodically it's useful for sending
- 10:31:27notifications scheduled updates or other
- 10:31:30repetitive actions timer task is the
- 10:31:33task you're going to perform when the
- 10:31:35timer says so in the next video we're
- 10:31:37going to use timer and timer task to
- 10:31:39create a countdown timer all right
- 10:31:41everybody so that is both timer and
- 10:31:42timer task in
- 10:31:44Java why hello there everybody in this
- 10:31:47video we're going to create a countdown
- 10:31:49timer program using Java this is meant
- 10:31:51to serve as more of a mini project to
- 10:31:53get us used to working with timer tasks
- 10:31:55let's begin we'll accept user input but
- 10:31:58we're going to set that up near the end
- 10:32:00it's going to be easier for us to
- 10:32:01understand this program at first if we
- 10:32:03set the countdown timer to be a raw
- 10:32:05number like 10 to work with timer tasks
- 10:32:08we'll need a timer to keep track of the
- 10:32:09time timer timer equals new
- 10:32:14timer we'll be importing our classes as
- 10:32:17we go along long be sure to do that
- 10:32:19import them from the Java utility
- 10:32:22package then we'll need a timer task
- 10:32:25timer task I'll name this task for short
- 10:32:28equals new timer task be sure to import
- 10:32:32this
- 10:32:33class now there's one issue though with
- 10:32:36our timer task object we have to
- 10:32:38override the run method one option
- 10:32:41although it's not the best is to create
- 10:32:42a new class and then inherit from the
- 10:32:44timer task class so this would be the
- 10:32:47parent class and then we can override
- 10:32:49the run method but since we don't plan
- 10:32:51on reusing that class what we could do
- 10:32:53instead is use an anonymous class after
- 10:32:56the set of parentheses add a set of
- 10:32:57curly braces then add that semicolon to
- 10:33:00the end for a onetime use we can
- 10:33:03override the run method of our timer
- 10:33:04task object because we only plan on
- 10:33:07using it once so we can write an
- 10:33:08anonymous class rather than create a
- 10:33:10whole new class that we're only going to
- 10:33:12use once so we will
- 10:33:15override the run method
- 10:33:18public void
- 10:33:21run when our timer executes our task
- 10:33:24what would we like to do well we're
- 10:33:26going to Output a number the number for
- 10:33:28our countdown timer so within our
- 10:33:30Anonymous class let's create an integer
- 10:33:33variable of count for the time being
- 10:33:36let's set this to a low number like five
- 10:33:40intelligent wants us to make this
- 10:33:41variable final but we'll be accepting
- 10:33:43user input momentarily so you don't need
- 10:33:45to do that my IDE of intelligent is
- 10:33:48recommending that you can just ignore
- 10:33:49that so let's say that our count is
- 10:33:51going to start at five and we'll
- 10:33:53decrement by one then display happy New
- 10:33:56Year when count to zero so why don't we
- 10:33:59do the following within our run method
- 10:34:02we'll output our count variable at first
- 10:34:06it's going to be
- 10:34:07five then we'll decrement count by one
- 10:34:11count minus
- 10:34:13minus we'll need to escape the run
- 10:34:15method well we can do that with an if
- 10:34:17statement
- 10:34:19we will check if our count is less than
- 10:34:23zero if it is let's output happy New
- 10:34:30Year and then to stop our timer we'll
- 10:34:33take our timer object called the cancel
- 10:34:37method now we have to set up our timer
- 10:34:40we will do that outside the timer task
- 10:34:42Anonymous
- 10:34:43class so right here is good we will take
- 10:34:47our timer called the schedule
- 10:34:51method schedule will take a task our
- 10:34:54timer
- 10:34:55task and execute it after a delay let's
- 10:34:58set the delay to be zero now this is
- 10:35:01only going to run once there's a better
- 10:35:03method for this let me just
- 10:35:05demonstrate so we get the number five
- 10:35:08and our program
- 10:35:11stops so what we're going to use instead
- 10:35:13is schedule at fixed rate
- 10:35:18the schedule at fixed rate method but
- 10:35:20there's three arguments we have to pass
- 10:35:22in first is the task the delay and then
- 10:35:26the
- 10:35:27period we have our task the delay is
- 10:35:30zero what is the period in between
- 10:35:33executions let's set that to be 1,000
- 10:35:36milliseconds every 1 second 1,000
- 10:35:40milliseconds perform your task and our
- 10:35:43task is to display our countdown
- 10:35:45whatever the count currently is
- 10:35:47all right let's test this again we're
- 10:35:50setting our count to be five initially
- 10:35:52but we'll be accepting user input
- 10:35:55momentarily we start at 5 and we're
- 10:35:57going to count down to zero then display
- 10:36:00happy New
- 10:36:01Year all right after zero we display
- 10:36:04happy New Year and our program exits
- 10:36:07because we canceled the
- 10:36:08timer now if we don't cancel the timer
- 10:36:11I'm just going to cut this momentarily
- 10:36:14it's going to continue forever
- 10:36:22we keep on displaying happy New Year and
- 10:36:24we're going into negative
- 10:36:26seconds so that's why we need to cancel
- 10:36:29the timer so that it stops all right now
- 10:36:32we'll accept user input because we want
- 10:36:34a user to type in the number to
- 10:36:36countdown from rather than us
- 10:36:38programmers just typing in a random
- 10:36:40number so we will need a
- 10:36:44scanner scanner scanner equals new
- 10:36:48scanner then pass in
- 10:36:51system.in import this class and then
- 10:36:55we'll need a
- 10:36:56prompt let's output the
- 10:36:58following enter number of seconds to
- 10:37:06countdown
- 10:37:07from I'll use print rather than print
- 10:37:11line let's create an integer variable of
- 10:37:15response set that equal to use our
- 10:37:18scanner call the next int method to get
- 10:37:21the next integer from the user then we
- 10:37:24will set our count variable equal to the
- 10:37:27response whatever the user types in so
- 10:37:30now the user can type in a number and we
- 10:37:32can count down from that number rather
- 10:37:34than just placing a random number here
- 10:37:36all right let's test
- 10:37:39this enter number of seconds to count
- 10:37:41down from let's say 10 we're going to
- 10:37:44count down from 10 so we start at 10 and
- 10:37:47we're going to count down to
- 10:37:55zero zero happy New Year and then the
- 10:37:58program exits so you can type in any
- 10:38:01number you could even set this to a
- 10:38:03higher number like
- 10:38:0660 but I'm not going to make you wait
- 10:38:08that
- 10:38:09long all right everybody that is a
- 10:38:12countdown timer program that you can
- 10:38:13write as a mini project using Java hello
- 10:38:17there every everybody in this video I
- 10:38:18got to cover generics in Java generics
- 10:38:21it's a concept it's where you can write
- 10:38:23a class interface or method and it's
- 10:38:26compatible with different data types
- 10:38:28there's two things we need a type
- 10:38:30parameter and a type argument so with
- 10:38:33type parameters it's a set of angle
- 10:38:35brackets with a letter inside for
- 10:38:37example T basically this acts as a
- 10:38:40placeholder and it gets replaced with
- 10:38:42the real type now on the other hand type
- 10:38:44arguments with various objects and data
- 10:38:46structures you'll see a pair of angle
- 10:38:48brackets with a type inside such as a
- 10:38:50string but it really can be anything it
- 10:38:52can be an integer or a
- 10:38:56Boolean really anything type arguments
- 10:38:59specify the type we send arguments to
- 10:39:02parameters really our type parameter is
- 10:39:04just acting as a placeholder anything
- 10:39:06that uses this type parameter it doesn't
- 10:39:08know the data type that it's going to
- 10:39:10receive it's set up to receive really
- 10:39:12anything or a primitive using a rapper
- 10:39:14class let me give you an example of
- 10:39:16where you already see a gener being used
- 10:39:18for example array lists to create an
- 10:39:21array list we add a set of angle
- 10:39:23brackets after array list and we have to
- 10:39:25specify the type what is this array list
- 10:39:28going to store for example strings we
- 10:39:30would write the data type of string
- 10:39:33let's say that this array list is named
- 10:39:35fruits fruits equals new array list and
- 10:39:40then we need a set of angle brackets
- 10:39:41again parenthesis semicolon in modern
- 10:39:45Java within the second set of angle
- 10:39:46brackets you don't necessarily need to
- 10:39:48type the data type again Java can infer
- 10:39:51that so you can leave it empty this is
- 10:39:53the diamond
- 10:39:54operator we have an array list that can
- 10:39:57store strings we have specified the type
- 10:39:59argument and now we can store some
- 10:40:01strings within our array list so let's
- 10:40:04take our array list of fruits call the
- 10:40:06ad method and add a few names of fruits
- 10:40:09such as an
- 10:40:10apple let's do two
- 10:40:12more Apple orange banana
- 10:40:18currently the way that we set up this
- 10:40:19array list it's compatible with strings
- 10:40:22because that's what we set the type
- 10:40:23argument to be now if I set this to be
- 10:40:26integers well we can't store strings
- 10:40:28anymore but we could store numbers 1 2 3
- 10:40:33that's
- 10:40:35fine let me change that back generally
- 10:40:38speaking an array data structure can
- 10:40:40store all sorts of different data types
- 10:40:43but when we create one we have to
- 10:40:44specify what we're storing that's the
- 10:40:47type argument it's because within the
- 10:40:49array list class we have a type
- 10:40:51parameter set up a set of angle brackets
- 10:40:54with a letter inside in many cases
- 10:40:56you'll see t meaning type for type
- 10:40:59parameter so let's actually take a look
- 10:41:01at our array list I am going to locate
- 10:41:05array list jump to Source here's the
- 10:41:08class for our array list there's a lot
- 10:41:10of advanced Java in here don't worry
- 10:41:12about that but what I want you to pay
- 10:41:14attention to is the type parameter that
- 10:41:16comes after after the array list class
- 10:41:18name we have angle brackets and a letter
- 10:41:20inside in this case e meaning element
- 10:41:23because an array list has elements of
- 10:41:25data by using this type parameter our
- 10:41:28array list is set up to store elements
- 10:41:30of various types we just have to specify
- 10:41:32what the type is going to be when we
- 10:41:34actually do store data and in my example
- 10:41:37we're using strings but like I said
- 10:41:39before we can change the data type of
- 10:41:41what we're storing we just have to
- 10:41:43change the type argument I'll store
- 10:41:45booleans true
- 10:41:50false
- 10:41:52true I don't know what that has to do
- 10:41:54with fruits but I'm just trying to prove
- 10:41:55a point
- 10:41:58here so now what we're going to do is
- 10:42:00actually use type parameters so let's
- 10:42:03delete our array list using generics you
- 10:42:06can write some logic within a class
- 10:42:08interface or method and it's compatible
- 10:42:10with many different data types so what
- 10:42:13we're going to do in this example is
- 10:42:14create a class of box
- 10:42:18class
- 10:42:20box we'll be using a type parameter to
- 10:42:23store all sorts of different things
- 10:42:24within our box we can store a string
- 10:42:27within our box an integer a double A
- 10:42:29Boolean even more complex objects so we
- 10:42:32will set up a type parameter with a set
- 10:42:35of angle brackets then type t t is a
- 10:42:38common convention meaning type let's say
- 10:42:41that when we create a box object our box
- 10:42:44is going to act as a container we'll
- 10:42:45store a value inside
- 10:42:48our box is going to be a reusable class
- 10:42:50we won't always know what the data type
- 10:42:52of what we're storing is going to be so
- 10:42:55what we could do is set the data type to
- 10:42:58be T let's say that this variable is an
- 10:43:01item we're storing an item within a box
- 10:43:04with t it does mean type but I like to
- 10:43:07think of it as thing we're storing a
- 10:43:10thing an item within our box we don't
- 10:43:13know what this thing is going to be but
- 10:43:16really it means type and that's not as
- 10:43:18fun T item we don't know what the data
- 10:43:21type is if it's always going to be a
- 10:43:23string we could just set this to be a
- 10:43:25string but what if somebody is storing
- 10:43:28an integer an INT or we can use the
- 10:43:31rapper class of integer we don't know
- 10:43:34what the type is going to be let's
- 10:43:36create a method where we will set our
- 10:43:38item let's say that this is public void
- 10:43:41it's not going to return anything set
- 10:43:46item we'll have one parameter what's the
- 10:43:49data type of what we're receiving well
- 10:43:51we don't know so we're going to use our
- 10:43:53type parameter of
- 10:43:55t t is the data type it's generic we
- 10:43:59will receive an
- 10:44:01item let's take this. item and set it to
- 10:44:05be the item that we
- 10:44:07receive we'll create a method to get our
- 10:44:10item public now we're going to be
- 10:44:13returning an item the data type is T so
- 10:44:17the return type is also going to be T
- 10:44:19we're returning a value of the T data
- 10:44:22type we will create a method to get
- 10:44:27item we will return this. item we can
- 10:44:32put things in our box and we can get
- 10:44:34things from our
- 10:44:36box all right this class is done our box
- 10:44:40class is set up to be reusable we can
- 10:44:42store all sorts of different things so
- 10:44:45to say within our box we will attempt to
- 10:44:48create a box object for now let's say
- 10:44:51box box equals new box but there's one
- 10:44:55step that we're
- 10:44:56missing we have a warning here raw use
- 10:45:00of parameterized class
- 10:45:02box we need to set up a type argument
- 10:45:06what are we going to be putting in our
- 10:45:07box what's its data type our box class
- 10:45:10wants to know so we will use a type
- 10:45:14argument after the class of box add a
- 10:45:17set of angle brackets then type what
- 10:45:19we're going to store so let's say we're
- 10:45:22going to store strings within our box
- 10:45:24values of the string data type and then
- 10:45:26we do need the diamond operator after
- 10:45:29the second box and we're good we now
- 10:45:32have a box object but it's set up to
- 10:45:34store strings let's add some items to
- 10:45:36our box we've even created a few methods
- 10:45:39for this set item let's take our box
- 10:45:43call set item but we have to pass in a
- 10:45:45string let's add a banana to our
- 10:45:49box and then we will get the banana from
- 10:45:52the box using the get item method and
- 10:45:56now I want to get the banana box. get
- 10:46:02item here we go we have our banana we
- 10:46:07have successfully retrieved the banana
- 10:46:09from the
- 10:46:10box our box class is compatible with all
- 10:46:13sorts of different data types this time
- 10:46:16let's store an integer we will use the
- 10:46:19wrapper class of integer but we can no
- 10:46:21longer add a banana to the Box because
- 10:46:23it's a string I'll attempt to do so
- 10:46:26string cannot be converted to
- 10:46:29integer unfortunately bananas are not
- 10:46:32numbers but I can add the number three
- 10:46:35to our
- 10:46:37box we have the number three within our
- 10:46:39box if I attempt to add a double like
- 10:46:423.14 we get a warning
- 10:46:47incompatible types double cannot be
- 10:46:49converted to
- 10:46:50integer so I would have to set the type
- 10:46:53argument to be double to add doubles to
- 10:46:56our
- 10:46:57box
- 10:46:593.14 basically with our box class we're
- 10:47:02writing the logic of how a box works you
- 10:47:05put something in and you can take
- 10:47:07something out and it's compatible with
- 10:47:09different data
- 10:47:10types we just have to use a type
- 10:47:12argument when creating a box object we
- 10:47:15have to let Java know what we're storing
- 10:47:17within it exactly all right let me give
- 10:47:19you another example now with type
- 10:47:21parameters you can have more than one
- 10:47:23type we can have two or more different
- 10:47:25types usually the common convention is
- 10:47:28to use U after
- 10:47:31T so we're going to create a new class
- 10:47:34file new Java class we will create a
- 10:47:37class of
- 10:47:39product here we're going to write the
- 10:47:41logic of how a product works with a
- 10:47:43product you have an item and you have a
- 10:47:46price we will set up a type parameter
- 10:47:49let me zoom in we'll have one type of t
- 10:47:52and a common convention for the second
- 10:47:54type is U because in the English
- 10:47:56alphabet the letter U comes after T if
- 10:47:59you had a third argument that would be V
- 10:48:01by Common
- 10:48:03convention so we'll just stick with t
- 10:48:05and
- 10:48:07u we will have a generic type of
- 10:48:12item and another generic type of price
- 10:48:17we don't know if our item is going to be
- 10:48:18a string or a more complex object our
- 10:48:21price could be an integer a floating
- 10:48:23Point number a double we don't know but
- 10:48:26that's why we're setting it up this
- 10:48:29way now we'll need a Constructor we will
- 10:48:31take our
- 10:48:33product then set up some parameters the
- 10:48:36data type of our item is t t item but if
- 10:48:41we're creating a product we also need a
- 10:48:43price the data type of our price is u
- 10:48:47U price this. item equals the item that
- 10:48:52we
- 10:48:52receive this. price equals the price
- 10:48:56that we
- 10:48:57receive okay let's do a test run let's
- 10:49:00go to our main Java
- 10:49:02file product product equals new
- 10:49:08product and again we need those type
- 10:49:11arguments so this time we actually need
- 10:49:14two because our class is set up to
- 10:49:16receive two type arguments T and
- 10:49:20U for our product let's say that the
- 10:49:23first type is going to be a string
- 10:49:25followed by a double and then we need
- 10:49:28the diamond operator after the second
- 10:49:31product all right now we have to pass in
- 10:49:33arguments to the class because we have a
- 10:49:35Constructor we have to pass in an item
- 10:49:38and its
- 10:49:39price and with our type arguments we set
- 10:49:42that up to be a string and a double we
- 10:49:44have to pass in those types for our
- 10:49:47product let's say that it's an apple and
- 10:49:50an apple is 50 Cents that's a
- 10:49:53double let's create two methods within
- 10:49:55our product class we will create a
- 10:49:58method to get our item let's say public
- 10:50:03we're returning an item the data type is
- 10:50:05T so we're going to return
- 10:50:07T get
- 10:50:11item we will return this do
- 10:50:15item then to return the
- 10:50:18price let's say public U cuz we're
- 10:50:22returning the price it has a type of
- 10:50:25U Get
- 10:50:28price return this do
- 10:50:33price we have our product
- 10:50:36object I'm going to Output take our
- 10:50:39product object called the get item
- 10:50:44method and this is going to return are
- 10:50:48apple or I could get the price get
- 10:50:54price and that is
- 10:50:560.5 you can use string format but that
- 10:50:59might be overkill for this lesson all
- 10:51:01right let's create a second product so
- 10:51:03we'll have product
- 10:51:05one and product
- 10:51:08two Now product two will use an integer
- 10:51:11instead of a
- 10:51:13double and let's say that these are
- 10:51:15tickets like movie tickets ticket and
- 10:51:19the price will be $15 for
- 10:51:22$15 let's take product
- 10:51:26two call the get item
- 10:51:30method so we have our
- 10:51:34ticket and we should have a price get
- 10:51:39price the price of a movie ticket is$ 15
- 10:51:43$15 all right everybody so that is an
- 10:51:45introduction to two generics it's a
- 10:51:48concept where you can write a class
- 10:51:49interface or method that is compatible
- 10:51:52with different data types basically
- 10:51:54we're writing the logic of how this
- 10:51:56class works and it's compatible with all
- 10:51:59different types you just have to set up
- 10:52:01a type parameter and then pass in type
- 10:52:04arguments and well everybody that is an
- 10:52:06introduction to generics in Java well
- 10:52:10it's up everybody so today I got to
- 10:52:12explain hashmaps in Java hashmaps are a
- 10:52:15popular data structure
- 10:52:17they store a pair of values a key value
- 10:52:20pair you have a key and a value
- 10:52:22associated with that key the keys have
- 10:52:24to be unique but the values can be
- 10:52:27duplicated hashmaps do not maintain any
- 10:52:30order but they are very memory efficient
- 10:52:32when creating a hashmap we have to set
- 10:52:34up type parameters we have to specify
- 10:52:37what the data type of the key is and its
- 10:52:39value here's how to create a hashmap we
- 10:52:42will type hashmap let's say map for
- 10:52:46short
- 10:52:47hashmap map equals new hash
- 10:52:52map we'll need to import this class it
- 10:52:55looks like we're missing something let's
- 10:52:57take a look raw use of parameterized
- 10:53:00class
- 10:53:01hashmap if we take a look at the hashmap
- 10:53:03class we have type parameter set up
- 10:53:06there's two values we learned about type
- 10:53:09parameters in the last video on generics
- 10:53:11we can store any two types of values
- 10:53:13within a hashmap we have a key and a
- 10:53:16value represented by K and V we have to
- 10:53:19send type arguments to the type
- 10:53:21parameters when creating a hashmap we'll
- 10:53:23have to set up some type arguments so
- 10:53:26after our hashmap we have to list the
- 10:53:28data type of what we're storing these
- 10:53:30have to be reference data types so let's
- 10:53:33say with our map we're going to have
- 10:53:34products an item and a price so for the
- 10:53:38item that could be a string a string
- 10:53:41representation of our
- 10:53:42item so for the price we could do an
- 10:53:45integer but I think a double would be
- 10:53:48better because you can have dollars and
- 10:53:50cents then we also need to use the
- 10:53:52diamond operator after the second
- 10:53:54hashmap you don't need to State the data
- 10:53:56types again even though you technically
- 10:53:58could Java can actually infer what the
- 10:54:01data type is so after the second hashmap
- 10:54:04add the diamond operator and there we go
- 10:54:07we have our hashmap that we have named
- 10:54:09map the way that we've set this up is
- 10:54:12that we've told the hashmap class we're
- 10:54:14going to store a string as the key and a
- 10:54:16double as the value so to put things in
- 10:54:19a hash map we're going to take our map
- 10:54:22called the put method we're going to put
- 10:54:24something in the hashmap we've specified
- 10:54:27that we're storing a string then a
- 10:54:29double a string for the key and a double
- 10:54:32for the value so let's say we have a
- 10:54:35store and in our store we sell produce
- 10:54:39we will have an apple and its price will
- 10:54:42be 50 CS
- 10:54:440.50 to put some something in a hashmap
- 10:54:47you use the put method we have to pass
- 10:54:49in two values as arguments all right
- 10:54:52let's add a few more
- 10:54:53things we'll have an
- 10:54:56orange an orange will be 75 cents let's
- 10:54:59say then a banana bananas are pretty
- 10:55:03cheap they will be 25 cents all right
- 10:55:08let's take a look at our hash map I'm
- 10:55:10going to show you what happens if we
- 10:55:12output our map directly system.out.print
- 10:55:15line our map our hash
- 10:55:18map and here's what we have we have all
- 10:55:21the key value pairs printed within our
- 10:55:23hash map we have a string and a double
- 10:55:27string double string
- 10:55:30double now with hash Maps the keys have
- 10:55:34to be unique for example let's say I try
- 10:55:37and put another orange within here we'll
- 10:55:39attempt to add another orange and we'll
- 10:55:41see what happens
- 10:55:42exactly put another Orange
- 10:55:47within our map and let's say that the
- 10:55:49price is 1
- 10:55:51million Venezuelan dollars yes oranges
- 10:55:55are very expensive in Venezuela because
- 10:55:57of inflation so here's what happens if
- 10:55:59we have duplicate
- 10:56:01Keys you actually overwrite the previous
- 10:56:04key value pair our orange is now worth 1
- 10:56:07million Venezuelan dollars so do keep
- 10:56:10that in mind hashmaps cannot have
- 10:56:13duplicate keys if you put another key
- 10:56:15value pair with than a hashmap and it
- 10:56:17already exists you'll overwrite it but
- 10:56:19that's also good if you want to change
- 10:56:21one of the key value pairs let's add one
- 10:56:23more key value pair let's say a coconut
- 10:56:26map. put we will put a coconut within
- 10:56:30our hash map coconuts are a little more
- 10:56:33expensive they'll be
- 10:56:35$1 let's print our map to test
- 10:56:38it there's our
- 10:56:41coconut then we can remove an element to
- 10:56:44remove an element take your map call the
- 10:56:46remove
- 10:56:48method here you're going to pass in a
- 10:56:50key let's remove our
- 10:56:53Apple then print our
- 10:56:57map there the apple is gone we have
- 10:57:00orange banana coconut but no apple so
- 10:57:03I'm going to keep that in but turn it
- 10:57:05into a
- 10:57:06comment to get the value associated with
- 10:57:09the key you can use the get method so
- 10:57:12let me delete this print line statement
- 10:57:16we will take our map called the get
- 10:57:20method let's get the value with this
- 10:57:24key give me the value where the key is
- 10:57:27Apple is what we're
- 10:57:28saying that would be
- 10:57:310.5 meaning
- 10:57:3350 or we can get our
- 10:57:38coconut the value associated with this
- 10:57:40key is one meaning
- 10:57:43$1 all right you can also check to see
- 10:57:45if a key or a value exists here's
- 10:57:50how we'll use the contains key method
- 10:57:52take our map call the contains key
- 10:57:56method and then pass in a key is there a
- 10:57:59key of
- 10:58:02banana that returns a Boolean in this
- 10:58:05case
- 10:58:06true is there a pineapple there is not
- 10:58:10so that returns
- 10:58:14false you could use this with than an if
- 10:58:16statement here's
- 10:58:19how we will check
- 10:58:21if let me zoom in if our map call the
- 10:58:26contains key
- 10:58:28method and then pass in a key does our
- 10:58:31map contain any
- 10:58:33apples contains key
- 10:58:36Apple if that returns true we'll
- 10:58:39output
- 10:58:41map. getet then we'll get our Apple
- 10:58:46this will give us the price else if
- 10:58:49contains key is false then we'll output
- 10:58:52something else we'll
- 10:58:56output key not
- 10:59:00found all right let's test this so we do
- 10:59:03have an apple we've set this up to
- 10:59:06return the
- 10:59:07price but let's say we are looking for a
- 10:59:10pineapple contains key
- 10:59:12pineapple if we do find a pineapple we
- 10:59:15will get the price price currently
- 10:59:17there's no pineapples within our map key
- 10:59:20not found so that's how the contains key
- 10:59:23method would be
- 10:59:24useful we also have the capability to
- 10:59:27check to see if a map contains a value
- 10:59:29so we will take our map within a print
- 10:59:31line statement called the
- 10:59:34contains Value method and then pass in a
- 10:59:37value is there anything that's $1
- 10:59:411.0 this returns a Boolean and that's
- 10:59:44true our key of coconut has a value of
- 10:59:481.0 $1 and you do have to pay attention
- 10:59:52to the data type too if I were to pass
- 10:59:54in an
- 10:59:55integer this does return false you do
- 10:59:58have to keep that in mind too that's how
- 11:00:01to check to see if a map contains a key
- 11:00:03and a
- 11:00:05value you can return the size of a map
- 11:00:08using map. size map dot called the size
- 11:00:12method currently there's four key value
- 11:00:14pairs within our map
- 11:00:16this will return the number four to get
- 11:00:19the size of a map call the size
- 11:00:22method all right now the last thing that
- 11:00:24we're going to do if we were to print
- 11:00:26our
- 11:00:28map we're given this ugly
- 11:00:31formatting where we have a set of curly
- 11:00:33braces then all the key value pairs
- 11:00:35comma
- 11:00:37separated this time using a for loop
- 11:00:40we're going to cycle through all the key
- 11:00:41value pairs we will create an enhanced
- 11:00:44for Loop
- 11:00:46what we're going to iterate through is
- 11:00:48every key and these have a string data
- 11:00:50type because that's the way that we set
- 11:00:52them
- 11:00:53up the data type of each element is
- 11:00:56going to be a string which we will name
- 11:00:58key colon think of it as in for every
- 11:01:02key in now what we have to do is get all
- 11:01:06the keys from our map and there's
- 11:01:08actually a method for that a beginners
- 11:01:11friendly way to get all the keys from
- 11:01:13our map what we can do is take our map
- 11:01:16call the key set
- 11:01:19method this will return all the keys
- 11:01:21within our map and they're iterable so
- 11:01:24we can use them within a for Loop for
- 11:01:26every key in our set of keys what do we
- 11:01:29want to do let's do the following within
- 11:01:32a print line statement let's print our
- 11:01:35key plus we'll list each product and its
- 11:01:39price key
- 11:01:41plus maybe I'll add a colon space then
- 11:01:45pick a unit of currency I'll pick
- 11:01:46American dollars for every key we need
- 11:01:49to get its value so we can use the get
- 11:01:52method plus map. getet pass in our key
- 11:01:58by using an enhanced for Loop we can
- 11:02:00customize the output of the key value
- 11:02:02Pairs and I think this looks a lot
- 11:02:05better we have each product each piece
- 11:02:07of fruit and its price laid out in this
- 11:02:11format you could technically use print F
- 11:02:13if you want to display two digits after
- 11:02:15the decimal
- 11:02:16but that might be a little too much for
- 11:02:17this lesson I want to keep it as simple
- 11:02:19as possible all right everybody so those
- 11:02:22are hashmaps it's a data structure that
- 11:02:24stores key value pairs the keys have to
- 11:02:27be unique but the values can be
- 11:02:29duplicated we have to specify the type
- 11:02:32for the key and its value and well
- 11:02:34everybody those are hashmaps in
- 11:02:37Java all right let's do this thing
- 11:02:39everybody today we're talking about
- 11:02:41enums en nums is short for enumerations
- 11:02:44they're a special kind of class they
- 11:02:46represent a fixed set of constants by
- 11:02:49using enums they improve code
- 11:02:51readability and are easy to maintain one
- 11:02:54of the major benefits of enums is that
- 11:02:56they're more efficient with switches
- 11:02:58when comparing strings so basically an
- 11:03:01enum is a special kind of class where we
- 11:03:04declare constants here's a demonstration
- 11:03:06we're going to create a new class file
- 11:03:09new Java class we're going to select
- 11:03:12enum enum is a special kind of class
- 11:03:15sort of like like how an interface is a
- 11:03:16special kind of
- 11:03:18class we will create an enum of
- 11:03:21day public enum Day within this Java
- 11:03:25class we can declare some constants what
- 11:03:28we're going to write is enum constants a
- 11:03:30common convention for these constants is
- 11:03:33that all the letters are uppercase so
- 11:03:35after the enum constant We'll add a set
- 11:03:37of parentheses then associate this
- 11:03:40constant with the value so let's say
- 11:03:42that we're working with days of the week
- 11:03:44Sunday will be the the first day of the
- 11:03:46week Sunday we'll have a value of one
- 11:03:49let's do this with the other days of the
- 11:03:51week Monday parentheses then a value
- 11:03:55Monday will be the second day of the
- 11:03:56week then we'll just follow this
- 11:04:11pattern and all these constants with a
- 11:04:14semicolon all right we have our enum
- 11:04:16constants they have values 1 through 7
- 11:04:20we're receiving some warnings here
- 11:04:22expected zero arguments but one found
- 11:04:25what we need to do is create a
- 11:04:27Constructor a Constructor for our day
- 11:04:30class when we Define these enum
- 11:04:32constants we're automatically going to
- 11:04:34call the Constructor for this enum class
- 11:04:37and then pass in this value let's create
- 11:04:39a field of day number for days of the
- 11:04:41week we'll make these values private
- 11:04:44we'll use the private access modifier we
- 11:04:47don't want the ability to change them
- 11:04:48we'll make them final these are integers
- 11:04:51that's the data type we'll name these
- 11:04:54values day
- 11:04:56number when we Define these enum
- 11:04:59constants we're automatically going to
- 11:05:01call the Constructor and then pass in
- 11:05:03this value to the
- 11:05:04Constructor for the parameters we'll
- 11:05:06have int then our field of day
- 11:05:10number this day
- 11:05:13number equals the day day number that we
- 11:05:18receive if you would like you can add a
- 11:05:20getter method to actually get the day
- 11:05:23number we'll use the public access
- 11:05:26modifier we're returning an integer
- 11:05:29we'll Define a method of get day
- 11:05:33number and we will return our field of
- 11:05:38this. day number all right and we have
- 11:05:41set up our enum class reading and
- 11:05:44understanding these enum constants is a
- 11:05:45lot easier than these numbers if I was
- 11:05:48looking at the number five I would have
- 11:05:49trouble understanding that that means
- 11:05:51Thursday now how do we work with these
- 11:05:53enum constants exactly here's how within
- 11:05:56our main Java
- 11:05:58file we will create a day object by
- 11:06:01using this enum
- 11:06:03class let me delete
- 11:06:06this day day rather than saying new day
- 11:06:12and then passing in a number like one
- 11:06:14there's a different approach
- 11:06:16we will access the day class Dot and
- 11:06:20then specify a constant a day of the
- 11:06:23week let's say Sunday remember that this
- 11:06:26is all caps our day is an object let's
- 11:06:30output our day and see what it
- 11:06:33outputs this will output Sunday all caps
- 11:06:37if you would rather get the day number
- 11:06:40the value that's associated with this
- 11:06:41enum
- 11:06:42constant then we can use that getter
- 11:06:44method
- 11:06:46day. get day
- 11:06:51number so we have Sunday and one where
- 11:06:55this is really going to be helpful is
- 11:06:57when working with
- 11:06:58switches here's a demonstration we will
- 11:07:01actually use an enhanced
- 11:07:03switch what are we examining we're
- 11:07:06examining our day object if our day
- 11:07:08matches a case of Monday now this isn't
- 11:07:12a string If This Were A String we would
- 11:07:14enclose this within a set of quotes but
- 11:07:17using enums within a switch is actually
- 11:07:19faster than using
- 11:07:20strings if it's Monday then we'll output
- 11:07:23the following we'll use an arrow because
- 11:07:25this is an enhanced switch then we'll
- 11:07:28output it is a
- 11:07:31weekday then if we have a case of
- 11:07:33Tuesday let me just copy
- 11:07:36this Tuesday we'll also output it is a
- 11:07:40weekday you can actually combine these
- 11:07:42cases together too you just have to
- 11:07:44comma separate them
- 11:07:46so case Monday comma Tuesday do
- 11:07:50this then let's do this with the other
- 11:07:52days of the week just for readability
- 11:07:54I'll put these on new lines each of
- 11:07:56these
- 11:07:57cases so we have
- 11:08:01Wednesday
- 11:08:04Thursday
- 11:08:06Friday these are all the weekdays Monday
- 11:08:09through
- 11:08:10Friday then if we have case Saturday
- 11:08:12then it's the weekend case Saturday
- 11:08:15today we'll do the following output it
- 11:08:20is the
- 11:08:22weekend and we will also add a case for
- 11:08:26Sunday so currently our day is
- 11:08:29Sunday that will output it is the
- 11:08:32weekend if it were let's say
- 11:08:38Wednesday then instead we output it is a
- 11:08:41weekday if we were comparing strings all
- 11:08:44these cases would be enclosed within a
- 11:08:45set of double quotes kind of like
- 11:08:50this but we don't need to do that it's
- 11:08:53actually more efficient to use
- 11:08:57enums for some practice what we'll do
- 11:08:59this time is accept some user input
- 11:09:01we'll need a
- 11:09:02scanner scanner scanner equals new
- 11:09:07scanner pass it
- 11:09:10system.in import this class be sure to
- 11:09:14close your scanner because I tend to
- 11:09:17forget scanner.
- 11:09:20close then we'll need a prompt we will
- 11:09:23output the following we'll use print
- 11:09:25rather than print
- 11:09:27Line enter a day of the
- 11:09:32week we will create a string variable
- 11:09:35let's say response equals use our
- 11:09:38scanner call the next line
- 11:09:42method all these constants are uppercase
- 11:09:45we'll method chain the two uppercase
- 11:09:47method to make our user input
- 11:09:50uppercase when creating a day object by
- 11:09:53using the enum class we will use our
- 11:09:56response we will use the value of
- 11:10:00method and then pass in our response now
- 11:10:03we should be able to type in a
- 11:10:06day enter a day of the week I will type
- 11:10:09Monday it is a weekday let's run this
- 11:10:12again I will type Sunday it is the
- 11:10:15weekend what if we type in a day that
- 11:10:17doesn't exist like pizza
- 11:10:20day well we get an exception an illegal
- 11:10:23argument
- 11:10:24exception we can catch that exception
- 11:10:26using a try and catch block any
- 11:10:28dangerous code we're going to enclose
- 11:10:30within a tri block we will try the
- 11:10:33dangerous
- 11:10:38code and catch any
- 11:10:41exceptions we will catch that illegal
- 11:10:46argument exception which we will name e
- 11:10:51if the user input doesn't match one of
- 11:10:52these e nums then we'll output the
- 11:10:55following please enter a valid
- 11:11:00day let's try it
- 11:11:03again enter day of the week let's try
- 11:11:07Tuesday it is a
- 11:11:10weekday
- 11:11:12Saturday it is the weekend
- 11:11:16Pizza day please enter a valid day Pizza
- 11:11:21day is not one of our enums but if we
- 11:11:23wanted to change it so it was we can add
- 11:11:28that pizza day will be the eighth day of
- 11:11:31the
- 11:11:32week at least it should
- 11:11:37be we'll need a case for pizza
- 11:11:41day Pizza day will also be part of the
- 11:11:43weekend that just makes sense to
- 11:11:47me all right let's try that
- 11:11:50again enter day of the week Pizza day it
- 11:11:54is the weekend all right all right
- 11:11:57everybody so those are enums meaning
- 11:11:59enumerations they're a special kind of
- 11:12:02class kind of like how interfaces are a
- 11:12:04special kind of class they represent a
- 11:12:07fixed set of constants they improve code
- 11:12:09readability and reliability and where
- 11:12:12they're really useful is when working
- 11:12:14with switches
- 11:12:15because when working with switches using
- 11:12:17enums is faster than strings if I wasn't
- 11:12:20using enum constants I could use
- 11:12:22integers which are also
- 11:12:27efficient but as a programmer this is
- 11:12:30way less readable for me at first clance
- 11:12:32I would have no idea what these numbers
- 11:12:34are but if they're enum constants this
- 11:12:37is perfectly readable to me these are
- 11:12:39days of the week that makes sense and
- 11:12:42well everybody those are enums in Java
- 11:12:45what's going on people in this video I'm
- 11:12:47going to teach you all about threading
- 11:12:48in Java threading allows a program to
- 11:12:51run multiple tasks simultaneously it
- 11:12:54helps to improve performance with
- 11:12:56timeconsuming
- 11:12:57operations such as file input output
- 11:13:00network communications or any other
- 11:13:02background tasks any code that is
- 11:13:04considered timec consuming we can have
- 11:13:06it run on a different thread so that it
- 11:13:08doesn't inconvenience our main program
- 11:13:10there's two options to creating a thread
- 11:13:13option one is to extend the thread class
- 11:13:16this is the simpler option but it's
- 11:13:18limited option two is to implement the
- 11:13:21runnable interface this option tends to
- 11:13:24be better because by extending the
- 11:13:25thread class we're limited to single
- 11:13:28inheritance but by implementing the
- 11:13:30redible interface that's one way around
- 11:13:32that in this demonstration I'm going to
- 11:13:34show you option two it's actually not
- 11:13:36that more difficult than option one and
- 11:13:39it gives you more possibilities I'll
- 11:13:41give you a demonstration of why
- 11:13:42threading is useful in this program
- 11:13:44where about to write let's say it's a
- 11:13:47game or a quiz or something a user has
- 11:13:5110 seconds to respond and if they don't
- 11:13:53times up currently we have one thread
- 11:13:56we're running our program on what is
- 11:13:57known as the main thread without using
- 11:14:00any other threads besides the main
- 11:14:01thread I might write my program
- 11:14:03something like this we'll start with the
- 11:14:05scanner because we'll be accepting user
- 11:14:07input scanner scanner equals new scanner
- 11:14:11pass in
- 11:14:13system.in import this class
- 11:14:16and close your scanner when you're done
- 11:14:18with it scanner.
- 11:14:23close we're going to let a user know
- 11:14:25that they have let's say 5 Seconds to
- 11:14:27enter their
- 11:14:29name you have 5 Seconds to enter your
- 11:14:36name we will create a
- 11:14:39prompt enter your
- 11:14:42name I'll use print rather than and
- 11:14:45print
- 11:14:46line we will assign a string variable of
- 11:14:49name use our scanner called the next
- 11:14:52line
- 11:14:53method and then once we have the name
- 11:14:56we'll
- 11:14:57output hello plus the user's name hello
- 11:15:01plus name so let's do a test run
- 11:15:04currently we don't have a timer
- 11:15:07setup you have 5 Seconds down to your
- 11:15:09name I can type in a name hit enter we
- 11:15:12display hello the name and then the
- 11:15:15program exits processed finished with
- 11:15:17exit Code Zero if we were creating some
- 11:15:20sort of timer we could do the
- 11:15:22following to make this simple we could
- 11:15:24use a for Loop the for Loop is going to
- 11:15:27cycle five times we will have an index
- 11:15:30of I in I equals 1 that's where we'll
- 11:15:33start we'll continue as long as I is
- 11:15:36less than or equal to 5 and then
- 11:15:39increment I by One during each cycle of
- 11:15:42the for Loop to mimic waiting for one
- 11:15:45second you can access the thread class
- 11:15:48call the Sleep Method pass in 1,000
- 11:15:52milliseconds when you use thread that
- 11:15:55refers to the current thread that we're
- 11:15:56working with in this case the main
- 11:15:58thread which is what our main program
- 11:16:00runs on but since our thread might be
- 11:16:03interrupted we do need a try and catch
- 11:16:05block because this is considered
- 11:16:07dangerous code we will try this code
- 11:16:10where we will have our main Java thread
- 11:16:12sleep for 1,000 milliseconds
- 11:16:16and we will catch the following
- 11:16:17exception an interrupted
- 11:16:20exception So within our catch block we
- 11:16:23will catch any interrupted exceptions
- 11:16:25which we will name e if our thread is
- 11:16:28interrupted we will
- 11:16:30say thread was
- 11:16:34interrupted in our imaginary timer we'll
- 11:16:37need an exit
- 11:16:39condition let's say if our index of I is
- 11:16:43equal to 5
- 11:16:45that means times up so we'll
- 11:16:48output times up I'm going to show you
- 11:16:52why this won't work the way that we
- 11:16:55think we have 5 Seconds to enter our
- 11:16:58name but there's no prompt or
- 11:17:01anything then we get the message times
- 11:17:04up and only after that Loop is complete
- 11:17:06are we allowed to enter our name and
- 11:17:08then we can type in our name we get the
- 11:17:11output of hello our name and then the
- 11:17:13program exits so the problem that we're
- 11:17:15running into is that all of this code is
- 11:17:18running on our main thread we have 5
- 11:17:20Seconds to enter our name but we can't
- 11:17:22reach that code until the 5 seconds are
- 11:17:24up because our main thread is waiting
- 11:17:27for this Loop to finish so what we could
- 11:17:29do is create a separate thread where
- 11:17:32we're counting to five and our main
- 11:17:34thread is going to be in charge of
- 11:17:36accepting our user input so here's how
- 11:17:39to create another thread we're going to
- 11:17:41extend the runable interface to create a
- 11:17:44runnable object
- 11:17:45let's go to file new Java class this
- 11:17:49will be a class we'll name this class my
- 11:17:53runnable or something unique that's
- 11:17:56relevant to what you're
- 11:17:58creating my runnable is going to
- 11:18:02implements because we're working with
- 11:18:04interfaces the runnable
- 11:18:08interface if you implement an interface
- 11:18:10it's kind of like you're signing a
- 11:18:11contract we have to override any
- 11:18:14required method me we need to override
- 11:18:17the run method we need the at override
- 11:18:20annotation then we will override the run
- 11:18:23method public void
- 11:18:26run within this run method we're going
- 11:18:28to add any code we want to run in the
- 11:18:30background on a separate thread going
- 11:18:33back to our main Java file we're going
- 11:18:35to cut this
- 11:18:37Loop and then paste it within the run
- 11:18:40method when we call this run method
- 11:18:43we'll have this countdown timer run
- 11:18:44running in the background on a separate
- 11:18:46thread any code you want to run on a
- 11:18:48separate thread place within the run
- 11:18:50method all right going back to our main
- 11:18:52Java file we'll tell the user they have
- 11:18:555 Seconds to enter their
- 11:18:57name we have our class of my runnable we
- 11:19:01will create a my runnable object or
- 11:19:04whatever it is that you named to this
- 11:19:06class let's say my runnable will be the
- 11:19:09name of the object equals new my
- 11:19:13runable we will take this runnable
- 11:19:15object pass it into the Constructor of
- 11:19:18the thread class here's where we're
- 11:19:20going to create a new thread thread
- 11:19:23thread equals new
- 11:19:26thread pass in your runnable object as
- 11:19:29an argument to the thread
- 11:19:31Constructor now we just need our thread
- 11:19:33to
- 11:19:34start you'll take your thread call the
- 11:19:37start method and this should work let's
- 11:19:40try
- 11:19:41it you have 5 Seconds to enter your name
- 11:19:45and we get that prompt of enter your
- 11:19:46name but I'm not going to type in
- 11:19:48anything we get the message times up
- 11:19:51because we ran out of
- 11:19:54time let's try it again I'll type in my
- 11:19:56name this
- 11:19:57time there we go we get our output hello
- 11:20:01bro then we get the message time is
- 11:20:04up both these threads are running at the
- 11:20:07same time the code within our run method
- 11:20:10is running on a separate thread it's
- 11:20:12running in the background while our main
- 11:20:14program is running running so that's why
- 11:20:15threads are useful so let me extend this
- 11:20:19time let's say 10
- 11:20:22seconds if I is equal to 10 just to give
- 11:20:25us some more
- 11:20:26time you have 10 seconds to enter your
- 11:20:30name now check this
- 11:20:33out you have 10 seconds to to your name
- 11:20:35I'll type in my name immediately we get
- 11:20:38the output hello whatever your name is
- 11:20:40but the program is still running because
- 11:20:42that second thread is still running in
- 11:20:43the background
- 11:20:45our program doesn't exit until all
- 11:20:47threads are
- 11:20:48done if your main thread is done you can
- 11:20:51end all other threads but you'll want to
- 11:20:53set those threads to be what is known as
- 11:20:55Damon threads a Damon thread will end
- 11:20:58when the main thread is over we will
- 11:21:00take our thread call the set Damon
- 11:21:04method pass and
- 11:21:07true this thread is going to end as soon
- 11:21:09as our main thread is finished let's try
- 11:21:12it again you have 10 seconds then to
- 11:21:15your name I'll type in my name we get
- 11:21:17our output and then our program exits
- 11:21:20without saying time is up processed
- 11:21:22finished with exit code
- 11:21:24zero that's because we set our second
- 11:21:27thread to be a Damon thread it ends when
- 11:21:30the main thread is over now on the other
- 11:21:32hand let's say that we wait for time to
- 11:21:35be up we wait the full 10
- 11:21:42seconds time's up our program isn't over
- 11:21:46yet we're still waiting for user input
- 11:21:48and I can still type it
- 11:21:50in hello bro process finished with exit
- 11:21:53Code Zero optionally what you could do
- 11:21:56to get your program to exit go to your
- 11:21:58runnable class wherever you would like
- 11:22:00your program to exit when seconds equals
- 11:22:0310 that's where I would like to exit to
- 11:22:06exit the program prematurely I can
- 11:22:08access system called the exit method
- 11:22:12pass in an exit code of zero that will
- 11:22:14ENT the program all right let's try this
- 11:22:19again you have 10 seconds to end to your
- 11:22:21name I'm not going to type in anything
- 11:22:24I'm just going to wait for the full 10
- 11:22:28seconds time's up and then our program
- 11:22:31exits but that's what we want though the
- 11:22:33way that we've set up this program is
- 11:22:35that we want this program to exit either
- 11:22:37when we finish typing in our name or we
- 11:22:39run out of
- 11:22:41time all right everybody so that is an
- 11:22:43introduction to in it allows a program
- 11:22:46to run multiple tasks simultaneously it
- 11:22:49helps to improve performance with
- 11:22:51timeconsuming
- 11:22:52operations it's useful with file input
- 11:22:54and output operations network
- 11:22:56communications or any other background
- 11:22:59tasks and well everybody that is an
- 11:23:01introduction to threading in Java yo we
- 11:23:05finally made it to multi-threading in
- 11:23:07Java multi-threading enables a program
- 11:23:09to run multiple threads concurrently at
- 11:23:12the same time as we've talked about in
- 11:23:14the previous lesson a thread is a set of
- 11:23:16instructions and they run independently
- 11:23:18from other threads multi-threading is
- 11:23:21useful for background tasks or other
- 11:23:23timeconsuming
- 11:23:24operations in the prior lesson we've
- 11:23:26learned that there's two ways to create
- 11:23:28threads we can either extend the thread
- 11:23:30class or implement the runable interface
- 11:23:33the second option is better in most
- 11:23:34cases because we're not limited by
- 11:23:36single inheritance here's how we can get
- 11:23:38started multi-threading we're going to
- 11:23:40create a new class file new Java class
- 11:23:44we'll call this class my
- 11:23:49runnable my runnable will
- 11:23:52implements the runnable interface if
- 11:23:55we're implementing an interface it's
- 11:23:57kind of like we're signing a contract we
- 11:23:59have to override any required methods we
- 11:24:02have to override the run
- 11:24:04method public void
- 11:24:08run whatever is within the run method
- 11:24:11this is the code that's going to perform
- 11:24:13when this thread is up and running
- 11:24:15so what would we like to do just for a
- 11:24:18beginner's example let's count to five
- 11:24:20using a four Loop we'll write a for
- 11:24:24Loop we will create an index of I I'll
- 11:24:27set that equal to be one right away
- 11:24:29we'll continue as long as I is less than
- 11:24:32or equal to 5 cuz we're counting to five
- 11:24:35then increment I by 1 during each cycle
- 11:24:39this for loop I would like this thread
- 11:24:41to wait for 1 second before printing
- 11:24:43whatever I is what whatever the current
- 11:24:44count is if we're counting we will
- 11:24:47access the thread class called The Sleep
- 11:24:50Method pass in 1,000 milliseconds to
- 11:24:53sleep for a
- 11:24:54second we will need to surround this
- 11:24:56method with try and catch
- 11:24:58blocks we'll catch any interrupted
- 11:25:02exceptions but let's output the
- 11:25:04following we'll say thread was
- 11:25:09interrupted after our thread sleeps for
- 11:25:121 second let's display the current count
- 11:25:14I'll just say I just to keep it
- 11:25:17simple let's go back to our main Java
- 11:25:20file we'll need to create a runable
- 11:25:22object my
- 11:25:24runable let's say my runable equals new
- 11:25:29my
- 11:25:33runable we have to pass in this runable
- 11:25:35object to a thread object let's say
- 11:25:38thread thread equals new thread we will
- 11:25:42pass in our runable object
- 11:25:45to get this thread to start we have to
- 11:25:47call the start method of our thread
- 11:25:49thread. start
- 11:25:50method and this should work we'll test
- 11:25:55it 1 2 3 4 5 and then we exit the
- 11:26:02program another shortcut you can do too
- 11:26:06instead of creating a named runable
- 11:26:07object we can pass in an anonymous
- 11:26:09renable object to the thread Constructor
- 11:26:12so let's copy this section delete this
- 11:26:15line of code and then pass in a new
- 11:26:17runable
- 11:26:19object this will work too and it's a lot
- 11:26:23more
- 11:26:28concise now we won't have just one
- 11:26:31thread we'll have two so let's rename
- 11:26:33thread as thread one copy this line of
- 11:26:36code paste it rename the second thread
- 11:26:39as thread two thread 1. start and thread
- 11:26:432. start
- 11:26:45so now we'll have two threads running at
- 11:26:47the same time they're running
- 11:26:51concurrently so they both say 1 2 3 4 5
- 11:26:57we have successfully
- 11:27:00multi-threaded you could differentiate
- 11:27:02these by getting their name here's an
- 11:27:05example to get each thread's name we're
- 11:27:07going to access the thread class called
- 11:27:10the current thread method
- 11:27:14and then method chain the get name
- 11:27:18method then I'll add a space plus our
- 11:27:21counter
- 11:27:23RI just so we can see each thread by its
- 11:27:25name and what it's counting exactly okay
- 11:27:29let's try this
- 11:27:31again we have thread one and thread zero
- 11:27:35now they won't always be in the same
- 11:27:37order they're running
- 11:27:39independently thread zero that's our
- 11:27:41thread one Java's thread one is our
- 11:27:44thread
- 11:27:47two okay here's an exercise we're going
- 11:27:50to have thread one say ping and thread 2
- 11:27:53say pong like we're playing a game of
- 11:27:55pingpong we'll modify the my runnable
- 11:28:00class we'll need some text a text
- 11:28:03variable we'll say private final we
- 11:28:06don't want it to
- 11:28:07change string text then we'll need a
- 11:28:11Constructor to assign it my
- 11:28:16runnable we have one parameter a string
- 11:28:20of text we will assign this. text equals
- 11:28:24the text we
- 11:28:27receive instead of printing our counter
- 11:28:30we'll print our
- 11:28:32text all right and that's all we need
- 11:28:35going back to our main Java file we have
- 11:28:37to pass in some text when we construct a
- 11:28:39runable
- 11:28:41object for the first thread we'll we'll
- 11:28:44say ping for the second thread we'll say
- 11:28:48pong so here's what we have
- 11:28:53currently pong
- 11:28:57ping and then we exit the
- 11:29:01program before we start the threads
- 11:29:04let's say game start there's one more
- 11:29:06thing I want to show you game
- 11:29:11start after we start the threads let's
- 11:29:14try and say game
- 11:29:15over game over here's what
- 11:29:20happens game start game over and our two
- 11:29:24threads are still
- 11:29:26running then we
- 11:29:28finish so the problem here is that in
- 11:29:31our main thread it doesn't Wait For
- 11:29:33Thread one and thread two to
- 11:29:35finish our main thread ends
- 11:29:37immediately we can have our main thread
- 11:29:40wait for these threads to finish by
- 11:29:42calling the join method of each thread
- 11:29:44our main thread we'll wait for these two
- 11:29:46threads to
- 11:29:47finish after these two threads start
- 11:29:50we'll use each join method thread 1.
- 11:29:54join and thread 2
- 11:30:00dojin and we do have to surround these
- 11:30:03with try and catch
- 11:30:10blocks try thread 1. jooin and thread 2
- 11:30:14do jooin if our main thread is
- 11:30:16interrupted we'll output main thread was
- 11:30:21interrupted okay this should work now
- 11:30:25before we display game over we're going
- 11:30:27to wait for our two threads to
- 11:30:29finish here's what we got game start and
- 11:30:32we display
- 11:30:34pingpong and we'll do that five times
- 11:30:37and then display game
- 11:30:40over all right everybody that is an
- 11:30:42introduction to multi-threading
- 11:30:44it enables a program to run multiple
- 11:30:46threads concurrently a thread is a set
- 11:30:49of instructions that run independently
- 11:30:52multi-threading is useful for background
- 11:30:54tasks or other timeconsuming
- 11:30:56operations and well everybody that is an
- 11:30:59introduction to multi-threading in Java
- 11:31:02hey everybody in this video we're going
- 11:31:03to create a working alarm clock that
- 11:31:05plays music using
- 11:31:09Java all right let's get started
- 11:31:12everybody this alarm clock is going to
- 11:31:13to serve as a final project for us will
- 11:31:16combine a lot of Concepts that we've
- 11:31:17learned throughout the series when
- 11:31:19starting a new project a good first step
- 11:31:21is to declare all your variables or
- 11:31:23other objects that you'll need
- 11:31:24throughout this program we'll begin with
- 11:31:26the scanner because we'll be accepting
- 11:31:28user input a user is going to enter in a
- 11:31:30Time scanner scanner equals new scanner
- 11:31:34pass in
- 11:31:36system.in and we'll be importing classes
- 11:31:39as we go along at the end of this
- 11:31:41program we're going to close our scan
- 11:31:44but we'll actually move it later on
- 11:31:46because our alarm clock class is
- 11:31:48actually going to be using it more on
- 11:31:50that later we have our scanner and
- 11:31:52currently we're closing it a user is
- 11:31:56going to use the scanner and enter in
- 11:31:58hours minutes and seconds to set the
- 11:32:00alarm when the user types in a string
- 11:32:02representation of a time we need to
- 11:32:04parse it into a local time object but
- 11:32:07we'll need a formatter object we will
- 11:32:10create a date time formatter object
- 11:32:14you'll need to import this class too
- 11:32:17we'll name this object
- 11:32:19formatter
- 11:32:21equals we will access the class of
- 11:32:23datetime formatter called the of pattern
- 11:32:29method within this method we have to
- 11:32:31pass in a pattern a pattern that
- 11:32:33represents the time for hours minutes
- 11:32:36and seconds within a string we will pass
- 11:32:38in two Capital H's for hours colon two
- 11:32:42lowercase M for minutes colon two
- 11:32:45lowercase s's for
- 11:32:47seconds when a user types in a string
- 11:32:50representation of a time using our
- 11:32:51scanner we'll use this formatter object
- 11:32:54when we parse it into a Time
- 11:32:56object then we will create a local time
- 11:33:00object which we will name alarm
- 11:33:05time we'll instantiate that later import
- 11:33:08this
- 11:33:09class now we'll need a user to enter in
- 11:33:12the time we'll create a
- 11:33:15prompt enter n alarm time we'll give
- 11:33:20them a prompt for this format hours
- 11:33:23minutes
- 11:33:26seconds let me switch this to print CU I
- 11:33:29like it on the same
- 11:33:32line let's create a string
- 11:33:35variable of let's name this input time
- 11:33:39equals use our
- 11:33:41scanner call the next next line method
- 11:33:45to get a line of text from the
- 11:33:47user all right now here's where we're
- 11:33:49going to parse our string our input time
- 11:33:52and assign it to our alarm time object
- 11:33:55so we have to convert it we have to
- 11:33:57parse it we will assign our alarm time
- 11:34:00object set it equal to access the class
- 11:34:05of local time and there is a parse
- 11:34:08method we have to pass in two arguments
- 11:34:11the first is a string representation of
- 11:34:13our
- 11:34:14time and our format object that we have
- 11:34:17previously declared and
- 11:34:20instantiated then we'll output the
- 11:34:21following
- 11:34:22message alarm set
- 11:34:264 plus our alarm time we can output it
- 11:34:32directly let's do a test
- 11:34:37run let's say I set my alarm for
- 11:34:41730 this is going to be in military time
- 11:34:45alarm set for
- 11:34:487:30 what if we type in a time that
- 11:34:51doesn't make
- 11:34:52sense uh like the word
- 11:34:54Pizza well we encounter an exception a
- 11:34:57date time parse exception we can use
- 11:35:00exception handling to catch this
- 11:35:01exception I'm just going to copy it and
- 11:35:04then paste it
- 11:35:05later this portion is considered
- 11:35:08dangerous code we can put that within a
- 11:35:09try block try this dangerous code catch
- 11:35:13and any of the following
- 11:35:15exceptions we will catch that date time
- 11:35:18parse exception which we will name
- 11:35:21e import this
- 11:35:23class okay then take all of this
- 11:35:26previous code stick it within our Tri
- 11:35:28block we will try this code and catch
- 11:35:32this exception if it pops up we'll
- 11:35:35output the following let's say invalid
- 11:35:41format please use
- 11:35:46hours
- 11:35:48minutes seconds let me change that to
- 11:35:54use I've already used Pizza let's go
- 11:35:57with Taco for the alarm time invalid
- 11:36:00format please use hours minutes
- 11:36:04seconds let's try
- 11:36:088:00 alarm set for
- 11:36:118:00 all right good we know that that
- 11:36:14works why don't we prompt the user to
- 11:36:17enter in the time again if it is an
- 11:36:18invalid format we can use a while
- 11:36:22loop while some condition is true
- 11:36:25continue to do the following let's cut
- 11:36:28our code our trying catch blocks stick
- 11:36:31it within the while loop what's our
- 11:36:34condition while our alarm time is empty
- 11:36:38there's actually a value that you can
- 11:36:39assign an object and that is null null
- 11:36:42has no value
- 11:36:44within our while loop we'll continue
- 11:36:46this while loop while our alarm time is
- 11:36:48equal to a value of
- 11:36:52null so if I were to just hit enter we
- 11:36:55get an invalid format please use hours
- 11:36:58minute seconds and it won't let me
- 11:37:00continue until I enter in a valid
- 11:37:03time so I will set this to be
- 11:37:079:00 and then our alarm is set for
- 11:37:119:00 the first section of this project
- 11:37:13is complete we have successfully set an
- 11:37:15alarm time our alarm time is a local
- 11:37:18time object the next part is to create
- 11:37:21an alarm clock class to create alarm
- 11:37:23clock objects we're going to create an
- 11:37:26alarm clock class file new Java class we
- 11:37:30will name this class alarm
- 11:37:36clock we're going to have our alarm
- 11:37:38clock run on a separate thread to do
- 11:37:41that we can implement
- 11:37:44the runnable
- 11:37:45interface so we can do some threading
- 11:37:48but if we implement the runable
- 11:37:50interface it's kind of like we're
- 11:37:51signing a contract we have to override
- 11:37:55the run method public void
- 11:37:59run when this alarm clock is running on
- 11:38:02a separate thread it's going to execute
- 11:38:04any code within the run method but we're
- 11:38:07going to fill that in later currently
- 11:38:09our alarm clock needs one thing it needs
- 11:38:12an alarm time
- 11:38:14which we will send it from the main Java
- 11:38:18file let's set that up as a
- 11:38:20field with our alarm time I'm going to
- 11:38:23make it private I only want the alarm
- 11:38:25clock class to be able to change it I
- 11:38:28will also make it final it's
- 11:38:30unchangeable the data type it's an
- 11:38:32object it's a local time object which we
- 11:38:36will name alarm time and then we do need
- 11:38:40to import this class too
- 11:38:43all right then we need a
- 11:38:45Constructor we will take our alarm
- 11:38:48clock we need one parameter
- 11:38:51currently a local time of alarm time we
- 11:38:56will assign this do alarm time equals
- 11:39:01the alarm time that we
- 11:39:03receive all right that's
- 11:39:06good okay let's go back to our main Java
- 11:39:09file before we close our scanner we're
- 11:39:12going to create an alarm clock
- 11:39:15object alarm clock is the class we will
- 11:39:19name this object alarm clock equals new
- 11:39:23alarm clock but the way that we set up
- 11:39:26this class is that we have to pass in an
- 11:39:29alarm time a local time
- 11:39:33object we have our alarm time we've
- 11:39:36already verified it using this while
- 11:39:39loop it should be a valid format we will
- 11:39:42pass in our local time of alarm time to
- 11:39:44the alarm clock in order to construct it
- 11:39:47since our alarm clock is going to be
- 11:39:49running on a separate thread we'll have
- 11:39:51to create a new
- 11:39:52thread thread let's name this alarm
- 11:39:55thread equals a new thread and then
- 11:40:00we'll pass in our alarm clock we can do
- 11:40:03this because our alarm clock it
- 11:40:05implements the runnable interface so our
- 11:40:07alarm clock is going to run on a
- 11:40:09different thread than the main thread to
- 11:40:11start a thread you will take your thread
- 11:40:14in this case alarm thread called the
- 11:40:16start
- 11:40:17method so once we start our alarm thread
- 11:40:21it's going to execute the run method all
- 11:40:24right so for testing purposes let's get
- 11:40:26the time right now to get the time right
- 11:40:29now we will create a local time object
- 11:40:32let's just call it now what's the time
- 11:40:34right now equals access the class of
- 11:40:37local time called the now method I will
- 11:40:41display the time right now just for
- 11:40:44testing purposes I want to be sure that
- 11:40:46our alarm clock class is
- 11:40:50working so we have to enter in a valid
- 11:40:52alarm time I'll just set it to be 8:00
- 11:40:54a.m. alarm is set and here's the current
- 11:40:57time right now at least for me I'm
- 11:41:00filming this video at 7:26 in the
- 11:41:03morning it is going to be in military
- 11:41:06time and then we have a bunch of
- 11:41:08milliseconds after we know that we're
- 11:41:10successfully calling the run method
- 11:41:15so we have the time right now we're
- 11:41:17going to compare times is the time right
- 11:41:20now before our alarm time if it is we'll
- 11:41:24continue sleeping we'll have our thread
- 11:41:26sleep we can delete this print statement
- 11:41:29I was just testing
- 11:41:30it we need to check if the time right
- 11:41:33now is before the alarm time if it is
- 11:41:37we'll continue
- 11:41:38waiting we can do this with the while
- 11:41:41loop take our time right
- 11:41:44now call the is before method we can
- 11:41:48compare local times is the time right
- 11:41:51now before our alarm
- 11:41:54time if it is we're going to sleep for 1
- 11:41:58second access the thread class sleep for
- 11:42:011,000
- 11:42:04milliseconds all right we do need to
- 11:42:06surround this with a TR and catch block
- 11:42:08let's do
- 11:42:09that try and sleep for 1,000
- 11:42:12milliseconds
- 11:42:13if this thread is interrupted we'll
- 11:42:15output the
- 11:42:18following thread was
- 11:42:22interrupted this is if we encounter an
- 11:42:24interrupted
- 11:42:26exception we're checking the time right
- 11:42:28now is it before our alarm time if it is
- 11:42:32let's output the time currently I'm
- 11:42:34going to condense some of this code
- 11:42:36outside of our while loop we're already
- 11:42:37creating an object of now so why don't
- 11:42:40we do this let's cut this line
- 11:42:46and do the following access the class of
- 11:42:49local time call the now method and then
- 11:42:53method chain the is before
- 11:42:56method basically every second we're
- 11:42:59going to get the time right now and
- 11:43:01compare it to our alarm time is the time
- 11:43:04right now before our alarm time if this
- 11:43:07while loop is running we'll output the
- 11:43:10time so we'll output
- 11:43:13local time called the now
- 11:43:17method let's do a test
- 11:43:21run ENT turn an alarm time I'll just say
- 11:43:248:00 a.m. to keep it
- 11:43:27simple alarm set for 8 a.m. and here's
- 11:43:30the current time it updates every 1
- 11:43:32second approximately every 1
- 11:43:35second now let's do some formatting
- 11:43:38because I don't like all these
- 11:43:39milliseconds afterwards
- 11:43:43here's what we could do we could create
- 11:43:46an integer variable of ours set it equal
- 11:43:51to access the class of local
- 11:43:54time called the now method then method
- 11:43:57chain the get hour method to return the
- 11:44:00number of
- 11:44:01hours then we're going to do this with
- 11:44:04minutes int minutes get
- 11:44:10minute int second
- 11:44:15seconds get
- 11:44:21second okay I'm going to show you what
- 11:44:22happens if I print ours plus a
- 11:44:29colon plus
- 11:44:31minutes plus a colon
- 11:44:35again plus
- 11:44:38seconds here's what we have currently
- 11:44:44I will set this to be 8
- 11:44:46a.m. alarm set for 8 a.m. so this looks
- 11:44:50pretty good but I would like some
- 11:44:52leading
- 11:44:58zeros we could do that with print F so
- 11:45:01let's switch this
- 11:45:03around print
- 11:45:06f for print F We'll add some
- 11:45:09placeholders which we can use with a
- 11:45:11percent sign to display two leading
- 11:45:13zeros we can use
- 11:45:1502 if we're going to insert an integer
- 11:45:18we have to use D for decimal so let's do
- 11:45:22this three times 02 D percent 02
- 11:45:29D we are inserting hours minutes and
- 11:45:34seconds let's try it
- 11:45:37again I will set the time to be 8:00
- 11:45:40a.m.
- 11:45:44oh and then I'm forgetting the colons
- 11:45:46and a new line
- 11:45:49character so let's insert some
- 11:45:52colons and then add a new line character
- 11:45:55to the end because I forgot to do
- 11:46:04that that's much better and we have that
- 11:46:07leading zero if one of these values is
- 11:46:09one digit that looks pretty good now
- 11:46:13what we'll do rather than updating the
- 11:46:15time and placing it on a new line every
- 11:46:171,000 milliseconds we're going to edit
- 11:46:20it in
- 11:46:21place here's how with our print F
- 11:46:24statement we're going to insert back SLR
- 11:46:28this is for carriage return it'll move
- 11:46:30the cursor back to the beginning and
- 11:46:32then we'll get rid of the new line
- 11:46:34character it should look something like
- 11:46:36this now
- 11:46:44our time is going to update in place
- 11:46:46rather than take up a new line for each
- 11:46:48time it
- 11:46:50updates I think this looks a lot
- 11:46:55better all right so we're going to
- 11:46:57condense some of these steps instead of
- 11:46:59accessing the local time class three
- 11:47:01separate times let's just do it once
- 11:47:04we'll create a local time object named
- 11:47:07now
- 11:47:09equals access the class of local time
- 11:47:12call the now
- 11:47:15method when we assign ours we will
- 11:47:18access our object of now now is our
- 11:47:21local time now. getet hour for minutes
- 11:47:25now doget minute and seconds now. getet
- 11:47:30second and then we'll insert them using
- 11:47:32our printf
- 11:47:34statement or even instead of assigning
- 11:47:37variables we could just call now doget
- 11:47:41hour now. get
- 11:47:44minute and now. getet second and we can
- 11:47:48delete these variables cuz we don't need
- 11:47:50them just for readability I'll place
- 11:47:52these on a new
- 11:47:56line and I'm just going to line them
- 11:48:01up all right this should work too let's
- 11:48:04make
- 11:48:10sure yes it does
- 11:48:14you can keep this code the way it was
- 11:48:15before but I like it a little more
- 11:48:18concise all right our while loop is
- 11:48:22done so once we escape the while loop
- 11:48:25that means the local time is our alarm
- 11:48:27time or past our alarm time so for
- 11:48:31testing purposes let's output the
- 11:48:33following let's just say alarm
- 11:48:36noises we haven't added any audio yet
- 11:48:44for my alarm time I'm just going to set
- 11:48:46it to be 1 minute past what it is
- 11:48:48currently in this case 7:44 but pick a
- 11:48:51different time depending on what time it
- 11:48:52is for you
- 11:48:54currently so I just need to wait for 20
- 11:49:00seconds all right once we reach our
- 11:49:02alarm time or it's past our alarm time
- 11:49:05we display alarm noises and then exit
- 11:49:08the
- 11:49:09program I'm going to add a new line
- 11:49:11character just before we display alarm
- 11:49:14noises for our alarm we'll be playing
- 11:49:17some music later just for now we're
- 11:49:19going to play a single beep just to be
- 11:49:21sure that everything is working
- 11:49:22basically to have Java make a beep noise
- 11:49:25you can do the following access the
- 11:49:27class of tool kit call the
- 11:49:31get
- 11:49:32default toolkit
- 11:49:36method call the beep method and then we
- 11:49:39do need to import this
- 11:49:41class once we escape the wild Loop we
- 11:49:44will display alarm noises and make a
- 11:49:46beep sound a single
- 11:49:51beep I will set my alarm to be
- 11:49:58747 I'll have to come back in one
- 11:50:04minute there we
- 11:50:06go we display our text of alarm noises
- 11:50:10and hopefully you heard that beep so we
- 11:50:12know that our alarm arm clock is working
- 11:50:13to make a more sophisticated version
- 11:50:15we're going to play an audio
- 11:50:18file I have an audio file here it's a
- 11:50:21wave file the following code isn't going
- 11:50:23to be compatible with MP3s if you do
- 11:50:26have an MP3 file you can always use an
- 11:50:28online MP3 to wave converter that's what
- 11:50:31I did to make this convenient for me I'm
- 11:50:33going to place this wave file within my
- 11:50:35project
- 11:50:37folder here's my wave file going back to
- 11:50:41our main Java file I will create a
- 11:50:43string variable a file path string file
- 11:50:48path
- 11:50:49equals here I can list a relative file
- 11:50:52path or an absolute file path this wave
- 11:50:54file is within my project folder I just
- 11:50:57need to list the file name so I'm going
- 11:50:59to copy
- 11:51:02path file
- 11:51:04name and paste it within that
- 11:51:06string with our alarm clock we're going
- 11:51:09to send it our file path we'll send it
- 11:51:12as the second argument along with the
- 11:51:13alarm time but we have to set up the
- 11:51:16Constructor so that we can accept
- 11:51:21it with our alarm clock class we will
- 11:51:24create a private final string of file
- 11:51:31path then we'll have to set up the
- 11:51:32parameters we are also accepting a
- 11:51:35string of file path the location to that
- 11:51:39file to play the
- 11:51:40audio this. file file
- 11:51:44path equals the file path that we
- 11:51:48receive with playing audio there's a lot
- 11:51:50of code we have to write I'll dedicate
- 11:51:52an entire method just to playing
- 11:51:54audio let's make this a private method
- 11:51:57so only our alarm clock can access it
- 11:52:00it's not going to return anything the
- 11:52:02return type is void let's name this
- 11:52:04method play
- 11:52:09sound once we reach the end we will call
- 11:52:13the play sound
- 11:52:16method but we're going to pass in our
- 11:52:18file
- 11:52:20path so we need a parameter setup of
- 11:52:23string file
- 11:52:25path and now we are within the play
- 11:52:27sound
- 11:52:29method to play a w file we're going to
- 11:52:32create a file object named audio file
- 11:52:37equals a new file and then pass in our
- 11:52:40file path
- 11:52:44import this
- 11:52:45class we'll be doing a lot of importing
- 11:52:48as we go
- 11:52:49along then we need to create an audio
- 11:52:51input
- 11:52:53stream audio input
- 11:52:56stream we'll name this audio
- 11:53:00stream
- 11:53:02equals access the class of audio
- 11:53:06system called the get audio input stream
- 11:53:12method
- 11:53:13yes that's a lot to
- 11:53:14type then we will pass in our audio
- 11:53:19file import this
- 11:53:23class import the class for audio system
- 11:53:26to there's a lot of different exceptions
- 11:53:28that we'll need to catch such as if our
- 11:53:31audio file is unsupported like it's an
- 11:53:33MP3 file we'll need some try and catch
- 11:53:37blocks try this code catch any
- 11:53:40exceptions
- 11:53:42here's one we'll start
- 11:53:46with we will catch any un
- 11:53:51supported audio file
- 11:53:54exceptions which we will name e import
- 11:53:58this
- 11:54:00class if we encounter this exception
- 11:54:03we'll output the
- 11:54:05following audio file format is not
- 11:54:09supported
- 11:54:12if the audio is unavailable for some
- 11:54:14reason let's say another resource is
- 11:54:16accessing it we can catch that
- 11:54:20too that exception is a
- 11:54:23line unavailable exception line
- 11:54:27unavailable exception of
- 11:54:30e let's say something like audio is
- 11:54:36unavailable import the
- 11:54:40class then as a safety net
- 11:54:43we will catch any IO exceptions input
- 11:54:45output
- 11:54:46exceptions IO exception e here we'll
- 11:54:50just
- 11:54:51say error
- 11:54:53reading audio file it is good to catch
- 11:54:57specific kinds of exceptions first which
- 11:54:59we are doing this just acts as a safety
- 11:55:04net so with modern Java you can try with
- 11:55:07resources you'll automatically close
- 11:55:10those resources when you're done with
- 11:55:11them when we create our audio input
- 11:55:14stream let's cut all this
- 11:55:17code and paste it within the triy with
- 11:55:20resources block then delete that
- 11:55:24semicolon so we have our audio stream we
- 11:55:27need to create a clip object a clip acts
- 11:55:30as a player that plays an audio stream
- 11:55:33within the tri block we will create a
- 11:55:34clip object clip
- 11:55:37clip equals access the class of audio
- 11:55:42system system called the get clip method
- 11:55:45to create a clip
- 11:55:47object import this
- 11:55:51class then we will use our
- 11:55:54clip and open our audio stream
- 11:56:00object to get the clip to play we will
- 11:56:03call the start method of our clip
- 11:56:05clip.art
- 11:56:08method one issue we're probably going to
- 11:56:10run into is that once we we start
- 11:56:12playing the clip the program is going to
- 11:56:14end immediately so we might not even
- 11:56:16hear it so temporarily I'm just going to
- 11:56:20sleep for a couple
- 11:56:22seconds let's just sleep for 5
- 11:56:25seconds and then I do need to add a
- 11:56:27catch clause for any interrupted
- 11:56:29exceptions we'll be deleting this don't
- 11:56:31worry I just want to be sure that it
- 11:56:36works I'm going to set it for
- 11:56:40802 I got about 40 seconds
- 11:56:48[Music]
- 11:56:53we played our music for 5 seconds and
- 11:56:55then we exit the program what I would
- 11:56:57like to do instead is that we stop the
- 11:57:00alarm if we hit enter here's how we can
- 11:57:03set that up we can delete this method
- 11:57:05where we sleep for 5 seconds instead
- 11:57:08we'll wait for a user to hit enter and
- 11:57:10delete that catch statement too
- 11:57:13we're going to be using a scanner within
- 11:57:16a big project different parts of your
- 11:57:17project may require different scanners
- 11:57:20but I like to just reuse the same one
- 11:57:23the problem with having multiple
- 11:57:24scanners is if they use system.in and
- 11:57:28one scanner closes
- 11:57:29it it's going to close system.in for all
- 11:57:32of them so rather than close the scanner
- 11:57:35at the end of our main Java file we're
- 11:57:38going to do that within the alarm clock
- 11:57:40class when everything is all done so
- 11:57:43we'll do that at the end of the tri
- 11:57:44block however our alarm clock class has
- 11:57:47no idea what the scanner is we're going
- 11:57:50to send it as an argument to the alarm
- 11:57:52clock class so we'll need to set that up
- 11:57:55we will pass in our scanner and reuse it
- 11:57:58within the alarm clock
- 11:58:03class within the alarm clock class we
- 11:58:06will create a private final scanner
- 11:58:10object which we will name
- 11:58:13scanner import this
- 11:58:16class set up the
- 11:58:19parameters we will receive a scanner
- 11:58:21object that we can use throughout the
- 11:58:23alarm clock
- 11:58:24class this do scanner equals the scanner
- 11:58:28that we receive all right and now we
- 11:58:31have access to that same scanner the
- 11:58:33same one that we have instantiated at
- 11:58:34the beginning of our
- 11:58:36program and remember we are not closing
- 11:58:38it at the end of the main Java file
- 11:58:40anymore
- 11:58:42once our alarm is playing we will wait
- 11:58:44for some user input let's create a
- 11:58:46prompt we'll say
- 11:58:49press
- 11:58:50enter to stop the
- 11:58:55alarm I'm going to use
- 11:58:59print we will use our scanner called the
- 11:59:03next line
- 11:59:05method our music is going to continue
- 11:59:07playing until we hit
- 11:59:09enter once the user hits enter we we are
- 11:59:12going to take our clip and call the stop
- 11:59:15method then close the scanner when we're
- 11:59:17done with
- 11:59:20it and that should be everything let's
- 11:59:22run this one last
- 11:59:25time I will set my alarm to be 807
- 11:59:36[Music]
- 11:59:44to get our alarm to stop we just have to
- 11:59:46press
- 11:59:48[Music]
- 11:59:49enter and the alarm stops and we exit
- 11:59:52the
- 11:59:53program all right everybody that is a
- 11:59:55working alarm clock that you can write
- 11:59:57using Java
About this transcript
This page contains the full transcript of Java Full Course for free ☕ by Bro Code, generated from the public captions YouTube serves with the video. The transcript has 95,874 words across 15,273 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.