YouTube2Text

Java Full Course for free ☕ — Transcript

by Bro Code · 95,874 words · 15,273 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hey how's it going everybody in this
  2. 0:02video I'm going to teach you everything
  3. 0:03you need to know to start coding with
  4. 0:05Java this series even contains about 15
  5. 0:08Hands-On projects that we're going to
  6. 0:09code together our final project will be
  7. 0:12a functioning alarm clock that plays
  8. 0:14music of your choice if you've never
  9. 0:16coded anything in your life don't worry
  10. 0:18we're going to start from the very
  11. 0:19beginning I encourage you to sit back
  12. 0:22relax and enjoy the
  13. 0:26show I don't like boring introductions
  14. 0:28so we're just going to Jump Right In to
  15. 0:30start coding with Java you'll need two
  16. 0:32things a Java development kit also known
  17. 0:35as a jdk a jdk contains a compiler a
  18. 0:38compiler will compile your source code
  19. 0:40your written code into bite code which
  20. 0:42can run on a machine you'll also need an
  21. 0:45integrated development environment IDE
  22. 0:48an IDE is a fancy term for a workspace
  23. 0:51in which we can write code because
  24. 0:53sometimes notepad just doesn't cut it
  25. 0:55we'll get started by downloading a jdk a
  26. 0:58Java development kit okay here's how to
  27. 1:01download the jdk the Java development
  28. 1:03kit you can easily just Google or do a
  29. 1:05search for jdk download we're going to
  30. 1:08use the one by Oracle so we'll click on
  31. 1:10this link I'll also post the link in the
  32. 1:13description of this video if you would
  33. 1:15like to access it that way we will
  34. 1:17download the latest jdk version as of
  35. 1:20the filming of this video we're on jdk
  36. 1:2323 we're going to look for our operating
  37. 1:26system I'm running Windows I will
  38. 1:29download the 64- bit installer by
  39. 1:31clicking this
  40. 1:32link then we just have to wait for it to
  41. 1:37download the download is complete we're
  42. 1:39going to open this
  43. 1:41executable select next
  44. 1:49next our jdk has been successfully
  45. 1:52installed we can close it and that's all
  46. 1:54we got to do for the
  47. 1:56jdk now we need to install an IDE an
  48. 1:59integrated development
  49. 2:01environment one I recommend is intellig
  50. 2:05you can easily just Google or do a
  51. 2:07search for intellig
  52. 2:09download we'll select the top
  53. 2:12link now if you're not brought to this
  54. 2:14page let's say you're at a different
  55. 2:17page go to the top right where it says
  56. 2:20get intellig click it and you should be
  57. 2:23brought to a download page this version
  58. 2:25is a 30-day trial but if you scroll down
  59. 2:28there is a free version this is the
  60. 2:30Community Edition because I don't like
  61. 2:32to pay for things select the correct
  62. 2:35download for your operating system I'm
  63. 2:36running Windows I will download the
  64. 2:41executable once the download is complete
  65. 2:43we will open this executable for
  66. 2:46intellig select
  67. 2:49next choose a destination folder I'll
  68. 2:52keep it as is Click
  69. 2:55next you can create a desktop shortcut I
  70. 2:58think I will just for convenience
  71. 3:01now this is important too you can update
  72. 3:04the path variable it basically tells
  73. 3:06your operating system where to find the
  74. 3:08executable files this is useful if
  75. 3:10you're going to be running Java code
  76. 3:12from command prompt or terminal but we
  77. 3:14won't be doing that in this series you
  78. 3:16can always come back later and update
  79. 3:18this but we'll just select next and
  80. 3:24install all right and we can finish we
  81. 3:27might as well start intellig right away
  82. 3:29I'm going to check this check boox to
  83. 3:30run intellig and
  84. 3:34finish we should now be at a welcome
  85. 3:36screen for intellig we need to create a
  86. 3:38project select new project I'll name my
  87. 3:42project my first
  88. 3:45project select the latest version of
  89. 3:48your jdk I have jdk version 23 there is
  90. 3:52a checkbox and it's most likely going to
  91. 3:54be checked that says add sample code
  92. 3:57just as a beginner I recommend
  93. 3:59unchecking this it'll autogenerate some
  94. 4:01sample code for us but we're going to do
  95. 4:03that ourselves I'll show you how and
  96. 4:06then
  97. 4:09create within our project folder
  98. 4:11navigate to your Source folder it has a
  99. 4:14name of
  100. 4:15SRC we need to create a main Java file
  101. 4:18go to the top toolbar go to file new
  102. 4:21Java class we'll name this class main
  103. 4:25it's going to contain the main body of
  104. 4:26our code then select class
  105. 4:31now the font is probably going to be
  106. 4:33really
  107. 4:34small here's how to change
  108. 4:37that go to the top toolbar go to
  109. 4:41settings go to
  110. 4:44editor
  111. 4:45General and you can check this checkbox
  112. 4:48for change font size with Control Plus
  113. 4:50Mouse wheel apply okay now you should be
  114. 4:55able to zoom in if you hold control and
  115. 4:57scroll your mouse wheel now we can
  116. 5:00actually read something in Java in order
  117. 5:03to get some code to run you need a main
  118. 5:05method within our class of main there's
  119. 5:07a set of curly braces we'll type the
  120. 5:11following public static void main add a
  121. 5:17set of parentheses then a set of curly
  122. 5:19braces within the set of parentheses
  123. 5:21you're going to type the following
  124. 5:24string straight brackets
  125. 5:27args like you're a pirate
  126. 5:30you need this method in order for your
  127. 5:32program to run over the course of this
  128. 5:34series we'll learn what each of these
  129. 5:36keywords means but as a beginner think
  130. 5:39of it as a magic spell that you have to
  131. 5:40say in order to get your program to run
  132. 5:43to run your program you're going to
  133. 5:44click the screen Arrow or you can hold
  134. 5:47shift plus
  135. 5:51F10 if we have written the main method
  136. 5:54successfully we should have no
  137. 5:56errors it says processed finished with
  138. 5:59exit code zero that means everything is
  139. 6:01running fine if I were to delete this
  140. 6:03main method we can't actually run the
  141. 6:07program so be sure that that method is
  142. 6:09in to Output some text to the console
  143. 6:12window we're going to type system with a
  144. 6:15capital s. out. print add a set of
  145. 6:19parentheses then a
  146. 6:21semicolon in Java you'll end most of
  147. 6:24your statements with a semicolon it's
  148. 6:26kind of like a period at the end of a
  149. 6:28sentence in the English language
  150. 6:30within the set of parentheses we'll add
  151. 6:32a set of double quotes whatever we would
  152. 6:35like to Output we're going to type
  153. 6:37within the set of double quotes think of
  154. 6:39a food you like I'll type I like
  155. 6:43pizza and then we can run this and this
  156. 6:45should be the
  157. 6:47output again I'm going to go to the
  158. 6:48Green Arrow or you can hold shift
  159. 6:52F10 and a console window should pop up
  160. 6:56that says I like pizza or whatever your
  161. 6:59favorite food is whatever you typed
  162. 7:03here let's add another line of
  163. 7:05code again type
  164. 7:10system.out.print end it with the
  165. 7:13semicolon within quotes which we type I
  166. 7:16like pizza it's really
  167. 7:20good let's run it
  168. 7:23again all right here's our output I like
  169. 7:27pizza it's really good
  170. 7:30now these are both on the same line to
  171. 7:32move your output down to the next line
  172. 7:35you can use print Ln meaning print
  173. 7:39line now it should look something like
  174. 7:42this I like pizza it's really
  175. 7:47good if you type back sln that's an
  176. 7:51escape sequence for a new line and that
  177. 7:53works too besides print line I like
  178. 7:57pizza it's really good
  179. 8:00let's add another line of text
  180. 8:04system.out.print or print
  181. 8:07line I like pizza it's really good buy
  182. 8:11me
  183. 8:12pizza and I'll add a new line
  184. 8:17character I like pizza it's really good
  185. 8:20buy me
  186. 8:22pizza or again you could use print
  187. 8:26line so print Ln for print
  188. 8:31line and this will do the same
  189. 8:35thing now you can write a comment
  190. 8:38comments aren't displayed as output to
  191. 8:40write a comment you use two forward
  192. 8:43slashes and the text should be gray out
  193. 8:46comments are good as notes for yourself
  194. 8:48or for other developers let's add a
  195. 8:50comment that this is my first Java
  196. 8:56program when we run this this won't
  197. 8:58display us out output it's really just
  198. 9:01used as notes we still have our output
  199. 9:04it hasn't
  200. 9:06changed you can even add a multi-line
  201. 9:08comment with a forward slash then an
  202. 9:12asterisk to end a multi-line comment you
  203. 9:15need an asterisk then a forward slash
  204. 9:18I'll write this is a
  205. 9:23multi- line
  206. 9:26comment this won't output either
  207. 9:31we don't see either of these
  208. 9:33comments this is specific for intellig
  209. 9:36but you can change the console font and
  210. 9:37the colors I kind of find this hard to
  211. 9:40read to do that we're going to go to the
  212. 9:42top menu under file go to
  213. 9:45settings under color scheme go to
  214. 9:48console font here you can change the
  215. 9:50font if you would like or the colors
  216. 9:52I'll change the colors I'll make the
  217. 9:54output bright green so it's easier to
  218. 9:57read I'll go to console
  219. 9:59standard output and select a new color
  220. 10:03I'll select something
  221. 10:04green apply and I think that looks
  222. 10:07pretty good it's more readable that way
  223. 10:10I'll also change the system output
  224. 10:13too I'll make it kind of gray out that's
  225. 10:18pretty
  226. 10:18good yeah and there's our output I like
  227. 10:22pizza it's really good buy me
  228. 10:25pizza now a shortcut to type system. out
  229. 10:27at print line if you're using intellig
  230. 10:30is to type s o u t then hit tab that's
  231. 10:34going to autogenerate a print line
  232. 10:35statement for you I didn't realize this
  233. 10:37trick until a little bit later so you
  234. 10:39don't always need to type out this
  235. 10:41statement you can just autogenerate it
  236. 10:43all right everybody and that is your
  237. 10:44first Java program your homework in the
  238. 10:47comment section is to post three print
  239. 10:49line statements maybe type A poem or
  240. 10:51some song lyrics or something and well
  241. 10:54everybody that is your first Java
  242. 10:55program
  243. 10:59all right everybody so we are moving on
  244. 11:01to variables in Java probably back in
  245. 11:04elementary school or middle school you
  246. 11:06learned about variables in algebra a
  247. 11:08variable was a representation of a
  248. 11:11number well in programming we have
  249. 11:13variables too a variable is a reusable
  250. 11:16container for a value a variable behaves
  251. 11:19as if it was the value it contains but
  252. 11:22variables can contain more than just
  253. 11:24numbers they can contain characters and
  254. 11:27even entire words there's different data
  255. 11:29types a whole number would be an integer
  256. 11:32a number that includes a decimal would
  257. 11:34be a double there's two categories of
  258. 11:36variables we'll discuss primitive and
  259. 11:39reference primitive variables are simple
  260. 11:42values like numbers they're stored
  261. 11:45directly in memory usually in a location
  262. 11:48known as the stack reference variables
  263. 11:52actually hold memory addresses on the
  264. 11:54stack they point to a location in a
  265. 11:57different area known as the Heap think
  266. 11:59of it this way if I give you a primitive
  267. 12:02variable that would be the same as me
  268. 12:04handing you
  269. 12:05$10 but if I give you a reference
  270. 12:08variable a memory address that would be
  271. 12:11the same as me giving you an IOU that
  272. 12:13says I owe you $10 but the actual money
  273. 12:16I owe you is at the bank that might be a
  274. 12:19beginner's way of thinking of the
  275. 12:20difference between a primitive and
  276. 12:22reference data type primitive I'm giving
  277. 12:24you $10 a reference data type I'm giving
  278. 12:27you an IOU that I owe you $10 we'll
  279. 12:31discuss the different data types we'll
  280. 12:32begin with integers an INT short for
  281. 12:36integer is a primitive data type I'll
  282. 12:39show you how we can create an integer
  283. 12:41variable in Java there's two steps to
  284. 12:44creating a variable the first is
  285. 12:46declaration the second is assignment
  286. 12:50let's say we would like to create a
  287. 12:51variable that stores a whole number an
  288. 12:53integer with the first step of
  289. 12:55Declaration we're going to first type
  290. 12:57the data type of what we would like to
  291. 12:59store exactly so for integers we will
  292. 13:02type int then we should think of a
  293. 13:04unique variable name we don't want to
  294. 13:06say X or something similar that would
  295. 13:09make sense in basic algebra but in
  296. 13:12programming you want your variables to
  297. 13:13be descriptive like shut up in
  298. 13:17programming you want your variables to
  299. 13:18be descriptive so let's say we are
  300. 13:20working with let's say in age end this
  301. 13:24line with a semicolon so this is
  302. 13:27declaration step one we are creating a
  303. 13:30variable named age if I was to print my
  304. 13:33variable of age
  305. 13:36system.out.print
  306. 13:38line let's print age and see what it
  307. 13:44is variable age might not have been
  308. 13:48initialized we're using a variable but
  309. 13:50we've never given it a value we've never
  310. 13:52completed step two of
  311. 13:54assignment so we can set age equal to a
  312. 13:57value so on my YouTube channel the
  313. 14:00average age of my viewers is
  314. 14:02approximately 21 or so so let's set age
  315. 14:05to be 21 and then we'll print
  316. 14:09it so that outputs 21 if I were to
  317. 14:13change this to
  318. 14:1530 then my variable age would behave as
  319. 14:19if it was the number 30 so those are
  320. 14:22integers they're whole numbers let's say
  321. 14:24that I'm
  322. 14:2530.5 let's see what happens exactly you
  323. 14:28can see that intellig is already giving
  324. 14:30us a warning that we're working with a
  325. 14:32different data type known as doubles so
  326. 14:35if I were to run this we get an error
  327. 14:38message we have incompatible types from
  328. 14:42double to in if we declare a variable as
  329. 14:45an integer they can only store whole
  330. 14:49integers let's think of a few more
  331. 14:51examples another example of an integer
  332. 14:53could be a year so let's say the year is
  333. 14:572025
  334. 14:59then we'll print our
  335. 15:02year
  336. 15:042025 or maybe a quantity int
  337. 15:09quantity equals
  338. 15:111 then we'll print our
  339. 15:14quantity one so in working with
  340. 15:17quantities they're whole numbers usually
  341. 15:20right we wouldn't have let's say half a
  342. 15:22product 1.5 I would like 1.5 Nintendo
  343. 15:26switches that wouldn't make sense right
  344. 15:28they would would be whole numbers like
  345. 15:30one or
  346. 15:33two now you can output a variable with a
  347. 15:36string of text as
  348. 15:38well let's say the year
  349. 15:43is then I'll add plus a variable year
  350. 15:48make sure that the variable is not
  351. 15:49within
  352. 15:51quotes So currently this says the year
  353. 15:54is
  354. 15:552025 you do have to pay attention to the
  355. 15:57spacing so after the word is I'm going
  356. 16:00to add a space to separate is and the
  357. 16:03variable
  358. 16:04year the year is
  359. 16:072025 now if our variable was within
  360. 16:11quotes we would literally be printing
  361. 16:14the word year the year is year so don't
  362. 16:19do that unless it's
  363. 16:21intentional let's go on to the next data
  364. 16:23type the next data type is double double
  365. 16:27is a number but it can contain a decimal
  366. 16:29portion unlike int ins are whole
  367. 16:33numbers so this time we will create a
  368. 16:35variable type of double what are some
  369. 16:38examples of a variable that could be a
  370. 16:40double let's say a price there could be
  371. 16:43dollars and cents
  372. 16:48$19.99 or perhaps a GPA a grade point
  373. 16:51average double GPA equal
  374. 16:573.5 or a temperature
  375. 17:00double temperature
  376. 17:04equals
  377. 17:0812.5 all right let's output some of
  378. 17:10these let's do
  379. 17:13Price
  380. 17:15system.out.print
  381. 17:18Line let's output the
  382. 17:23price
  383. 17:2419.99 let's add a unit of currency I'll
  384. 17:27pick American dollars
  385. 17:31so Dollar Sign Plus price
  386. 17:35$19.99 if you were to assign a variable
  387. 17:38that's a double data type but store an
  388. 17:40integer here's what would
  389. 17:44happen we would just add 0 Z to the end
  390. 17:47it's still a double so those are doubles
  391. 17:51they're numbers that can contain a
  392. 17:52decimal portion unlike
  393. 17:55integers okay then the next data type we
  394. 17:58have is charart
  395. 17:59meaning character Char pronounced like
  396. 18:02the word
  397. 18:04Charizard so with chars what are a few
  398. 18:07examples of single characters we could
  399. 18:09use let's say a grade like a letter
  400. 18:13grade with chars characters we enclose
  401. 18:16them within single quotes let's say we
  402. 18:19have the letter A that's the grade we
  403. 18:22got on the last test or
  404. 18:24something or maybe a symbol
  405. 18:29maybe I'll put an exclamation point or
  406. 18:32something or currency Char currency
  407. 18:37equals pick a unit of currency I'll pick
  408. 18:41dollars okay then let's output some of
  409. 18:43these
  410. 18:45system.out.print
  411. 18:48line let's print our
  412. 18:53grade which is an
  413. 18:55A or our symbol
  414. 18:59an exclamation point or our
  415. 19:03currency which is American
  416. 19:06dollars so then we have something called
  417. 19:09booleans booleans are either true or
  418. 19:13false we will declare this variable as a
  419. 19:17Boolean let's check to see if somebody
  420. 19:20is a student our variable name will be
  421. 19:22is student and this equals true or false
  422. 19:27so if I am a student I would say true
  423. 19:31now a common naming convention in Java
  424. 19:33is called camel case naming convention
  425. 19:36if your variable name takes up two words
  426. 19:39such as is and student when you combine
  427. 19:43them the first letter of the first word
  428. 19:45should be lowercase then any word after
  429. 19:48the first letter is going to be
  430. 19:50uppercase so is student and I will set
  431. 19:54that to be true maybe something is for
  432. 19:57sale or not Boolean for sale equals
  433. 20:02false it's not for sale is somebody
  434. 20:06online Boolean is online now I will set
  435. 20:11that to be true these are a few examples
  436. 20:14of Boolean
  437. 20:16variables now you could output them
  438. 20:18directly using system.out.print line
  439. 20:22Let's test it system.out.print line
  440. 20:26let's output is student
  441. 20:29are we a student that is true is
  442. 20:32something for sale for
  443. 20:35sale that is false now with Boolean
  444. 20:39variables we don't typically output them
  445. 20:41directly to the console we tend to use
  446. 20:43them more internally within a
  447. 20:46program one place in which you might see
  448. 20:48them is with the use of if statements
  449. 20:51and this is a future topic we'll revisit
  450. 20:53it later but I want to demonstrate the
  451. 20:55usefulness of booleans so this is an if
  452. 20:58statement we can check some condition
  453. 21:01parenthesis then curly
  454. 21:03braces if our condition is true if is
  455. 21:08student double equals true but we could
  456. 21:12simplify this
  457. 21:13to if is student if this variable
  458. 21:18contains true which it does we can
  459. 21:20execute some code
  460. 21:23system.out.print
  461. 21:25line we will print the following
  462. 21:28you are a
  463. 21:31student if this Boolean variable
  464. 21:34contains true we will print the
  465. 21:37following you are a
  466. 21:40student or we could add else an else
  467. 21:43Clause else let's
  468. 21:46print you are not a
  469. 21:50student So currently we're still a
  470. 21:53student but if this Boolean variable
  471. 21:55contained a value of false we would
  472. 21:58perform form the else
  473. 22:00statement you are not a student so we'll
  474. 22:04cover if statements in a future topic
  475. 22:06you don't necessarily need to know how
  476. 22:08these work right now but that's one of
  477. 22:10the uses of booleans they can only
  478. 22:12contain true or
  479. 22:15false these are a few of the basic
  480. 22:17primitive data types for beginners int
  481. 22:20for integers they contain whole numbers
  482. 22:23doubles are numbers but they can contain
  483. 22:25a decimal chars meaning characters are
  484. 22:28single characters and booleans which are
  485. 22:31either true or false these are a few of
  486. 22:34the beginner primitive data types but
  487. 22:37there's still a lot more than this
  488. 22:39there's floats there's long doubles all
  489. 22:41sorts of different data types but you
  490. 22:43don't need to know those as a beginner I
  491. 22:45would just stick to these four for now
  492. 22:47so then we have the reference data types
  493. 22:50A few of which include strings arrays
  494. 22:52and objects in this topic we're only
  495. 22:55going to discuss strings a series of
  496. 22:57characters phas and objects are really
  497. 22:59complex we'll revisit these later so
  498. 23:02we're going to create a string a series
  499. 23:05of
  500. 23:05characters so the data type is going to
  501. 23:08be string an example of a series of
  502. 23:11characters could be a name what's your
  503. 23:13name with strings we enclose them within
  504. 23:16double quotes chars single characters
  505. 23:20are single quotes it's really easy to
  506. 23:23mix the two up you might do it a few
  507. 23:25times as a beginner so type in your name
  508. 23:28in including a space I'll use my YouTube
  509. 23:30channel
  510. 23:32name and then we will output our name
  511. 23:36system.out.print Line Print your
  512. 23:41name and your first and last name should
  513. 23:44appear now we could use this variable of
  514. 23:47name within another string this is known
  515. 23:49as string
  516. 23:50concatenation so let's
  517. 23:52say
  518. 23:55hello plus our name
  519. 23:59hello bro code or whatever your name is
  520. 24:03what are a few other examples of strings
  521. 24:04we could
  522. 24:06write let's add string food
  523. 24:10equals again within a set of double
  524. 24:13quotes add your favorite food mine is
  525. 24:17pizza then let's type again
  526. 24:20system.out.print
  527. 24:23line your favorite food is
  528. 24:29plus the variable
  529. 24:31food your favorite food is
  530. 24:38pizza what about an email string email
  531. 24:42equals make up an email fake1
  532. 24:46[email protected]
  533. 24:50strings they can contain numbers but we
  534. 24:54treat them more as characters rather
  535. 24:56than actual numbers that we can use for
  536. 24:58any sort of math or
  537. 25:00arithmetic so let's print our email your
  538. 25:04email
  539. 25:05is plus
  540. 25:10email your email is fake1 [email protected]
  541. 25:15or what about a
  542. 25:18car string
  543. 25:20car think of a car that you like I will
  544. 25:23pick a Mustang that's another example of
  545. 25:26a string or a color
  546. 25:29string color equals
  547. 25:33red so strings are a series of
  548. 25:36characters they have a reference data
  549. 25:38type you may have also noticed that the
  550. 25:41font color of strings is different from
  551. 25:43the primitive data types too of booleans
  552. 25:45chars doubles and Ins at least within my
  553. 25:48workspace the font color is orange and
  554. 25:51with strings the font color of these
  555. 25:53data types is white now we're going to
  556. 25:56cover a few exercises
  557. 25:58so let's start with our name
  558. 26:01system.out.print line we will print
  559. 26:06hello plus our name variable it's a
  560. 26:10string hello your first and last
  561. 26:15name let's print our age
  562. 26:18system.out.print
  563. 26:20line u
  564. 26:23r we're going to insert our variable age
  565. 26:26then add another string another string
  566. 26:28literal you are age years old again do
  567. 26:33pay attention to the
  568. 26:35spacing you are 30 years
  569. 26:39old let's use my
  570. 26:42GPA that's a
  571. 26:45double system.out.print
  572. 26:51line your GPA
  573. 26:54is then we will insert our GPA
  574. 26:59your GPA is
  575. 27:023.5 okay let's use our Char of
  576. 27:08grade
  577. 27:09system.out.print
  578. 27:13line your average letter
  579. 27:17grade
  580. 27:21is our variable
  581. 27:25grade your average letter grade is is a
  582. 27:29now within a single print line statement
  583. 27:31we're going to combine multiple
  584. 27:33variables let's say we're going to
  585. 27:35Output our
  586. 27:37color a
  587. 27:39year and our
  588. 27:41car all within one print statement
  589. 27:46system.out.print
  590. 27:48line let's say we're choosing a car your
  591. 27:52choice is a We'll add our color variable
  592. 27:59plus the
  593. 28:01year plus our car let's see what happens
  594. 28:04exactly if we attempt
  595. 28:07this so we've combined all three
  596. 28:10variables red 2025 and our car of
  597. 28:13Mustang so we're going to add some space
  598. 28:16characters between each to separate them
  599. 28:19so plus a space character add that after
  600. 28:22the
  601. 28:23color after the
  602. 28:26year we'll add a space between color and
  603. 28:29year and year and
  604. 28:32car so your choice is a red 2025
  605. 28:38Mustang let's display the
  606. 28:41currency and a
  607. 28:44price
  608. 28:47system.out.print
  609. 28:50line the price
  610. 28:55is plus currency
  611. 28:58I picked American
  612. 29:00dollars plus
  613. 29:04price the price is
  614. 29:07$9.99 uh for a car let's up that that's
  615. 29:10way too cheap that's too good to be true
  616. 29:13let's say
  617. 29:15$9,999
  618. 29:1999 that's more
  619. 29:21realistic now we're going to use this
  620. 29:24Boolean variable of for
  621. 29:26sale if
  622. 29:29for sale if this is true then we're
  623. 29:33going to
  624. 29:39print there
  625. 29:42is
  626. 29:44a plus
  627. 29:47Car Plus the text of for
  628. 29:52sale else we will print something else
  629. 29:55I'll just copy this
  630. 29:59let's
  631. 30:01say
  632. 30:04the plus Car Plus is not for sale for
  633. 30:11sale
  634. 30:12currently that's set to false the
  635. 30:15Mustang is not for sale but if this were
  636. 30:20true there is a Mustang for sale for
  637. 30:24sale is set to true all right everybody
  638. 30:27so those are variables in Java a
  639. 30:30variable is a reusable container for a
  640. 30:33value a variable behaves as if it was
  641. 30:36the value that it contains there's two
  642. 30:39different categories of variables
  643. 30:41primitive and reference primitive data
  644. 30:43types are simple values like integers
  645. 30:47floats single characters and
  646. 30:51booleans they're stored directly in
  647. 30:53memory usually in a location known as
  648. 30:56the stack reference variables such as
  649. 30:59strings which are a series of characters
  650. 31:02like your name or an email they're more
  651. 31:05complicated we store a memory address
  652. 31:08that points to a location in the Heap
  653. 31:11and your homework assignment is in the
  654. 31:13comment section post five variables a
  655. 31:16string an integer a double A Char and a
  656. 31:20Boolean and if you don't do your
  657. 31:22homework you're going to make me sad and
  658. 31:24well everybody that is an introduction
  659. 31:26to variables in
  660. 31:29Java hello again friends so today I'm
  661. 31:32going to show you how we can accept user
  662. 31:34input in Java we will need the help of
  663. 31:37what is called a scanner a scanner is an
  664. 31:40object that allows us to accept user
  665. 31:42input in Java however in order to use a
  666. 31:44scanner we actually need to import it
  667. 31:47from the certain package called
  668. 31:48utilities or util at the top of our Java
  669. 31:51file we're going to write the following
  670. 31:54statement import Java do
  671. 31:58util this is a package do scanner
  672. 32:03scanner is a class so make sure you have
  673. 32:06this import at the top before we get
  674. 32:07started to sum up programming we
  675. 32:10typically accept input process it then
  676. 32:13produce output so that's why accepting
  677. 32:15user input is important we would like to
  678. 32:17do something with the input so to accept
  679. 32:21user input we're going to create a
  680. 32:23scanner object so type this scanner
  681. 32:27that's the name of the class
  682. 32:28class scanner that's going to be the
  683. 32:31name of the object we'll be working with
  684. 32:34do pay attention to the
  685. 32:35capitalization our scanner object is all
  686. 32:38lowercase characters whereas in the
  687. 32:40first scanner has an uppercase
  688. 32:42s scanner scanner equals new scanner add
  689. 32:47a set of parenthesis then a semicolon
  690. 32:50within the set of parentheses we're
  691. 32:52going to type
  692. 32:54system.in our scanner object can read
  693. 32:57user input however it is good practice
  694. 33:00if you create a scanner object at the
  695. 33:02end of your program when you're done
  696. 33:04we're going to take our scanner object
  697. 33:07and use the close
  698. 33:10method so scanner. close parenthesis
  699. 33:14semicolon if you don't close your
  700. 33:16scanner it can lead to unexpected
  701. 33:19behavior for example let's say that we
  702. 33:21read a file well after we're done
  703. 33:23reading the file we'll want to close the
  704. 33:25file when we open something we typically
  705. 33:28want to close
  706. 33:29it we're going to create a prompt within
  707. 33:32our console that says enter your name
  708. 33:34then a user will be able to type in
  709. 33:36their name we'll need a user prompt to
  710. 33:38work with so let's say system.out.print
  711. 33:42Line enter your
  712. 33:48name after receiving this prompt a user
  713. 33:51is going to be able to type in their
  714. 33:53name we're going to use our scanner
  715. 33:55object scanner
  716. 33:58next line parentheses semicolon so a
  717. 34:03user is going to be able to type in a
  718. 34:05string of text so let's type in our full
  719. 34:07name then hit
  720. 34:08enter however with this user input we're
  721. 34:11not doing anything with it so let's
  722. 34:14assign it to a
  723. 34:15variable so with our line of scanner.
  724. 34:19next line we will declare a variable and
  725. 34:22assign it string name equals scanner.
  726. 34:26nextline we have declaration and
  727. 34:30assignment those two
  728. 34:32steps and then once we have our name
  729. 34:35let's output it system.out.print line
  730. 34:39let's say hello plus
  731. 34:44name so enter your name type in your
  732. 34:47full name hit enter and the output
  733. 34:50should be hello whatever your first and
  734. 34:53last name is now if you prefer we can
  735. 34:56put the input on the same line as the
  736. 34:59prompt we're going to use print instead
  737. 35:02of print Line Print line will add a new
  738. 35:04line character to the
  739. 35:06end so let's stick with
  740. 35:09print so the input will be on the same
  741. 35:12line as the prompt if you prefer it that
  742. 35:15way so the next line method of our
  743. 35:18scanner object it reads A String of
  744. 35:20characters including any spaces if you
  745. 35:23don't want any spaces you can use next
  746. 35:29so if I were to type in my full name
  747. 35:31with the first and last name separated
  748. 35:33with the
  749. 35:34space we only get the first name next
  750. 35:38doesn't read any
  751. 35:40spaces so use what you would prefer I
  752. 35:43typically just use next
  753. 35:45line to read an integer there's a
  754. 35:48different method called Next int this
  755. 35:51time let's create a prompt of enter your
  756. 35:55age I'll use print instead of print line
  757. 35:59we will assign a variable of age int age
  758. 36:03equals use our scanner
  759. 36:06object use the built-in next int
  760. 36:10method then once we have our integer of
  761. 36:13age let's output it system.out.print
  762. 36:17line u
  763. 36:20r plus our variable age plus years old
  764. 36:30enter your name type in your name enter
  765. 36:32your age oops I'm going to add a colon
  766. 36:36and a space after my prompt just because
  767. 36:38I think it'll look
  768. 36:41better okay enter your full
  769. 36:44name enter your age hello your name you
  770. 36:49are whatever your age is years old so
  771. 36:53that's how to accept an integer if you
  772. 36:56were to type in a floating point number
  773. 36:58or a double anything that includes a
  774. 37:00decimal now check this out our variable
  775. 37:04of age should be an integer if we type
  776. 37:06in something that contains a decimal
  777. 37:08like a floating Point number or a double
  778. 37:10let's say that I'm
  779. 37:1325.9 well we get an error here it says
  780. 37:16we have an error at line 12 where we
  781. 37:18accept an integer so if you need a
  782. 37:22double there's a next Double method of
  783. 37:26scanners so this time let's ask for a
  784. 37:30GPA what is your GPA your grade point
  785. 37:35average we will assign a variable that's
  786. 37:37going to be of the double data type
  787. 37:40double GPA equals again use our
  788. 37:44scanner use the next Double
  789. 37:48method and then let's output
  790. 37:52it system.out.print
  791. 37:55line your GPA is plus our variable
  792. 38:02GPA so again let's go through the
  793. 38:04prompts type in your name type in your
  794. 38:08age what is your GPA uh let's use print
  795. 38:11instead of print
  796. 38:15Line enter your name enter your age what
  797. 38:20is your GPA let's say my GPA is 2.1 I'm
  798. 38:24just barely passing hey C get degrees
  799. 38:28right your GPA is the GPA variable so
  800. 38:33that's how to accept the double use the
  801. 38:35next Double method of scanners now for
  802. 38:38some reason if you need a Boolean
  803. 38:40booleans are either true or false here's
  804. 38:43how we can accept
  805. 38:45that system.out.print line let's ask are
  806. 38:49you a
  807. 38:51student then I'll add a prompt for
  808. 38:54true or false so the US user knows to
  809. 38:58type in one of these instead of yes or
  810. 39:00no so we will create a Boolean variable
  811. 39:04of is student equals use our scanner
  812. 39:09object use the next Boolean
  813. 39:13method and then let's output
  814. 39:16it system.out.print
  815. 39:19line let's say student colon space plus
  816. 39:24is student I think an if statement would
  817. 39:26be better we'll cover that in a
  818. 39:29moment again go through the prompts type
  819. 39:31in your name type in your age type in a
  820. 39:35GPA are you a student true or false
  821. 39:39let's say that's true student
  822. 39:43true and again I'm going to use print
  823. 39:45instead of print
  824. 39:47line not necessary but I think it looks
  825. 39:49better let's use an if statement instead
  826. 39:52when checking is student is true or not
  827. 39:55so if is student again if statements
  828. 39:58will be a future lesson we'll cover
  829. 40:00pretty soon if is student if that's true
  830. 40:04then we will
  831. 40:06print let's print you are
  832. 40:11enrolled as a
  833. 40:14student else we will print something
  834. 40:16else if that's not true if it's false
  835. 40:20you
  836. 40:23are
  837. 40:25not enrolled
  838. 40:32okay enter your name enter your age
  839. 40:35enter a GPA are you a student let's say
  840. 40:39that it's
  841. 40:40false okay we have our name our age our
  842. 40:44GPA and then we're outputting you are
  843. 40:47not enrolled you are not enrolled in
  844. 40:49classes because we said
  845. 40:51false now if we put true
  846. 41:00well then we get the output you are
  847. 41:02enrolled as a student that's how to
  848. 41:05accept user input of different data
  849. 41:07types A few of which include strings
  850. 41:10integers doubles and
  851. 41:12booleans now there's one common issue
  852. 41:15when accepting an integer or double than
  853. 41:18accepting a string let me
  854. 41:24demonstrate so we have our scanner
  855. 41:27let's say we ask for a user's
  856. 41:30age enter your
  857. 41:33age and I'll make this a print statement
  858. 41:36instead of print
  859. 41:37line int age equals use our scanner use
  860. 41:41the next int
  861. 41:44method and then let's ask what the
  862. 41:47user's favorite color is enter your
  863. 41:51favorite
  864. 41:53color we will store that within a
  865. 41:55variable named color and it's going to
  866. 41:57be of the string data type string color
  867. 41:59equals use our scanner object use the in
  868. 42:03next line
  869. 42:05method then we'll output the
  870. 42:08following system.out.print line you are
  871. 42:13Plus
  872. 42:15Age years
  873. 42:18old as well
  874. 42:20as you like the
  875. 42:24color plus our variable color
  876. 42:28here's what
  877. 42:35happens enter your age 25 hit
  878. 42:39enter enter your favorite color you are
  879. 42:4225 years old you like the color so
  880. 42:45what's going on here exactly so when we
  881. 42:48type in a number for example 25 then hit
  882. 42:51enter within the input buffer there's
  883. 42:54still a new line character because we
  884. 42:56hit enter
  885. 42:57so the next line method is picking up
  886. 42:59that new line character and using that
  887. 43:02as the
  888. 43:03input so we need to get rid of that new
  889. 43:05line character this is a common problem
  890. 43:08that you might see in Java if you accept
  891. 43:10an integer or a double then accept a
  892. 43:13string of text so one way in which we
  893. 43:15can clear the input buffer to get rid of
  894. 43:18that new line character is after
  895. 43:20accepting an integer or a double we can
  896. 43:22use our scanner and call the next line
  897. 43:26method but don't assign it to anything
  898. 43:29that should get rid of that new line
  899. 43:30character in the input buffer if that
  900. 43:32problem comes up so let's try this again
  901. 43:35enter an age enter a color you are 25
  902. 43:39years old you like the color red so if
  903. 43:43you encounter that problem I would just
  904. 43:45call the next line method of your
  905. 43:47scanner object to clear the input buffer
  906. 43:50now we're going to cover an
  907. 43:52exercise in this exercise we're going to
  908. 43:55calculate the area of a rectangle
  909. 43:57what I like to do when creating a
  910. 43:59project is declare all of my variables
  911. 44:01at the top again make sure that you're
  912. 44:04importing the scanner class I will
  913. 44:06create a double variable of width I'll
  914. 44:11set that to be
  915. 44:12zero and a double variable of height
  916. 44:16also
  917. 44:18zero and double
  918. 44:21area I will set that to be
  919. 44:24zero we're going to accept some user
  920. 44:26input
  921. 44:27and reassign width and
  922. 44:30height so we need a scanner object to
  923. 44:33accept user input scanner scanner equals
  924. 44:36new scanner parentheses semicolon within
  925. 44:40the parenthesis we will type
  926. 44:44system.in and again since we're opening
  927. 44:46a scanner it's good practice to close it
  928. 44:49I sometimes forget to do that scanner.
  929. 44:52close we'll create a prompt to tell the
  930. 44:55user to enter in a width we can use
  931. 44:58system.out.print line or print for
  932. 45:01prompts I like to use
  933. 45:03print enter the
  934. 45:07width we will take our width we've
  935. 45:10already declared our width once we don't
  936. 45:11need to type the data type again we can
  937. 45:14reuse it width equals use our scanner
  938. 45:19object use the an next Double method
  939. 45:22because we're accepting a double a width
  940. 45:27let's do this with height I'll just copy
  941. 45:29these two lines then change width to
  942. 45:38height all right let's test it
  943. 45:42currently let's make up some numbers 3.1
  944. 45:45and
  945. 45:464.2 all right once we have the width and
  946. 45:49the height we have to calculate the area
  947. 45:53so we're going to take our area variable
  948. 45:56equals
  949. 45:57our width times our
  950. 46:02height and then let's output
  951. 46:05it system.out.print line the area
  952. 46:10is plus our variable
  953. 46:14area so let's say 3.2 and
  954. 46:194.3 the area is
  955. 46:27after area We'll add centimeters you
  956. 46:30could do centimet squared if you prefer
  957. 46:33or you could even add a superscript of
  958. 46:35two so if you're on Windows make sure
  959. 46:37num lock is on hold alt then type 0178
  960. 46:42on the
  961. 46:43numpad if you would like a superscript
  962. 46:45of two you know because we're working
  963. 46:48with
  964. 46:49areas so let's say
  965. 46:525.3 and
  966. 46:556.4 the area is is
  967. 47:27we're going to create a game of Mad Libs
  968. 47:29Mad Libs is a game where you have a
  969. 47:31story and a user fills in different
  970. 47:33words the result is that it gives you a
  971. 47:35story that's really silly or doesn't
  972. 47:37necessarily make sense at times but
  973. 47:39that's what we'll be doing in this video
  974. 47:41it will help us practice accepting user
  975. 47:43input since we're accepting user input
  976. 47:45we'll need to import the scanner class
  977. 47:47at the top of our Java file import
  978. 47:52java.util.scanner
  979. 47:54a scanner is going to help us accept
  980. 47:57user input we'll declare that and assign
  981. 48:00it scanner scanner equals new scanner
  982. 48:04parenthesis
  983. 48:05semicolon then within the parenthesis we
  984. 48:08will type
  985. 48:11system.in when writing a program I like
  986. 48:13to declare all of my variables first all
  987. 48:16these variables are going to be of the
  988. 48:17string data type we will need an
  989. 48:20adjective you're going to get an English
  990. 48:22lesson today
  991. 48:24too adjective one an adjective describes
  992. 48:28something like Fast slow cheap
  993. 48:32expensive we'll need a noun string noun
  994. 48:37one a noun is a person place or thing
  995. 48:41we'll need another
  996. 48:43adjective adjective
  997. 48:45two we'll need a verb a verb is an
  998. 48:49action like sleeping or running verb one
  999. 48:54then another adjective a again an
  1000. 48:57adjective describe something all right
  1001. 49:00so let's work on our
  1002. 49:02story we'll have four
  1003. 49:10lines so for the first line we'll say
  1004. 49:14today I went to
  1005. 49:18a we'll insert an adjective adjective
  1006. 49:21one we're going to describe a zoo that
  1007. 49:24we went to
  1008. 49:27we haven't asked for user input yet
  1009. 49:29we're getting a warning that this
  1010. 49:31variable has not been initialized that's
  1011. 49:34okay we'll take care of that
  1012. 49:36soon so today I went to a a description
  1013. 49:40an adjective
  1014. 49:42zoo in an
  1015. 49:47exhibit I saw
  1016. 49:50a plus a noun a noun is a person place
  1017. 49:55or thing like a gorilla or
  1018. 49:59harambe then we'll add some punctuation
  1019. 50:02a
  1020. 50:03period we'll say noun
  1021. 50:08one
  1022. 50:12was plus adjective
  1023. 50:18two
  1024. 50:21and our verb verb one then add some
  1025. 50:25punctuation
  1026. 50:30so we could say that harambe was big and
  1027. 50:35sleeping so a verb is an
  1028. 50:40action I
  1029. 50:43was plus adjective
  1030. 50:46three then add some
  1031. 50:48punctuation all right now we just need
  1032. 50:50to accept user input so let's do that
  1033. 50:54after assigning the variables we'll need
  1034. 50:56five print
  1035. 51:06statements we will prompt the user to
  1036. 51:09enter an
  1037. 51:11adjective enter an adjective I'll give a
  1038. 51:14hint that an adjective is a
  1039. 51:16description we're describing
  1040. 51:21something enter a
  1041. 51:25noun enter a
  1042. 51:29noun I'll give a hint that we are
  1043. 51:31looking for
  1044. 51:33a animal or
  1045. 51:40person we'll need another adjective we
  1046. 51:43can just copy this
  1047. 51:44line then we need a
  1048. 51:46verb enter a verb and we want it in
  1049. 51:50present tense so ending with ing like
  1050. 51:58running I'll give a hint that a verb is
  1051. 52:01an
  1052. 52:02action and then another
  1053. 52:05adjective all right now we just need to
  1054. 52:07assign these variables we've already
  1055. 52:09declared
  1056. 52:10them adjective one equals take our
  1057. 52:14scanner use the next line
  1058. 52:19method and really we can just copy this
  1059. 52:22line paste it and then change what we're
  1060. 52:24assigning it to
  1061. 52:27we're accepting strings for each of
  1062. 52:29these lines we have adjective one noun
  1063. 52:33one adjective
  1064. 52:352 verb one and adjective
  1065. 52:393 all right now the last thing that I'm
  1066. 52:41forgetting since we opened up a scanner
  1067. 52:44it is good practice to close it at the
  1068. 52:46end of our file if we don't it can lead
  1069. 52:48to unexpected Behavior if we leave any
  1070. 52:50open
  1071. 52:51resources scanner. close all right we
  1072. 52:55are ready to run this
  1073. 52:57I'm going to prefix a new line character
  1074. 52:59before displaying our
  1075. 53:01story just so that it's not so close to
  1076. 53:03the prompts when outputting
  1077. 53:05it enter an adjective a
  1078. 53:09description I'm going to say
  1079. 53:13suspicious enter a noun so I like making
  1080. 53:17fun of Mark Zuckerberg I'm going to
  1081. 53:19write the name of a person Mark
  1082. 53:24Zuckerberg enter an adjective
  1083. 53:27smelly enter a verb ending with
  1084. 53:30ing
  1085. 53:32screaming enter an adjective
  1086. 53:36happy and here's our Mad Libs game today
  1087. 53:39I went to a suspicious zoo in an exhibit
  1088. 53:43I saw a Mark
  1089. 53:44Zuckerberg Mark Zuckerberg was smelly
  1090. 53:47and screaming I was happy so for your
  1091. 53:51Mad Libs game post your results in the
  1092. 53:53comment section because I would like to
  1093. 53:55read them I want to see what you you
  1094. 53:56guys wrote or if you wrote a different
  1095. 53:58story post that too and well everybody
  1096. 54:01that is a simple Mad Libs game that we
  1097. 54:04can write using
  1098. 54:07Java all right everybody so we got a
  1099. 54:10really important topic to discuss today
  1100. 54:12although I can be a little dry and that
  1101. 54:14is the use of different arithmetic
  1102. 54:15operators in Java when it comes to
  1103. 54:17programming you got to math sometimes
  1104. 54:20it's a pretty important topic so let's
  1105. 54:22start with some basic addition in this
  1106. 54:24demonstration we will have three
  1107. 54:25variables X will equal 10 y will equal 2
  1108. 54:31and Z will store the result we'll
  1109. 54:34declare it but not yet assign it so for
  1110. 54:36basic addition we'll store our result
  1111. 54:39within z z equals X for addition you can
  1112. 54:42use the plus sign
  1113. 54:45Y what is 10 + 2 then we'll display
  1114. 54:49it we're storing the result within
  1115. 54:53Z which gives us 12
  1116. 54:58then we have
  1117. 54:59subtraction z = x -
  1118. 55:03y 10 - 2 is
  1119. 55:078
  1120. 55:09multiplication for multiplication we're
  1121. 55:12going to use an asterisk 10 *
  1122. 55:152 is
  1123. 55:1820 for division we use a forward slash z
  1124. 55:22= x / y 10 /
  1125. 55:282 is
  1126. 55:305 there's also the modulus operator
  1127. 55:32which is a percent sign that would give
  1128. 55:35you the remainder of any
  1129. 55:36division Z is going to store the
  1130. 55:39remainder this time x modulus y so
  1131. 55:44modulus is a percent sign 10 divides by
  1132. 55:47two evenly so the remainder is going to
  1133. 55:49be
  1134. 55:51zero but if we divided 10 by three that
  1135. 55:55doesn't divide evenly
  1136. 55:5710 modulus 3 gives us a remainder of one
  1137. 56:01the modulus operator is really useful if
  1138. 56:03you need to determine if a number is
  1139. 56:05even or not or when working with time
  1140. 56:07you need to see if something is
  1141. 56:09divisible by 60 seconds or 60
  1142. 56:13minutes we'll get more into that later
  1143. 56:15but you should at least know what the
  1144. 56:16modulus operator
  1145. 56:18is now we have something called
  1146. 56:20augmented assignment operators instead
  1147. 56:23of storing the result within Z this time
  1148. 56:25we're going to reassign the result to
  1149. 56:27variable X so let's say we would like to
  1150. 56:30add 10 and 3 together but store it
  1151. 56:33within X well we could say x = x + y
  1152. 56:38this would technically
  1153. 56:41work so 10 + 3 is 13 but there is a
  1154. 56:46shortcut though that we could use and
  1155. 56:48that is by using augmented assignment
  1156. 56:50operators we'll condense some of these
  1157. 56:53steps instead of saying x = x + y we
  1158. 56:56simply say x + = y and that does the
  1159. 57:01same
  1160. 57:02thing 10 + 3 is
  1161. 57:0513 there's also
  1162. 57:08subtraction x = X -
  1163. 57:13y 10 - 3 is 7 but we could condense it
  1164. 57:17to be x - equal
  1165. 57:21y that gives us
  1166. 57:247 or multiplication x = x * y 10 * 3 is
  1167. 57:3130 but we can condense it to be x * =
  1168. 57:37y that is also
  1169. 57:4030 for division x = x /
  1170. 57:46y that gives us three now one thing to
  1171. 57:49pay attention to with division all right
  1172. 57:51so 10 / 3 should be 3.33 repeating right
  1173. 57:55well we're using something called
  1174. 57:57integer division we're not able to store
  1175. 57:59the decimal portion because we're
  1176. 58:01working with whole integers so that's
  1177. 58:03something to pay attention to when
  1178. 58:04you're using integer Division if we were
  1179. 58:07working with
  1180. 58:09doubles then we would retain that
  1181. 58:11decimal portion so do pay attention to
  1182. 58:14that we'll discuss that in another video
  1183. 58:16when it comes to typ casting for the
  1184. 58:18augmented assignment version that would
  1185. 58:20be x / = Y and let's change that to be 2
  1186. 58:26so 10 / 2 is
  1187. 58:295 then we have the modulus operator x =
  1188. 58:34x modulus y 10 divides by 2
  1189. 58:39evenly X will be zero the augmented
  1190. 58:43assignment version of this would be X
  1191. 58:45modulus equals
  1192. 58:47y and that would also be 0o but if y
  1193. 58:51were
  1194. 58:52three then that would be one now we have
  1195. 58:55increment and decrement operators you
  1196. 58:58see this a lot with looping structures
  1197. 59:00let's say that x equal 1 this time
  1198. 59:04instead of saying x = x + 1 which would
  1199. 59:08give us 2 or even the augmented
  1200. 59:11assignment inversion which would be X+ =
  1201. 59:161 well there's even a shortcut for this
  1202. 59:19if we're only incrementing by one and
  1203. 59:22that is
  1204. 59:24x++ we're incrementing X by
  1205. 59:27one you tend to see this in a lot of
  1206. 59:29looping structures but that's a topic
  1207. 59:31for another day each time I increment X
  1208. 59:34by
  1209. 59:351 it's going to be retained so now x = 3
  1210. 59:40x ++ again well now X is
  1211. 59:444 now there's also the decrement
  1212. 59:47operator x = X - one of course that's
  1213. 59:52going to be zero this
  1214. 59:54time so to decrement you can use x -
  1215. 59:59minus so now X is zero let's do it again
  1216. 1:00:03X
  1217. 1:00:04-- now X is -1 x
  1218. 1:00:08-- X is
  1219. 1:00:11-2 so those are the increment and
  1220. 1:00:14decrement operators you'll see these
  1221. 1:00:16again when we reach the topic on Loops
  1222. 1:00:19the last thing I need to discuss today
  1223. 1:00:21is the order of operations and there's
  1224. 1:00:23an acronym PEMDAS it's parentheses
  1225. 1:00:26exponents multiplication division
  1226. 1:00:29addition then subtraction in that order
  1227. 1:00:32you may hear the phrase to remember this
  1228. 1:00:34please excuse my dear Aunt Sally but I
  1229. 1:00:37like the phrase please excuse my dope
  1230. 1:00:40ass swag that's my version that I like
  1231. 1:00:42to use let's say we have the equation
  1232. 1:00:45double result equals 3 + 4 time
  1233. 1:00:52parentheses 7 - 5 divided by
  1234. 1:00:572.0 I'm dividing by 2.0 because right
  1235. 1:01:01now we're using integer division which
  1236. 1:01:03we previously talked about so I'm going
  1237. 1:01:05to use 2.0 so we retain that decimal
  1238. 1:01:07portion again that's a topic for another
  1239. 1:01:10day let's display the
  1240. 1:01:13result so I don't know what the result
  1241. 1:01:15is but it is 7
  1242. 1:01:207.0 in what order do we solve this
  1243. 1:01:22equation exactly well we can use that
  1244. 1:01:24acronym of pendos
  1245. 1:01:26from the left to the right we solve
  1246. 1:01:28anything within parentheses first so
  1247. 1:01:30going from the left to the right we skip
  1248. 1:01:33over any addition multiplication we're
  1249. 1:01:35looking for parentheses which we have
  1250. 1:01:37right here if you enclose part of an
  1251. 1:01:39equation within parentheses you would
  1252. 1:01:41solve that first 7 - 5 is 2 there's no
  1253. 1:01:46more
  1254. 1:01:48parentheses then we check any exponents
  1255. 1:01:51there are no
  1256. 1:01:52exponents
  1257. 1:01:54multiplication well we have
  1258. 1:01:56multiplication right here we would solve
  1259. 1:01:57that next 4 * 2 is 8 there's no more
  1260. 1:02:02multiplication then we have
  1261. 1:02:04division we have 8 / 2 which gives us
  1262. 1:02:084 then any addition 3 + 4 is 7 and that
  1263. 1:02:14was our original result and then you
  1264. 1:02:16would do any subtraction last so that is
  1265. 1:02:18the order of operations when it comes to
  1266. 1:02:20arithmetic operators all right everybody
  1267. 1:02:23so that is some basic arithmetic and
  1268. 1:02:25different operators you can use in
  1269. 1:02:28Java hey everybody so in this video
  1270. 1:02:31we're going to create a shopping cart
  1271. 1:02:33program a user is going to be able to
  1272. 1:02:34type in an item set a price for each and
  1273. 1:02:37then ask for a quantity the output is
  1274. 1:02:40that it's going to give you a total
  1275. 1:02:41based on the price and how much you're
  1276. 1:02:43buying this would be a good exercise for
  1277. 1:02:45us to get us more comfortable with
  1278. 1:02:46accepting user input all right let's get
  1279. 1:02:48started everybody we'll need the help of
  1280. 1:02:50a scanner the scanner allows us to
  1281. 1:02:53accept user input we'll need to import
  1282. 1:02:55that at the at the top so be sure that
  1283. 1:02:56you have this line of code import
  1284. 1:03:00java.util.scanner
  1285. 1:03:03we'll create a scanner object scanner
  1286. 1:03:07scanner equals new scanner parentheses
  1287. 1:03:11semicolon type
  1288. 1:03:14system.in then if we open a scanner it's
  1289. 1:03:17good practice to close it when we're
  1290. 1:03:19done with it scanner. close if you leave
  1291. 1:03:23resources open it can lead to unexpected
  1292. 1:03:25behavior when creating a project I like
  1293. 1:03:28to declare all of my variables at the
  1294. 1:03:30top let's say we have an item name that
  1295. 1:03:33data type is going to be a string string
  1296. 1:03:36item what are we buying
  1297. 1:03:38exactly we need a price that would be a
  1298. 1:03:41double because there might be dollars
  1299. 1:03:44and cents a quantity how many of
  1300. 1:03:48something are we buying int quantity
  1301. 1:03:51pick a unit of currency this could be a
  1302. 1:03:54character char
  1303. 1:03:56currency I'm going to initialize this
  1304. 1:03:59right away American
  1305. 1:04:01dollars and then a total we're going to
  1306. 1:04:04multiply the price by the quantity to
  1307. 1:04:07give a
  1308. 1:04:08total double total now if you would like
  1309. 1:04:12you can initialize some of these right
  1310. 1:04:14away and assign them for example let's
  1311. 1:04:16say we are at a pizza
  1312. 1:04:19restaurant I could set the item to be
  1313. 1:04:21pizza right
  1314. 1:04:22away or within this program we could
  1315. 1:04:25fill it in then let's create some
  1316. 1:04:28prompts I'll use
  1317. 1:04:33system.out.print what item would you
  1318. 1:04:36like to
  1319. 1:04:38buy then let's accept user input we're
  1320. 1:04:42going to assign our string of item equal
  1321. 1:04:44to use our scanner object and use the in
  1322. 1:04:48next line
  1323. 1:04:50method then let's just print it to
  1324. 1:04:53verify that it's working
  1325. 1:04:58what item would you like to buy maybe
  1326. 1:05:00this time I am buying a hamburger that
  1327. 1:05:03will output hamburger we know that we
  1328. 1:05:06can get user input so let's delete this
  1329. 1:05:08line I was just testing it let's ask for
  1330. 1:05:12a
  1331. 1:05:13price you could set an initial
  1332. 1:05:16price if you would
  1333. 1:05:19like or we could ask for one depending
  1334. 1:05:21on how you want to write this
  1335. 1:05:23program so let's system. about the
  1336. 1:05:28print what is the price for
  1337. 1:05:34each if we're working with a price a
  1338. 1:05:37double we'll use the next Double method
  1339. 1:05:40we're going to assign our price equal to
  1340. 1:05:44use our scanner. next
  1341. 1:05:48Double and then let's test it by
  1342. 1:05:50outputting the
  1343. 1:05:54price okay let's say I'm buying a pizza
  1344. 1:05:57this time what is the price for each
  1345. 1:06:00$4.99 and that outputs
  1346. 1:06:03$4.99 so it is a good idea every once in
  1347. 1:06:06a while to test your code just to be
  1348. 1:06:09sure that it all works as you go
  1349. 1:06:11along then we'll need a
  1350. 1:06:16quantity let's ask how many would you
  1351. 1:06:22like we will assign our quantity
  1352. 1:06:24variable
  1353. 1:06:26equals
  1354. 1:06:28scanner. next int since we're working
  1355. 1:06:31with whole numbers this
  1356. 1:06:33time then let's test our quantity to see
  1357. 1:06:36if it
  1358. 1:06:38works I don't know maybe I'm buying a
  1359. 1:06:40hot dog this time what is the price per
  1360. 1:06:42each
  1361. 1:06:43$4.99 how many would you like I would
  1362. 1:06:46like 12 hot dogs
  1363. 1:06:4912 and I'm just going to make sure I'm
  1364. 1:06:51using print instead of print line just
  1365. 1:06:53because I think it looks better
  1366. 1:06:57now we'll create a total we don't need
  1367. 1:06:59user input for that we're going to use
  1368. 1:07:02the variables that we've already had
  1369. 1:07:04after assigning them total equals the
  1370. 1:07:08price times the
  1371. 1:07:13quantity and then I will just test the
  1372. 1:07:19total maybe I'm buying a pizza or
  1373. 1:07:22pizzas each is $4.99
  1374. 1:07:26and I buying three of
  1375. 1:07:28them
  1376. 1:07:3144.97 instead let's output the
  1377. 1:07:36following I'm going to use a new line
  1378. 1:07:38character just to separate our output
  1379. 1:07:40from the
  1380. 1:07:41prompts you have
  1381. 1:07:45bought plus our variable of
  1382. 1:07:51quantity plus a space character plus our
  1383. 1:07:55variable
  1384. 1:07:57item but it might be more than one so
  1385. 1:07:59let's say/ s it might be pizza or
  1386. 1:08:05pizzas then we will display the price
  1387. 1:08:08including the unit of
  1388. 1:08:11currency your total
  1389. 1:08:15is I'll add our unit of currency it's a
  1390. 1:08:19character I picked American dollars but
  1391. 1:08:22pick whatever you would like plus our
  1392. 1:08:25total
  1393. 1:08:26that we have
  1394. 1:08:28calculated all right let's see the
  1395. 1:08:31result what item would you like to buy I
  1396. 1:08:33would like to buy a pizza what is the
  1397. 1:08:36price for each
  1398. 1:08:38$4.99 how many would you like I would
  1399. 1:08:41like eight pizzas maybe it's for a party
  1400. 1:08:44you have bought eight pizzas your total
  1401. 1:08:47is
  1402. 1:08:51$19.92 all right everybody so that is a
  1403. 1:08:54shopping cart program that you can write
  1404. 1:08:55as an exercise using
  1405. 1:08:59Java hello everybody today we're going
  1406. 1:09:01to discuss if statements in Java an if
  1407. 1:09:04statement performs a block of code if
  1408. 1:09:07its condition is true we've discussed
  1409. 1:09:10how Boolean variables can be used within
  1410. 1:09:12if statements we may be a little
  1411. 1:09:14familiar with them already in this
  1412. 1:09:16demonstration we will have a variable of
  1413. 1:09:19age set it equal to be some
  1414. 1:09:22age age will be an integer I would like
  1415. 1:09:25to check if my variable of age is
  1416. 1:09:27greater than or equal to 18 if so we can
  1417. 1:09:30print a message that you are an adult to
  1418. 1:09:33write an if statement will type if
  1419. 1:09:35parentheses curly braces within the set
  1420. 1:09:38of curly braces we can check a condition
  1421. 1:09:40to see if it's true so let's check to
  1422. 1:09:43see if age is greater than or equal to
  1423. 1:09:5018 if that's true we'll print the
  1424. 1:09:53following you are an
  1425. 1:09:59adult that would result in US outputting
  1426. 1:10:02you are an
  1427. 1:10:04adult now if this condition was false if
  1428. 1:10:08it's not true let's say that my age is
  1429. 1:10:1212 well then we skip over the if
  1430. 1:10:15statement we check if this condition is
  1431. 1:10:17true if it is we perform this code if
  1432. 1:10:20not we skip it alternatively we could
  1433. 1:10:24add an else clause
  1434. 1:10:26this would be another statement we would
  1435. 1:10:28add else if this is false then do
  1436. 1:10:31something else so let's output the
  1437. 1:10:34following you are a
  1438. 1:10:38child since the age is 12 we would
  1439. 1:10:42output you are a
  1440. 1:10:46child there's also an else if Clause so
  1441. 1:10:49we have one of two
  1442. 1:10:51possibilities do this or do that now we
  1443. 1:10:54could add something called else if
  1444. 1:10:57Clause let's check else
  1445. 1:11:00if age is less than
  1446. 1:11:04zero if this statement is
  1447. 1:11:07true then let's say you haven't been
  1448. 1:11:12born
  1449. 1:11:14yet now we're doing one of three
  1450. 1:11:18things but it all depends on these
  1451. 1:11:21conditions our else Clause is kind of
  1452. 1:11:24like the default if none of the above
  1453. 1:11:26conditions are
  1454. 1:11:27true so let's test this my age variable
  1455. 1:11:31is set to
  1456. 1:11:3219 this would display you are an
  1457. 1:11:35adult if my age variable was -1 we would
  1458. 1:11:40display you haven't been born
  1459. 1:11:42yet this statement was false so we go
  1460. 1:11:45down to the next
  1461. 1:11:47one this is true NE 1 is less than zero
  1462. 1:11:51so we do
  1463. 1:11:52this or if I was 10
  1464. 1:11:56well then these top two conditions are
  1465. 1:11:58both false so we do whatever is within
  1466. 1:12:00the lse
  1467. 1:12:01statement you can add as many else if
  1468. 1:12:04Clauses as you would
  1469. 1:12:05like so here's another
  1470. 1:12:09one else
  1471. 1:12:12if our variable age is equal to zero
  1472. 1:12:17exactly double equals is the comparison
  1473. 1:12:20operator we're checking if a variable or
  1474. 1:12:23a value is equal to another value or
  1475. 1:12:27variable you don't want to use single
  1476. 1:12:29equals because that's the assignment
  1477. 1:12:31operator Java thinks we're trying to
  1478. 1:12:33assign a variable when making a
  1479. 1:12:36comparison you use double
  1480. 1:12:38equals is our variable age equal to
  1481. 1:12:42zero then we'll print something else
  1482. 1:12:45okay we're going to print you are a
  1483. 1:12:50baby I will set variable H to be zero
  1484. 1:12:55you are a baby because our variable age
  1485. 1:12:59is equal to zero
  1486. 1:13:02exactly let's add another L
  1487. 1:13:05statement there's something I want to
  1488. 1:13:07demonstrate let's check to
  1489. 1:13:10see if age is greater than or equal to
  1490. 1:13:1665 then we will print you are a
  1491. 1:13:22senior I'll set age to be 70
  1492. 1:13:26here's the
  1493. 1:13:27result you are an adult so why did we
  1494. 1:13:30print this statement rather than this
  1495. 1:13:33one well with these if statements we
  1496. 1:13:36check them from the top down this
  1497. 1:13:39condition is technically true as well as
  1498. 1:13:41this one 70 is greater than or equal to
  1499. 1:13:4418 so we execute this code and skip over
  1500. 1:13:48the rest even though this one is also
  1501. 1:13:50true you do have to pay attention to the
  1502. 1:13:52positioning of your if and else if
  1503. 1:13:54statements in this situation it would be
  1504. 1:13:56better if we were to check to see if
  1505. 1:13:58somebody's age was greater than or equal
  1506. 1:14:00to 65 first so let's turn this into the
  1507. 1:14:03if
  1508. 1:14:08statement and then where we check if age
  1509. 1:14:10is greater than or equal to 18 we will
  1510. 1:14:12use an else if Clause instead now this
  1511. 1:14:15should
  1512. 1:14:17work you are a
  1513. 1:14:20senior but if the age was 50
  1514. 1:14:26well then you are an adult now we're
  1515. 1:14:28going to modify this program
  1516. 1:14:31slightly this time we will ask for user
  1517. 1:14:33input we'll need the help of a scanner
  1518. 1:14:36at the top of our Java file we're going
  1519. 1:14:38to import
  1520. 1:14:41java.util dothe class of
  1521. 1:14:44scanner we'll need to create a scanner
  1522. 1:14:46object scanner scanner equals new
  1523. 1:14:51scanner within the set of parentheses
  1524. 1:14:53type system.in
  1525. 1:14:56then when we open a scanner it is a good
  1526. 1:14:58idea to close it when we're done with
  1527. 1:15:00it just so we don't leave any resources
  1528. 1:15:04open with our variable of age we're
  1529. 1:15:07going to create a prompt to have a user
  1530. 1:15:08enter in their age I'm going to declare
  1531. 1:15:11a variable of age but we'll assign it
  1532. 1:15:13later let's create a
  1533. 1:15:16prompt enter your
  1534. 1:15:18age I'll use print instead of print
  1535. 1:15:22line and then we will assign age equal
  1536. 1:15:26to scanner. next
  1537. 1:15:30int okay let's try this
  1538. 1:15:33again and to your age let's say that I'm
  1539. 1:15:3721 you are an
  1540. 1:15:40adult what if I was 99 you are a
  1541. 1:15:46senior I am negative 1 years old you
  1542. 1:15:49haven't been born
  1543. 1:15:52yet I am zero exactly you are a
  1544. 1:15:56baby or maybe I'm 10 you are a
  1545. 1:16:01child so with if statements we check a
  1546. 1:16:04condition if it's true we do it we do
  1547. 1:16:07this code if not we don't otherwise we
  1548. 1:16:10can check another condition with else if
  1549. 1:16:13and if none of these conditions are true
  1550. 1:16:15we can perform whatever is within an
  1551. 1:16:17else statement else acts like the
  1552. 1:16:19default code if none of the above
  1553. 1:16:21conditions are true now this time we're
  1554. 1:16:23going to work with strings
  1555. 1:16:26we're going to create a variable of name
  1556. 1:16:28and it's going to be a string we'll
  1557. 1:16:30declare it but not yet assign
  1558. 1:16:33it we will prompt the user to enter in
  1559. 1:16:36their name I will use print instead of
  1560. 1:16:38print
  1561. 1:16:39Line enter your
  1562. 1:16:43name we will assign our variable of name
  1563. 1:16:47equal to scanner. next line to get a
  1564. 1:16:52line of text
  1565. 1:16:55this group of if else if and else
  1566. 1:16:58statements let's set this to be group
  1567. 1:17:01two we're going to create another
  1568. 1:17:05set this will be group one for the
  1569. 1:17:09name when somebody enters in their name
  1570. 1:17:12they could skip the prompt like this
  1571. 1:17:15enter your name I'm just going to hit
  1572. 1:17:17enter to skip
  1573. 1:17:19it we will check if our name variable is
  1574. 1:17:22empty here's how
  1575. 1:17:26if we'll check if our name is empty
  1576. 1:17:29there's a string method for this we
  1577. 1:17:30haven't discussed string Methods yet
  1578. 1:17:33instead of saying name is equal to an
  1579. 1:17:35empty space there is a built-in method
  1580. 1:17:38of strings and that is to take our
  1581. 1:17:39variable name dot is
  1582. 1:17:44empty this is another way to check to
  1583. 1:17:46see if a string is empty this will be
  1584. 1:17:48our condition is empty will give you a
  1585. 1:17:51Boolean of true or false
  1586. 1:17:55so let's print the following if our name
  1587. 1:17:57variable is
  1588. 1:17:58empty let's say you didn't enter your
  1589. 1:18:05name else if our name is not empty that
  1590. 1:18:09means somebody typed in their name let's
  1591. 1:18:11say hello plus name and I'll add an
  1592. 1:18:16exclamation
  1593. 1:18:18point okay let's try this
  1594. 1:18:22again enter your name if I hit enter
  1595. 1:18:25we'll perform then whatever is within
  1596. 1:18:26the if statement then enter an age you
  1597. 1:18:30didn't enter your name you are an adult
  1598. 1:18:33this time let's type in our name our
  1599. 1:18:34first
  1600. 1:18:37name hello whatever your name is you are
  1601. 1:18:41an
  1602. 1:18:41adult this time we have two groups of if
  1603. 1:18:44else statements group one is for the
  1604. 1:18:47name group two is for the user's age
  1605. 1:18:50this time let's create a
  1606. 1:18:52Boolean Boolean is
  1607. 1:18:57student then we'll create a
  1608. 1:19:00prompt let's ask are you a student but
  1609. 1:19:04we need a value of true or
  1610. 1:19:08false cuz booleans can only be true or
  1611. 1:19:11false
  1612. 1:19:13normally is student equals use our
  1613. 1:19:16scanner to get some user input next
  1614. 1:19:22Boolean now we'll create another group
  1615. 1:19:24of if and lse if
  1616. 1:19:26statements group
  1617. 1:19:29three we need an if
  1618. 1:19:34statement so with booleans we could say
  1619. 1:19:37is student is equal to true but there is
  1620. 1:19:40a way to simplify this a
  1621. 1:19:42shortcut you can just say is student it
  1622. 1:19:46holds a direct value of true or false we
  1623. 1:19:49don't necessarily need to write like a
  1624. 1:19:51whole condition
  1625. 1:19:54so if we're a student we'll
  1626. 1:19:57output you are a
  1627. 1:20:00student else if a student is
  1628. 1:20:04false then we'll output you are not a
  1629. 1:20:09student we have three groups of if else
  1630. 1:20:12statements group three is for is
  1631. 1:20:16student group two is for the
  1632. 1:20:20age group one is for the
  1633. 1:20:23name just to make this more fun I'm
  1634. 1:20:26going to add some emojis I think it
  1635. 1:20:28would make it more interesting but you
  1636. 1:20:30don't have to so if somebody doesn't
  1637. 1:20:32enter their name I'm going to add a
  1638. 1:20:34pouting
  1639. 1:20:35face if somebody enters their name I
  1640. 1:20:38will add a smiley
  1641. 1:20:40face if somebody is a senior maybe we
  1642. 1:20:44can find an emoji for a
  1643. 1:20:48grandpa let's find one for an
  1644. 1:20:52adult you haven't been born yet uh we
  1645. 1:20:55could do like a spirit or
  1646. 1:21:00something you are a
  1647. 1:21:04baby you are a
  1648. 1:21:10child you are a student we could do a
  1649. 1:21:17school you are not a student we could do
  1650. 1:21:20an
  1651. 1:21:21office all right so this is our final
  1652. 1:21:24program
  1653. 1:21:26again we have three groups of if
  1654. 1:21:27statements this time one for the name
  1655. 1:21:30one for the age and one for is student
  1656. 1:21:34and then for the prompt for are you a
  1657. 1:21:35student I'm going to use print instead
  1658. 1:21:37of print
  1659. 1:21:39line here's the result enter your name
  1660. 1:21:43enter your age are you a
  1661. 1:21:49student hello your name you are an adult
  1662. 1:21:52you are not a student
  1663. 1:21:55let's try some of the other statements
  1664. 1:21:56I'm not going to enter a name for the
  1665. 1:21:59age let's say that I'm
  1666. 1:22:01ne1 are you a student
  1667. 1:22:04true you didn't enter your name you
  1668. 1:22:07haven't been born yet you are a
  1669. 1:22:09student so those are if statements
  1670. 1:22:12everybody they perform a block of code
  1671. 1:22:15if its condition is true check a
  1672. 1:22:18condition if it's true do this if not
  1673. 1:22:21you can do something else and well
  1674. 1:22:23everybody that summar mizes if
  1675. 1:22:25statements in
  1676. 1:22:27Java hey everybody in this video I'm
  1677. 1:22:30going to show you how we can work with
  1678. 1:22:31the random class and generate some
  1679. 1:22:33random numbers let's begin to work with
  1680. 1:22:35random numbers we'll need to import the
  1681. 1:22:37random class at the top of our Java file
  1682. 1:22:40we will import java. u.
  1683. 1:22:45random much like what we do with the
  1684. 1:22:47scanner class we're going to follow a
  1685. 1:22:49similar pattern for creating a random
  1686. 1:22:51object much like a scanner object we
  1687. 1:22:54will type the name of the class random
  1688. 1:22:57the name of the random object random
  1689. 1:23:00equals new
  1690. 1:23:02random so random random equals new
  1691. 1:23:07random let's say we have an integer an
  1692. 1:23:10integer variable of number I'm going to
  1693. 1:23:13assign our number variable equal to and
  1694. 1:23:17here's where we'll generate a random
  1695. 1:23:18number we will take our random object
  1696. 1:23:21then we will use the next int method
  1697. 1:23:25this will generate a random number
  1698. 1:23:27however there's one issue with this I'm
  1699. 1:23:30going to print our
  1700. 1:23:32number the number is going to be very
  1701. 1:23:36large so the random number that's
  1702. 1:23:39generated it's going to be within a
  1703. 1:23:41range between just about -2 billion and
  1704. 1:23:44positive2 billion I'm guessing you don't
  1705. 1:23:46want a number that big So within the
  1706. 1:23:49next integer method we can set some
  1707. 1:23:52bounds if I would like a random number
  1708. 1:23:54between 1 and six like I'm rolling a
  1709. 1:23:56dice or die I would say 1 comma 6 this
  1710. 1:24:01is actually going to give us a random
  1711. 1:24:02number between 1 and
  1712. 1:24:07five the first number is inclusive the
  1713. 1:24:11second number is exclusive no matter how
  1714. 1:24:14many times I rerolled this die I'm never
  1715. 1:24:16going to get
  1716. 1:24:20six since this number is exclusive I'm
  1717. 1:24:23going to inrease this to be seven that
  1718. 1:24:26will give us 1 through
  1719. 1:24:32six and there's six let's say this time
  1720. 1:24:35you would like three random numbers
  1721. 1:24:37you're rolling three dice let's say we
  1722. 1:24:39have number one number two and number
  1723. 1:24:43three all
  1724. 1:24:45integers we'll assign number one with a
  1725. 1:24:48random integer and we'll do this with
  1726. 1:24:50numbers two and three as well number one
  1727. 1:24:53number two number number
  1728. 1:24:55three then we will print them number one
  1729. 1:24:59number two number
  1730. 1:25:02three 2
  1731. 1:25:0546 23 4
  1732. 1:25:08225 good enough for random numbers
  1733. 1:25:11between 1 and 100 the second number is
  1734. 1:25:13going to be 101 because it's
  1735. 1:25:17exclusive so now we're generating three
  1736. 1:25:20random numbers between 1 and 100
  1737. 1:25:30not only can you generate random numbers
  1738. 1:25:32you can Generate random
  1739. 1:25:37doubles so let's set this back to be
  1740. 1:25:42number let's change the data type and
  1741. 1:25:44number to be
  1742. 1:25:49double we're going to use the next
  1743. 1:25:52Double method
  1744. 1:25:55this is going to generate a random
  1745. 1:25:57number between zero and
  1746. 1:25:59one just the decimal portion
  1747. 1:26:08really you can also generate a Boolean
  1748. 1:26:11this might be good in a situation where
  1749. 1:26:13you need to flip a coin let's say we
  1750. 1:26:16have a Boolean of is
  1751. 1:26:19heads when we flip a coin is it heads
  1752. 1:26:25random. nextext
  1753. 1:26:28Boolean then we will display if the coin
  1754. 1:26:31we flip is heads or not is it heads or
  1755. 1:26:34it's Tails that's true it is
  1756. 1:26:37heads now it's false it's
  1757. 1:26:40Tails we could even use an if
  1758. 1:26:44statement if is heads if our coin flip
  1759. 1:26:48is on
  1760. 1:26:49heads let's
  1761. 1:26:51print heads
  1762. 1:26:54else we will
  1763. 1:26:57print
  1764. 1:27:00Tails so this will simulate us flipping
  1765. 1:27:03a
  1766. 1:27:09coin we're getting a lot of
  1767. 1:27:18Tails all right everybody that is how
  1768. 1:27:20you can Generate random numbers and use
  1769. 1:27:22the random module in Java
  1770. 1:27:28hey everybody in this video we're going
  1771. 1:27:30to cover some useful math related
  1772. 1:27:32constants and methods that you may be
  1773. 1:27:34interested in as a beginner then at the
  1774. 1:27:35end of this topic we'll cover a few
  1775. 1:27:37exercises let's begin if at any time you
  1776. 1:27:40need the value of pi there's a built-in
  1777. 1:27:42constant of the math class so I'm going
  1778. 1:27:45to print the
  1779. 1:27:46following math with a capital M Pi all
  1780. 1:27:51capital so that would give you the value
  1781. 1:27:553.14 and some change so instead of
  1782. 1:27:58creating a variable for pi it's always
  1783. 1:28:00available to you within the math class
  1784. 1:28:02you just have to access it there's also
  1785. 1:28:07E math. capital
  1786. 1:28:12E this is the exponential constant also
  1787. 1:28:15known as ul's number you may never use
  1788. 1:28:18this but it's available to you in case
  1789. 1:28:20you do so again access the math class
  1790. 1:28:23Dot to name the
  1791. 1:28:25constant this time let's create a double
  1792. 1:28:28variable named result here's a few
  1793. 1:28:30methods I'm going to reassign result
  1794. 1:28:33equal to access math dot to raise a base
  1795. 1:28:38to a given power you can use the pal
  1796. 1:28:41method let's raise 2 to the^ of 3 then
  1797. 1:28:45print
  1798. 1:28:46it let's print our variable result 2 ^
  1799. 1:28:51of 3 would be 8
  1800. 1:28:55and again I'm using a double that's why
  1801. 1:28:57we have the decimal portion 2^ of 4
  1802. 1:29:00would
  1803. 1:29:01be 16 2 the^ of
  1804. 1:29:055 is
  1805. 1:29:0732 po meaning power you can raise a base
  1806. 1:29:11to a given power these can be values or
  1807. 1:29:14variables there's the absolute value
  1808. 1:29:17method math.abs for absolute value
  1809. 1:29:22what's the absolute value
  1810. 1:29:27of5 that would be five positive 5
  1811. 1:29:30basically speaking the absolute value
  1812. 1:29:32method gives you that number but
  1813. 1:29:34positive if it's negative think of it as
  1814. 1:29:36the distance away from zero if that
  1815. 1:29:39helps for square root there's a square
  1816. 1:29:42root method result equals
  1817. 1:29:45math.sqrt
  1818. 1:29:46what is the square root of 9 I did make
  1819. 1:29:50a spelling mistake here if you're
  1820. 1:29:52accessing the math class make sure it's
  1821. 1:29:54with a capital m so the square root of 9
  1822. 1:29:58would be
  1823. 1:30:023 we can also round a
  1824. 1:30:05number again access the math class use
  1825. 1:30:08the round
  1826. 1:30:12method let's round
  1827. 1:30:143.14 round will round a number to the
  1828. 1:30:17nearest whole
  1829. 1:30:19integer again we're working with the
  1830. 1:30:21double though so that gives us
  1831. 1:30:263.0 to always round up there's the
  1832. 1:30:30ceiling
  1833. 1:30:32method math. c
  1834. 1:30:35l again I accidentally used lowercase M
  1835. 1:30:38it's capital M let's round 3.14 up to
  1836. 1:30:42the
  1837. 1:30:43ceiling so 3.14 rounded up is four to
  1838. 1:30:49always round down you can use the floor
  1839. 1:30:51method
  1840. 1:30:54math.
  1841. 1:30:56floor so this time let's round 3.99 down
  1842. 1:31:01normally it should round up
  1843. 1:31:03right so 3.99 rounded down is
  1844. 1:31:07three you can find the max between two
  1845. 1:31:10values or variables
  1846. 1:31:14math. Max method we'll have two numbers
  1847. 1:31:18which are comma separated 10 and 20 what
  1848. 1:31:21is the max between these two values or
  1849. 1:31:23variables
  1850. 1:31:25the greater number is 20 there's also
  1851. 1:31:29Min I'll just copy this cuz I'm lazy
  1852. 1:31:32replace Max with Min what is the minimum
  1853. 1:31:35value between these two
  1854. 1:31:37values that would be
  1855. 1:31:3910 so those are a few useful math
  1856. 1:31:42methods and constants that you may be
  1857. 1:31:44interested in we're going to cover some
  1858. 1:31:46math related exercises if you would like
  1859. 1:31:48additional practice
  1860. 1:31:54in this first exercise we're going to
  1861. 1:31:56find the hypotenuse of a right triangle
  1862. 1:31:59you can follow this formula C the
  1863. 1:32:02hypotenuse equals the square root of a^
  1864. 1:32:052 one of the sides plus b^2 the other
  1865. 1:32:08side we're going to accept some user
  1866. 1:32:11input we'll need a scanner we need to
  1867. 1:32:14import the scanner class import
  1868. 1:32:17java.util do
  1869. 1:32:20scanner then we'll create a scanner
  1870. 1:32:22object scan scanner scanner equals new
  1871. 1:32:28scanner then place system.in within the
  1872. 1:32:31set of
  1873. 1:32:32parentheses then when we open a scanner
  1874. 1:32:34it is a good idea to close it so we
  1875. 1:32:36don't leave any resources open scanner.
  1876. 1:32:39close we'll prompt the user to enter in
  1877. 1:32:42the length of side A and B let's declare
  1878. 1:32:45those as variables double a double b
  1879. 1:32:50we'll also declare Double C that will
  1880. 1:32:52store the hypotenuse
  1881. 1:32:54Double
  1882. 1:32:56C we'll need two
  1883. 1:32:58prompts I will use print instead of
  1884. 1:33:00print
  1885. 1:33:01Line enter the length of side
  1886. 1:33:09a with inside a within our variable of a
  1887. 1:33:14we'll assign it to scanner. next
  1888. 1:33:19Double let's do this with side B we can
  1889. 1:33:22copy these two lines paste them replace
  1890. 1:33:25a with
  1891. 1:33:28B that is lowercase
  1892. 1:33:31b let's do a test run just to be sure
  1893. 1:33:33that these two work
  1894. 1:33:36temporarily I'm going to print a then
  1895. 1:33:44B enter the length as side a 3 B will be
  1896. 1:33:48four these work I just wanted to test
  1897. 1:33:52that it is a good idea to test your code
  1898. 1:33:54as you're writing it then we have to
  1899. 1:33:57follow this
  1900. 1:33:59equation C equals math. square root of
  1901. 1:34:03a^2 +
  1902. 1:34:05b^2 there is a square root method within
  1903. 1:34:08the math class but we have to raise side
  1904. 1:34:11a to the power of two plus b to the^ of
  1905. 1:34:14two here's how we can do that so within
  1906. 1:34:17the square root method we're going to
  1907. 1:34:19raise a to the power of two first math
  1908. 1:34:24po to raise a base to a given power
  1909. 1:34:27raise a comma to the power of
  1910. 1:34:31two plus again math.pow to raise a base
  1911. 1:34:36to a given power B to the power of
  1912. 1:34:40two and that's it so let's display
  1913. 1:34:43whatever C is the
  1914. 1:34:47hypotenuse
  1915. 1:34:49the
  1916. 1:34:52hypotenuse I'll write that this is C
  1917. 1:34:55side
  1918. 1:34:58C is the
  1919. 1:35:02following plus
  1920. 1:35:07C if side a is three side B is four side
  1921. 1:35:12C should be five which it is if you
  1922. 1:35:15would prefer we can add a unit of
  1923. 1:35:17measurement such as centimet or inches
  1924. 1:35:20so after side C add cm for centimet or I
  1925. 1:35:25for inches whatever you
  1926. 1:35:28prefer so let's try that again side a
  1927. 1:35:31will be four side B will be
  1928. 1:35:34five and side C is this really long
  1929. 1:35:37number we haven't talked about the print
  1930. 1:35:39F statement yet using print F you can
  1931. 1:35:41display a given amount of digits but we
  1932. 1:35:43do have centimeters at the end which is
  1933. 1:35:44what we were looking for so let's try
  1934. 1:35:47three and four and that gives us 5
  1935. 1:35:51cm all right let's cover another
  1936. 1:35:55exercise given the radius of a circle or
  1937. 1:35:58a sphere we'll return the circumference
  1938. 1:36:01the area then the volume that's if we're
  1939. 1:36:04working with the sphere given the radius
  1940. 1:36:07so be sure that we're importing our
  1941. 1:36:08scanner at the top and we'll create a
  1942. 1:36:10scanner object scanner scanner equals
  1943. 1:36:14new
  1944. 1:36:17scanner system.in then be sure to close
  1945. 1:36:20your
  1946. 1:36:22scanner scanner .
  1947. 1:36:25close we'll prompt the user for a
  1948. 1:36:28radius because we need a radius to work
  1949. 1:36:31with we'll create a variable it will be
  1950. 1:36:33a double named radius we'll declare it
  1951. 1:36:36but not yet assign
  1952. 1:36:37it we'll prompt the user using print
  1953. 1:36:40instead of print
  1954. 1:36:42Line enter the
  1955. 1:36:45radius then we will assign our radius
  1956. 1:36:48variable radius
  1957. 1:36:50equals scanner.
  1958. 1:36:53next
  1959. 1:36:56Double once we have a radius we have to
  1960. 1:36:58follow these formulas so we need a
  1961. 1:37:00circumference area and volume let's
  1962. 1:37:03declare those at the top of our program
  1963. 1:37:05but assign them later double
  1964. 1:37:09circumference I think I spelled that
  1965. 1:37:13right double
  1966. 1:37:15area double
  1967. 1:37:18volume we will calculate the
  1968. 1:37:20circumference
  1969. 1:37:24here's the
  1970. 1:37:25Formula 2 times we need pi to work with
  1971. 1:37:30instead of typing
  1972. 1:37:333.14159 so on and so forth we can access
  1973. 1:37:36that from the math class
  1974. 1:37:38math.pi Pi is always available to
  1975. 1:37:42us then multiply this by the
  1976. 1:37:46radius let's perform a test
  1977. 1:37:49run I will display the circumference
  1978. 1:37:54the
  1979. 1:37:55circumference
  1980. 1:37:57is plus our variable of
  1981. 1:38:00circumference then I'll add a unit of
  1982. 1:38:02measurement afterwards centimeters is
  1983. 1:38:04good or whatever you
  1984. 1:38:07prefer enter the radius I'll enter in
  1985. 1:38:10five the circumference is 31.4 CM in the
  1986. 1:38:14future using print F we can limit the
  1987. 1:38:17amount of digits that are displayed I'll
  1988. 1:38:19show you how to do that later on in this
  1989. 1:38:21video then we need the
  1990. 1:38:25area area
  1991. 1:38:27equals here's the formula again we need
  1992. 1:38:31Pi Pi is always available to us math. pi
  1993. 1:38:35times our radius to the^ of two so we
  1994. 1:38:39need the power method of math
  1995. 1:38:42math.pow raise the radius to the power
  1996. 1:38:45of
  1997. 1:38:47two and I'll just copy this line because
  1998. 1:38:49I'm
  1999. 1:38:51lazy the area is our variable area now
  2000. 1:38:56when working with an area the unit of
  2001. 1:38:59measurement is going to be squared so we
  2002. 1:39:01can raise it to the power of two or you
  2003. 1:39:04can use superscript to if you're on
  2004. 1:39:06Windows make sure numlock is on hold alt
  2005. 1:39:10then on the numpad type
  2006. 1:39:120178 for a superscript of two if you
  2007. 1:39:15would like to use that
  2008. 1:39:16instead so now what is the
  2009. 1:39:20area enter the radius 5
  2010. 1:39:23the area is 78.53982
  2011. 1:39:57doubles times math. pi and again make
  2012. 1:40:01sure the m is
  2013. 1:40:04capital times our radius to the^ of
  2014. 1:40:103 math. power method raise our radius to
  2015. 1:40:15the power of
  2016. 1:40:19three then display the volume the volume
  2017. 1:40:24is our variable volume cm
  2018. 1:40:28cubed for a superscript of three make
  2019. 1:40:31sure numb lock is on hold alt then type
  2020. 1:40:350179 that's if you're on
  2021. 1:40:37Windows all right let's print the
  2022. 1:40:41volume again my radius is
  2023. 1:40:44five then we have the
  2024. 1:40:47circumference the area and the
  2025. 1:40:50volume and the unit of measurement is
  2026. 1:40:52cenm cubed now we haven't talked about
  2027. 1:40:55the print F statement we could display
  2028. 1:40:58just a few digits here's
  2029. 1:41:00how again this is a future topic replace
  2030. 1:41:04print line with print
  2031. 1:41:09F instead of adding plus our variable
  2032. 1:41:12name we're going to add a
  2033. 1:41:15placeholder
  2034. 1:41:17percent F to display a double but if you
  2035. 1:41:21would like one digit between the percent
  2036. 1:41:22and F type .1 so let's do this with area
  2037. 1:41:27and
  2038. 1:41:28volume replace the plus with a
  2039. 1:41:34comma then instead of adding plus centim
  2040. 1:41:37after our
  2041. 1:41:38placeholder add
  2042. 1:41:40CM then cm
  2043. 1:41:43squar and cm
  2044. 1:41:46cubed then we can get rid of the portion
  2045. 1:41:48where we add the centimeters to the end
  2046. 1:41:57so let's say the radius is five then we
  2047. 1:42:00should add a new line character to the
  2048. 1:42:01end of each of
  2049. 1:42:12these that looks much
  2050. 1:42:14better we're only displaying one digit
  2051. 1:42:17after the decimal again don't worry
  2052. 1:42:19about this this is a future topic the
  2053. 1:42:21print F statement but if you would only
  2054. 1:42:23like to display one digit that's how you
  2055. 1:42:25can do that again we'll cover this later
  2056. 1:42:29all right everybody so those are a few
  2057. 1:42:30math related constants and methods that
  2058. 1:42:33you may be interested in as a
  2059. 1:42:37beginner what's up everybody in this
  2060. 1:42:39video I'm going to discuss the print F
  2061. 1:42:41statement in Java print f is a method
  2062. 1:42:44used to format output we already do have
  2063. 1:42:46a little bit of experience using print F
  2064. 1:42:49in the last few topics involving math
  2065. 1:42:51we'll get more in depth with the print F
  2066. 1:42:52stat in this topic print f is an
  2067. 1:42:55alternative to print and print line
  2068. 1:42:57wherever we would like to insert a
  2069. 1:42:59variable we can add a placeholder of a
  2070. 1:43:01percent sign then add one of a few
  2071. 1:43:03characters following it to format the
  2072. 1:43:05output in this demonstration let's
  2073. 1:43:07create a few variables let's create a
  2074. 1:43:09string of name I'll assign some name
  2075. 1:43:13I'll pick SpongeBob because many people
  2076. 1:43:15know who SpongeBob is we'll need a
  2077. 1:43:18character let's say character first
  2078. 1:43:21letter equals
  2079. 1:43:24s int age equals
  2080. 1:43:2830 and a double double
  2081. 1:43:32height equals
  2082. 1:43:3660.5 and a Boolean Boolean is
  2083. 1:43:40employed I will set that to be true
  2084. 1:43:43SpongeBob is employed at the K crusty
  2085. 1:43:45Krab so we have some variables they're
  2086. 1:43:48all different data types we can insert
  2087. 1:43:50them into print F statements when we
  2088. 1:43:52output string literal here's a
  2089. 1:43:57demonstration instead of using print
  2090. 1:43:59line I'm going to use print
  2091. 1:44:02F we will display the
  2092. 1:44:05following
  2093. 1:44:06hello then I would like to insert this
  2094. 1:44:08variable of name we're going to use a
  2095. 1:44:10percent sign as a
  2096. 1:44:13placeholder percent then we need a
  2097. 1:44:15specifier our character what is the data
  2098. 1:44:18type of the variable we're trying to
  2099. 1:44:20insert if you're trying to insert a
  2100. 1:44:22string you're going to use a lowercase
  2101. 1:44:25s so after this word of hello we're
  2102. 1:44:28adding a placeholder then inserting a
  2103. 1:44:30string at this location so instead of
  2104. 1:44:32adding plus the variable
  2105. 1:44:36name we're going to comma separate any
  2106. 1:44:38variables we're inserting so comma name
  2107. 1:44:42here's the
  2108. 1:44:44output hello our variable name which
  2109. 1:44:47contains
  2110. 1:44:49SpongeBob if we need to insert a
  2111. 1:44:51character that would be lower case C so
  2112. 1:44:55let's use
  2113. 1:44:56system.out.print
  2114. 1:44:59F I don't know let's say your name
  2115. 1:45:03starts with a we're going to insert a
  2116. 1:45:06variable we need a placeholder that
  2117. 1:45:08perent sign lower case C for a Char
  2118. 1:45:13variable then comma separate any
  2119. 1:45:16variables comma our variable of first
  2120. 1:45:21letter so here's an important thing too
  2121. 1:45:24with a print F statement we have to
  2122. 1:45:26manually insert any new line characters
  2123. 1:45:28we'll end each print F statement with a
  2124. 1:45:30new line
  2125. 1:45:31character so after our format specifier
  2126. 1:45:35We'll add back sln to insert a new
  2127. 1:45:41line that's better otherwise the output
  2128. 1:45:43is going to be all on the same
  2129. 1:45:45line so that's how to insert a character
  2130. 1:45:48percent C for
  2131. 1:45:50chars then for an integer we use
  2132. 1:45:55D so print
  2133. 1:45:59f u r we're going to insert a variable
  2134. 1:46:04we use D for
  2135. 1:46:07integers you are our variable age years
  2136. 1:46:12old than a new line character then we
  2137. 1:46:15will insert our variable
  2138. 1:46:20age you are 30 years old
  2139. 1:46:24and in the middle is our variable of
  2140. 1:46:27age for a double that would be
  2141. 1:46:32f
  2142. 1:46:34system.out.print
  2143. 1:46:38f you are add a placeholder f for
  2144. 1:46:43floating Point number think of that as a
  2145. 1:46:47double inches tall or you can pick
  2146. 1:46:50centimeters up to you
  2147. 1:46:53then we will insert our variable of
  2148. 1:46:59height you are
  2149. 1:47:0160.5 then a bunch of zeros inches tall
  2150. 1:47:05we can limit the amount of digits that
  2151. 1:47:07are displayed after the decimal we'll
  2152. 1:47:09have to set a Precision but I'll explain
  2153. 1:47:11that
  2154. 1:47:13later then a Boolean all we need is the
  2155. 1:47:16letter
  2156. 1:47:17b
  2157. 1:47:19system.out.print
  2158. 1:47:21f let's say
  2159. 1:47:24employed add a
  2160. 1:47:26placeholder B then a new line
  2161. 1:47:31character we will display our variable
  2162. 1:47:34of is
  2163. 1:47:37employed employed then our variable of
  2164. 1:47:40true now you can insert multiple
  2165. 1:47:43variables within the same line here's
  2166. 1:47:46how we'll use
  2167. 1:47:48system.out.print
  2168. 1:47:50f we're going to inst insert our
  2169. 1:47:53variable of name
  2170. 1:47:55SpongeBob for a string the specifier
  2171. 1:47:58character is going to be lowercase s for
  2172. 1:48:00a string so string SpongeBob
  2173. 1:48:04is we're inserting an integer we need
  2174. 1:48:07percent
  2175. 1:48:08D years old we'll comma separate all
  2176. 1:48:13variables we're inserting we have a
  2177. 1:48:15string followed by an integer so that
  2178. 1:48:18would be name comma age we're inserting
  2179. 1:48:21two variables of this time SpongeBob is
  2180. 1:48:2530 years
  2181. 1:48:27old so those are specifier characters
  2182. 1:48:31add a percent sign a placeholder then
  2183. 1:48:33follow this placeholder with a specifier
  2184. 1:48:35character depending on what the data
  2185. 1:48:37type is s for Strings C for
  2186. 1:48:41chars D for integers f for doubles or
  2187. 1:48:45other floating Point numbers and B for
  2188. 1:48:48booleans it might take a little bit of
  2189. 1:48:50time to remember
  2190. 1:48:51these now we'll cover Precision with
  2191. 1:48:54setting the Precision we can limit the
  2192. 1:48:57amount of digits that display after a
  2193. 1:48:59decimal let's create three different
  2194. 1:49:01prices and these will be of the double
  2195. 1:49:03data type price 1 equals
  2196. 1:49:08$9.99 price
  2197. 1:49:10two I'm just making up numbers here
  2198. 1:49:1510.15 and double price 3
  2199. 1:49:21equals 5
  2200. 1:49:24$41 we're going to use print F to
  2201. 1:49:27display these
  2202. 1:49:29prices
  2203. 1:49:31system.out.print F we're going to insert
  2204. 1:49:34we'll use that percent placeholder then
  2205. 1:49:36use f to display a double f for floating
  2206. 1:49:39Point
  2207. 1:49:41number comma our variable of price one
  2208. 1:49:45then we'll do this with price two and
  2209. 1:49:46price
  2210. 1:49:47three price one price two price three
  2211. 1:49:53and here are the prices oh and we have
  2212. 1:49:55to add a new line character too because
  2213. 1:49:57they're all in the same line I forgot to
  2214. 1:50:00do that so back sln back
  2215. 1:50:03sln back
  2216. 1:50:06sln that's better naturally when we're
  2217. 1:50:10using print F to display a floating
  2218. 1:50:12Point number such as a double normally
  2219. 1:50:14it displays six digits after the decimal
  2220. 1:50:17point but we would like to limit that
  2221. 1:50:20here's how we're going to set the
  2222. 1:50:22Precision
  2223. 1:50:24to display one digit after the decimal
  2224. 1:50:26between the percent sign and the F Type
  2225. 1:50:30.1 to display one
  2226. 1:50:36digit so we have
  2227. 1:50:3910.0 this will automatically round the
  2228. 1:50:41output so we have 10.0 100.2 and
  2229. 1:50:4654.0 for two digits that would be 02
  2230. 1:50:54999
  2231. 1:50:5610.15 and
  2232. 1:50:5954.1 or we could do
  2233. 1:51:02three
  2234. 1:51:053F then we have three digits after the
  2235. 1:51:10decimal so that is precision it's useful
  2236. 1:51:13for displaying prices then we have Flags
  2237. 1:51:16next let's set the Precision to be 0.2
  2238. 1:51:22here's different flags that we can add
  2239. 1:51:24by adding a plus after the percent sign
  2240. 1:51:27we can output a plus for any positive
  2241. 1:51:37numbers positive 999 positive 10.15
  2242. 1:51:4354.1 is still
  2243. 1:51:46negative to display a plus before any
  2244. 1:51:49positive numbers just add a plus sign
  2245. 1:51:53let's increase these
  2246. 1:51:55prices let's add three zeros to the end
  2247. 1:51:58of
  2248. 1:52:04each if you add the flag of a comma it's
  2249. 1:52:07a comma grouping separator for any
  2250. 1:52:09thousand's place after the percent sign
  2251. 1:52:12add a
  2252. 1:52:13comma at any thousand's place we have a
  2253. 1:52:17comma grouping separator again this is
  2254. 1:52:20useful for displaying prices
  2255. 1:52:24by adding a single left parentheses any
  2256. 1:52:27negative numbers are enclosed in a set
  2257. 1:52:29of parentheses so let's revert these
  2258. 1:52:31prices
  2259. 1:52:33back after the percent sign add a left
  2260. 1:52:39parentheses so we only have one negative
  2261. 1:52:42price-
  2262. 1:52:4754.1 and then we have a space character
  2263. 1:52:50this will display a minus if negative
  2264. 1:52:52space space if positive we have two
  2265. 1:52:54positive numbers in one negative one so
  2266. 1:52:58by using a space
  2267. 1:53:03character any positive numbers are
  2268. 1:53:06preceded with a space 54.1 is negative
  2269. 1:53:09so we do not have that space it's good
  2270. 1:53:11for aligning
  2271. 1:53:13numbers the last specifier we have to
  2272. 1:53:15talk about is
  2273. 1:53:17width we're going to create some ID
  2274. 1:53:19numbers these will be integers int id1 =
  2275. 1:53:261 int id2 =
  2276. 1:53:3123 in
  2277. 1:53:33ID3 equal
  2278. 1:53:37456 and int id4 equals
  2279. 1:53:457890 these integers have a varying
  2280. 1:53:48number of digits 1 2 3 4 by setting the
  2281. 1:53:53width we can align them here's how again
  2282. 1:53:56we're going to use print F we're
  2283. 1:53:59displaying an integer so we need percent
  2284. 1:54:01D we will Begin by displaying
  2285. 1:54:05id1 then let's do this with 2 three and
  2286. 1:54:07four as
  2287. 1:54:09well 2 3
  2288. 1:54:144 and again I am missing that new line
  2289. 1:54:17character new line new line
  2290. 1:54:22new
  2291. 1:54:23line new
  2292. 1:54:26line that's better so we have our four
  2293. 1:54:31IDs by adding a zero after the percent
  2294. 1:54:34sign we can zero pad some of these
  2295. 1:54:42values but Java is giving us a warning
  2296. 1:54:45by how many characters do we want to pad
  2297. 1:54:47these numbers with
  2298. 1:54:49zero let's say four we have one variable
  2299. 1:54:53that has four digits we'll set it to be
  2300. 1:54:55four to be the maximum so
  2301. 1:54:5904 and here we are these ID numbers are
  2302. 1:55:02all now zero
  2303. 1:55:04padded and they align vertically which
  2304. 1:55:07is kind of
  2305. 1:55:09nice so to add some zero padding follow
  2306. 1:55:12our placeholder with zero then the
  2307. 1:55:15amount of digits to Zer pad in this case
  2308. 1:55:18four by setting a positive number we can
  2309. 1:55:21WR justify these variables
  2310. 1:55:23so let's replace zero with just
  2311. 1:55:27four rather than having that zero
  2312. 1:55:29padding we just have some spaces if you
  2313. 1:55:31would prefer it that
  2314. 1:55:33way or with a negative number we can
  2315. 1:55:36left justify these
  2316. 1:55:38values so let's set four to be instead
  2317. 1:55:42-4 now these values are left Justified
  2318. 1:55:45and we still have those spaces
  2319. 1:55:47afterwards except for the last variable
  2320. 1:55:49because it's already four digits
  2321. 1:55:52all right everybody so that is the print
  2322. 1:55:54F statement it's a method used to format
  2323. 1:55:56output you add a placeholder then one of
  2324. 1:55:59a few characters afterwards there is
  2325. 1:56:01Flags width precision and specifier
  2326. 1:56:03characters depending on the data type of
  2327. 1:56:05what you want to display we'll have more
  2328. 1:56:07practice with this in the future and
  2329. 1:56:09well everybody that is print F in
  2330. 1:56:13Java hey everybody this video serves as
  2331. 1:56:16a project we're going to create a
  2332. 1:56:17compound interest calculator using Java
  2333. 1:56:20if you put money in a bank account or
  2334. 1:56:22the stock market using the given formula
  2335. 1:56:24we can determine what our final amount
  2336. 1:56:25is going to be plus interest after a
  2337. 1:56:27given amount of time this project serves
  2338. 1:56:29as an exercise using Java let's begin
  2339. 1:56:32all right let's get started everybody
  2340. 1:56:34the first thing we'll need is a scanner
  2341. 1:56:35we'll need to import it at the top of
  2342. 1:56:37our Java file import input
  2343. 1:56:41import
  2344. 1:56:44java.util.scanner
  2345. 1:56:48we need to create a scanner object again
  2346. 1:56:51scanner scanner equals new
  2347. 1:56:54scanner then within the set of
  2348. 1:56:56parentheses type
  2349. 1:56:58system.in when you open a scanner it is
  2350. 1:57:01good practice to close the scanner when
  2351. 1:57:03you're done with it so you don't leave
  2352. 1:57:04any resources
  2353. 1:57:06open after creating our scanner here the
  2354. 1:57:09different variables we'll need double
  2355. 1:57:13principal this will store the principal
  2356. 1:57:16amount what's our original investment an
  2357. 1:57:19interest rate double rate
  2358. 1:57:23an integer for times compounded how
  2359. 1:57:27often does our interest compound like
  2360. 1:57:30yearly quarterly
  2361. 1:57:32monthly then we need an amount of time
  2362. 1:57:34such as years into
  2363. 1:57:38years and the final amount
  2364. 1:57:42double
  2365. 1:57:44amount now we have to get some user
  2366. 1:57:46input we'll need some prompts first we
  2367. 1:57:49need the principal amount all use
  2368. 1:57:55system.out.print enter the principal I
  2369. 1:57:58never spell principal right enter the
  2370. 1:58:01principal
  2371. 1:58:05amount we are working with the double so
  2372. 1:58:08we need the next Double method of our
  2373. 1:58:10scanner principal equals our
  2374. 1:58:15scanner do next
  2375. 1:58:20Double then we need the interest rate
  2376. 1:58:23this is going to be a
  2377. 1:58:26double
  2378. 1:58:29system.out.print enter the interest
  2379. 1:58:34rate I'll add within our prompt that
  2380. 1:58:36this is in a percentage form like 5% or
  2381. 1:58:40so the number five instead of
  2382. 1:58:4405 so then we have our interest
  2383. 1:58:47rate so we need our scanner do next
  2384. 1:58:53double so let's say somebody types in
  2385. 1:58:56five for
  2386. 1:58:575% we would like the rate to be 05
  2387. 1:59:01rather than 5 after accepting the user
  2388. 1:59:03input let's divide that number by 100 or
  2389. 1:59:07let's say somebody types in seven we
  2390. 1:59:09don't want the rate to be seven we want
  2391. 1:59:10it to be
  2392. 1:59:1207 that's why we're dividing by
  2393. 1:59:15100 we'll fill in the times compounded
  2394. 1:59:19variable we're going to use print
  2395. 1:59:24enter the number of
  2396. 1:59:28times
  2397. 1:59:30compounded per
  2398. 1:59:32year so this could be yearly that would
  2399. 1:59:35be one quarterly four or monthly would
  2400. 1:59:38be 12 in most cases it's probably going
  2401. 1:59:40to be one for yearly times
  2402. 1:59:43compounded equals use our
  2403. 1:59:48scanner
  2404. 1:59:50next int cuz we're accepting an
  2405. 1:59:54integer then we need the amount of times
  2406. 1:59:56our money is going to be
  2407. 2:00:00invested enter the number of
  2408. 2:00:05years then we need to assign our
  2409. 2:00:08variable years years equals
  2410. 2:00:12scanner. next int because we're working
  2411. 2:00:15with an integer then we need to
  2412. 2:00:17calculate the total amount here's the
  2413. 2:00:19formula
  2414. 2:00:23amount
  2415. 2:00:24equals we need our principal
  2416. 2:00:29amount times access the math class use
  2417. 2:00:33the power
  2418. 2:00:35method raay is 1 plus the interest
  2419. 2:00:41rate divided by the variable times
  2420. 2:00:47compounded so this is going to be the
  2421. 2:00:50base we Comm separate the base and the
  2422. 2:00:53power we are raising this part of our
  2423. 2:00:56equation to the
  2424. 2:00:58power of times
  2425. 2:01:02compounded times the number of
  2426. 2:01:06years and that's our
  2427. 2:01:08formula it's a little confusing but it
  2428. 2:01:10should
  2429. 2:01:12work then we will display the
  2430. 2:01:15amount system.out.print
  2431. 2:01:19line the amount
  2432. 2:01:25after plus years
  2433. 2:01:30plus
  2434. 2:01:31is pick a unit of currency I'll pick
  2435. 2:01:34American
  2436. 2:01:36dollars plus
  2437. 2:01:39amount let's test
  2438. 2:01:42this so let's say we have $10,000 or so
  2439. 2:01:46and we're putting it in the
  2440. 2:01:48bank what is the interest rate
  2441. 2:01:52maybe
  2442. 2:01:533% so I'm going to type three enter the
  2443. 2:01:57amount of times compounded per year I'll
  2444. 2:01:59go with
  2445. 2:02:01one enter the number of years
  2446. 2:02:04one so we should have 10,300 in our
  2447. 2:02:09bank which we do
  2448. 2:02:1210,300 if you would like you can use a
  2449. 2:02:15print F statement to format the
  2450. 2:02:18output so if you're familiar with that
  2451. 2:02:20let's replace print line with print
  2452. 2:02:24F instead of concatenating these
  2453. 2:02:26variables we'll add some placeholders
  2454. 2:02:34instead so to display an integer we need
  2455. 2:02:37percent D within print
  2456. 2:02:40F
  2457. 2:02:42years is then to display a double we
  2458. 2:02:45need percent F then we'll list our
  2459. 2:02:48arguments
  2460. 2:02:50years comma
  2461. 2:02:55amount so to display two decimals within
  2462. 2:02:58a double between the percent and the F
  2463. 2:03:01we're going to type 2 to display two
  2464. 2:03:04digits after the
  2465. 2:03:05decimal we'll proceed this placeholder
  2466. 2:03:08with the unit of currency let's do
  2467. 2:03:09American dollars okay let's see the
  2468. 2:03:11total
  2469. 2:03:12amount enter the principal amount
  2470. 2:03:16$110,000 enter the interest rate 2%
  2471. 2:03:20enter the number of times compounded per
  2472. 2:03:21year this time let's do quarterly so
  2473. 2:03:24four because there's four quarters in a
  2474. 2:03:26year enter the number of years let's do
  2475. 2:03:29two
  2476. 2:03:31actually and here's the amount in our
  2477. 2:03:33bank account after 2 years
  2478. 2:03:39$1,477 all right everybody so that is a
  2479. 2:03:42compound interest calculator you can
  2480. 2:03:43write using
  2481. 2:03:46Java what is going on everybody in this
  2482. 2:03:49video I'm going to explain nested if
  2483. 2:03:50statements in Java
  2484. 2:03:52in Java it is possible to have if
  2485. 2:03:54statements within other if statements
  2486. 2:03:57kind of like
  2487. 2:03:58this after checking some condition you
  2488. 2:04:01could follow with another condition if
  2489. 2:04:03the first condition is true you can add
  2490. 2:04:06if
  2491. 2:04:07statements L if
  2492. 2:04:09statements and or else
  2493. 2:04:12statements really depends on the program
  2494. 2:04:14that you're trying to write so what
  2495. 2:04:15we're going to do in this demonstration
  2496. 2:04:17is have two Boolean
  2497. 2:04:19variables a Boolean of is
  2498. 2:04:23student let's say that we're selling
  2499. 2:04:25movie tickets if somebody's a student
  2500. 2:04:28they get a 10% discount on their movie
  2501. 2:04:30ticket we also have a senior discount
  2502. 2:04:33Boolean is
  2503. 2:04:36senior if somebody is 65 or older they
  2504. 2:04:39get a 20% senior
  2505. 2:04:41discount or if they're a student and a
  2506. 2:04:44senior they get a combined total of 30%
  2507. 2:04:47off their ticket
  2508. 2:04:49price let's say we have a double of
  2509. 2:04:53price the price of a movie ticket will
  2510. 2:04:55be $9.99 let's say that's actually kind
  2511. 2:04:59of cheap
  2512. 2:05:00nowadays we'll write an if statement to
  2513. 2:05:03check to see if somebody is a student if
  2514. 2:05:06is student is equal to true but since
  2515. 2:05:09this is a Boolean variable we don't need
  2516. 2:05:11the equals true portion we can just say
  2517. 2:05:14if is student I will go ahead and set
  2518. 2:05:17this to be true right away so if
  2519. 2:05:20somebody is a student they get a student
  2520. 2:05:22discount else they
  2521. 2:05:27won't so if a student is true let's
  2522. 2:05:30output the
  2523. 2:05:32following you get a
  2524. 2:05:36student discount of
  2525. 2:05:4010% then we will take our
  2526. 2:05:44price price equals price time
  2527. 2:05:490.9 that will give you a 10% discount
  2528. 2:05:51account you could shorten this using the
  2529. 2:05:54augmented assignment operator price
  2530. 2:05:56times equals
  2531. 2:05:580.9 that will set the price to be
  2532. 2:06:0090% or a 10% discount so to say if
  2533. 2:06:04somebody's not a student they're going
  2534. 2:06:05to pay full price price times equals 1
  2535. 2:06:10that really doesn't do anything but it
  2536. 2:06:11might help with this
  2537. 2:06:14visualization then we'll output the
  2538. 2:06:18following the price of a ticket
  2539. 2:06:24is pick unit of currency then I'll add
  2540. 2:06:27our price let's perform a test
  2541. 2:06:31run currently we're a student you get a
  2542. 2:06:35discount of 10% the price of a ticket is
  2543. 2:06:40$8.99 to limit the amount of digits that
  2544. 2:06:43display let's use a printf statement
  2545. 2:06:46we'll use print F instead of print line
  2546. 2:06:49after the dollar sign I will add a
  2547. 2:06:51format specifier we're going to display
  2548. 2:06:54a double we'll use f for a floating
  2549. 2:06:56Point
  2550. 2:06:57number comma price to display two digits
  2551. 2:07:01after the decimal use 0
  2552. 2:07:032 now we can display a
  2553. 2:07:06price you get a student discount of 10%
  2554. 2:07:09the price of a ticket is
  2555. 2:07:12$8.99 now if we weren't a student I'll
  2556. 2:07:15set this to be
  2557. 2:07:18false we don't get that discount the
  2558. 2:07:21price of a ticket is $9.99 what it was
  2559. 2:07:25originally now what if somebody is a
  2560. 2:07:27senior and a student within our if
  2561. 2:07:31statement we'll write a nested if
  2562. 2:07:34statement as well as an else
  2563. 2:07:37statement if is senior they get a
  2564. 2:07:40student discount and a senior
  2565. 2:07:43discount let's cut these two lines
  2566. 2:07:46within the if statement and stick them
  2567. 2:07:49within the nested else statement
  2568. 2:07:52if somebody is a senior and a
  2569. 2:07:55student you get a senior
  2570. 2:08:00discount of
  2571. 2:08:0420% and a student discount of
  2572. 2:08:0810% so let's take our original
  2573. 2:08:11price times equals
  2574. 2:08:180.7 and an additional 10% off the orig
  2575. 2:08:24so let's say is senior is true for
  2576. 2:08:28now okay else if they're not a student
  2577. 2:08:32but they are a
  2578. 2:08:35senior we'll check that with a nested if
  2579. 2:08:37statement within the else
  2580. 2:08:40statement if is senior they get that
  2581. 2:08:44senior
  2582. 2:08:46discount but it's only 20% instead of 30
  2583. 2:08:50because they're not also as
  2584. 2:08:53student price times equals
  2585. 2:08:580.8 you could add
  2586. 2:09:00else price times equals
  2587. 2:09:041 that's if somebody is not a student
  2588. 2:09:08and not a senior but you don't
  2589. 2:09:11necessarily need the sell
  2590. 2:09:12statement all right let's perform a test
  2591. 2:09:17run let's say that we're not a student
  2592. 2:09:19and not a senior
  2593. 2:09:24the price of a ticket is
  2594. 2:09:25$9.99 what if we were a student but not
  2595. 2:09:28a
  2596. 2:09:30senior you get a student discount of 10%
  2597. 2:09:33the price of a ticket is
  2598. 2:09:36$8.99 what if we're not a student but we
  2599. 2:09:39are a
  2600. 2:09:42senior you get a senior discount of 20%
  2601. 2:09:46the price of a ticket is
  2602. 2:09:48$7.99 what if we're both a student and a
  2603. 2:09:53senior you get a senior discount of 20%
  2604. 2:09:57you get a student discount of 10% the
  2605. 2:10:00price of a ticket is
  2606. 2:10:02$6.99 that's how nesa if statements can
  2607. 2:10:05be useful you could check another
  2608. 2:10:07condition after checking a condition
  2609. 2:10:09already so that's just one example of
  2610. 2:10:12nesa if statements and well everybody
  2611. 2:10:14those are Nesta if statements in
  2612. 2:10:19Java hey yeah so today I'm going to show
  2613. 2:10:22you a few useful string Methods you may
  2614. 2:10:24be interested in as a beginner using
  2615. 2:10:26Java in this demonstration let's create
  2616. 2:10:28a variable of name the data type is
  2617. 2:10:30going to be a string type in your full
  2618. 2:10:32name including a
  2619. 2:10:34space I'll just use my YouTube channel
  2620. 2:10:36name if at any time you need the length
  2621. 2:10:39of a string you can use the length
  2622. 2:10:40method I'll store that as an integer int
  2623. 2:10:44length equals to access these string
  2624. 2:10:47Methods you need a string or a variable
  2625. 2:10:51that is a string
  2626. 2:10:53dot then we have one of a few methods if
  2627. 2:10:56you need the length you can use the
  2628. 2:10:57length
  2629. 2:10:59method and then let's print
  2630. 2:11:03it print the length how many characters
  2631. 2:11:07are in the string
  2632. 2:11:09eight 1 2 3 4 5 6 7 8 this could be
  2633. 2:11:14useful let's say somebody is setting a
  2634. 2:11:16password and you have a maximum size of
  2635. 2:11:1812 characters well you can check the
  2636. 2:11:19length to see if it's under 12
  2637. 2:11:21characters so that is the length
  2638. 2:11:24method there's Char at that will give
  2639. 2:11:27you the character at a specified index
  2640. 2:11:29we'll be given a single character we'll
  2641. 2:11:31store that as a variable Char letter
  2642. 2:11:35equals take our string of name use the
  2643. 2:11:39Char at
  2644. 2:11:42method within the Char at method we'll
  2645. 2:11:45list an index zero is the index of the
  2646. 2:11:47first character because computers like
  2647. 2:11:49to start at zero what what is the
  2648. 2:11:51character at index
  2649. 2:11:53zero well that letter is B at index
  2650. 2:11:59one that would be R and
  2651. 2:12:02two would be
  2652. 2:12:04o that is the charat method it returns a
  2653. 2:12:08character at a given index within a
  2654. 2:12:10string we also have the capability of
  2655. 2:12:12finding a letter at a given
  2656. 2:12:14index let's say we have a variable of
  2657. 2:12:17index take our name use the index of
  2658. 2:12:23method find the first occurrence of an
  2659. 2:12:27O so in my example that should be 2 0 1
  2660. 2:12:322 that is the first index of this
  2661. 2:12:35character o what about a
  2662. 2:12:39space well that would be three 0 1 2
  2663. 2:12:463 there's also last
  2664. 2:12:49index last index equals take our string
  2665. 2:12:53of
  2666. 2:12:54name then use the last index of
  2667. 2:13:00method what is the last index of an O
  2668. 2:13:03because index of gives you the first
  2669. 2:13:05index last gives you well the last
  2670. 2:13:09one what is the last index of O where is
  2671. 2:13:12it exactly
  2672. 2:13:15five 0 1 2 3 4 5
  2673. 2:13:21we can take a string and make all the
  2674. 2:13:23characters uppercase all reassign that
  2675. 2:13:25to our name variable so we're going to
  2676. 2:13:28take our name use the two uppercase
  2677. 2:13:32method then print our name after
  2678. 2:13:35reassigning
  2679. 2:13:36it now all the letters are
  2680. 2:13:39uppercase there's also two
  2681. 2:13:42lowercase we'll replace upper with
  2682. 2:13:46lower and I should probably turn this
  2683. 2:13:49into a comment
  2684. 2:13:52now all the characters are
  2685. 2:13:55lowercase we can trim any white space
  2686. 2:13:57before and after a string let's add a
  2687. 2:13:59few spaces before and after then print
  2688. 2:14:02the
  2689. 2:14:03name we can eliminate all this white
  2690. 2:14:06space we'll use the trim method for that
  2691. 2:14:09we'll reassign name equals name and use
  2692. 2:14:13the trim
  2693. 2:14:15method now while that white space is
  2694. 2:14:19gone we can replace a character with
  2695. 2:14:22another
  2696. 2:14:23one name equals name we will replace one
  2697. 2:14:28character with another let's replace any
  2698. 2:14:31o's with A's for your own name feel free
  2699. 2:14:35to change
  2700. 2:14:38these so what is my name
  2701. 2:14:42now
  2702. 2:14:44broade there are methods that return
  2703. 2:14:46Boolean values to check to see if a
  2704. 2:14:49string is empty you can use the is empty
  2705. 2:14:52method this would return a Boolean to
  2706. 2:14:55demonstrate I'm going to put this within
  2707. 2:14:57a print
  2708. 2:15:01statement so my string is not empty
  2709. 2:15:04that's false but if it was it would
  2710. 2:15:08return true but I have to turn these
  2711. 2:15:10lines into
  2712. 2:15:12comments that would return true my name
  2713. 2:15:15is empty this could be useful within an
  2714. 2:15:18if statement
  2715. 2:15:22if name do is empty
  2716. 2:15:26method if our name is empty we will
  2717. 2:15:28print your name is
  2718. 2:15:32empty else we will
  2719. 2:15:36print
  2720. 2:15:37hello plus
  2721. 2:15:39name currently my name is
  2722. 2:15:42empty your name is
  2723. 2:15:44empty this method returns true but if it
  2724. 2:15:48wasn't
  2725. 2:15:51it would return
  2726. 2:15:52false hello whatever your name
  2727. 2:15:58is I'll use a multi-line comment just to
  2728. 2:16:01comment all this
  2729. 2:16:06out we can check to see if our string
  2730. 2:16:09contains a character that would return a
  2731. 2:16:13Boolean let's check to see if our
  2732. 2:16:17name contains any spaces
  2733. 2:16:23if that's
  2734. 2:16:27true your name
  2735. 2:16:31contains a
  2736. 2:16:34space else will print your
  2737. 2:16:39name doesn't
  2738. 2:16:42contain any
  2739. 2:16:44spaces so my name does contain a
  2740. 2:16:48space that would return true your name
  2741. 2:16:51contains a
  2742. 2:16:52space if we're working with let's say a
  2743. 2:16:55username usernames typically don't
  2744. 2:16:57contain
  2745. 2:16:58spaces your name doesn't contain any
  2746. 2:17:01spaces there is a method to check to see
  2747. 2:17:04if two strings are
  2748. 2:17:05equal again I'm going to use a
  2749. 2:17:07multi-line comment just to comment this
  2750. 2:17:15out if our
  2751. 2:17:18name equals
  2752. 2:17:22using the equals method we can check to
  2753. 2:17:24see if two strings are equal the given
  2754. 2:17:26characters within them let's see if our
  2755. 2:17:29name equals
  2756. 2:17:33password this actually would be more
  2757. 2:17:35appropriate if we were working with the
  2758. 2:17:36string named password if our name if the
  2759. 2:17:40string of name is equal to this string
  2760. 2:17:43if they have the same characters exactly
  2761. 2:17:45then we'll print your name can't be
  2762. 2:17:50password
  2763. 2:17:53else we will
  2764. 2:17:56print
  2765. 2:17:57hello plus
  2766. 2:18:00name I will set my name to be
  2767. 2:18:05password your name can't be password but
  2768. 2:18:08if it
  2769. 2:18:12wasn't well then this returns false
  2770. 2:18:15these two strings are not equal and we
  2771. 2:18:18execute the L statement hello your name
  2772. 2:18:21now the equals method doesn't account
  2773. 2:18:23for case sensitivity so let's say our
  2774. 2:18:26password has a capital
  2775. 2:18:31P well then this method returns false
  2776. 2:18:34hello password to ignore case
  2777. 2:18:37sensitivity you can use equals ignore
  2778. 2:18:42case then that would return true your
  2779. 2:18:45name can't be password all right
  2780. 2:18:48everybody those are a few useful string
  2781. 2:18:50Methods you may be interested in as a
  2782. 2:18:52beginner using
  2783. 2:18:54Java all right everybody in this video
  2784. 2:18:57I'm going to explain how the substring
  2785. 2:18:58method Works in Java substring is a
  2786. 2:19:01method that's used to extract a portion
  2787. 2:19:04of a string strings have a built-in
  2788. 2:19:06method of substring within the substring
  2789. 2:19:08method you can list one of two indices
  2790. 2:19:11this method will create a new string
  2791. 2:19:13based on the positioning of the indices
  2792. 2:19:15the start and the end I can best
  2793. 2:19:18demonstrate this with us creating an
  2794. 2:19:20email SCE ER program so what we'll do in
  2795. 2:19:22this demonstration is create a string of
  2796. 2:19:27email type in your own email I don't
  2797. 2:19:29want to give you guys my actual email
  2798. 2:19:31because some of you guys are weird no
  2799. 2:19:33offense my email will be bro1
  2800. 2:19:37[email protected]
  2801. 2:19:41our variable of email is a string
  2802. 2:19:44strings have a built-in method of
  2803. 2:19:46substring we can create a new string
  2804. 2:19:48from portions of the original string
  2805. 2:19:51here's
  2806. 2:19:52how let's say I would like the first six
  2807. 2:19:56letters but depending on your own email
  2808. 2:19:58it may be different the substring method
  2809. 2:20:00will return a new string so let's create
  2810. 2:20:03a new variable the data type of string
  2811. 2:20:06of usern
  2812. 2:20:07name equals to create a substring we
  2813. 2:20:11will take the original string use the
  2814. 2:20:13substring
  2815. 2:20:15method then list one of two possible
  2816. 2:20:17indexes or indices the first index will
  2817. 2:20:21be zero at what position would we like
  2818. 2:20:23this substring to end I would like to
  2819. 2:20:26end right before this at sign so that
  2820. 2:20:28would be 0 1 2 3 4 5
  2821. 2:20:326 the second index is exclusive so we're
  2822. 2:20:36creating a new string of username by
  2823. 2:20:39using the substring method of our
  2824. 2:20:41original string of email then let's
  2825. 2:20:43print it to test it we will display our
  2826. 2:20:47username and now we have a new string
  2827. 2:20:50mine is bro 1 123 but depending on what
  2828. 2:20:53you wrote for your email it may be
  2829. 2:20:54different now I would like a new
  2830. 2:20:56substring of every character after the
  2831. 2:20:59at sign this will be a variable named
  2832. 2:21:02domain what is the domain of our email
  2833. 2:21:05again we're taking our original string
  2834. 2:21:08using the substring method then list one
  2835. 2:21:11of two possible indices so again let's
  2836. 2:21:14count this is index zero because in
  2837. 2:21:16programming we tend to start with zero 1
  2838. 2:21:192 3 4 5 6
  2839. 2:21:247 so 7 8 9 10 11 12 13 14 15
  2840. 2:21:3316 then we will display our
  2841. 2:21:37domain so my domain is
  2842. 2:21:41gmail.com within your substring method
  2843. 2:21:43if you have a starting index but you
  2844. 2:21:45would like all the characters that come
  2845. 2:21:47after all the way till the end you don't
  2846. 2:21:49necessarily need an ending index X we
  2847. 2:21:51could just say seven that would work too
  2848. 2:21:56gmail.com there's one issue with how we
  2849. 2:21:58wrote this program what if somebody has
  2850. 2:22:00a different length of an email there's a
  2851. 2:22:02greater or lesser number of characters
  2852. 2:22:05so let's say I have a different
  2853. 2:22:11email this program doesn't work as
  2854. 2:22:14intended it's not flexible so my domain
  2855. 2:22:17is [email protected]
  2856. 2:22:19I could could modify the indices of the
  2857. 2:22:22substring method so 0 1 2 3 4 5 6
  2858. 2:22:3178 then the domain would be nine because
  2859. 2:22:33I want everything after the at
  2860. 2:22:36sign and then my username let's check
  2861. 2:22:40that so this would be my username bro
  2862. 2:22:43code one to make this program more
  2863. 2:22:46flexible instead of manually entering
  2864. 2:22:48the indices we can determine this number
  2865. 2:22:50should be with another string method
  2866. 2:22:53we're going to use the index of method
  2867. 2:22:55in place of a number we're going to find
  2868. 2:22:57the at character let's replace our
  2869. 2:23:00second
  2870. 2:23:01index with email our string dot using
  2871. 2:23:06the index of method then we will find
  2872. 2:23:09the index of our at character we're
  2873. 2:23:12going to replace this number with email.
  2874. 2:23:15index of and find the at sign so now
  2875. 2:23:19let's try printing our
  2876. 2:23:22username so here's my
  2877. 2:23:25username then let's print the
  2878. 2:23:29domain currently we have that at sign
  2879. 2:23:32the index of method returns a number
  2880. 2:23:35which we're using as the index it's
  2881. 2:23:37returning the index of this at sign I'm
  2882. 2:23:40going to increase it by one so we get
  2883. 2:23:42everything after the at sign so plus
  2884. 2:23:46one and now the at sign is removed
  2885. 2:23:50even better yet let's accept some user
  2886. 2:23:54input we'll need a scanner import
  2887. 2:23:58java.util do scanner we'll create a
  2888. 2:24:01scanner
  2889. 2:24:02object scanner scanner equals new
  2890. 2:24:08scanner then we will need
  2891. 2:24:11system.in then when we open a scanner it
  2892. 2:24:14is a good idea to close it when we're
  2893. 2:24:15done with it I sometimes forget
  2894. 2:24:20we'll create a prompt for a user to type
  2895. 2:24:22in their
  2896. 2:24:24email enter your email I'll use print
  2897. 2:24:28instead of print line I'm going to
  2898. 2:24:31declare my variables after our scanner
  2899. 2:24:34that's just how I like to arrange things
  2900. 2:24:36we'll declare our email but not yet
  2901. 2:24:38assign
  2902. 2:24:39it same thing applies with our
  2903. 2:24:42username and our
  2904. 2:24:45domain we've already declared these
  2905. 2:24:47variables we don't need to do that again
  2906. 2:24:53instead of manually assigning our email
  2907. 2:24:55we're going to use our scanner to accept
  2908. 2:24:57user input scanner. nextt
  2909. 2:25:02line let's display our username and our
  2910. 2:25:10domain okay enter your email I'll make
  2911. 2:25:12up something bro1 [email protected]
  2912. 2:25:17here's my username
  2913. 2:25:20and the
  2914. 2:25:21domain we can take this a step further
  2915. 2:25:24too we can check to see if our email is
  2916. 2:25:26valid we'll use some of the string
  2917. 2:25:28Methods we learned about in the last
  2918. 2:25:30video so after accepting our email we're
  2919. 2:25:34going to write an if
  2920. 2:25:35statement if our email contains the at
  2921. 2:25:43character our email is only valid if we
  2922. 2:25:45have this at character if it is
  2923. 2:25:50then we'll perform all this code so
  2924. 2:25:51let's cut it then stick it within the if
  2925. 2:25:54statement else we will do something
  2926. 2:25:58else let's print emails must contain
  2927. 2:26:05at let's type in an email that's not
  2928. 2:26:08valid I won't include the at
  2929. 2:26:12sign emails must contain the at
  2930. 2:26:16character let's try that
  2931. 2:26:18again fake
  2932. 2:26:21guy one at maybe they're using Yahoo
  2933. 2:26:26yahoo.com so my username would be fake
  2934. 2:26:29guy one the domain is
  2935. 2:26:32yahoo.com all right everybody so that is
  2936. 2:26:34the substring method and how to create
  2937. 2:26:36an email slicer program the substring
  2938. 2:26:39method is used to extract a portion of a
  2939. 2:26:41string you can list one of two possible
  2940. 2:26:43indices within the substring method a
  2941. 2:26:45starting index and an ending index we
  2942. 2:26:48use the substring method on an email
  2943. 2:26:50then created two new substrings one of
  2944. 2:26:52username and the other of domain and
  2945. 2:26:54well everybody that is the substring
  2946. 2:26:56method in
  2947. 2:26:59Java hey everybody in this video we're
  2948. 2:27:01going to create a weight conversion
  2949. 2:27:03program to convert a weight from pounds
  2950. 2:27:05to kilogram or vice versa it will help
  2951. 2:27:07us become more familiar with if
  2952. 2:27:09statements so let's get started okay
  2953. 2:27:11let's get started everybody what might
  2954. 2:27:13be helpful to you as a beginner when
  2955. 2:27:14creating a project is to use comments to
  2956. 2:27:17describe what you want to do at each
  2957. 2:27:18part of your program this is also known
  2958. 2:27:20as pseudo code for example the first
  2959. 2:27:22thing we'll do is declare our variables
  2960. 2:27:25declare
  2961. 2:27:28variables then we'll create a welcome
  2962. 2:27:33message then a prompt for user
  2963. 2:27:36input prompt for user input or
  2964. 2:27:43Choice we'll say option
  2965. 2:27:46one convert lbs meaning pounds to to kgs
  2966. 2:27:52kilog option two convert kgs to
  2967. 2:27:58LBS if somebody doesn't select option
  2968. 2:28:01one or two we'll use an else statement
  2969. 2:28:03else print not a valid
  2970. 2:28:08choice so here's a rough outline of our
  2971. 2:28:10program and what it's going to do we'll
  2972. 2:28:12begin at the top we'll declare any
  2973. 2:28:14variables that we'll need so we will
  2974. 2:28:16need a scanner we'll create a scanner
  2975. 2:28:18object scanner scanner equals new
  2976. 2:28:21scanner then type system.in within set
  2977. 2:28:24of parentheses if you hover over the
  2978. 2:28:27word scanner you can import it
  2979. 2:28:29automatically import
  2980. 2:28:31java.util.scanner or otherwise you can
  2981. 2:28:34type it
  2982. 2:28:35in what are some other variables we'll
  2983. 2:28:38need double weight for the original
  2984. 2:28:41weight double new
  2985. 2:28:44weight that's after we convert the
  2986. 2:28:46weight we'll need to store it and int
  2987. 2:28:49choice
  2988. 2:28:50a user is going to type in one to
  2989. 2:28:52convert pounds to kilograms or two to
  2990. 2:28:55convert kilograms to
  2991. 2:28:57pounds we have our variables now we need
  2992. 2:29:00a welcome
  2993. 2:29:02message I'll use a few print
  2994. 2:29:04statements let's say weight
  2995. 2:29:08conversion
  2996. 2:29:15program one C and
  2997. 2:29:18space this means opt one convert lbs
  2998. 2:29:22meaning pounds to kgs
  2999. 2:29:26kilog or Choice
  3000. 2:29:282 to colon space convert kilogram to
  3001. 2:29:34pounds
  3002. 2:29:36lbs then we'll prompt for a user's
  3003. 2:29:41Choice I'll use print instead of print
  3004. 2:29:45line choose an option
  3005. 2:29:51then we'll need to store that choice
  3006. 2:29:53variable Choice equals use our scanner
  3007. 2:29:56to accept user input we're accepting an
  3008. 2:29:59integer we'll use next
  3009. 2:30:03int let's do a test run and I will print
  3010. 2:30:06our choice
  3011. 2:30:10temporarily weight conversion program
  3012. 2:30:12one to convert pounds to kilograms or
  3013. 2:30:15two to convert kilograms to pounds I'll
  3014. 2:30:18type in one we get one
  3015. 2:30:21or
  3016. 2:30:22two then we output two we can delete
  3017. 2:30:26this print line statement we were just
  3018. 2:30:27checking to see if it
  3019. 2:30:29works okay option one if somebody
  3020. 2:30:32selects option one we have to convert
  3021. 2:30:34pounds to
  3022. 2:30:35kilograms well we'll need to check to
  3023. 2:30:37see if choice is one we can use an if
  3024. 2:30:40statement for
  3025. 2:30:42that if choice is equal to one be sure
  3026. 2:30:46you use double equals that is the
  3027. 2:30:48comparison operator we're checking if
  3028. 2:30:51these two values are equal if you use a
  3029. 2:30:53single equals that is the assignment
  3030. 2:30:56operator and Java thinks you're trying
  3031. 2:30:58to assign one to choice so double equals
  3032. 2:31:01for
  3033. 2:31:03comparisons we're converting pounds to
  3034. 2:31:05kilograms we'll ask a user to enter the
  3035. 2:31:11weight in
  3036. 2:31:13lbs I'll use print instead of print
  3037. 2:31:18line we we will assign our
  3038. 2:31:22weight equals use our scanner then use
  3039. 2:31:26the next Double method then we will
  3040. 2:31:29accept a double value so once we have
  3041. 2:31:32our weight we have to convert it to
  3042. 2:31:34kilograms because it's currently in
  3043. 2:31:36pounds we will assign our new weight
  3044. 2:31:40equal to the weight our current weight
  3045. 2:31:43that
  3046. 2:31:44is times
  3047. 2:31:460.45 3592
  3048. 2:31:51then we'll output the new
  3049. 2:31:55weight the
  3050. 2:31:57new weight in
  3051. 2:32:00kgs
  3052. 2:32:02is then we'll add our new
  3053. 2:32:07weight okay let's perform a test
  3054. 2:32:10run we'll type in one to convert pounds
  3055. 2:32:13to kilogram enter the weight in pounds
  3056. 2:32:16what's 150 lb converted to kilog
  3057. 2:32:2168.0
  3058. 2:32:23388 if you would like to limit the
  3059. 2:32:25amount of digits that display after the
  3060. 2:32:27decimal you can use a print F statement
  3061. 2:32:29instead of print line so to do that
  3062. 2:32:32we'll convert this to print
  3063. 2:32:35F where we would like to insert a value
  3064. 2:32:37or variable we'll use percent since
  3065. 2:32:40we're displaying a double we'll use f
  3066. 2:32:44meaning floating Point number to display
  3067. 2:32:46two digits after the decimal we'll use 0
  3068. 2:32:492 or 0.1 to display one digit depending
  3069. 2:32:52on your preference replace the plus with
  3070. 2:32:55a
  3071. 2:32:56comma we're inserting this variable at
  3072. 2:32:59this location this placeholder let's
  3073. 2:33:01perform that
  3074. 2:33:02again choose an option convert pounds to
  3075. 2:33:05kilograms enter the weight in pounds 150
  3076. 2:33:09is good the new weight in kilogram is
  3077. 2:33:1368.4
  3078. 2:33:15kilg now if Choice equals 2 we're going
  3079. 2:33:19to convert kilog to pounds and really we
  3080. 2:33:22can just copy the if statement paste it
  3081. 2:33:25then convert
  3082. 2:33:27it else if Choice equals 2 the user
  3083. 2:33:31wants to convert kilogram to pounds
  3084. 2:33:34enter the weight in
  3085. 2:33:36kilog the new formula is going to be
  3086. 2:33:39weight time
  3087. 2:33:422.24
  3088. 2:33:4462 the new weight in pounds lbs is our
  3089. 2:33:50new
  3090. 2:33:52weight let's try
  3091. 2:33:54it choose an option we will convert
  3092. 2:33:57kilograms to pounds I'm going to press
  3093. 2:33:59two enter the weight in kilogram let's
  3094. 2:34:03say 68
  3095. 2:34:05kg the new weight in pounds is
  3096. 2:34:0914991
  3097. 2:34:11lb what if somebody types in a number
  3098. 2:34:14that's not
  3099. 2:34:15valid so we can get rid of this comment
  3100. 2:34:18is use an else statement to print print
  3101. 2:34:20that the user didn't select a valid
  3102. 2:34:23choice we will follow this with an lse
  3103. 2:34:26statement we will follow else if with an
  3104. 2:34:28else statement else
  3105. 2:34:31print that was not a valid
  3106. 2:34:38choice we have options one or two if I
  3107. 2:34:41type in three we execute the L statement
  3108. 2:34:44that was not a valid
  3109. 2:34:46choice and again like usual I'm
  3110. 2:34:49forgetting to close my scanner because I
  3111. 2:34:51sometimes forget to do that so at the
  3112. 2:34:53end of your program close your scanner
  3113. 2:34:55scanner. close because you don't want to
  3114. 2:34:58leave resources
  3115. 2:34:59open okay let's do one final test run
  3116. 2:35:02let's convert pounds to kilograms what's
  3117. 2:35:06200 lb converted to
  3118. 2:35:08kilograms 90.7
  3119. 2:35:102 all right everybody so that is a
  3120. 2:35:13weight conversion program you can write
  3121. 2:35:15using
  3122. 2:35:16[Music]
  3123. 2:35:18Java hey everybody in this video I'm
  3124. 2:35:20going to show you how we can use the
  3125. 2:35:22tary operator in Java in Java the tary
  3126. 2:35:25operator is a question mark followed by
  3127. 2:35:27one of two possible values here's the
  3128. 2:35:29formula we check a condition then use a
  3129. 2:35:32question mark like we're asking a
  3130. 2:35:33question is this condition true if it is
  3131. 2:35:37we'll return a value if not we return
  3132. 2:35:40something else a different value it's a
  3133. 2:35:43simpler version to an if else statement
  3134. 2:35:45here's a
  3135. 2:35:46demonstration let's say we have an
  3136. 2:35:48integer of a score score will equal 70
  3137. 2:35:52that's technically
  3138. 2:35:54passing using an if statement I will
  3139. 2:35:56check if my score is greater than or
  3140. 2:35:59equal to
  3141. 2:36:0260 if it is then we will print pass else
  3142. 2:36:07we will
  3143. 2:36:10print fail so this does
  3144. 2:36:14work with a score of 70 I have passed if
  3145. 2:36:18it were 55
  3146. 2:36:20well then I fail there's another way in
  3147. 2:36:23which we can write this and that is by
  3148. 2:36:24using the tary
  3149. 2:36:26operator what we're going to do let's
  3150. 2:36:29create a variable a string variable of
  3151. 2:36:33pass or
  3152. 2:36:35fail and we're basically just following
  3153. 2:36:37this formula we have our condition is
  3154. 2:36:41our score greater than or equal to
  3155. 2:36:4560 question mark we're asking a question
  3156. 2:36:49is this condition true if it is let's
  3157. 2:36:52return a string of pass colon think of
  3158. 2:36:56it like
  3159. 2:36:58otherwise will return
  3160. 2:37:02fail and then I will print pass or fail
  3161. 2:37:05to display it so 55 that means we fail
  3162. 2:37:10but if it were
  3163. 2:37:1375 that means we pass by using the tary
  3164. 2:37:16operator it's an alternative to writing
  3165. 2:37:19an if else statement and it can be
  3166. 2:37:21simpler in many cases here's a few other
  3167. 2:37:24examples let's create a variable of
  3168. 2:37:27number set it equal to some
  3169. 2:37:31number using the Turner operator we will
  3170. 2:37:34check if this number is even or
  3171. 2:37:36odd I'll create a string of even or odd
  3172. 2:37:42equals our
  3173. 2:37:44condition we can check if a number is
  3174. 2:37:47even or odd by using the modulus
  3175. 2:37:48operator
  3176. 2:37:50we will take our
  3177. 2:37:52number modulus 2 the modulus operator
  3178. 2:37:56gives you the remainder of any division
  3179. 2:37:58does 3 divide by two evenly if it does
  3180. 2:38:01that would equal
  3181. 2:38:05zero 3 doesn't divide by two evenly
  3182. 2:38:08there's a remainder of one and that does
  3183. 2:38:10not equal
  3184. 2:38:11zero is this number divisible by two
  3185. 2:38:14we'll use the trary operator if it is
  3186. 2:38:18we'll return a string of even otherwise
  3187. 2:38:21we'll return a string of odd is my
  3188. 2:38:24number even or
  3189. 2:38:26odd so three is
  3190. 2:38:29odd but if it were
  3191. 2:38:32four well then it's
  3192. 2:38:35even here's a more practical example
  3193. 2:38:38let's say we're working with a
  3194. 2:38:41time in hours this will be in military
  3195. 2:38:45time hours equals
  3196. 2:38:4813 that would be the the same as 1
  3197. 2:38:51p.m. I will create a string of time of
  3198. 2:38:56day equals we'll check if our hours is
  3199. 2:39:00greater than or equal to 12 if it is
  3200. 2:39:03we'll return PM if not will return
  3201. 2:39:06am hours less than
  3202. 2:39:1012 tary operator is our hours less than
  3203. 2:39:1512 if it is we'll return a.m.
  3204. 2:39:19otherwise will return
  3205. 2:39:22p.m and then we will display the time of
  3206. 2:39:25day 13 hours that's the same as 1:
  3207. 2:39:30p.m. yep the time is 1: p.m. or if the
  3208. 2:39:33hours were 9 that would be
  3209. 2:39:39am all right last example let's say we
  3210. 2:39:42have an income or
  3211. 2:39:45salary income equals $60,000
  3212. 2:39:50let's pretend that you're a software
  3213. 2:39:52engineer in the united states in the
  3214. 2:39:54United States we have different tax
  3215. 2:39:56brackets depending on your income this
  3216. 2:39:58time we will assign a variable of tax
  3217. 2:40:01rate equals we'll check if our
  3218. 2:40:05income is greater than or equal
  3219. 2:40:09to $40,000 for
  3220. 2:40:13$40,000 question mark turnar
  3221. 2:40:16operator if that's true then our tax
  3222. 2:40:19rate is going to be
  3223. 2:40:220.25 otherwise it will be
  3224. 2:40:270.15 so with our income what is the tax
  3225. 2:40:33rate
  3226. 2:40:340.25 but if I changed it to
  3227. 2:40:3730,000 well then our tax rate would be
  3228. 2:40:400.15 for
  3229. 2:40:4315% so that is the tary operator you
  3230. 2:40:46follow this formula you check a
  3231. 2:40:49condition within a set of parentheses
  3232. 2:40:51then use the tary operator like you're
  3233. 2:40:53asking a question if this condition is
  3234. 2:40:56true return this otherwise if it's false
  3235. 2:41:00return something else and then if you
  3236. 2:41:01would like you can assign it to a
  3237. 2:41:03variable and well everybody that is the
  3238. 2:41:05tary operator in
  3239. 2:41:09Java all right everybody in this video
  3240. 2:41:11we're going to create a temperature
  3241. 2:41:13conversion program to convert from
  3242. 2:41:15Celsius to Fahrenheit or Fahrenheit to
  3243. 2:41:17Celsius this is a practice project to
  3244. 2:41:19get us more familiar using the tary
  3245. 2:41:21operator so let's get started we will be
  3246. 2:41:23accepting some user input so let's get
  3247. 2:41:25the scanner out of the way first scanner
  3248. 2:41:28scanner equals new scanner we need a
  3249. 2:41:31scanner to accept user input within the
  3250. 2:41:34set of parentheses type
  3251. 2:41:36system.in and then I will just
  3252. 2:41:37automatically import this class so at
  3253. 2:41:40the top of your Java file you need this
  3254. 2:41:42import import
  3255. 2:41:44java.util.scanner then when you open a
  3256. 2:41:47scanner it is a good idea to close it at
  3257. 2:41:48the end of your file when you're done
  3258. 2:41:50with it because you don't want to leave
  3259. 2:41:52resources open we have our scanner to
  3260. 2:41:54accept user input let's declare some
  3261. 2:41:57variables what will we need we will need
  3262. 2:42:00the original temperature this will be of
  3263. 2:42:02the double data type double temp we'll
  3264. 2:42:06need a new temperature let's say double
  3265. 2:42:09new temp that's after we convert it and
  3266. 2:42:12a unit we could use Char for the unit
  3267. 2:42:15you know the letter F or c for
  3268. 2:42:17Fahrenheit or Celsius but just to keep
  3269. 2:42:20it simple I'm going to use a string
  3270. 2:42:22because there's a built-in method of two
  3271. 2:42:24uppercase we can take the input and make
  3272. 2:42:26it uppercase automatically just to make
  3273. 2:42:28it simple we'll use a string rather than
  3274. 2:42:30a Char for the
  3275. 2:42:32unit okay now we just need to get the
  3276. 2:42:34temperature we'll need a prompt I'll use
  3277. 2:42:37print instead of print line we will ask
  3278. 2:42:40the user to enter the
  3279. 2:42:46temperature we will assign the
  3280. 2:42:48temperature temp equals scanner. next
  3281. 2:42:52Double because we're accepting a value
  3282. 2:42:55we're signing a variable of the double
  3283. 2:42:57data type now we need the unit is it
  3284. 2:43:00going to be Celsius or
  3285. 2:43:02Fahrenheit we'll need another prompt
  3286. 2:43:04I'll use print instead of print
  3287. 2:43:06line we will ask the user convert to
  3288. 2:43:12Celsius or Fahrenheit
  3289. 2:43:20C or
  3290. 2:43:24F let's ass our unit
  3291. 2:43:27variable use our
  3292. 2:43:30scanner then use the next method to get
  3293. 2:43:33the next character we'll use the next
  3294. 2:43:35method to get the next
  3295. 2:43:38character we don't necessarily need next
  3296. 2:43:40line we just want a single
  3297. 2:43:43character so check this out I'm going to
  3298. 2:43:46Output our temp
  3299. 2:43:49and our
  3300. 2:43:57unit what if somebody types in a
  3301. 2:43:59lowercase C or F so let's say the
  3302. 2:44:02temperature is
  3303. 2:44:0475 75° F and we would like to convert it
  3304. 2:44:08to Celsius I'll type lowercase C rather
  3305. 2:44:11than uppercase
  3306. 2:44:14C our unit is currently a lowercase C we
  3307. 2:44:18can convert it to be
  3308. 2:44:20uppercase following the next method of
  3309. 2:44:22our scanner where we assign our unit
  3310. 2:44:24we're going to do something called
  3311. 2:44:25method
  3312. 2:44:26chaining we'll add another dot then use
  3313. 2:44:29the two uppercase method of
  3314. 2:44:33strings let's try that again and see if
  3315. 2:44:35it
  3316. 2:44:36works
  3317. 2:44:3975 I'll type in lower case
  3318. 2:44:44C the two uppercase method will take
  3319. 2:44:46that lowercase C and make it uppercase
  3320. 2:44:50which is what we'll need in this next
  3321. 2:44:51section we can delete these two
  3322. 2:44:55statements now we're going to use the
  3323. 2:44:57tary
  3324. 2:44:58operator how the trary operator works is
  3325. 2:45:02that we check a
  3326. 2:45:03condition followed by a question mark
  3327. 2:45:06like we're asking a question we will
  3328. 2:45:08return some code if that condition is
  3329. 2:45:10true colon meaning else if it's false
  3330. 2:45:14we'll do some other code or return some
  3331. 2:45:16other value we're going to follow this
  3332. 2:45:19formula so what is our condition let's
  3333. 2:45:23check to see if our unit is equal to a c
  3334. 2:45:27take our variable of
  3335. 2:45:29unit use the equals method because
  3336. 2:45:32strings have a built-in method of
  3337. 2:45:34equals does our unit equal in uppercase
  3338. 2:45:38C question mark That's the tary operator
  3339. 2:45:42we're asking a
  3340. 2:45:44question if this is true that means
  3341. 2:45:46we're trying to convert from Fahrenheit
  3342. 2:45:48to Celsius
  3343. 2:45:50the formula to do that would be the
  3344. 2:45:52following we would be executing this
  3345. 2:45:55code since it's
  3346. 2:45:56true take our temp subtract
  3347. 2:46:0032 then multiply 5 /
  3348. 2:46:059 now what if our unit does not equal C
  3349. 2:46:09that means we're trying to convert to
  3350. 2:46:11Fahrenheit we'll need that colon for the
  3351. 2:46:13next part of the
  3352. 2:46:14section now here's the formula to
  3353. 2:46:16convert from Celsius to Fahrenheit
  3354. 2:46:20this will be the false section of the
  3355. 2:46:22trinary
  3356. 2:46:24operator take our temp * 5 /
  3357. 2:46:309 +
  3358. 2:46:3232 with the trinary operator how we've
  3359. 2:46:36written this code is that we're going to
  3360. 2:46:37be returning a double after converting
  3361. 2:46:40it we need to do something with it so
  3362. 2:46:42we're going to assign it to a variable
  3363. 2:46:45our variable of new temp then we can do
  3364. 2:46:47something with it
  3365. 2:46:50once we have our new temperature let's
  3366. 2:46:52display it I'll perform a test run let's
  3367. 2:46:56print our new
  3368. 2:46:58temp let's say it's 100° F and we're
  3369. 2:47:02going to convert to Celsius I'll type in
  3370. 2:47:05see well the temperature in Celsius is
  3371. 2:47:0937.7 repeating we're going to format
  3372. 2:47:12this in a
  3373. 2:47:14moment let's add a degree
  3374. 2:47:16symbol plus a string on Windows to add a
  3375. 2:47:21degree symbol make sure num lock is on
  3376. 2:47:23hold alt then type
  3377. 2:47:280176 plus our unit we're converting to
  3378. 2:47:32let's try that
  3379. 2:47:34again what is 100° fah converted to
  3380. 2:47:39Celsius
  3381. 2:47:4137.7 repeating de
  3382. 2:47:44C to limit the amount of digits that
  3383. 2:47:46display after the decimal we we can use
  3384. 2:47:49a print F statement let's convert our
  3385. 2:47:51print line for the new temp to be print
  3386. 2:47:53F rather than print
  3387. 2:47:55line we'll add a
  3388. 2:47:59placeholder we're displaying a double so
  3389. 2:48:02we need F to display one digit type 0.1
  3390. 2:48:06between the percent sign and the
  3391. 2:48:08F comma new
  3392. 2:48:14temp after our placeholder We'll add our
  3393. 2:48:17degree symbol again I'm holding alt numb
  3394. 2:48:20lock is on I'm typing
  3395. 2:48:230176 then we will display the unit it's
  3396. 2:48:26a string percent s for
  3397. 2:48:29Strings we're inserting a new variable
  3398. 2:48:32we're going to comma separate them then
  3399. 2:48:34add our
  3400. 2:48:35unit and let me just close this Gap
  3401. 2:48:39let's see what the final product
  3402. 2:48:41is let's convert
  3403. 2:48:4430.1 this will be in Celsius to
  3404. 2:48:47Fahrenheit
  3405. 2:48:49and the temperature in Fahrenheit is
  3406. 2:48:5148.7 De
  3407. 2:48:55fahit okay everybody that is a
  3408. 2:48:57temperature conversion program that you
  3409. 2:48:59can write using the tary operator in
  3410. 2:49:04Java why hello there today I got to
  3411. 2:49:07explain something called enhanced
  3412. 2:49:09switches in Java in Java there switches
  3413. 2:49:12and then there's enhanced switches
  3414. 2:49:15enhanced switches are a Java 14 feature
  3415. 2:49:17I would recommend using using enhanced
  3416. 2:49:19switches over standard switches so what
  3417. 2:49:21is a switch exactly a switch is a
  3418. 2:49:24replacement to using many l statements
  3419. 2:49:27here's an example of us using a lot of
  3420. 2:49:29lse if statements in this sample program
  3421. 2:49:32we have a day of the week I set mine to
  3422. 2:49:34be Friday with each of these if
  3423. 2:49:36statements I am checking to see if the
  3424. 2:49:38day equals Monday Tuesday so on and so
  3425. 2:49:42forth currently my day is set to
  3426. 2:49:45Friday this would give me it is a
  3427. 2:49:47weekday
  3428. 2:49:49if I set the day to be
  3429. 2:49:52Saturday well then it is the
  3430. 2:49:55weekend or Sunday let me show you the
  3431. 2:49:59code it is the weekend then I have an
  3432. 2:50:02else statement if none of the above
  3433. 2:50:04conditions are true for example let's
  3434. 2:50:07say day is pizza day which doesn't exist
  3435. 2:50:10but it really
  3436. 2:50:11should Pizza day is not a day this code
  3437. 2:50:15does work but it could be more efficient
  3438. 2:50:18we have a lot of redundancies we're
  3439. 2:50:20using a lot of else if statements that
  3440. 2:50:22all basically do the same
  3441. 2:50:25thing an improvement would be to use an
  3442. 2:50:28enhanced switch let me demonstrate how
  3443. 2:50:31to make
  3444. 2:50:33that so let's set our day to be Monday
  3445. 2:50:37for example to create a switch we will
  3446. 2:50:40type switch at a set of parentheses then
  3447. 2:50:43a set of curly
  3448. 2:50:44braces it's similar to an if statement
  3449. 2:50:47already with within the set of
  3450. 2:50:49parentheses for the switch we're going
  3451. 2:50:51to examine a value or variable we're
  3452. 2:50:53going to examine our string of day we
  3453. 2:50:56will examine this value against any
  3454. 2:50:59matching cases the first case will be a
  3455. 2:51:02string of
  3456. 2:51:04Monday if day is equal to this value a
  3457. 2:51:07string of Monday we'll do
  3458. 2:51:10something we'll use an arrow an arrow is
  3459. 2:51:13the arrow operator in Java think of it
  3460. 2:51:15as meaning do something do this if day
  3461. 2:51:19equals Monday do this code so let's
  3462. 2:51:22print
  3463. 2:51:23something let's print it is a
  3464. 2:51:28weekday and then for fun you don't have
  3465. 2:51:30to but I'm going to add an
  3466. 2:51:33emoji nobody likes Mondays let's be
  3467. 2:51:36honest if day is Monday do this code
  3468. 2:51:40let's add another case let me zoom in a
  3469. 2:51:43little if Case is
  3470. 2:51:47Tuesday Arrow op operator meaning do
  3471. 2:51:49this code we will print we'll print the
  3472. 2:51:53same thing it is a
  3473. 2:51:55weekday we'll do this with the other
  3474. 2:51:57days too I'll just copy this code then
  3475. 2:52:01repurpose it to save some time case
  3476. 2:52:04Wednesday it is a
  3477. 2:52:07weekday case
  3478. 2:52:10Thursday it is a
  3479. 2:52:12weekday case
  3480. 2:52:15Friday it is a weekday now if it's sat
  3481. 2:52:18Saturday that means it's the
  3482. 2:52:20weekend case
  3483. 2:52:23Saturday it is the weekend let's change
  3484. 2:52:28the
  3485. 2:52:32Emoji case Sunday it is the
  3486. 2:52:39weekend let's perform a test run if
  3487. 2:52:42you're using intellig you may have These
  3488. 2:52:44Warnings it's basically saying that
  3489. 2:52:46these sections of code are unreachable
  3490. 2:52:48we're manually setting a variable these
  3491. 2:52:51sections of code are not going to
  3492. 2:52:52execute so I would just ignore that for
  3493. 2:52:55now so let's perform a test
  3494. 2:52:58run my day is Monday day is equal to the
  3495. 2:53:02case of Monday so we do this code if it
  3496. 2:53:06were
  3497. 2:53:10Friday case Friday we do this
  3498. 2:53:14code if Case were Saturday
  3499. 2:53:19well we do this
  3500. 2:53:21code and
  3501. 2:53:25Sunday case Sunday so we do this code it
  3502. 2:53:29is the
  3503. 2:53:30weekend what if we have a day that
  3504. 2:53:32doesn't exist like pizza
  3505. 2:53:37day well we can add a default case
  3506. 2:53:40here's how we'll type the keyword
  3507. 2:53:43default Arrow meaning do
  3508. 2:53:46this we'll print
  3509. 2:53:49our variable
  3510. 2:53:51day plus a string of is not a
  3511. 2:53:57day Pizza day is not a day the default
  3512. 2:54:01case it behaves similarly to your else
  3513. 2:54:04statement if no above conditions are
  3514. 2:54:07true then we will do
  3515. 2:54:09this so what we'll do now is accept some
  3516. 2:54:12user input using a scanner we'll need to
  3517. 2:54:14create a scanner object scanner scanner
  3518. 2:54:18equals new scanner within the set of
  3519. 2:54:21parentheses type
  3520. 2:54:22system.in and then I'm just going to
  3521. 2:54:24automatically import this class at the
  3522. 2:54:26top of my Java file import
  3523. 2:54:29java.util.scanner
  3524. 2:54:31we'll create a prompt for a user to type
  3525. 2:54:34in the
  3526. 2:54:35day enter the day of the week and I will
  3527. 2:54:40use print instead of print line instead
  3528. 2:54:43of manually assigning a variable we'll
  3529. 2:54:45use our scanner scanner. nextt line to
  3530. 2:54:48accept a
  3531. 2:54:51string okay let's try this
  3532. 2:54:54again enter the day of the week it is
  3533. 2:54:57Tuesday it is a weekday let's try that
  3534. 2:55:00again enter the day of the week it is
  3535. 2:55:03Saturday it is the
  3536. 2:55:05weekend and let's make up a
  3537. 2:55:08day uh let's go with taco
  3538. 2:55:11day taco day is not a
  3539. 2:55:15day we could actually improve this widg
  3540. 2:55:18even further intellig is saying we have
  3541. 2:55:20a duplicate branch in our switch we have
  3542. 2:55:23two or more cases that are doing the
  3543. 2:55:25same thing we are outputting it is a
  3544. 2:55:30weekday we could consolidate some of
  3545. 2:55:32these cases here's how the cases of
  3546. 2:55:35Monday Tuesday Wednesday Thursday and
  3547. 2:55:37Friday they all output the same thing so
  3548. 2:55:40we're going to consolidate them I'll
  3549. 2:55:43copy one of these print statements and
  3550. 2:55:45delete all of them
  3551. 2:55:54we can comma separate these cases so
  3552. 2:55:57case Monday
  3553. 2:55:59comma
  3554. 2:56:01Tuesday
  3555. 2:56:04comma
  3556. 2:56:06Wednesday
  3557. 2:56:08comma
  3558. 2:56:10Thursday
  3559. 2:56:13comma
  3560. 2:56:16Friday then Arrow
  3561. 2:56:19if our case is Monday Tuesday Wednesday
  3562. 2:56:21Thursday or Friday then do the following
  3563. 2:56:25we'll print it is a
  3564. 2:56:27weekday now Saturday and Sunday do the
  3565. 2:56:30same thing
  3566. 2:56:30too let's cut one of these
  3567. 2:56:34lines and delete
  3568. 2:56:37them case
  3569. 2:56:39Saturday comma
  3570. 2:56:42Sunday Arrow meaning do this it is the
  3571. 2:56:46weekend then we'll keep keep our default
  3572. 2:56:49case in case there are no matches let's
  3573. 2:56:52try this
  3574. 2:56:53again enter the day of the week it is
  3575. 2:56:58Wednesday it is a
  3576. 2:57:01weekday it is
  3577. 2:57:04Saturday it is the
  3578. 2:57:06weekend it is Sunday it is the
  3579. 2:57:11weekend uh then let's go with uh
  3580. 2:57:13Hamburger
  3581. 2:57:14Day hamburger day is not a day
  3582. 2:57:19so those are enhanced switches they're a
  3583. 2:57:21replacement to using many LF
  3584. 2:57:24statements if you find yourself using a
  3585. 2:57:26lot of lsif statements I would recommend
  3586. 2:57:28using a switch instead but not just any
  3587. 2:57:31switch an enhanced switch which is a
  3588. 2:57:34Java 14 feature that you should know
  3589. 2:57:36about and well everybody those are
  3590. 2:57:38enhanced switches in
  3591. 2:57:41Java what is going on everybody in this
  3592. 2:57:44video we're going to create a simple
  3593. 2:57:46calculator program using what we've
  3594. 2:57:48learned with enhanced switches this is
  3595. 2:57:50meant to be more of a practice project
  3596. 2:57:52let's get started the first thing we'll
  3597. 2:57:54need is a scanner we will create a
  3598. 2:57:56scanner object scanner scanner equals
  3599. 2:57:59new scanner within the set of
  3600. 2:58:01parentheses type
  3601. 2:58:03system.in and then when you open a
  3602. 2:58:05scanner it is a good idea to close it
  3603. 2:58:07when you're done with
  3604. 2:58:09it scanner. close then be sure to import
  3605. 2:58:13the class at the top of your Java file
  3606. 2:58:16make sure you have this import import
  3607. 2:58:20java.util.scanner
  3608. 2:58:21what are the different variables we'll
  3609. 2:58:24need a user is going to type in two
  3610. 2:58:26numbers and an operator such as a plus
  3611. 2:58:29for addition minus for subtraction so on
  3612. 2:58:32and so forth the first variable will be
  3613. 2:58:34up the double data type double num
  3614. 2:58:38one double num two then an operator this
  3615. 2:58:43will be a Char Char operator
  3616. 2:58:49and a result double
  3617. 2:58:53result there's going to be one more
  3618. 2:58:55variable but we'll fill that in later so
  3619. 2:58:58we'll stick with these four for now
  3620. 2:59:00we'll prompt a user to type in the first
  3621. 2:59:02number followed by an operator than the
  3622. 2:59:05second
  3623. 2:59:06number here's how we can write
  3624. 2:59:08that let's ask the user to enter the
  3625. 2:59:12first number and I will use print
  3626. 2:59:16instead of print line
  3627. 2:59:20we will assign num one equal to use our
  3628. 2:59:24scanner to get user
  3629. 2:59:25input next
  3630. 2:59:30Double then we need the operator we will
  3631. 2:59:34use a print
  3632. 2:59:36statement enter n
  3633. 2:59:40operator we'll have Plus for addition
  3634. 2:59:43minus for subtraction an asterisk for
  3635. 2:59:46multiplication a for slash for division
  3636. 2:59:49let's add one more let's add a carrot
  3637. 2:59:53this will raise a base to a given
  3638. 2:59:55power feel free to add more if you would
  3639. 2:59:57like but we'll just stick with these
  3640. 3:00:00five a user is going to enter in a
  3641. 3:00:03character one of these operators
  3642. 3:00:05hopefully we will assign our variable of
  3643. 3:00:08operator equal to
  3644. 3:00:11scanner.
  3645. 3:00:16next the next method will give you a
  3646. 3:00:18string we can method chain the Char at
  3647. 3:00:22method to return a single character when
  3648. 3:00:25a user types in something just give me
  3649. 3:00:27the first character this will also
  3650. 3:00:30convert it to be a character rather than
  3651. 3:00:32keep it a
  3652. 3:00:33string so ideally our operator is going
  3653. 3:00:36to be a plus a minus an asterisk a
  3654. 3:00:39forward slash or a
  3655. 3:00:43carrot then we need num two we can just
  3656. 3:00:46copy these two lines paste them change
  3657. 3:00:49first to be second and num one to be num
  3658. 3:00:53two we have our two numbers and our
  3659. 3:00:58operator we have to determine what the
  3660. 3:01:00operator is we can use an enhanced
  3661. 3:01:03switch for that we will create a
  3662. 3:01:06switch within the set of
  3663. 3:01:08parentheses we are examining our
  3664. 3:01:12operator we're examining our operator
  3665. 3:01:15against any matching cases the first
  3666. 3:01:18case will be a plus sign and these cases
  3667. 3:01:21are going to be within single quotes not
  3668. 3:01:23double quotes because we're working with
  3669. 3:01:26a character not a
  3670. 3:01:27string with our switch if our operator
  3671. 3:01:31is a plus character we're going to write
  3672. 3:01:34an arrow meaning do
  3673. 3:01:36this we will take our variable of
  3674. 3:01:39result set it equal to be num one that's
  3675. 3:01:43the first number plus num two
  3676. 3:01:48that will be the case for addition then
  3677. 3:01:50we have
  3678. 3:01:51subtraction case minus Arrow do this
  3679. 3:01:56assign our result equal to num one minus
  3680. 3:01:59num
  3681. 3:02:012 multiplication would be
  3682. 3:02:04case a character vaster risk arrow take
  3683. 3:02:08our result equals num one * num
  3684. 3:02:15two now for division
  3685. 3:02:17we have a forward
  3686. 3:02:19slash arrow take our result equals num
  3687. 3:02:24one divided by num
  3688. 3:02:28two now with division if somebody
  3689. 3:02:31divides by zero we'll return to this
  3690. 3:02:33case later and make it a little more
  3691. 3:02:35sophisticated where we check to see if
  3692. 3:02:37num 2 is zero I would like for us to get
  3693. 3:02:39a solid foundation first for this
  3694. 3:02:43program so if somebody would like to
  3695. 3:02:45raise a base to a given power we will
  3696. 3:02:47use the
  3697. 3:02:51carrot we will assign our result equal
  3698. 3:02:53to now we're going to use the math
  3699. 3:02:56class the power method of the math class
  3700. 3:02:59we can raise a base to a given power we
  3701. 3:03:01will raise num one to the power of num
  3702. 3:03:06two let's perform a test run just to be
  3703. 3:03:09sure that it
  3704. 3:03:10works I'm going to display our result
  3705. 3:03:13after the
  3706. 3:03:15switch so Java is giving us a warning
  3707. 3:03:18that variable result might not have been
  3708. 3:03:21initialized that's because it's possible
  3709. 3:03:23there's no matching
  3710. 3:03:24cases and we would be keeping our result
  3711. 3:03:27uninitialized so to clear that up when
  3712. 3:03:30we declare our variable we can also
  3713. 3:03:32assign it I'll just set it to be
  3714. 3:03:35zero let's perform a test
  3715. 3:03:40run enter the first number let's do
  3716. 3:03:433.14 enter an operator let's do addition
  3717. 3:03:46and the second number
  3718. 3:03:491.1 the result is
  3719. 3:03:524.24 let's do subtraction
  3720. 3:03:563.14 minus 1.1 which gives us
  3721. 3:04:022.04
  3722. 3:04:03multiplication
  3723. 3:04:063.14 times let's do
  3724. 3:04:08two
  3725. 3:04:126.28 then division 3.14
  3726. 3:04:18divided by
  3727. 3:04:20two that gives us
  3728. 3:04:231.57 then we have power what is
  3729. 3:04:273 to the power of
  3730. 3:04:31three that is
  3731. 3:04:3427 3 * 3 is 9 * 3 is 27 3 ^ of
  3732. 3:04:403 we have one potential issue though
  3733. 3:04:44what if somebody divides by zero here's
  3734. 3:04:47what happens
  3735. 3:04:48what is
  3736. 3:04:503.14 /
  3737. 3:04:530 we get the output of
  3738. 3:04:56infinity here's how we could prevent
  3739. 3:04:59that underneath the case where we do
  3740. 3:05:03division we'll write a few lines rather
  3741. 3:05:05than just one but we're going to enclose
  3742. 3:05:07them within a set of curly
  3743. 3:05:09braces then I'm going to cut this line
  3744. 3:05:12of
  3745. 3:05:13code within this case we'll write an if
  3746. 3:05:16statement we'll
  3747. 3:05:18check
  3748. 3:05:20if num 2 is equal to
  3749. 3:05:24zero that's because we can't normally
  3750. 3:05:26divide by zero if it is then let's print
  3751. 3:05:31cannot divide by
  3752. 3:05:39zero
  3753. 3:05:41else we'll assign our result of num one
  3754. 3:05:44/ by num 2 we'll execute the L statement
  3755. 3:05:48if num 2 doesn't equal zero because if
  3756. 3:05:51it did this would be true and we execute
  3757. 3:05:53this code instead let's try that
  3758. 3:05:57again what is
  3759. 3:06:003.14 /
  3760. 3:06:030 cannot divide by
  3761. 3:06:05zero however we still do display a
  3762. 3:06:08result and that is by using this print
  3763. 3:06:11statement what if somebody types in an
  3764. 3:06:13operator that doesn't exist 3.14
  3765. 3:06:18I'm going to type in pizza for the
  3766. 3:06:21operator type in I don't know
  3767. 3:06:23one well we still display a result so
  3768. 3:06:27let's modify this program let's display
  3769. 3:06:30a result only if we have a valid
  3770. 3:06:33operator because right now we don't
  3771. 3:06:35Pizza is not a valid
  3772. 3:06:40operator let's add a Boolean variable of
  3773. 3:06:44valid
  3774. 3:06:46operation I will set this to be true
  3775. 3:06:48from the
  3776. 3:06:52beginning if somebody attempts to divide
  3777. 3:06:54by zero I'm going to take our valid
  3778. 3:06:57operation variable and set it to be
  3779. 3:06:59false because we do not want to continue
  3780. 3:07:02and display the
  3781. 3:07:05result we're going to add a default case
  3782. 3:07:08the default case will be the
  3783. 3:07:12following so I'm going to write two
  3784. 3:07:14lines of code for the default
  3785. 3:07:16case let's
  3786. 3:07:19output invalid
  3787. 3:07:24operator then we will take our Boolean
  3788. 3:07:26variable of valid operation and set it
  3789. 3:07:29to be
  3790. 3:07:30false so this is what our switch looks
  3791. 3:07:33like feel free to pause the video if you
  3792. 3:07:36would like a moment to look it
  3793. 3:07:38over within our case of division we do
  3794. 3:07:41have a few lines of code same thing
  3795. 3:07:44applies with the default
  3796. 3:07:46case we're only going to print the
  3797. 3:07:49result if our Boolean of valid operation
  3798. 3:07:52is
  3799. 3:07:54true if valid operation is equal to true
  3800. 3:07:59but really we can just shorten this to
  3801. 3:08:01if valid
  3802. 3:08:04operation then we'll print the
  3803. 3:08:07result then we don't necessarily need an
  3804. 3:08:09else
  3805. 3:08:11statement because we're already printing
  3806. 3:08:13either invalid operator or cannot divide
  3807. 3:08:16by zero
  3808. 3:08:18okay let's run this one final
  3809. 3:08:20time
  3810. 3:08:233.14 plus
  3811. 3:08:252.1 is
  3812. 3:08:285.24
  3813. 3:08:313.14
  3814. 3:08:32minus 1.1 is
  3815. 3:08:392.04
  3816. 3:08:423.14 time
  3817. 3:08:452.1 is 6594
  3818. 3:08:494
  3819. 3:08:513.14 okay 3.14 / 0 Let's see what
  3820. 3:08:55happens cannot divide by zero and we do
  3821. 3:08:59not display the result because our
  3822. 3:09:01equation is invalid which is good we
  3823. 3:09:03don't want to display the result we're
  3824. 3:09:05displaying a type of error message
  3825. 3:09:09instead let's raise
  3826. 3:09:124 to the
  3827. 3:09:15power of two which gives us
  3828. 3:09:1816 now let's type in an operator that
  3829. 3:09:21doesn't exist
  3830. 3:09:253.14
  3831. 3:09:26Pizza
  3832. 3:09:2869 invalid operator that's because Pizza
  3833. 3:09:32is not a valid
  3834. 3:09:34operator all right everybody that is a
  3835. 3:09:36calculator program that you can write
  3836. 3:09:38the purpose of this program is to more
  3837. 3:09:40or less help us with enhanced switch
  3838. 3:09:42statements depending on the operator we
  3839. 3:09:44can perform one of a few operations
  3840. 3:09:47we've also learned that your cases can
  3841. 3:09:49take up more than one line if you would
  3842. 3:09:50like and well everybody that is a
  3843. 3:09:53calculator program in
  3844. 3:09:57Java yo yo yo in today's video I'm going
  3845. 3:10:00to explain logical operators in Java
  3846. 3:10:02logical operators they allow us to check
  3847. 3:10:05or modify more than one condition
  3848. 3:10:07there's and or not we'll discuss and
  3849. 3:10:11first which is represented by two Amper
  3850. 3:10:13Sands let's declare a variable of
  3851. 3:10:16temperature temp meaning temperature I
  3852. 3:10:19will set this to be 20 I would like to
  3853. 3:10:22check to see if my temperature Falls
  3854. 3:10:23within a certain range well to do that I
  3855. 3:10:26could use the and logical operator we
  3856. 3:10:29can check more than one
  3857. 3:10:31condition so first I would like to see
  3858. 3:10:33if my
  3859. 3:10:35temp is less than or equal to 30 30°
  3860. 3:10:40C and if the temp is greater than or
  3861. 3:10:45equal to zero
  3862. 3:10:48we have two conditions this time if this
  3863. 3:10:50is true and this is true then do this
  3864. 3:10:54code so let's output the
  3865. 3:10:57following if our temperature Falls
  3866. 3:11:00within this range we'll
  3867. 3:11:03output the weather is good and for fun
  3868. 3:11:07I'll let a smiley face cuz why not okay
  3869. 3:11:11let's see how this works the temperature
  3870. 3:11:14is currently 20 20° C
  3871. 3:11:18the weather is good if the temperature
  3872. 3:11:21were 40 40°
  3873. 3:11:24C well we don't do
  3874. 3:11:26anything this condition evaluated to be
  3875. 3:11:29false but this one is true our
  3876. 3:11:33temperature is greater than or equal to
  3877. 3:11:35zero that is true but this one is false
  3878. 3:11:38and using the and logical operator both
  3879. 3:11:41conditions must be true in order for
  3880. 3:11:43this entire statement to be true
  3881. 3:11:47since only one of them was true where
  3882. 3:11:49temp is greater than or equal to zero we
  3883. 3:11:52don't execute this code the and logical
  3884. 3:11:55operator is used to check more than one
  3885. 3:11:57condition we can add as many as we would
  3886. 3:11:59like to this time let's create a
  3887. 3:12:02variable of is sunny this will be a
  3888. 3:12:05Boolean Boolean is sunny is it sunny
  3889. 3:12:09outside I will set that to be
  3890. 3:12:13true not only are we going to check to
  3891. 3:12:15see if our temperature is within a
  3892. 3:12:16certain range we're also going to check
  3893. 3:12:20and is sunny is this true so now we are
  3894. 3:12:25linking three different conditions all
  3895. 3:12:28three must be true so if all three are
  3896. 3:12:31true we'll outut the following it is
  3897. 3:12:35sunny
  3898. 3:12:42outside let's set is sunny to be false
  3899. 3:12:46and then we are going to set our
  3900. 3:12:47temperature to not
  3901. 3:12:49240
  3902. 3:12:5320 these two conditions are true but
  3903. 3:12:57this one is false so we don't do any of
  3904. 3:12:59it we skip over it with the and logical
  3905. 3:13:02operator all conditions must be
  3906. 3:13:05true if is sunny we're true
  3907. 3:13:09then well then we execute this code the
  3908. 3:13:13temperature Falls within this range this
  3909. 3:13:15is true and this this is true and this
  3910. 3:13:18is true all three conditions are true so
  3911. 3:13:21we execute this code the weather is good
  3912. 3:13:24and it is sunny
  3913. 3:13:26outside we check more than one condition
  3914. 3:13:29and both must be true there's also the
  3915. 3:13:32not logical operator it's a little
  3916. 3:13:34different how it works is that it gives
  3917. 3:13:36you the opposite you can check to see if
  3918. 3:13:38something is not true let's add an elive
  3919. 3:13:43statement we're going to copy these
  3920. 3:13:46three conditions but make one change
  3921. 3:13:49we're going to precede is Sunny with the
  3922. 3:13:51not logical operator represented by an
  3923. 3:13:54exclamation
  3924. 3:13:55point it checks the opposite we are
  3925. 3:13:58checking to see if something is not true
  3926. 3:14:02is our temperature less than or equal to
  3927. 3:14:0430 and greater than or equal to zero and
  3928. 3:14:08is it not
  3929. 3:14:10Sunny so if something is normally false
  3930. 3:14:13using the not logical operator it
  3931. 3:14:15becomes true if it's normally true it
  3932. 3:14:18becomes false it gives you the opposite
  3933. 3:14:20for a Boolean value
  3934. 3:14:23basically if the weather is good but
  3935. 3:14:26it's not Sunny that must mean it's
  3936. 3:14:28cloudy it is cloudy outside then I'll
  3937. 3:14:33add an emoji of a
  3938. 3:14:38cloud the temperature is good but we'll
  3939. 3:14:42set is sunny to be
  3940. 3:14:45false the weather is
  3941. 3:14:48good but it is cloudy
  3942. 3:14:50outside the temperature Falls within
  3943. 3:14:52this
  3944. 3:14:53range and it is not
  3945. 3:14:56Sunny the not logical operator checks to
  3946. 3:15:00see if something is not true then we
  3947. 3:15:03have the or logical operator represented
  3948. 3:15:05by two vertical bars or is similar to
  3949. 3:15:08the and logical operator however only
  3950. 3:15:11one condition needs to be true that are
  3951. 3:15:13linked by the or logical operator with
  3952. 3:15:16and both must be true let's say our
  3953. 3:15:18temperature is really cold it is -10°
  3954. 3:15:22C but it is sunny
  3955. 3:15:26outside we actually don't end up doing
  3956. 3:15:28anything we skip over the if statement
  3957. 3:15:31and the else if statement let's add
  3958. 3:15:33another LF
  3959. 3:15:36statement we're going to check to see if
  3960. 3:15:38our temperature is really hot or really
  3961. 3:15:42cold we will check if our temp is
  3962. 3:15:45greater than 30
  3963. 3:15:47or using two vertical bars temp is less
  3964. 3:15:52than
  3965. 3:15:53zero then we'll
  3966. 3:15:56output the weather is
  3967. 3:16:03bad if our temperature is
  3968. 3:16:07-10 the weather is bad with the or
  3969. 3:16:10logical operator at least one condition
  3970. 3:16:13must be true this condition was false
  3971. 3:16:16but this one was true so we execute this
  3972. 3:16:19code because using the or logical
  3973. 3:16:21operator at least one condition needs to
  3974. 3:16:23be
  3975. 3:16:25true or if the temperature was 35 35°
  3976. 3:16:31C this time this condition was true but
  3977. 3:16:34this one was false so we execute this
  3978. 3:16:38code all right let's go over another
  3979. 3:16:40example we're going to use these logical
  3980. 3:16:43operators to validate a username when
  3981. 3:16:45somebody tries to set a
  3982. 3:16:47username so in this project we'll need a
  3983. 3:16:50scanner scanner scanner equals new
  3984. 3:16:54scanner we're going to type
  3985. 3:16:58system.in then import this
  3986. 3:17:00class then be sure to close your scanner
  3987. 3:17:03at the end of the file scanner.
  3988. 3:17:08close our username has a few
  3989. 3:17:12rules I'll add these as comments
  3990. 3:17:14username must be between 4 through 12
  3991. 3:17:21characters username must
  3992. 3:17:25not
  3993. 3:17:26contain
  3994. 3:17:28spaces or
  3995. 3:17:32underscores we'll create a string
  3996. 3:17:34variable of
  3997. 3:17:36username we'll create a prompt to have a
  3998. 3:17:38user enter in a
  3999. 3:17:41username enter your new
  4000. 3:17:45username I'll use print instead of print
  4001. 3:17:48line we will assign our username using
  4002. 3:17:51the scanner username equals scanner.
  4003. 3:17:55next
  4004. 3:17:59line we'll write a condition to check if
  4005. 3:18:02our username is between 4 to 12
  4006. 3:18:04characters we're going to check if our
  4007. 3:18:07username is under four characters or
  4008. 3:18:10greater than 12 characters we'll check
  4009. 3:18:12that within an if
  4010. 3:18:13statement if our username
  4011. 3:18:17use the length method to return the
  4012. 3:18:19length of this string if the length of
  4013. 3:18:22our username is less than four use the
  4014. 3:18:25or logical operator or our
  4015. 3:18:28username the length of it using the
  4016. 3:18:30length method is greater than 12 that
  4017. 3:18:34means our username is too short or too
  4018. 3:18:36long we'll output the following username
  4019. 3:18:40must be between 4 through 12 characters
  4020. 3:18:47let's perform a test
  4021. 3:18:49run enter your new username I'll type in
  4022. 3:18:52something that's less than four
  4023. 3:18:54characters username must be between 4 to
  4024. 3:18:5712
  4025. 3:18:58characters let's type in a really long
  4026. 3:19:01username I'll just make up something
  4027. 3:19:04username must be between 4 to 12
  4028. 3:19:07characters let's type in something
  4029. 3:19:09actually within that
  4030. 3:19:14range oh and then we need to add an lse
  4031. 3:19:16state statement because I
  4032. 3:19:18forgot else we will
  4033. 3:19:22print
  4034. 3:19:24welcome plus
  4035. 3:19:28username enter your new
  4036. 3:19:32username welcome whatever your username
  4037. 3:19:36is the second part of this assignment is
  4038. 3:19:39that we need to check to make sure that
  4039. 3:19:41there are no spaces or underscores
  4040. 3:19:43within our username after the if
  4041. 3:19:45statement We'll add an L if statement to
  4042. 3:19:47check
  4043. 3:19:49that using the contains method of
  4044. 3:19:52strings we can check if a string
  4045. 3:19:54contains a certain character with our
  4046. 3:19:56username we will use the contains method
  4047. 3:20:00does our username contain any
  4048. 3:20:04spaces or does our
  4049. 3:20:07username
  4050. 3:20:09contains any
  4051. 3:20:11underscores if so we'll output the
  4052. 3:20:15following user usame must
  4053. 3:20:18not contain spaces or
  4054. 3:20:26underscores let's perform another test
  4055. 3:20:28run type in your first and last name
  4056. 3:20:30including a space username must not
  4057. 3:20:33contain spaces or
  4058. 3:20:36underscores this time I will use an
  4059. 3:20:38underscore to separate the first and
  4060. 3:20:40last
  4061. 3:20:41name username must not contain spaces or
  4062. 3:20:44underscores let's type in a usern name
  4063. 3:20:46that follows the
  4064. 3:20:48rules that seems to
  4065. 3:20:50work so that is the or logical operator
  4066. 3:20:54if at least one condition is true then
  4067. 3:20:56the entire statement is true and we
  4068. 3:20:59execute it all right everybody so those
  4069. 3:21:01are logical operators and checks more
  4070. 3:21:04than one condition both must be true or
  4071. 3:21:07checks more than one condition at least
  4072. 3:21:09one needs to be true not not is a little
  4073. 3:21:12different it checks if something is not
  4074. 3:21:15true basically does the inverse and well
  4075. 3:21:18everybody those are logical operators in
  4076. 3:21:23Java what is going on everybody in this
  4077. 3:21:26video I need to explain while Loops in
  4078. 3:21:28Java if I could summarize a while loop
  4079. 3:21:30in one sentence a while loop will repeat
  4080. 3:21:32some code Forever while some condition
  4081. 3:21:36remains true first I'll give you a
  4082. 3:21:37demonstration using an if statement then
  4083. 3:21:39we'll convert it to be a while loop and
  4084. 3:21:41we'll see how it's useful we'll need to
  4085. 3:21:43accept user input in this demonstration
  4086. 3:21:46I will create a scanner I'm pretty sure
  4087. 3:21:48we know how to create one by
  4088. 3:21:50now be assured to import the scanner
  4089. 3:21:52class import java.util.scanner then be
  4090. 3:21:56sure to close your scanner because I
  4091. 3:21:58always forget to do
  4092. 3:21:59that all right we are going to create a
  4093. 3:22:03string of
  4094. 3:22:05name I'll use an if statement to check
  4095. 3:22:07to see if my name is
  4096. 3:22:09empty if name use the is empty method
  4097. 3:22:14now Java wants us to assign this name
  4098. 3:22:16right away okay so I'm going to go ahead
  4099. 3:22:17and initialize it we can initialize it
  4100. 3:22:20with an empty space if our name is empty
  4101. 3:22:23which it will be we will prompt the user
  4102. 3:22:25to enter your name I'll use print
  4103. 3:22:28instead of print line to keep it on the
  4104. 3:22:30same line then we will assign our
  4105. 3:22:33variable of name name equals scanner.
  4106. 3:22:37next
  4107. 3:22:39line after we escape the if statement
  4108. 3:22:42we'll output our name let's say hello
  4109. 3:22:46Plus
  4110. 3:22:47name so this program will work but
  4111. 3:22:51there's one issue what if I don't type
  4112. 3:22:52in anything when I enter my name I'll
  4113. 3:22:54just hit
  4114. 3:22:56enter well we still continue with the
  4115. 3:22:58rest of the program the output is hello
  4116. 3:23:01we don't display a name because we
  4117. 3:23:03didn't type in anything how can we
  4118. 3:23:05prevent people from doing stuff like
  4119. 3:23:06this skipping
  4120. 3:23:08prompts well we could use a while loop
  4121. 3:23:12rather than an if statement replace if
  4122. 3:23:15with while
  4123. 3:23:17what we're doing is while this condition
  4124. 3:23:20remains true continue this code Forever
  4125. 3:23:24Until this condition is no longer true
  4126. 3:23:28let's try that
  4127. 3:23:30again enter your name I'm just going to
  4128. 3:23:32hit enter enter your name no enter your
  4129. 3:23:35name no enter your name no okay I give
  4130. 3:23:39up I'll type in my name then we escape
  4131. 3:23:41the while loop hello your
  4132. 3:23:44name so that's why a while loop Loop is
  4133. 3:23:46useful we can repeat some code possibly
  4134. 3:23:49Forever at times we may not want a user
  4135. 3:23:51to continue without doing something a
  4136. 3:23:54while loop would be really helpful for
  4137. 3:23:55that they would be stuck until this
  4138. 3:23:57condition is no longer true at the end
  4139. 3:24:00of your while loop we go back to the
  4140. 3:24:01beginning to check the condition if the
  4141. 3:24:03condition is no longer true then we
  4142. 3:24:05escape the while loop and move on with
  4143. 3:24:07the rest of the program there's one
  4144. 3:24:09thing you should be cautious of though
  4145. 3:24:11and that is an infinite Loop let me give
  4146. 3:24:12you a
  4147. 3:24:15demonstration with our while loop if we
  4148. 3:24:17have a condition that we can't change
  4149. 3:24:19from within the loop that's called an
  4150. 3:24:21infinite Loop so let's say while the
  4151. 3:24:23number one is equal to 1 even my IDE
  4152. 3:24:27intellig is giving me a warning that 1
  4153. 3:24:30is equal to 1 is not updated inside the
  4154. 3:24:33loop I would not recommend following me
  4155. 3:24:35along for this part your computer might
  4156. 3:24:37freeze up but I'm going to print
  4157. 3:24:40help I'm stuck in a loop
  4158. 3:24:46within this while loop I have no way to
  4159. 3:24:48change it from inside of it so we're
  4160. 3:24:50going to be stuck in this while loop
  4161. 3:24:52forever here's a
  4162. 3:24:55demonstration we're just going to print
  4163. 3:24:57this code
  4164. 3:24:59forever let's check back later one
  4165. 3:25:03eternity later it's still going we're
  4166. 3:25:06stuck in a loop this may happen to you
  4167. 3:25:08if you have some condition that you
  4168. 3:25:10can't change within your while loop
  4169. 3:25:12you'll need some way to update your
  4170. 3:25:13condition
  4171. 3:25:16let me give you another example again
  4172. 3:25:18we'll need our
  4173. 3:25:27scanner we're going to pretend that
  4174. 3:25:29we're playing a game for a user to quit
  4175. 3:25:31they have to press the q key we will
  4176. 3:25:33declare a string of
  4177. 3:25:36response we'll use a while
  4178. 3:25:38loop now for my condition for us to be
  4179. 3:25:41able to escape the while
  4180. 3:25:43loop our response I'm going to use the
  4181. 3:25:46not logical
  4182. 3:25:49operator
  4183. 3:25:50equals a capital Q and this will be a
  4184. 3:25:54string so while our response does not
  4185. 3:25:58equal Q we continue the loop forever in
  4186. 3:26:02order for somebody to escape this game
  4187. 3:26:04they have to press Q to quit Java wants
  4188. 3:26:07me to assign this variable right away
  4189. 3:26:08and initialize it so I'll do so we don't
  4190. 3:26:12want our variable to be empty when we
  4191. 3:26:14use it to check a condition
  4192. 3:26:17then intellig is giving us another
  4193. 3:26:19warning that our response is not updated
  4194. 3:26:21inside the loop if we don't update it
  4195. 3:26:23we'll be stuck in an infinite
  4196. 3:26:25Loop let's pretend that we're playing a
  4197. 3:26:28game you are playing a
  4198. 3:26:35game then we'll
  4199. 3:26:37output
  4200. 3:26:38press Q to quit
  4201. 3:26:46then we will need to update our response
  4202. 3:26:49response equals use our scanner for
  4203. 3:26:52Single Character we can use
  4204. 3:26:55next in case somebody types in a
  4205. 3:26:57lowercase que we'll follow this method
  4206. 3:27:00with the two uppercase
  4207. 3:27:03method if a user was to type in a
  4208. 3:27:05lowercase Q we'll convert it to be an
  4209. 3:27:07uppercase q so that it
  4210. 3:27:10matches once we escape the while loop
  4211. 3:27:13we'll
  4212. 3:27:14print you have have quit the
  4213. 3:27:20game okay let's perform a test
  4214. 3:27:23run you are playing a game press Q to
  4215. 3:27:26quit no
  4216. 3:27:28yes
  4217. 3:27:30okay as long as our response does not
  4218. 3:27:33equal Q we are still playing our
  4219. 3:27:36imaginary
  4220. 3:27:43game in order for us to quit to escape
  4221. 3:27:46the while loop we have to press Q to
  4222. 3:27:48quit I will press q and we escape the
  4223. 3:27:51loop you have quit the
  4224. 3:27:54game while our response using the not
  4225. 3:27:57logical operator while our response does
  4226. 3:28:00not equal Q continue the loop forever
  4227. 3:28:04let's go over another example we'll ask
  4228. 3:28:06a user for their
  4229. 3:28:09age int age I will set this to be zero
  4230. 3:28:12right
  4231. 3:28:14away we'll prompt a user to enter in
  4232. 3:28:16their age enter your
  4233. 3:28:20age we will assign our age
  4234. 3:28:24variable AG equal scanner. next
  4235. 3:28:30int then at the end of this program I
  4236. 3:28:32will
  4237. 3:28:33print you are our variable
  4238. 3:28:38age plus years
  4239. 3:28:43old let's say I would like to prevent
  4240. 3:28:45somebody from typing in a negative
  4241. 3:28:47number like -1 you are ne1 years old
  4242. 3:28:51well we could use a while loop to
  4243. 3:28:52reprompt the user before displaying the
  4244. 3:28:56result so after we assign our age once
  4245. 3:28:59let's use a while
  4246. 3:29:01loop while age is less than zero we'll
  4247. 3:29:07output the
  4248. 3:29:10following your age can't be negative
  4249. 3:29:16then we'll reprompt the user again we
  4250. 3:29:18can just copy these two lines and then
  4251. 3:29:20paste them within the while loop we'll
  4252. 3:29:23ask the user to enter in their age again
  4253. 3:29:25and reassign
  4254. 3:29:27it if we keep on typing negative
  4255. 3:29:30numbers we can't escape the while
  4256. 3:29:33loop we're stuck until we type in
  4257. 3:29:35something that's
  4258. 3:29:39valid you are 21 years old that's a
  4259. 3:29:42valid
  4260. 3:29:43number from the beginning if I type in
  4261. 3:29:46something that's valid we don't enter
  4262. 3:29:48the while loop at all it is possible to
  4263. 3:29:51skip over
  4264. 3:29:52it we check the condition of a while
  4265. 3:29:54loop before entering it if we type in a
  4266. 3:29:58number that's valid let's say ages
  4267. 3:30:0125 we check the condition it's false
  4268. 3:30:04from the beginning so we don't enter the
  4269. 3:30:06while loop at all we skip over it and go
  4270. 3:30:09straight to the end in this program we
  4271. 3:30:12only get caught in the while loop if we
  4272. 3:30:14type in user input that's not
  4273. 3:30:16valid there's a variation of the while
  4274. 3:30:19loop known as a do while loop we do some
  4275. 3:30:21code first and then check the
  4276. 3:30:24condition if I was to convert this code
  4277. 3:30:26to be a do while loop I would cut the
  4278. 3:30:29while part do this code then check the
  4279. 3:30:33condition at the
  4280. 3:30:34end this is how the program would run
  4281. 3:30:37now it's going to be a little bit
  4282. 3:30:41different your age can't be negative
  4283. 3:30:43enter your age I am years old your age
  4284. 3:30:47can't be negative enter your age you are
  4285. 3:30:50whatever your age is years old so with
  4286. 3:30:54the do while loop we do this code first
  4287. 3:30:57and then check the condition at the end
  4288. 3:30:59where it's different compared to a
  4289. 3:31:00standard while loop is that with the
  4290. 3:31:03standard while
  4291. 3:31:04loop you may not enter the while loop at
  4292. 3:31:07all if this condition is false but with
  4293. 3:31:10the do while loop you always do this
  4294. 3:31:13code at least once and then check the
  4295. 3:31:15condition at the end let's go over one
  4296. 3:31:18last example with the do while loop
  4297. 3:31:21we'll prompt a user to type in a number
  4298. 3:31:23that's between a certain range we will
  4299. 3:31:25have int number and I will assign this
  4300. 3:31:28to be zero right
  4301. 3:31:30away let's use a basic while loop
  4302. 3:31:33first
  4303. 3:31:35while our number is less than one we'll
  4304. 3:31:39use the or logical operator to check or
  4305. 3:31:43if our number is greater than 10
  4306. 3:31:46then do
  4307. 3:31:49this prompt the user to enter a
  4308. 3:31:53number between 1 through
  4309. 3:32:0110 then we will assign our variable of
  4310. 3:32:03number equals use our scanner use the
  4311. 3:32:07next int
  4312. 3:32:10method and then at the end we will print
  4313. 3:32:12our
  4314. 3:32:13number you picked
  4315. 3:32:18plus
  4316. 3:32:21number so let's run this our number is
  4317. 3:32:23set to
  4318. 3:32:26zero enter a number between 1 and 10
  4319. 3:32:29let's do negative
  4320. 3:32:301 11 -2 one
  4321. 3:32:36kajillion I think I broke it okay I'll
  4322. 3:32:39type in something that's valid like five
  4323. 3:32:41you picked
  4324. 3:32:44five rather than check the the condition
  4325. 3:32:46from the beginning we could check it at
  4326. 3:32:48the end so let's convert the while loop
  4327. 3:32:51to a do while loop do this code once and
  4328. 3:32:55then check the
  4329. 3:32:59condition so this would work
  4330. 3:33:07similarly so do this code once check the
  4331. 3:33:11condition at the
  4332. 3:33:12end all right everybody so those are
  4333. 3:33:14while Loops they allow you to execute
  4334. 3:33:16some code possibly forever while some
  4335. 3:33:19condition remains true they're really
  4336. 3:33:22useful when accepting user input because
  4337. 3:33:24the user might not type in something
  4338. 3:33:26that's valid and you have to keep on
  4339. 3:33:27repr prompting them that's one good use
  4340. 3:33:30there's two variations of while Loops a
  4341. 3:33:33standard while loop and a do while
  4342. 3:33:37loop it depends if you want to check the
  4343. 3:33:39condition before entering the loop and
  4344. 3:33:41well everybody those are while Loops in
  4345. 3:33:44Java
  4346. 3:33:47hello everybody in today's video we're
  4347. 3:33:50going to create a number guessing game
  4348. 3:33:51using Java let's get started in this
  4349. 3:33:54program we'll be using random numbers
  4350. 3:33:56we'll need to create a random object
  4351. 3:33:59random random equals new
  4352. 3:34:03random then import this class so at the
  4353. 3:34:07top of our Java file import
  4354. 3:34:09java.util random this random object can
  4355. 3:34:13Generate random numbers for us we'll
  4356. 3:34:16also accept user input we'll need a
  4357. 3:34:18scanner scanner scanner equals new
  4358. 3:34:23scanner then within the set of
  4359. 3:34:25parentheses type
  4360. 3:34:26system.in then import this class as well
  4361. 3:34:30import
  4362. 3:34:31java.util.scanner we have a random
  4363. 3:34:34object to Generate random numbers and a
  4364. 3:34:36scanner object to accept user
  4365. 3:34:39input what are the different variables
  4366. 3:34:41we'll need we'll need the following an
  4367. 3:34:45integer to hold our
  4368. 3:34:47guess so let's say we're going to guess
  4369. 3:34:49a random number between 1 and 100 we'll
  4370. 3:34:52use the scanner to accept an integer and
  4371. 3:34:55place it within this variable of
  4372. 3:34:57guess for example the guess might be
  4373. 3:35:0050 we need to keep track of the amount
  4374. 3:35:02of attempts it took a user to get the
  4375. 3:35:04correct number int attempts we'll assign
  4376. 3:35:09these
  4377. 3:35:10later and we'll need a random number int
  4378. 3:35:13random number
  4379. 3:35:16let's go ahead and assign this right
  4380. 3:35:18away to generate a random number we're
  4381. 3:35:20going to use our random
  4382. 3:35:22object use the next int method of a
  4383. 3:35:26random object within this method we can
  4384. 3:35:29list two numbers a range if I would like
  4385. 3:35:32a number between let's say 1 and 10 my
  4386. 3:35:36two numbers would be 1 comma 11 the
  4387. 3:35:39first number is inclusive the second
  4388. 3:35:42number is
  4389. 3:35:43exclusive let's perform a test run of a
  4390. 3:35:45random number I just want to be sure
  4391. 3:35:47that it's generating correctly when
  4392. 3:35:50writing a program it is a good idea to
  4393. 3:35:51test your code every once in a while
  4394. 3:35:53just to be sure that everything is
  4395. 3:35:56working so we should get random numbers
  4396. 3:35:58between 1 and 10 1 10 5 8 good enough a
  4397. 3:36:07random number stores a random number
  4398. 3:36:09after generating
  4399. 3:36:12it let's create a welcome message let's
  4400. 3:36:15say number guessing
  4401. 3:36:19game we will prompt the
  4402. 3:36:23user guess a
  4403. 3:36:25number
  4404. 3:36:28between let's start with 1 through 10
  4405. 3:36:32later on we'll increase it to
  4406. 3:36:38100 now we'll need a while loop but not
  4407. 3:36:41just any while loop we'll create a do
  4408. 3:36:43while loop so do this code once while
  4409. 3:36:48this condition remains true and let me
  4410. 3:36:50zoom in a little
  4411. 3:36:52bit so what's our condition going to be
  4412. 3:36:55we're going to continue playing this
  4413. 3:36:57game while our
  4414. 3:37:00guess does not equal our random number
  4415. 3:37:05we'll keep on playing until we get the
  4416. 3:37:06random number exclamation point equals
  4417. 3:37:09means not equal so if our guess does not
  4418. 3:37:13equal our random number this return
  4419. 3:37:15turns true then we want to keep playing
  4420. 3:37:18while our guess doesn't equal the random
  4421. 3:37:21number within this do while
  4422. 3:37:24loop will prompt the user to enter in
  4423. 3:37:27their guess I'm going to use print line
  4424. 3:37:30instead of print for guess a number
  4425. 3:37:31between 1 and
  4426. 3:37:3310 now we'll prompt the user to enter a
  4427. 3:37:36number enter a guess enter a
  4428. 3:37:40guess for this I'll use print instead of
  4429. 3:37:43print line we will assign Our Guest
  4430. 3:37:48variable equal to use our scanner use
  4431. 3:37:52the next int
  4432. 3:37:56method we're also keeping track the
  4433. 3:37:58amount of attempts it takes a user to
  4434. 3:38:00get the correct
  4435. 3:38:01number so we can increment attempts by
  4436. 3:38:04adding
  4437. 3:38:07Plus+ and let's go ahead and initialize
  4438. 3:38:10attempts we'll set that to be zero
  4439. 3:38:15temporarily outside the while loop I'm
  4440. 3:38:18going to
  4441. 3:38:20print you have
  4442. 3:38:23one my random number right now is only
  4443. 3:38:25between 1 and 10 I'm just going to keep
  4444. 3:38:28guessing until we get the right
  4445. 3:38:33number so I'll just start with one and
  4446. 3:38:35go all the way to
  4447. 3:38:4210 the answer was nine we escaped the
  4448. 3:38:45wild
  4449. 3:38:46Loop so we know that this is working
  4450. 3:38:49currently one thing this program is
  4451. 3:38:51missing is that we'll want to give a
  4452. 3:38:53user a clue if their guess is too low or
  4453. 3:38:57too high because we have no way to
  4454. 3:38:59tell I'm just typing in random numbers
  4455. 3:39:02and I have no clue how close I am to the
  4456. 3:39:04actual number that we need to
  4457. 3:39:07guess let's get rid of this print
  4458. 3:39:10statement we were using it for testing
  4459. 3:39:12purposes within the do while loop we'll
  4460. 3:39:15write an if statement we'll check if our
  4461. 3:39:19guess is less than our random
  4462. 3:39:23number that means we guess too
  4463. 3:39:26low if that's the case we'll output the
  4464. 3:39:30following two low try
  4465. 3:39:39again else
  4466. 3:39:41if our guess is greater than our random
  4467. 3:39:46number that means we guess too
  4468. 3:39:49high too high try
  4469. 3:39:54again if our guess isn't lower than our
  4470. 3:39:56random number and our guess isn't
  4471. 3:39:59greater than our random number that
  4472. 3:40:01means our guess must equal the random
  4473. 3:40:04number so if that's the case we would be
  4474. 3:40:07executing this lse statement because the
  4475. 3:40:09preceding two conditions would be false
  4476. 3:40:12that means we have the right answer the
  4477. 3:40:14right guess so we'll output the
  4478. 3:40:18following
  4479. 3:40:22correct the let me zoom
  4480. 3:40:25out the number
  4481. 3:40:28was plus our random
  4482. 3:40:36number and then we'll display the amount
  4483. 3:40:38of attempts it
  4484. 3:40:40took number of attempts
  4485. 3:40:46plus our variable of attempts after each
  4486. 3:40:49guess we increment it by
  4487. 3:40:52one the last thing I'm forgetting is to
  4488. 3:40:55close my scanner because I always forget
  4489. 3:40:58to close
  4490. 3:40:59it scanner.
  4491. 3:41:02close okay let's perform a test run
  4492. 3:41:06we're only guessing a number between 1
  4493. 3:41:07and 10
  4494. 3:41:09currently I'll guess something right in
  4495. 3:41:11the middle five too low try again the
  4496. 3:41:15random number we have to guess should be
  4497. 3:41:17between 5 and 10 then I'll go with seven
  4498. 3:41:20too low 8 too low nine too low okay it
  4499. 3:41:26must be 10 then correct the number was
  4500. 3:41:2910 number of attempts it took
  4501. 3:41:32five we're going to improve this code
  4502. 3:41:35now what if we would like the numbers 1
  4503. 3:41:37through 100 this time well we could make
  4504. 3:41:40this program a little more flexible
  4505. 3:41:43instead of hardcoding these numbers
  4506. 3:41:45within the next integer method we'll
  4507. 3:41:47Place some variables here instead we
  4508. 3:41:50will create int Min equals whatever you
  4509. 3:41:53want the minimum to be let's say one int
  4510. 3:41:56Max I will set the max to be 100 let's
  4511. 3:42:00replace these two
  4512. 3:42:01numbers the first one will be Min comma
  4513. 3:42:06Max so these variables behave as if they
  4514. 3:42:09were these numbers within this range the
  4515. 3:42:12second number is exclusive so TW include
  4516. 3:42:15100 I'm going to add +
  4517. 3:42:18one give me the numbers between 1 and
  4518. 3:42:22100 within this print statement let's
  4519. 3:42:25use print F
  4520. 3:42:28instead we'll replace these numbers with
  4521. 3:42:31the format specifier a placeholder we
  4522. 3:42:35will display an integer so we need D
  4523. 3:42:38let's do the same thing with 10
  4524. 3:42:43d uh let's do comma Min comma
  4525. 3:42:49Max and then add a new line at the
  4526. 3:42:53end let's do one final
  4527. 3:42:56run so now we're guessing a number
  4528. 3:42:58between one and 100 I'll guess something
  4529. 3:43:01right in the middle too high try again
  4530. 3:43:04our numberers going to be between 1 and
  4531. 3:43:0650 then 25 too high 12 too high 6 too
  4532. 3:43:13high three
  4533. 3:43:16too low between three and six four
  4534. 3:43:20correct the number was four the number
  4535. 3:43:22of attempts it took six
  4536. 3:43:24attempts all right everybody so that is
  4537. 3:43:26a number guessing game that you can
  4538. 3:43:28create using
  4539. 3:43:32Java hi there everybody so in this video
  4540. 3:43:34I'm going to explain four Loops in Java
  4541. 3:43:37a four Loop is similar to a while loop
  4542. 3:43:39however a for Loop executes some code a
  4543. 3:43:43certain amount of times a for Loop is
  4544. 3:43:45different from a while loop a while loop
  4545. 3:43:48could execute an infinite amount of
  4546. 3:43:49times until its condition is no longer
  4547. 3:43:52true so with for Loops we want to do
  4548. 3:43:54something a certain amount of times to
  4549. 3:43:56create a for Loop type four parentheses
  4550. 3:43:59curly braces now within the condition of
  4551. 3:44:02the for Loop there's three statements
  4552. 3:44:05each is separated with a
  4553. 3:44:07semicolon we have statement one
  4554. 3:44:10statement two and statement three the
  4555. 3:44:13first statement is used for ini ization
  4556. 3:44:16we can create a counter to keep track of
  4557. 3:44:18how many times we have iterated this
  4558. 3:44:20Loop basically we're declaring a
  4559. 3:44:22variable a counter this will be int now
  4560. 3:44:26a common practice for a for Loop is to
  4561. 3:44:28create a counter named I meaning index
  4562. 3:44:31in I equal Z or some other number I is
  4563. 3:44:35going to be used as a counter within
  4564. 3:44:38this Loop also known as a loop control
  4565. 3:44:41variable the second statement is a
  4566. 3:44:44condition when do we want to
  4567. 3:44:46stop maybe we would like to execute some
  4568. 3:44:49code 10 times we'll continue this loop
  4569. 3:44:52as long as I is less than
  4570. 3:44:5510 now the third statement the third
  4571. 3:44:58statement is the step we can increment
  4572. 3:45:01our counter of I by One or another
  4573. 3:45:03number or even decrement after each
  4574. 3:45:06iteration let's increase I by One
  4575. 3:45:10i++ let's perform a test run I'm just
  4576. 3:45:13going to print the word
  4577. 3:45:18pizza so we should execute this code 10
  4578. 3:45:21times using this for Loop 1 2 3 4 5 6 7
  4579. 3:45:278 9
  4580. 3:45:2910 so a for Loop is good if you want to
  4581. 3:45:32repeat some code a certain amount of
  4582. 3:45:35times but there's a little bit of setup
  4583. 3:45:37you have to do we'll need some sort of
  4584. 3:45:39index or counter a condition in which
  4585. 3:45:42you want to stop and you'll need to
  4586. 3:45:44update the counter
  4587. 3:45:45one way or another
  4588. 3:45:48initialization condition update we can
  4589. 3:45:52also print this index of I rather than
  4590. 3:45:55printing the word Pizza let's print I to
  4591. 3:45:58see what we're working with during the
  4592. 3:46:00first iteration I is zero after our Loop
  4593. 3:46:04is complete we increment I by One our
  4594. 3:46:07counter so we have 0 1 2 3 4 5 6 7 8 9
  4595. 3:46:13We're looping 10 times
  4596. 3:46:15but we set I to be zero to begin with if
  4597. 3:46:18I were to set I to be 1 and I want to
  4598. 3:46:21iterate 10 times I could say while I is
  4599. 3:46:24less than or equal to
  4600. 3:46:2710 so now we're starting at 1 and
  4601. 3:46:30counting up to
  4602. 3:46:3210 we can even decrement
  4603. 3:46:35to let's set I to be 10 we'll continue
  4604. 3:46:38the loop as long as I is greater than
  4605. 3:46:42zero to decrement we'll set I to be
  4606. 3:46:45minus
  4607. 3:46:48minus now we're starting at 10 and
  4608. 3:46:53decrementing but we're still looping 10
  4609. 3:46:56times let's go back to the
  4610. 3:47:01beginning using the update statement we
  4611. 3:47:04can even increment by a certain number
  4612. 3:47:06instead of incrementing by one let's
  4613. 3:47:08increment by two and see what
  4614. 3:47:11happens so this time we start at one and
  4615. 3:47:15update our counter of I two each time or
  4616. 3:47:19even three i+ equal
  4617. 3:47:233 1 4 7
  4618. 3:47:2710 we can decrement by a given
  4619. 3:47:30number let's set I to be 10 we'll
  4620. 3:47:32continue this loop as long as I is
  4621. 3:47:35greater than zero and we will decrement
  4622. 3:47:38our step we will decrement by 2 i - = 2
  4623. 3:47:45now we're starting at 10 and
  4624. 3:47:47decrementing by two or even three IUS =
  4625. 3:47:543
  4626. 3:47:5710741 for this next example we're going
  4627. 3:48:00to accept user input again we'll need a
  4628. 3:48:10scanner import the scanner class and
  4629. 3:48:13close your scanner at the end
  4630. 3:48:20we'll create a
  4631. 3:48:22prompt enter how many times you want to
  4632. 3:48:31Loop I'll create a variable of Max Max
  4633. 3:48:36will equal use our scanner use the next
  4634. 3:48:40int
  4635. 3:48:41method and now we're going to create a
  4636. 3:48:43loop a for Loop
  4637. 3:48:47again there's three
  4638. 3:48:51statements each separated with a
  4639. 3:48:54semicolon we'll need an index or counter
  4640. 3:48:56to keep track of how many times we have
  4641. 3:48:59looped let's say in I equals
  4642. 3:49:030 for the next statement what's our
  4643. 3:49:06condition we want to continue as long as
  4644. 3:49:08I is less than the max I is less than
  4645. 3:49:13our Max variable
  4646. 3:49:15then increment I by
  4647. 3:49:19one then we'll print I or something
  4648. 3:49:25else let's try this how many times do
  4649. 3:49:28you want to Loop let's Loop five
  4650. 3:49:32times 1 2 3 4
  4651. 3:49:365 again we set I to be zero so that's
  4652. 3:49:39why we're beginning with
  4653. 3:49:42zero if you would rather begin with one
  4654. 3:49:44we can set I to be 1 then the condition
  4655. 3:49:47would be I is less than or equal to Max
  4656. 3:49:51let's try that again let's Loop six
  4657. 3:49:55times 1 2 3 4 5
  4658. 3:49:596 all right now we're going to create a
  4659. 3:50:01mini
  4660. 3:50:02project we'll create a program to
  4661. 3:50:05simulate a
  4662. 3:50:08countdown perhaps we have a variable of
  4663. 3:50:11int
  4664. 3:50:12start let's start at 10 we're counting
  4665. 3:50:15down from
  4666. 3:50:1710 then we'll create a for
  4667. 3:50:19Loop we will set our index of I equal to
  4668. 3:50:23our
  4669. 3:50:24start our start variable behaves as if
  4670. 3:50:27it was the number
  4671. 3:50:2910 we'll continue as long as I is
  4672. 3:50:32greater than zero then we will decrement
  4673. 3:50:35by one with IUS
  4674. 3:50:37minus during each iteration let's just
  4675. 3:50:41print
  4676. 3:50:42I then when we escape the the for Loop
  4677. 3:50:45let's print happy New
  4678. 3:50:49Year kind of like it's a countdown to a
  4679. 3:50:51new year here's what we have
  4680. 3:50:55currently we start at 10 count down to
  4681. 3:50:58one then display happy New Year if you
  4682. 3:51:02would like we can use the thread class
  4683. 3:51:04and have our program sleep for about 1
  4684. 3:51:07second between each
  4685. 3:51:09Loop here's how you can do that after
  4686. 3:51:12printing I this is a little Advan Java
  4687. 3:51:15at this level you don't need to
  4688. 3:51:16understand how this works but we're
  4689. 3:51:18going to use the thread class called The
  4690. 3:51:21Sleep Method then pass in an amount of
  4691. 3:51:24milliseconds in which we would like to
  4692. 3:51:26sleep 1,000
  4693. 3:51:29milliseconds now Java wants us to add
  4694. 3:51:31the
  4695. 3:51:33following Java wants this method of main
  4696. 3:51:36to throw this exception if our thread is
  4697. 3:51:39interrupted this is intermediate Java we
  4698. 3:51:43will need the section of code in order
  4699. 3:51:45for our program to sleep after you
  4700. 3:51:47finish this lesson be sure to remove
  4701. 3:51:49throws interrupted exception we'll no
  4702. 3:51:51longer need it print I then our program
  4703. 3:51:54is going to sleep for 1 second 1,000
  4704. 3:51:57milliseconds all right and here's our
  4705. 3:51:59mini
  4706. 3:52:01project 10 9 8 7 6 5 4 3 2 one Happy New
  4707. 3:52:12Year
  4708. 3:52:14let's modify this program a little bit
  4709. 3:52:17now we'll use a scanner to accept user
  4710. 3:52:18input scanner scanner equals new
  4711. 3:52:22scanner we have already imported the
  4712. 3:52:25class with start we're going to accept
  4713. 3:52:27some user
  4714. 3:52:28input but we'll need a
  4715. 3:52:31prompt how many seconds to countown
  4716. 3:52:37from we will assign start to be use our
  4717. 3:52:41scanner then use the next int method
  4718. 3:52:47all right let's try this
  4719. 3:52:51again let's say I would like to count
  4720. 3:52:54down from
  4721. 3:52:5720 and it looks like it's working I'll
  4722. 3:53:00fast forward the video till we get to
  4723. 3:53:02the
  4724. 3:53:04end 3 2
  4725. 3:53:071 happy New
  4726. 3:53:10Year so those are four Loops we can
  4727. 3:53:13execute some code a certain amount of
  4728. 3:53:16times it's very similar to a while loop
  4729. 3:53:19in fact there's a lot of overlap where
  4730. 3:53:21you could use either a for Loop or a
  4731. 3:53:23while loop use a for Loop if you want to
  4732. 3:53:25do something a limited amount of times
  4733. 3:53:28and well everybody those are four Loops
  4734. 3:53:30in
  4735. 3:53:33Java I have a pretty short video for you
  4736. 3:53:35today we're going to discuss two
  4737. 3:53:37important keywords when it comes to
  4738. 3:53:39Loops break and continue break is used
  4739. 3:53:42to break out of a loop continue skips
  4740. 3:53:45the current iteration of a loop think of
  4741. 3:53:48the break keyword as the stop button on
  4742. 3:53:50a remote continue would be similar to
  4743. 3:53:53skip with break we're stopping with
  4744. 3:53:56continuing we're skipping let me give
  4745. 3:53:58you a
  4746. 3:53:59demonstration let's create a for Loop
  4747. 3:54:01that will iterate 10 times let's say in
  4748. 3:54:05I equals 0 will continue as long as I is
  4749. 3:54:09less than
  4750. 3:54:1010 then increment I by 1 during each
  4751. 3:54:14iteration I'll just print I then add a
  4752. 3:54:19space here's what we
  4753. 3:54:22got we have the numbers 0 through 9 so
  4754. 3:54:27let's say when we reach five I would
  4755. 3:54:29like to break out of the loop we're
  4756. 3:54:32going to stop it well I could add an if
  4757. 3:54:36statement let's
  4758. 3:54:38say if I is equal to 5 then we will use
  4759. 3:54:43the break keyword
  4760. 3:54:45and break out of the loop
  4761. 3:54:47entirely here's what
  4762. 3:54:50happens once we hit five we break out of
  4763. 3:54:53the loop previously we continued all the
  4764. 3:54:56way until
  4765. 3:54:579 now with the continue keyword we don't
  4766. 3:55:00break out of the loop we just skip the
  4767. 3:55:03current iteration like a skip button on
  4768. 3:55:06a remote this time let's use continue
  4769. 3:55:10rather than
  4770. 3:55:11break and we'll see the differences
  4771. 3:55:13between the two
  4772. 3:55:16now since we're using continue the
  4773. 3:55:18number five is missing we skipped that
  4774. 3:55:21specific cycle of the loop and went back
  4775. 3:55:23to the beginning that's why five is
  4776. 3:55:25missing between four and six but we
  4777. 3:55:28still continue to the end when working
  4778. 3:55:30with loops if you ever need to break out
  4779. 3:55:33you can use the break keyword to skip
  4780. 3:55:35the occurrent iteration you can use
  4781. 3:55:37continue and well everybody that is both
  4782. 3:55:40the break and continue keywords in Java
  4783. 3:55:45hello again friends today I'm going to
  4784. 3:55:46explain nested Loops in Java a nested
  4785. 3:55:49Loop is really just a loop inside of
  4786. 3:55:52another loop you often see nested Loops
  4787. 3:55:55within a matrix or matrices or when we
  4788. 3:55:58reach the topic of data structures and
  4789. 3:56:00algorithms they might not come up for a
  4790. 3:56:03while yeah this can be any combination
  4791. 3:56:05of for Loops or while Loops any kind of
  4792. 3:56:08loop inside of another loop let me give
  4793. 3:56:10you a
  4794. 3:56:12demonstration let's say I would like to
  4795. 3:56:14print the numbers 1 through N9 I can do
  4796. 3:56:16that pretty easily with a for Loop so we
  4797. 3:56:19will create a for loop I will set an
  4798. 3:56:22index of I equal to 1 we'll continue
  4799. 3:56:25this loop as long as I is less than or
  4800. 3:56:28equal to 9 then I will increment I by
  4801. 3:56:31One during each iteration I will display
  4802. 3:56:36I so this will print the
  4803. 3:56:39numbers 1- N9 or if you prefer you can
  4804. 3:56:43have it all on the same l line by using
  4805. 3:56:45print instead of print line then it
  4806. 3:56:47would look something like
  4807. 3:56:49this 1 2 3 4 5 6 7 8 9 you can even
  4808. 3:56:52separate them with the space after
  4809. 3:56:55printing our index of I let's print a
  4810. 3:56:57space
  4811. 3:56:59character and now the output looks
  4812. 3:57:01something like
  4813. 3:57:02this what if you would like to do this
  4814. 3:57:05code three times let's say print the
  4815. 3:57:07numbers 1 through 9 three
  4816. 3:57:09times well you could copy and paste this
  4817. 3:57:12for
  4818. 3:57:13Loop then do that two additional
  4819. 3:57:16times so we have the numbers 1 through 9
  4820. 3:57:19three
  4821. 3:57:22times to put each row on a new line can
  4822. 3:57:25use an empty print line
  4823. 3:57:29statement then it would look something
  4824. 3:57:31like this a
  4825. 3:57:34matrix we seem to be repeating our code
  4826. 3:57:36a lot in programming we like to follow
  4827. 3:57:38the dry principle don't repeat yourself
  4828. 3:57:41if you don't have to
  4829. 3:57:44instead why don't we create a for Loop
  4830. 3:57:46and stick this for Loop inside of that
  4831. 3:57:48Loop let's create a for Loop to iterate
  4832. 3:57:52three times I will create an index of I
  4833. 3:57:55equal to 1 we'll continue as long as I
  4834. 3:57:59is less than or equal to three then
  4835. 3:58:01increment I by One all we're going to do
  4836. 3:58:04is take our code including the for Loop
  4837. 3:58:08and stick it within this other for Loop
  4838. 3:58:10do pay attention to the
  4839. 3:58:12indentation but we have we have one
  4840. 3:58:15problem we're reusing this index of I
  4841. 3:58:18you can see that Java is already giving
  4842. 3:58:19us a warning here's what
  4843. 3:58:23happens variable I is already defined in
  4844. 3:58:26method
  4845. 3:58:28main if we have a loop inside of another
  4846. 3:58:30loop we can't use the same
  4847. 3:58:33index a common naming convention for a
  4848. 3:58:36nested Loop is to use an index of J
  4849. 3:58:39rather than
  4850. 3:58:40I because J comes next in the alphabet
  4851. 3:58:43after I
  4852. 3:58:44instead of printing I we're going to
  4853. 3:58:46print
  4854. 3:58:47J let's see how this works and here's
  4855. 3:58:51our Matrix of data we're printing our
  4856. 3:58:53index of J three
  4857. 3:58:58times after escaping the nested for Loop
  4858. 3:59:01we have a print line statement here if I
  4859. 3:59:03were to get rid of
  4860. 3:59:05that we'll print the numbers 1 through 9
  4861. 3:59:08three times all on the same
  4862. 3:59:11line let's put that back in
  4863. 3:59:15nested Loops are really good when you're
  4864. 3:59:16working with a matrix of data nested
  4865. 3:59:19Loops come up pretty often in the topic
  4866. 3:59:21of data structures and algorithms
  4867. 3:59:23because we have to Loop over sets of
  4868. 3:59:25data now we're going to cover a mini
  4869. 3:59:28project we're going to create a matrix
  4870. 3:59:30of a symbol that a user is going to type
  4871. 3:59:33in a user will set the rows and columns
  4872. 3:59:35for this Matrix we'll declare the
  4873. 3:59:37following variables int
  4874. 3:59:40rows int
  4875. 3:59:42columns and and Char
  4876. 3:59:48symbol we'll need a scanner so let's
  4877. 3:59:50declare that we'll need a scanner
  4878. 3:59:53scanner scanner equals new
  4879. 3:59:56scanner within the set of parentheses
  4880. 3:59:59type
  4881. 3:59:59system.in let's be sure to import this
  4882. 4:00:02class of scanner at the top of our Java
  4883. 4:00:04file and close the scanner when we're
  4884. 4:00:07done with it scanner.
  4885. 4:00:11close we're going to accept some user
  4886. 4:00:14input a user is going to specify the
  4887. 4:00:16rows the columns and the symbol we're
  4888. 4:00:18going to
  4889. 4:00:19use let's create some
  4890. 4:00:22prompts enter the
  4891. 4:00:25number of rows I'll use print instead of
  4892. 4:00:29print line we will assign our variable
  4893. 4:00:33of
  4894. 4:00:34rows equal to use our scanner use the
  4895. 4:00:39next int
  4896. 4:00:40method then let's do this with columns
  4897. 4:00:44enter the number of
  4898. 4:00:48columns columns equals scanner.
  4899. 4:00:51nextend and the symbol we're going to
  4900. 4:00:58use enter the symbol to
  4901. 4:01:04use we will assign our symbol equal to
  4902. 4:01:09scanner.
  4903. 4:01:10nextt and we can use the chart at method
  4904. 4:01:16give me the character at index of
  4905. 4:01:19zero let's create a for Loop for the
  4906. 4:01:21columns
  4907. 4:01:24first let me zoom
  4908. 4:01:28in we're going to set in
  4909. 4:01:32I we can set it equal to
  4910. 4:01:34zero we'll continue as long as I is less
  4911. 4:01:38than our columns whatever the user types
  4912. 4:01:41in then increment I by One
  4913. 4:01:44during each
  4914. 4:01:45iteration I'll use print instead of
  4915. 4:01:48print line we will print our
  4916. 4:01:52symbol let's perform a test
  4917. 4:01:55run enter the rows it doesn't matter we
  4918. 4:01:59didn't set up the rows yet uh for the
  4919. 4:02:01columns let's do
  4920. 4:02:03eight enter the symbol to use Let's
  4921. 4:02:08do let's do a dollar sign that'd be
  4922. 4:02:12cool we have the column set set up
  4923. 4:02:14currently we should have eight dollar
  4924. 4:02:16signs 1 2 3 4 5 6 7 8 we need to repeat
  4925. 4:02:22this for Loop for as many rows as we
  4926. 4:02:25have so we're going to stick this Loop
  4927. 4:02:28within another
  4928. 4:02:36loop with our nested Loop let's replace
  4929. 4:02:39our index of I with j
  4930. 4:02:44for the outer for Loop we will create a
  4931. 4:02:46new index of I equal to zero we'll
  4932. 4:02:50continue this as long as I is less than
  4933. 4:02:53the rows the number of rows that a user
  4934. 4:02:55types in then increment I by
  4935. 4:02:59One let's perform another test
  4936. 4:03:02run let's say I would like three rows of
  4937. 4:03:06six and I will use an at
  4938. 4:03:09sign so these are all on the same line
  4939. 4:03:12we should have a total of
  4940. 4:03:1418 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  4941. 4:03:2116 17 18 our inner loop ends after each
  4942. 4:03:25group of six I'm going to print a new
  4943. 4:03:30line we can use an empty print line
  4944. 4:03:33statement our outer for Loop is in
  4945. 4:03:35charge of the rows our inner for Loop is
  4946. 4:03:38in charge of the columns let's see the
  4947. 4:03:40final product I would like five
  4948. 4:03:44rows of three three
  4949. 4:03:47columns I'll use an Amper sand here we
  4950. 4:03:52are we have three columns and five
  4951. 4:03:59rows all right everybody those are
  4952. 4:04:01nested Loops it's really just any kind
  4953. 4:04:03of loop inside of another kind of loop
  4954. 4:04:07it doesn't matter if it's a for Loop or
  4955. 4:04:09a while loop we often use nested loops
  4956. 4:04:12with matrices or when we reach the topic
  4957. 4:04:15of data structures and algorithms they
  4958. 4:04:17might not come up very often but you
  4959. 4:04:19should at least be aware of their
  4960. 4:04:21existence and well everybody those are
  4961. 4:04:23nested Loops in
  4962. 4:04:27Java what is going on everybody in this
  4963. 4:04:29video I'm going to discuss methods in
  4964. 4:04:31Java think of a method as a block of
  4965. 4:04:34reusable code that is executed when you
  4966. 4:04:36call it to call a method you use a set
  4967. 4:04:38of parentheses kind of like it's a pair
  4968. 4:04:41of telephones talking to each other why
  4969. 4:04:43are methods is useful well with the
  4970. 4:04:44method you can write some code once and
  4971. 4:04:47reuse it whenever you want you just have
  4972. 4:04:49to call it let me show you why a method
  4973. 4:04:51is useful so let's say we have a task we
  4974. 4:04:53have to sing a happy birthday song three
  4975. 4:04:56times so without using a method we can
  4976. 4:04:58do something like this we'll create one
  4977. 4:05:00verse of a happy birthday song I'll just
  4978. 4:05:03make something up let's
  4979. 4:05:06say happy birthday to you
  4980. 4:05:14happy birthday dear
  4981. 4:05:19you you are we'll insert somebody's age
  4982. 4:05:23here but let's say you are X years
  4983. 4:05:27old then happy birthday to you
  4984. 4:05:30again then I'll add a new line character
  4985. 4:05:32to the end so when we run this
  4986. 4:05:35code we sing Happy Birthday once if I
  4987. 4:05:39need to sing Happy Birthday let's say
  4988. 4:05:41three times well without using a method
  4989. 4:05:44we could although it's inefficient copy
  4990. 4:05:46this code and paste it to additional
  4991. 4:05:49times then we're singing Happy Birthday
  4992. 4:05:52a total of three times well you know
  4993. 4:05:54this does work but we tend to be
  4994. 4:05:56repeating ourselves a lot in programming
  4995. 4:05:59we like to follow the dry principle
  4996. 4:06:01don't repeat yourself how can we not
  4997. 4:06:03repeat ourselves if we don't have
  4998. 4:06:06to well let's create a method a method
  4999. 4:06:09is just a block of reusable code so to
  5000. 4:06:12create a method in Java outside of the
  5001. 4:06:14main method do pay attention to these
  5002. 4:06:16curly braces we'll create a new method a
  5003. 4:06:19method can return something for now
  5004. 4:06:21we'll type void much like our main
  5005. 4:06:23method and we need a name for this
  5006. 4:06:26method the method should be descriptive
  5007. 4:06:28of what it does let's call it the happy
  5008. 4:06:31birthday method all it does is sing
  5009. 4:06:33happy birthday we need a return type a
  5010. 4:06:36name for the method that's descriptive
  5011. 4:06:38parentheses then curly
  5012. 4:06:41braces let's cut our song of Happy
  5013. 4:06:43Birthday
  5014. 4:06:44birthday and paste it within the happy
  5015. 4:06:46birthday method at any point to use this
  5016. 4:06:49method I have to call it call it by its
  5017. 4:06:52name happy birthday then add a set of
  5018. 4:06:55parentheses so we have one issue in Java
  5019. 4:06:58Java wants this method to be static much
  5020. 4:07:01like the main method that we work within
  5021. 4:07:03so in order for this program to
  5022. 4:07:06work we need to add this keyword of
  5023. 4:07:08static normally you don't need this
  5024. 4:07:10keyword but since we're calling this
  5025. 4:07:12method of happy birthday from another
  5026. 4:07:14method that's static this method also
  5027. 4:07:16needs to be static later on in the
  5028. 4:07:18series we'll discuss what static does
  5029. 4:07:20you'll likely have to include that for
  5030. 4:07:22now all right let's do a test run so we
  5031. 4:07:25are going to sing Happy Birthday once so
  5032. 4:07:28again to call a method type the name of
  5033. 4:07:30the method then a set of parentheses the
  5034. 4:07:32pair of parentheses kind of represent a
  5035. 4:07:34pair of telephones that's how I think of
  5036. 4:07:36it at least so now we should sing Happy
  5037. 4:07:40Birthday now this code is reusable we
  5038. 4:07:43write it once and reuse it whenever we
  5039. 4:07:45want we just have to call it again we
  5040. 4:07:48will call the happy birthday method
  5041. 4:07:52again now we are executing that block of
  5042. 4:07:55code two times or even
  5043. 4:07:59three happy
  5044. 4:08:05birthday one thing you should be aware
  5045. 4:08:07of is that methods are unfamiliar with
  5046. 4:08:10any variables declared within other
  5047. 4:08:12methods so let's say we have two
  5048. 4:08:14variables a string of
  5049. 4:08:18name type in your first name whatever it
  5050. 4:08:21is and an integer of age set it to be
  5051. 4:08:25some
  5052. 4:08:26age if I was to use these variables
  5053. 4:08:29within my happy birthday method well the
  5054. 4:08:31happy birthday method actually doesn't
  5055. 4:08:33know what these
  5056. 4:08:34are so let's attempt to use them and see
  5057. 4:08:37what happens within our happy birthday
  5058. 4:08:39song Let's convert this print statement
  5059. 4:08:41to be a print F statement and we will
  5060. 4:08:44insert our name variable replace U with
  5061. 4:08:48the format specifier and we are
  5062. 4:08:50displaying a string see we already have
  5063. 4:08:53a warning cannot resolve symbol name
  5064. 4:08:56I'll attempt to run this code and we'll
  5065. 4:08:58see what happens
  5066. 4:09:00exactly Java cannot find variable
  5067. 4:09:04name methods are not familiar with
  5068. 4:09:07variables inside of other methods if we
  5069. 4:09:10live in this method of main if it's our
  5070. 4:09:12house so to say
  5071. 4:09:13we can't see what's going on in our
  5072. 4:09:15neighbor's house one way in which we can
  5073. 4:09:17solve this is to pass arguments to this
  5074. 4:09:20method from one method we can send
  5075. 4:09:22information to another method these are
  5076. 4:09:25known as
  5077. 4:09:26arguments so when we sing Happy Birthday
  5078. 4:09:30within the set of parentheses within the
  5079. 4:09:31pair of telephones we can pass a value
  5080. 4:09:34or a variable so let's send our variable
  5081. 4:09:37of name to this method of happy birthday
  5082. 4:09:41anything that you send a method is known
  5083. 4:09:43as an argument but we have one issue
  5084. 4:09:47though our method of happy birthday
  5085. 4:09:50isn't set up to receive arguments if you
  5086. 4:09:53send any values or variables to a method
  5087. 4:09:56via arguments you need a matching set of
  5088. 4:09:59what is called parameters our happy
  5089. 4:10:01birthday method isn't set up to receive
  5090. 4:10:03these arguments we need a matching set
  5091. 4:10:06of parameters to set up some parameters
  5092. 4:10:09we're going to list the data type of
  5093. 4:10:10what we're receiving we're receiving a
  5094. 4:10:12string Val value so the data type is
  5095. 4:10:15going to be string and then we need a
  5096. 4:10:17name for what we're receiving it doesn't
  5097. 4:10:20have to be exactly the same but it might
  5098. 4:10:22be helpful to keep these consistent so
  5099. 4:10:25let's perform a test run after this
  5100. 4:10:27print F statement I'm going to add a new
  5101. 4:10:29line
  5102. 4:10:33character happy birthday to you happy
  5103. 4:10:35birthday dear your variable
  5104. 4:10:38name so if I was to change my name to
  5105. 4:10:41let's say sponge Bob we'll insert this
  5106. 4:10:45value at this location within our
  5107. 4:10:49method happy birthday dear
  5108. 4:10:53SpongeBob let's also pass in our
  5109. 4:10:55variable of age so any arguments you're
  5110. 4:10:57sending to a method you're going to
  5111. 4:10:59comma
  5112. 4:11:01separate however we need a matching set
  5113. 4:11:03of parameters our method isn't set up to
  5114. 4:11:06receive this variable of age let's
  5115. 4:11:08perform another test
  5116. 4:11:10run so we have an error actual inform
  5117. 4:11:13formal argument lists differ in length
  5118. 4:11:16we need a matching set of parameters to
  5119. 4:11:19the arguments we're receiving not only
  5120. 4:11:21do we need a string
  5121. 4:11:23variable we need an integer variable
  5122. 4:11:25because we're receiving an integer so
  5123. 4:11:28let's say int
  5124. 4:11:30age we'll insert our age variable right
  5125. 4:11:33here we're displaying an integer so we
  5126. 4:11:35need percent D this will be a print F
  5127. 4:11:38statement then I will include a new line
  5128. 4:11:41character we are inserting our age
  5129. 4:11:44variable let's say SpongeBob is 30 years
  5130. 4:11:48old all right let's see how this works
  5131. 4:11:52now happy birthday to you happy birthday
  5132. 4:11:55dear SpongeBob you are 30 years old
  5133. 4:11:58happy birthday to
  5134. 4:12:00you so with these parameters you can
  5135. 4:12:03rename them when you receive these
  5136. 4:12:05arguments just be sure that the order
  5137. 4:12:07and their data type is correct for
  5138. 4:12:09example we could rename name as let's
  5139. 4:12:12say birth day
  5140. 4:12:17boy you know this would work too
  5141. 4:12:20SpongeBob is the birthday
  5142. 4:12:23boy the names of your parameters can be
  5143. 4:12:25different from the
  5144. 4:12:29arguments just be sure that you get the
  5145. 4:12:31data type correct and the order in which
  5146. 4:12:34you receive these
  5147. 4:12:35arguments let's replace the name of
  5148. 4:12:37SpongeBob with Patrick Patrick will be
  5149. 4:12:4138 so now now with the happy birthday
  5150. 4:12:44method we're inserting new variables
  5151. 4:12:47happy birthday dear Patrick you are 38
  5152. 4:12:50years
  5153. 4:12:51old with methods they also have the
  5154. 4:12:54capability to return a value so I'm
  5155. 4:12:57going to collapse this happy birthday
  5156. 4:12:59method we won't be working with it
  5157. 4:13:02anymore let's create a method to square
  5158. 4:13:05a number we'll create a method to square
  5159. 4:13:07a number then return the result normally
  5160. 4:13:10if we're not returning anything we would
  5161. 4:13:12type this keyword avoid void but since
  5162. 4:13:15we're returning a double we have to list
  5163. 4:13:17the data type of what we're returning
  5164. 4:13:19we're returning a double then we need a
  5165. 4:13:21name for this method let's say double
  5166. 4:13:27square all we're going to do is use the
  5167. 4:13:30return keyword let me zoom in a
  5168. 4:13:33little let's return a number times
  5169. 4:13:37itself number we're going to receive one
  5170. 4:13:41argument an argument of number we have
  5171. 4:13:43to set up a matching parameter our
  5172. 4:13:46number is going to be a double double
  5173. 4:13:49number let's call the square method I
  5174. 4:13:52will Square the number three and again
  5175. 4:13:55since we're calling a method that's not
  5176. 4:13:57static from another static method we
  5177. 4:13:59need to add this keyword of static in
  5178. 4:14:02order for it to run there'll be more on
  5179. 4:14:03the static modifier in the future so if
  5180. 4:14:06I was to run
  5181. 4:14:09this we get no output we're returning a
  5182. 4:14:12double back to the exact spot in which
  5183. 4:14:14we call it we could assign it to a
  5184. 4:14:16variable or print it
  5185. 4:14:18directly let's say double result equals
  5186. 4:14:22Square the number three then display the
  5187. 4:14:26result so 3^ squar would give us
  5188. 4:14:30nine so when returning something just
  5189. 4:14:33imagine that after the method ends we're
  5190. 4:14:35replacing that method call with whatever
  5191. 4:14:37we're returning in this case nine
  5192. 4:14:40picture it that way and then we're
  5193. 4:14:41assigning nine to our double of
  5194. 4:14:44result or we can just output it directly
  5195. 4:14:46if you would prefer to do it that way
  5196. 4:14:49let's print 3^
  5197. 4:14:53squar and that would also give us
  5198. 4:14:58n now let's create a method to cube
  5199. 4:15:01we're going to return a double double
  5200. 4:15:04Cube we have one
  5201. 4:15:07parameter a double of
  5202. 4:15:10number we're going to return our number
  5203. 4:15:14time number time number that would cube
  5204. 4:15:18a number that we're
  5205. 4:15:19given and again we need to add static
  5206. 4:15:22because we're calling a method from
  5207. 4:15:24another method that's static all right
  5208. 4:15:26this time we will call the cube method
  5209. 4:15:29and return the
  5210. 4:15:31result so 3 cubed is 27 3 * 3 * 3 is
  5211. 4:15:3727 one last
  5212. 4:15:40example we'll create a method to return
  5213. 4:15:43full name after being given a first name
  5214. 4:15:45and a last name we're going to return a
  5215. 4:15:49string we'll call this method get full
  5216. 4:15:52name we'll have two
  5217. 4:15:55parameters a string of first for first
  5218. 4:15:58name and a string of last for last name
  5219. 4:16:02we're going to
  5220. 4:16:04return our first
  5221. 4:16:06name plus a space to separate
  5222. 4:16:09them plus our last name of last
  5223. 4:16:14and again we have to add
  5224. 4:16:16static okay let's declare a string
  5225. 4:16:20variable of full name equals call be get
  5226. 4:16:25full name method then pass in some
  5227. 4:16:28arguments let's pass in
  5228. 4:16:31SpongeBob then for the next argument
  5229. 4:16:35SquarePants and then we'll print the
  5230. 4:16:37full
  5231. 4:16:38name print full name
  5232. 4:16:43our method returns one long string of
  5233. 4:16:46SpongeBob
  5234. 4:16:47SquarePants the return keyword will
  5235. 4:16:50return a value after the method ends it
  5236. 4:16:52will return that value back to the place
  5237. 4:16:55in which you call that method after this
  5238. 4:16:58method of get full name is complete just
  5239. 4:17:00imagine that we're replacing it with one
  5240. 4:17:03long string of SpongeBob
  5241. 4:17:05SquarePants like this and then assigning
  5242. 4:17:08it to this
  5243. 4:17:09variable okay so I lied I have one last
  5244. 4:17:12good example to give you will create a
  5245. 4:17:14method to verify a user's age normally
  5246. 4:17:17in the United States at least you have
  5247. 4:17:19to be 18 or older to sign up for things
  5248. 4:17:22like a credit card we'll create a method
  5249. 4:17:24to check a user's age again we're going
  5250. 4:17:26to use static because we're calling a
  5251. 4:17:28method from another method that's static
  5252. 4:17:31let me zoom in we're going to return a
  5253. 4:17:33Boolean this time the name of the method
  5254. 4:17:37let's name it age
  5255. 4:17:41check and there's going to be one
  5256. 4:17:43parameter and integer of age if we're
  5257. 4:17:47going to do an age verification check we
  5258. 4:17:50could use an if statement so within our
  5259. 4:17:52method we'll write an if statement if
  5260. 4:17:55our age parameter is greater than or
  5261. 4:17:58equal to
  5262. 4:17:5918 then let's return
  5263. 4:18:04true else we're going to return
  5264. 4:18:08false this can be simplified to the
  5265. 4:18:11following
  5266. 4:18:13return AG is greater than or equal to 18
  5267. 4:18:15you could do it either way for a
  5268. 4:18:18beginner it might be easier to read if
  5269. 4:18:20we have separate IFL statements but both
  5270. 4:18:22work the same all right so we now have a
  5271. 4:18:25method to check a user's
  5272. 4:18:28age so now we can use this
  5273. 4:18:31method I will create an integer variable
  5274. 4:18:34of age set it equal to some
  5275. 4:18:37age I'll use an if statement we're
  5276. 4:18:40returning a Boolean value I can use that
  5277. 4:18:43within an if
  5278. 4:18:44statement if H
  5279. 4:18:47check then pass in an argument we will
  5280. 4:18:50pass in our variable of
  5281. 4:18:52age if this is
  5282. 4:18:54true if age is greater than or equal to
  5283. 4:18:5718 you return
  5284. 4:19:00true so let's print the
  5285. 4:19:03following you may sign up maybe we're
  5286. 4:19:07signing up for a credit
  5287. 4:19:08card else let's
  5288. 4:19:11print you must be 18 + 2 sign
  5289. 4:19:17up our ages
  5290. 4:19:2021 you may sign up but if we were
  5291. 4:19:2512 that method returns false you must be
  5292. 4:19:2918 plus to sign
  5293. 4:19:31up so that's a method everybody it's a
  5294. 4:19:34block of reusable code that is executed
  5295. 4:19:37when called again I like to think of the
  5296. 4:19:40parentheses after a method as a pair of
  5297. 4:19:42telones talking to each other to use a
  5298. 4:19:44method you have to call it and well
  5299. 4:19:47everybody those are methods in
  5300. 4:19:51Java hello everybody in this video I'm
  5301. 4:19:53going to explain overloaded methods in
  5302. 4:19:55Java in Java overloaded methods are
  5303. 4:19:58methods that share the same name but
  5304. 4:20:00they have different parameters a
  5305. 4:20:03method's name plus its parameters will
  5306. 4:20:06give you a unique method signature each
  5307. 4:20:09method signature needs to be unique no
  5308. 4:20:11two methods can share the same signature
  5309. 4:20:13but they can share the same name let me
  5310. 4:20:16give you a demonstration let's say we
  5311. 4:20:18have a method it's going to return a
  5312. 4:20:20double the method name will be add we'll
  5313. 4:20:24add two numbers together add is the
  5314. 4:20:27method name and then we'll set up some
  5315. 4:20:29parameters we'll have double A for the
  5316. 4:20:32first number and Double B all we're
  5317. 4:20:36going to do is return a plus b it's a
  5318. 4:20:39simple
  5319. 4:20:41method so I'm I'm going to
  5320. 4:20:44print add the numbers 1 + 2 and then
  5321. 4:20:48print
  5322. 4:20:49it this will give us
  5323. 4:20:52three what if I would like to add a
  5324. 4:20:54third
  5325. 4:20:55number you can see that Java is already
  5326. 4:20:57giving us a
  5327. 4:20:59warning expected two arguments but three
  5328. 4:21:01found so if I were to run this program
  5329. 4:21:04we get an error actual and formal
  5330. 4:21:07argument lists differ in
  5331. 4:21:11length once solution is that I could
  5332. 4:21:13create another method also named ad but
  5333. 4:21:16it has different parameters where we
  5334. 4:21:18accept three numbers so let me just copy
  5335. 4:21:21this method paste it then I'll add
  5336. 4:21:23another parameter for Double C return a
  5337. 4:21:28plus b plus C and that should
  5338. 4:21:31work now the number is six these two
  5339. 4:21:35methods share the same name but they
  5340. 4:21:38have different
  5341. 4:21:39parameters so that's actually legal in
  5342. 4:21:42Java
  5343. 4:21:43a method's name plus its parameters will
  5344. 4:21:46give you a unique method signature no
  5345. 4:21:48two methods can share the same
  5346. 4:21:51signature with our second method let's
  5347. 4:21:53delete the last parameter of Double C
  5348. 4:21:56and attempt to run this and we'll see
  5349. 4:21:58what happens
  5350. 4:22:00exactly well we get an error message
  5351. 4:22:03method add is already defined in class
  5352. 4:22:05main Java is saying that we have two
  5353. 4:22:08methods that share the same name and the
  5354. 4:22:11same exact parameters methods can share
  5355. 4:22:13the same name but they need different
  5356. 4:22:15parameters if that's the case now if I
  5357. 4:22:18would like to add another number like
  5358. 4:22:19four I could create another overloaded
  5359. 4:22:22method also named add but this one will
  5360. 4:22:25have four parameters Double D return a
  5361. 4:22:30plus B+ C+
  5362. 4:22:33D and this works
  5363. 4:22:36too let's go over another
  5364. 4:22:39example let's pretend that we're baking
  5365. 4:22:42a pizza
  5366. 4:22:43we'll have a string variable of
  5367. 4:22:46pizza we'll create a method to bake a
  5368. 4:22:48pizza but we have to pass an ingredients
  5369. 4:22:51we'll declare a method with static we'll
  5370. 4:22:54return a string that's the return type
  5371. 4:22:57the method name will be Bake
  5372. 4:22:59Pizza our parameters are going to be a
  5373. 4:23:02set of
  5374. 4:23:03ingredients for this method we'll just
  5375. 4:23:06accept a string of
  5376. 4:23:10bread we'll just return
  5377. 4:23:14our bread variable plus the word
  5378. 4:23:19pizza so this could be for example a
  5379. 4:23:22thick crust pizza or a flatbread pizza
  5380. 4:23:25so to
  5381. 4:23:26say let's set our pizza variable equal
  5382. 4:23:29to called the bake pizza
  5383. 4:23:32method now this method only has a bread
  5384. 4:23:35setup as a parameter an
  5385. 4:23:38ingredient let's say flat bread for the
  5386. 4:23:42bread and then we'll print our
  5387. 4:23:45pizza system. out. print line our string
  5388. 4:23:48variable of
  5389. 4:23:50pizza here's what we have we have a
  5390. 4:23:53string of flatbread pizza so when baking
  5391. 4:23:56a pizza there's a lot of different
  5392. 4:23:58ingredients let's create another method
  5393. 4:24:00of baked pizza but we have a parameter
  5394. 4:24:03of bread and a parameter of cheese let's
  5395. 4:24:06copy this method paste it but we have to
  5396. 4:24:09change the parameters we're already
  5397. 4:24:11receiving a warning
  5398. 4:24:12we'll have string bread and string
  5399. 4:24:15cheese no pun intended let me zoom in
  5400. 4:24:19now those warnings have went away we
  5401. 4:24:21have two methods with the same name but
  5402. 4:24:23they have different parameters which is
  5403. 4:24:25fine we can do that so this time let's
  5404. 4:24:28return a string of cheese plus a space
  5405. 4:24:34character plus our bread plus the word
  5406. 4:24:37Pizza let's pass in a type of
  5407. 4:24:40cheese Mozzarella
  5408. 4:24:43I probably misspelled that actually I
  5409. 4:24:45spelled it correctly okay now we'll bake
  5410. 4:24:47our
  5411. 4:24:50pizza we have a mozzarella flatbread
  5412. 4:24:53pizza if there's two or more methods
  5413. 4:24:56with the same name we'll end up using
  5414. 4:24:57the method where the parameters match
  5415. 4:25:00we'll create one more overloaded method
  5416. 4:25:02we'll create another baked pizza method
  5417. 4:25:04but this one will have a custom topping
  5418. 4:25:07string
  5419. 4:25:08topping we'll return topping
  5420. 4:25:13plus a space
  5421. 4:25:14character plus everything we had
  5422. 4:25:18previously then we can pass in A new
  5423. 4:25:23ingredient how do you spell pepperoni I
  5424. 4:25:25can never spell
  5425. 4:25:26it
  5426. 4:25:31pepperoni now this time we have a
  5427. 4:25:33pepperoni mozzarella flatbread
  5428. 4:25:36pizza all right everybody so those are
  5429. 4:25:39overloaded methods methods can share the
  5430. 4:25:41same name but they need to have a
  5431. 4:25:43different list of
  5432. 4:25:45parameters a method's name plus its
  5433. 4:25:47parameters will give you a unique method
  5434. 4:25:49signature and no two methods can share
  5435. 4:25:52the same signature and well everybody
  5436. 4:25:54those are overloaded methods in
  5437. 4:25:58Java I got a relatively quick video for
  5438. 4:26:01you today today we're going to discuss
  5439. 4:26:03variable scope in Java variable scope is
  5440. 4:26:06where a variable can be accessed there's
  5441. 4:26:08two levels we'll discuss today local and
  5442. 4:26:11class
  5443. 4:26:12we'll cover local
  5444. 4:26:14first inside of a method if you declare
  5445. 4:26:17a variable it has what is considered a
  5446. 4:26:19local scope in this demonstration let's
  5447. 4:26:22create a variable of x x will equal some
  5448. 4:26:25number such as one and then you know we
  5449. 4:26:27can print whatever X
  5450. 4:26:30is xal
  5451. 4:26:321 a variable declared inside of a method
  5452. 4:26:36has a local scope and I'll just add a
  5453. 4:26:38comment that this is a local variable
  5454. 4:26:42outside of the set of curly braces where
  5455. 4:26:45this variable is declared X isn't going
  5456. 4:26:47to be recognized that's why if we create
  5457. 4:26:49another method we would have to pass it
  5458. 4:26:51as an
  5459. 4:26:52argument let's create another method
  5460. 4:26:55this will be a static method that
  5461. 4:26:57doesn't return anything I'm just going
  5462. 4:26:59to say it's the do something
  5463. 4:27:01method inside of other methods you can
  5464. 4:27:04declare variables that have the same
  5465. 4:27:06name within this other method we're
  5466. 4:27:08going to create an integer variable of X
  5467. 4:27:11but X is going to equal two so Java
  5468. 4:27:14isn't giving us any
  5469. 4:27:16warnings it is legal to have variables
  5470. 4:27:19with the same name as long as they're
  5471. 4:27:21within different methods the moment I
  5472. 4:27:23place this variable within my main
  5473. 4:27:26method Java is giving us a warning
  5474. 4:27:29variable X is already defined in the
  5475. 4:27:31scope this local scope variable X within
  5476. 4:27:35main has a local scope variable X within
  5477. 4:27:38the do something method also has a local
  5478. 4:27:41scope and I'll just add a comment that
  5479. 4:27:43this is a local
  5480. 4:27:45variable if I was to print X within this
  5481. 4:27:49method and if I was to call this
  5482. 4:27:52method what do you think will output one
  5483. 4:27:56or two take a
  5484. 4:27:59guess
  5485. 4:28:00two within the context of our do
  5486. 4:28:03something method xal 2 this method isn't
  5487. 4:28:07aware of the existence of this variable
  5488. 4:28:09within this method of main they each
  5489. 4:28:11have a local scope it's kind of like
  5490. 4:28:14their neighbors they can't see inside of
  5491. 4:28:16each other's house now a class variable
  5492. 4:28:19is declared inside of a class but not
  5493. 4:28:22within any methods you usually see them
  5494. 4:28:24at the top let's create a static
  5495. 4:28:27variable an integer of X where x equals
  5496. 4:28:323 variable X within this class of main
  5497. 4:28:35has a class scope in our analogy of
  5498. 4:28:39houses you could say that this variable
  5499. 4:28:42is out in the street these two methods
  5500. 4:28:44can look out their window and see
  5501. 4:28:46variable X and that it equals
  5502. 4:28:48three if I was to delete these two local
  5503. 4:28:52variables and print X here's the
  5504. 4:28:56result xal
  5505. 4:28:593 or within our main method if I was to
  5506. 4:29:02print
  5507. 4:29:04x x also equals 3 let's say that each
  5508. 4:29:09method is a house they're neighbors you
  5509. 4:29:12you could say that a class variable is
  5510. 4:29:14outside they're walking down the street
  5511. 4:29:17both of these methods can look outside
  5512. 4:29:19their window and see this variable now
  5513. 4:29:22if you had a mix of both local and class
  5514. 4:29:24variables that share the same name let's
  5515. 4:29:27say int xals 1 and this was a local
  5516. 4:29:31variable and within our method of do
  5517. 4:29:34something xal
  5518. 4:29:38two let's call the do something method
  5519. 4:29:43we'll print X within the main method and
  5520. 4:29:46then within the do something
  5521. 4:29:48method what do you think the output is
  5522. 4:29:50going to
  5523. 4:29:51be well we have one and
  5524. 4:29:55two even though there's a third X
  5525. 4:29:57variable within the class scope Java
  5526. 4:30:00likes to use any local variables first
  5527. 4:30:02if they share the same name as any class
  5528. 4:30:05variables within our method of main this
  5529. 4:30:09method thinks xal 1 it's going to use
  5530. 4:30:11theal scope first you'll likely want to
  5531. 4:30:14stick with local variables over class
  5532. 4:30:17variables there are a few situations
  5533. 4:30:19when a class variable would be better
  5534. 4:30:21such as if you have constants where the
  5535. 4:30:23value doesn't change or when working
  5536. 4:30:26with object-oriented programming which
  5537. 4:30:27is a whole another topic so that is
  5538. 4:30:30variable scope in Java anything with the
  5539. 4:30:32local scope is declared inside of a
  5540. 4:30:34method or otherwise within a set of
  5541. 4:30:36curly braces and is only recognized
  5542. 4:30:39within this method other methods aren't
  5543. 4:30:41aware of its existence that's sometimes
  5544. 4:30:43why we pass them as arguments to other
  5545. 4:30:45methods class variables are declared
  5546. 4:30:48inside of a class they are recognized
  5547. 4:30:50throughout the entire class and well
  5548. 4:30:52everybody that is variable scope in
  5549. 4:30:57Java hello everybody in this video we're
  5550. 4:30:59going to create a very simple banking
  5551. 4:31:02program using Java this video is meant
  5552. 4:31:04to be a beginner's project to help us
  5553. 4:31:06understand how methods work so if that
  5554. 4:31:08sounds good to you let's get started as
  5555. 4:31:11a beginner it might be helpful to you to
  5556. 4:31:13break up your project into separate
  5557. 4:31:15steps then tackle this project one step
  5558. 4:31:17at a time I'll add a few comments of the
  5559. 4:31:20different steps our program is going to
  5560. 4:31:21take so first we're going to declare our
  5561. 4:31:25variables I misspelled
  5562. 4:31:29that then we will display a
  5563. 4:31:33menu a user is going to pick a choice
  5564. 4:31:35from this menu depending on what they
  5565. 4:31:37want to do they can make a deposit show
  5566. 4:31:39their balance make a withdrawal so on
  5567. 4:31:41and so forth we will get and
  5568. 4:31:46process users
  5569. 4:31:50Choice we'll create a method of show
  5570. 4:31:53balance in order to display the balance
  5571. 4:31:55to the
  5572. 4:31:56user let me zoom
  5573. 4:31:59in another method to make a
  5574. 4:32:02deposit then a method to withdraw
  5575. 4:32:05withdraw funds we'll just say
  5576. 4:32:08withdraw and then we will create an exit
  5577. 4:32:11message at at the end once the user
  5578. 4:32:15exits so again as a beginner it might be
  5579. 4:32:18helpful to you to break up your program
  5580. 4:32:20into different steps we'll start with
  5581. 4:32:22our step of declare variables here's the
  5582. 4:32:25different variables we'll need we have
  5583. 4:32:27an account our account has a balance
  5584. 4:32:29such as dollars and cents so a double
  5585. 4:32:32would make sense for the balance because
  5586. 4:32:35it includes a decimal portion sents so
  5587. 4:32:38we have our balance we'll need a scanner
  5588. 4:32:40because we're accepting us user input
  5589. 4:32:42scanner scanner equals new
  5590. 4:32:46scanner type system.in import this
  5591. 4:32:50class import java.util.scanner and then
  5592. 4:32:54we are going to close our scanner when
  5593. 4:32:56we're done with it let's take care of
  5594. 4:32:58that now because I'm probably going to
  5595. 4:33:02forget we have our scanner our
  5596. 4:33:05balance we're going to continue this
  5597. 4:33:07program as long as a user doesn't exit
  5598. 4:33:10we'll need a while loop we will declare
  5599. 4:33:12and assign a Boolean variable of is
  5600. 4:33:15running set that to be true continue
  5601. 4:33:19this program while is running is
  5602. 4:33:23true then we will accept a choice from
  5603. 4:33:25the user this will be an integer a user
  5604. 4:33:28is going to pick one to show balance two
  5605. 4:33:31to deposit three to withdraw or four to
  5606. 4:33:34exit the user is going to fill this in
  5607. 4:33:36later using the scanner all right we
  5608. 4:33:38have declared our
  5609. 4:33:40variables now we'll a menu to the
  5610. 4:33:43user I will print the following let's
  5611. 4:33:46say banking
  5612. 4:33:48program now you don't need to do this
  5613. 4:33:51but I like to add some separators using
  5614. 4:33:53a bunch of
  5615. 4:33:55asterisks I just think it makes it look
  5616. 4:34:02cool we will lay out the different
  5617. 4:34:05options we will have option one option
  5618. 4:34:09one will be to show balance
  5619. 4:34:15option two will be to
  5620. 4:34:20deposit option three will be to
  5621. 4:34:27withdraw four will be to
  5622. 4:34:30exit and then I'll add another
  5623. 4:34:33separator again not necessary I think it
  5624. 4:34:36would just look more organized now we
  5625. 4:34:38need to get and process the user's
  5626. 4:34:40choice
  5627. 4:34:43we have a variable of choice we'll need
  5628. 4:34:46to create a
  5629. 4:34:48prompt we will say enter your choice
  5630. 4:34:53then hint one through
  5631. 4:34:564 I'll use print instead of print line
  5632. 4:34:59because I like the user input on the
  5633. 4:35:01same line as the prompt we will assign
  5634. 4:35:04our variable of choice equal to use the
  5635. 4:35:07scanner call the next int method
  5636. 4:35:12ideally a user is going to type in a
  5637. 4:35:14number 1 through 4 to do one of the
  5638. 4:35:16following now we'll process the user's
  5639. 4:35:19choice I think an enhanced switch would
  5640. 4:35:21be great for this we will create a
  5641. 4:35:25switch and examine our variable of
  5642. 4:35:28choice we're examining our choice
  5643. 4:35:30against matching cases if Choice equals
  5644. 4:35:33a case of one we'll write an arrow to do
  5645. 4:35:37something we'll do this we're going to
  5646. 4:35:40call a method to show our B balance
  5647. 4:35:42because one means the user wants to show
  5648. 4:35:44their
  5649. 4:35:45balance we'll fill that in later let's
  5650. 4:35:48write a test message I'll use
  5651. 4:35:50system.out.print LINE let's say show
  5652. 4:35:56balance case two will be for
  5653. 4:36:00deposit we'll just print a test message
  5654. 4:36:03of deposit again we're going to fill
  5655. 4:36:05this method in later and call
  5656. 4:36:10it case three is for
  5657. 4:36:12withdraw case
  5658. 4:36:14three
  5659. 4:36:18withdraw case four is to exit that we
  5660. 4:36:21can actually fill in right away it's
  5661. 4:36:23pretty
  5662. 4:36:25simple we'll take our Boolean variable
  5663. 4:36:27of is
  5664. 4:36:29running and set it to be
  5665. 4:36:32false then we'll add a default case in
  5666. 4:36:35case somebody doesn't type in a number 1
  5667. 4:36:37through
  5668. 4:36:38four we'll output
  5669. 4:36:42invalid
  5670. 4:36:44Choice Let's test numbers 1 through
  5671. 4:36:47three
  5672. 4:36:49first enter a choice I'll pick one
  5673. 4:36:52that's for show balance again we'll be
  5674. 4:36:54calling a method later to show our
  5675. 4:36:57balance two is for deposit we're
  5676. 4:37:02depositing three is for
  5677. 4:37:04withdraw four is to
  5678. 4:37:07exit but it doesn't do anything
  5679. 4:37:09currently
  5680. 4:37:12then any other choice such as
  5681. 4:37:15five prints out invalid
  5682. 4:37:19choice we want this program to continue
  5683. 4:37:21running as long as is running remains
  5684. 4:37:25true so let's enclose our code within a
  5685. 4:37:29loop a while
  5686. 4:37:31loop
  5687. 4:37:33while is running you don't necessarily
  5688. 4:37:37need to say equals true if it's a
  5689. 4:37:39Boolean you can just say the Boolean
  5690. 4:37:40variables name name while is running is
  5691. 4:37:43true then do all this code so I'm going
  5692. 4:37:46to cut our code currently and paste it
  5693. 4:37:49within the wild
  5694. 4:37:51Loop now we can test is
  5695. 4:37:55running one is to show
  5696. 4:37:58balance we show our balance two is to
  5697. 4:38:03deposit we
  5698. 4:38:04deposit three is to
  5699. 4:38:07withdraw withdraw four is to exit and we
  5700. 4:38:11should exit processed finished with exit
  5701. 4:38:14Code Zero we know that that works
  5702. 4:38:17now our next step is to show balance and
  5703. 4:38:21this is going to be a
  5704. 4:38:23method outside of our main method we're
  5705. 4:38:27going to create a new method of show
  5706. 4:38:31balance this has to be a static method
  5707. 4:38:34because our main method is also
  5708. 4:38:38static there will be no return type the
  5709. 4:38:41return type is void the method name will
  5710. 4:38:44be show
  5711. 4:38:46balance there's going to be one
  5712. 4:38:48parameter a double of balance when we
  5713. 4:38:52call this method we have to pass in our
  5714. 4:38:54balance
  5715. 4:38:55variable within this method we're going
  5716. 4:38:58to use a print F
  5717. 4:39:03statement and I'll zoom in a little
  5718. 4:39:06within this print F statement pick a
  5719. 4:39:08unit of currency you would like to use
  5720. 4:39:09for this banking program all use
  5721. 4:39:11American dollars we need a placeholder
  5722. 4:39:14we'll use a percent sign and then F to
  5723. 4:39:17display a double f means floating Point
  5724. 4:39:19number we will display our
  5725. 4:39:23balance and from the beginning I'm
  5726. 4:39:25actually going to set my balance equal
  5727. 4:39:27to zero we're going to be testing
  5728. 4:39:31it then let me zoom out for case one
  5729. 4:39:36we're going to call the show balance
  5730. 4:39:38method but we have to pass in our
  5731. 4:39:40balance variable
  5732. 4:39:44all right let's test
  5733. 4:39:46it enter a choice I'll pick one to show
  5734. 4:39:49our
  5735. 4:39:50balance uh here's what we got currently
  5736. 4:39:530 and a bunch of zeros after the decimal
  5737. 4:39:57we are forgetting that new line
  5738. 4:39:58character too so let's add a new line
  5739. 4:40:00character to the end before I
  5740. 4:40:03forget if you would like to display two
  5741. 4:40:05digits after the decimal for
  5742. 4:40:09example normally when you display a
  5743. 4:40:11floating Point number using print F you
  5744. 4:40:13display six digits after the decimal to
  5745. 4:40:16limit it to two we can add the
  5746. 4:40:18following after our placeholder our
  5747. 4:40:20percent sign we're going to add
  5748. 4:40:2502 and to your choice I will type in one
  5749. 4:40:28to show our balance our balance is $0
  5750. 4:40:31and0 we're displaying two digits after
  5751. 4:40:34the
  5752. 4:40:36decimal let's change our initial balance
  5753. 4:40:38to be $10 and
  5754. 4:40:4499 ENT to your choice I will type in one
  5755. 4:40:47our balance is $10 and 99 I think I'm
  5756. 4:40:51also going to add a separator before
  5757. 4:40:52displaying the balance too to help with
  5758. 4:40:54readability although it's not necessary
  5759. 4:40:57so I'm going to cut one of these lines
  5760. 4:40:59where we display a bunch of
  5761. 4:41:01asterisks and print it right before
  5762. 4:41:03displaying the
  5763. 4:41:06balance let's show our balance and
  5764. 4:41:09here's our balance $10 and 99 and we
  5765. 4:41:12have some of those separators before and
  5766. 4:41:14after which I think looks
  5767. 4:41:16nice our show balance method is complete
  5768. 4:41:19I'm going to collapse this method we
  5769. 4:41:21don't need to code within it
  5770. 4:41:24anymore our next method is to make a
  5771. 4:41:26deposit we'll create a method of
  5772. 4:41:31deposit oh and then be sure to set your
  5773. 4:41:33balance back to
  5774. 4:41:38zero we will create a static method the
  5775. 4:41:41return type is going to be double we
  5776. 4:41:44will declare this method as the deposit
  5777. 4:41:46method there will be no parameters but
  5778. 4:41:49we will be returning something a double
  5779. 4:41:52inside of this method I will create a
  5780. 4:41:54local
  5781. 4:41:55variable the data type is double the
  5782. 4:41:58name will be amount a user is going to
  5783. 4:42:01type in an amount the amount that
  5784. 4:42:03they're depositing using this method
  5785. 4:42:05this method isn't going to work properly
  5786. 4:42:07until we return
  5787. 4:42:09something add return statement
  5788. 4:42:12just temporarily so everything runs fine
  5789. 4:42:14I'm just going to return zero we'll take
  5790. 4:42:17care of this later I just want
  5791. 4:42:18everything to run a compile fine we will
  5792. 4:42:21prompt a user to type in an
  5793. 4:42:23amount I will use print instead of print
  5794. 4:42:25line that's my own preference for
  5795. 4:42:28prompts enter an amount to be
  5796. 4:42:35deposited now here's one issue we're
  5797. 4:42:38accepting user input from a user amount
  5798. 4:42:41equals if I attempt to use our scanner
  5799. 4:42:44scanner. next Double there's an
  5800. 4:42:47issue our method isn't aware what the
  5801. 4:42:50scanner is because we declared the
  5802. 4:42:53scanner within a separate method our
  5803. 4:42:56main method methods can't see inside of
  5804. 4:42:58each other there's a few different
  5805. 4:43:01options one we could create a local
  5806. 4:43:03scanner within this method you know
  5807. 4:43:06scanner scanner equals new scanner and
  5808. 4:43:08all that I don't like the idea of
  5809. 4:43:10creating creting several different
  5810. 4:43:11scanners within this project because
  5811. 4:43:13then we're leaving them all open to
  5812. 4:43:15accept user input so why don't we do
  5813. 4:43:18this let's take our scanner move it from
  5814. 4:43:21this local scope and put it within a
  5815. 4:43:23class scope so let's cut our scanner
  5816. 4:43:26within the main
  5817. 4:43:28method and stick it within the main
  5818. 4:43:32class but it does need to be
  5819. 4:43:34static because some static methods are
  5820. 4:43:37using it this one scanner is accessible
  5821. 4:43:39throughout the whole program now
  5822. 4:43:42and that warning goes away so I think
  5823. 4:43:45this would be better than creating
  5824. 4:43:46several different scanners and leaving
  5825. 4:43:48them all open throughout this program
  5826. 4:43:50we're just going to reuse the same one
  5827. 4:43:53at this point we're going to verify the
  5828. 4:43:54amount that the user types
  5829. 4:43:56in I'll use an if statement first if a
  5830. 4:44:00user types in an amount that's negative
  5831. 4:44:02well they can't make a deposit they
  5832. 4:44:04can't deposit negative money that's
  5833. 4:44:05stupid so we will check if our amount
  5834. 4:44:09variable that the user types in is less
  5835. 4:44:11than zero if that's the case we will
  5836. 4:44:14print the
  5837. 4:44:16following amount can't be
  5838. 4:44:21negative and then we will return
  5839. 4:44:26zero if the user does type in an amount
  5840. 4:44:29that's valid we'll execute an lse
  5841. 4:44:32statement where we're going to return
  5842. 4:44:35let's cut our return statement at the
  5843. 4:44:36end we'll return our amount
  5844. 4:44:41now when we call our method we can
  5845. 4:44:44delete this print line
  5846. 4:44:46statement if a user selects two to make
  5847. 4:44:48a deposit we're going to assign our
  5848. 4:44:51balance
  5849. 4:44:53variable balance equals our balance plus
  5850. 4:44:58call the deposit
  5851. 4:45:02method our deposit method is going to
  5852. 4:45:05return a number an amount if it's valid
  5853. 4:45:08we're going to return it back to the
  5854. 4:45:09place in which we call it it we will add
  5855. 4:45:12this to our current balance and reassign
  5856. 4:45:14it to balance our balance will equal our
  5857. 4:45:17current balance plus the deposit that we
  5858. 4:45:19make we can shorten this too we could
  5859. 4:45:22just say balance plus equals
  5860. 4:45:25deposit okay let's do a test
  5861. 4:45:29run banking program let's show our
  5862. 4:45:32balance our balance is currently zero
  5863. 4:45:35we'll select two to make a
  5864. 4:45:37deposit two enter an amount to be
  5865. 4:45:40deposited
  5866. 4:45:42let's try and break it first let's say1
  5867. 4:45:47million amount can't be negative then
  5868. 4:45:50we're brought back to the
  5869. 4:45:51menu let's try and make a deposit again
  5870. 4:45:54enter an amount to be deposited let's
  5871. 4:45:56say
  5872. 4:45:58$150 and a nickel 5
  5873. 4:46:02cents that seems to have worked let's
  5874. 4:46:05show our balance again and here's our
  5875. 4:46:08New Balance $150
  5876. 4:46:11and 5 cents all right the deposit method
  5877. 4:46:14is
  5878. 4:46:15done we can collapse it we no longer
  5879. 4:46:18need to code within it and now we need a
  5880. 4:46:20withdraw
  5881. 4:46:22method we will declare a new
  5882. 4:46:26method it's a static method we'll be
  5883. 4:46:28returning a
  5884. 4:46:30double the name of this method will be
  5885. 4:46:36withdraw there will be one parameter we
  5886. 4:46:39will pass in a balance double
  5887. 4:46:43balance we do need to return something
  5888. 4:46:45so temporarily just so our program runs
  5889. 4:46:47correctly I'm just going to return zero
  5890. 4:46:50we'll change that
  5891. 4:46:52later within this method I'm going to
  5892. 4:46:54create a local variable of amount double
  5893. 4:46:58amount we will prompt a
  5894. 4:47:03user enter amount to be withdrawn
  5895. 4:47:12we will assign our amount equal to we're
  5896. 4:47:15going to use our scanner and remember
  5897. 4:47:18it's within the class scope so we have
  5898. 4:47:20access to it within this method scanner.
  5899. 4:47:23nextt double to accept a
  5900. 4:47:26double if somebody tries to withdraw
  5901. 4:47:29money that they don't have we can check
  5902. 4:47:32that within an if
  5903. 4:47:33statement if the amount is greater than
  5904. 4:47:37our balance we're passing in our balance
  5905. 4:47:40so we know what what it is if somebody
  5906. 4:47:42tries to withdraw a million dollars but
  5907. 4:47:45they have no money in their bank account
  5908. 4:47:47well they can't do
  5909. 4:47:48that we will
  5910. 4:47:51print
  5911. 4:47:54insufficient
  5912. 4:47:57funds what if they try and withdraw
  5913. 4:47:59negative money we can check that within
  5914. 4:48:01an else if
  5915. 4:48:02statement else if our amount that the
  5916. 4:48:06user types in is less than
  5917. 4:48:09zero will
  5918. 4:48:12output amount can't be
  5919. 4:48:20negative else if everything checks out
  5920. 4:48:23we'll return the amount I will cut our
  5921. 4:48:26current return statement we will return
  5922. 4:48:29the amount that the user types
  5923. 4:48:31in return
  5924. 4:48:33amount So within our if statement return
  5925. 4:48:37zero within our else if statement return
  5926. 4:48:40zero
  5927. 4:48:42when we reach the end we do need to
  5928. 4:48:44return something even if it is
  5929. 4:48:46zero and I'm going to use print instead
  5930. 4:48:49of print
  5931. 4:48:50line so let's go back to our switch
  5932. 4:48:53right here we're going to be returning a
  5933. 4:48:56double we will take our
  5934. 4:48:59balance equals we're assigning it take
  5935. 4:49:03our current balance minus call the
  5936. 4:49:06withdraw
  5937. 4:49:08method but we have to pass in our our
  5938. 4:49:11balance our original
  5939. 4:49:13balance from our current balance we're
  5940. 4:49:16subtracting the amount that we're
  5941. 4:49:18withdrawing we can simplify this
  5942. 4:49:20expression too we could say balance
  5943. 4:49:23minus equals then call the withdraw
  5944. 4:49:27method all right let's do a test
  5945. 4:49:30run first we'll make a
  5946. 4:49:33deposit I will deposit $100 to keep the
  5947. 4:49:36simple I will show our balance we have
  5948. 4:49:39$100
  5949. 4:49:41I will try and withdraw like a kajillion
  5950. 4:49:45dollars insufficient funds we don't have
  5951. 4:49:48a kajillion
  5952. 4:49:50dollar let's withdraw again I will try
  5953. 4:49:53and withdraw negative money $
  5954. 4:49:5842069 amount can't be negative we can't
  5955. 4:50:01withdraw negative money okay let's try
  5956. 4:50:03and withdraw something that's valid
  5957. 4:50:08$501 that seems to have worked let's
  5958. 4:50:11show our balance our balance is now
  5959. 4:50:15$49.99 the last step is to exit and
  5960. 4:50:18display a goodby
  5961. 4:50:21message let's print the
  5962. 4:50:24following thank
  5963. 4:50:26you have a nice
  5964. 4:50:29day just so the user knows that they
  5965. 4:50:31have exited the
  5966. 4:50:32program and for fun I'm going to add
  5967. 4:50:35some text decoration some
  5968. 4:50:38separators not necessary but I think
  5969. 4:50:40it'll look
  5970. 4:50:44nice all right let's run this one last
  5971. 4:50:48time banking program enter your choice I
  5972. 4:50:50will show our balance we have 0 I will
  5973. 4:50:53make a deposit I will deposit
  5974. 4:50:56$11,000 show our balance we have
  5975. 4:50:58$1,000 I will
  5976. 4:51:01withdraw let's withdraw a single penny
  5977. 4:51:050.01 show our balance we now have
  5978. 4:51:08$999.99
  5979. 4:51:10sense I will press for to exit and we
  5980. 4:51:14exit thank you have a nice
  5981. 4:51:17day all right everybody that is a Java
  5982. 4:51:21banking program that you can write as a
  5983. 4:51:25beginner hey there everybody in this
  5984. 4:51:27video we're going to create a dice
  5985. 4:51:29rolling program a user is going to type
  5986. 4:51:31in a number of dice they would like to
  5987. 4:51:32roll based on the roll of each die we
  5988. 4:51:35will displace masy art then at the end
  5989. 4:51:37we will give the user a total the sum of
  5990. 4:51:39all the dice that they have rolled so if
  5991. 4:51:41that sounds good to you let's begin as a
  5992. 4:51:44beginner it might be helpful to you to
  5993. 4:51:46break down a large project into smaller
  5994. 4:51:48steps let's list the order of steps
  5995. 4:51:50we're going to take we will Begin by
  5996. 4:51:52declaring our
  5997. 4:51:54variables declare
  5998. 4:51:57variables then we need to get the number
  5999. 4:52:01of Dice from the
  6000. 4:52:04user after we have the number of dice
  6001. 4:52:07let's check to see if it's greater than
  6002. 4:52:09zero we don't want somebody rolling
  6003. 4:52:11negative dice check if number of
  6004. 4:52:15dice is greater than
  6005. 4:52:19zero we will roll all the
  6006. 4:52:24dice get the
  6007. 4:52:26total or the sum you could say the final
  6008. 4:52:30part of this project we will display the
  6009. 4:52:32asy art of
  6010. 4:52:36dice all right here's a rough outline of
  6011. 4:52:38our project the first step is to declare
  6012. 4:52:41our
  6013. 4:52:42variables we're accepting user input so
  6014. 4:52:44we'll need a scanner scanner scanner
  6015. 4:52:47equals new
  6016. 4:52:49scanner type
  6017. 4:52:51system.in import this class import
  6018. 4:52:56java.util.scanner and then close our
  6019. 4:52:58scanner when we're done with it scanner.
  6020. 4:53:01close we'll be working with random
  6021. 4:53:03numbers we'll need a random object to
  6022. 4:53:05Generate random numbers random random
  6023. 4:53:08equals new random
  6024. 4:53:11import the
  6025. 4:53:13class import java. u. random we'll need
  6026. 4:53:18a number of dice the user is going to
  6027. 4:53:20fill this in int num of dice how many
  6028. 4:53:25dice are we
  6029. 4:53:27rolling and a total int total we'll sum
  6030. 4:53:31up all the dice for a
  6031. 4:53:33total all right now we need to get the
  6032. 4:53:36number of Dice from the user we'll need
  6033. 4:53:38some input but we'll create a prompt
  6034. 4:53:41first enter
  6035. 4:53:44the number of dice to
  6036. 4:53:48roll I'll use print rather than print
  6037. 4:53:51line we will assign our variable of
  6038. 4:53:54number of dice number of dice we will
  6039. 4:53:58use our scanner to accept some user
  6040. 4:54:00input we are looking for an integer
  6041. 4:54:03we'll use the next int method of our
  6042. 4:54:05scanner scanner. next
  6043. 4:54:08in now we need to to check to see if the
  6044. 4:54:11number of dice number of dice is greater
  6045. 4:54:14than
  6046. 4:54:15zero so let's take care of that we'll
  6047. 4:54:18use an if statement to check
  6048. 4:54:20that if num of dice is greater than zero
  6049. 4:54:26then we'll roll some
  6050. 4:54:27dice we'll get to that
  6051. 4:54:30momentarily else we will print the
  6052. 4:54:34following let's say number of
  6053. 4:54:37dice must be greater than
  6054. 4:54:42zero within our if statement let's say
  6055. 4:54:45just
  6056. 4:54:46temporarily you
  6057. 4:54:49roll the dice okay we're doing a test
  6058. 4:54:54run enter the number of dice to roll
  6059. 4:54:57let's say I try and roll zero dice
  6060. 4:55:00number of dice must be greater than zero
  6061. 4:55:04let's try this again let's say I roll
  6062. 4:55:06three
  6063. 4:55:07Dice and then we're just printing a test
  6064. 4:55:10message
  6065. 4:55:10you rolled the dice okay we know that
  6066. 4:55:13that works we can delete this print
  6067. 4:55:15statement we no longer need it we were
  6068. 4:55:17just testing
  6069. 4:55:18it now we actually need to roll all the
  6070. 4:55:21dice we'll take care of that within our
  6071. 4:55:23if statement if the number of dice is
  6072. 4:55:25greater than zero then we will roll the
  6073. 4:55:28dice we're going to create a for Loop
  6074. 4:55:30that will iterate once based on the
  6075. 4:55:32number of dice we have so let's create a
  6076. 4:55:35for
  6077. 4:55:36Loop for in I will be our index in I
  6078. 4:55:41equals z we will continue as long as I
  6079. 4:55:44is less than our number of
  6080. 4:55:46dice then increment our index of I by
  6081. 4:55:49One during each
  6082. 4:55:51cycle within this for loop we're going
  6083. 4:55:53to generate a random number I will
  6084. 4:55:56create a local variable of r r will
  6085. 4:56:00equal now we'll use a random object to
  6086. 4:56:02generate a random number a random number
  6087. 4:56:05between 1 and six we will use the next
  6088. 4:56:08int method of our random object
  6089. 4:56:11we will specify our range 1 through 7
  6090. 4:56:15the first number is inclusive the second
  6091. 4:56:17number is exclusive this will give us a
  6092. 4:56:20random number between 1 and six
  6093. 4:56:22then once we have our roll let's print
  6094. 4:56:26it
  6095. 4:56:27temporarily let's print you rolled colon
  6096. 4:56:33space plus our local variable of Ro
  6097. 4:56:41let's say we would like to roll four
  6098. 4:56:43dice you rolled five you rolled five you
  6099. 4:56:46rolled one you rolled two then we'll add
  6100. 4:56:49these numbers together for a sum or
  6101. 4:56:52total let's take our
  6102. 4:56:55total total equals our total plus our
  6103. 4:57:00roll and this is within the for Loop so
  6104. 4:57:04Java does want us to initialize this
  6105. 4:57:07variable so set total equal to zero when
  6106. 4:57:10you declare it so total equals total
  6107. 4:57:14whatever it currently is in this case
  6108. 4:57:16zero plus roll we can shorten this too
  6109. 4:57:19we could just say total plus equals roll
  6110. 4:57:22we'll use the augmented assignment
  6111. 4:57:24operator for
  6112. 4:57:25addition then when we escape the for
  6113. 4:57:28Loop we will print the
  6114. 4:57:31total let's say total colon space plus
  6115. 4:57:35our variable of total let's do a test
  6116. 4:57:38run I Will Roll three dice we have 3 2 2
  6117. 4:57:44for a total of seven 3 5 7 okay we know
  6118. 4:57:49that that
  6119. 4:57:50works we have rolled all the dice and
  6120. 4:57:52get the
  6121. 4:57:55total now here comes the hard part we're
  6122. 4:57:57going to display some ASI art based on
  6123. 4:58:00the result of each die that is
  6124. 4:58:02rolled so we're going to declare a
  6125. 4:58:04method to take care of that for
  6126. 4:58:07us let's do so outside of the main
  6127. 4:58:09method
  6128. 4:58:11we will create a static method that's
  6129. 4:58:14not going to return anything the return
  6130. 4:58:16type is void let's name this method
  6131. 4:58:20print
  6132. 4:58:21D D is the singular version of the word
  6133. 4:58:25dice we're going to set up one parameter
  6134. 4:58:28int roll what did we roll we're going to
  6135. 4:58:31call this method before we display the
  6136. 4:58:34output of you rolled whatever the RO
  6137. 4:58:37is we will call the method of printing
  6138. 4:58:41die and pass in our
  6139. 4:58:43roll it's going to be a number between 1
  6140. 4:58:46and
  6141. 4:58:47six let's head back to our print die
  6142. 4:58:50method within the print die method we're
  6143. 4:58:53going to create some asky art the asky
  6144. 4:58:55art is going to be made of strings let's
  6145. 4:58:57create a string of dice one equals now
  6146. 4:59:01instead of a standard string that takes
  6147. 4:59:03up one line we're going to create a
  6148. 4:59:05multi-line string we need a triple set
  6149. 4:59:09of double quotes
  6150. 4:59:12anything between these two lines is a
  6151. 4:59:14multi-line string it's within here that
  6152. 4:59:17we're going to create some dice some asy
  6153. 4:59:19art of dice so this will be an art
  6154. 4:59:21lesson today let's start with seven
  6155. 4:59:26dashes on the next line we'll create a
  6156. 4:59:28vertical bar and on the other side we
  6157. 4:59:31need a vertical bar
  6158. 4:59:33too I'm going to shift these dashes over
  6159. 4:59:35by one same thing with the vertical bar
  6160. 4:59:39we need a another set of vertical
  6161. 4:59:41bars another
  6162. 4:59:43set and then we can copy our dashes
  6163. 4:59:47again so we should have a shape like
  6164. 4:59:50this right in the middle we're going to
  6165. 4:59:52add a bullet point the easiest way to do
  6166. 4:59:54this is to pull up the charm map
  6167. 4:59:57application so if you hold windows and
  6168. 5:00:00type R you can look up charm map hit
  6169. 5:00:05enter if you scroll all the way down to
  6170. 5:00:07the bottom and up a little bit
  6171. 5:00:13it there should be a bullet
  6172. 5:00:16point let's select it copy it close
  6173. 5:00:20character
  6174. 5:00:21map and paste that bullet point or
  6175. 5:00:24otherwise you can find it from someplace
  6176. 5:00:26else online let's print it just
  6177. 5:00:29once I'm just going to print dice
  6178. 5:00:33one let's do a test run I'm just going
  6179. 5:00:36to roll one dice and this isn't going to
  6180. 5:00:38be accurate right away
  6181. 5:00:40but we do have an asky art of a
  6182. 5:00:42one-sided
  6183. 5:00:46dice then we need dice two really we can
  6184. 5:00:50copy everything for dice one paste it
  6185. 5:00:53change dice one to dice two and move
  6186. 5:00:56this bullet point
  6187. 5:00:58around so this is Dice two then we need
  6188. 5:01:02dice
  6189. 5:01:03three dice
  6190. 5:01:05three there will be a bullet right in
  6191. 5:01:08the middle
  6192. 5:01:11dice
  6193. 5:01:15four we need the bullets in each
  6194. 5:01:19corner and none in the center that's
  6195. 5:01:21dice
  6196. 5:01:23four dice
  6197. 5:01:27five we'll have a bullet point right in
  6198. 5:01:30the middle along with the corners and
  6199. 5:01:32dice
  6200. 5:01:38six we have two rows of bullet points
  6201. 5:01:42and those are the dice that we
  6202. 5:01:44need now we'll create an enhanced
  6203. 5:01:49switch what are we
  6204. 5:01:52examining so going back to our
  6205. 5:01:54parameters right here we're examining
  6206. 5:01:56the role that we pass in it's going to
  6207. 5:01:59be a number 1 through six we'll examine
  6208. 5:02:01our role against matching
  6209. 5:02:03cases if roll matches a case of one
  6210. 5:02:07we'll do this
  6211. 5:02:10we will print rather than print
  6212. 5:02:13line dice
  6213. 5:02:17one dice one is this image the
  6214. 5:02:24string if roll matches a case of two we
  6215. 5:02:28will print dice
  6216. 5:02:30two if Case is three we will print dice
  6217. 5:02:34three four if Case is four print dice
  6218. 5:02:39four
  6219. 5:02:42five print dice five and
  6220. 5:02:46six print dice
  6221. 5:02:49six and a default case we might as well
  6222. 5:02:52add
  6223. 5:02:54one if there are no matches we'll print
  6224. 5:02:56the
  6225. 5:02:58following we'll say invalid
  6226. 5:03:02roll all right and that's it let's run
  6227. 5:03:06this enter the number of dice to roll
  6228. 5:03:09I'll roll three
  6229. 5:03:11Dice and here's what we got I rolled a
  6230. 5:03:14six a two and a
  6231. 5:03:17six all right everybody so that is a
  6232. 5:03:20dice rolling program that you can make
  6233. 5:03:22using
  6234. 5:03:25Java what is going on everybody in this
  6235. 5:03:28video I'm going to discuss arrays in
  6236. 5:03:30Java an array is a collection of values
  6237. 5:03:33of the same data type as a beginner it
  6238. 5:03:35might be helpful to think of an array as
  6239. 5:03:38a variable that can store more than one
  6240. 5:03:40value it's a little more complicated
  6241. 5:03:42than that but that might be a good way
  6242. 5:03:43to think of it let me give you a
  6243. 5:03:46demonstration suppose we have a string
  6244. 5:03:48variable a fruit pick a fruit I will
  6245. 5:03:52pick an
  6246. 5:03:53apple and then you know we can print our
  6247. 5:03:55variable of
  6248. 5:03:57fruit which would give me an
  6249. 5:04:00Apple well what if I told you that this
  6250. 5:04:03variable can store more than one value
  6251. 5:04:05if we were to turn it into an array
  6252. 5:04:08here's how after the data type of our
  6253. 5:04:10variable add a set of straight brackets
  6254. 5:04:13enclose any values within a set of curly
  6255. 5:04:16braces and there we go we now have an
  6256. 5:04:19array we have an array of
  6257. 5:04:22strings this array can store more than
  6258. 5:04:24one value but they all have to be the
  6259. 5:04:26same data type in this case strings
  6260. 5:04:30let's add some more fruit let's add an
  6261. 5:04:34orange a
  6262. 5:04:36banana and a coconut
  6263. 5:04:40the name of your array should be
  6264. 5:04:42descriptive of what it contains let's
  6265. 5:04:44say fruits instead of
  6266. 5:04:46fruit technically in the English
  6267. 5:04:48language fruit would still be plural but
  6268. 5:04:51by adding an S it's more descriptive
  6269. 5:04:53that it contains more than one value I
  6270. 5:04:55know English is a weird language at
  6271. 5:04:57times if I was to print my array of
  6272. 5:05:00fruits here's the
  6273. 5:05:03result while we get a memory address
  6274. 5:05:06arrays fall under the category of a
  6275. 5:05:08reference data type if we access the
  6276. 5:05:11name of our array we're given a memory
  6277. 5:05:13address each value within an array is
  6278. 5:05:16known as an element to access an element
  6279. 5:05:19we have to indicate an index number
  6280. 5:05:22after our array name let's add a set of
  6281. 5:05:24straight brackets and then an index
  6282. 5:05:27number as is the case with many things
  6283. 5:05:29in programming toxis the first of
  6284. 5:05:32something it usually has an index of
  6285. 5:05:34zero that applies to arrays as well
  6286. 5:05:37print our array of fruits at index of
  6287. 5:05:40zero that's going to give you the first
  6288. 5:05:42element of Apple for the next element
  6289. 5:05:45that would be an index of
  6290. 5:05:47one which is
  6291. 5:05:50orange
  6292. 5:05:52two would be
  6293. 5:05:54banana
  6294. 5:05:56three coconut if we attempt to access an
  6295. 5:05:59element that doesn't exist here's what
  6296. 5:06:03happens we get an exception an exception
  6297. 5:06:06is similar to an error we have an array
  6298. 5:06:09in index out of bounds
  6299. 5:06:11exception this error message appears
  6300. 5:06:13when you try and access an element that
  6301. 5:06:15doesn't exist within an array there is
  6302. 5:06:18no index of four there's only 0 1 2 3 in
  6303. 5:06:22this case now you do have the capability
  6304. 5:06:26to change the value at a given
  6305. 5:06:28index to do so we will take the name of
  6306. 5:06:30our array in this case fruits specify an
  6307. 5:06:34index using the set of straight brackets
  6308. 5:06:37at index of zero let's set that equal to
  6309. 5:06:40a string of pineapple rather than
  6310. 5:06:43Apple and then we'll print it print our
  6311. 5:06:46array of fruits at index of
  6312. 5:06:49zero that would give us a pineapple
  6313. 5:06:52rather than an apple by specifying the
  6314. 5:06:55index you can change any element so
  6315. 5:06:58fruits at index of one is now a
  6316. 5:07:03pineapple if at any time you need the
  6317. 5:07:05length of an array there's a built-in
  6318. 5:07:07property for that let's say say we have
  6319. 5:07:09an integer variable of
  6320. 5:07:12num of fruits meaning number of
  6321. 5:07:16fruits to access the length of an array
  6322. 5:07:19take our array in this case
  6323. 5:07:22fruits use dot which is the axis
  6324. 5:07:25modifier then type
  6325. 5:07:28length give me the length of my array of
  6326. 5:07:32fruits how many elements are within this
  6327. 5:07:34array take a
  6328. 5:07:37guess four
  6329. 5:07:40there's four elements within this array
  6330. 5:07:421 2 3 4 if you ever need the length of
  6331. 5:07:46an array use the length property of your
  6332. 5:07:48array this will return an integer which
  6333. 5:07:51you can assign to a variable if you
  6334. 5:07:53would like if you need to print all of
  6335. 5:07:55the elements of an array you could use a
  6336. 5:07:57for Loop here's
  6337. 5:08:01how well declare for
  6338. 5:08:04Loop we'll create an index of I equal to
  6339. 5:08:07zero
  6340. 5:08:09how many times do we want to iterate
  6341. 5:08:11this Loop let's continue as long as I is
  6342. 5:08:15less than we need to determine the
  6343. 5:08:17length of this array well we can use
  6344. 5:08:19that length property that we discussed
  6345. 5:08:21take our array of fruits access the
  6346. 5:08:24length property this should return the
  6347. 5:08:27number four continue as long as I is
  6348. 5:08:30less than four then we will increment
  6349. 5:08:32our index of I so our for Loop in this
  6350. 5:08:35case is going to iterate four times
  6351. 5:08:39let's perform a test run if I was to
  6352. 5:08:41print my array of fruits at index of
  6353. 5:08:44zero this is what happens
  6354. 5:08:48exactly it's going to print fruits at
  6355. 5:08:50index zero four times apple apple apple
  6356. 5:08:55apple what I need is to print each
  6357. 5:08:58element of this array we could use our
  6358. 5:09:00index V for that so replace zero with i
  6359. 5:09:04i is going to change during each
  6360. 5:09:06iteration of this loop we're increasing
  6361. 5:09:09it by one after we complete each cycle
  6362. 5:09:11of the loop so here's what happens now
  6363. 5:09:15we get all the elements of our array
  6364. 5:09:18Apple orange banana
  6365. 5:09:20coconut this index of I is going to
  6366. 5:09:23increase after each cycle of this Loop
  6367. 5:09:25each
  6368. 5:09:26iteration alternatively rather than
  6369. 5:09:29printing each fruit on a new line we
  6370. 5:09:31could use
  6371. 5:09:32print then after printing each element
  6372. 5:09:35we could add a space
  6373. 5:09:38character that would print each element
  6374. 5:09:40and separate each with a space if you so
  6375. 5:09:44choose there's something called an
  6376. 5:09:46enhanced for Loop an enhanced for Loop
  6377. 5:09:49will cycle once for each element within
  6378. 5:09:51an
  6379. 5:09:52array we'll start with a normal for Loop
  6380. 5:09:55to create an enhanced for Loop within
  6381. 5:09:57the set of parentheses we will list the
  6382. 5:10:00data type of each element of the array
  6383. 5:10:02in this case
  6384. 5:10:05strings now we need a unique identifier
  6385. 5:10:07for each element within the Sur array
  6386. 5:10:10since we're working with an array of
  6387. 5:10:12fruits let's say fruit
  6388. 5:10:14singular then we need a colon then our
  6389. 5:10:17array that we're iterating through in
  6390. 5:10:20this case
  6391. 5:10:22fruits basically we're saying for every
  6392. 5:10:25fruit in my array of fruits do
  6393. 5:10:29this let's print each
  6394. 5:10:32fruit fruit will be the current element
  6395. 5:10:34we're cycling
  6396. 5:10:36through here's what happens we print
  6397. 5:10:39each fruit we have all the elements of
  6398. 5:10:42our array of fruits Apple orange banana
  6399. 5:10:46coconut this is an enhanced for Loop
  6400. 5:10:49also known as a 4 each Loop it
  6401. 5:10:51simplifies iterating through a
  6402. 5:10:53collection such as an
  6403. 5:10:55array if you need to sort your array
  6404. 5:10:58there's a built-in sort method we're
  6405. 5:11:00going to access the class of arrays you
  6406. 5:11:03may need to import it import class
  6407. 5:11:07arrays so make sure you have this import
  6408. 5:11:11import
  6409. 5:11:12java.util.arrays
  6410. 5:11:14this class provides a lot of
  6411. 5:11:16functionality when working with arrays
  6412. 5:11:19there's a built-in sort
  6413. 5:11:21method we're going to pass in our array
  6414. 5:11:24of fruits to this method we'll end up
  6415. 5:11:26sorting the elements in this array
  6416. 5:11:32alphabetically originally the order of
  6417. 5:11:34our elements was Apple orange banana
  6418. 5:11:37coconut but after sorting there they're
  6419. 5:11:39now apple banana coconut orange they're
  6420. 5:11:42all arranged alphabetically or if these
  6421. 5:11:45were numbers they would be arranged in
  6422. 5:11:47ascending
  6423. 5:11:49order there's also a fill method with
  6424. 5:11:51arrays we can fill all the elements of
  6425. 5:11:53an array with a given value again we're
  6426. 5:11:56going to access the arrays class called
  6427. 5:11:59The Fill
  6428. 5:12:00method pass in our array comma the value
  6429. 5:12:04we're going to fill the array with let's
  6430. 5:12:07fill our array with pineapples
  6431. 5:12:11then we'll use a for each Loop to print
  6432. 5:12:13each
  6433. 5:12:14element and here's our new array we have
  6434. 5:12:17filled our array of fruits with the
  6435. 5:12:20value of
  6436. 5:12:21pineapple all right everybody that's an
  6437. 5:12:24introduction to arrays think of an array
  6438. 5:12:26as a variable but it can store more than
  6439. 5:12:28one value and well everybody those are
  6440. 5:12:31arrays in
  6441. 5:12:34Java what is going on everybody in this
  6442. 5:12:36video I'm going to show you how we can
  6443. 5:12:38enter in user input into an array so
  6444. 5:12:40let's get started before assigning
  6445. 5:12:42values into an array our compiler needs
  6446. 5:12:44to know the size of the array beforehand
  6447. 5:12:47we have to first allocate space for the
  6448. 5:12:49array let's begin by creating an array
  6449. 5:12:51of strings we'll create an array of
  6450. 5:12:53strings we'll enter in some food I will
  6451. 5:12:56name this array Foods equals add a set
  6452. 5:12:59of krly braces and then within this
  6453. 5:13:01array you can enter in some values pick
  6454. 5:13:03some food that you like I'll enter in a
  6455. 5:13:06pizza
  6456. 5:13:07taco and and
  6457. 5:13:12hamburger the size of this array is
  6458. 5:13:15three
  6459. 5:13:16elements once you set a size for an
  6460. 5:13:18array normally you can't change it this
  6461. 5:13:20array will have three elements however
  6462. 5:13:23all the elements are already filled with
  6463. 5:13:25values to create an empty array you
  6464. 5:13:27don't want a set of empty curly braces
  6465. 5:13:30then we're creating an array of no
  6466. 5:13:31elements just to demonstrate that I'm
  6467. 5:13:34going to print the length of our array
  6468. 5:13:36Foods foods. length
  6469. 5:13:41this is going to give us
  6470. 5:13:43zero and we can't assign any values even
  6471. 5:13:46if I attempt to let's say Foods at index
  6472. 5:13:50zero equals a string of pizza here's
  6473. 5:13:55what
  6474. 5:13:57happens well we have an exception and
  6475. 5:14:00array index out of bounds exception
  6476. 5:14:03before assigning values we'll want to be
  6477. 5:14:05sure that we're setting a size of the
  6478. 5:14:07array even if it is empty instead of
  6479. 5:14:10assigning a set of values we'll create
  6480. 5:14:12an empty array we'll set our array equal
  6481. 5:14:15to use the new keyword type the data
  6482. 5:14:18type of our array straight brackets and
  6483. 5:14:22within the straight brackets we'll set a
  6484. 5:14:24size a number so let's say three we're
  6485. 5:14:28creating an empty array that has space
  6486. 5:14:30for three elements three values let's
  6487. 5:14:33try this
  6488. 5:14:35again the length of our array is three
  6489. 5:14:39and we can assign the values so let's
  6490. 5:14:41say Foods at index
  6491. 5:14:43one equals a
  6492. 5:14:46taco Foods at index 2 will be a
  6493. 5:14:55hamburger I'll print all the foods in
  6494. 5:14:57this array we can use an enhanced for
  6495. 5:15:00Loop list the data type we're working
  6496. 5:15:02with an array of strings for every food
  6497. 5:15:05in our array of
  6498. 5:15:07foods we will print each
  6499. 5:15:11food that would give us pizza Taco
  6500. 5:15:16hamburger we have declared and
  6501. 5:15:18instantiated an empty array and filled
  6502. 5:15:21in the values
  6503. 5:15:22later let's modify this program so a
  6504. 5:15:25user can type in some values let's
  6505. 5:15:27delete these lines if we're accepting
  6506. 5:15:29user input we will need a scanner we'll
  6507. 5:15:32declare that and instantiate it scanner
  6508. 5:15:34scanner equals new
  6509. 5:15:36scanner within the set of parentheses
  6510. 5:15:39typ
  6511. 5:15:40system.in import the class for
  6512. 5:15:42scanner import java.util.scanner and
  6513. 5:15:46close the scanner when you're done
  6514. 5:15:48because I sometimes forget to do that
  6515. 5:15:50still scanner.
  6516. 5:15:52close so we have an empty array of three
  6517. 5:15:56elements we'll create a for Loop to
  6518. 5:15:59iterate over the length of our array of
  6519. 5:16:02foods so four we'll create a counter of
  6520. 5:16:06in I equals z will continue as long as I
  6521. 5:16:10is less
  6522. 5:16:11than our array of foods length property
  6523. 5:16:15which returns three because there's
  6524. 5:16:17three elements then increment I by One
  6525. 5:16:21during each iteration of this Loop let's
  6526. 5:16:23do the
  6527. 5:16:24following we'll output the following
  6528. 5:16:27prompt enter a
  6529. 5:16:30food I'll use print instead of print
  6530. 5:16:32line because I prefer
  6531. 5:16:34that we'll assign our array of foods at
  6532. 5:16:38index of I I changes during each cycle
  6533. 5:16:42at first it's zero then 1 then two then
  6534. 5:16:45three we'll set that equal to use our
  6535. 5:16:49scanner to accept some user input call
  6536. 5:16:51the next line
  6537. 5:16:54method okay let's perform a test run
  6538. 5:16:57currently we have an array with three
  6539. 5:16:59empty elements we'll cycle this loop
  6540. 5:17:02three times because the length property
  6541. 5:17:04of the array is three then print all the
  6542. 5:17:07elements when we're done
  6543. 5:17:09here's what happens exactly enter a food
  6544. 5:17:13pizza Taco
  6545. 5:17:16hamburger here are the elements within
  6546. 5:17:18our array of food pizza Taco
  6547. 5:17:22hamburger let's say that we change the
  6548. 5:17:25size of the array let's say for this
  6549. 5:17:29time Pizza Taco
  6550. 5:17:32hamburger hot
  6551. 5:17:36dog Pizza Taco hamburger hot dog
  6552. 5:17:40we're going to make a few changes
  6553. 5:17:41instead of setting a static size for the
  6554. 5:17:43array we'll have a user enter in the
  6555. 5:17:46number of food they would like to enter
  6556. 5:17:48we'll replace this number with a
  6557. 5:17:49variable to determine the size what
  6558. 5:17:52we'll do now is we will declare our
  6559. 5:17:55string array of foods but instantiate it
  6560. 5:17:58later once we know the
  6561. 5:18:00size we'll
  6562. 5:18:02output what number of food do you want
  6563. 5:18:10I'll use print instead of print
  6564. 5:18:12line I'll also create an integer
  6565. 5:18:14variable of size I'll declare it but not
  6566. 5:18:17yet fill it in until after our
  6567. 5:18:20prompt let's set the size the size of
  6568. 5:18:23the array that is equal to use our
  6569. 5:18:26scanner and call the next int method we
  6570. 5:18:31are accepting an
  6571. 5:18:32integer once we know the size of the
  6572. 5:18:34array we can finish instantiating it
  6573. 5:18:38let's take our array of foods that we've
  6574. 5:18:40declared but have not yet assigned and
  6575. 5:18:43we'll assign it our array is made up of
  6576. 5:18:45strings we'll use the new keyword new
  6577. 5:18:49list the data type
  6578. 5:18:51string it's an array so we need a set of
  6579. 5:18:53straight
  6580. 5:18:54brackets and instead of a number we're
  6581. 5:18:57going to use our variable the user is
  6582. 5:18:59going to determine what this number is
  6583. 5:19:00going to
  6584. 5:19:02be there's one final step this is
  6585. 5:19:04specific for scanners since we're
  6586. 5:19:06accepting an integer and then we're
  6587. 5:19:08accepting a string for our user input we
  6588. 5:19:11have to clear the input buffer next
  6589. 5:19:13integer isn't going to pick up the new
  6590. 5:19:15line character at the end so just to
  6591. 5:19:18demonstrate
  6592. 5:19:19that let's say we want one food
  6593. 5:19:23item there's still that new line
  6594. 5:19:25character within the input buffer we
  6595. 5:19:26didn't get the opportunity to enter in a
  6596. 5:19:28food we are just going to clear our
  6597. 5:19:30scanner by using scanner. next line and
  6598. 5:19:34that will clear it
  6599. 5:19:36up all right let's try this again
  6600. 5:19:40what number of food do you want I want
  6601. 5:19:43five five food items enter in some food
  6602. 5:19:47pizza
  6603. 5:19:49Taco
  6604. 5:19:51hamburger hot dog and
  6605. 5:19:54Sushi and here's our food pizza Taco
  6606. 5:19:58hamburger hot dog
  6607. 5:20:00sushi in summary to enter values into an
  6608. 5:20:03array you'll likely want to create an
  6609. 5:20:05empty array but you need to know the
  6610. 5:20:07size of the array first
  6611. 5:20:09if the size of the array is
  6612. 5:20:10predetermined you can always set that to
  6613. 5:20:11be a number otherwise if one way or
  6614. 5:20:14another if a user is going to determine
  6615. 5:20:16the size of the array you'll have to get
  6616. 5:20:18that from the user and you can set the
  6617. 5:20:20size of the array to whatever the user
  6618. 5:20:21types in just be sure to do that before
  6619. 5:20:24filling in the values if you would like
  6620. 5:20:26a copy of this code you can look in the
  6621. 5:20:27comment section down below be sure that
  6622. 5:20:29you're looking in the playlist and well
  6623. 5:20:31everybody that is how to enter and user
  6624. 5:20:33data into an array using
  6625. 5:20:37Java what is is going on everybody in
  6626. 5:20:39this video I'm going to show you how we
  6627. 5:20:41can search through the elements of an
  6628. 5:20:42array let's begin we'll need an array to
  6629. 5:20:45work with to keep the simple let's
  6630. 5:20:47create an array of integers we'll create
  6631. 5:20:49an array of integers named numbers
  6632. 5:20:52equals make up some numbers to put
  6633. 5:20:54within this array I'll just type
  6634. 5:20:56whatever comes to
  6635. 5:21:01mind good enough we'll also need a
  6636. 5:21:03Target what number are we searching for
  6637. 5:21:06let's say I'm searching for the number
  6638. 5:21:08number two I will create an integer
  6639. 5:21:11variable of Target set it equal to
  6640. 5:21:15two in data structures and algorithms
  6641. 5:21:18we'll be performing what is known as a
  6642. 5:21:19linear search we're going to use a for
  6643. 5:21:22Loop to iterate through these elements
  6644. 5:21:24and then see if there's a match we'll
  6645. 5:21:27create a for Loop create an index of I I
  6646. 5:21:31will equal zero we'll continue as long
  6647. 5:21:33as I is less than R array of numbers
  6648. 5:21:37access the length property that will
  6649. 5:21:40return the length as a number then
  6650. 5:21:42increment I by
  6651. 5:21:44One I will check using an if
  6652. 5:21:47statement if our
  6653. 5:21:50Target is
  6654. 5:21:52equal to our array at the current index
  6655. 5:21:56of I at first I will be zero and then we
  6656. 5:22:00increment it during each cycle of this
  6657. 5:22:03Loop if our Target equals the current
  6658. 5:22:06element let's print the following
  6659. 5:22:10let's say element found at
  6660. 5:22:15index plus I that's our
  6661. 5:22:19index in case we find the target for
  6662. 5:22:21example two we don't need to search
  6663. 5:22:24through the rest of the array we could
  6664. 5:22:26add what is known as a break statement
  6665. 5:22:28to break out of the
  6666. 5:22:29loop if we find the target break out of
  6667. 5:22:32the loop let's perform a test run we're
  6668. 5:22:36searching for the number two
  6669. 5:22:38and we'll be given the index element
  6670. 5:22:41found at index
  6671. 5:22:432 let's look for
  6672. 5:22:46one one is found at index
  6673. 5:22:50zero four which is near the end is found
  6674. 5:22:54at index 6 0 1 2 3 4 5 6 what if we
  6675. 5:23:01don't find our Target I don't have the
  6676. 5:23:03number seven in
  6677. 5:23:06here well there's no output
  6678. 5:23:10since this if statement is never true
  6679. 5:23:12once we reach the end of the array we
  6680. 5:23:15don't do anything so let's add a Boolean
  6681. 5:23:19variable of is
  6682. 5:23:22found and set it to be
  6683. 5:23:25false we'll check if is found is false
  6684. 5:23:29if that is the case we'll output a
  6685. 5:23:31message that element wasn't found in the
  6686. 5:23:33array so if we do find our Target we'll
  6687. 5:23:37take our Boolean variable of is found
  6688. 5:23:40and set it to be true we have found our
  6689. 5:23:43Target that we're looking
  6690. 5:23:46for if we exit the for Loop and never
  6691. 5:23:48find our Target we can check
  6692. 5:23:52that we'll say is
  6693. 5:23:55found but we'll use the not logical
  6694. 5:23:58operator the not logical operator gives
  6695. 5:24:02you the inverse of a Boolean value or
  6696. 5:24:05variable if is found is false if
  6697. 5:24:08something isn't true if we don't find
  6698. 5:24:10the target then we execute this if
  6699. 5:24:13statement where we will print the
  6700. 5:24:15following let's say something such as
  6701. 5:24:19element not found in the
  6702. 5:24:24array all right we are looking for the
  6703. 5:24:26number seven which does not exist within
  6704. 5:24:29this
  6705. 5:24:30array that would give us this message
  6706. 5:24:33element not found in the array because
  6707. 5:24:36is found was false we could never find
  6708. 5:24:39it within this for
  6709. 5:24:41Loop so we're checking if is found is
  6710. 5:24:44not
  6711. 5:24:46true what if we have an array of strings
  6712. 5:24:49and we're searching for an element well
  6713. 5:24:51instead of using the comparison operator
  6714. 5:24:53we have to use the equals method of
  6715. 5:24:55strings because strings are a reference
  6716. 5:24:57data type so this time let's say we have
  6717. 5:24:59an array of
  6718. 5:25:01strings named fruits think of some
  6719. 5:25:05fruit I will pick an apple
  6720. 5:25:09an
  6721. 5:25:11orange and a
  6722. 5:25:13banana the target this time will be a
  6723. 5:25:19string let's say I'm searching for an
  6724. 5:25:24orange will iterate through our array of
  6725. 5:25:27fruits the condition for the for Loop
  6726. 5:25:30will be I is less than the length of our
  6727. 5:25:32array of fruits now we can't necessarily
  6728. 5:25:36say is our Target equal to
  6729. 5:25:39fruits at index of
  6730. 5:25:41I we're comparing two strings and since
  6731. 5:25:44strings are reference data types we
  6732. 5:25:46would end up comparing the memory
  6733. 5:25:48addresses of two different strings we
  6734. 5:25:50would be saying are these two memory
  6735. 5:25:52addresses the same are they at the same
  6736. 5:25:55location which isn't the case if we're
  6737. 5:25:57comparing the value of strings we'll use
  6738. 5:26:00the equals
  6739. 5:26:01method let's take fruits at index of I
  6740. 5:26:05call the built-in equals method of
  6741. 5:26:07strings and pass in our Target as an
  6742. 5:26:11argument now this should work we are
  6743. 5:26:14searching for an
  6744. 5:26:17orange element found at index one what
  6745. 5:26:21about a
  6746. 5:26:26pineapple element not found in the array
  6747. 5:26:30there was no pineapple let's modify this
  6748. 5:26:33program slightly we'll accept some user
  6749. 5:26:35input but we'll need a scanner
  6750. 5:26:44import the
  6751. 5:26:46class java.util.scanner and close your
  6752. 5:26:49scanner when you're done with it
  6753. 5:26:51scanner.
  6754. 5:26:53close instead of assigning our Target
  6755. 5:26:56we'll accept user input we'll use our
  6756. 5:26:58scanner use the next line
  6757. 5:27:02method and we will also need a
  6758. 5:27:06prompt let's print the following
  6759. 5:27:09enter a fruit to search
  6760. 5:27:16for just to make this a little more
  6761. 5:27:18clean I'm going to move this variable of
  6762. 5:27:20is found to the top
  6763. 5:27:23here I'll declare our string of Target
  6764. 5:27:26but not assign it and then after the
  6765. 5:27:28prompt I'll assign our name of Target
  6766. 5:27:31this is just how I like to arrange
  6767. 5:27:33things but it all works the same now we
  6768. 5:27:36can search for an element after acting
  6769. 5:27:38user input and I will convert this to be
  6770. 5:27:40a print statement instead of print line
  6771. 5:27:43all right let's search for some fruit is
  6772. 5:27:46there a coconut element not found in the
  6773. 5:27:52array perhaps a banana element found at
  6774. 5:27:56index 2 0 1 2 all right everybody so
  6775. 5:28:01that's how to search through an array
  6776. 5:28:03using
  6777. 5:28:06Java hello everybody in this video I'm
  6778. 5:28:08going to explain variable arguments also
  6779. 5:28:11known as VAR args in Java variable
  6780. 5:28:14arguments allow a method to accept a
  6781. 5:28:17varying number of arguments they make
  6782. 5:28:20methods more flexible and there's no
  6783. 5:28:22need for overloaded methods overloaded
  6784. 5:28:25methods are methods that share the same
  6785. 5:28:27name but have different
  6786. 5:28:29parameters in this example I have
  6787. 5:28:31several add methods they each have
  6788. 5:28:33different amounts of parameters if I was
  6789. 5:28:36to add a few numbers together for
  6790. 5:28:38example I'll add 1 + 2 + 3 well then I
  6791. 5:28:43would use this method what if I told you
  6792. 5:28:45instead of creating several different
  6793. 5:28:46methods that basically do the same thing
  6794. 5:28:49we could instead create one method that
  6795. 5:28:51accepts any number of
  6796. 5:28:53arguments we'll only need to declare one
  6797. 5:28:57method we will create one add method
  6798. 5:29:00that accepts a varying amount of
  6799. 5:29:02arguments so let's create a method the
  6800. 5:29:05return type let's say is an integer
  6801. 5:29:08we'll create a method of add our method
  6802. 5:29:11will have one parameter what is the data
  6803. 5:29:14type of the values we're sending this
  6804. 5:29:15method in this case
  6805. 5:29:18integers we're going to follow the data
  6806. 5:29:20type with three dots and
  6807. 5:29:23ellipses how this works is that the Java
  6808. 5:29:26compiler is going to pack the arguments
  6809. 5:29:28we send it into an array we'll need a
  6810. 5:29:31name for this array let's say
  6811. 5:29:35numbers just to demonstrate this
  6812. 5:29:38temporarily I'm going to print my array
  6813. 5:29:40of
  6814. 5:29:43numbers when we call the add method we
  6815. 5:29:46can send any number of arguments for
  6816. 5:29:48example 1 2 3 4 and they're going to be
  6817. 5:29:52packed into an
  6818. 5:29:53array within this method we're now
  6819. 5:29:56working with an array let's perform a
  6820. 5:29:58test
  6821. 5:29:59run let's return void
  6822. 5:30:03actually so we're given a memory address
  6823. 5:30:06of this array now let's actually add
  6824. 5:30:09these numbers
  6825. 5:30:10together we need to cycle through the
  6826. 5:30:12elements of this array we can use a for
  6827. 5:30:15Loop or an enhanced for Loop for that so
  6828. 5:30:17let's return an integer in this
  6829. 5:30:20case let's declare a local variable of
  6830. 5:30:24sum I will set that to be
  6831. 5:30:29zero let's create an enhanced for
  6832. 5:30:32Loop the data type will be int for every
  6833. 5:30:35number in my array of numbers
  6834. 5:30:39let's take our sum and add the current
  6835. 5:30:43number sum plus equals
  6836. 5:30:45number then at the end we will return
  6837. 5:30:48our
  6838. 5:30:50sum and there we go and then we need to
  6839. 5:30:53print the output I'm going to cut this
  6840. 5:30:56and paste it within a print
  6841. 5:30:59statement let's perform a test run the
  6842. 5:31:02sum when we add these four numbers
  6843. 5:31:04together the sum is 10 and we can pass
  6844. 5:31:07in any number of
  6845. 5:31:11arguments the result is now
  6846. 5:31:1421 basically the Java compiler is
  6847. 5:31:16packing all of these arguments into an
  6848. 5:31:19array when we send them to a method then
  6849. 5:31:22within the context of this method we
  6850. 5:31:24would just need to work with this array
  6851. 5:31:26one way or another but it's a lot easier
  6852. 5:31:28than having several different overloaded
  6853. 5:31:30methods okay this time let's create a
  6854. 5:31:32new method we'll create a method of
  6855. 5:31:35average average will find the average
  6856. 5:31:37value of whatever we send it so we will
  6857. 5:31:40create a static method that's going to
  6858. 5:31:42return a double this time the method
  6859. 5:31:45name will be
  6860. 5:31:47average what is the data type of what
  6861. 5:31:50we're sending let's say double then we
  6862. 5:31:52need the ellipses the three dots then a
  6863. 5:31:56unique name for this array let's say
  6864. 5:31:59numbers so to find the average we can
  6865. 5:32:01perform the following
  6866. 5:32:03code let's say we have
  6867. 5:32:06double sum
  6868. 5:32:09I will set that equal to
  6869. 5:32:11zero we will create an enhanced for Loop
  6870. 5:32:15the data type is double for every number
  6871. 5:32:19in my array of numbers do the following
  6872. 5:32:22we will take our
  6873. 5:32:24sum and add the current number sum plus
  6874. 5:32:27equals
  6875. 5:32:29number then at the end we will return
  6876. 5:32:33sum divided by now since we're working
  6877. 5:32:36with an array there is a built-in length
  6878. 5:32:39property numbers. length to find the
  6879. 5:32:43average you take all the numbers add
  6880. 5:32:45them together then divide it by the
  6881. 5:32:47amount of numbers that you
  6882. 5:32:50have let's perform a test
  6883. 5:32:52run I'm going to
  6884. 5:32:55print call the average
  6885. 5:32:57method let's find the average of a few
  6886. 5:33:00numbers I don't know what that's going
  6887. 5:33:02to be so the average of these four
  6888. 5:33:05numbers is 2.5
  6889. 5:33:09I can pass in any number of
  6890. 5:33:12arguments so the average of these
  6891. 5:33:14numbers is four
  6892. 5:33:184.0 you can even pass in no arguments if
  6893. 5:33:21you so
  6894. 5:33:23choose but with this method that we have
  6895. 5:33:25created here's what
  6896. 5:33:27happens we're actually dividing by zero
  6897. 5:33:31because the length of our array is zero
  6898. 5:33:33there's no elements that we're passing
  6899. 5:33:35in in this specific example let's create
  6900. 5:33:38an if
  6901. 5:33:39statement if our array of
  6902. 5:33:42numbers if the length of it we can
  6903. 5:33:45access the length property is equal to
  6904. 5:33:48zero let's return zero instead of
  6905. 5:33:52receiving noted number we're calling the
  6906. 5:33:55average method but passing in no
  6907. 5:33:57arguments this
  6908. 5:33:59time that would give us zero because the
  6909. 5:34:02length of our array is zero because
  6910. 5:34:05we're passing in no arguments I already
  6911. 5:34:07everybody so those are variable
  6912. 5:34:09arguments they allow a method to accept
  6913. 5:34:12a varying number of arguments it makes
  6914. 5:34:14methods more flexible and there's no
  6915. 5:34:16need for overloaded methods the Java
  6916. 5:34:19compiler will pack the arguments into an
  6917. 5:34:21array when setting up the parameters of
  6918. 5:34:23a method declare the data type and add a
  6919. 5:34:26suffix of an ellipsis Three Dots and
  6920. 5:34:30well everybody those are variable
  6921. 5:34:32arguments in
  6922. 5:34:35Java hey there everybody so today I'm
  6923. 5:34:38going to explain 2D arrays in Java also
  6924. 5:34:41known as multi-dimensional arrays a
  6925. 5:34:43two-dimensional array is an array where
  6926. 5:34:46each element is an array it's useful for
  6927. 5:34:49storing a matrix of data let me give you
  6928. 5:34:52an example let's say we have some basic
  6929. 5:34:54one-dimensional arrays I will create a
  6930. 5:34:57string array of
  6931. 5:34:59fruits think of some
  6932. 5:35:02fruit I'll add an
  6933. 5:35:04apple an orange and a
  6934. 5:35:08banana then I'll create another separate
  6935. 5:35:11array let's create a string array of
  6936. 5:35:15vegetables think of some
  6937. 5:35:17vegetables I will add a
  6938. 5:35:20potato an
  6939. 5:35:22onion and a
  6940. 5:35:24carrot let's create one more I will
  6941. 5:35:27create a string array of
  6942. 5:35:31meats I will add
  6943. 5:35:35chicken pork
  6944. 5:35:39beef let's add one more and
  6945. 5:35:44fish these three arrays are all separate
  6946. 5:35:47one-dimensional arrays well what if I
  6947. 5:35:50told you we can make an array made of
  6948. 5:35:52arrays where each element is an array
  6949. 5:35:55here's how we're going to combine all
  6950. 5:35:58these arrays together into a grid or
  6951. 5:36:01Matrix I will create a two-dimensional
  6952. 5:36:04array of strings we will need two sets
  6953. 5:36:07of straight brackets I will name this
  6954. 5:36:09two-dimensional array
  6955. 5:36:15groceries and then we need our curly
  6956. 5:36:17braces this two-dimensional array each
  6957. 5:36:20element within this two-dimensional
  6958. 5:36:21array is going to be an array we have
  6959. 5:36:26fruits
  6960. 5:36:27vegetables and
  6961. 5:36:31meats let's attempt to display these
  6962. 5:36:33elements you can use a for Loop or an
  6963. 5:36:36enhanced for Loop
  6964. 5:36:38within this for Loop what is the data
  6965. 5:36:40type of each element well each element
  6966. 5:36:43is a string
  6967. 5:36:45array so we need to list the data type
  6968. 5:36:47as a string array let's give a nickname
  6969. 5:36:50of each element as
  6970. 5:36:52Foods in our two-dimensional array of
  6971. 5:36:57groceries let me show you what happens
  6972. 5:36:59if I print each element within our
  6973. 5:37:00two-dimensional array of
  6974. 5:37:02groceries I will print each array of
  6975. 5:37:05foods here's what we got
  6976. 5:37:08well we get a bunch of memory addresses
  6977. 5:37:10because arrays are a reference data type
  6978. 5:37:13we're referring to some place in
  6979. 5:37:15memory we'll need to use a nested
  6980. 5:37:21Loop an enhanced for Loop would be good
  6981. 5:37:23for this with each of these arrays what
  6982. 5:37:26is the data type of each element within
  6983. 5:37:28each well we're working with strings so
  6984. 5:37:31the data type is going to be string
  6985. 5:37:34let's give a nickname of each element as
  6986. 5:37:36food
  6987. 5:37:37in our array of
  6988. 5:37:40foods during each cycle of this Loop
  6989. 5:37:43let's print not print line each food and
  6990. 5:37:47then I'll add a
  6991. 5:37:50space here's what we got
  6992. 5:37:54currently we have our first array and
  6993. 5:37:58our second then
  6994. 5:38:00third after finishing each array I'm
  6995. 5:38:03going to add a new line
  6996. 5:38:04character we can use a print line
  6997. 5:38:06statement for that
  6998. 5:38:09here's what we got now what we have is a
  6999. 5:38:13grid or Matrix of data you can say that
  7000. 5:38:16there's
  7001. 5:38:18columns and
  7002. 5:38:22rows each row is a separate
  7003. 5:38:27array now when initializing an array you
  7004. 5:38:30don't necessarily needed names let's
  7005. 5:38:33remove
  7006. 5:38:34fruits copy the elements of this array
  7007. 5:38:37including the curly braces and paste
  7008. 5:38:39them within this two-dimensional array
  7009. 5:38:41just for readability I'm going to place
  7010. 5:38:43each row on a new
  7011. 5:38:46line let's replace vegetables with the
  7012. 5:38:50elements including the curly
  7013. 5:38:53braces same thing applies with
  7014. 5:38:57meats and we can delete all
  7015. 5:39:00this this would work the
  7016. 5:39:03same groceries is a matrix of data a gri
  7017. 5:39:08let's print
  7018. 5:39:10this and that works the
  7019. 5:39:13same if you ever need to access or
  7020. 5:39:15change an element you'll need to use two
  7021. 5:39:17indices for example let's replace apple
  7022. 5:39:21with the pineapple we will take our
  7023. 5:39:24two-dimensional array of groceries and
  7024. 5:39:26list two
  7025. 5:39:28indices the first index applies to the
  7026. 5:39:30row the second index applies to the
  7027. 5:39:33column so we're saying at row 0 column
  7028. 5:39:380 we can change it or access it let's
  7029. 5:39:42say that row 0 column 0 is now a
  7030. 5:39:45pineapple rather than an apple then we
  7031. 5:39:48have pineapple orange
  7032. 5:39:51banana let's replace our potato with
  7033. 5:39:53something
  7034. 5:39:54else that would be Row one column 0 Row
  7035. 5:39:59one column 0 is now
  7036. 5:40:05celery celery onion
  7037. 5:40:10carrot let's replace the carrot with
  7038. 5:40:12celery then that would be Row one column
  7039. 5:40:162 0 1 2 Row one column
  7040. 5:40:222 potato onion
  7041. 5:40:26celery let's replace pork with eggs that
  7042. 5:40:29would be row two 0 1 2 column 1 0 1
  7043. 5:40:37row two column one is now
  7044. 5:40:42eggs we have chicken eggs beef
  7045. 5:40:47fish to access or modify an element
  7046. 5:40:49within a 2d array you need to list two
  7047. 5:40:52indices the first for the row the second
  7048. 5:40:54for the
  7049. 5:40:55column okay now we're going to create a
  7050. 5:40:57mini
  7051. 5:40:59project we'll create a two-dimensional
  7052. 5:41:01array that resembles a telephone number
  7053. 5:41:03pad this two-dimensional array will be
  7054. 5:41:06made up of characters
  7055. 5:41:07the data type is Char and then we have
  7056. 5:41:10two sets of straight brackets let's name
  7057. 5:41:12this two-dimensional array telephone
  7058. 5:41:15equals we have an array of arrays we'll
  7059. 5:41:19have four
  7060. 5:41:24rows and just for readability I'm going
  7061. 5:41:26to place each of these on a new
  7062. 5:41:28line just so it resembles a grid or
  7063. 5:41:33Matrix for the first row we'll have one
  7064. 5:41:39two three and these are all characters
  7065. 5:41:42not strings we're using single quotes
  7066. 5:41:44not double quotes for the second row
  7067. 5:41:47we'll have
  7068. 5:41:48four
  7069. 5:41:505
  7070. 5:41:526 the third row will be
  7071. 5:41:567
  7072. 5:41:588
  7073. 5:42:009 now for the last row we'll have an
  7074. 5:42:03asterisk
  7075. 5:42:05character Zero
  7076. 5:42:09and pound or hashtag as I like to call
  7077. 5:42:13it so here's our Matrix our grid of
  7078. 5:42:17characters we have an array of arrays it
  7079. 5:42:20has rows and
  7080. 5:42:24columns now let's display our data we'll
  7081. 5:42:27need nested Loops for
  7082. 5:42:31this our outer for Loop is going to be
  7083. 5:42:34in charge of the rows what is the data
  7084. 5:42:36type of each element within this
  7085. 5:42:38two-dimensional array well each row is
  7086. 5:42:41an array of characters that's going to
  7087. 5:42:43be the data type of the outer for Loop
  7088. 5:42:46we have an array of characters let's
  7089. 5:42:48give a nickname of each inner array as a
  7090. 5:42:51row in our two-dimensional array of
  7091. 5:42:55telephone for every Row in our telephone
  7092. 5:42:59do this then we'll need a nested
  7093. 5:43:03Loop what is the data type of each
  7094. 5:43:05element within each array their
  7095. 5:43:07characters the data type will be
  7096. 5:43:10Char let's say for every number
  7097. 5:43:15in each array of row we'll print the
  7098. 5:43:19following we'll use print rather than
  7099. 5:43:21print
  7100. 5:43:23line we'll print each number and I'll
  7101. 5:43:26add a space
  7102. 5:43:29character let's do a test
  7103. 5:43:32run after each row I'm going to print a
  7104. 5:43:35new line character
  7105. 5:43:37so when we escape the inner four Loop
  7106. 5:43:40I'll add an empty print line
  7107. 5:43:43statement and here's our telephone we
  7108. 5:43:46have
  7109. 5:43:47rows and
  7110. 5:43:49columns it's a two-dimensional array of
  7111. 5:43:53characters so those are two-dimensional
  7112. 5:43:56arrays they're an array where each
  7113. 5:43:58element is an array it's useful for
  7114. 5:44:00storing a matrix of data we probably
  7115. 5:44:03won't be using 2D arrays too often but
  7116. 5:44:06you should still be familiar with them
  7117. 5:44:08and well everybody those are two
  7118. 5:44:09dimensional arrays in
  7119. 5:44:13Java hello everybody in this video we're
  7120. 5:44:15going to create an interactive quiz game
  7121. 5:44:17using Java for this project you'll
  7122. 5:44:19create an array of your own custom
  7123. 5:44:21questions and a two-dimensional array of
  7124. 5:44:23all the different options for answers
  7125. 5:44:24that we have you'll type in a number 1
  7126. 5:44:26through four depending on your guess
  7127. 5:44:28after all of the questions are answered
  7128. 5:44:31we will display a final score depending
  7129. 5:44:33on the amount of correct guesses that
  7130. 5:44:35you have so let's begin as a beginner it
  7131. 5:44:38might be helpful to you to break down
  7132. 5:44:39your project into different steps as
  7133. 5:44:42comments we'll list all of the different
  7134. 5:44:43steps we need to take to get this
  7135. 5:44:45program up and running the first thing
  7136. 5:44:47that we need is an array of questions
  7137. 5:44:51questions will be an
  7138. 5:44:52array we will also need
  7139. 5:44:56options options will be a
  7140. 5:44:58two-dimensional
  7141. 5:45:00array we will declare our
  7142. 5:45:03variables we'll need a welcome message
  7143. 5:45:09we will list each question using a
  7144. 5:45:11loop I'll just write in parenthesis that
  7145. 5:45:14we will use a loop now within this loop
  7146. 5:45:17we're going to do a few things we will
  7147. 5:45:20list our
  7148. 5:45:23options then get guess from
  7149. 5:45:27user then check our guess to see if it's
  7150. 5:45:32correct at the end of this program we
  7151. 5:45:34will display find fin
  7152. 5:45:40score so here's a rough outline of our
  7153. 5:45:43project we'll fill this project in one
  7154. 5:45:46step at a time let's begin with creating
  7155. 5:45:48an array of questions we'll ask the
  7156. 5:45:51user the data type will be a string
  7157. 5:45:54array of
  7158. 5:45:57questions here's where you're going to
  7159. 5:45:59ask all of your questions in this
  7160. 5:46:01project we'll have five
  7161. 5:46:03questions they're all going to be very
  7162. 5:46:06long strings
  7163. 5:46:10for our first question you don't need to
  7164. 5:46:12type in the same questions that I do
  7165. 5:46:14feel free to come up with your own
  7166. 5:46:16here's a few that I've looked up online
  7167. 5:46:18my first question will be what is the
  7168. 5:46:22main function of a
  7169. 5:46:27router that'll be the first question
  7170. 5:46:30I'll put each of these questions on a
  7171. 5:46:31new line just for
  7172. 5:46:33readability the second question will be
  7173. 5:46:37which part of the
  7174. 5:46:39computer is
  7175. 5:46:43considered the
  7176. 5:46:46brain then I'll go to a new
  7177. 5:46:52line what year was Facebook
  7178. 5:47:02launched who is known as the father of
  7179. 5:47:08computers then for my last
  7180. 5:47:12question what was the
  7181. 5:47:16first programming
  7182. 5:47:20language all right here are the
  7183. 5:47:22questions questions is an array of
  7184. 5:47:24strings a one-dimensional array now
  7185. 5:47:28we'll create a two-dimensional array of
  7186. 5:47:31options we will create a two-dimensional
  7187. 5:47:34array of strings named options
  7188. 5:47:39we have five questions that means we
  7189. 5:47:42need five arrays one array for each
  7190. 5:47:53question I'll Place each of these on a
  7191. 5:47:55new line for
  7192. 5:48:04readability our first array within our
  7193. 5:48:062D array of options will hold all of the
  7194. 5:48:09options for our first question each
  7195. 5:48:12value within this first array is going
  7196. 5:48:13to be strings and I'm going to zoom in a
  7197. 5:48:16little so we can both read it what is
  7198. 5:48:18the main function of a router here's
  7199. 5:48:20where you're going to list all of the
  7200. 5:48:21different possible answers but only one
  7201. 5:48:24of them is going to be correct I'll say
  7202. 5:48:26one. space storing
  7203. 5:48:32files for option two that will be
  7204. 5:48:36encrypting
  7205. 5:48:41data option
  7206. 5:48:44three will be
  7207. 5:48:46for
  7208. 5:48:48directing internet traffic this will be
  7209. 5:48:51the correct answer number three number
  7210. 5:48:55four will
  7211. 5:48:57be managing
  7212. 5:49:01passwords this will be my first array
  7213. 5:49:04all the options for question one now for
  7214. 5:49:07question
  7215. 5:49:13two for question two we'll have four
  7216. 5:49:21options honestly I'm just going to copy
  7217. 5:49:24these and then paste them within each
  7218. 5:49:26array just to speed things up what part
  7219. 5:49:29of the computer is considered the
  7220. 5:49:32brain here's the different options
  7221. 5:49:34option one will be CPU that's going to
  7222. 5:49:37be the correct answer the central
  7223. 5:49:39processing unit two hard
  7224. 5:49:43drive three
  7225. 5:49:46Ram four
  7226. 5:49:50GPU question three what year was
  7227. 5:49:52Facebook
  7228. 5:49:53launched option one will be 2000 option
  7229. 5:49:57two 2004 and that's the correct
  7230. 5:50:01answer
  7231. 5:50:032006 or 2008
  7232. 5:50:07who is known as the father of
  7233. 5:50:09computers let's say Steve
  7234. 5:50:14Jobs Bill
  7235. 5:50:17Gates Allan
  7236. 5:50:21Turing or Charles Babbage and that's the
  7237. 5:50:23correct
  7238. 5:50:29answer what was the first programming
  7239. 5:50:32language either
  7240. 5:50:34cobal C
  7241. 5:50:38for
  7242. 5:50:39Tran or
  7243. 5:50:42assembly the correct answer is actually
  7244. 5:50:44Fortran it's the first commercially
  7245. 5:50:47available programming language one more
  7246. 5:50:49thing we're going to create a
  7247. 5:50:51one-dimensional array what are the
  7248. 5:50:53correct answers we'll need some sort of
  7249. 5:50:55key we could include this with our
  7250. 5:50:57declare variable
  7251. 5:50:59step we will create an integer array of
  7252. 5:51:03answers we will list the correct answers
  7253. 5:51:06as integers the correct answer to
  7254. 5:51:08question one is directing internet
  7255. 5:51:11traffic so we will list three for the
  7256. 5:51:15next question what part of the computer
  7257. 5:51:16is considered the brain that is the CPU
  7258. 5:51:19option
  7259. 5:51:20one Facebook was launched in the year
  7260. 5:51:232004 that's
  7261. 5:51:25two who is the father of computers that
  7262. 5:51:28is Charles
  7263. 5:51:31Babbage that's
  7264. 5:51:33four and the first commercially
  7265. 5:51:35available program ining language is for
  7266. 5:51:37Tran that is three here are the correct
  7267. 5:51:41answers are key there's a few more
  7268. 5:51:43variables we'll need we will create an
  7269. 5:51:46integer variable of score we can set
  7270. 5:51:49that to be zero if we would like and an
  7271. 5:51:52integer of guess guess is going to store
  7272. 5:51:56whatever the user types in 1 through
  7273. 5:51:58four so it's going to be an integer we
  7274. 5:52:01will also need a scanner to accept user
  7275. 5:52:04input scanner scanner equals new scanner
  7276. 5:52:09it doesn't really matter where we
  7277. 5:52:10declare it just as long as we declare it
  7278. 5:52:12before we use it system.in and then we
  7279. 5:52:15will import this
  7280. 5:52:17class import java.util.scanner and then
  7281. 5:52:21close our scanner at the end of our
  7282. 5:52:23program when we're done with it scanner.
  7283. 5:52:28close all right here are the variables
  7284. 5:52:30we'll need including our options and our
  7285. 5:52:33questions our next step is to create a
  7286. 5:52:35welcome message
  7287. 5:52:38let's output the
  7288. 5:52:40following let's say welcome to the Java
  7289. 5:52:45quiz
  7290. 5:52:48game and then I'll add some
  7291. 5:52:50separators a bunch of asterisks because
  7292. 5:52:53I think it'll look cool
  7293. 5:52:59mostly we have our welcome message and
  7294. 5:53:01I'll just perform a test run it's a good
  7295. 5:53:04idea to test your code as you're coding
  7296. 5:53:05it just sure that everything is working
  7297. 5:53:07fine and that is our welcome
  7298. 5:53:11message now we'll Loop through all of
  7299. 5:53:13the questions that we
  7300. 5:53:17have we'll probably want to use a for
  7301. 5:53:21Loop we will create an index of I in I
  7302. 5:53:25equals z we'll continue this loop as
  7303. 5:53:27long as I is less
  7304. 5:53:30than the length of our array of
  7305. 5:53:32questions questions access the length
  7306. 5:53:36property
  7307. 5:53:37which will return a number in this case
  7308. 5:53:39five because there's five questions we
  7309. 5:53:41will increment I by
  7310. 5:53:43one as a test let's print our
  7311. 5:53:48questions at index of I I will start at
  7312. 5:53:52zero so we're returning the first
  7313. 5:53:54question at index
  7314. 5:53:56zero then during each iteration of this
  7315. 5:53:58Loop we will display each question let's
  7316. 5:54:01do a test
  7317. 5:54:03run so here are the five questions
  7318. 5:54:07we're displaying each element of our
  7319. 5:54:09array of
  7320. 5:54:10questions after each question we have to
  7321. 5:54:13list all of the different
  7322. 5:54:16options that's the next
  7323. 5:54:18step we'll display a question then
  7324. 5:54:22create a nested Loop to display all of
  7325. 5:54:24the
  7326. 5:54:26options I'm going to use an enhanced for
  7327. 5:54:29Loop for this what is the data type of
  7328. 5:54:33each value within each array within
  7329. 5:54:35options
  7330. 5:54:37well we're working with
  7331. 5:54:38strings the inner for Loop is going to
  7332. 5:54:40be an enhanced for Loop the data type of
  7333. 5:54:43each value is a
  7334. 5:54:45string string option colon meaning
  7335. 5:54:50in rray of options at index of
  7336. 5:54:55i options is a two-dimensional array if
  7337. 5:54:59we retrieve a value at a given index
  7338. 5:55:01will be given an array display every
  7339. 5:55:04option in each inner array
  7340. 5:55:07so to demonstrate that let's print each
  7341. 5:55:14option so here's what we have after
  7342. 5:55:18printing each question we will Loop
  7343. 5:55:20through and print all of the
  7344. 5:55:26options now before moving on to the next
  7345. 5:55:28question we have to get some user input
  7346. 5:55:31we don't want to display all the
  7347. 5:55:32questions right away just one at a time
  7348. 5:55:36so we will do this within the outer for
  7349. 5:55:38loop after printing all of the options
  7350. 5:55:42this is where we will get the guess from
  7351. 5:55:43the
  7352. 5:55:46user we will create a prompt to enter
  7353. 5:55:50your guests and I will use print instead
  7354. 5:55:53of print line because I like the user
  7355. 5:55:55input on the same
  7356. 5:55:56line we will assign our variable of
  7357. 5:56:00guess guess equal
  7358. 5:56:03scanner. next int the user going to type
  7359. 5:56:06in a number 1 through
  7360. 5:56:094 then we have to check our guess is it
  7361. 5:56:11correct or
  7362. 5:56:14not we can do the
  7363. 5:56:16following we'll use an if statement if
  7364. 5:56:19our guess is equal to our array of
  7365. 5:56:24answers it's a one-dimensional
  7366. 5:56:26array at index of I we're using the
  7367. 5:56:30index of I because we're still within
  7368. 5:56:32our for Loop during the first iteration
  7369. 5:56:35of this Loop I will be zero so is our
  7370. 5:56:38guess equal to answers at index zero if
  7371. 5:56:41that is correct let's print the
  7372. 5:56:43following let's say correct and for fun
  7373. 5:56:47I'll add some text decoration some of
  7374. 5:56:49those
  7375. 5:56:51separators again it's not
  7376. 5:56:54necessary I just think it'll look
  7377. 5:56:58cool else if our guest doesn't equal our
  7378. 5:57:01answer well we can check that with an
  7379. 5:57:03lse statement if we don't have the
  7380. 5:57:05correct answer that means we have the
  7381. 5:57:07wrong answer and I will output
  7382. 5:57:11wrong and I will just space this out a
  7383. 5:57:13little
  7384. 5:57:14bit all right let's do a test
  7385. 5:57:17run for the first question the correct
  7386. 5:57:20answer is three I'll type in three then
  7387. 5:57:22we print
  7388. 5:57:24correct which part of the computer is
  7389. 5:57:26considered the brain I will
  7390. 5:57:27intentionally get this wrong let's say
  7391. 5:57:30the
  7392. 5:57:32GPU
  7393. 5:57:33wrong and I'll guess the others
  7394. 5:57:36wrong
  7395. 5:57:38wrong wrong then we have to calculate a
  7396. 5:57:41final
  7397. 5:57:43score okay then we have to display the
  7398. 5:57:45final score so after we get the correct
  7399. 5:57:48answer we will increment our score by
  7400. 5:57:50one score
  7401. 5:57:52Plus+ then we will display the final
  7402. 5:57:58score your final score is I'll use
  7403. 5:58:03string catnation to keep this simple
  7404. 5:58:06we'll add our score variable plus the
  7405. 5:58:10words out
  7406. 5:58:13of plus we're going to get the length of
  7407. 5:58:17our questions questions is an
  7408. 5:58:20array questions. length
  7409. 5:58:24property I'm just going to add a space
  7410. 5:58:26right before out because I'm forgetting
  7411. 5:58:29that let's run this one last
  7412. 5:58:33time what is the main function of a
  7413. 5:58:35router that that is three directing
  7414. 5:58:36internet traffic which part of the
  7415. 5:58:39computer is considered the brain that is
  7416. 5:58:40the
  7417. 5:58:42CPU what year was Facebook launched I'm
  7418. 5:58:44going to get this one wrong
  7419. 5:58:45intentionally I'm going to say 2000 and
  7420. 5:58:48that is wrong who is known as the father
  7421. 5:58:50of computers that is Charles babage
  7422. 5:58:534 what was the first programming
  7423. 5:58:56language well the first commercially
  7424. 5:58:58available programming language that
  7425. 5:59:00would be three for
  7426. 5:59:01Tran correct your final score is four
  7427. 5:59:05out of five
  7428. 5:59:07all right everybody so that is an
  7429. 5:59:08interactive quiz game that you can
  7430. 5:59:10create using
  7431. 5:59:13Java what is going on everybody in this
  7432. 5:59:16video we're going to create a game of
  7433. 5:59:17rock paper scissors for beginners so
  7434. 5:59:20let's get started as a beginner when
  7435. 5:59:22creating a project it could be helpful
  7436. 5:59:24to you to break down a project into
  7437. 5:59:26smaller steps just as comments I'm going
  7438. 5:59:29to list the different steps that we'll
  7439. 5:59:30need to take so let's say first we will
  7440. 5:59:32declare our
  7441. 5:59:34variables then then get a choice from
  7442. 5:59:38the
  7443. 5:59:40user get random choice for the
  7444. 5:59:46computer check win conditions who
  7445. 5:59:51won ask to play
  7446. 5:59:55again then at the end we will display a
  7447. 5:59:57goodbye
  7448. 6:00:00message it may be helpful to you as a
  7449. 6:00:02beginner to list the different steps
  7450. 6:00:04that your program is going to take just
  7451. 6:00:06for more organization all right let's
  7452. 6:00:08work on step one we will declare our
  7453. 6:00:12variables we'll need a scanner because
  7454. 6:00:14we're accepting user input scanner
  7455. 6:00:16scanner equals new
  7456. 6:00:18scanner within the set of parentheses we
  7457. 6:00:21will type system.
  7458. 6:00:22in import the class for scanners import
  7459. 6:00:26java.util.scanner and then we will close
  7460. 6:00:29our scanner when we're done with it
  7461. 6:00:31scanner. close because we don't want to
  7462. 6:00:33leave any resources open we will be
  7463. 6:00:36generating random numbers we will need
  7464. 6:00:38to create a random object random random
  7465. 6:00:41equals new
  7466. 6:00:43random and then import this class as
  7467. 6:00:46well import
  7468. 6:00:48java.util
  7469. 6:00:50random we're going to create an array of
  7470. 6:00:53choices they will be strings rock paper
  7471. 6:00:56or scissors we will create a string
  7472. 6:00:59array named
  7473. 6:01:01choices equals an array this will be an
  7474. 6:01:06array of
  7475. 6:01:06strings at the first index we'll have
  7476. 6:01:10Rock then
  7477. 6:01:12paper then
  7478. 6:01:16scissors we'll store our choice within a
  7479. 6:01:18variable this will be a string we'll
  7480. 6:01:21name this variable player
  7481. 6:01:25choice I will declare this variable but
  7482. 6:01:27not yet assign it and the computer's
  7483. 6:01:30Choice string computer choice
  7484. 6:01:36we're going to create a while loop that
  7485. 6:01:38will ask the user to play again when
  7486. 6:01:40they reach the end we'll create a string
  7487. 6:01:43variable of play again and I will set
  7488. 6:01:48this to be
  7489. 6:01:50yes basically while play again is yes
  7490. 6:01:54continue the
  7491. 6:01:55game let's work on our next step get the
  7492. 6:01:58choice from the
  7493. 6:02:00user we need to prompt the
  7494. 6:02:02user we'll use a print line statement
  7495. 6:02:05and say
  7496. 6:02:06enter your move I'll add a set of
  7497. 6:02:09parentheses that says rock paper
  7498. 6:02:14scissors just so the user knows what to
  7499. 6:02:16type in I'll use print instead of print
  7500. 6:02:19line because I prefer user input on the
  7501. 6:02:21same
  7502. 6:02:22line once we have our prompt we're going
  7503. 6:02:25to assign our player choice variable
  7504. 6:02:27player choice equals we will use our
  7505. 6:02:30scanner to get some user input we need
  7506. 6:02:33to accept a string so we can use the
  7507. 6:02:35next
  7508. 6:02:36line
  7509. 6:02:37method to accept a line of text in case
  7510. 6:02:41a user types in any uppercase characters
  7511. 6:02:43for example Rock we could follow this
  7512. 6:02:46with the two lowercase method just to
  7513. 6:02:50make it all lowercase whatever the user
  7514. 6:02:52types
  7515. 6:02:53in accept the next line of user input
  7516. 6:02:56then make it lowercase just for
  7517. 6:03:00consistency now we should have our
  7518. 6:03:01player
  7519. 6:03:03choice we need a way to determine to see
  7520. 6:03:05if this user input is valid did the user
  7521. 6:03:08pick rock paper or scissors or something
  7522. 6:03:10else entirely well we could use an if
  7523. 6:03:13statement for
  7524. 6:03:18that we're going to be checking to see
  7525. 6:03:20if our player choice doesn't equal Rock
  7526. 6:03:24and doesn't equal paper and doesn't
  7527. 6:03:27equal scissors so we can write the
  7528. 6:03:29following statement we're going to use
  7529. 6:03:31the not logical operator first not will
  7530. 6:03:35give you the opposite the
  7531. 6:03:38inverse if player choice use the equals
  7532. 6:03:42method pass in a string of rock if the
  7533. 6:03:46Player's Choice doesn't equal Rock then
  7534. 6:03:50we'll use the and logical
  7535. 6:03:53operator and the Player's
  7536. 6:03:56Choice doesn't again I'm using the not
  7537. 6:03:58logical
  7538. 6:04:00operator
  7539. 6:04:03equals
  7540. 6:04:04paper again we're going to use the and
  7541. 6:04:07logical
  7542. 6:04:08operator and just to help with
  7543. 6:04:11readability cuz we're going to have one
  7544. 6:04:13really long line I'm going to place
  7545. 6:04:15these conditions on a new line each this
  7546. 6:04:18will help with
  7547. 6:04:21readability and take our player
  7548. 6:04:25choice not
  7549. 6:04:30equals
  7550. 6:04:33scissors we're linking three conditions
  7551. 6:04:35together
  7552. 6:04:36if our player choice doesn't equal Rock
  7553. 6:04:40and our player choice doesn't equal
  7554. 6:04:43paper and our player choice doesn't
  7555. 6:04:47equal
  7556. 6:04:48scissors if all of these conditions are
  7557. 6:04:51true that means a user didn't type in
  7558. 6:04:53something that's valid so let's do the
  7559. 6:04:55following again I'm within my set of
  7560. 6:04:57curly
  7561. 6:04:59braces we'll output the
  7562. 6:05:01following we'll say something like
  7563. 6:05:04invalid choice
  7564. 6:05:07all right let's perform a test
  7565. 6:05:12run enter your move rock paper scissors
  7566. 6:05:16let's choose Rock nothing happens that's
  7567. 6:05:20good paper nothing
  7568. 6:05:24happens
  7569. 6:05:27scissors nothing happens all right I'm
  7570. 6:05:29going to type in something that's not
  7571. 6:05:31rock paper or scissors uh I choose the
  7572. 6:05:34gun invalid
  7573. 6:05:37Choice that's good that's what we
  7574. 6:05:40want we're picking something that's not
  7575. 6:05:42rock and is not paper and is not
  7576. 6:05:45scissors so we execute this if statement
  7577. 6:05:47if that's the
  7578. 6:05:49case now for the next step we have to
  7579. 6:05:51get a random choice for the
  7580. 6:05:54computer we don't need user input this
  7581. 6:05:56time we will assign our variable of
  7582. 6:05:59computer
  7583. 6:06:01Choice equals access our array of
  7584. 6:06:05choices
  7585. 6:06:07at a given
  7586. 6:06:09index zero would be Rock because it's
  7587. 6:06:12the first value in our
  7588. 6:06:15array one would be paper two would be
  7589. 6:06:18scissors well why don't we use our
  7590. 6:06:20random object random and call the next
  7591. 6:06:23int
  7592. 6:06:24method then pass in
  7593. 6:06:26three this method of a random object
  7594. 6:06:30would return a random number between 0
  7595. 6:06:32and two zero for rock one for p paper
  7596. 6:06:36two for
  7597. 6:06:37scissors then we'll output it using a
  7598. 6:06:39print line
  7599. 6:06:41statement let's say computer Choice col
  7600. 6:06:45in space then add our variable of
  7601. 6:06:48computer
  7602. 6:06:49Choice let's do a test
  7603. 6:06:53run I'll type in rock the computer picks
  7604. 6:06:59paper I will pick paper the computer
  7605. 6:07:02picks
  7606. 6:07:04scissors I will will pick
  7607. 6:07:07scissors the computer picks scissors as
  7608. 6:07:09well all right we know that that
  7609. 6:07:12works using this method of a random
  7610. 6:07:15object we're generating a random number
  7611. 6:07:18as the
  7612. 6:07:19index now here's the tricky part we have
  7613. 6:07:22to check all the win
  7614. 6:07:23conditions I feel like the best way to
  7615. 6:07:25handle this would be to create an if
  7616. 6:07:27statement followed by an else if
  7617. 6:07:30statement followed by an else
  7618. 6:07:33statement within our if statement this
  7619. 6:07:35will be for a tie let's output the
  7620. 6:07:39following it's a
  7621. 6:07:42tie our Eli statement will be used if
  7622. 6:07:45you win we'll
  7623. 6:07:47output you
  7624. 6:07:49win else will be used for you
  7625. 6:07:53lose you
  7626. 6:07:56lose if we have a tie that means the
  7627. 6:07:59Player's Choice is equal to the
  7628. 6:08:01computer's
  7629. 6:08:02Choice that's how you get a tie within
  7630. 6:08:05the of statement we can check the
  7631. 6:08:06following condition if our player choice
  7632. 6:08:10variable use the equals method of
  7633. 6:08:14strings then pass in the variable of
  7634. 6:08:17computer
  7635. 6:08:19choice we're comparing two strings
  7636. 6:08:21the're reference data types so we're
  7637. 6:08:23going to use the equals method you can't
  7638. 6:08:25use double equals because then you're
  7639. 6:08:27comparing memory addresses of two
  7640. 6:08:29different strings we're going to be
  7641. 6:08:31using equals the equals method if these
  7642. 6:08:34two strings are equal will print it's a
  7643. 6:08:36tie now for the wi conditions within
  7644. 6:08:39this LF statement this is where things
  7645. 6:08:41are going to get kind of tricky we're
  7646. 6:08:43going to list all of the different win
  7647. 6:08:45conditions within our LF statement first
  7648. 6:08:48we'll check if the player picks Rock and
  7649. 6:08:50the computer picks scissors if our
  7650. 6:08:52player
  7651. 6:08:53choice use the equals
  7652. 6:08:57method if the player picks Rock a string
  7653. 6:09:01of
  7654. 6:09:03rock and the the computer's
  7655. 6:09:08Choice equals let me zoom out a little
  7656. 6:09:12bit scissors that means we
  7657. 6:09:16win within our condition of our LF
  7658. 6:09:18statement we'll be adding the different
  7659. 6:09:20win conditions but temporarily I'm going
  7660. 6:09:23to add a few else if statements just to
  7661. 6:09:25help me explain this so if we pick Rock
  7662. 6:09:28and the computer pick scissors that
  7663. 6:09:30means we win but we will also win if the
  7664. 6:09:34player
  7665. 6:09:36pick
  7666. 6:09:37paper and the
  7667. 6:09:39computer let's add that right
  7668. 6:09:42here picks rock that means we also
  7669. 6:09:46win and then temporarily I'll add
  7670. 6:09:48another LF
  7671. 6:09:51statement else if the player picks
  7672. 6:09:57scissors and the computer picks
  7673. 6:10:01paper that also means we win
  7674. 6:10:06we could condense all of these LF
  7675. 6:10:08statements into one but we'll do that in
  7676. 6:10:10a moment I just want to be sure that it
  7677. 6:10:12all works first let's do a test run I
  7678. 6:10:16will pick Rock the computer picks
  7679. 6:10:18scissors that means we
  7680. 6:10:21win I will pick paper the computer picks
  7681. 6:10:24paper it's a
  7682. 6:10:26tie I'll keep playing until I lose I
  7683. 6:10:29will pick scissors the computer picks
  7684. 6:10:32paper we win let's try scissors again
  7685. 6:10:36the computer picks Rock we lose we know
  7686. 6:10:39that these different wi conditions are
  7687. 6:10:42working this is fairly readable but if
  7688. 6:10:45you prefer we can condense all of these
  7689. 6:10:47steps down together into one else if
  7690. 6:10:50statement optionally what you could do
  7691. 6:10:53with our first win condition let's
  7692. 6:10:55enclose them within a set of
  7693. 6:10:59parentheses then I will use the or
  7694. 6:11:02logical Operator Let Me zoom in so you
  7695. 6:11:04can see
  7696. 6:11:05it I'm adding or to the
  7697. 6:11:08end
  7698. 6:11:12or let's take the entire condition
  7699. 6:11:14within our LS if statement then paste it
  7700. 6:11:17after the or logical
  7701. 6:11:20operator and these do need to be
  7702. 6:11:22enclosed within a set of
  7703. 6:11:26parentheses and we can delete this LF
  7704. 6:11:30statement We'll add the or logical
  7705. 6:11:32operator to the end
  7706. 6:11:36cut this condition for the next LF
  7707. 6:11:39statement and delete this LF
  7708. 6:11:42statement and paste
  7709. 6:11:45it so here's our LF statement there is a
  7710. 6:11:49lot going on here this should work too
  7711. 6:11:51but if you prefer to keep it the other
  7712. 6:11:53way just for readability feel free to do
  7713. 6:11:55so if you like to condense some of the
  7714. 6:11:57code here's how you could do that so let
  7715. 6:12:00me just be sure that everything's
  7716. 6:12:01working fine
  7717. 6:12:05I will pick Rock computer picks Rock
  7718. 6:12:07it's a
  7719. 6:12:08tie I will pick paper computer picks
  7720. 6:12:11scissors you
  7721. 6:12:13lose I will pick scissors computer picks
  7722. 6:12:16paper you win all right this LF
  7723. 6:12:20statement does work again if you would
  7724. 6:12:22prefer to add more LF statements feel
  7725. 6:12:25free to keep it the other way now we
  7726. 6:12:27need to ask the user if they want to
  7727. 6:12:28play
  7728. 6:12:29again after displaying either it's a tie
  7729. 6:12:32you win or you lose We'll add a prompt I
  7730. 6:12:36will use print instead of print
  7731. 6:12:39line I'll ask play
  7732. 6:12:43again yes slash
  7733. 6:12:47no we have one variable we haven't used
  7734. 6:12:50yet our play again variable and
  7735. 6:12:52currently it's
  7736. 6:12:54yes play again equals use our scanner
  7737. 6:12:58use the next line
  7738. 6:13:00method and I will take the user's input
  7739. 6:13:02and make it lowercase right away to
  7740. 6:13:05lowercase
  7741. 6:13:06method we're going to enclose all of our
  7742. 6:13:09code right before the variables within a
  7743. 6:13:12loop a do while loop so we'll say do all
  7744. 6:13:16this code while some condition remains
  7745. 6:13:20true let's cut all of our
  7746. 6:13:24code cut it and then paste it within
  7747. 6:13:27this do while
  7748. 6:13:31loop so with the do while loop we do all
  7749. 6:13:33of this code once
  7750. 6:13:35and then check the condition at the
  7751. 6:13:37end what's our condition going to be
  7752. 6:13:41we'll continue playing while our play
  7753. 6:13:43again variable equals a string of yes
  7754. 6:13:47play again equals
  7755. 6:13:50yes one more change we're going to
  7756. 6:13:54make if the user picks something that
  7757. 6:13:57isn't valid we're going to skip the
  7758. 6:13:59current iteration of this Loop we can
  7759. 6:14:01use continue for that
  7760. 6:14:05we want to go back to the beginning if a
  7761. 6:14:08user types in something that is
  7762. 6:14:10invalid then the last step is to create
  7763. 6:14:13a goodbye message we're doing this
  7764. 6:14:15outside of the while loop cuz when we're
  7765. 6:14:17outside of the while loop that means
  7766. 6:14:18we're no longer playing let's print the
  7767. 6:14:22following thanks for
  7768. 6:14:25playing all right let's run this one
  7769. 6:14:28last
  7770. 6:14:30time I will pick Rock computer pick
  7771. 6:14:33scissors you win play again again I'm
  7772. 6:14:35going to type yes enter your move I will
  7773. 6:14:38pick paper computer picks rock you win I
  7774. 6:14:41will play again I will pick
  7775. 6:14:45scissors the computer picks Rock You
  7776. 6:14:47Lose I'm going to type no to play again
  7777. 6:14:51and we escape that Doh Loop thanks for
  7778. 6:14:54playing all right everybody so that is a
  7779. 6:14:56game of rock paper scissors you can make
  7780. 6:14:58using
  7781. 6:15:01Java hello everybody in this video we're
  7782. 6:15:04going to create a slot machine program
  7783. 6:15:06using Java a user is going to bet an
  7784. 6:15:08amount for the symbols of our slot
  7785. 6:15:10machine we'll be using emojis if a user
  7786. 6:15:13gets any matching symbols they'll
  7787. 6:15:15receive a payout then at the end we'll
  7788. 6:15:17give a final balance if that sounds good
  7789. 6:15:20to you let's get started all right
  7790. 6:15:23everybody when working on a project it
  7791. 6:15:25might be helpful to you to tackle your
  7792. 6:15:27project one step at a time as comments
  7793. 6:15:29we'll list the different steps that our
  7794. 6:15:31program is going to take this will give
  7795. 6:15:33us a rough outline of our project the
  7796. 6:15:35first thing we'll do is declare our
  7797. 6:15:39variables then display a welcome message
  7798. 6:15:42display welcome
  7799. 6:15:46message we will play if our balance
  7800. 6:15:50meaning the money that we have is
  7801. 6:15:52greater than
  7802. 6:15:54zero if we don't have any money we can't
  7803. 6:15:57play it is a slot machine after all a
  7804. 6:16:00user is going to enter bet amount then
  7805. 6:16:03I'll write a few Subs steps underneath
  7806. 6:16:07here we need to
  7807. 6:16:09verify if the bet is greater than the
  7808. 6:16:12balance you can't bet money that you
  7809. 6:16:15don't
  7810. 6:16:16have then
  7811. 6:16:19verify if bet is greater than zero you
  7812. 6:16:23can't bet negative money so we'll need
  7813. 6:16:25to check
  7814. 6:16:27that then subtract bet from our balance
  7815. 6:16:34once we subtract the BET from the
  7816. 6:16:36balance then we will spin our
  7817. 6:16:39row our row is going to contain emojis
  7818. 6:16:43but we'll need to display it we'll
  7819. 6:16:45create a method to print our row of
  7820. 6:16:49emojis once we display our row we need
  7821. 6:16:52to get a payout if we win something if
  7822. 6:16:55we have any
  7823. 6:16:57matches then ask to play
  7824. 6:17:01again if the user doesn't want to play
  7825. 6:17:04we will display an exit message display
  7826. 6:17:09exit message so here's a rough outline
  7827. 6:17:12of our project we will Begin by
  7828. 6:17:14declaring our
  7829. 6:17:16variables we will be accepting user
  7830. 6:17:18input we'll need a scanner for that
  7831. 6:17:20scanner scanner equals new
  7832. 6:17:23scanner type system.in within the set of
  7833. 6:17:27parentheses import this class import
  7834. 6:17:30java.util.scanner then when we're done
  7835. 6:17:33with our scanner we'll want want to
  7836. 6:17:35close it scanner.
  7837. 6:17:40close we'll need a balance how much
  7838. 6:17:42money do we have I will set that to be
  7839. 6:17:46100 100 as in
  7840. 6:17:48$100 this could also be done with other
  7841. 6:17:50units of currency we'll need an amount
  7842. 6:17:53for our bet int bet how much are we
  7843. 6:17:56betting how much are we
  7844. 6:17:58risking int
  7845. 6:18:00payout how much money do we win we'll be
  7846. 6:18:04adding our pay pay out to our balance if
  7847. 6:18:06we
  7848. 6:18:07win and a string array named
  7849. 6:18:11row our row is going to contain our
  7850. 6:18:14symbols our emojis that will display to
  7851. 6:18:16the output window that's why we're
  7852. 6:18:18creating a string array to store all of
  7853. 6:18:20them here are the variables we'll need
  7854. 6:18:23we'll move on to the next step our next
  7855. 6:18:25step is to display a welcome
  7856. 6:18:28message let's say something like this
  7857. 6:18:30I'll use a print line statement we'll
  7858. 6:18:33say welcome to
  7859. 6:18:35Java
  7860. 6:18:37slots underneath this line we'll add
  7861. 6:18:40another print line statement I will
  7862. 6:18:42print the word symbols and here we'll
  7863. 6:18:45list all the different symbols we would
  7864. 6:18:46like to use within our program I'll be
  7865. 6:18:49using emojis to pull up the Emoji menu
  7866. 6:18:52if you're on Windows hold on the window
  7867. 6:18:53key then hit the semicolon
  7868. 6:18:57button so pick some emojis that you like
  7869. 6:19:00here's a few that I typically see
  7870. 6:19:03cherries watermelon
  7871. 6:19:05lemons bells and
  7872. 6:19:09stars if you would like to stick to a
  7873. 6:19:11certain theme feel free to change
  7874. 6:19:14these but these are a few symbols you
  7875. 6:19:16may see on a slot
  7876. 6:19:18machine and I'm just going to work on
  7877. 6:19:21the spacing a little bit that looks
  7878. 6:19:23pretty
  7879. 6:19:24good just to make this a little more
  7880. 6:19:26fancy I'm going to add a bunch of
  7881. 6:19:28asterisks as separators before and after
  7882. 6:19:31not necessary I just think it looks cool
  7883. 6:19:34and that's a good enough
  7884. 6:19:35reason let's perform a test run just to
  7885. 6:19:38be sure that this looks
  7886. 6:19:41fine I think that looks pretty good all
  7887. 6:19:44right we have our welcome
  7888. 6:19:47message our next step is to play our
  7889. 6:19:49game if our balance is greater than zero
  7890. 6:19:52this can be done with the while
  7891. 6:19:54loop while our balance is greater than
  7892. 6:19:58zero we will continue
  7893. 6:20:01playing then we have to enter the BET
  7894. 6:20:03amount that's the next step
  7895. 6:20:06first we'll display our current
  7896. 6:20:08balance current balance colon
  7897. 6:20:12space pick unit of currency I'll pick
  7898. 6:20:15American
  7899. 6:20:16dollars plus we will display our balance
  7900. 6:20:20variable we will ask a user for some
  7901. 6:20:24input place your bet
  7902. 6:20:27amount I'll use print rather than print
  7903. 6:20:30line because I like the user input on
  7904. 6:20:32the same line we will assign our bet
  7905. 6:20:37variable use our scanner to accept some
  7906. 6:20:40input let's say next int we won't deal
  7907. 6:20:44with cents we'll just bet whole
  7908. 6:20:47dollars then we have a couple substeps
  7909. 6:20:50we have to verify if our bet is greater
  7910. 6:20:53than our
  7911. 6:20:56balance we'll write an if
  7912. 6:20:59statement if bet is greater than our
  7913. 6:21:02balance we'll output the following
  7914. 6:21:05let's say insufficient
  7915. 6:21:10funds we'll want to skip the current
  7916. 6:21:12cycle of this Loop we'll use continue to
  7917. 6:21:15go back to the
  7918. 6:21:17beginning then we'll verify if the bet
  7919. 6:21:19is greater than
  7920. 6:21:22zero we don't want a user to bet
  7921. 6:21:24negative
  7922. 6:21:26money else
  7923. 6:21:30if our bet let's say less than or equal
  7924. 6:21:34to zero we don't want to use our betting
  7925. 6:21:37no
  7926. 6:21:40money bet must be greater than
  7927. 6:21:45zero else we have to subtract the BET
  7928. 6:21:48from the
  7929. 6:21:50balance we could say balance equals
  7930. 6:21:54balance minus bet but we can shorten
  7931. 6:21:57this using the augmented assignment
  7932. 6:21:59operator we can say balance minus equals
  7933. 6:22:03bet Within our LF statement we're going
  7934. 6:22:05to use the continue keyword to skip the
  7935. 6:22:08current iteration of this Loop so
  7936. 6:22:11temporarily within my else statement I'm
  7937. 6:22:13just going to print our balance right
  7938. 6:22:17after just to be sure that it's all
  7939. 6:22:20working let's do a test
  7940. 6:22:23run okay welcome to Java slots current
  7941. 6:22:26balance $100 place your bet amount I
  7942. 6:22:29will bet money that I don't have one1
  7943. 6:22:31kajillion dollars insufficient funds
  7944. 6:22:35current balance $100 place your bet
  7945. 6:22:38amount we can't bet negative money I'll
  7946. 6:22:41bet1 bet must be greater than zero then
  7947. 6:22:44we're brought back to the beginning I
  7948. 6:22:47will bet
  7949. 6:22:48$10 and our new balance is
  7950. 6:22:51$90 all right we know that that
  7951. 6:22:55works we can delete this print line
  7952. 6:22:57statement we no longer need it we were
  7953. 6:22:59just testing something our next step is
  7954. 6:23:02that we're going to spin our row of
  7955. 6:23:03symbols our
  7956. 6:23:06emojis after we subtract our bet from
  7957. 6:23:09our balance it's here that we'll spin
  7958. 6:23:11our Row first I'll print the following
  7959. 6:23:14message let's say the word
  7960. 6:23:18spinning then we will call a method to
  7961. 6:23:20spin our row but we will need to declare
  7962. 6:23:23this method we will do that outside of
  7963. 6:23:26the main
  7964. 6:23:28method this will be a static method the
  7965. 6:23:32return type is going to be an array of
  7966. 6:23:34strings
  7967. 6:23:35string array and the name of the method
  7968. 6:23:38will be spin
  7969. 6:23:41row we'll be creating a few local
  7970. 6:23:44variables within this method but first
  7971. 6:23:46we do need to return
  7972. 6:23:48something because we promised that we're
  7973. 6:23:51going to return a string array in order
  7974. 6:23:53for this program to run a compile fine I
  7975. 6:23:55will add The Following return statement
  7976. 6:23:57return a new empty array of strings
  7977. 6:24:01we'll be replacing this later but we do
  7978. 6:24:03have to return something in order for
  7979. 6:24:05this method to at least work we'll be
  7980. 6:24:07creating a few local variables the first
  7981. 6:24:11is a string
  7982. 6:24:12array named
  7983. 6:24:14symbols equals then within a set of cly
  7984. 6:24:18braces we need our emojis let's go back
  7985. 6:24:21to the
  7986. 6:24:22beginning copy our
  7987. 6:24:25emojis then place them within this array
  7988. 6:24:28they do need to be surrounded with a set
  7989. 6:24:30of double quotes because this is an
  7990. 6:24:32array of strings
  7991. 6:24:34so let's take a moment to take care of
  7992. 6:24:43that all right that is good we will
  7993. 6:24:46declare a string array named row our row
  7994. 6:24:51is going to be empty it's going to be a
  7995. 6:24:53new string array that can hold three
  7996. 6:24:57elements row is an empty array that can
  7997. 6:25:00hold three strings which will be picked
  7998. 6:25:02at random
  7999. 6:25:05we will be generating random numbers we
  8000. 6:25:07need a random object random random
  8001. 6:25:10equals new
  8002. 6:25:12random then import this
  8003. 6:25:15class at the top import java.util
  8004. 6:25:22random temporarily there's something I'm
  8005. 6:25:25going to demonstrate let's display our
  8006. 6:25:28aray of symbols at index of zero that
  8007. 6:25:31should output this first Emoji a cherry
  8008. 6:25:36I'll just bet a dollar doesn't matter
  8009. 6:25:38and we have our
  8010. 6:25:41Cherry one should be our
  8011. 6:25:46watermelon
  8012. 6:25:49watermelon then four should be our
  8013. 6:25:53star
  8014. 6:25:56star we need to choose a random number
  8015. 6:25:59between zero and four when we select our
  8016. 6:26:02array of symbols within the straight
  8017. 6:26:04brackets where we list an index we will
  8018. 6:26:07take our random object call the next int
  8019. 6:26:12method we will generate a random number
  8020. 6:26:15between zero and
  8021. 6:26:17five the first number is inclusive the
  8022. 6:26:20second number is exclusive if your range
  8023. 6:26:22starts with zero you don't technically
  8024. 6:26:24need this first number you could just
  8025. 6:26:25say
  8026. 6:26:26five let's run this a few times just to
  8027. 6:26:29see what
  8028. 6:26:30happens so that time I got a bell
  8029. 6:26:36a
  8030. 6:26:39cherry and a
  8031. 6:26:42watermelon if we have more or less
  8032. 6:26:44emojis we would need to change this
  8033. 6:26:46number why don't we use the length of
  8034. 6:26:49our symbols instead symbols then give me
  8035. 6:26:52the length property of this
  8036. 6:26:55array if we were to add more or less
  8037. 6:26:57symbols our length is going to change
  8038. 6:26:59automatically when we run and compile
  8039. 6:27:01this
  8040. 6:27:02program so we'll do a test run again
  8041. 6:27:05again and that seems to
  8042. 6:27:10work you're getting a random symbol each
  8043. 6:27:13time let's delete the print line
  8044. 6:27:16statement portion of
  8045. 6:27:18this we need to generate three random
  8046. 6:27:21symbols we can use a for Loop for that
  8047. 6:27:24for in I equals 0 we will continue as
  8048. 6:27:29long as I is less than three then
  8049. 6:27:32increment I by One
  8050. 6:27:34we will take our row at index of I in
  8051. 6:27:39the beginning I is going to equal zero
  8052. 6:27:41then one then two then the four Loop
  8053. 6:27:43ends we will assign row at index of I
  8054. 6:27:47equal to cut this line of
  8055. 6:27:51code we're picking a random symbol from
  8056. 6:27:54our array of strings our array of
  8057. 6:27:57emojis at the end we will return our row
  8058. 6:28:01we're returning our array of strings
  8059. 6:28:04just to test it I'm going to Output
  8060. 6:28:07using print row at index
  8061. 6:28:13zero plus row at index
  8062. 6:28:18one plus row at index 2 I just want to
  8063. 6:28:22be sure that we get three random
  8064. 6:28:27symbols there we go we get three random
  8065. 6:28:31symbols we'll format the output later so
  8066. 6:28:33don't
  8067. 6:28:36wor we can delete this statement we were
  8068. 6:28:38just testing
  8069. 6:28:40something now this method is complete we
  8070. 6:28:43can close
  8071. 6:28:45it we're returning a string array back
  8072. 6:28:48to the place in which we called this
  8073. 6:28:49method we can assign this
  8074. 6:28:52array back to the beginning we will
  8075. 6:28:54assign our string array of
  8076. 6:28:58row our row equals call the spin row
  8077. 6:29:02method and this will return a string
  8078. 6:29:05array which we're assigning to row once
  8079. 6:29:08we have our row we're going to print our
  8080. 6:29:10row we'll need to create another method
  8081. 6:29:13for that we will create a static method
  8082. 6:29:16it's not going to return anything the
  8083. 6:29:18return type is void the name will be
  8084. 6:29:21print
  8085. 6:29:23row there will be one parameter we
  8086. 6:29:26accept a string array which we will name
  8087. 6:29:31row after assigning our row we will call
  8088. 6:29:34call the print row
  8089. 6:29:35method but we have to pass in our
  8090. 6:29:38row because we have one parameter we
  8091. 6:29:41have to pass in a string array which we
  8092. 6:29:45are so here's where we'll print and
  8093. 6:29:47display our row of emojis let's output
  8094. 6:29:50the following I will display a space
  8095. 6:29:53character plus now the string class has
  8096. 6:29:56a few useful methods type string with a
  8097. 6:29:59capital s dot call the join method
  8098. 6:30:04for each character within a string we
  8099. 6:30:06can join them together with another
  8100. 6:30:08character I will join them by a space a
  8101. 6:30:12vertical bar then a space comma
  8102. 6:30:16Row for every string in my string array
  8103. 6:30:19of row we're going to join them with the
  8104. 6:30:21vertical bar along with a space before
  8105. 6:30:23and after here's the output
  8106. 6:30:26currently I'll just bet a dollar here's
  8107. 6:30:29what we have we have three
  8108. 6:30:31emojis each separated with the IAL
  8109. 6:30:36bar I'll add some of those separators
  8110. 6:30:38too those
  8111. 6:30:40asterisks 14 Aster risks should be
  8112. 6:30:51enough all right let's take another look
  8113. 6:30:53I'll bet
  8114. 6:30:55$10 that looks pretty good I'm happy
  8115. 6:30:57with that our next step is to get a
  8116. 6:31:00payout if there's any
  8117. 6:31:02matches we can collapse this method we
  8118. 6:31:05no longer need
  8119. 6:31:07it now we need to get our
  8120. 6:31:10payout after printing our row we will
  8121. 6:31:13call a method of get payout which we
  8122. 6:31:15still need to
  8123. 6:31:17declare we'll pass in two things our
  8124. 6:31:20string array of row and our bet how much
  8125. 6:31:24money did we
  8126. 6:31:25bet we need to declare this
  8127. 6:31:28method after the print row method we
  8128. 6:31:31will declare a static method this method
  8129. 6:31:34is going to return an integer our payout
  8130. 6:31:37the method name will be get
  8131. 6:31:40payout we have to set up the parameters
  8132. 6:31:43we're accepting a string
  8133. 6:31:46array we will name the string array
  8134. 6:31:49row and a bet our bet is an integer int
  8135. 6:31:55bet we do need to return something just
  8136. 6:31:58temporarily at the end I'm going to
  8137. 6:32:00return
  8138. 6:32:01zero we need to check to see if we have
  8139. 6:32:04any
  8140. 6:32:05matches what we could do first is check
  8141. 6:32:08to see if all three elements match are
  8142. 6:32:11all these strings the same symbol here's
  8143. 6:32:13how we can do
  8144. 6:32:15that let's write an if
  8145. 6:32:18statement if our
  8146. 6:32:20row at index of zero that's the first
  8147. 6:32:24symbol now you can't use the comparison
  8148. 6:32:27operator for Strings cuz they're
  8149. 6:32:28reference data
  8150. 6:32:30types you would think you could say if
  8151. 6:32:32row at index 0 is equal to row at index
  8152. 6:32:36one but you actually need to use
  8153. 6:32:40equals if row at index
  8154. 6:32:42zero equals row at index
  8155. 6:32:50one if the first two symbols match row
  8156. 6:32:53at index0 and row at index one well then
  8157. 6:32:57the first two symbols match but not only
  8158. 6:32:59that we have to compare row at index one
  8159. 6:33:01and row at index 2 do those match as
  8160. 6:33:04well we're checking to see if all three
  8161. 6:33:06symbols match we will use the and
  8162. 6:33:08logical operator and we can copy this
  8163. 6:33:12check paste it and if row at index one
  8164. 6:33:17equals row at index 2 if all three
  8165. 6:33:20symbols match then we execute this if
  8166. 6:33:23statement why don't we use an enhanced
  8167. 6:33:25switch a switch can return something
  8168. 6:33:28we'll use the return
  8169. 6:33:30keyword create a switch within the
  8170. 6:33:34switch where are we examining let's
  8171. 6:33:36examine one of these symbols we can just
  8172. 6:33:38pick any I'll just pick the first
  8173. 6:33:40one we are examining our row at index
  8174. 6:33:43zero all three symbols are going to be
  8175. 6:33:46the same so it doesn't matter which one
  8176. 6:33:47you pick we'll examine this value this
  8177. 6:33:51Emoji against any matching cases the
  8178. 6:33:54first case will be our first symbol of a
  8179. 6:33:57cherry if these two values match we'll
  8180. 6:34:00do the following we'll write an arrow
  8181. 6:34:04and we're going to be returning
  8182. 6:34:05something let's return our bet times
  8183. 6:34:08three let's
  8184. 6:34:10say we get a payout of three times what
  8185. 6:34:13we bet for our next
  8186. 6:34:16case that was a
  8187. 6:34:19watermelon the payout will be
  8188. 6:34:24four for a
  8189. 6:34:28lemon let's set the payout to be
  8190. 6:34:32five a
  8191. 6:34:39Bell three matching Bells will be worth
  8192. 6:34:4210 three stars let's say is
  8193. 6:34:5020 bet times
  8194. 6:34:5320 We'll add a default case too in case
  8195. 6:34:55there's no
  8196. 6:34:56matches default Arrow return
  8197. 6:35:02zero looks like I'm missing a semicolon
  8198. 6:35:04at the end if all three symbols match
  8199. 6:35:08return our bet times a certain amount
  8200. 6:35:10depending on which symbols
  8201. 6:35:12match the get payout method is going to
  8202. 6:35:15return an
  8203. 6:35:16integer we will assign our variable of
  8204. 6:35:19payout equal to get
  8205. 6:35:23payout if our payout is greater than
  8206. 6:35:28zero we'll do the following that means
  8207. 6:35:30we won something if that's the case
  8208. 6:35:33let's let's output U1 pick a unit of
  8209. 6:35:38currency plus our payout whatever we
  8210. 6:35:42win then we will add that to our balance
  8211. 6:35:45balance equals balance plus our payout
  8212. 6:35:49but we can shorten this using the
  8213. 6:35:51augmented assignment operator for
  8214. 6:35:53addition balance plus equals our
  8215. 6:35:58payout else if we don't get a payout if
  8216. 6:36:01our payout is zero that means we didn't
  8217. 6:36:04win anything we'll
  8218. 6:36:06output sorry you lost this round okay
  8219. 6:36:11let's do a few test runs I'm going to
  8220. 6:36:13keep on running this until we get three
  8221. 6:36:15matching symbols just to be sure that it
  8222. 6:36:17all works I will bet a
  8223. 6:36:20dollar our message is sorry you lost
  8224. 6:36:22this round and the current balance is
  8225. 6:36:24$99 I'll bet another dollar you lost you
  8226. 6:36:29lost you
  8227. 6:36:32lost it's hard to get three matching
  8228. 6:36:35symbols here we go I have three matching
  8229. 6:36:38cherries you won $3 and my current
  8230. 6:36:41balance is
  8231. 6:36:43$86 now if you would like to check to
  8232. 6:36:45see if the first two symbols match or
  8233. 6:36:47the second two you can add the following
  8234. 6:36:51code let's copy our if
  8235. 6:36:53statement paste it change if to be else
  8236. 6:36:57if we'll only be checking the first two
  8237. 6:37:00symbols we can delete the second
  8238. 6:37:03condition
  8239. 6:37:04if the first two symbols match we'll
  8240. 6:37:07give a reduced payout let's say for two
  8241. 6:37:09matching cherries that will be two for
  8242. 6:37:12watermelon that will be three
  8243. 6:37:154 5 and then
  8244. 6:37:1810 if the second symbol matches the
  8245. 6:37:21third
  8246. 6:37:22one we'll write another L
  8247. 6:37:26statement index one and index two will
  8248. 6:37:29give the same
  8249. 6:37:31payout hello this is from the future
  8250. 6:37:34there's just one bug that I need to fix
  8251. 6:37:36the else if statement where we check if
  8252. 6:37:38our second symbol matches the third
  8253. 6:37:41within our switch we have to examine one
  8254. 6:37:43of these two symbols I forgot to change
  8255. 6:37:45that originally both these symbols
  8256. 6:37:48should be the same so it doesn't matter
  8257. 6:37:49which one we pick I'll just pick the
  8258. 6:37:51index at one back to the previous LF
  8259. 6:37:54statement index zero is fine because the
  8260. 6:37:57first two symbols should match I just
  8261. 6:37:59needed to fix that that is all let's
  8262. 6:38:02check to be sure that works I'll bet a
  8263. 6:38:07dollar so my first two symbols match we
  8264. 6:38:10have a reduced payout of $3 but if it
  8265. 6:38:13was three watermelons we would win $4
  8266. 6:38:18instead we can close this get payout
  8267. 6:38:20method we're done with
  8268. 6:38:21it here we'll ask the user to play again
  8269. 6:38:24if they would like to
  8270. 6:38:28continue we'll create a
  8271. 6:38:31prompt do you want to play again I'll
  8272. 6:38:36use print rather than print
  8273. 6:38:39line there's one more variable we'll
  8274. 6:38:42need that I'm
  8275. 6:38:43forgetting let's create a string
  8276. 6:38:46variable of play
  8277. 6:38:54again we will assign play again equal to
  8278. 6:38:58use our scanner call the next line
  8279. 6:39:01method
  8280. 6:39:04we'll want a user to type in either
  8281. 6:39:06capital y or capital N after accepting
  8282. 6:39:10user input we'll make it uppercase using
  8283. 6:39:12the two uppercase
  8284. 6:39:17method using an if statement we'll check
  8285. 6:39:21we'll check if play again doesn't
  8286. 6:39:26equal for comparing strings we use
  8287. 6:39:28equals if play again doesn't equal a
  8288. 6:39:32capital Y then we will break to break
  8289. 6:39:35out of the loop that we're
  8290. 6:39:37in then go to the exit the exit
  8291. 6:39:40message one more thing we need to add
  8292. 6:39:43though I'm going to play a
  8293. 6:39:46bet we get that prompt to play again but
  8294. 6:39:50the program exits after typing in a
  8295. 6:39:53number there's still a new line
  8296. 6:39:55character within the input
  8297. 6:39:57buffer because we're using next int next
  8298. 6:40:00integer we need to clear that input
  8299. 6:40:03buffer
  8300. 6:40:04after this line of codee where we call
  8301. 6:40:06the next int method we can take our
  8302. 6:40:08scanner called the next line method to
  8303. 6:40:11get rid of that new line character and
  8304. 6:40:13that should
  8305. 6:40:15work I'll bet a dollar then we can play
  8306. 6:40:18again if we type in
  8307. 6:40:21y we display our current balance we can
  8308. 6:40:24place another
  8309. 6:40:25bet hey $50 that time I'll type n to not
  8310. 6:40:30play again then we exit
  8311. 6:40:33the last thing we're going to do is
  8312. 6:40:35display an exit
  8313. 6:40:37message we'll take care of that right
  8314. 6:40:39here once we're out of the
  8315. 6:40:41loop let's output the
  8316. 6:40:44following game
  8317. 6:40:47over your final
  8318. 6:40:50balance is pick unit of
  8319. 6:40:54currency then I will add our variable of
  8320. 6:40:57balance okay let's run this one last
  8321. 6:41:00time welcome to Java slots I'll bet a
  8322. 6:41:04dollar sorry you lost would you like to
  8323. 6:41:07play again I will type y for yes current
  8324. 6:41:10balance $99 I will bet a dollar
  8325. 6:41:13again I
  8326. 6:41:15lost I'll just keep playing till I win
  8327. 6:41:18hey I won $10 we have two matching Stars
  8328. 6:41:22I'll play one more time I will bet
  8329. 6:41:25$10 and I won 20 we have two matching
  8330. 6:41:29symbols I will type in no to not play
  8331. 6:41:31again and we get our game over message
  8332. 6:41:34your final balance is
  8333. 6:41:37$117 so we came out ahead all right
  8334. 6:41:40everybody that is a slot machine game
  8335. 6:41:42you can create using
  8336. 6:41:46Java yo we have finally made it to
  8337. 6:41:49object-oriented programming in Java so
  8338. 6:41:52what is an object exactly look around
  8339. 6:41:54wherever you're sitting right now you
  8340. 6:41:56are surrounded by different objects for
  8341. 6:41:59example next to me I have my phone a cup
  8342. 6:42:03of coffee and a microphone in
  8343. 6:42:06programming an object can represent a
  8344. 6:42:08real world entity they can do one of a
  8345. 6:42:11few things objects can hold data and
  8346. 6:42:14they can perform actions the data they
  8347. 6:42:17hold are known as attributes the actions
  8348. 6:42:20that they can perform are known as
  8349. 6:42:22methods objects can have things and they
  8350. 6:42:25can do things some attributes that a
  8351. 6:42:27person may have would be a name their
  8352. 6:42:30age their height and their weight a few
  8353. 6:42:33methods meaning actions that a human
  8354. 6:42:36could do a person well they could eat
  8355. 6:42:38and they could sleep objects are
  8356. 6:42:40reference data types we store the data
  8357. 6:42:43for an object in a location known as the
  8358. 6:42:46Heap in this demonstration we're going
  8359. 6:42:48to be creating some car objects but
  8360. 6:42:50we'll need a Class A Class can serve as
  8361. 6:42:53a blueprint to create objects so what
  8362. 6:42:56we're going to do is create a new Java
  8363. 6:42:59class we will create a class of car
  8364. 6:43:04we have two classes our main class and
  8365. 6:43:07our car
  8366. 6:43:08Class A Class can behave as a blueprint
  8367. 6:43:11for creating objects we'll start by
  8368. 6:43:13listing the different attributes that a
  8369. 6:43:15car may have so what does a car have
  8370. 6:43:18exactly well a car could have a make
  8371. 6:43:21that would be a
  8372. 6:43:22string we can assign it if we would like
  8373. 6:43:25think of a car you'd like or a car you
  8374. 6:43:27drive I'll pick my favorite car the make
  8375. 6:43:30will be Ford another attribute could be
  8376. 6:43:33a
  8377. 6:43:34model and I will pick a
  8378. 6:43:38Mustang cars could have a year that
  8379. 6:43:40would be an integer into Year I'll pick
  8380. 6:43:42the year
  8381. 6:43:442025 a price that could be a double
  8382. 6:43:47double price
  8383. 6:43:49equals let's say
  8384. 6:43:53$58,000
  8385. 6:43:5599 a car's engine could be running that
  8386. 6:43:58could be a Boolean Boolean is running I
  8387. 6:44:02will set that to be true
  8388. 6:44:03or false if the car is turned off things
  8389. 6:44:07that an object has in this case car
  8390. 6:44:09these are known as attributes they are
  8391. 6:44:12things that an object
  8392. 6:44:14has now heading back to our main class
  8393. 6:44:17we will create a car
  8394. 6:44:19object we're already a little familiar
  8395. 6:44:21with working with objects for example a
  8396. 6:44:24scanner is an object to create a scanner
  8397. 6:44:26we would type scanner scanner equals new
  8398. 6:44:32scanner then you type
  8399. 6:44:34system.in or with random objects we
  8400. 6:44:37would type random random equals new
  8401. 6:44:42random Now to create a car object we're
  8402. 6:44:45going to be utilizing this
  8403. 6:44:47class we'll type car then think of a
  8404. 6:44:50name for this car we'll just say car all
  8405. 6:44:52lowercase equals new car we're following
  8406. 6:44:56a similar
  8407. 6:44:57pattern car car equals new
  8408. 6:45:00car we don't need the scanner or the
  8409. 6:45:03random object I was just demonstrating
  8410. 6:45:06something we now have a car object that
  8411. 6:45:08we can use our car object has these
  8412. 6:45:12attributes these
  8413. 6:45:15things if I was to print my car object
  8414. 6:45:19here's what we can
  8415. 6:45:21see well since objects are a reference
  8416. 6:45:24data type if you were to print your car
  8417. 6:45:26directly you get a memory
  8418. 6:45:30address if you need to access one of
  8419. 6:45:32these attributes
  8420. 6:45:34you have to follow the object name with
  8421. 6:45:36a DOT a DOT is known as the dot operator
  8422. 6:45:41it allows you to access things within an
  8423. 6:45:43object if I need the model of my car I
  8424. 6:45:47would take my object name use the dot
  8425. 6:45:50operator followed by the attribute name
  8426. 6:45:53if I was to print the model of my
  8427. 6:45:55car that would output
  8428. 6:45:59Mustang let's print some of the other
  8429. 6:46:01attributes car. make who manufactures
  8430. 6:46:05the
  8431. 6:46:06car
  8432. 6:46:08Ford what is the year of my car car.
  8433. 6:46:14year
  8434. 6:46:182025 what's the price car.
  8435. 6:46:23price
  8436. 6:46:25$58,000 and
  8437. 6:46:2899 is my car running or not car do is
  8438. 6:46:32running
  8439. 6:46:37false the car is not currently running
  8440. 6:46:40you can modify and change these
  8441. 6:46:41attributes too with our car object let's
  8442. 6:46:45access the is running attribute I will
  8443. 6:46:48set that to be true like we're turning
  8444. 6:46:49the car
  8445. 6:46:51on is running is now true because we
  8446. 6:46:55changed the attribute of is
  8447. 6:46:58running not only do objects have the
  8448. 6:47:00capability to hold data meaning
  8449. 6:47:03attributes they can perform actions they
  8450. 6:47:06can have their own methods going back to
  8451. 6:47:08our car class we'll Define a few
  8452. 6:47:11methods what sorts of actions can a car
  8453. 6:47:14take well we could start a car within
  8454. 6:47:17this class we will Define a method we
  8455. 6:47:20don't need that static keyword this time
  8456. 6:47:23we need a return type we'll say void
  8457. 6:47:25we're not returning anything we will
  8458. 6:47:27Define a method of
  8459. 6:47:29start let's do the following let's
  8460. 6:47:32output
  8461. 6:47:33you start the
  8462. 6:47:36engine we'll create another
  8463. 6:47:39method
  8464. 6:47:41void
  8465. 6:47:47stop let's say you stop the
  8466. 6:47:51engine so now with our
  8467. 6:47:54car we can delete these print line
  8468. 6:47:57statements we'll take our car object use
  8469. 6:48:00the dot operator call the start method
  8470. 6:48:03method this should output you start the
  8471. 6:48:07engine or we can stop car.
  8472. 6:48:13stop you start the engine you stop the
  8473. 6:48:16engine not only that Within These
  8474. 6:48:18methods let's change one of these
  8475. 6:48:20variables one of these
  8476. 6:48:22attributes within our start method let's
  8477. 6:48:25take is running set that to be
  8478. 6:48:28true within the stop method we'll take
  8479. 6:48:31is running set that to be
  8480. 6:48:35false going back to our main
  8481. 6:48:38class before starting the car let's
  8482. 6:48:41output car do is
  8483. 6:48:44running after starting the car let's
  8484. 6:48:47output car dot is running and then
  8485. 6:48:50stopping the car will output car dot is
  8486. 6:48:55running the car is currently not running
  8487. 6:48:57that's false you start the engine the
  8488. 6:49:00car is now running that's set to true
  8489. 6:49:02you stop the engine the car is no longer
  8490. 6:49:05running that is
  8491. 6:49:06false let's add two more methods we can
  8492. 6:49:10start we can stop we can drive we'll
  8493. 6:49:13create a method to drive void
  8494. 6:49:19Drive let's output you drive the I'm
  8495. 6:49:24going to use string concatenation we'll
  8496. 6:49:26concatenate the model of the
  8497. 6:49:29car so my car is a Mustang
  8498. 6:49:33we will create a method to
  8499. 6:49:38break
  8500. 6:49:40you break the plus our model variable
  8501. 6:49:45our model
  8502. 6:49:47attribute we can delete these lines of
  8503. 6:49:50code let's drive the car car.
  8504. 6:49:54drive you drive the Mustang and here's
  8505. 6:49:57the model of the car and we can break
  8506. 6:50:00car. break
  8507. 6:50:05you drive the Mustang you break the
  8508. 6:50:08Mustang there's one issue with this
  8509. 6:50:10though with our class of car every car
  8510. 6:50:14that we make has the same attributes and
  8511. 6:50:16methods we can only create 225 Ford
  8512. 6:50:22Mustangs if I was to create another car
  8513. 6:50:25object let's say car car 2
  8514. 6:50:29equals new
  8515. 6:50:31car we'll rename the first car as car
  8516. 6:50:351 and I'll output some of their
  8517. 6:50:38attributes Car
  8518. 6:50:411make Plus a
  8519. 6:50:44space plus car
  8520. 6:50:471 model we'll do this with our second
  8521. 6:50:50car object too Car 2 car 2. make car 2.
  8522. 6:50:56model these cars have the same
  8523. 6:50:59attributes it would be nice if we had a
  8524. 6:51:01way to customize them so that each car
  8525. 6:51:04is unique because currently they're all
  8526. 6:51:06the same they're different cars but they
  8527. 6:51:08have the same attributes and methods
  8528. 6:51:10which is kind of lame that's why in the
  8529. 6:51:12next video we're going to discuss
  8530. 6:51:14Constructors by passing in arguments we
  8531. 6:51:17can create unique objects in summary an
  8532. 6:51:20object is an entity that holds data it
  8533. 6:51:23has attributes and they can perform
  8534. 6:51:26actions they can perform
  8535. 6:51:28methods they have things and they can do
  8536. 6:51:31things and and well everybody those are
  8537. 6:51:34objects in
  8538. 6:51:37Java hey yeah today we're talking about
  8539. 6:51:39Constructors in Java a Constructor is
  8540. 6:51:42just a special method within a class
  8541. 6:51:44it's used to initialize objects by using
  8542. 6:51:47a Constructor we can create objects with
  8543. 6:51:50unique values when you create an object
  8544. 6:51:52you can pass arguments to a Constructor
  8545. 6:51:54to set up the initial values here's a
  8546. 6:51:57demonstration what we're going to do is
  8547. 6:52:00create a new class let's go to file new
  8548. 6:52:04Java
  8549. 6:52:05class let's create a class of students
  8550. 6:52:09this will be the student
  8551. 6:52:11class what are some attributes that a
  8552. 6:52:14student should have they should have a
  8553. 6:52:17name let me zoom in a little so you can
  8554. 6:52:19read it NH AG int
  8555. 6:52:22ag ag GPA meaning grade point
  8556. 6:52:26average let's create a Boolean of is
  8557. 6:52:30enrolled
  8558. 6:52:33that is good enough for now there's one
  8559. 6:52:35issue with this let's say that all
  8560. 6:52:38students that I create using this class
  8561. 6:52:41they're all going to be named
  8562. 6:52:43SpongeBob going back to my Java file
  8563. 6:52:46I'll create a few
  8564. 6:52:47students we type the name of the class
  8565. 6:52:49and name for this object let's say
  8566. 6:52:51student one equals new student the name
  8567. 6:52:55of the class
  8568. 6:52:56again then we'll create a second
  8569. 6:52:59student student two
  8570. 6:53:03I'm going to Output student one's name
  8571. 6:53:06student 1. name as well as student 2's
  8572. 6:53:13name the issue that we have is that all
  8573. 6:53:16students that we create all student
  8574. 6:53:18objects they're all named
  8575. 6:53:21SpongeBob what if we would like to give
  8576. 6:53:24each object a unique name well we can do
  8577. 6:53:27that with the help of a Constructor a
  8578. 6:53:29Constructor is just a special method
  8579. 6:53:31within a class
  8580. 6:53:33to set up a Constructor we type the name
  8581. 6:53:35of the class in this case student you
  8582. 6:53:38need a set of parentheses then a set of
  8583. 6:53:40curly
  8584. 6:53:41braces we automatically call this
  8585. 6:53:45Constructor when we instantiate an
  8586. 6:53:47object of that class when we say new
  8587. 6:53:51then the name of the class
  8588. 6:53:53parenthesis behind the scenes we're
  8589. 6:53:56calling this
  8590. 6:53:57Constructor and we can pass in
  8591. 6:53:59arguments within the set of parentheses
  8592. 6:54:01we can pass in arguments so let's pass
  8593. 6:54:04in what are we missing a name age GPA is
  8594. 6:54:08enrolled as well but we'll take care of
  8595. 6:54:10that momentarily let's pass in a name
  8596. 6:54:13age and
  8597. 6:54:14GPA student one's name will be
  8598. 6:54:19SpongeBob SpongeBob's age will be let's
  8599. 6:54:21say 30 he seems about 30 SpongeBob's GPA
  8600. 6:54:25can be 3.2 that's pretty
  8601. 6:54:29good we have a warning our Constructor
  8602. 6:54:32isn't set up to take these arguments
  8603. 6:54:35going back to our Constructor since
  8604. 6:54:37we're receiving arguments we need a
  8605. 6:54:39matching set of parameters just like any
  8606. 6:54:41other method we have a string for name
  8607. 6:54:44integer of age double GPA we'll take
  8608. 6:54:47care of these three first we have string
  8609. 6:54:50name int age double
  8610. 6:54:55GPA to assign some of these attributes
  8611. 6:54:59we're going to use the this keyword this
  8612. 6:55:02dot the name of the attribute this do
  8613. 6:55:06name equals our parameter of
  8614. 6:55:10name this. AG equals the age that we
  8615. 6:55:15receive this. GPA equals the GPA that we
  8616. 6:55:20receive going back to our Java file we
  8617. 6:55:23can now construct an object if I attempt
  8618. 6:55:26to run this currently we are receiving a
  8619. 6:55:30warning our Constructor expects three
  8620. 6:55:33arguments but we don't pass in any zero
  8621. 6:55:35are found if I were to run
  8622. 6:55:38this we get this error message actual
  8623. 6:55:42and formal argument lists differ in
  8624. 6:55:44length in order for us to create a scent
  8625. 6:55:47object we have to pass in these three
  8626. 6:55:49things a string for the name an integer
  8627. 6:55:52for the age and a double for the
  8628. 6:55:55GPA if we are missing these three we
  8629. 6:55:58can't construct an object but previously
  8630. 6:56:01we could with without the Constructor
  8631. 6:56:03for our second student let's pass in a
  8632. 6:56:06name of Patrick Patrick will be
  8633. 6:56:0934 Patrick's GPA isn't too good it's a
  8634. 6:56:161.5 all right let's test this we're
  8635. 6:56:18printing student one's name and student
  8636. 6:56:202's
  8637. 6:56:22name we get SpongeBob and
  8638. 6:56:25Patrick let's display some of the other
  8639. 6:56:30attributes we have
  8640. 6:56:33name age
  8641. 6:56:36GPA we'll do this with student 2 as well
  8642. 6:56:39student 2.age student 2.
  8643. 6:56:44GPA SpongeBob SpongeBob is 30 years old
  8644. 6:56:48SpongeBob's GPA is
  8645. 6:56:503.2 these are all the unique attributes
  8646. 6:56:53of our student object student
  8647. 6:56:55one student 2's attributes are Patrick
  8648. 6:56:59age is 34 GPA is 1.5
  8649. 6:57:04with the this keyword this refers to the
  8650. 6:57:07object we're currently constructing or
  8651. 6:57:09otherwise working with imagine that when
  8652. 6:57:12we construct student
  8653. 6:57:14one imagine we replace the this keyword
  8654. 6:57:17with the name of the object we're
  8655. 6:57:20assigning student one's name equal to
  8656. 6:57:22the name that we receive same thing
  8657. 6:57:25applies with our age and our
  8658. 6:57:28GPA the same thing would apply with
  8659. 6:57:30student 2 when we construct student 2
  8660. 6:57:34imagine during the process we replace
  8661. 6:57:36this with student
  8662. 6:57:392 this refers to the object we're
  8663. 6:57:41currently working with or
  8664. 6:57:43constructing with these parameter names
  8665. 6:57:46they don't necessarily need to be the
  8666. 6:57:47same as your attribute names just to
  8667. 6:57:50demonstrate let's rename name as a age
  8668. 6:57:53as B GPA as C this.name equals a this.
  8669. 6:57:59age equals B this. GPA equals c
  8670. 6:58:03this should work
  8671. 6:58:05too each object still has their unique
  8672. 6:58:11attributes but I like to keep the
  8673. 6:58:12parameter names the same as the
  8674. 6:58:14attributes it makes the code easier to
  8675. 6:58:16read and understand we haven't assigned
  8676. 6:58:19to this attribute of is
  8677. 6:58:21enrolled each object that we create
  8678. 6:58:23using this Constructor let's assign this
  8679. 6:58:26objects is enrolled attribute equal to
  8680. 6:58:29you don't always necessarily need to
  8681. 6:58:32assign arguments to these attributes for
  8682. 6:58:34example let's just say that when you
  8683. 6:58:36become a student you are enrolled that
  8684. 6:58:39is true we don't necessarily need to
  8685. 6:58:41pass in an argument for that going back
  8686. 6:58:44to our main class let's print take
  8687. 6:58:48student ones is enrolled attribute and
  8688. 6:58:51then print it we'll do the same thing
  8689. 6:58:54with student two
  8690. 6:58:57Patrick when we create a student object
  8691. 6:59:00we're automatically setting is a
  8692. 6:59:02enrolled to be true they are enrolled in
  8693. 6:59:04classes and that applies to Patrick as
  8694. 6:59:07well student to Let's create another
  8695. 6:59:09student object just so that we get the
  8696. 6:59:11hang of this repetition is going to help
  8697. 6:59:13you remember let's create student 3
  8698. 6:59:17student 3 is an object
  8699. 6:59:19new
  8700. 6:59:21student to create a student we have to
  8701. 6:59:23pass in three things a string for the
  8702. 6:59:25name an integer for the age and a double
  8703. 6:59:28for the
  8704. 6:59:30GPA student 3 will be be
  8705. 6:59:33Sandy Sandy is 27 let's say Sandy is
  8706. 6:59:37smart she'll have a 4.0
  8707. 6:59:40GPA and then we'll print student 3's
  8708. 6:59:43attributes student 3. name student 3's
  8709. 6:59:47age student 3's GPA student 3 is
  8710. 6:59:52enrolled our object of student 3 has
  8711. 6:59:55these attributes Sandy 27 4.0 and
  8712. 7:00:00true if if your class has any methods
  8713. 7:00:03for example all students have the method
  8714. 7:00:06to
  8715. 7:00:09study void study I'll just output the
  8716. 7:00:13following let's take our name
  8717. 7:00:16attribute but I'm going to use
  8718. 7:00:19this.name plus the text of is
  8719. 7:00:24studying let's delete these print line
  8720. 7:00:27statements we'll take student one call
  8721. 7:00:30the study method
  8722. 7:00:33then we'll do this with student two and
  8723. 7:00:35student
  8724. 7:00:393 SpongeBob is studying Patrick is
  8725. 7:00:42studying Sandy is
  8726. 7:00:45studying after assigning values to your
  8727. 7:00:48attributes using a Constructor you can
  8728. 7:00:50use them within methods too or change
  8729. 7:00:53them this refers to the object we're
  8730. 7:00:56currently working with if student 3
  8731. 7:00:58meaning Sandy was to call her study
  8732. 7:01:00method imagine we're replacing this with
  8733. 7:01:03student 3 student 3's name
  8734. 7:01:08attribute all right everybody those are
  8735. 7:01:10Constructors they're a special method
  8736. 7:01:12found within a class to initialize
  8737. 7:01:14objects you can pass arguments to a
  8738. 7:01:17Constructor when you initialize them
  8739. 7:01:19they're used to set up the initial
  8740. 7:01:21values when you construct an object a
  8741. 7:01:24Constructor is automatically called when
  8742. 7:01:26you create a new object but you need a
  8743. 7:01:28matching set of arguments if there's any
  8744. 7:01:31parameter setup just like any other
  8745. 7:01:34method by using a Constructor we can
  8746. 7:01:37create objects with unique values and
  8747. 7:01:40well everybody those are Constructors in
  8748. 7:01:44Java what is going on people today I'm
  8749. 7:01:47going to explain overloaded Constructors
  8750. 7:01:50overloaded Constructors allow a class to
  8751. 7:01:52have multiple Constructors with
  8752. 7:01:54different parameter lists much like
  8753. 7:01:56overloaded methods they enable an object
  8754. 7:01:59to be initialized in various ways here's
  8755. 7:02:02a demonstration we are going to create a
  8756. 7:02:04new class let's go to file new Java
  8757. 7:02:08class we will create a class for users
  8758. 7:02:11we will create a user
  8759. 7:02:13class let's say that users have three
  8760. 7:02:17things they will have a string of
  8761. 7:02:21username a string of
  8762. 7:02:24email and an integer in
  8763. 7:02:27age let's say that when we create a user
  8764. 7:02:30object these three fields are optional
  8765. 7:02:34if these three attributes are optional
  8766. 7:02:36we may or may not receive them when we
  8767. 7:02:38construct a user object so let's do this
  8768. 7:02:41let's create a Constructor if somebody
  8769. 7:02:43were to create a user object with just a
  8770. 7:02:46username to create a Constructor we type
  8771. 7:02:49the name of the class in this case
  8772. 7:02:52user we need to set up a parameter for
  8773. 7:02:55our username we will receive a string of
  8774. 7:02:58username we will assign this St user
  8775. 7:03:01usern name equal to the username that we
  8776. 7:03:04receive as an argument let's go back to
  8777. 7:03:07our main Java
  8778. 7:03:10file we will create a new
  8779. 7:03:12user let's say user user one equals new
  8780. 7:03:20user we're receiving a warning
  8781. 7:03:23though in order to create a user object
  8782. 7:03:26we have to pass in a string of
  8783. 7:03:29username let's say that our user is
  8784. 7:03:31going to be SpongeBob because that's the
  8785. 7:03:33first thing that came to mind for
  8786. 7:03:35me we'll use this Constructor where we
  8787. 7:03:37have a username we can also set up the
  8788. 7:03:39default values for our attributes too
  8789. 7:03:42let's say this. email equals a string of
  8790. 7:03:46not
  8791. 7:03:49provided this. AG equals
  8792. 7:03:520 going back to user
  8793. 7:03:55one I will output the
  8794. 7:03:58following user one dot
  8795. 7:04:05username user one's email user 1's age
  8796. 7:04:11here's what we got
  8797. 7:04:13currently username is SpongeBob email is
  8798. 7:04:16not provided age is
  8799. 7:04:19zero these attributes were optional when
  8800. 7:04:23we construct this object we're just
  8801. 7:04:25assigning them to some default values
  8802. 7:04:27what if we create a user that has an
  8803. 7:04:29email well we could create another
  8804. 7:04:31Constructor with the same
  8805. 7:04:33name but we need a different set of
  8806. 7:04:36parameters this is very similar to
  8807. 7:04:39Method overloading you can have methods
  8808. 7:04:41with the same name but they need
  8809. 7:04:43different
  8810. 7:04:45parameters not only do we have a string
  8811. 7:04:47of username we'll have a string of
  8812. 7:04:50email our email attribute equals the
  8813. 7:04:54email that we receive we can use either
  8814. 7:04:57of these Constructors depending on the
  8815. 7:04:58arguments that are passed in
  8816. 7:05:01let's create a second user user user 2
  8817. 7:05:06equals new
  8818. 7:05:09user the username will be
  8819. 7:05:12Patrick Patrick's email is let's say
  8820. 7:05:17p Patrick is probably still using AOL
  8821. 7:05:20for some reason let's say
  8822. 7:05:23aol.com then we'll print user 2's
  8823. 7:05:26attributes user 2. username user two.
  8824. 7:05:29email user 2.age
  8825. 7:05:32here's what we got we have all the
  8826. 7:05:35attributes for user one and user two
  8827. 7:05:38Patrick P star
  8828. 7:05:40aol.com our age is still
  8829. 7:05:42zero we can create different objects
  8830. 7:05:45with a varying number of
  8831. 7:05:47arguments let's create a third
  8832. 7:05:50user this time we'll have a parameter
  8833. 7:05:52for our age
  8834. 7:05:54attribute I'm just going to copy this
  8835. 7:05:55cuz I'm lazy we'll have an INT age we
  8836. 7:06:00will assign our age variable
  8837. 7:06:02with the age argument we
  8838. 7:06:04receive we'll create user
  8839. 7:06:063 user user 3 equals new
  8840. 7:06:14user username will be
  8841. 7:06:18Sandy email will be S
  8842. 7:06:22cheeks
  8843. 7:06:24gmail.com then an age let's say
  8844. 7:06:2727 then I'll print user 3's attributes
  8845. 7:06:31user 3. username email then
  8846. 7:06:36age here is all of sy's attributes
  8847. 7:06:40username email
  8848. 7:06:44age let's set up a Constructor that
  8849. 7:06:46accepts no
  8850. 7:06:50arguments this will be kind of an
  8851. 7:06:52anonymous
  8852. 7:06:54profile this. username will equal guest
  8853. 7:06:58or a guest
  8854. 7:07:00profile this . email
  8855. 7:07:03equals not
  8856. 7:07:08provided this. AG equals
  8857. 7:07:12z now we could create a user object then
  8858. 7:07:15pass in no arguments user user 4 equals
  8859. 7:07:19new user this time we don't have any
  8860. 7:07:23arguments then I'll display user Force
  8861. 7:07:29attributes username
  8862. 7:07:38email
  8863. 7:07:40age here's what we got username guessed
  8864. 7:07:43email not provided age is
  8865. 7:07:46zero in a way by creating an object and
  8866. 7:07:49passing in no arguments you can set up
  8867. 7:07:51some default
  8868. 7:07:53values all right everybody those are
  8869. 7:07:55overloaded Constructors they allow a
  8870. 7:07:57class to have multiple Constructors they
  8871. 7:08:01just need different parameter lists they
  8872. 7:08:03enable objects to be initialized in
  8873. 7:08:05various Ways by passing in a varying
  8874. 7:08:08amount of
  8875. 7:08:09arguments this would be great in a
  8876. 7:08:11situation where some fields are optional
  8877. 7:08:14when creating an object and well
  8878. 7:08:16everybody those are overloaded
  8879. 7:08:18Constructors in
  8880. 7:08:21Java hey everybody in this video I'm
  8881. 7:08:23going to show you how we can create an
  8882. 7:08:25array of objects using Java it's pretty
  8883. 7:08:27useful at times and you should probably
  8884. 7:08:28know how to do it we'll Begin by
  8885. 7:08:30creating a new class let's go to file
  8886. 7:08:32new class we will create a car
  8887. 7:08:36class let's say that cars have the
  8888. 7:08:38following
  8889. 7:08:39attributes we'll just pick two to keep
  8890. 7:08:41it simple a string of model and a string
  8891. 7:08:45of color we'll need a car
  8892. 7:08:48Constructor we will pass in a string of
  8893. 7:08:51model and a string of color we will
  8894. 7:08:55assign this. model equals the model we
  8895. 7:08:59receive this. color equals the color
  8896. 7:09:02that we receive we'll add one method
  8897. 7:09:06void drive all cars can drive well I'll
  8898. 7:09:10put the following using a print line
  8899. 7:09:13statement you drive
  8900. 7:09:17the plus this. color plus a space plus
  8901. 7:09:23this. model okay we are done with the
  8902. 7:09:26car
  8903. 7:09:28class we'll construct a few car objects
  8904. 7:09:31let's start with car one car car 1
  8905. 7:09:34equals a new car we have to pass in a
  8906. 7:09:37model and a
  8907. 7:09:38color think of some cars that you like I
  8908. 7:09:42will pick a red Mustang for one car
  8909. 7:09:44let's create Car 2 Car 2 equals a new
  8910. 7:09:48car I will pick a
  8911. 7:09:52Corvette that is blue one more car car 3
  8912. 7:09:57car car 3 equals a new
  8913. 7:10:00car a yellow
  8914. 7:10:05charger all right we have our three car
  8915. 7:10:07objects we can create an array but for
  8916. 7:10:10an array you have to list the data type
  8917. 7:10:12of what you're storing we're going to be
  8918. 7:10:14storing car objects the data type of the
  8919. 7:10:16array is going to be car objects car
  8920. 7:10:20then for an array we need a set of
  8921. 7:10:21straight
  8922. 7:10:22brackets what is the name of this array
  8923. 7:10:24going to be well I think cars would be
  8924. 7:10:27appropriate cars equals if you're
  8925. 7:10:29creating an empty array you're going to
  8926. 7:10:31use the new keyword list the data type
  8927. 7:10:34of
  8928. 7:10:35car within the straight brackets specify
  8929. 7:10:38the size of this array let's say three
  8930. 7:10:40elements Max or we could go ahead and
  8931. 7:10:43just assign these right away too using a
  8932. 7:10:45set of cly braces let's Place car 1 Car
  8933. 7:10:492 and car 3 within this array we'll
  8934. 7:10:52place these three car objects within
  8935. 7:10:54this array of cars now you could use a
  8936. 7:10:57for Loop to iterate through this array
  8937. 7:10:59here's how we'll start with the standard
  8938. 7:11:02for Loop we'll need an index in I equals
  8939. 7:11:06z we'll continue as long as I is less
  8940. 7:11:09than our cars length property then
  8941. 7:11:12increment I by one we'll access our
  8942. 7:11:15array of cars at index of I which is
  8943. 7:11:18going to increase each time but it
  8944. 7:11:19starts at zero we will call the drive
  8945. 7:11:22method and let's see what
  8946. 7:11:27happens you drive the red Mustang you
  8947. 7:11:29drive the blue Corvette you drive the
  8948. 7:11:31yellow
  8949. 7:11:32charger now an enhanced for Loop would
  8950. 7:11:35probably be more appropriate for
  8951. 7:11:39this if you want to use an enhanced for
  8952. 7:11:41Loop type four parentheses curly
  8953. 7:11:44braces what is the data type of each
  8954. 7:11:47element within this array car objects
  8955. 7:11:50we'll need a nickname for each element
  8956. 7:11:52within this array let's say car colon
  8957. 7:11:55think of colon as in then our array of
  8958. 7:11:59cars for every car object in our array
  8959. 7:12:02of cars do the following we'll take each
  8960. 7:12:05car object call the drive
  8961. 7:12:08method and that's going to do the same
  8962. 7:12:10thing you drive the red Mustang you
  8963. 7:12:12drive the blue Corvette you drive the
  8964. 7:12:14yellow
  8965. 7:12:15charger another thing you can do to when
  8966. 7:12:17creating an array of objects you can
  8967. 7:12:20pass in Anonymous objects rather than
  8968. 7:12:22first instantiating the objects then
  8969. 7:12:24assigning them to the array here's how
  8970. 7:12:26you can do
  8971. 7:12:29that for each element we're going to
  8972. 7:12:31call the car Constructor with the new
  8973. 7:12:34keyword call the car Constructor then
  8974. 7:12:36pass in the details for each car the
  8975. 7:12:38necessary arguments we'll pass in our
  8976. 7:12:42Mustang that's red that's the first
  8977. 7:12:44element I'll just copy it to save some
  8978. 7:12:47time then we're creating a new car but
  8979. 7:12:50this will be a
  8980. 7:12:52Corvette that's
  8981. 7:12:54blue and I'll put each of these on a new
  8982. 7:12:56line just for readability
  8983. 7:13:04and then we have our
  8984. 7:13:05charger that's
  8985. 7:13:09yellow and this will do the same thing
  8986. 7:13:13too we have our three
  8987. 7:13:16cars constructing a new object without
  8988. 7:13:18giving the object a unique identifier
  8989. 7:13:20like car 1 Car 2 or car 3 these are
  8990. 7:13:23known as Anonymous
  8991. 7:13:25objects So within an array you can
  8992. 7:13:28create Anonymous objects if you would
  8993. 7:13:30rather not give each of the car is a
  8994. 7:13:31unique identifier another thing you can
  8995. 7:13:33do too with an array of objects is let's
  8996. 7:13:36change the color of each car using our
  8997. 7:13:38enhanced for Loop car do
  8998. 7:13:41color let's change the color to be black
  8999. 7:13:44for each
  9000. 7:13:45car we'll use another enhanced for Loop
  9001. 7:13:48to display each car car die
  9002. 7:13:53drive you drive the black Mustang you
  9003. 7:13:56drive the black Corvette you drive the
  9004. 7:13:58black
  9005. 7:13:59charger all all right everybody those
  9006. 7:14:01are a few things you can do with an
  9007. 7:14:03array of objects in
  9008. 7:14:07Java hello everybody today I'm going to
  9009. 7:14:09explain the static keyword using Java
  9010. 7:14:12static is a keyword it modifies a
  9011. 7:14:14variable or method so that it belongs to
  9012. 7:14:17the class rather than any specific
  9013. 7:14:19object we see the static keyword within
  9014. 7:14:21the main method when you modify a
  9015. 7:14:23variable or method with the static
  9016. 7:14:25keyword static is used to create utility
  9017. 7:14:28methods or other shared resources let me
  9018. 7:14:31give you a demonstration we're going to
  9019. 7:14:32create a new class let's go to file new
  9020. 7:14:36class we'll create a front class we will
  9021. 7:14:39create some friend objects because
  9022. 7:14:41you're
  9023. 7:14:42lonely let's say that all friends will
  9024. 7:14:44have a name attribute string
  9025. 7:14:47name we need a Constructor it's the same
  9026. 7:14:50as the class name parenthesis curly
  9027. 7:14:52braces when we construct a friend object
  9028. 7:14:54we have to pass in a string of name we
  9029. 7:14:57will assign this object's name attribute
  9030. 7:15:01equal to the name parameter that we
  9031. 7:15:03receive let's construct two friends
  9032. 7:15:06we'll say friend friend one equals new
  9033. 7:15:11friend but we have to pass in a name
  9034. 7:15:14pick a name my default is SpongeBob
  9035. 7:15:17because most people know who SpongeBob
  9036. 7:15:19is if I was to Output friend one's name
  9037. 7:15:23then it should be SpongeBob we just want
  9038. 7:15:25to be sure that it
  9039. 7:15:27works yes it does SpongeBob
  9040. 7:15:31now going back to our friend class let's
  9041. 7:15:34say we would like to keep track of how
  9042. 7:15:35many friend objects we create well that
  9043. 7:15:38could be an attribute let's say we have
  9044. 7:15:40an integer of num of friends meaning
  9045. 7:15:45number of friends whenever we create a
  9046. 7:15:47friend object we will increment number
  9047. 7:15:50of friends by one num of friends
  9048. 7:15:54Plus+ so going back let's output friend
  9049. 7:15:59On's number of friends attribute num of
  9050. 7:16:03friends so this should be one yep num
  9051. 7:16:07friends is one we have one friend but
  9052. 7:16:10what if we create a second friend object
  9053. 7:16:12I'll just copy this line of code paste
  9054. 7:16:14it change friend one to friend two we'll
  9055. 7:16:17pass in a new name let's say
  9056. 7:16:19Patrick we'll print friend One's number
  9057. 7:16:22of friends and friend two's number of
  9058. 7:16:24friends how many friend objects have we
  9059. 7:16:27created we have created two but what's
  9060. 7:16:30the out
  9061. 7:16:31put well it's one for
  9062. 7:16:34both here's why each friend object has
  9063. 7:16:38their own copy of the number of friends
  9064. 7:16:40variable we're incrementing each copy of
  9065. 7:16:43number of friends by one when we create
  9066. 7:16:46the object of friend one we're
  9067. 7:16:48incrementing its attribute of number of
  9068. 7:16:50friends by one same thing applies with
  9069. 7:16:52friend two when we create friend two
  9070. 7:16:54friend 2's number of friends is zero and
  9071. 7:16:57we're incrementing it by one what if we
  9072. 7:16:59would like to keep track of the total
  9073. 7:17:01amount of friends that we created and
  9074. 7:17:02store it within one variable well there
  9075. 7:17:05is a way we can do that and that is with
  9076. 7:17:07the static
  9077. 7:17:08modifier so going back to our friend
  9078. 7:17:10class we're going to precede the data
  9079. 7:17:13type of this attribute with the static
  9080. 7:17:15modifier static int number of friends
  9081. 7:17:19rather than all objects all friend
  9082. 7:17:21objects having their own copy of number
  9083. 7:17:23of friends they're all going to share
  9084. 7:17:25one so going back let's print a number
  9085. 7:17:28of friends now
  9086. 7:17:31it's two we have two total friends if I
  9087. 7:17:34was to create a third friend friend
  9088. 7:17:38three friend three will be
  9089. 7:17:44Squidward and then I'll output friend
  9090. 7:17:463's number of
  9091. 7:17:49friends we have three total friends we
  9092. 7:17:52are receiving a suggestion when
  9093. 7:17:54accessing a static variable or calling a
  9094. 7:17:56static method it's actually best to do
  9095. 7:17:59so through the class itself
  9096. 7:18:01rather than any object created from that
  9097. 7:18:03class instead of saying you know friend
  9098. 7:18:05one then accessing the static member we
  9099. 7:18:08will instead use the name of the class
  9100. 7:18:10in this case friend it's better for
  9101. 7:18:12clarity to access a static member by the
  9102. 7:18:15name of the class itself rather than any
  9103. 7:18:17object because you and other developers
  9104. 7:18:19will know that this is a static
  9105. 7:18:21attribute or method to display the
  9106. 7:18:24static variable we would want to access
  9107. 7:18:26it by the class name then access that
  9108. 7:18:27static attribute how many friends do we
  9109. 7:18:30have now three let's create a fourth
  9110. 7:18:34friend friend four will be Sandy then
  9111. 7:18:40display the number of friends we
  9112. 7:18:42have now we have four friends going back
  9113. 7:18:45to the friend class it's as if all these
  9114. 7:18:48objects are sharing the same variable
  9115. 7:18:51but rather than any one object having
  9116. 7:18:53ownership of this variable rather the
  9117. 7:18:55friend class owns it and all objects
  9118. 7:18:58have access to it methods can also be
  9119. 7:19:01modified by the static
  9120. 7:19:03keyword we will create a static method
  9121. 7:19:06that doesn't return anything the return
  9122. 7:19:08type is void this method will be the
  9123. 7:19:11show Friends method we'll just output
  9124. 7:19:14the following we'll output you
  9125. 7:19:18have
  9126. 7:19:20plus normally when you access an
  9127. 7:19:22attribute you type this dot the name of
  9128. 7:19:24the attribute such as you know name if
  9129. 7:19:27you're working with the static attribute
  9130. 7:19:29you don't need this
  9131. 7:19:31for example this. number of friends you
  9132. 7:19:33don't need this we even have a warning
  9133. 7:19:36here that friends. this cannot be
  9134. 7:19:38referenced from a static context if
  9135. 7:19:40you're working with a static attribute
  9136. 7:19:42you don't need this this refers to the
  9137. 7:19:44object you're currently working with but
  9138. 7:19:47since this attribute belongs to the
  9139. 7:19:48class we don't need this you have your
  9140. 7:19:51static attribute of number of friends
  9141. 7:19:54then the words total friends
  9142. 7:20:02to call a static method type the name of
  9143. 7:20:04the class in this case friend dot the
  9144. 7:20:07name of the method show
  9145. 7:20:12friends you have four total friends
  9146. 7:20:15let's create another friend friend
  9147. 7:20:19five friend five will be
  9148. 7:20:23Gary friend. show friends you have five
  9149. 7:20:27total
  9150. 7:20:28friends another place in which you see
  9151. 7:20:30static members is through the math class
  9152. 7:20:33for example let's say you would like to
  9153. 7:20:34round a number you would type the name
  9154. 7:20:36of the math class math. round and you
  9155. 7:20:39can round a
  9156. 7:20:40number round is a utility method we
  9157. 7:20:44access it through the class name of math
  9158. 7:20:47you don't need to go ahead and create a
  9159. 7:20:48math object such as math math 1 equals
  9160. 7:20:52new math and then access it through this
  9161. 7:20:56object that would be
  9162. 7:20:58silly round is a static method it
  9163. 7:21:01behaves as a utility method we can
  9164. 7:21:03access it through the class name and we
  9165. 7:21:04don't need to create any math objects
  9166. 7:21:06because that's stupid and to prove this
  9167. 7:21:09let's go to the math class I'm going to
  9168. 7:21:11hover over math jump to
  9169. 7:21:16Source here's the math class and let's
  9170. 7:21:19find the round method I'm going to do a
  9171. 7:21:21search for
  9172. 7:21:23it here's the round
  9173. 7:21:25method so round is a method and we have
  9174. 7:21:29that static keyword round is a utility
  9175. 7:21:32method for the math class we can access
  9176. 7:21:35it through the name of the class all
  9177. 7:21:37right everybody so that is the static
  9178. 7:21:39keyword it modifies a variable or method
  9179. 7:21:42so that it belongs to the class rather
  9180. 7:21:44than any specific object it's commonly
  9181. 7:21:47used for utility methods or shared
  9182. 7:21:50resources among objects created from
  9183. 7:21:52that class such as the number of friends
  9184. 7:21:55all those friend objects share the same
  9185. 7:21:57attribute rather than each of them
  9186. 7:21:58having their own copy and and well
  9187. 7:22:00everybody that is the static keyword in
  9188. 7:22:03Java what is going on people today we
  9189. 7:22:06got a very important topic to discuss
  9190. 7:22:08today and that is inheritance
  9191. 7:22:10inheritance is where one class inherits
  9192. 7:22:13the attributes and methods from another
  9193. 7:22:15class much like how a child can inherit
  9194. 7:22:18traits from A parent well in programming
  9195. 7:22:20we could do something similar one class
  9196. 7:22:23a child's class can inherit traits from
  9197. 7:22:25A parent here's an example we're going
  9198. 7:22:28to create a few classes but we'll start
  9199. 7:22:30with a new class of animal let's go to
  9200. 7:22:33new class I will create an animal
  9201. 7:22:38class what do animals have let's say
  9202. 7:22:41that all animals are alive they will
  9203. 7:22:44start by being alive is
  9204. 7:22:47alive let's create a Constructor of
  9205. 7:22:51animal I will set is alive to be true
  9206. 7:22:54when we construct a new animal object
  9207. 7:22:57but not only are all animals alive all
  9208. 7:23:00animals can eat they will have an eat
  9209. 7:23:02method void eat we'll print something
  9210. 7:23:05like the
  9211. 7:23:06following the animal is eating easting
  9212. 7:23:12eating that is good enough for this
  9213. 7:23:14class so what we're going to do is
  9214. 7:23:17create two more classes file new class
  9215. 7:23:21we will create a dog
  9216. 7:23:23class and a cat
  9217. 7:23:29class dog and cats are types of animals
  9218. 7:23:33our animal class will be the parent the
  9219. 7:23:35dog and cat class will be its children
  9220. 7:23:38in order for a class to inherit all the
  9221. 7:23:40attributes and methods from another
  9222. 7:23:42class after the class name you will use
  9223. 7:23:44the extend
  9224. 7:23:46keyword then specify the parent class
  9225. 7:23:49dog extends animal our dog class will
  9226. 7:23:53inherit all these traits these
  9227. 7:23:55attributes and methods from the animal
  9228. 7:23:57class the cat and dog class don't have
  9229. 7:24:00any anything within them let's see if
  9230. 7:24:02they actually do have these attributes
  9231. 7:24:03and methods even though there's nothing
  9232. 7:24:05within them let's create a dog object
  9233. 7:24:08dog dog equals new dog and a cat cat cat
  9234. 7:24:13equals new
  9235. 7:24:16cat let's print our dogs is alive
  9236. 7:24:20variable is my dog alive I sure hope
  9237. 7:24:24so cat do is
  9238. 7:24:28alive look at that they're both
  9239. 7:24:31true they should also be able to eat
  9240. 7:24:34dog. eat method cat. eat
  9241. 7:24:41method the animal is eating the animal
  9242. 7:24:43is
  9243. 7:24:45eating even though these classes of dog
  9244. 7:24:47and cat don't have anything within them
  9245. 7:24:49since they extend to the animal class
  9246. 7:24:52we're specifying that the animal class
  9247. 7:24:54is the parent the cat and dog classes
  9248. 7:24:57are the children of the animal class
  9249. 7:24:59they inherit all these attributes and
  9250. 7:25:01methods from its parent of animal even
  9251. 7:25:04though they don't have anything within
  9252. 7:25:05them this isn't as useful if you only
  9253. 7:25:07have a few classes such as two because
  9254. 7:25:10you know you could just copy and paste
  9255. 7:25:12all this code within
  9256. 7:25:14each and then just change the
  9257. 7:25:18Constructor but imagine if you had
  9258. 7:25:20hundreds of classes hundreds of children
  9259. 7:25:22you'd have to do that for hundreds of
  9260. 7:25:24classes but not only that if you have to
  9261. 7:25:26make any changes to say you know this
  9262. 7:25:29animal is eating
  9263. 7:25:31you'd have to make that change for
  9264. 7:25:32hundreds of classes and we are repeating
  9265. 7:25:34ourselves a lot it'd be a lot easier if
  9266. 7:25:37we can just make that change in one
  9267. 7:25:40place if we have to change the to this
  9268. 7:25:43all of the children classes will inherit
  9269. 7:25:45this updated method we're following the
  9270. 7:25:48dry principle don't repeat yourself
  9271. 7:25:50we're writing the code once and just
  9272. 7:25:52reusing it all the children classes will
  9273. 7:25:56inherit this updated method but not only
  9274. 7:25:59that each child class can have their own
  9275. 7:26:01unique attributes and methods too let's
  9276. 7:26:04say that all dogs will have one life int
  9277. 7:26:08lives equals 1 they will also have their
  9278. 7:26:12own unique speak method void
  9279. 7:26:16speak when our dog uses the speak method
  9280. 7:26:19let's output the following let's say the
  9281. 7:26:21dog goes
  9282. 7:26:25woof then within the cat
  9283. 7:26:28class cats will have have nine
  9284. 7:26:32lives and they can speak but they will
  9285. 7:26:35say something different when they speak
  9286. 7:26:37let's output the cat goes
  9287. 7:26:43meow going
  9288. 7:26:46back let's output our dog objects lives
  9289. 7:26:50how many lives does our dog have same
  9290. 7:26:53thing applies with our cat cat.
  9291. 7:26:57lives our dog has one life our cat has
  9292. 7:27:00nine
  9293. 7:27:01lives they also have their own unique
  9294. 7:27:03speak methods dog
  9295. 7:27:06dope cat
  9296. 7:27:11dope the dog goes woof the cat goes
  9297. 7:27:15meow there's also the concept of
  9298. 7:27:17multi-level inheritance we'll create a
  9299. 7:27:19new class to serve as a sort of
  9300. 7:27:23grandparent a child class will inherit
  9301. 7:27:25from a parent a parent will inherit from
  9302. 7:27:28a grandparent so let's change this
  9303. 7:27:30program around a little bit we're going
  9304. 7:27:32to create a new class file new Java
  9305. 7:27:35class we will create a class of
  9306. 7:27:41organism animals will extend the
  9307. 7:27:45organism
  9308. 7:27:47class let's cut our is aive variable and
  9309. 7:27:50the
  9310. 7:27:52Constructor if you inherit from the
  9311. 7:27:54animal class you will get an eat
  9312. 7:27:57method if you inherit from the organism
  9313. 7:28:00class you are alive and that's it but we
  9314. 7:28:03need to change the
  9315. 7:28:05Constructor animal inherits from
  9316. 7:28:08organism dog and cat inherit from the
  9317. 7:28:10animal class so from the perspective of
  9318. 7:28:13our dog and cat class the organism class
  9319. 7:28:16is you could say a grandparent going
  9320. 7:28:19back to our main Java file let's take a
  9321. 7:28:22look at our is alive variables dog. is
  9322. 7:28:26alive same thing with our cat
  9323. 7:28:30cat dot is
  9324. 7:28:33alive yes our dog is alive and our cat
  9325. 7:28:36is alive let's take this a step
  9326. 7:28:39further let's create a class of plant
  9327. 7:28:42plant is going to inherit from the
  9328. 7:28:44organism class because plants are a type
  9329. 7:28:46of organism but they're not related to
  9330. 7:28:49animals let's create a new class of
  9331. 7:28:58plant let's say that if you're a plant
  9332. 7:29:00you get a method to well plants don't
  9333. 7:29:04eat but they can
  9334. 7:29:05photosynthesize
  9335. 7:29:07photosynthesize I think I spelled that
  9336. 7:29:09right probably not but who
  9337. 7:29:12cares if you're a plant and you
  9338. 7:29:14photosynthesize then the
  9339. 7:29:18plant absorbs
  9340. 7:29:22sunlight hey this is bro from the future
  9341. 7:29:24I forgot to extend the organism class so
  9342. 7:29:28let's take care of that heading back to
  9343. 7:29:31our main Java file let's create a plant
  9344. 7:29:34object plant plant equals new
  9345. 7:29:40plant our plant object should have an is
  9346. 7:29:43alive attribute is alive which it
  9347. 7:29:47inherits from the organism class that's
  9348. 7:29:50true and plants can
  9349. 7:29:52photosynthesize plant
  9350. 7:29:55do photosynthesize
  9351. 7:30:02the plant absorbs
  9352. 7:30:04sunlight it's kind of like we have a
  9353. 7:30:07whole family tree dog and cat inherit
  9354. 7:30:09from animal animal inherits from
  9355. 7:30:11organism and plant inherits from
  9356. 7:30:14organism but animals and plants aren't
  9357. 7:30:17related they're siblings but they're not
  9358. 7:30:20the same going back to our main Java
  9359. 7:30:22file let's see if our dog has a
  9360. 7:30:24photosynthesize method dog.
  9361. 7:30:28photosynthesize
  9362. 7:30:35looks like they can't we do not have a
  9363. 7:30:37photosynthesize method for dogs they do
  9364. 7:30:40not inherit this method because they're
  9365. 7:30:42not related to
  9366. 7:30:44plants all right everybody so that's
  9367. 7:30:46inheritance it's where one class
  9368. 7:30:48inherits the attributes and methods from
  9369. 7:30:50another class A Child class will inherit
  9370. 7:30:53all the attributes and methods from a
  9371. 7:30:55parent class parents can also be
  9372. 7:30:57considered children too and inherit it
  9373. 7:31:00from a let's say grandparent class you
  9374. 7:31:03just have to use the extends keyword and
  9375. 7:31:06well everybody that's inheritance in
  9376. 7:31:08Java yo so let's get started today I got
  9377. 7:31:12to discuss the super keyword super just
  9378. 7:31:15refers to the parent class when using
  9379. 7:31:17inheritance a child class is also known
  9380. 7:31:19as the sub class the parent class is
  9381. 7:31:22also known as the super class super just
  9382. 7:31:25means parent for all intents and
  9383. 7:31:27purposes the super keyword is used
  9384. 7:31:29within Constructors and Method
  9385. 7:31:31overwriting method overwriting is a
  9386. 7:31:33whole another topic that we'll discuss
  9387. 7:31:35we're more focused on Constructors in
  9388. 7:31:36this topic we use the super keyword to
  9389. 7:31:39call the parent Constructor to
  9390. 7:31:41initialize attributes let me give you an
  9391. 7:31:43example we'll be creating a few
  9392. 7:31:45different classes let's go to file new
  9393. 7:31:49class we will Begin by creating a class
  9394. 7:31:52of
  9395. 7:31:53person person will be the parent the
  9396. 7:31:56super
  9397. 7:31:58class let's say that any person object
  9398. 7:32:01has the following attributes they will
  9399. 7:32:03have a string of first meaning first
  9400. 7:32:06name then string last meaning last name
  9401. 7:32:10we'll need a Constructor it's going to
  9402. 7:32:12be the same as the class name Person
  9403. 7:32:15parentheses curly braces when we
  9404. 7:32:17construct a person object we will need
  9405. 7:32:19to pass in the following arguments a
  9406. 7:32:22string for first name first and a string
  9407. 7:32:25for last name last this objects first
  9408. 7:32:30name attribute this. first
  9409. 7:32:33equals the argument that we receive for
  9410. 7:32:36first same thing applies with last this.
  9411. 7:32:40last equals the argument we receive for
  9412. 7:32:43last and then we'll create one method in
  9413. 7:32:46this demonstration this method won't
  9414. 7:32:48return anything we will name the method
  9415. 7:32:51show
  9416. 7:32:53name all we'll do is output the
  9417. 7:32:57following this do first plus a space
  9418. 7:33:02plus this. last okay let's test this
  9419. 7:33:06class
  9420. 7:33:07out person person equals new
  9421. 7:33:12person but we do have to pass in a first
  9422. 7:33:14and last name I've already used
  9423. 7:33:16SpongeBob for too many examples so let's
  9424. 7:33:18mix it up this time I'll pick Harry
  9425. 7:33:20Potter or feel free to pick a different
  9426. 7:33:22franchise that you like so one person
  9427. 7:33:25from Harry Potter is
  9428. 7:33:27Tom Riddle
  9429. 7:33:31we have a person object with this first
  9430. 7:33:33name and this last name first
  9431. 7:33:38last our person is going to have a show
  9432. 7:33:41name method show name then call
  9433. 7:33:45it there we go there's our person
  9434. 7:33:47objects first and last name Tom
  9435. 7:33:51Riddle our person class will be the
  9436. 7:33:54parent class we'll create a few other
  9437. 7:33:56classes to inherit from this person
  9438. 7:33:58class we're going to create a class of
  9439. 7:34:00student that's going to inherit from the
  9440. 7:34:03person class the student class will
  9441. 7:34:05inherit these attributes and
  9442. 7:34:08methods let's create a new class of
  9443. 7:34:13student student
  9444. 7:34:16will extends the parent or super class
  9445. 7:34:21of
  9446. 7:34:22person since student is inheriting from
  9447. 7:34:25person student is going to have a first
  9448. 7:34:27and last name already and a show name
  9449. 7:34:31method what other attributes does a
  9450. 7:34:33student have that a person might not
  9451. 7:34:36well students have a GPA a grade point
  9452. 7:34:38average we'll declare that as an
  9453. 7:34:40attribute double
  9454. 7:34:42GPA we're going to create a student
  9455. 7:34:46Constructor let's say that when we
  9456. 7:34:48create a student we need to pass in a
  9457. 7:34:51string for first name String
  9458. 7:34:54first string
  9459. 7:34:56last and double GPA what's their grade
  9460. 7:35:00point average now rather than assigning
  9461. 7:35:02our first and last name within this
  9462. 7:35:04Constructor such as this. first equals
  9463. 7:35:08first this. last equals
  9464. 7:35:12last and this. GPA equals GPA we are
  9465. 7:35:18receiving a
  9466. 7:35:20warning there is no parameterless
  9467. 7:35:23Constructor available in person we're
  9468. 7:35:25not able to assign these attributes of
  9469. 7:35:27first and last within the Constructor
  9470. 7:35:29for student this is because student
  9471. 7:35:32inherits from the person class since the
  9472. 7:35:34Constructor of our parent requires a
  9473. 7:35:37first and last name we have to pass
  9474. 7:35:40these arguments to its parent of person
  9475. 7:35:42how we can do that going back to the
  9476. 7:35:44child class we're going to be using the
  9477. 7:35:47super keyword any arguments that the
  9478. 7:35:49parent requires we have to send the
  9479. 7:35:52parent these arguments from the child
  9480. 7:35:55Constructor and how we can do that is
  9481. 7:35:57use the super keyword
  9482. 7:36:01and then pass in those arguments of
  9483. 7:36:03first and last and now Java is
  9484. 7:36:07Happy super just refers to the parent
  9485. 7:36:11imagine that we replace the super
  9486. 7:36:13keyword with
  9487. 7:36:15person think of it like that and now
  9488. 7:36:18this should
  9489. 7:36:19work going back to our main file we
  9490. 7:36:23should be able to create a student
  9491. 7:36:24object student student equals new
  9492. 7:36:28student
  9493. 7:36:31we have to pass in a first name first
  9494. 7:36:33name will be
  9495. 7:36:34Harry last name will be
  9496. 7:36:36Potter then we have to pass in a GPA
  9497. 7:36:39because our student Constructor requires
  9498. 7:36:42it Harry Potter has a
  9499. 7:36:453.25 GPA let's say our student object
  9500. 7:36:50should have these attributes this
  9501. 7:36:53method and this attribute from the
  9502. 7:36:56student class of GPA
  9503. 7:36:59Let's test it student. show
  9504. 7:37:03name this student's name is Harry
  9505. 7:37:07Potter and they should have a
  9506. 7:37:10GPA let's print students GPA
  9507. 7:37:15attribute
  9508. 7:37:173.25 but not only that let's create a
  9509. 7:37:19method to display their GPA within the
  9510. 7:37:22student class we will create a method
  9511. 7:37:24that doesn't return anything it will be
  9512. 7:37:27show GPA
  9513. 7:37:30we will output the
  9514. 7:37:31following let's say this.
  9515. 7:37:35first plus a string apostrophe s so
  9516. 7:37:40Harry's
  9517. 7:37:42GPA
  9518. 7:37:45is plus this.
  9519. 7:37:51GPA let's take our student object call
  9520. 7:37:54the show GPA method that we have
  9521. 7:37:57declared
  9522. 7:38:00Harry's GPA is
  9523. 7:38:043.25 let's create one more class we will
  9524. 7:38:06create an employee
  9525. 7:38:09class class
  9526. 7:38:14employee employee will inherit from the
  9527. 7:38:17person
  9528. 7:38:18class employee extends
  9529. 7:38:25person since employees aren't studying
  9530. 7:38:27they're not students they instead get a
  9531. 7:38:30salary we will create an integer
  9532. 7:38:32attribute of
  9533. 7:38:35salary and then we need that Constructor
  9534. 7:38:37for our employee
  9535. 7:38:40object all employees will require the
  9536. 7:38:43following
  9537. 7:38:44arguments a string for first name String
  9538. 7:38:48first a string for last name String last
  9539. 7:38:52and an integer for our employee salary I
  9540. 7:38:56will attempt to assign these attributes
  9541. 7:38:58for our employee again it's not going to
  9542. 7:39:00work but let's try it this. first equals
  9543. 7:39:04first this. last equals
  9544. 7:39:08last this do
  9545. 7:39:10salary equals
  9546. 7:39:13salary again we're receiving that
  9547. 7:39:15warning there is no parameter list
  9548. 7:39:18Constructor available in
  9549. 7:39:20person since employee extends person and
  9550. 7:39:23our person Constructor requires a first
  9551. 7:39:25name and a last name we have to send our
  9552. 7:39:27person Constructor these arguments first
  9553. 7:39:30and last so again going back to our
  9554. 7:39:32employee
  9555. 7:39:33class we will call the Constructor of
  9556. 7:39:36our parent using the super keyword and
  9557. 7:39:39then passing in those arguments first
  9558. 7:39:41and
  9559. 7:39:42last let's create a method within our
  9560. 7:39:44employee class void Show
  9561. 7:39:50salary let's output the following we'll
  9562. 7:39:53say this. first this person's first
  9563. 7:39:57name apostrophe yes we'll create the
  9564. 7:40:01employee of Hagrid their
  9565. 7:40:04salary is pick unit of currency I'll
  9566. 7:40:07pick American dollars plus this do
  9567. 7:40:11salary now we'll need to construct an
  9568. 7:40:13employee
  9569. 7:40:17object
  9570. 7:40:18employee
  9571. 7:40:20employee equals new
  9572. 7:40:23employee we have to pass in a first name
  9573. 7:40:25a last name and a salary in order to
  9574. 7:40:28construct an employee object we're going
  9575. 7:40:30to pick Hagrid Hagrid's first name is
  9576. 7:40:33actually
  9577. 7:40:35rubius last name
  9578. 7:40:40Hagrid Hagrid's salary will be
  9579. 7:40:44$50,000 let's
  9580. 7:40:46say all right let's take our
  9581. 7:40:49employee call its Show salary
  9582. 7:40:55method and there we go rubius ALS also
  9583. 7:40:59known as Hagrid rubi's salary is
  9584. 7:41:03$50,000 in conclusion super refers to
  9585. 7:41:06the parent class student and employee
  9586. 7:41:09inherit from person since the
  9587. 7:41:11Constructor of the parent of person
  9588. 7:41:13requires a first name and a last name
  9589. 7:41:16any children objects that we create we
  9590. 7:41:19have to call the Constructor of the
  9591. 7:41:20parent and pass in those arguments to
  9592. 7:41:23satisfy Java if the parent didn't
  9593. 7:41:25require these
  9594. 7:41:27arguments then you wouldn't need to
  9595. 7:41:34and well everybody that is the super
  9596. 7:41:35keyword in Java what's going on people
  9597. 7:41:39today I'm going to discuss method
  9598. 7:41:40overwriting using Java method
  9599. 7:41:42overwriting is when a subass provides
  9600. 7:41:44its own implementation of a method
  9601. 7:41:46rather than use one that's inherited
  9602. 7:41:47from a parent it allows for code
  9603. 7:41:49reusability and you can give specific
  9604. 7:41:52implementations of a method let me give
  9605. 7:41:54you a demonstration we'll be creating a
  9606. 7:41:55few classes file new class we will Begin
  9607. 7:41:59by creating a class of
  9608. 7:42:02animal then a new
  9609. 7:42:04class of
  9610. 7:42:07dog a
  9611. 7:42:10class of
  9612. 7:42:13cat then finally a class of
  9613. 7:42:18fish dog cat and fish are going to
  9614. 7:42:21extend the animal class after the class
  9615. 7:42:24name type extends
  9616. 7:42:27animal so let's do the this with cat and
  9617. 7:42:32fish the dog cat and fish classes are
  9618. 7:42:35all children of the animal class animal
  9619. 7:42:38class is the parent class there's not a
  9620. 7:42:41lot we have to write with this example
  9621. 7:42:42if a child class inherits from the
  9622. 7:42:44animal parent class they will inherit a
  9623. 7:42:47method to move let's define a method of
  9624. 7:42:50move all we'll say is this animal is
  9625. 7:42:57running we can keep the dog cat and fish
  9626. 7:43:00class is empty for now so going back to
  9627. 7:43:03our main Java file let's construct a dog
  9628. 7:43:06object dog dog equals new dog a cat
  9629. 7:43:09object cat cat equals new cat and a fish
  9630. 7:43:15fish fish equals new
  9631. 7:43:18fish all three of these animal objects
  9632. 7:43:21dog cat and fish should have a move
  9633. 7:43:24method let's take our dog call its move
  9634. 7:43:27method say same thing with the cat and
  9635. 7:43:30the
  9636. 7:43:31fish cat. move method fish. move method
  9637. 7:43:35currently when we run this program we
  9638. 7:43:37get the following
  9639. 7:43:39output our dog is running our cat is
  9640. 7:43:42running and our fish is running but fish
  9641. 7:43:46don't have legs though they can only
  9642. 7:43:49swim the move method that the fish class
  9643. 7:43:52inherits isn't appropriate for that
  9644. 7:43:54class this animal is swimming would be
  9645. 7:43:56more appropriate so why don't we do this
  9646. 7:43:59within the fish class why don't we do
  9647. 7:44:01some method overwriting specifically for
  9648. 7:44:03the fish class we will create a unique
  9649. 7:44:05move method only for this class we will
  9650. 7:44:08Define a new move
  9651. 7:44:11method where we will
  9652. 7:44:14say this animal is
  9653. 7:44:21swimming however it is good practice to
  9654. 7:44:23add this annotation of at override so
  9655. 7:44:27that you and other Developers know that
  9656. 7:44:29this method is being overridden our fish
  9657. 7:44:32class does inherit a move method from
  9658. 7:44:34its parent of animal if you have the
  9659. 7:44:37same named method defined in a child
  9660. 7:44:39class you'll use that one first it has
  9661. 7:44:42precedence going back to our main Java
  9662. 7:44:45file let's call the move method for our
  9663. 7:44:48fish our dog is running our cat is
  9664. 7:44:51running but our fish is swimming within
  9665. 7:44:55the fish class we have overridden the
  9666. 7:44:57move method we have given a specific
  9667. 7:45:00implementation for this method just for
  9668. 7:45:02this one class the cat and dog classes
  9669. 7:45:05will use what's inherited from their
  9670. 7:45:07parent not only is a good practice to
  9671. 7:45:10add this annotation of at override but
  9672. 7:45:12let's say that you misspell this
  9673. 7:45:14overwritten method rather than move
  9674. 7:45:16let's say moves even though it should be
  9675. 7:45:19move we're receiving a warning that this
  9676. 7:45:22method does not override the method from
  9677. 7:45:24its super class it's parent if you
  9678. 7:45:27didn't have this annotation you may not
  9679. 7:45:29notice that you misspelled this method
  9680. 7:45:31and you're not actually overwriting
  9681. 7:45:34it adding the at override annotation
  9682. 7:45:37provides a system of checks and balances
  9683. 7:45:40when overwriting method this error will
  9684. 7:45:42go away once we actually are overwriting
  9685. 7:45:44a
  9686. 7:45:45method all right everybody so that is
  9687. 7:45:47Method overwriting it's when a subass
  9688. 7:45:49provides its own implementation of a
  9689. 7:45:51method rather than use one that's
  9690. 7:45:53already defined it allows for code
  9691. 7:45:55reusability and you can give specific
  9692. 7:45:58implementations of a method we'll be
  9693. 7:45:59seeing method overwriting again with the
  9694. 7:46:01two string method coming up in a future
  9695. 7:46:04topic and well everybody that is method
  9696. 7:46:06overwriting in Java hello everybody
  9697. 7:46:09today I got to explain the two- string
  9698. 7:46:11method using Java the two- string method
  9699. 7:46:13is inherited from the object class
  9700. 7:46:15whenever you create an object behind the
  9701. 7:46:18scenes the object class is a super class
  9702. 7:46:21the two-ring method is used to return a
  9703. 7:46:24string representation of an object if
  9704. 7:46:26you were to Output it directly using
  9705. 7:46:28system.out print line the default
  9706. 7:46:30behavior of the two- string method is
  9707. 7:46:32that it returns a hash code as a unique
  9708. 7:46:34identifier for that object but it can be
  9709. 7:46:36overridden to provide meaningful details
  9710. 7:46:39which is why we've learned about method
  9711. 7:46:40overriding in the last video in this
  9712. 7:46:43demonstration let's create a new class
  9713. 7:46:45file new class we will create a car
  9714. 7:46:49class let's say that all cars should
  9715. 7:46:51have the following attributes a string
  9716. 7:46:54of make who manufactures the car a
  9717. 7:46:57string of model what's the model of the
  9718. 7:46:59car int year for the year and string
  9719. 7:47:03color for the color we'll need a
  9720. 7:47:06Constructor and then we'll assign these
  9721. 7:47:08attributes we have a string of make a
  9722. 7:47:13string of model an INT of year and a
  9723. 7:47:18string of color then we'll assign these
  9724. 7:47:22attributes this St make equals make this
  9725. 7:47:26St model equals model
  9726. 7:47:30this.e equals
  9727. 7:47:33year this. color equals
  9728. 7:47:37color that looks good enough for now
  9729. 7:47:40going back to our main Java file we'll
  9730. 7:47:42construct a car object car car equals
  9731. 7:47:45new car but we have to pass in the
  9732. 7:47:48details for our car Constructor our car
  9733. 7:47:51Constructor requires a make a model a
  9734. 7:47:53year and a color so pick a car that you
  9735. 7:47:56like I like Ford Mustang I'll pick a
  9736. 7:47:59Ford Mustang the make will be Ford Model
  9737. 7:48:03will be
  9738. 7:48:04Mustang I need a year 2025 and a color
  9739. 7:48:08I'll pick the color
  9740. 7:48:10red car is an object if I were to Output
  9741. 7:48:13my car object directly using
  9742. 7:48:15system.out.print line here's what you'll
  9743. 7:48:20see if you output your card directly
  9744. 7:48:23technically you'll be given a hash code
  9745. 7:48:25a unique identifier for that car object
  9746. 7:48:28hashing is kind of an advanced topic
  9747. 7:48:30that I don't want to introduce right now
  9748. 7:48:32but typically with hashing it's a unique
  9749. 7:48:34identifier that uses the object's memory
  9750. 7:48:36address to calculate a hash you don't
  9751. 7:48:38need to know any more than that right
  9752. 7:48:39now but wouldn't it be useful if we were
  9753. 7:48:42to print our car object directly we were
  9754. 7:48:44given the details of the car normally
  9755. 7:48:46what we would have to do is output
  9756. 7:48:49something like this this is if we wanted
  9757. 7:48:51to Output the details of the car we
  9758. 7:48:54could say something such as car. color
  9759. 7:48:58that would would be first plus a space
  9760. 7:49:00character plus car do which should be
  9761. 7:49:03next let's say the year color year then
  9762. 7:49:07make and
  9763. 7:49:09model car. make plus a
  9764. 7:49:13space plus car.
  9765. 7:49:16model this print line statement is
  9766. 7:49:19giving us meaningful details of this car
  9767. 7:49:22object this is the
  9768. 7:49:24output red 2025 Ford Mustang it would be
  9769. 7:49:28nice if we were to print our car object
  9770. 7:49:30directly using print or print line we
  9771. 7:49:33could be given these details instead
  9772. 7:49:35well we can do that with method
  9773. 7:49:36overwriting going to our car
  9774. 7:49:39class we will be overriding a method we
  9775. 7:49:42need the at override
  9776. 7:49:45annotation we do need this keyword of
  9777. 7:49:48public this is a publicly accessible
  9778. 7:49:51method the return type is string the
  9779. 7:49:54name of the method is to string
  9780. 7:50:00everything within this print line
  9781. 7:50:02statement we're going to cut and delete
  9782. 7:50:05this print line
  9783. 7:50:07statement going to our car class within
  9784. 7:50:09the two string method we have to return
  9785. 7:50:12a string because we set the return type
  9786. 7:50:14to be a string we promised that we would
  9787. 7:50:16return a string let's paste all the
  9788. 7:50:19details of that car we'll need to
  9789. 7:50:21replace car with
  9790. 7:50:23this this refers to the object we're
  9791. 7:50:26currently working with that and add a
  9792. 7:50:28semicolon to the end if we were to print
  9793. 7:50:32our car object directly using this print
  9794. 7:50:34line statement or a print statement or
  9795. 7:50:36even printf instead of that hash
  9796. 7:50:40code we're instead given meaningful
  9797. 7:50:42details of that car that we've
  9798. 7:50:44customized the cars color year make and
  9799. 7:50:49model we have overridden the two- string
  9800. 7:50:51method that's normally available to all
  9801. 7:50:55objects let's construct another car
  9802. 7:50:57object let's rename car is car one we'll
  9803. 7:51:00have car car 2 equals new
  9804. 7:51:04car pick some different details for a
  9805. 7:51:06car I will pick a
  9806. 7:51:10Chevrolet
  9807. 7:51:12Corvette the year will be
  9808. 7:51:142026 and the color will be blue I'll
  9809. 7:51:18print car
  9810. 7:51:19one then print Car
  9811. 7:51:242 here's car one and car two Car 2 is a
  9812. 7:51:29blue 2026 Chevrolet
  9813. 7:51:32Corvette all right everybody so that is
  9814. 7:51:34the two-ring method objects inherit from
  9815. 7:51:37the object class the object class does
  9816. 7:51:40have a two string method but it can be
  9817. 7:51:42overridden so that when you print an
  9818. 7:51:44object directly you can display
  9819. 7:51:46meaningful details such as the details
  9820. 7:51:48of that object instead of displaying a
  9821. 7:51:50hash code if you were to print the
  9822. 7:51:52object it's pretty useful at times and
  9823. 7:51:55well everybody that is the two string
  9824. 7:51:57method in Java
  9825. 7:51:59all right everybody it's about time we
  9826. 7:52:00get into abstraction overall abstraction
  9827. 7:52:03is the process of hiding implementation
  9828. 7:52:06details and showing only the essential
  9829. 7:52:08features think of it like this if I'm
  9830. 7:52:10teaching somebody how to drive I'm not
  9831. 7:52:12going to open up the hood of the car and
  9832. 7:52:14explain how the engine works I'm instead
  9833. 7:52:16going to tell the user that the gas
  9834. 7:52:18pedal is to drive and the brake is to
  9835. 7:52:20stop the car abstraction is the process
  9836. 7:52:22of hiding implementation details a user
  9837. 7:52:25just needs to know what the gas pedal is
  9838. 7:52:27and what the brake is we can make
  9839. 7:52:29classes and methods abstract by using
  9840. 7:52:31this abstract keyword this provides a
  9841. 7:52:33few benefits abstract classes can't be
  9842. 7:52:37instantiated directly abstract classes
  9843. 7:52:39can also contain abstract methods which
  9844. 7:52:41must be implemented they can also
  9845. 7:52:43contain concrete methods which are the
  9846. 7:52:46opposite these are real and physical
  9847. 7:52:47methods you could say concrete methods
  9848. 7:52:49are inherited so I know that's a lot let
  9849. 7:52:52me give you an example we're going to be
  9850. 7:52:54creating an abstract class that can't be
  9851. 7:52:56instantiated directly meaning we can't
  9852. 7:52:58create any objects from this class in
  9853. 7:53:01this demonstration we're going to create
  9854. 7:53:02some shape objects shape is going to be
  9855. 7:53:04the parent then we'll have circle
  9856. 7:53:07triangle and rectangle classes as its
  9857. 7:53:09children so we're going to go to new
  9858. 7:53:12Java class we will create a shape
  9859. 7:53:16class then a class for
  9860. 7:53:23Circle a class for triangle
  9861. 7:53:31then a class for
  9862. 7:53:37rectangle our shape class is going to be
  9863. 7:53:40abstract we can't create any shape
  9864. 7:53:42objects before the word of class we will
  9865. 7:53:44add this keyword of abstract we can't
  9866. 7:53:47create any shape objects it's an
  9867. 7:53:49abstract class now circle triangle and
  9868. 7:53:52rectangle are going to extend the shape
  9869. 7:53:54class Circle extends shape circles are
  9870. 7:53:58type of shape same thing applies with
  9871. 7:54:01triangle and
  9872. 7:54:03rectangle let's attempt to create a
  9873. 7:54:05shape object shape shape equals new
  9874. 7:54:11shape and we get an error message shape
  9875. 7:54:14is abstract it cannot be
  9876. 7:54:17instantiated since shape is an abstract
  9877. 7:54:19class we can't create any objects from
  9878. 7:54:22this class it adds a little bit of
  9879. 7:54:23security to our program we don't want
  9880. 7:54:26anybody creating any generic shape
  9881. 7:54:27objects
  9882. 7:54:29we want them to create a certain kind of
  9883. 7:54:30shape a circle a triangle or a rectangle
  9884. 7:54:34but we can create some circles triangles
  9885. 7:54:36and rectangles so let's do so circle
  9886. 7:54:39circle equals new
  9887. 7:54:43circle triangle
  9888. 7:54:47triangle equals new
  9889. 7:54:51triangle
  9890. 7:54:52rectangle
  9891. 7:54:54rectangle equals new rectangle
  9892. 7:55:01we can create circle triangle and
  9893. 7:55:03rectangle objects just not shape because
  9894. 7:55:06it's an abstract
  9895. 7:55:07class that's good though because we
  9896. 7:55:09don't want people creating any shape
  9897. 7:55:11objects it's too generic we want a user
  9898. 7:55:13to pick a certain kind of shape abstract
  9899. 7:55:16classes can contain abstract methods and
  9900. 7:55:19concrete methods an abstract method is a
  9901. 7:55:23method which must be implemented by its
  9902. 7:55:25children within our shape class
  9903. 7:55:28we will define an abstract method use
  9904. 7:55:31the abstract keyword let's list a return
  9905. 7:55:34type of double every child class of the
  9906. 7:55:38shape class needs an area method I'll
  9907. 7:55:41add a comment that this is an abstract
  9908. 7:55:44method
  9909. 7:55:46abstract So within our Circle class we
  9910. 7:55:49get an error
  9911. 7:55:50message Circle must either be declared
  9912. 7:55:53abstract or implement the abstract
  9913. 7:55:55method of area circle is going to
  9914. 7:55:58inherit this abstract method of area
  9915. 7:56:00from its parent of shape we need to
  9916. 7:56:02implement this method so let's do
  9917. 7:56:05so this is going to be an overridden
  9918. 7:56:08method we need The annotation of
  9919. 7:56:11override we need to define a method of
  9920. 7:56:16area but we'll fill it in momentarily
  9921. 7:56:19just for now I'm going to return zero
  9922. 7:56:22and now that error message goes away
  9923. 7:56:24since we inherit from the shape class
  9924. 7:56:26and there's an abstract method of area
  9925. 7:56:28we need to implement this method this
  9926. 7:56:30helps to ensure consistency among the
  9927. 7:56:32children classes you can see that we're
  9928. 7:56:34missing that same method with triangle
  9929. 7:56:36and rectangle so why don't we copy this
  9930. 7:56:38code where we override the area method
  9931. 7:56:41and paste
  9932. 7:56:42it we'll be filling these in
  9933. 7:56:46later going back to our shape class
  9934. 7:56:49abstract classes can contain abstract
  9935. 7:56:51methods and concrete methods which are
  9936. 7:56:54kind of the opposite concrete methods
  9937. 7:56:57are defined and inherited within an
  9938. 7:56:59abstract class let's define a method of
  9939. 7:57:04display this method doesn't return
  9940. 7:57:06anything the return type is void we'll
  9941. 7:57:08just output the
  9942. 7:57:09following this is a shape I'll add a
  9943. 7:57:13comment that this is a concrete method
  9944. 7:57:17in an abstract class a concrete method
  9945. 7:57:19is
  9946. 7:57:20inherited within our circle triangle and
  9947. 7:57:23rectangle classes we don't need to
  9948. 7:57:25implement it we don't need to override
  9949. 7:57:27this method it's
  9950. 7:57:29inherited going back to our main Java
  9951. 7:57:32file if I was to take my circle object
  9952. 7:57:35call the display method we get the
  9953. 7:57:38following
  9954. 7:57:40output this is a shape and that applies
  9955. 7:57:43with triangle and rectangle to triangle.
  9956. 7:57:48display rectangle.
  9957. 7:57:55display this is a shape this is a shape
  9958. 7:57:57this is a a
  9959. 7:58:00shape this concrete method of display
  9960. 7:58:02was inherited from the shape class their
  9961. 7:58:05parent now we'll Define the area methods
  9962. 7:58:08area is an abstract method that means
  9963. 7:58:10that the children classes have to Define
  9964. 7:58:12it to calculate the area of a circle
  9965. 7:58:15we'll need a radius we'll Define that as
  9966. 7:58:16an attribute all circles will have a
  9967. 7:58:19radius attribute Double Radius and we'll
  9968. 7:58:23need a Constructor a circle Constructor
  9969. 7:58:26when we construct a circle object we
  9970. 7:58:29have to pass in a double for the
  9971. 7:58:32radius this. rius equals the radius we
  9972. 7:58:37receive now the formula to calculate the
  9973. 7:58:40area of a circle will be we're going to
  9974. 7:58:42return access the math class access the
  9975. 7:58:46constant of Pi from the math class times
  9976. 7:58:51radius time
  9977. 7:58:55radius that's how to calculate the area
  9978. 7:58:57of a circle
  9979. 7:58:58now for our triangle we need the
  9980. 7:59:02following a double for base a double for
  9981. 7:59:07height we need a
  9982. 7:59:13Constructor parameter 1 will be double
  9983. 7:59:16base parameter 2 will be double
  9984. 7:59:19height this.
  9985. 7:59:22base equals
  9986. 7:59:24base this do height equals height
  9987. 7:59:29for the area we will
  9988. 7:59:32return
  9989. 7:59:350.5 time base time
  9990. 7:59:39height then we have a
  9991. 7:59:44rectangle double
  9992. 7:59:47length double
  9993. 7:59:49width we need our Constructor of
  9994. 7:59:53rectangle parameter 1 will be double
  9995. 7:59:56length parameter 2 will be double
  9996. 8:00:00width this do length equals
  9997. 8:00:04length this. width equals
  9998. 8:00:09width
  9999. 8:00:11return length time width within the area
  10000. 8:00:15method and we're good let's go back to
  10001. 8:00:18our main class we're missing those
  10002. 8:00:20arguments we have to pass them in for
  10003. 8:00:23our Circle object let's say the radius
  10004. 8:00:25is three for the triangle the base and
  10005. 8:00:26height will be 4 and 5 the rectangle the
  10006. 8:00:30length and width will be 6 and
  10007. 8:00:327 we are going to Output using
  10008. 8:00:35system.out.print line take our circle
  10009. 8:00:37called the area
  10010. 8:00:39method then do this with triangle and
  10011. 8:00:47rectangle here's the area of each of our
  10012. 8:00:49shapes our Circle our triangle and a
  10013. 8:00:53rectangle so that's the abstract keyword
  10014. 8:00:55abstraction is the process of hiding
  10015. 8:00:57implementation details from a user and
  10016. 8:01:00showing only the essential features
  10017. 8:01:02that's why we've created a shape class
  10018. 8:01:03that's abstract we can't directly create
  10019. 8:01:06any shape objects we want a user to
  10020. 8:01:08create a certain kind of shape whether
  10021. 8:01:10it's a circle a triangle or a rectangle
  10022. 8:01:12a shape is too generic an abstract class
  10023. 8:01:15can contain abstract methods which need
  10024. 8:01:17to be implemented by the children
  10025. 8:01:19classes or concrete methods which are
  10026. 8:01:21inherited and the children classes will
  10027. 8:01:24have access to it all right everybody so
  10028. 8:01:26that is abstraction and the of the
  10029. 8:01:28abstract keyword in Java all right
  10030. 8:01:31what's going on everybody in this video
  10031. 8:01:32I got to explain interfaces using Java
  10032. 8:01:35an interface is very similar to an
  10033. 8:01:37abstract class in fact there's a lot of
  10034. 8:01:39overlap where you could use either one
  10035. 8:01:41but there's a few key differences an
  10036. 8:01:42interface is a blueprint for a class it
  10037. 8:01:45specifies a set of abstract methods that
  10038. 8:01:47any implementing classes must Define one
  10039. 8:01:50key difference is that by using
  10040. 8:01:52interfaces we can achieve multiple
  10041. 8:01:54inheritance like Behavior normally with
  10042. 8:01:56inheritance a class can only have a
  10043. 8:01:59single parent but by using interfaces a
  10044. 8:02:01class can have multiple parents like two
  10045. 8:02:04three or more so in this demonstration
  10046. 8:02:06we're going to create two interfaces
  10047. 8:02:09let's go to file new Java class rather
  10048. 8:02:13than creating a class we're going to
  10049. 8:02:14create an interface we will create an
  10050. 8:02:17interface of prey and the other for
  10051. 8:02:20predator file new Java class
  10052. 8:02:24interface
  10053. 8:02:26Predator we're going to be declaring
  10054. 8:02:28some methods but not defining them if a
  10055. 8:02:31class inherits from the prey interface
  10056. 8:02:34they will have to finish defining a
  10057. 8:02:36method of flea the return type will be
  10058. 8:02:39void if you're considered prey you need
  10059. 8:02:41a flea method to run away if you're a
  10060. 8:02:45predator you need to define a method to
  10061. 8:02:48hunt cuz you're a predator now we'll be
  10062. 8:02:50creating three classes file new Java
  10063. 8:02:54class the first class will be rabbit
  10064. 8:03:00then fish we'll create a fish
  10065. 8:03:02class and
  10066. 8:03:08Hawk going to our rabbit class rabbits
  10067. 8:03:11are typically considered prey not
  10068. 8:03:14Predators unless you count that one
  10069. 8:03:15rabbit from Monty Python and the Holy
  10070. 8:03:17Grail that's the exception our rabbit
  10071. 8:03:19class is going to
  10072. 8:03:21implements the prey interface we get a
  10073. 8:03:24warning message class rabbit must either
  10074. 8:03:27declare abstract or Implement abstract
  10075. 8:03:30method flee since the rabbit class is
  10076. 8:03:33implementing the prey interface we have
  10077. 8:03:35to finish defining this method that's
  10078. 8:03:38declared within the prey interface it's
  10079. 8:03:40kind of like a contract the prey
  10080. 8:03:42interface is telling the rabbit class
  10081. 8:03:44hey if you're going to implement me you
  10082. 8:03:45need to Define this method a flea we'll
  10083. 8:03:48need to do that in order to create rabid
  10084. 8:03:49objects we're going to be overwriting
  10085. 8:03:52this method using the override
  10086. 8:03:54annotation this method will be flee void
  10087. 8:03:58flee and we'll want to add this axis
  10088. 8:04:01modifier of public it's a publicly
  10089. 8:04:03accessible method in this method we're
  10090. 8:04:06going to Output the following let's say
  10091. 8:04:10the rabbit is running
  10092. 8:04:14away and that error should go away let's
  10093. 8:04:17go back to our main class we'll create a
  10094. 8:04:19rabbit object and test it rabbit rabbit
  10095. 8:04:23equals new rabbit let's take our rabbit
  10096. 8:04:26object have it call the flea
  10097. 8:04:29method and this should
  10098. 8:04:32work yes the rabbit is running away now
  10099. 8:04:36let's go to our Hawk class Hawk will
  10100. 8:04:40implement the Predator
  10101. 8:04:43interface but we have to override this
  10102. 8:04:45method of hunt because with The Predator
  10103. 8:04:48interface it's kind of like we're
  10104. 8:04:50signing a contract if Hawk is going to
  10105. 8:04:52implement the Predator interface then we
  10106. 8:04:55have to finish defining this method we
  10107. 8:04:57will add the at override
  10108. 8:05:00annotation and Define this method and
  10109. 8:05:03then again we do need an axis modifier
  10110. 8:05:06we'll pick
  10111. 8:05:07public let's output the
  10112. 8:05:10following the hawk is
  10113. 8:05:13hunting let's create a hawk
  10114. 8:05:15object Hawk Hawk equals New
  10115. 8:05:21Hawk our rabbit is fleeing let's take
  10116. 8:05:23our Hawk object use the hunt method
  10117. 8:05:29okay we have the rabbit is running away
  10118. 8:05:31the hawk is
  10119. 8:05:33hunting now rabbits don't have a hunt
  10120. 8:05:35method and Hawks don't have a flea
  10121. 8:05:41method now one key difference with
  10122. 8:05:43interfaces compared to abstract classes
  10123. 8:05:46is that you can Implement more than one
  10124. 8:05:48interface so fish they eat smaller fish
  10125. 8:05:51and they flee larger fish they could be
  10126. 8:05:53considered both prey and
  10127. 8:05:55predators we will implement two
  10128. 8:05:58interfaces Implement prey comma Predator
  10129. 8:06:03then we need both of these methods we
  10130. 8:06:04have to finish defining both again we're
  10131. 8:06:07going to use the at override
  10132. 8:06:10annotation we will create a publicly
  10133. 8:06:12accessible method a flea where we will
  10134. 8:06:16print the fish is
  10135. 8:06:19swimming away and we need a hunt method
  10136. 8:06:26too add the at override annotation this
  10137. 8:06:30is a public method of hunt where we will
  10138. 8:06:35print the fish is hunting let's say all
  10139. 8:06:41right let's test it we will create a
  10140. 8:06:43fish object fish fish equals new
  10141. 8:06:48fish our fish can
  10142. 8:06:51flee and our fish can
  10143. 8:06:56hunt the fish is swimming away the fish
  10144. 8:06:59is hunting our fish object implements
  10145. 8:07:02both the prey and Predator interfaces it
  10146. 8:07:04has to Define these two methods so
  10147. 8:07:07that's one key difference with
  10148. 8:07:08interfaces compared to abstract classes
  10149. 8:07:10a class can inherit more than one
  10150. 8:07:12interface normally with inheritance you
  10151. 8:07:15can only have one parent but this is a
  10152. 8:07:17way around that using interfaces fish
  10153. 8:07:19are considered both prey and predators
  10154. 8:07:21all right everybody so that is a quick
  10155. 8:07:23introduction to interfaces an interface
  10156. 8:07:25is a blueprint for a class that's
  10157. 8:07:27specifies a set of abstract methods that
  10158. 8:07:29implementing classes must Define and it
  10159. 8:07:32supports multiple inheritance like
  10160. 8:07:34Behavior there's a lot of overlap where
  10161. 8:07:36you can use abstract classes or
  10162. 8:07:37interfaces you should be familiar with
  10163. 8:07:39both and well everybody that's a quick
  10164. 8:07:41introduction to interfaces using Java so
  10165. 8:07:45yeah it's about time I explain
  10166. 8:07:46polymorphism using Java polymorphism is
  10167. 8:07:49a Greek word poly means many morph means
  10168. 8:07:52shape together you get the concept of
  10169. 8:07:55many shapes or forms it's a general
  10170. 8:07:58programming concept objects can identify
  10171. 8:08:00as other objects objects of different
  10172. 8:08:03types objects can be treated as objects
  10173. 8:08:06of a common super Class A dog identifies
  10174. 8:08:09as a dog but it can also identify as an
  10175. 8:08:11animal it can also identify as an
  10176. 8:08:13organism it can also identify as an
  10177. 8:08:15object it can identify as more than one
  10178. 8:08:18thing in this topic we're going to
  10179. 8:08:19create a few vehicle objects let's
  10180. 8:08:21create a few classes file new Java class
  10181. 8:08:25we will create a super class of vehicle
  10182. 8:08:28iicle this class doesn't have to be
  10183. 8:08:30abstract but let's make it abstract we
  10184. 8:08:32can't create any vehicle
  10185. 8:08:34objects we will declare an abstract
  10186. 8:08:37method that doesn't return anything the
  10187. 8:08:40return type is void if you're a vehicle
  10188. 8:08:42you need a go method so what we're going
  10189. 8:08:45to be doing is creating a few different
  10190. 8:08:47types of vehicles a car bike and boat
  10191. 8:08:50class which will inherit from the class
  10192. 8:08:52of vehicle so let's create these classes
  10193. 8:08:54file new Java class we will create a car
  10194. 8:08:57class that extends the vehicle class but
  10195. 8:09:02there is an abstract method of go that
  10196. 8:09:04we need to
  10197. 8:09:06Define we will use the at override
  10198. 8:09:10annotation the method will be return
  10199. 8:09:13type of void go all we'll do is output
  10200. 8:09:17you drive the car all right let's do
  10201. 8:09:21this with a few other classes I'll just
  10202. 8:09:24copy this method because we'll reuse it
  10203. 8:09:28we'll create a bike class
  10204. 8:09:31bike bike extends
  10205. 8:09:35vehicle and we need that method of go
  10206. 8:09:39you ride the
  10207. 8:09:42bike and a boat class file new Java
  10208. 8:09:45class
  10209. 8:09:48Boat Boat extends
  10210. 8:09:54vehicle you sail the boat
  10211. 8:09:59that is good enough for now going back
  10212. 8:10:01to our main Java file let's create these
  10213. 8:10:04objects starting with our car car car
  10214. 8:10:07equals new
  10215. 8:10:09car bike bike equals new
  10216. 8:10:13bike Boat Boat equals new
  10217. 8:10:18boat each of these objects has a go
  10218. 8:10:21method for example let's have our car
  10219. 8:10:23object use its go method followed by
  10220. 8:10:26bike bike Dogo method and Boat Boat Dogo
  10221. 8:10:33method you drive the car you ride the
  10222. 8:10:35bike you sail the boat so let's say we
  10223. 8:10:38would like to have a
  10224. 8:10:40race we're going to place all of these
  10225. 8:10:42objects within an array our car bike and
  10226. 8:10:45boat objects so what should the data
  10227. 8:10:48type of the array be let's attempt to
  10228. 8:10:51create an array of car objects and place
  10229. 8:10:53our bike and boat objects within that
  10230. 8:10:55array and we'll just see what happens
  10231. 8:10:57exactly for learning purposes I will
  10232. 8:10:59create an array of
  10233. 8:11:01cars the data type is car it's an array
  10234. 8:11:05named cars equals curly braces let's
  10235. 8:11:09stick our car bike and boat objects
  10236. 8:11:12within here we're already getting a few
  10237. 8:11:14error
  10238. 8:11:17messages incompatible types bike cannot
  10239. 8:11:20be converted to car bikes and boats
  10240. 8:11:24don't identify as cars cars identify as
  10241. 8:11:27cars s that's one of the shapes that
  10242. 8:11:29bikes and boats don't have they don't
  10243. 8:11:31identify as cars if this were an array
  10244. 8:11:33of
  10245. 8:11:34bikes bike bikes equals R array of
  10246. 8:11:41objects well then we have another a
  10247. 8:11:43message car cannot be converted to a
  10248. 8:11:46bike and you can assume the same thing
  10249. 8:11:48applies with
  10250. 8:11:50boats cars and bikes don't identify as
  10251. 8:11:53boats what we could do is declare our
  10252. 8:11:56array of what they have in common well
  10253. 8:11:58our car bike and boat they all extend
  10254. 8:12:01the vehicle class you could say they
  10255. 8:12:03also identify as Vehicles we're going to
  10256. 8:12:06change the data type of our array to
  10257. 8:12:07hold Vehicles we have an array of
  10258. 8:12:11vehicle
  10259. 8:12:12objects and then let's change our array
  10260. 8:12:14name to be Vehicles cars identifies cars
  10261. 8:12:18but also Vehicles bikes identifies bikes
  10262. 8:12:21but also Vehicles boats identify as
  10263. 8:12:24boats but also Vehicles that's the one
  10264. 8:12:26thing they have in common they're all
  10265. 8:12:29extending the vehicle super class
  10266. 8:12:31they're also considered Vehicles that's
  10267. 8:12:34another one of their forms they shapes
  10268. 8:12:36for every object within this array I
  10269. 8:12:38would like to have each object use its
  10270. 8:12:40go method like we're
  10271. 8:12:42racing well what we could do is use a
  10272. 8:12:45for each
  10273. 8:12:48Loop what's the data type of the array
  10274. 8:12:51well it's vehicle we have an array of
  10275. 8:12:54vehicles we will give a nickname of each
  10276. 8:12:56vehicle
  10277. 8:12:58as vehicle in my array of
  10278. 8:13:01vehicles for every vehicle in this array
  10279. 8:13:05of vehicles let's take each
  10280. 8:13:07vehicle call the go method take every
  10281. 8:13:10vehicle object call its go method here's
  10282. 8:13:13the result you drive the car you ride
  10283. 8:13:16the bike you sail the
  10284. 8:13:19boat oh and another thing I'm forgetting
  10285. 8:13:22polymorphism can also be achieved
  10286. 8:13:24through using interfaces let's delete
  10287. 8:13:26our vehicle
  10288. 8:13:30class we'll create a new Java class this
  10289. 8:13:34will be an interface a
  10290. 8:13:38vehicle rather than extending the
  10291. 8:13:41vehicle class we will implements the
  10292. 8:13:44vehicle
  10293. 8:13:48interface we will declare a method of go
  10294. 8:13:52that doesn't return
  10295. 8:13:53anything these methods do need an access
  10296. 8:13:56modifier let's just say
  10297. 8:14:04public so polymorphism can be achieved
  10298. 8:14:07this way too through using interfaces if
  10299. 8:14:10you prefer interfaces in summary
  10300. 8:14:12polymorphism means many shapes it's
  10301. 8:14:15where objects can identify as themselves
  10302. 8:14:17but also other objects objects can be
  10303. 8:14:20treated as objects of a common super
  10304. 8:14:22class and well everybody that is an
  10305. 8:14:24introduction to polymorphism using jav
  10306. 8:14:27Ava what's going on people today I got
  10307. 8:14:29to explain a runtime polymorphism also
  10308. 8:14:32known as Dynamic polymorphism how I
  10309. 8:14:34would explain it in a single sentence is
  10310. 8:14:37when the method that gets executed is
  10311. 8:14:39decided at runtime based on the actual
  10312. 8:14:42type of the object in this demonstration
  10313. 8:14:45a user is going to pick a type of animal
  10314. 8:14:47they want as a pet a dog or a cat but
  10315. 8:14:50that's going to be determined at runtime
  10316. 8:14:52after the program is already running
  10317. 8:14:54we'll create a total of three classes
  10318. 8:14:56first we'll create an animal class which
  10319. 8:14:59will be a parent class we will create a
  10320. 8:15:01class of animal we might as well make
  10321. 8:15:04this an abstract class for good practice
  10322. 8:15:07we don't want anybody creating any
  10323. 8:15:08animal objects we will have an abstract
  10324. 8:15:11method the return type is void this
  10325. 8:15:14method will be speak when an animal
  10326. 8:15:17speaks they'll say something
  10327. 8:15:19unique we'll create a dog class file new
  10328. 8:15:23Java class we will create a class of dog
  10329. 8:15:27that
  10330. 8:15:28extends the animal class but we do need
  10331. 8:15:31a speak method we will use method
  10332. 8:15:34overwriting the return type is void with
  10333. 8:15:37our speak
  10334. 8:15:38method we will output the
  10335. 8:15:41following the dog goes woof all right
  10336. 8:15:47then we need a cat class file new Java
  10337. 8:15:50class we will create a class of
  10338. 8:15:54cat cat extends
  10339. 8:15:58animal and we'll just copy the speak
  10340. 8:16:00method just to save a little bit of
  10341. 8:16:03time when the cat speaks we'll output
  10342. 8:16:07the cat goes meow that's good enough for
  10343. 8:16:10this
  10344. 8:16:12example since animal is an abstract
  10345. 8:16:15class we can't create any animal objects
  10346. 8:16:17I'll attempt to do so animal animal
  10347. 8:16:20equals new
  10348. 8:16:23animal and we get an error message
  10349. 8:16:25animal is abstract can be instantiated
  10350. 8:16:28that's good though animals are too
  10351. 8:16:30generic we would rather have a user
  10352. 8:16:32create a certain kind of animal either a
  10353. 8:16:34dog or a cat in this demonstration we're
  10354. 8:16:37going to have a user pick if they want a
  10355. 8:16:39dog or a cat but we don't know which one
  10356. 8:16:41they're going to pick so why don't we do
  10357. 8:16:43this let's declare an animal object but
  10358. 8:16:47not instantiate it we will assign our
  10359. 8:16:49animal equal to a new dog or a new cat
  10360. 8:16:52based on the user input so animal animal
  10361. 8:16:56this will either be animal equals a new
  10362. 8:16:59dog or animal equals a new cat based on
  10363. 8:17:04the user
  10364. 8:17:06input we will declare an object that's
  10365. 8:17:08at least an animal we'll accept some
  10366. 8:17:10user input we'll need a scanner scanner
  10367. 8:17:13scanner equals new
  10368. 8:17:16scanner we will pass in
  10369. 8:17:19system.in and import this class
  10370. 8:17:22java.util.scanner
  10371. 8:17:25we'll need a prompt
  10372. 8:17:31would you like a dog or a
  10373. 8:17:36cat we'll say 1 equals dog 2 equals
  10374. 8:17:43cat I'll use print rather than print
  10375. 8:17:46line because I like the input on the
  10376. 8:17:47same line let's declare a variable of
  10377. 8:17:50choice this will be an integer we'll use
  10378. 8:17:52our scanner called the next int method
  10379. 8:17:58and then we'll do the
  10380. 8:17:59following we'll examine if our choice
  10381. 8:18:03variable is equal to one if it is we'll
  10382. 8:18:06finish instantiating our animal object
  10383. 8:18:09but we'll assign it to be a new dog
  10384. 8:18:12object then we will have our animal use
  10385. 8:18:15its speak
  10386. 8:18:17method so if it's a dog it should go
  10387. 8:18:23woof else if
  10388. 8:18:29choice is equal to two then we will
  10389. 8:18:32instantiate our animal object as a new C
  10390. 8:18:36and have our animal use its speak
  10391. 8:18:42method you could add an else Clause but
  10392. 8:18:45it's not really
  10393. 8:18:46necessary this is good enough for
  10394. 8:18:49now before running the program we don't
  10395. 8:18:52know what kind of animal we're going to
  10396. 8:18:53create it's either going to be a dog or
  10397. 8:18:55a cat
  10398. 8:19:00if I select one we get a dog the dog
  10399. 8:19:02goes
  10400. 8:19:03woof if I select two we get a cat the
  10401. 8:19:06cat goes meow the actual method that we
  10402. 8:19:09call is determined at runtime after the
  10403. 8:19:12program is already
  10404. 8:19:13running before we run the program we
  10405. 8:19:16know that we're going to use a speak
  10406. 8:19:17method we're just not sure which one so
  10407. 8:19:20that's runtime polymorphism it's when
  10408. 8:19:22the method that gets executed is decided
  10409. 8:19:25at runtime based on the actual type of
  10410. 8:19:28object that we create and well everybody
  10411. 8:19:30that is an introduction to runtime
  10412. 8:19:32polymorphism using
  10413. 8:19:34Java all right everybody today I'm going
  10414. 8:19:36to be explaining getter and Setter
  10415. 8:19:38methods using Java getter and Setter
  10416. 8:19:40methods help protect object data and
  10417. 8:19:42they add rules for accessing or
  10418. 8:19:44modifying that data here's a
  10419. 8:19:46demonstration we're going to create a
  10420. 8:19:48class of car file new class we will
  10421. 8:19:52create a car
  10422. 8:19:53Class A few attributes that cars could
  10423. 8:19:55have include a string of model A String
  10424. 8:20:00of color and an INT let's say price then
  10425. 8:20:05we'll need a
  10426. 8:20:08Constructor we'll have three
  10427. 8:20:10parameters a string of model A String of
  10428. 8:20:14color and an inch of price we will
  10429. 8:20:18assign this do
  10430. 8:20:20model equals
  10431. 8:20:22model this do color equals color
  10432. 8:20:27this St price equals price let's
  10433. 8:20:31construct a car
  10434. 8:20:34object car car equals new
  10435. 8:20:38car pick a model a year and a price I've
  10436. 8:20:42already used red Mustangs for too many
  10437. 8:20:44examples I'll pick something else let's
  10438. 8:20:46say a
  10439. 8:20:48charger that is yellow and the price
  10440. 8:20:51will be
  10441. 8:20:52$10,000 but I don't know if that's good
  10442. 8:20:54or not we don't even know the year of
  10443. 8:20:56the car anyways so we have our car
  10444. 8:20:58object and then let's output these
  10445. 8:21:00attributes just to test them car. color
  10446. 8:21:03I'll add a space character to plus car.
  10447. 8:21:07model plus a space
  10448. 8:21:10character plus car. price I'm
  10449. 8:21:13intentionally not adding a unit of
  10450. 8:21:16currency so here's our
  10451. 8:21:19car I have a yellow charger and the
  10452. 8:21:22price is 10,000 10,000 of a unit of
  10453. 8:21:24currency that you choose but that's not
  10454. 8:21:26important
  10455. 8:21:27with our car object its attributes are
  10456. 8:21:30publicly accessible meaning we can view
  10457. 8:21:32and change them
  10458. 8:21:33easily to demonstrate let's say that the
  10459. 8:21:36model of our car is now a
  10460. 8:21:43Corvette we have a yellow Corvette that
  10461. 8:21:46is
  10462. 8:21:46$10,000 we don't want the model of our
  10463. 8:21:49car to magically become a Corvette once
  10464. 8:21:51we assign its model to be charger we
  10465. 8:21:53don't want it to change what we could do
  10466. 8:21:56when to declaring these attributes is
  10467. 8:21:58add this access modifier of private
  10468. 8:22:00preceding the data type private string
  10469. 8:22:03model private string color private and
  10470. 8:22:07price we can't access them and let's
  10471. 8:22:10test that you can see that the font
  10472. 8:22:12color already changed to
  10473. 8:22:15Red I will attempt to print the car's
  10474. 8:22:17color model and
  10475. 8:22:21price color has private access and car
  10476. 8:22:25model has private ACC access in car and
  10477. 8:22:28price has private access in
  10478. 8:22:30car since these attributes are private
  10479. 8:22:33we can't normally access them outside of
  10480. 8:22:35the car class but there is a way around
  10481. 8:22:37that and that is by using getter and
  10482. 8:22:39Setter methods getter methods make a
  10483. 8:22:42field readable Setter methods make a
  10484. 8:22:45field writable use get to read set to
  10485. 8:22:49write we'll set up some getter methods
  10486. 8:22:52first I would like the car's color model
  10487. 8:22:55and price
  10488. 8:22:57here's how we can do that within the car
  10489. 8:22:59class we will create a method we'll
  10490. 8:23:02start with the model the return type
  10491. 8:23:04will be string because our model is a
  10492. 8:23:06string data type we'll create a getter
  10493. 8:23:08method following this naming convention
  10494. 8:23:11get then the name of the attribute
  10495. 8:23:14model all we're going to do is return
  10496. 8:23:18this.
  10497. 8:23:20model and then we'll do this with the
  10498. 8:23:22color and the price and I'll just copy
  10499. 8:23:25this method because I'm I'm feeling a
  10500. 8:23:26little lazy we have get
  10501. 8:23:29model get color return this.
  10502. 8:23:33color and get
  10503. 8:23:37price return this.
  10504. 8:23:39price oh and this returns an integer not
  10505. 8:23:42a string because our price is an integer
  10506. 8:23:46let's go back to our main Java file
  10507. 8:23:48rather than accessing these attributes
  10508. 8:23:51directly because we can't we're going to
  10509. 8:23:54call these getter methods get model if
  10510. 8:23:56you need the model get color if you need
  10511. 8:23:58the color get price if you need the
  10512. 8:24:01price replace car. color with car do get
  10513. 8:24:05color and this is a method car. getet
  10514. 8:24:11model and car. getet price and that
  10515. 8:24:16should
  10516. 8:24:18work yep we can now read those
  10517. 8:24:21attributes we have a yellow charger and
  10518. 8:24:23the price is
  10519. 8:24:2510,000 another thing you can do too with
  10520. 8:24:27getter methods you can add additional
  10521. 8:24:29logic so with our get price method let's
  10522. 8:24:32instead return a string we will return a
  10523. 8:24:36unit of currency I'll pick American
  10524. 8:24:38dollars plus the price let's see how
  10525. 8:24:41that
  10526. 8:24:42changes again we are calling the get
  10527. 8:24:44price
  10528. 8:24:47method there we go yellow charger
  10529. 8:24:52$110,000 getter methods make a field
  10530. 8:24:55readable and you can add additional
  10531. 8:24:57logic when retrieving one of these
  10532. 8:24:59attributes and in this demonstration I
  10533. 8:25:01just added a dollar sign just to keep it
  10534. 8:25:03simple then we have Setter methods
  10535. 8:25:05Setter methods make a field writable
  10536. 8:25:09going to our car class we have model
  10537. 8:25:11color and price I don't want the model
  10538. 8:25:14attribute to be writable once we've
  10539. 8:25:16declared the model of our car we don't
  10540. 8:25:18want to change it our charger can't
  10541. 8:25:20magically become a Corvette even though
  10542. 8:25:22that'd be pretty cool but the color you
  10543. 8:25:24could change you can paint your car and
  10544. 8:25:26the price you can sell your car for a
  10545. 8:25:28different price we'll declare Setter
  10546. 8:25:30methods for our color and price but not
  10547. 8:25:32the model because we don't want this
  10548. 8:25:34attribute to be
  10549. 8:25:35writable so let's scroll down to the
  10550. 8:25:38bottom following a similar pattern we're
  10551. 8:25:40going to create a method the return type
  10552. 8:25:43is void this will be set
  10553. 8:25:47color we have one parameter a string of
  10554. 8:25:52color we will assign this do color equal
  10555. 8:25:56to the new color that we
  10556. 8:25:59receive we'll do this with our price
  10557. 8:26:02too void set
  10558. 8:26:06price we'll have one parameter and int
  10559. 8:26:10of
  10560. 8:26:11price this. price equals price the new
  10561. 8:26:15price that we receive okay let's test
  10562. 8:26:18these
  10563. 8:26:22things I'll attempt to access our color
  10564. 8:26:25directly car. color equals I don't know
  10565. 8:26:29blue that's the first thing that came to
  10566. 8:26:31mind and car. price equals you know what
  10567. 8:26:36the car's on sale it's
  10568. 8:26:38$5,000 well I can't change these we
  10569. 8:26:40still have that red text for color and
  10570. 8:26:44price because the color and price
  10571. 8:26:46attributes are still
  10572. 8:26:48private instead we're going to call
  10573. 8:26:50these Setter
  10574. 8:26:52methods we will set the color and then
  10575. 8:26:55pass in the new
  10576. 8:26:57color rather than assign it directly set
  10577. 8:27:00color pass in a new
  10578. 8:27:03color set price passing a new price
  10579. 8:27:075,000 then using our Getters we should
  10580. 8:27:09be able to print these
  10581. 8:27:12attributes yes we now have a blue
  10582. 8:27:14charger and the price is
  10583. 8:27:17$5,000 we don't have a Setter method set
  10584. 8:27:19up for the model that's good though we
  10585. 8:27:22don't want the model to
  10586. 8:27:24change we only have set
  10587. 8:27:26and set price if I attempt to access a
  10588. 8:27:29set model
  10589. 8:27:31method we shouldn't be able to change
  10590. 8:27:36it because we never declared a set model
  10591. 8:27:38method it doesn't exist but if you did
  10592. 8:27:41want to change the model if you want
  10593. 8:27:43this attribute to be writable well then
  10594. 8:27:46you could add a Setter
  10595. 8:27:47method void set
  10596. 8:27:50model we'll pass in a string for the new
  10597. 8:27:54model this model equals
  10598. 8:27:59model and then you could change
  10599. 8:28:02it we have a blue corvet for $5,000
  10600. 8:28:05which is really
  10601. 8:28:07cheap but you know what I don't want the
  10602. 8:28:09model to be writable just
  10603. 8:28:12readable if you don't want an attribute
  10604. 8:28:14to be writable when you declare it you
  10605. 8:28:16can also add this keyword a final that
  10606. 8:28:18adds an extra security
  10607. 8:28:22measure one last thing we should do with
  10608. 8:28:25the price let's add some additional
  10609. 8:28:27logic so with our price let's do the
  10610. 8:28:29following let's add a
  10611. 8:28:31check if our price is less than zero
  10612. 8:28:36then let's output the
  10613. 8:28:38following price can't be less than
  10614. 8:28:44zero else if everything checks out if
  10615. 8:28:47our price is zero or above then we'll
  10616. 8:28:49assign this do price equals the price
  10617. 8:28:52that we receive as an argument Let's
  10618. 8:28:55test it
  10619. 8:28:56I'll change the color of my car to be
  10620. 8:28:58blue but the price is going to
  10621. 8:29:04be100 we get that message price can't be
  10622. 8:29:07less than zero the color changed but not
  10623. 8:29:10the price it stayed the same at
  10624. 8:29:13$10,000 so those are getter and Setter
  10625. 8:29:15methods they help protect object data
  10626. 8:29:18and add rules for accessing or modifying
  10627. 8:29:20that data getter methods make a field
  10628. 8:29:23readable Setter methods make a a field
  10629. 8:29:26writable depending on your object's
  10630. 8:29:28attributes you may want some to be
  10631. 8:29:30readable or writable or both it's up to
  10632. 8:29:34you and well everybody those are getter
  10633. 8:29:36and Setter methods using Java all right
  10634. 8:29:39let's do this today I got to talk about
  10635. 8:29:41aggregation in programming aggregation
  10636. 8:29:44represents a has a relationship between
  10637. 8:29:47objects one object contains another
  10638. 8:29:49object as part of its structure but the
  10639. 8:29:52contained object or objects can exist
  10640. 8:29:54independently B basically speaking an
  10641. 8:29:57object can contain another object but
  10642. 8:30:00those objects can exist independently
  10643. 8:30:02what we'll do for this demonstration is
  10644. 8:30:04create some book objects then we'll
  10645. 8:30:05create a library object to contain the
  10646. 8:30:08book objects the books and the library
  10647. 8:30:10can exist independently that's
  10648. 8:30:12aggregation the library object will have
  10649. 8:30:15book objects we'll create a new class of
  10650. 8:30:18book file new class
  10651. 8:30:22book and a class for Library file new
  10652. 8:30:26class
  10653. 8:30:29Library let's design our book
  10654. 8:30:31objects let's say that books have a
  10655. 8:30:34string of title and an integer for Pages
  10656. 8:30:38you could include an author too if you
  10657. 8:30:39would like but I don't want this example
  10658. 8:30:41to be too big we'll need a Constructor
  10659. 8:30:43for our book
  10660. 8:30:44objects we'll set up the parameters we
  10661. 8:30:47need a string of title and an integer
  10662. 8:30:50for
  10663. 8:30:51Pages this. tile equals title
  10664. 8:30:57this. Pages equals Pages if you would
  10665. 8:31:00like you can set these attributes to be
  10666. 8:31:02private but that might be overkill for
  10667. 8:31:04this lesson why don't we create a method
  10668. 8:31:06to display the book's info this method
  10669. 8:31:08will return a string we'll Define a
  10670. 8:31:10method to display info we will display
  10671. 8:31:14the info of this book Let's return the
  10672. 8:31:17following let's return this.
  10673. 8:31:21tile plus a space character
  10674. 8:31:26plus this.
  10675. 8:31:29pages and a string of the word
  10676. 8:31:35pages and I think I'm going to enclose
  10677. 8:31:38the number of pages within a set of
  10678. 8:31:40parenthesis you can also use a print F
  10679. 8:31:43statement if that's easier for you but I
  10680. 8:31:45want to try and keep this beginner
  10681. 8:31:46friendly okay that's good enough for the
  10682. 8:31:48book class let's create a few book
  10683. 8:31:51objects we'll start with book one book
  10684. 8:31:54book one equals a new book but to
  10685. 8:31:58construct a book we have to pass in at
  10686. 8:32:00least the following a title and a number
  10687. 8:32:03of pages so I've picked out a few books
  10688. 8:32:06already feel free to choose your own if
  10689. 8:32:08you would rather choose different books
  10690. 8:32:09for the first book that I'll pick I will
  10691. 8:32:11pick the Fellowship of the Ring this
  10692. 8:32:14will be all one
  10693. 8:32:16string and the number of pages at least
  10694. 8:32:19according to wekipedia is
  10695. 8:32:21423 then we'll create another book
  10696. 8:32:24another book object this will be book
  10697. 8:32:27two book two will be the two towers the
  10698. 8:32:32number of pages will be
  10699. 8:32:35352 and then book
  10700. 8:32:38three book three will be the Return of
  10701. 8:32:43the
  10702. 8:32:45King the number of pages will be
  10703. 8:32:48416 all right we have our independent
  10704. 8:32:50book objects then what we'll do is stick
  10705. 8:32:53them all within an array we'll create an
  10706. 8:32:55array of book objects named books equals
  10707. 8:33:00then within a set of curly braces we can
  10708. 8:33:02place our book objects within here book
  10709. 8:33:04one book two book three we have an array
  10710. 8:33:08of book objects before moving on let's
  10711. 8:33:11test the display info method of each of
  10712. 8:33:13these books so we'll need a print
  10713. 8:33:16statement book one. display
  10714. 8:33:21info so we should get book one The
  10715. 8:33:24Fellowship of the Ring 423 Pages I'm
  10716. 8:33:27just going to make one
  10717. 8:33:30change I'll put the right parentheses at
  10718. 8:33:32the
  10719. 8:33:36end that's
  10720. 8:33:38better let's display book
  10721. 8:33:41two the two towers 352
  10722. 8:33:44Pages book
  10723. 8:33:47three the Return of the King 46
  10724. 8:33:52Pages you could also Loop through this
  10725. 8:33:54array using an enhanced for for
  10726. 8:33:57Loop the data type of each element in my
  10727. 8:34:00array is book for every book in my array
  10728. 8:34:03of
  10729. 8:34:04books will print take each book call the
  10730. 8:34:09display info
  10731. 8:34:13method and here's all the books that I
  10732. 8:34:15have in my array The Fellowship of the
  10733. 8:34:18Ring The Two Towers The Return of the
  10734. 8:34:20King and we can delete this for Loop we
  10735. 8:34:23no longer need it
  10736. 8:34:26now we're going to create a library
  10737. 8:34:27object our library object will contain
  10738. 8:34:30our array of book objects because
  10739. 8:34:32libraries contain books going to our
  10740. 8:34:34library
  10741. 8:34:36class what should libraries have let's
  10742. 8:34:39say that our library should have a name
  10743. 8:34:41what's the name of the library maybe a
  10744. 8:34:43year in which the library was
  10745. 8:34:45established in year and libraries should
  10746. 8:34:48contain book objects we will create an
  10747. 8:34:51array of book objects named
  10748. 8:34:54books we'll need to instructor for the
  10749. 8:34:56library
  10750. 8:34:57class we'll need the following a string
  10751. 8:35:00for the name of the
  10752. 8:35:02library an integer for the year of the
  10753. 8:35:04library when was the library built and
  10754. 8:35:08an array of book
  10755. 8:35:10objects this St name equals
  10756. 8:35:13name this doe equals
  10757. 8:35:18year this. books equals the books that
  10758. 8:35:23we receive this array
  10759. 8:35:26all right going back to our main Java
  10760. 8:35:27file this is where aggregation is going
  10761. 8:35:30to come
  10762. 8:35:31in we will create a library
  10763. 8:35:34object
  10764. 8:35:36library
  10765. 8:35:37library equals new
  10766. 8:35:41library but we have to pass in the
  10767. 8:35:43following as arguments to the
  10768. 8:35:45Constructor a name for the library a
  10769. 8:35:48year for the library and an array of
  10770. 8:35:51book
  10771. 8:35:52objects so for the library I'm going to
  10772. 8:35:55say the NYC the New York
  10773. 8:35:58City public
  10774. 8:36:01library after a quick Google search this
  10775. 8:36:04library was built in the year
  10776. 8:36:081897 then we also need an array of book
  10777. 8:36:10objects I will pass in my array of book
  10778. 8:36:13objects named
  10779. 8:36:15books this is
  10780. 8:36:18aggregation our library object has book
  10781. 8:36:22objects within the library class let's
  10782. 8:36:24also create a display info method for
  10783. 8:36:27the library we'll make it a little bit
  10784. 8:36:29different this method won't return
  10785. 8:36:31anything let's say the return type is
  10786. 8:36:34void we'll name this method display info
  10787. 8:36:38we'll display the following information
  10788. 8:36:40about this Library let's output the
  10789. 8:36:44following
  10790. 8:36:45the this.e of the
  10791. 8:36:49library plus a space
  10792. 8:36:51character plus
  10793. 8:36:54this.name you can also also use a printf
  10794. 8:36:56statement if you would prefer to use
  10795. 8:36:58that I'm trying to keep this as beginner
  10796. 8:37:00friendly as possible so let's test
  10797. 8:37:03this we will take our library object
  10798. 8:37:07call it display info
  10799. 8:37:13method the
  10800. 8:37:141897 New York City public
  10801. 8:37:18library then within this method of
  10802. 8:37:20display info for the library let's also
  10803. 8:37:23list all the books that it has this
  10804. 8:37:25Library
  10805. 8:37:26has aggregated a whole bunch of book
  10806. 8:37:28objects so then we can display them or
  10807. 8:37:30use them for something let's output the
  10808. 8:37:33following let's output
  10809. 8:37:37books
  10810. 8:37:39available then we'll use an enhanced for
  10811. 8:37:42Loop the data type of what we're
  10812. 8:37:44iterating through is book objects for
  10813. 8:37:48every book in my array of books take
  10814. 8:37:52each book call the books display in
  10815. 8:37:57method and then we do have to print it
  10816. 8:37:59using a print line statement because
  10817. 8:38:02this method returns a
  10818. 8:38:05string there we go let's test
  10819. 8:38:07it so after calling the display info
  10820. 8:38:10method of the
  10821. 8:38:12library we should display the
  10822. 8:38:14information of the library including its
  10823. 8:38:16books the 1897 New York City public
  10824. 8:38:19library and we display the books
  10825. 8:38:21available which are the array of books
  10826. 8:38:23that we have created
  10827. 8:38:25so that's aggregation it represents a
  10828. 8:38:28has a relationship between objects our
  10829. 8:38:31library object has book objects within
  10830. 8:38:34it there's another similar concept
  10831. 8:38:36called composition with aggregation if
  10832. 8:38:39you were to delete this Library class
  10833. 8:38:41for example I'll delete
  10834. 8:38:45it well these book objects can exist
  10835. 8:38:47independently outside of the library the
  10836. 8:38:50books aren't built into the library so
  10837. 8:38:53they can exist independently which is a
  10838. 8:38:55key compared to composition which is the
  10839. 8:38:57next topic we'll cover and well
  10840. 8:38:59everybody that is aggregation using Java
  10841. 8:39:02hey everybody in this video I'm going to
  10842. 8:39:04explain composition to you in
  10843. 8:39:05programming composition represents a
  10844. 8:39:08part of relationship between objects for
  10845. 8:39:11example an engine is part of a car an
  10846. 8:39:15object can be part of another object
  10847. 8:39:18this allows for complex objects to be
  10848. 8:39:20constructed from smaller objects in this
  10849. 8:39:23demonstration we're going to create a
  10850. 8:39:24car object
  10851. 8:39:26the car object is composed of an engine
  10852. 8:39:28object we will create a class of car and
  10853. 8:39:32a class of
  10854. 8:39:38engine we'll begin with our class of car
  10855. 8:39:41what sorts of attributes should cars
  10856. 8:39:43have let's say a string of
  10857. 8:39:46model an INT of year and an engine
  10858. 8:39:51object engine engine
  10859. 8:39:55we'll need a
  10860. 8:39:57Constructor we'll pass in a string for
  10861. 8:40:00the
  10862. 8:40:00model an INT for the
  10863. 8:40:04year now for the engine we're not going
  10864. 8:40:06to be passing in an engine object let's
  10865. 8:40:08say that when we construct a car object
  10866. 8:40:11we have to pass in a string for the
  10867. 8:40:14engine
  10868. 8:40:15type is it an inline engine is it a V6
  10869. 8:40:19that sort of thing we will assign this
  10870. 8:40:22St model equals the model that we
  10871. 8:40:24receive
  10872. 8:40:26this. year equals the year that we
  10873. 8:40:28receive now with our engine this. engine
  10874. 8:40:33we're going to call the Constructor for
  10875. 8:40:35a new engine
  10876. 8:40:37object and then we can pass in our
  10877. 8:40:39engine
  10878. 8:40:40type but we still however have to set up
  10879. 8:40:43the Constructor for our engine class
  10880. 8:40:46when constructing a car object we will
  10881. 8:40:48also be constructing a new engine object
  10882. 8:40:51so let's go to our engine
  10883. 8:40:53class let's say that engine
  10884. 8:40:55have a
  10885. 8:40:56string of type you could say engine type
  10886. 8:41:02but that would be redundant I would say
  10887. 8:41:04let's just say type then we need a
  10888. 8:41:07Constructor for our engine class we will
  10889. 8:41:11need a string of type to pass
  10890. 8:41:14in this.type equals
  10891. 8:41:19type so going back to our car class when
  10892. 8:41:22we pass in a string for the engine type
  10893. 8:41:25you could say that this is
  10894. 8:41:27type this term is kind of ambiguous
  10895. 8:41:29though because a user is going to think
  10896. 8:41:31that the type is for the car type rather
  10897. 8:41:33than the engine type so when you pass
  10898. 8:41:36arguments your parameter names can be
  10899. 8:41:39different from the argument names that
  10900. 8:41:41you pass in we simply rename them when
  10901. 8:41:43this Constructor receives that argument
  10902. 8:41:46but type refers to the engine type all
  10903. 8:41:49right so this is a basic demonstration
  10904. 8:41:51of composition now we'll construct a car
  10905. 8:41:53object car car equals new
  10906. 8:41:56car and then we have to pass in the
  10907. 8:41:59following a string for the model an inch
  10908. 8:42:01for the year and a string for the engine
  10909. 8:42:04type pick a card that you like this time
  10910. 8:42:06I will pick a
  10911. 8:42:08Corvette the year will be
  10912. 8:42:1120125 and for the engine I will pick a
  10913. 8:42:14V8
  10914. 8:42:16engine then let's see if we have these
  10915. 8:42:19details I'm going to Output my car
  10916. 8:42:22objects model and these attributes I did
  10917. 8:42:26not set to private but you can set them
  10918. 8:42:28to private and use Getters and Setters
  10919. 8:42:30if you would like so we have
  10920. 8:42:34model
  10921. 8:42:38year and we have an engine
  10922. 8:42:43object here's what we have we have our
  10923. 8:42:46model Corvette year 2025 now for the
  10924. 8:42:50engine our engine is an object it's a
  10925. 8:42:52reference data type so if you you output
  10926. 8:42:55it directly you're given a hash ID to
  10927. 8:42:58represent that engine object so if we
  10928. 8:43:00need the type of engine we're going to
  10929. 8:43:03access our engine object then get the
  10930. 8:43:05type so following our engine object we
  10931. 8:43:08will use that access modifier that
  10932. 8:43:11dot then access the type the
  10933. 8:43:16attribute so the engine type is a
  10934. 8:43:19V8 so it's like we have an object that's
  10935. 8:43:22composed with another object our n
  10936. 8:43:25is part of our car you know what let's
  10937. 8:43:28create a method for the engine we'll
  10938. 8:43:30create a start
  10939. 8:43:31method void
  10940. 8:43:34start we'll output the following let's
  10941. 8:43:37say using a print line
  10942. 8:43:39statement you start
  10943. 8:43:42the plus this. type meaning type of the
  10944. 8:43:47engine plus the word engine then going
  10945. 8:43:50to our car class let's create a start
  10946. 8:43:52method for the car class
  10947. 8:43:55void
  10948. 8:43:57start when we start the car we also
  10949. 8:43:59start the engine let's take this.
  10950. 8:44:03engine call it start
  10951. 8:44:07method and then we can output something
  10952. 8:44:09else too if we would like let's
  10953. 8:44:12output the plus this do
  10954. 8:44:17model plus the words is
  10955. 8:44:22running now if we start the car let's
  10956. 8:44:25take our car call it start
  10957. 8:44:28method and we should get the following
  10958. 8:44:31you start the V8 engine the Corvette is
  10959. 8:44:34running now a key difference with
  10960. 8:44:37composition is that if we delete our car
  10961. 8:44:40object that should also delete our
  10962. 8:44:43engine cuz our engine is part of our car
  10963. 8:44:47so to demonstrate that I'm going to
  10964. 8:44:50delete our car object
  10965. 8:44:55and we no longer have access to the
  10966. 8:44:59engine all right everybody that is
  10967. 8:45:01composition it represents a part of
  10968. 8:45:03relationship between objects for example
  10969. 8:45:06an engine is part of a car this allows
  10970. 8:45:09for complex objects to be constructed
  10971. 8:45:12from smaller objects and well everybody
  10972. 8:45:14that is composition using Java well look
  10973. 8:45:18who showed up today I got to talk about
  10974. 8:45:20rapper classes rapper classes allow
  10975. 8:45:22primitive values you know such as as
  10976. 8:45:25integers characters doubles booleans
  10977. 8:45:27those sorts of things rapper classes
  10978. 8:45:29allow these primitive values to be
  10979. 8:45:31treated as objects or basically wrapping
  10980. 8:45:33primitive values within an object kind
  10981. 8:45:36of like a Christmas present generally
  10982. 8:45:38you don't need to wrap Primitives unless
  10983. 8:45:40you need an object for some reason like
  10984. 8:45:42if you're using a collections framework
  10985. 8:45:44in Java and rapper classes also give you
  10986. 8:45:46access to a lot of useful static utility
  10987. 8:45:48methods so let me give you a
  10988. 8:45:50demonstration of rapper
  10989. 8:45:52classes the method of creating rapper
  10990. 8:45:54classes I'm about about to show you is
  10991. 8:45:55actually depreciated but it's going to
  10992. 8:45:57help you visualize how this works
  10993. 8:45:59wrapping A Primitive within an object
  10994. 8:46:02the modern approach is using a technique
  10995. 8:46:04called autoboxing but the method I'm
  10996. 8:46:06about to show you is a good way to
  10997. 8:46:07visualize this whole thing so let's say
  10998. 8:46:09we have an INT of a variable a a equal
  10999. 8:46:15123 we can treat this primitive as an
  11000. 8:46:19object by using a wrapper class here's
  11001. 8:46:21how rather than declaring this variable
  11002. 8:46:24as an INT
  11003. 8:46:25we will use the rapper class of integer
  11004. 8:46:29integer variable a equals a new integer
  11005. 8:46:35and then we're going to pass in that
  11006. 8:46:37number that integer as an argument a is
  11007. 8:46:40an object we're passing in that
  11008. 8:46:42primitive data type that int as an
  11009. 8:46:45argument to the integer class so a is an
  11010. 8:46:48object but it does contain A Primitive
  11011. 8:46:50value again this is depreciated as a
  11012. 8:46:53version 9 so you shouldn't do it this
  11013. 8:46:55way but this is a good way to visualize
  11014. 8:46:57it then if you need a double we'll use
  11015. 8:47:00the wrapper class of double let's say
  11016. 8:47:02variable b equals a new double then
  11017. 8:47:06we'll pass in a double 3.14 is
  11018. 8:47:09good then we have char which is
  11019. 8:47:13character that's the rapper class
  11020. 8:47:15character C equals a new character then
  11021. 8:47:19pass in a character I'll pass in a
  11022. 8:47:22dollar
  11023. 8:47:23sign We'll add one more let's say a
  11024. 8:47:26Boolean we will use the wrapper class of
  11025. 8:47:28Boolean Boolean D equals a new boan then
  11026. 8:47:34pass in a Boolean
  11027. 8:47:35value here's just a few examples these
  11028. 8:47:38are all objects a b c and d by wrapping
  11029. 8:47:42these Primitives within an object that
  11030. 8:47:44allows us to use these primitive values
  11031. 8:47:47within collections Frameworks such as
  11032. 8:47:49array lists which we'll talk about in
  11033. 8:47:50the next topic the modern way to use
  11034. 8:47:53rapper classes is actually just to
  11035. 8:47:55assign them directly to their primitive
  11036. 8:47:56values the class of integer a equals
  11037. 8:48:01123 doubleb equal
  11038. 8:48:043.14 character C equals a dollar sign
  11039. 8:48:08character and then D equals
  11040. 8:48:11true this technique is called
  11041. 8:48:16autoboxing We're directly assigning
  11042. 8:48:18these Primitives into an object using a
  11043. 8:48:21rapper class now this is very similar to
  11044. 8:48:24how we declare and assign strings to
  11045. 8:48:26create a string you would say string
  11046. 8:48:29let's say E equals a string of
  11047. 8:48:32characters such as the word Pizza you
  11048. 8:48:36can see that there's a similar pattern
  11049. 8:48:37we have always created strings this way
  11050. 8:48:40but with autoboxing it's a similar
  11051. 8:48:42process these are all reference data
  11052. 8:48:44types they're a type of
  11053. 8:48:46object there's also a technique called
  11054. 8:48:49unboxing to convert a rapper class back
  11055. 8:48:51to its primitive this is
  11056. 8:48:56unboxing so let's say we would like to
  11057. 8:48:58convert our object of a it's an integer
  11058. 8:49:01object back to its primitive let's say
  11059. 8:49:04int x equals all you got to do is set it
  11060. 8:49:08equal to your object in this case our
  11061. 8:49:11integer object of a this is unboxing
  11062. 8:49:15we're taking a primitive that's wrapped
  11063. 8:49:17in an object and unboxing it we
  11064. 8:49:20unwrapping it and setting it back to its
  11065. 8:49:22primitive if we were unboxing a double
  11066. 8:49:24we would say double some variable equals
  11067. 8:49:29b or chars Char x
  11068. 8:49:33equals our character object of c and
  11069. 8:49:36then Boolean Boolean x equals D again
  11070. 8:49:40this is autoboxing we're wrapping up a
  11071. 8:49:43Christmas present we're wrapping up a
  11072. 8:49:45primitive within an object and then
  11073. 8:49:47unboxing is removing it we're opening
  11074. 8:49:49the Christmas present now these rapper
  11075. 8:49:51classes themselves do have some pretty
  11076. 8:49:53useful utility methods that are static
  11077. 8:49:56let me demonstrate a few you might be
  11078. 8:49:57interested in but there would be too
  11079. 8:49:59many to cover in just this one video if
  11080. 8:50:01you ever need to convert a primitive
  11081. 8:50:03data type into a string there's a two-
  11082. 8:50:05string method of these utility classes
  11083. 8:50:07so let's say we have string
  11084. 8:50:09a string
  11085. 8:50:11B string
  11086. 8:50:13C and string
  11087. 8:50:18D I can convert an integer into a string
  11088. 8:50:22using the integer wrapper class it's ACC
  11089. 8:50:24statically so we type the name of the
  11090. 8:50:26class integer called the two string
  11091. 8:50:29method which is static and then pass it
  11092. 8:50:32an integer like
  11093. 8:50:33123 so let's do this with the
  11094. 8:50:36double we will access the double wrapper
  11095. 8:50:39class called the two string method and
  11096. 8:50:41then pass in a double
  11097. 8:50:453.14 then to convert a character to a
  11098. 8:50:47string we would use the character
  11099. 8:50:49wrapper class character to string and
  11100. 8:50:53then pass in a character
  11101. 8:50:56and then
  11102. 8:50:57Boolean we will access the Boolean
  11103. 8:50:59wrapper class called the two string
  11104. 8:51:02method and then pass in a
  11105. 8:51:05Boolean so strings can be concatenated
  11106. 8:51:08together we should be able to do the
  11107. 8:51:11following let's say string X = A + B + C
  11108. 8:51:17+
  11109. 8:51:18D and then we'll output whatever our
  11110. 8:51:21variable X is
  11111. 8:51:25we get 1 123 3.14 at false it's all one
  11112. 8:51:30long string it's because we use these
  11113. 8:51:32rapper classes to convert these
  11114. 8:51:34primitive data types into Strings if you
  11115. 8:51:37ever need to convert a primitive to a
  11116. 8:51:38string then use that primitive data
  11117. 8:51:40types rapper class and call the two
  11118. 8:51:42string method then you just need to pass
  11119. 8:51:44that value in as an argument then it's
  11120. 8:51:46going to spit out a string for you now
  11121. 8:51:48on the other hand to convert a string to
  11122. 8:51:50a primitive data type there's another
  11123. 8:51:51useful utility method of wrapper classes
  11124. 8:51:55so what we'll do is
  11125. 8:51:56parsing we will have an INT of
  11126. 8:52:00a a double of
  11127. 8:52:03B A Char of
  11128. 8:52:06c and a Boolean of
  11129. 8:52:10D int a will equal we're going to call
  11130. 8:52:14the integer wrapper class call a static
  11131. 8:52:17method of parse int we can convert a
  11132. 8:52:21string to an integer I will convert a
  11133. 8:52:24string of 123 to an
  11134. 8:52:27integer now with doubles you will call
  11135. 8:52:29the double wrapper class and we will
  11136. 8:52:32parse a
  11137. 8:52:35double we'll pass in a string of
  11138. 8:52:393.14 now characters don't have a parse
  11139. 8:52:42method what you could do instead is with
  11140. 8:52:45any string let's say we have a string of
  11141. 8:52:46pizza strings do have built-in methods
  11142. 8:52:49one of which is the Char at
  11143. 8:52:52method return the character the first
  11144. 8:52:55position where index is zero again
  11145. 8:52:57character doesn't have a parse character
  11146. 8:52:59method so you would have to use charart
  11147. 8:53:01at which is unrelated to wrapper
  11148. 8:53:05classes so then with the
  11149. 8:53:07Boolean we would access the Boolean
  11150. 8:53:09wrapper class called the parse Boolean
  11151. 8:53:13method and we can convert a string
  11152. 8:53:16representation of a Boolean such as true
  11153. 8:53:19into an actual Boolean and then let's
  11154. 8:53:21output them now we shouldn't be able to
  11155. 8:53:23add these together much like what we did
  11156. 8:53:25when we used the two string method for
  11157. 8:53:27example string X = A + B + C + D this
  11158. 8:53:34isn't going to work because we're mixing
  11159. 8:53:36and matching different data types and
  11160. 8:53:38then if I was to Output
  11161. 8:53:42X that would give us a compilation error
  11162. 8:53:45to convert a string to its primitive
  11163. 8:53:47data type there is various parse methods
  11164. 8:53:49to handle that within our rapper classes
  11165. 8:53:52it depends on the data type you're
  11166. 8:53:53working with
  11167. 8:53:55there's a couple other miscellaneous
  11168. 8:53:57utility methods within rapper classes
  11169. 8:53:59I'll just cover two more so let's say we
  11170. 8:54:02have a Char of letter pick a
  11171. 8:54:06letter I will pick the letter lowercase
  11172. 8:54:10B I'm going to use a print line
  11173. 8:54:12statement we can check to see if our
  11174. 8:54:14letter actually is a letter we can
  11175. 8:54:17access the character wrapper class call
  11176. 8:54:20the is letter method and then pass in
  11177. 8:54:23our letter
  11178. 8:54:24this returns a Boolean true or false is
  11179. 8:54:27this character a
  11180. 8:54:30letter that is
  11181. 8:54:32true this would be useful for verifying
  11182. 8:54:35user input what if this was a dollar
  11183. 8:54:37sign is this a letter well this returns
  11184. 8:54:41false that is not a
  11185. 8:54:43letter I'll switch this back to
  11186. 8:54:45lowercase b there's also a built-in
  11187. 8:54:48method to check to see if this letter is
  11188. 8:54:50uppercase or not there's a built-in
  11189. 8:54:52utility method of the character rapper
  11190. 8:54:54class class to check to see if this is
  11191. 8:54:56uppercase or
  11192. 8:54:57not
  11193. 8:54:59character is uppercase and then pass in
  11194. 8:55:03our letter is it uppercase currently it
  11195. 8:55:07isn't that is
  11196. 8:55:09false but if it were
  11197. 8:55:13uppercase that returns
  11198. 8:55:15true some of these utility methods of
  11199. 8:55:18the character rapper class would be
  11200. 8:55:20great for verifying user input like a
  11201. 8:55:22username or a password all right
  11202. 8:55:24everybody so those are wrapper classes
  11203. 8:55:26we're basically taking a primitive and
  11204. 8:55:28wrapping it in an object kind of like a
  11205. 8:55:30Christmas present in the next topic
  11206. 8:55:32we're going to be working with array
  11207. 8:55:34lists and array lists only work with
  11208. 8:55:36objects so we need to utilize these
  11209. 8:55:38rapper classes in order to work with
  11210. 8:55:40array lists and wrapper classes do
  11211. 8:55:42provide some useful static utility
  11212. 8:55:44methods that you can always access all
  11213. 8:55:46right everybody and those are wrapper
  11214. 8:55:47classes and in the next video we're
  11215. 8:55:49going to cover array lists all right
  11216. 8:55:52people it's about time we get to array
  11217. 8:55:53lists and Java an array list is a
  11218. 8:55:56resizable array that stores objects you
  11219. 8:55:58can store Primitives if so you'll be
  11220. 8:56:01using autoboxing which we learned about
  11221. 8:56:03in the last topic on rapper classes
  11222. 8:56:05arrays are fixed in size but array lists
  11223. 8:56:08can change they're Dynamic here's how to
  11224. 8:56:11create an array list first we'll follow
  11225. 8:56:13our similar pattern with creating many
  11226. 8:56:15objects in Java array list let's say
  11227. 8:56:19list equals new array list we will need
  11228. 8:56:23to make One Import import java. u.
  11229. 8:56:27arraylist following the type of array
  11230. 8:56:30list we're going to use the diamond
  11231. 8:56:32operator this has to deal with generics
  11232. 8:56:34which is a future topic but for now
  11233. 8:56:37we're going to specify what type of
  11234. 8:56:39object we're going to be storing within
  11235. 8:56:40our array list and you can always use
  11236. 8:56:43the appropriate wrapper class to store
  11237. 8:56:45Primitives for example if you need to
  11238. 8:56:47store integers you can say integer and
  11239. 8:56:50then we do need that diamond operator
  11240. 8:56:51after the second array list
  11241. 8:56:54you don't also need to specify the data
  11242. 8:56:56type within the second Diamond operator
  11243. 8:56:59Java can infer that you're going to
  11244. 8:57:01follow this pattern to create an array
  11245. 8:57:03list this array list can store integers
  11246. 8:57:06you just have to specify that type so
  11247. 8:57:08for example let's take our list and add
  11248. 8:57:10a few
  11249. 8:57:11numbers
  11250. 8:57:15three 1 and
  11251. 8:57:21two and then we can print our list our
  11252. 8:57:25list I will print our
  11253. 8:57:28list and we have three numbers
  11254. 8:57:32312 now if you were storing doubles you
  11255. 8:57:35would set the type to be double and use
  11256. 8:57:37the double wrapper class 3.14 1.99
  11257. 8:57:422.01 I'm just making up numbers
  11258. 8:57:46here there we go so let's store
  11259. 8:57:51strings let's create an array list named
  11260. 8:57:54fruits we will store the names of some
  11261. 8:57:56fruit so the array name is going to be
  11262. 8:57:58fruits and then we will print our array
  11263. 8:58:01list of fruits think of some fruit I
  11264. 8:58:03will add an
  11265. 8:58:06apple an
  11266. 8:58:09orange a
  11267. 8:58:12banana and a coconut that should be good
  11268. 8:58:14enough for this
  11269. 8:58:18example and now we have an array list of
  11270. 8:58:21strings named fruits Apple Orange banana
  11271. 8:58:25coconut to add an element to an array
  11272. 8:58:27list you can use the add method add is a
  11273. 8:58:30built-in method of array lists to remove
  11274. 8:58:32an element you can use the remove method
  11275. 8:58:34then specify an index so let's take
  11276. 8:58:36fruits and remove the element at index
  11277. 8:58:40zero that should get rid of our
  11278. 8:58:44Apple then we have orange banana
  11279. 8:58:47coconut if we were to remove the element
  11280. 8:58:50at index one that should get rid of our
  11281. 8:58:52orange apple banana Coco
  11282. 8:58:55nut there's also the set method take our
  11283. 8:58:58Ray list of fruits called the set method
  11284. 8:59:01add a certain index let's say zero we
  11285. 8:59:04can set that element to be something
  11286. 8:59:05else let's replace apple with
  11287. 8:59:08pineapple replace the element at index
  11288. 8:59:11zero with
  11289. 8:59:13pineapple then we have pineapple orange
  11290. 8:59:15banana coconut or index
  11291. 8:59:19one would be apple pineapple banana
  11292. 8:59:22coconut
  11293. 8:59:24that's the set
  11294. 8:59:25method with an array list to get an
  11295. 8:59:28element at a certain index you can use
  11296. 8:59:29the get method then specify the element
  11297. 8:59:32number get the element at index zero
  11298. 8:59:36that's going to return
  11299. 8:59:38Apple get the element at index one would
  11300. 8:59:41be
  11301. 8:59:42orange
  11302. 8:59:45two
  11303. 8:59:46banana
  11304. 8:59:493
  11305. 8:59:51coconut you can also get the size of an
  11306. 8:59:54array list take your array list call the
  11307. 8:59:57size method that will return the total
  11308. 9:00:00amount of elements within your array
  11309. 9:00:02list so my array list has four
  11310. 9:00:06elements to sort your array list you
  11311. 9:00:09will use the collections framework call
  11312. 9:00:12the sort method and then pass in your
  11313. 9:00:15array list our array list is named
  11314. 9:00:17fruits and then you do need to import
  11315. 9:00:19this
  11316. 9:00:20class java.util doc collections
  11317. 9:00:24after sorting our array list let's print
  11318. 9:00:27it print my array list of
  11319. 9:00:30fruits so these all should be in
  11320. 9:00:32alphabetical order starting with a for
  11321. 9:00:34apple b for banana C for coconut o for
  11322. 9:00:37orange so that's how to sort an array
  11323. 9:00:39list you can use the collections
  11324. 9:00:41framework you could use an enhanced for
  11325. 9:00:44Loop to iterate through all the elements
  11326. 9:00:46of an array list so the data type of my
  11327. 9:00:48aray list is strings we specified that
  11328. 9:00:52type for every fruit in my array list of
  11329. 9:00:56fruits let's print each
  11330. 9:01:02fruit and here they are apple banana
  11331. 9:01:05coconut
  11332. 9:01:06orange all right so now we're going to
  11333. 9:01:08cover an
  11334. 9:01:11exercise we'll create an array and
  11335. 9:01:13accept user input a user will enter in
  11336. 9:01:16all the food that they want within this
  11337. 9:01:17array list so to help us with this
  11338. 9:01:19exercise we will need a scanner scanner
  11339. 9:01:22scanner equals new scanner
  11340. 9:01:25pass in
  11341. 9:01:27system.in import this class and then be
  11342. 9:01:31sure to close your scanner at the end
  11343. 9:01:32because I tend to forget scanner.
  11344. 9:01:36close we'll need an array
  11345. 9:01:39list again we'll follow a similar
  11346. 9:01:41pattern with creating objects array list
  11347. 9:01:45let's say list equals new array
  11348. 9:01:49list using the diamond operator we're
  11349. 9:01:52going to specify the type of what we're
  11350. 9:01:54storing within this array list we'll
  11351. 9:01:56stick with
  11352. 9:01:58strings let's rename our list as Foods
  11353. 9:02:02we'll be storing some food well at least
  11354. 9:02:04the name of
  11355. 9:02:06food we'll create a prompt we'll ask a
  11356. 9:02:09user enter the number of food they would
  11357. 9:02:11like to
  11358. 9:02:12store enter the number of food you would
  11359. 9:02:19like and I'll use print rather than
  11360. 9:02:21print line for the user input
  11361. 9:02:25we'll create a variable of num of food
  11362. 9:02:28to store the number of food that we're
  11363. 9:02:30going to store equals take our scanner
  11364. 9:02:34call the next int method to get the next
  11365. 9:02:36integer from the
  11366. 9:02:38user once we accept an integer we should
  11367. 9:02:41clear the input buffer because there's
  11368. 9:02:42going to be a new line character within
  11369. 9:02:44there take our scanner call the next
  11370. 9:02:47line method to clear it let's do a test
  11371. 9:02:52run and enter the number of food you
  11372. 9:02:54would like I'll just say three and that
  11373. 9:02:57seems to work for now we have the number
  11374. 9:02:59of food we're going to store within this
  11375. 9:03:01array list in my example let's say I
  11376. 9:03:03would like to enter three food items
  11377. 9:03:06well we could use a for Loop to iterate
  11378. 9:03:08three times we will ask your user to
  11379. 9:03:10enter in a food three times then so we
  11380. 9:03:13can use a for Loop for that within our
  11381. 9:03:15for loop we're going to create a counter
  11382. 9:03:17of I meaning index we can set that to be
  11383. 9:03:20zero or one let's stick with one we'll
  11384. 9:03:23continue this for loop as long as I is
  11385. 9:03:26less than or equal to our number of food
  11386. 9:03:30variable if I would like to enter in
  11387. 9:03:32four food items this would be four so we
  11388. 9:03:35should iterate this Loop four times we
  11389. 9:03:38will increment I by One during each
  11390. 9:03:40cycle of this
  11391. 9:03:41Loop during each iteration of this Loop
  11392. 9:03:44we will ask for a food item I'll use
  11393. 9:03:47print rather than print line we will ask
  11394. 9:03:50a user to enter food number
  11395. 9:03:54then I'll add plus our index of I so
  11396. 9:03:57during the first cycle I is going to be
  11397. 9:03:59one then 2 3 four so on and so
  11398. 9:04:03forth then I'll add a colon then a
  11399. 9:04:07space we'll create a local variable of
  11400. 9:04:09food this will be of the string data
  11401. 9:04:11type we will set this equal to use our
  11402. 9:04:14scanner called the next line method to
  11403. 9:04:17get a line of text from the
  11404. 9:04:19user once we have our string of food
  11405. 9:04:21we're going to add it to our array list
  11406. 9:04:23of Foods so take your array list of
  11407. 9:04:26foods call the ad method to add an
  11408. 9:04:29element we will add our string of food
  11409. 9:04:32to our array list of
  11410. 9:04:34foods and then once we're done let's
  11411. 9:04:36print our array list using a print line
  11412. 9:04:39statement we will print our Ray list of
  11413. 9:04:42foods all right let's test
  11414. 9:04:44it enter the number of food you would
  11415. 9:04:47like I'll say four so we should ask for
  11416. 9:04:50four food items enter food number one
  11417. 9:04:53I'll say
  11418. 9:04:54pizza hot
  11419. 9:04:56dog hamburger and then
  11420. 9:05:00Taco and here's my array list of food
  11421. 9:05:03pizza hot dog hamburger
  11422. 9:05:06taco all right everybody those are array
  11423. 9:05:09lists an array list is a resizable array
  11424. 9:05:12that stores objects you can store
  11425. 9:05:14Primitives you're just going to use
  11426. 9:05:16Java's autoboxing feature if that's the
  11427. 9:05:18case once you have your array list
  11428. 9:05:21there's various methods to add remove
  11429. 9:05:23move or set elements and well everybody
  11430. 9:05:26those are array lists in Java today I'm
  11431. 9:05:29going to talk about exceptions using
  11432. 9:05:31Java an exception is an event that
  11433. 9:05:33interrupts the normal flow of a program
  11434. 9:05:36these exceptions pop up if you do things
  11435. 9:05:38like divide by zero you can't find a
  11436. 9:05:41resource for some reason or if there's a
  11437. 9:05:43mismatch with the input type let's say
  11438. 9:05:45the program asks for a number and you
  11439. 9:05:47type in a string of characters these
  11440. 9:05:49sorts of events interrupt your program
  11441. 9:05:51they're called exceptions there's a way
  11442. 9:05:53that we can handle them and that is by
  11443. 9:05:55using a few blocks of code try catch and
  11444. 9:05:59optionally finally so any code that's
  11445. 9:06:02considered dangerous where it might
  11446. 9:06:04interrupt your program you can surround
  11447. 9:06:06with a tri block here's an example so
  11448. 9:06:09let's say I'm going to take the number
  11449. 9:06:12one and divide it by zero mathematically
  11450. 9:06:15speaking you can't divide by
  11451. 9:06:19zero that causes an exception more
  11452. 9:06:22specifically and arithmetic exception
  11453. 9:06:25I'm actually just going to copy this and
  11454. 9:06:26save
  11455. 9:06:27it this code is dangerous it's
  11456. 9:06:30interrupting our program by causing an
  11457. 9:06:33exception we can gracefully handle this
  11458. 9:06:36exception so that it doesn't interrupt
  11459. 9:06:38the normal flow of our program any
  11460. 9:06:41dangerous code we're going to surround
  11461. 9:06:43with a tri block try and a set of curly
  11462. 9:06:50braces there we go however if we have a
  11463. 9:06:53Tri block we also need a catch
  11464. 9:06:56block we will catch any
  11465. 9:06:59exceptions but we're going to list the
  11466. 9:07:01specific kind of exception we're going
  11467. 9:07:03to catch well let's catch that one
  11468. 9:07:05exception that arithmetic
  11469. 9:07:07exception this is the type we're setting
  11470. 9:07:10up a parameter here arithmetic exception
  11471. 9:07:13is the type we will give this exception
  11472. 9:07:15a name of e for a
  11473. 9:07:17nickname if we encounter this exception
  11474. 9:07:20we can take a different course of action
  11475. 9:07:22instead of interrup the program so let's
  11476. 9:07:25do the
  11477. 9:07:25following let's
  11478. 9:07:27output you can't divide by zero idiot so
  11479. 9:07:33if we run this again it shouldn't
  11480. 9:07:36interrupt our
  11481. 9:07:38program we catch this exception and
  11482. 9:07:41output this text
  11483. 9:07:43instead so any code that's dangerous
  11484. 9:07:46where it might cause an exception you
  11485. 9:07:48can surround with the tri block you can
  11486. 9:07:50add more than one catch block to catch
  11487. 9:07:52and handle spefic specific exceptions so
  11488. 9:07:55this time we're going to accept some
  11489. 9:07:56user input we'll need a scanner for now
  11490. 9:07:59I'm going to write the scanner outside
  11491. 9:08:01of the tri Block near the end of this
  11492. 9:08:03video there's a feature called used with
  11493. 9:08:05resources I'll show you how to do that
  11494. 9:08:07later scanner scanner equals new scanner
  11495. 9:08:11type
  11496. 9:08:12system.in and then import this
  11497. 9:08:15class import java.util.scanner
  11498. 9:08:19anytime you accept user input it's
  11499. 9:08:22almost always d dangerous code because a
  11500. 9:08:24user can type in
  11501. 9:08:26anything this time let's prompt a user
  11502. 9:08:30to enter a
  11503. 9:08:32number and I will use
  11504. 9:08:35print let's create a local variable of
  11505. 9:08:38int number equals use our scanner called
  11506. 9:08:42the next int method to accept an integer
  11507. 9:08:46and then we'll just output our
  11508. 9:08:52number and enter a number uh how about
  11509. 9:08:55no I'm going to type in the word Pizza
  11510. 9:08:57instead well we get an exception an
  11511. 9:09:00input mismatch exception Java was
  11512. 9:09:03expecting an integer but we typed in a
  11513. 9:09:05string instead we can handle these
  11514. 9:09:07exceptions too and I'm just going to
  11515. 9:09:09copy the name of this exception because
  11516. 9:09:12we'll reuse it let's catch the following
  11517. 9:09:15let's
  11518. 9:09:17catch any input mismatch
  11519. 9:09:20exceptions this is the type we will give
  11520. 9:09:23this exception a nickname of e so this
  11521. 9:09:26exception we actually do have to
  11522. 9:09:28import import java.util do input
  11523. 9:09:32mismatch exception in case there's a
  11524. 9:09:34mismatch with the data type we're asking
  11525. 9:09:36the user for a number but they type in a
  11526. 9:09:38string Let's help with the following
  11527. 9:09:41let's
  11528. 9:09:42say that wasn't a
  11529. 9:09:45number let's try it
  11530. 9:09:47again enter a number uh I'll type in the
  11531. 9:09:50word I already did pizza let's do Taco
  11532. 9:09:53that wasn't a
  11533. 9:09:55number and our program finished with no
  11534. 9:09:58problems you can catch and handle more
  11535. 9:10:00than one exception there is a catch all
  11536. 9:10:03statement that you could
  11537. 9:10:06add you could catch all exceptions
  11538. 9:10:10exception e using this by itself isn't
  11539. 9:10:13good
  11540. 9:10:14practice you want to let a user know
  11541. 9:10:16exactly what went wrong if all else
  11542. 9:10:19fails and there's an exception that you
  11543. 9:10:21don't anticipate you could just say okay
  11544. 9:10:23something went
  11545. 9:10:26wrong you could technically use this by
  11546. 9:10:31itself enter a number for example I'll
  11547. 9:10:33type in hot dog something went wrong
  11548. 9:10:37this does prevent our program from being
  11549. 9:10:39interrupted it is good practice to let
  11550. 9:10:41the user know what went wrong exactly
  11551. 9:10:44I'm looking at you Microsoft catching
  11552. 9:10:47all exceptions acts as some sort of
  11553. 9:10:49safety net you should only do it at the
  11554. 9:10:51end in case there's something you don't
  11555. 9:10:53anticipate it is better to catch and
  11556. 9:10:55handle specific types of exceptions
  11557. 9:10:58because you can let the user know what
  11558. 9:10:59went wrong
  11559. 9:11:00exactly now there's also the finally
  11560. 9:11:03block this is
  11561. 9:11:05optional finally will always execute
  11562. 9:11:08whether there's an exception or not this
  11563. 9:11:11is where you might clean up any
  11564. 9:11:12resources for example with my scanner I
  11565. 9:11:15forgot to close it we can actually close
  11566. 9:11:17it within the final
  11567. 9:11:19block when we're done with our scanner
  11568. 9:11:22let's close it
  11569. 9:11:24because if we encounter an exception we
  11570. 9:11:26might not close the scanner other times
  11571. 9:11:29finally might be useful is if you open a
  11572. 9:11:31file within a tri
  11573. 9:11:33block then you'll want to close the file
  11574. 9:11:36when you're done with it you can do that
  11575. 9:11:37within the finally block for testing
  11576. 9:11:39purposes I'm just going to add this
  11577. 9:11:43always
  11578. 9:11:45executes all right let's do a test
  11579. 9:11:49run enter a number I'll type in 12 three
  11580. 9:11:53and then we output 123 see that finally
  11581. 9:11:56block does execute this always
  11582. 9:12:00executes if we encounter an exception
  11583. 9:12:03enter a number I will type in
  11584. 9:12:05Taco we get our exception message for
  11585. 9:12:09input mismatch and then that finally
  11586. 9:12:11block does
  11587. 9:12:12execute finally is quite often used for
  11588. 9:12:15cleanup cleaning up your program when
  11589. 9:12:17you're done with it when you open a file
  11590. 9:12:19or resource you want to close it we'll
  11591. 9:12:22have more practice with this on the next
  11592. 9:12:23topic of file handling now there's also
  11593. 9:12:26try with
  11594. 9:12:27resources and actually Java is
  11595. 9:12:29recommending this rather than declaring
  11596. 9:12:32our scanner outside of the tri block we
  11597. 9:12:35can place within a set of parentheses
  11598. 9:12:37and then Java is automatically going to
  11599. 9:12:39close those resources when it's done
  11600. 9:12:40opening them so this works
  11601. 9:12:43too so enter a number
  11602. 9:12:48123 enter a number
  11603. 9:12:50Pizza that wasn't a number
  11604. 9:12:53all right everybody so those are
  11605. 9:12:55exceptions they're events that interrupt
  11606. 9:12:57the normal flow of a program exceptions
  11607. 9:12:59occur when you divide by zero you can't
  11608. 9:13:02locate a resource or there's a mismatch
  11609. 9:13:04of the input type that a user types in
  11610. 9:13:07you'll want to surround any dangerous
  11611. 9:13:08code with a tri block anytime you accept
  11612. 9:13:11user input or try and locate an external
  11613. 9:13:14resource that's almost always dangerous
  11614. 9:13:16code try any dangerous code catch any
  11615. 9:13:19exceptions and optionally you can add
  11616. 9:13:22finally to do any resource cleanup and
  11617. 9:13:25well everybody those are exceptions in
  11618. 9:13:28Java all right everybody in this video
  11619. 9:13:30I'm going to show you how you can write
  11620. 9:13:31a file using Java after doing some
  11621. 9:13:33research I've encountered four popular
  11622. 9:13:36options the first way I'm about to show
  11623. 9:13:37you is the most simple and is what I'll
  11624. 9:13:39be demonstrating in this topic we'll be
  11625. 9:13:41using a file writer object it's good for
  11626. 9:13:43writing small or medium-sized text files
  11627. 9:13:46there's also buffered writer this has
  11628. 9:13:48better performance for large amounts of
  11629. 9:13:50text print writer is best for structure
  11630. 9:13:53data like reports or logs then there's
  11631. 9:13:57file output stream which is best for
  11632. 9:13:59binary files such as images or audio
  11633. 9:14:02files files of that nature in this topic
  11634. 9:14:05I'm going to cover file writer just so
  11635. 9:14:06we get the hang of the basics I might
  11636. 9:14:08make videos on the other writers but I
  11637. 9:14:10haven't decided yet here's how we can
  11638. 9:14:11write a basic file using Java we'll need
  11639. 9:14:13a file writer object file writer I'll
  11640. 9:14:18name this file writer just writer for
  11641. 9:14:20short equals new file
  11642. 9:14:24writer then we need to pass in a file
  11643. 9:14:27path or a file name let's create a file
  11644. 9:14:29named test and include a file extension
  11645. 9:14:32let's say it's a txt file a plain text
  11646. 9:14:34file now you could include an absolute
  11647. 9:14:36file path I'll show you how to do that
  11648. 9:14:38later our plain text file a test is
  11649. 9:14:41going to appear in our source folder
  11650. 9:14:44reading or writing files is kind of
  11651. 9:14:45unpredictable you could consider that
  11652. 9:14:47dangerous code any dangerous code we
  11653. 9:14:49want to enclose within a tri block try
  11654. 9:14:53this dangerous code catch any exceptions
  11655. 9:14:55that might interrupt our
  11656. 9:14:56program with modern Java you can try and
  11657. 9:15:00open up some resource add a set of
  11658. 9:15:02parentheses after try we will try to
  11659. 9:15:05open up a file writer
  11660. 9:15:09object so file writer writer equals new
  11661. 9:15:12file writer then we have our file name
  11662. 9:15:15including the
  11663. 9:15:16extension now we do have to import this
  11664. 9:15:19class import
  11665. 9:15:21java. file writer
  11666. 9:15:24if for some reason we can't write this
  11667. 9:15:25file we might encounter an exception we
  11668. 9:15:28will catch any IO exceptions IO meaning
  11669. 9:15:31input output catch any input output
  11670. 9:15:34exceptions that's the type we will give
  11671. 9:15:37this exception a nickname of
  11672. 9:15:39e then we do have to import this class
  11673. 9:15:42too in order to work with IO exceptions
  11674. 9:15:45java.io doio
  11675. 9:15:47exception in case there's a problem
  11676. 9:15:50writing this file we'll catch this
  11677. 9:15:52exception if it comes up this acts as
  11678. 9:15:54more of a safety net if we encounter
  11679. 9:15:56this exception we can say something
  11680. 9:15:58generic such as could not write file but
  11681. 9:16:04it is best practice to handle specific
  11682. 9:16:06kind of exceptions first momentarily
  11683. 9:16:08we're going to add a catch clause for a
  11684. 9:16:10file not found exception in case we
  11685. 9:16:13can't locate a file path but we'll take
  11686. 9:16:14care of that in a moment within the tri
  11687. 9:16:17Block in order to write a file using
  11688. 9:16:19Java using file writer we will take our
  11689. 9:16:21file writer which we named
  11690. 9:16:24writer called the write method and then
  11691. 9:16:27pass in a
  11692. 9:16:28string let's say I like then pick your
  11693. 9:16:31favorite food as you know my favorite
  11694. 9:16:34food is
  11695. 9:16:35pizza then after writing this file if we
  11696. 9:16:38don't encounter any exceptions we'll
  11697. 9:16:40print a confirmation
  11698. 9:16:42message let's say the
  11699. 9:16:44following file has been
  11700. 9:16:48written all right let's test this
  11701. 9:16:53file has been
  11702. 9:16:56written since we didn't specify a file
  11703. 9:16:58path your file is likely going to be
  11704. 9:17:01within your Source folder and here's
  11705. 9:17:03mine test.txt and it says the following
  11706. 9:17:07I like
  11707. 9:17:08pizza now you could pick a different
  11708. 9:17:10location for example you could use an
  11709. 9:17:13absolute file path so let's say I would
  11710. 9:17:15like to write this file to my desktop so
  11711. 9:17:18what I could do I'm going to go to my
  11712. 9:17:20desktop I'm going to get the file
  11713. 9:17:23location my desktop I'll just right
  11714. 9:17:25click on one of these
  11715. 9:17:26folders go to properties copy this
  11716. 9:17:31location then I'm going to paste that
  11717. 9:17:33file
  11718. 9:17:36path you'll likely need to use double
  11719. 9:17:39forward slashes Java interprets a single
  11720. 9:17:42forward slash as an escape sequence so
  11721. 9:17:44you may need to use double slashes when
  11722. 9:17:47I write this file it should be at this
  11723. 9:17:49location this absolute file path Let's
  11724. 9:17:52test it
  11725. 9:17:53file has been written let's go to my
  11726. 9:17:55desktop and here's that
  11727. 9:17:57file
  11728. 9:17:59test it says I like
  11729. 9:18:05pizza let's say that we get the file
  11730. 9:18:07path wrong instead of desktop I'll
  11731. 9:18:10misspell this as
  11732. 9:18:12deskto run this again we counter that
  11733. 9:18:15exception could not write file that's
  11734. 9:18:19because Java couldn't locate this file
  11735. 9:18:20path I misspelled it now when handling
  11736. 9:18:23exceptions it is best practice to catch
  11737. 9:18:26specific types of exceptions first here
  11738. 9:18:29we're relying solely on our safety net
  11739. 9:18:31that catches all input output exceptions
  11740. 9:18:34another kind of exception that we can
  11741. 9:18:35catch is the
  11742. 9:18:37following file not
  11743. 9:18:41found exception we will give this a
  11744. 9:18:44nickname of e then we do need to import
  11745. 9:18:47this class
  11746. 9:18:49java.io file not found
  11747. 9:18:51exception this this is if we can't
  11748. 9:18:53locate a file or a file path is
  11749. 9:18:56invalid so let's output the
  11750. 9:18:59following could not locate file location
  11751. 9:19:05let's try that again remember that my
  11752. 9:19:08file path is
  11753. 9:19:10misspelled could not locate file
  11754. 9:19:14location let's spell this correctly this
  11755. 9:19:16time I'm going to delete this
  11756. 9:19:19file maybe I'll add another line of text
  11757. 9:19:23I like
  11758. 9:19:24pizza it's really good okay let's try
  11759. 9:19:28that
  11760. 9:19:30again file has been written going to my
  11761. 9:19:33desktop here's that file and it says I
  11762. 9:19:37like pizza it's really
  11763. 9:19:40good for better organization what you
  11764. 9:19:43could do is create a string a file path
  11765. 9:19:47and set that equal to a
  11766. 9:19:50string we're going to cut everything
  11767. 9:19:53within our file
  11768. 9:19:55writer and paste it within the string
  11769. 9:19:58we're going to assign the string of text
  11770. 9:20:00equal to a string variable a file path
  11771. 9:20:03then we'll pass in the string variable
  11772. 9:20:05to the file
  11773. 9:20:07writer and instead of passing in a
  11774. 9:20:09string literal to the right method of a
  11775. 9:20:11file writer for better organization
  11776. 9:20:14let's create a string of let's say text
  11777. 9:20:19content equals and then we'll cut our
  11778. 9:20:28text and then pass in the text
  11779. 9:20:32content let's see I'll add another line
  11780. 9:20:34I like pizza it's really
  11781. 9:20:36good buy me
  11782. 9:20:40pizza okay let's run this
  11783. 9:20:43again file has been written go to our
  11784. 9:20:46test file and here's our test file I
  11785. 9:20:49like pizza it's really good buy me pizza
  11786. 9:20:53if you have a string that takes up
  11787. 9:20:55multiple lines another option is to
  11788. 9:20:57write a multi-line string by using a
  11789. 9:21:00triple set of double quotes So This is
  11790. 9:21:03if you have a lot of
  11791. 9:21:05text this time I'm going to write a
  11792. 9:21:08poem roses are
  11793. 9:21:11red violets are
  11794. 9:21:14blue booty booty
  11795. 9:21:19booty rocking everywhere
  11796. 9:21:23all right so this is a multi-line string
  11797. 9:21:26you use a triple set of double
  11798. 9:21:28quotes that's if you have a lot of text
  11799. 9:21:30to write okay let's test it
  11800. 9:21:33again file has been written let's go to
  11801. 9:21:36our desktop open this file and here's
  11802. 9:21:39our poem roses are red violets are blue
  11803. 9:21:42booty booty booty rocking
  11804. 9:21:45everywhere all right everybody so that
  11805. 9:21:47is how to write a file using Java to
  11806. 9:21:49write small or mediumsized textt files
  11807. 9:21:51you can use a file right writer but you
  11808. 9:21:53will need a try and catch block and well
  11809. 9:21:55everybody that's how to write a file
  11810. 9:21:57using Java hey everybody in this video
  11811. 9:21:59I'm going to show you how we can read a
  11812. 9:22:01file using Java after doing some
  11813. 9:22:03research there's three popular options
  11814. 9:22:06the first and which I'll demonstrate in
  11815. 9:22:08this video is to combine a buffered
  11816. 9:22:10reader and a file reader both together
  11817. 9:22:13this technique is best for reading text
  11818. 9:22:15files line by line buffered reader can't
  11819. 9:22:17read a file by itself you can say that
  11820. 9:22:20it acts as a middleman between the
  11821. 9:22:21program and the file system it helps us
  11822. 9:22:23read files more efficiently and a file
  11823. 9:22:25reader is what actually reads the file
  11824. 9:22:28so we'll be using these two together
  11825. 9:22:29which I will demonstrate momentarily
  11826. 9:22:31another technique is to use a file input
  11827. 9:22:34stream which is best for binary files
  11828. 9:22:36such as images and audio files then
  11829. 9:22:39there's also Random Access file that's
  11830. 9:22:41best for reading or writing specific
  11831. 9:22:43portions of a large file so use this if
  11832. 9:22:46you need to read something specific from
  11833. 9:22:48a big file in today's video we're going
  11834. 9:22:50to combine a buffered reader and a file
  11835. 9:22:52reader together but first we need a file
  11836. 9:22:54to work with pick a location where you
  11837. 9:22:56would like to read this file from for me
  11838. 9:22:59I'm going to create a file on my desktop
  11839. 9:23:01I will create a new text document I'll
  11840. 9:23:05name this
  11841. 9:23:06test and what should our file say think
  11842. 9:23:09of something to write so there's a poem
  11843. 9:23:11that I like to use roses are
  11844. 9:23:14red violets are
  11845. 9:23:16blue booty booty
  11846. 9:23:20booty rocking
  11847. 9:23:25everywhere we will need the name of this
  11848. 9:23:27file including the file extension I'm
  11849. 9:23:29going to right click go to
  11850. 9:23:32properties this file is a plain text
  11851. 9:23:34file a txt file I'm going to copy this
  11852. 9:23:40location I will create a string variable
  11853. 9:23:43named file path equals then within a set
  11854. 9:23:47of quotes cuz we're working with strings
  11855. 9:23:50I will paste that file path and include
  11856. 9:23:52the file name including the extension
  11857. 9:23:55test.txt
  11858. 9:23:57now we're going to be combining a
  11859. 9:23:59buffered reader and a file reader
  11860. 9:24:01together first we will create a buffered
  11861. 9:24:03reader
  11862. 9:24:04object
  11863. 9:24:06buffered reader we'll name this reader
  11864. 9:24:10equals new buffer
  11865. 9:24:14reader we will need to import this
  11866. 9:24:18class import
  11867. 9:24:20java.io buffered reader
  11868. 9:24:23we can't create a buffered reader object
  11869. 9:24:26without first passing in a reader object
  11870. 9:24:29which is why we'll be combining this
  11871. 9:24:30with a file reader object within the
  11872. 9:24:32Constructor we'll be passing in an
  11873. 9:24:34object a new file reader
  11874. 9:24:38object and then we do need to import
  11875. 9:24:40this class
  11876. 9:24:41too import java.io file reader within
  11877. 9:24:46the Constructor of our file reader we're
  11878. 9:24:49going to pass in that file path that
  11879. 9:24:51string
  11880. 9:24:53when reading and writing files you'll
  11881. 9:24:54need try and catch blocks you can see
  11882. 9:24:57that Java is already giving us a warning
  11883. 9:24:59when working with files it's considered
  11884. 9:25:01dangerous code because you don't know if
  11885. 9:25:03you'll actually locate that file or not
  11886. 9:25:05or reading or writing that file might be
  11887. 9:25:06unsuccessful for some reason we will use
  11888. 9:25:09a try and catch block try this dangerous
  11889. 9:25:11code catch any
  11890. 9:25:13exceptions with modern Java you can use
  11891. 9:25:16with
  11892. 9:25:17resources we will add a set of
  11893. 9:25:19parentheses after
  11894. 9:25:20try copy the snippet of code where we
  11895. 9:25:23create a buffered reader
  11896. 9:25:26object and then paste it within that set
  11897. 9:25:28of parentheses remove the semicolon when
  11898. 9:25:30using try with resources you'll
  11899. 9:25:32automatically close this reader when
  11900. 9:25:34you're done with it we're going to catch
  11901. 9:25:36any file not found exceptions within the
  11902. 9:25:39catch
  11903. 9:25:40block so catch file not found exception
  11904. 9:25:44that's the type we will give this
  11905. 9:25:46exception a nickname of e within the
  11906. 9:25:48parameter and we do need to import this
  11907. 9:25:50class as well import java.io file not
  11908. 9:25:55found
  11909. 9:25:56exception if for some reason we can't
  11910. 9:25:58locate this file at this file location
  11911. 9:26:00let's output the
  11912. 9:26:02following could not locate
  11913. 9:26:07file to catch all other input output
  11914. 9:26:10exceptions We'll add another catch block
  11915. 9:26:12this will act as a safety
  11916. 9:26:15net we will catch any
  11917. 9:26:17IO exceptions IO exception we will give
  11918. 9:26:21it a nickname name of
  11919. 9:26:23e this is where we'll say something went
  11920. 9:26:28wrong and import this class as
  11921. 9:26:31well just temporarily within our Tri
  11922. 9:26:33block I'm going to Output that file
  11923. 9:26:38exists so let's test
  11924. 9:26:41it that file
  11925. 9:26:43exists so that file is at this file
  11926. 9:26:46location on my
  11927. 9:26:49desktop for some reason if we can't
  11928. 9:26:51locate that file let's say that I
  11929. 9:26:54misspell desktop as deskto run this
  11930. 9:26:58again well we get that exception that
  11931. 9:27:00file not found exception could not
  11932. 9:27:02locate
  11933. 9:27:04file let's make sure the file path is
  11934. 9:27:07correct now here's how to actually read
  11935. 9:27:09the file within our Tri block let's
  11936. 9:27:12create a local variable of line we'll be
  11937. 9:27:14reading our file line by
  11938. 9:27:20line since we're reading the file line
  11939. 9:27:22by line we'll be using a while
  11940. 9:27:25loop during each cycle of the while loop
  11941. 9:27:28we're going to write the following
  11942. 9:27:29within a set of parentheses we're going
  11943. 9:27:31to set our string of line equal to use
  11944. 9:27:35our buffered reader object call the read
  11945. 9:27:39line method we're taking our reader
  11946. 9:27:42reading the current line this will
  11947. 9:27:44return a string which we're assigning to
  11948. 9:27:46the string variable of line after
  11949. 9:27:49reading one line our reader is going to
  11950. 9:27:51point to the next line however if we run
  11951. 9:27:53out of lines the readline method is
  11952. 9:27:56going to return null so we're going to
  11953. 9:27:58continue reading lines while line does
  11954. 9:28:01not equal a value of null use the reader
  11955. 9:28:05read each line assign it to this
  11956. 9:28:07variable of line if we run out of lines
  11957. 9:28:10if this is null we'll escape the while
  11958. 9:28:12loop during each cycle of the while loop
  11959. 9:28:15we'll output each line it's going to be
  11960. 9:28:17a string and now this should work let's
  11961. 9:28:20test it
  11962. 9:28:24and here's our poem roses are red
  11963. 9:28:26violets are blue booty booty booty
  11964. 9:28:28rocking everywhere so that's one way in
  11965. 9:28:31which you can read a file using Java you
  11966. 9:28:33can combine a buffered reader and a file
  11967. 9:28:35reader it's best for reading text files
  11968. 9:28:38line by line but there's other methods
  11969. 9:28:40you should be aware of too such as file
  11970. 9:28:42input stream and random access file I'm
  11971. 9:28:44not sure if I'll make videos on these
  11972. 9:28:46yet and well everybody that's how to
  11973. 9:28:48read a file using Java hey everybody in
  11974. 9:28:51this video we're going to create an
  11975. 9:28:52audio player using
  11976. 9:28:54[Music]
  11977. 9:28:57Java let's get started all right let's
  11978. 9:29:00get started everybody now in this
  11979. 9:29:01following project what I'm about to show
  11980. 9:29:03you isn't compatible with MP3 files but
  11981. 9:29:06you could easily convert an MP3 file to
  11982. 9:29:08a wave file you can do that online
  11983. 9:29:11otherwise to play an MP3 file you'll
  11984. 9:29:12need an external library or framework
  11985. 9:29:14such as Java FX and I do have another
  11986. 9:29:17video on that if you need some sample of
  11987. 9:29:19music you can always use YouTube's audio
  11988. 9:29:21library these songs are free to use as
  11989. 9:29:23long as they're within the context of
  11990. 9:29:25YouTube you could search for songs find
  11991. 9:29:28one that you like and download
  11992. 9:29:31it this is the song I picked it's
  11993. 9:29:34currently an MP3 file I'll need to
  11994. 9:29:36convert it to a wave file you can easily
  11995. 9:29:38look up an mp3 to wave
  11996. 9:29:41converter select a
  11997. 9:29:47file convert to
  11998. 9:29:50wave convert
  11999. 9:29:54download for convenience I'm going to
  12000. 9:29:57put this on my
  12001. 9:29:59desktop I'll delete the MP3
  12002. 9:30:02file and we now have a wave file to make
  12003. 9:30:05this even more convenient I'll place
  12004. 9:30:07this wave file within my source
  12005. 9:30:11folder so that I don't need to use an
  12006. 9:30:14absolute file path so we're ready to
  12007. 9:30:16begin once we have a wave file we're
  12008. 9:30:18going to create a string for our file
  12009. 9:30:20path
  12010. 9:30:22you can use an absolute file path or
  12011. 9:30:24relative file path since this wave file
  12012. 9:30:27is right next to my main Java file I can
  12013. 9:30:29use a relative file path easily we're
  12014. 9:30:32going to copy the name of this file
  12015. 9:30:34we're going to copy path file name and
  12016. 9:30:39then paste it within the string the song
  12017. 9:30:41that I picked is a caring friend and
  12018. 9:30:43this is a wave file be sure to include
  12019. 9:30:45the extension too once we have our file
  12020. 9:30:47path we'll create a file object but we
  12021. 9:30:51have to pass in our file file path as an
  12022. 9:30:52argument to create a file object file
  12023. 9:30:56file equals new
  12024. 9:30:58file then within the Constructor we will
  12025. 9:31:01pass in our file path to the file
  12026. 9:31:03Constructor then be sure to import this
  12027. 9:31:06class for
  12028. 9:31:07file when handling files it's considered
  12029. 9:31:10dangerous code because accessing files
  12030. 9:31:12can be unpredictable we'll use a try and
  12031. 9:31:14catch
  12032. 9:31:15block try this dangerous code catch any
  12033. 9:31:19exceptions one exception that we may run
  12034. 9:31:21into
  12035. 9:31:22is an IO exception we'll give it a
  12036. 9:31:25nickname of e then be sure to import
  12037. 9:31:28this
  12038. 9:31:29class this catch statement will act as a
  12039. 9:31:31safety net to catch any unexpected IO
  12040. 9:31:34exceptions here we'll output something
  12041. 9:31:37went wrong it is better to handle
  12042. 9:31:40specific kinds of exceptions first we
  12043. 9:31:42will do that momentarily within the tri
  12044. 9:31:45block we will create an audio input
  12045. 9:31:47stream object that's the first step to
  12046. 9:31:49playing audio the type of object is
  12047. 9:31:53audio input
  12048. 9:31:55stream let's name this object just audio
  12049. 9:31:59stream
  12050. 9:32:01equals we will access the audio system
  12051. 9:32:05class we're accessing it statically so
  12052. 9:32:08we type the name of the class call the
  12053. 9:32:11get audio let me zoom out a little get
  12054. 9:32:15audio input stream
  12055. 9:32:20method and then we will we'll pass in
  12056. 9:32:22our file
  12057. 9:32:24object be sure to import this class too
  12058. 9:32:26we'll be doing a lot of importing so get
  12059. 9:32:28used to it now with the get audio input
  12060. 9:32:31stream method we have to add a catch
  12061. 9:32:34clause for any unsupported audio file
  12062. 9:32:37exceptions well we can write a catch
  12063. 9:32:38block for
  12064. 9:32:41that catch the following exception type
  12065. 9:32:44where the type is unsupported audio file
  12066. 9:32:48exception this is if somebody tries to
  12067. 9:32:50use an audio file that's not imported
  12068. 9:32:52not one of the following we will give
  12069. 9:32:54this a nickname of e import this class
  12070. 9:32:58then we'll output the
  12071. 9:33:01following audio file is not
  12072. 9:33:06supported be sure to import the audio
  12073. 9:33:09input stream class
  12074. 9:33:13too now when you open this audio stream
  12075. 9:33:16you do want to close it so in modern
  12076. 9:33:18Java you can use try with resources add
  12077. 9:33:21a of parentheses after try we will cut
  12078. 9:33:24the snippet of
  12079. 9:33:25code and paste it within the set of
  12080. 9:33:28parentheses delete the
  12081. 9:33:31semicolon if you use triy with resources
  12082. 9:33:34you'll automatically close this object
  12083. 9:33:36when you're done with it you don't need
  12084. 9:33:37that finally Clause so now we're going
  12085. 9:33:40to create a clip
  12086. 9:33:42object clip clip equals access the audio
  12087. 9:33:48system class we're doing it
  12088. 9:33:50statically call the get clip method
  12089. 9:33:54import this class for clip in simple
  12090. 9:33:57terms a clip is like a music or Sound
  12091. 9:34:00Player it allows you to load an audio
  12092. 9:34:02file and then play pause stop or reset
  12093. 9:34:04the audio so it gives you some
  12094. 9:34:07controls and we do need a catch clause
  12095. 9:34:09for this too for any line unavailable
  12096. 9:34:13exceptions so let's add that catch
  12097. 9:34:16clause in case we encounter a line
  12098. 9:34:20unavailable exception
  12099. 9:34:22which we will nickname e you might
  12100. 9:34:24encounter this exception if another
  12101. 9:34:26resource is trying to access that audio
  12102. 9:34:28file or if it's unplayable for some
  12103. 9:34:30reason it's
  12104. 9:34:31unavailable we could output something
  12105. 9:34:34such
  12106. 9:34:35as unable to access audio
  12107. 9:34:41resource then import this class as well
  12108. 9:34:45there we
  12109. 9:34:46go once we have our clip it gives us
  12110. 9:34:49some controls but we do have to use it
  12111. 9:34:51to open the audio stream
  12112. 9:34:54object we will take our clip object call
  12113. 9:34:58the open method and then pass in our
  12114. 9:35:01audio stream
  12115. 9:35:03object the clip object which is our
  12116. 9:35:06player is going to open our audio
  12117. 9:35:09stream now we're not going to play it
  12118. 9:35:11quite yet before we actually do play it
  12119. 9:35:13I'm going to add another catch
  12120. 9:35:16Clause we're going to catch any file not
  12121. 9:35:18found exceptions if we can't locate our
  12122. 9:35:20audio file file not
  12123. 9:35:24found exception of
  12124. 9:35:28e will
  12125. 9:35:30output could not
  12126. 9:35:33locate file import this class
  12127. 9:35:38two so we have four catch blocks the
  12128. 9:35:41last one acts as a safety net now let's
  12129. 9:35:44be sure that we can locate this file
  12130. 9:35:45first so
  12131. 9:35:48temporarily let's output no problems
  12132. 9:35:53detected I'm going to run this
  12133. 9:35:56program could not locate
  12134. 9:36:00file if that's the case with our
  12135. 9:36:02relative file path we're going to access
  12136. 9:36:04the source folder then access that audio
  12137. 9:36:09file there it is no problems
  12138. 9:36:13detected if this file is unsupported
  12139. 9:36:15let's say it's MP3 I'll bring back my
  12140. 9:36:18MP3 file move it to my source folder
  12141. 9:36:27then I'll switch this to
  12142. 9:36:29MP3 then we should get an unsupported
  12143. 9:36:31audio file exception if that's the
  12144. 9:36:33case yes audio file is not supported
  12145. 9:36:37because we're trying to use an MP3 file
  12146. 9:36:39so let's switch that back to wave or
  12147. 9:36:42whatever yours was
  12148. 9:36:43originally and delete our MP3 file we
  12149. 9:36:46were just testing it all right now we're
  12150. 9:36:48actually going to play the clip
  12151. 9:36:52now one last thing that I'm going to add
  12152. 9:36:54it's optional I'm going to add a finally
  12153. 9:36:57block once we're done with this program
  12154. 9:36:59we can output the following let's just
  12155. 9:37:01say
  12156. 9:37:03by this will make sense in just a moment
  12157. 9:37:05why I'm doing this there's one problem
  12158. 9:37:07we're going to encounter when we play
  12159. 9:37:08our audio now to actually play the audio
  12160. 9:37:12we're going to take our clip
  12161. 9:37:14object call the start method to start
  12162. 9:37:17the
  12163. 9:37:18audio here's what happens when I run
  12164. 9:37:20this currently
  12165. 9:37:25the music may play for just a brief
  12166. 9:37:26second we print by then exit the program
  12167. 9:37:29right away the issue that we're running
  12168. 9:37:31into is that our program doesn't wait
  12169. 9:37:33for the audio to finish once we start
  12170. 9:37:35the clip we immediately end the
  12171. 9:37:38program so we need some way to keep the
  12172. 9:37:40clip open and there's a bunch of
  12173. 9:37:42different options you could use
  12174. 9:37:43threading within a while loop to check
  12175. 9:37:45to see if the clip is still running but
  12176. 9:37:47I haven't taught that yet so here's
  12177. 9:37:49another option we create a prompt for
  12178. 9:37:52the user to play stop reset or quit the
  12179. 9:37:55audio program the audio will continue
  12180. 9:37:58playing unless the user decides to stop
  12181. 9:38:00or quit the program so we'll need a
  12182. 9:38:02scanner to work with to accept user
  12183. 9:38:04input scanner scanner equals new scanner
  12184. 9:38:09pass in
  12185. 9:38:12system.in import this
  12186. 9:38:15class now if you open a scanner you do
  12187. 9:38:17want to close it when you're done with
  12188. 9:38:19it you can do that within the final
  12189. 9:38:21finally block like this scanner.
  12190. 9:38:24close with modern Java you can use try
  12191. 9:38:28with resources which will automatically
  12192. 9:38:30close any resources and we can do that
  12193. 9:38:32with our scanner too let's cut our
  12194. 9:38:35scanner paste it within the tri block
  12195. 9:38:38now be sure to include the
  12196. 9:38:41semicolon our scanner is going to
  12197. 9:38:43automatically close when we're done with
  12198. 9:38:44this program because we're using try
  12199. 9:38:46with resources you can use try with
  12200. 9:38:49resources if an object implements the
  12201. 9:38:51autoc closable interface scanner and
  12202. 9:38:54audio stream both do clip doesn't so
  12203. 9:38:56we'll keep that within the tri block
  12204. 9:38:59rather than start our clip let's create
  12205. 9:39:01a string variable of
  12206. 9:39:03response we'll create a while
  12207. 9:39:07loop while our
  12208. 9:39:11response
  12209. 9:39:13equals let's say capital Q now we want
  12210. 9:39:16to say while our response doesn't equal
  12211. 9:39:19capital Q we'll use the Notch logical
  12212. 9:39:22operator and to make our while loop
  12213. 9:39:24happy we do have to initialize our
  12214. 9:39:26response we'll set that to be an empty
  12215. 9:39:28string so a user is going to type in a
  12216. 9:39:31response as long as it's not Q meaning
  12217. 9:39:33quit we'll continue the
  12218. 9:39:35program within the while loop we'll give
  12219. 9:39:38a user a few
  12220. 9:39:39options let's output the
  12221. 9:39:42following let's say p equals
  12222. 9:39:46play Let's copy this print line
  12223. 9:39:49statement s equals
  12224. 9:39:53stop R is to
  12225. 9:39:57reset and Q is to
  12226. 9:40:04quit then we will ask a user to enter
  12227. 9:40:08your choice I'll use print rather than
  12228. 9:40:11print line to keep the user input on the
  12229. 9:40:13same
  12230. 9:40:15line we'll take our response which is a
  12231. 9:40:18string set it equal to use our scanner
  12232. 9:40:21get the next character by calling the
  12233. 9:40:23next method in case a user types in a
  12234. 9:40:26lowercase character we can make it
  12235. 9:40:28uppercase by Method chaining the two
  12236. 9:40:31uppercase
  12237. 9:40:32method all right we'll do a test run and
  12238. 9:40:36let's be sure that we start the clip
  12239. 9:40:38just temporarily clip.art method what's
  12240. 9:40:42likely going to happen is that we'll
  12241. 9:40:43start the audio clip and it won't stop
  12242. 9:40:46until we press Q to quit
  12243. 9:40:52so here's the audio hopefully you can
  12244. 9:40:54hear me once I press Q the program's
  12245. 9:40:58going to
  12246. 9:40:59stop it's because the program closes
  12247. 9:41:01down and
  12248. 9:41:03exits now we want to give the user a few
  12249. 9:41:06options after they type in their
  12250. 9:41:08response we can use an enhanced switch
  12251. 9:41:11for this let's also be sure that we
  12252. 9:41:13delete the start method of our
  12253. 9:41:17clip we don't want the audio clip to
  12254. 9:41:19start until the user presses p to play
  12255. 9:41:22we will create a
  12256. 9:41:24switch we will examine our
  12257. 9:41:27response does our response match any
  12258. 9:41:31cases if our response matches a case of
  12259. 9:41:35P we'll write an arrow to do the
  12260. 9:41:37following take our clip then call the
  12261. 9:41:40start
  12262. 9:41:42method if our response matches a case of
  12263. 9:41:45s that means stop take our clip object
  12264. 9:41:49called the stop method
  12265. 9:41:52if our response matches a case of R do
  12266. 9:41:55the following take our clip to reset
  12267. 9:41:58you're going to call the set
  12268. 9:42:01micro
  12269. 9:42:03second
  12270. 9:42:05position then pass in
  12271. 9:42:07zero do pay attention to the
  12272. 9:42:10capitalization s is a
  12273. 9:42:13lowercase if our response matches a case
  12274. 9:42:16of Q take our clip call the close method
  12275. 9:42:20to close the clip
  12276. 9:42:22then if there's no matching cases let's
  12277. 9:42:24output the
  12278. 9:42:25following system.out.print line invalid
  12279. 9:42:31Choice all right and that should be
  12280. 9:42:37everything enter your choice just to be
  12281. 9:42:40sure that the default case is working
  12282. 9:42:42I'm going to type
  12283. 9:42:44Taco invalid Choice we'll press P to
  12284. 9:42:48play
  12285. 9:42:52s to
  12286. 9:42:54stop P to play
  12287. 9:42:57again R to
  12288. 9:43:01reset and Q to
  12289. 9:43:03[Music]
  12290. 9:43:05quit all right everybody that is an
  12291. 9:43:07audio player that you can write using
  12292. 9:43:09Java what's up everybody in this video
  12293. 9:43:12we're going to create a working game of
  12294. 9:43:13hangman using Java a user is going to
  12295. 9:43:16guess certain characters and we'll see
  12296. 9:43:17if there's a match within a secret word
  12297. 9:43:19near the end of this video topic will be
  12298. 9:43:22importing a file of words and one of
  12299. 9:43:24those words will be secretly selected so
  12300. 9:43:26let's get started all right let's get
  12301. 9:43:28started everybody near the end of this
  12302. 9:43:30video we'll have our program choose a
  12303. 9:43:32random word from a list of words but to
  12304. 9:43:34keep this program simple in the
  12305. 9:43:35beginning for a testing purposes let's
  12306. 9:43:38create a string variable of word here
  12307. 9:43:41we'll select a word that we're going to
  12308. 9:43:42use throughout the program just for
  12309. 9:43:44testing pick a word for me I'll say you
  12310. 9:43:46know what let's go with pizza so this
  12311. 9:43:48will be my word so we have our word
  12312. 9:43:51we'll be accepting user input so we need
  12313. 9:43:53a scanner scanner scanner equals new
  12314. 9:43:57scanner pass in
  12315. 9:44:00system.in import the scanner class and
  12316. 9:44:04close your scanner at the end of the
  12317. 9:44:05program scanner.
  12318. 9:44:09close here's where we'll be working with
  12319. 9:44:11array lists we will create an array list
  12320. 9:44:14of characters to do that we type array
  12321. 9:44:17list then specify the type within a set
  12322. 9:44:19of angle brackets we can't use
  12323. 9:44:21Primitives such as Char we'll have to
  12324. 9:44:23use the rapper class of character to
  12325. 9:44:26store characters I will name this array
  12326. 9:44:28list word State equals a
  12327. 9:44:33new array list use the diamond operator
  12328. 9:44:37set of parenthesis semicolon we'll need
  12329. 9:44:40to import this
  12330. 9:44:43class now within the diamond operator
  12331. 9:44:45you don't need to list character again
  12332. 9:44:47even though that does work Java can
  12333. 9:44:49infer what the type is in the beginning
  12334. 9:44:52our word state is going to contain a
  12335. 9:44:54bunch of underscores and we're going to
  12336. 9:44:55fill them in one letter at a time by
  12337. 9:44:57guessing we'll also need a variable to
  12338. 9:45:00keep track of our wrong guesses int
  12339. 9:45:02wrong
  12340. 9:45:03guesses this will initially be zero
  12341. 9:45:07we'll set that equal to be
  12342. 9:45:09zero within our array of word State
  12343. 9:45:12we're going to add an underscore for
  12344. 9:45:14every letter within our word we'll
  12345. 9:45:16create a for Loop that will cycle once
  12346. 9:45:18for every letter in our word here's how
  12347. 9:45:20we can do that
  12348. 9:45:22so we'll start with a basic for Loop
  12349. 9:45:24create an INT of I for index equals 0
  12350. 9:45:29we'll continue as long as I is less than
  12351. 9:45:32take our word and strings have a
  12352. 9:45:34built-in length method return the length
  12353. 9:45:37of this word so in this case it's going
  12354. 9:45:39to be five because there's five letters
  12355. 9:45:41in this word then increment I by One
  12356. 9:45:45during each cycle of this for loop we're
  12357. 9:45:47going to take our array of word State
  12358. 9:45:50and and add a character by using the add
  12359. 9:45:52method we will pass in an underscore an
  12360. 9:45:56underscore character be sure you're
  12361. 9:45:58using single quotes not double quotes
  12362. 9:46:01because these are chars technically not
  12363. 9:46:03strings all right let's do a test run
  12364. 9:46:05I'm going to Output our word state to
  12365. 9:46:08see what we have so we should get 5core
  12366. 9:46:12characters and we do one 2 3 four five p
  12367. 9:46:19i z z a throughout this game by guessing
  12368. 9:46:23letters we'll be filling in this word
  12369. 9:46:24state by replacing the underscores if
  12370. 9:46:26there's a
  12371. 9:46:28match let's change this word just so we
  12372. 9:46:30know that everything's working I will
  12373. 9:46:32pick watermelon because that's the first
  12374. 9:46:34thing that came to mind so watermelon
  12375. 9:46:37has 10
  12376. 9:46:38characters 1 2 3 4 5 6 7 8 9 10 all
  12377. 9:46:44right we know that that works I will
  12378. 9:46:46switch it back to Pizza delete this
  12379. 9:46:49print statement and we can move
  12380. 9:46:51on word state is an array list made up
  12381. 9:46:55of characters currently they're all
  12382. 9:46:57underscores but we'll be replacing each
  12383. 9:46:59with the character if we get the correct
  12384. 9:47:01guess after the for Loop why don't we
  12385. 9:47:03create a welcome
  12386. 9:47:04message let's say welcome to Java
  12387. 9:47:10hangman now you don't need to do this
  12388. 9:47:13but I'm going to add some decorators
  12389. 9:47:15just a bunch of asterisks before and
  12390. 9:47:18after again you don't need to but I
  12391. 9:47:20think it looks
  12392. 9:47:24cool let's make sure everything looks
  12393. 9:47:27okay welcome to Java
  12394. 9:47:32hangman for our hangman we're going to
  12395. 9:47:34display some asky art outside of our
  12396. 9:47:37main method we're going to declare
  12397. 9:47:38another method that's in charge of
  12398. 9:47:40returning asky art based on the number
  12399. 9:47:42of wrong guesses that we have this will
  12400. 9:47:44be a static method it's going to return
  12401. 9:47:47a string we'll name this method get
  12402. 9:47:51hangman
  12403. 9:47:53art we will have one parameter we have
  12404. 9:47:56to pass in how many wrong guesses we
  12405. 9:47:59have then depending on the number of
  12406. 9:48:01wrong guesses we'll return a certain
  12407. 9:48:03image represented as a
  12408. 9:48:06string within our get hangman art method
  12409. 9:48:09we'll be returning a string a string
  12410. 9:48:11representation of our asky art we can
  12411. 9:48:14best do this with the switch now a
  12412. 9:48:16switch can return something we'll use
  12413. 9:48:18the return keyword create a
  12414. 9:48:22switch end it with a semicolon within
  12415. 9:48:25our switch what are we examining we're
  12416. 9:48:28examining the number of wrong guesses
  12417. 9:48:30this will be a number 0 through 6 we'll
  12418. 9:48:33need a case for each of those numbers
  12419. 9:48:35for each of those guesses depending on
  12420. 9:48:37how many wrong guesses the user has
  12421. 9:48:39we'll display one of a few images so
  12422. 9:48:41here we'll be working on our art skills
  12423. 9:48:43today too if we have a matching case
  12424. 9:48:46where wrong guesses matches a case of
  12425. 9:48:48zero that means they have no wrong
  12426. 9:48:50guesses
  12427. 9:48:52case0 Arrow meaning do this will return
  12428. 9:48:55the following in Java you can write a
  12429. 9:48:57multi-line string but you have to use a
  12430. 9:49:00triple set of double quotes and I'm just
  12431. 9:49:02going to line these
  12432. 9:49:04up within this triple set of double
  12433. 9:49:06quotes I'll have three empty lines
  12434. 9:49:08between
  12435. 9:49:10them we won't display anything if the
  12436. 9:49:12number of wrong guesses is
  12437. 9:49:14zero then for case one if a user has one
  12438. 9:49:18wrong
  12439. 9:49:18guess we'll display our man's
  12440. 9:49:22head represented by an
  12441. 9:49:25O if wrong guesses is two we can
  12442. 9:49:29represent that with case two we will
  12443. 9:49:31draw the man's
  12444. 9:49:33body represented by a vertical
  12445. 9:49:36bar case three will be the left
  12446. 9:49:41arm for the left arm we'll use a forward
  12447. 9:49:45slash for case four
  12448. 9:49:50that will be for the right arm we'll use
  12449. 9:49:52a backslash in Java a backslash is
  12450. 9:49:55actually an escape sequence to display a
  12451. 9:49:57backslash we have to use double back
  12452. 9:50:00slashes case five will be for the left
  12453. 9:50:07leg it's kind of like we're assembling
  12454. 9:50:09Exodia if you know the
  12455. 9:50:12reference and then k6 is for the right
  12456. 9:50:16leg we'll need two backs
  12457. 9:50:19slashes we'll want to a default case if
  12458. 9:50:21there's no matching
  12459. 9:50:22cases we'll just return an empty string
  12460. 9:50:25just to make Java happy all right and
  12461. 9:50:28this method is done based on the number
  12462. 9:50:31of wrong guesses we'll return one of a
  12463. 9:50:33few
  12464. 9:50:36strings Let's test it I'm going to use a
  12465. 9:50:40print line
  12466. 9:50:41statement call our get hangman art
  12467. 9:50:43method we'll start with zero zero wrong
  12468. 9:50:47guesses we should end up displaying
  12469. 9:50:49three empty lines
  12470. 9:50:52if wrong guesses is one we should
  12471. 9:50:55display The Hangman's
  12472. 9:50:57head
  12473. 9:51:00two two is for the
  12474. 9:51:03body three is for the left
  12475. 9:51:08arm
  12476. 9:51:11four is for the right
  12477. 9:51:14arm five is for the left leg
  12478. 9:51:20and six is the full
  12479. 9:51:24body that's when we lose when wrong
  12480. 9:51:26guesses is six all right we know that
  12481. 9:51:29the get hangman art method works I will
  12482. 9:51:31collapse it because we're done with it
  12483. 9:51:33to get the art for our hangman we just
  12484. 9:51:35have to call this method then pass in
  12485. 9:51:37the current number of wrong guesses
  12486. 9:51:40let's delete this print line statement
  12487. 9:51:41we no longer need
  12488. 9:51:43it now when we print our word
  12489. 9:51:46State let me show you
  12490. 9:51:50we get this ugly formatting where we
  12491. 9:51:53have a set of straight brackets then all
  12492. 9:51:55the underscores are comma separated we
  12493. 9:51:57can customize that so here's what we'll
  12494. 9:52:00do after our welcome message we'll
  12495. 9:52:02output the
  12496. 9:52:05following we'll output the word word
  12497. 9:52:10colon space I'll use print I want to
  12498. 9:52:14stay on the same line we'll create a for
  12499. 9:52:17Loop an enhanced for Loop
  12500. 9:52:21for every character in our array list of
  12501. 9:52:23word State we're going to display an
  12502. 9:52:25underscore with no
  12503. 9:52:27formatting using an enhanced for Loop we
  12504. 9:52:29have to list the data type of what we're
  12505. 9:52:31cycling through we're cycling through an
  12506. 9:52:33array of characters Char a common
  12507. 9:52:36convention with enhanced for Loops is to
  12508. 9:52:39name each letter C because we can't use
  12509. 9:52:41Char because that's a data
  12510. 9:52:44type and character is kind of long so C
  12511. 9:52:47is short for character for every
  12512. 9:52:49character of C
  12513. 9:52:51in our array list our array list is
  12514. 9:52:54named word
  12515. 9:52:56State we'll do the
  12516. 9:52:58following we will output our character
  12517. 9:53:01of C plus a
  12518. 9:53:04space so then when we output
  12519. 9:53:07this oh let's use print rather than
  12520. 9:53:10print line because we want them all in
  12521. 9:53:12the same
  12522. 9:53:15line that's much better that output
  12523. 9:53:18looks a lot nicer each letter letter is
  12524. 9:53:21an underscore they're all separated with
  12525. 9:53:23a space when we guess each letter we'll
  12526. 9:53:26be flipping these underscores to its
  12527. 9:53:28matching character if they do
  12528. 9:53:30match I'll add an empty print line just
  12529. 9:53:32so we go down to the next
  12530. 9:53:35line okay for the next part we'll have a
  12531. 9:53:38user guess a letter we'll need some user
  12532. 9:53:41input we'll need a
  12533. 9:53:44prompt guess a letter colon space I'll
  12534. 9:53:48use print not print line
  12535. 9:53:51we'll create a variable of guess guess
  12536. 9:53:53will be a
  12537. 9:53:54letter we'll need to accept user input
  12538. 9:53:57we'll need to accept user input using
  12539. 9:53:59our scanner scanner. next next will get
  12540. 9:54:03the next
  12541. 9:54:05character now since we're working with
  12542. 9:54:07lowercase characters we can method chain
  12543. 9:54:10the two lowercase method just to make it
  12544. 9:54:12lowercase if somebody types in an
  12545. 9:54:14uppercase character to lower case
  12546. 9:54:20okay to lowercase method using next the
  12547. 9:54:24user input is going to be a string not a
  12548. 9:54:26character we can easily convert that to
  12549. 9:54:28a character by using the Char at
  12550. 9:54:31method but we have to pass it an index
  12551. 9:54:34we're only accepting a single character
  12552. 9:54:36so we'll use Char at zero we're telling
  12553. 9:54:39Java with the string that the user types
  12554. 9:54:41in give me just the first character and
  12555. 9:54:44convert it to be a Char so guess is
  12556. 9:54:47going to be a single letter it'll be of
  12557. 9:54:49the Char data type
  12558. 9:54:51all right then let's test it I will
  12559. 9:54:53output our
  12560. 9:54:57guess I will guess an uppercase
  12561. 9:55:00P we'll end up outputting a lowercase p
  12562. 9:55:03which is
  12563. 9:55:04good how about a z yep our guess is z if
  12564. 9:55:09I were to type in a full word like
  12565. 9:55:11pineapple we still end up with just the
  12566. 9:55:13first character the first character in
  12567. 9:55:15our
  12568. 9:55:16string which is good though let's delete
  12569. 9:55:19this print line statement we no longer
  12570. 9:55:20need it for this next part we're going
  12571. 9:55:23to check to see if our guess matches any
  12572. 9:55:26letters within our
  12573. 9:55:28word we'll use an if statement for
  12574. 9:55:31that if take our word it's a string in
  12575. 9:55:34this case Pizza we'll use the built-in
  12576. 9:55:37index of
  12577. 9:55:40method and then pass in our
  12578. 9:55:44guess so basically with the index of
  12579. 9:55:47method it's going to return the index
  12580. 9:55:49within the string of the first
  12581. 9:55:51occurrence of the specified
  12582. 9:55:54character for example let's say I guess
  12583. 9:55:56the letter Z well Z is in the word Pizza
  12584. 9:55:59it's at index 2 that's the first
  12585. 9:56:03occurrence if my guess was Z index of is
  12586. 9:56:06going to return the number two because
  12587. 9:56:08that's the first occurrence of Z we will
  12588. 9:56:10check if the first occurrence is greater
  12589. 9:56:13than or equal to zero that means there's
  12590. 9:56:15a matching letter if that's the
  12591. 9:56:17case if there's a matching letter well
  12592. 9:56:20will
  12593. 9:56:21output
  12594. 9:56:22correct guess and then I'm just going to
  12595. 9:56:26add a new line character to the
  12596. 9:56:28end all right let's do a test
  12597. 9:56:31run I will guess the letter Z that's a
  12598. 9:56:35correct
  12599. 9:56:37guess but are there any
  12600. 9:56:39X's there is not so we don't output
  12601. 9:56:43correct
  12602. 9:56:44guess if it's the wrong guess we can
  12603. 9:56:46output using an lse statement
  12604. 9:56:50wrong guess then I'll add a new line
  12605. 9:56:53character to the end let's try that
  12606. 9:56:56again I will guess an X wrong
  12607. 9:57:01guess there are no x's in the word
  12608. 9:57:05Pizza okay so within our if statement if
  12609. 9:57:07we get the correct guess we're going to
  12610. 9:57:09change our word state to reflect
  12611. 9:57:12it that's one less letter that the user
  12612. 9:57:14has to
  12613. 9:57:15guess we're going to cycle through the
  12614. 9:57:17length of our word using a for Loop for
  12615. 9:57:21in I equals 0 we'll continue as long as
  12616. 9:57:25I is less than our words length by
  12617. 9:57:29calling the length method then increment
  12618. 9:57:31I by
  12619. 9:57:33one we have to see where there's a match
  12620. 9:57:35exactly which index within our word
  12621. 9:57:38we'll use an if
  12622. 9:57:39statement we will take our word in this
  12623. 9:57:42case Pizza called the Char at method
  12624. 9:57:46then pass in our index of I during each
  12625. 9:57:49cycle I is going to increment but during
  12626. 9:57:51the first cycle it's going to be zero so
  12627. 9:57:54we're saying take our word in this
  12628. 9:57:56example Pizza give me the first
  12629. 9:57:58character so for pizza that would be the
  12630. 9:58:00letter
  12631. 9:58:02P is that equal to using the comparison
  12632. 9:58:05operator are
  12633. 9:58:07guess so let's say I do guess P the
  12634. 9:58:10first character of my word is P that
  12635. 9:58:13means that they match we're going to
  12636. 9:58:15take our array list of word State and
  12637. 9:58:19we're going to update it by using the
  12638. 9:58:21set
  12639. 9:58:22method we have to pass in an index and a
  12640. 9:58:25new value our
  12641. 9:58:29guess let's test
  12642. 9:58:31it so with the word Pizza are there any
  12643. 9:58:35z's correct guess oh then we have to
  12644. 9:58:38Output it
  12645. 9:58:39too we're going to continue playing as
  12646. 9:58:41long as wrong guesses is less than six
  12647. 9:58:45once wrong guesses is six or greater
  12648. 9:58:47then the game
  12649. 9:58:48ends so so let's cut this portion of
  12650. 9:58:52code cut it don't delete it we'll write
  12651. 9:58:55a while
  12652. 9:58:56loop
  12653. 9:58:58while our wrong guesses variable wrong
  12654. 9:59:02guesses is less than six we'll continue
  12655. 9:59:05playing this game within the while loop
  12656. 9:59:08we'll paste all that code again let's do
  12657. 9:59:11a test
  12658. 9:59:13run guess a letter I'm going to guess Z
  12659. 9:59:17so that should update our word to
  12660. 9:59:19reflect two Z's because my word is pizza
  12661. 9:59:22but if there's no matching
  12662. 9:59:23letters it doesn't get updated because
  12663. 9:59:26there's no matches let me fill this in p
  12664. 9:59:30i ZZ a and we get the full word all
  12665. 9:59:35right that's good so far so
  12666. 9:59:38good so we'll need to keep track of
  12667. 9:59:40wrong guesses within our else statement
  12668. 9:59:43we're going to take our variable of
  12669. 9:59:44wrong guesses incremented by one using
  12670. 9:59:48the increment operator
  12671. 9:59:51then back to the beginning of the loop
  12672. 9:59:53we're going to display our hangman art
  12673. 9:59:55by calling the get hangman art
  12674. 9:59:58method this method will return a string
  12675. 10:00:01so we can put that within a print
  12676. 10:00:03statement let's use print call the get
  12677. 10:00:07hangman art method then pass in the
  12678. 10:00:09number of wrong
  12679. 10:00:11guesses let's do another test
  12680. 10:00:14run we have three empty spaces because
  12681. 10:00:17we haven't displayed any parts of the
  12682. 10:00:18hangman yet I will guess the wrong
  12683. 10:00:20letter X we get the head of the hangman
  12684. 10:00:25I'll guess some more wrong
  12685. 10:00:26letters
  12686. 10:00:28CV b n
  12687. 10:00:34m at the end we'll have to display the
  12688. 10:00:36full hangman when we
  12689. 10:00:38lose let's display the right
  12690. 10:00:40letters p i z
  12691. 10:00:45a okay good what happens when we lose if
  12692. 10:00:49wrong guesses is six or
  12693. 10:00:53greater so outside of the while loop our
  12694. 10:00:56while loop ends right
  12695. 10:00:58here we'll write an if
  12696. 10:01:01statement if our variable of wrong
  12697. 10:01:04guesses is greater than or equal to
  12698. 10:01:07six okay we will again use a print
  12699. 10:01:11statement called the get hangman art
  12700. 10:01:14method and then pass in the number of
  12701. 10:01:17wrong
  12702. 10:01:18guesses we'll also display a game over
  12703. 10:01:22message game
  12704. 10:01:25over then display the
  12705. 10:01:28word the word
  12706. 10:01:31was colon space plus our original word
  12707. 10:01:36of
  12708. 10:01:37pizza let's do a test run
  12709. 10:01:40again I'm going to guess the wrong
  12710. 10:01:47characters all right game over we
  12711. 10:01:49display the the full hangman the word
  12712. 10:01:51was
  12713. 10:01:53pizza I think I'm going to make a few
  12714. 10:01:55changes I'm going to remove these new
  12715. 10:01:57line
  12716. 10:01:58characters it's a little too much space
  12717. 10:02:00I
  12718. 10:02:01think all right we're
  12719. 10:02:03good so how do we win let's add that
  12720. 10:02:07after the for
  12721. 10:02:08Loop if we get a correct guess we'll
  12722. 10:02:12check to see if our word State doesn't
  12723. 10:02:14contain any lowercase
  12724. 10:02:17characters so take our array list of
  12725. 10:02:19word
  12726. 10:02:22State call the contains
  12727. 10:02:25method and then pass in an underscore
  12728. 10:02:28character but we want to check to see if
  12729. 10:02:30our word State doesn't contain any
  12730. 10:02:32lowercase characters we can prefix this
  12731. 10:02:35with the not logical operator an
  12732. 10:02:37exclamation point if our word our word
  12733. 10:02:41State doesn't contain any lowercase
  12734. 10:02:43characters that means that we guessed
  12735. 10:02:45all the letters and we
  12736. 10:02:47win if that's true let's display our
  12737. 10:02:49hangman One Last Time
  12738. 10:02:52system.out.print get hangman art pass in
  12739. 10:02:55the number of wrong guesses and we'll
  12740. 10:02:59display you
  12741. 10:03:02win and display the word
  12742. 10:03:06again the word was colon space plus our
  12743. 10:03:12word then we'll want to break out of
  12744. 10:03:14this Loop cuz we're stuck in a while
  12745. 10:03:16loop we can use a break statement after
  12746. 10:03:18we win to break out of the
  12747. 10:03:22loop all right let's see what we're
  12748. 10:03:24working
  12749. 10:03:27with I'll guess a few wrong letters x c
  12750. 10:03:32v b n all right one more wrong guess and
  12751. 10:03:37I lose game over the word was
  12752. 10:03:40pizza all right I'll actually win this
  12753. 10:03:43time but I'll guess a few wrong
  12754. 10:03:44characters q
  12755. 10:03:47w t
  12756. 10:03:49I'll guess some correct characters a z i
  12757. 10:03:55p you win and we display our current
  12758. 10:03:58score the current state of our hangman
  12759. 10:04:01we display you win the word was
  12760. 10:04:04pizza in this next part of this video
  12761. 10:04:06we're going to create a more advanced
  12762. 10:04:08version of this program by importing a
  12763. 10:04:10file of words a word is going to be
  12764. 10:04:12randomly chosen from a file instead of
  12765. 10:04:15just setting one in the beginning okay
  12766. 10:04:17everybody in this next section we're
  12767. 10:04:19going to create a file that contains all
  12768. 10:04:21the words that we can possibly use for
  12769. 10:04:23this Hangman game within our project
  12770. 10:04:24folder why don't we create a new file
  12771. 10:04:27file new file I'm going to name this
  12772. 10:04:31file words and this will be a txt
  12773. 10:04:34file for all the different possible
  12774. 10:04:36words in this game place within this
  12775. 10:04:38file you can copy and paste them from
  12776. 10:04:40somewhere online if you would like just
  12777. 10:04:42to make reading this file a simple as
  12778. 10:04:44possible I'm going to list all the words
  12779. 10:04:45on a separate line so think of some
  12780. 10:04:47words or like I said copying paste them
  12781. 10:04:50from somewhere I'll just add several
  12782. 10:04:52Apple orange banana
  12783. 10:04:56coconut
  12784. 10:04:58pineapple grape lime lemon that's good
  12785. 10:05:05enough in my project folder I have this
  12786. 10:05:07file words.txt
  12787. 10:05:10temporarily with my game I'm going to
  12788. 10:05:13write a multiline comment and just
  12789. 10:05:15comment all of this out until we're
  12790. 10:05:17ready to bring it back in
  12791. 10:05:23at the top of my Hangman game I will
  12792. 10:05:26create a string named file
  12793. 10:05:28path this will contain either a relative
  12794. 10:05:31file path or an absolute file path since
  12795. 10:05:34this file is in my project folder I just
  12796. 10:05:36have to list the file name words.txt
  12797. 10:05:41we're going to read this file line by
  12798. 10:05:44line we'll store each word in an array
  12799. 10:05:46list we'll create a new array list AR
  12800. 10:05:49array list the data type is Going to Be
  12801. 10:05:52Strings so string I will name this array
  12802. 10:05:56list words equals new array list Diamond
  12803. 10:06:01operator parenthesis
  12804. 10:06:03semicolon now we'll actually need to
  12805. 10:06:05read this file reading files is
  12806. 10:06:08considered dangerous code because it's
  12807. 10:06:10highly likely that something can go
  12808. 10:06:11wrong we will write a try with resources
  12809. 10:06:14block we need that set of parentheses
  12810. 10:06:16after try we will catch any except
  12811. 10:06:21options when you try with resources
  12812. 10:06:23you'll automatically close any resources
  12813. 10:06:25listed within the set of parentheses
  12814. 10:06:27within the set of parentheses we will
  12815. 10:06:29create a buffered reader
  12816. 10:06:31object buffered reader which we will
  12817. 10:06:35name reader for short equals new
  12818. 10:06:38buffered
  12819. 10:06:42reader now a buffered reader by itself
  12820. 10:06:44can't read files we do have to pass in a
  12821. 10:06:47file reader to the Constructor
  12822. 10:06:49new file reader then within our file
  12823. 10:06:53reader Constructor we will pass in our
  12824. 10:06:56file path the location to that
  12825. 10:06:59file now our file reader object wants us
  12826. 10:07:02to catch any file not found
  12827. 10:07:04exceptions we'll place that within our
  12828. 10:07:06catch block file not found exception of
  12829. 10:07:10e then we'll output if we can't find
  12830. 10:07:12this
  12831. 10:07:13file could not find
  12832. 10:07:17file We'll add another catch block just
  12833. 10:07:19as a safety net we'll catch any
  12834. 10:07:23IO
  12835. 10:07:26exception e
  12836. 10:07:28exceptions this is where we'll say
  12837. 10:07:31something went wrong this just acts as a
  12838. 10:07:35safety net if there's something we don't
  12839. 10:07:37anticipate so we have our reader object
  12840. 10:07:40within the tri block we will create a
  12841. 10:07:42local variable it's a string of
  12842. 10:07:46line we will read this file line by line
  12843. 10:07:49using a while
  12844. 10:07:51loop while within a set of parentheses
  12845. 10:07:55assign our line equal to use our reader
  12846. 10:07:59called the read line
  12847. 10:08:02method if we run out of lines this is
  12848. 10:08:05going to return null while the value
  12849. 10:08:08that's returned does not equal null
  12850. 10:08:11we'll continue reading this file reader
  12851. 10:08:14is going to give us each of these words
  12852. 10:08:16line by line we're going to assign them
  12853. 10:08:19to our array list of words take our
  12854. 10:08:21array list of words call the add method
  12855. 10:08:24we will add the current
  12856. 10:08:27line if there's any white space after
  12857. 10:08:30these words this will pick that up and
  12858. 10:08:32our program will consider them to be
  12859. 10:08:34characters we will use the trim method
  12860. 10:08:37after line to trim that white
  12861. 10:08:41space let's perform a test run I'm going
  12862. 10:08:44to Output our array list of words
  12863. 10:08:49yep and here's our words all within an
  12864. 10:08:51array
  12865. 10:08:53list let's delete that line we're going
  12866. 10:08:56to choose one of these words at random
  12867. 10:08:59we'll need a random object random random
  12868. 10:09:02equals new
  12869. 10:09:05random import the
  12870. 10:09:08class here's where we're going to choose
  12871. 10:09:10our word I'm going to copy this from the
  12872. 10:09:12previous
  12873. 10:09:14code and paste
  12874. 10:09:16it rather than setting our word to be
  12875. 10:09:19hard-coded string we're going to take
  12876. 10:09:21our array list of
  12877. 10:09:26words call the get
  12878. 10:09:28method but we have to specify an
  12879. 10:09:31index with our array list return a
  12880. 10:09:34string at a given index that index is
  12881. 10:09:37going to be randomly generated by using
  12882. 10:09:39this random object we will pass in a
  12883. 10:09:41random object call the next int method
  12884. 10:09:45to generate a random integer the range
  12885. 10:09:48of random numbers is going to be
  12886. 10:09:49dependent on the size of our array list
  12887. 10:09:52of
  12888. 10:09:53words take our words call the size
  12889. 10:09:56method so with our file I have let's see
  12890. 10:10:00how many words how many strings 1 2 3 4
  12891. 10:10:035 6 7
  12892. 10:10:058 the size method of words is going to
  12893. 10:10:07return the number eight just imagine
  12894. 10:10:09that I'm replacing this with the number
  12895. 10:10:11eight return a random index between 0
  12896. 10:10:13and 8 that's kind of how that works and
  12897. 10:10:17then let's output our word for testing
  12898. 10:10:18purposes
  12899. 10:10:21what is my
  12900. 10:10:22word this time my word was pineapple but
  12901. 10:10:25it's likely going to change every time
  12902. 10:10:26you run it Apple lime Apple again
  12903. 10:10:32pineapple
  12904. 10:10:34grape it really likes pineapple for some
  12905. 10:10:38reason let's delete this print line
  12906. 10:10:40statement we'll integrate our code back
  12907. 10:10:42into the program again we can delete
  12908. 10:10:44that multi-line
  12909. 10:10:47comment and let's R this program one
  12910. 10:10:50last time we should now have a randomly
  12911. 10:10:52chosen
  12912. 10:10:53word I don't know what it's going to be
  12913. 10:10:56for me it's six letters I'll guess some
  12914. 10:10:58of the vowels a there's one a e i o
  12915. 10:11:06u it's probably orange but I'm going to
  12916. 10:11:09guess some more wrong letters for
  12917. 10:11:11testing
  12918. 10:11:12purposes okay game
  12919. 10:11:15over the word was Orange let's run it
  12920. 10:11:18again
  12921. 10:11:20this one is seven
  12922. 10:11:22letters a e i o u it's
  12923. 10:11:29coconut
  12924. 10:11:31Co n u t you win the word was
  12925. 10:11:36coconut all right everybody that is a
  12926. 10:11:39game of hangman that you can code using
  12927. 10:11:41Java hello there everybody in this video
  12928. 10:11:44I'm going to show you how we can work
  12929. 10:11:45with dates and times using Java this
  12930. 10:11:47serves as more of an introduction we can
  12931. 10:11:49work with local dates times date times
  12932. 10:11:52and UTC timestamps let's get the date
  12933. 10:11:55right now using local date we will
  12934. 10:11:57create an object of local date let's
  12935. 10:12:00name this date for short
  12936. 10:12:03equals access the class of local
  12937. 10:12:06date called the now
  12938. 10:12:09method if I was to Output our date
  12939. 10:12:12object we'll print our date that's going
  12940. 10:12:15to give you the current date whatever it
  12941. 10:12:17is right now I'm filling this video
  12942. 10:12:20December 17th
  12943. 10:12:222024 post the date that you're watching
  12944. 10:12:24this in the comment section down below
  12945. 10:12:26that is local date there's local time to
  12946. 10:12:29get the time let's replace date with
  12947. 10:12:32time local time.
  12948. 10:12:35now and rename this as time cuz that's
  12949. 10:12:38more
  12950. 10:12:39appropriate print the time currently the
  12951. 10:12:42time for me is a little after 9:00 a.m.
  12952. 10:12:469 hours 29 minutes 52 seconds
  12953. 10:12:49and a bunch of
  12954. 10:12:51milliseconds to retrieve both the date
  12955. 10:12:53and the time you can use date time local
  12956. 10:12:57date
  12957. 10:13:00time local dat time.now method print the
  12958. 10:13:04current date
  12959. 10:13:06time and currently for me it is December
  12960. 10:13:0917th 2024 at about 9:30 in the
  12961. 10:13:13morning you can also use UTC time that's
  12962. 10:13:16a little different we will create a
  12963. 10:13:19instant object instant instant equals
  12964. 10:13:24access the class of instant called the
  12965. 10:13:26now
  12966. 10:13:27method and print our instant
  12967. 10:13:30object which will give us the current
  12968. 10:13:33date and time in
  12969. 10:13:34UTC December 17th 2024 in UTC time I
  12970. 10:13:39think that's 300 p.m. it's in military
  12971. 10:13:42time that's how to get either the
  12972. 10:13:44current date time date time or UTC
  12973. 10:13:48timestamp when you output the current
  12974. 10:13:50date and/ or time it's kind of ugly and
  12975. 10:13:52hard to read well we can write a custom
  12976. 10:13:54format to display our date time here's
  12977. 10:14:00how here we'll create a custom format
  12978. 10:14:03for our date time we will need the
  12979. 10:14:05current date
  12980. 10:14:06time local date
  12981. 10:14:09time we will name this date time equals
  12982. 10:14:13access the class of local date time
  12983. 10:14:16called the now method
  12984. 10:14:19then we'll need to create a formatter
  12985. 10:14:21object date time
  12986. 10:14:25formatter we'll name this formatter
  12987. 10:14:28equals date time formatter I'm just
  12988. 10:14:30going to copy it call the of pattern
  12989. 10:14:34method and then we're going to pass on a
  12990. 10:14:37string let me move the cursor so you can
  12991. 10:14:40see this within the string we will write
  12992. 10:14:43pattern letters what is the pattern in
  12993. 10:14:45which we would like to display our date
  12994. 10:14:47and our time if you go to the official
  12995. 10:14:49Java documentation there's an extensive
  12996. 10:14:51list here's a few beginner friendly ones
  12997. 10:14:54for the day type 2DS dash for the month
  12998. 10:14:58two Capital M's for the year Das y y y y
  12999. 10:15:03four
  13000. 10:15:04y's this will display the day the month
  13001. 10:15:08and the year I'm an American so we
  13002. 10:15:10display the month first followed by the
  13003. 10:15:12day so I'm going to switch this around
  13004. 10:15:14but you can keep it as it originally was
  13005. 10:15:16if you prefer so that's how to display
  13006. 10:15:18the date in that
  13007. 10:15:19format month day year now for the time
  13008. 10:15:23you can do the following for hours do
  13009. 10:15:25two Capital H's colon minutes is two
  13010. 10:15:29lowercase M's colon then for seconds
  13011. 10:15:32it's two lowercase s's that is a good
  13012. 10:15:36pattern we will create a new
  13013. 10:15:38string named new date
  13014. 10:15:42time equals take our current date time
  13015. 10:15:45it's an object call the format method
  13016. 10:15:50we are going to pass in our formatter to
  13017. 10:15:53format our date time to this
  13018. 10:15:56pattern once we have our new date time
  13019. 10:15:59we're going to print it let's output our
  13020. 10:16:02new date time and it should be in that
  13021. 10:16:06format yeah that looks pretty good
  13022. 10:16:08December 17th 2024 about 9:00 in the
  13023. 10:16:11morning 9:30 at least for me instead of
  13024. 10:16:15getting the time and date right now we
  13025. 10:16:17can create a custom date time object
  13026. 10:16:20here's how so we'll start with a local
  13027. 10:16:23date local date I'll name this object
  13028. 10:16:27date equals local date we're accessing
  13029. 10:16:32the class statically we'll call the of
  13030. 10:16:34method depending on the date you would
  13031. 10:16:36like to create we'll pass them in as
  13032. 10:16:38arguments let's say I would like to pick
  13033. 10:16:40Christmas of
  13034. 10:16:422024 the first argument corresponds to
  13035. 10:16:44the year I will pick
  13036. 10:16:462024 the next is for the month 12
  13037. 10:16:49meaning
  13038. 10:16:50December then the 25th 25th of December
  13039. 10:16:54I'll type in
  13040. 10:16:5525 all right just to test it I'm going
  13041. 10:16:57to Output it let's print our
  13042. 10:17:02date the year is 2024 December
  13043. 10:17:0825th you can also use date time let's
  13044. 10:17:11switch local date to local date
  13045. 10:17:16time we need a few more arguments ments
  13046. 10:17:19so let's say noon on Christmas Day that
  13047. 10:17:22would be
  13048. 10:17:2312 for the minutes 0o for the seconds
  13049. 10:17:26zero then let's print this again let's
  13050. 10:17:30output our date well technically it
  13051. 10:17:32should be date time but that still makes
  13052. 10:17:36sense here's our date
  13053. 10:17:38time December 25th 2024 at noon 12:00
  13054. 10:17:43p.m. you can also compare dates too
  13055. 10:17:46let's copy the snippet of code paste
  13056. 10:17:50it rename the first date as date one the
  13057. 10:17:53second date as date two for the second
  13058. 10:17:56date let's set that to New Year's Day
  13059. 10:17:582025 the month will be January the 1st
  13060. 10:18:02of
  13061. 10:18:04January at midnight that would be
  13062. 10:18:06zero let's print date one and then date
  13063. 10:18:12two so we have
  13064. 10:18:14Christmas December 25th 2024 and new
  13065. 10:18:18Year's Day January 1st
  13066. 10:18:212025 all right now to compare dates we
  13067. 10:18:24can use the following we'll use an if
  13068. 10:18:27statement let's check if date
  13069. 10:18:30one called the is before method dates
  13070. 10:18:35have a built-in is before method pass in
  13071. 10:18:37date two is date one before date two and
  13072. 10:18:41that is
  13073. 10:18:42true let's output date one plus a string
  13074. 10:18:47of is earlier than plus date 2 let's
  13075. 10:18:53check that our first date Christmas is
  13076. 10:18:57earlier than New Year's our second
  13077. 10:19:02date else
  13078. 10:19:04if date
  13079. 10:19:07one let's just copy this date one is
  13080. 10:19:11after date two then we'll output
  13081. 10:19:14something
  13082. 10:19:15different date one is later than date
  13083. 10:19:19two so let's change date 1 to be January
  13084. 10:19:242nd of
  13085. 10:19:282025 the second of January is later than
  13086. 10:19:32New Year's Day our second
  13087. 10:19:35date you can also check if they're
  13088. 10:19:38equal let's add else
  13089. 10:19:41if let me scroll down date 1 is equal to
  13090. 10:19:48date 2
  13091. 10:19:50and then we'll say that they're
  13092. 10:19:52equal date 1 is equal
  13093. 10:19:57to date
  13094. 10:19:59two let's set these to be the same New
  13095. 10:20:03Year's Day at
  13096. 10:20:06midnight New Year's Day at midnight is
  13097. 10:20:09equal to New Year's Day at midnight this
  13098. 10:20:12could be good for some sort of alarm
  13099. 10:20:13clock checking to see if two date times
  13100. 10:20:15are
  13101. 10:20:17equal all right everybody so that is an
  13102. 10:20:19introduction to working with dates and
  13103. 10:20:21times using
  13104. 10:20:23Java well hello again friends today I
  13105. 10:20:25got to explain Anonymous classes in Java
  13106. 10:20:28so what is an anonymous class it's a
  13107. 10:20:30class that doesn't have a name meaning
  13108. 10:20:32it cannot be reused it's when you want
  13109. 10:20:34to add custom Behavior without having to
  13110. 10:20:36create a new class because it can be
  13111. 10:20:39really inconvenient to create a new
  13112. 10:20:40class just for one object if that one
  13113. 10:20:43object is different from the rest in one
  13114. 10:20:45way or another they're often used for
  13115. 10:20:47onetime uses when utilizing features
  13116. 10:20:49such as timer task the runnable
  13117. 10:20:52interface or callbacks these are more
  13118. 10:20:54advanced Java topics so let me
  13119. 10:20:57demonstrate why an anonymous class would
  13120. 10:20:59be useful let's begin with this example
  13121. 10:21:01where we create a dog class file new
  13122. 10:21:04Java class we will create a class of
  13123. 10:21:08dog let's say that in this dog class
  13124. 10:21:10there's a single method a speak
  13125. 10:21:13method void speak all we'll do is I'll
  13126. 10:21:17put the following
  13127. 10:21:18the dog goes woof and then let's create
  13128. 10:21:23a dog
  13129. 10:21:24object dog dog equals new
  13130. 10:21:28dog we'll take our dog object have it
  13131. 10:21:31use its speak method which will
  13132. 10:21:34output the dog goes wo so what if
  13133. 10:21:38there's a unique kind of dog it's
  13134. 10:21:40different from the rest let's say it's
  13135. 10:21:42scoobydoo now
  13136. 10:21:44Scooby-Doo he doesn't speak dog he
  13137. 10:21:46speaks English if I want Scooby be do to
  13138. 10:21:48say something else I'd have to override
  13139. 10:21:50this method so one option would be to
  13140. 10:21:53create a new class again this is me not
  13141. 10:21:55using Anonymous classes you'll see their
  13142. 10:21:57usefulness in just a moment let's create
  13143. 10:22:00a new Java class let's create a talking
  13144. 10:22:03dog class I know it's kind of a weird
  13145. 10:22:06example so our talking dog class will
  13146. 10:22:09extend the dog class and then we'll
  13147. 10:22:12override the speak
  13148. 10:22:14method
  13149. 10:22:16override void speak then we'll I'll put
  13150. 10:22:20the
  13151. 10:22:21following Scooby Do says what's he
  13152. 10:22:25usually say rut row or something rut row
  13153. 10:22:29I think that's how you spell it but you
  13154. 10:22:31get the idea going back to our main Java
  13155. 10:22:34file I can create a talking dog object
  13156. 10:22:38talking dog talking dog equals new
  13157. 10:22:43talking dog that's a lot to
  13158. 10:22:45type and then I can have my talking dog
  13159. 10:22:49use its speak
  13160. 10:22:53method our dog goes woof and Scooby do
  13161. 10:22:56our talking dog says rutro like uh- oh
  13162. 10:23:00it is a lot of work to create a whole
  13163. 10:23:02new class just for one unique object our
  13164. 10:23:05talking dog of Scooby-Doo is the
  13165. 10:23:07exception so rather than creating an
  13166. 10:23:09entirely new class just for this one
  13167. 10:23:11object just for Scooby-Doo our talking
  13168. 10:23:14dog let's instead create an anonymous
  13169. 10:23:17class we'll take our our talking dog
  13170. 10:23:19class and delete
  13171. 10:23:25it instead of creating a talking dog
  13172. 10:23:28object let's create another dog object
  13173. 10:23:31but it's going to have some unique
  13174. 10:23:32features that set it apart from other
  13175. 10:23:34dogs such as the ability to speak
  13176. 10:23:36English let's rename dog is Dog one
  13177. 10:23:40we'll create another dog named dog 2
  13178. 10:23:43equals new dog add aside a parenthesis
  13179. 10:23:47now to create an anonymous class you're
  13180. 10:23:49going to add a set of curly
  13181. 10:23:51braces then make sure there's that
  13182. 10:23:53semicolon at the end within the set of
  13183. 10:23:55curly braces you can Define any unique
  13184. 10:23:58features or override any methods dog 2
  13185. 10:24:02is going to be different from all other
  13186. 10:24:03dogs with it speak method so let's
  13187. 10:24:06override that method at override void
  13188. 10:24:12speak and then we'll
  13189. 10:24:15say Scooby Do
  13190. 10:24:19says rut
  13191. 10:24:22row dog one is going to speak and dog
  13192. 10:24:29two the dog goes woof Scooby-Doo says
  13193. 10:24:33rut
  13194. 10:24:34R so instead of creating an entirely new
  13195. 10:24:37class just for one dog Scooby-Doo we can
  13196. 10:24:40create a normal dog and just override
  13197. 10:24:42one of the methods using an anonymous
  13198. 10:24:44class so those are Anonymous classes
  13199. 10:24:47it's a class that doesn't have a name
  13200. 10:24:49and it cannot be reused if you need to
  13201. 10:24:51reuse a class you're better off just
  13202. 10:24:53creating a new class it allows you to
  13203. 10:24:55add custom Behavior without having to
  13204. 10:24:57create a new class which is what we did
  13205. 10:24:59with the speak method Anonymous classes
  13206. 10:25:01are often used with timer task which
  13207. 10:25:03we'll talk about in the next video the
  13208. 10:25:05runnable interface and callbacks so it
  13209. 10:25:08is an important topic that you need to
  13210. 10:25:09know going forward at least to create an
  13211. 10:25:12anonymous class after you construct an
  13212. 10:25:14object add a set of curly braces within
  13213. 10:25:16that set of curly braces you can Define
  13214. 10:25:18new behavior and well everybody those
  13215. 10:25:21are Anonymous classes in Java so uh yeah
  13216. 10:25:25I got to talk about timers and timer
  13217. 10:25:27tasks in Java timer is a class that
  13218. 10:25:30schedules tasks at specific times or
  13219. 10:25:33periodically timer is useful for sending
  13220. 10:25:36notifications scheduled updates or
  13221. 10:25:38repetitive actions when you need to
  13222. 10:25:40schedule a task at a specific time you
  13223. 10:25:43can use the timer class now timer task
  13224. 10:25:46is the task that's going to execute cute
  13225. 10:25:48when our timer says so in order to use
  13226. 10:25:51timer task you're going to extend the
  13227. 10:25:54timer task class and then Define your
  13228. 10:25:56task and we're going to be doing this by
  13229. 10:25:58using Anonymous classes which we learned
  13230. 10:26:01about in the last video we'll Begin by
  13231. 10:26:03creating a timer we need a timer to well
  13232. 10:26:05keep track of the time let's say timer
  13233. 10:26:08timer equals new
  13234. 10:26:11timer and then import these classes I'm
  13235. 10:26:14pretty sure you're used to importing
  13236. 10:26:16these by now
  13237. 10:26:18we're going to select the timer from the
  13238. 10:26:20utility
  13239. 10:26:21package all right we have our timer then
  13240. 10:26:25we need a timer task what are we going
  13241. 10:26:27to do when the timer is
  13242. 10:26:31up let's just say task timer task task
  13243. 10:26:35equals new timer task import the
  13244. 10:26:42class all right so here's the
  13245. 10:26:45situation with our timer class we have
  13246. 10:26:47to implement the run method to override
  13247. 10:26:50the run method we could create a child
  13248. 10:26:52class from the timer task class but
  13249. 10:26:55here's a better option we're going to
  13250. 10:26:57create an anonymous class after the set
  13251. 10:26:59of parentheses we will add a set of
  13252. 10:27:01curly braces then add that semicolon to
  13253. 10:27:03the end within this Anonymous class we
  13254. 10:27:06can update or change the behavior of our
  13255. 10:27:08timer task object what we'll do is
  13256. 10:27:11override the run
  13257. 10:27:13method
  13258. 10:27:16override to overrun this method
  13259. 10:27:18this will be public void run it's the
  13260. 10:27:23run
  13261. 10:27:24method when we execute our task what
  13262. 10:27:27would we like to do let's output the
  13263. 10:27:29following let's just say
  13264. 10:27:32hello so our task is to print
  13265. 10:27:36hello but we have to do it when our
  13266. 10:27:38timer says so so they work
  13267. 10:27:41together let's say I would like to say
  13268. 10:27:43hello after 3 seconds to do that we're
  13269. 10:27:46going to take our timer
  13270. 10:27:48call the schedule
  13271. 10:27:51method and then there's two arguments
  13272. 10:27:53we're going to pass in our task what are
  13273. 10:27:56we going to do that's the first argument
  13274. 10:27:59the next argument is the delay after how
  13275. 10:28:02many milliseconds will we execute this
  13276. 10:28:04task I said 3 seconds let's say 3,000
  13277. 10:28:08milliseconds then after a delay of 3
  13278. 10:28:11seconds 3,000 milliseconds do your task
  13279. 10:28:15that we're passing in so after 3 seconds
  13280. 10:28:18will
  13281. 10:28:21output
  13282. 10:28:23hello or if this were 1,000 millisecs it
  13283. 10:28:26would only take a
  13284. 10:28:28second hello you can also schedule at a
  13285. 10:28:32fixed rate our timer can do things
  13286. 10:28:36periodically so let's repeat our task of
  13287. 10:28:38saying hello more than just once so we
  13288. 10:28:41have our delay you know what I'll set
  13289. 10:28:44that to be zero the next argument is for
  13290. 10:28:47the period what is the delay in
  13291. 10:28:49milliseconds between repetitions let's
  13292. 10:28:52say 1,000 milliseconds so 1
  13293. 10:28:55second starting immediately 0
  13294. 10:28:59milliseconds print hello every 1,000
  13295. 10:29:02milliseconds every second let's try
  13296. 10:29:07it hello hello hello hello I think you
  13297. 10:29:12get the idea or you could add a delay
  13298. 10:29:15that's the second argument let's say
  13299. 10:29:17after a delay delay of 3,000
  13300. 10:29:19milliseconds 3 seconds repeat this line
  13301. 10:29:22of code every 1,000 milliseconds 1
  13302. 10:29:25second so now there's going to be a
  13303. 10:29:28delay of 3
  13304. 10:29:29seconds then we output hello every 1
  13305. 10:29:33second now how do you cancel this timer
  13306. 10:29:36once it's started well you can use the
  13307. 10:29:38cancel
  13308. 10:29:40method where should we add that exactly
  13309. 10:29:43well we can add that within our task
  13310. 10:29:45we'll need some sort of stopping
  13311. 10:29:46condition kind of like a wild Loop so
  13312. 10:29:49let's say this I would like to say hello
  13313. 10:29:51three times I'll create a local variable
  13314. 10:29:55of count I'll set that to be
  13315. 10:29:59three intellig wants me to make this
  13316. 10:30:01final you don't have to I'll keep it as
  13317. 10:30:04is so again we are within our Anonymous
  13318. 10:30:07class we're declaring a variable to use
  13319. 10:30:09within the context of our
  13320. 10:30:11task within the run method we'll output
  13321. 10:30:15hello decrement our count by one count
  13322. 10:30:18minus
  13323. 10:30:20minus then using an if statement this is
  13324. 10:30:23how we're going to escape if our count
  13325. 10:30:27is less than
  13326. 10:30:28zero then let's
  13327. 10:30:31say task
  13328. 10:30:35complete and then to cancel the timer we
  13329. 10:30:37will take our timer object called the
  13330. 10:30:40cancel
  13331. 10:30:42method all right let's try it I'll set
  13332. 10:30:46the delay to be zero so it starts
  13333. 10:30:48immediately every 1,000 milliseconds
  13334. 10:30:51print hello whoops it looks like that's
  13335. 10:30:53less than or equal to zero I fixed it
  13336. 10:30:57all right so starting immediately the
  13337. 10:30:59delay is zero every 1,000 milliseconds
  13338. 10:31:02print hello but only do it three times
  13339. 10:31:05and then we cancel the timer to stop
  13340. 10:31:07because we don't want to say hello
  13341. 10:31:08forever and here it
  13342. 10:31:11is 1 2 3 task
  13343. 10:31:16complete all right everybody so that is
  13344. 10:31:18timer and timer task timer is a class
  13345. 10:31:21that schedules tasks at specific times
  13346. 10:31:24or periodically it's useful for sending
  13347. 10:31:27notifications scheduled updates or other
  13348. 10:31:30repetitive actions timer task is the
  13349. 10:31:33task you're going to perform when the
  13350. 10:31:35timer says so in the next video we're
  13351. 10:31:37going to use timer and timer task to
  13352. 10:31:39create a countdown timer all right
  13353. 10:31:41everybody so that is both timer and
  13354. 10:31:42timer task in
  13355. 10:31:44Java why hello there everybody in this
  13356. 10:31:47video we're going to create a countdown
  13357. 10:31:49timer program using Java this is meant
  13358. 10:31:51to serve as more of a mini project to
  13359. 10:31:53get us used to working with timer tasks
  13360. 10:31:55let's begin we'll accept user input but
  13361. 10:31:58we're going to set that up near the end
  13362. 10:32:00it's going to be easier for us to
  13363. 10:32:01understand this program at first if we
  13364. 10:32:03set the countdown timer to be a raw
  13365. 10:32:05number like 10 to work with timer tasks
  13366. 10:32:08we'll need a timer to keep track of the
  13367. 10:32:09time timer timer equals new
  13368. 10:32:14timer we'll be importing our classes as
  13369. 10:32:17we go along long be sure to do that
  13370. 10:32:19import them from the Java utility
  13371. 10:32:22package then we'll need a timer task
  13372. 10:32:25timer task I'll name this task for short
  13373. 10:32:28equals new timer task be sure to import
  13374. 10:32:32this
  13375. 10:32:33class now there's one issue though with
  13376. 10:32:36our timer task object we have to
  13377. 10:32:38override the run method one option
  13378. 10:32:41although it's not the best is to create
  13379. 10:32:42a new class and then inherit from the
  13380. 10:32:44timer task class so this would be the
  13381. 10:32:47parent class and then we can override
  13382. 10:32:49the run method but since we don't plan
  13383. 10:32:51on reusing that class what we could do
  13384. 10:32:53instead is use an anonymous class after
  13385. 10:32:56the set of parentheses add a set of
  13386. 10:32:57curly braces then add that semicolon to
  13387. 10:33:00the end for a onetime use we can
  13388. 10:33:03override the run method of our timer
  13389. 10:33:04task object because we only plan on
  13390. 10:33:07using it once so we can write an
  13391. 10:33:08anonymous class rather than create a
  13392. 10:33:10whole new class that we're only going to
  13393. 10:33:12use once so we will
  13394. 10:33:15override the run method
  13395. 10:33:18public void
  13396. 10:33:21run when our timer executes our task
  13397. 10:33:24what would we like to do well we're
  13398. 10:33:26going to Output a number the number for
  13399. 10:33:28our countdown timer so within our
  13400. 10:33:30Anonymous class let's create an integer
  13401. 10:33:33variable of count for the time being
  13402. 10:33:36let's set this to a low number like five
  13403. 10:33:40intelligent wants us to make this
  13404. 10:33:41variable final but we'll be accepting
  13405. 10:33:43user input momentarily so you don't need
  13406. 10:33:45to do that my IDE of intelligent is
  13407. 10:33:48recommending that you can just ignore
  13408. 10:33:49that so let's say that our count is
  13409. 10:33:51going to start at five and we'll
  13410. 10:33:53decrement by one then display happy New
  13411. 10:33:56Year when count to zero so why don't we
  13412. 10:33:59do the following within our run method
  13413. 10:34:02we'll output our count variable at first
  13414. 10:34:06it's going to be
  13415. 10:34:07five then we'll decrement count by one
  13416. 10:34:11count minus
  13417. 10:34:13minus we'll need to escape the run
  13418. 10:34:15method well we can do that with an if
  13419. 10:34:17statement
  13420. 10:34:19we will check if our count is less than
  13421. 10:34:23zero if it is let's output happy New
  13422. 10:34:30Year and then to stop our timer we'll
  13423. 10:34:33take our timer object called the cancel
  13424. 10:34:37method now we have to set up our timer
  13425. 10:34:40we will do that outside the timer task
  13426. 10:34:42Anonymous
  13427. 10:34:43class so right here is good we will take
  13428. 10:34:47our timer called the schedule
  13429. 10:34:51method schedule will take a task our
  13430. 10:34:54timer
  13431. 10:34:55task and execute it after a delay let's
  13432. 10:34:58set the delay to be zero now this is
  13433. 10:35:01only going to run once there's a better
  13434. 10:35:03method for this let me just
  13435. 10:35:05demonstrate so we get the number five
  13436. 10:35:08and our program
  13437. 10:35:11stops so what we're going to use instead
  13438. 10:35:13is schedule at fixed rate
  13439. 10:35:18the schedule at fixed rate method but
  13440. 10:35:20there's three arguments we have to pass
  13441. 10:35:22in first is the task the delay and then
  13442. 10:35:26the
  13443. 10:35:27period we have our task the delay is
  13444. 10:35:30zero what is the period in between
  13445. 10:35:33executions let's set that to be 1,000
  13446. 10:35:36milliseconds every 1 second 1,000
  13447. 10:35:40milliseconds perform your task and our
  13448. 10:35:43task is to display our countdown
  13449. 10:35:45whatever the count currently is
  13450. 10:35:47all right let's test this again we're
  13451. 10:35:50setting our count to be five initially
  13452. 10:35:52but we'll be accepting user input
  13453. 10:35:55momentarily we start at 5 and we're
  13454. 10:35:57going to count down to zero then display
  13455. 10:36:00happy New
  13456. 10:36:01Year all right after zero we display
  13457. 10:36:04happy New Year and our program exits
  13458. 10:36:07because we canceled the
  13459. 10:36:08timer now if we don't cancel the timer
  13460. 10:36:11I'm just going to cut this momentarily
  13461. 10:36:14it's going to continue forever
  13462. 10:36:22we keep on displaying happy New Year and
  13463. 10:36:24we're going into negative
  13464. 10:36:26seconds so that's why we need to cancel
  13465. 10:36:29the timer so that it stops all right now
  13466. 10:36:32we'll accept user input because we want
  13467. 10:36:34a user to type in the number to
  13468. 10:36:36countdown from rather than us
  13469. 10:36:38programmers just typing in a random
  13470. 10:36:40number so we will need a
  13471. 10:36:44scanner scanner scanner equals new
  13472. 10:36:48scanner then pass in
  13473. 10:36:51system.in import this class and then
  13474. 10:36:55we'll need a
  13475. 10:36:56prompt let's output the
  13476. 10:36:58following enter number of seconds to
  13477. 10:37:06countdown
  13478. 10:37:07from I'll use print rather than print
  13479. 10:37:11line let's create an integer variable of
  13480. 10:37:15response set that equal to use our
  13481. 10:37:18scanner call the next int method to get
  13482. 10:37:21the next integer from the user then we
  13483. 10:37:24will set our count variable equal to the
  13484. 10:37:27response whatever the user types in so
  13485. 10:37:30now the user can type in a number and we
  13486. 10:37:32can count down from that number rather
  13487. 10:37:34than just placing a random number here
  13488. 10:37:36all right let's test
  13489. 10:37:39this enter number of seconds to count
  13490. 10:37:41down from let's say 10 we're going to
  13491. 10:37:44count down from 10 so we start at 10 and
  13492. 10:37:47we're going to count down to
  13493. 10:37:55zero zero happy New Year and then the
  13494. 10:37:58program exits so you can type in any
  13495. 10:38:01number you could even set this to a
  13496. 10:38:03higher number like
  13497. 10:38:0660 but I'm not going to make you wait
  13498. 10:38:08that
  13499. 10:38:09long all right everybody that is a
  13500. 10:38:12countdown timer program that you can
  13501. 10:38:13write as a mini project using Java hello
  13502. 10:38:17there every everybody in this video I
  13503. 10:38:18got to cover generics in Java generics
  13504. 10:38:21it's a concept it's where you can write
  13505. 10:38:23a class interface or method and it's
  13506. 10:38:26compatible with different data types
  13507. 10:38:28there's two things we need a type
  13508. 10:38:30parameter and a type argument so with
  13509. 10:38:33type parameters it's a set of angle
  13510. 10:38:35brackets with a letter inside for
  13511. 10:38:37example T basically this acts as a
  13512. 10:38:40placeholder and it gets replaced with
  13513. 10:38:42the real type now on the other hand type
  13514. 10:38:44arguments with various objects and data
  13515. 10:38:46structures you'll see a pair of angle
  13516. 10:38:48brackets with a type inside such as a
  13517. 10:38:50string but it really can be anything it
  13518. 10:38:52can be an integer or a
  13519. 10:38:56Boolean really anything type arguments
  13520. 10:38:59specify the type we send arguments to
  13521. 10:39:02parameters really our type parameter is
  13522. 10:39:04just acting as a placeholder anything
  13523. 10:39:06that uses this type parameter it doesn't
  13524. 10:39:08know the data type that it's going to
  13525. 10:39:10receive it's set up to receive really
  13526. 10:39:12anything or a primitive using a rapper
  13527. 10:39:14class let me give you an example of
  13528. 10:39:16where you already see a gener being used
  13529. 10:39:18for example array lists to create an
  13530. 10:39:21array list we add a set of angle
  13531. 10:39:23brackets after array list and we have to
  13532. 10:39:25specify the type what is this array list
  13533. 10:39:28going to store for example strings we
  13534. 10:39:30would write the data type of string
  13535. 10:39:33let's say that this array list is named
  13536. 10:39:35fruits fruits equals new array list and
  13537. 10:39:40then we need a set of angle brackets
  13538. 10:39:41again parenthesis semicolon in modern
  13539. 10:39:45Java within the second set of angle
  13540. 10:39:46brackets you don't necessarily need to
  13541. 10:39:48type the data type again Java can infer
  13542. 10:39:51that so you can leave it empty this is
  13543. 10:39:53the diamond
  13544. 10:39:54operator we have an array list that can
  13545. 10:39:57store strings we have specified the type
  13546. 10:39:59argument and now we can store some
  13547. 10:40:01strings within our array list so let's
  13548. 10:40:04take our array list of fruits call the
  13549. 10:40:06ad method and add a few names of fruits
  13550. 10:40:09such as an
  13551. 10:40:10apple let's do two
  13552. 10:40:12more Apple orange banana
  13553. 10:40:18currently the way that we set up this
  13554. 10:40:19array list it's compatible with strings
  13555. 10:40:22because that's what we set the type
  13556. 10:40:23argument to be now if I set this to be
  13557. 10:40:26integers well we can't store strings
  13558. 10:40:28anymore but we could store numbers 1 2 3
  13559. 10:40:33that's
  13560. 10:40:35fine let me change that back generally
  13561. 10:40:38speaking an array data structure can
  13562. 10:40:40store all sorts of different data types
  13563. 10:40:43but when we create one we have to
  13564. 10:40:44specify what we're storing that's the
  13565. 10:40:47type argument it's because within the
  13566. 10:40:49array list class we have a type
  13567. 10:40:51parameter set up a set of angle brackets
  13568. 10:40:54with a letter inside in many cases
  13569. 10:40:56you'll see t meaning type for type
  13570. 10:40:59parameter so let's actually take a look
  13571. 10:41:01at our array list I am going to locate
  13572. 10:41:05array list jump to Source here's the
  13573. 10:41:08class for our array list there's a lot
  13574. 10:41:10of advanced Java in here don't worry
  13575. 10:41:12about that but what I want you to pay
  13576. 10:41:14attention to is the type parameter that
  13577. 10:41:16comes after after the array list class
  13578. 10:41:18name we have angle brackets and a letter
  13579. 10:41:20inside in this case e meaning element
  13580. 10:41:23because an array list has elements of
  13581. 10:41:25data by using this type parameter our
  13582. 10:41:28array list is set up to store elements
  13583. 10:41:30of various types we just have to specify
  13584. 10:41:32what the type is going to be when we
  13585. 10:41:34actually do store data and in my example
  13586. 10:41:37we're using strings but like I said
  13587. 10:41:39before we can change the data type of
  13588. 10:41:41what we're storing we just have to
  13589. 10:41:43change the type argument I'll store
  13590. 10:41:45booleans true
  13591. 10:41:50false
  13592. 10:41:52true I don't know what that has to do
  13593. 10:41:54with fruits but I'm just trying to prove
  13594. 10:41:55a point
  13595. 10:41:58here so now what we're going to do is
  13596. 10:42:00actually use type parameters so let's
  13597. 10:42:03delete our array list using generics you
  13598. 10:42:06can write some logic within a class
  13599. 10:42:08interface or method and it's compatible
  13600. 10:42:10with many different data types so what
  13601. 10:42:13we're going to do in this example is
  13602. 10:42:14create a class of box
  13603. 10:42:18class
  13604. 10:42:20box we'll be using a type parameter to
  13605. 10:42:23store all sorts of different things
  13606. 10:42:24within our box we can store a string
  13607. 10:42:27within our box an integer a double A
  13608. 10:42:29Boolean even more complex objects so we
  13609. 10:42:32will set up a type parameter with a set
  13610. 10:42:35of angle brackets then type t t is a
  13611. 10:42:38common convention meaning type let's say
  13612. 10:42:41that when we create a box object our box
  13613. 10:42:44is going to act as a container we'll
  13614. 10:42:45store a value inside
  13615. 10:42:48our box is going to be a reusable class
  13616. 10:42:50we won't always know what the data type
  13617. 10:42:52of what we're storing is going to be so
  13618. 10:42:55what we could do is set the data type to
  13619. 10:42:58be T let's say that this variable is an
  13620. 10:43:01item we're storing an item within a box
  13621. 10:43:04with t it does mean type but I like to
  13622. 10:43:07think of it as thing we're storing a
  13623. 10:43:10thing an item within our box we don't
  13624. 10:43:13know what this thing is going to be but
  13625. 10:43:16really it means type and that's not as
  13626. 10:43:18fun T item we don't know what the data
  13627. 10:43:21type is if it's always going to be a
  13628. 10:43:23string we could just set this to be a
  13629. 10:43:25string but what if somebody is storing
  13630. 10:43:28an integer an INT or we can use the
  13631. 10:43:31rapper class of integer we don't know
  13632. 10:43:34what the type is going to be let's
  13633. 10:43:36create a method where we will set our
  13634. 10:43:38item let's say that this is public void
  13635. 10:43:41it's not going to return anything set
  13636. 10:43:46item we'll have one parameter what's the
  13637. 10:43:49data type of what we're receiving well
  13638. 10:43:51we don't know so we're going to use our
  13639. 10:43:53type parameter of
  13640. 10:43:55t t is the data type it's generic we
  13641. 10:43:59will receive an
  13642. 10:44:01item let's take this. item and set it to
  13643. 10:44:05be the item that we
  13644. 10:44:07receive we'll create a method to get our
  13645. 10:44:10item public now we're going to be
  13646. 10:44:13returning an item the data type is T so
  13647. 10:44:17the return type is also going to be T
  13648. 10:44:19we're returning a value of the T data
  13649. 10:44:22type we will create a method to get
  13650. 10:44:27item we will return this. item we can
  13651. 10:44:32put things in our box and we can get
  13652. 10:44:34things from our
  13653. 10:44:36box all right this class is done our box
  13654. 10:44:40class is set up to be reusable we can
  13655. 10:44:42store all sorts of different things so
  13656. 10:44:45to say within our box we will attempt to
  13657. 10:44:48create a box object for now let's say
  13658. 10:44:51box box equals new box but there's one
  13659. 10:44:55step that we're
  13660. 10:44:56missing we have a warning here raw use
  13661. 10:45:00of parameterized class
  13662. 10:45:02box we need to set up a type argument
  13663. 10:45:06what are we going to be putting in our
  13664. 10:45:07box what's its data type our box class
  13665. 10:45:10wants to know so we will use a type
  13666. 10:45:14argument after the class of box add a
  13667. 10:45:17set of angle brackets then type what
  13668. 10:45:19we're going to store so let's say we're
  13669. 10:45:22going to store strings within our box
  13670. 10:45:24values of the string data type and then
  13671. 10:45:26we do need the diamond operator after
  13672. 10:45:29the second box and we're good we now
  13673. 10:45:32have a box object but it's set up to
  13674. 10:45:34store strings let's add some items to
  13675. 10:45:36our box we've even created a few methods
  13676. 10:45:39for this set item let's take our box
  13677. 10:45:43call set item but we have to pass in a
  13678. 10:45:45string let's add a banana to our
  13679. 10:45:49box and then we will get the banana from
  13680. 10:45:52the box using the get item method and
  13681. 10:45:56now I want to get the banana box. get
  13682. 10:46:02item here we go we have our banana we
  13683. 10:46:07have successfully retrieved the banana
  13684. 10:46:09from the
  13685. 10:46:10box our box class is compatible with all
  13686. 10:46:13sorts of different data types this time
  13687. 10:46:16let's store an integer we will use the
  13688. 10:46:19wrapper class of integer but we can no
  13689. 10:46:21longer add a banana to the Box because
  13690. 10:46:23it's a string I'll attempt to do so
  13691. 10:46:26string cannot be converted to
  13692. 10:46:29integer unfortunately bananas are not
  13693. 10:46:32numbers but I can add the number three
  13694. 10:46:35to our
  13695. 10:46:37box we have the number three within our
  13696. 10:46:39box if I attempt to add a double like
  13697. 10:46:423.14 we get a warning
  13698. 10:46:47incompatible types double cannot be
  13699. 10:46:49converted to
  13700. 10:46:50integer so I would have to set the type
  13701. 10:46:53argument to be double to add doubles to
  13702. 10:46:56our
  13703. 10:46:57box
  13704. 10:46:593.14 basically with our box class we're
  13705. 10:47:02writing the logic of how a box works you
  13706. 10:47:05put something in and you can take
  13707. 10:47:07something out and it's compatible with
  13708. 10:47:09different data
  13709. 10:47:10types we just have to use a type
  13710. 10:47:12argument when creating a box object we
  13711. 10:47:15have to let Java know what we're storing
  13712. 10:47:17within it exactly all right let me give
  13713. 10:47:19you another example now with type
  13714. 10:47:21parameters you can have more than one
  13715. 10:47:23type we can have two or more different
  13716. 10:47:25types usually the common convention is
  13717. 10:47:28to use U after
  13718. 10:47:31T so we're going to create a new class
  13719. 10:47:34file new Java class we will create a
  13720. 10:47:37class of
  13721. 10:47:39product here we're going to write the
  13722. 10:47:41logic of how a product works with a
  13723. 10:47:43product you have an item and you have a
  13724. 10:47:46price we will set up a type parameter
  13725. 10:47:49let me zoom in we'll have one type of t
  13726. 10:47:52and a common convention for the second
  13727. 10:47:54type is U because in the English
  13728. 10:47:56alphabet the letter U comes after T if
  13729. 10:47:59you had a third argument that would be V
  13730. 10:48:01by Common
  13731. 10:48:03convention so we'll just stick with t
  13732. 10:48:05and
  13733. 10:48:07u we will have a generic type of
  13734. 10:48:12item and another generic type of price
  13735. 10:48:17we don't know if our item is going to be
  13736. 10:48:18a string or a more complex object our
  13737. 10:48:21price could be an integer a floating
  13738. 10:48:23Point number a double we don't know but
  13739. 10:48:26that's why we're setting it up this
  13740. 10:48:29way now we'll need a Constructor we will
  13741. 10:48:31take our
  13742. 10:48:33product then set up some parameters the
  13743. 10:48:36data type of our item is t t item but if
  13744. 10:48:41we're creating a product we also need a
  13745. 10:48:43price the data type of our price is u
  13746. 10:48:47U price this. item equals the item that
  13747. 10:48:52we
  13748. 10:48:52receive this. price equals the price
  13749. 10:48:56that we
  13750. 10:48:57receive okay let's do a test run let's
  13751. 10:49:00go to our main Java
  13752. 10:49:02file product product equals new
  13753. 10:49:08product and again we need those type
  13754. 10:49:11arguments so this time we actually need
  13755. 10:49:14two because our class is set up to
  13756. 10:49:16receive two type arguments T and
  13757. 10:49:20U for our product let's say that the
  13758. 10:49:23first type is going to be a string
  13759. 10:49:25followed by a double and then we need
  13760. 10:49:28the diamond operator after the second
  13761. 10:49:31product all right now we have to pass in
  13762. 10:49:33arguments to the class because we have a
  13763. 10:49:35Constructor we have to pass in an item
  13764. 10:49:38and its
  13765. 10:49:39price and with our type arguments we set
  13766. 10:49:42that up to be a string and a double we
  13767. 10:49:44have to pass in those types for our
  13768. 10:49:47product let's say that it's an apple and
  13769. 10:49:50an apple is 50 Cents that's a
  13770. 10:49:53double let's create two methods within
  13771. 10:49:55our product class we will create a
  13772. 10:49:58method to get our item let's say public
  13773. 10:50:03we're returning an item the data type is
  13774. 10:50:05T so we're going to return
  13775. 10:50:07T get
  13776. 10:50:11item we will return this do
  13777. 10:50:15item then to return the
  13778. 10:50:18price let's say public U cuz we're
  13779. 10:50:22returning the price it has a type of
  13780. 10:50:25U Get
  13781. 10:50:28price return this do
  13782. 10:50:33price we have our product
  13783. 10:50:36object I'm going to Output take our
  13784. 10:50:39product object called the get item
  13785. 10:50:44method and this is going to return are
  13786. 10:50:48apple or I could get the price get
  13787. 10:50:54price and that is
  13788. 10:50:560.5 you can use string format but that
  13789. 10:50:59might be overkill for this lesson all
  13790. 10:51:01right let's create a second product so
  13791. 10:51:03we'll have product
  13792. 10:51:05one and product
  13793. 10:51:08two Now product two will use an integer
  13794. 10:51:11instead of a
  13795. 10:51:13double and let's say that these are
  13796. 10:51:15tickets like movie tickets ticket and
  13797. 10:51:19the price will be $15 for
  13798. 10:51:22$15 let's take product
  13799. 10:51:26two call the get item
  13800. 10:51:30method so we have our
  13801. 10:51:34ticket and we should have a price get
  13802. 10:51:39price the price of a movie ticket is$ 15
  13803. 10:51:43$15 all right everybody so that is an
  13804. 10:51:45introduction to two generics it's a
  13805. 10:51:48concept where you can write a class
  13806. 10:51:49interface or method that is compatible
  13807. 10:51:52with different data types basically
  13808. 10:51:54we're writing the logic of how this
  13809. 10:51:56class works and it's compatible with all
  13810. 10:51:59different types you just have to set up
  13811. 10:52:01a type parameter and then pass in type
  13812. 10:52:04arguments and well everybody that is an
  13813. 10:52:06introduction to generics in Java well
  13814. 10:52:10it's up everybody so today I got to
  13815. 10:52:12explain hashmaps in Java hashmaps are a
  13816. 10:52:15popular data structure
  13817. 10:52:17they store a pair of values a key value
  13818. 10:52:20pair you have a key and a value
  13819. 10:52:22associated with that key the keys have
  13820. 10:52:24to be unique but the values can be
  13821. 10:52:27duplicated hashmaps do not maintain any
  13822. 10:52:30order but they are very memory efficient
  13823. 10:52:32when creating a hashmap we have to set
  13824. 10:52:34up type parameters we have to specify
  13825. 10:52:37what the data type of the key is and its
  13826. 10:52:39value here's how to create a hashmap we
  13827. 10:52:42will type hashmap let's say map for
  13828. 10:52:46short
  13829. 10:52:47hashmap map equals new hash
  13830. 10:52:52map we'll need to import this class it
  13831. 10:52:55looks like we're missing something let's
  13832. 10:52:57take a look raw use of parameterized
  13833. 10:53:00class
  13834. 10:53:01hashmap if we take a look at the hashmap
  13835. 10:53:03class we have type parameter set up
  13836. 10:53:06there's two values we learned about type
  13837. 10:53:09parameters in the last video on generics
  13838. 10:53:11we can store any two types of values
  13839. 10:53:13within a hashmap we have a key and a
  13840. 10:53:16value represented by K and V we have to
  13841. 10:53:19send type arguments to the type
  13842. 10:53:21parameters when creating a hashmap we'll
  13843. 10:53:23have to set up some type arguments so
  13844. 10:53:26after our hashmap we have to list the
  13845. 10:53:28data type of what we're storing these
  13846. 10:53:30have to be reference data types so let's
  13847. 10:53:33say with our map we're going to have
  13848. 10:53:34products an item and a price so for the
  13849. 10:53:38item that could be a string a string
  13850. 10:53:41representation of our
  13851. 10:53:42item so for the price we could do an
  13852. 10:53:45integer but I think a double would be
  13853. 10:53:48better because you can have dollars and
  13854. 10:53:50cents then we also need to use the
  13855. 10:53:52diamond operator after the second
  13856. 10:53:54hashmap you don't need to State the data
  13857. 10:53:56types again even though you technically
  13858. 10:53:58could Java can actually infer what the
  13859. 10:54:01data type is so after the second hashmap
  13860. 10:54:04add the diamond operator and there we go
  13861. 10:54:07we have our hashmap that we have named
  13862. 10:54:09map the way that we've set this up is
  13863. 10:54:12that we've told the hashmap class we're
  13864. 10:54:14going to store a string as the key and a
  13865. 10:54:16double as the value so to put things in
  13866. 10:54:19a hash map we're going to take our map
  13867. 10:54:22called the put method we're going to put
  13868. 10:54:24something in the hashmap we've specified
  13869. 10:54:27that we're storing a string then a
  13870. 10:54:29double a string for the key and a double
  13871. 10:54:32for the value so let's say we have a
  13872. 10:54:35store and in our store we sell produce
  13873. 10:54:39we will have an apple and its price will
  13874. 10:54:42be 50 CS
  13875. 10:54:440.50 to put some something in a hashmap
  13876. 10:54:47you use the put method we have to pass
  13877. 10:54:49in two values as arguments all right
  13878. 10:54:52let's add a few more
  13879. 10:54:53things we'll have an
  13880. 10:54:56orange an orange will be 75 cents let's
  13881. 10:54:59say then a banana bananas are pretty
  13882. 10:55:03cheap they will be 25 cents all right
  13883. 10:55:08let's take a look at our hash map I'm
  13884. 10:55:10going to show you what happens if we
  13885. 10:55:12output our map directly system.out.print
  13886. 10:55:15line our map our hash
  13887. 10:55:18map and here's what we have we have all
  13888. 10:55:21the key value pairs printed within our
  13889. 10:55:23hash map we have a string and a double
  13890. 10:55:27string double string
  13891. 10:55:30double now with hash Maps the keys have
  13892. 10:55:34to be unique for example let's say I try
  13893. 10:55:37and put another orange within here we'll
  13894. 10:55:39attempt to add another orange and we'll
  13895. 10:55:41see what happens
  13896. 10:55:42exactly put another Orange
  13897. 10:55:47within our map and let's say that the
  13898. 10:55:49price is 1
  13899. 10:55:51million Venezuelan dollars yes oranges
  13900. 10:55:55are very expensive in Venezuela because
  13901. 10:55:57of inflation so here's what happens if
  13902. 10:55:59we have duplicate
  13903. 10:56:01Keys you actually overwrite the previous
  13904. 10:56:04key value pair our orange is now worth 1
  13905. 10:56:07million Venezuelan dollars so do keep
  13906. 10:56:10that in mind hashmaps cannot have
  13907. 10:56:13duplicate keys if you put another key
  13908. 10:56:15value pair with than a hashmap and it
  13909. 10:56:17already exists you'll overwrite it but
  13910. 10:56:19that's also good if you want to change
  13911. 10:56:21one of the key value pairs let's add one
  13912. 10:56:23more key value pair let's say a coconut
  13913. 10:56:26map. put we will put a coconut within
  13914. 10:56:30our hash map coconuts are a little more
  13915. 10:56:33expensive they'll be
  13916. 10:56:35$1 let's print our map to test
  13917. 10:56:38it there's our
  13918. 10:56:41coconut then we can remove an element to
  13919. 10:56:44remove an element take your map call the
  13920. 10:56:46remove
  13921. 10:56:48method here you're going to pass in a
  13922. 10:56:50key let's remove our
  13923. 10:56:53Apple then print our
  13924. 10:56:57map there the apple is gone we have
  13925. 10:57:00orange banana coconut but no apple so
  13926. 10:57:03I'm going to keep that in but turn it
  13927. 10:57:05into a
  13928. 10:57:06comment to get the value associated with
  13929. 10:57:09the key you can use the get method so
  13930. 10:57:12let me delete this print line statement
  13931. 10:57:16we will take our map called the get
  13932. 10:57:20method let's get the value with this
  13933. 10:57:24key give me the value where the key is
  13934. 10:57:27Apple is what we're
  13935. 10:57:28saying that would be
  13936. 10:57:310.5 meaning
  13937. 10:57:3350 or we can get our
  13938. 10:57:38coconut the value associated with this
  13939. 10:57:40key is one meaning
  13940. 10:57:43$1 all right you can also check to see
  13941. 10:57:45if a key or a value exists here's
  13942. 10:57:50how we'll use the contains key method
  13943. 10:57:52take our map call the contains key
  13944. 10:57:56method and then pass in a key is there a
  13945. 10:57:59key of
  13946. 10:58:02banana that returns a Boolean in this
  13947. 10:58:05case
  13948. 10:58:06true is there a pineapple there is not
  13949. 10:58:10so that returns
  13950. 10:58:14false you could use this with than an if
  13951. 10:58:16statement here's
  13952. 10:58:19how we will check
  13953. 10:58:21if let me zoom in if our map call the
  13954. 10:58:26contains key
  13955. 10:58:28method and then pass in a key does our
  13956. 10:58:31map contain any
  13957. 10:58:33apples contains key
  13958. 10:58:36Apple if that returns true we'll
  13959. 10:58:39output
  13960. 10:58:41map. getet then we'll get our Apple
  13961. 10:58:46this will give us the price else if
  13962. 10:58:49contains key is false then we'll output
  13963. 10:58:52something else we'll
  13964. 10:58:56output key not
  13965. 10:59:00found all right let's test this so we do
  13966. 10:59:03have an apple we've set this up to
  13967. 10:59:06return the
  13968. 10:59:07price but let's say we are looking for a
  13969. 10:59:10pineapple contains key
  13970. 10:59:12pineapple if we do find a pineapple we
  13971. 10:59:15will get the price price currently
  13972. 10:59:17there's no pineapples within our map key
  13973. 10:59:20not found so that's how the contains key
  13974. 10:59:23method would be
  13975. 10:59:24useful we also have the capability to
  13976. 10:59:27check to see if a map contains a value
  13977. 10:59:29so we will take our map within a print
  13978. 10:59:31line statement called the
  13979. 10:59:34contains Value method and then pass in a
  13980. 10:59:37value is there anything that's $1
  13981. 10:59:411.0 this returns a Boolean and that's
  13982. 10:59:44true our key of coconut has a value of
  13983. 10:59:481.0 $1 and you do have to pay attention
  13984. 10:59:52to the data type too if I were to pass
  13985. 10:59:54in an
  13986. 10:59:55integer this does return false you do
  13987. 10:59:58have to keep that in mind too that's how
  13988. 11:00:01to check to see if a map contains a key
  13989. 11:00:03and a
  13990. 11:00:05value you can return the size of a map
  13991. 11:00:08using map. size map dot called the size
  13992. 11:00:12method currently there's four key value
  13993. 11:00:14pairs within our map
  13994. 11:00:16this will return the number four to get
  13995. 11:00:19the size of a map call the size
  13996. 11:00:22method all right now the last thing that
  13997. 11:00:24we're going to do if we were to print
  13998. 11:00:26our
  13999. 11:00:28map we're given this ugly
  14000. 11:00:31formatting where we have a set of curly
  14001. 11:00:33braces then all the key value pairs
  14002. 11:00:35comma
  14003. 11:00:37separated this time using a for loop
  14004. 11:00:40we're going to cycle through all the key
  14005. 11:00:41value pairs we will create an enhanced
  14006. 11:00:44for Loop
  14007. 11:00:46what we're going to iterate through is
  14008. 11:00:48every key and these have a string data
  14009. 11:00:50type because that's the way that we set
  14010. 11:00:52them
  14011. 11:00:53up the data type of each element is
  14012. 11:00:56going to be a string which we will name
  14013. 11:00:58key colon think of it as in for every
  14014. 11:01:02key in now what we have to do is get all
  14015. 11:01:06the keys from our map and there's
  14016. 11:01:08actually a method for that a beginners
  14017. 11:01:11friendly way to get all the keys from
  14018. 11:01:13our map what we can do is take our map
  14019. 11:01:16call the key set
  14020. 11:01:19method this will return all the keys
  14021. 11:01:21within our map and they're iterable so
  14022. 11:01:24we can use them within a for Loop for
  14023. 11:01:26every key in our set of keys what do we
  14024. 11:01:29want to do let's do the following within
  14025. 11:01:32a print line statement let's print our
  14026. 11:01:35key plus we'll list each product and its
  14027. 11:01:39price key
  14028. 11:01:41plus maybe I'll add a colon space then
  14029. 11:01:45pick a unit of currency I'll pick
  14030. 11:01:46American dollars for every key we need
  14031. 11:01:49to get its value so we can use the get
  14032. 11:01:52method plus map. getet pass in our key
  14033. 11:01:58by using an enhanced for Loop we can
  14034. 11:02:00customize the output of the key value
  14035. 11:02:02Pairs and I think this looks a lot
  14036. 11:02:05better we have each product each piece
  14037. 11:02:07of fruit and its price laid out in this
  14038. 11:02:11format you could technically use print F
  14039. 11:02:13if you want to display two digits after
  14040. 11:02:15the decimal
  14041. 11:02:16but that might be a little too much for
  14042. 11:02:17this lesson I want to keep it as simple
  14043. 11:02:19as possible all right everybody so those
  14044. 11:02:22are hashmaps it's a data structure that
  14045. 11:02:24stores key value pairs the keys have to
  14046. 11:02:27be unique but the values can be
  14047. 11:02:29duplicated we have to specify the type
  14048. 11:02:32for the key and its value and well
  14049. 11:02:34everybody those are hashmaps in
  14050. 11:02:37Java all right let's do this thing
  14051. 11:02:39everybody today we're talking about
  14052. 11:02:41enums en nums is short for enumerations
  14053. 11:02:44they're a special kind of class they
  14054. 11:02:46represent a fixed set of constants by
  14055. 11:02:49using enums they improve code
  14056. 11:02:51readability and are easy to maintain one
  14057. 11:02:54of the major benefits of enums is that
  14058. 11:02:56they're more efficient with switches
  14059. 11:02:58when comparing strings so basically an
  14060. 11:03:01enum is a special kind of class where we
  14061. 11:03:04declare constants here's a demonstration
  14062. 11:03:06we're going to create a new class file
  14063. 11:03:09new Java class we're going to select
  14064. 11:03:12enum enum is a special kind of class
  14065. 11:03:15sort of like like how an interface is a
  14066. 11:03:16special kind of
  14067. 11:03:18class we will create an enum of
  14068. 11:03:21day public enum Day within this Java
  14069. 11:03:25class we can declare some constants what
  14070. 11:03:28we're going to write is enum constants a
  14071. 11:03:30common convention for these constants is
  14072. 11:03:33that all the letters are uppercase so
  14073. 11:03:35after the enum constant We'll add a set
  14074. 11:03:37of parentheses then associate this
  14075. 11:03:40constant with the value so let's say
  14076. 11:03:42that we're working with days of the week
  14077. 11:03:44Sunday will be the the first day of the
  14078. 11:03:46week Sunday we'll have a value of one
  14079. 11:03:49let's do this with the other days of the
  14080. 11:03:51week Monday parentheses then a value
  14081. 11:03:55Monday will be the second day of the
  14082. 11:03:56week then we'll just follow this
  14083. 11:04:11pattern and all these constants with a
  14084. 11:04:14semicolon all right we have our enum
  14085. 11:04:16constants they have values 1 through 7
  14086. 11:04:20we're receiving some warnings here
  14087. 11:04:22expected zero arguments but one found
  14088. 11:04:25what we need to do is create a
  14089. 11:04:27Constructor a Constructor for our day
  14090. 11:04:30class when we Define these enum
  14091. 11:04:32constants we're automatically going to
  14092. 11:04:34call the Constructor for this enum class
  14093. 11:04:37and then pass in this value let's create
  14094. 11:04:39a field of day number for days of the
  14095. 11:04:41week we'll make these values private
  14096. 11:04:44we'll use the private access modifier we
  14097. 11:04:47don't want the ability to change them
  14098. 11:04:48we'll make them final these are integers
  14099. 11:04:51that's the data type we'll name these
  14100. 11:04:54values day
  14101. 11:04:56number when we Define these enum
  14102. 11:04:59constants we're automatically going to
  14103. 11:05:01call the Constructor and then pass in
  14104. 11:05:03this value to the
  14105. 11:05:04Constructor for the parameters we'll
  14106. 11:05:06have int then our field of day
  14107. 11:05:10number this day
  14108. 11:05:13number equals the day day number that we
  14109. 11:05:18receive if you would like you can add a
  14110. 11:05:20getter method to actually get the day
  14111. 11:05:23number we'll use the public access
  14112. 11:05:26modifier we're returning an integer
  14113. 11:05:29we'll Define a method of get day
  14114. 11:05:33number and we will return our field of
  14115. 11:05:38this. day number all right and we have
  14116. 11:05:41set up our enum class reading and
  14117. 11:05:44understanding these enum constants is a
  14118. 11:05:45lot easier than these numbers if I was
  14119. 11:05:48looking at the number five I would have
  14120. 11:05:49trouble understanding that that means
  14121. 11:05:51Thursday now how do we work with these
  14122. 11:05:53enum constants exactly here's how within
  14123. 11:05:56our main Java
  14124. 11:05:58file we will create a day object by
  14125. 11:06:01using this enum
  14126. 11:06:03class let me delete
  14127. 11:06:06this day day rather than saying new day
  14128. 11:06:12and then passing in a number like one
  14129. 11:06:14there's a different approach
  14130. 11:06:16we will access the day class Dot and
  14131. 11:06:20then specify a constant a day of the
  14132. 11:06:23week let's say Sunday remember that this
  14133. 11:06:26is all caps our day is an object let's
  14134. 11:06:30output our day and see what it
  14135. 11:06:33outputs this will output Sunday all caps
  14136. 11:06:37if you would rather get the day number
  14137. 11:06:40the value that's associated with this
  14138. 11:06:41enum
  14139. 11:06:42constant then we can use that getter
  14140. 11:06:44method
  14141. 11:06:46day. get day
  14142. 11:06:51number so we have Sunday and one where
  14143. 11:06:55this is really going to be helpful is
  14144. 11:06:57when working with
  14145. 11:06:58switches here's a demonstration we will
  14146. 11:07:01actually use an enhanced
  14147. 11:07:03switch what are we examining we're
  14148. 11:07:06examining our day object if our day
  14149. 11:07:08matches a case of Monday now this isn't
  14150. 11:07:12a string If This Were A String we would
  14151. 11:07:14enclose this within a set of quotes but
  14152. 11:07:17using enums within a switch is actually
  14153. 11:07:19faster than using
  14154. 11:07:20strings if it's Monday then we'll output
  14155. 11:07:23the following we'll use an arrow because
  14156. 11:07:25this is an enhanced switch then we'll
  14157. 11:07:28output it is a
  14158. 11:07:31weekday then if we have a case of
  14159. 11:07:33Tuesday let me just copy
  14160. 11:07:36this Tuesday we'll also output it is a
  14161. 11:07:40weekday you can actually combine these
  14162. 11:07:42cases together too you just have to
  14163. 11:07:44comma separate them
  14164. 11:07:46so case Monday comma Tuesday do
  14165. 11:07:50this then let's do this with the other
  14166. 11:07:52days of the week just for readability
  14167. 11:07:54I'll put these on new lines each of
  14168. 11:07:56these
  14169. 11:07:57cases so we have
  14170. 11:08:01Wednesday
  14171. 11:08:04Thursday
  14172. 11:08:06Friday these are all the weekdays Monday
  14173. 11:08:09through
  14174. 11:08:10Friday then if we have case Saturday
  14175. 11:08:12then it's the weekend case Saturday
  14176. 11:08:15today we'll do the following output it
  14177. 11:08:20is the
  14178. 11:08:22weekend and we will also add a case for
  14179. 11:08:26Sunday so currently our day is
  14180. 11:08:29Sunday that will output it is the
  14181. 11:08:32weekend if it were let's say
  14182. 11:08:38Wednesday then instead we output it is a
  14183. 11:08:41weekday if we were comparing strings all
  14184. 11:08:44these cases would be enclosed within a
  14185. 11:08:45set of double quotes kind of like
  14186. 11:08:50this but we don't need to do that it's
  14187. 11:08:53actually more efficient to use
  14188. 11:08:57enums for some practice what we'll do
  14189. 11:08:59this time is accept some user input
  14190. 11:09:01we'll need a
  14191. 11:09:02scanner scanner scanner equals new
  14192. 11:09:07scanner pass it
  14193. 11:09:10system.in import this class be sure to
  14194. 11:09:14close your scanner because I tend to
  14195. 11:09:17forget scanner.
  14196. 11:09:20close then we'll need a prompt we will
  14197. 11:09:23output the following we'll use print
  14198. 11:09:25rather than print
  14199. 11:09:27Line enter a day of the
  14200. 11:09:32week we will create a string variable
  14201. 11:09:35let's say response equals use our
  14202. 11:09:38scanner call the next line
  14203. 11:09:42method all these constants are uppercase
  14204. 11:09:45we'll method chain the two uppercase
  14205. 11:09:47method to make our user input
  14206. 11:09:50uppercase when creating a day object by
  14207. 11:09:53using the enum class we will use our
  14208. 11:09:56response we will use the value of
  14209. 11:10:00method and then pass in our response now
  14210. 11:10:03we should be able to type in a
  14211. 11:10:06day enter a day of the week I will type
  14212. 11:10:09Monday it is a weekday let's run this
  14213. 11:10:12again I will type Sunday it is the
  14214. 11:10:15weekend what if we type in a day that
  14215. 11:10:17doesn't exist like pizza
  14216. 11:10:20day well we get an exception an illegal
  14217. 11:10:23argument
  14218. 11:10:24exception we can catch that exception
  14219. 11:10:26using a try and catch block any
  14220. 11:10:28dangerous code we're going to enclose
  14221. 11:10:30within a tri block we will try the
  14222. 11:10:33dangerous
  14223. 11:10:38code and catch any
  14224. 11:10:41exceptions we will catch that illegal
  14225. 11:10:46argument exception which we will name e
  14226. 11:10:51if the user input doesn't match one of
  14227. 11:10:52these e nums then we'll output the
  14228. 11:10:55following please enter a valid
  14229. 11:11:00day let's try it
  14230. 11:11:03again enter day of the week let's try
  14231. 11:11:07Tuesday it is a
  14232. 11:11:10weekday
  14233. 11:11:12Saturday it is the weekend
  14234. 11:11:16Pizza day please enter a valid day Pizza
  14235. 11:11:21day is not one of our enums but if we
  14236. 11:11:23wanted to change it so it was we can add
  14237. 11:11:28that pizza day will be the eighth day of
  14238. 11:11:31the
  14239. 11:11:32week at least it should
  14240. 11:11:37be we'll need a case for pizza
  14241. 11:11:41day Pizza day will also be part of the
  14242. 11:11:43weekend that just makes sense to
  14243. 11:11:47me all right let's try that
  14244. 11:11:50again enter day of the week Pizza day it
  14245. 11:11:54is the weekend all right all right
  14246. 11:11:57everybody so those are enums meaning
  14247. 11:11:59enumerations they're a special kind of
  14248. 11:12:02class kind of like how interfaces are a
  14249. 11:12:04special kind of class they represent a
  14250. 11:12:07fixed set of constants they improve code
  14251. 11:12:09readability and reliability and where
  14252. 11:12:12they're really useful is when working
  14253. 11:12:14with switches
  14254. 11:12:15because when working with switches using
  14255. 11:12:17enums is faster than strings if I wasn't
  14256. 11:12:20using enum constants I could use
  14257. 11:12:22integers which are also
  14258. 11:12:27efficient but as a programmer this is
  14259. 11:12:30way less readable for me at first clance
  14260. 11:12:32I would have no idea what these numbers
  14261. 11:12:34are but if they're enum constants this
  14262. 11:12:37is perfectly readable to me these are
  14263. 11:12:39days of the week that makes sense and
  14264. 11:12:42well everybody those are enums in Java
  14265. 11:12:45what's going on people in this video I'm
  14266. 11:12:47going to teach you all about threading
  14267. 11:12:48in Java threading allows a program to
  14268. 11:12:51run multiple tasks simultaneously it
  14269. 11:12:54helps to improve performance with
  14270. 11:12:56timeconsuming
  14271. 11:12:57operations such as file input output
  14272. 11:13:00network communications or any other
  14273. 11:13:02background tasks any code that is
  14274. 11:13:04considered timec consuming we can have
  14275. 11:13:06it run on a different thread so that it
  14276. 11:13:08doesn't inconvenience our main program
  14277. 11:13:10there's two options to creating a thread
  14278. 11:13:13option one is to extend the thread class
  14279. 11:13:16this is the simpler option but it's
  14280. 11:13:18limited option two is to implement the
  14281. 11:13:21runnable interface this option tends to
  14282. 11:13:24be better because by extending the
  14283. 11:13:25thread class we're limited to single
  14284. 11:13:28inheritance but by implementing the
  14285. 11:13:30redible interface that's one way around
  14286. 11:13:32that in this demonstration I'm going to
  14287. 11:13:34show you option two it's actually not
  14288. 11:13:36that more difficult than option one and
  14289. 11:13:39it gives you more possibilities I'll
  14290. 11:13:41give you a demonstration of why
  14291. 11:13:42threading is useful in this program
  14292. 11:13:44where about to write let's say it's a
  14293. 11:13:47game or a quiz or something a user has
  14294. 11:13:5110 seconds to respond and if they don't
  14295. 11:13:53times up currently we have one thread
  14296. 11:13:56we're running our program on what is
  14297. 11:13:57known as the main thread without using
  14298. 11:14:00any other threads besides the main
  14299. 11:14:01thread I might write my program
  14300. 11:14:03something like this we'll start with the
  14301. 11:14:05scanner because we'll be accepting user
  14302. 11:14:07input scanner scanner equals new scanner
  14303. 11:14:11pass in
  14304. 11:14:13system.in import this class
  14305. 11:14:16and close your scanner when you're done
  14306. 11:14:18with it scanner.
  14307. 11:14:23close we're going to let a user know
  14308. 11:14:25that they have let's say 5 Seconds to
  14309. 11:14:27enter their
  14310. 11:14:29name you have 5 Seconds to enter your
  14311. 11:14:36name we will create a
  14312. 11:14:39prompt enter your
  14313. 11:14:42name I'll use print rather than and
  14314. 11:14:45print
  14315. 11:14:46line we will assign a string variable of
  14316. 11:14:49name use our scanner called the next
  14317. 11:14:52line
  14318. 11:14:53method and then once we have the name
  14319. 11:14:56we'll
  14320. 11:14:57output hello plus the user's name hello
  14321. 11:15:01plus name so let's do a test run
  14322. 11:15:04currently we don't have a timer
  14323. 11:15:07setup you have 5 Seconds down to your
  14324. 11:15:09name I can type in a name hit enter we
  14325. 11:15:12display hello the name and then the
  14326. 11:15:15program exits processed finished with
  14327. 11:15:17exit Code Zero if we were creating some
  14328. 11:15:20sort of timer we could do the
  14329. 11:15:22following to make this simple we could
  14330. 11:15:24use a for Loop the for Loop is going to
  14331. 11:15:27cycle five times we will have an index
  14332. 11:15:30of I in I equals 1 that's where we'll
  14333. 11:15:33start we'll continue as long as I is
  14334. 11:15:36less than or equal to 5 and then
  14335. 11:15:39increment I by One during each cycle of
  14336. 11:15:42the for Loop to mimic waiting for one
  14337. 11:15:45second you can access the thread class
  14338. 11:15:48call the Sleep Method pass in 1,000
  14339. 11:15:52milliseconds when you use thread that
  14340. 11:15:55refers to the current thread that we're
  14341. 11:15:56working with in this case the main
  14342. 11:15:58thread which is what our main program
  14343. 11:16:00runs on but since our thread might be
  14344. 11:16:03interrupted we do need a try and catch
  14345. 11:16:05block because this is considered
  14346. 11:16:07dangerous code we will try this code
  14347. 11:16:10where we will have our main Java thread
  14348. 11:16:12sleep for 1,000 milliseconds
  14349. 11:16:16and we will catch the following
  14350. 11:16:17exception an interrupted
  14351. 11:16:20exception So within our catch block we
  14352. 11:16:23will catch any interrupted exceptions
  14353. 11:16:25which we will name e if our thread is
  14354. 11:16:28interrupted we will
  14355. 11:16:30say thread was
  14356. 11:16:34interrupted in our imaginary timer we'll
  14357. 11:16:37need an exit
  14358. 11:16:39condition let's say if our index of I is
  14359. 11:16:43equal to 5
  14360. 11:16:45that means times up so we'll
  14361. 11:16:48output times up I'm going to show you
  14362. 11:16:52why this won't work the way that we
  14363. 11:16:55think we have 5 Seconds to enter our
  14364. 11:16:58name but there's no prompt or
  14365. 11:17:01anything then we get the message times
  14366. 11:17:04up and only after that Loop is complete
  14367. 11:17:06are we allowed to enter our name and
  14368. 11:17:08then we can type in our name we get the
  14369. 11:17:11output of hello our name and then the
  14370. 11:17:13program exits so the problem that we're
  14371. 11:17:15running into is that all of this code is
  14372. 11:17:18running on our main thread we have 5
  14373. 11:17:20Seconds to enter our name but we can't
  14374. 11:17:22reach that code until the 5 seconds are
  14375. 11:17:24up because our main thread is waiting
  14376. 11:17:27for this Loop to finish so what we could
  14377. 11:17:29do is create a separate thread where
  14378. 11:17:32we're counting to five and our main
  14379. 11:17:34thread is going to be in charge of
  14380. 11:17:36accepting our user input so here's how
  14381. 11:17:39to create another thread we're going to
  14382. 11:17:41extend the runable interface to create a
  14383. 11:17:44runnable object
  14384. 11:17:45let's go to file new Java class this
  14385. 11:17:49will be a class we'll name this class my
  14386. 11:17:53runnable or something unique that's
  14387. 11:17:56relevant to what you're
  14388. 11:17:58creating my runnable is going to
  14389. 11:18:02implements because we're working with
  14390. 11:18:04interfaces the runnable
  14391. 11:18:08interface if you implement an interface
  14392. 11:18:10it's kind of like you're signing a
  14393. 11:18:11contract we have to override any
  14394. 11:18:14required method me we need to override
  14395. 11:18:17the run method we need the at override
  14396. 11:18:20annotation then we will override the run
  14397. 11:18:23method public void
  14398. 11:18:26run within this run method we're going
  14399. 11:18:28to add any code we want to run in the
  14400. 11:18:30background on a separate thread going
  14401. 11:18:33back to our main Java file we're going
  14402. 11:18:35to cut this
  14403. 11:18:37Loop and then paste it within the run
  14404. 11:18:40method when we call this run method
  14405. 11:18:43we'll have this countdown timer run
  14406. 11:18:44running in the background on a separate
  14407. 11:18:46thread any code you want to run on a
  14408. 11:18:48separate thread place within the run
  14409. 11:18:50method all right going back to our main
  14410. 11:18:52Java file we'll tell the user they have
  14411. 11:18:555 Seconds to enter their
  14412. 11:18:57name we have our class of my runnable we
  14413. 11:19:01will create a my runnable object or
  14414. 11:19:04whatever it is that you named to this
  14415. 11:19:06class let's say my runnable will be the
  14416. 11:19:09name of the object equals new my
  14417. 11:19:13runable we will take this runnable
  14418. 11:19:15object pass it into the Constructor of
  14419. 11:19:18the thread class here's where we're
  14420. 11:19:20going to create a new thread thread
  14421. 11:19:23thread equals new
  14422. 11:19:26thread pass in your runnable object as
  14423. 11:19:29an argument to the thread
  14424. 11:19:31Constructor now we just need our thread
  14425. 11:19:33to
  14426. 11:19:34start you'll take your thread call the
  14427. 11:19:37start method and this should work let's
  14428. 11:19:40try
  14429. 11:19:41it you have 5 Seconds to enter your name
  14430. 11:19:45and we get that prompt of enter your
  14431. 11:19:46name but I'm not going to type in
  14432. 11:19:48anything we get the message times up
  14433. 11:19:51because we ran out of
  14434. 11:19:54time let's try it again I'll type in my
  14435. 11:19:56name this
  14436. 11:19:57time there we go we get our output hello
  14437. 11:20:01bro then we get the message time is
  14438. 11:20:04up both these threads are running at the
  14439. 11:20:07same time the code within our run method
  14440. 11:20:10is running on a separate thread it's
  14441. 11:20:12running in the background while our main
  14442. 11:20:14program is running running so that's why
  14443. 11:20:15threads are useful so let me extend this
  14444. 11:20:19time let's say 10
  14445. 11:20:22seconds if I is equal to 10 just to give
  14446. 11:20:25us some more
  14447. 11:20:26time you have 10 seconds to enter your
  14448. 11:20:30name now check this
  14449. 11:20:33out you have 10 seconds to to your name
  14450. 11:20:35I'll type in my name immediately we get
  14451. 11:20:38the output hello whatever your name is
  14452. 11:20:40but the program is still running because
  14453. 11:20:42that second thread is still running in
  14454. 11:20:43the background
  14455. 11:20:45our program doesn't exit until all
  14456. 11:20:47threads are
  14457. 11:20:48done if your main thread is done you can
  14458. 11:20:51end all other threads but you'll want to
  14459. 11:20:53set those threads to be what is known as
  14460. 11:20:55Damon threads a Damon thread will end
  14461. 11:20:58when the main thread is over we will
  14462. 11:21:00take our thread call the set Damon
  14463. 11:21:04method pass and
  14464. 11:21:07true this thread is going to end as soon
  14465. 11:21:09as our main thread is finished let's try
  14466. 11:21:12it again you have 10 seconds then to
  14467. 11:21:15your name I'll type in my name we get
  14468. 11:21:17our output and then our program exits
  14469. 11:21:20without saying time is up processed
  14470. 11:21:22finished with exit code
  14471. 11:21:24zero that's because we set our second
  14472. 11:21:27thread to be a Damon thread it ends when
  14473. 11:21:30the main thread is over now on the other
  14474. 11:21:32hand let's say that we wait for time to
  14475. 11:21:35be up we wait the full 10
  14476. 11:21:42seconds time's up our program isn't over
  14477. 11:21:46yet we're still waiting for user input
  14478. 11:21:48and I can still type it
  14479. 11:21:50in hello bro process finished with exit
  14480. 11:21:53Code Zero optionally what you could do
  14481. 11:21:56to get your program to exit go to your
  14482. 11:21:58runnable class wherever you would like
  14483. 11:22:00your program to exit when seconds equals
  14484. 11:22:0310 that's where I would like to exit to
  14485. 11:22:06exit the program prematurely I can
  14486. 11:22:08access system called the exit method
  14487. 11:22:12pass in an exit code of zero that will
  14488. 11:22:14ENT the program all right let's try this
  14489. 11:22:19again you have 10 seconds to end to your
  14490. 11:22:21name I'm not going to type in anything
  14491. 11:22:24I'm just going to wait for the full 10
  14492. 11:22:28seconds time's up and then our program
  14493. 11:22:31exits but that's what we want though the
  14494. 11:22:33way that we've set up this program is
  14495. 11:22:35that we want this program to exit either
  14496. 11:22:37when we finish typing in our name or we
  14497. 11:22:39run out of
  14498. 11:22:41time all right everybody so that is an
  14499. 11:22:43introduction to in it allows a program
  14500. 11:22:46to run multiple tasks simultaneously it
  14501. 11:22:49helps to improve performance with
  14502. 11:22:51timeconsuming
  14503. 11:22:52operations it's useful with file input
  14504. 11:22:54and output operations network
  14505. 11:22:56communications or any other background
  14506. 11:22:59tasks and well everybody that is an
  14507. 11:23:01introduction to threading in Java yo we
  14508. 11:23:05finally made it to multi-threading in
  14509. 11:23:07Java multi-threading enables a program
  14510. 11:23:09to run multiple threads concurrently at
  14511. 11:23:12the same time as we've talked about in
  14512. 11:23:14the previous lesson a thread is a set of
  14513. 11:23:16instructions and they run independently
  14514. 11:23:18from other threads multi-threading is
  14515. 11:23:21useful for background tasks or other
  14516. 11:23:23timeconsuming
  14517. 11:23:24operations in the prior lesson we've
  14518. 11:23:26learned that there's two ways to create
  14519. 11:23:28threads we can either extend the thread
  14520. 11:23:30class or implement the runable interface
  14521. 11:23:33the second option is better in most
  14522. 11:23:34cases because we're not limited by
  14523. 11:23:36single inheritance here's how we can get
  14524. 11:23:38started multi-threading we're going to
  14525. 11:23:40create a new class file new Java class
  14526. 11:23:44we'll call this class my
  14527. 11:23:49runnable my runnable will
  14528. 11:23:52implements the runnable interface if
  14529. 11:23:55we're implementing an interface it's
  14530. 11:23:57kind of like we're signing a contract we
  14531. 11:23:59have to override any required methods we
  14532. 11:24:02have to override the run
  14533. 11:24:04method public void
  14534. 11:24:08run whatever is within the run method
  14535. 11:24:11this is the code that's going to perform
  14536. 11:24:13when this thread is up and running
  14537. 11:24:15so what would we like to do just for a
  14538. 11:24:18beginner's example let's count to five
  14539. 11:24:20using a four Loop we'll write a for
  14540. 11:24:24Loop we will create an index of I I'll
  14541. 11:24:27set that equal to be one right away
  14542. 11:24:29we'll continue as long as I is less than
  14543. 11:24:32or equal to 5 cuz we're counting to five
  14544. 11:24:35then increment I by 1 during each cycle
  14545. 11:24:39this for loop I would like this thread
  14546. 11:24:41to wait for 1 second before printing
  14547. 11:24:43whatever I is what whatever the current
  14548. 11:24:44count is if we're counting we will
  14549. 11:24:47access the thread class called The Sleep
  14550. 11:24:50Method pass in 1,000 milliseconds to
  14551. 11:24:53sleep for a
  14552. 11:24:54second we will need to surround this
  14553. 11:24:56method with try and catch
  14554. 11:24:58blocks we'll catch any interrupted
  14555. 11:25:02exceptions but let's output the
  14556. 11:25:04following we'll say thread was
  14557. 11:25:09interrupted after our thread sleeps for
  14558. 11:25:121 second let's display the current count
  14559. 11:25:14I'll just say I just to keep it
  14560. 11:25:17simple let's go back to our main Java
  14561. 11:25:20file we'll need to create a runable
  14562. 11:25:22object my
  14563. 11:25:24runable let's say my runable equals new
  14564. 11:25:29my
  14565. 11:25:33runable we have to pass in this runable
  14566. 11:25:35object to a thread object let's say
  14567. 11:25:38thread thread equals new thread we will
  14568. 11:25:42pass in our runable object
  14569. 11:25:45to get this thread to start we have to
  14570. 11:25:47call the start method of our thread
  14571. 11:25:49thread. start
  14572. 11:25:50method and this should work we'll test
  14573. 11:25:55it 1 2 3 4 5 and then we exit the
  14574. 11:26:02program another shortcut you can do too
  14575. 11:26:06instead of creating a named runable
  14576. 11:26:07object we can pass in an anonymous
  14577. 11:26:09renable object to the thread Constructor
  14578. 11:26:12so let's copy this section delete this
  14579. 11:26:15line of code and then pass in a new
  14580. 11:26:17runable
  14581. 11:26:19object this will work too and it's a lot
  14582. 11:26:23more
  14583. 11:26:28concise now we won't have just one
  14584. 11:26:31thread we'll have two so let's rename
  14585. 11:26:33thread as thread one copy this line of
  14586. 11:26:36code paste it rename the second thread
  14587. 11:26:39as thread two thread 1. start and thread
  14588. 11:26:432. start
  14589. 11:26:45so now we'll have two threads running at
  14590. 11:26:47the same time they're running
  14591. 11:26:51concurrently so they both say 1 2 3 4 5
  14592. 11:26:57we have successfully
  14593. 11:27:00multi-threaded you could differentiate
  14594. 11:27:02these by getting their name here's an
  14595. 11:27:05example to get each thread's name we're
  14596. 11:27:07going to access the thread class called
  14597. 11:27:10the current thread method
  14598. 11:27:14and then method chain the get name
  14599. 11:27:18method then I'll add a space plus our
  14600. 11:27:21counter
  14601. 11:27:23RI just so we can see each thread by its
  14602. 11:27:25name and what it's counting exactly okay
  14603. 11:27:29let's try this
  14604. 11:27:31again we have thread one and thread zero
  14605. 11:27:35now they won't always be in the same
  14606. 11:27:37order they're running
  14607. 11:27:39independently thread zero that's our
  14608. 11:27:41thread one Java's thread one is our
  14609. 11:27:44thread
  14610. 11:27:47two okay here's an exercise we're going
  14611. 11:27:50to have thread one say ping and thread 2
  14612. 11:27:53say pong like we're playing a game of
  14613. 11:27:55pingpong we'll modify the my runnable
  14614. 11:28:00class we'll need some text a text
  14615. 11:28:03variable we'll say private final we
  14616. 11:28:06don't want it to
  14617. 11:28:07change string text then we'll need a
  14618. 11:28:11Constructor to assign it my
  14619. 11:28:16runnable we have one parameter a string
  14620. 11:28:20of text we will assign this. text equals
  14621. 11:28:24the text we
  14622. 11:28:27receive instead of printing our counter
  14623. 11:28:30we'll print our
  14624. 11:28:32text all right and that's all we need
  14625. 11:28:35going back to our main Java file we have
  14626. 11:28:37to pass in some text when we construct a
  14627. 11:28:39runable
  14628. 11:28:41object for the first thread we'll we'll
  14629. 11:28:44say ping for the second thread we'll say
  14630. 11:28:48pong so here's what we have
  14631. 11:28:53currently pong
  14632. 11:28:57ping and then we exit the
  14633. 11:29:01program before we start the threads
  14634. 11:29:04let's say game start there's one more
  14635. 11:29:06thing I want to show you game
  14636. 11:29:11start after we start the threads let's
  14637. 11:29:14try and say game
  14638. 11:29:15over game over here's what
  14639. 11:29:20happens game start game over and our two
  14640. 11:29:24threads are still
  14641. 11:29:26running then we
  14642. 11:29:28finish so the problem here is that in
  14643. 11:29:31our main thread it doesn't Wait For
  14644. 11:29:33Thread one and thread two to
  14645. 11:29:35finish our main thread ends
  14646. 11:29:37immediately we can have our main thread
  14647. 11:29:40wait for these threads to finish by
  14648. 11:29:42calling the join method of each thread
  14649. 11:29:44our main thread we'll wait for these two
  14650. 11:29:46threads to
  14651. 11:29:47finish after these two threads start
  14652. 11:29:50we'll use each join method thread 1.
  14653. 11:29:54join and thread 2
  14654. 11:30:00dojin and we do have to surround these
  14655. 11:30:03with try and catch
  14656. 11:30:10blocks try thread 1. jooin and thread 2
  14657. 11:30:14do jooin if our main thread is
  14658. 11:30:16interrupted we'll output main thread was
  14659. 11:30:21interrupted okay this should work now
  14660. 11:30:25before we display game over we're going
  14661. 11:30:27to wait for our two threads to
  14662. 11:30:29finish here's what we got game start and
  14663. 11:30:32we display
  14664. 11:30:34pingpong and we'll do that five times
  14665. 11:30:37and then display game
  14666. 11:30:40over all right everybody that is an
  14667. 11:30:42introduction to multi-threading
  14668. 11:30:44it enables a program to run multiple
  14669. 11:30:46threads concurrently a thread is a set
  14670. 11:30:49of instructions that run independently
  14671. 11:30:52multi-threading is useful for background
  14672. 11:30:54tasks or other timeconsuming
  14673. 11:30:56operations and well everybody that is an
  14674. 11:30:59introduction to multi-threading in Java
  14675. 11:31:02hey everybody in this video we're going
  14676. 11:31:03to create a working alarm clock that
  14677. 11:31:05plays music using
  14678. 11:31:09Java all right let's get started
  14679. 11:31:12everybody this alarm clock is going to
  14680. 11:31:13to serve as a final project for us will
  14681. 11:31:16combine a lot of Concepts that we've
  14682. 11:31:17learned throughout the series when
  14683. 11:31:19starting a new project a good first step
  14684. 11:31:21is to declare all your variables or
  14685. 11:31:23other objects that you'll need
  14686. 11:31:24throughout this program we'll begin with
  14687. 11:31:26the scanner because we'll be accepting
  14688. 11:31:28user input a user is going to enter in a
  14689. 11:31:30Time scanner scanner equals new scanner
  14690. 11:31:34pass in
  14691. 11:31:36system.in and we'll be importing classes
  14692. 11:31:39as we go along at the end of this
  14693. 11:31:41program we're going to close our scan
  14694. 11:31:44but we'll actually move it later on
  14695. 11:31:46because our alarm clock class is
  14696. 11:31:48actually going to be using it more on
  14697. 11:31:50that later we have our scanner and
  14698. 11:31:52currently we're closing it a user is
  14699. 11:31:56going to use the scanner and enter in
  14700. 11:31:58hours minutes and seconds to set the
  14701. 11:32:00alarm when the user types in a string
  14702. 11:32:02representation of a time we need to
  14703. 11:32:04parse it into a local time object but
  14704. 11:32:07we'll need a formatter object we will
  14705. 11:32:10create a date time formatter object
  14706. 11:32:14you'll need to import this class too
  14707. 11:32:17we'll name this object
  14708. 11:32:19formatter
  14709. 11:32:21equals we will access the class of
  14710. 11:32:23datetime formatter called the of pattern
  14711. 11:32:29method within this method we have to
  14712. 11:32:31pass in a pattern a pattern that
  14713. 11:32:33represents the time for hours minutes
  14714. 11:32:36and seconds within a string we will pass
  14715. 11:32:38in two Capital H's for hours colon two
  14716. 11:32:42lowercase M for minutes colon two
  14717. 11:32:45lowercase s's for
  14718. 11:32:47seconds when a user types in a string
  14719. 11:32:50representation of a time using our
  14720. 11:32:51scanner we'll use this formatter object
  14721. 11:32:54when we parse it into a Time
  14722. 11:32:56object then we will create a local time
  14723. 11:33:00object which we will name alarm
  14724. 11:33:05time we'll instantiate that later import
  14725. 11:33:08this
  14726. 11:33:09class now we'll need a user to enter in
  14727. 11:33:12the time we'll create a
  14728. 11:33:15prompt enter n alarm time we'll give
  14729. 11:33:20them a prompt for this format hours
  14730. 11:33:23minutes
  14731. 11:33:26seconds let me switch this to print CU I
  14732. 11:33:29like it on the same
  14733. 11:33:32line let's create a string
  14734. 11:33:35variable of let's name this input time
  14735. 11:33:39equals use our
  14736. 11:33:41scanner call the next next line method
  14737. 11:33:45to get a line of text from the
  14738. 11:33:47user all right now here's where we're
  14739. 11:33:49going to parse our string our input time
  14740. 11:33:52and assign it to our alarm time object
  14741. 11:33:55so we have to convert it we have to
  14742. 11:33:57parse it we will assign our alarm time
  14743. 11:34:00object set it equal to access the class
  14744. 11:34:05of local time and there is a parse
  14745. 11:34:08method we have to pass in two arguments
  14746. 11:34:11the first is a string representation of
  14747. 11:34:13our
  14748. 11:34:14time and our format object that we have
  14749. 11:34:17previously declared and
  14750. 11:34:20instantiated then we'll output the
  14751. 11:34:21following
  14752. 11:34:22message alarm set
  14753. 11:34:264 plus our alarm time we can output it
  14754. 11:34:32directly let's do a test
  14755. 11:34:37run let's say I set my alarm for
  14756. 11:34:41730 this is going to be in military time
  14757. 11:34:45alarm set for
  14758. 11:34:487:30 what if we type in a time that
  14759. 11:34:51doesn't make
  14760. 11:34:52sense uh like the word
  14761. 11:34:54Pizza well we encounter an exception a
  14762. 11:34:57date time parse exception we can use
  14763. 11:35:00exception handling to catch this
  14764. 11:35:01exception I'm just going to copy it and
  14765. 11:35:04then paste it
  14766. 11:35:05later this portion is considered
  14767. 11:35:08dangerous code we can put that within a
  14768. 11:35:09try block try this dangerous code catch
  14769. 11:35:13and any of the following
  14770. 11:35:15exceptions we will catch that date time
  14771. 11:35:18parse exception which we will name
  14772. 11:35:21e import this
  14773. 11:35:23class okay then take all of this
  14774. 11:35:26previous code stick it within our Tri
  14775. 11:35:28block we will try this code and catch
  14776. 11:35:32this exception if it pops up we'll
  14777. 11:35:35output the following let's say invalid
  14778. 11:35:41format please use
  14779. 11:35:46hours
  14780. 11:35:48minutes seconds let me change that to
  14781. 11:35:54use I've already used Pizza let's go
  14782. 11:35:57with Taco for the alarm time invalid
  14783. 11:36:00format please use hours minutes
  14784. 11:36:04seconds let's try
  14785. 11:36:088:00 alarm set for
  14786. 11:36:118:00 all right good we know that that
  14787. 11:36:14works why don't we prompt the user to
  14788. 11:36:17enter in the time again if it is an
  14789. 11:36:18invalid format we can use a while
  14790. 11:36:22loop while some condition is true
  14791. 11:36:25continue to do the following let's cut
  14792. 11:36:28our code our trying catch blocks stick
  14793. 11:36:31it within the while loop what's our
  14794. 11:36:34condition while our alarm time is empty
  14795. 11:36:38there's actually a value that you can
  14796. 11:36:39assign an object and that is null null
  14797. 11:36:42has no value
  14798. 11:36:44within our while loop we'll continue
  14799. 11:36:46this while loop while our alarm time is
  14800. 11:36:48equal to a value of
  14801. 11:36:52null so if I were to just hit enter we
  14802. 11:36:55get an invalid format please use hours
  14803. 11:36:58minute seconds and it won't let me
  14804. 11:37:00continue until I enter in a valid
  14805. 11:37:03time so I will set this to be
  14806. 11:37:079:00 and then our alarm is set for
  14807. 11:37:119:00 the first section of this project
  14808. 11:37:13is complete we have successfully set an
  14809. 11:37:15alarm time our alarm time is a local
  14810. 11:37:18time object the next part is to create
  14811. 11:37:21an alarm clock class to create alarm
  14812. 11:37:23clock objects we're going to create an
  14813. 11:37:26alarm clock class file new Java class we
  14814. 11:37:30will name this class alarm
  14815. 11:37:36clock we're going to have our alarm
  14816. 11:37:38clock run on a separate thread to do
  14817. 11:37:41that we can implement
  14818. 11:37:44the runnable
  14819. 11:37:45interface so we can do some threading
  14820. 11:37:48but if we implement the runable
  14821. 11:37:50interface it's kind of like we're
  14822. 11:37:51signing a contract we have to override
  14823. 11:37:55the run method public void
  14824. 11:37:59run when this alarm clock is running on
  14825. 11:38:02a separate thread it's going to execute
  14826. 11:38:04any code within the run method but we're
  14827. 11:38:07going to fill that in later currently
  14828. 11:38:09our alarm clock needs one thing it needs
  14829. 11:38:12an alarm time
  14830. 11:38:14which we will send it from the main Java
  14831. 11:38:18file let's set that up as a
  14832. 11:38:20field with our alarm time I'm going to
  14833. 11:38:23make it private I only want the alarm
  14834. 11:38:25clock class to be able to change it I
  14835. 11:38:28will also make it final it's
  14836. 11:38:30unchangeable the data type it's an
  14837. 11:38:32object it's a local time object which we
  14838. 11:38:36will name alarm time and then we do need
  14839. 11:38:40to import this class too
  14840. 11:38:43all right then we need a
  14841. 11:38:45Constructor we will take our alarm
  14842. 11:38:48clock we need one parameter
  14843. 11:38:51currently a local time of alarm time we
  14844. 11:38:56will assign this do alarm time equals
  14845. 11:39:01the alarm time that we
  14846. 11:39:03receive all right that's
  14847. 11:39:06good okay let's go back to our main Java
  14848. 11:39:09file before we close our scanner we're
  14849. 11:39:12going to create an alarm clock
  14850. 11:39:15object alarm clock is the class we will
  14851. 11:39:19name this object alarm clock equals new
  14852. 11:39:23alarm clock but the way that we set up
  14853. 11:39:26this class is that we have to pass in an
  14854. 11:39:29alarm time a local time
  14855. 11:39:33object we have our alarm time we've
  14856. 11:39:36already verified it using this while
  14857. 11:39:39loop it should be a valid format we will
  14858. 11:39:42pass in our local time of alarm time to
  14859. 11:39:44the alarm clock in order to construct it
  14860. 11:39:47since our alarm clock is going to be
  14861. 11:39:49running on a separate thread we'll have
  14862. 11:39:51to create a new
  14863. 11:39:52thread thread let's name this alarm
  14864. 11:39:55thread equals a new thread and then
  14865. 11:40:00we'll pass in our alarm clock we can do
  14866. 11:40:03this because our alarm clock it
  14867. 11:40:05implements the runnable interface so our
  14868. 11:40:07alarm clock is going to run on a
  14869. 11:40:09different thread than the main thread to
  14870. 11:40:11start a thread you will take your thread
  14871. 11:40:14in this case alarm thread called the
  14872. 11:40:16start
  14873. 11:40:17method so once we start our alarm thread
  14874. 11:40:21it's going to execute the run method all
  14875. 11:40:24right so for testing purposes let's get
  14876. 11:40:26the time right now to get the time right
  14877. 11:40:29now we will create a local time object
  14878. 11:40:32let's just call it now what's the time
  14879. 11:40:34right now equals access the class of
  14880. 11:40:37local time called the now method I will
  14881. 11:40:41display the time right now just for
  14882. 11:40:44testing purposes I want to be sure that
  14883. 11:40:46our alarm clock class is
  14884. 11:40:50working so we have to enter in a valid
  14885. 11:40:52alarm time I'll just set it to be 8:00
  14886. 11:40:54a.m. alarm is set and here's the current
  14887. 11:40:57time right now at least for me I'm
  14888. 11:41:00filming this video at 7:26 in the
  14889. 11:41:03morning it is going to be in military
  14890. 11:41:06time and then we have a bunch of
  14891. 11:41:08milliseconds after we know that we're
  14892. 11:41:10successfully calling the run method
  14893. 11:41:15so we have the time right now we're
  14894. 11:41:17going to compare times is the time right
  14895. 11:41:20now before our alarm time if it is we'll
  14896. 11:41:24continue sleeping we'll have our thread
  14897. 11:41:26sleep we can delete this print statement
  14898. 11:41:29I was just testing
  14899. 11:41:30it we need to check if the time right
  14900. 11:41:33now is before the alarm time if it is
  14901. 11:41:37we'll continue
  14902. 11:41:38waiting we can do this with the while
  14903. 11:41:41loop take our time right
  14904. 11:41:44now call the is before method we can
  14905. 11:41:48compare local times is the time right
  14906. 11:41:51now before our alarm
  14907. 11:41:54time if it is we're going to sleep for 1
  14908. 11:41:58second access the thread class sleep for
  14909. 11:42:011,000
  14910. 11:42:04milliseconds all right we do need to
  14911. 11:42:06surround this with a TR and catch block
  14912. 11:42:08let's do
  14913. 11:42:09that try and sleep for 1,000
  14914. 11:42:12milliseconds
  14915. 11:42:13if this thread is interrupted we'll
  14916. 11:42:15output the
  14917. 11:42:18following thread was
  14918. 11:42:22interrupted this is if we encounter an
  14919. 11:42:24interrupted
  14920. 11:42:26exception we're checking the time right
  14921. 11:42:28now is it before our alarm time if it is
  14922. 11:42:32let's output the time currently I'm
  14923. 11:42:34going to condense some of this code
  14924. 11:42:36outside of our while loop we're already
  14925. 11:42:37creating an object of now so why don't
  14926. 11:42:40we do this let's cut this line
  14927. 11:42:46and do the following access the class of
  14928. 11:42:49local time call the now method and then
  14929. 11:42:53method chain the is before
  14930. 11:42:56method basically every second we're
  14931. 11:42:59going to get the time right now and
  14932. 11:43:01compare it to our alarm time is the time
  14933. 11:43:04right now before our alarm time if this
  14934. 11:43:07while loop is running we'll output the
  14935. 11:43:10time so we'll output
  14936. 11:43:13local time called the now
  14937. 11:43:17method let's do a test
  14938. 11:43:21run ENT turn an alarm time I'll just say
  14939. 11:43:248:00 a.m. to keep it
  14940. 11:43:27simple alarm set for 8 a.m. and here's
  14941. 11:43:30the current time it updates every 1
  14942. 11:43:32second approximately every 1
  14943. 11:43:35second now let's do some formatting
  14944. 11:43:38because I don't like all these
  14945. 11:43:39milliseconds afterwards
  14946. 11:43:43here's what we could do we could create
  14947. 11:43:46an integer variable of ours set it equal
  14948. 11:43:51to access the class of local
  14949. 11:43:54time called the now method then method
  14950. 11:43:57chain the get hour method to return the
  14951. 11:44:00number of
  14952. 11:44:01hours then we're going to do this with
  14953. 11:44:04minutes int minutes get
  14954. 11:44:10minute int second
  14955. 11:44:15seconds get
  14956. 11:44:21second okay I'm going to show you what
  14957. 11:44:22happens if I print ours plus a
  14958. 11:44:29colon plus
  14959. 11:44:31minutes plus a colon
  14960. 11:44:35again plus
  14961. 11:44:38seconds here's what we have currently
  14962. 11:44:44I will set this to be 8
  14963. 11:44:46a.m. alarm set for 8 a.m. so this looks
  14964. 11:44:50pretty good but I would like some
  14965. 11:44:52leading
  14966. 11:44:58zeros we could do that with print F so
  14967. 11:45:01let's switch this
  14968. 11:45:03around print
  14969. 11:45:06f for print F We'll add some
  14970. 11:45:09placeholders which we can use with a
  14971. 11:45:11percent sign to display two leading
  14972. 11:45:13zeros we can use
  14973. 11:45:1502 if we're going to insert an integer
  14974. 11:45:18we have to use D for decimal so let's do
  14975. 11:45:22this three times 02 D percent 02
  14976. 11:45:29D we are inserting hours minutes and
  14977. 11:45:34seconds let's try it
  14978. 11:45:37again I will set the time to be 8:00
  14979. 11:45:40a.m.
  14980. 11:45:44oh and then I'm forgetting the colons
  14981. 11:45:46and a new line
  14982. 11:45:49character so let's insert some
  14983. 11:45:52colons and then add a new line character
  14984. 11:45:55to the end because I forgot to do
  14985. 11:46:04that that's much better and we have that
  14986. 11:46:07leading zero if one of these values is
  14987. 11:46:09one digit that looks pretty good now
  14988. 11:46:13what we'll do rather than updating the
  14989. 11:46:15time and placing it on a new line every
  14990. 11:46:171,000 milliseconds we're going to edit
  14991. 11:46:20it in
  14992. 11:46:21place here's how with our print F
  14993. 11:46:24statement we're going to insert back SLR
  14994. 11:46:28this is for carriage return it'll move
  14995. 11:46:30the cursor back to the beginning and
  14996. 11:46:32then we'll get rid of the new line
  14997. 11:46:34character it should look something like
  14998. 11:46:36this now
  14999. 11:46:44our time is going to update in place
  15000. 11:46:46rather than take up a new line for each
  15001. 11:46:48time it
  15002. 11:46:50updates I think this looks a lot
  15003. 11:46:55better all right so we're going to
  15004. 11:46:57condense some of these steps instead of
  15005. 11:46:59accessing the local time class three
  15006. 11:47:01separate times let's just do it once
  15007. 11:47:04we'll create a local time object named
  15008. 11:47:07now
  15009. 11:47:09equals access the class of local time
  15010. 11:47:12call the now
  15011. 11:47:15method when we assign ours we will
  15012. 11:47:18access our object of now now is our
  15013. 11:47:21local time now. getet hour for minutes
  15014. 11:47:25now doget minute and seconds now. getet
  15015. 11:47:30second and then we'll insert them using
  15016. 11:47:32our printf
  15017. 11:47:34statement or even instead of assigning
  15018. 11:47:37variables we could just call now doget
  15019. 11:47:41hour now. get
  15020. 11:47:44minute and now. getet second and we can
  15021. 11:47:48delete these variables cuz we don't need
  15022. 11:47:50them just for readability I'll place
  15023. 11:47:52these on a new
  15024. 11:47:56line and I'm just going to line them
  15025. 11:48:01up all right this should work too let's
  15026. 11:48:04make
  15027. 11:48:10sure yes it does
  15028. 11:48:14you can keep this code the way it was
  15029. 11:48:15before but I like it a little more
  15030. 11:48:18concise all right our while loop is
  15031. 11:48:22done so once we escape the while loop
  15032. 11:48:25that means the local time is our alarm
  15033. 11:48:27time or past our alarm time so for
  15034. 11:48:31testing purposes let's output the
  15035. 11:48:33following let's just say alarm
  15036. 11:48:36noises we haven't added any audio yet
  15037. 11:48:44for my alarm time I'm just going to set
  15038. 11:48:46it to be 1 minute past what it is
  15039. 11:48:48currently in this case 7:44 but pick a
  15040. 11:48:51different time depending on what time it
  15041. 11:48:52is for you
  15042. 11:48:54currently so I just need to wait for 20
  15043. 11:49:00seconds all right once we reach our
  15044. 11:49:02alarm time or it's past our alarm time
  15045. 11:49:05we display alarm noises and then exit
  15046. 11:49:08the
  15047. 11:49:09program I'm going to add a new line
  15048. 11:49:11character just before we display alarm
  15049. 11:49:14noises for our alarm we'll be playing
  15050. 11:49:17some music later just for now we're
  15051. 11:49:19going to play a single beep just to be
  15052. 11:49:21sure that everything is working
  15053. 11:49:22basically to have Java make a beep noise
  15054. 11:49:25you can do the following access the
  15055. 11:49:27class of tool kit call the
  15056. 11:49:31get
  15057. 11:49:32default toolkit
  15058. 11:49:36method call the beep method and then we
  15059. 11:49:39do need to import this
  15060. 11:49:41class once we escape the wild Loop we
  15061. 11:49:44will display alarm noises and make a
  15062. 11:49:46beep sound a single
  15063. 11:49:51beep I will set my alarm to be
  15064. 11:49:58747 I'll have to come back in one
  15065. 11:50:04minute there we
  15066. 11:50:06go we display our text of alarm noises
  15067. 11:50:10and hopefully you heard that beep so we
  15068. 11:50:12know that our alarm arm clock is working
  15069. 11:50:13to make a more sophisticated version
  15070. 11:50:15we're going to play an audio
  15071. 11:50:18file I have an audio file here it's a
  15072. 11:50:21wave file the following code isn't going
  15073. 11:50:23to be compatible with MP3s if you do
  15074. 11:50:26have an MP3 file you can always use an
  15075. 11:50:28online MP3 to wave converter that's what
  15076. 11:50:31I did to make this convenient for me I'm
  15077. 11:50:33going to place this wave file within my
  15078. 11:50:35project
  15079. 11:50:37folder here's my wave file going back to
  15080. 11:50:41our main Java file I will create a
  15081. 11:50:43string variable a file path string file
  15082. 11:50:48path
  15083. 11:50:49equals here I can list a relative file
  15084. 11:50:52path or an absolute file path this wave
  15085. 11:50:54file is within my project folder I just
  15086. 11:50:57need to list the file name so I'm going
  15087. 11:50:59to copy
  15088. 11:51:02path file
  15089. 11:51:04name and paste it within that
  15090. 11:51:06string with our alarm clock we're going
  15091. 11:51:09to send it our file path we'll send it
  15092. 11:51:12as the second argument along with the
  15093. 11:51:13alarm time but we have to set up the
  15094. 11:51:16Constructor so that we can accept
  15095. 11:51:21it with our alarm clock class we will
  15096. 11:51:24create a private final string of file
  15097. 11:51:31path then we'll have to set up the
  15098. 11:51:32parameters we are also accepting a
  15099. 11:51:35string of file path the location to that
  15100. 11:51:39file to play the
  15101. 11:51:40audio this. file file
  15102. 11:51:44path equals the file path that we
  15103. 11:51:48receive with playing audio there's a lot
  15104. 11:51:50of code we have to write I'll dedicate
  15105. 11:51:52an entire method just to playing
  15106. 11:51:54audio let's make this a private method
  15107. 11:51:57so only our alarm clock can access it
  15108. 11:52:00it's not going to return anything the
  15109. 11:52:02return type is void let's name this
  15110. 11:52:04method play
  15111. 11:52:09sound once we reach the end we will call
  15112. 11:52:13the play sound
  15113. 11:52:16method but we're going to pass in our
  15114. 11:52:18file
  15115. 11:52:20path so we need a parameter setup of
  15116. 11:52:23string file
  15117. 11:52:25path and now we are within the play
  15118. 11:52:27sound
  15119. 11:52:29method to play a w file we're going to
  15120. 11:52:32create a file object named audio file
  15121. 11:52:37equals a new file and then pass in our
  15122. 11:52:40file path
  15123. 11:52:44import this
  15124. 11:52:45class we'll be doing a lot of importing
  15125. 11:52:48as we go
  15126. 11:52:49along then we need to create an audio
  15127. 11:52:51input
  15128. 11:52:53stream audio input
  15129. 11:52:56stream we'll name this audio
  15130. 11:53:00stream
  15131. 11:53:02equals access the class of audio
  15132. 11:53:06system called the get audio input stream
  15133. 11:53:12method
  15134. 11:53:13yes that's a lot to
  15135. 11:53:14type then we will pass in our audio
  15136. 11:53:19file import this
  15137. 11:53:23class import the class for audio system
  15138. 11:53:26to there's a lot of different exceptions
  15139. 11:53:28that we'll need to catch such as if our
  15140. 11:53:31audio file is unsupported like it's an
  15141. 11:53:33MP3 file we'll need some try and catch
  15142. 11:53:37blocks try this code catch any
  15143. 11:53:40exceptions
  15144. 11:53:42here's one we'll start
  15145. 11:53:46with we will catch any un
  15146. 11:53:51supported audio file
  15147. 11:53:54exceptions which we will name e import
  15148. 11:53:58this
  15149. 11:54:00class if we encounter this exception
  15150. 11:54:03we'll output the
  15151. 11:54:05following audio file format is not
  15152. 11:54:09supported
  15153. 11:54:12if the audio is unavailable for some
  15154. 11:54:14reason let's say another resource is
  15155. 11:54:16accessing it we can catch that
  15156. 11:54:20too that exception is a
  15157. 11:54:23line unavailable exception line
  15158. 11:54:27unavailable exception of
  15159. 11:54:30e let's say something like audio is
  15160. 11:54:36unavailable import the
  15161. 11:54:40class then as a safety net
  15162. 11:54:43we will catch any IO exceptions input
  15163. 11:54:45output
  15164. 11:54:46exceptions IO exception e here we'll
  15165. 11:54:50just
  15166. 11:54:51say error
  15167. 11:54:53reading audio file it is good to catch
  15168. 11:54:57specific kinds of exceptions first which
  15169. 11:54:59we are doing this just acts as a safety
  15170. 11:55:04net so with modern Java you can try with
  15171. 11:55:07resources you'll automatically close
  15172. 11:55:10those resources when you're done with
  15173. 11:55:11them when we create our audio input
  15174. 11:55:14stream let's cut all this
  15175. 11:55:17code and paste it within the triy with
  15176. 11:55:20resources block then delete that
  15177. 11:55:24semicolon so we have our audio stream we
  15178. 11:55:27need to create a clip object a clip acts
  15179. 11:55:30as a player that plays an audio stream
  15180. 11:55:33within the tri block we will create a
  15181. 11:55:34clip object clip
  15182. 11:55:37clip equals access the class of audio
  15183. 11:55:42system system called the get clip method
  15184. 11:55:45to create a clip
  15185. 11:55:47object import this
  15186. 11:55:51class then we will use our
  15187. 11:55:54clip and open our audio stream
  15188. 11:56:00object to get the clip to play we will
  15189. 11:56:03call the start method of our clip
  15190. 11:56:05clip.art
  15191. 11:56:08method one issue we're probably going to
  15192. 11:56:10run into is that once we we start
  15193. 11:56:12playing the clip the program is going to
  15194. 11:56:14end immediately so we might not even
  15195. 11:56:16hear it so temporarily I'm just going to
  15196. 11:56:20sleep for a couple
  15197. 11:56:22seconds let's just sleep for 5
  15198. 11:56:25seconds and then I do need to add a
  15199. 11:56:27catch clause for any interrupted
  15200. 11:56:29exceptions we'll be deleting this don't
  15201. 11:56:31worry I just want to be sure that it
  15202. 11:56:36works I'm going to set it for
  15203. 11:56:40802 I got about 40 seconds
  15204. 11:56:48[Music]
  15205. 11:56:53we played our music for 5 seconds and
  15206. 11:56:55then we exit the program what I would
  15207. 11:56:57like to do instead is that we stop the
  15208. 11:57:00alarm if we hit enter here's how we can
  15209. 11:57:03set that up we can delete this method
  15210. 11:57:05where we sleep for 5 seconds instead
  15211. 11:57:08we'll wait for a user to hit enter and
  15212. 11:57:10delete that catch statement too
  15213. 11:57:13we're going to be using a scanner within
  15214. 11:57:16a big project different parts of your
  15215. 11:57:17project may require different scanners
  15216. 11:57:20but I like to just reuse the same one
  15217. 11:57:23the problem with having multiple
  15218. 11:57:24scanners is if they use system.in and
  15219. 11:57:28one scanner closes
  15220. 11:57:29it it's going to close system.in for all
  15221. 11:57:32of them so rather than close the scanner
  15222. 11:57:35at the end of our main Java file we're
  15223. 11:57:38going to do that within the alarm clock
  15224. 11:57:40class when everything is all done so
  15225. 11:57:43we'll do that at the end of the tri
  15226. 11:57:44block however our alarm clock class has
  15227. 11:57:47no idea what the scanner is we're going
  15228. 11:57:50to send it as an argument to the alarm
  15229. 11:57:52clock class so we'll need to set that up
  15230. 11:57:55we will pass in our scanner and reuse it
  15231. 11:57:58within the alarm clock
  15232. 11:58:03class within the alarm clock class we
  15233. 11:58:06will create a private final scanner
  15234. 11:58:10object which we will name
  15235. 11:58:13scanner import this
  15236. 11:58:16class set up the
  15237. 11:58:19parameters we will receive a scanner
  15238. 11:58:21object that we can use throughout the
  15239. 11:58:23alarm clock
  15240. 11:58:24class this do scanner equals the scanner
  15241. 11:58:28that we receive all right and now we
  15242. 11:58:31have access to that same scanner the
  15243. 11:58:33same one that we have instantiated at
  15244. 11:58:34the beginning of our
  15245. 11:58:36program and remember we are not closing
  15246. 11:58:38it at the end of the main Java file
  15247. 11:58:40anymore
  15248. 11:58:42once our alarm is playing we will wait
  15249. 11:58:44for some user input let's create a
  15250. 11:58:46prompt we'll say
  15251. 11:58:49press
  15252. 11:58:50enter to stop the
  15253. 11:58:55alarm I'm going to use
  15254. 11:58:59print we will use our scanner called the
  15255. 11:59:03next line
  15256. 11:59:05method our music is going to continue
  15257. 11:59:07playing until we hit
  15258. 11:59:09enter once the user hits enter we we are
  15259. 11:59:12going to take our clip and call the stop
  15260. 11:59:15method then close the scanner when we're
  15261. 11:59:17done with
  15262. 11:59:20it and that should be everything let's
  15263. 11:59:22run this one last
  15264. 11:59:25time I will set my alarm to be 807
  15265. 11:59:36[Music]
  15266. 11:59:44to get our alarm to stop we just have to
  15267. 11:59:46press
  15268. 11:59:48[Music]
  15269. 11:59:49enter and the alarm stops and we exit
  15270. 11:59:52the
  15271. 11:59:53program all right everybody that is a
  15272. 11:59:55working alarm clock that you can write
  15273. 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.