[{"Name":"Arrays","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Arrays","Duration":"6m 51s","ChapterTopicVideoID":27226,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":"https://www.proprep.uk/Images/Videos_Thumbnails/27226.jpeg","UploadDate":"2021-11-11T07:22:13.7170000","DurationForVideoObject":"PT6M51S","Description":null,"MetaTitle":"Arrays: Video + Workbook | Proprep","MetaDescription":"Further Programming Concepts - Arrays. Watch the video made by an expert in the field. Download the workbook and maximize your learning.","Canonical":"https://www.proprep.uk/general-modules/all/principles-of-programing/further-programming-concepts/arrays/vid28347","VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.820","Text":"Hello, welcome to this video on arrays."},{"Start":"00:02.820 ","End":"00:03.960","Text":"By the end of this section,"},{"Start":"00:03.960 ","End":"00:07.575","Text":"we\u0027ll be able to state the purpose of an array within a program,"},{"Start":"00:07.575 ","End":"00:10.305","Text":"explain how arrays are organized in memory,"},{"Start":"00:10.305 ","End":"00:15.915","Text":"and create an access elements from single or multi-dimensional arrays."},{"Start":"00:15.915 ","End":"00:20.460","Text":"Earlier in the course, we learned that variables are used in programs to store data."},{"Start":"00:20.460 ","End":"00:22.440","Text":"In imperative languages,"},{"Start":"00:22.440 ","End":"00:25.950","Text":"they are fundamental to being able to create a useful program."},{"Start":"00:25.950 ","End":"00:28.920","Text":"However, there is 1 serious flaw with variables."},{"Start":"00:28.920 ","End":"00:32.550","Text":"They\u0027re used to store individual isolated values"},{"Start":"00:32.550 ","End":"00:35.790","Text":"which are located using a unique identifier."},{"Start":"00:35.790 ","End":"00:38.130","Text":"What if you wanted to store a set of"},{"Start":"00:38.130 ","End":"00:42.555","Text":"data items that were related to each other in some way?"},{"Start":"00:42.555 ","End":"00:46.520","Text":"For example, say we wanted to gather the amount of rainfall in"},{"Start":"00:46.520 ","End":"00:51.860","Text":"millimeters in a particular location over consecutive days of the week?"},{"Start":"00:51.860 ","End":"00:57.544","Text":"To store these, it will be cumbersome to create multiple variables with similar names."},{"Start":"00:57.544 ","End":"00:59.610","Text":"For example, day 1 rain,"},{"Start":"00:59.610 ","End":"01:02.180","Text":"day 2 rain, day 3 rain, and so on."},{"Start":"01:02.180 ","End":"01:06.050","Text":"It would also be cumbersome to create elegant code to"},{"Start":"01:06.050 ","End":"01:10.690","Text":"access the values of each of these variables and to do calculations."},{"Start":"01:10.690 ","End":"01:13.730","Text":"For example, to calculate average rainfall,"},{"Start":"01:13.730 ","End":"01:17.884","Text":"we\u0027d have to specify 7 different variable names in an expression."},{"Start":"01:17.884 ","End":"01:23.869","Text":"Also, what if we wanted to create a 3-day average or a 10-day or 30-day average?"},{"Start":"01:23.869 ","End":"01:28.090","Text":"We\u0027d end up writing very long repetitive lines of code."},{"Start":"01:28.090 ","End":"01:31.595","Text":"Fortunately, there\u0027s a better way of doing these sorts of operations,"},{"Start":"01:31.595 ","End":"01:36.095","Text":"which involves grouping related data into something called an array."},{"Start":"01:36.095 ","End":"01:40.160","Text":"An array is a collection of data elements organized in a manner which"},{"Start":"01:40.160 ","End":"01:45.110","Text":"allows efficient access and modification of individual values."},{"Start":"01:45.110 ","End":"01:52.385","Text":"An element is a single data item within an array whose value can be accessed or modified."},{"Start":"01:52.385 ","End":"01:58.490","Text":"An index is a value used to locate a specific element within an array."},{"Start":"01:58.490 ","End":"02:01.520","Text":"The usefulness of the array comes in the ability to quickly and"},{"Start":"02:01.520 ","End":"02:04.745","Text":"predictably access elements using an index."},{"Start":"02:04.745 ","End":"02:06.140","Text":"At the machine level,"},{"Start":"02:06.140 ","End":"02:09.350","Text":"this is enabled by the ability of CPUs to access"},{"Start":"02:09.350 ","End":"02:13.310","Text":"memory locations using something called indexed addressing."},{"Start":"02:13.310 ","End":"02:17.945","Text":"Arrays are usually arranged in memory in consecutive memory locations."},{"Start":"02:17.945 ","End":"02:25.580","Text":"Here you can see an array of 4 elements occupying 4 memory locations from 100-103."},{"Start":"02:25.580 ","End":"02:30.445","Text":"A base address in memory is used to locate the array itself."},{"Start":"02:30.445 ","End":"02:35.840","Text":"Then individual data items within the array are accessed using the index."},{"Start":"02:35.840 ","End":"02:38.420","Text":"To locate a particular element in an array,"},{"Start":"02:38.420 ","End":"02:41.120","Text":"we need the base address and the index and the"},{"Start":"02:41.120 ","End":"02:44.885","Text":"2 added together will give us the address of the data."},{"Start":"02:44.885 ","End":"02:49.280","Text":"This example assumes that each item of data in the array is"},{"Start":"02:49.280 ","End":"02:53.930","Text":"1 byte and each memory location stores exactly 1 byte."},{"Start":"02:53.930 ","End":"02:57.575","Text":"If we wanted to access the element located by index 2,"},{"Start":"02:57.575 ","End":"02:59.720","Text":"we\u0027d add 2 to the base address,"},{"Start":"02:59.720 ","End":"03:02.030","Text":"giving us address 102,"},{"Start":"03:02.030 ","End":"03:07.550","Text":"and the element stored at that location has the value 76."},{"Start":"03:07.550 ","End":"03:10.880","Text":"Similarly, for the element located by index 3,"},{"Start":"03:10.880 ","End":"03:13.055","Text":"we\u0027d add 3 to the base address,"},{"Start":"03:13.055 ","End":"03:15.380","Text":"giving us address 103,"},{"Start":"03:15.380 ","End":"03:19.820","Text":"and the element stored at that location has the value 80."},{"Start":"03:19.820 ","End":"03:22.985","Text":"If the data occupied more than 1 memory location,"},{"Start":"03:22.985 ","End":"03:26.240","Text":"for example, a 32-bit int datatype,"},{"Start":"03:26.240 ","End":"03:29.930","Text":"we\u0027d have to multiply the index by the size of an int,"},{"Start":"03:29.930 ","End":"03:31.340","Text":"which is 4 bytes,"},{"Start":"03:31.340 ","End":"03:34.055","Text":"and then we\u0027d add that to the base address"},{"Start":"03:34.055 ","End":"03:37.295","Text":"to find the address of each element of the array."},{"Start":"03:37.295 ","End":"03:40.520","Text":"Arrays tend to only store 1 type of data because we need"},{"Start":"03:40.520 ","End":"03:43.775","Text":"to know the size of the data to access each element."},{"Start":"03:43.775 ","End":"03:46.595","Text":"If each item in the array was a different type of data,"},{"Start":"03:46.595 ","End":"03:49.790","Text":"we couldn\u0027t do a simple calculation like we can here,"},{"Start":"03:49.790 ","End":"03:53.990","Text":"where every element occupies 4 consecutive memory locations."},{"Start":"03:53.990 ","End":"03:57.935","Text":"Most high-level programming languages use 0 indexing,"},{"Start":"03:57.935 ","End":"03:59.900","Text":"meaning the first element of the array,"},{"Start":"03:59.900 ","End":"04:03.455","Text":"sometimes referred to as the 0th element of the array,"},{"Start":"04:03.455 ","End":"04:06.755","Text":"is accessed using the index 0."},{"Start":"04:06.755 ","End":"04:09.620","Text":"You can see why that\u0027s convenient from this image."},{"Start":"04:09.620 ","End":"04:13.130","Text":"The 0th element is stored at the base address because"},{"Start":"04:13.130 ","End":"04:17.300","Text":"0 times 4 is 0 so we\u0027re just left with the base address."},{"Start":"04:17.300 ","End":"04:19.850","Text":"Fortunately, we don\u0027t need to worry about the details of"},{"Start":"04:19.850 ","End":"04:22.985","Text":"memory addresses when working in high-level languages."},{"Start":"04:22.985 ","End":"04:26.630","Text":"A rise in a high-level language will have an identifier which"},{"Start":"04:26.630 ","End":"04:30.550","Text":"the compiler or interpreter can translate into a memory address."},{"Start":"04:30.550 ","End":"04:34.190","Text":"Any specific element of the array can then be accessed by"},{"Start":"04:34.190 ","End":"04:38.660","Text":"specifying the array and to identify an index value."},{"Start":"04:38.660 ","End":"04:42.410","Text":"Here, my array has 4 elements."},{"Start":"04:42.410 ","End":"04:45.440","Text":"To access an element in high-level program,"},{"Start":"04:45.440 ","End":"04:48.095","Text":"we simply specify the name of the array,"},{"Start":"04:48.095 ","End":"04:50.014","Text":"a pair of square brackets,"},{"Start":"04:50.014 ","End":"04:53.555","Text":"and an index value inside the square brackets."},{"Start":"04:53.555 ","End":"05:02.180","Text":"This line of code would output 72 to the screen because my array 0 contains 72."},{"Start":"05:02.180 ","End":"05:05.180","Text":"To change the value stored in a specific element,"},{"Start":"05:05.180 ","End":"05:08.195","Text":"we simply put the array name, brackets,"},{"Start":"05:08.195 ","End":"05:12.669","Text":"and index on the left-hand side of an assignment operator,"},{"Start":"05:12.669 ","End":"05:18.560","Text":"and then the value we want to assign on the right-hand side of the assignment operator."},{"Start":"05:18.560 ","End":"05:23.735","Text":"What was previously in that array element will be overwritten with the new value."},{"Start":"05:23.735 ","End":"05:27.815","Text":"There are 2 steps to create an array in most languages."},{"Start":"05:27.815 ","End":"05:32.060","Text":"Give the array an identifier and specify its datatype."},{"Start":"05:32.060 ","End":"05:35.060","Text":"For example, in Java, we do this."},{"Start":"05:35.060 ","End":"05:39.890","Text":"Note the difference, there are a pair of square brackets next to the datatype."},{"Start":"05:39.890 ","End":"05:43.010","Text":"You then create the array in memory by specifying"},{"Start":"05:43.010 ","End":"05:46.490","Text":"the size or by initializing it with values."},{"Start":"05:46.490 ","End":"05:49.115","Text":"Again, as an example in Java,"},{"Start":"05:49.115 ","End":"05:56.885","Text":"you\u0027d use new int 6 to create an empty integer array with 6 elements."},{"Start":"05:56.885 ","End":"06:00.640","Text":"These 2 lines are usually executed together like so."},{"Start":"06:00.640 ","End":"06:03.860","Text":"This third line does the same as the previous 2,"},{"Start":"06:03.860 ","End":"06:06.905","Text":"the first half declaring the datatype and identifier."},{"Start":"06:06.905 ","End":"06:08.690","Text":"The second bar in blue,"},{"Start":"06:08.690 ","End":"06:10.865","Text":"setting the size of the array."},{"Start":"06:10.865 ","End":"06:14.540","Text":"Alternatively, an array can be given initial values when it\u0027s"},{"Start":"06:14.540 ","End":"06:20.150","Text":"declared by specifying the values in braces separated by commas."},{"Start":"06:20.150 ","End":"06:23.240","Text":"We don\u0027t need to worry about a size of the array here as it will"},{"Start":"06:23.240 ","End":"06:26.855","Text":"automatically be set by the number of items specified."},{"Start":"06:26.855 ","End":"06:28.250","Text":"In this case 4,"},{"Start":"06:28.250 ","End":"06:30.410","Text":"if an initial value is not supplied,"},{"Start":"06:30.410 ","End":"06:33.890","Text":"each array element will be given a default initial value."},{"Start":"06:33.890 ","End":"06:37.175","Text":"For example, a value of 0 for an array of integers,"},{"Start":"06:37.175 ","End":"06:38.930","Text":"or for an array of chars,"},{"Start":"06:38.930 ","End":"06:40.940","Text":"the ASCII null character."},{"Start":"06:40.940 ","End":"06:42.575","Text":"That\u0027s it for this video."},{"Start":"06:42.575 ","End":"06:45.410","Text":"In the next one, we\u0027ll look at how we can quickly access and"},{"Start":"06:45.410 ","End":"06:51.930","Text":"process array data using a loop to iterate through each element. Thanks for watching."}],"ID":28347},{"Watched":false,"Name":"Iterating And Multiple Dimensional Arrays","Duration":"8m 37s","ChapterTopicVideoID":27227,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.935","Text":"Hello, welcome back."},{"Start":"00:01.935 ","End":"00:05.490","Text":"We previously saw how storing data in individual variables can be"},{"Start":"00:05.490 ","End":"00:09.285","Text":"limiting and can create lengthy and unmanageable code."},{"Start":"00:09.285 ","End":"00:13.820","Text":"The array can be used as an alternative to multiple individual variables,"},{"Start":"00:13.820 ","End":"00:18.810","Text":"so long as the data is all of the same datatype and the data is related in some way."},{"Start":"00:18.810 ","End":"00:21.680","Text":"Let\u0027s take our previous rainfall example."},{"Start":"00:21.680 ","End":"00:27.565","Text":"We could store consecutive days of rainfall in an array of 31 floats."},{"Start":"00:27.565 ","End":"00:30.135","Text":"If we wanted to take a 3-day average,"},{"Start":"00:30.135 ","End":"00:33.210","Text":"we could use 3 consecutive elements in the array,"},{"Start":"00:33.210 ","End":"00:34.740","Text":"add up their contents,"},{"Start":"00:34.740 ","End":"00:36.300","Text":"and divide by 3."},{"Start":"00:36.300 ","End":"00:38.025","Text":"For a 7 day average,"},{"Start":"00:38.025 ","End":"00:40.725","Text":"we use 7 consecutive elements in the array,"},{"Start":"00:40.725 ","End":"00:42.119","Text":"add up their contents,"},{"Start":"00:42.119 ","End":"00:43.905","Text":"and divide by 7."},{"Start":"00:43.905 ","End":"00:47.090","Text":"If we want to take an average over the first 7 days,"},{"Start":"00:47.090 ","End":"00:54.680","Text":"we iterate the index between 0 and 6 and access 1 element at a time using the index."},{"Start":"00:54.680 ","End":"00:58.685","Text":"Here, the first index of 0 would give us the element value"},{"Start":"00:58.685 ","End":"01:02.375","Text":"1.4 and if we add that to the running total,"},{"Start":"01:02.375 ","End":"01:04.345","Text":"it would just be 1.4."},{"Start":"01:04.345 ","End":"01:07.550","Text":"Index 1 whose element value is 2.3,"},{"Start":"01:07.550 ","End":"01:13.010","Text":"added to the total would give us 3.7, and so on."},{"Start":"01:13.010 ","End":"01:19.145","Text":"We can divide the total by 7 and get an average temperature over those 7 days."},{"Start":"01:19.145 ","End":"01:22.280","Text":"The code to do what we\u0027ve just described is remarkably short,"},{"Start":"01:22.280 ","End":"01:24.815","Text":"thanks to the power of the loop and the array."},{"Start":"01:24.815 ","End":"01:28.740","Text":"Once we\u0027ve declared our variable here,"},{"Start":"01:28.740 ","End":"01:32.510","Text":"we just need 1 short line in our loop and"},{"Start":"01:32.510 ","End":"01:36.515","Text":"another to calculate and display the average rainfall."},{"Start":"01:36.515 ","End":"01:37.850","Text":"Not only is it short,"},{"Start":"01:37.850 ","End":"01:39.905","Text":"it\u0027s also easier to change."},{"Start":"01:39.905 ","End":"01:43.880","Text":"It would be a simple case of changing a couple of numbers to make the code"},{"Start":"01:43.880 ","End":"01:48.864","Text":"calculate a 10 day average or a monthly average."},{"Start":"01:48.864 ","End":"01:53.870","Text":"An array could also be used in another way to hold parts of an overall output."},{"Start":"01:53.870 ","End":"01:56.900","Text":"Take the example of time stored in seconds."},{"Start":"01:56.900 ","End":"02:00.770","Text":"Once the number of seconds is greater than the integer value 59,"},{"Start":"02:00.770 ","End":"02:05.015","Text":"we\u0027d much rather express the time in minutes, hours, and seconds."},{"Start":"02:05.015 ","End":"02:08.990","Text":"You could store the amount of hours in the first element of an array,"},{"Start":"02:08.990 ","End":"02:12.710","Text":"the amount of minutes in the second element of the array,"},{"Start":"02:12.710 ","End":"02:17.045","Text":"and the number of seconds in the third element of the array."},{"Start":"02:17.045 ","End":"02:21.740","Text":"7,410 seconds, is converted into an hours,"},{"Start":"02:21.740 ","End":"02:25.055","Text":"minutes and seconds representation as 2,"},{"Start":"02:25.055 ","End":"02:27.695","Text":"3 and 30, respectively,"},{"Start":"02:27.695 ","End":"02:31.565","Text":"each part being stored in a different element in the array."},{"Start":"02:31.565 ","End":"02:34.310","Text":"The code would look something like this."},{"Start":"02:34.310 ","End":"02:36.305","Text":"We declare first of all,"},{"Start":"02:36.305 ","End":"02:38.390","Text":"an array of 3 elements,"},{"Start":"02:38.390 ","End":"02:42.770","Text":"we allocate the first element\u0027s value using this formula,"},{"Start":"02:42.770 ","End":"02:46.055","Text":"the second element\u0027s value using this formula,"},{"Start":"02:46.055 ","End":"02:49.100","Text":"and the third element\u0027s value using this formula."},{"Start":"02:49.100 ","End":"02:52.220","Text":"To print out the time in hours, minutes and seconds,"},{"Start":"02:52.220 ","End":"02:56.150","Text":"we simply print the 3 element values."},{"Start":"02:56.150 ","End":"03:00.395","Text":"Wherever arrays really come into their own is when you have multiple dimensions."},{"Start":"03:00.395 ","End":"03:03.560","Text":"The rainfall example was a 1-dimensional array."},{"Start":"03:03.560 ","End":"03:07.460","Text":"We located an element by specifying a single index."},{"Start":"03:07.460 ","End":"03:10.300","Text":"Here\u0027s another 1-dimensional array."},{"Start":"03:10.300 ","End":"03:14.000","Text":"We can use these 1-dimensional arrays to look up values."},{"Start":"03:14.000 ","End":"03:18.935","Text":"For example, what\u0027s the nth term in the Fibonacci sequence?"},{"Start":"03:18.935 ","End":"03:24.184","Text":"The 6th element in the array gives us the 6th term in the Fibonacci sequence,"},{"Start":"03:24.184 ","End":"03:27.005","Text":"where each number is the sum of the preceding 2 numbers."},{"Start":"03:27.005 ","End":"03:31.625","Text":"You can think of a 1D array as a single row of a table which has many columns."},{"Start":"03:31.625 ","End":"03:34.465","Text":"In this case, there are 8 columns."},{"Start":"03:34.465 ","End":"03:37.130","Text":"A 2-dimensional array can be thought of as a table with"},{"Start":"03:37.130 ","End":"03:39.860","Text":"multiple rows and multiple columns."},{"Start":"03:39.860 ","End":"03:42.950","Text":"Instead of locating the element with a single index,"},{"Start":"03:42.950 ","End":"03:46.760","Text":"we need to supply 2 indices to locate an element."},{"Start":"03:46.760 ","End":"03:52.325","Text":"Here we see a sequence of char stored in a 2D array with 4 rows and 4 columns."},{"Start":"03:52.325 ","End":"03:54.860","Text":"The characters stored in row 0,"},{"Start":"03:54.860 ","End":"04:00.180","Text":"Column 0 is capital T. Row 2,"},{"Start":"04:00.180 ","End":"04:05.525","Text":"Column 1 contains the lowercase letter n."},{"Start":"04:05.525 ","End":"04:12.280","Text":"Row 3 Column 3 has the lowercase letter y."},{"Start":"04:12.280 ","End":"04:15.890","Text":"We could use a 2D array for our weather example and"},{"Start":"04:15.890 ","End":"04:19.400","Text":"now instead of having a single rainfall figure for the day,"},{"Start":"04:19.400 ","End":"04:23.500","Text":"we can store the maximum and minimum temperatures for the day too."},{"Start":"04:23.500 ","End":"04:26.750","Text":"Here we have 7 columns representing the days."},{"Start":"04:26.750 ","End":"04:31.355","Text":"For each day, the blue items on Row 0 represent rainfall."},{"Start":"04:31.355 ","End":"04:32.960","Text":"The green items on Row 1,"},{"Start":"04:32.960 ","End":"04:37.850","Text":"the maximum temperature and the red items on Row 2, the low temperature."},{"Start":"04:37.850 ","End":"04:40.760","Text":"In order to declare the array above,"},{"Start":"04:40.760 ","End":"04:45.730","Text":"we need to specify both dimensions using a pair of square brackets."},{"Start":"04:45.730 ","End":"04:48.375","Text":"As we could for 1-dimensional arrays,"},{"Start":"04:48.375 ","End":"04:52.220","Text":"we can also specify the initial values for each element if we want to,"},{"Start":"04:52.220 ","End":"04:53.750","Text":"when declaring the array."},{"Start":"04:53.750 ","End":"04:58.595","Text":"But this time we\u0027d need to use nested braces to give the values for each row,"},{"Start":"04:58.595 ","End":"05:00.920","Text":"and then the columns within that row."},{"Start":"05:00.920 ","End":"05:04.220","Text":"The outer braces here are shown in black."},{"Start":"05:04.220 ","End":"05:07.610","Text":"Then each row is shown in a different color."},{"Start":"05:07.610 ","End":"05:12.545","Text":"Red would be the first row here as it\u0027s declared first."},{"Start":"05:12.545 ","End":"05:15.410","Text":"Then when the inner brace is closed,"},{"Start":"05:15.410 ","End":"05:20.090","Text":"we see a coma and then the next row is declared in blue."},{"Start":"05:20.090 ","End":"05:24.935","Text":"Then the same again for the final row in green."},{"Start":"05:24.935 ","End":"05:32.074","Text":"Then a closing outer brace to show the whole array has been given its initial values."},{"Start":"05:32.074 ","End":"05:34.625","Text":"We can still iterate through a 2D array,"},{"Start":"05:34.625 ","End":"05:38.525","Text":"but now we\u0027d need 2 nested loops to be able to access every element."},{"Start":"05:38.525 ","End":"05:41.345","Text":"The inner loop here is iterating the column,"},{"Start":"05:41.345 ","End":"05:44.195","Text":"and the outer loop is iterating the row."},{"Start":"05:44.195 ","End":"05:46.025","Text":"If we ran this code,"},{"Start":"05:46.025 ","End":"05:48.905","Text":"this is the output we\u0027d expect to see,"},{"Start":"05:48.905 ","End":"05:54.080","Text":"1 and the first column of the first row would be output."},{"Start":"05:54.080 ","End":"05:58.475","Text":"Then the second column of the first row, the third column."},{"Start":"05:58.475 ","End":"06:01.730","Text":"Then we move on to the next row and output the first column,"},{"Start":"06:01.730 ","End":"06:04.655","Text":"then the second column, and so on."},{"Start":"06:04.655 ","End":"06:07.460","Text":"It\u0027s possible to go beyond 2D by"},{"Start":"06:07.460 ","End":"06:11.750","Text":"specifying another pair of square brackets to add another dimension."},{"Start":"06:11.750 ","End":"06:16.790","Text":"You can think of the blue face here as the 2D array we saw earlier,"},{"Start":"06:16.790 ","End":"06:20.450","Text":"with rows going across and columns going down."},{"Start":"06:20.450 ","End":"06:22.010","Text":"Then behind this blue face,"},{"Start":"06:22.010 ","End":"06:25.235","Text":"you have layers which are the third dimension."},{"Start":"06:25.235 ","End":"06:28.430","Text":"You can think of the position of any element in this cube as"},{"Start":"06:28.430 ","End":"06:31.775","Text":"being specified by layer, row and column."},{"Start":"06:31.775 ","End":"06:35.135","Text":"Here, we have 3 by 3 by 3,"},{"Start":"06:35.135 ","End":"06:38.060","Text":"which is 27 elements in total."},{"Start":"06:38.060 ","End":"06:42.230","Text":"Each, with their own value and access using 3 separate indices,"},{"Start":"06:42.230 ","End":"06:44.450","Text":"it\u0027s hard to show this on a 2D screen,"},{"Start":"06:44.450 ","End":"06:46.970","Text":"but look at it as layer, row and column."},{"Start":"06:46.970 ","End":"06:49.280","Text":"Looking from the blue phase here,"},{"Start":"06:49.280 ","End":"06:55.835","Text":"the top line here is defining the entire blue face here."},{"Start":"06:55.835 ","End":"07:01.190","Text":"The next line here would be defining the layer here behind"},{"Start":"07:01.190 ","End":"07:08.060","Text":"the first blue face and the final line here would be displaying the layer at the back."},{"Start":"07:08.060 ","End":"07:10.340","Text":"Layer 0, Row 1."},{"Start":"07:10.340 ","End":"07:12.245","Text":"Remember we\u0027re counting from 0."},{"Start":"07:12.245 ","End":"07:19.440","Text":"Column 0 would give us the value 4 with looking at Layer 0,"},{"Start":"07:19.440 ","End":"07:21.865","Text":"Row 1, Column 1,"},{"Start":"07:21.865 ","End":"07:24.065","Text":"which gives us this blue square here."},{"Start":"07:24.065 ","End":"07:28.630","Text":"Layer 1, counting from 0, Row 1,"},{"Start":"07:28.630 ","End":"07:32.900","Text":"Column 0 gives 24,"},{"Start":"07:32.900 ","End":"07:34.655","Text":"and it will be this blue square,"},{"Start":"07:34.655 ","End":"07:36.530","Text":"but the layer behind it,"},{"Start":"07:36.530 ","End":"07:39.360","Text":"so behind this 1 here."},{"Start":"07:39.360 ","End":"07:41.550","Text":"Finally, Layer 2,"},{"Start":"07:41.550 ","End":"07:44.760","Text":"the back layer at the back here, Row 1,"},{"Start":"07:44.760 ","End":"07:52.975","Text":"Column 0 gives us 34 and that would be 2 layers behind this blue square here."},{"Start":"07:52.975 ","End":"07:55.700","Text":"With the help of a 3D cube as a reference point,"},{"Start":"07:55.700 ","End":"08:01.865","Text":"we can visualize how a 3-dimensional array can be used and how elements can be located."},{"Start":"08:01.865 ","End":"08:03.950","Text":"We could even have a 4D array,"},{"Start":"08:03.950 ","End":"08:08.045","Text":"perhaps storing a sequence of 3D coordinates that change over time,"},{"Start":"08:08.045 ","End":"08:11.375","Text":"with time being stored as the fourth dimension."},{"Start":"08:11.375 ","End":"08:13.090","Text":"That\u0027s it for now on arrays,"},{"Start":"08:13.090 ","End":"08:16.595","Text":"a really useful way of storing and processing data."},{"Start":"08:16.595 ","End":"08:20.695","Text":"In this section, we learned how to state the purpose of an array within a program,"},{"Start":"08:20.695 ","End":"08:23.600","Text":"to explain how arrays are organized in memory,"},{"Start":"08:23.600 ","End":"08:28.580","Text":"and to create an access elements from single or multi-dimensional arrays."},{"Start":"08:28.580 ","End":"08:31.280","Text":"Be sure to deepen your understanding through working"},{"Start":"08:31.280 ","End":"08:34.114","Text":"through the exercises in the workbook."},{"Start":"08:34.114 ","End":"08:37.530","Text":"I\u0027ll see you soon. Thank you for watching."}],"ID":28348},{"Watched":false,"Name":"Exercise 1","Duration":"7m 25s","ChapterTopicVideoID":27228,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.910","Text":"Welcome to this first exercise in which we\u0027ve been asked to create"},{"Start":"00:02.910 ","End":"00:06.615","Text":"a new project within a class called arraysApp."},{"Start":"00:06.615 ","End":"00:11.250","Text":"Inside the class, we then must create a method called outputOddNumbers."},{"Start":"00:11.250 ","End":"00:15.330","Text":"In Part a, we\u0027re asked to declare an array of bytes with"},{"Start":"00:15.330 ","End":"00:20.250","Text":"the following elements and a1 states the values of each of those elements."},{"Start":"00:20.250 ","End":"00:24.780","Text":"Part b, we\u0027re asked to use a for loop to iterate through the array,"},{"Start":"00:24.780 ","End":"00:30.360","Text":"outputting to the screen each element of the array on a new line and then in Part c,"},{"Start":"00:30.360 ","End":"00:33.435","Text":"we\u0027re asked to modify the code inside the loop"},{"Start":"00:33.435 ","End":"00:37.690","Text":"so that numbers are output only if they are odd."},{"Start":"00:37.850 ","End":"00:44.565","Text":"We create a new project and within a class arraysApp, as we\u0027ve been asked."},{"Start":"00:44.565 ","End":"00:50.540","Text":"Then we can proceed to create a method called outputOddNumbers."},{"Start":"00:50.540 ","End":"00:52.160","Text":"I\u0027ll go ahead and do that."},{"Start":"00:52.160 ","End":"00:56.625","Text":"Then, within the outputOddNumbers method,"},{"Start":"00:56.625 ","End":"01:00.110","Text":"we\u0027ve been asked in Part a to create"},{"Start":"01:00.110 ","End":"01:04.655","Text":"a array of bytes and with a set of elements that are given to us."},{"Start":"01:04.655 ","End":"01:06.695","Text":"To create an array,"},{"Start":"01:06.695 ","End":"01:09.740","Text":"you give the datatype first and it\u0027s a byte datatype,"},{"Start":"01:09.740 ","End":"01:13.955","Text":"but you follow it with square brackets to say it\u0027s not single byte,"},{"Start":"01:13.955 ","End":"01:15.905","Text":"it\u0027s an array of bytes."},{"Start":"01:15.905 ","End":"01:20.795","Text":"Then we give an identifier for our array."},{"Start":"01:20.795 ","End":"01:25.400","Text":"Let\u0027s call mine numbersArray and then you"},{"Start":"01:25.400 ","End":"01:30.350","Text":"can either size the array by saying new and byte and then how many one."},{"Start":"01:30.350 ","End":"01:33.110","Text":"Or in our case, we\u0027re going to initialize it with values."},{"Start":"01:33.110 ","End":"01:34.400","Text":"So we don\u0027t need to do that."},{"Start":"01:34.400 ","End":"01:40.985","Text":"We put a pair of braces followed by a semicolon and inside those braces,"},{"Start":"01:40.985 ","End":"01:42.950","Text":"we can put the elements of the array."},{"Start":"01:42.950 ","End":"01:45.560","Text":"The initial values are separated by commas."},{"Start":"01:45.560 ","End":"01:52.901","Text":"Mine we have 1,"},{"Start":"01:52.901 ","End":"01:54.387","Text":"2, 4, 5, 10, 11, 12, 14, 17,"},{"Start":"01:54.387 ","End":"01:57.555","Text":"20, and 21."},{"Start":"01:57.555 ","End":"02:01.220","Text":"There is our array declared with initial values,"},{"Start":"02:01.220 ","End":"02:02.810","Text":"and it\u0027s a byte datatype,"},{"Start":"02:02.810 ","End":"02:05.240","Text":"and the identifier is numbers array."},{"Start":"02:05.240 ","End":"02:07.850","Text":"It wasn\u0027t stated what you should call it,"},{"Start":"02:07.850 ","End":"02:11.060","Text":"so just come up with a sensible name for it."},{"Start":"02:11.060 ","End":"02:14.300","Text":"Not particularly sensible in a very large program,"},{"Start":"02:14.300 ","End":"02:19.340","Text":"but for just the small demo application we\u0027re doing, that should be fine."},{"Start":"02:19.340 ","End":"02:23.180","Text":"In Part b, then we\u0027re asked to use a for loop to iterate through"},{"Start":"02:23.180 ","End":"02:27.560","Text":"the array outputting to the screen each element of the array on a new line."},{"Start":"02:27.560 ","End":"02:30.680","Text":"We\u0027ll need a for loop as we\u0027ve looked at previously,"},{"Start":"02:30.680 ","End":"02:33.200","Text":"for loop has the following syntax."},{"Start":"02:33.200 ","End":"02:38.480","Text":"We start with variable that we are going to use for the iteration and its initial value."},{"Start":"02:38.480 ","End":"02:43.340","Text":"So I have identifying as minus i for index and the initial value of"},{"Start":"02:43.340 ","End":"02:48.680","Text":"0 because I want to start at element 0 in the array and then when will I end?"},{"Start":"02:48.680 ","End":"02:51.482","Text":"Well, I can count that this has 2, 4,"},{"Start":"02:51.482 ","End":"02:54.525","Text":"6, 8, 10, 11 elements."},{"Start":"02:54.525 ","End":"02:58.815","Text":"So I could say i is less than 11,"},{"Start":"02:58.815 ","End":"03:00.590","Text":"and that would be fine."},{"Start":"03:00.590 ","End":"03:06.950","Text":"But actually it\u0027s better practice to use the length of the array itself."},{"Start":"03:06.950 ","End":"03:09.980","Text":"If we added another element to that array would still work"},{"Start":"03:09.980 ","End":"03:13.370","Text":"rather than me having to change this number from 11 to 12 or whatever."},{"Start":"03:13.370 ","End":"03:18.500","Text":"We use, the attribute that\u0027s built into every array in Java,"},{"Start":"03:18.500 ","End":"03:21.665","Text":"called length, it will return us"},{"Start":"03:21.665 ","End":"03:26.450","Text":"the current length of the array and we can compare i to that."},{"Start":"03:26.450 ","End":"03:28.160","Text":"That\u0027s a better way of doing it."},{"Start":"03:28.160 ","End":"03:31.520","Text":"The last part of a for loop is to say how are we going to increment,"},{"Start":"03:31.520 ","End":"03:33.400","Text":"and we\u0027re going to increment by 1"},{"Start":"03:33.400 ","End":"03:37.230","Text":"because we\u0027re going to go through each element 1 at a time."},{"Start":"03:37.250 ","End":"03:42.230","Text":"That\u0027s has done for the opening line of the for loop."},{"Start":"03:42.230 ","End":"03:44.615","Text":"What are we actually going to do inside the loop?"},{"Start":"03:44.615 ","End":"03:48.865","Text":"We\u0027re just going to print out each individual element."},{"Start":"03:48.865 ","End":"03:53.210","Text":"I\u0027m going to just state the name of the array,"},{"Start":"03:53.210 ","End":"03:56.130","Text":"which is numbersArray,"},{"Start":"03:56.450 ","End":"04:03.580","Text":"and inside the square brackets I then put 0 or 1 or 2 or whatever I just put i,"},{"Start":"04:03.580 ","End":"04:06.305","Text":"because I\u0027m using that as a variable to"},{"Start":"04:06.305 ","End":"04:10.130","Text":"iterate through each of the elements in the array."},{"Start":"04:10.130 ","End":"04:13.610","Text":"I\u0027m going to test that that does what I expect,"},{"Start":"04:13.610 ","End":"04:15.395","Text":"which is to print every element in the array."},{"Start":"04:15.395 ","End":"04:20.225","Text":"It should print all 11 elements and let\u0027s see if that works."},{"Start":"04:20.225 ","End":"04:23.600","Text":"It seems to have output all the elements,"},{"Start":"04:23.600 ","End":"04:25.205","Text":"all 11 of them."},{"Start":"04:25.205 ","End":"04:26.570","Text":"In Part c,"},{"Start":"04:26.570 ","End":"04:31.805","Text":"I\u0027ve been asked to modify it so that only odd numbers are output."},{"Start":"04:31.805 ","End":"04:36.740","Text":"Clearly I\u0027m going to need some conditional statement here and the question is,"},{"Start":"04:36.740 ","End":"04:38.620","Text":"what should that be made up of?"},{"Start":"04:38.620 ","End":"04:40.775","Text":"If I have an if statement,"},{"Start":"04:40.775 ","End":"04:44.390","Text":"what I need to know is when not i,"},{"Start":"04:44.390 ","End":"04:48.860","Text":"but the element pointed to by i is an odd number."},{"Start":"04:48.860 ","End":"04:51.770","Text":"If I divide a number by 2,"},{"Start":"04:51.770 ","End":"04:53.240","Text":"if it\u0027s an odd number,"},{"Start":"04:53.240 ","End":"04:55.105","Text":"the remainder is going to be 1."},{"Start":"04:55.105 ","End":"04:59.660","Text":"That\u0027s the maths I need to use to work out which elements to print."},{"Start":"04:59.660 ","End":"05:05.510","Text":"I use the modulo operator to return the remainder when I divide by 2,"},{"Start":"05:05.510 ","End":"05:08.470","Text":"and that should give me whether it\u0027s an odd number or not."},{"Start":"05:08.470 ","End":"05:14.430","Text":"I\u0027m looking at the element in the array that it\u0027s currently been iterated through."},{"Start":"05:14.430 ","End":"05:17.610","Text":"So by saying numbersArray i,"},{"Start":"05:17.610 ","End":"05:20.340","Text":"and then I say modulo 2."},{"Start":"05:20.340 ","End":"05:22.680","Text":"I want to know if the number I\u0027m currently looking at."},{"Start":"05:22.680 ","End":"05:26.765","Text":"So it could be this one or this one or this one all the way up to the end."},{"Start":"05:26.765 ","End":"05:28.940","Text":"Is that number an odd number?"},{"Start":"05:28.940 ","End":"05:30.830","Text":"I\u0027ll know if it\u0027s an odd number,"},{"Start":"05:30.830 ","End":"05:32.660","Text":"if the remainder is 1."},{"Start":"05:32.660 ","End":"05:34.625","Text":"That\u0027s all I need to do."},{"Start":"05:34.625 ","End":"05:39.995","Text":"I don\u0027t even need curly brackets here because there\u0027s only a single line in the loop,"},{"Start":"05:39.995 ","End":"05:43.310","Text":"I can actually do away with the curly brackets."},{"Start":"05:43.310 ","End":"05:45.050","Text":"Let\u0027s see if that works."},{"Start":"05:45.050 ","End":"05:49.055","Text":"Yes, it does. Because it\u0027s only 1 line in the block,"},{"Start":"05:49.055 ","End":"05:50.750","Text":"I just end it with a semicolon."},{"Start":"05:50.750 ","End":"05:57.410","Text":"It\u0027s actually conventional to include it on the same line just for ease of reading."},{"Start":"05:57.410 ","End":"05:59.390","Text":"If you look at it that way,"},{"Start":"05:59.390 ","End":"06:01.940","Text":"I think it makes a little bit more sense."},{"Start":"06:01.940 ","End":"06:07.985","Text":"We only execute this bit up to the semicolon if that condition is true,"},{"Start":"06:07.985 ","End":"06:11.090","Text":"which in this case would be only for odd numbers."},{"Start":"06:11.090 ","End":"06:14.274","Text":"I only expect to see 1, 5, 11,"},{"Start":"06:14.274 ","End":"06:18.530","Text":"17, and 21 printed if I\u0027ve got this right."},{"Start":"06:18.530 ","End":"06:22.720","Text":"Let\u0027s see if I have by running it."},{"Start":"06:22.720 ","End":"06:26.544","Text":"Sure enough this time it\u0027s only printed 1, 5, 11,"},{"Start":"06:26.544 ","End":"06:30.545","Text":"17, and 21 and the other numbers were not printed."},{"Start":"06:30.545 ","End":"06:33.890","Text":"If I had done it with clear screens at methods call,"},{"Start":"06:33.890 ","End":"06:35.585","Text":"you would just see those numbers."},{"Start":"06:35.585 ","End":"06:37.270","Text":"Just approve that that\u0027s correct."},{"Start":"06:37.270 ","End":"06:38.790","Text":"I just output the numbers."},{"Start":"06:38.790 ","End":"06:41.700","Text":"There it goes, just outputting those 5 numbers."},{"Start":"06:41.700 ","End":"06:44.885","Text":"Now just to prove that the thing about the length is true,"},{"Start":"06:44.885 ","End":"06:49.040","Text":"let\u0027s add another element on to the array,"},{"Start":"06:49.040 ","End":"06:51.230","Text":"and it should also print this number because it\u0027s an odd number."},{"Start":"06:51.230 ","End":"06:53.620","Text":"Let\u0027s see if it still works,"},{"Start":"06:53.620 ","End":"06:59.550","Text":"which I would hope it does. It does."},{"Start":"06:59.550 ","End":"07:03.680","Text":"You see, I didn\u0027t have to change any other lines of code because I was using"},{"Start":"07:03.680 ","End":"07:09.155","Text":"the length property of the array here to determine when this loop should terminate."},{"Start":"07:09.155 ","End":"07:12.710","Text":"If I had left it at 11 as I did before,"},{"Start":"07:12.710 ","End":"07:17.320","Text":"it would still work, but it would ignore this last number anything past 11."},{"Start":"07:17.320 ","End":"07:18.710","Text":"That\u0027s it for this exercise."},{"Start":"07:18.710 ","End":"07:20.720","Text":"I hope that was straightforward enough,"},{"Start":"07:20.720 ","End":"07:25.800","Text":"and we\u0027ll see you soon for the next one where we get a little bit more complex."}],"ID":28349},{"Watched":false,"Name":"Exercise 2","Duration":"10m 50s","ChapterTopicVideoID":27229,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.240 ","End":"00:03.700","Text":"Hello again everyone. In this next exercise,"},{"Start":"00:03.700 ","End":"00:07.630","Text":"we\u0027ve been asked to create another method in the same class as before called topThree."},{"Start":"00:07.630 ","End":"00:08.920","Text":"In part a, we are told,"},{"Start":"00:08.920 ","End":"00:10.780","Text":"inside the method to declare an array of"},{"Start":"00:10.780 ","End":"00:13.405","Text":"bytes called scores with the following elements,"},{"Start":"00:13.405 ","End":"00:14.950","Text":"and in part a_i,"},{"Start":"00:14.950 ","End":"00:16.930","Text":"those values are listed."},{"Start":"00:16.930 ","End":"00:20.140","Text":"Part b, we\u0027re asked to declare a second empty array"},{"Start":"00:20.140 ","End":"00:23.470","Text":"of bytes called results with a size of 3."},{"Start":"00:23.470 ","End":"00:28.315","Text":"In part c, we are then asked to use a loop to iterate through the scores array,"},{"Start":"00:28.315 ","End":"00:33.520","Text":"comparing each element value with what\u0027s stored in results[0]."},{"Start":"00:33.520 ","End":"00:36.370","Text":"If the current value during iteration is higher"},{"Start":"00:36.370 ","End":"00:39.310","Text":"than what is already stored in result[0],"},{"Start":"00:39.310 ","End":"00:42.785","Text":"we replace result [0] with a current value."},{"Start":"00:42.785 ","End":"00:48.140","Text":"Part d, results[0] should now contain the largest value in the array."},{"Start":"00:48.140 ","End":"00:51.755","Text":"After the loop, we output to the screen the message,"},{"Start":"00:51.755 ","End":"00:57.320","Text":"\"The highest score is followed by the contents of results[0].\""},{"Start":"00:57.320 ","End":"01:02.300","Text":"In part e, we\u0027re asked to add a second loop after the above code which finds"},{"Start":"01:02.300 ","End":"01:05.810","Text":"the second highest value by comparing the current element"},{"Start":"01:05.810 ","End":"01:09.470","Text":"with results[1] and results [0]."},{"Start":"01:09.470 ","End":"01:13.460","Text":"If it\u0027s higher than results[1] but lower than result[0],"},{"Start":"01:13.460 ","End":"01:16.695","Text":"we replace results[1] with a current value."},{"Start":"01:16.695 ","End":"01:22.250","Text":"In part f, results[1] should now contain the second largest value in the array."},{"Start":"01:22.250 ","End":"01:25.565","Text":"We output that to the screen alongside the message,"},{"Start":"01:25.565 ","End":"01:27.895","Text":"\"The 2nd highest score is.\""},{"Start":"01:27.895 ","End":"01:32.570","Text":"In part g, we\u0027re asked to add a final loop after all previous code which finds"},{"Start":"01:32.570 ","End":"01:37.230","Text":"the third highest value and stores it into results[2]."},{"Start":"01:37.230 ","End":"01:40.100","Text":"Results[2] should now contain the third largest value in the array."},{"Start":"01:40.100 ","End":"01:41.990","Text":"We output to the screen the message,"},{"Start":"01:41.990 ","End":"01:47.370","Text":"\"The third highest score is:\" followed by the contents of results[2]."},{"Start":"01:47.370 ","End":"01:49.350","Text":"Then finally in part i,"},{"Start":"01:49.350 ","End":"01:52.850","Text":"we\u0027re asked to change the first element in the scores array from"},{"Start":"01:52.850 ","End":"01:56.240","Text":"55-97 and to check that the program"},{"Start":"01:56.240 ","End":"02:00.800","Text":"still works with new numbers appearing as the top 3 scores."},{"Start":"02:00.800 ","End":"02:06.665","Text":"Let\u0027s go ahead and create a new method called topThree."},{"Start":"02:06.665 ","End":"02:11.540","Text":"Then, the first job we got to do in part a_i is to declare"},{"Start":"02:11.540 ","End":"02:18.350","Text":"an array of bytes called score with a set of elements that have been given to us."},{"Start":"02:18.350 ","End":"02:23.540","Text":"An array of bytes will be byte followed by a pair of square brackets and"},{"Start":"02:23.540 ","End":"02:28.760","Text":"the identifier score and a pair of braces and a semi-colon,"},{"Start":"02:28.760 ","End":"02:32.030","Text":"and then inside the braces we have our values."},{"Start":"02:32.030 ","End":"02:35.200","Text":"The values would be given 55,75,12,"},{"Start":"02:38.200 ","End":"02:41.615","Text":"67, 23,"},{"Start":"02:41.615 ","End":"02:46.565","Text":"95, 23,"},{"Start":"02:46.565 ","End":"02:50.740","Text":"61, 82,"},{"Start":"02:50.740 ","End":"02:53.430","Text":"43, and 37."},{"Start":"02:53.430 ","End":"02:55.380","Text":"These are the first array,"},{"Start":"02:55.380 ","End":"03:02.345","Text":"and then we\u0027ve been asked to create a second empty array of bytes again called results."},{"Start":"03:02.345 ","End":"03:04.385","Text":"So a slightly different syntax here."},{"Start":"03:04.385 ","End":"03:09.635","Text":"We still say the datatype and square brackets to signify it\u0027s an array."},{"Start":"03:09.635 ","End":"03:14.495","Text":"The identifier this time is results,"},{"Start":"03:14.495 ","End":"03:18.400","Text":"but the size is being set and there\u0027s been no initial values."},{"Start":"03:18.400 ","End":"03:21.260","Text":"So to do that we say new and"},{"Start":"03:21.260 ","End":"03:24.980","Text":"then the datatype and then in square brackets the size we want."},{"Start":"03:24.980 ","End":"03:27.710","Text":"That will now create an array called results,"},{"Start":"03:27.710 ","End":"03:31.550","Text":"which is of datatype byte of Size 3."},{"Start":"03:31.550 ","End":"03:35.580","Text":"That has done for parts a and b."},{"Start":"03:35.580 ","End":"03:37.360","Text":"Then in part c,"},{"Start":"03:37.360 ","End":"03:40.250","Text":"we\u0027re asked to use a loop to iterate through"},{"Start":"03:40.250 ","End":"03:45.380","Text":"the scores array comparing each element value with what\u0027s stored in result[0]."},{"Start":"03:45.380 ","End":"03:49.400","Text":"If the current value is higher than what\u0027s already stored in result[0],"},{"Start":"03:49.400 ","End":"03:51.665","Text":"then we replace result[0] with the current value."},{"Start":"03:51.665 ","End":"03:53.960","Text":"What we\u0027re trying to do is we\u0027re trying to get"},{"Start":"03:53.960 ","End":"03:56.570","Text":"into the first element of the results array,"},{"Start":"03:56.570 ","End":"03:59.975","Text":"the highest number in the scores array."},{"Start":"03:59.975 ","End":"04:06.735","Text":"Let\u0027s go ahead and create our loop and we use i again,"},{"Start":"04:06.735 ","End":"04:12.210","Text":"and then we\u0027re iterating through the score array."},{"Start":"04:12.350 ","End":"04:18.870","Text":"We want to make sure that i is less than score.length,"},{"Start":"04:18.870 ","End":"04:20.910","Text":"otherwise we will terminate,"},{"Start":"04:20.910 ","End":"04:26.210","Text":"and then we increment by 1 each time around the loop."},{"Start":"04:26.210 ","End":"04:28.715","Text":"Inside the loop, then what do we do?"},{"Start":"04:28.715 ","End":"04:38.900","Text":"We are going to compare what\u0027s in the current element with what\u0027s stored in results[0]."},{"Start":"04:38.900 ","End":"04:40.670","Text":"We need an if statement."},{"Start":"04:40.670 ","End":"04:50.720","Text":"If the current element score[i] is greater than what\u0027s in results[0],"},{"Start":"04:50.720 ","End":"04:53.630","Text":"we hardcode the 0 because we\u0027re interested in the highest score which"},{"Start":"04:53.630 ","End":"04:56.660","Text":"is stored in element[0]."},{"Start":"04:56.660 ","End":"05:00.620","Text":"If that\u0027s true, then what we want to do is we want"},{"Start":"05:00.620 ","End":"05:06.440","Text":"to store a current value into results[0]."},{"Start":"05:06.440 ","End":"05:12.845","Text":"Results [0] is given the value of the current element,"},{"Start":"05:12.845 ","End":"05:15.260","Text":"which we\u0027re pointing to, which is score[i]."},{"Start":"05:15.260 ","End":"05:17.975","Text":"It doesn\u0027t seem to have any syntax errors,"},{"Start":"05:17.975 ","End":"05:20.630","Text":"so that looks like it\u0027s fine."},{"Start":"05:20.630 ","End":"05:23.390","Text":"Then I\u0027m asked in part d,"},{"Start":"05:23.390 ","End":"05:28.610","Text":"after the loop to output to the screen the message,"},{"Start":"05:28.610 ","End":"05:33.620","Text":"\"The highest score is followed by the contents of results[0].\""},{"Start":"05:33.620 ","End":"05:42.200","Text":"Let us do that. We\u0027re going to join that string to the value of results[0],"},{"Start":"05:42.200 ","End":"05:50.315","Text":"which will be converted into a string forth by the print statement and that should be it."},{"Start":"05:50.315 ","End":"05:56.390","Text":"If this works, then what should happen is the highest value in here is currently 95,"},{"Start":"05:56.390 ","End":"06:00.605","Text":"so we should see the highest score is 95 printed to the screen."},{"Start":"06:00.605 ","End":"06:02.395","Text":"Let\u0027s see if that works."},{"Start":"06:02.395 ","End":"06:06.030","Text":"It seems to work fine, so that\u0027s great."},{"Start":"06:06.030 ","End":"06:09.710","Text":"Let\u0027s move on to part e now where it says to"},{"Start":"06:09.710 ","End":"06:13.415","Text":"add a second loop above the code that we\u0027ve just got there,"},{"Start":"06:13.415 ","End":"06:15.950","Text":"which does the same thing basically,"},{"Start":"06:15.950 ","End":"06:21.470","Text":"but it compares the current element with results[1] and result[0]."},{"Start":"06:21.470 ","End":"06:25.010","Text":"If it\u0027s higher than results[1] but lower than result[0],"},{"Start":"06:25.010 ","End":"06:27.720","Text":"then replace results[1] with the current value."},{"Start":"06:27.720 ","End":"06:30.710","Text":"What I\u0027m going to do is I\u0027m going to actually copy"},{"Start":"06:30.710 ","End":"06:36.770","Text":"that below here to save me some typing because it\u0027s going to be very similar."},{"Start":"06:36.770 ","End":"06:40.160","Text":"The loop is still going to iterate over the same elements"},{"Start":"06:40.160 ","End":"06:43.790","Text":"so that stays exactly the same except this time,"},{"Start":"06:43.790 ","End":"06:49.730","Text":"what I\u0027m going to do here is I\u0027m checking whether the current score is"},{"Start":"06:49.730 ","End":"06:52.620","Text":"greater than results[1] not result[0] because I want"},{"Start":"06:52.620 ","End":"06:56.120","Text":"the second highest score in results[1],"},{"Start":"06:56.120 ","End":"07:02.600","Text":"but I also need to check that it\u0027s less than the top score which is stored in results[0]."},{"Start":"07:02.600 ","End":"07:04.820","Text":"I want to do an and there,"},{"Start":"07:04.820 ","End":"07:13.170","Text":"so score[i] has to be greater than results[1] and has to be less than results[0]."},{"Start":"07:13.170 ","End":"07:19.755","Text":"If it is, then I replace results[1] with the current score."},{"Start":"07:19.755 ","End":"07:22.770","Text":"This is checking whether the current"},{"Start":"07:22.770 ","End":"07:26.760","Text":"element\u0027s bigger than any previous one that is stored in"},{"Start":"07:26.760 ","End":"07:29.420","Text":"results[1] but it has to be smaller than"},{"Start":"07:29.420 ","End":"07:33.710","Text":"the highest score of all which is stored in results[0]."},{"Start":"07:33.710 ","End":"07:37.820","Text":"If that works, then I can now put the second highest score,"},{"Start":"07:37.820 ","End":"07:41.700","Text":"which I\u0027ll just copy and paste and change what\u0027s in the brackets here to"},{"Start":"07:41.700 ","End":"07:46.755","Text":"1 and I\u0027ll add the word second here."},{"Start":"07:46.755 ","End":"07:51.660","Text":"I\u0027ll test that to see if it works."},{"Start":"07:51.660 ","End":"07:53.450","Text":"I think that\u0027s right."},{"Start":"07:53.450 ","End":"07:55.160","Text":"If we look back at our program,"},{"Start":"07:55.160 ","End":"07:58.920","Text":"the second highest value in this array is indeed 82."},{"Start":"07:58.920 ","End":"08:00.360","Text":"So 95 is the biggest,"},{"Start":"08:00.360 ","End":"08:01.965","Text":"82 is the next biggest."},{"Start":"08:01.965 ","End":"08:04.665","Text":"The next biggest here is 75."},{"Start":"08:04.665 ","End":"08:05.810","Text":"When we do the final part,"},{"Start":"08:05.810 ","End":"08:08.075","Text":"we should expect to see 75."},{"Start":"08:08.075 ","End":"08:11.465","Text":"We\u0027re now on part g,"},{"Start":"08:11.465 ","End":"08:14.570","Text":"the final loop after all previous code which finds"},{"Start":"08:14.570 ","End":"08:17.810","Text":"the third highest value and stores it into results[2]."},{"Start":"08:17.810 ","End":"08:19.535","Text":"We\u0027re not given a lot of help here,"},{"Start":"08:19.535 ","End":"08:24.365","Text":"but we can probably figure out what we need to do from this statement here."},{"Start":"08:24.365 ","End":"08:26.615","Text":"It\u0027s going to be exactly the same as before"},{"Start":"08:26.615 ","End":"08:29.660","Text":"except this time we want to make sure it\u0027s greater than"},{"Start":"08:29.660 ","End":"08:36.780","Text":"results[2] and less than result[0] and less than results[1]."},{"Start":"08:36.780 ","End":"08:39.110","Text":"It\u0027s going to be very similar to what we got here."},{"Start":"08:39.110 ","End":"08:42.350","Text":"Let us just, to save ourselves some typing,"},{"Start":"08:42.350 ","End":"08:45.050","Text":"take everything we\u0027ve got here and then we\u0027ll"},{"Start":"08:45.050 ","End":"08:48.275","Text":"paste it below and change what we need to change."},{"Start":"08:48.275 ","End":"08:51.020","Text":"There it is. This time what"},{"Start":"08:51.020 ","End":"08:53.225","Text":"we\u0027re going to do is we\u0027re going to add an extra statement here."},{"Start":"08:53.225 ","End":"08:54.680","Text":"All of this stays the same."},{"Start":"08:54.680 ","End":"09:00.050","Text":"If score[i] is greater than results[2] because we\u0027re looking at results[2] now,"},{"Start":"09:00.050 ","End":"09:01.550","Text":"the third highest score."},{"Start":"09:01.550 ","End":"09:06.040","Text":"But the score has to be less than results[1] and it also has to be"},{"Start":"09:06.040 ","End":"09:12.150","Text":"less than result[0] and results[1]."},{"Start":"09:12.150 ","End":"09:17.840","Text":"If that\u0027s true, then this third highest score gets stored."},{"Start":"09:17.840 ","End":"09:22.250","Text":"We take its value from the current value that we\u0027re looking at is."},{"Start":"09:22.250 ","End":"09:27.620","Text":"I need to change this final line to output the third highest score and to print"},{"Start":"09:27.620 ","End":"09:33.170","Text":"out the contents of the third element in the array, which is results[2]."},{"Start":"09:33.170 ","End":"09:34.490","Text":"If I\u0027ve done everything right,"},{"Start":"09:34.490 ","End":"09:37.910","Text":"I expect it to print 95 as the highest score,"},{"Start":"09:37.910 ","End":"09:39.680","Text":"82 as the second highest score,"},{"Start":"09:39.680 ","End":"09:43.695","Text":"and 75 as the final of the 3."},{"Start":"09:43.695 ","End":"09:49.085","Text":"Let\u0027s see if that does work and it seems to have worked. That\u0027s fantastic."},{"Start":"09:49.085 ","End":"09:53.280","Text":"But just to test slightly more robustly,"},{"Start":"09:53.280 ","End":"09:58.550","Text":"we\u0027re asked to change in part i the first element in the scores array,"},{"Start":"09:58.550 ","End":"10:01.835","Text":"which is currently 55-97."},{"Start":"10:01.835 ","End":"10:08.620","Text":"We should expect now 97 to be the top score and 95 should be the second highest score,"},{"Start":"10:08.620 ","End":"10:11.690","Text":"82 will be the third highest score,"},{"Start":"10:11.690 ","End":"10:14.930","Text":"and 75 will drop out of the picture because we\u0027re only storing"},{"Start":"10:14.930 ","End":"10:18.845","Text":"the top 3 scores and 97 is now the top score."},{"Start":"10:18.845 ","End":"10:21.140","Text":"Let\u0027s see if that works."},{"Start":"10:21.140 ","End":"10:25.550","Text":"If the algorithm is correct and our implementation of it is correct,"},{"Start":"10:25.550 ","End":"10:31.265","Text":"this should indeed print the new top 3 and it does."},{"Start":"10:31.265 ","End":"10:32.870","Text":"That\u0027s done."},{"Start":"10:32.870 ","End":"10:35.510","Text":"There\u0027s slightly more complex examples in the first one,"},{"Start":"10:35.510 ","End":"10:36.890","Text":"a little bit more useful as well."},{"Start":"10:36.890 ","End":"10:43.310","Text":"We\u0027re printing the 3 highest scores in an array and if those elements change,"},{"Start":"10:43.310 ","End":"10:47.240","Text":"we\u0027ll still print the correct 3 from the array."},{"Start":"10:47.240 ","End":"10:50.820","Text":"Thanks very much and I\u0027ll see you in the next one."}],"ID":28350},{"Watched":false,"Name":"Exercise 3","Duration":"6m 16s","ChapterTopicVideoID":27230,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.250","Text":"Hello again. In this exercise,"},{"Start":"00:02.250 ","End":"00:07.335","Text":"we were asked to create a new method called common and in Part a to declare 2 arrays,"},{"Start":"00:07.335 ","End":"00:09.150","Text":"arrayA and arrayB,"},{"Start":"00:09.150 ","End":"00:10.875","Text":"to contain the following values."},{"Start":"00:10.875 ","End":"00:12.180","Text":"In Part a1,"},{"Start":"00:12.180 ","End":"00:14.430","Text":"the values for arrayA are listed,"},{"Start":"00:14.430 ","End":"00:18.375","Text":"in Part 2, the values for arrayB are listed."},{"Start":"00:18.375 ","End":"00:22.890","Text":"Then in Part b, we\u0027re asked to use a loop to iterate through arrayA,"},{"Start":"00:22.890 ","End":"00:27.060","Text":"checking whether each element is present in arrayB."},{"Start":"00:27.060 ","End":"00:29.775","Text":"When an element is found in both arrays,"},{"Start":"00:29.775 ","End":"00:34.650","Text":"we output the value alongside the message common value found."},{"Start":"00:34.650 ","End":"00:40.770","Text":"Let\u0027s go ahead and create the method common, as we\u0027ve been asked to do,"},{"Start":"00:40.770 ","End":"00:45.275","Text":"and this time we\u0027ve been asked to create 2 arrays,"},{"Start":"00:45.275 ","End":"00:48.920","Text":"arrayA and arrayB and haven\u0027t been told a data type,"},{"Start":"00:48.920 ","End":"00:54.510","Text":"it looks like all the values are below 7 bits, which would be a 127."},{"Start":"00:54.510 ","End":"00:57.020","Text":"We can use a byte array again here"},{"Start":"00:57.020 ","End":"01:00.665","Text":"but you could have used an integer array or anything else if you wanted to,"},{"Start":"01:00.665 ","End":"01:04.020","Text":"as long as it was able to hold a whole number."},{"Start":"01:04.370 ","End":"01:08.075","Text":"We\u0027ll call it arrayA as we\u0027ve been asked,"},{"Start":"01:08.075 ","End":"01:14.115","Text":"and give it the values that are stated in the question."},{"Start":"01:14.115 ","End":"01:17.970","Text":"We got 4, 7, 10,"},{"Start":"01:17.970 ","End":"01:21.737","Text":"16, 25, 28,"},{"Start":"01:21.737 ","End":"01:24.248","Text":"40, 53,"},{"Start":"01:24.248 ","End":"01:28.980","Text":"79, 92, and 111."},{"Start":"01:28.980 ","End":"01:33.360","Text":"That\u0027s the first array done and the second one is going to be"},{"Start":"01:33.360 ","End":"01:38.700","Text":"the same data type and the name is arrayB."},{"Start":"01:38.700 ","End":"01:42.566","Text":"This time, the values are 2, 5,"},{"Start":"01:42.566 ","End":"01:44.372","Text":"6, 10, 12, 19,"},{"Start":"01:44.372 ","End":"01:46.342","Text":"28, 33, 44, 51,"},{"Start":"01:46.342 ","End":"01:55.880","Text":"87, and 92."},{"Start":"01:55.880 ","End":"01:57.530","Text":"We\u0027re going to need a loop,"},{"Start":"01:57.530 ","End":"02:01.985","Text":"as we\u0027ve been asked in part B to iterate through arrayA,"},{"Start":"02:01.985 ","End":"02:05.510","Text":"checking whether each element present in arrayB."},{"Start":"02:05.510 ","End":"02:07.190","Text":"We\u0027ll use our old friend,"},{"Start":"02:07.190 ","End":"02:09.335","Text":"the for loop, as usual."},{"Start":"02:09.335 ","End":"02:12.335","Text":"Could use a while loop if you needed to, if you wanted to,"},{"Start":"02:12.335 ","End":"02:15.796","Text":"but for loop is good for any takeaway,"},{"Start":"02:15.796 ","End":"02:22.340","Text":"you know in advance how many items you have to iterate through, which we do here."},{"Start":"02:22.340 ","End":"02:27.004","Text":"Array length is what we\u0027re interested in and I need to be less than it,"},{"Start":"02:27.004 ","End":"02:30.395","Text":"otherwise, the loop will terminate."},{"Start":"02:30.395 ","End":"02:34.115","Text":"Then we add 1 to i each time around the loop."},{"Start":"02:34.115 ","End":"02:37.805","Text":"Now, the thing here that you might not find obvious"},{"Start":"02:37.805 ","End":"02:43.835","Text":"is you need to have missed out for the variable name here. Just over."},{"Start":"02:43.835 ","End":"02:49.205","Text":"You need to have a loop inside this outer loop because we\u0027ve got to check"},{"Start":"02:49.205 ","End":"02:55.415","Text":"every element in arrayA against every element in arrayB."},{"Start":"02:55.415 ","End":"02:59.405","Text":"A nested loop is what would do that job for us."},{"Start":"02:59.405 ","End":"03:01.820","Text":"Let\u0027s have another variable, we\u0027ll call it j,"},{"Start":"03:01.820 ","End":"03:03.695","Text":"i and j normally come in pairs,"},{"Start":"03:03.695 ","End":"03:05.225","Text":"so we\u0027ll stick with that."},{"Start":"03:05.225 ","End":"03:12.295","Text":"At this time, we\u0027re looking for this loop to end when we\u0027ve got to the end of arrayB."},{"Start":"03:12.295 ","End":"03:18.050","Text":"So the arrayA and B don\u0027t have to be the same length in this solution."},{"Start":"03:18.050 ","End":"03:22.250","Text":"Because we\u0027re going to iterate through the inner array using"},{"Start":"03:22.250 ","End":"03:27.810","Text":"the variable j and we\u0027re going to iterate through the other array,"},{"Start":"03:27.810 ","End":"03:32.780","Text":"arrayA using the outer loop so that\u0027s 2 loops"},{"Start":"03:32.780 ","End":"03:38.670","Text":"then with i and j as the index variables to each array,"},{"Start":"03:38.670 ","End":"03:41.605","Text":"j for arrayB, i for arrayA."},{"Start":"03:41.605 ","End":"03:45.980","Text":"Now our job is to see if the item"},{"Start":"03:45.980 ","End":"03:50.690","Text":"in arrayA that\u0027s being compared to the item in arrayB are the same."},{"Start":"03:50.690 ","End":"03:52.820","Text":"That\u0027s straightforward enough as long as we use"},{"Start":"03:52.820 ","End":"03:56.945","Text":"the correct identifiers and index variables."},{"Start":"03:56.945 ","End":"04:02.720","Text":"For arrayA, we\u0027re using the index variable i and we\u0027re checking to see whether it\u0027s"},{"Start":"04:02.720 ","End":"04:05.300","Text":"the same value as"},{"Start":"04:05.300 ","End":"04:11.870","Text":"the arrayB element that we\u0027re looking at currently and if they are the same,"},{"Start":"04:11.870 ","End":"04:15.350","Text":"we\u0027re going to output a message because there\u0027s only one line needed there."},{"Start":"04:15.350 ","End":"04:16.430","Text":"I can do it on the same line."},{"Start":"04:16.430 ","End":"04:19.070","Text":"I don\u0027t need a pair of braces."},{"Start":"04:19.070 ","End":"04:22.865","Text":"System out print line as usual."},{"Start":"04:22.865 ","End":"04:28.460","Text":"What we\u0027ve been asked to print is the value so it doesn\u0027t matter which one I"},{"Start":"04:28.460 ","End":"04:34.220","Text":"print arrayA\u0027s element or arrayB\u0027s element because they\u0027re the same."},{"Start":"04:34.220 ","End":"04:36.605","Text":"When this output happens,"},{"Start":"04:36.605 ","End":"04:38.360","Text":"we\u0027ll do arrayA(i),"},{"Start":"04:38.360 ","End":"04:41.645","Text":"but it could\u0027ve been arrayB(j) because they\u0027re both the same value."},{"Start":"04:41.645 ","End":"04:45.360","Text":"Then afterwards we\u0027re asked to say common value found."},{"Start":"04:45.360 ","End":"04:49.895","Text":"Let\u0027s see if that does what we need it to do."},{"Start":"04:49.895 ","End":"04:51.950","Text":"If we look at these 2 arrays,"},{"Start":"04:51.950 ","End":"04:59.005","Text":"we would be expect it to match 10 and 28, and 92."},{"Start":"04:59.005 ","End":"05:00.755","Text":"Let\u0027s see if that\u0027s what we get."},{"Start":"05:00.755 ","End":"05:04.370","Text":"We\u0027re looking for 3 items to be found and sure enough,"},{"Start":"05:04.370 ","End":"05:05.960","Text":"we found the 3 values,"},{"Start":"05:05.960 ","End":"05:08.810","Text":"although we\u0027re missing some spaces in there."},{"Start":"05:08.810 ","End":"05:13.730","Text":"Let\u0027s add another value in just to see if our code still works."},{"Start":"05:13.730 ","End":"05:15.950","Text":"These seem to be a little bit suspicious."},{"Start":"05:15.950 ","End":"05:19.370","Text":"These numbers are in order, so let\u0027s prove it still works if something\u0027s in there,"},{"Start":"05:19.370 ","End":"05:21.035","Text":"that is not in the order."},{"Start":"05:21.035 ","End":"05:26.315","Text":"Let\u0027s put 44 in as well because that will now be a common value as well."},{"Start":"05:26.315 ","End":"05:29.435","Text":"We would expect to see 4 things found."},{"Start":"05:29.435 ","End":"05:34.280","Text":"Let\u0027s have a look to see if that works and seems to have done the job."},{"Start":"05:34.280 ","End":"05:38.330","Text":"It\u0027s found 44 as well and because it was the first item it inspected,"},{"Start":"05:38.330 ","End":"05:41.150","Text":"that\u0027s the first type of output we see,"},{"Start":"05:41.150 ","End":"05:45.589","Text":"the first output value we see and the others follow as before."},{"Start":"05:45.589 ","End":"05:49.430","Text":"We\u0027ve done the job which is to compare the items"},{"Start":"05:49.430 ","End":"05:53.150","Text":"in array to the items in B and if they match,"},{"Start":"05:53.150 ","End":"05:55.910","Text":"print a message and the value that was found."},{"Start":"05:55.910 ","End":"05:57.530","Text":"Nice straightforward one there."},{"Start":"05:57.530 ","End":"06:01.640","Text":"Any real twist there was we need to use 2 arrays to make sure we\u0027re checking"},{"Start":"06:01.640 ","End":"06:07.430","Text":"every item in arrayA against every item in arrayB,"},{"Start":"06:07.430 ","End":"06:10.130","Text":"so arrayB gets iterated in the inner part of the loop and"},{"Start":"06:10.130 ","End":"06:14.570","Text":"arrayA gets to be iterated in the outer loop."},{"Start":"06:14.570 ","End":"06:17.640","Text":"That\u0027s it for this exercise. Thanks for watching."}],"ID":28351},{"Watched":false,"Name":"Exercise 4","Duration":"11m 6s","ChapterTopicVideoID":27231,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.150","Text":"Hello again. In this exercise,"},{"Start":"00:03.150 ","End":"00:08.190","Text":"we\u0027ve been asked to create a method called lookup and in part a to declare 2 arrays."},{"Start":"00:08.190 ","End":"00:13.470","Text":"Part a i says to declare the first array as an integer array called"},{"Start":"00:13.470 ","End":"00:21.090","Text":"grade boundaries with initial values 35,43,50,65, and 77."},{"Start":"00:21.090 ","End":"00:27.030","Text":"In part 2, we\u0027re asked to create a char array called grades with initial values,"},{"Start":"00:27.030 ","End":"00:29.295","Text":"\u0027U\u0027, \u0027E\u0027, \u0027D\u0027,"},{"Start":"00:29.295 ","End":"00:31.675","Text":"\u0027C\u0027, \u0027B\u0027, and \u0027A\u0027."},{"Start":"00:31.675 ","End":"00:35.990","Text":"In part b, we\u0027re asked to declare a variable called mark,"},{"Start":"00:35.990 ","End":"00:39.425","Text":"which is an integer with an initial value of 22,"},{"Start":"00:39.425 ","End":"00:43.745","Text":"and part b ii a boolean called found with an initial value of false,"},{"Start":"00:43.745 ","End":"00:45.290","Text":"and in part b3,"},{"Start":"00:45.290 ","End":"00:50.640","Text":"an integer called position within an initial value of 0."},{"Start":"00:50.640 ","End":"00:54.590","Text":"In part c, we\u0027re asked to create a do...while loop which ends"},{"Start":"00:54.590 ","End":"00:58.820","Text":"when position is equal to the length of the grade boundaries array,"},{"Start":"00:58.820 ","End":"01:01.715","Text":"or when found is equal to true."},{"Start":"01:01.715 ","End":"01:05.390","Text":"In part d, we\u0027re asked inside the do...while"},{"Start":"01:05.390 ","End":"01:08.720","Text":"loop to see if the value of mark is less than"},{"Start":"01:08.720 ","End":"01:12.205","Text":"the value of the element in the grade boundaries array"},{"Start":"01:12.205 ","End":"01:17.075","Text":"currently being pointed to by the index variable position."},{"Start":"01:17.075 ","End":"01:22.055","Text":"If it is, we are to assign the value true to the found variable."},{"Start":"01:22.055 ","End":"01:25.925","Text":"If it is not, we increment the position variable."},{"Start":"01:25.925 ","End":"01:29.990","Text":"In part e, we\u0027re asked outside the loop to print the element in"},{"Start":"01:29.990 ","End":"01:34.864","Text":"the grades array that\u0027s pointed to by the index variable position."},{"Start":"01:34.864 ","End":"01:39.740","Text":"In part f, we test the code outputs the character \u0027U\u0027."},{"Start":"01:39.740 ","End":"01:44.180","Text":"In part g, we changed the value of marks to 43,"},{"Start":"01:44.180 ","End":"01:51.600","Text":"and 77 to confirm whether grades of \u0027D\u0027 and \u0027A\u0027 are output respectively."},{"Start":"01:51.710 ","End":"01:54.690","Text":"Let\u0027s create our method."},{"Start":"01:54.690 ","End":"01:57.030","Text":"Lookup as we\u0027ve been asked to do."},{"Start":"01:57.030 ","End":"02:02.269","Text":"The first thing we\u0027ve been asked in part a is to declare 2 arrays."},{"Start":"02:02.269 ","End":"02:03.680","Text":"First 1 in a,"},{"Start":"02:03.680 ","End":"02:08.970","Text":"1 is an integer array called grade boundaries,"},{"Start":"02:08.970 ","End":"02:13.230","Text":"and it\u0027s not an array unless I put square brackets there."},{"Start":"02:13.230 ","End":"02:16.285","Text":"Let\u0027s also capitalize the \u0027B\u0027."},{"Start":"02:16.285 ","End":"02:18.310","Text":"We\u0027re using camel case here,"},{"Start":"02:18.310 ","End":"02:28.440","Text":"and then the initial values are 35,43,50,65, and 77."},{"Start":"02:28.440 ","End":"02:31.935","Text":"The next array is a char array,"},{"Start":"02:31.935 ","End":"02:35.875","Text":"and this 1 is called grades,"},{"Start":"02:35.875 ","End":"02:41.120","Text":"and the initial values are specified inside"},{"Start":"02:41.120 ","End":"02:47.010","Text":"a single quote because that\u0027s how chars are referenced in."},{"Start":"02:47.010 ","End":"02:49.735","Text":"Strings have speech marks,"},{"Start":"02:49.735 ","End":"02:50.780","Text":"which is 2 quotes,"},{"Start":"02:50.780 ","End":"02:53.570","Text":"and that\u0027s usually shift in 2 or most keyboards."},{"Start":"02:53.570 ","End":"02:59.090","Text":"Some languages do allow you to use either single quotes or speech marks."},{"Start":"02:59.090 ","End":"03:00.440","Text":"Python being 1 of them,"},{"Start":"03:00.440 ","End":"03:03.530","Text":"but Java distinguishes between strings and"},{"Start":"03:03.530 ","End":"03:06.860","Text":"characters and expects single quotes for characters."},{"Start":"03:06.860 ","End":"03:08.915","Text":"There are 2 arrays,"},{"Start":"03:08.915 ","End":"03:13.040","Text":"and then we\u0027ve been asked to declare in part b3 variables."},{"Start":"03:13.040 ","End":"03:15.605","Text":"First one is an integer called mark,"},{"Start":"03:15.605 ","End":"03:17.690","Text":"and initial value of 22."},{"Start":"03:17.690 ","End":"03:24.465","Text":"Second one is a boolean called found with an initial value of false,"},{"Start":"03:24.465 ","End":"03:28.820","Text":"and then finally, a variable called position,"},{"Start":"03:28.820 ","End":"03:32.820","Text":"which is also an integer with initial value of 0."},{"Start":"03:32.820 ","End":"03:34.905","Text":"Then went to part c,"},{"Start":"03:34.905 ","End":"03:37.320","Text":"which is a main bit of the program,"},{"Start":"03:37.320 ","End":"03:40.430","Text":"and we\u0027ve been this time asked to use a do...while loop."},{"Start":"03:40.430 ","End":"03:43.910","Text":"We\u0027ve been previously using for-loops because arrays tend to have"},{"Start":"03:43.910 ","End":"03:49.940","Text":"a fixed size for-loops of very sensible choice of loop in that situation."},{"Start":"03:49.940 ","End":"03:53.165","Text":"But sometimes you have some other logic that applies,"},{"Start":"03:53.165 ","End":"03:56.390","Text":"and then you\u0027ll have to use a while loop or a do while loop."},{"Start":"03:56.390 ","End":"03:59.000","Text":"In this case, we\u0027ve been asked to use a do...while loop."},{"Start":"03:59.000 ","End":"04:02.220","Text":"Let\u0027s create brackets,"},{"Start":"04:02.220 ","End":"04:08.405","Text":"and then the while goes on the end and some logic inside those brackets there."},{"Start":"04:08.405 ","End":"04:11.390","Text":"What is the logic we need to use it?"},{"Start":"04:11.390 ","End":"04:13.400","Text":"It says in part c,"},{"Start":"04:13.400 ","End":"04:19.490","Text":"it ends when position is equal to the length of the array or when found is equal to true."},{"Start":"04:19.490 ","End":"04:23.360","Text":"We want to continue while those things are not true."},{"Start":"04:23.360 ","End":"04:28.945","Text":"While position is less than the length of the array."},{"Start":"04:28.945 ","End":"04:34.800","Text":"The array we\u0027re talking about is the grade boundaries array,"},{"Start":"04:34.800 ","End":"04:40.180","Text":"whilst the position is less than the grade boundaries array."},{"Start":"04:40.180 ","End":"04:43.630","Text":"We can carry on because it\u0027s not yet equal,"},{"Start":"04:43.630 ","End":"04:49.535","Text":"and we\u0027ve been told it\u0027s got an end when it\u0027s equal or ends when found is equal to true."},{"Start":"04:49.535 ","End":"04:58.335","Text":"We also want it to continue on while found is equal to false,"},{"Start":"04:58.335 ","End":"05:02.720","Text":"whilst these conditions are both true,"},{"Start":"05:02.720 ","End":"05:06.650","Text":"position is less than grade boundaries dot length,"},{"Start":"05:06.650 ","End":"05:09.710","Text":"and found is equal to false."},{"Start":"05:09.710 ","End":"05:13.505","Text":"Then we will carry on the loop as soon as one of those things is not true anymore,"},{"Start":"05:13.505 ","End":"05:16.535","Text":"we will end the do...while loop."},{"Start":"05:16.535 ","End":"05:22.100","Text":"Either position reaches the end of the array or found becomes true."},{"Start":"05:22.100 ","End":"05:25.670","Text":"Now onto part d inside the do...while loop,"},{"Start":"05:25.670 ","End":"05:29.240","Text":"see if the value of mark is less than the value of the element in"},{"Start":"05:29.240 ","End":"05:33.830","Text":"the grade boundaries array currently being pointed to by the index variable position."},{"Start":"05:33.830 ","End":"05:35.915","Text":"We now know what position is being useful."},{"Start":"05:35.915 ","End":"05:40.430","Text":"It\u0027s being used in the square brackets as the index variable."},{"Start":"05:40.430 ","End":"05:43.100","Text":"What we\u0027ve been asked to do is to compare"},{"Start":"05:43.100 ","End":"05:47.450","Text":"mark to the current element in the array pointed to by position,"},{"Start":"05:47.450 ","End":"05:50.540","Text":"and if mark\u0027s less than it, we do one thing."},{"Start":"05:50.540 ","End":"05:52.445","Text":"If it\u0027s not, we do something else."},{"Start":"05:52.445 ","End":"05:56.330","Text":"We\u0027re going to need a if statement in brackets."},{"Start":"05:56.330 ","End":"05:58.520","Text":"The thing we\u0027re looking at is mark,"},{"Start":"05:58.520 ","End":"06:04.040","Text":"and we want to know if it\u0027s less than whatever we\u0027re pointed to in"},{"Start":"06:04.040 ","End":"06:10.145","Text":"the grade boundaries array using the position variable."},{"Start":"06:10.145 ","End":"06:12.395","Text":"If mark is less than"},{"Start":"06:12.395 ","End":"06:17.430","Text":"the current value and the element that we\u0027re looking at, we do something."},{"Start":"06:17.430 ","End":"06:19.530","Text":"What\u0027s it that we do?"},{"Start":"06:19.530 ","End":"06:26.645","Text":"We in d1 asked if it is true to assign the value true to the bound variable."},{"Start":"06:26.645 ","End":"06:29.975","Text":"Found becomes true."},{"Start":"06:29.975 ","End":"06:32.644","Text":"Otherwise, we\u0027re going to need an else."},{"Start":"06:32.644 ","End":"06:38.940","Text":"We just increment the position variable, that is \u0027D\u0027."},{"Start":"06:38.940 ","End":"06:40.630","Text":"It\u0027s quite a few steps in \u0027D\u0027."},{"Start":"06:40.630 ","End":"06:43.685","Text":"We are basically checking to see if"},{"Start":"06:43.685 ","End":"06:48.020","Text":"the element were pointed to in the grade boundaries array,"},{"Start":"06:48.020 ","End":"06:50.705","Text":"which will successively be these values."},{"Start":"06:50.705 ","End":"06:53.495","Text":"If it\u0027s less than mark,"},{"Start":"06:53.495 ","End":"06:55.640","Text":"then we say found is true,"},{"Start":"06:55.640 ","End":"06:58.220","Text":"otherwise, we increment the position."},{"Start":"06:58.220 ","End":"07:02.330","Text":"What we\u0027re doing is we\u0027re going around this loop incrementing the position only"},{"Start":"07:02.330 ","End":"07:07.865","Text":"when mark is bigger than the value we\u0027re currently looking at,"},{"Start":"07:07.865 ","End":"07:10.175","Text":"or equal to actually as well."},{"Start":"07:10.175 ","End":"07:12.545","Text":"If mark is less than that value,"},{"Start":"07:12.545 ","End":"07:16.790","Text":"it will stop because part of the while loop condition"},{"Start":"07:16.790 ","End":"07:20.810","Text":"here just keep going while found is false,"},{"Start":"07:20.810 ","End":"07:22.385","Text":"but soon as found becomes true,"},{"Start":"07:22.385 ","End":"07:24.080","Text":"the loop will stop."},{"Start":"07:24.080 ","End":"07:27.340","Text":"This loop stops in 2 different ways."},{"Start":"07:27.340 ","End":"07:30.090","Text":"If you reached the end of the array, it will stop,"},{"Start":"07:30.090 ","End":"07:32.450","Text":"and if it finds the value it\u0027s looking for,"},{"Start":"07:32.450 ","End":"07:33.485","Text":"it will also stop."},{"Start":"07:33.485 ","End":"07:36.295","Text":"Whichever one happens first, it will stop."},{"Start":"07:36.295 ","End":"07:39.630","Text":"That is 2 part d,"},{"Start":"07:39.630 ","End":"07:42.670","Text":"and then what we\u0027re asked to do is to outside the loop,"},{"Start":"07:42.670 ","End":"07:48.845","Text":"print the element in the grades array that\u0027s pointed to by the index variable position."},{"Start":"07:48.845 ","End":"07:53.445","Text":"That\u0027s in part e. Simple enough to just 1 line,"},{"Start":"07:53.445 ","End":"07:59.340","Text":"system.out.print line and we\u0027re"},{"Start":"07:59.340 ","End":"08:04.230","Text":"printing from the grades array."},{"Start":"08:04.230 ","End":"08:11.285","Text":"The element pointed to by the position index variable. That\u0027s it."},{"Start":"08:11.285 ","End":"08:16.675","Text":"We\u0027re asked now to test that this works and outputs the character \u0027U\u0027."},{"Start":"08:16.675 ","End":"08:18.240","Text":"Why would it output \u0027U\u0027?"},{"Start":"08:18.240 ","End":"08:21.165","Text":"Because mark is 22."},{"Start":"08:21.165 ","End":"08:23.780","Text":"When we get to the first element in the array,"},{"Start":"08:23.780 ","End":"08:27.605","Text":"mark is less than that first element in the array,"},{"Start":"08:27.605 ","End":"08:30.230","Text":"and therefore found will be marked as true."},{"Start":"08:30.230 ","End":"08:33.370","Text":"It was skipped to the end here and asked the question."},{"Start":"08:33.370 ","End":"08:37.235","Text":"Is found false, and if not false, it\u0027s true."},{"Start":"08:37.235 ","End":"08:41.600","Text":"Stop, and it will print out the first value from"},{"Start":"08:41.600 ","End":"08:44.330","Text":"the grades array because we\u0027re on"},{"Start":"08:44.330 ","End":"08:47.615","Text":"position 0 when we start and we haven\u0027t incremented it yet."},{"Start":"08:47.615 ","End":"08:52.940","Text":"The very first-grade boundary we found this mark was lower than that grade boundary."},{"Start":"08:52.940 ","End":"08:56.104","Text":"It\u0027s going to stop on 0 and it\u0027s going to output."},{"Start":"08:56.104 ","End":"08:58.430","Text":"We hope that first element of the array."},{"Start":"08:58.430 ","End":"09:01.205","Text":"Let\u0027s see if that does work,"},{"Start":"09:01.205 ","End":"09:04.580","Text":"and it does indeed print \u0027U\u0027."},{"Start":"09:04.580 ","End":"09:07.280","Text":"We\u0027ve been asked to test it a little bit more in part"},{"Start":"09:07.280 ","End":"09:11.195","Text":"g by changing the value of mark to 43 and 77."},{"Start":"09:11.195 ","End":"09:13.535","Text":"We should see grades of D and A."},{"Start":"09:13.535 ","End":"09:15.200","Text":"Let\u0027s do that."},{"Start":"09:15.200 ","End":"09:21.195","Text":"43 would then stop when position is 1."},{"Start":"09:21.195 ","End":"09:30.345","Text":"Position of 1 would be \u0027D\u0027 because it\u0027s not a \u0027U\u0027 which would be below 35,"},{"Start":"09:30.345 ","End":"09:31.950","Text":"is not an \u0027E\u0027,"},{"Start":"09:31.950 ","End":"09:34.710","Text":"which would be below 43,"},{"Start":"09:34.710 ","End":"09:38.389","Text":"it would be \u0027D\u0027 because it\u0027s exactly equal to 43."},{"Start":"09:38.389 ","End":"09:40.655","Text":"Let\u0027s see if that happens."},{"Start":"09:40.655 ","End":"09:42.425","Text":"Unfortunately, it does."},{"Start":"09:42.425 ","End":"09:44.345","Text":"It looks like we\u0027re good at the moment."},{"Start":"09:44.345 ","End":"09:48.290","Text":"Let\u0027s just test it without loss value happens to be at the end of the array there"},{"Start":"09:48.290 ","End":"09:52.940","Text":"anything of 77 and above should give us a grade \u0027A\u0027."},{"Start":"09:52.940 ","End":"09:59.570","Text":"In that case, it won\u0027t have found a value that\u0027s less than grade boundaries position,"},{"Start":"09:59.570 ","End":"10:01.205","Text":"but it will reach the end of the array."},{"Start":"10:01.205 ","End":"10:02.855","Text":"That\u0027s why the loop will end,"},{"Start":"10:02.855 ","End":"10:06.290","Text":"and then it will print the last element of the array,"},{"Start":"10:06.290 ","End":"10:08.630","Text":"which is \u0027A\u0027 in this case."},{"Start":"10:08.630 ","End":"10:11.485","Text":"Let\u0027s see if that does work."},{"Start":"10:11.485 ","End":"10:13.815","Text":"It does print \u0027A\u0027."},{"Start":"10:13.815 ","End":"10:16.220","Text":"Quite a short little program,"},{"Start":"10:16.220 ","End":"10:19.070","Text":"but actually very useful in this thing called"},{"Start":"10:19.070 ","End":"10:22.400","Text":"a lookup table is very common in programming,"},{"Start":"10:22.400 ","End":"10:26.540","Text":"especially where there\u0027s seemingly arbitrary looking numbers."},{"Start":"10:26.540 ","End":"10:28.115","Text":"These aren\u0027t arbitrary-looking numbers."},{"Start":"10:28.115 ","End":"10:29.840","Text":"These would have been set by an exam board,"},{"Start":"10:29.840 ","End":"10:33.230","Text":"perhaps deciding what number to associate"},{"Start":"10:33.230 ","End":"10:37.085","Text":"with passing greater than \u0027E\u0027 and anything below that would be a,"},{"Start":"10:37.085 ","End":"10:41.720","Text":"\u0027U\u0027, and so on for the grades of \u0027D\u0027, \u0027C\u0027, \u0027B\u0027, and \u0027A\u0027."},{"Start":"10:41.720 ","End":"10:43.985","Text":"They can be all set in advance."},{"Start":"10:43.985 ","End":"10:46.460","Text":"Then we look through our table by"},{"Start":"10:46.460 ","End":"10:49.535","Text":"looking at each element in the array until we find a match."},{"Start":"10:49.535 ","End":"10:51.620","Text":"Then we can look up, in this case from"},{"Start":"10:51.620 ","End":"10:55.295","Text":"another table which is parallel to the previous one,"},{"Start":"10:55.295 ","End":"10:58.385","Text":"what grade is associated with them?"},{"Start":"10:58.385 ","End":"11:00.320","Text":"That\u0027s lookup tables."},{"Start":"11:00.320 ","End":"11:03.005","Text":"Quite an interesting use of an array there."},{"Start":"11:03.005 ","End":"11:04.565","Text":"Thanks very much for watching,"},{"Start":"11:04.565 ","End":"11:06.900","Text":"and I\u0027ll see you in the next video."}],"ID":28352},{"Watched":false,"Name":"Exercise 5","Duration":"12m 14s","ChapterTopicVideoID":27232,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.260","Text":"Hello everyone. In this exercise,"},{"Start":"00:02.260 ","End":"00:06.745","Text":"we\u0027ve been given some pseudo code for an algorithm called a Bubble sort."},{"Start":"00:06.745 ","End":"00:10.420","Text":"In the pseudo code we\u0027re shown an array called numbers,"},{"Start":"00:10.420 ","End":"00:14.080","Text":"which has a set of unordered numbers declared."},{"Start":"00:14.080 ","End":"00:16.420","Text":"Then a variable called numItems,"},{"Start":"00:16.420 ","End":"00:21.455","Text":"which takes the length of that array and stores the value into numItems."},{"Start":"00:21.455 ","End":"00:23.920","Text":"We then have a pair of nested loops."},{"Start":"00:23.920 ","End":"00:28.510","Text":"The outer loop iterates i from 0 to numItems minus 2."},{"Start":"00:28.510 ","End":"00:34.705","Text":"The inner loop iterates j from 0 to numItems minus i minus 2."},{"Start":"00:34.705 ","End":"00:40.830","Text":"We then have a conditional statement comparing numbers j to numbers j plus 1,"},{"Start":"00:40.830 ","End":"00:47.735","Text":"and some statements here which swap the values of numbers j and numbers j plus 1."},{"Start":"00:47.735 ","End":"00:53.000","Text":"We then at the end of the loop, output numbers,"},{"Start":"00:53.000 ","End":"00:56.600","Text":"the numbers array and we output a new line character,"},{"Start":"00:56.600 ","End":"01:00.380","Text":"and then the loops both finish."},{"Start":"01:00.380 ","End":"01:04.070","Text":"We\u0027re asked in part a to turn this pseudo code into Java method called"},{"Start":"01:04.070 ","End":"01:07.505","Text":"bubble sort and part b to make sure that we output"},{"Start":"01:07.505 ","End":"01:10.420","Text":"the entire array on one line using a loop and"},{"Start":"01:10.420 ","End":"01:16.550","Text":"the other line output backslash n is to output a new line character."},{"Start":"01:16.550 ","End":"01:20.150","Text":"Then in part c we are asked to change the numbers array by adding"},{"Start":"01:20.150 ","End":"01:23.540","Text":"the number 2 as the last element after"},{"Start":"01:23.540 ","End":"01:26.720","Text":"the number 11 and to test the bubble sort method"},{"Start":"01:26.720 ","End":"01:31.384","Text":"again to see how many times the number 2 gets swapped."},{"Start":"01:31.384 ","End":"01:36.080","Text":"This is a slightly different exercise than what we\u0027ve seen before."},{"Start":"01:36.080 ","End":"01:39.740","Text":"In this case, we\u0027ve been given something called pseudo code,"},{"Start":"01:39.740 ","End":"01:42.140","Text":"which is a way of expression and"},{"Start":"01:42.140 ","End":"01:46.190","Text":"algorithm in a form that looks like a programming language,"},{"Start":"01:46.190 ","End":"01:49.250","Text":"but it\u0027s not a specific programming language."},{"Start":"01:49.250 ","End":"01:52.565","Text":"It\u0027s the job of the programmer to read that and then translate it"},{"Start":"01:52.565 ","End":"01:58.265","Text":"into an actual programming language that they\u0027re familiar with in our case, Java."},{"Start":"01:58.265 ","End":"02:01.220","Text":"You can make sense of all of this and"},{"Start":"02:01.220 ","End":"02:04.040","Text":"the hard work has been done by the designer of the algorithm,"},{"Start":"02:04.040 ","End":"02:09.770","Text":"and your job really is just to translate it into code that can be compiled and run."},{"Start":"02:09.770 ","End":"02:18.810","Text":"Let\u0027s start it off by giving it a name and then we can get on with creating the arrays."},{"Start":"02:18.810 ","End":"02:21.680","Text":"The first thing here, so we\u0027ve been told about the data type or"},{"Start":"02:21.680 ","End":"02:25.080","Text":"anything in the syntax you can see is different than Java."},{"Start":"02:25.080 ","End":"02:28.250","Text":"We\u0027ve got to translate that to Java syntax."},{"Start":"02:28.250 ","End":"02:33.140","Text":"Let\u0027s make it a byte array because those numbers look quite small."},{"Start":"02:33.140 ","End":"02:35.255","Text":"It\u0027s been called numbers,"},{"Start":"02:35.255 ","End":"02:36.880","Text":"so we\u0027ll stick with that,"},{"Start":"02:36.880 ","End":"02:40.910","Text":"and then we don\u0027t use square brackets for our initial values in Java,"},{"Start":"02:40.910 ","End":"02:43.880","Text":"we use braces, curly brackets."},{"Start":"02:43.880 ","End":"02:48.860","Text":"Let\u0027s make sure we do that and then we can put our values in."},{"Start":"02:48.860 ","End":"02:52.395","Text":"15, 3, 8,"},{"Start":"02:52.395 ","End":"02:56.625","Text":"and 11, so it\u0027s just a smaller array with the 7 elements in it."},{"Start":"02:56.625 ","End":"02:59.570","Text":"Then we\u0027ve been asked to create a variable called numItems,"},{"Start":"02:59.570 ","End":"03:02.030","Text":"which contains the length of the array."},{"Start":"03:02.030 ","End":"03:04.595","Text":"I\u0027m going to make mine an int,"},{"Start":"03:04.595 ","End":"03:09.845","Text":"because the property of arrays that returns the length of the array,"},{"Start":"03:09.845 ","End":"03:11.480","Text":"which is called length,"},{"Start":"03:11.480 ","End":"03:13.220","Text":"always returns an int,"},{"Start":"03:13.220 ","End":"03:15.950","Text":"so if we store it into a byte,"},{"Start":"03:15.950 ","End":"03:18.260","Text":"which in this case would work in terms of the length."},{"Start":"03:18.260 ","End":"03:20.645","Text":"It\u0027s only 7 elements long."},{"Start":"03:20.645 ","End":"03:25.655","Text":"It would give me a warning saying it\u0027s being shortened from an int to a byte,"},{"Start":"03:25.655 ","End":"03:28.190","Text":"so are you sure you want to do that and so"},{"Start":"03:28.190 ","End":"03:31.260","Text":"I\u0027m not going to worry about that error message."},{"Start":"03:31.260 ","End":"03:33.185","Text":"Rather than do that,"},{"Start":"03:33.185 ","End":"03:36.220","Text":"I am going to store it int,"},{"Start":"03:36.220 ","End":"03:38.360","Text":"so I miss the name of the variable there,"},{"Start":"03:38.360 ","End":"03:40.130","Text":"which is why it\u0027s complaining."},{"Start":"03:40.130 ","End":"03:43.385","Text":"Then we got numItems equals numbers.length."},{"Start":"03:43.385 ","End":"03:44.600","Text":"That\u0027s the second line done."},{"Start":"03:44.600 ","End":"03:47.240","Text":"Then we come to a for loop."},{"Start":"03:47.240 ","End":"03:52.235","Text":"We have got plenty of practice doing for loops since we\u0027ve been doing this topic."},{"Start":"03:52.235 ","End":"03:57.885","Text":"It\u0027s going from 0 up to numItems minus 2,"},{"Start":"03:57.885 ","End":"04:06.420","Text":"so I is going to be less than or equal to numItems minus 2."},{"Start":"04:06.420 ","End":"04:10.085","Text":"If you don\u0027t put the less than or equals to it just make less than,"},{"Start":"04:10.085 ","End":"04:17.540","Text":"you\u0027ll have one item short because you want to go up to numItems not below numItems."},{"Start":"04:17.540 ","End":"04:20.685","Text":"Therefore we need to just increment"},{"Start":"04:20.685 ","End":"04:25.165","Text":"the variable now which we\u0027re using in the loop, which is I."},{"Start":"04:25.165 ","End":"04:29.525","Text":"That\u0027s the outer loop done then there\u0027s an inner loop that follows."},{"Start":"04:29.525 ","End":"04:33.830","Text":"This time we\u0027ve got j going from 0,"},{"Start":"04:33.830 ","End":"04:44.070","Text":"and it actually goes to numItems minus i not j but i minus 2,"},{"Start":"04:44.070 ","End":"04:46.235","Text":"and then we want to increment j."},{"Start":"04:46.235 ","End":"04:52.165","Text":"What, I haven\u0027t done here is compare j. I need to make sure I do that."},{"Start":"04:52.165 ","End":"04:58.555","Text":"j is again less than or equal to numItems minus i minus 2."},{"Start":"04:58.555 ","End":"05:04.825","Text":"2 loops and then inside the inner loop,"},{"Start":"05:04.825 ","End":"05:13.200","Text":"I\u0027ve got a conditional statement where numbers j is being compared to numbers j plus 1,"},{"Start":"05:13.200 ","End":"05:18.010","Text":"and the syntax for that is almost identical as it is in the pseudo code."},{"Start":"05:18.010 ","End":"05:19.960","Text":"We don\u0027t have the keyword,"},{"Start":"05:19.960 ","End":"05:27.160","Text":"then we just need a pair of curly brackets and then inside that conditional block,"},{"Start":"05:27.160 ","End":"05:34.445","Text":"we say temp equals numbers j,"},{"Start":"05:34.445 ","End":"05:37.785","Text":"because I haven\u0027t got a variable called temp."},{"Start":"05:37.785 ","End":"05:41.185","Text":"I need to declare it, so I need to put a data type in front."},{"Start":"05:41.185 ","End":"05:51.495","Text":"Let\u0027s make that an integer and numbers j equals numbers j plus 1."},{"Start":"05:51.495 ","End":"05:55.940","Text":"What this algorithm seems to be doing is seeing if one number"},{"Start":"05:55.940 ","End":"06:00.620","Text":"is bigger than another in adjacent locations in the array."},{"Start":"06:00.620 ","End":"06:05.150","Text":"If they are it swaps them over and when in the process of swapping,"},{"Start":"06:05.150 ","End":"06:06.740","Text":"it needs to have a temporary variable,"},{"Start":"06:06.740 ","End":"06:12.440","Text":"which we\u0027ve got here to contain one of the values to prevent it being overwritten."},{"Start":"06:12.440 ","End":"06:15.260","Text":"It\u0027s stored somewhere else in a temporary variable."},{"Start":"06:15.260 ","End":"06:18.560","Text":"There\u0027s almost done in the conditional part,"},{"Start":"06:18.560 ","End":"06:24.035","Text":"so we now swap the other item using the temp variable."},{"Start":"06:24.035 ","End":"06:28.960","Text":"So far that looks like the pseudo code if I just check here,"},{"Start":"06:28.960 ","End":"06:31.340","Text":"that looks about right,"},{"Start":"06:31.340 ","End":"06:32.510","Text":"so there\u0027s no end if,"},{"Start":"06:32.510 ","End":"06:39.050","Text":"the closing curly brace does that with no next needed"},{"Start":"06:39.050 ","End":"06:45.620","Text":"because we\u0027ve closed our inner loop here and we\u0027ve closed that other inner loop here."},{"Start":"06:45.620 ","End":"06:47.130","Text":"There is a complaint somewhere,"},{"Start":"06:47.130 ","End":"06:50.540","Text":"so perhaps I\u0027ve done something not quite right."},{"Start":"06:50.540 ","End":"06:56.180","Text":"Cannot find symbol variable number because I\u0027ve missed an s somewhere presumably."},{"Start":"06:56.180 ","End":"06:58.250","Text":"There we go. The erase got numbers,"},{"Start":"06:58.250 ","End":"07:00.755","Text":"not number, and that\u0027s why it\u0027s complaining there."},{"Start":"07:00.755 ","End":"07:05.075","Text":"That seems to do what I need to do."},{"Start":"07:05.075 ","End":"07:09.845","Text":"I\u0027ve copied the pseudo code apart from this bit to do with output,"},{"Start":"07:09.845 ","End":"07:11.825","Text":"I just got another error here."},{"Start":"07:11.825 ","End":"07:18.310","Text":"I\u0027m swapping an int and a byte here so temp actually should be a byte"},{"Start":"07:18.310 ","End":"07:20.915","Text":"because the value that I\u0027m swapping"},{"Start":"07:20.915 ","End":"07:24.240","Text":"is a byte and I\u0027m trying to swap it into a variable which is an int,"},{"Start":"07:24.240 ","End":"07:27.080","Text":"and it\u0027s worried about whether there\u0027ll be some loss there,"},{"Start":"07:27.080 ","End":"07:28.815","Text":"so I\u0027ll make that go away."},{"Start":"07:28.815 ","End":"07:33.660","Text":"The last part is to do what it says in part b,"},{"Start":"07:33.660 ","End":"07:36.710","Text":"which is, it does say there in the pseudo code, output numbers,"},{"Start":"07:36.710 ","End":"07:40.700","Text":"and output backslash n. What\u0027s going on there is"},{"Start":"07:40.700 ","End":"07:45.110","Text":"I\u0027m going to output each element of the array on one line,"},{"Start":"07:45.110 ","End":"07:48.290","Text":"and then I\u0027m going to force the output onto a new line afterwards."},{"Start":"07:48.290 ","End":"07:52.850","Text":"That would be a simple case of creating another loop."},{"Start":"07:52.850 ","End":"07:56.120","Text":"I might as well call my variable k here."},{"Start":"07:56.120 ","End":"07:58.610","Text":"It won\u0027t actually matter because all of these actually"},{"Start":"07:58.610 ","End":"08:01.570","Text":"will matter because it\u0027s inside the other loop,"},{"Start":"08:01.570 ","End":"08:04.085","Text":"so I need to have a different name."},{"Start":"08:04.085 ","End":"08:13.320","Text":"I\u0027ve got k equals 0. k is less than numItems."},{"Start":"08:13.320 ","End":"08:17.810","Text":"I want to print the elements of the array."},{"Start":"08:17.810 ","End":"08:21.170","Text":"I can use system.out."},{"Start":"08:21.170 ","End":"08:26.105","Text":"not print line but print and this will print 1 item."},{"Start":"08:26.105 ","End":"08:28.190","Text":"Then I can put a space after it."},{"Start":"08:28.190 ","End":"08:34.700","Text":"So the item look into print is numbers k. I want to"},{"Start":"08:34.700 ","End":"08:37.760","Text":"print the whole array on one line and then"},{"Start":"08:37.760 ","End":"08:41.255","Text":"I follow it with a space so that next time I print,"},{"Start":"08:41.255 ","End":"08:43.325","Text":"there\u0027ll be a gap between them."},{"Start":"08:43.325 ","End":"08:44.570","Text":"That\u0027s all on one line,"},{"Start":"08:44.570 ","End":"08:48.065","Text":"I don\u0027t need braces here because it\u0027s only a single line of code."},{"Start":"08:48.065 ","End":"08:54.365","Text":"That\u0027s the only thing that will be inside this for-loop that\u0027s using the k variable."},{"Start":"08:54.365 ","End":"08:58.895","Text":"On the next line, I\u0027m just going to print nothing essentially."},{"Start":"08:58.895 ","End":"09:01.730","Text":"But if I use system out print line,"},{"Start":"09:01.730 ","End":"09:04.730","Text":"it will force it to print onto a new line."},{"Start":"09:04.730 ","End":"09:06.230","Text":"I could do it that way."},{"Start":"09:06.230 ","End":"09:11.390","Text":"The other way I could do it is to say print and then print in a little string,"},{"Start":"09:11.390 ","End":"09:15.005","Text":"the new line character and that will have the same effect."},{"Start":"09:15.005 ","End":"09:18.120","Text":"Now I\u0027m in a position where I can test it,"},{"Start":"09:18.120 ","End":"09:20.480","Text":"so let\u0027s go ahead and do that."},{"Start":"09:20.480 ","End":"09:24.560","Text":"I\u0027ll create an object and then run the bubble sort method."},{"Start":"09:24.560 ","End":"09:28.655","Text":"There we go it\u0027s output the bubble sort output."},{"Start":"09:28.655 ","End":"09:31.730","Text":"What we\u0027re seeing is at the end of the output,"},{"Start":"09:31.730 ","End":"09:33.905","Text":"the numbers are now in order."},{"Start":"09:33.905 ","End":"09:36.200","Text":"Whereas the first line of output here,"},{"Start":"09:36.200 ","End":"09:41.000","Text":"that\u0027s not in order apart from 11 and 15 seemed to moved into place."},{"Start":"09:41.000 ","End":"09:44.180","Text":"Then in each subsequent lines some more swapping happens"},{"Start":"09:44.180 ","End":"09:47.660","Text":"until we eventually get to a sorted list."},{"Start":"09:47.660 ","End":"09:52.010","Text":"In fact, we got to a sorted list here because the values we were dealing with,"},{"Start":"09:52.010 ","End":"09:54.710","Text":"there were no more swapping to do after we got to this point."},{"Start":"09:54.710 ","End":"09:59.810","Text":"That is something we\u0027re going to test actually in part c. If we go to part c,"},{"Start":"09:59.810 ","End":"10:03.725","Text":"we need to add a new element to the array."},{"Start":"10:03.725 ","End":"10:04.930","Text":"I\u0027m just going to add the number 2,"},{"Start":"10:04.930 ","End":"10:07.850","Text":"so we now have 8 elements in the array."},{"Start":"10:07.850 ","End":"10:10.910","Text":"We\u0027ll see how many times that swaps,"},{"Start":"10:10.910 ","End":"10:13.100","Text":"cause it\u0027s the largest number it\u0027s going to"},{"Start":"10:13.100 ","End":"10:17.800","Text":"swap multiple times the way a bubble sort works,"},{"Start":"10:17.800 ","End":"10:21.320","Text":"is the larger values bubbled to the end of"},{"Start":"10:21.320 ","End":"10:25.220","Text":"the array and the smaller values drop down to the bottom of the array."},{"Start":"10:25.220 ","End":"10:32.165","Text":"But the largest item will be swapped the very first time you run your loop here,"},{"Start":"10:32.165 ","End":"10:34.370","Text":"it would have already swapped the largest value to the end,"},{"Start":"10:34.370 ","End":"10:36.910","Text":"and that\u0027s what we saw 15 was going to the end."},{"Start":"10:36.910 ","End":"10:38.900","Text":"Then the next biggest value,"},{"Start":"10:38.900 ","End":"10:41.030","Text":"the next time around this loop will end up there,"},{"Start":"10:41.030 ","End":"10:43.340","Text":"which was why 11 got there and so on."},{"Start":"10:43.340 ","End":"10:46.910","Text":"The smallest value takes a while to get down to the bottom there,"},{"Start":"10:46.910 ","End":"10:49.115","Text":"especially if it was the last element of the array,"},{"Start":"10:49.115 ","End":"10:50.510","Text":"and that\u0027s what we\u0027ll see,"},{"Start":"10:50.510 ","End":"10:52.185","Text":"so let\u0027s just check,"},{"Start":"10:52.185 ","End":"10:53.820","Text":"if that\u0027s the case."},{"Start":"10:53.820 ","End":"10:56.540","Text":"That is what we see, so 2 get swapped there,"},{"Start":"10:56.540 ","End":"10:58.370","Text":"then again here and then again here,"},{"Start":"10:58.370 ","End":"11:00.660","Text":"here and here,"},{"Start":"11:00.660 ","End":"11:05.460","Text":"so 7 swaps in total because while we had 8 elements,"},{"Start":"11:05.460 ","End":"11:06.990","Text":"we\u0027re swapping in pairs,"},{"Start":"11:06.990 ","End":"11:09.225","Text":"so we are going to do 7 iterations."},{"Start":"11:09.225 ","End":"11:12.785","Text":"That\u0027s why we have the minus 2 here."},{"Start":"11:12.785 ","End":"11:16.850","Text":"One is to account for the fact that we\u0027re counting from 0."},{"Start":"11:16.850 ","End":"11:19.860","Text":"So we didn\u0027t go all the way up to numItems of 7,"},{"Start":"11:19.860 ","End":"11:21.475","Text":"we go from 0-6."},{"Start":"11:21.475 ","End":"11:26.165","Text":"But then we subtract one more because we\u0027re dealing with pairs of numbers here."},{"Start":"11:26.165 ","End":"11:28.685","Text":"You\u0027ll see that when we\u0027re doing the swapping,"},{"Start":"11:28.685 ","End":"11:30.020","Text":"here is the pair,"},{"Start":"11:30.020 ","End":"11:32.975","Text":"it\u0027s dealing with, j and j plus 1."},{"Start":"11:32.975 ","End":"11:37.880","Text":"Those are the numbers that get swapped and that\u0027s why it\u0027s also minus 2 here as well."},{"Start":"11:37.880 ","End":"11:40.805","Text":"The program seems to work as we expect."},{"Start":"11:40.805 ","End":"11:45.125","Text":"It swaps pairs of numbers and it keeps on doing it using the outer loop"},{"Start":"11:45.125 ","End":"11:50.525","Text":"until everything has been swapped if it needs to be,"},{"Start":"11:50.525 ","End":"11:53.570","Text":"and the array will therefore be sorted."},{"Start":"11:53.570 ","End":"11:59.585","Text":"There\u0027s actually a fairly useful use of an array and it\u0027s a single array."},{"Start":"11:59.585 ","End":"12:04.490","Text":"But we had a pair of nested loops in order to do the bubble sort,"},{"Start":"12:04.490 ","End":"12:10.670","Text":"which eventually resulted in this unsorted array being sorted."},{"Start":"12:10.670 ","End":"12:12.395","Text":"That\u0027s it for this exercise."},{"Start":"12:12.395 ","End":"12:15.180","Text":"I\u0027ll see you soon for the next one."}],"ID":28353},{"Watched":false,"Name":"Exercise 6","Duration":"11m 45s","ChapterTopicVideoID":27233,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.110 ","End":"00:03.120","Text":"Hello again. In this exercise,"},{"Start":"00:03.120 ","End":"00:06.495","Text":"we\u0027ve been asked to create a method called 2 word 2,"},{"Start":"00:06.495 ","End":"00:10.200","Text":"that is not the number 2D and within it to declare"},{"Start":"00:10.200 ","End":"00:16.409","Text":"a 2-dimensional integer array called grid with 5 rows and 7 columns."},{"Start":"00:16.409 ","End":"00:21.240","Text":"In Part B, we\u0027ve been asked to use a pair of nested loops and then to"},{"Start":"00:21.240 ","End":"00:26.940","Text":"assign to each element of the array of value according to this formula."},{"Start":"00:26.940 ","End":"00:32.835","Text":"When the element value is equal to column index plus row index times 7."},{"Start":"00:32.835 ","End":"00:37.260","Text":"In Part c, we\u0027re asked after the nested loops in Part b,"},{"Start":"00:37.260 ","End":"00:42.055","Text":"which we write values to the array to create another pair of nested loops."},{"Start":"00:42.055 ","End":"00:43.340","Text":"In the inner loop,"},{"Start":"00:43.340 ","End":"00:49.429","Text":"we output each element from the array on the same line using system out print."},{"Start":"00:49.429 ","End":"00:51.920","Text":"Then after each element is output,"},{"Start":"00:51.920 ","End":"00:58.580","Text":"we output the tab character again on the same line using system out print."},{"Start":"00:58.580 ","End":"01:00.590","Text":"Then after the inner loop,"},{"Start":"01:00.590 ","End":"01:02.285","Text":"but inside the outer loop,"},{"Start":"01:02.285 ","End":"01:04.669","Text":"we output a new line character."},{"Start":"01:04.669 ","End":"01:10.460","Text":"In Part d, we\u0027re asked to test the program to see that it outputs the numbers 0 to 34."},{"Start":"01:10.460 ","End":"01:14.225","Text":"We make a change to the program in Part e. It"},{"Start":"01:14.225 ","End":"01:19.144","Text":"outputs the numbers 1 to 35 instead of 0 to 34."},{"Start":"01:19.144 ","End":"01:22.760","Text":"In Part f, we\u0027re asked to change the program again so"},{"Start":"01:22.760 ","End":"01:26.465","Text":"that when the initial size of the array is changed from 5 rows,"},{"Start":"01:26.465 ","End":"01:29.370","Text":"7 columns to 8 rows,"},{"Start":"01:29.370 ","End":"01:33.970","Text":"5 columns, the numbers 1 to 40 are printed."},{"Start":"01:33.970 ","End":"01:39.940","Text":"First thing to do is to then create a new method called 2D."},{"Start":"01:39.940 ","End":"01:42.300","Text":"The reason it\u0027s the word 2,"},{"Start":"01:42.300 ","End":"01:43.760","Text":"rather than the number 2,"},{"Start":"01:43.760 ","End":"01:49.160","Text":"is you cannot use a number as the first Part of an identifier."},{"Start":"01:49.160 ","End":"01:51.583","Text":"So I\u0027d put the number 2D,"},{"Start":"01:51.583 ","End":"01:53.690","Text":"it would give me an error message."},{"Start":"01:53.690 ","End":"01:57.335","Text":"We avoid that by spelling out as a word."},{"Start":"01:57.335 ","End":"01:59.075","Text":"Then in Part a,"},{"Start":"01:59.075 ","End":"02:05.450","Text":"I must declare a 2-dimensional integer array called grid with 5 rows and 7 columns."},{"Start":"02:05.450 ","End":"02:10.175","Text":"What I need to do there is to specify the data type. That\u0027s an integer."},{"Start":"02:10.175 ","End":"02:13.550","Text":"This time not just 1 bracket to signify it\u0027s an array,"},{"Start":"02:13.550 ","End":"02:18.395","Text":"but a pair of brackets to signify that it\u0027s 2-dimensional array,"},{"Start":"02:18.395 ","End":"02:21.170","Text":"then I\u0027m not initializing it with values,"},{"Start":"02:21.170 ","End":"02:22.850","Text":"I\u0027m just sizing it."},{"Start":"02:22.850 ","End":"02:24.725","Text":"What I need to do is to say,"},{"Start":"02:24.725 ","End":"02:27.155","Text":"it\u0027s an integer of size,"},{"Start":"02:27.155 ","End":"02:30.995","Text":"how many rows, followed by how many columns?"},{"Start":"02:30.995 ","End":"02:34.340","Text":"In Java syntax, you specify the row in the first pair"},{"Start":"02:34.340 ","End":"02:37.280","Text":"of brackets and the column in the second pair of brackets."},{"Start":"02:37.280 ","End":"02:39.710","Text":"That\u0027s Part 6a done,"},{"Start":"02:39.710 ","End":"02:46.055","Text":"we\u0027ve declared a 2-dimensional array with 5 rows and 7 columns."},{"Start":"02:46.055 ","End":"02:50.720","Text":"Now, I need to initialize my array or haven\u0027t given a name here,"},{"Start":"02:50.720 ","End":"02:52.640","Text":"so it\u0027s giving me an error."},{"Start":"02:52.640 ","End":"02:56.075","Text":"The name I\u0027m supposed to give it its grid."},{"Start":"02:56.075 ","End":"03:00.350","Text":"Now I\u0027m supposed to initialize all of the values"},{"Start":"03:00.350 ","End":"03:04.090","Text":"in the array by using a pair of nested loops in Part B,"},{"Start":"03:04.090 ","End":"03:07.130","Text":"and I assign a value according to the formula that\u0027s given."},{"Start":"03:07.130 ","End":"03:09.135","Text":"Let\u0027s have a pair of loops here."},{"Start":"03:09.135 ","End":"03:12.980","Text":"One\u0027s going to iterate through the rows and one\u0027s going to iterate through the columns."},{"Start":"03:12.980 ","End":"03:14.960","Text":"We start with the rows."},{"Start":"03:14.960 ","End":"03:21.260","Text":"Row 0, and while the number of rows is less than 5,"},{"Start":"03:21.260 ","End":"03:22.910","Text":"because I\u0027ve got 5 rows,"},{"Start":"03:22.910 ","End":"03:24.470","Text":"I\u0027m going to keep going,"},{"Start":"03:24.470 ","End":"03:28.550","Text":"and I\u0027ll increment row each time around the loop."},{"Start":"03:28.550 ","End":"03:32.120","Text":"Then I do the same thing for columns."},{"Start":"03:32.120 ","End":"03:36.525","Text":"Columns has to be less than 7."},{"Start":"03:36.525 ","End":"03:40.490","Text":"That\u0027s the pair of loops that I need to actually write"},{"Start":"03:40.490 ","End":"03:43.990","Text":"the value to specify which element I\u0027m looking for,"},{"Start":"03:43.990 ","End":"03:46.085","Text":"the name of the array is grid."},{"Start":"03:46.085 ","End":"03:50.370","Text":"I follow the row with the column."},{"Start":"03:50.370 ","End":"03:53.120","Text":"I\u0027m using 2 index variables to address"},{"Start":"03:53.120 ","End":"03:59.120","Text":"a 2-dimensional coordinate effectively in the grid in a 2-dimensional array."},{"Start":"03:59.120 ","End":"04:03.710","Text":"Then the value I actually want to write to it is column index,"},{"Start":"04:03.710 ","End":"04:08.600","Text":"which is a is stored in column plus Row Index,"},{"Start":"04:08.600 ","End":"04:15.945","Text":"which is stored in row multiplied by 7 because there\u0027s 7 columns."},{"Start":"04:15.945 ","End":"04:18.140","Text":"Each time I go to a new row,"},{"Start":"04:18.140 ","End":"04:21.935","Text":"I want to increase the number by 7."},{"Start":"04:21.935 ","End":"04:24.575","Text":"Then obviously add the column value on as well."},{"Start":"04:24.575 ","End":"04:29.660","Text":"This is going to write the values 0 to 34 in the array,"},{"Start":"04:29.660 ","End":"04:33.200","Text":"so by row and then across the columns."},{"Start":"04:33.200 ","End":"04:35.840","Text":"We\u0027ll see that in a moment when we display it."},{"Start":"04:35.840 ","End":"04:38.424","Text":"That\u0027s the first bit done then b."},{"Start":"04:38.424 ","End":"04:40.669","Text":"Then in Part c,"},{"Start":"04:40.669 ","End":"04:45.560","Text":"we\u0027re asked to create another pair of nested loops and they\u0027ll write out the values."},{"Start":"04:45.560 ","End":"04:47.210","Text":"We\u0027ve got 3 parts to do this."},{"Start":"04:47.210 ","End":"04:49.490","Text":"It\u0027s going to follow the previous array."},{"Start":"04:49.490 ","End":"04:50.630","Text":"It\u0027s not nested inside it."},{"Start":"04:50.630 ","End":"04:52.550","Text":"What I\u0027m going to do is I\u0027m actually going to copy it,"},{"Start":"04:52.550 ","End":"04:54.490","Text":"save me some typing,"},{"Start":"04:54.490 ","End":"04:56.484","Text":"I\u0027ll put it below,"},{"Start":"04:56.484 ","End":"04:59.720","Text":"and then I\u0027ll just take out what\u0027s inside the loop."},{"Start":"04:59.720 ","End":"05:02.600","Text":"Exactly what I had above there."},{"Start":"05:02.600 ","End":"05:04.370","Text":"This time I\u0027m not going to write values."},{"Start":"05:04.370 ","End":"05:07.280","Text":"I\u0027m going to output the values in the array."},{"Start":"05:07.280 ","End":"05:09.365","Text":"In Part 1,"},{"Start":"05:09.365 ","End":"05:12.590","Text":"I\u0027m asked to use system out print,"},{"Start":"05:12.590 ","End":"05:14.960","Text":"output each element on the same line."},{"Start":"05:14.960 ","End":"05:18.515","Text":"So I\u0027ll do that, the elements,"},{"Start":"05:18.515 ","End":"05:23.350","Text":"as we know, is addressed by row and then column."},{"Start":"05:23.870 ","End":"05:28.620","Text":"That\u0027s C1 and C2."},{"Start":"05:28.620 ","End":"05:31.770","Text":"I\u0027m asked to output the tab character."},{"Start":"05:31.770 ","End":"05:35.930","Text":"A tab character will just move the print."},{"Start":"05:35.930 ","End":"05:39.200","Text":"The next item that\u0027s going to be printed the cursor"},{"Start":"05:39.200 ","End":"05:42.470","Text":"effectively is moved across by a certain amount,"},{"Start":"05:42.470 ","End":"05:44.750","Text":"which is set by the system settings."},{"Start":"05:44.750 ","End":"05:52.640","Text":"Just means that we can have a nice space that output, that\u0027s that done."},{"Start":"05:52.640 ","End":"05:55.760","Text":"C2, C3 after the inner loop,"},{"Start":"05:55.760 ","End":"05:57.365","Text":"but inside the outer loop,"},{"Start":"05:57.365 ","End":"05:58.880","Text":"we output a new line character."},{"Start":"05:58.880 ","End":"06:01.145","Text":"The inner loop ends here."},{"Start":"06:01.145 ","End":"06:04.015","Text":"Therefore, we\u0027re still in the outer loop."},{"Start":"06:04.015 ","End":"06:10.400","Text":"On this line here, if I do system.out.print line,"},{"Start":"06:10.400 ","End":"06:13.940","Text":"that will print a newline character if I just leave it as that."},{"Start":"06:13.940 ","End":"06:17.915","Text":"Or I could print explicitly using system out print,"},{"Start":"06:17.915 ","End":"06:20.615","Text":"the newline character like this."},{"Start":"06:20.615 ","End":"06:22.730","Text":"Either we do the same job."},{"Start":"06:22.730 ","End":"06:25.430","Text":"That\u0027s it done. Hopefully,"},{"Start":"06:25.430 ","End":"06:29.270","Text":"that will now output the numbers from 0 to 34,"},{"Start":"06:29.270 ","End":"06:33.205","Text":"so we\u0027ve been asked to test in Part d. Let\u0027s go ahead and test it."},{"Start":"06:33.205 ","End":"06:37.010","Text":"What we get as the output is indeed 0 to 34."},{"Start":"06:37.010 ","End":"06:41.360","Text":"You can see they\u0027re spaced out because we\u0027ve got the tab character after each character."},{"Start":"06:41.360 ","End":"06:44.240","Text":"Then when we get to the end of a row,"},{"Start":"06:44.240 ","End":"06:49.945","Text":"we print a newline character to force the next row to go onto a new line."},{"Start":"06:49.945 ","End":"06:52.360","Text":"That\u0027s great. Part e,"},{"Start":"06:52.360 ","End":"06:56.930","Text":"we\u0027re asked to change the program outputs the numbers 1 to 35."},{"Start":"06:56.930 ","End":"07:00.260","Text":"Instead, we\u0027ve gotten from 0 to 34."},{"Start":"07:00.260 ","End":"07:04.580","Text":"That\u0027s a very easy change to make all we need to do is go to this line where it\u0027s"},{"Start":"07:04.580 ","End":"07:10.080","Text":"setting the initial values for each element and add 1 to it."},{"Start":"07:10.080 ","End":"07:11.420","Text":"It\u0027d be as simple as that."},{"Start":"07:11.420 ","End":"07:16.290","Text":"But just to prove it, I\u0027ll run it again just to show you that it does actually happen."},{"Start":"07:17.590 ","End":"07:21.485","Text":"You can see it was 0 to 34 previously,"},{"Start":"07:21.485 ","End":"07:25.180","Text":"and it\u0027s now 1 to 35."},{"Start":"07:25.180 ","End":"07:29.810","Text":"Last bit then it involved quite few changes actually,"},{"Start":"07:29.810 ","End":"07:35.885","Text":"it says to change the program so that when the initial size of the array is set,"},{"Start":"07:35.885 ","End":"07:37.820","Text":"it\u0027s not 5 rows and 7 columns,"},{"Start":"07:37.820 ","End":"07:40.445","Text":"but it\u0027s 8 rows and 5 columns."},{"Start":"07:40.445 ","End":"07:42.214","Text":"That bit\u0027s easy."},{"Start":"07:42.214 ","End":"07:48.020","Text":"But our program will now be broken because both this formula is going to be"},{"Start":"07:48.020 ","End":"07:53.850","Text":"wrong and the limits that we\u0027ve set in our loops are also going to be wrong."},{"Start":"07:53.850 ","End":"07:55.670","Text":"I could just manually change those,"},{"Start":"07:55.670 ","End":"07:59.510","Text":"but a better way of doing this so that it would cope with any change of"},{"Start":"07:59.510 ","End":"08:04.040","Text":"the array size is to use the length of a row and"},{"Start":"08:04.040 ","End":"08:08.780","Text":"the length of the syntax for that is quite interesting because it\u0027s possible in"},{"Start":"08:08.780 ","End":"08:14.405","Text":"Java to have a variable number of columns in a particular row."},{"Start":"08:14.405 ","End":"08:19.265","Text":"But the number of rows is always going to be 1 number."},{"Start":"08:19.265 ","End":"08:22.925","Text":"When we\u0027re talking about the number of rows,"},{"Start":"08:22.925 ","End":"08:26.345","Text":"we just need to say grid.length,"},{"Start":"08:26.345 ","End":"08:30.810","Text":"and that will return the number of rows in the array."},{"Start":"08:30.810 ","End":"08:32.355","Text":"That will be fine."},{"Start":"08:32.355 ","End":"08:37.595","Text":"I\u0027m going to change this line here also to say grid.length."},{"Start":"08:37.595 ","End":"08:41.120","Text":"However, this part is a little bit more tricky because I just"},{"Start":"08:41.120 ","End":"08:44.840","Text":"said it could be a variable number of columns."},{"Start":"08:44.840 ","End":"08:47.175","Text":"So 1 row could have 8 columns,"},{"Start":"08:47.175 ","End":"08:49.230","Text":"1 could have 7 columns,"},{"Start":"08:49.230 ","End":"08:53.240","Text":"and you\u0027ll never know unless you use this syntax,"},{"Start":"08:53.240 ","End":"08:54.905","Text":"which is to say grid."},{"Start":"08:54.905 ","End":"08:59.090","Text":"Then specify which row you\u0027re interested in."},{"Start":"08:59.090 ","End":"09:01.820","Text":"I\u0027m just going to say row because I already,"},{"Start":"09:01.820 ","End":"09:04.475","Text":"how about nonverbal and then length."},{"Start":"09:04.475 ","End":"09:09.155","Text":"Notice the difference between this syntax here and this syntax here."},{"Start":"09:09.155 ","End":"09:11.705","Text":"This returns the number of rows."},{"Start":"09:11.705 ","End":"09:16.850","Text":"This because I\u0027ve got a pair of brackets before length there,"},{"Start":"09:16.850 ","End":"09:22.135","Text":"will return the number of columns in the current row."},{"Start":"09:22.135 ","End":"09:25.355","Text":"My 1 has the same number of columns in every row"},{"Start":"09:25.355 ","End":"09:28.210","Text":"that might not always be true in a Java programs."},{"Start":"09:28.210 ","End":"09:31.490","Text":"The correct syntax is that 1."},{"Start":"09:31.490 ","End":"09:34.070","Text":"So I\u0027ll also need to change that down here as well,"},{"Start":"09:34.070 ","End":"09:37.835","Text":"where I\u0027m outputting not less than 7,"},{"Start":"09:37.835 ","End":"09:45.560","Text":"but less than grid brackets, row.length."},{"Start":"09:45.560 ","End":"09:48.695","Text":"I\u0027m just going to check that the syntax is right."},{"Start":"09:48.695 ","End":"09:52.250","Text":"Now. That will do something and it shouldn\u0027t give me any errors,"},{"Start":"09:52.250 ","End":"09:56.165","Text":"but I\u0027ll find that the calculation of the numbers is wrong."},{"Start":"09:56.165 ","End":"09:59.105","Text":"I\u0027ll show you what I mean by running it."},{"Start":"09:59.105 ","End":"10:03.290","Text":"If we were to just run what we had there,"},{"Start":"10:03.290 ","End":"10:07.835","Text":"we would see that it would still work and we\u0027d have a different layout here."},{"Start":"10:07.835 ","End":"10:11.270","Text":"But you\u0027ll see here there\u0027s a discontinuity between 5,"},{"Start":"10:11.270 ","End":"10:12.590","Text":"I would expect it to go into 6,"},{"Start":"10:12.590 ","End":"10:14.600","Text":"7, 8, and so on, but it\u0027s gone to 8."},{"Start":"10:14.600 ","End":"10:18.509","Text":"That\u0027s because I was multiplying by 7 always."},{"Start":"10:18.509 ","End":"10:20.850","Text":"I need to correct that formula,"},{"Start":"10:20.850 ","End":"10:23.535","Text":"to not say always by 7,"},{"Start":"10:23.535 ","End":"10:28.030","Text":"but by the number of columns in this row."},{"Start":"10:28.030 ","End":"10:33.080","Text":"Again, I use that syntax grid, row.length,"},{"Start":"10:33.080 ","End":"10:36.590","Text":"and that will return the number of columns in the current row and"},{"Start":"10:36.590 ","End":"10:40.500","Text":"then multiply that by this variable here row."},{"Start":"10:40.500 ","End":"10:43.915","Text":"We should then get the correct output."},{"Start":"10:43.915 ","End":"10:46.679","Text":"Let\u0027s test that, numbers we expect,"},{"Start":"10:46.679 ","End":"10:49.470","Text":"and we do so it\u0027s now going 1 to 5,"},{"Start":"10:49.470 ","End":"10:50.983","Text":"then 6, 7, 8, 9, 10,"},{"Start":"10:50.983 ","End":"10:54.365","Text":"and the next row and so on until we get to 40."},{"Start":"10:54.365 ","End":"10:58.985","Text":"Just to prove that this will work for any size of arrays,"},{"Start":"10:58.985 ","End":"11:03.570","Text":"I might put some of these changes to 9 and 6,"},{"Start":"11:03.570 ","End":"11:07.510","Text":"for example, and see if that works."},{"Start":"11:07.790 ","End":"11:10.980","Text":"Run that one more time."},{"Start":"11:10.980 ","End":"11:15.210","Text":"Here we go. Fingers crossed."},{"Start":"11:15.210 ","End":"11:18.410","Text":"It appears to do what we expect."},{"Start":"11:18.410 ","End":"11:19.850","Text":"That\u0027s it for this one,"},{"Start":"11:19.850 ","End":"11:23.090","Text":"is a little bit more complex because it was 2-dimensions."},{"Start":"11:23.090 ","End":"11:26.930","Text":"Also, even more complex because we have to consider this issue of"},{"Start":"11:26.930 ","End":"11:31.729","Text":"the length of the row and the length of the array overall."},{"Start":"11:31.729 ","End":"11:37.475","Text":"We found this new syntax helps us to find out how many columns are in a particular row."},{"Start":"11:37.475 ","End":"11:42.680","Text":"This regular syntax just tells us how many rows there are in the array overall."},{"Start":"11:42.680 ","End":"11:46.360","Text":"Thanks for watching, and I\u0027ll see you in the next one."}],"ID":28354},{"Watched":false,"Name":"Exercise 7","Duration":"10m 23s","ChapterTopicVideoID":27234,"CourseChapterTopicPlaylistID":268879,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.890","Text":"Hi again, everyone. In this video,"},{"Start":"00:01.890 ","End":"00:05.430","Text":"we\u0027ve been asked to create a method called transpose and within it to declare"},{"Start":"00:05.430 ","End":"00:09.735","Text":"a 2D char array initialized with the following values."},{"Start":"00:09.735 ","End":"00:13.545","Text":"We have a number of rows with character values in each row."},{"Start":"00:13.545 ","End":"00:16.320","Text":"In the first row, the characters are h,"},{"Start":"00:16.320 ","End":"00:18.240","Text":"i, s, t,"},{"Start":"00:18.240 ","End":"00:20.970","Text":"s. The second row is e,"},{"Start":"00:20.970 ","End":"00:23.295","Text":"s, e, space, a."},{"Start":"00:23.295 ","End":"00:25.890","Text":"Third row is r space c,"},{"Start":"00:25.890 ","End":"00:29.610","Text":"m, g. Fourth row is e, a,"},{"Start":"00:29.610 ","End":"00:31.170","Text":"r, e, e,"},{"Start":"00:31.170 ","End":"00:32.775","Text":"and fifth row is space,"},{"Start":"00:32.775 ","End":"00:34.260","Text":"space, e,"},{"Start":"00:34.260 ","End":"00:36.495","Text":"s, and a full stop."},{"Start":"00:36.495 ","End":"00:39.605","Text":"In part b, we\u0027re asked to declare a pair of integer variables,"},{"Start":"00:39.605 ","End":"00:43.375","Text":"row and column, both initialized to 0."},{"Start":"00:43.375 ","End":"00:47.390","Text":"In part c, we are then asked to create a pair of nested loops with"},{"Start":"00:47.390 ","End":"00:51.815","Text":"the inner loop iterating column and the outer loop iterating row."},{"Start":"00:51.815 ","End":"00:53.225","Text":"Inside the inner loop,"},{"Start":"00:53.225 ","End":"00:56.855","Text":"we output the element pointed to by row and column"},{"Start":"00:56.855 ","End":"01:01.145","Text":"using system out print so all characters appear on the same line."},{"Start":"01:01.145 ","End":"01:05.465","Text":"We test the program outputs the characters as we\u0027ve seen in part a,"},{"Start":"01:05.465 ","End":"01:08.960","Text":"in the order that they appear in the array by row."},{"Start":"01:08.960 ","End":"01:14.105","Text":"Then we\u0027re asked to swap the order of the loops created in part c so"},{"Start":"01:14.105 ","End":"01:20.330","Text":"the column is iterated in the outer loop and the row is iterated in the inner loop."},{"Start":"01:20.330 ","End":"01:25.955","Text":"We then test again and we\u0027re asked to explain the output that we see."},{"Start":"01:25.955 ","End":"01:31.295","Text":"Let\u0027s go ahead and create a method called transpose."},{"Start":"01:31.295 ","End":"01:35.860","Text":"The first thing we need to do is to declare a 2D char array."},{"Start":"01:35.860 ","End":"01:41.925","Text":"To do that, we need to specify the char datatype and because it\u0027s 2-dimensional,"},{"Start":"01:41.925 ","End":"01:49.575","Text":"we need a pair of brackets like this and then we give the identifier,"},{"Start":"01:49.575 ","End":"01:53.555","Text":"having been given a name here so we can choose anything we like."},{"Start":"01:53.555 ","End":"01:58.340","Text":"I\u0027m just going to call mine grid and then we\u0027ve been given a series of"},{"Start":"01:58.340 ","End":"02:03.620","Text":"values to initialize with and so because this is a 2-dimensional array,"},{"Start":"02:03.620 ","End":"02:09.115","Text":"the outer dimension needs a pair of brackets, like so."},{"Start":"02:09.115 ","End":"02:15.610","Text":"Then everything inside it will also have a pair of braces,"},{"Start":"02:15.610 ","End":"02:17.275","Text":"I should say curly brackets,"},{"Start":"02:17.275 ","End":"02:21.620","Text":"like this and rather than type them all out individually,"},{"Start":"02:21.620 ","End":"02:23.440","Text":"I could type them out h,"},{"Start":"02:23.440 ","End":"02:25.005","Text":"i, and so on."},{"Start":"02:25.005 ","End":"02:29.100","Text":"I\u0027ve actually just cheated and copied them to paste."},{"Start":"02:29.100 ","End":"02:31.080","Text":"We\u0027ll copy and paste them."},{"Start":"02:31.080 ","End":"02:36.335","Text":"I\u0027m just going to do that just to save us some time here in this video."},{"Start":"02:36.335 ","End":"02:41.840","Text":"Those are rows and just the key part there was to make sure that we\u0027ve"},{"Start":"02:41.840 ","End":"02:49.110","Text":"got an opening brace at the front and that defines the overall array."},{"Start":"02:49.110 ","End":"02:55.620","Text":"Then each row within the array is being defined by these inner braces"},{"Start":"02:55.620 ","End":"02:59.270","Text":"here and then each row is separated from the next row with"},{"Start":"02:59.270 ","End":"03:03.080","Text":"a comma so slightly compilers happy with that."},{"Start":"03:03.080 ","End":"03:04.820","Text":"There\u0027s no errors reported."},{"Start":"03:04.820 ","End":"03:06.500","Text":"There\u0027s my 2D array."},{"Start":"03:06.500 ","End":"03:11.975","Text":"Now, I\u0027ve been asked to declare a pair of integer variables, row and column."},{"Start":"03:11.975 ","End":"03:13.640","Text":"I\u0027m going to go ahead and do that,"},{"Start":"03:13.640 ","End":"03:17.275","Text":"and they both initialized to 0 so I can do that at the same time."},{"Start":"03:17.275 ","End":"03:20.555","Text":"Row is 0 and column is 0."},{"Start":"03:20.555 ","End":"03:24.560","Text":"Then in part c, I\u0027m asked to create a pair of nested loops with"},{"Start":"03:24.560 ","End":"03:30.230","Text":"the inner loop iterating column and the outer loop iterating row."},{"Start":"03:30.230 ","End":"03:35.855","Text":"I\u0027m going to do that with a for-loop because we know in advance how big"},{"Start":"03:35.855 ","End":"03:41.180","Text":"our loop is because of the fact that it\u0027s been initialized and we,"},{"Start":"03:41.180 ","End":"03:46.135","Text":"therefore, know how many elements are going to be in each part of the array."},{"Start":"03:46.135 ","End":"03:49.515","Text":"If we count the number of rows,"},{"Start":"03:49.515 ","End":"03:52.290","Text":"it\u0027s 1, 2,"},{"Start":"03:52.290 ","End":"03:54.555","Text":"and the second one here, 3,"},{"Start":"03:54.555 ","End":"03:59.745","Text":"4 and looks like it."},{"Start":"03:59.745 ","End":"04:04.295","Text":"There\u0027s another one here. There\u0027s 5 rows in total."},{"Start":"04:04.295 ","End":"04:12.180","Text":"We want to iterate row from 0 up to 5,"},{"Start":"04:12.180 ","End":"04:14.310","Text":"but not including 5 and we\u0027re going to say,"},{"Start":"04:14.310 ","End":"04:16.620","Text":"row = row plus 1."},{"Start":"04:16.620 ","End":"04:21.030","Text":"Then in the inner loop,"},{"Start":"04:21.030 ","End":"04:27.240","Text":"we will iterate column between 0 and 1,"},{"Start":"04:27.240 ","End":"04:32.465","Text":"2, 3, 4, 5 characters in each pair of braces here."},{"Start":"04:32.465 ","End":"04:35.155","Text":"That means we\u0027ve got 5 columns."},{"Start":"04:35.155 ","End":"04:37.485","Text":"It\u0027s a 5-by-5 array."},{"Start":"04:37.485 ","End":"04:42.860","Text":"Yes, I could do this in a slightly different way in looking at the length,"},{"Start":"04:42.860 ","End":"04:49.220","Text":"but I\u0027m not going to complicate things for the moment and we\u0027ll do that step last."},{"Start":"04:49.220 ","End":"04:51.800","Text":"There\u0027s a reason for that because it does get a"},{"Start":"04:51.800 ","End":"04:54.485","Text":"little bit more involved than it has been up till now."},{"Start":"04:54.485 ","End":"04:58.760","Text":"Let\u0027s now undo what it says in part d to output"},{"Start":"04:58.760 ","End":"05:03.200","Text":"the element pointed to by row and column using system out print,"},{"Start":"05:03.200 ","End":"05:06.095","Text":"not system out print line."},{"Start":"05:06.095 ","End":"05:12.330","Text":"The array is called grid and row comes first in Java,"},{"Start":"05:12.330 ","End":"05:17.480","Text":"column comes second so the way this will print is,"},{"Start":"05:17.480 ","End":"05:22.100","Text":"we will start with row 0 and then column 0, 1, 2, 3,"},{"Start":"05:22.100 ","End":"05:24.295","Text":"4, and then row 1,"},{"Start":"05:24.295 ","End":"05:25.890","Text":"column 0, 1, 2, 3,"},{"Start":"05:25.890 ","End":"05:28.040","Text":"4 and so on and so on."},{"Start":"05:28.040 ","End":"05:35.030","Text":"That\u0027s it actually in part e,"},{"Start":"05:35.030 ","End":"05:36.725","Text":"we just go ahead and test."},{"Start":"05:36.725 ","End":"05:43.670","Text":"Let\u0027s see what happens when we run this and we get what looks like some gibberish,"},{"Start":"05:43.670 ","End":"05:48.200","Text":"but that does appear to be the gibberish that we started with so h, i,"},{"Start":"05:48.200 ","End":"05:52.820","Text":"s, t, s was the first row and then we\u0027ve got e, s, c,"},{"Start":"05:52.820 ","End":"05:58.545","Text":"space, a on the next row and then we\u0027ve got r,"},{"Start":"05:58.545 ","End":"06:00.480","Text":"space, c, m,"},{"Start":"06:00.480 ","End":"06:02.805","Text":"g, and so on till we get to the end."},{"Start":"06:02.805 ","End":"06:06.110","Text":"It does appear to have done what is supposed to have done,"},{"Start":"06:06.110 ","End":"06:10.760","Text":"which is to print out each character in row order."},{"Start":"06:10.760 ","End":"06:14.165","Text":"Now, what we\u0027ve been asked to do is to"},{"Start":"06:14.165 ","End":"06:18.380","Text":"swap the orders of the loops that we created so that"},{"Start":"06:18.380 ","End":"06:26.165","Text":"column is iterated in the outer loop and row is iterated in the inner loop."},{"Start":"06:26.165 ","End":"06:27.950","Text":"Let\u0027s do that."},{"Start":"06:27.950 ","End":"06:30.710","Text":"We\u0027ll do column up here."},{"Start":"06:30.710 ","End":"06:38.025","Text":"Column is less than 5 and then row on the inner loop."},{"Start":"06:38.025 ","End":"06:41.210","Text":"The effect of this would be to print down"},{"Start":"06:41.210 ","End":"06:46.740","Text":"the columns across all of the rows so we iterate row 2,"},{"Start":"06:46.740 ","End":"06:48.765","Text":"0, 1, 2, 3, 4,"},{"Start":"06:48.765 ","End":"06:51.240","Text":"and we print in column 0,"},{"Start":"06:51.240 ","End":"06:52.605","Text":"1, 2, 3, 4."},{"Start":"06:52.605 ","End":"06:55.020","Text":"Then we print column 1, 1, 2, 3,"},{"Start":"06:55.020 ","End":"06:56.580","Text":"4, column 2, 1,"},{"Start":"06:56.580 ","End":"06:58.485","Text":"2, 3, 4, and so on."},{"Start":"06:58.485 ","End":"07:03.590","Text":"We\u0027ve transposed the columns and the rows in terms of how we\u0027re"},{"Start":"07:03.590 ","End":"07:11.070","Text":"outputting and so let\u0027s see what the effect of that is in f,"},{"Start":"07:11.070 ","End":"07:12.920","Text":"run our method again, or literally,"},{"Start":"07:12.920 ","End":"07:16.700","Text":"all we\u0027ve changed is the order in which we\u0027ve got our inner and outer loops,"},{"Start":"07:16.700 ","End":"07:20.720","Text":"we\u0027ve swapped them over and this suddenly magically,"},{"Start":"07:20.720 ","End":"07:26.390","Text":"the gibberish has been transferred into some texts that we can understand and it says,"},{"Start":"07:26.390 ","End":"07:28.490","Text":"here is a secret message."},{"Start":"07:28.490 ","End":"07:30.740","Text":"To explain what\u0027s going on in here,"},{"Start":"07:30.740 ","End":"07:35.660","Text":"what\u0027s happened is that we were printing across,"},{"Start":"07:35.660 ","End":"07:41.600","Text":"hence we saw the gibberish and if we were to align all of these underneath each other,"},{"Start":"07:41.600 ","End":"07:45.085","Text":"you\u0027d see exactly what\u0027s happened."},{"Start":"07:45.085 ","End":"07:46.655","Text":"I\u0027m going to do that here."},{"Start":"07:46.655 ","End":"07:48.320","Text":"Sorry if it takes a moment to do,"},{"Start":"07:48.320 ","End":"07:51.890","Text":"but I think it\u0027s worth it to understand what\u0027s actually going on."},{"Start":"07:51.890 ","End":"07:55.475","Text":"You should see a pattern emerging here,"},{"Start":"07:55.475 ","End":"08:01.700","Text":"which is if you read down the columns like this,"},{"Start":"08:01.700 ","End":"08:08.165","Text":"we\u0027re seeing here is a secret message, full stop."},{"Start":"08:08.165 ","End":"08:13.640","Text":"I didn\u0027t lay it out like that in the question deliberately so you wouldn\u0027t just spot it."},{"Start":"08:13.640 ","End":"08:15.215","Text":"I put it all on one line."},{"Start":"08:15.215 ","End":"08:19.805","Text":"But what you\u0027ve seen is if you are printing out in this direction,"},{"Start":"08:19.805 ","End":"08:22.340","Text":"you would see that gibberish message that we saw."},{"Start":"08:22.340 ","End":"08:26.510","Text":"All we\u0027ve done is print in a slightly different order so that"},{"Start":"08:26.510 ","End":"08:31.985","Text":"the text as it was input here is displayed out going down this way."},{"Start":"08:31.985 ","End":"08:35.435","Text":"This is a very simple form of encryption,"},{"Start":"08:35.435 ","End":"08:40.530","Text":"which starts with a transpose and in this grid, it\u0027s symmetrical 5-by-5."},{"Start":"08:40.530 ","End":"08:47.570","Text":"I did say earlier and there was a reason I didn\u0027t have an odd-sized row and column,"},{"Start":"08:47.570 ","End":"08:50.825","Text":"and I could\u0027ve used.length here if I needed to in that case."},{"Start":"08:50.825 ","End":"08:55.294","Text":"But I didn\u0027t want to do that because just to keep it simple,"},{"Start":"08:55.294 ","End":"08:59.070","Text":"it\u0027s much easier to have a symmetrical array."},{"Start":"08:59.090 ","End":"09:04.220","Text":"This type of cipher basically allows you to input"},{"Start":"09:04.220 ","End":"09:09.230","Text":"a set of characters and you write them into the array row by row."},{"Start":"09:09.230 ","End":"09:12.350","Text":"Here we initialized our array, row by row,"},{"Start":"09:12.350 ","End":"09:15.530","Text":"but we could have got a string and written the contents of"},{"Start":"09:15.530 ","End":"09:19.085","Text":"that string to each element in the row and when we got to the end of a row,"},{"Start":"09:19.085 ","End":"09:20.945","Text":"move to the next row,"},{"Start":"09:20.945 ","End":"09:26.030","Text":"we would end up with a string of a little bit gibberish like this."},{"Start":"09:26.030 ","End":"09:28.400","Text":"Then when we output the string,"},{"Start":"09:28.400 ","End":"09:32.990","Text":"we output by going down the columns and it would then"},{"Start":"09:32.990 ","End":"09:35.630","Text":"unscramble the encrypted string"},{"Start":"09:35.630 ","End":"09:38.435","Text":"if we did it in reverse and we put English in the first place,"},{"Start":"09:38.435 ","End":"09:40.430","Text":"it would scramble it if we displayed it"},{"Start":"09:40.430 ","End":"09:44.630","Text":"downwards and so doing the reverse process would unscramble it."},{"Start":"09:44.630 ","End":"09:50.315","Text":"This tiny little program allows us to do encryption in a way."},{"Start":"09:50.315 ","End":"09:53.090","Text":"As I\u0027ve said, I\u0027ve actually hard-coded these characters"},{"Start":"09:53.090 ","End":"09:55.865","Text":"into the array because we\u0027re doing an arrays exercise."},{"Start":"09:55.865 ","End":"09:59.870","Text":"But if I had put it into a string and then written some code that"},{"Start":"09:59.870 ","End":"10:04.080","Text":"would take the contents of that string out and write it into the array,"},{"Start":"10:04.080 ","End":"10:07.460","Text":"then we would have had a very simple encryption solution."},{"Start":"10:07.460 ","End":"10:09.469","Text":"That\u0027s it for this exercise."},{"Start":"10:09.469 ","End":"10:13.190","Text":"I hope that was enough to understand how 2D"},{"Start":"10:13.190 ","End":"10:17.420","Text":"arrays work and we\u0027ve used a slightly different datatype this time,"},{"Start":"10:17.420 ","End":"10:21.425","Text":"which was the char, which ended up producing a quite interesting result."},{"Start":"10:21.425 ","End":"10:24.450","Text":"Thanks for watching, and I\u0027ll see you soon."}],"ID":28355}],"Thumbnail":null,"ID":268879},{"Name":"Strings and String Processing","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"String processing basics","Duration":"9m 54s","ChapterTopicVideoID":27241,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.510","Text":"Hello and welcome to this video on strings and string processing."},{"Start":"00:03.510 ","End":"00:04.620","Text":"By the end of this section,"},{"Start":"00:04.620 ","End":"00:08.910","Text":"you\u0027ll be able to describe the relationship between primitive and compound data types."},{"Start":"00:08.910 ","End":"00:11.265","Text":"Explain the need for string processing,"},{"Start":"00:11.265 ","End":"00:16.110","Text":"apply string manipulation techniques in typical programming languages and"},{"Start":"00:16.110 ","End":"00:21.405","Text":"to select the most appropriate techniques to apply in a given situation."},{"Start":"00:21.405 ","End":"00:23.730","Text":"We\u0027ve already seen the types of data that can be"},{"Start":"00:23.730 ","End":"00:25.890","Text":"stored in variables in most programming languages,"},{"Start":"00:25.890 ","End":"00:28.875","Text":"and we\u0027ve seen this definition for datatypes."},{"Start":"00:28.875 ","End":"00:31.470","Text":"A datatype is a property of variables that"},{"Start":"00:31.470 ","End":"00:34.275","Text":"determines how stored data should be interpreted,"},{"Start":"00:34.275 ","End":"00:37.650","Text":"constrained, and used in expressions."},{"Start":"00:37.650 ","End":"00:42.060","Text":"Common datatypes we encountered included various sizes of integers,"},{"Start":"00:42.060 ","End":"00:47.050","Text":"real numbers and the Boolean and character datatypes."},{"Start":"00:47.050 ","End":"00:52.670","Text":"We refer to all these as fundamental or primitive datatypes."},{"Start":"00:52.670 ","End":"00:58.534","Text":"They use to create a simple variable that usually relates to a single memory location."},{"Start":"00:58.534 ","End":"01:02.660","Text":"We also mentioned that these primitive data types can be used"},{"Start":"01:02.660 ","End":"01:05.960","Text":"as the building blocks for more complex data types."},{"Start":"01:05.960 ","End":"01:10.070","Text":"One of the most important of these is a datatype called a string,"},{"Start":"01:10.070 ","End":"01:12.980","Text":"which makes use of the char primitive."},{"Start":"01:12.980 ","End":"01:17.255","Text":"Imagine a bunch of chars strung together on a piece of string,"},{"Start":"01:17.255 ","End":"01:22.235","Text":"1 after another and you\u0027ll understand why we call these strings."},{"Start":"01:22.235 ","End":"01:24.275","Text":"In many programming languages,"},{"Start":"01:24.275 ","End":"01:30.170","Text":"strings are implemented as a compound data structure using characters and an array."},{"Start":"01:30.170 ","End":"01:35.375","Text":"An array remember, is a way of storing multiple items of the same datatype,"},{"Start":"01:35.375 ","End":"01:40.100","Text":"which we reference using a single identifier and an index."},{"Start":"01:40.100 ","End":"01:43.205","Text":"Let\u0027s say we needed to store a password in a string."},{"Start":"01:43.205 ","End":"01:46.340","Text":"We use Python as our example high-level language."},{"Start":"01:46.340 ","End":"01:52.175","Text":"If we gave our string variable the identifier password and the value secret,"},{"Start":"01:52.175 ","End":"01:55.610","Text":"we\u0027d expect it to be stored like this."},{"Start":"01:55.610 ","End":"02:00.740","Text":"Each individual character is stored in a sequential memory location."},{"Start":"02:00.740 ","End":"02:04.670","Text":"We\u0027re not interested in what memory location each character is stored at,"},{"Start":"02:04.670 ","End":"02:08.300","Text":"we simply want to access the string and we can do so"},{"Start":"02:08.300 ","End":"02:13.210","Text":"by stating the identifier password in this case."},{"Start":"02:13.880 ","End":"02:17.000","Text":"Using the identifier inside the brackets of"},{"Start":"02:17.000 ","End":"02:21.530","Text":"a print statement outputs the contents of the string."},{"Start":"02:21.530 ","End":"02:25.775","Text":"However, if we want to access 1 individual character from the string,"},{"Start":"02:25.775 ","End":"02:31.340","Text":"in some languages, you can do so by using an index inside a pair of square brackets."},{"Start":"02:31.340 ","End":"02:33.125","Text":"In Python, for example,"},{"Start":"02:33.125 ","End":"02:38.280","Text":"the following line will only print the first character in the string."},{"Start":"02:39.010 ","End":"02:43.310","Text":"Remember arrays are usually 0 indexed,"},{"Start":"02:43.310 ","End":"02:45.335","Text":"meaning you start from 0,"},{"Start":"02:45.335 ","End":"02:48.290","Text":"and therefore strings also follow this format."},{"Start":"02:48.290 ","End":"02:54.530","Text":"Hence why we put 0 inside the brackets here and not 1 to print the first character."},{"Start":"02:54.530 ","End":"02:57.350","Text":"This line would output the letter R,"},{"Start":"02:57.350 ","End":"03:00.380","Text":"because R is the fourth letter in the string,"},{"Start":"03:00.380 ","End":"03:03.050","Text":"but we\u0027re counting from 0."},{"Start":"03:03.050 ","End":"03:07.580","Text":"Alternatively, we could store the first letter of the string into another variable,"},{"Start":"03:07.580 ","End":"03:11.540","Text":"which could be another string variable or a char variable."},{"Start":"03:11.540 ","End":"03:15.605","Text":"C doesn\u0027t have a built-in string datatype."},{"Start":"03:15.605 ","End":"03:20.410","Text":"But the equivalent in C would be to create an array of chars called password."},{"Start":"03:20.410 ","End":"03:24.380","Text":"If we give the array of chars the initial value secret,"},{"Start":"03:24.380 ","End":"03:30.415","Text":"each element of that char array would be assigned 1 letter from the word secret."},{"Start":"03:30.415 ","End":"03:33.740","Text":"Because we use an index value of 2 here the"},{"Start":"03:33.740 ","End":"03:36.965","Text":"third character in the string is stored into letter,"},{"Start":"03:36.965 ","End":"03:40.610","Text":"and the output is therefore the letter C. Java"},{"Start":"03:40.610 ","End":"03:44.375","Text":"uses a slightly different syntax using an object oriented style."},{"Start":"03:44.375 ","End":"03:47.495","Text":"Instead of using square brackets and an index,"},{"Start":"03:47.495 ","End":"03:50.810","Text":"we follow the identifier with a full stop or dot,"},{"Start":"03:50.810 ","End":"03:55.070","Text":"followed by charAt and a pair of round brackets."},{"Start":"03:55.070 ","End":"03:56.885","Text":"Inside the round brackets,"},{"Start":"03:56.885 ","End":"04:00.185","Text":"we give the position that we are looking to get the character from"},{"Start":"04:00.185 ","End":"04:05.380","Text":"equivalent to the index we used in the Python and C examples."},{"Start":"04:05.380 ","End":"04:08.150","Text":"This is almost identical to the C example,"},{"Start":"04:08.150 ","End":"04:12.274","Text":"but here we are extracting the last character from the string password,"},{"Start":"04:12.274 ","End":"04:16.325","Text":"and we\u0027re storing it into the variable last letter,"},{"Start":"04:16.325 ","End":"04:21.650","Text":"resulting in T being output to the screen."},{"Start":"04:21.650 ","End":"04:25.040","Text":"This active extracting a single character is"},{"Start":"04:25.040 ","End":"04:28.340","Text":"possibly something we\u0027d want to do in a typical program."},{"Start":"04:28.340 ","End":"04:32.360","Text":"But an even more common task we might want to perform would be extracting"},{"Start":"04:32.360 ","End":"04:36.845","Text":"several characters from 1 string and storing them in another string."},{"Start":"04:36.845 ","End":"04:40.775","Text":"We call this substring extraction."},{"Start":"04:40.775 ","End":"04:44.540","Text":"Take the example of someone\u0027s full name stored in a string."},{"Start":"04:44.540 ","End":"04:46.760","Text":"We might want to extract the first name into"},{"Start":"04:46.760 ","End":"04:50.420","Text":"1 string and perhaps the last name into another."},{"Start":"04:50.420 ","End":"04:56.735","Text":"This example in Python shows that we want to extract the first string from index 0 up to,"},{"Start":"04:56.735 ","End":"04:59.600","Text":"but not including index 4."},{"Start":"04:59.600 ","End":"05:04.550","Text":"The last string is extracted from index 5 up to the end of the string."},{"Start":"05:04.550 ","End":"05:07.235","Text":"While this works,"},{"Start":"05:07.235 ","End":"05:12.740","Text":"it will only work correctly for a name that has exactly 4 letters in the first name."},{"Start":"05:12.740 ","End":"05:15.680","Text":"Another very common string operation that we need to"},{"Start":"05:15.680 ","End":"05:19.730","Text":"perform is to search for a particular character in a string."},{"Start":"05:19.730 ","End":"05:22.850","Text":"In this case, we want to find the space so that we know"},{"Start":"05:22.850 ","End":"05:26.105","Text":"where the first name ends and the surname begins."},{"Start":"05:26.105 ","End":"05:30.100","Text":"Python has a keyword called Find that will do this."},{"Start":"05:30.100 ","End":"05:33.145","Text":"Find has to be supplied with a character to look for,"},{"Start":"05:33.145 ","End":"05:36.880","Text":"and it will return an integer value indicating where the character was"},{"Start":"05:36.880 ","End":"05:40.660","Text":"found or minus 1 if it was not found."},{"Start":"05:40.660 ","End":"05:43.435","Text":"Given this particular string in name,"},{"Start":"05:43.435 ","End":"05:48.880","Text":"position of space will contain ball after these lines run,"},{"Start":"05:48.880 ","End":"05:52.660","Text":"because the space is the fifth character in the string."},{"Start":"05:52.660 ","End":"05:58.550","Text":"An improved version of our program would be like so."},{"Start":"05:58.550 ","End":"06:01.150","Text":"This still works for the name John Lennon,"},{"Start":"06:01.150 ","End":"06:03.910","Text":"but now I\u0027ll work even if we change the contents of"},{"Start":"06:03.910 ","End":"06:07.750","Text":"the string to contain a longer first name than John,"},{"Start":"06:07.750 ","End":"06:12.200","Text":"because we\u0027re finding the space to show us where the first name ends."},{"Start":"06:12.200 ","End":"06:13.970","Text":"Note for the last name,"},{"Start":"06:13.970 ","End":"06:17.060","Text":"we add one to where the space was found because we"},{"Start":"06:17.060 ","End":"06:20.225","Text":"actually want the substring to skip over the space character."},{"Start":"06:20.225 ","End":"06:24.320","Text":"If we didn\u0027t do this, the last name would start with a space."},{"Start":"06:24.320 ","End":"06:27.065","Text":"Let us look at the same example in Java."},{"Start":"06:27.065 ","End":"06:31.009","Text":"In Java, the length of a string can be obtained as follows."},{"Start":"06:31.009 ","End":"06:32.990","Text":"Using dot notation,"},{"Start":"06:32.990 ","End":"06:35.420","Text":"the identifier followed by a dot,"},{"Start":"06:35.420 ","End":"06:41.360","Text":"we specify length and the length of the string currently in name will be returned."},{"Start":"06:41.360 ","End":"06:43.085","Text":"In this case 11."},{"Start":"06:43.085 ","End":"06:46.820","Text":"We can find the position of a space using index of,"},{"Start":"06:46.820 ","End":"06:49.990","Text":"which works like Find in Python."},{"Start":"06:49.990 ","End":"06:53.030","Text":"Index of is not as clear name is fine,"},{"Start":"06:53.030 ","End":"06:56.270","Text":"but I guess it makes sense as we\u0027re being asked to find a character,"},{"Start":"06:56.270 ","End":"07:01.595","Text":"and then we\u0027re returning the index of that character within the string."},{"Start":"07:01.595 ","End":"07:04.400","Text":"To complete our Java version of the program,"},{"Start":"07:04.400 ","End":"07:09.245","Text":"extracting a substring from a string in Java can be done like this."},{"Start":"07:09.245 ","End":"07:13.880","Text":"C doesn\u0027t have any simple in-built string manipulation facilities,"},{"Start":"07:13.880 ","End":"07:15.950","Text":"although you can make use of libraries of"},{"Start":"07:15.950 ","End":"07:19.775","Text":"pre-written code that can make strings easier to work with."},{"Start":"07:19.775 ","End":"07:22.685","Text":"To do what we\u0027ve just done in Python and Java,"},{"Start":"07:22.685 ","End":"07:28.550","Text":"we could create 2 new empty strings for the first and last names."},{"Start":"07:28.550 ","End":"07:34.175","Text":"Then we can use a loop to move through each character in the name string,"},{"Start":"07:34.175 ","End":"07:37.100","Text":"extracting the characters found into"},{"Start":"07:37.100 ","End":"07:41.330","Text":"the first array and stopping when we find the first space."},{"Start":"07:41.330 ","End":"07:43.595","Text":"We then carry on to the end of the string,"},{"Start":"07:43.595 ","End":"07:46.490","Text":"extracting characters and adding them to"},{"Start":"07:46.490 ","End":"07:52.970","Text":"the last array before finally outputting the contents of the first and last arrays,"},{"Start":"07:52.970 ","End":"07:57.100","Text":"which will now contain the first name and last name respectively."},{"Start":"07:57.100 ","End":"08:00.860","Text":"We could\u0027ve used this technique in Python or Java if we wanted, for example,"},{"Start":"08:00.860 ","End":"08:05.000","Text":"if we wanted to inspect each character in the string and"},{"Start":"08:05.000 ","End":"08:09.440","Text":"perhaps react in some way to specific characters that are found."},{"Start":"08:09.440 ","End":"08:12.110","Text":"Imagine we had a string which contained instructions for"},{"Start":"08:12.110 ","End":"08:15.245","Text":"a robot traversing a grid of squares,"},{"Start":"08:15.245 ","End":"08:18.225","Text":"understands the commands F for forward,"},{"Start":"08:18.225 ","End":"08:20.475","Text":"L turn left 90 degrees,"},{"Start":"08:20.475 ","End":"08:23.300","Text":"and R for turn right 90 degrees."},{"Start":"08:23.300 ","End":"08:25.385","Text":"When we encounter the first character,"},{"Start":"08:25.385 ","End":"08:27.920","Text":"we might move forward one square."},{"Start":"08:27.920 ","End":"08:31.835","Text":"We do the same for the second character and the third."},{"Start":"08:31.835 ","End":"08:34.640","Text":"Then we encounter an L character."},{"Start":"08:34.640 ","End":"08:39.230","Text":"We turn left and forward again and twice more."},{"Start":"08:39.230 ","End":"08:42.740","Text":"Then we encounter in our character so we turn right,"},{"Start":"08:42.740 ","End":"08:45.095","Text":"move forward, and so on."},{"Start":"08:45.095 ","End":"08:47.600","Text":"We call this act of reading from a string and"},{"Start":"08:47.600 ","End":"08:50.645","Text":"interpreting the symbols within it parsing,"},{"Start":"08:50.645 ","End":"08:54.230","Text":"a more complex form of parsing is used by interpreters and"},{"Start":"08:54.230 ","End":"09:00.430","Text":"compilers to translate high-level languages into something that can be executed by CPU."},{"Start":"09:00.430 ","End":"09:03.395","Text":"A common basic string manipulation we could do"},{"Start":"09:03.395 ","End":"09:06.530","Text":"is to join one string onto the end of another."},{"Start":"09:06.530 ","End":"09:10.055","Text":"We can do this using the string concatenation operator."},{"Start":"09:10.055 ","End":"09:13.055","Text":"In most languages, this is the plus symbol."},{"Start":"09:13.055 ","End":"09:17.105","Text":"Rather than adding 2 values together as it would do with integers or floats,"},{"Start":"09:17.105 ","End":"09:20.405","Text":"it\u0027s used to join one string onto the end of another."},{"Start":"09:20.405 ","End":"09:22.410","Text":"After running these 3 lines,"},{"Start":"09:22.410 ","End":"09:25.610","Text":"the third string will contain the characters."},{"Start":"09:25.610 ","End":"09:27.755","Text":"Here is a string dot,"},{"Start":"09:27.755 ","End":"09:29.855","Text":"and here is another dot,"},{"Start":"09:29.855 ","End":"09:33.830","Text":"because the concatenation operator joined the 2 strings together."},{"Start":"09:33.830 ","End":"09:37.715","Text":"Note that there\u0027s no spaces between the full stop and the end."},{"Start":"09:37.715 ","End":"09:40.490","Text":"Exactly the same sequence of characters in"},{"Start":"09:40.490 ","End":"09:43.835","Text":"the 2 original strings will be put into the output string,"},{"Start":"09:43.835 ","End":"09:46.340","Text":"with no additions or emissions."},{"Start":"09:46.340 ","End":"09:49.670","Text":"That\u0027s it for this video on the basics of string manipulation."},{"Start":"09:49.670 ","End":"09:54.930","Text":"In the next video, we\u0027ll look at some more complex operations. I\u0027ll see you then."}],"ID":28356},{"Watched":false,"Name":"Additional string processing","Duration":"8m 29s","ChapterTopicVideoID":27242,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.235","Text":"Hello again, welcome back."},{"Start":"00:02.235 ","End":"00:05.760","Text":"In the previous video, we saw that some of the basic operations needed for"},{"Start":"00:05.760 ","End":"00:09.570","Text":"string manipulation were finding the length of a string,"},{"Start":"00:09.570 ","End":"00:11.640","Text":"finding a character in a string,"},{"Start":"00:11.640 ","End":"00:14.220","Text":"extracting a character from a string,"},{"Start":"00:14.220 ","End":"00:17.250","Text":"and extracting a substring from a string."},{"Start":"00:17.250 ","End":"00:18.570","Text":"To this we can add"},{"Start":"00:18.570 ","End":"00:22.635","Text":"some other desirable operations that a typical program might want to perform."},{"Start":"00:22.635 ","End":"00:26.040","Text":"Counting how many times a character appears within a string,"},{"Start":"00:26.040 ","End":"00:28.335","Text":"changing the case of a string,"},{"Start":"00:28.335 ","End":"00:31.020","Text":"trimming the string so that the beginning and"},{"Start":"00:31.020 ","End":"00:33.807","Text":"the end don\u0027t contain a particular character,"},{"Start":"00:33.807 ","End":"00:37.260","Text":"splitting a string into several substrings,"},{"Start":"00:37.260 ","End":"00:42.300","Text":"and performing some more advanced matching and replacement operations on a string."},{"Start":"00:42.300 ","End":"00:48.470","Text":"In Python, 2 strings can be compared simply by using the equality operator like this."},{"Start":"00:48.470 ","End":"00:51.424","Text":"In Java and C, it is not so simple."},{"Start":"00:51.424 ","End":"00:56.570","Text":"In Java, comparing 2 strings like this will not generate any syntax errors,"},{"Start":"00:56.570 ","End":"00:59.255","Text":"but you\u0027ll not achieve the outcome you\u0027re looking for."},{"Start":"00:59.255 ","End":"01:05.360","Text":"This is because Java interprets the comparison string 1= string"},{"Start":"01:05.360 ","End":"01:11.630","Text":"2 as do these 2 strings occupy the same memory locations."},{"Start":"01:11.630 ","End":"01:13.940","Text":"In most cases we\u0027re not really interested in"},{"Start":"01:13.940 ","End":"01:16.610","Text":"whether they occupy the same memory locations or not."},{"Start":"01:16.610 ","End":"01:19.820","Text":"But we are interested in whether the strings are"},{"Start":"01:19.820 ","End":"01:25.740","Text":"the same length and contain exactly the same characters in the same case,"},{"Start":"01:25.740 ","End":"01:27.405","Text":"and in the same order."},{"Start":"01:27.405 ","End":"01:30.890","Text":"Obviously none of these strings are equal."},{"Start":"01:30.890 ","End":"01:34.460","Text":"Fortunately, there is a method built into every Java string that allows us to"},{"Start":"01:34.460 ","End":"01:38.255","Text":"compare that strings characters to another strings."},{"Start":"01:38.255 ","End":"01:42.215","Text":"The method called equals returns a Boolean value"},{"Start":"01:42.215 ","End":"01:46.310","Text":"depending on whether the 2 strings contain the same characters or not."},{"Start":"01:46.310 ","End":"01:50.465","Text":"Once again, there\u0027s no easy way to do this in C without library code,"},{"Start":"01:50.465 ","End":"01:54.110","Text":"you\u0027d either need to compare every character in both strings to each other using"},{"Start":"01:54.110 ","End":"01:57.740","Text":"a loop or use the library function string compare."},{"Start":"01:57.740 ","End":"02:02.930","Text":"This compares 2 strings and returns the integer 0 if they\u0027re equal."},{"Start":"02:02.930 ","End":"02:08.330","Text":"Python and Java provide a lot of built-in support for some of the other operations."},{"Start":"02:08.330 ","End":"02:10.430","Text":"Take count, for example."},{"Start":"02:10.430 ","End":"02:15.380","Text":"In Python, we can state the identifier of a string variable and using dot notation,"},{"Start":"02:15.380 ","End":"02:17.750","Text":"follow it with count and then in brackets,"},{"Start":"02:17.750 ","End":"02:19.565","Text":"the character we\u0027re looking for,"},{"Start":"02:19.565 ","End":"02:23.795","Text":"number of commas here will contain 2 after these lines are"},{"Start":"02:23.795 ","End":"02:28.430","Text":"run because there are 2 commas in the string sentence."},{"Start":"02:28.430 ","End":"02:31.220","Text":"Python\u0027s nice and easy when you\u0027re counting characters."},{"Start":"02:31.220 ","End":"02:33.470","Text":"Unfortunately, there\u0027s no direct equivalent in C"},{"Start":"02:33.470 ","End":"02:36.064","Text":"or Java without making the use of libraries."},{"Start":"02:36.064 ","End":"02:40.280","Text":"But we could instead use a loop to inspect each character and a count"},{"Start":"02:40.280 ","End":"02:45.160","Text":"variable to keep track of how many of the target character we found."},{"Start":"02:45.160 ","End":"02:50.220","Text":"Here we use the number of commas variable to keep track."},{"Start":"02:50.220 ","End":"02:53.165","Text":"Each time as we iterate around the loop,"},{"Start":"02:53.165 ","End":"02:54.710","Text":"we look at a character,"},{"Start":"02:54.710 ","End":"02:56.300","Text":"compare it to target."},{"Start":"02:56.300 ","End":"03:00.155","Text":"If we find that the character we\u0027re looking at is equal to target,"},{"Start":"03:00.155 ","End":"03:02.105","Text":"we increment number of commas,"},{"Start":"03:02.105 ","End":"03:05.075","Text":"and we keep doing that until we get to the end of the string."},{"Start":"03:05.075 ","End":"03:09.500","Text":"Both Python and Java provide facilities to change the case of a string."},{"Start":"03:09.500 ","End":"03:11.660","Text":"For example, a string with a mixture of"},{"Start":"03:11.660 ","End":"03:16.505","Text":"upper and lowercase letters can be forced to be all uppercase."},{"Start":"03:16.505 ","End":"03:20.145","Text":"The source string only had capital J and L,"},{"Start":"03:20.145 ","End":"03:23.765","Text":"but the output has been transformed to uppercase letters"},{"Start":"03:23.765 ","End":"03:28.130","Text":"throughout so all of the letters here are in uppercase."},{"Start":"03:28.130 ","End":"03:31.700","Text":"Once again, it\u0027s more complex in C. What we can do is"},{"Start":"03:31.700 ","End":"03:35.500","Text":"make use of the fact that characters are represented by ASCII values."},{"Start":"03:35.500 ","End":"03:38.205","Text":"For the Latin characters capital A-Z,"},{"Start":"03:38.205 ","End":"03:42.380","Text":"you can simply add 32 to the ASCII value to"},{"Start":"03:42.380 ","End":"03:46.235","Text":"force the uppercase character into a lowercase character."},{"Start":"03:46.235 ","End":"03:49.385","Text":"To change all the characters to lowercase A-Z,"},{"Start":"03:49.385 ","End":"03:56.060","Text":"we\u0027d have to change the 64 and 91 here to 96 and 123,"},{"Start":"03:56.060 ","End":"03:58.760","Text":"so the if statement only picks up characters"},{"Start":"03:58.760 ","End":"04:02.225","Text":"if they\u0027re in the lowercase range in the ASCII character set."},{"Start":"04:02.225 ","End":"04:07.670","Text":"Then we\u0027d subtract 32 to force the character into uppercase."},{"Start":"04:07.670 ","End":"04:10.310","Text":"Another common string manipulation is to remove"},{"Start":"04:10.310 ","End":"04:13.385","Text":"characters from the beginning or end of a string."},{"Start":"04:13.385 ","End":"04:17.750","Text":"For example, it\u0027s common to find a new line character at the end of some strings."},{"Start":"04:17.750 ","End":"04:19.760","Text":"We might want to remove that."},{"Start":"04:19.760 ","End":"04:23.360","Text":"Python and Java both provide an easy way to do this."},{"Start":"04:23.360 ","End":"04:29.315","Text":"In Python, the strip function removes any of the specified search character."},{"Start":"04:29.315 ","End":"04:33.380","Text":"In Java, we use replace all to search for 1 character,"},{"Start":"04:33.380 ","End":"04:36.605","Text":"in this case, newline and replace it with another."},{"Start":"04:36.605 ","End":"04:39.725","Text":"In this case, we replace newline with nothing."},{"Start":"04:39.725 ","End":"04:44.150","Text":"The effect will be to strip all the currencies of newline out of the string."},{"Start":"04:44.150 ","End":"04:45.830","Text":"replaceAll can make use of"},{"Start":"04:45.830 ","End":"04:50.770","Text":"more sophisticated replacements using something called a regular expression."},{"Start":"04:50.770 ","End":"04:56.105","Text":"A regular expression is a sequence of characters used to specify a search pattern."},{"Start":"04:56.105 ","End":"05:00.620","Text":"This search pattern can then be applied to a string to find matches."},{"Start":"05:00.620 ","End":"05:02.750","Text":"Regular expressions can be complex,"},{"Start":"05:02.750 ","End":"05:07.040","Text":"but they\u0027re very powerful and can save lots of programmer time if understood well,"},{"Start":"05:07.040 ","End":"05:09.620","Text":"we\u0027ll look at a few very quick examples."},{"Start":"05:09.620 ","End":"05:13.159","Text":"Here\u0027s a string with a mix of upper and lowercase numbers,"},{"Start":"05:13.159 ","End":"05:15.875","Text":"letters, and other symbols."},{"Start":"05:15.875 ","End":"05:17.570","Text":"In the first example,"},{"Start":"05:17.570 ","End":"05:20.510","Text":"the Java replaceAll function is looking for"},{"Start":"05:20.510 ","End":"05:24.530","Text":"matches for any of the digits between 0 and 9."},{"Start":"05:24.530 ","End":"05:26.150","Text":"Anytime it finds a match,"},{"Start":"05:26.150 ","End":"05:29.165","Text":"it\u0027s replaced with an X character."},{"Start":"05:29.165 ","End":"05:30.905","Text":"In the second example,"},{"Start":"05:30.905 ","End":"05:35.480","Text":"it\u0027s looking for matches for any of the uppercase letters capital A-Z."},{"Start":"05:35.480 ","End":"05:37.775","Text":"Anytime it finds a match,"},{"Start":"05:37.775 ","End":"05:40.475","Text":"it\u0027s replaced with a dot character."},{"Start":"05:40.475 ","End":"05:42.034","Text":"In the final example,"},{"Start":"05:42.034 ","End":"05:44.060","Text":"it\u0027s looking for anything that is not"},{"Start":"05:44.060 ","End":"05:47.149","Text":"a lowercase letter and replacing it with an underscore."},{"Start":"05:47.149 ","End":"05:49.340","Text":"Now a caret symbol,"},{"Start":"05:49.340 ","End":"05:51.740","Text":"which is Shift and 6 on most symbols,"},{"Start":"05:51.740 ","End":"05:54.934","Text":"which means not in a regular expression."},{"Start":"05:54.934 ","End":"05:56.765","Text":"The caret symbols here."},{"Start":"05:56.765 ","End":"06:00.095","Text":"If you see 2 backslashes in a regular expression,"},{"Start":"06:00.095 ","End":"06:03.380","Text":"the first slash is escaping the second one,"},{"Start":"06:03.380 ","End":"06:05.555","Text":"basically saying, I don\u0027t mean"},{"Start":"06:05.555 ","End":"06:11.720","Text":"the regular expression metacharacter dot or the metacharacter open bracket."},{"Start":"06:11.720 ","End":"06:15.740","Text":"I literally want to match a dot or an open bracket."},{"Start":"06:15.740 ","End":"06:20.885","Text":"The backslash character itself is also a regular expression metacharacter."},{"Start":"06:20.885 ","End":"06:25.954","Text":"If you want to search for a backslash inside a string using regular expressions,"},{"Start":"06:25.954 ","End":"06:28.130","Text":"you have to escape it first."},{"Start":"06:28.130 ","End":"06:32.705","Text":"That brings us to the final operation that\u0027s very common when manipulating strings"},{"Start":"06:32.705 ","End":"06:37.954","Text":"and that is to split out a component part of a string into individual items."},{"Start":"06:37.954 ","End":"06:40.385","Text":"This is sometimes called tokenizing,"},{"Start":"06:40.385 ","End":"06:43.010","Text":"as each small part is called a token."},{"Start":"06:43.010 ","End":"06:47.930","Text":"Here we have a string called instring and we use the split function,"},{"Start":"06:47.930 ","End":"06:51.785","Text":"which puts each token within the string into an array."},{"Start":"06:51.785 ","End":"06:54.950","Text":"In this case, it\u0027s splitting on the space character."},{"Start":"06:54.950 ","End":"06:59.615","Text":"Each new word will be stored as a different element in out array,"},{"Start":"06:59.615 ","End":"07:01.025","Text":"and here are the elements."},{"Start":"07:01.025 ","End":"07:02.180","Text":"In the next example,"},{"Start":"07:02.180 ","End":"07:07.300","Text":"what looks like a line of Java code is stored inside the string instring,"},{"Start":"07:07.300 ","End":"07:11.930","Text":"the splits more complex and makes use of a regular expression here."},{"Start":"07:11.930 ","End":"07:15.965","Text":"The regular expression is matching an opening bracket,"},{"Start":"07:15.965 ","End":"07:18.380","Text":"a closing or a closing bracket,"},{"Start":"07:18.380 ","End":"07:19.985","Text":"or a full stop."},{"Start":"07:19.985 ","End":"07:22.280","Text":"Anytime it encounters one of these,"},{"Start":"07:22.280 ","End":"07:25.760","Text":"it will use that character as a delimiter and put the string that"},{"Start":"07:25.760 ","End":"07:29.635","Text":"follows into a new element in the out array."},{"Start":"07:29.635 ","End":"07:32.255","Text":"We\u0027ll get an array with 5 elements."},{"Start":"07:32.255 ","End":"07:38.870","Text":"The word system out print line will be the first 3 elements because the dot was"},{"Start":"07:38.870 ","End":"07:42.020","Text":"matched as a delimiter and then will be an element containing"},{"Start":"07:42.020 ","End":"07:45.655","Text":"num because the open bracket element matched,"},{"Start":"07:45.655 ","End":"07:48.545","Text":"and then the last element will contain a semicolon,"},{"Start":"07:48.545 ","End":"07:53.750","Text":"which was split into the final array element because of the closing bracket delimiter,"},{"Start":"07:53.750 ","End":"07:57.110","Text":"the tokens in this example could be passed on to a compiler or"},{"Start":"07:57.110 ","End":"08:01.535","Text":"interpreter to check the syntax of a line of code before translation."},{"Start":"08:01.535 ","End":"08:04.280","Text":"In this section, we learned how to describe"},{"Start":"08:04.280 ","End":"08:07.415","Text":"the relationship between primitive and compound data types."},{"Start":"08:07.415 ","End":"08:10.241","Text":"Explain the need for string processing,"},{"Start":"08:10.241 ","End":"08:14.030","Text":"apply string manipulation techniques in typical programming languages"},{"Start":"08:14.030 ","End":"08:18.575","Text":"and to select the most appropriate techniques to apply in a given situation."},{"Start":"08:18.575 ","End":"08:20.810","Text":"Thank you for watching. Be sure to complete"},{"Start":"08:20.810 ","End":"08:24.277","Text":"the exercises in the workbook to consolidate your understanding,"},{"Start":"08:24.277 ","End":"08:29.640","Text":"and I\u0027ll see you in the next video where we\u0027ll introduce the idea of subprograms."}],"ID":28357},{"Watched":false,"Name":"Exercise 1","Duration":"6m 14s","ChapterTopicVideoID":27243,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.210","Text":"Hello everyone, welcome to this first exercise on strings."},{"Start":"00:03.210 ","End":"00:04.470","Text":"We\u0027ve been asked first of all to create"},{"Start":"00:04.470 ","End":"00:07.800","Text":"a new project and within it a class called stringApp."},{"Start":"00:07.800 ","End":"00:09.240","Text":"Then in part a,"},{"Start":"00:09.240 ","End":"00:12.119","Text":"we\u0027ve been asked to create a new method called reverse string."},{"Start":"00:12.119 ","End":"00:15.105","Text":"In part b to declare a string variable"},{"Start":"00:15.105 ","End":"00:18.885","Text":"and to initialize it with a literal string stressed."},{"Start":"00:18.885 ","End":"00:23.729","Text":"In part c then, we use a loop to iterate through each character,"},{"Start":"00:23.729 ","End":"00:26.235","Text":"starting from the back of the string."},{"Start":"00:26.235 ","End":"00:29.175","Text":"We output each character as it\u0027s encountered,"},{"Start":"00:29.175 ","End":"00:32.505","Text":"all on 1 line using system out print."},{"Start":"00:32.505 ","End":"00:36.335","Text":"Finally, in part d, we test the program by running it."},{"Start":"00:36.335 ","End":"00:37.700","Text":"If it works correctly,"},{"Start":"00:37.700 ","End":"00:40.055","Text":"it will print the input string reversed."},{"Start":"00:40.055 ","End":"00:43.820","Text":"We try some other input strings like spam and peels,"},{"Start":"00:43.820 ","End":"00:47.345","Text":"all 3 should output familiar words."},{"Start":"00:47.345 ","End":"00:50.760","Text":"We\u0027ve created a class called"},{"Start":"00:50.760 ","End":"00:55.995","Text":"stringApp already within a new project that I\u0027ve called Chapter 3 Strings,"},{"Start":"00:55.995 ","End":"00:57.785","Text":"you can call yours whatever you like."},{"Start":"00:57.785 ","End":"01:01.760","Text":"Let\u0027s go ahead and do the first part,"},{"Start":"01:01.760 ","End":"01:06.740","Text":"part a, which is to create a new method called reverse string."},{"Start":"01:06.740 ","End":"01:10.010","Text":"In part b, we\u0027ve been asked to declare a string variable"},{"Start":"01:10.010 ","End":"01:12.860","Text":"and to initialize it with a literal string stressed."},{"Start":"01:12.860 ","End":"01:15.277","Text":"Let\u0027s do that."},{"Start":"01:15.277 ","End":"01:16.490","Text":"We can call it anything we like,"},{"Start":"01:16.490 ","End":"01:21.050","Text":"I\u0027m going to call mine inputstring and there\u0027s the initial value."},{"Start":"01:21.050 ","End":"01:24.770","Text":"Then the main part of this exercise is to use a loop to"},{"Start":"01:24.770 ","End":"01:29.150","Text":"iterate through each character starting from the back of the string notice,"},{"Start":"01:29.150 ","End":"01:32.005","Text":"outputting each character as it\u0027s encountered,"},{"Start":"01:32.005 ","End":"01:34.710","Text":"all on 1 line using system out print."},{"Start":"01:34.710 ","End":"01:36.600","Text":"We\u0027re going to need a loop,"},{"Start":"01:36.600 ","End":"01:42.305","Text":"a for-loop is best here because it is a fixed number of characters in the string."},{"Start":"01:42.305 ","End":"01:44.780","Text":"I\u0027ll just form the block there,"},{"Start":"01:44.780 ","End":"01:48.530","Text":"the initial value for our index variable I,"},{"Start":"01:48.530 ","End":"01:51.260","Text":"is not going to be 0 as it normally is"},{"Start":"01:51.260 ","End":"01:54.470","Text":"starting from the front of an array or this front of a string."},{"Start":"01:54.470 ","End":"01:56.510","Text":"We want to start from the back of the strings so"},{"Start":"01:56.510 ","End":"01:58.850","Text":"how do we get a position at the back of the string?"},{"Start":"01:58.850 ","End":"02:03.650","Text":"Well, every string has a method built-in called length,"},{"Start":"02:03.650 ","End":"02:07.985","Text":"which will give you the length of the string and that is what we need."},{"Start":"02:07.985 ","End":"02:10.970","Text":"inputString.length, now notice,"},{"Start":"02:10.970 ","End":"02:12.590","Text":"if we did this,"},{"Start":"02:12.590 ","End":"02:16.325","Text":"we\u0027d actually get an error from the compiler because,"},{"Start":"02:16.325 ","End":"02:20.570","Text":"length on an array is a property of the array,"},{"Start":"02:20.570 ","End":"02:23.045","Text":"so it\u0027s a variable effectively."},{"Start":"02:23.045 ","End":"02:25.070","Text":"In case of a string,"},{"Start":"02:25.070 ","End":"02:28.640","Text":"it\u0027s a method called length which returns a value."},{"Start":"02:28.640 ","End":"02:31.000","Text":"Therefore, we need to put brackets afterwards,"},{"Start":"02:31.000 ","End":"02:34.260","Text":"because methods always have brackets,"},{"Start":"02:34.260 ","End":"02:36.500","Text":"and there\u0027s usually things that go inside the brackets."},{"Start":"02:36.500 ","End":"02:38.795","Text":"In this case, there\u0027s nothing that goes inside the bracket."},{"Start":"02:38.795 ","End":"02:42.770","Text":"What\u0027s going to happen here is length will return a value"},{"Start":"02:42.770 ","End":"02:46.610","Text":"which is the number of characters that are an input string."},{"Start":"02:46.610 ","End":"02:48.380","Text":"If I leave this as it is, I\u0027ll get an error,"},{"Start":"02:48.380 ","End":"02:51.530","Text":"but I\u0027m going to leave it as it is for the moment just to see what"},{"Start":"02:51.530 ","End":"02:55.570","Text":"the error is because I don\u0027t expect many of you would have got the same error."},{"Start":"02:55.570 ","End":"02:58.220","Text":"Let\u0027s move on to the next bit that\u0027s the initial value,"},{"Start":"02:58.220 ","End":"03:00.410","Text":"we start from the back of the string."},{"Start":"03:00.410 ","End":"03:03.620","Text":"What do we want to do to terminate the string?"},{"Start":"03:03.620 ","End":"03:08.330","Text":"Well, so long as i is greater than or equal to 0,"},{"Start":"03:08.330 ","End":"03:11.655","Text":"we\u0027ll keep going because we\u0027re going backward and we\u0027re going to"},{"Start":"03:11.655 ","End":"03:15.539","Text":"decrement I by 1 each time around the loop."},{"Start":"03:15.539 ","End":"03:19.310","Text":"That looks good up to a point for the loop,"},{"Start":"03:19.310 ","End":"03:20.600","Text":"but I know I\u0027m going to get an error,"},{"Start":"03:20.600 ","End":"03:22.640","Text":"but I\u0027m just going to leave it here for the moment just so that we"},{"Start":"03:22.640 ","End":"03:25.085","Text":"can see it happen and we can talk about it."},{"Start":"03:25.085 ","End":"03:28.280","Text":"Then what I\u0027m supposed to do is you system out print,"},{"Start":"03:28.280 ","End":"03:31.465","Text":"not print line, so it\u0027s all in the same output line."},{"Start":"03:31.465 ","End":"03:37.100","Text":"What I\u0027ve got to do is print the character that I\u0027m currently pointing at with i."},{"Start":"03:37.100 ","End":"03:40.115","Text":"The way we do that is in"},{"Start":"03:40.115 ","End":"03:44.900","Text":"some other languages you just put square brackets and the index variable."},{"Start":"03:44.900 ","End":"03:46.355","Text":"In the case of Java,"},{"Start":"03:46.355 ","End":"03:48.230","Text":"to extract a character,"},{"Start":"03:48.230 ","End":"03:52.370","Text":"we use charAT and then in round brackets,"},{"Start":"03:52.370 ","End":"03:54.604","Text":"not square brackets,"},{"Start":"03:54.604 ","End":"03:58.655","Text":"we put the position that we want to extract the char from."},{"Start":"03:58.655 ","End":"04:00.920","Text":"That will do the job for us."},{"Start":"04:00.920 ","End":"04:04.025","Text":"It\u0027s going to print out the last character,"},{"Start":"04:04.025 ","End":"04:05.900","Text":"then the last character, but 1,"},{"Start":"04:05.900 ","End":"04:08.105","Text":"a last character, but 2, and so on."},{"Start":"04:08.105 ","End":"04:10.690","Text":"It will start here and it will go backwards."},{"Start":"04:10.690 ","End":"04:14.150","Text":"However, I would get an error if I run this"},{"Start":"04:14.150 ","End":"04:17.765","Text":"and it will give me what\u0027s called an out-of-bounds error because"},{"Start":"04:17.765 ","End":"04:25.525","Text":"where I\u0027m starting input string length will return me 1,2,3,4,5,6,7,8,"},{"Start":"04:25.525 ","End":"04:27.784","Text":"but there isn\u0027t an eighth character."},{"Start":"04:27.784 ","End":"04:30.350","Text":"If I try to do charAT 8,"},{"Start":"04:30.350 ","End":"04:31.835","Text":"it would give me an error,"},{"Start":"04:31.835 ","End":"04:34.655","Text":"because remember we count from 0,"},{"Start":"04:34.655 ","End":"04:37.745","Text":"and so the last character would actually be at 7."},{"Start":"04:37.745 ","End":"04:39.545","Text":"To fix that problem,"},{"Start":"04:39.545 ","End":"04:45.920","Text":"we need to put minus 1 here so that the last character is pointed to correctly now,"},{"Start":"04:45.920 ","End":"04:48.560","Text":"because its length, which is 8 minus 1,"},{"Start":"04:48.560 ","End":"04:49.690","Text":"which will give us 7."},{"Start":"04:49.690 ","End":"04:51.905","Text":"No matter what we put into the string,"},{"Start":"04:51.905 ","End":"04:54.530","Text":"this will always give us the last character."},{"Start":"04:54.530 ","End":"04:59.900","Text":"That\u0027s a very common error you might have when you\u0027re first attempting this exercise."},{"Start":"04:59.900 ","End":"05:01.436","Text":"Let\u0027s see if it works then."},{"Start":"05:01.436 ","End":"05:06.090","Text":"This should now output this string backwards."},{"Start":"05:06.090 ","End":"05:08.580","Text":"Let\u0027s have a look to see if it does,"},{"Start":"05:08.580 ","End":"05:14.496","Text":"and it does indeed output the word stressed backwards, which spells desserts."},{"Start":"05:14.496 ","End":"05:18.380","Text":"We could test it with some other values and in fact rather than doing it each time,"},{"Start":"05:18.380 ","End":"05:22.935","Text":"I\u0027m just going to put all 3 strings into this 1 string."},{"Start":"05:22.935 ","End":"05:25.305","Text":"The word peels,"},{"Start":"05:25.305 ","End":"05:29.760","Text":"spam and stressed will all be printed backwards."},{"Start":"05:29.760 ","End":"05:34.025","Text":"Let\u0027s see if that works because we now got different length string."},{"Start":"05:34.025 ","End":"05:38.380","Text":"It\u0027s a nice test of whether it works and it does."},{"Start":"05:38.380 ","End":"05:40.650","Text":"We get 3 different words,"},{"Start":"05:40.650 ","End":"05:43.070","Text":"all seem to be intelligible words."},{"Start":"05:43.070 ","End":"05:47.795","Text":"Those input words we chose just happen to have the English meaning backwards as well."},{"Start":"05:47.795 ","End":"05:51.690","Text":"That\u0027s it for this first very straightforward exercise"},{"Start":"05:51.690 ","End":"05:54.380","Text":"there are a few [inaudible] potentially here,"},{"Start":"05:54.380 ","End":"05:56.326","Text":"particularly this part here."},{"Start":"05:56.326 ","End":"05:58.460","Text":"You may have tried to sprint from"},{"Start":"05:58.460 ","End":"06:01.760","Text":"the beginning of the string rather than the back of the string,"},{"Start":"06:01.760 ","End":"06:03.819","Text":"and then you would just get the same output."},{"Start":"06:03.819 ","End":"06:06.005","Text":"That wouldn\u0027t have been what we were asked to do."},{"Start":"06:06.005 ","End":"06:09.845","Text":"Apart from that, it\u0027s a pretty straightforward exercise."},{"Start":"06:09.845 ","End":"06:15.270","Text":"That was straightforward enough and I\u0027ll see you in the next one."}],"ID":28358},{"Watched":false,"Name":"Exercise 2","Duration":"5m 55s","ChapterTopicVideoID":27235,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.400","Text":"Hello. Welcome to this exercise in which you\u0027ve been"},{"Start":"00:02.400 ","End":"00:04.650","Text":"asked to create a new method called robot."},{"Start":"00:04.650 ","End":"00:07.380","Text":"In part a, we\u0027re asked to declare 2 variables."},{"Start":"00:07.380 ","End":"00:10.470","Text":"The first one is a string variable called commands."},{"Start":"00:10.470 ","End":"00:16.510","Text":"We initialize it with the literal string, an FFLFFRFFLFFFFRRFFH."},{"Start":"00:20.510 ","End":"00:26.715","Text":"Then the char variable is declared called currentCommand with no initial value."},{"Start":"00:26.715 ","End":"00:31.290","Text":"In part b, we\u0027re asked to use a loop to iterate through each character in the string,"},{"Start":"00:31.290 ","End":"00:34.800","Text":"extracting the current character into currentCommand."},{"Start":"00:34.800 ","End":"00:39.690","Text":"In part c, we\u0027re asked inside the loop to display a message as follows,"},{"Start":"00:39.690 ","End":"00:42.060","Text":"depending on the contents of currentCommand."},{"Start":"00:42.060 ","End":"00:45.615","Text":"For F, we output forward, for R,"},{"Start":"00:45.615 ","End":"00:48.750","Text":"we output right turn 90 degrees, for L,"},{"Start":"00:48.750 ","End":"00:51.060","Text":"we output a left-hand 90 degrees,"},{"Start":"00:51.060 ","End":"00:53.390","Text":"and for H we output halt."},{"Start":"00:53.390 ","End":"00:59.015","Text":"In part d, we test the code to make sure it produces the expected output."},{"Start":"00:59.015 ","End":"01:06.865","Text":"Let\u0027s go ahead and add a new method called robots into our class."},{"Start":"01:06.865 ","End":"01:12.935","Text":"The first thing we\u0027ve been asked to do is to create a string variable called commands,"},{"Start":"01:12.935 ","End":"01:16.625","Text":"which contains a long sequence of characters."},{"Start":"01:16.625 ","End":"01:19.540","Text":"Let\u0027s try and get those in."},{"Start":"01:19.540 ","End":"01:26.385","Text":"Then a char variable called currentCommand."},{"Start":"01:26.385 ","End":"01:28.530","Text":"No initial value given."},{"Start":"01:28.530 ","End":"01:32.400","Text":"That\u0027s our first 2 lines."},{"Start":"01:32.400 ","End":"01:38.090","Text":"In part b then we\u0027re asked to use a loop"},{"Start":"01:38.090 ","End":"01:40.910","Text":"to iterate through each character in the string and extract"},{"Start":"01:40.910 ","End":"01:44.515","Text":"the current character into currentCommand."},{"Start":"01:44.515 ","End":"01:48.395","Text":"For-loops, probably the right thing to do here."},{"Start":"01:48.395 ","End":"01:55.280","Text":"We declare an index variable called i and we terminate the loop"},{"Start":"01:55.280 ","End":"02:03.260","Text":"when i has reached the value of the length of commands."},{"Start":"02:03.260 ","End":"02:05.450","Text":"That\u0027s great."},{"Start":"02:05.450 ","End":"02:08.300","Text":"We need the round brackets here because this"},{"Start":"02:08.300 ","End":"02:12.394","Text":"is a method which returns the length of commands."},{"Start":"02:12.394 ","End":"02:17.120","Text":"Then i is incremented,"},{"Start":"02:17.120 ","End":"02:20.890","Text":"and then we do whatever it is we need to do in our loop."},{"Start":"02:20.890 ","End":"02:24.755","Text":"That looks good as a starting point."},{"Start":"02:24.755 ","End":"02:29.630","Text":"Now what we need to do is extract the current character into currentCommand."},{"Start":"02:29.630 ","End":"02:38.775","Text":"If the current character means we look at the commands from the beginning and char at i,"},{"Start":"02:38.775 ","End":"02:42.070","Text":"gives us the currentCommand."},{"Start":"02:42.320 ","End":"02:49.715","Text":"We need to say currentCommand equals commands.charAt(i)."},{"Start":"02:49.715 ","End":"02:52.760","Text":"That will extract 1 character at a time from"},{"Start":"02:52.760 ","End":"02:56.270","Text":"this string and then we\u0027re going to do something with it,"},{"Start":"02:56.270 ","End":"02:59.240","Text":"and if we had a real robot,"},{"Start":"02:59.240 ","End":"03:02.270","Text":"we could activate some motors and things."},{"Start":"03:02.270 ","End":"03:05.704","Text":"In this case, we\u0027re just going to output to the screen."},{"Start":"03:05.704 ","End":"03:07.880","Text":"A nice mechanism for this would be,"},{"Start":"03:07.880 ","End":"03:09.935","Text":"we could use a series of if statements,"},{"Start":"03:09.935 ","End":"03:13.805","Text":"but probably best to use a switch statement in this case."},{"Start":"03:13.805 ","End":"03:18.330","Text":"We\u0027re going to switch on currentCommand."},{"Start":"03:19.100 ","End":"03:24.330","Text":"Then inside the switch block we\u0027ll need a case statement."},{"Start":"03:24.970 ","End":"03:31.625","Text":"Let\u0027s start with the first one of forward."},{"Start":"03:31.625 ","End":"03:35.285","Text":"When we have an F in the character,"},{"Start":"03:35.285 ","End":"03:41.170","Text":"we will output a message saying \"Forward\"."},{"Start":"03:41.170 ","End":"03:44.790","Text":"That\u0027s the first command dealt with,"},{"Start":"03:44.790 ","End":"03:49.085","Text":"although we need a break here so that it doesn\u0027t follow through into the next command."},{"Start":"03:49.085 ","End":"03:52.280","Text":"Probably the easiest thing for us to do while we\u0027re editing this is to"},{"Start":"03:52.280 ","End":"03:55.475","Text":"just copy these 2 lines and change them."},{"Start":"03:55.475 ","End":"04:01.350","Text":"For right, we have a different output."},{"Start":"04:01.350 ","End":"04:10.965","Text":"We\u0027re going to say, \"Right turn 90 degrees,\" and that\u0027s that one done."},{"Start":"04:10.965 ","End":"04:15.530","Text":"Then I\u0027ll do the same thing again for left turn."},{"Start":"04:15.530 ","End":"04:19.690","Text":"Just change the R to an L here,"},{"Start":"04:19.690 ","End":"04:23.665","Text":"and the word right to left, obviously."},{"Start":"04:23.665 ","End":"04:29.555","Text":"Then finally, we\u0027ve got the last one which is for halt,"},{"Start":"04:29.555 ","End":"04:39.670","Text":"so I\u0027ll just copy those lines there and change this to H and \"Halt\" at the end."},{"Start":"04:39.910 ","End":"04:46.940","Text":"As we iterate through the different chars in the input string,"},{"Start":"04:46.940 ","End":"04:49.250","Text":"which is called commands,"},{"Start":"04:49.250 ","End":"04:54.470","Text":"we will extract each one into a single char using"},{"Start":"04:54.470 ","End":"04:56.210","Text":"this line and then output"},{"Start":"04:56.210 ","End":"04:59.980","Text":"a different message depending on what the value of the char was."},{"Start":"04:59.980 ","End":"05:04.954","Text":"Let\u0027s do part d and test that we get what we expect."},{"Start":"05:04.954 ","End":"05:10.535","Text":"We expect to see 2 forwards and then a left turn 90 degrees, 2 forwards,"},{"Start":"05:10.535 ","End":"05:12.620","Text":"a right turn 90 degrees, 2 forwards,"},{"Start":"05:12.620 ","End":"05:14.015","Text":"a left-hand 90 degrees,"},{"Start":"05:14.015 ","End":"05:16.545","Text":"4 forwards, 2 right turns,"},{"Start":"05:16.545 ","End":"05:18.225","Text":"2 forwards, and a halt."},{"Start":"05:18.225 ","End":"05:21.710","Text":"Let\u0027s see if it does what we expect."},{"Start":"05:21.710 ","End":"05:24.440","Text":"It does seem to have followed the right pattern,"},{"Start":"05:24.440 ","End":"05:25.610","Text":"2 forwards, left turn,"},{"Start":"05:25.610 ","End":"05:26.870","Text":"2 forwards, right turn,"},{"Start":"05:26.870 ","End":"05:29.455","Text":"2 forwards, left turn, 4 forwards,"},{"Start":"05:29.455 ","End":"05:32.525","Text":"2 right turns, 2 forwards, and a halt."},{"Start":"05:32.525 ","End":"05:36.080","Text":"Very simple example of parsing here,"},{"Start":"05:36.080 ","End":"05:39.860","Text":"where we\u0027re just going through an input string and"},{"Start":"05:39.860 ","End":"05:44.315","Text":"treating each char as a command and then doing something with it."},{"Start":"05:44.315 ","End":"05:47.390","Text":"Another example of iterating through a string,"},{"Start":"05:47.390 ","End":"05:52.730","Text":"extracting characters and doing something very similar to a previous exercise."},{"Start":"05:52.730 ","End":"05:56.040","Text":"That\u0027s it for this one. I\u0027ll see you in the next one."}],"ID":28359},{"Watched":false,"Name":"Exercise 3","Duration":"8m 12s","ChapterTopicVideoID":27236,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.020","Text":"Hello again. In this exercise we\u0027ve been asked to create a new method"},{"Start":"00:04.020 ","End":"00:08.100","Text":"called extractInitials() and then to declare a string variable,"},{"Start":"00:08.100 ","End":"00:11.955","Text":"fullName, containing the initial value, Robert Womack."},{"Start":"00:11.955 ","End":"00:13.830","Text":"In part b, we\u0027ve been asked to declare"},{"Start":"00:13.830 ","End":"00:18.525","Text":"an integer variable position with 0 as the initial value."},{"Start":"00:18.525 ","End":"00:20.460","Text":"Then in part c, to declare"},{"Start":"00:20.460 ","End":"00:26.475","Text":"a string variable initials with an empty string as the initial value."},{"Start":"00:26.475 ","End":"00:30.540","Text":"In part d, we\u0027re asked to add the first character of fullName"},{"Start":"00:30.540 ","End":"00:34.835","Text":"to the initials variable using the concatenation operator."},{"Start":"00:34.835 ","End":"00:37.970","Text":"In part e, we\u0027re asked to use a loop to iterate"},{"Start":"00:37.970 ","End":"00:40.985","Text":"through each character in the fullName string."},{"Start":"00:40.985 ","End":"00:43.280","Text":"In part e,i, we\u0027re asked to then,"},{"Start":"00:43.280 ","End":"00:46.137","Text":"each time a space or a hyphen is found,"},{"Start":"00:46.137 ","End":"00:50.660","Text":"to extract the next character and add it to initials."},{"Start":"00:50.660 ","End":"00:55.955","Text":"We keep going until there are no more characters left in the fullName string."},{"Start":"00:55.955 ","End":"01:00.115","Text":"In part f, we\u0027re asked to output the contents of the fullName"},{"Start":"01:00.115 ","End":"01:04.970","Text":"and the initial strings separated by a suitable character or message."},{"Start":"01:04.970 ","End":"01:06.350","Text":"In part g,"},{"Start":"01:06.350 ","End":"01:09.650","Text":"we\u0027re asked to test with the following initial values for fullName,"},{"Start":"01:09.650 ","End":"01:12.750","Text":"Robert Womack, Penny Martha Lane,"},{"Start":"01:12.750 ","End":"01:22.680","Text":"David Michael-Cox, and George Frederick Ernest Albert Saxe-Coburg-Gotha."},{"Start":"01:22.680 ","End":"01:28.100","Text":"Let\u0027s go ahead and create a new method called extractInitials."},{"Start":"01:28.100 ","End":"01:32.270","Text":"Then the first thing we\u0027ve been asked to do is to declare"},{"Start":"01:32.270 ","End":"01:37.280","Text":"a string variable called fullName containing the initial value Robert Womack."},{"Start":"01:37.280 ","End":"01:40.280","Text":"String is data type,"},{"Start":"01:40.280 ","End":"01:42.935","Text":"the identifier is fullName,"},{"Start":"01:42.935 ","End":"01:49.040","Text":"and then I assign it an initial value called Robert Womack."},{"Start":"01:49.040 ","End":"01:55.770","Text":"Then we declare an integer variable called position with the initial value of"},{"Start":"01:55.770 ","End":"02:04.345","Text":"zero and another string called initials with an empty string as the initial value."},{"Start":"02:04.345 ","End":"02:06.950","Text":"Now, in part d,"},{"Start":"02:06.950 ","End":"02:10.160","Text":"we\u0027re asked to add the first character of fullName to"},{"Start":"02:10.160 ","End":"02:14.615","Text":"the initial string using the concatenation operator."},{"Start":"02:14.615 ","End":"02:18.240","Text":"We\u0027re going to add 2 initials,"},{"Start":"02:18.240 ","End":"02:24.709","Text":"so we have to say initials=initials+ the first character of fullName."},{"Start":"02:24.709 ","End":"02:27.110","Text":"String concatenation is done with the plus,"},{"Start":"02:27.110 ","End":"02:28.745","Text":"basically joining 1 string to another."},{"Start":"02:28.745 ","End":"02:32.180","Text":"What this will do is add to nothing,"},{"Start":"02:32.180 ","End":"02:36.400","Text":"which is what we currently have in initials, the first initial."},{"Start":"02:36.400 ","End":"02:38.145","Text":"We know how to do that."},{"Start":"02:38.145 ","End":"02:42.540","Text":"We do fullName.charAt."},{"Start":"02:42.540 ","End":"02:46.220","Text":"Because the first character is going to be the first initial."},{"Start":"02:46.220 ","End":"02:48.410","Text":"We can just hard-code a 0 in there,"},{"Start":"02:48.410 ","End":"02:51.970","Text":"because that\u0027s always going to be the first initial."},{"Start":"02:51.970 ","End":"02:56.810","Text":"That\u0027s part d done and then the main part of the program is part e,"},{"Start":"02:56.810 ","End":"03:00.408","Text":"use a loop to iterate through each character in the string,"},{"Start":"03:00.408 ","End":"03:02.660","Text":"this could use a for-loop or you could use a while-loop."},{"Start":"03:02.660 ","End":"03:04.880","Text":"Just to be different I\u0027ll use a while-loop this time,"},{"Start":"03:04.880 ","End":"03:07.285","Text":"but you could use a for-loop if you want to."},{"Start":"03:07.285 ","End":"03:09.030","Text":"In a while-loop,"},{"Start":"03:09.030 ","End":"03:11.271","Text":"we have the condition at the top."},{"Start":"03:11.271 ","End":"03:13.220","Text":"Then we have our block,"},{"Start":"03:13.220 ","End":"03:15.615","Text":"so what should the condition be?"},{"Start":"03:15.615 ","End":"03:19.970","Text":"Well, I\u0027m going to keep going until there\u0027s no more characters left in the string."},{"Start":"03:19.970 ","End":"03:21.755","Text":"How do I know that?"},{"Start":"03:21.755 ","End":"03:27.620","Text":"Well it\u0027s when position reaches the value of the length of the string."},{"Start":"03:27.620 ","End":"03:36.180","Text":"While position is less than the length of the string and we get that by fullName.length."},{"Start":"03:36.190 ","End":"03:39.230","Text":"Then I can keep going."},{"Start":"03:39.230 ","End":"03:44.910","Text":"I don\u0027t need to minus 1 from here because it\u0027s less than fullName.length."},{"Start":"03:44.910 ","End":"03:47.640","Text":"It\u0027s while it\u0027s less than say, what have we got here?"},{"Start":"03:47.640 ","End":"03:53.960","Text":"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 characters,"},{"Start":"03:53.960 ","End":"03:57.420","Text":"so long as we\u0027re below 13,"},{"Start":"03:57.420 ","End":"04:00.590","Text":"so 0-12, we\u0027re good."},{"Start":"04:00.590 ","End":"04:03.140","Text":"That will be fine to use a less than."},{"Start":"04:03.140 ","End":"04:07.280","Text":"If I put less than or equals, I\u0027ll get an out of bounds error on the last character."},{"Start":"04:07.280 ","End":"04:11.450","Text":"It\u0027s less than and that should be fine and I need to minus 1 I think."},{"Start":"04:11.450 ","End":"04:15.620","Text":"That\u0027s the part that will iterate through each character in the string,"},{"Start":"04:15.620 ","End":"04:19.970","Text":"although we still got to deal with position but we\u0027ll do that in a moment."},{"Start":"04:19.970 ","End":"04:24.995","Text":"Now, what we\u0027ve been asked to do is each time in e,i,"},{"Start":"04:24.995 ","End":"04:27.620","Text":"we find a space or a hyphen,"},{"Start":"04:27.620 ","End":"04:32.180","Text":"we extract the next character and add it to the string initials."},{"Start":"04:32.180 ","End":"04:34.250","Text":"We\u0027re searching for spaces,"},{"Start":"04:34.250 ","End":"04:37.535","Text":"which we\u0027d find one there or hyphens."},{"Start":"04:37.535 ","End":"04:41.750","Text":"This one doesn\u0027t have one, but if it did either of those should result"},{"Start":"04:41.750 ","End":"04:45.680","Text":"in adding the next character to initials."},{"Start":"04:45.680 ","End":"04:48.800","Text":"We\u0027re searching for a new word basically,"},{"Start":"04:48.800 ","End":"04:53.224","Text":"by it being signaled to us by a space or by a hyphen."},{"Start":"04:53.224 ","End":"04:57.065","Text":"This is a simple If statement with an or involved."},{"Start":"04:57.065 ","End":"05:07.120","Text":"We\u0027re going to say if fullName.charAt(position) is equal"},{"Start":"05:07.120 ","End":"05:09.736","Text":"to a space"},{"Start":"05:09.736 ","End":"05:59.870","Text":"[inaudible]."},{"Start":"05:59.870 ","End":"06:31.140","Text":"Let\u0027s"},{"Start":"06:31.140 ","End":"06:32.280","Text":"see if that works."},{"Start":"06:32.280 ","End":"06:37.890","Text":"We\u0027ve had the fullName printed out and the initials RW, which is correct."},{"Start":"06:37.890 ","End":"06:39.889","Text":"That seems to work."},{"Start":"06:39.889 ","End":"06:45.150","Text":"Just to check that it works with a series of other different names."},{"Start":"06:45.350 ","End":"06:47.970","Text":"I won\u0027t do Penny Martha Lane,"},{"Start":"06:47.970 ","End":"06:51.205","Text":"but I will do David Michael-Cox,"},{"Start":"06:51.205 ","End":"06:55.280","Text":"because this is a slightly different one and it\u0027s a combination of characters."},{"Start":"06:55.280 ","End":"06:59.555","Text":"David and Michael are separated by spaces,"},{"Start":"06:59.555 ","End":"07:03.230","Text":"but Michael and Cox are separated by a hyphen."},{"Start":"07:03.230 ","End":"07:05.465","Text":"This is testing at all here."},{"Start":"07:05.465 ","End":"07:07.265","Text":"That\u0027s an interesting one to test."},{"Start":"07:07.265 ","End":"07:09.520","Text":"Let\u0027s see if that works."},{"Start":"07:09.520 ","End":"07:11.600","Text":"Yeah, it gives us DMC."},{"Start":"07:11.600 ","End":"07:13.535","Text":"That seems to have worked as well."},{"Start":"07:13.535 ","End":"07:23.674","Text":"Let\u0027s do the final very long one which has got multiple spaces and multiple hyphens,"},{"Start":"07:23.674 ","End":"07:28.190","Text":"so it\u0027s a very good one to test with."},{"Start":"07:28.190 ","End":"07:31.170","Text":"I will name this one."},{"Start":"07:31.280 ","End":"07:35.270","Text":"There we go, is we\u0027re going to test with multiple spaces and"},{"Start":"07:35.270 ","End":"07:40.220","Text":"multiple hyphens as well and see if that still stands up,"},{"Start":"07:40.220 ","End":"07:42.800","Text":"if our code implementation is correct."},{"Start":"07:42.800 ","End":"07:45.215","Text":"There we go, G,"},{"Start":"07:45.215 ","End":"07:47.630","Text":"F, E, A, S, C,"},{"Start":"07:47.630 ","End":"07:52.780","Text":"G, G-F-E-A-S-C-G. Done the job."},{"Start":"07:52.780 ","End":"07:55.025","Text":"Not too bad this one."},{"Start":"07:55.025 ","End":"07:58.385","Text":"A little bit of a loop and a little bit logic inside the loop."},{"Start":"07:58.385 ","End":"08:01.550","Text":"The only really difficult thing here is knowing to get"},{"Start":"08:01.550 ","End":"08:06.935","Text":"the next character and also the \"or\" inside the conditional statement here."},{"Start":"08:06.935 ","End":"08:09.050","Text":"Apart from that, pretty straightforward."},{"Start":"08:09.050 ","End":"08:12.840","Text":"That\u0027s it for this one. I\u0027ll see you in the next one."}],"ID":28360},{"Watched":false,"Name":"Exercise 4","Duration":"9m 17s","ChapterTopicVideoID":27237,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.590","Text":"Hi, everyone. In this video,"},{"Start":"00:01.590 ","End":"00:03.610","Text":"we\u0027ve been asked to create a new method called"},{"Start":"00:03.610 ","End":"00:07.625","Text":"parseAssembly and then in part a to declare 2 variables."},{"Start":"00:07.625 ","End":"00:11.670","Text":"A string variable with the initial value LDA 101"},{"Start":"00:11.670 ","End":"00:18.120","Text":"STA 100 NOP LDA 102 NOP HLT."},{"Start":"00:18.120 ","End":"00:24.465","Text":"In part 2, to declare a string variable called strMatch with the initial value LDA."},{"Start":"00:24.465 ","End":"00:28.530","Text":"In part b, we\u0027re asked to split the input string on spaces"},{"Start":"00:28.530 ","End":"00:33.480","Text":"into a new array so that each substring is stored in a new array element."},{"Start":"00:33.480 ","End":"00:37.205","Text":"For example, LDA will be stored in array element 0,"},{"Start":"00:37.205 ","End":"00:40.430","Text":"101 in array element 1, and so on."},{"Start":"00:40.430 ","End":"00:44.450","Text":"In part c, we\u0027re asked to use a loop to inspect each element in"},{"Start":"00:44.450 ","End":"00:48.979","Text":"the array to see if it contains the same value as strMatch."},{"Start":"00:48.979 ","End":"00:53.150","Text":"If it does, we output the following all on the same line."},{"Start":"00:53.150 ","End":"00:56.930","Text":"The contents of strMatch, a space character,"},{"Start":"00:56.930 ","End":"00:59.945","Text":"the contents of the next element in the array,"},{"Start":"00:59.945 ","End":"01:01.970","Text":"and a new line character."},{"Start":"01:01.970 ","End":"01:05.135","Text":"In part d, we\u0027re asked to test that the program outputs"},{"Start":"01:05.135 ","End":"01:09.785","Text":"LDA 101 and LDA 102 and nothing else."},{"Start":"01:09.785 ","End":"01:13.310","Text":"In part e, we\u0027re asked to change the initial value of strMatch to"},{"Start":"01:13.310 ","End":"01:18.325","Text":"STA and check the output is STA 100."},{"Start":"01:18.325 ","End":"01:23.885","Text":"Let\u0027s create a new method called parseAssembly."},{"Start":"01:23.885 ","End":"01:28.370","Text":"First thing we need to do is declare a string variable which"},{"Start":"01:28.370 ","End":"01:33.555","Text":"contains an initial value given to us in part a1."},{"Start":"01:33.555 ","End":"01:41.135","Text":"Let\u0027s call our string source and put the texts that we are required to put in."},{"Start":"01:41.135 ","End":"01:46.115","Text":"These appear to be assembly language instructions."},{"Start":"01:46.115 ","End":"01:49.325","Text":"Hence the name of the program."},{"Start":"01:49.325 ","End":"01:51.440","Text":"I just check off those."},{"Start":"01:51.440 ","End":"01:56.895","Text":"LDA STA NOP LDA NOP and HLT."},{"Start":"01:56.895 ","End":"01:59.175","Text":"That\u0027s our source string."},{"Start":"01:59.175 ","End":"02:05.940","Text":"Then another string variable called string match or STR match."},{"Start":"02:05.940 ","End":"02:10.264","Text":"That is going to have the initial value LDA."},{"Start":"02:10.264 ","End":"02:12.320","Text":"Now in part b,"},{"Start":"02:12.320 ","End":"02:15.470","Text":"we\u0027ve been asked to split the input string on"},{"Start":"02:15.470 ","End":"02:20.900","Text":"spaces and to store each substring into a new array element."},{"Start":"02:20.900 ","End":"02:24.305","Text":"For example, LDA will be stored in array element 0,"},{"Start":"02:24.305 ","End":"02:27.140","Text":"101 will be stored in array,"},{"Start":"02:27.140 ","End":"02:32.455","Text":"in fact, it will be stored in element 1 and so on."},{"Start":"02:32.455 ","End":"02:35.180","Text":"What we need to do is we could do,"},{"Start":"02:35.180 ","End":"02:37.895","Text":"as we\u0027ve previously done, search for spaces."},{"Start":"02:37.895 ","End":"02:39.275","Text":"Once we find a space,"},{"Start":"02:39.275 ","End":"02:44.990","Text":"now we\u0027ve got a new save data and extract that data and just keep doing that."},{"Start":"02:44.990 ","End":"02:48.665","Text":"Then putting each substring into an element of an array."},{"Start":"02:48.665 ","End":"02:55.910","Text":"But there\u0027s actually an in-built string function that will do that in Java called split."},{"Start":"02:55.910 ","End":"02:58.070","Text":"We might as well just use it that."},{"Start":"02:58.070 ","End":"03:04.880","Text":"The way split works is you need to put the result into a string array."},{"Start":"03:04.880 ","End":"03:10.985","Text":"I\u0027m going to call mine out because it\u0027s the output of the split function."},{"Start":"03:10.985 ","End":"03:16.760","Text":"Then we need to do is you need to give the name of the string and non-cases source,"},{"Start":"03:16.760 ","End":"03:18.790","Text":"and you need to say split."},{"Start":"03:18.790 ","End":"03:21.050","Text":"Then inside these brackets here,"},{"Start":"03:21.050 ","End":"03:23.915","Text":"you say what you want to split on."},{"Start":"03:23.915 ","End":"03:26.450","Text":"In our case, we\u0027re going to split on a space."},{"Start":"03:26.450 ","End":"03:28.730","Text":"We could split on a comma or"},{"Start":"03:28.730 ","End":"03:32.345","Text":"a hash or wherever it is that separates 1 thing from another."},{"Start":"03:32.345 ","End":"03:34.610","Text":"A very common one is a space."},{"Start":"03:34.610 ","End":"03:36.485","Text":"We\u0027re going to split on a space."},{"Start":"03:36.485 ","End":"03:41.945","Text":"Now what we should have at the end of this line is we should have an array full"},{"Start":"03:41.945 ","End":"03:45.050","Text":"of tokens which are made up of"},{"Start":"03:45.050 ","End":"03:48.540","Text":"these individual words here because they\u0027d been split by line."},{"Start":"03:48.540 ","End":"03:50.330","Text":"If that\u0027s the case,"},{"Start":"03:50.330 ","End":"03:54.290","Text":"what we can now do is using a loop as we\u0027re asked to do in part c,"},{"Start":"03:54.290 ","End":"03:56.870","Text":"we can inspect each element in the array to see if it"},{"Start":"03:56.870 ","End":"04:01.635","Text":"contains the same value as strMatch."},{"Start":"04:01.635 ","End":"04:04.845","Text":"We may also use a for-loop to do this."},{"Start":"04:04.845 ","End":"04:08.615","Text":"Let\u0027s go index variable i as usual."},{"Start":"04:08.615 ","End":"04:16.220","Text":"Now, we want to keep going while we haven\u0027t got all the way through the output array."},{"Start":"04:16.220 ","End":"04:20.930","Text":"We want to go i is less than out.length."},{"Start":"04:20.930 ","End":"04:25.640","Text":"No brackets required because we\u0027re dealing with an array of strings and"},{"Start":"04:25.640 ","End":"04:30.610","Text":"an array has a property called length as opposed to a method called length."},{"Start":"04:30.610 ","End":"04:33.400","Text":"We don\u0027t need a pair of round brackets there."},{"Start":"04:33.400 ","End":"04:38.585","Text":"We keep going incrementing i by 1 each time around the loop."},{"Start":"04:38.585 ","End":"04:41.015","Text":"There\u0027s our for-loop."},{"Start":"04:41.015 ","End":"04:47.960","Text":"Now what we\u0027ve been asked to do is to output the following on the same line,"},{"Start":"04:47.960 ","End":"04:54.135","Text":"but only if the current value we are looking at is the same as string match."},{"Start":"04:54.135 ","End":"04:56.890","Text":"We need a selection statement here."},{"Start":"04:56.890 ","End":"05:05.389","Text":"Now we can\u0027t say double equals because that doesn\u0027t work for strings."},{"Start":"05:05.389 ","End":"05:08.060","Text":"That we\u0027ll just compare the address at which"},{"Start":"05:08.060 ","End":"05:10.670","Text":"the strings are stored and we don\u0027t want that."},{"Start":"05:10.670 ","End":"05:14.135","Text":"We want to know that the values inside the string are the same."},{"Start":"05:14.135 ","End":"05:17.615","Text":"In this case, I\u0027m looking to see that LDA,"},{"Start":"05:17.615 ","End":"05:23.015","Text":"that string is the current value of the current element I\u0027m looking at."},{"Start":"05:23.015 ","End":"05:25.070","Text":"In order to do that,"},{"Start":"05:25.070 ","End":"05:26.540","Text":"I need to do something like this."},{"Start":"05:26.540 ","End":"05:30.980","Text":"I need to say the name of the string, which is strMatch."},{"Start":"05:30.980 ","End":"05:34.400","Text":"In this case, I use a method called equals."},{"Start":"05:34.400 ","End":"05:36.725","Text":"Then inside these round brackets,"},{"Start":"05:36.725 ","End":"05:41.585","Text":"I supply the thing that I want to compare it to for equality."},{"Start":"05:41.585 ","End":"05:48.335","Text":"In this case, it\u0027s the current element that I\u0027m looking at in the out array."},{"Start":"05:48.335 ","End":"05:50.330","Text":"If they do match,"},{"Start":"05:50.330 ","End":"05:52.465","Text":"then I do something."},{"Start":"05:52.465 ","End":"05:56.325","Text":"What is it that I didn\u0027t? Let me just check I\u0027ve got my brackets right there."},{"Start":"05:56.325 ","End":"05:58.220","Text":"Rather than a semicolon,"},{"Start":"05:58.220 ","End":"06:00.725","Text":"let me put a pair of braces."},{"Start":"06:00.725 ","End":"06:07.680","Text":"What I want to do is output the contents of string match."},{"Start":"06:07.680 ","End":"06:09.275","Text":"This is along the same line."},{"Start":"06:09.275 ","End":"06:12.240","Text":"I want to use printout and print line."},{"Start":"06:12.250 ","End":"06:15.515","Text":"Contents of string match."},{"Start":"06:15.515 ","End":"06:20.015","Text":"I could have another line here doing several system out print."},{"Start":"06:20.015 ","End":"06:24.050","Text":"But what I\u0027ll do is I\u0027ll just concatenate all of these things that I\u0027m supposed to put."},{"Start":"06:24.050 ","End":"06:25.880","Text":"I can make it appear on 1 line."},{"Start":"06:25.880 ","End":"06:29.130","Text":"Part c2 asked me to put a space character in."},{"Start":"06:29.130 ","End":"06:33.440","Text":"Let\u0027s concatenate a space character onto the contents of a string match."},{"Start":"06:33.440 ","End":"06:37.955","Text":"Then part 3 says the contents of the next element in the array."},{"Start":"06:37.955 ","End":"06:41.240","Text":"That would be out i plus 1."},{"Start":"06:41.240 ","End":"06:45.725","Text":"That bit done."},{"Start":"06:45.725 ","End":"06:50.315","Text":"Then a newline character is the final thing that we need."},{"Start":"06:50.315 ","End":"06:55.295","Text":"For this output onto a new line, presumably, that\u0027s it."},{"Start":"06:55.295 ","End":"06:58.530","Text":"If we compile that, if we got any errors didn\u0027t seem that we have,"},{"Start":"06:58.530 ","End":"07:00.710","Text":"we can see what the output looks like."},{"Start":"07:00.710 ","End":"07:04.285","Text":"What we\u0027re looking for here is to match to this."},{"Start":"07:04.285 ","End":"07:07.856","Text":"Anywhere wherein the inputs in string it sees LDA,"},{"Start":"07:07.856 ","End":"07:09.095","Text":"it will find a match,"},{"Start":"07:09.095 ","End":"07:11.045","Text":"it will then output it."},{"Start":"07:11.045 ","End":"07:14.105","Text":"Then the next element in the array,"},{"Start":"07:14.105 ","End":"07:15.920","Text":"which will be the next token in the eraser."},{"Start":"07:15.920 ","End":"07:18.350","Text":"The only thing we won\u0027t see is spaces because"},{"Start":"07:18.350 ","End":"07:21.125","Text":"the output array has been stripped of all its spaces"},{"Start":"07:21.125 ","End":"07:23.720","Text":"because we\u0027re splitting on them and then that\u0027s what we call"},{"Start":"07:23.720 ","End":"07:27.215","Text":"delimiters and we don\u0027t include the delimiter in this array,"},{"Start":"07:27.215 ","End":"07:31.520","Text":"is just used to mark the beginning and end of 2 items."},{"Start":"07:31.520 ","End":"07:36.700","Text":"We should see LDA 101 it\u0027s not matching to STA,"},{"Start":"07:36.700 ","End":"07:37.985","Text":"so we\u0027ll ignore this."},{"Start":"07:37.985 ","End":"07:40.280","Text":"This is not matching to NOP so ignore that."},{"Start":"07:40.280 ","End":"07:41.975","Text":"It is matching here,"},{"Start":"07:41.975 ","End":"07:44.306","Text":"LDA and it should bring 102,"},{"Start":"07:44.306 ","End":"07:48.410","Text":"you\u0027ll ignore the NOP and ignore the HLT instruction as well."},{"Start":"07:48.410 ","End":"07:52.945","Text":"Let\u0027s see if that does actually do what we expect it to do."},{"Start":"07:52.945 ","End":"07:58.815","Text":"Yes, sure enough, we get LDA 101 and then a new line, LDA 102."},{"Start":"07:58.815 ","End":"08:01.725","Text":"Skip to over all these other instructions."},{"Start":"08:01.725 ","End":"08:06.725","Text":"It\u0027s extracted the LDA instructions plus the value that follows."},{"Start":"08:06.725 ","End":"08:09.065","Text":"An assembler might do something like this."},{"Start":"08:09.065 ","End":"08:14.075","Text":"It\u0027s taking out what we call the opcode and the operand for the opcode."},{"Start":"08:14.075 ","End":"08:17.630","Text":"It\u0027s only doing it for LDA in this case."},{"Start":"08:17.630 ","End":"08:19.945","Text":"These other commands like NOP,"},{"Start":"08:19.945 ","End":"08:21.595","Text":"they don\u0027t have an operand."},{"Start":"08:21.595 ","End":"08:23.350","Text":"There\u0027s nothing to get out for those."},{"Start":"08:23.350 ","End":"08:28.930","Text":"We\u0027d need a slightly different set of code if we matched it to a NOP or a HLT."},{"Start":"08:28.930 ","End":"08:31.540","Text":"We can have a big switch statement here doing that stuff."},{"Start":"08:31.540 ","End":"08:34.180","Text":"But you\u0027re not being asked to do that in this question."},{"Start":"08:34.180 ","End":"08:39.540","Text":"But we will test it as it\u0027s asked us to in part e with STA."},{"Start":"08:39.540 ","End":"08:42.250","Text":"This time we should skip everything except for"},{"Start":"08:42.250 ","End":"08:45.430","Text":"STA and we should see a 100 printed after it."},{"Start":"08:45.430 ","End":"08:47.230","Text":"If we\u0027ve done it right."},{"Start":"08:47.230 ","End":"08:51.300","Text":"Let\u0027s see if we have, it seems we have."},{"Start":"08:51.300 ","End":"08:54.340","Text":"There we go. Another example of pausing."},{"Start":"08:54.340 ","End":"08:56.290","Text":"We did the similar thing with the robot."},{"Start":"08:56.290 ","End":"09:00.095","Text":"This time it wasn\u0027t a single command, it was substrings."},{"Start":"09:00.095 ","End":"09:07.925","Text":"Not only that, we were having pairs of substrings for the cases of load and store."},{"Start":"09:07.925 ","End":"09:13.639","Text":"You can imagine this being the very simple basis of an assembler."},{"Start":"09:13.639 ","End":"09:15.335","Text":"That\u0027s it for this exercise."},{"Start":"09:15.335 ","End":"09:18.240","Text":"Thank you, and I\u0027ll see you in the next one."}],"ID":28361},{"Watched":false,"Name":"Exercise 5","Duration":"11m 48s","ChapterTopicVideoID":27238,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.880","Text":"Welcome back everyone. In this exercise,"},{"Start":"00:02.880 ","End":"00:06.330","Text":"we\u0027ve been asked to create a method called isGraphicFileType."},{"Start":"00:06.330 ","End":"00:09.674","Text":"In Part A, we\u0027ve been asked to declare a number of variables."},{"Start":"00:09.674 ","End":"00:13.155","Text":"A1, we\u0027re asked to declare a string array called"},{"Start":"00:13.155 ","End":"00:17.760","Text":"files containing the following values; MyThesis.doc,"},{"Start":"00:17.760 ","End":"00:21.720","Text":"profilepic.jpg, budget.xls,"},{"Start":"00:21.720 ","End":"00:25.320","Text":"favjpg.pdf, funny.gif,"},{"Start":"00:25.320 ","End":"00:29.775","Text":"temp, logo.png, and photo.jpeg."},{"Start":"00:29.775 ","End":"00:33.555","Text":"In Part 2, we declare a string variable called fileType,"},{"Start":"00:33.555 ","End":"00:36.925","Text":"and in Part 3, an integer variable called position."},{"Start":"00:36.925 ","End":"00:38.840","Text":"In Part B, we\u0027re asked to iterate through"},{"Start":"00:38.840 ","End":"00:41.180","Text":"the elements of the array outputting in the value"},{"Start":"00:41.180 ","End":"00:45.605","Text":"of each element without adding a new line character at the end."},{"Start":"00:45.605 ","End":"00:50.240","Text":"In Part C, we\u0027re asked inside the loop below the output line in Part B,"},{"Start":"00:50.240 ","End":"00:54.005","Text":"to use a suitable string method to find whether the string contains"},{"Start":"00:54.005 ","End":"00:58.910","Text":"a dot and to store the position where it\u0027s found into position."},{"Start":"00:58.910 ","End":"01:00.470","Text":"In Part C2,"},{"Start":"01:00.470 ","End":"01:06.500","Text":"we\u0027re asked that if the dot was found to extract a substring into filetype,"},{"Start":"01:06.500 ","End":"01:10.040","Text":"so the filetype contains the characters after the dot."},{"Start":"01:10.040 ","End":"01:11.360","Text":"In Part C3,"},{"Start":"01:11.360 ","End":"01:15.185","Text":"we\u0027re asked to compare the substring with a file extensions jpg,"},{"Start":"01:15.185 ","End":"01:19.100","Text":"jpeg with an e, gif and png."},{"Start":"01:19.100 ","End":"01:23.900","Text":"If there\u0027s a match, we output a tab character followed by"},{"Start":"01:23.900 ","End":"01:29.620","Text":"an asterix and is a graphic file type and another asterix without a new line."},{"Start":"01:29.620 ","End":"01:30.820","Text":"In Part C4,"},{"Start":"01:30.820 ","End":"01:34.010","Text":"we\u0027re asked to then output a new line character inside"},{"Start":"01:34.010 ","End":"01:38.305","Text":"the loop but outside the selection structure in Parts 2-3."},{"Start":"01:38.305 ","End":"01:41.420","Text":"In Part D, we\u0027re asked to test the method outputs,"},{"Start":"01:41.420 ","End":"01:44.390","Text":"all the filenames with an indication that the file is"},{"Start":"01:44.390 ","End":"01:47.675","Text":"a graphic file type for only the relevant files."},{"Start":"01:47.675 ","End":"01:51.440","Text":"In Part E, we are then asked to change the file type for"},{"Start":"01:51.440 ","End":"01:57.775","Text":"logo.png to logo.pdf and to confirm the difference in the output."},{"Start":"01:57.775 ","End":"02:03.660","Text":"So let\u0027s go ahead and create our method called, isGraphicFileType,"},{"Start":"02:03.660 ","End":"02:11.160","Text":"and we\u0027ve been asked first of all in Part A1 to declare a string array called files."},{"Start":"02:11.160 ","End":"02:16.655","Text":"We\u0027ve been given a list of initial values so we need a pair of braces for that."},{"Start":"02:16.655 ","End":"02:18.760","Text":"Let\u0027s start with,"},{"Start":"02:18.760 ","End":"02:23.690","Text":"the first one is, MyThesis.doc,"},{"Start":"02:23.690 ","End":"02:31.625","Text":"and profilepic.jpg, budget.xls,"},{"Start":"02:31.625 ","End":"02:37.100","Text":"favjpg.pdf, funny.gif,"},{"Start":"02:37.100 ","End":"02:41.560","Text":"temp, logo.png,"},{"Start":"02:41.560 ","End":"02:49.789","Text":"and photo.jpeg with an E. So those are all common graphics filetypes,"},{"Start":"02:49.789 ","End":"02:52.685","Text":"and I\u0027ll just put it out to another line actually,"},{"Start":"02:52.685 ","End":"02:57.950","Text":"and that will be our list of files that we traverse through"},{"Start":"02:57.950 ","End":"03:03.755","Text":"trying to extract something from the contents in a later part of the question."},{"Start":"03:03.755 ","End":"03:06.365","Text":"So then we are just going to create our other 2 variables."},{"Start":"03:06.365 ","End":"03:09.230","Text":"One is a string variable called fileType,"},{"Start":"03:09.230 ","End":"03:14.360","Text":"no initial value specified and we put a T in there,"},{"Start":"03:14.360 ","End":"03:16.444","Text":"and then another variable,"},{"Start":"03:16.444 ","End":"03:21.430","Text":"an integer called position with no initial value."},{"Start":"03:21.430 ","End":"03:26.440","Text":"That\u0027s Part A done and then Part B"},{"Start":"03:26.440 ","End":"03:31.805","Text":"is asking us to iterate through each element in the array."},{"Start":"03:31.805 ","End":"03:34.705","Text":"So let\u0027s create an index variable I."},{"Start":"03:34.705 ","End":"03:37.190","Text":"We start from the beginning of the array."},{"Start":"03:37.190 ","End":"03:40.010","Text":"The array is called files and it has"},{"Start":"03:40.010 ","End":"03:46.985","Text":"a length so we need to make sure the I is less than the length of the array,"},{"Start":"03:46.985 ","End":"03:49.490","Text":"not less than or equal to, but less than,"},{"Start":"03:49.490 ","End":"03:54.170","Text":"and we increment by one each time around the loop."},{"Start":"03:54.170 ","End":"03:57.620","Text":"There\u0027s our for loop and we\u0027ve been asked to"},{"Start":"03:57.620 ","End":"04:01.940","Text":"output the value of each element without a new line character."},{"Start":"04:01.940 ","End":"04:05.630","Text":"So we know that system out print rather than print line,"},{"Start":"04:05.630 ","End":"04:12.920","Text":"and the actual element we\u0027ll be looking at is going to be files[i]."},{"Start":"04:12.920 ","End":"04:15.215","Text":"So that\u0027s that part done."},{"Start":"04:15.215 ","End":"04:17.420","Text":"Now below this line,"},{"Start":"04:17.420 ","End":"04:19.460","Text":"we\u0027re on Part C1 now,"},{"Start":"04:19.460 ","End":"04:23.930","Text":"we are going to use a suitable string method to find whether the string"},{"Start":"04:23.930 ","End":"04:29.230","Text":"contains a full stop or a dot, whatever you want to call it."},{"Start":"04:29.230 ","End":"04:32.420","Text":"There is a method in Java that will do that,"},{"Start":"04:32.420 ","End":"04:36.110","Text":"which is the name of the string in this case,"},{"Start":"04:36.110 ","End":"04:41.840","Text":"it\u0027s files and the element we\u0027re looking at.indexof,"},{"Start":"04:41.840 ","End":"04:44.600","Text":"will find whatever it is you\u0027re looking for."},{"Start":"04:44.600 ","End":"04:48.830","Text":"So we\u0027re looking for a full stop in this case and it will"},{"Start":"04:48.830 ","End":"04:54.765","Text":"return an integer which says where it found that dot,"},{"Start":"04:54.765 ","End":"04:56.390","Text":"and we can store that somewhere."},{"Start":"04:56.390 ","End":"04:58.880","Text":"We\u0027ve been given a variable call position,"},{"Start":"04:58.880 ","End":"05:00.800","Text":"presumably that\u0027s what it\u0027s for."},{"Start":"05:00.800 ","End":"05:07.655","Text":"We can store where we found the full stop in the current string."},{"Start":"05:07.655 ","End":"05:10.250","Text":"Was it this one or this one or this one?"},{"Start":"05:10.250 ","End":"05:14.300","Text":"Where do we find the full stop in that string?"},{"Start":"05:14.300 ","End":"05:15.770","Text":"Once we\u0027ve got that,"},{"Start":"05:15.770 ","End":"05:18.680","Text":"we can use it to extract a substring."},{"Start":"05:18.680 ","End":"05:23.390","Text":"There is a statement here that says if it does,"},{"Start":"05:23.390 ","End":"05:27.409","Text":"in Part C2, then extract a substring into filetype."},{"Start":"05:27.409 ","End":"05:30.005","Text":"So filetype contains the characters after the dot."},{"Start":"05:30.005 ","End":"05:36.310","Text":"The if comes from the fact that we could have tried to get the index of the full stop,"},{"Start":"05:36.310 ","End":"05:38.240","Text":"but what if there is no full stop in"},{"Start":"05:38.240 ","End":"05:40.310","Text":"one of these file names and you\u0027ll find that there is one"},{"Start":"05:40.310 ","End":"05:44.795","Text":"here that would not return a position of a full stop because there isn\u0027t one,"},{"Start":"05:44.795 ","End":"05:46.700","Text":"it will return minus 1."},{"Start":"05:46.700 ","End":"05:49.774","Text":"So what we have to do is we have to check that position"},{"Start":"05:49.774 ","End":"05:53.720","Text":"is greater than 0 in order to proceed."},{"Start":"05:53.720 ","End":"05:55.925","Text":"Otherwise, we\u0027re not interested in proceeding"},{"Start":"05:55.925 ","End":"05:59.075","Text":"because that means we didn\u0027t find a full stop."},{"Start":"05:59.075 ","End":"06:01.550","Text":"So if position is greater than 0,"},{"Start":"06:01.550 ","End":"06:07.385","Text":"then we can extract the substring and we extract that into filetype,"},{"Start":"06:07.385 ","End":"06:09.350","Text":"the variable that we declared earlier,"},{"Start":"06:09.350 ","End":"06:12.140","Text":"and we say filetype equals"},{"Start":"06:12.140 ","End":"06:18.320","Text":"files[i].substring and then we"},{"Start":"06:18.320 ","End":"06:22.830","Text":"simply need to provide a position of the full stop,"},{"Start":"06:22.830 ","End":"06:26.450","Text":"and it will go from that position to the end of the string."},{"Start":"06:26.450 ","End":"06:28.175","Text":"There\u0027s 2 versions of substring."},{"Start":"06:28.175 ","End":"06:32.915","Text":"You can say position and then the length of the string following by a comma."},{"Start":"06:32.915 ","End":"06:35.730","Text":"If you want to have it in the middle of the string somewhere."},{"Start":"06:35.730 ","End":"06:38.000","Text":"But if you want it to go all the way up to the end of the string,"},{"Start":"06:38.000 ","End":"06:41.480","Text":"all you need to supply is the stop position"},{"Start":"06:41.480 ","End":"06:45.260","Text":"and it will automatically extract to the end of the string."},{"Start":"06:45.260 ","End":"06:47.315","Text":"That will do the job however,"},{"Start":"06:47.315 ","End":"06:51.665","Text":"we will probably come back to this line in a moment as we might need to tweak it."},{"Start":"06:51.665 ","End":"06:56.145","Text":"That\u0027s Part C2 done."},{"Start":"06:56.145 ","End":"06:58.430","Text":"Now what we\u0027re going to do is we\u0027re going to compare"},{"Start":"06:58.430 ","End":"07:02.900","Text":"the substring with the file extensions and jpg,"},{"Start":"07:02.900 ","End":"07:05.440","Text":"J-P-E-G so JPEG again,"},{"Start":"07:05.440 ","End":"07:07.440","Text":"GIF and PNG,"},{"Start":"07:07.440 ","End":"07:10.355","Text":"and if we match them we display a message."},{"Start":"07:10.355 ","End":"07:12.725","Text":"We\u0027re going to need an if statement,"},{"Start":"07:12.725 ","End":"07:14.660","Text":"but we\u0027re going to have multiple matches"},{"Start":"07:14.660 ","End":"07:17.810","Text":"here and your temptation might be to use the equals sign."},{"Start":"07:17.810 ","End":"07:21.365","Text":"Remember strings don\u0027t use the equal signs in Java."},{"Start":"07:21.365 ","End":"07:23.720","Text":"So what we\u0027ve got to say is if"},{"Start":"07:23.720 ","End":"07:33.260","Text":"fileType.equals and then I can put a literal in here, for example, jpeg."},{"Start":"07:33.260 ","End":"07:36.200","Text":"If that is true,"},{"Start":"07:36.200 ","End":"07:39.840","Text":"and I\u0027ll copy this to save me some typing,"},{"Start":"07:39.840 ","End":"07:42.720","Text":"or, so we need logical or,"},{"Start":"07:42.720 ","End":"07:48.645","Text":"this is true, jpg without an e or it\u0027s a png,"},{"Start":"07:48.645 ","End":"07:52.675","Text":"or it\u0027s a gif."},{"Start":"07:52.675 ","End":"07:58.445","Text":"Any of those situations would result in a Boolean condition of true."},{"Start":"07:58.445 ","End":"07:59.870","Text":"I\u0027m only going to have one line here,"},{"Start":"07:59.870 ","End":"08:03.470","Text":"so I don\u0027t need a pair of braces."},{"Start":"08:03.470 ","End":"08:07.400","Text":"Just put it on the same line or I broken out to another line,"},{"Start":"08:07.400 ","End":"08:12.005","Text":"it will look for a semicolon and I want to use print not print line,"},{"Start":"08:12.005 ","End":"08:13.895","Text":"because it has said here,"},{"Start":"08:13.895 ","End":"08:15.601","Text":"without a new line."},{"Start":"08:15.601 ","End":"08:20.705","Text":"The message I want to print is a tab character and then an asterix,"},{"Start":"08:20.705 ","End":"08:27.800","Text":"and then is a graphic file type and close it off with an asterix."},{"Start":"08:27.800 ","End":"08:30.440","Text":"That will do the job."},{"Start":"08:30.440 ","End":"08:35.390","Text":"Now the problem I have here is I\u0027ve tried to match on just JPEG,"},{"Start":"08:35.390 ","End":"08:37.280","Text":"just JPG, just PNG,"},{"Start":"08:37.280 ","End":"08:42.320","Text":"and so on but actually the substring will have a full stop in it."},{"Start":"08:42.320 ","End":"08:44.870","Text":"So what I should really do is put a full stop there."},{"Start":"08:44.870 ","End":"08:47.810","Text":"I\u0027m not going to do that, instead I\u0027m going to adjust it up here."},{"Start":"08:47.810 ","End":"08:54.545","Text":"I\u0027m going to say position plus 1 and then what will happen is the substring will extract"},{"Start":"08:54.545 ","End":"08:58.100","Text":"the file extension but without the full stop because it\u0027ll"},{"Start":"08:58.100 ","End":"09:01.700","Text":"skip past it and it will extract the remaining characters."},{"Start":"09:01.700 ","End":"09:05.139","Text":"It\u0027s just a little trick there to save me putting.jpg"},{"Start":"09:05.139 ","End":"09:10.670","Text":"or.jpeg and so on in front of each line. I\u0027m nearly done."},{"Start":"09:10.670 ","End":"09:17.480","Text":"I simply need to now do Part 4 of C which is still inside the loop,"},{"Start":"09:17.480 ","End":"09:20.875","Text":"but outside the selection structure that we\u0027ve got there,"},{"Start":"09:20.875 ","End":"09:22.880","Text":"output a new line character."},{"Start":"09:22.880 ","End":"09:25.790","Text":"Just that one it\u0027s saying will output a new line character."},{"Start":"09:25.790 ","End":"09:32.985","Text":"I want it there because this is going to appear for some of the files that are printed."},{"Start":"09:32.985 ","End":"09:35.640","Text":"The file names are printed up here, remember."},{"Start":"09:35.640 ","End":"09:37.860","Text":"But it\u0027s not going to do it for all of them."},{"Start":"09:37.860 ","End":"09:39.330","Text":"Let\u0027s see what happens here."},{"Start":"09:39.330 ","End":"09:43.090","Text":"Looks like a build an error. Let\u0027s see where I\u0027m missing something."},{"Start":"09:43.090 ","End":"09:46.560","Text":"I\u0027m missing an or here I think."},{"Start":"09:46.560 ","End":"09:48.810","Text":"Let\u0027s see if that works."},{"Start":"09:48.810 ","End":"09:50.765","Text":"That seems to have done the job."},{"Start":"09:50.765 ","End":"09:55.805","Text":"So let\u0027s see if our program works as expected."},{"Start":"09:55.805 ","End":"09:58.910","Text":"The filenames are printed and we\u0027ve got a little bit"},{"Start":"09:58.910 ","End":"10:01.565","Text":"of a gap and then that is the tab character,"},{"Start":"10:01.565 ","End":"10:03.860","Text":"and then we\u0027ve got that message and it does"},{"Start":"10:03.860 ","End":"10:06.710","Text":"appear only for the ones that are graphics files."},{"Start":"10:06.710 ","End":"10:09.110","Text":"So it picks up for the JPG and the GIF,"},{"Start":"10:09.110 ","End":"10:13.850","Text":"and the PNG and the other JPEG and it doesn\u0027t for the others."},{"Start":"10:13.850 ","End":"10:18.635","Text":"Notice this is trick filename here which has JPEG inside the file name."},{"Start":"10:18.635 ","End":"10:21.980","Text":"It shouldn\u0027t pick up on that because it\u0027s looking for a dot in"},{"Start":"10:21.980 ","End":"10:25.955","Text":"front of it and it also didn\u0027t complain when we had"},{"Start":"10:25.955 ","End":"10:30.365","Text":"a file which doesn\u0027t even have a full stop in it or file"},{"Start":"10:30.365 ","End":"10:34.670","Text":"extension and that is because we did correctly looked to"},{"Start":"10:34.670 ","End":"10:38.780","Text":"see if there was one at all in this line here and we didn\u0027t do"},{"Start":"10:38.780 ","End":"10:43.650","Text":"anything about extracting unless we knew that it actually found a full stop."},{"Start":"10:43.650 ","End":"10:44.930","Text":"So that\u0027s it."},{"Start":"10:44.930 ","End":"10:47.285","Text":"Apart from just test again with"},{"Start":"10:47.285 ","End":"10:52.900","Text":"another change which was just to change logo, I think PNG."},{"Start":"10:52.900 ","End":"10:56.600","Text":"I think we said to logo.pdf and see if that disappeared."},{"Start":"10:56.600 ","End":"11:00.700","Text":"We had 4 matches previously so we should only have"},{"Start":"11:00.700 ","End":"11:06.825","Text":"3 matches now if we retest with that small change."},{"Start":"11:06.825 ","End":"11:09.045","Text":"So let\u0027s see what happens."},{"Start":"11:09.045 ","End":"11:14.040","Text":"Sure enough, we now only get 3 matches and logo.pdf,"},{"Start":"11:14.040 ","End":"11:19.250","Text":"which was logo.png is no longer displaying as a graphic filetype."},{"Start":"11:19.250 ","End":"11:21.410","Text":"So fairly straightforward one,"},{"Start":"11:21.410 ","End":"11:25.610","Text":"so long as you understand how substring works and we used one of the versions"},{"Start":"11:25.610 ","End":"11:30.170","Text":"of substring that doesn\u0027t require 2 parameters here and it needs 1."},{"Start":"11:30.170 ","End":"11:32.000","Text":"Just need to know what the position is,"},{"Start":"11:32.000 ","End":"11:33.770","Text":"where you want to start extracting"},{"Start":"11:33.770 ","End":"11:35.930","Text":"from and it will go all the way up to the end of the string."},{"Start":"11:35.930 ","End":"11:38.930","Text":"There is another version as I said that will say,"},{"Start":"11:38.930 ","End":"11:40.595","Text":"where do you want it to end?"},{"Start":"11:40.595 ","End":"11:43.625","Text":"If you don\u0027t supply a value saying where you want it to end,"},{"Start":"11:43.625 ","End":"11:45.440","Text":"it\u0027ll just go to the end of the string."},{"Start":"11:45.440 ","End":"11:48.780","Text":"Thanks for watching, and I\u0027ll see you in the next one."}],"ID":28362},{"Watched":false,"Name":"Exercise 6","Duration":"9m 35s","ChapterTopicVideoID":27239,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.110","Text":"Hello again. In this exercise,"},{"Start":"00:02.110 ","End":"00:05.170","Text":"you\u0027ve been asked to create a method called isPalindrome."},{"Start":"00:05.170 ","End":"00:06.940","Text":"In part a, we\u0027re asked to declare"},{"Start":"00:06.940 ","End":"00:10.405","Text":"a string variable called inString with the initial value,"},{"Start":"00:10.405 ","End":"00:14.135","Text":"did Hannah see bees? Hannah did."},{"Start":"00:14.135 ","End":"00:16.470","Text":"In part b, we\u0027re asked to create"},{"Start":"00:16.470 ","End":"00:20.645","Text":"2 more string variables called rawString and reverseRawString,"},{"Start":"00:20.645 ","End":"00:23.335","Text":"both with initial values of blank."},{"Start":"00:23.335 ","End":"00:26.185","Text":"In part c, we\u0027re asked to copy the contents of"},{"Start":"00:26.185 ","End":"00:30.220","Text":"inString to rawString using regular expressions and"},{"Start":"00:30.220 ","End":"00:34.640","Text":"Java string functions so that any uppercase letters in"},{"Start":"00:34.640 ","End":"00:39.930","Text":"rawString are forced to lowercase and only letters are in rawString,"},{"Start":"00:39.930 ","End":"00:42.480","Text":"and all other characters are ignored."},{"Start":"00:42.480 ","End":"00:44.340","Text":"Using a loop in part d,"},{"Start":"00:44.340 ","End":"00:49.635","Text":"we\u0027re asked to output the rawString characters into reverseRawString in reverse order,"},{"Start":"00:49.635 ","End":"00:54.720","Text":"ie, the first character in reverseRawString will be the last character in rawString."},{"Start":"00:54.720 ","End":"00:59.170","Text":"In part e, we\u0027re asked to compare rawString to reverseRawString."},{"Start":"00:59.170 ","End":"01:00.710","Text":"If they are equal,"},{"Start":"01:00.710 ","End":"01:05.165","Text":"we output inString followed by the message, is a palindrome."},{"Start":"01:05.165 ","End":"01:06.770","Text":"If they\u0027re not equal,"},{"Start":"01:06.770 ","End":"01:08.240","Text":"we output inString,"},{"Start":"01:08.240 ","End":"01:09.620","Text":"followed by the message,"},{"Start":"01:09.620 ","End":"01:11.509","Text":"is not a palindrome."},{"Start":"01:11.509 ","End":"01:14.315","Text":"In part f, we\u0027re asked to test if the program works."},{"Start":"01:14.315 ","End":"01:17.345","Text":"It should find that the input string is a palindrome."},{"Start":"01:17.345 ","End":"01:21.875","Text":"We remove 1 letter from the initial value to see if it now finds,"},{"Start":"01:21.875 ","End":"01:24.640","Text":"it\u0027s not a palindrome."},{"Start":"01:24.640 ","End":"01:29.495","Text":"Let\u0027s create a method called isPalindrome."},{"Start":"01:29.495 ","End":"01:34.700","Text":"First thing to do is to declare a string variable called inString,"},{"Start":"01:34.700 ","End":"01:40.175","Text":"containing this initial value with a mixture of"},{"Start":"01:40.175 ","End":"01:46.145","Text":"upper and lowercase letters and some punctuation characters,"},{"Start":"01:46.145 ","End":"01:49.810","Text":"which are an important part of the problem."},{"Start":"01:49.810 ","End":"01:51.810","Text":"That\u0027s part a done."},{"Start":"01:51.810 ","End":"01:56.000","Text":"Part b, create 2 more string variables called rawString and reverseString,"},{"Start":"01:56.000 ","End":"01:59.195","Text":"both with initial values of an empty string."},{"Start":"01:59.195 ","End":"02:03.425","Text":"Let\u0027s do that, rawString."},{"Start":"02:03.425 ","End":"02:06.770","Text":"Incidentally, you don\u0027t always have to do this,"},{"Start":"02:06.770 ","End":"02:10.700","Text":"but sometimes the compiler can complain if"},{"Start":"02:10.700 ","End":"02:15.020","Text":"you\u0027re trying to add onto a string and it thinks it doesn\u0027t have an initial value,"},{"Start":"02:15.020 ","End":"02:17.915","Text":"even though they generally do get default values."},{"Start":"02:17.915 ","End":"02:20.525","Text":"But this will make an error go away."},{"Start":"02:20.525 ","End":"02:22.945","Text":"That\u0027s part b done."},{"Start":"02:22.945 ","End":"02:29.060","Text":"Now part c, we\u0027ve got to use regular expressions in Java string functions to,"},{"Start":"02:29.060 ","End":"02:35.450","Text":"first of all, take any uppercase letters in rawString and force them into lowercase."},{"Start":"02:35.450 ","End":"02:38.885","Text":"We\u0027re going to take the input string,"},{"Start":"02:38.885 ","End":"02:42.080","Text":"first of all, and put it into rawString."},{"Start":"02:42.080 ","End":"02:46.715","Text":"But we\u0027re going to force it to lowercase, first of all."},{"Start":"02:46.715 ","End":"02:51.425","Text":"So there\u0027s a function in Java called toLowerCase,"},{"Start":"02:51.425 ","End":"02:54.530","Text":"and that\u0027s all we need to do and we\u0027ll do that job."},{"Start":"02:54.530 ","End":"02:56.480","Text":"I\u0027ve got my string, inputString,"},{"Start":"02:56.480 ","End":"02:58.580","Text":"here I put a inString above."},{"Start":"02:58.580 ","End":"03:03.325","Text":"That\u0027s first part done, that\u0027s c1."},{"Start":"03:03.325 ","End":"03:05.555","Text":"Now the second part is the complex bit."},{"Start":"03:05.555 ","End":"03:08.360","Text":"All the letters in rawString should be"},{"Start":"03:08.360 ","End":"03:11.497","Text":"included and they\u0027re all going to be a lowercase letters now,"},{"Start":"03:11.497 ","End":"03:15.330","Text":"but any other characters should be ignored."},{"Start":"03:15.330 ","End":"03:19.275","Text":"The way we do that is we use a friend,"},{"Start":"03:19.275 ","End":"03:22.510","Text":"replaceAll, and we\u0027re going to do it on the rawString,"},{"Start":"03:22.510 ","End":"03:25.505","Text":"so we can take what\u0027s already in rawString."},{"Start":"03:25.505 ","End":"03:29.840","Text":"We\u0027re going to store it back in once we\u0027ve done this replaceAll operation."},{"Start":"03:29.840 ","End":"03:33.920","Text":"That\u0027s why we do rawString equals rawString.replaceAll."},{"Start":"03:33.920 ","End":"03:38.580","Text":"Now we need to have our regular expression in quotes."},{"Start":"03:38.580 ","End":"03:43.735","Text":"The regular expression we\u0027re going to use is not a-z."},{"Start":"03:43.735 ","End":"03:49.470","Text":"Anything that is not a-z will match this regular expression."},{"Start":"03:49.470 ","End":"03:51.560","Text":"Then wherever we get a match,"},{"Start":"03:51.560 ","End":"03:54.765","Text":"we\u0027re going to replace it with nothing."},{"Start":"03:54.765 ","End":"03:59.450","Text":"What that will do is that will get rid of anything that\u0027s"},{"Start":"03:59.450 ","End":"04:05.195","Text":"not a lowercase letter between a and z because it\u0027s going to replace it with nothing."},{"Start":"04:05.195 ","End":"04:08.030","Text":"Then that will all be stored back into rawString."},{"Start":"04:08.030 ","End":"04:09.530","Text":"Nice and simple,"},{"Start":"04:09.530 ","End":"04:13.150","Text":"and that\u0027s really down to the power of regular expressions here."},{"Start":"04:13.150 ","End":"04:18.485","Text":"We saw an almost identical example in the tutorial video."},{"Start":"04:18.485 ","End":"04:20.750","Text":"Now we have done part c2,"},{"Start":"04:20.750 ","End":"04:25.490","Text":"we use a loop to output the rawString characters into reverseRawString."},{"Start":"04:25.490 ","End":"04:28.170","Text":"We do that in reverse order, ie,"},{"Start":"04:28.170 ","End":"04:32.335","Text":"the first character in reverseRawString will be the last character in rawString."},{"Start":"04:32.335 ","End":"04:36.925","Text":"We\u0027ve done something similar to this in a previous exercise."},{"Start":"04:36.925 ","End":"04:41.795","Text":"What we had to do is we have to start at the back of the string."},{"Start":"04:41.795 ","End":"04:44.465","Text":"Int I is going to start at the back,"},{"Start":"04:44.465 ","End":"04:49.715","Text":"and it\u0027s going to be rawString.length."},{"Start":"04:49.715 ","End":"04:51.320","Text":"Remembering this is a method,"},{"Start":"04:51.320 ","End":"04:52.760","Text":"so we put brackets."},{"Start":"04:52.760 ","End":"04:55.690","Text":"That\u0027s not an array and we minus 1,"},{"Start":"04:55.690 ","End":"05:02.760","Text":"otherwise we\u0027ll get an index error because we go from 0 to the length minus 1,"},{"Start":"05:02.760 ","End":"05:04.380","Text":"because we\u0027re counting from 0,"},{"Start":"05:04.380 ","End":"05:10.835","Text":"and then we want to keep going while I is greater than or equal to 0,"},{"Start":"05:10.835 ","End":"05:18.810","Text":"and then we go down 1 on I working backwards through the string to 0."},{"Start":"05:18.810 ","End":"05:20.825","Text":"There is our loop,"},{"Start":"05:20.825 ","End":"05:28.165","Text":"and we simply now need to output the characters into reverseRawString."},{"Start":"05:28.165 ","End":"05:33.140","Text":"I\u0027m just saying reverseRawString equals"},{"Start":"05:33.140 ","End":"05:40.450","Text":"reverseRawString plus the character we\u0027re currently looking at."},{"Start":"05:40.450 ","End":"05:45.810","Text":"We\u0027re concatenating this character onto reverseString, which is a string."},{"Start":"05:45.810 ","End":"05:47.610","Text":"Then we\u0027re dealing with 2 different data types,"},{"Start":"05:47.610 ","End":"05:50.280","Text":"but the concatenation operator is quite happily"},{"Start":"05:50.280 ","End":"05:54.030","Text":"dealing with a char being added to a string."},{"Start":"05:54.030 ","End":"05:57.675","Text":"That should do the job."},{"Start":"05:57.675 ","End":"05:59.340","Text":"By the end of this loop,"},{"Start":"05:59.340 ","End":"06:05.500","Text":"we will have what was in rawString in reverseRawString."},{"Start":"06:06.290 ","End":"06:11.130","Text":"All we have to do now is part e,"},{"Start":"06:11.130 ","End":"06:16.545","Text":"we\u0027re going to compare rawString to reverseRawString,"},{"Start":"06:16.545 ","End":"06:18.350","Text":"and if they\u0027re equal we output 1 message,"},{"Start":"06:18.350 ","End":"06:20.630","Text":"if they\u0027re not, we output different message."},{"Start":"06:20.630 ","End":"06:23.975","Text":"Let\u0027s do the comparison and do an if statement."},{"Start":"06:23.975 ","End":"06:25.855","Text":"We say, if,"},{"Start":"06:25.855 ","End":"06:30.060","Text":"no matter which order we do this in reverseRawStrings,"},{"Start":"06:30.060 ","End":"06:35.610","Text":"say, dot equals rawString."},{"Start":"06:35.610 ","End":"06:38.080","Text":"If they are equal,"},{"Start":"06:38.080 ","End":"06:40.475","Text":"then we do 1 thing."},{"Start":"06:40.475 ","End":"06:41.865","Text":"Otherwise, we do something else."},{"Start":"06:41.865 ","End":"06:43.245","Text":"This is going to be a single line."},{"Start":"06:43.245 ","End":"06:46.860","Text":"I\u0027m not going to use any braces."},{"Start":"06:46.860 ","End":"06:51.120","Text":"I\u0027m going to say the name of the string."},{"Start":"06:51.120 ","End":"06:53.115","Text":"What was our inputString called?"},{"Start":"06:53.115 ","End":"06:57.240","Text":"InString plus a space,"},{"Start":"06:57.240 ","End":"07:01.510","Text":"and then the message, is a palindrome."},{"Start":"07:04.180 ","End":"07:09.350","Text":"Because it\u0027s the same backwards and forwards."},{"Start":"07:09.350 ","End":"07:12.050","Text":"Part of lies else."},{"Start":"07:12.050 ","End":"07:15.210","Text":"I\u0027m going to output the message."},{"Start":"07:15.790 ","End":"07:19.350","Text":"Whatever the input was,"},{"Start":"07:23.120 ","End":"07:27.880","Text":"was not a palindrome."},{"Start":"07:33.920 ","End":"07:39.660","Text":"Let\u0027s see what happens when we run that."},{"Start":"07:39.660 ","End":"07:44.640","Text":"Great. We create our object and then"},{"Start":"07:44.640 ","End":"07:49.245","Text":"run isPalindrome and we\u0027re expecting it to say is a palindrome,"},{"Start":"07:49.245 ","End":"07:50.950","Text":"and it is indeed."},{"Start":"07:50.950 ","End":"07:53.815","Text":"If we read this backwards."},{"Start":"07:53.815 ","End":"07:58.785","Text":"Did Hannah see bees?"},{"Start":"07:58.785 ","End":"08:02.310","Text":"It\u0027s the same way Hannah did."},{"Start":"08:02.310 ","End":"08:05.860","Text":"It\u0027s the same way backwards as forwards if you ignore"},{"Start":"08:05.860 ","End":"08:10.285","Text":"spaces and the question mark and the full stop."},{"Start":"08:10.285 ","End":"08:15.940","Text":"To prove that our code actually works as we intended it to should take 1 character out,"},{"Start":"08:15.940 ","End":"08:17.995","Text":"then it wouldn\u0027t be the same backwards and forwards."},{"Start":"08:17.995 ","End":"08:23.245","Text":"You can take any alphabetical character out and it should work."},{"Start":"08:23.245 ","End":"08:27.215","Text":"It should say this is no longer a palindrome."},{"Start":"08:27.215 ","End":"08:29.680","Text":"It does. It says it\u0027s not a palindrome."},{"Start":"08:29.680 ","End":"08:33.265","Text":"I can prove that by using another palindrome if I wanted to,"},{"Start":"08:33.265 ","End":"08:35.740","Text":"just to check it, but I can assure you it does work."},{"Start":"08:35.740 ","End":"08:38.530","Text":"What we\u0027ve done is very little code."},{"Start":"08:38.530 ","End":"08:40.810","Text":"We\u0027ve done something quite complicated here really."},{"Start":"08:40.810 ","End":"08:43.720","Text":"We\u0027ve taken an inputString,"},{"Start":"08:43.720 ","End":"08:48.365","Text":"stripped out any non-alphabetic characters,"},{"Start":"08:48.365 ","End":"08:51.330","Text":"and kept everything lowercase."},{"Start":"08:51.330 ","End":"08:54.155","Text":"Then what we did is we reverse this string,"},{"Start":"08:54.155 ","End":"08:55.819","Text":"which we called rawStrings,"},{"Start":"08:55.819 ","End":"08:59.480","Text":"so we stripped out any non-characters into rawString,"},{"Start":"08:59.480 ","End":"09:05.480","Text":"any non-alphabetic characters and any uppercase was turning to lowercase."},{"Start":"09:05.480 ","End":"09:10.175","Text":"Then we took each character 1 at a time"},{"Start":"09:10.175 ","End":"09:14.810","Text":"from the back of rawString and put it into reverseRawString,"},{"Start":"09:14.810 ","End":"09:18.170","Text":"and then we could work out whether the 2 things are equal."},{"Start":"09:18.170 ","End":"09:20.390","Text":"If they were, we had a palindrome."},{"Start":"09:20.390 ","End":"09:22.205","Text":"If not, we didn\u0027t."},{"Start":"09:22.205 ","End":"09:24.860","Text":"Relatively straightforward in the end,"},{"Start":"09:24.860 ","End":"09:26.720","Text":"but the key to it really was"},{"Start":"09:26.720 ","End":"09:32.495","Text":"this regular expression here and the replaceAll string function in Java."},{"Start":"09:32.495 ","End":"09:35.820","Text":"See you in the next video."}],"ID":28363},{"Watched":false,"Name":"Exercise 7","Duration":"13m 56s","ChapterTopicVideoID":27240,"CourseChapterTopicPlaylistID":269238,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.789","Text":"Hi everyone in this exercise,"},{"Start":"00:02.789 ","End":"00:05.190","Text":"we were asked to create a new method called findSongs,"},{"Start":"00:05.190 ","End":"00:06.450","Text":"and in part a,"},{"Start":"00:06.450 ","End":"00:12.010","Text":"to declare a string variable called source string with an initial value as follows."},{"Start":"00:12.010 ","End":"00:15.150","Text":"There\u0027s a long string here with a number of tags."},{"Start":"00:15.150 ","End":"00:19.125","Text":"Opening tag album, greatest opening tag song."},{"Start":"00:19.125 ","End":"00:20.865","Text":"Track ID equals 1."},{"Start":"00:20.865 ","End":"00:22.545","Text":"Text song for you."},{"Start":"00:22.545 ","End":"00:25.620","Text":"Close tag song, open new tag song,"},{"Start":"00:25.620 ","End":"00:29.205","Text":"truck ID equals 2, text sad song,"},{"Start":"00:29.205 ","End":"00:33.000","Text":"close tag song, open another tag song,"},{"Start":"00:33.000 ","End":"00:35.130","Text":"and text another song,"},{"Start":"00:35.130 ","End":"00:38.475","Text":"close tag song, close tag album."},{"Start":"00:38.475 ","End":"00:40.510","Text":"In part A 2,"},{"Start":"00:40.510 ","End":"00:45.230","Text":"we declare a string array called songs with 10 uninitialized elements."},{"Start":"00:45.230 ","End":"00:48.964","Text":"In part A 3, we declare integer variables called index,"},{"Start":"00:48.964 ","End":"00:52.535","Text":"start and end, all initialized to 0."},{"Start":"00:52.535 ","End":"00:58.415","Text":"In Part 4, we declare a Boolean variable called finished, initialized to false."},{"Start":"00:58.415 ","End":"01:02.990","Text":"In part B, we use a loop which terminates when finished is true,"},{"Start":"01:02.990 ","End":"01:05.540","Text":"and we pause source string to extract"},{"Start":"01:05.540 ","End":"01:10.250","Text":"the song name between the tag song and closing tag song."},{"Start":"01:10.250 ","End":"01:13.280","Text":"Note that some songs have extra texts between"},{"Start":"01:13.280 ","End":"01:17.655","Text":"song and the greater than symbol here, and here."},{"Start":"01:17.655 ","End":"01:21.170","Text":"In part B 1, we\u0027re asked to use start and end to"},{"Start":"01:21.170 ","End":"01:25.070","Text":"store the beginning and end of the substring to be extracted."},{"Start":"01:25.070 ","End":"01:29.270","Text":"In B 2, to extract each sub-string and store it"},{"Start":"01:29.270 ","End":"01:33.860","Text":"into the next element in the songs array using index."},{"Start":"01:33.860 ","End":"01:35.270","Text":"In part B 3,"},{"Start":"01:35.270 ","End":"01:37.910","Text":"we\u0027re told that when there are no more songs to be found,"},{"Start":"01:37.910 ","End":"01:40.475","Text":"we should set finished to true."},{"Start":"01:40.475 ","End":"01:43.235","Text":"In part C, outside the parse loop,"},{"Start":"01:43.235 ","End":"01:48.035","Text":"we\u0027re asked to use another loop to output the contents of the songs array,"},{"Start":"01:48.035 ","End":"01:51.710","Text":"with each element appearing on a new line in the output."},{"Start":"01:51.710 ","End":"01:57.710","Text":"In part d, we\u0027re asked to test that the method stores 3 strings into the songs array."},{"Start":"01:57.710 ","End":"02:02.990","Text":"In part A, we\u0027re asked using the format of song, name of song,"},{"Start":"02:02.990 ","End":"02:08.690","Text":"close song to add 2 more songs at the end of the initial value of source string,"},{"Start":"02:08.690 ","End":"02:10.910","Text":"and to test whether these additional songs"},{"Start":"02:10.910 ","End":"02:14.885","Text":"appear in the songs array and therefore the output."},{"Start":"02:14.885 ","End":"02:23.915","Text":"Let\u0027s begin by naming a method as we\u0027ve been asked to find songs,"},{"Start":"02:23.915 ","End":"02:30.620","Text":"and then we\u0027ve got a very long string variable to initialize."},{"Start":"02:30.620 ","End":"02:33.885","Text":"The string itself is called source string,"},{"Start":"02:33.885 ","End":"02:37.120","Text":"and I\u0027ll show you a little trick here to spread it over several lines."},{"Start":"02:37.120 ","End":"02:40.195","Text":"You can just treat 2 constants,"},{"Start":"02:40.195 ","End":"02:43.480","Text":"literal constants as any other string and join"},{"Start":"02:43.480 ","End":"02:46.825","Text":"them together with a plus symbol to concatenate them."},{"Start":"02:46.825 ","End":"02:50.035","Text":"So let\u0027s do the first half,"},{"Start":"02:50.035 ","End":"02:52.255","Text":"1 line and the next half on another line."},{"Start":"02:52.255 ","End":"02:54.250","Text":"This format of the string,"},{"Start":"02:54.250 ","End":"02:57.460","Text":"by the way, you may have seen before,"},{"Start":"02:57.460 ","End":"03:03.340","Text":"or recognize it\u0027s the same format as HTML uses."},{"Start":"03:03.340 ","End":"03:10.465","Text":"It\u0027s called XML. Programs like iTunes use XML to exchange data,"},{"Start":"03:10.465 ","End":"03:15.415","Text":"save data, make it in a format that can be exported."},{"Start":"03:15.415 ","End":"03:20.125","Text":"In this case, we\u0027re putting song names in-between an opening tag called song,"},{"Start":"03:20.125 ","End":"03:22.435","Text":"and a closing tag,"},{"Start":"03:22.435 ","End":"03:24.370","Text":"mark to slash song."},{"Start":"03:24.370 ","End":"03:27.565","Text":"We\u0027ve got these extra characters here between"},{"Start":"03:27.565 ","End":"03:30.970","Text":"the opening tag and the greater than symbol,"},{"Start":"03:30.970 ","End":"03:34.090","Text":"and they\u0027re called attributes."},{"Start":"03:34.090 ","End":"03:40.420","Text":"This attribute here is marking the track number presumably if this is a song on an album."},{"Start":"03:40.420 ","End":"03:45.110","Text":"I\u0027ll break this onto another line now by closing the quotes,"},{"Start":"03:45.110 ","End":"03:49.055","Text":"and just putting a plus symbol on the end, no semicolon needed."},{"Start":"03:49.055 ","End":"03:51.430","Text":"Then I can just carry on the next line."},{"Start":"03:51.430 ","End":"03:55.210","Text":"This makes it much easier to read and change,"},{"Start":"03:55.210 ","End":"04:00.245","Text":"and then we close off the whole thing with slash album."},{"Start":"04:00.245 ","End":"04:02.735","Text":"Then a semicolon on the end."},{"Start":"04:02.735 ","End":"04:08.460","Text":"That\u0027s Part 1 done,"},{"Start":"04:08.460 ","End":"04:09.955","Text":"and then in part A2,"},{"Start":"04:09.955 ","End":"04:14.100","Text":"we need to create a string array called songs."},{"Start":"04:14.450 ","End":"04:20.100","Text":"Songs, string, and it\u0027s uninitialized."},{"Start":"04:20.100 ","End":"04:24.960","Text":"We just say it\u0027s an array of strings of size 10."},{"Start":"04:24.960 ","End":"04:28.830","Text":"Then we\u0027ve got that 1 way round, haven\u0027t I?"},{"Start":"04:28.830 ","End":"04:32.352","Text":"String should go there,"},{"Start":"04:32.352 ","End":"04:34.700","Text":"and songs should go there."},{"Start":"04:34.700 ","End":"04:37.352","Text":"Then I\u0027ve got in Part 3,"},{"Start":"04:37.352 ","End":"04:42.725","Text":"to create a number of integer variables all initialized to zeros. So let\u0027s do that."},{"Start":"04:42.725 ","End":"04:44.580","Text":"Index is 0,"},{"Start":"04:44.580 ","End":"04:46.930","Text":"start is 0,"},{"Start":"04:46.930 ","End":"04:49.235","Text":"and end is 0."},{"Start":"04:49.235 ","End":"04:54.510","Text":"Then Boolean variable in Part 4 called finished,"},{"Start":"04:54.510 ","End":"04:57.690","Text":"and that\u0027s set to false."},{"Start":"04:57.690 ","End":"04:59.770","Text":"Now moving on to part B,"},{"Start":"04:59.770 ","End":"05:03.785","Text":"where we\u0027ve been asked to use a loop which terminates when finished is true."},{"Start":"05:03.785 ","End":"05:07.550","Text":"We use this loop to pass source string to extract the song name"},{"Start":"05:07.550 ","End":"05:12.420","Text":"between song here and flash song."},{"Start":"05:12.420 ","End":"05:14.670","Text":"As we\u0027ve been reminded here,"},{"Start":"05:14.670 ","End":"05:18.945","Text":"some songs have extra texts between the massive,"},{"Start":"05:18.945 ","End":"05:20.990","Text":"that extra text attribute here,"},{"Start":"05:20.990 ","End":"05:22.510","Text":"between the words song,"},{"Start":"05:22.510 ","End":"05:24.080","Text":"and the greater than symbol."},{"Start":"05:24.080 ","End":"05:25.685","Text":"So you have to take account for that,"},{"Start":"05:25.685 ","End":"05:27.500","Text":"because it happens here for this song,"},{"Start":"05:27.500 ","End":"05:30.410","Text":"and this song, but it doesn\u0027t happen for this song."},{"Start":"05:30.410 ","End":"05:33.065","Text":"Let\u0027s use a while loop to do that."},{"Start":"05:33.065 ","End":"05:39.110","Text":"We say while and then we\u0027re looking to terminate when finished is true."},{"Start":"05:39.110 ","End":"05:42.845","Text":"I could say finished equals true."},{"Start":"05:42.845 ","End":"05:46.520","Text":"But actually what we\u0027re looking for is while it\u0027s not finished,"},{"Start":"05:46.520 ","End":"05:49.080","Text":"that\u0027s where we want to carry on the loop,"},{"Start":"05:49.080 ","End":"05:51.350","Text":"and as soon as finished goes to true,"},{"Start":"05:51.350 ","End":"05:54.290","Text":"not finished will be false,"},{"Start":"05:54.290 ","End":"05:55.685","Text":"therefore it will end."},{"Start":"05:55.685 ","End":"05:59.540","Text":"So that\u0027s the inside of the loop started,"},{"Start":"05:59.540 ","End":"06:02.060","Text":"and we got to use start and end to store the beginning,"},{"Start":"06:02.060 ","End":"06:06.380","Text":"and end of the substring to be extracted in part B 1."},{"Start":"06:06.380 ","End":"06:11.990","Text":"Let\u0027s start by just finding in"},{"Start":"06:11.990 ","End":"06:17.510","Text":"sourceString a position of the bit that we\u0027re looking for,"},{"Start":"06:17.510 ","End":"06:22.370","Text":"which is song and I\u0027m not going to put"},{"Start":"06:22.370 ","End":"06:25.550","Text":"that because it wouldn\u0027t work for all cases that we"},{"Start":"06:25.550 ","End":"06:28.880","Text":"see in his string here because we\u0027ve got the attribute to think of."},{"Start":"06:28.880 ","End":"06:30.890","Text":"That\u0027s what we\u0027re going to start with."},{"Start":"06:30.890 ","End":"06:32.390","Text":"You need to store that somewhere,"},{"Start":"06:32.390 ","End":"06:35.010","Text":"so I\u0027ll store it in start."},{"Start":"06:35.210 ","End":"06:44.750","Text":"We\u0027ll only proceed if stopped is equal to anything but minus 1."},{"Start":"06:44.750 ","End":"06:46.640","Text":"If it is minus 1,"},{"Start":"06:46.640 ","End":"06:48.410","Text":"that means we haven\u0027t found song,"},{"Start":"06:48.410 ","End":"06:53.675","Text":"and we can finish because there\u0027s no more songs to be found."},{"Start":"06:53.675 ","End":"06:57.500","Text":"So we\u0027ll do that by setting finish to true."},{"Start":"06:57.500 ","End":"07:00.860","Text":"If we haven\u0027t found a minus 1,"},{"Start":"07:00.860 ","End":"07:04.805","Text":"which means we did find the fragment here less than the song,"},{"Start":"07:04.805 ","End":"07:07.190","Text":"then we need to do something with it."},{"Start":"07:07.190 ","End":"07:10.115","Text":"What we\u0027re going to do first of all is,"},{"Start":"07:10.115 ","End":"07:14.525","Text":"we\u0027re going to strip off everything that came before,"},{"Start":"07:14.525 ","End":"07:17.500","Text":"we store back into sourceString."},{"Start":"07:17.500 ","End":"07:24.230","Text":"SouceStrings substring from where we found less than song to the end of the string."},{"Start":"07:24.230 ","End":"07:29.300","Text":"If we do that what this will have the effect of doing is we\u0027ll find"},{"Start":"07:29.300 ","End":"07:34.475","Text":"the first song here and everything before it will strip off,"},{"Start":"07:34.475 ","End":"07:38.195","Text":"and then it will store the result back into sourceString."},{"Start":"07:38.195 ","End":"07:42.875","Text":"Then from there, we want to look for the next symbol,"},{"Start":"07:42.875 ","End":"07:46.035","Text":"which is this greater than symbol."},{"Start":"07:46.035 ","End":"07:49.325","Text":"We found this position stripped off everything in front."},{"Start":"07:49.325 ","End":"07:52.520","Text":"The very next time we see a greater than symbol,"},{"Start":"07:52.520 ","End":"07:57.500","Text":"we won\u0027t want to stop there because that\u0027s the part we want to start extracting from."},{"Start":"07:57.500 ","End":"08:01.640","Text":"Let\u0027s store a new value into Start."},{"Start":"08:01.640 ","End":"08:05.475","Text":"That will be by looking for that symbol,"},{"Start":"08:05.475 ","End":"08:08.415","Text":"and then we can look for the end as well,"},{"Start":"08:08.415 ","End":"08:13.230","Text":"and what we\u0027re looking for for the end is backslash song."},{"Start":"08:13.230 ","End":"08:16.430","Text":"Now we found the start position and the end position."},{"Start":"08:16.430 ","End":"08:24.410","Text":"We can extract the sub string and store it into the songs array at position index."},{"Start":"08:24.410 ","End":"08:28.230","Text":"I\u0027ve got 2 nicely named variables start and end,"},{"Start":"08:28.230 ","End":"08:32.060","Text":"we\u0027re going from a start position to the end position."},{"Start":"08:32.060 ","End":"08:36.980","Text":"That will be from here to here for the first song,"},{"Start":"08:36.980 ","End":"08:39.905","Text":"so we should get that out as the first song name."},{"Start":"08:39.905 ","End":"08:41.765","Text":"We\u0027ve now stored it."},{"Start":"08:41.765 ","End":"08:46.355","Text":"What we also need to do now is to strip off what we\u0027ve already dealt with."},{"Start":"08:46.355 ","End":"08:51.295","Text":"If we say source string equals sourceString.substring,"},{"Start":"08:51.295 ","End":"08:53.750","Text":"but this time we will say end."},{"Start":"08:53.750 ","End":"08:58.215","Text":"What will happen is where it found that previous song,"},{"Start":"08:58.215 ","End":"09:01.370","Text":"it will strip off everything in front of it from there to there."},{"Start":"09:01.370 ","End":"09:04.670","Text":"We already stripped a whole load of stuff off earlier,"},{"Start":"09:04.670 ","End":"09:08.330","Text":"but this will strip the track part that we just dealt with there."},{"Start":"09:08.330 ","End":"09:15.485","Text":"The only final thing to do is to increment index. Do that."},{"Start":"09:15.485 ","End":"09:20.285","Text":"That\u0027s the inside of the loop done and dealt with,"},{"Start":"09:20.285 ","End":"09:25.865","Text":"and what we need to do now is to do part C,"},{"Start":"09:25.865 ","End":"09:27.560","Text":"and that\u0027s outside the loop,"},{"Start":"09:27.560 ","End":"09:28.730","Text":"not inside the loop."},{"Start":"09:28.730 ","End":"09:33.575","Text":"Here we need to output the songs array."},{"Start":"09:33.575 ","End":"09:36.590","Text":"I want to reuse the index variable."},{"Start":"09:36.590 ","End":"09:38.720","Text":"Then what we\u0027re going to do is,"},{"Start":"09:38.720 ","End":"09:40.535","Text":"this is going to be 1 line in this."},{"Start":"09:40.535 ","End":"09:47.030","Text":"We want to print out the song pointed to by index."},{"Start":"09:47.030 ","End":"09:50.210","Text":"That will allow us to test whether we\u0027ve"},{"Start":"09:50.210 ","End":"09:53.750","Text":"successfully extracted the 3 songs from the string,"},{"Start":"09:53.750 ","End":"09:55.820","Text":"and it does what we needed to."},{"Start":"09:55.820 ","End":"09:58.040","Text":"I\u0027ve misspelled something there."},{"Start":"09:58.040 ","End":"10:01.780","Text":"Song should be songs."},{"Start":"10:01.780 ","End":"10:05.340","Text":"Correct that. It seems to be happier."},{"Start":"10:05.340 ","End":"10:07.890","Text":"Let\u0027s compile that then,"},{"Start":"10:07.890 ","End":"10:10.550","Text":"and see what happens when we run it."},{"Start":"10:10.550 ","End":"10:11.945","Text":"There will be a small error here,"},{"Start":"10:11.945 ","End":"10:14.570","Text":"which I\u0027m aware of and I\u0027ll just leave it there so that you"},{"Start":"10:14.570 ","End":"10:17.480","Text":"can fix it in a moment. There we go."},{"Start":"10:17.480 ","End":"10:19.010","Text":"It\u0027s pretty much worked,"},{"Start":"10:19.010 ","End":"10:21.155","Text":"we\u0027ve extracted 3 songs."},{"Start":"10:21.155 ","End":"10:24.469","Text":"The lines here represent songs."},{"Start":"10:24.469 ","End":"10:26.180","Text":"There were no more songs in the array,"},{"Start":"10:26.180 ","End":"10:29.930","Text":"so the value that was written into the array element was null, so it\u0027s empty."},{"Start":"10:29.930 ","End":"10:32.180","Text":"In other words, it doesn\u0027t look great on the display,"},{"Start":"10:32.180 ","End":"10:33.470","Text":"so we can maybe fix that."},{"Start":"10:33.470 ","End":"10:36.410","Text":"The other thing we need to fix here is we\u0027ve included"},{"Start":"10:36.410 ","End":"10:39.310","Text":"the greater than symbol in the song title,"},{"Start":"10:39.310 ","End":"10:41.220","Text":"which is clearly not good."},{"Start":"10:41.220 ","End":"10:44.199","Text":"We can easily fix that in 2 places."},{"Start":"10:44.199 ","End":"10:49.885","Text":"We either can fix it here by adding 1 at the end of this line here."},{"Start":"10:49.885 ","End":"10:54.310","Text":"Or we could fix it just as easily by putting plus 1 here,"},{"Start":"10:54.310 ","End":"10:57.535","Text":"and then it will skip over that character."},{"Start":"10:57.535 ","End":"11:02.505","Text":"Because at the moment it starts and includes that character."},{"Start":"11:02.505 ","End":"11:05.275","Text":"As I said, I could have added 1 there, or I could add 1 here,"},{"Start":"11:05.275 ","End":"11:09.130","Text":"either way, it will skip over that character, that\u0027s fixed."},{"Start":"11:09.130 ","End":"11:12.700","Text":"The other thing we could do is rather than using a four-loop here,"},{"Start":"11:12.700 ","End":"11:14.920","Text":"we could use a while loop,"},{"Start":"11:14.920 ","End":"11:18.265","Text":"and I\u0027m again going to reuse the index variable."},{"Start":"11:18.265 ","End":"11:22.700","Text":"But that does mean I\u0027ve got to give it an initial value from 0,"},{"Start":"11:22.700 ","End":"11:24.783","Text":"and inside here,"},{"Start":"11:24.783 ","End":"11:27.380","Text":"this time I\u0027m going to say,"},{"Start":"11:27.380 ","End":"11:36.005","Text":"while the song we\u0027re currently pointing to is not equal to null,"},{"Start":"11:36.005 ","End":"11:40.640","Text":"then keep printing otherwise, stop."},{"Start":"11:40.640 ","End":"11:45.260","Text":"I\u0027m then responsible for updating the index variables, so understand that."},{"Start":"11:45.260 ","End":"11:47.540","Text":"That will do exactly what we had before,"},{"Start":"11:47.540 ","End":"11:51.590","Text":"except it won\u0027t print from any null songs."},{"Start":"11:51.590 ","End":"11:55.080","Text":"Just print first 3 songs in this case."},{"Start":"11:55.080 ","End":"11:58.940","Text":"Trust me, believe that will work and we\u0027ll check it out in a moment."},{"Start":"11:58.940 ","End":"12:00.770","Text":"But let\u0027s do the last part,"},{"Start":"12:00.770 ","End":"12:02.730","Text":"which is part E,"},{"Start":"12:02.730 ","End":"12:07.520","Text":"which is to add a few more songs into the list here,"},{"Start":"12:07.520 ","End":"12:11.765","Text":"and we are going to put them here."},{"Start":"12:11.765 ","End":"12:14.310","Text":"Then the final one."},{"Start":"12:15.470 ","End":"12:20.845","Text":"Now it\u0027s added 2 songs to the list."},{"Start":"12:20.845 ","End":"12:23.615","Text":"We now should see 5 outputs,"},{"Start":"12:23.615 ","End":"12:26.610","Text":"and we shouldn\u0027t see those nulls appear anymore,"},{"Start":"12:26.610 ","End":"12:30.605","Text":"or the greater than character if we fixed all those things."},{"Start":"12:30.605 ","End":"12:34.140","Text":"Let\u0027s see if that happens and it seems to have worked fine."},{"Start":"12:34.140 ","End":"12:37.300","Text":"We\u0027ve got 5 songs because we had 5 in the input string."},{"Start":"12:37.300 ","End":"12:39.230","Text":"We don\u0027t see anything at the end of the array."},{"Start":"12:39.230 ","End":"12:41.855","Text":"Potentially, we could have 5 more songs in the array."},{"Start":"12:41.855 ","End":"12:44.570","Text":"But we don\u0027t see the nulls displayed,"},{"Start":"12:44.570 ","End":"12:46.565","Text":"which is much better on the output."},{"Start":"12:46.565 ","End":"12:51.215","Text":"That was a lot more complex than any other ones that we\u0027ve done up until now."},{"Start":"12:51.215 ","End":"12:53.915","Text":"The main difference here was that we used"},{"Start":"12:53.915 ","End":"12:58.235","Text":"substring with 2 values inside the brackets here."},{"Start":"12:58.235 ","End":"13:00.860","Text":"The starting position we wanted to cut from"},{"Start":"13:00.860 ","End":"13:03.740","Text":"and the ending position we wanted to cut up to,"},{"Start":"13:03.740 ","End":"13:08.735","Text":"so it would just get the name of the song from here to here,"},{"Start":"13:08.735 ","End":"13:12.195","Text":"or from here to here and so on."},{"Start":"13:12.195 ","End":"13:14.960","Text":"We did use the other version of substring where it just"},{"Start":"13:14.960 ","End":"13:18.425","Text":"goes from a certain position all the way to the end of the string,"},{"Start":"13:18.425 ","End":"13:20.960","Text":"and we use it here and here. That\u0027s fine as well."},{"Start":"13:20.960 ","End":"13:23.405","Text":"That was the other little innovation here."},{"Start":"13:23.405 ","End":"13:26.600","Text":"We basically were cutting part of the string out,"},{"Start":"13:26.600 ","End":"13:29.030","Text":"and storing it back into the same string."},{"Start":"13:29.030 ","End":"13:32.330","Text":"Basically as we were dealing with each little part,"},{"Start":"13:32.330 ","End":"13:34.970","Text":"we were cutting off what we\u0027d already worked on and"},{"Start":"13:34.970 ","End":"13:37.720","Text":"dealt with so that we didn\u0027t have to cover it again,"},{"Start":"13:37.720 ","End":"13:41.700","Text":"and it would just keep doing that until there\u0027s nothing left to process."},{"Start":"13:41.700 ","End":"13:44.375","Text":"That\u0027s a fairly typical pausing processes."},{"Start":"13:44.375 ","End":"13:48.500","Text":"Slightly more complex one than any of the examples we\u0027ve done so far."},{"Start":"13:48.500 ","End":"13:51.245","Text":"But I hope that was useful,"},{"Start":"13:51.245 ","End":"13:53.570","Text":"and that\u0027s it for this one."},{"Start":"13:53.570 ","End":"13:55.920","Text":"I\u0027ll see you in the next video. See you soon."}],"ID":28364}],"Thumbnail":null,"ID":269238},{"Name":"Functions","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Introduction to Functions","Duration":"7m 37s","ChapterTopicVideoID":27686,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.270","Text":"Hello, welcome to this first video on functions."},{"Start":"00:03.270 ","End":"00:04.500","Text":"By the end of this section,"},{"Start":"00:04.500 ","End":"00:07.470","Text":"you\u0027ll be able to explain the need for and advantages of"},{"Start":"00:07.470 ","End":"00:10.695","Text":"subprograms as a mean of organization,"},{"Start":"00:10.695 ","End":"00:12.420","Text":"describe the difference between"},{"Start":"00:12.420 ","End":"00:16.230","Text":"local and global variables, distinguish between arguments,"},{"Start":"00:16.230 ","End":"00:18.315","Text":"parameters and return values,"},{"Start":"00:18.315 ","End":"00:22.950","Text":"and evaluate the impact of passing by value or by memory reference."},{"Start":"00:22.950 ","End":"00:24.570","Text":"Up until this point in the course,"},{"Start":"00:24.570 ","End":"00:28.035","Text":"we\u0027ve not been too concerned about how our code is organized."},{"Start":"00:28.035 ","End":"00:30.660","Text":"We\u0027ve simply written out the code in the order we want it"},{"Start":"00:30.660 ","End":"00:33.545","Text":"to execute in one long sequence."},{"Start":"00:33.545 ","End":"00:37.520","Text":"Whilst this might be acceptable for very small fragments of code,"},{"Start":"00:37.520 ","End":"00:39.890","Text":"once you start to do anything more involved,"},{"Start":"00:39.890 ","End":"00:42.575","Text":"you run up against some limitations."},{"Start":"00:42.575 ","End":"00:46.895","Text":"Specifically, the code becomes difficult to follow,"},{"Start":"00:46.895 ","End":"00:50.360","Text":"and that makes it harder to develop and maintain."},{"Start":"00:50.360 ","End":"00:53.885","Text":"It also means it can be prone to logical errors."},{"Start":"00:53.885 ","End":"00:56.510","Text":"If a certain task needs performing more than once,"},{"Start":"00:56.510 ","End":"00:58.475","Text":"there\u0027s duplication of code,"},{"Start":"00:58.475 ","End":"01:01.580","Text":"and you start to suddenly have many variables"},{"Start":"01:01.580 ","End":"01:04.595","Text":"and it becomes difficult to name them and manage them."},{"Start":"01:04.595 ","End":"01:09.320","Text":"Code can then also be very difficult to reuse in other programs."},{"Start":"01:09.320 ","End":"01:14.090","Text":"These problems were evident very early on in the advent of programming as an activity,"},{"Start":"01:14.090 ","End":"01:18.395","Text":"and so even the earliest systems and languages required a solution to this problem,"},{"Start":"01:18.395 ","End":"01:21.905","Text":"which they called the sub program, or subroutine."},{"Start":"01:21.905 ","End":"01:25.610","Text":"A subroutine is a sequence of instructions grouped into"},{"Start":"01:25.610 ","End":"01:29.710","Text":"a single callable unit to perform one specific task."},{"Start":"01:29.710 ","End":"01:33.845","Text":"Subroutine is the name that we still use for low-level programming."},{"Start":"01:33.845 ","End":"01:36.020","Text":"In this assembly language program,"},{"Start":"01:36.020 ","End":"01:41.090","Text":"there\u0027s a main block of code which makes use of the subroutine sub1."},{"Start":"01:41.090 ","End":"01:44.060","Text":"When talking about subroutines in high-level languages,"},{"Start":"01:44.060 ","End":"01:47.314","Text":"we use a series of different names depending on the language."},{"Start":"01:47.314 ","End":"01:50.645","Text":"For basic, subroutines still applies."},{"Start":"01:50.645 ","End":"01:55.160","Text":"For other languages, we use the words procedure or function."},{"Start":"01:55.160 ","End":"02:00.860","Text":"In Pascal, procedures and functions both exist and do slightly different things."},{"Start":"02:00.860 ","End":"02:03.620","Text":"Whereas in other languages like C and Python,"},{"Start":"02:03.620 ","End":"02:07.985","Text":"we already talked about functions and there\u0027s no such thing as a procedure."},{"Start":"02:07.985 ","End":"02:11.030","Text":"Object oriented languages use the word method or"},{"Start":"02:11.030 ","End":"02:15.755","Text":"member function rather than just function like C or Python."},{"Start":"02:15.755 ","End":"02:18.140","Text":"You\u0027ll never hear about procedures."},{"Start":"02:18.140 ","End":"02:21.065","Text":"If you were to write a program with subroutines,"},{"Start":"02:21.065 ","End":"02:23.810","Text":"you divide up the tasks that need to be done and use"},{"Start":"02:23.810 ","End":"02:27.980","Text":"a single subroutine to complete each of those tasks."},{"Start":"02:27.980 ","End":"02:33.860","Text":"We call this process of breaking down the tasks into subtasks, decomposition."},{"Start":"02:33.860 ","End":"02:37.460","Text":"Decomposition is solving a complex problem by"},{"Start":"02:37.460 ","End":"02:41.495","Text":"breaking it up into smaller, more manageable tasks."},{"Start":"02:41.495 ","End":"02:43.700","Text":"To get your head around decomposition,"},{"Start":"02:43.700 ","End":"02:45.184","Text":"think of the opposite,"},{"Start":"02:45.184 ","End":"02:50.930","Text":"a composition which is made up of individual parts that are brought together as a whole."},{"Start":"02:50.930 ","End":"02:53.150","Text":"A musical composition is made up of"},{"Start":"02:53.150 ","End":"02:56.720","Text":"different instruments playing different melodies and rhythms."},{"Start":"02:56.720 ","End":"03:01.025","Text":"Here we have 5 instruments with 5 separate parts written out."},{"Start":"03:01.025 ","End":"03:05.840","Text":"The whole thing comes together in a single piece of music we call a composition."},{"Start":"03:05.840 ","End":"03:08.630","Text":"If we were to decompose a piece of music,"},{"Start":"03:08.630 ","End":"03:13.925","Text":"we\u0027re breaking it down into its constituent pieces as maybe a music teacher,"},{"Start":"03:13.925 ","End":"03:16.315","Text":"or a music critic might do."},{"Start":"03:16.315 ","End":"03:21.920","Text":"The same idea can be extended further into the idea of modular development."},{"Start":"03:21.920 ","End":"03:26.585","Text":"Here we\u0027re breaking down the overall system into individual modules of code,"},{"Start":"03:26.585 ","End":"03:30.695","Text":"each of which can be implemented separately and then brought together."},{"Start":"03:30.695 ","End":"03:34.655","Text":"Often different programmers work on different modules individually,"},{"Start":"03:34.655 ","End":"03:36.455","Text":"and these modules when completed,"},{"Start":"03:36.455 ","End":"03:39.230","Text":"are brought together to complete the system."},{"Start":"03:39.230 ","End":"03:41.000","Text":"Looking at this example,"},{"Start":"03:41.000 ","End":"03:44.915","Text":"imagine we\u0027ve been asked to write some code for a website that\u0027s being developed."},{"Start":"03:44.915 ","End":"03:47.210","Text":"We might have specifically been asked to write"},{"Start":"03:47.210 ","End":"03:50.435","Text":"the code to create a new user account on the website."},{"Start":"03:50.435 ","End":"03:54.425","Text":"If we were to decompose this into individual tasks that need completing,"},{"Start":"03:54.425 ","End":"03:56.705","Text":"it might look something like this."},{"Start":"03:56.705 ","End":"04:00.170","Text":"One task is to get the user details from the keyboard."},{"Start":"04:00.170 ","End":"04:02.975","Text":"For example, username, password,"},{"Start":"04:02.975 ","End":"04:04.775","Text":"email address, and so on."},{"Start":"04:04.775 ","End":"04:09.155","Text":"Then we check to see if an account already exists for that e-mail address."},{"Start":"04:09.155 ","End":"04:11.510","Text":"If it does, we first warn"},{"Start":"04:11.510 ","End":"04:15.005","Text":"the user that the e-mail address has already been used on the site,"},{"Start":"04:15.005 ","End":"04:18.380","Text":"and then can give the option to send an e-mail that"},{"Start":"04:18.380 ","End":"04:22.160","Text":"allows them to reset their password if they\u0027ve forgotten it."},{"Start":"04:22.160 ","End":"04:26.075","Text":"Finally, we can attempt to create the new account,"},{"Start":"04:26.075 ","End":"04:28.640","Text":"but first we will call the validate password"},{"Start":"04:28.640 ","End":"04:32.428","Text":"sub-routine to make sure that the password is long enough,"},{"Start":"04:32.428 ","End":"04:35.450","Text":"and contains a mix of upper and lowercase letters,"},{"Start":"04:35.450 ","End":"04:38.180","Text":"alpha-numeric symbols, and so on."},{"Start":"04:38.180 ","End":"04:43.130","Text":"Note that a subroutine itself can be made up from further subroutines."},{"Start":"04:43.130 ","End":"04:48.470","Text":"In this case, password reminder is being used by the check existing"},{"Start":"04:48.470 ","End":"04:55.070","Text":"subroutine and validate password is being used by the create account subroutine."},{"Start":"04:55.070 ","End":"04:57.560","Text":"One advantage of this approach is"},{"Start":"04:57.560 ","End":"05:01.640","Text":"the password reminder can be called from several places on the website."},{"Start":"05:01.640 ","End":"05:03.920","Text":"Not just when someone forgets that they already have"},{"Start":"05:03.920 ","End":"05:06.125","Text":"an account and tries to create a new account,"},{"Start":"05:06.125 ","End":"05:08.645","Text":"but also when a user knows that they have an account,"},{"Start":"05:08.645 ","End":"05:10.315","Text":"but have forgotten the password."},{"Start":"05:10.315 ","End":"05:12.380","Text":"They can go directly to a link somewhere on"},{"Start":"05:12.380 ","End":"05:15.394","Text":"the website that allows them to reset the password."},{"Start":"05:15.394 ","End":"05:17.135","Text":"The same code is called,"},{"Start":"05:17.135 ","End":"05:19.715","Text":"and run from the 2 different places on the website,"},{"Start":"05:19.715 ","End":"05:22.220","Text":"but it only has to be written once."},{"Start":"05:22.220 ","End":"05:24.455","Text":"Now we know the purpose of subroutines."},{"Start":"05:24.455 ","End":"05:26.600","Text":"How do we actually create and use them?"},{"Start":"05:26.600 ","End":"05:29.690","Text":"In Python, we call a subroutine or a function,"},{"Start":"05:29.690 ","End":"05:32.990","Text":"and we use the def keyword to define a function"},{"Start":"05:32.990 ","End":"05:36.500","Text":"following it with an identifier just like we would for a variable."},{"Start":"05:36.500 ","End":"05:38.405","Text":"We\u0027ve been using functions already."},{"Start":"05:38.405 ","End":"05:41.990","Text":"For example, the print function to show output on the screen."},{"Start":"05:41.990 ","End":"05:45.350","Text":"These are provided to us by the programming language and are called"},{"Start":"05:45.350 ","End":"05:48.965","Text":"inbuilt functions or pre-defined functions."},{"Start":"05:48.965 ","End":"05:52.250","Text":"When we want to create our own functions as we\u0027re doing here,"},{"Start":"05:52.250 ","End":"05:54.905","Text":"they\u0027re called user-defined functions."},{"Start":"05:54.905 ","End":"06:00.110","Text":"The statements inside the function are executed one at a time until all have been run,"},{"Start":"06:00.110 ","End":"06:02.930","Text":"and then the function exits and execution"},{"Start":"06:02.930 ","End":"06:05.780","Text":"continues on from where the function was called."},{"Start":"06:05.780 ","End":"06:07.380","Text":"In a flowchart,"},{"Start":"06:07.380 ","End":"06:09.770","Text":"our website example would look like this."},{"Start":"06:09.770 ","End":"06:11.630","Text":"When the program starts,"},{"Start":"06:11.630 ","End":"06:14.715","Text":"the Get User details subroutine is first chord,"},{"Start":"06:14.715 ","End":"06:18.005","Text":"and then when all the statements inside that subroutine have run,"},{"Start":"06:18.005 ","End":"06:22.295","Text":"execution comes back to Point A and the next subroutine runs."},{"Start":"06:22.295 ","End":"06:26.235","Text":"When that completes, execution continues on from Point B,"},{"Start":"06:26.235 ","End":"06:28.475","Text":"and then when the final subroutine here,"},{"Start":"06:28.475 ","End":"06:30.305","Text":"create account as completed,"},{"Start":"06:30.305 ","End":"06:36.035","Text":"we return back to Point C and because there\u0027s nothing left to execute, the program ends."},{"Start":"06:36.035 ","End":"06:38.615","Text":"In some languages like C and Java,"},{"Start":"06:38.615 ","End":"06:41.344","Text":"a special function called main exists,"},{"Start":"06:41.344 ","End":"06:43.970","Text":"which is the starting point of the program."},{"Start":"06:43.970 ","End":"06:47.750","Text":"Code written inside the main function will run first when the program is"},{"Start":"06:47.750 ","End":"06:52.760","Text":"executed and other functions can be called from the main function as needed."},{"Start":"06:52.760 ","End":"06:57.215","Text":"The first line here inside main is run and outputs this message."},{"Start":"06:57.215 ","End":"07:00.550","Text":"Then the function called my subroutine is called,"},{"Start":"07:00.550 ","End":"07:04.400","Text":"instruction inside my subroutine will run, in this case,"},{"Start":"07:04.400 ","End":"07:06.020","Text":"outputting the message inside"},{"Start":"07:06.020 ","End":"07:09.665","Text":"the subroutine and as there\u0027s only one line in this subroutine,"},{"Start":"07:09.665 ","End":"07:11.230","Text":"once it\u0027s executed,"},{"Start":"07:11.230 ","End":"07:14.510","Text":"execution will continue on from after"},{"Start":"07:14.510 ","End":"07:19.250","Text":"the subroutine call in the main function and will output the final message."},{"Start":"07:19.250 ","End":"07:23.230","Text":"The main function has now completed and the program ends."},{"Start":"07:23.230 ","End":"07:25.070","Text":"So that\u0027s it for this first video,"},{"Start":"07:25.070 ","End":"07:28.864","Text":"which gave a brief introduction to the purpose and use of functions."},{"Start":"07:28.864 ","End":"07:32.810","Text":"In the next video, we\u0027ll look at some of the additional complexities that we\u0027ll need to"},{"Start":"07:32.810 ","End":"07:37.740","Text":"take account of when making use of functions. See you soon."}],"ID":29168},{"Watched":false,"Name":"Variable scope and lifetime","Duration":"7m 41s","ChapterTopicVideoID":27976,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:05.580","Text":"Hello again. In the previous video we saw how subroutines called functions or methods in"},{"Start":"00:05.580 ","End":"00:08.385","Text":"most high-level languages can make development of code"},{"Start":"00:08.385 ","End":"00:11.325","Text":"more manageable by allowing programs to be written in small,"},{"Start":"00:11.325 ","End":"00:14.655","Text":"reusable units which can be called at-will."},{"Start":"00:14.655 ","End":"00:20.265","Text":"One change that arises with programs made up of subroutines is with variables."},{"Start":"00:20.265 ","End":"00:22.770","Text":"Variables that are accessible to all parts of"},{"Start":"00:22.770 ","End":"00:25.440","Text":"the code in a program are called global variable,"},{"Start":"00:25.440 ","End":"00:29.555","Text":"and we refer to such variables being of global scope."},{"Start":"00:29.555 ","End":"00:31.925","Text":"In this example C code,"},{"Start":"00:31.925 ","End":"00:37.345","Text":"the variables i and j are declared outside the main and my subroutine functions,"},{"Start":"00:37.345 ","End":"00:39.190","Text":"and so are accessible everywhere,"},{"Start":"00:39.190 ","End":"00:41.720","Text":"and hence are global variables."},{"Start":"00:41.720 ","End":"00:46.084","Text":"Global variables are generally best avoided as they can cause problems,"},{"Start":"00:46.084 ","End":"00:50.615","Text":"especially when multiple programmers are working on a single program,"},{"Start":"00:50.615 ","End":"00:53.075","Text":"and also when writing more complex programs"},{"Start":"00:53.075 ","End":"00:55.715","Text":"that make use of something called multithreading."},{"Start":"00:55.715 ","End":"00:58.355","Text":"One exception is for constants,"},{"Start":"00:58.355 ","End":"01:03.470","Text":"a type of variable whose values set once and then never changes throughout the program."},{"Start":"01:03.470 ","End":"01:07.460","Text":"These can safely be used as global variables."},{"Start":"01:07.460 ","End":"01:10.910","Text":"When writing programs made up of subroutines,"},{"Start":"01:10.910 ","End":"01:15.905","Text":"a variable usually belongs to the subroutine it\u0027s declared inside."},{"Start":"01:15.905 ","End":"01:18.935","Text":"We call these local variables."},{"Start":"01:18.935 ","End":"01:22.160","Text":"These variables will only be accessible to the subroutine,"},{"Start":"01:22.160 ","End":"01:26.405","Text":"and their values cannot be read or accessed by other subroutines."},{"Start":"01:26.405 ","End":"01:30.604","Text":"We say that these variables have local scope."},{"Start":"01:30.604 ","End":"01:34.610","Text":"This idea of global and local variables means we can actually use"},{"Start":"01:34.610 ","End":"01:38.830","Text":"the same variable identifier multiple times in a program."},{"Start":"01:38.830 ","End":"01:40.535","Text":"Here\u0027s the program again,"},{"Start":"01:40.535 ","End":"01:43.700","Text":"where we can see 2 variables called i and j are"},{"Start":"01:43.700 ","End":"01:48.245","Text":"declared outside of any subroutine at what we call the top level,"},{"Start":"01:48.245 ","End":"01:50.833","Text":"i has been given the value of 100,"},{"Start":"01:50.833 ","End":"01:53.885","Text":"and j has been given the value 42."},{"Start":"01:53.885 ","End":"01:59.870","Text":"Inside main, the value of the global variable i is printed."},{"Start":"01:59.870 ","End":"02:04.520","Text":"Then my subroutine when it runs declares a new variable,"},{"Start":"02:04.520 ","End":"02:08.689","Text":"also called i and assigned it the value 50."},{"Start":"02:08.689 ","End":"02:10.340","Text":"This is a local variable,"},{"Start":"02:10.340 ","End":"02:14.015","Text":"so only accessible inside the my subroutine function."},{"Start":"02:14.015 ","End":"02:17.150","Text":"My subroutine then also makes reference to"},{"Start":"02:17.150 ","End":"02:22.530","Text":"variable j with a keyword called extern, meaning external."},{"Start":"02:22.530 ","End":"02:24.770","Text":"In effect saying, hey, compiler,"},{"Start":"02:24.770 ","End":"02:27.710","Text":"there\u0027s already a global variable declared called j,"},{"Start":"02:27.710 ","End":"02:29.990","Text":"when I refer to it in this subroutine,"},{"Start":"02:29.990 ","End":"02:31.970","Text":"that is what I\u0027m talking about."},{"Start":"02:31.970 ","End":"02:35.165","Text":"My subroutine then prints the value of i,"},{"Start":"02:35.165 ","End":"02:41.060","Text":"hence 50 is the value printed because it\u0027s printing the value of the local variable i,"},{"Start":"02:41.060 ","End":"02:44.720","Text":"not the global variable which is also called i."},{"Start":"02:44.720 ","End":"02:46.998","Text":"Then the value of j is printed."},{"Start":"02:46.998 ","End":"02:48.770","Text":"There is only one variable called j,"},{"Start":"02:48.770 ","End":"02:50.164","Text":"the global variable,"},{"Start":"02:50.164 ","End":"02:52.900","Text":"so we see the value 42 printed."},{"Start":"02:52.900 ","End":"02:56.675","Text":"The subroutine then exits and returns to main,"},{"Start":"02:56.675 ","End":"03:03.055","Text":"which executes the final line which outputs the value of the global variable i."},{"Start":"03:03.055 ","End":"03:07.750","Text":"We see 100 output as before at the beginning of the program,"},{"Start":"03:07.750 ","End":"03:09.888","Text":"showing that the local variable i,"},{"Start":"03:09.888 ","End":"03:13.585","Text":"and global variable i are completely separate and distinct,"},{"Start":"03:13.585 ","End":"03:15.520","Text":"and have nothing to do with each other."},{"Start":"03:15.520 ","End":"03:18.835","Text":"The code in Java is almost identical to C,"},{"Start":"03:18.835 ","End":"03:24.205","Text":"the only difference being the extern keyword is not needed inside my subroutine."},{"Start":"03:24.205 ","End":"03:28.420","Text":"Java will assume when we are talking about j inside my subroutine,"},{"Start":"03:28.420 ","End":"03:31.105","Text":"that we mean the global variable j."},{"Start":"03:31.105 ","End":"03:36.070","Text":"Note that in Java all code is contained inside a class definition,"},{"Start":"03:36.070 ","End":"03:43.179","Text":"so variables declared outside the methods are global to all methods inside that class."},{"Start":"03:43.179 ","End":"03:45.705","Text":"Python is again very similar,"},{"Start":"03:45.705 ","End":"03:47.750","Text":"though there\u0027s a subtle difference."},{"Start":"03:47.750 ","End":"03:53.390","Text":"The example program from C and Java was working exactly the same way in Python."},{"Start":"03:53.390 ","End":"03:58.640","Text":"But, if we wanted to change the value of the global variable j inside my subroutine,"},{"Start":"03:58.640 ","End":"04:00.425","Text":"Java would let us do it,"},{"Start":"04:00.425 ","End":"04:02.585","Text":"but Python would give us an error."},{"Start":"04:02.585 ","End":"04:04.250","Text":"To get around it,"},{"Start":"04:04.250 ","End":"04:09.260","Text":"there\u0027s a keyword in Python which works like extern in C called global."},{"Start":"04:09.260 ","End":"04:14.260","Text":"This allows a subroutine to change the value of the global variable that\u0027s identified."},{"Start":"04:14.260 ","End":"04:17.180","Text":"Without the line global j at the top."},{"Start":"04:17.180 ","End":"04:21.230","Text":"This line here where we increment the global variable j would give us"},{"Start":"04:21.230 ","End":"04:25.711","Text":"an error in Python because we\u0027re changing the value of a global variable."},{"Start":"04:25.711 ","End":"04:28.310","Text":"Because we have put the line global j in here,"},{"Start":"04:28.310 ","End":"04:32.900","Text":"Python is now quite happy to let the subroutine change the value of j."},{"Start":"04:32.900 ","End":"04:35.360","Text":"The global variable i was unchanged,"},{"Start":"04:35.360 ","End":"04:37.006","Text":"so no issues here,"},{"Start":"04:37.006 ","End":"04:40.535","Text":"but j\u0027s value has been increased by one,"},{"Start":"04:40.535 ","End":"04:43.205","Text":"and 43 is output at the end of the program,"},{"Start":"04:43.205 ","End":"04:49.430","Text":"even though the main program makes no changes to j whose initial value was 42."},{"Start":"04:49.430 ","End":"04:54.470","Text":"Another difference between global and local variables is their lifetime."},{"Start":"04:54.470 ","End":"04:58.160","Text":"Global variables stay in memory for as long as the program is running."},{"Start":"04:58.160 ","End":"05:00.830","Text":"Whereas local variables are destroyed as soon as"},{"Start":"05:00.830 ","End":"05:03.800","Text":"the subroutine they were declared in exits."},{"Start":"05:03.800 ","End":"05:07.130","Text":"When the main function is running in the C example,"},{"Start":"05:07.130 ","End":"05:10.010","Text":"the variables i and j have been created,"},{"Start":"05:10.010 ","End":"05:13.205","Text":"and will persist for as long as the program is running."},{"Start":"05:13.205 ","End":"05:20.014","Text":"The local variable i only gets created once the my subroutine function runs."},{"Start":"05:20.014 ","End":"05:25.400","Text":"When my subroutine completes its instructions and exits,"},{"Start":"05:25.400 ","End":"05:28.565","Text":"the local variable i would have disappeared,"},{"Start":"05:28.565 ","End":"05:30.380","Text":"and no longer exists."},{"Start":"05:30.380 ","End":"05:36.170","Text":"But a global variables i and j will still be there until the whole program ends,"},{"Start":"05:36.170 ","End":"05:39.920","Text":"at which point they will also cease to exist."},{"Start":"05:39.920 ","End":"05:44.015","Text":"This distinction between local and global variable lifetimes"},{"Start":"05:44.015 ","End":"05:46.955","Text":"means that compilers can do some optimization."},{"Start":"05:46.955 ","End":"05:53.615","Text":"For example, to store local variables inside CPU registers rather than external memory,"},{"Start":"05:53.615 ","End":"05:59.192","Text":"which makes them faster to access and therefore can increase the speed of a program,"},{"Start":"05:59.192 ","End":"06:04.250","Text":"is yet another reason to prefer local variables over global ones."},{"Start":"06:04.250 ","End":"06:08.345","Text":"But if we did want to keep the variable alive across function calls,"},{"Start":"06:08.345 ","End":"06:12.770","Text":"we can do so in C by using the keyword static."},{"Start":"06:12.770 ","End":"06:16.940","Text":"Static tells the compiler not to use a temporary store for a variable,"},{"Start":"06:16.940 ","End":"06:20.770","Text":"but to use main memory as it would for global variables."},{"Start":"06:20.770 ","End":"06:23.255","Text":"i here is still a local variable,"},{"Start":"06:23.255 ","End":"06:28.070","Text":"but its lifetime is now not limited to how long the subroutine is being executed for."},{"Start":"06:28.070 ","End":"06:29.810","Text":"Without the static, remember,"},{"Start":"06:29.810 ","End":"06:34.790","Text":"i would cease to exist once my subroutine finishes running."},{"Start":"06:34.790 ","End":"06:37.700","Text":"The static keyword changes this."},{"Start":"06:37.700 ","End":"06:40.715","Text":"In this slightly modified version of the program earlier,"},{"Start":"06:40.715 ","End":"06:45.343","Text":"we\u0027ve declared the local variable i a static,"},{"Start":"06:45.343 ","End":"06:47.765","Text":"and output its value as before,"},{"Start":"06:47.765 ","End":"06:53.120","Text":"but now we increment its value before exiting the subroutine."},{"Start":"06:53.120 ","End":"06:56.975","Text":"We normally expect a local variable to have disappeared by now,"},{"Start":"06:56.975 ","End":"07:00.220","Text":"but because of the static it lives on."},{"Start":"07:00.220 ","End":"07:02.675","Text":"When we call it a second time,"},{"Start":"07:02.675 ","End":"07:08.405","Text":"the previous value has been preserved and so we see 51 output to the screen."},{"Start":"07:08.405 ","End":"07:11.330","Text":"When we increment the local variable once more,"},{"Start":"07:11.330 ","End":"07:14.390","Text":"this failure again will persist and the variable will"},{"Start":"07:14.390 ","End":"07:18.275","Text":"still be alive when my subroutine exits."},{"Start":"07:18.275 ","End":"07:23.480","Text":"Object-oriented languages like Java and Python can also have static variables,"},{"Start":"07:23.480 ","End":"07:25.895","Text":"although they use in a slightly different way"},{"Start":"07:25.895 ","End":"07:28.595","Text":"which we\u0027ll learn more about later in the course."},{"Start":"07:28.595 ","End":"07:31.295","Text":"Let\u0027s pause there one final time in this topic."},{"Start":"07:31.295 ","End":"07:32.630","Text":"In the next video,"},{"Start":"07:32.630 ","End":"07:35.434","Text":"we\u0027ll look at the most powerful aspect of subroutines,"},{"Start":"07:35.434 ","End":"07:37.700","Text":"the ability to pass values backwards and"},{"Start":"07:37.700 ","End":"07:42.390","Text":"forwards between different pieces of a program. See you then."}],"ID":29169},{"Watched":false,"Name":"Arguments and return values","Duration":"10m 55s","ChapterTopicVideoID":27977,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.650","Text":"We established in the previous video that variables can be local or global."},{"Start":"00:04.650 ","End":"00:08.610","Text":"It\u0027s possible to access global variables inside subroutines,"},{"Start":"00:08.610 ","End":"00:12.390","Text":"even though we said that it wasn\u0027t a good idea to use global variables at all."},{"Start":"00:12.390 ","End":"00:14.910","Text":"Another way to pass data into subroutines,"},{"Start":"00:14.910 ","End":"00:16.830","Text":"let\u0027s call them functions from now on,"},{"Start":"00:16.830 ","End":"00:21.555","Text":"is to use a mechanism that high-level language is provide called arguments."},{"Start":"00:21.555 ","End":"00:25.305","Text":"Up until now, all the code we\u0027ve written was self-contained"},{"Start":"00:25.305 ","End":"00:29.415","Text":"and didn\u0027t involve passing data from one block of code to another."},{"Start":"00:29.415 ","End":"00:32.250","Text":"Let us look at some functions that take in arguments that"},{"Start":"00:32.250 ","End":"00:35.235","Text":"were passed into them by some other part of the program."},{"Start":"00:35.235 ","End":"00:38.820","Text":"It\u0027s useful to think of a function as a box into which we"},{"Start":"00:38.820 ","End":"00:43.280","Text":"pass values which affect what happens inside the box."},{"Start":"00:43.280 ","End":"00:46.475","Text":"These values we call arguments."},{"Start":"00:46.475 ","End":"00:51.320","Text":"In this example, the value we pass in changes the color of the box."},{"Start":"00:51.320 ","End":"00:54.935","Text":"Here we\u0027ve passed in an argument with a value red."},{"Start":"00:54.935 ","End":"00:57.065","Text":"When the function receives it,"},{"Start":"00:57.065 ","End":"01:00.395","Text":"it changes its color to red."},{"Start":"01:00.395 ","End":"01:02.750","Text":"If we pass it blue,"},{"Start":"01:02.750 ","End":"01:06.215","Text":"it would change to blue, and so on."},{"Start":"01:06.215 ","End":"01:11.945","Text":"An argument then is a value passed as an input to a function."},{"Start":"01:11.945 ","End":"01:16.460","Text":"Argument\u0027s the word we use for the value we pass in when we call the function."},{"Start":"01:16.460 ","End":"01:18.350","Text":"But inside the function,"},{"Start":"01:18.350 ","End":"01:22.765","Text":"the argument is received into something called a parameter."},{"Start":"01:22.765 ","End":"01:25.550","Text":"A parameter is a named location used to"},{"Start":"01:25.550 ","End":"01:29.555","Text":"temporarily store data being passed into a function."},{"Start":"01:29.555 ","End":"01:32.690","Text":"The parameter is a bit like a variable that\u0027s"},{"Start":"01:32.690 ","End":"01:36.485","Text":"initialized with the value that is being passed in by the calling code."},{"Start":"01:36.485 ","End":"01:38.359","Text":"Just like a local variable,"},{"Start":"01:38.359 ","End":"01:40.235","Text":"once the function exits,"},{"Start":"01:40.235 ","End":"01:42.043","Text":"the parameter disappears,"},{"Start":"01:42.043 ","End":"01:43.700","Text":"and cannot be recovered."},{"Start":"01:43.700 ","End":"01:47.480","Text":"The important thing about the parameter is the identifier."},{"Start":"01:47.480 ","End":"01:51.875","Text":"Some languages also require a data type to be provided."},{"Start":"01:51.875 ","End":"01:53.990","Text":"Sticking with the previous example,"},{"Start":"01:53.990 ","End":"01:59.345","Text":"let us see the code in Python to call a function into which we will pass a color."},{"Start":"01:59.345 ","End":"02:02.690","Text":"Rather than change the color of a box on the screen,"},{"Start":"02:02.690 ","End":"02:04.675","Text":"we\u0027ll just print out the color."},{"Start":"02:04.675 ","End":"02:07.605","Text":"Our function has been given the identifier,"},{"Start":"02:07.605 ","End":"02:12.575","Text":"change color, and the parameter has been given the identifier, color."},{"Start":"02:12.575 ","End":"02:14.300","Text":"When we call the function,"},{"Start":"02:14.300 ","End":"02:16.760","Text":"the value we pass in as an argument is"},{"Start":"02:16.760 ","End":"02:20.585","Text":"inside the round brackets following the function identifier."},{"Start":"02:20.585 ","End":"02:22.460","Text":"On the first call here,"},{"Start":"02:22.460 ","End":"02:25.895","Text":"we\u0027re passing in an argument with a value red."},{"Start":"02:25.895 ","End":"02:27.830","Text":"When we get into the function,"},{"Start":"02:27.830 ","End":"02:32.425","Text":"the argument value is available inside the parameter, color."},{"Start":"02:32.425 ","End":"02:35.900","Text":"When the single line of code inside the function runs,"},{"Start":"02:35.900 ","End":"02:37.445","Text":"we get this output."},{"Start":"02:37.445 ","End":"02:39.088","Text":"We exit the function,"},{"Start":"02:39.088 ","End":"02:43.145","Text":"and are back to the next line in the main program where we call the function again,"},{"Start":"02:43.145 ","End":"02:47.185","Text":"but this time with the value of blue being passed in as the argument."},{"Start":"02:47.185 ","End":"02:49.880","Text":"This time on entry to the change color function,"},{"Start":"02:49.880 ","End":"02:53.090","Text":"we will see the value blue into the parameter called color,"},{"Start":"02:53.090 ","End":"02:54.935","Text":"which results in this output."},{"Start":"02:54.935 ","End":"02:57.065","Text":"Once again, the function exits,"},{"Start":"02:57.065 ","End":"02:59.615","Text":"and notice color is no longer available."},{"Start":"02:59.615 ","End":"03:05.300","Text":"But, when we call the function one final time with the argument value of green, again,"},{"Start":"03:05.300 ","End":"03:08.060","Text":"the value is passed into the parameter called color,"},{"Start":"03:08.060 ","End":"03:11.120","Text":"which is then available to use in the line here,"},{"Start":"03:11.120 ","End":"03:13.040","Text":"which produces the output,"},{"Start":"03:13.040 ","End":"03:14.945","Text":"changing color to green."},{"Start":"03:14.945 ","End":"03:17.200","Text":"Then the program\u0027s finished."},{"Start":"03:17.200 ","End":"03:19.760","Text":"To do the same thing in Java or C,"},{"Start":"03:19.760 ","End":"03:23.015","Text":"in addition to specifying an identifier for the parameter,"},{"Start":"03:23.015 ","End":"03:26.200","Text":"we need to supply the data type of the parameter also."},{"Start":"03:26.200 ","End":"03:28.230","Text":"In this fragment of Java,"},{"Start":"03:28.230 ","End":"03:30.965","Text":"the identifier is called color as before,"},{"Start":"03:30.965 ","End":"03:34.280","Text":"but we\u0027re specifying that the datatype of color is string."},{"Start":"03:34.280 ","End":"03:38.135","Text":"It\u0027s possible to have multiple arguments passed into a function."},{"Start":"03:38.135 ","End":"03:42.230","Text":"Hence, we need to specify multiple parameters by simply"},{"Start":"03:42.230 ","End":"03:46.280","Text":"separating them with commas that can be of different data types if we want."},{"Start":"03:46.280 ","End":"03:49.625","Text":"Here we have an integer datatype parameter called pressure,"},{"Start":"03:49.625 ","End":"03:52.160","Text":"and a Boolean datatype called coldfront."},{"Start":"03:52.160 ","End":"03:53.465","Text":"When we call the function,"},{"Start":"03:53.465 ","End":"03:57.920","Text":"we have to pass the arguments in the same order as we declared the parameters."},{"Start":"03:57.920 ","End":"04:02.600","Text":"Hence, this line will output rain likely to the screen,"},{"Start":"04:02.600 ","End":"04:05.990","Text":"but this line will cause the compiler to stop with"},{"Start":"04:05.990 ","End":"04:10.535","Text":"an error message because it\u0027s expecting an int followed by a Boolean,"},{"Start":"04:10.535 ","End":"04:15.260","Text":"but the function is being called with a Boolean followed by an int."},{"Start":"04:15.260 ","End":"04:18.605","Text":"We now know how to get values into a function."},{"Start":"04:18.605 ","End":"04:21.724","Text":"But, what about receiving values back from functions?"},{"Start":"04:21.724 ","End":"04:24.620","Text":"The answer lies with return values."},{"Start":"04:24.620 ","End":"04:29.825","Text":"Return values are the values that a function returns when it\u0027s finished executing."},{"Start":"04:29.825 ","End":"04:35.315","Text":"They\u0027re sent back from the function to their calling code using the keyword return."},{"Start":"04:35.315 ","End":"04:41.315","Text":"In Java and C, we need to specify what the data type is of the value being returned."},{"Start":"04:41.315 ","End":"04:44.585","Text":"It proceeds the name of the function when it\u0027s declared."},{"Start":"04:44.585 ","End":"04:47.165","Text":"Here our function called work or relax,"},{"Start":"04:47.165 ","End":"04:50.065","Text":"returns a string to the calling code."},{"Start":"04:50.065 ","End":"04:54.740","Text":"The variable message will contain either work day or relax."},{"Start":"04:54.740 ","End":"04:58.235","Text":"It\u0027s the weekend depending on the value of the argument."},{"Start":"04:58.235 ","End":"05:03.965","Text":"The argument of 1 here will result in work day being returned and stored."},{"Start":"05:03.965 ","End":"05:06.935","Text":"The return keyword will not only return the value,"},{"Start":"05:06.935 ","End":"05:09.155","Text":"but we\u0027ll also exit the function."},{"Start":"05:09.155 ","End":"05:13.160","Text":"If there are lines of code after the line containing the return keyword,"},{"Start":"05:13.160 ","End":"05:19.145","Text":"they\u0027ll not run and may even cause a compiler warning or error depending on the language."},{"Start":"05:19.145 ","End":"05:22.325","Text":"If a function doesn\u0027t return a value in C or Java,"},{"Start":"05:22.325 ","End":"05:25.550","Text":"we mark a return datatype as void."},{"Start":"05:25.550 ","End":"05:27.785","Text":"We saw this in the example earlier,"},{"Start":"05:27.785 ","End":"05:32.530","Text":"where all we did was print out color and no return value was needed."},{"Start":"05:32.530 ","End":"05:38.080","Text":"Java and C only allow 1 return value to be returned from a function."},{"Start":"05:38.080 ","End":"05:40.600","Text":"Whereas Python, and some other languages allow"},{"Start":"05:40.600 ","End":"05:44.050","Text":"multiple return values to be returned from a single function."},{"Start":"05:44.050 ","End":"05:47.915","Text":"In this example, the function is passed 2 values,"},{"Start":"05:47.915 ","End":"05:50.440","Text":"and the highest value is returned first,"},{"Start":"05:50.440 ","End":"05:52.270","Text":"followed by the other value."},{"Start":"05:52.270 ","End":"05:53.935","Text":"Just as with parameters,"},{"Start":"05:53.935 ","End":"05:55.825","Text":"the order is important."},{"Start":"05:55.825 ","End":"06:01.440","Text":"The first value returned ends up in the first variable specified here, high."},{"Start":"06:01.440 ","End":"06:06.475","Text":"The second value returned is in the next variable after the comma, low."},{"Start":"06:06.475 ","End":"06:10.360","Text":"The only difference therefore between the 2 lines in the if and else blocks is"},{"Start":"06:10.360 ","End":"06:15.145","Text":"the order in which the inputs val1 and val2 are being returned."},{"Start":"06:15.145 ","End":"06:19.760","Text":"Some languages let you choose how arguments are passed into the function."},{"Start":"06:19.760 ","End":"06:25.370","Text":"Passing an argument by value means that the function gets a copy of the original data."},{"Start":"06:25.370 ","End":"06:28.685","Text":"Therefore, if changes are made to the value inside the function,"},{"Start":"06:28.685 ","End":"06:32.539","Text":"the original value is left intact and cannot be changed."},{"Start":"06:32.539 ","End":"06:35.450","Text":"Let\u0027s look at an example in C. Here,"},{"Start":"06:35.450 ","End":"06:41.555","Text":"i and j are global variables which starts out with the values 25 and 50."},{"Start":"06:41.555 ","End":"06:44.585","Text":"Swap by vowel is called."},{"Start":"06:44.585 ","End":"06:51.480","Text":"Copies of i and j are being passed into num1 and num2 respectively."},{"Start":"06:52.340 ","End":"06:57.925","Text":"A local variable temp takes on the value of num1."},{"Start":"06:57.925 ","End":"07:01.575","Text":"Num1 takes on the value of num2."},{"Start":"07:01.575 ","End":"07:04.860","Text":"Num2 takes on the value of temp."},{"Start":"07:04.860 ","End":"07:11.300","Text":"Now we\u0027ve swapped over the values in the 2 parameters which were taken from i and j."},{"Start":"07:11.300 ","End":"07:16.200","Text":"But when the swap by val function completes and exits,"},{"Start":"07:16.200 ","End":"07:19.275","Text":"and the program prints the values of i and j,"},{"Start":"07:19.275 ","End":"07:23.485","Text":"they are unchanged even though inside the swap by val routine,"},{"Start":"07:23.485 ","End":"07:26.050","Text":"the values passed in was swapped over."},{"Start":"07:26.050 ","End":"07:29.080","Text":"This matches our definition of pass by value."},{"Start":"07:29.080 ","End":"07:33.850","Text":"Copies of the data values are passed in and the original data is not affected."},{"Start":"07:33.850 ","End":"07:36.185","Text":"When passing arguments by reference,"},{"Start":"07:36.185 ","End":"07:38.110","Text":"a memory reference, in other words,"},{"Start":"07:38.110 ","End":"07:41.085","Text":"an address is passed into a function which"},{"Start":"07:41.085 ","End":"07:44.845","Text":"allows the function to read and modify data value."},{"Start":"07:44.845 ","End":"07:47.385","Text":"In contrast to passing by value,"},{"Start":"07:47.385 ","End":"07:49.730","Text":"here we\u0027re not working with a copy of data,"},{"Start":"07:49.730 ","End":"07:51.545","Text":"but with the original data."},{"Start":"07:51.545 ","End":"07:54.830","Text":"Some languages like Pascal, Visual Basic,"},{"Start":"07:54.830 ","End":"07:59.750","Text":"C#, and C plus plus support this type of argument explicitly."},{"Start":"07:59.750 ","End":"08:05.285","Text":"C doesn\u0027t directly, but the same effect can be achieved using a pointer."},{"Start":"08:05.285 ","End":"08:09.065","Text":"Here, instead of passing a copy of the values,"},{"Start":"08:09.065 ","End":"08:15.055","Text":"we\u0027re actually passing the address of where the variables i and j are stored in memory."},{"Start":"08:15.055 ","End":"08:18.020","Text":"The ampersand symbol in C is an operator that"},{"Start":"08:18.020 ","End":"08:21.200","Text":"returns the address of the variable that follows."},{"Start":"08:21.200 ","End":"08:24.770","Text":"Now when we enter our function called swap by ref,"},{"Start":"08:24.770 ","End":"08:30.110","Text":"we\u0027re receiving the address of variable i and the address of variable j."},{"Start":"08:30.110 ","End":"08:33.620","Text":"This line here is declared as having 2 parameters,"},{"Start":"08:33.620 ","End":"08:39.605","Text":"num1 and num2, which point to 2 different integers."},{"Start":"08:39.605 ","End":"08:45.020","Text":"But the implication of being past addresses is that parameters num1 and num2"},{"Start":"08:45.020 ","End":"08:46.580","Text":"actually have access to"},{"Start":"08:46.580 ","End":"08:50.725","Text":"the original values and therefore can change those original values."},{"Start":"08:50.725 ","End":"08:54.170","Text":"Temp takes on the value of num1 as before,"},{"Start":"08:54.170 ","End":"08:56.840","Text":"num1 takes on the value of num2."},{"Start":"08:56.840 ","End":"09:02.480","Text":"Num2 takes on the value of temp and the function exits."},{"Start":"09:02.480 ","End":"09:08.075","Text":"But this time, when we return and print the values of i and j after the function returns,"},{"Start":"09:08.075 ","End":"09:12.395","Text":"we find that when we did the swap inside the swap by ref function,"},{"Start":"09:12.395 ","End":"09:15.365","Text":"we changed the original values passed in."},{"Start":"09:15.365 ","End":"09:18.500","Text":"This is because we have access to the memory addresses."},{"Start":"09:18.500 ","End":"09:23.405","Text":"So num1 was referring directly to the storage location for i,"},{"Start":"09:23.405 ","End":"09:26.075","Text":"and num2 is doing the same for j."},{"Start":"09:26.075 ","End":"09:29.449","Text":"This is the implication of passing by reference."},{"Start":"09:29.449 ","End":"09:31.640","Text":"Rather than returning values,"},{"Start":"09:31.640 ","End":"09:36.875","Text":"we can manipulate the original data directly so there\u0027s no need to return anything."},{"Start":"09:36.875 ","End":"09:42.245","Text":"The values passed in would\u0027ve changed in their original locations."},{"Start":"09:42.245 ","End":"09:48.170","Text":"We effectively used num1 and num2 here as different names for i and j,"},{"Start":"09:48.170 ","End":"09:51.500","Text":"which we can then read and modify directly."},{"Start":"09:51.500 ","End":"09:53.210","Text":"This is how C does it,"},{"Start":"09:53.210 ","End":"09:54.920","Text":"but the general principle is the same for"},{"Start":"09:54.920 ","End":"09:59.060","Text":"other languages that support by reference arguments directly."},{"Start":"09:59.060 ","End":"10:02.615","Text":"In Java, the situation is different again."},{"Start":"10:02.615 ","End":"10:05.960","Text":"All arguments that are primitive data types are passed by"},{"Start":"10:05.960 ","End":"10:09.560","Text":"value and everything else is passed by reference."},{"Start":"10:09.560 ","End":"10:14.120","Text":"This makes sense if you think about passing an array as an argument to a function,"},{"Start":"10:14.120 ","End":"10:16.837","Text":"it could potentially be very large,"},{"Start":"10:16.837 ","End":"10:21.335","Text":"and if you have to create a copy of the array every time you pass it into a function,"},{"Start":"10:21.335 ","End":"10:26.695","Text":"that would have implications on memory usage and speed of execution."},{"Start":"10:26.695 ","End":"10:29.340","Text":"That\u0027s it now for functions."},{"Start":"10:29.340 ","End":"10:34.400","Text":"In this section, we learned how to explain the need for an advantages of subprograms as"},{"Start":"10:34.400 ","End":"10:39.605","Text":"a means of organization to describe the difference between local and global variables,"},{"Start":"10:39.605 ","End":"10:41.735","Text":"to distinguish between arguments,"},{"Start":"10:41.735 ","End":"10:43.865","Text":"parameters and return values,"},{"Start":"10:43.865 ","End":"10:49.040","Text":"and to evaluate the impact on passing by value or by memory reference."},{"Start":"10:49.040 ","End":"10:51.950","Text":"Be sure to complete all the exercises for this topic."},{"Start":"10:51.950 ","End":"10:55.800","Text":"I\u0027ll see you for the next one. Thank you."}],"ID":29170},{"Watched":false,"Name":"Exercise 1","Duration":"5m 28s","ChapterTopicVideoID":27687,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.875","Text":"Hello everyone, welcome to this first video solution on functions,"},{"Start":"00:04.875 ","End":"00:07.365","Text":"in this exercise we\u0027ve been asked to first of all create"},{"Start":"00:07.365 ","End":"00:10.560","Text":"a new project and within a classical functions app,"},{"Start":"00:10.560 ","End":"00:14.505","Text":"within that class we create a method called checkLengthOK"},{"Start":"00:14.505 ","End":"00:19.215","Text":"accepting 1 parameter which is a string and it returns a Boolean."},{"Start":"00:19.215 ","End":"00:21.895","Text":"In part b we are asked to then return"},{"Start":"00:21.895 ","End":"00:26.540","Text":"true if the string passed in is 8 or more characters long,"},{"Start":"00:26.540 ","End":"00:31.940","Text":"otherwise it returns false and finally in part c we test the method works with"},{"Start":"00:31.940 ","End":"00:39.110","Text":"the input string computer returning true and the input string compute returning false."},{"Start":"00:39.110 ","End":"00:44.360","Text":"Having created the new project and a class within it called Functions"},{"Start":"00:44.360 ","End":"00:51.065","Text":"app we\u0027re going to go ahead and do part a and create a method called checkLengthOK,"},{"Start":"00:51.065 ","End":"00:55.490","Text":"now, up until now we\u0027ve been always putting nothing in"},{"Start":"00:55.490 ","End":"00:59.555","Text":"the brackets here and putting void in front"},{"Start":"00:59.555 ","End":"01:05.390","Text":"of the function names because we weren\u0027t interested in getting values out"},{"Start":"01:05.390 ","End":"01:07.580","Text":"so now for the first time we are"},{"Start":"01:07.580 ","End":"01:11.780","Text":"interested in getting values out so we don\u0027t need void here anymore,"},{"Start":"01:11.780 ","End":"01:18.170","Text":"we\u0027re told in part a that this function method we\u0027ll call it in Java,"},{"Start":"01:18.170 ","End":"01:19.805","Text":"returns a Boolean,"},{"Start":"01:19.805 ","End":"01:23.150","Text":"so rather than putting void there we now need to put"},{"Start":"01:23.150 ","End":"01:28.235","Text":"Boolean and then inside the brackets this is where we define what parameters we have,"},{"Start":"01:28.235 ","End":"01:34.130","Text":"in this question we only have 1 parameter and it\u0027s a string so in Java you need to"},{"Start":"01:34.130 ","End":"01:40.415","Text":"put the datatype first of the parameter and then give an identifier to the parameter."},{"Start":"01:40.415 ","End":"01:46.650","Text":"Let\u0027s call ours in string and we\u0027re good to go from that point onwards."},{"Start":"01:46.650 ","End":"01:49.585","Text":"All we need to do is in part b,"},{"Start":"01:49.585 ","End":"01:52.910","Text":"return the value true if the string passed in"},{"Start":"01:52.910 ","End":"01:57.340","Text":"is 8 or more characters long otherwise it returns false."},{"Start":"01:57.340 ","End":"01:59.160","Text":"We don\u0027t have any variables,"},{"Start":"01:59.160 ","End":"02:01.700","Text":"we don\u0027t need to define any variables because all we are going"},{"Start":"02:01.700 ","End":"02:04.460","Text":"to operate on is this input parameter,"},{"Start":"02:04.460 ","End":"02:05.945","Text":"which is called string,"},{"Start":"02:05.945 ","End":"02:09.170","Text":"which we use basically like a variable so we can"},{"Start":"02:09.170 ","End":"02:13.700","Text":"say inString.length which is the string function that"},{"Start":"02:13.700 ","End":"02:17.210","Text":"returns the length of a string and we can compare"},{"Start":"02:17.210 ","End":"02:22.400","Text":"that to 8 so we want to say greater than or equal to 8."},{"Start":"02:22.400 ","End":"02:26.810","Text":"Let\u0027s put all of that inside some brackets and if in"},{"Start":"02:26.810 ","End":"02:32.240","Text":"front so that then if we passed in the string is greater than"},{"Start":"02:32.240 ","End":"02:37.085","Text":"or equal to 8 characters we\u0027re going to return the value"},{"Start":"02:37.085 ","End":"02:42.380","Text":"true otherwise we\u0027re going to return the value false,"},{"Start":"02:42.380 ","End":"02:43.910","Text":"I can do this 2 ways,"},{"Start":"02:43.910 ","End":"02:46.895","Text":"I can either put else return false"},{"Start":"02:46.895 ","End":"02:50.810","Text":"or actually I don\u0027t need the else because what would happen is"},{"Start":"02:50.810 ","End":"02:57.500","Text":"remember that return not only returns a value it exits the function as well so it"},{"Start":"02:57.500 ","End":"03:00.545","Text":"would never get to this line here return false"},{"Start":"03:00.545 ","End":"03:04.625","Text":"if it had already executed this line here, return true."},{"Start":"03:04.625 ","End":"03:07.250","Text":"So when the condition evaluates to true,"},{"Start":"03:07.250 ","End":"03:09.620","Text":"we\u0027re returning a value of true and then"},{"Start":"03:09.620 ","End":"03:13.340","Text":"exiting the function therefore we\u0027ll never get to this line here."},{"Start":"03:13.340 ","End":"03:17.210","Text":"Let\u0027s have a go at running that and testing it"},{"Start":"03:17.210 ","End":"03:20.750","Text":"so now we don\u0027t actually need to use any variables at all,"},{"Start":"03:20.750 ","End":"03:24.619","Text":"we\u0027re going to pass in a value as an argument into inString"},{"Start":"03:24.619 ","End":"03:29.240","Text":"and BlueJ will actually return as a value of true or false,"},{"Start":"03:29.240 ","End":"03:31.895","Text":"the environment is set up that way."},{"Start":"03:31.895 ","End":"03:36.900","Text":"Let\u0027s do that and so we\u0027ll create an object and then within"},{"Start":"03:36.900 ","End":"03:41.745","Text":"that we will run our method called checkLengthOK."},{"Start":"03:41.745 ","End":"03:48.680","Text":"Now BlueJ comes up with a dialog box here asking us to supply the value of the argument."},{"Start":"03:48.680 ","End":"03:51.200","Text":"You wouldn\u0027t see this in a normal program"},{"Start":"03:51.200 ","End":"03:54.425","Text":"this is only because we\u0027re using the object workbench here to"},{"Start":"03:54.425 ","End":"04:00.680","Text":"launch the method and therefore it knows it needs a string parameter,"},{"Start":"04:00.680 ","End":"04:04.265","Text":"an argument and it gets passed into a parameter"},{"Start":"04:04.265 ","End":"04:09.345","Text":"and we\u0027re expected to give it here so I\u0027m going to put in the string,"},{"Start":"04:09.345 ","End":"04:12.920","Text":"it needs to be 8 or more characters long,"},{"Start":"04:12.920 ","End":"04:16.910","Text":"so I\u0027ll just use the digits 1-8 and now I\u0027ve got the right length."},{"Start":"04:16.910 ","End":"04:20.180","Text":"You have to put it in quotes because it\u0027s a string"},{"Start":"04:20.180 ","End":"04:23.510","Text":"or you\u0027ll get an error message from BlueJ and then we click on"},{"Start":"04:23.510 ","End":"04:26.795","Text":"\"Okay\" and we get a dialog box coming back"},{"Start":"04:26.795 ","End":"04:31.070","Text":"and it\u0027s telling us it\u0027s return as a value this function"},{"Start":"04:31.070 ","End":"04:34.550","Text":"which is true and that\u0027s what we were expecting because"},{"Start":"04:34.550 ","End":"04:39.620","Text":"the input string was greater than or equal to A in this case it was exactly equal to A,"},{"Start":"04:39.620 ","End":"04:41.030","Text":"so I don\u0027t need to recompile,"},{"Start":"04:41.030 ","End":"04:43.820","Text":"I can just run my method again,"},{"Start":"04:43.820 ","End":"04:46.220","Text":"this time passing a different length,"},{"Start":"04:46.220 ","End":"04:48.080","Text":"let\u0027s just pass 1, 2, 3,"},{"Start":"04:48.080 ","End":"04:53.375","Text":"clearly much less than 8 characters and I\u0027ve returned the value false."},{"Start":"04:53.375 ","End":"04:55.340","Text":"I\u0027ve done this exercise,"},{"Start":"04:55.340 ","End":"05:00.260","Text":"that\u0027s part c done while actually we were asked to use the word computer,"},{"Start":"05:00.260 ","End":"05:04.850","Text":"computer is exactly of 8 characters so just for"},{"Start":"05:04.850 ","End":"05:09.360","Text":"completeness sake let\u0027s do that and count those characters,"},{"Start":"05:09.360 ","End":"05:11.630","Text":"it is 8 and it does return true."},{"Start":"05:11.630 ","End":"05:14.470","Text":"That\u0027s it for the first exercise on functions,"},{"Start":"05:14.470 ","End":"05:16.415","Text":"we have been using functions in a way,"},{"Start":"05:16.415 ","End":"05:19.130","Text":"but not as they\u0027re intended to be used."},{"Start":"05:19.130 ","End":"05:22.370","Text":"This was the first exercise where we\u0027ve started to use"},{"Start":"05:22.370 ","End":"05:24.590","Text":"return values and parameters which we\u0027ll"},{"Start":"05:24.590 ","End":"05:28.740","Text":"continue in future exercises. See you in the next one."}],"ID":29171},{"Watched":false,"Name":"Exercise 2","Duration":"5m 55s","ChapterTopicVideoID":27688,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.545","Text":"Hello, welcome back."},{"Start":"00:01.545 ","End":"00:03.390","Text":"In this exercise, we\u0027ve been told that"},{"Start":"00:03.390 ","End":"00:06.480","Text":"Pythagoras\u0027 Theorem states that we can find the length of"},{"Start":"00:06.480 ","End":"00:12.150","Text":"a side h in a right angle triangle given the other 2 sides a and o,"},{"Start":"00:12.150 ","End":"00:16.890","Text":"according to the formula h^2 equals a^2 plus o^2."},{"Start":"00:16.890 ","End":"00:20.010","Text":"In part a, we\u0027re asked to use Code Pad in BlueJ to"},{"Start":"00:20.010 ","End":"00:23.280","Text":"experiment with the Java math library functions,"},{"Start":"00:23.280 ","End":"00:25.430","Text":"pow, and square root,"},{"Start":"00:25.430 ","End":"00:29.960","Text":"which allow you to square a number and take the square root of a number respectively."},{"Start":"00:29.960 ","End":"00:34.610","Text":"Math.pow 3,2 will return 3^2, which is 9."},{"Start":"00:34.610 ","End":"00:38.150","Text":"Math.squareroot 25 returns 5,"},{"Start":"00:38.150 ","End":"00:40.685","Text":"which is the square root of 25."},{"Start":"00:40.685 ","End":"00:45.770","Text":"In part b, we create a method that accepts 2 doubles and returns a double."},{"Start":"00:45.770 ","End":"00:47.160","Text":"In part c,"},{"Start":"00:47.160 ","End":"00:50.980","Text":"we use the 2 input values as a, and o,"},{"Start":"00:50.980 ","End":"00:55.025","Text":"and use Pythagoras to calculate and return the length of"},{"Start":"00:55.025 ","End":"00:59.870","Text":"h by taking the square root of a^2 plus o^2."},{"Start":"00:59.870 ","End":"01:06.495","Text":"In part d, we test that the input arguments of 4 and 3 return the value 5."},{"Start":"01:06.495 ","End":"01:11.375","Text":"In this exercise, we\u0027ve been asked to find using Pythagoras Theorem,"},{"Start":"01:11.375 ","End":"01:15.905","Text":"the length of a side h in a right-angled triangle."},{"Start":"01:15.905 ","End":"01:21.320","Text":"That gives us a formula of h^2 equals a^2 plus o^2."},{"Start":"01:21.320 ","End":"01:23.735","Text":"We\u0027re trying to work out h here basically."},{"Start":"01:23.735 ","End":"01:27.590","Text":"The first thing we\u0027ve been asked to do in part a is to use the Code Pad,"},{"Start":"01:27.590 ","End":"01:30.860","Text":"which we have been using at the beginning of the course."},{"Start":"01:30.860 ","End":"01:33.500","Text":"But I\u0027m used for a while just to experiment with"},{"Start":"01:33.500 ","End":"01:37.640","Text":"the math library functions that are available in Java."},{"Start":"01:37.640 ","End":"01:43.040","Text":"Pow allows you to raise a number to a power and"},{"Start":"01:43.040 ","End":"01:48.320","Text":"returns the result and square root takes the square root of a number and returns it too."},{"Start":"01:48.320 ","End":"01:50.615","Text":"Let\u0027s have a little try of both of those."},{"Start":"01:50.615 ","End":"01:54.515","Text":"We have to put math in front of a keyword."},{"Start":"01:54.515 ","End":"01:58.215","Text":"If I do 2,2,"},{"Start":"01:58.215 ","End":"02:03.240","Text":"I\u0027m saying basically give me 2^2 and it returns me 4 if I say it\u0027s"},{"Start":"02:03.240 ","End":"02:09.180","Text":"3,2 is saying give me 3^2 and I get 9."},{"Start":"02:09.180 ","End":"02:14.920","Text":"You get the idea. We can do cubed and we can do anything we want to basically."},{"Start":"02:14.920 ","End":"02:22.320","Text":"That\u0027s Math.pow, Math.squareroot will take the square of a number."},{"Start":"02:22.320 ","End":"02:24.480","Text":"If we try with 9,"},{"Start":"02:24.480 ","End":"02:27.850","Text":"we should get back to 3, which we do."},{"Start":"02:27.850 ","End":"02:30.885","Text":"If it\u0027s 25, we should get back to 5."},{"Start":"02:30.885 ","End":"02:36.950","Text":"You get the idea that this is a function that\u0027s been provided to us in a library in Java."},{"Start":"02:36.950 ","End":"02:39.260","Text":"There\u0027s many different libraries in Java."},{"Start":"02:39.260 ","End":"02:41.390","Text":"One of the most fundamental ones you\u0027d want to use at"},{"Start":"02:41.390 ","End":"02:44.390","Text":"the beginning is the math library and"},{"Start":"02:44.390 ","End":"02:50.600","Text":"squaring power between them will allow us to solve this problem in this exercise."},{"Start":"02:50.600 ","End":"02:53.390","Text":"If we go to part b now,"},{"Start":"02:53.390 ","End":"02:57.830","Text":"we\u0027ve been asked to create a method that accepts 2 doubles and returns a double,"},{"Start":"02:57.830 ","End":"03:00.200","Text":"so we know how to do that now,"},{"Start":"03:00.200 ","End":"03:01.520","Text":"we don\u0027t put void in front."},{"Start":"03:01.520 ","End":"03:03.365","Text":"We put the return data type,"},{"Start":"03:03.365 ","End":"03:04.940","Text":"which is going to be double."},{"Start":"03:04.940 ","End":"03:08.260","Text":"Give method a name."},{"Start":"03:08.260 ","End":"03:18.690","Text":"Let\u0027s call it triangle side H. Then let\u0027s say we\u0027ll add 2 parameters are going to be."},{"Start":"03:18.690 ","End":"03:22.375","Text":"One is going to be o for opposite,"},{"Start":"03:22.375 ","End":"03:26.260","Text":"and the other one is going to be a for adjacent."},{"Start":"03:26.260 ","End":"03:29.550","Text":"That\u0027s the first bit done."},{"Start":"03:29.550 ","End":"03:36.340","Text":"I\u0027ve declared a function which returns a double and takes 2 doubles as arguments,"},{"Start":"03:36.340 ","End":"03:40.395","Text":"which end up in parameters called o and a."},{"Start":"03:40.395 ","End":"03:46.510","Text":"Then what I need to do is I simply need to return the value of"},{"Start":"03:46.510 ","End":"03:52.905","Text":"h. I can calculate h by taking a and squaring it,"},{"Start":"03:52.905 ","End":"03:54.755","Text":"adding that to o,"},{"Start":"03:54.755 ","End":"03:56.450","Text":"which is also squared,"},{"Start":"03:56.450 ","End":"03:58.310","Text":"and then the square root of the whole thing."},{"Start":"03:58.310 ","End":"04:00.185","Text":"Let\u0027s do the inner part first,"},{"Start":"04:00.185 ","End":"04:05.855","Text":"I\u0027m going to do Math.pow o^2"},{"Start":"04:05.855 ","End":"04:12.945","Text":"and add to that Math.pow a^2,"},{"Start":"04:12.945 ","End":"04:15.210","Text":"and then all of that,"},{"Start":"04:15.210 ","End":"04:17.119","Text":"when it\u0027s added together,"},{"Start":"04:17.119 ","End":"04:20.345","Text":"I want to take the square root of."},{"Start":"04:20.345 ","End":"04:27.475","Text":"What I need to do is Math.squareroot is the function I use."},{"Start":"04:27.475 ","End":"04:31.670","Text":"The value that\u0027s calculated by adding"},{"Start":"04:31.670 ","End":"04:37.195","Text":"those 2 parts of this expression together is the thing that\u0027s going to be,"},{"Start":"04:37.195 ","End":"04:39.490","Text":"have the square root taken."},{"Start":"04:39.490 ","End":"04:44.130","Text":"That should be all I need to do."},{"Start":"04:44.130 ","End":"04:46.419","Text":"Another very short function,"},{"Start":"04:46.419 ","End":"04:48.700","Text":"but obviously a little bit more complex than what we had"},{"Start":"04:48.700 ","End":"04:51.225","Text":"before and actually quite useful."},{"Start":"04:51.225 ","End":"04:53.185","Text":"Let\u0027s just test that now."},{"Start":"04:53.185 ","End":"04:58.105","Text":"Gives us the value we expect in part d. Let us see what we\u0027ve got an error here."},{"Start":"04:58.105 ","End":"05:00.040","Text":"Illegal style expression, of course,"},{"Start":"05:00.040 ","End":"05:01.825","Text":"I don\u0027t need to put equals,"},{"Start":"05:01.825 ","End":"05:05.950","Text":"you\u0027re not assigning a value to something just by force of habit."},{"Start":"05:05.950 ","End":"05:08.350","Text":"You\u0027re usually putting something into a variable and"},{"Start":"05:08.350 ","End":"05:10.870","Text":"I\u0027ve tried to put it into variable code return,"},{"Start":"05:10.870 ","End":"05:12.370","Text":"which I haven\u0027t graded."},{"Start":"05:12.370 ","End":"05:14.305","Text":"Return is a keyword."},{"Start":"05:14.305 ","End":"05:18.090","Text":"I\u0027m trying to return a value so I don\u0027t need to put an equals there."},{"Start":"05:18.090 ","End":"05:21.965","Text":"I\u0027m just going to return whatever this expression evaluates to,"},{"Start":"05:21.965 ","End":"05:24.110","Text":"so of course, that was an error."},{"Start":"05:24.110 ","End":"05:26.925","Text":"Let\u0027s now run it."},{"Start":"05:26.925 ","End":"05:29.475","Text":"Let\u0027s see what we get."},{"Start":"05:29.475 ","End":"05:34.635","Text":"Let\u0027s pass to it the 2 values, 4 and 3."},{"Start":"05:34.635 ","End":"05:36.300","Text":"4 is one of them,"},{"Start":"05:36.300 ","End":"05:37.760","Text":"3 is the other,"},{"Start":"05:37.760 ","End":"05:41.225","Text":"and we do get the value 5 that we expected."},{"Start":"05:41.225 ","End":"05:44.600","Text":"It seems that function has done the job,"},{"Start":"05:44.600 ","End":"05:49.580","Text":"which is to calculate the size h given o and a."},{"Start":"05:49.580 ","End":"05:52.160","Text":"Assuming that\u0027s a right-angled triangle."},{"Start":"05:52.160 ","End":"05:55.290","Text":"That\u0027s it for this exercise. See you in the next one."}],"ID":29172},{"Watched":false,"Name":"Exercise 3","Duration":"5m 10s","ChapterTopicVideoID":27689,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.860","Text":"Hello again. In this exercise,"},{"Start":"00:01.860 ","End":"00:06.510","Text":"we\u0027ve been asked to create a method called linearSearch that accepts an array"},{"Start":"00:06.510 ","End":"00:11.640","Text":"of ints to search and a single int to search for,"},{"Start":"00:11.640 ","End":"00:13.740","Text":"and it returns an int."},{"Start":"00:13.740 ","End":"00:17.505","Text":"In part a, we use a loop to iterate through each element in the array."},{"Start":"00:17.505 ","End":"00:20.640","Text":"In part b, if the integer is found in the array,"},{"Start":"00:20.640 ","End":"00:23.055","Text":"the method returns the position it was found at."},{"Start":"00:23.055 ","End":"00:25.680","Text":"If not, it returns minus 1."},{"Start":"00:25.680 ","End":"00:30.630","Text":"In part c, we test in Part 1 of c we\u0027ve 1, 3, 5, 7,"},{"Start":"00:30.630 ","End":"00:34.185","Text":"9 and the value 1 to search for in Part 2,"},{"Start":"00:34.185 ","End":"00:38.490","Text":"1, 3, 5, 7, 9 again and the Value 2 this time to search for."},{"Start":"00:38.490 ","End":"00:40.815","Text":"In Part 3, 2, 4, 6, 8,"},{"Start":"00:40.815 ","End":"00:44.120","Text":"10 and the Value 10 to search for."},{"Start":"00:44.120 ","End":"00:48.315","Text":"Let\u0027s create the method linearSearch."},{"Start":"00:48.315 ","End":"00:52.925","Text":"Just still have the habit of typing void in front"},{"Start":"00:52.925 ","End":"00:57.635","Text":"after all these exercises we\u0027ve done where we just have no return value."},{"Start":"00:57.635 ","End":"00:59.640","Text":"But of course, in this question,"},{"Start":"00:59.640 ","End":"01:05.475","Text":"we\u0027re told that it returns an integer so let\u0027s put int in front instead of a void."},{"Start":"01:05.475 ","End":"01:09.620","Text":"Inside the brackets, we\u0027re told that it accepts an array of"},{"Start":"01:09.620 ","End":"01:14.165","Text":"integers to search and a single integer to search for."},{"Start":"01:14.165 ","End":"01:16.670","Text":"If we\u0027re going to put an array of integers in,"},{"Start":"01:16.670 ","End":"01:19.430","Text":"we now have to declare an array, which is that."},{"Start":"01:19.430 ","End":"01:23.660","Text":"We simply need to give an identifier to the parameter."},{"Start":"01:23.660 ","End":"01:26.870","Text":"Let\u0027s call that arrayToSearch maybe,"},{"Start":"01:26.870 ","End":"01:29.285","Text":"and then separated with a comma,"},{"Start":"01:29.285 ","End":"01:32.970","Text":"the next argument to be passed in and"},{"Start":"01:32.970 ","End":"01:37.370","Text":"so I need to give a data type for the parameter and a name for the parameter."},{"Start":"01:37.370 ","End":"01:41.840","Text":"That\u0027s a datatype of an int and let\u0027s give it an identifier"},{"Start":"01:41.840 ","End":"01:46.640","Text":"of target because it\u0027s the target value that we\u0027re looking for."},{"Start":"01:46.640 ","End":"01:51.620","Text":"That\u0027s the beginning done so in part I asked us to use a loop to"},{"Start":"01:51.620 ","End":"01:56.720","Text":"iterate through each element in the array so we know how to do that now."},{"Start":"01:56.720 ","End":"01:58.970","Text":"Let\u0027s create an index variable,"},{"Start":"01:58.970 ","End":"02:00.995","Text":"which we start at 0,"},{"Start":"02:00.995 ","End":"02:04.295","Text":"keep going until we get to the end of the array."},{"Start":"02:04.295 ","End":"02:09.890","Text":"We really using it as if it was an array defined in our method,"},{"Start":"02:09.890 ","End":"02:12.340","Text":"but it\u0027s not, it\u0027s been passed in."},{"Start":"02:12.340 ","End":"02:20.310","Text":"But I can use it just as if it was local to this array and that\u0027s the first part done."},{"Start":"02:20.310 ","End":"02:24.919","Text":"I\u0027ve got my loop part a and now all I\u0027ve got to do is"},{"Start":"02:24.919 ","End":"02:31.055","Text":"find the integer in the array at the current position and then return it if it\u0027s found."},{"Start":"02:31.055 ","End":"02:37.325","Text":"What I\u0027m looking for is I\u0027m looking to see if arrayToSearch i,"},{"Start":"02:37.325 ","End":"02:39.215","Text":"which is the current element,"},{"Start":"02:39.215 ","End":"02:43.580","Text":"is equal to the target value that we\u0027re actually looking for."},{"Start":"02:43.580 ","End":"02:45.285","Text":"That\u0027s all I need to do."},{"Start":"02:45.285 ","End":"02:52.280","Text":"If it is, then I return i because that\u0027s what I\u0027m looking for,"},{"Start":"02:52.280 ","End":"02:55.170","Text":"is the position that I\u0027ve found"},{"Start":"02:55.270 ","End":"03:01.760","Text":"and it will carry on merrily chuntering away until it gets to the end of the array."},{"Start":"03:01.760 ","End":"03:04.040","Text":"If it does get to the end of the array,"},{"Start":"03:04.040 ","End":"03:09.500","Text":"I haven\u0027t found it so I\u0027m told in part b to"},{"Start":"03:09.500 ","End":"03:16.220","Text":"return minus 1 if I don\u0027t find it and it will never get to this line if it does find it."},{"Start":"03:16.220 ","End":"03:18.020","Text":"Because remember X,"},{"Start":"03:18.020 ","End":"03:20.825","Text":"it\u0027s a function when it finds a return keyword."},{"Start":"03:20.825 ","End":"03:23.060","Text":"If it found it in the first position,"},{"Start":"03:23.060 ","End":"03:26.180","Text":"it will return 0 and we\u0027d never get to this line."},{"Start":"03:26.180 ","End":"03:28.130","Text":"If we go all the way to the end of the array,"},{"Start":"03:28.130 ","End":"03:31.310","Text":"it would return minus 1 instead."},{"Start":"03:31.310 ","End":"03:35.590","Text":"That\u0027s enough let\u0027s test to see whether it works."},{"Start":"03:35.590 ","End":"03:40.310","Text":"It looks slightly odd when you type in the parameters for an array,"},{"Start":"03:40.310 ","End":"03:43.535","Text":"but it works just like the previous example we looked at a minute"},{"Start":"03:43.535 ","End":"03:47.560","Text":"ago except this time we\u0027ve got to type in an array of integers."},{"Start":"03:47.560 ","End":"03:52.230","Text":"Those go into braces,"},{"Start":"03:52.230 ","End":"03:54.075","Text":"so 1, 3,"},{"Start":"03:54.075 ","End":"03:56.690","Text":"5, 7, 9,"},{"Start":"03:56.690 ","End":"03:58.160","Text":"that\u0027s the array that we\u0027re passing"},{"Start":"03:58.160 ","End":"04:02.105","Text":"in and we want to test to see if the Value 1 is in there."},{"Start":"04:02.105 ","End":"04:06.620","Text":"Of course a is in the same position 0 and that\u0027s what we\u0027d expect to see."},{"Start":"04:06.620 ","End":"04:09.350","Text":"We do get back 0 so that\u0027s great."},{"Start":"04:09.350 ","End":"04:13.915","Text":"Let\u0027s try it again with the same set of numbers,"},{"Start":"04:13.915 ","End":"04:16.350","Text":"1, 3, 5, 7,"},{"Start":"04:16.350 ","End":"04:21.720","Text":"9, but this time a value that\u0027s not in the array,"},{"Start":"04:21.720 ","End":"04:24.210","Text":"so we should see minus 1."},{"Start":"04:24.210 ","End":"04:28.530","Text":"That seems to have worked as well and then finally,"},{"Start":"04:28.530 ","End":"04:31.770","Text":"1 last 1 of 2, 4,"},{"Start":"04:31.770 ","End":"04:35.674","Text":"6, 8, 10 as the array values."},{"Start":"04:35.674 ","End":"04:39.965","Text":"Let\u0027s look for a Value 10 which is the last item in that array."},{"Start":"04:39.965 ","End":"04:41.765","Text":"I missed the comma here."},{"Start":"04:41.765 ","End":"04:43.520","Text":"We should be seeing that 0,"},{"Start":"04:43.520 ","End":"04:44.690","Text":"1, 2, 3,"},{"Start":"04:44.690 ","End":"04:49.245","Text":"4 and it does return for the position at which it found it."},{"Start":"04:49.245 ","End":"04:53.420","Text":"Another simple method done and in this case we were"},{"Start":"04:53.420 ","End":"04:58.230","Text":"passing in an array and primitive and integer."},{"Start":"04:58.230 ","End":"05:02.780","Text":"It\u0027s quite happy to work with things that are larger than primitives and we\u0027re able"},{"Start":"05:02.780 ","End":"05:07.115","Text":"to use it just like we would if we declared it inside this method."},{"Start":"05:07.115 ","End":"05:11.220","Text":"That\u0027s it for this one. See you for the next exercise."}],"ID":29173},{"Watched":false,"Name":"Exercise 4","Duration":"12m 54s","ChapterTopicVideoID":27690,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.460","Text":"Hello, welcome back. In this video,"},{"Start":"00:02.460 ","End":"00:08.790","Text":"we\u0027ve been told that the quadratic equation takes the form ax^2 plus bx plus c=0."},{"Start":"00:08.790 ","End":"00:12.030","Text":"In Part a, we\u0027ve been told that the string value of"},{"Start":"00:12.030 ","End":"00:16.995","Text":"function converts any number inside the brackets to a string representation."},{"Start":"00:16.995 ","End":"00:19.783","Text":"We\u0027re asked to try this with various sizes of integer,"},{"Start":"00:19.783 ","End":"00:21.905","Text":"and real numbers in the code pad."},{"Start":"00:21.905 ","End":"00:24.620","Text":"For example, string.value of 1, 2,"},{"Start":"00:24.620 ","End":"00:30.380","Text":"3 in Part 1 or string.value of 3.14 in Part 2."},{"Start":"00:30.380 ","End":"00:35.225","Text":"In Part b, we\u0027re asked to write a method called quadratic that takes in 3 doubles,"},{"Start":"00:35.225 ","End":"00:38.015","Text":"a, b, and c and returns a string."},{"Start":"00:38.015 ","End":"00:44.660","Text":"In Part c, we\u0027re asked to return the values of x as a string using the formula"},{"Start":"00:44.660 ","End":"00:51.570","Text":"x=minus b plus or minus square root b^2 minus 4ac divided by 2a."},{"Start":"00:51.570 ","End":"00:55.940","Text":"In Part i, we\u0027re told to note the plus or minus symbol means that there will"},{"Start":"00:55.940 ","End":"01:00.725","Text":"be 2 solutions but if b^2 minus 4ac=0,"},{"Start":"01:00.725 ","End":"01:04.625","Text":"then both solutions will be the same so we can just return one value."},{"Start":"01:04.625 ","End":"01:06.290","Text":"If there are 2 solutions,"},{"Start":"01:06.290 ","End":"01:08.420","Text":"we return both solutions as a string,"},{"Start":"01:08.420 ","End":"01:13.510","Text":"and the format x=xxx or x=yyy,"},{"Start":"01:13.510 ","End":"01:17.365","Text":"where x and y represent the value of x for each of the 2 solutions."},{"Start":"01:17.365 ","End":"01:18.880","Text":"There\u0027s only 1 solution,"},{"Start":"01:18.880 ","End":"01:24.350","Text":"then we output the solution as a string using the format x equals xxx."},{"Start":"01:24.350 ","End":"01:28.010","Text":"If b^2 minus 4ac is less than 0,"},{"Start":"01:28.010 ","End":"01:31.805","Text":"then we return the string complex roots are not supported."},{"Start":"01:31.805 ","End":"01:34.640","Text":"If the value of a or c passed in was 0,"},{"Start":"01:34.640 ","End":"01:37.510","Text":"we return the string invalid input."},{"Start":"01:37.510 ","End":"01:41.520","Text":"In Part d, we\u0027re asked to then test with inputs of a is 1,"},{"Start":"01:41.520 ","End":"01:42.660","Text":"b is minus 6,"},{"Start":"01:42.660 ","End":"01:47.220","Text":"and c is 5 giving the result x is 1 or x is 5."},{"Start":"01:47.220 ","End":"01:53.435","Text":"Inputs of a is 2b is 4 and c is 2 give the result x= minus 1."},{"Start":"01:53.435 ","End":"01:55.070","Text":"Inputs of a is 3,"},{"Start":"01:55.070 ","End":"01:57.545","Text":"b is 5, and c is 3,"},{"Start":"01:57.545 ","End":"02:02.870","Text":"give the result complex roots not supported and inputs of any value of b and"},{"Start":"02:02.870 ","End":"02:08.705","Text":"either a=0 or c=0 gives the results invalid input."},{"Start":"02:08.705 ","End":"02:13.430","Text":"In the first part of the question here we\u0027ve been asked to use the"},{"Start":"02:13.430 ","End":"02:20.160","Text":"string.value of function in the code pad just to see how it works."},{"Start":"02:20.160 ","End":"02:22.550","Text":"Let\u0027s go and do that."},{"Start":"02:22.550 ","End":"02:26.825","Text":"What this function does is whatever integer you pass in,"},{"Start":"02:26.825 ","End":"02:28.310","Text":"it will turn it into a string."},{"Start":"02:28.310 ","End":"02:29.503","Text":"If it looks like an integer,"},{"Start":"02:29.503 ","End":"02:31.640","Text":"and it can turn it into a string,"},{"Start":"02:31.640 ","End":"02:36.965","Text":"it will do, it will work for floating point numbers as well."},{"Start":"02:36.965 ","End":"02:41.120","Text":"Let\u0027s save it as string representation that will actually even work for Boolean."},{"Start":"02:41.120 ","End":"02:43.505","Text":"So if we put a value true or false,"},{"Start":"02:43.505 ","End":"02:46.250","Text":"they will be encoded as strings as well."},{"Start":"02:46.250 ","End":"02:48.590","Text":"What if we put a letter in here?"},{"Start":"02:48.590 ","End":"02:50.090","Text":"Doesn\u0027t know what to do,"},{"Start":"02:50.090 ","End":"02:52.745","Text":"can\u0027t find symbols, what it says variable."},{"Start":"02:52.745 ","End":"02:55.270","Text":"It\u0027s assuming it\u0027s a variable,"},{"Start":"02:55.270 ","End":"02:57.520","Text":"or let\u0027s try and put a variable in there."},{"Start":"02:57.520 ","End":"03:01.310","Text":"If we create an integer variable called a and put 1,"},{"Start":"03:01.310 ","End":"03:02.705","Text":"2, 3 into it."},{"Start":"03:02.705 ","End":"03:04.535","Text":"Let\u0027s try that again."},{"Start":"03:04.535 ","End":"03:08.160","Text":"String.value of a now it\u0027s successful,"},{"Start":"03:08.160 ","End":"03:11.390","Text":"it takes the contents of the variable and turns it into a string."},{"Start":"03:11.390 ","End":"03:17.690","Text":"A very useful little function that we can use in this exercise."},{"Start":"03:17.690 ","End":"03:24.965","Text":"In Part b, we\u0027ve been asked to create a method called quadratic,"},{"Start":"03:24.965 ","End":"03:28.710","Text":"and it returns a string,"},{"Start":"03:28.710 ","End":"03:35.520","Text":"so we put string in front with S and there are 3 parameters,"},{"Start":"03:35.520 ","End":"03:38.093","Text":"all doubles: aa, bb,"},{"Start":"03:38.093 ","End":"03:44.405","Text":"and cc, and then we\u0027re asked in Part c to return the values of"},{"Start":"03:44.405 ","End":"03:47.675","Text":"x using the formula given"},{"Start":"03:47.675 ","End":"03:52.445","Text":"and because there\u0027s a plus or minus symbol is going to be 2 solutions."},{"Start":"03:52.445 ","End":"03:55.100","Text":"But if b^2 minus 4ac,"},{"Start":"03:55.100 ","End":"03:56.480","Text":"which is called the discriminator,"},{"Start":"03:56.480 ","End":"04:00.350","Text":"is equal to 0, then both the solutions will be the same,"},{"Start":"04:00.350 ","End":"04:02.570","Text":"so we can just return 1."},{"Start":"04:02.570 ","End":"04:07.445","Text":"It would probably make sense given that it\u0027s mentioned elsewhere in the question as well,"},{"Start":"04:07.445 ","End":"04:11.165","Text":"b^2 minus 4ac to actually store that locally."},{"Start":"04:11.165 ","End":"04:14.510","Text":"Let\u0027s create a variable called d to contain"},{"Start":"04:14.510 ","End":"04:20.390","Text":"the discriminator and let\u0027s get b^2 minus 4ac and store it in there."},{"Start":"04:20.390 ","End":"04:24.410","Text":"We need math.pow in order to square"},{"Start":"04:24.410 ","End":"04:29.675","Text":"b. I put b as the first argument and then 2 is the second one."},{"Start":"04:29.675 ","End":"04:31.730","Text":"Now we\u0027ve got b^2,"},{"Start":"04:31.730 ","End":"04:35.100","Text":"but we want to take away 4ac,"},{"Start":"04:35.100 ","End":"04:36.525","Text":"so that\u0027s 4 times"},{"Start":"04:36.525 ","End":"04:42.140","Text":"a times c and I\u0027ve put in brackets and this will be a theme in this question."},{"Start":"04:42.140 ","End":"04:44.930","Text":"We do need to be very careful about how we evaluate things."},{"Start":"04:44.930 ","End":"04:46.430","Text":"This probably would\u0027ve been okay,"},{"Start":"04:46.430 ","End":"04:48.170","Text":"but in some cases,"},{"Start":"04:48.170 ","End":"04:51.875","Text":"with operators of equal precedence like divide and multiply,"},{"Start":"04:51.875 ","End":"04:53.650","Text":"you\u0027re not quite sure what\u0027s going to happen."},{"Start":"04:53.650 ","End":"04:56.870","Text":"It\u0027 so it\u0027s always better to put some brackets around"},{"Start":"04:56.870 ","End":"05:00.920","Text":"any expressions that you want to force to be evaluated first."},{"Start":"05:00.920 ","End":"05:05.060","Text":"There\u0027s a discriminator now stored in d,"},{"Start":"05:05.060 ","End":"05:07.370","Text":"and we can use that and that\u0027s fine out there\u0027s"},{"Start":"05:07.370 ","End":"05:11.480","Text":"1 solution or 2 solutions as it is also still in Part c_1?"},{"Start":"05:11.480 ","End":"05:18.140","Text":"But actually we could do c_2 and 3 first be a bit more efficient for us to do that."},{"Start":"05:18.140 ","End":"05:21.740","Text":"That\u0027s because, remember, return will stop"},{"Start":"05:21.740 ","End":"05:26.105","Text":"execution and not go any further if it\u0027s encountered."},{"Start":"05:26.105 ","End":"05:29.675","Text":"Why bother calculating something if it can\u0027t be calculated?"},{"Start":"05:29.675 ","End":"05:36.660","Text":"Let\u0027s get the b^2 minus 4ac check being equal to 0 done first."},{"Start":"05:36.660 ","End":"05:46.190","Text":"If d=0, we do one thing and that\u0027s for the 2 different solutions."},{"Start":"05:46.190 ","End":"05:55.565","Text":"But what we want to check before that is if d is less than 0, because if it is,"},{"Start":"05:55.565 ","End":"05:58.955","Text":"we want to quit right there and we want to return"},{"Start":"05:58.955 ","End":"06:03.005","Text":"complex roots not supported as we\u0027d have imaginary numbers"},{"Start":"06:03.005 ","End":"06:06.505","Text":"involved so let\u0027s not go there and we might as well do"},{"Start":"06:06.505 ","End":"06:11.690","Text":"Part c_3 head if the value of a or c passed in was 0,"},{"Start":"06:11.690 ","End":"06:13.700","Text":"we return the string invalid input."},{"Start":"06:13.700 ","End":"06:19.360","Text":"You can\u0027t have a as 0 or c is 0 in a quadratic equation."},{"Start":"06:19.360 ","End":"06:24.800","Text":"Let\u0027s deal with those inputs if they were passed in as 0."},{"Start":"06:24.800 ","End":"06:29.864","Text":"If a is 0 or c is 0,"},{"Start":"06:29.864 ","End":"06:33.630","Text":"then we return invalid inputs."},{"Start":"06:33.630 ","End":"06:38.670","Text":"We\u0027ll never get to this line here if either of these 2 lines run,"},{"Start":"06:38.670 ","End":"06:41.475","Text":"we\u0027ll never get to this line in fact, if this one runs."},{"Start":"06:41.475 ","End":"06:46.340","Text":"It\u0027s worth getting them out of the way to save the CPU doing anymore instructions,"},{"Start":"06:46.340 ","End":"06:51.570","Text":"because we can\u0027t calculate anything without them so let\u0027s put them first."},{"Start":"06:51.570 ","End":"06:55.050","Text":"There we go then. If d is 0,"},{"Start":"06:55.050 ","End":"06:58.210","Text":"we do one thing otherwise,"},{"Start":"06:58.210 ","End":"06:59.690","Text":"we do another thing."},{"Start":"06:59.690 ","End":"07:01.850","Text":"What is the two things that we want to do?"},{"Start":"07:01.850 ","End":"07:04.055","Text":"Well if d is 0,"},{"Start":"07:04.055 ","End":"07:08.660","Text":"the discriminant is 0, then there is only 1 solution."},{"Start":"07:08.660 ","End":"07:17.750","Text":"We are asked to return the solution in a particular way and we return, first of all,"},{"Start":"07:17.750 ","End":"07:23.420","Text":"it says x equals because it\u0027s only going to be 1 solution and then on the end of that,"},{"Start":"07:23.420 ","End":"07:26.870","Text":"we want to join the calculation that we need to"},{"Start":"07:26.870 ","End":"07:31.940","Text":"perform and because it\u0027s a calculation and we\u0027re going to join it to a string,"},{"Start":"07:31.940 ","End":"07:35.455","Text":"we need to use string.valueof,"},{"Start":"07:35.455 ","End":"07:40.430","Text":"as we\u0027ve just seen and then the calculation is going to go inside these brackets"},{"Start":"07:40.430 ","End":"07:46.970","Text":"here and that\u0027s the thing that will be returned on the end of x equals,"},{"Start":"07:46.970 ","End":"07:49.160","Text":"which is this little string here."},{"Start":"07:49.160 ","End":"07:52.280","Text":"The objective of this method is to return"},{"Start":"07:52.280 ","End":"07:55.730","Text":"a string that\u0027s because it could be an error message or it could"},{"Start":"07:55.730 ","End":"07:58.310","Text":"be a value and that\u0027s why we have to convert"},{"Start":"07:58.310 ","End":"08:02.240","Text":"here the calculation that we do into a string."},{"Start":"08:02.240 ","End":"08:06.440","Text":"The calculation is a little bit simpler here"},{"Start":"08:06.440 ","End":"08:11.405","Text":"because we eliminate square root of b^2 minus 4ac,"},{"Start":"08:11.405 ","End":"08:14.310","Text":"because b^2 minus 4ac is 0,"},{"Start":"08:14.310 ","End":"08:18.815","Text":"the square root of 0 is 0 so we can just leave that part of the equation."},{"Start":"08:18.815 ","End":"08:22.100","Text":"But we do need to do the first bit which is minus b."},{"Start":"08:22.100 ","End":"08:23.990","Text":"To do minus b we have to say"},{"Start":"08:23.990 ","End":"08:29.525","Text":"minus 1 times b and I\u0027m going to put that into some brackets,"},{"Start":"08:29.525 ","End":"08:34.140","Text":"so minus 1 times b divided by 2a."},{"Start":"08:34.140 ","End":"08:42.025","Text":"I need to put that in brackets as well to ensure that I don\u0027t just divide by 2,"},{"Start":"08:42.025 ","End":"08:45.620","Text":"all this stuff divided by 2 and then multiply the result by a,"},{"Start":"08:45.620 ","End":"08:48.020","Text":"I want to divide by 2a."},{"Start":"08:48.020 ","End":"08:54.050","Text":"That should be it for 1 solution."},{"Start":"08:54.050 ","End":"08:59.180","Text":"It\u0027s a nice simple formula because we eliminated the part that says square root"},{"Start":"08:59.180 ","End":"09:04.790","Text":"of b^2 minus 4ac and now we can do the part where there are 2 solutions."},{"Start":"09:04.790 ","End":"09:07.220","Text":"It\u0027s going to start in the same way,"},{"Start":"09:07.220 ","End":"09:10.280","Text":"x equals as it\u0027s asked us to do in"},{"Start":"09:10.280 ","End":"09:16.085","Text":"the question and then this time we\u0027re going to have the full formula."},{"Start":"09:16.085 ","End":"09:19.040","Text":"Let\u0027s put some space in here."},{"Start":"09:19.040 ","End":"09:23.375","Text":"Again, we want minus 1 times b,"},{"Start":"09:23.375 ","End":"09:29.445","Text":"but we also want now to add the square root."},{"Start":"09:29.445 ","End":"09:36.140","Text":"I do math.squareroot of d and then let\u0027s put brackets around all of"},{"Start":"09:36.140 ","End":"09:43.355","Text":"this because all of that is then to be divided by 2a so it has to be brackets around it."},{"Start":"09:43.355 ","End":"09:46.895","Text":"It won\u0027t calculate the correct answer."},{"Start":"09:46.895 ","End":"09:53.630","Text":"There\u0027s the first solution so minus b plus square root of"},{"Start":"09:53.630 ","End":"10:00.805","Text":"the discriminant divided by 2a and then the second part,"},{"Start":"10:00.805 ","End":"10:06.705","Text":"b, let\u0027s put a space in here as well and I can just actually copy this here."},{"Start":"10:06.705 ","End":"10:10.460","Text":"It\u0027s going to be exactly the same instead of a plus symbol here."},{"Start":"10:10.460 ","End":"10:12.245","Text":"It\u0027s going to be a minus symbol."},{"Start":"10:12.245 ","End":"10:16.815","Text":"To save myself some errors potentially and typing,"},{"Start":"10:16.815 ","End":"10:19.250","Text":"I\u0027ll put that there and that\u0027s it."},{"Start":"10:19.250 ","End":"10:24.290","Text":"We just need a semicolon on the end of that line and that should be it."},{"Start":"10:24.290 ","End":"10:30.880","Text":"It\u0027s complaining about something and that\u0027s because I\u0027ve missed a plus symbol"},{"Start":"10:30.880 ","End":"10:37.660","Text":"here I\u0027m joining this string to the results of this calculation,"},{"Start":"10:37.660 ","End":"10:42.355","Text":"then another string here and the results of that calculation at the end."},{"Start":"10:42.355 ","End":"10:47.440","Text":"It\u0027s 4 little bits to this overall string that\u0027s being returned."},{"Start":"10:47.440 ","End":"10:51.280","Text":"I think that\u0027s it because we\u0027ve done c_1,"},{"Start":"10:51.280 ","End":"10:52.495","Text":"2, and 3."},{"Start":"10:52.495 ","End":"10:56.155","Text":"Now we can test whether this does what we expect it to do."},{"Start":"10:56.155 ","End":"11:01.525","Text":"Let\u0027s give it the values that we\u0027re asked to in Part d, d_1."},{"Start":"11:01.525 ","End":"11:03.445","Text":"It says a is 1,"},{"Start":"11:03.445 ","End":"11:05.515","Text":"b is minus 6,"},{"Start":"11:05.515 ","End":"11:09.940","Text":"and c is 5 and that\u0027ll give us 1 and 5,"},{"Start":"11:09.940 ","End":"11:13.110","Text":"let\u0027s see, Oops,"},{"Start":"11:13.110 ","End":"11:15.975","Text":"and it does, 5 there and 1 there."},{"Start":"11:15.975 ","End":"11:18.950","Text":"That\u0027s good. Let\u0027s test the next one."},{"Start":"11:18.950 ","End":"11:21.020","Text":"d_2 are asked to put 2,"},{"Start":"11:21.020 ","End":"11:22.640","Text":"4, and 2 for a, b,"},{"Start":"11:22.640 ","End":"11:26.205","Text":"and c respectively and we should get minus 1 and great,"},{"Start":"11:26.205 ","End":"11:31.295","Text":"we do get minus 1 and this notice is there\u0027s only 1 solution here."},{"Start":"11:31.295 ","End":"11:35.420","Text":"This would have kicked in to the discriminant is 0,"},{"Start":"11:35.420 ","End":"11:40.065","Text":"then the last one to check is 3, 5,"},{"Start":"11:40.065 ","End":"11:42.470","Text":"and 3 and it does give us"},{"Start":"11:42.470 ","End":"11:46.895","Text":"complex roots not supported because we\u0027ve got discriminant less than 0."},{"Start":"11:46.895 ","End":"11:50.045","Text":"We have been also asked actually to check with"},{"Start":"11:50.045 ","End":"11:55.710","Text":"the final part in d_4 with any value of b but either a is 0,"},{"Start":"11:55.710 ","End":"11:58.935","Text":"c is 0 we should get an invalid input message."},{"Start":"11:58.935 ","End":"12:06.740","Text":"Let us put a is 0, then the value of b and 2 for c and we do get invalid inputs."},{"Start":"12:06.740 ","End":"12:09.860","Text":"I\u0027m going to check to see as well, which I\u0027m sure it well,"},{"Start":"12:09.860 ","End":"12:15.400","Text":"let\u0027s just be complete 1 and 2 and 0 there and it\u0027s done."},{"Start":"12:15.400 ","End":"12:18.410","Text":"An interesting exercise, that one, because, I mean,"},{"Start":"12:18.410 ","End":"12:22.235","Text":"there\u0027s obviously quite a complex expression here but also,"},{"Start":"12:22.235 ","End":"12:27.650","Text":"we\u0027ve seen that if you do need to return a string with numbers in it, you can\u0027t do that."},{"Start":"12:27.650 ","End":"12:31.774","Text":"This really needs that because if the numbers that you input"},{"Start":"12:31.774 ","End":"12:37.205","Text":"were invalid or your function is not going to support complex roots,"},{"Start":"12:37.205 ","End":"12:41.630","Text":"then you\u0027re going to return this message otherwise you will return the calculation,"},{"Start":"12:41.630 ","End":"12:45.515","Text":"but there\u0027s going to be some text around it as well as there was here."},{"Start":"12:45.515 ","End":"12:51.665","Text":"It\u0027s a quite useful technique to be able to convert a number into a string."},{"Start":"12:51.665 ","End":"12:54.960","Text":"That\u0027s it for this one. I\u0027ll see you in the next one."}],"ID":29174},{"Watched":false,"Name":"Exercise 5","Duration":"15m 48s","ChapterTopicVideoID":27691,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.540","Text":"Hey again everyone. In this exercise we\u0027ve been asked to"},{"Start":"00:03.540 ","End":"00:06.825","Text":"bring together several methods to solve one overall problem."},{"Start":"00:06.825 ","End":"00:09.660","Text":"In Part a, we use the code path to experiment with"},{"Start":"00:09.660 ","End":"00:12.540","Text":"a couple of methods to check characters."},{"Start":"00:12.540 ","End":"00:16.110","Text":"The first one being isLowerCase and we check it with the"},{"Start":"00:16.110 ","End":"00:20.040","Text":"letter a in single quotes and in round brackets here."},{"Start":"00:20.040 ","End":"00:22.665","Text":"In Part 2 isDigit."},{"Start":"00:22.665 ","End":"00:27.210","Text":"Again in round brackets and a single quote character 2."},{"Start":"00:27.210 ","End":"00:30.420","Text":"In Part b, we use the Codepad to see how a string can be"},{"Start":"00:30.420 ","End":"00:33.855","Text":"checked for a specific character like so."},{"Start":"00:33.855 ","End":"00:36.015","Text":"In Part 1, string space,"},{"Start":"00:36.015 ","End":"00:40.730","Text":"list of chars equals speech marks and then a number of symbols here,"},{"Start":"00:40.730 ","End":"00:42.725","Text":"dollar percent, caret,"},{"Start":"00:42.725 ","End":"00:45.035","Text":"and asterisks, close quotes."},{"Start":"00:45.035 ","End":"00:47.120","Text":"We can also use in Part b too"},{"Start":"00:47.120 ","End":"00:50.660","Text":"listofChars.contains pair of brackets"},{"Start":"00:50.660 ","End":"00:53.930","Text":"and then inside the brackets we have string dot value of,"},{"Start":"00:53.930 ","End":"00:58.955","Text":"another inner brackets pair and single quotes and a percent character there."},{"Start":"00:58.955 ","End":"01:02.420","Text":"In Part c, we create a method called hasUpperCase,"},{"Start":"01:02.420 ","End":"01:05.630","Text":"which accepts a string argument and returns a Boolean."},{"Start":"01:05.630 ","End":"01:08.210","Text":"In Part 1, we loop through each character"},{"Start":"01:08.210 ","End":"01:11.240","Text":"in the input string looking for uppercase letters."},{"Start":"01:11.240 ","End":"01:16.010","Text":"In Part 2, we return true only if the string contains at least one character,"},{"Start":"01:16.010 ","End":"01:17.615","Text":"which is an uppercase letter."},{"Start":"01:17.615 ","End":"01:20.240","Text":"Otherwise we return false."},{"Start":"01:20.240 ","End":"01:23.990","Text":"In Part d, we create another method called hasLowerCase,"},{"Start":"01:23.990 ","End":"01:29.225","Text":"which returns true only if a string passed into it contains at least one character,"},{"Start":"01:29.225 ","End":"01:31.130","Text":"which is a lowercase letter."},{"Start":"01:31.130 ","End":"01:33.335","Text":"In Part e, we do the same again,"},{"Start":"01:33.335 ","End":"01:36.590","Text":"but we call a method hasDigit which returns true"},{"Start":"01:36.590 ","End":"01:40.220","Text":"only if a string passed into it contains at least one character,"},{"Start":"01:40.220 ","End":"01:43.395","Text":"whereas a digit between 0 and."},{"Start":"01:43.395 ","End":"01:45.635","Text":"In Part f, once again,"},{"Start":"01:45.635 ","End":"01:46.865","Text":"we do the same thing,"},{"Start":"01:46.865 ","End":"01:50.840","Text":"but the methods called hasSymbol and it returns true and if it"},{"Start":"01:50.840 ","End":"01:54.870","Text":"contains one of these symbols listed inside the speech marks here."},{"Start":"01:54.870 ","End":"01:58.879","Text":"In Part g, we create one final method called validatePassword,"},{"Start":"01:58.879 ","End":"02:01.955","Text":"which is passed the string and returns true"},{"Start":"02:01.955 ","End":"02:05.480","Text":"only if the string contains at least one uppercase character,"},{"Start":"02:05.480 ","End":"02:09.490","Text":"one lowercase character, one digit, and one symbol."},{"Start":"02:09.490 ","End":"02:14.780","Text":"It uses those three methods created above in Part c to f. Finally,"},{"Start":"02:14.780 ","End":"02:23.120","Text":"we test with input passwords of Secret7 and an exclamation mark which should return true."},{"Start":"02:23.120 ","End":"02:28.265","Text":"Then ABCd$ should return false."},{"Start":"02:28.265 ","End":"02:35.990","Text":"Week1 should return false and password1,"},{"Start":"02:35.990 ","End":"02:40.040","Text":"and an exclamation mark should also return false."},{"Start":"02:40.040 ","End":"02:45.020","Text":"Part a once again, gets us to use the Codepad to explore"},{"Start":"02:45.020 ","End":"02:49.640","Text":"some of the methods available that we\u0027ll need in this exercise."},{"Start":"02:49.640 ","End":"02:55.640","Text":"This one is using the character class in Java,"},{"Start":"02:55.640 ","End":"03:00.650","Text":"which gives us various functions that allow us to"},{"Start":"03:00.650 ","End":"03:06.020","Text":"check if the characters are uppercase or lowercase or digits."},{"Start":"03:06.020 ","End":"03:08.269","Text":"Character isLowerCase."},{"Start":"03:08.269 ","End":"03:12.005","Text":"Then I supply with a lowercase a gives me true."},{"Start":"03:12.005 ","End":"03:14.285","Text":"By changing it to an uppercase a,"},{"Start":"03:14.285 ","End":"03:16.205","Text":"it will give me false."},{"Start":"03:16.205 ","End":"03:19.535","Text":"I can do the same with uppercase."},{"Start":"03:19.535 ","End":"03:22.204","Text":"It\u0027s true obviously in that case."},{"Start":"03:22.204 ","End":"03:25.565","Text":"False, If I put a lowercase one in."},{"Start":"03:25.565 ","End":"03:29.000","Text":"What about is digit?"},{"Start":"03:29.000 ","End":"03:32.600","Text":"Letter a is not a digit, so expect false."},{"Start":"03:32.600 ","End":"03:35.760","Text":"But the number 2 is,"},{"Start":"03:35.760 ","End":"03:38.115","Text":"I get true. That\u0027s Part a."},{"Start":"03:38.115 ","End":"03:44.510","Text":"It\u0027s useful to look at some of the methods available to work with characters."},{"Start":"03:44.510 ","End":"03:52.355","Text":"Then we\u0027ve been asked to create a string variable called list of chars,"},{"Start":"03:52.355 ","End":"04:00.410","Text":"which has some symbols in there that we\u0027re going to search for any of these symbols."},{"Start":"04:00.410 ","End":"04:03.275","Text":"We want to know if they\u0027re in there."},{"Start":"04:03.275 ","End":"04:08.005","Text":"The method we\u0027ve been asked to use is contains."},{"Start":"04:08.005 ","End":"04:10.840","Text":"All strings have a method called"},{"Start":"04:10.840 ","End":"04:16.255","Text":"contains and we can pass to contains per single character."},{"Start":"04:16.255 ","End":"04:19.119","Text":"If I pass it the percent, for example,"},{"Start":"04:19.119 ","End":"04:22.794","Text":"actually probably give me an error because it\u0027s a char."},{"Start":"04:22.794 ","End":"04:26.200","Text":"Whereas I need to put in a string. There we go."},{"Start":"04:26.200 ","End":"04:31.135","Text":"Now it\u0027s true if I put symbol that doesn\u0027t exist like pound symbol in my string,"},{"Start":"04:31.135 ","End":"04:32.755","Text":"then it gives me false."},{"Start":"04:32.755 ","End":"04:37.274","Text":"Just to go back to this idea of the string versus a character,"},{"Start":"04:37.274 ","End":"04:44.300","Text":"I previously used the string.valueof method to turn a number into a string."},{"Start":"04:44.300 ","End":"04:47.825","Text":"We can use it to turn a char into a string as well."},{"Start":"04:47.825 ","End":"04:50.150","Text":"If I put, for example,"},{"Start":"04:50.150 ","End":"04:53.345","Text":"percent in here, now,"},{"Start":"04:53.345 ","End":"04:56.840","Text":"that should be okay, what\u0027s happened there?"},{"Start":"04:56.840 ","End":"04:59.672","Text":"Cannot find method value."},{"Start":"04:59.672 ","End":"05:04.475","Text":"It\u0027s because I\u0027ve missed a lowercase and it should be an uppercase. There we go."},{"Start":"05:04.475 ","End":"05:08.599","Text":"It has found this string which has been converted"},{"Start":"05:08.599 ","End":"05:13.615","Text":"from a char into a string inside the other string."},{"Start":"05:13.615 ","End":"05:16.835","Text":"Any of the chars I put in here,"},{"Start":"05:16.835 ","End":"05:21.050","Text":"if they appeared in the original list of chars here in this string,"},{"Start":"05:21.050 ","End":"05:24.110","Text":"then it should put a star and see if that works."},{"Start":"05:24.110 ","End":"05:27.275","Text":"I get true. That\u0027s a and b done,"},{"Start":"05:27.275 ","End":"05:30.410","Text":"and it\u0027s allowed me to just experiment with those methods."},{"Start":"05:30.410 ","End":"05:37.705","Text":"Now I can create some methods to automate the check."},{"Start":"05:37.705 ","End":"05:42.830","Text":"The first one we\u0027re going to call hasUppercase."},{"Start":"05:42.830 ","End":"05:46.400","Text":"That returns a Boolean value depending on"},{"Start":"05:46.400 ","End":"05:50.450","Text":"whether there is an uppercase letter in there or not."},{"Start":"05:50.450 ","End":"05:54.019","Text":"The parameter is going to be a string."},{"Start":"05:54.019 ","End":"05:57.794","Text":"Let\u0027s call it to check because that\u0027s the string we\u0027re going to check."},{"Start":"05:57.794 ","End":"05:59.965","Text":"We need to check."},{"Start":"05:59.965 ","End":"06:01.720","Text":"What do we need to do here?"},{"Start":"06:01.720 ","End":"06:06.505","Text":"We need to loop through each character in the input string looking for uppercase letters."},{"Start":"06:06.505 ","End":"06:09.220","Text":"We know how to do a loop."},{"Start":"06:09.220 ","End":"06:13.489","Text":"We\u0027re looking to go to the end of the string."},{"Start":"06:13.620 ","End":"06:16.390","Text":"There\u0027s no more characters to check."},{"Start":"06:16.390 ","End":"06:20.380","Text":"We go up to but not including length."},{"Start":"06:20.380 ","End":"06:27.520","Text":"Then we add one to i so we can start the next index value."},{"Start":"06:27.520 ","End":"06:30.910","Text":"Then what we\u0027re actually doing inside the loop is we\u0027re going"},{"Start":"06:30.910 ","End":"06:34.780","Text":"to return true only if the string contains at least one character,"},{"Start":"06:34.780 ","End":"06:35.950","Text":"which is uppercase,"},{"Start":"06:35.950 ","End":"06:37.615","Text":"otherwise we return false."},{"Start":"06:37.615 ","End":"06:42.550","Text":"What we\u0027re going to do is we\u0027re going to check if the current thing that we\u0027re looking"},{"Start":"06:42.550 ","End":"06:48.099","Text":"at is equal to a character, which isLowerCase."},{"Start":"06:48.099 ","End":"06:53.645","Text":"If character.isUppercase."},{"Start":"06:53.645 ","End":"06:56.650","Text":"Then in the brackets here is the thing that we\u0027re"},{"Start":"06:56.650 ","End":"07:00.535","Text":"going to check whether it\u0027s uppercase or not."},{"Start":"07:00.535 ","End":"07:09.647","Text":"Of course we get a single character by using charAt and then the index."},{"Start":"07:09.647 ","End":"07:12.765","Text":"This will get a single character from"},{"Start":"07:12.765 ","End":"07:19.635","Text":"the string toCheck and it will pass that to the isUpperCase function."},{"Start":"07:19.635 ","End":"07:22.695","Text":"If it is an uppercase letter,"},{"Start":"07:22.695 ","End":"07:24.180","Text":"it will return true."},{"Start":"07:24.180 ","End":"07:26.310","Text":"This if will become true."},{"Start":"07:26.310 ","End":"07:34.095","Text":"Then i can return the value true to the calling code."},{"Start":"07:34.095 ","End":"07:36.150","Text":"That\u0027s all I need to do,"},{"Start":"07:36.150 ","End":"07:38.535","Text":"apart from when it ends,"},{"Start":"07:38.535 ","End":"07:45.300","Text":"I need to make sure that the loop presented didn\u0027t find an uppercase character,"},{"Start":"07:45.300 ","End":"07:47.310","Text":"so I have to return false."},{"Start":"07:47.310 ","End":"07:49.290","Text":"It\u0027s worth just checking before we go any"},{"Start":"07:49.290 ","End":"07:52.320","Text":"further whether that does what we want it to do."},{"Start":"07:52.320 ","End":"07:55.320","Text":"Let\u0027s run it and see if it does."},{"Start":"07:55.320 ","End":"07:56.790","Text":"It has uppercase."},{"Start":"07:56.790 ","End":"07:59.393","Text":"Let\u0027s pass it a string ABCDE."},{"Start":"07:59.393 ","End":"08:00.570","Text":"They\u0027re all uppercase."},{"Start":"08:00.570 ","End":"08:03.150","Text":"I hope that would work. It gives me true."},{"Start":"08:03.150 ","End":"08:06.285","Text":"Let\u0027s try it with something a little trickier."},{"Start":"08:06.285 ","End":"08:09.270","Text":"Let\u0027s do abcDef."},{"Start":"08:09.270 ","End":"08:11.820","Text":"That works as well."},{"Start":"08:11.820 ","End":"08:15.160","Text":"Let\u0027s try all lowercase."},{"Start":"08:16.460 ","End":"08:19.740","Text":"It shouldn\u0027t return true. It should return false and it does."},{"Start":"08:19.740 ","End":"08:22.680","Text":"That\u0027s part C done."},{"Start":"08:22.680 ","End":"08:29.879","Text":"We could do exactly the same thing for part E with very little change."},{"Start":"08:29.879 ","End":"08:36.135","Text":"I\u0027m just going to copy it actually and just change a couple of lines where I need to."},{"Start":"08:36.135 ","End":"08:40.230","Text":"This method is going to be called hasLowerCase."},{"Start":"08:40.230 ","End":"08:42.390","Text":"What we\u0027ll do is we\u0027ll use"},{"Start":"08:42.390 ","End":"08:49.755","Text":"the isLowerCase method rather than isUpperCase in the character class."},{"Start":"08:49.755 ","End":"08:52.403","Text":"That is done."},{"Start":"08:52.403 ","End":"08:53.835","Text":"That\u0027ll do exactly what this does,"},{"Start":"08:53.835 ","End":"08:55.410","Text":"but it\u0027s only going to return true if there\u0027s"},{"Start":"08:55.410 ","End":"08:58.740","Text":"a lowercase letter somewhere in the input string."},{"Start":"08:58.740 ","End":"09:00.834","Text":"Then we can do the same thing again,"},{"Start":"09:00.834 ","End":"09:04.560","Text":"but check this time for a digit."},{"Start":"09:04.560 ","End":"09:11.565","Text":"This would be part E. We\u0027ve been asked to call that hasDigit."},{"Start":"09:11.565 ","End":"09:17.025","Text":"We use the character method isDigit in this case."},{"Start":"09:17.025 ","End":"09:21.570","Text":"Let\u0027s just check that it\u0027s compiling and it seems to be fine."},{"Start":"09:21.570 ","End":"09:25.575","Text":"We have the 3 methods that we needed."},{"Start":"09:25.575 ","End":"09:28.215","Text":"What we\u0027re going to do now is"},{"Start":"09:28.215 ","End":"09:33.795","Text":"the final method we\u0027ve been asked to create is called hasSymbol."},{"Start":"09:33.795 ","End":"09:36.645","Text":"This is slightly different."},{"Start":"09:36.645 ","End":"09:39.690","Text":"The general format is the same."},{"Start":"09:39.690 ","End":"09:42.435","Text":"We still need a loop."},{"Start":"09:42.435 ","End":"09:48.555","Text":"This time the loop is going to need to work through a string,"},{"Start":"09:48.555 ","End":"09:50.715","Text":"which contains some symbols."},{"Start":"09:50.715 ","End":"09:53.160","Text":"Let\u0027s define those symbols."},{"Start":"09:53.160 ","End":"09:55.845","Text":"The symbols we\u0027ve been asked to use are;"},{"Start":"09:55.845 ","End":"09:59.250","Text":"an exclamation mark, dollar, per cent,"},{"Start":"09:59.250 ","End":"10:02.895","Text":"caret, ampersand, asterisks,"},{"Start":"10:02.895 ","End":"10:06.960","Text":"brackets, dot, and question mark."},{"Start":"10:06.960 ","End":"10:11.385","Text":"Any of those symbols are the ones that we\u0027re looking to find."},{"Start":"10:11.385 ","End":"10:16.723","Text":"Again, we can go through the string looking at each character,"},{"Start":"10:16.723 ","End":"10:19.650","Text":"but what we\u0027re going to do this time is we\u0027re going"},{"Start":"10:19.650 ","End":"10:30.420","Text":"to use the string name and name contains."},{"Start":"10:30.420 ","End":"10:32.100","Text":"Actually, symbols,"},{"Start":"10:32.100 ","End":"10:34.740","Text":"we should be checking through,"},{"Start":"10:34.740 ","End":"10:38.475","Text":"so symbols.length because we\u0027re going to check"},{"Start":"10:38.475 ","End":"10:44.310","Text":"a whole string and we\u0027re going to check if any of these symbols are in a string."},{"Start":"10:44.310 ","End":"10:53.670","Text":"ToCheck.contains symbols.charAt, whatever the current symbol is we\u0027re looking at."},{"Start":"10:53.670 ","End":"10:55.710","Text":"If to check contains this symbol,"},{"Start":"10:55.710 ","End":"10:57.420","Text":"or the symbol, or this symbol,"},{"Start":"10:57.420 ","End":"10:59.205","Text":"or this symbol, and so on."},{"Start":"10:59.205 ","End":"11:00.795","Text":"We\u0027ve got a match."},{"Start":"11:00.795 ","End":"11:06.090","Text":"We need to put an if statement and if it has matched, we return true."},{"Start":"11:06.090 ","End":"11:08.610","Text":"If it comes to this point here,"},{"Start":"11:08.610 ","End":"11:11.745","Text":"then it has not found a match."},{"Start":"11:11.745 ","End":"11:13.695","Text":"Just to summarize that one again,"},{"Start":"11:13.695 ","End":"11:15.450","Text":"is it\u0027s slightly different to what we had before."},{"Start":"11:15.450 ","End":"11:18.840","Text":"This time we\u0027re been passed a string and we\u0027re checking"},{"Start":"11:18.840 ","End":"11:20.940","Text":"the entire string to see if it"},{"Start":"11:20.940 ","End":"11:24.257","Text":"contains an individual symbol and this will do it in one go,"},{"Start":"11:24.257 ","End":"11:26.640","Text":"but this symbols that we\u0027re checking,"},{"Start":"11:26.640 ","End":"11:27.675","Text":"we check one at a time;"},{"Start":"11:27.675 ","End":"11:31.260","Text":"that symbol or that symbol or that one, and so on."},{"Start":"11:31.260 ","End":"11:32.625","Text":"That\u0027s why we still need a loop."},{"Start":"11:32.625 ","End":"11:35.940","Text":"This time we\u0027re iterating through the symbol string,"},{"Start":"11:35.940 ","End":"11:38.835","Text":"not iterating through the toCheck string."},{"Start":"11:38.835 ","End":"11:40.620","Text":"A slight variation,"},{"Start":"11:40.620 ","End":"11:43.530","Text":"but it works on the same principle."},{"Start":"11:43.530 ","End":"11:52.309","Text":"This is a char and I can\u0027t compare a string to a char."},{"Start":"11:52.309 ","End":"11:53.930","Text":"I\u0027m going to compare it to another string."},{"Start":"11:53.930 ","End":"11:56.300","Text":"This is checking whether it contains a sub-string."},{"Start":"11:56.300 ","End":"12:00.187","Text":"The sub-string can be just 1 character long or 1 item long,"},{"Start":"12:00.187 ","End":"12:04.285","Text":"but I do need to use the method that we used earlier,"},{"Start":"12:04.285 ","End":"12:11.285","Text":"which is string.valueOf to turn this char into a string,"},{"Start":"12:11.285 ","End":"12:13.160","Text":"I\u0027ve got a lot of brackets here."},{"Start":"12:13.160 ","End":"12:15.635","Text":"Let\u0027s just see where they\u0027re all coming from."},{"Start":"12:15.635 ","End":"12:18.500","Text":"Symbols.charAt is the inner brackets,"},{"Start":"12:18.500 ","End":"12:21.506","Text":"the valueOf is that bracket there,"},{"Start":"12:21.506 ","End":"12:26.655","Text":"then this bracket is for the string function,"},{"Start":"12:26.655 ","End":"12:28.065","Text":"this one here,"},{"Start":"12:28.065 ","End":"12:30.585","Text":"and then this final one is for the if."},{"Start":"12:30.585 ","End":"12:35.340","Text":"Sometimes helps to space those out just as you can see where they are, but that\u0027s fine."},{"Start":"12:35.340 ","End":"12:37.905","Text":"Now that should work without a problem."},{"Start":"12:37.905 ","End":"12:42.450","Text":"The last thing to do now then is we\u0027ve been asked,"},{"Start":"12:42.450 ","End":"12:46.065","Text":"in part G, to create a method called validatePassword."},{"Start":"12:46.065 ","End":"12:48.300","Text":"This brings it all together."},{"Start":"12:48.300 ","End":"12:51.330","Text":"ValidatePassword is passed as a string."},{"Start":"12:51.330 ","End":"12:54.123","Text":"We can call it toCheck as we have before."},{"Start":"12:54.123 ","End":"12:58.320","Text":"Parameter names can be reused in different functions and methods."},{"Start":"12:58.320 ","End":"13:01.680","Text":"What we\u0027re going to do here is we\u0027re going to call all of"},{"Start":"13:01.680 ","End":"13:06.105","Text":"the different functions and we\u0027re going to check that all of them pass true."},{"Start":"13:06.105 ","End":"13:08.835","Text":"If they do, then we return true."},{"Start":"13:08.835 ","End":"13:15.270","Text":"If hasUpperCase and hasLowerCase"},{"Start":"13:15.270 ","End":"13:21.250","Text":"and hasDigit and hasSymbol,"},{"Start":"13:21.250 ","End":"13:24.540","Text":"if all of those things are true,"},{"Start":"13:24.540 ","End":"13:26.550","Text":"then we return true."},{"Start":"13:26.550 ","End":"13:28.245","Text":"We don\u0027t need an else."},{"Start":"13:28.245 ","End":"13:32.040","Text":"We just put return false because it will only ever get to"},{"Start":"13:32.040 ","End":"13:36.720","Text":"this line if one of these things is not true."},{"Start":"13:36.720 ","End":"13:38.160","Text":"This one I\u0027ve run,"},{"Start":"13:38.160 ","End":"13:43.605","Text":"then we\u0027ll get to here only if all of these are true,"},{"Start":"13:43.605 ","End":"13:45.045","Text":"we would return true."},{"Start":"13:45.045 ","End":"13:47.043","Text":"Otherwise, we\u0027ll get to this line here,"},{"Start":"13:47.043 ","End":"13:49.980","Text":"return false. Still 1 error here."},{"Start":"13:49.980 ","End":"13:54.960","Text":"Let\u0027s have a look. Cannot find symbol hasLowerCase."},{"Start":"13:54.960 ","End":"13:57.458","Text":"It\u0027s because the code it has lower."},{"Start":"13:57.458 ","End":"14:06.030","Text":"Or It\u0027s because I used a lowercase l here and I use an uppercase L there."},{"Start":"14:06.030 ","End":"14:08.535","Text":"That should do it now."},{"Start":"14:08.535 ","End":"14:16.367","Text":"Now I can test in part H. The first one we\u0027re going to try should work."},{"Start":"14:16.367 ","End":"14:21.945","Text":"Secret with a capital S and then a number 7 and a symbol."},{"Start":"14:21.945 ","End":"14:23.580","Text":"It\u0027s got everything I need."},{"Start":"14:23.580 ","End":"14:25.765","Text":"It does give me true."},{"Start":"14:25.765 ","End":"14:27.920","Text":"Let\u0027s try the next one,"},{"Start":"14:27.920 ","End":"14:34.040","Text":"which is combination of uppercase and lowercase letters is good."},{"Start":"14:34.040 ","End":"14:36.245","Text":"I\u0027ve got a symbol,"},{"Start":"14:36.245 ","End":"14:37.910","Text":"but I don\u0027t have a digit,"},{"Start":"14:37.910 ","End":"14:40.775","Text":"so it should give me false, and it does."},{"Start":"14:40.775 ","End":"14:43.230","Text":"Then I\u0027m going to try,"},{"Start":"14:43.230 ","End":"14:45.425","Text":"in H3, capital letter,"},{"Start":"14:45.425 ","End":"14:47.629","Text":"lowercase letters, and a digit,"},{"Start":"14:47.629 ","End":"14:52.280","Text":"but no symbol and that returns false as I would want it to."},{"Start":"14:52.280 ","End":"15:01.515","Text":"Then the final one is to have all lowercase characters and a number digit,"},{"Start":"15:01.515 ","End":"15:04.790","Text":"and a symbol, but I don\u0027t have an uppercase."},{"Start":"15:04.790 ","End":"15:07.835","Text":"That should fail as well and it does."},{"Start":"15:07.835 ","End":"15:10.895","Text":"Quite a long one that one."},{"Start":"15:10.895 ","End":"15:14.990","Text":"All very similar, but what it does demonstrate is"},{"Start":"15:14.990 ","End":"15:20.660","Text":"lots of methods can be called inside another method."},{"Start":"15:20.660 ","End":"15:24.190","Text":"This is quite readable when you look at it from here."},{"Start":"15:24.190 ","End":"15:28.880","Text":"If hasUpperCase and hasLowerCase and hasDigit and hasSymbol return true."},{"Start":"15:28.880 ","End":"15:31.700","Text":"It reads quite nicely and we don\u0027t have to look at"},{"Start":"15:31.700 ","End":"15:34.520","Text":"the detail of what\u0027s in the actual methods itself."},{"Start":"15:34.520 ","End":"15:37.370","Text":"That\u0027s obviously one of the big advantages"},{"Start":"15:37.370 ","End":"15:42.080","Text":"of making your code more modular and breaking it up into subroutines,"},{"Start":"15:42.080 ","End":"15:45.740","Text":"is that it does make your code more readable and reusable."},{"Start":"15:45.740 ","End":"15:49.050","Text":"That\u0027s it for this one. See you in the next one."}],"ID":29175},{"Watched":false,"Name":"Exercise 6","Duration":"9m 39s","ChapterTopicVideoID":27684,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.810","Text":"Hello, welcome to this video solution on functions."},{"Start":"00:03.810 ","End":"00:07.290","Text":"In this exercise, we\u0027re told that Java can take input from"},{"Start":"00:07.290 ","End":"00:11.580","Text":"the keyboard using methods in the java.util library."},{"Start":"00:11.580 ","End":"00:13.305","Text":"In Part A therefore,"},{"Start":"00:13.305 ","End":"00:15.944","Text":"at the top of the file above the class definition,"},{"Start":"00:15.944 ","End":"00:19.710","Text":"we\u0027re asked to add the line import java.util.Scanner."},{"Start":"00:19.710 ","End":"00:22.740","Text":"In Part B, we create a new method which returns"},{"Start":"00:22.740 ","End":"00:26.565","Text":"no values and has no arguments called passwordChecker."},{"Start":"00:26.565 ","End":"00:30.645","Text":"In Part C, we add the following lines into the passwordChecker method."},{"Start":"00:30.645 ","End":"00:35.040","Text":"The first one being Scanner in equals new Scanner(System.in),"},{"Start":"00:35.040 ","End":"00:37.670","Text":"and the second one is String password."},{"Start":"00:37.670 ","End":"00:41.540","Text":"In Part D, we\u0027re asked to use a do-while loop to repeatedly read"},{"Start":"00:41.540 ","End":"00:45.785","Text":"a string from the keyboard to store into the password variable."},{"Start":"00:45.785 ","End":"00:49.700","Text":"Using System.out.print enter a password to check,"},{"Start":"00:49.700 ","End":"00:53.165","Text":"and password equals in.next()."},{"Start":"00:53.165 ","End":"00:55.610","Text":"In Part E and still inside the loop,"},{"Start":"00:55.610 ","End":"00:57.665","Text":"we\u0027re asked to call the checkLengthOk(),"},{"Start":"00:57.665 ","End":"01:02.975","Text":"and validate password methods using password as the argument to both."},{"Start":"01:02.975 ","End":"01:05.480","Text":"If true is returned by both methods,"},{"Start":"01:05.480 ","End":"01:06.890","Text":"we output the message,"},{"Start":"01:06.890 ","End":"01:08.225","Text":"\"Password valid \","},{"Start":"01:08.225 ","End":"01:11.749","Text":"otherwise we output the message \"Password invalid.\""},{"Start":"01:11.749 ","End":"01:17.315","Text":"In Part F, we end the loop when the user types end the password."},{"Start":"01:17.315 ","End":"01:19.280","Text":"Then Part G is about testing."},{"Start":"01:19.280 ","End":"01:25.010","Text":"We test by running passwordChecker and entering Shed7 with a S,"},{"Start":"01:25.010 ","End":"01:26.300","Text":"and an exclamation mark,"},{"Start":"01:26.300 ","End":"01:28.860","Text":"which should give us invalid."},{"Start":"01:28.860 ","End":"01:32.465","Text":"Richest1% with a capital R, which should give us valid."},{"Start":"01:32.465 ","End":"01:35.270","Text":"Longest12 with a capital L,"},{"Start":"01:35.270 ","End":"01:36.890","Text":"which should be invalid,"},{"Start":"01:36.890 ","End":"01:40.400","Text":"and finally end, which should end the program."},{"Start":"01:40.400 ","End":"01:42.890","Text":"In this exercise for the first time,"},{"Start":"01:42.890 ","End":"01:46.610","Text":"we\u0027re going to use the keyboard to input some data,"},{"Start":"01:46.610 ","End":"01:48.240","Text":"which we haven\u0027t done up until now."},{"Start":"01:48.240 ","End":"01:51.785","Text":"That makes use of some library functions in Java."},{"Start":"01:51.785 ","End":"01:53.390","Text":"First thing we need to do,"},{"Start":"01:53.390 ","End":"01:59.310","Text":"and you\u0027re going to make use of any library functions is to import the library file,"},{"Start":"01:59.310 ","End":"02:01.910","Text":"and then use the line import,"},{"Start":"02:01.910 ","End":"02:05.150","Text":"and then the name of the library."},{"Start":"02:05.150 ","End":"02:08.525","Text":"They are all packaged up in a particular format,"},{"Start":"02:08.525 ","End":"02:11.885","Text":"which is separated with dots."},{"Start":"02:11.885 ","End":"02:18.320","Text":"This is the individual code that we want to import and it belongs to the util library,"},{"Start":"02:18.320 ","End":"02:21.035","Text":"which is part of the Java library set."},{"Start":"02:21.035 ","End":"02:24.860","Text":"That\u0027s the first part done as we have been asked to do."},{"Start":"02:24.860 ","End":"02:26.795","Text":"Now we need to add our code,"},{"Start":"02:26.795 ","End":"02:30.770","Text":"which is going to be called passwordChecker."},{"Start":"02:30.770 ","End":"02:33.980","Text":"Back to the old days of not returning a value."},{"Start":"02:33.980 ","End":"02:38.030","Text":"So we can put void there, put the identifier,"},{"Start":"02:38.030 ","End":"02:39.805","Text":"which is passwordChecker,"},{"Start":"02:39.805 ","End":"02:45.155","Text":"and empty brackets because there are no arguments for this."},{"Start":"02:45.155 ","End":"02:49.400","Text":"Part C asked me to add the following lines in."},{"Start":"02:49.400 ","End":"02:53.780","Text":"We\u0027re going to make use of the scanner library."},{"Start":"02:53.780 ","End":"03:00.725","Text":"The syntax here is object-oriented syntax to create an object."},{"Start":"03:00.725 ","End":"03:03.600","Text":"We haven\u0027t covered that explicitly yet,"},{"Start":"03:03.600 ","End":"03:06.094","Text":"it\u0027ll come in the next chapter,"},{"Start":"03:06.094 ","End":"03:09.230","Text":"but suffice to say it just allows us to use some of the methods that"},{"Start":"03:09.230 ","End":"03:13.655","Text":"are in the libraries to read from the keyboard."},{"Start":"03:13.655 ","End":"03:18.685","Text":"Then we\u0027re going to create a string called password."},{"Start":"03:18.685 ","End":"03:20.890","Text":"Then that\u0027s Part C done."},{"Start":"03:20.890 ","End":"03:24.650","Text":"Now we\u0027ve been asked to use a do-while loop to repeatedly read a string from"},{"Start":"03:24.650 ","End":"03:30.530","Text":"the keyboard and to store into the password variable."},{"Start":"03:30.530 ","End":"03:33.420","Text":"Let\u0027s do that. Do-while loop,"},{"Start":"03:33.420 ","End":"03:37.330","Text":"you remember, looks like this."},{"Start":"03:38.210 ","End":"03:42.530","Text":"The braces there are the block that\u0027s going to repeat."},{"Start":"03:42.530 ","End":"03:49.045","Text":"The while clause is a condition that stops the loop continuing on."},{"Start":"03:49.045 ","End":"03:50.835","Text":"While this is true,"},{"Start":"03:50.835 ","End":"03:53.630","Text":"it will continue soon as it\u0027s not true anymore,"},{"Start":"03:53.630 ","End":"03:55.790","Text":"it will exit out to the bottom here."},{"Start":"03:55.790 ","End":"03:58.990","Text":"What do we need to do in our while loop?"},{"Start":"03:58.990 ","End":"04:04.170","Text":"We need to read a string from the keyboard it says."},{"Start":"04:04.170 ","End":"04:07.225","Text":"First of all, we\u0027re going to prompt the user by"},{"Start":"04:07.225 ","End":"04:09.740","Text":"telling them what they\u0027re supposed to type in."},{"Start":"04:09.740 ","End":"04:13.615","Text":"We just use output statement to do that,"},{"Start":"04:13.615 ","End":"04:22.175","Text":"\"Enter a password to check\" and then we use built-in function to the library called next,"},{"Start":"04:22.175 ","End":"04:27.290","Text":"which takes a string from the keyboard,"},{"Start":"04:27.290 ","End":"04:30.604","Text":"and stores it into password."},{"Start":"04:30.604 ","End":"04:32.210","Text":"That\u0027s Part D done,"},{"Start":"04:32.210 ","End":"04:33.620","Text":"didn\u0027t really have to do any thinking there,"},{"Start":"04:33.620 ","End":"04:35.975","Text":"just did as we were asked to do."},{"Start":"04:35.975 ","End":"04:37.490","Text":"But in Part E,"},{"Start":"04:37.490 ","End":"04:41.465","Text":"we now have been asked inside the loop to call the checkLengthOk(),"},{"Start":"04:41.465 ","End":"04:45.260","Text":"and validate password methods using password as"},{"Start":"04:45.260 ","End":"04:49.280","Text":"the argument to both and if true is returned by both methods,"},{"Start":"04:49.280 ","End":"04:52.070","Text":"we output the message, \"Password valid\" otherwise we"},{"Start":"04:52.070 ","End":"04:55.685","Text":"output the message \"Password invalid.\""},{"Start":"04:55.685 ","End":"04:59.450","Text":"We\u0027re going to call 2 methods."},{"Start":"04:59.450 ","End":"05:04.715","Text":"If check checkLengthOk(),"},{"Start":"05:04.715 ","End":"05:06.900","Text":"and we\u0027ve got a pass to it"},{"Start":"05:06.900 ","End":"05:10.025","Text":"password because that\u0027s the thing we\u0027re checking the length of,"},{"Start":"05:10.025 ","End":"05:15.605","Text":"that has to be true and validate password has to return true."},{"Start":"05:15.605 ","End":"05:17.735","Text":"Probably a better name for that method would have been"},{"Start":"05:17.735 ","End":"05:22.040","Text":"isValidPassword because it returns true if it is a valid password."},{"Start":"05:22.040 ","End":"05:24.185","Text":"But, we\u0027d call it validate password."},{"Start":"05:24.185 ","End":"05:29.410","Text":"We are again going to pass in the password that we\u0027re looking to check."},{"Start":"05:29.410 ","End":"05:31.420","Text":"If both of those are true,"},{"Start":"05:31.420 ","End":"05:34.845","Text":"we output a message \"Password valid.\""},{"Start":"05:34.845 ","End":"05:36.830","Text":"I\u0027m just going to copy that to save me"},{"Start":"05:36.830 ","End":"05:39.965","Text":"some typing because I\u0027ll need that on the next slide."},{"Start":"05:39.965 ","End":"05:45.405","Text":"If it\u0027s true, we do this, otherwise this."},{"Start":"05:45.405 ","End":"05:49.540","Text":"That\u0027s it, apart from ending the loop."},{"Start":"05:49.540 ","End":"05:56.765","Text":"To end the loop, I want to check whether the string contains end."},{"Start":"05:56.765 ","End":"06:00.710","Text":"Remember I cannot use the equal sign for a string."},{"Start":"06:00.710 ","End":"06:06.440","Text":"I have to use the name of the string and then dot equals."},{"Start":"06:06.440 ","End":"06:10.565","Text":"Then I can compare it to something inside brackets."},{"Start":"06:10.565 ","End":"06:14.635","Text":"I want to compare it to end."},{"Start":"06:14.635 ","End":"06:17.930","Text":"While password is not equal to end,"},{"Start":"06:17.930 ","End":"06:20.780","Text":"so I put a logical NOT in front of"},{"Start":"06:20.780 ","End":"06:24.500","Text":"it because I want it to keep going while it\u0027s not equal to end."},{"Start":"06:24.500 ","End":"06:26.105","Text":"As soon as it is equal to end,"},{"Start":"06:26.105 ","End":"06:28.470","Text":"the loop will stop."},{"Start":"06:29.080 ","End":"06:31.685","Text":"That should be it."},{"Start":"06:31.685 ","End":"06:35.270","Text":"Let\u0027s test that then with the inputs that"},{"Start":"06:35.270 ","End":"06:38.720","Text":"they ask us to in Part G. So many methods here,"},{"Start":"06:38.720 ","End":"06:40.405","Text":"and it\u0027s quite hard to see."},{"Start":"06:40.405 ","End":"06:43.325","Text":"PasswordChecker is the one we\u0027re looking for."},{"Start":"06:43.325 ","End":"06:45.800","Text":"For the first time we\u0027re seeing it prompting us in"},{"Start":"06:45.800 ","End":"06:48.140","Text":"BlueJ to type some input and press \"Enter\"."},{"Start":"06:48.140 ","End":"06:51.870","Text":"It doesn\u0027t know the string has been input until you press the \"Enter\" key."},{"Start":"06:51.880 ","End":"06:54.745","Text":"Let\u0027s try the first one."},{"Start":"06:54.745 ","End":"06:59.220","Text":"Shared with a capital S 7!"},{"Start":"06:59.220 ","End":"07:01.140","Text":"Now that\u0027s too short,"},{"Start":"07:01.140 ","End":"07:02.490","Text":"it\u0027s not 8 characters long,"},{"Start":"07:02.490 ","End":"07:05.505","Text":"which is what checkLengthOk() will be checking for,"},{"Start":"07:05.505 ","End":"07:09.860","Text":"so I expect it to say invalid, and it does."},{"Start":"07:09.860 ","End":"07:12.170","Text":"Whereas the next one, g2,"},{"Start":"07:12.170 ","End":"07:14.105","Text":"has got a capital letter,"},{"Start":"07:14.105 ","End":"07:16.040","Text":"has got a digit,"},{"Start":"07:16.040 ","End":"07:18.425","Text":"and has got a symbol."},{"Start":"07:18.425 ","End":"07:22.280","Text":"So all of that and it\u0027s over 8 characters,"},{"Start":"07:22.280 ","End":"07:25.315","Text":"should result in a true."},{"Start":"07:25.315 ","End":"07:27.785","Text":"I don\u0027t need to put quotes around this, by the way,"},{"Start":"07:27.785 ","End":"07:30.185","Text":"you see as I\u0027m typing at the keyboard,"},{"Start":"07:30.185 ","End":"07:33.745","Text":"and it does say password valid."},{"Start":"07:33.745 ","End":"07:38.265","Text":"Part 3 has asked me to input Longest12,"},{"Start":"07:38.265 ","End":"07:39.705","Text":"that doesn\u0027t have a symbol in,"},{"Start":"07:39.705 ","End":"07:41.045","Text":"although it\u0027s long enough."},{"Start":"07:41.045 ","End":"07:43.475","Text":"It doesn\u0027t have a symbol in so that should fail,"},{"Start":"07:43.475 ","End":"07:46.115","Text":"and it does as well invalid."},{"Start":"07:46.115 ","End":"07:49.525","Text":"I can end the program by typing end."},{"Start":"07:49.525 ","End":"07:53.420","Text":"However, it will say password invalid because it\u0027s"},{"Start":"07:53.420 ","End":"07:57.695","Text":"actually assuming that end is something I want it to check."},{"Start":"07:57.695 ","End":"08:02.480","Text":"I could improve that slightly by putting down here,"},{"Start":"08:02.480 ","End":"08:06.065","Text":"instead of having else to print password invalid,"},{"Start":"08:06.065 ","End":"08:16.165","Text":"I\u0027m going to do another check else-if NOT, password.equals(\"end\")."},{"Start":"08:16.165 ","End":"08:21.860","Text":"I\u0027m only going to output this if the password wasn\u0027t valid,"},{"Start":"08:21.860 ","End":"08:25.265","Text":"and the password that was typed in is not equal to end."},{"Start":"08:25.265 ","End":"08:27.320","Text":"If both of those things are true,"},{"Start":"08:27.320 ","End":"08:30.270","Text":"then I\u0027ll print, \"Password invalid.\""},{"Start":"08:30.270 ","End":"08:33.780","Text":"If only one of them is true,"},{"Start":"08:33.780 ","End":"08:42.170","Text":"it\u0027s not end, then it will print \"Password invalid.\""},{"Start":"08:42.170 ","End":"08:44.720","Text":"Let\u0027s see what happens there."},{"Start":"08:44.720 ","End":"08:47.720","Text":"I\u0027m just missing a bracket there."},{"Start":"08:47.720 ","End":"08:49.945","Text":"We still got an error."},{"Start":"08:49.945 ","End":"08:54.600","Text":"Equals not equal and so let\u0027s try that again."},{"Start":"08:54.600 ","End":"08:56.520","Text":"I\u0027ll try it with a valid password,"},{"Start":"08:56.520 ","End":"08:57.750","Text":"an invalid password,"},{"Start":"08:57.750 ","End":"08:59.235","Text":"and try it with end,"},{"Start":"08:59.235 ","End":"09:01.440","Text":"and see what happens."},{"Start":"09:01.440 ","End":"09:07.330","Text":"We know Shed7 is invalid because it\u0027s too short."},{"Start":"09:07.330 ","End":"09:12.590","Text":"We know that Richest1% is valid, so that works."},{"Start":"09:12.590 ","End":"09:14.320","Text":"Now let\u0027s try end,"},{"Start":"09:14.320 ","End":"09:17.795","Text":"and it does end because I\u0027m not been asked for input anymore."},{"Start":"09:17.795 ","End":"09:20.930","Text":"This time it didn\u0027t print password invalid"},{"Start":"09:20.930 ","End":"09:25.280","Text":"because of the little bit we added in here to say,"},{"Start":"09:25.280 ","End":"09:29.000","Text":"we\u0027ll only print password invalid if the password wasn\u0027t equal to end,"},{"Start":"09:29.000 ","End":"09:30.500","Text":"because that\u0027s not really a password."},{"Start":"09:30.500 ","End":"09:34.085","Text":"It\u0027s just signaling that we don\u0027t want to input anymore."},{"Start":"09:34.085 ","End":"09:35.975","Text":"That\u0027s it for this exercise."},{"Start":"09:35.975 ","End":"09:37.565","Text":"Nice and simple in the end."},{"Start":"09:37.565 ","End":"09:39.690","Text":"I\u0027ll see you for the next one."}],"ID":29176},{"Watched":false,"Name":"Exercise 7","Duration":"16m 14s","ChapterTopicVideoID":27685,"CourseChapterTopicPlaylistID":274665,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.550","Text":"Hello again. In this exercise,"},{"Start":"00:02.550 ","End":"00:07.170","Text":"we\u0027ve been asked to create 3 methods to check random letters from a given password."},{"Start":"00:07.170 ","End":"00:10.755","Text":"In Part A, we create a new method called get random position,"},{"Start":"00:10.755 ","End":"00:14.880","Text":"which takes a string as the argument and returns an integer,"},{"Start":"00:14.880 ","End":"00:18.645","Text":"which is a random number between 0 and the length of the string."},{"Start":"00:18.645 ","End":"00:23.250","Text":"In order to do this we first use the Codepad to see how the Math.random method"},{"Start":"00:23.250 ","End":"00:28.695","Text":"returns a random number which is greater than 0 and less than 1.0."},{"Start":"00:28.695 ","End":"00:30.870","Text":"In Part 2 then in the method,"},{"Start":"00:30.870 ","End":"00:35.305","Text":"we multiply the random number generated by Math.random,"},{"Start":"00:35.305 ","End":"00:37.025","Text":"by the length of the string,"},{"Start":"00:37.025 ","End":"00:39.200","Text":"and then we cast it to an integer."},{"Start":"00:39.200 ","End":"00:42.575","Text":"In Part 3 we then return this integer value."},{"Start":"00:42.575 ","End":"00:45.860","Text":"In Part B, we now create a second method called check"},{"Start":"00:45.860 ","End":"00:49.535","Text":"random letter which takes a string as the argument and returns a Boolean."},{"Start":"00:49.535 ","End":"00:50.900","Text":"In Part B1, we use"},{"Start":"00:50.900 ","End":"00:55.370","Text":"the get random position function to get around and position in the string,"},{"Start":"00:55.370 ","End":"00:59.554","Text":"and store the return value to a local variable position."},{"Start":"00:59.554 ","End":"01:03.200","Text":"In Part 2, we output a message telling the user to type in"},{"Start":"01:03.200 ","End":"01:06.770","Text":"a character from the string position indicated by position,"},{"Start":"01:06.770 ","End":"01:11.055","Text":"bear in mind however that humans like counting from 1 not 0."},{"Start":"01:11.055 ","End":"01:14.995","Text":"In Part 3 we use a Scanner to get input from the keyboard,"},{"Start":"01:14.995 ","End":"01:19.920","Text":"using Scanner in equals new Scanner System.in,"},{"Start":"01:19.920 ","End":"01:24.890","Text":"String userInput, userInput equals in.next."},{"Start":"01:24.890 ","End":"01:29.135","Text":"In Part 4, we compare the character at position within the string,"},{"Start":"01:29.135 ","End":"01:33.115","Text":"to the first character of the string typed in at the keyboard."},{"Start":"01:33.115 ","End":"01:36.080","Text":"In Part 5, if the 2 characters match,"},{"Start":"01:36.080 ","End":"01:39.709","Text":"we return true, otherwise we return false."},{"Start":"01:39.709 ","End":"01:42.200","Text":"Finally, we create a method called authenticate,"},{"Start":"01:42.200 ","End":"01:46.280","Text":"which has a string password as a parameter and returns a Boolean."},{"Start":"01:46.280 ","End":"01:50.975","Text":"In Part C1, we\u0027re told it should call check random letter 3 times,"},{"Start":"01:50.975 ","End":"01:53.720","Text":"passing it password as an argument."},{"Start":"01:53.720 ","End":"01:57.260","Text":"In Part C2 we\u0027re told that if the characters at all"},{"Start":"01:57.260 ","End":"02:01.595","Text":"3 random positions are correctly given, we return true."},{"Start":"02:01.595 ","End":"02:06.350","Text":"We return false as soon as 1 character is entered incorrectly,"},{"Start":"02:06.350 ","End":"02:08.180","Text":"we\u0027re told in Part C3."},{"Start":"02:08.180 ","End":"02:12.350","Text":"Then finally in part D we test using the authenticate method."},{"Start":"02:12.350 ","End":"02:15.425","Text":"First by correctly inputting 3 characters,"},{"Start":"02:15.425 ","End":"02:19.145","Text":"then by inputting the first character incorrectly,"},{"Start":"02:19.145 ","End":"02:24.790","Text":"and then by inputting the last character incorrectly in Part D3."},{"Start":"02:24.790 ","End":"02:31.275","Text":"In this exercise we\u0027ve got to first create a method called, get random position."},{"Start":"02:31.275 ","End":"02:36.880","Text":"Let\u0027s get the name in first before we sort out the parameters and so on."},{"Start":"02:36.880 ","End":"02:41.420","Text":"It also has a return value which is an integer,"},{"Start":"02:41.420 ","End":"02:46.205","Text":"and it\u0027s passed in as an argument, a string."},{"Start":"02:46.205 ","End":"02:48.525","Text":"Let\u0027s just call that str."},{"Start":"02:48.525 ","End":"02:53.090","Text":"We\u0027re asked in Part A1 to just use the Codepad to have"},{"Start":"02:53.090 ","End":"02:58.645","Text":"a little play around with the Math.random function so let\u0027s do that."},{"Start":"02:58.645 ","End":"03:04.265","Text":"The math library in Java has all interesting methods in there,"},{"Start":"03:04.265 ","End":"03:07.340","Text":"that do weird and wonderful things and Math.random returns"},{"Start":"03:07.340 ","End":"03:10.655","Text":"you a random number between 0 and 1."},{"Start":"03:10.655 ","End":"03:12.140","Text":"You run it a few times,"},{"Start":"03:12.140 ","End":"03:15.380","Text":"you\u0027ll see that you get different value out every time."},{"Start":"03:15.380 ","End":"03:17.690","Text":"One of the few math functions that you don\u0027t actually"},{"Start":"03:17.690 ","End":"03:21.785","Text":"supply an argument to, its empty brackets."},{"Start":"03:21.785 ","End":"03:24.875","Text":"But it will return you a value which is a random number."},{"Start":"03:24.875 ","End":"03:27.690","Text":"That\u0027s great, but I suppose"},{"Start":"03:27.690 ","End":"03:31.580","Text":"0 and 1 are not the most useful numbers to want to go between."},{"Start":"03:31.580 ","End":"03:35.030","Text":"Usually when I go between 0 and a larger number if it was dice for example,"},{"Start":"03:35.030 ","End":"03:38.270","Text":"you want to go up to 6 and actually do want to start from 1."},{"Start":"03:38.270 ","End":"03:41.180","Text":"But we\u0027re happy to go from 0 in a string,"},{"Start":"03:41.180 ","End":"03:43.565","Text":"but we want to go up to the length of the string."},{"Start":"03:43.565 ","End":"03:47.420","Text":"If we multiply Math.random by an integer,"},{"Start":"03:47.420 ","End":"03:51.530","Text":"we will get a number not equal to that integer,"},{"Start":"03:51.530 ","End":"03:53.015","Text":"but up to that integer."},{"Start":"03:53.015 ","End":"03:55.070","Text":"Because we could get 0,"},{"Start":"03:55.070 ","End":"03:58.565","Text":"we\u0027re never going to get 1 exactly back from the random function."},{"Start":"03:58.565 ","End":"04:01.640","Text":"That\u0027s great, and if I want to turn that into an integer,"},{"Start":"04:01.640 ","End":"04:04.265","Text":"what I can do is I can put"},{"Start":"04:04.265 ","End":"04:08.840","Text":"the cost notation in front of it and I can cast it into an integer."},{"Start":"04:08.840 ","End":"04:11.765","Text":"By the way, you need to put brackets around this expression as well,"},{"Start":"04:11.765 ","End":"04:16.625","Text":"because it will cast the first thing in the expression here and ignore the last bit."},{"Start":"04:16.625 ","End":"04:19.880","Text":"It would always try and cost Math.random,"},{"Start":"04:19.880 ","End":"04:24.965","Text":"the number it returns from between 0 and 1 so you\u0027ll always get 0 back basically,"},{"Start":"04:24.965 ","End":"04:27.740","Text":"so you have to have that in brackets."},{"Start":"04:27.740 ","End":"04:29.960","Text":"It\u0027s okay if we get 0 occasionally,"},{"Start":"04:29.960 ","End":"04:31.880","Text":"but we don\u0027t 0 all the time."},{"Start":"04:31.880 ","End":"04:35.285","Text":"There we go, we\u0027ve got numbers between 0 and 5 now"},{"Start":"04:35.285 ","End":"04:39.125","Text":"being returned merrily every time I generate a random number."},{"Start":"04:39.125 ","End":"04:44.645","Text":"But what we were asked to do is to get a random number from the length of a string."},{"Start":"04:44.645 ","End":"04:47.645","Text":"If I have a string called str,"},{"Start":"04:47.645 ","End":"04:51.050","Text":"and it has a word in it, let\u0027s just call it."},{"Start":"04:51.050 ","End":"04:52.850","Text":"Let\u0027s make it a little bit longer."},{"Start":"04:52.850 ","End":"04:55.775","Text":"Let\u0027s make it a word secret."},{"Start":"04:55.775 ","End":"04:58.280","Text":"Then do what we were doing before,"},{"Start":"04:58.280 ","End":"05:00.020","Text":"but instead of multiplying by 6,"},{"Start":"05:00.020 ","End":"05:04.325","Text":"we multiply it by the length of secret."},{"Start":"05:04.325 ","End":"05:06.195","Text":"We should now have,"},{"Start":"05:06.195 ","End":"05:07.710","Text":"use the capital L there,"},{"Start":"05:07.710 ","End":"05:11.540","Text":"we should now have position in the string."},{"Start":"05:11.540 ","End":"05:14.540","Text":"Never pass the last character in the string,"},{"Start":"05:14.540 ","End":"05:16.270","Text":"never less than 0,"},{"Start":"05:16.270 ","End":"05:18.465","Text":"and I\u0027ve see an integer as well."},{"Start":"05:18.465 ","End":"05:20.190","Text":"That seems to be doing the job,"},{"Start":"05:20.190 ","End":"05:24.305","Text":"you might get the same random number several times in a row."},{"Start":"05:24.305 ","End":"05:27.080","Text":"That\u0027s apache of random numbers,"},{"Start":"05:27.080 ","End":"05:30.230","Text":"and that seems to be working just fine."},{"Start":"05:30.230 ","End":"05:34.100","Text":"I\u0027m never getting a number that\u0027s longer than the length of the string."},{"Start":"05:34.100 ","End":"05:38.435","Text":"Okay, so now I can go back and do my function which is easy enough,"},{"Start":"05:38.435 ","End":"05:43.550","Text":"because I can just use what I\u0027ve learned there to return a random number using"},{"Start":"05:43.550 ","End":"05:50.765","Text":"Math.random multiplied by the length of the string, which is str.length."},{"Start":"05:50.765 ","End":"05:53.900","Text":"But remember that whole expression needs"},{"Start":"05:53.900 ","End":"05:59.705","Text":"brackets because I want to then cast it into an integer."},{"Start":"05:59.705 ","End":"06:07.275","Text":"Whatever this returns will be a number somewhere between 0 and the length of the string,"},{"Start":"06:07.275 ","End":"06:08.915","Text":"with a fractional part."},{"Start":"06:08.915 ","End":"06:11.495","Text":"This will ignore the fractional part."},{"Start":"06:11.495 ","End":"06:16.880","Text":"I\u0027ll never get the length of the string because I never get 1 returned from Math.random,"},{"Start":"06:16.880 ","End":"06:21.575","Text":"there\u0027ll always be slightly less than 1 so that works exactly as I\u0027d want it to."},{"Start":"06:21.575 ","End":"06:23.855","Text":"That\u0027s the first part done,"},{"Start":"06:23.855 ","End":"06:26.135","Text":"Part A. I\u0027m going to test this in a moment,"},{"Start":"06:26.135 ","End":"06:27.980","Text":"but not quite now."},{"Start":"06:27.980 ","End":"06:29.975","Text":"I\u0027m just going to do the second method,"},{"Start":"06:29.975 ","End":"06:32.420","Text":"which is called check random letter."},{"Start":"06:32.420 ","End":"06:37.760","Text":"This lets me take a random letter in the password and test whether its right."},{"Start":"06:37.760 ","End":"06:40.895","Text":"Let\u0027s call our parameter password."},{"Start":"06:40.895 ","End":"06:44.870","Text":"This method is going to return a Boolean."},{"Start":"06:44.870 ","End":"06:47.525","Text":"Let\u0027s put that in front of the method name."},{"Start":"06:47.525 ","End":"06:50.195","Text":"Now, the code itself,"},{"Start":"06:50.195 ","End":"06:53.030","Text":"we\u0027re going to use the get random position to get"},{"Start":"06:53.030 ","End":"06:57.140","Text":"a random position in the string and store it in a local variable called position."},{"Start":"06:57.140 ","End":"06:59.690","Text":"Because it returns an integer,"},{"Start":"06:59.690 ","End":"07:03.199","Text":"I\u0027m going to have to make position an integer variable."},{"Start":"07:03.199 ","End":"07:08.685","Text":"Int position equals get random position,"},{"Start":"07:08.685 ","End":"07:11.555","Text":"and I\u0027m going to pass it as an argument,"},{"Start":"07:11.555 ","End":"07:14.060","Text":"whatever is currently in password,"},{"Start":"07:14.060 ","End":"07:18.755","Text":"which was also passed as an argument into this function into this method."},{"Start":"07:18.755 ","End":"07:22.115","Text":"There we go, that\u0027s part done B1,"},{"Start":"07:22.115 ","End":"07:24.905","Text":"and then we\u0027re asked to output a message."},{"Start":"07:24.905 ","End":"07:27.875","Text":"We use system out print line to do that,"},{"Start":"07:27.875 ","End":"07:30.500","Text":"to get tell the user to type in a character"},{"Start":"07:30.500 ","End":"07:33.680","Text":"from the string position indicated by position."},{"Start":"07:33.680 ","End":"07:38.075","Text":"Type in character, leave a space there,"},{"Start":"07:38.075 ","End":"07:43.730","Text":"and then the nice thing about system out print line is if this is not a string,"},{"Start":"07:43.730 ","End":"07:45.590","Text":"it will convert it into a string."},{"Start":"07:45.590 ","End":"07:47.930","Text":"Put brackets in too, until you ware in a moment."},{"Start":"07:47.930 ","End":"07:50.075","Text":"There is the last part,"},{"Start":"07:50.075 ","End":"07:52.774","Text":"type character, typing character,"},{"Start":"07:52.774 ","End":"07:56.110","Text":"whatever it is from the string,"},{"Start":"07:56.110 ","End":"07:58.775","Text":"so that is a statement."},{"Start":"07:58.775 ","End":"08:00.950","Text":"Now it says in the question,"},{"Start":"08:00.950 ","End":"08:03.200","Text":"humans like counting from 1 not 0,"},{"Start":"08:03.200 ","End":"08:08.450","Text":"so 0 could be the value we get back and it will say type in character"},{"Start":"08:08.450 ","End":"08:14.090","Text":"0 from the string which is a bit odd to hear character 0 for a regular user."},{"Start":"08:14.090 ","End":"08:15.855","Text":"For a programmer, it might not be,"},{"Start":"08:15.855 ","End":"08:20.290","Text":"but it might be better to say character 1,2,3,4,5 when we"},{"Start":"08:20.290 ","End":"08:24.790","Text":"have character position 0,1,2,3,4 in reality,"},{"Start":"08:24.790 ","End":"08:27.895","Text":"and all we need to do is to add 1 onto"},{"Start":"08:27.895 ","End":"08:32.335","Text":"whatever position is and it will print it out in a human friendly way."},{"Start":"08:32.335 ","End":"08:36.510","Text":"But we can still check positions 0,1,2,3,4."},{"Start":"08:36.510 ","End":"08:39.840","Text":"Internally so as long as we\u0027re consistent,"},{"Start":"08:39.840 ","End":"08:41.130","Text":"it won\u0027t make a difference."},{"Start":"08:41.130 ","End":"08:46.455","Text":"But the output itself probably should be a bit more human friendly."},{"Start":"08:46.455 ","End":"08:49.020","Text":"That\u0027s that part and now we\u0027re going to get"},{"Start":"08:49.020 ","End":"08:53.175","Text":"some keyboard input so we need scanner for that."},{"Start":"08:53.175 ","End":"08:55.800","Text":"We\u0027ve got a scanner object here called,"},{"Start":"08:55.800 ","End":"08:58.514","Text":"in which we create from scanner,"},{"Start":"08:58.514 ","End":"09:04.740","Text":"which is pass the reference to the system in object,"},{"Start":"09:04.740 ","End":"09:06.855","Text":"which allows us to access a keyboard."},{"Start":"09:06.855 ","End":"09:13.665","Text":"Then String userInput is a variable we\u0027re going to store the user input."},{"Start":"09:13.665 ","End":"09:15.510","Text":"I could do this all on 1 line,"},{"Start":"09:15.510 ","End":"09:17.115","Text":"but I\u0027ll put another line."},{"Start":"09:17.115 ","End":"09:20.460","Text":"We\u0027re only going to get 1 character, which is unusual,"},{"Start":"09:20.460 ","End":"09:26.235","Text":"but we\u0027re going to get it in userInput=in.next."},{"Start":"09:26.235 ","End":"09:30.225","Text":"We\u0027re going to get in to the user input variable."},{"Start":"09:30.225 ","End":"09:31.710","Text":"That\u0027s Part 3 done,"},{"Start":"09:31.710 ","End":"09:33.960","Text":"we just literally had to type what was in Part 3,"},{"Start":"09:33.960 ","End":"09:35.160","Text":"but for Part 4,"},{"Start":"09:35.160 ","End":"09:37.650","Text":"we have got to compare the character position within"},{"Start":"09:37.650 ","End":"09:40.785","Text":"the string to the first character of the string type to the keyboard."},{"Start":"09:40.785 ","End":"09:42.510","Text":"It sounds quite odd."},{"Start":"09:42.510 ","End":"09:44.520","Text":"We\u0027re comparing characters, we\u0027ve got strings here."},{"Start":"09:44.520 ","End":"09:47.310","Text":"Why are we doing that? Well, we\u0027ll see why,"},{"Start":"09:47.310 ","End":"09:49.260","Text":"because we\u0027re getting an individual character."},{"Start":"09:49.260 ","End":"09:52.215","Text":"I have a string, we\u0027re going to use charAt."},{"Start":"09:52.215 ","End":"10:01.140","Text":"The character we\u0027re looking for is password.charAt(position)."},{"Start":"10:01.140 ","End":"10:04.980","Text":"Whatever random character we\u0027re pointing at,"},{"Start":"10:04.980 ","End":"10:08.985","Text":"we want that to be the thing that the user has just typed in"},{"Start":"10:08.985 ","End":"10:14.040","Text":"so we\u0027re going to say userInput,"},{"Start":"10:14.040 ","End":"10:19.560","Text":"and because it\u0027s a string and we\u0027re expecting them to only have typed in 1 character,"},{"Start":"10:19.560 ","End":"10:23.445","Text":"we just need to get charAt(0)."},{"Start":"10:23.445 ","End":"10:28.650","Text":"Just to summarize, if the random character that we\u0027re searching"},{"Start":"10:28.650 ","End":"10:34.230","Text":"for is equal to the letter that has just been typed into the string,"},{"Start":"10:34.230 ","End":"10:35.655","Text":"and this should only be 1,"},{"Start":"10:35.655 ","End":"10:37.395","Text":"but that\u0027s the only one we\u0027re interested in,"},{"Start":"10:37.395 ","End":"10:40.830","Text":"the 0th position, the first character."},{"Start":"10:40.830 ","End":"10:42.675","Text":"If they are equal,"},{"Start":"10:42.675 ","End":"10:47.205","Text":"then I can return true because they match."},{"Start":"10:47.205 ","End":"10:50.300","Text":"That would be the end of my program, if they match."},{"Start":"10:50.300 ","End":"10:52.100","Text":"But it\u0027s not on my method,"},{"Start":"10:52.100 ","End":"10:53.225","Text":"I should say not programmed,"},{"Start":"10:53.225 ","End":"10:57.105","Text":"I need to return false if they don\u0027t match."},{"Start":"10:57.105 ","End":"11:00.480","Text":"You don\u0027t actually need an else here because it will never get to"},{"Start":"11:00.480 ","End":"11:05.625","Text":"this line if it returned at this line and it\u0027s only 1 character we\u0027re checking."},{"Start":"11:05.625 ","End":"11:08.625","Text":"It\u0027s only 1 thing that they\u0027ve typed in."},{"Start":"11:08.625 ","End":"11:10.500","Text":"If it exits here,"},{"Start":"11:10.500 ","End":"11:11.760","Text":"that\u0027s because they matched."},{"Start":"11:11.760 ","End":"11:15.180","Text":"If they didn\u0027t match, that must mean that I should return false."},{"Start":"11:15.180 ","End":"11:17.730","Text":"If you don\u0027t put return false here, by the way,"},{"Start":"11:17.730 ","End":"11:21.075","Text":"you possibly get an error from the compiler saying,"},{"Start":"11:21.075 ","End":"11:25.455","Text":"it\u0027s a potentially no return value coming back from this method."},{"Start":"11:25.455 ","End":"11:28.920","Text":"That looks like it\u0027s fine and we could carry on to the last bit."},{"Start":"11:28.920 ","End":"11:32.580","Text":"It\u0027s probably better to just have a quick test of that."},{"Start":"11:32.580 ","End":"11:36.720","Text":"Let\u0027s just see that what we\u0027ve done does what we want it to."},{"Start":"11:36.720 ","End":"11:44.730","Text":"Let\u0027s do check random letter and if we type in a password of secret,"},{"Start":"11:44.730 ","End":"11:47.490","Text":"I\u0027ll just save that, save me typing all the time."},{"Start":"11:47.490 ","End":"11:53.205","Text":"Typing character 2 SE would be character 2 and it\u0027s come back with true."},{"Start":"11:53.205 ","End":"11:54.390","Text":"Let\u0027s type the wrong one,"},{"Start":"11:54.390 ","End":"11:55.620","Text":"let\u0027s call it again,"},{"Start":"11:55.620 ","End":"11:58.950","Text":"check random letter paste backward I copied a moment ago,"},{"Start":"11:58.950 ","End":"12:00.720","Text":"and that would be E again."},{"Start":"12:00.720 ","End":"12:02.310","Text":"But let\u0027s get it wrong."},{"Start":"12:02.310 ","End":"12:07.905","Text":"Let\u0027s put D and it returns false so it does seem to do what we expect it to do."},{"Start":"12:07.905 ","End":"12:10.830","Text":"I can go back now and do the final part,"},{"Start":"12:10.830 ","End":"12:16.830","Text":"which is Part C. That is to write a method called authenticate,"},{"Start":"12:16.830 ","End":"12:18.825","Text":"which again returns a Boolean."},{"Start":"12:18.825 ","End":"12:26.175","Text":"It takes a string and it\u0027s even given us the name for the parameter of password."},{"Start":"12:26.175 ","End":"12:28.695","Text":"It returns the Boolean, as we said."},{"Start":"12:28.695 ","End":"12:32.490","Text":"Now, all it\u0027s going to do is called check random letter 3 times,"},{"Start":"12:32.490 ","End":"12:34.815","Text":"passing it password as an argument."},{"Start":"12:34.815 ","End":"12:37.769","Text":"If all 3 characters are correct,"},{"Start":"12:37.769 ","End":"12:39.495","Text":"then we return true,"},{"Start":"12:39.495 ","End":"12:44.370","Text":"otherwise return false as soon as 1 character is entered incorrectly."},{"Start":"12:44.370 ","End":"12:47.115","Text":"Because we\u0027re always going to do 3 here,"},{"Start":"12:47.115 ","End":"12:49.350","Text":"for loops away to go,"},{"Start":"12:49.350 ","End":"12:51.030","Text":"because that\u0027s what it\u0027s designed for."},{"Start":"12:51.030 ","End":"12:52.530","Text":"Let\u0027s call that count."},{"Start":"12:52.530 ","End":"12:56.130","Text":"Start at 0, 1 \u003c3."},{"Start":"12:56.130 ","End":"12:59.040","Text":"We keep going and all we got to do is"},{"Start":"12:59.040 ","End":"13:02.685","Text":"call method that we\u0027ve created called check random letter."},{"Start":"13:02.685 ","End":"13:09.105","Text":"We\u0027re going to pass it password because that\u0027s the argument name or the parameter name,"},{"Start":"13:09.105 ","End":"13:11.640","Text":"and it\u0027s not good enough to just call the method."},{"Start":"13:11.640 ","End":"13:12.750","Text":"We\u0027ve got to do something with it."},{"Start":"13:12.750 ","End":"13:19.395","Text":"We can store in a variable or we could just directly compare what is returned from here,"},{"Start":"13:19.395 ","End":"13:22.380","Text":"true or false, because if they typed it in correctly, we get true."},{"Start":"13:22.380 ","End":"13:24.990","Text":"If they typed it in incorrectly, we get false."},{"Start":"13:24.990 ","End":"13:28.245","Text":"If at any point we get false back,"},{"Start":"13:28.245 ","End":"13:32.190","Text":"we know that it\u0027s failed to authenticate."},{"Start":"13:32.190 ","End":"13:34.785","Text":"It\u0027s the wrong letters from the password,"},{"Start":"13:34.785 ","End":"13:37.515","Text":"so we can just return false."},{"Start":"13:37.515 ","End":"13:41.865","Text":"Once again, we can return true here."},{"Start":"13:41.865 ","End":"13:44.550","Text":"But actually we don\u0027t want to immediately return true."},{"Start":"13:44.550 ","End":"13:50.685","Text":"We only want to return true if all 3 letters have been correctly typed in."},{"Start":"13:50.685 ","End":"13:55.695","Text":"Where we should put that is on this line here outside the for loop,"},{"Start":"13:55.695 ","End":"13:59.055","Text":"so we either return true or we return false."},{"Start":"13:59.055 ","End":"14:02.460","Text":"False will be returned immediately soon as we get 1 wrong,"},{"Start":"14:02.460 ","End":"14:08.715","Text":"whereas true will be returned if all 3 are correct and only if all 3 are correct."},{"Start":"14:08.715 ","End":"14:10.530","Text":"Let\u0027s see if that works."},{"Start":"14:10.530 ","End":"14:11.610","Text":"We\u0027ll see any issues,"},{"Start":"14:11.610 ","End":"14:13.935","Text":"but part D is to check it."},{"Start":"14:13.935 ","End":"14:16.350","Text":"Let\u0027s do that. You got authenticate."},{"Start":"14:16.350 ","End":"14:18.960","Text":"Let\u0027s use a really basic password of secret or"},{"Start":"14:18.960 ","End":"14:22.995","Text":"lowercase and type in character 6 would be t,"},{"Start":"14:22.995 ","End":"14:25.350","Text":"typing character 1 would be s,"},{"Start":"14:25.350 ","End":"14:28.950","Text":"typing character 2 would be e,"},{"Start":"14:28.950 ","End":"14:31.410","Text":"the second E and there we go."},{"Start":"14:31.410 ","End":"14:33.630","Text":"True, I get 3, correct,"},{"Start":"14:33.630 ","End":"14:34.650","Text":"so it\u0027s returned true,"},{"Start":"14:34.650 ","End":"14:36.029","Text":"which is what I expected."},{"Start":"14:36.029 ","End":"14:38.010","Text":"Now let\u0027s do D2,"},{"Start":"14:38.010 ","End":"14:41.025","Text":"which is to input the first character incorrectly."},{"Start":"14:41.025 ","End":"14:43.800","Text":"Let\u0027s use secret as our password, again,"},{"Start":"14:43.800 ","End":"14:45.960","Text":"type in the first character incorrectly,"},{"Start":"14:45.960 ","End":"14:47.490","Text":"so it\u0027s expecting an s,"},{"Start":"14:47.490 ","End":"14:49.380","Text":"let\u0027s put a z instead."},{"Start":"14:49.380 ","End":"14:52.080","Text":"We expect it to return false, which it does."},{"Start":"14:52.080 ","End":"14:55.800","Text":"Great. Now, let\u0027s see if we get 2 right and the last"},{"Start":"14:55.800 ","End":"15:01.065","Text":"one wrong if that still works as we\u0027ve been asked to in D3."},{"Start":"15:01.065 ","End":"15:03.435","Text":"Secret again."},{"Start":"15:03.435 ","End":"15:07.890","Text":"SEC would be character 3,"},{"Start":"15:07.890 ","End":"15:13.590","Text":"SE would be character 2 and 4BSECR,"},{"Start":"15:13.590 ","End":"15:15.930","Text":"but we\u0027re not going to type in R. Let\u0027s type in"},{"Start":"15:15.930 ","End":"15:20.970","Text":"z and it should exit with a false, which it does."},{"Start":"15:20.970 ","End":"15:26.100","Text":"That\u0027s it. 3, what look like quite short functions,"},{"Start":"15:26.100 ","End":"15:28.290","Text":"and that\u0027s how they really should look,"},{"Start":"15:28.290 ","End":"15:32.055","Text":"is fairly short and pretty reusable."},{"Start":"15:32.055 ","End":"15:34.920","Text":"You could see this being used in another program definitely."},{"Start":"15:34.920 ","End":"15:38.640","Text":"This is nice and elegant because it\u0027s making use of 1 method,"},{"Start":"15:38.640 ","End":"15:40.740","Text":"which makes use of another method."},{"Start":"15:40.740 ","End":"15:42.390","Text":"You might ask question here,"},{"Start":"15:42.390 ","End":"15:45.300","Text":"why does position need to be stored in a variable?"},{"Start":"15:45.300 ","End":"15:50.745","Text":"Well, that\u0027s because when we output to the user what position we\u0027re interested in,"},{"Start":"15:50.745 ","End":"15:54.255","Text":"that must be the same position that we\u0027re checking later."},{"Start":"15:54.255 ","End":"15:57.735","Text":"If we call the getRandomPosition function again,"},{"Start":"15:57.735 ","End":"16:02.337","Text":"we\u0027d ask them for 1 random character and we\u0027d be checking another random character so"},{"Start":"16:02.337 ","End":"16:04.590","Text":"we\u0027re going to make sure that we only get a random number"},{"Start":"16:04.590 ","End":"16:06.960","Text":"once and store it in a variable."},{"Start":"16:06.960 ","End":"16:10.245","Text":"That\u0027s why we\u0027ve done it right at the beginning of the method."},{"Start":"16:10.245 ","End":"16:11.835","Text":"That\u0027s it for this one."},{"Start":"16:11.835 ","End":"16:14.710","Text":"Thank you very much and I\u0027ll see you soon."}],"ID":29177}],"Thumbnail":null,"ID":274665},{"Name":"Exceptions","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"The Need for Exceptions","Duration":"11m 50s","ChapterTopicVideoID":28092,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.895","Text":"Hello. Welcome to this video on exception handling."},{"Start":"00:02.895 ","End":"00:04.080","Text":"By the end of this section,"},{"Start":"00:04.080 ","End":"00:07.680","Text":"you\u0027ll be able to distinguish between the handling of logical syntax,"},{"Start":"00:07.680 ","End":"00:12.375","Text":"and runtime errors to explain the meaning of the term defensive programming,"},{"Start":"00:12.375 ","End":"00:16.200","Text":"and to apply different mechanisms for runtime error handling."},{"Start":"00:16.200 ","End":"00:19.160","Text":"Early on in the course we learned that there are different categories"},{"Start":"00:19.160 ","End":"00:22.040","Text":"of error that can occur when developing programs."},{"Start":"00:22.040 ","End":"00:26.660","Text":"Syntax errors involved the checking of code to see whether it\u0027s valid or invalid"},{"Start":"00:26.660 ","End":"00:31.340","Text":"when mechanically following rules around what keywords and symbols are allowed,"},{"Start":"00:31.340 ","End":"00:35.149","Text":"and also the rules around arrangement of those keywords and symbols."},{"Start":"00:35.149 ","End":"00:38.350","Text":"In this case we\u0027re missing a pair of closing speech marks,"},{"Start":"00:38.350 ","End":"00:43.145","Text":"and the compiler will stop with a syntax error when the program is compiled."},{"Start":"00:43.145 ","End":"00:46.160","Text":"With logical errors no errors"},{"Start":"00:46.160 ","End":"00:49.564","Text":"reported during compilation or during the running of the program,"},{"Start":"00:49.564 ","End":"00:51.650","Text":"but a logical flaw in the design,"},{"Start":"00:51.650 ","End":"00:54.860","Text":"or implementation of the program means that the program doesn\u0027t"},{"Start":"00:54.860 ","End":"00:58.675","Text":"do what we intended it to do with all possible inputs."},{"Start":"00:58.675 ","End":"01:05.465","Text":"In this case, we\u0027ve forgotten to account for the situation where a is exactly a=b."},{"Start":"01:05.465 ","End":"01:08.330","Text":"Logical errors can most easily be understood and"},{"Start":"01:08.330 ","End":"01:11.525","Text":"fixed through the use of a piece of software called a debugger."},{"Start":"01:11.525 ","End":"01:14.068","Text":"This is software to facilitate the test,"},{"Start":"01:14.068 ","End":"01:19.550","Text":"and debug of other programs enabling step-by-step execution of a program,"},{"Start":"01:19.550 ","End":"01:21.350","Text":"setting of breakpoints,"},{"Start":"01:21.350 ","End":"01:23.150","Text":"and inspection of data."},{"Start":"01:23.150 ","End":"01:27.260","Text":"Debuggers are generally integrated into a development environment."},{"Start":"01:27.260 ","End":"01:32.380","Text":"Here\u0027s an example of the debugger in BlueJ\u0027s development environment."},{"Start":"01:32.380 ","End":"01:35.690","Text":"Using a debugger, we can stop a running program."},{"Start":"01:35.690 ","End":"01:36.770","Text":"At 1 or more points,"},{"Start":"01:36.770 ","End":"01:38.960","Text":"we can decide and code breakpoints."},{"Start":"01:38.960 ","End":"01:43.955","Text":"In BlueJ you can do this by clicking in the margin in the source file,"},{"Start":"01:43.955 ","End":"01:47.000","Text":"and a red stop icon appears."},{"Start":"01:47.000 ","End":"01:49.320","Text":"When a program is compiled and run,"},{"Start":"01:49.320 ","End":"01:51.260","Text":"if a break point has been hit,"},{"Start":"01:51.260 ","End":"01:55.160","Text":"the program stops and is under the control of the debugger."},{"Start":"01:55.160 ","End":"01:57.890","Text":"Variables and other data can now be"},{"Start":"01:57.890 ","End":"02:02.180","Text":"inspected and the program can be continued either aligned at"},{"Start":"02:02.180 ","End":"02:05.330","Text":"a time using step or can be run to"},{"Start":"02:05.330 ","End":"02:09.845","Text":"the next break point or program completion using continue."},{"Start":"02:09.845 ","End":"02:14.210","Text":"Usually by inspecting the variable values when a break point has been hit,"},{"Start":"02:14.210 ","End":"02:18.185","Text":"it\u0027s possible to figure out why the program\u0027s not doing what was intended."},{"Start":"02:18.185 ","End":"02:21.775","Text":"The program can then be edited to fix the error."},{"Start":"02:21.775 ","End":"02:25.600","Text":"If it\u0027s possible to thoroughly test the code with every possible input,"},{"Start":"02:25.600 ","End":"02:31.855","Text":"a logical error should never occur in a fully tested release version of a program."},{"Start":"02:31.855 ","End":"02:35.450","Text":"Runtime errors are the third category of error."},{"Start":"02:35.450 ","End":"02:39.260","Text":"The lines of Java here could potentially generate a runtime error."},{"Start":"02:39.260 ","End":"02:40.850","Text":"The syntax is correct,"},{"Start":"02:40.850 ","End":"02:44.510","Text":"so the compiler or interpreter would not signal a syntax error,"},{"Start":"02:44.510 ","End":"02:47.000","Text":"and there\u0027s no discernible flow on the logic."},{"Start":"02:47.000 ","End":"02:50.255","Text":"The code simply attempts to read data from a file,"},{"Start":"02:50.255 ","End":"02:53.360","Text":"and may work fine most of the time, however,"},{"Start":"02:53.360 ","End":"02:57.320","Text":"if the File ( \"data.txt\" ) was moved, or renamed,"},{"Start":"02:57.320 ","End":"03:03.325","Text":"or deleted, an error would occur at the time the second line was executed."},{"Start":"03:03.325 ","End":"03:06.950","Text":"This code example would generate a runtime error generally known as"},{"Start":"03:06.950 ","End":"03:11.875","Text":"an out-of-bounds error reported in Python as string index out of range."},{"Start":"03:11.875 ","End":"03:15.320","Text":"It\u0027s happening because we\u0027re trying to read past the end of the string to"},{"Start":"03:15.320 ","End":"03:19.565","Text":"the ninth character when there are only 8 characters present."},{"Start":"03:19.565 ","End":"03:21.980","Text":"While it seems an obvious error in this example,"},{"Start":"03:21.980 ","End":"03:26.105","Text":"it\u0027s more likely that the value inside the square brackets would come from a variable,"},{"Start":"03:26.105 ","End":"03:29.165","Text":"not the literal value 8 we have here."},{"Start":"03:29.165 ","End":"03:33.950","Text":"The value of that variable could change or the size of the string might change,"},{"Start":"03:33.950 ","End":"03:36.935","Text":"either of which could cause a runtime error."},{"Start":"03:36.935 ","End":"03:40.535","Text":"This line in C could also cause a runtime error."},{"Start":"03:40.535 ","End":"03:46.685","Text":"The malloc function in C requests memory from the operating environment for storing data."},{"Start":"03:46.685 ","End":"03:52.100","Text":"Here it\u0027s trying to request a 150 billion bytes of data to store a string."},{"Start":"03:52.100 ","End":"03:58.085","Text":"If it works, the system returns the address of the first address of 150 billion bytes."},{"Start":"03:58.085 ","End":"04:01.610","Text":"If it doesn\u0027t work and a subsequent line of code tries"},{"Start":"04:01.610 ","End":"04:05.380","Text":"to access the memory that hasn\u0027t been allocated,"},{"Start":"04:05.380 ","End":"04:07.310","Text":"bad things might happen."},{"Start":"04:07.310 ","End":"04:10.655","Text":"Worst-case, the whole system could crash."},{"Start":"04:10.655 ","End":"04:12.680","Text":"This might seem an obvious problem for"},{"Start":"04:12.680 ","End":"04:16.010","Text":"such a ridiculous request as 150 billion bytes to store,"},{"Start":"04:16.010 ","End":"04:20.270","Text":"but it could arise with more reasonable requests just because"},{"Start":"04:20.270 ","End":"04:23.180","Text":"the operating environment might not have a lot of memory"},{"Start":"04:23.180 ","End":"04:27.710","Text":"available at the particular point in time that the memory was requested."},{"Start":"04:27.710 ","End":"04:32.210","Text":"Hence, this is another runtime error because you can only know about it"},{"Start":"04:32.210 ","End":"04:36.380","Text":"whilst the program\u0027s running and not at the compilation stage."},{"Start":"04:36.380 ","End":"04:38.900","Text":"However, you could argue that with thorough testing,"},{"Start":"04:38.900 ","End":"04:42.995","Text":"it\u0027s possible to predict whether these sorts of errors could occur."},{"Start":"04:42.995 ","End":"04:46.550","Text":"Defensive programming is a set of techniques used to ensure"},{"Start":"04:46.550 ","End":"04:50.780","Text":"the robustness of programs in unforeseen circumstances."},{"Start":"04:50.780 ","End":"04:53.642","Text":"Programs can be vulnerable to lots of different issues"},{"Start":"04:53.642 ","End":"04:56.359","Text":"including mischievous or malicious users,"},{"Start":"04:56.359 ","End":"05:00.455","Text":"excessive resource usage, hardware failure,"},{"Start":"05:00.455 ","End":"05:05.435","Text":"even minute timing differences between multiple tasks running on a system."},{"Start":"05:05.435 ","End":"05:08.431","Text":"Defensive programming is a mindset that says,"},{"Start":"05:08.431 ","End":"05:10.970","Text":"something could go wrong here,"},{"Start":"05:10.970 ","End":"05:15.140","Text":"and if it does I\u0027ll deal with it in this way."},{"Start":"05:15.140 ","End":"05:18.740","Text":"A starting point for thinking about defensive programming is to consider"},{"Start":"05:18.740 ","End":"05:23.165","Text":"the potential relationship between logical and runtime errors."},{"Start":"05:23.165 ","End":"05:26.060","Text":"It\u0027s not the programmer\u0027s fault if a file gets"},{"Start":"05:26.060 ","End":"05:29.459","Text":"moved or renamed generating a runtime error,"},{"Start":"05:29.459 ","End":"05:34.310","Text":"but it is the programmer\u0027s fault if they tried to write code that attempts to open"},{"Start":"05:34.310 ","End":"05:40.730","Text":"a file without first checking whether it exists as we do here in the second line."},{"Start":"05:40.730 ","End":"05:43.850","Text":"An added IF statement which checks first"},{"Start":"05:43.850 ","End":"05:47.030","Text":"to see whether the specified File( \"data.txt\" ) is"},{"Start":"05:47.030 ","End":"05:50.540","Text":"actually present does not cause the runtime error to"},{"Start":"05:50.540 ","End":"05:54.440","Text":"happen because the code in the IF block is skipped."},{"Start":"05:54.440 ","End":"05:57.140","Text":"The original code could be considered first as"},{"Start":"05:57.140 ","End":"06:00.610","Text":"a logical error which then leads to a runtime error."},{"Start":"06:00.610 ","End":"06:03.020","Text":"A common way of checking whether an operation has been"},{"Start":"06:03.020 ","End":"06:05.510","Text":"successful it\u0027s when any functions that return"},{"Start":"06:05.510 ","End":"06:07.940","Text":"something where failure might happen to return"},{"Start":"06:07.940 ","End":"06:10.655","Text":"a results code which indicates the failure."},{"Start":"06:10.655 ","End":"06:16.070","Text":"To take the simple case of the find function in Python or its equivalent in Java index"},{"Start":"06:16.070 ","End":"06:22.675","Text":"of find being used here is looking for a space within the string full name,"},{"Start":"06:22.675 ","End":"06:25.325","Text":"and it returns the position where it found it."},{"Start":"06:25.325 ","End":"06:30.400","Text":"The value returned is then stored into the position variable,"},{"Start":"06:30.400 ","End":"06:33.890","Text":"then the first character and the character after the space are"},{"Start":"06:33.890 ","End":"06:39.500","Text":"extracted and stored into the initials variable which is then output."},{"Start":"06:39.500 ","End":"06:44.690","Text":"This code is flawed because it assumes that there will be a space in the full name."},{"Start":"06:44.690 ","End":"06:47.030","Text":"If that space was removed,"},{"Start":"06:47.030 ","End":"06:50.420","Text":"it will output the first initial twice as"},{"Start":"06:50.420 ","End":"06:54.985","Text":"position will contain minus 1 when no space is found."},{"Start":"06:54.985 ","End":"06:58.317","Text":"Once again we extract the first initial node problem,"},{"Start":"06:58.317 ","End":"07:01.040","Text":"but for the second initial we blindly extract"},{"Start":"07:01.040 ","End":"07:04.790","Text":"the character at position plus 1 as we did before,"},{"Start":"07:04.790 ","End":"07:11.140","Text":"but minus 1 plus 1 gives 0 and the second initial will be the same as the first 1."},{"Start":"07:11.140 ","End":"07:14.660","Text":"The code should have checked whether minus 1 was returned"},{"Start":"07:14.660 ","End":"07:18.049","Text":"by find and then change the output accordingly."},{"Start":"07:18.049 ","End":"07:21.710","Text":"For example in this adapted version of the code,"},{"Start":"07:21.710 ","End":"07:23.300","Text":"a message is output saying,"},{"Start":"07:23.300 ","End":"07:30.230","Text":"\"Error in input\" if the input string doesn\u0027t contain a space as expected in a full name."},{"Start":"07:30.230 ","End":"07:33.004","Text":"This is an example of defensive programming,"},{"Start":"07:33.004 ","End":"07:38.255","Text":"but in this case there was no danger of a system crash or the program terminating early."},{"Start":"07:38.255 ","End":"07:43.955","Text":"It\u0027s simply willing to generate incorrect results because of the flawed input data."},{"Start":"07:43.955 ","End":"07:48.905","Text":"The most common type of error leading to a crash is called a null pointer error."},{"Start":"07:48.905 ","End":"07:52.370","Text":"We\u0027ve come across the value null before when we discussed using"},{"Start":"07:52.370 ","End":"07:57.230","Text":"a null character to indicate an empty string or the end of a string."},{"Start":"07:57.230 ","End":"08:00.145","Text":"It\u0027s the ASCII value 0."},{"Start":"08:00.145 ","End":"08:04.280","Text":"Pointers are variables that contain a memory address."},{"Start":"08:04.280 ","End":"08:07.880","Text":"We use the pointer to store and retrieve values at"},{"Start":"08:07.880 ","End":"08:11.900","Text":"a particular memory location or a series of memory locations."},{"Start":"08:11.900 ","End":"08:15.275","Text":"Here the pointer is storing the address of a string."},{"Start":"08:15.275 ","End":"08:21.695","Text":"In C, a null pointer refers to a pointer that has the value null."},{"Start":"08:21.695 ","End":"08:25.865","Text":"Essentially the pointer is set to the beginning memory location 0"},{"Start":"08:25.865 ","End":"08:31.115","Text":"which by convention is regarded as meaning it doesn\u0027t point to anything."},{"Start":"08:31.115 ","End":"08:35.780","Text":"Here we\u0027re trying to get a ridiculous amount of memory using malloc as we did earlier,"},{"Start":"08:35.780 ","End":"08:40.370","Text":"but this time we check whether the pointer we get back is null."},{"Start":"08:40.370 ","End":"08:46.145","Text":"If it is, then we see a warning that the memory requested was not allocated."},{"Start":"08:46.145 ","End":"08:51.260","Text":"It\u0027s a bit like receiving minus 1 back from the find function in Python"},{"Start":"08:51.260 ","End":"08:54.110","Text":"except instead of failing to find a specific character we\u0027re"},{"Start":"08:54.110 ","End":"08:56.975","Text":"looking for we fail to allocate memory,"},{"Start":"08:56.975 ","End":"09:02.614","Text":"and so we don\u0027t get back a pointer to where the memory is by pointer to memory address 0."},{"Start":"09:02.614 ","End":"09:05.315","Text":"In other words, a null pointer."},{"Start":"09:05.315 ","End":"09:08.810","Text":"A more general use of null is to either indicate that something has"},{"Start":"09:08.810 ","End":"09:12.425","Text":"not been set yet or to indicate that it does not exist."},{"Start":"09:12.425 ","End":"09:16.880","Text":"For example, we declare a string variable in Java and assign no initial value."},{"Start":"09:16.880 ","End":"09:19.850","Text":"The variable will contain the value null."},{"Start":"09:19.850 ","End":"09:22.250","Text":"As soon as we assign a value to the string,"},{"Start":"09:22.250 ","End":"09:25.880","Text":"the initial null value is overwritten with a new value."},{"Start":"09:25.880 ","End":"09:28.730","Text":"You can even assign the value null to the string"},{"Start":"09:28.730 ","End":"09:31.885","Text":"again if you want to signify that is empty."},{"Start":"09:31.885 ","End":"09:34.445","Text":"Because Java is an object-oriented language,"},{"Start":"09:34.445 ","End":"09:36.740","Text":"we can create objects from our classes."},{"Start":"09:36.740 ","End":"09:39.125","Text":"If the object cannot be created,"},{"Start":"09:39.125 ","End":"09:42.785","Text":"we end up again with a value of null being assigned to the object"},{"Start":"09:42.785 ","End":"09:47.525","Text":"rather than a memory location which contains the object data and code."},{"Start":"09:47.525 ","End":"09:51.361","Text":"Here\u0027s a Java code we saw earlier which tried to open a file."},{"Start":"09:51.361 ","End":"09:55.850","Text":"Here readObject can\u0027t be created if the File ( \"data.txt\" ) is not found,"},{"Start":"09:55.850 ","End":"09:58.945","Text":"so readObject would contain the value null."},{"Start":"09:58.945 ","End":"10:00.860","Text":"We say it has a null reference,"},{"Start":"10:00.860 ","End":"10:03.200","Text":"meaning it refers to nothing valid."},{"Start":"10:03.200 ","End":"10:05.420","Text":"Whether it\u0027s a character and a string,"},{"Start":"10:05.420 ","End":"10:06.500","Text":"a value, and a variable,"},{"Start":"10:06.500 ","End":"10:09.655","Text":"a pointer to some data or a reference to an object,"},{"Start":"10:09.655 ","End":"10:13.250","Text":"null basically is saying there\u0027s nothing here."},{"Start":"10:13.250 ","End":"10:17.975","Text":"Ideally, a program would always continue functioning whatever the circumstances."},{"Start":"10:17.975 ","End":"10:23.105","Text":"We also don\u0027t want to bother use of our programs with error messages if at all possible."},{"Start":"10:23.105 ","End":"10:26.960","Text":"Where the error is to do with something that the user has input or fail to"},{"Start":"10:26.960 ","End":"10:31.040","Text":"input like here where the user\u0027s forgotten to type their email address in,"},{"Start":"10:31.040 ","End":"10:34.760","Text":"it would be reasonable to inform the user."},{"Start":"10:34.760 ","End":"10:39.335","Text":"However, if the problem is something to do with what a programmer has done,"},{"Start":"10:39.335 ","End":"10:41.313","Text":"it\u0027s not the user\u0027s issue."},{"Start":"10:41.313 ","End":"10:43.970","Text":"A string index out of range error is not"},{"Start":"10:43.970 ","End":"10:47.015","Text":"something an end-user understands or can do anything about."},{"Start":"10:47.015 ","End":"10:51.248","Text":"These sorts of errors are intended for the developer of the program,"},{"Start":"10:51.248 ","End":"10:53.785","Text":"and in an ideal world we wouldn\u0027t see them at all."},{"Start":"10:53.785 ","End":"10:55.940","Text":"Using defensive programming techniques,"},{"Start":"10:55.940 ","End":"10:57.665","Text":"we could catch a lot of errors."},{"Start":"10:57.665 ","End":"11:00.815","Text":"For example, by checking our pointers, variables,"},{"Start":"11:00.815 ","End":"11:05.127","Text":"and object references don\u0027t contain null before we try to do something with them."},{"Start":"11:05.127 ","End":"11:07.880","Text":"This does increase development time and the adding of"},{"Start":"11:07.880 ","End":"11:11.840","Text":"instructions slows down the execution of the overall program too,"},{"Start":"11:11.840 ","End":"11:15.095","Text":"but in most cases this may not be noticeable."},{"Start":"11:15.095 ","End":"11:20.405","Text":"Another problem arises which is that by avoiding error messages and potentially crashes,"},{"Start":"11:20.405 ","End":"11:24.290","Text":"we\u0027re hiding the existence of potential flaws in our code"},{"Start":"11:24.290 ","End":"11:28.670","Text":"that might have arisen due to logical errors or poor design."},{"Start":"11:28.670 ","End":"11:32.990","Text":"Ideally, there\u0027d be a balance between keeping the program going and not troubling"},{"Start":"11:32.990 ","End":"11:37.895","Text":"the user with error reports and informing the developer that something\u0027s happened."},{"Start":"11:37.895 ","End":"11:40.790","Text":"Fortunately, there is something that allows this balance to be"},{"Start":"11:40.790 ","End":"11:43.955","Text":"struck, the so-called exception."},{"Start":"11:43.955 ","End":"11:46.700","Text":"We\u0027ll pause here and in the next video we\u0027ll look at"},{"Start":"11:46.700 ","End":"11:51.630","Text":"the concepts and syntax involved in dealing with exceptions."}],"ID":29286},{"Watched":false,"Name":"Exception Handling","Duration":"8m 45s","ChapterTopicVideoID":28093,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.530","Text":"Hello. Welcome back."},{"Start":"00:01.530 ","End":"00:02.550","Text":"In the previous video,"},{"Start":"00:02.550 ","End":"00:06.570","Text":"we reviewed the categories of error that could arise when developing programs."},{"Start":"00:06.570 ","End":"00:09.930","Text":"The need to differentiate between reporting errors to the user of"},{"Start":"00:09.930 ","End":"00:14.040","Text":"a program and reporting errors to the developer of the program."},{"Start":"00:14.040 ","End":"00:17.610","Text":"Also striking a balance between defensive programming"},{"Start":"00:17.610 ","End":"00:21.360","Text":"and revealing flaws in our code that needs to be fixed."},{"Start":"00:21.360 ","End":"00:25.425","Text":"We were told that we could do this by using something called an Exception."},{"Start":"00:25.425 ","End":"00:29.400","Text":"An Exception is a mechanism in high-level programming languages which"},{"Start":"00:29.400 ","End":"00:33.840","Text":"allows management of program failure and reporting on its causes."},{"Start":"00:33.840 ","End":"00:35.810","Text":"That\u0027s a pretty dry description,"},{"Start":"00:35.810 ","End":"00:40.155","Text":"so let\u0027s use a visual analogy to illustrate exceptions."},{"Start":"00:40.155 ","End":"00:44.735","Text":"Some highways feature of runaway vehicle lane on mountainous roads."},{"Start":"00:44.735 ","End":"00:48.530","Text":"It allows vehicles that are experiencing break failure to gradually"},{"Start":"00:48.530 ","End":"00:52.850","Text":"come to a stop without endangering other road users."},{"Start":"00:52.850 ","End":"00:54.470","Text":"If the brakes fail,"},{"Start":"00:54.470 ","End":"00:57.320","Text":"a driver can steer into the runaway lane,"},{"Start":"00:57.320 ","End":"01:00.110","Text":"usually filled with sand and facing upwards on"},{"Start":"01:00.110 ","End":"01:03.215","Text":"a slight incline and the vehicle will slow down."},{"Start":"01:03.215 ","End":"01:05.540","Text":"You could regard the rare occasion that the driver"},{"Start":"01:05.540 ","End":"01:08.525","Text":"needs to use this lane as an exception."},{"Start":"01:08.525 ","End":"01:12.785","Text":"Fortunately, the provision of this road feature means that rather than crash,"},{"Start":"01:12.785 ","End":"01:16.715","Text":"the vehicle comes to a gradual controlled halt."},{"Start":"01:16.715 ","End":"01:21.655","Text":"So it is with a program written in a language that supports Exception handling."},{"Start":"01:21.655 ","End":"01:24.440","Text":"Rather than crash and stop the program,"},{"Start":"01:24.440 ","End":"01:29.765","Text":"we can manage the faults so that the program connects it gracefully without crashing."},{"Start":"01:29.765 ","End":"01:33.055","Text":"It could even recover and continue running."},{"Start":"01:33.055 ","End":"01:35.330","Text":"With the vehicle, a mechanic would"},{"Start":"01:35.330 ","End":"01:38.585","Text":"inspect the brakes to try and work out why they failed."},{"Start":"01:38.585 ","End":"01:42.380","Text":"In a similar way, a programmer can look at data that the exception"},{"Start":"01:42.380 ","End":"01:46.685","Text":"generated and use it to work out why the program failed."},{"Start":"01:46.685 ","End":"01:49.705","Text":"Not all languages support exception handling,"},{"Start":"01:49.705 ","End":"01:51.155","Text":"C being 1 of them,"},{"Start":"01:51.155 ","End":"01:58.040","Text":"but those that do tend to use the try keyword to mark a block of code that may fail."},{"Start":"01:58.040 ","End":"02:03.340","Text":"Java uses the keywords try and catch to mark 2 blocks of code."},{"Start":"02:03.340 ","End":"02:07.430","Text":"The first block marked by the keyword try and a pair of braces,"},{"Start":"02:07.430 ","End":"02:09.290","Text":"is a block of code that contains"},{"Start":"02:09.290 ","End":"02:13.100","Text":"at least 1 line where a failure might be expected to occur."},{"Start":"02:13.100 ","End":"02:16.400","Text":"If nothing fails, then each line will be"},{"Start":"02:16.400 ","End":"02:21.360","Text":"executed in order until the closing bracket is reached,"},{"Start":"02:21.360 ","End":"02:26.420","Text":"and then execution carries on after the catch blocks closing brace."},{"Start":"02:26.420 ","End":"02:28.975","Text":"However, if a line does fail,"},{"Start":"02:28.975 ","End":"02:32.370","Text":"execution continues from the first line of"},{"Start":"02:32.370 ","End":"02:37.924","Text":"the catch block and then all statements inside that block run to completion."},{"Start":"02:37.924 ","End":"02:40.925","Text":"Python uses the same structure and syntax,"},{"Start":"02:40.925 ","End":"02:43.055","Text":"but with the keyword, \u0027Except\u0027,"},{"Start":"02:43.055 ","End":"02:45.425","Text":"instead of Java\u0027s catch."},{"Start":"02:45.425 ","End":"02:47.720","Text":"It\u0027s also possible to distinguish between"},{"Start":"02:47.720 ","End":"02:51.604","Text":"different types of exception and handle them differently."},{"Start":"02:51.604 ","End":"02:55.070","Text":"For example, some code might be attempted to work with"},{"Start":"02:55.070 ","End":"02:58.850","Text":"a file and the exceptions it could encounter might be different."},{"Start":"02:58.850 ","End":"03:03.170","Text":"For example, it may not be able to access the file at all because it doesn\u0027t exist,"},{"Start":"03:03.170 ","End":"03:05.750","Text":"or it finds the file and attempts to read it,"},{"Start":"03:05.750 ","End":"03:07.355","Text":"but the file\u0027s empty,"},{"Start":"03:07.355 ","End":"03:11.615","Text":"or maybe the file is there but the program is not given read permission,"},{"Start":"03:11.615 ","End":"03:15.125","Text":"so is denied access by the operating system."},{"Start":"03:15.125 ","End":"03:18.380","Text":"For this reason, multiple catch blocks can be provided,"},{"Start":"03:18.380 ","End":"03:21.950","Text":"each 1 dealing with a different type of Exception."},{"Start":"03:21.950 ","End":"03:23.795","Text":"In this job, for example,"},{"Start":"03:23.795 ","End":"03:27.785","Text":"the statements in the try block might attempt to open the file and read from it,"},{"Start":"03:27.785 ","End":"03:30.215","Text":"and depending on which Exception was triggered,"},{"Start":"03:30.215 ","End":"03:32.570","Text":"it will be caught by the relevant block."},{"Start":"03:32.570 ","End":"03:37.010","Text":"See how to syntax for catching Java involves an argument being passed into"},{"Start":"03:37.010 ","End":"03:42.320","Text":"the catch block with a parameter named E for Exception by convention."},{"Start":"03:42.320 ","End":"03:45.455","Text":"E will contain details of the exception."},{"Start":"03:45.455 ","End":"03:48.440","Text":"For example, if it was a file, the file name,"},{"Start":"03:48.440 ","End":"03:50.720","Text":"which could be output to a console display or"},{"Start":"03:50.720 ","End":"03:54.605","Text":"a log file for study by the programmers who wrote the code."},{"Start":"03:54.605 ","End":"03:57.335","Text":"The order of the catch blocks is important."},{"Start":"03:57.335 ","End":"04:00.695","Text":"The first matching exception block will be caught."},{"Start":"04:00.695 ","End":"04:05.600","Text":"Say end of file Exception is triggered by a line of code in this try-block."},{"Start":"04:05.600 ","End":"04:09.020","Text":"Each line of code in that catch block will be executed,"},{"Start":"04:09.020 ","End":"04:11.930","Text":"and once all the lines in that block have run,"},{"Start":"04:11.930 ","End":"04:16.460","Text":"execution will carry on from after the last catch block."},{"Start":"04:16.460 ","End":"04:22.970","Text":"An exception type of Exception in Java is a catch-all for all types of exception."},{"Start":"04:22.970 ","End":"04:28.640","Text":"So it shouldn\u0027t be put first as every type of catch block after it will never be reached."},{"Start":"04:28.640 ","End":"04:32.660","Text":"It should be the only or last catch block."},{"Start":"04:32.660 ","End":"04:35.270","Text":"The same general principles apply to Python,"},{"Start":"04:35.270 ","End":"04:37.580","Text":"with the only difference being that instead of brackets,"},{"Start":"04:37.580 ","End":"04:40.885","Text":"and an identifier for a parameter following exception type,"},{"Start":"04:40.885 ","End":"04:43.640","Text":"Python uses the keyword \u0027as\u0027 to associate"},{"Start":"04:43.640 ","End":"04:46.820","Text":"the details of the exception with an identifier."},{"Start":"04:46.820 ","End":"04:51.560","Text":"Now, again, the exception block like Java can be used as a catch-all,"},{"Start":"04:51.560 ","End":"04:55.205","Text":"either alongside other except clauses or instead of them,"},{"Start":"04:55.205 ","End":"04:57.515","Text":"so you could have just the one."},{"Start":"04:57.515 ","End":"05:01.475","Text":"The finally clause allows the programmer to define a block that will"},{"Start":"05:01.475 ","End":"05:05.435","Text":"always run regardless of whether an error happened or not."},{"Start":"05:05.435 ","End":"05:08.270","Text":"If the try-block competes without errors,"},{"Start":"05:08.270 ","End":"05:11.330","Text":"execution will continue into the finally block."},{"Start":"05:11.330 ","End":"05:13.385","Text":"If 1 of the catch blocks is triggered,"},{"Start":"05:13.385 ","End":"05:17.615","Text":"an execution will also continue into the finally block."},{"Start":"05:17.615 ","End":"05:21.800","Text":"It\u0027s a useful mechanism to allow for tidying up operations, for example,"},{"Start":"05:21.800 ","End":"05:26.300","Text":"where a file must be closed or memory must be released back to the operating system."},{"Start":"05:26.300 ","End":"05:30.230","Text":"Even if there was a return statement inside 1 of the trial catch blocks,"},{"Start":"05:30.230 ","End":"05:33.850","Text":"the statements inside finally would still be executed."},{"Start":"05:33.850 ","End":"05:37.010","Text":"This breaks the normal rule that there cannot be any code"},{"Start":"05:37.010 ","End":"05:40.355","Text":"after a return statement because it will never be reached."},{"Start":"05:40.355 ","End":"05:45.185","Text":"The finally clause here will be reached despite the return."},{"Start":"05:45.185 ","End":"05:47.900","Text":"Because finally always executes,"},{"Start":"05:47.900 ","End":"05:50.330","Text":"it\u0027s possible to see a try-block followed by"},{"Start":"05:50.330 ","End":"05:54.080","Text":"a finally block without any catch in-between."},{"Start":"05:54.080 ","End":"05:57.635","Text":"In that case, if some of the try block executes,"},{"Start":"05:57.635 ","End":"05:59.645","Text":"but at 1 particular line,"},{"Start":"05:59.645 ","End":"06:04.655","Text":"there is a failure, execution will continue in the finally block."},{"Start":"06:04.655 ","End":"06:07.430","Text":"The ability to raise an Exception is so useful"},{"Start":"06:07.430 ","End":"06:10.144","Text":"in programs that it even has its own keyword."},{"Start":"06:10.144 ","End":"06:12.670","Text":"Throw in Java,"},{"Start":"06:12.670 ","End":"06:14.700","Text":"or raise in Python."},{"Start":"06:14.700 ","End":"06:16.610","Text":"In a program, you can evaluate"},{"Start":"06:16.610 ","End":"06:20.605","Text":"some expression and use it as the basis to throw an Exception."},{"Start":"06:20.605 ","End":"06:22.775","Text":"Without handling catch block,"},{"Start":"06:22.775 ","End":"06:25.625","Text":"these lines will have the effect of ending the program,"},{"Start":"06:25.625 ","End":"06:29.270","Text":"which might be useful to a programmer in some situations,"},{"Start":"06:29.270 ","End":"06:33.334","Text":"more useful maybe than letting the program continue."},{"Start":"06:33.334 ","End":"06:35.270","Text":"Java is fairly unique among"},{"Start":"06:35.270 ","End":"06:38.720","Text":"high-level languages in that it distinguishes between code that"},{"Start":"06:38.720 ","End":"06:44.425","Text":"must have exception handling code and code that should have exception handling code."},{"Start":"06:44.425 ","End":"06:48.080","Text":"So called checked exceptions relate to code"},{"Start":"06:48.080 ","End":"06:51.695","Text":"that must have Exception Handling code or will not compile."},{"Start":"06:51.695 ","End":"06:56.420","Text":"An example would be many basic file or other input and output operations."},{"Start":"06:56.420 ","End":"06:59.720","Text":"They\u0027re so likely to generate exceptions that they\u0027ve been"},{"Start":"06:59.720 ","End":"07:03.730","Text":"singled out by the compiler to be checked exceptions."},{"Start":"07:03.730 ","End":"07:06.290","Text":"Most other common exceptions in Java, for example,"},{"Start":"07:06.290 ","End":"07:12.050","Text":"null points or number format or out of bounds exceptions will be unchecked exceptions."},{"Start":"07:12.050 ","End":"07:16.535","Text":"If your program doesn\u0027t have any exception handling code around these types of exception,"},{"Start":"07:16.535 ","End":"07:18.695","Text":"the program will still compile,"},{"Start":"07:18.695 ","End":"07:24.080","Text":"but obviously you could potentially see your program and if a runtime error occurs."},{"Start":"07:24.080 ","End":"07:28.370","Text":"It\u0027s also possible to declare in your own Java methods that an exception should be"},{"Start":"07:28.370 ","End":"07:33.650","Text":"checked by adding the throws keyword in the method declaration."},{"Start":"07:33.650 ","End":"07:38.485","Text":"Let\u0027s say we were writing a game which stored 100 highest scores in a file."},{"Start":"07:38.485 ","End":"07:43.500","Text":"Each time we send this method update high scores a single score,"},{"Start":"07:43.500 ","End":"07:47.325","Text":"it adds it to the file if it\u0027s in the top 100 scores."},{"Start":"07:47.325 ","End":"07:51.560","Text":"The throws keyword here is notifying the compiler that it should be"},{"Start":"07:51.560 ","End":"07:57.735","Text":"a checked exception because the code inside the method could throw an IOException,"},{"Start":"07:57.735 ","End":"08:00.905","Text":"for example, if the file can\u0027t be accessed or is corrupted."},{"Start":"08:00.905 ","End":"08:05.450","Text":"If we try to call the update high scores method from somewhere else in our program,"},{"Start":"08:05.450 ","End":"08:07.715","Text":"the compiler will give us an error."},{"Start":"08:07.715 ","End":"08:09.380","Text":"To make the error go away,"},{"Start":"08:09.380 ","End":"08:13.280","Text":"we have to wrap the method inside a try-block and catch it."},{"Start":"08:13.280 ","End":"08:16.790","Text":"The program will then compile without errors."},{"Start":"08:16.790 ","End":"08:18.935","Text":"That\u0027s it for exceptions."},{"Start":"08:18.935 ","End":"08:24.433","Text":"In this section, we learned how to distinguish between the handling of logical syntax,"},{"Start":"08:24.433 ","End":"08:27.440","Text":"and runtime errors to explain the meaning of the term"},{"Start":"08:27.440 ","End":"08:33.185","Text":"defensive programming and to apply different mechanisms for runtime error handling."},{"Start":"08:33.185 ","End":"08:37.145","Text":"You should now have complete the exercises for this section to embed what you\u0027ve learned."},{"Start":"08:37.145 ","End":"08:42.320","Text":"In the next topic, we\u0027ll make use of exception handling to enable us to work with files."},{"Start":"08:42.320 ","End":"08:45.540","Text":"Thanks for watching and see you soon."}],"ID":29287},{"Watched":false,"Name":"Exercise 1","Duration":"9m 17s","ChapterTopicVideoID":28094,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.650","Text":"Hello, welcome to this first exercise on exception handling in which we\u0027ve been"},{"Start":"00:04.650 ","End":"00:09.295","Text":"asked to create a new project and within a class called ExceptionsApp."},{"Start":"00:09.295 ","End":"00:13.754","Text":"In part a, then we\u0027re asked to create a method called lookup calories,"},{"Start":"00:13.754 ","End":"00:19.275","Text":"which returns a double and accepts an int as a parameter called index."},{"Start":"00:19.275 ","End":"00:23.369","Text":"In part b, we\u0027re asked to declare an array of doubles called table,"},{"Start":"00:23.369 ","End":"00:26.100","Text":"and it contains the following values."},{"Start":"00:26.100 ","End":"00:30.810","Text":"In part c, we\u0027re asked to use try and catch blocks to return the value"},{"Start":"00:30.810 ","End":"00:35.460","Text":"pointed to by index or return 0 in the catch block,"},{"Start":"00:35.460 ","End":"00:38.450","Text":"if a runtime exception is thrown along with"},{"Start":"00:38.450 ","End":"00:42.605","Text":"the message \u0027\u0027invalid lookup value 0-7 only\u0027\u0027."},{"Start":"00:42.605 ","End":"00:44.255","Text":"In part d, then finally,"},{"Start":"00:44.255 ","End":"00:47.390","Text":"we\u0027re asked to test our method with the following values."},{"Start":"00:47.390 ","End":"00:52.665","Text":"D1 uses 1 and should return 0.238892."},{"Start":"00:52.665 ","End":"00:55.980","Text":"D2 with 0 should return 0."},{"Start":"00:55.980 ","End":"01:00.855","Text":"D3 with 7 should return 1.672241,"},{"Start":"01:00.855 ","End":"01:03.850","Text":"and D4 with the input of 8,"},{"Start":"01:03.850 ","End":"01:08.750","Text":"should return 0 and report an error to the terminal."},{"Start":"01:08.750 ","End":"01:13.490","Text":"Let\u0027s first create the method that we\u0027ve been asked to create."},{"Start":"01:13.490 ","End":"01:16.550","Text":"I\u0027ve already got my class granted called ExceptionsApp,"},{"Start":"01:16.550 ","End":"01:18.425","Text":"you should know how to do that by now."},{"Start":"01:18.425 ","End":"01:21.320","Text":"Let\u0027s do the method and it\u0027s called"},{"Start":"01:21.320 ","End":"01:29.580","Text":"lookupCalories and it has integer parameter called index,"},{"Start":"01:29.580 ","End":"01:33.110","Text":"so datatype first and then the name of the parameter."},{"Start":"01:33.110 ","End":"01:36.860","Text":"It has a return value of a double."},{"Start":"01:36.860 ","End":"01:40.795","Text":"That goes in front of the function name."},{"Start":"01:40.795 ","End":"01:44.885","Text":"Now we have our method block."},{"Start":"01:44.885 ","End":"01:49.190","Text":"We\u0027ve been asked to declare an array of doubles called table."},{"Start":"01:49.190 ","End":"01:53.360","Text":"An array of doubles is declared that way."},{"Start":"01:53.360 ","End":"01:56.915","Text":"Identifier is table, and rather than type it all out,"},{"Start":"01:56.915 ","End":"02:00.920","Text":"I\u0027ve got that in the keyboard buffer having just copied and pasted it."},{"Start":"02:00.920 ","End":"02:06.080","Text":"By the way, if you ever copy and paste something and it gives you an error at the end,"},{"Start":"02:06.080 ","End":"02:10.100","Text":"it\u0027s quite often because you\u0027ve copied it from a Word document or a PDF."},{"Start":"02:10.100 ","End":"02:12.620","Text":"There\u0027s some stray characters in the end,"},{"Start":"02:12.620 ","End":"02:15.440","Text":"often just deleting past that last character"},{"Start":"02:15.440 ","End":"02:20.090","Text":"and retyping it will get rid of that error if you ever see that."},{"Start":"02:20.090 ","End":"02:22.640","Text":"I\u0027ve got my array setup now,"},{"Start":"02:22.640 ","End":"02:24.080","Text":"so that\u0027s Part b done."},{"Start":"02:24.080 ","End":"02:27.645","Text":"Now the main part of the program is the try and catch block."},{"Start":"02:27.645 ","End":"02:31.919","Text":"We want to write that in 1 overall structure."},{"Start":"02:31.919 ","End":"02:35.610","Text":"There\u0027s the try and here\u0027s the catch."},{"Start":"02:35.610 ","End":"02:41.455","Text":"Catch will require a pair of round brackets as well as the braces."},{"Start":"02:41.455 ","End":"02:48.680","Text":"Inside the round brackets we put the type of exception we\u0027re expecting to catch."},{"Start":"02:48.680 ","End":"02:53.030","Text":"In Java, we can have a generic exception"},{"Start":"02:53.030 ","End":"02:57.110","Text":"called exception that will catch all types of exceptions."},{"Start":"02:57.110 ","End":"03:03.470","Text":"Then you have to put the name you want to use for the parameter inside the catch block."},{"Start":"03:03.470 ","End":"03:05.750","Text":"We\u0027re not going to do anything with it in this exercise,"},{"Start":"03:05.750 ","End":"03:08.705","Text":"but we still need to have this or the compiler will complain."},{"Start":"03:08.705 ","End":"03:11.420","Text":"By convention, we call that parameter e,"},{"Start":"03:11.420 ","End":"03:13.760","Text":"but obviously you can call parameters anything you\u0027d like."},{"Start":"03:13.760 ","End":"03:16.285","Text":"There\u0027s that 2 blocks,"},{"Start":"03:16.285 ","End":"03:18.260","Text":"sorted, try and catch."},{"Start":"03:18.260 ","End":"03:20.540","Text":"What is it that we\u0027re actually trying to do?"},{"Start":"03:20.540 ","End":"03:29.155","Text":"All we\u0027re going to do is we\u0027re going to return the value from table pointed to by index."},{"Start":"03:29.155 ","End":"03:31.760","Text":"That\u0027s all we need to do."},{"Start":"03:31.760 ","End":"03:37.340","Text":"We want to return one of these values depending on which index we\u0027ve used,"},{"Start":"03:37.340 ","End":"03:38.990","Text":"so 0 will return that one,"},{"Start":"03:38.990 ","End":"03:40.190","Text":"1 would return that one,"},{"Start":"03:40.190 ","End":"03:41.750","Text":"2 that one and so on."},{"Start":"03:41.750 ","End":"03:45.140","Text":"However, the issue here is that if we try and"},{"Start":"03:45.140 ","End":"03:49.310","Text":"use an index that\u0027s greater than the last element."},{"Start":"03:49.310 ","End":"03:51.350","Text":"We\u0027ve got 8 elements here,"},{"Start":"03:51.350 ","End":"03:53.510","Text":"but a numbered from 0-7."},{"Start":"03:53.510 ","End":"03:55.535","Text":"This one will be 7, if we try and 8,"},{"Start":"03:55.535 ","End":"03:57.635","Text":"there isn\u0027t a ninth element,"},{"Start":"03:57.635 ","End":"04:01.950","Text":"so we would be generating a out-of-bounds error,"},{"Start":"04:01.950 ","End":"04:04.175","Text":"and we don\u0027t want that to happen,"},{"Start":"04:04.175 ","End":"04:07.835","Text":"and hence the catch is going to take care of that situation."},{"Start":"04:07.835 ","End":"04:10.790","Text":"If we try to compile now and it wouldn\u0027t let us"},{"Start":"04:10.790 ","End":"04:13.700","Text":"because it knows that this can return a value."},{"Start":"04:13.700 ","End":"04:15.140","Text":"But what about this one?"},{"Start":"04:15.140 ","End":"04:17.690","Text":"We are expected to return a double."},{"Start":"04:17.690 ","End":"04:20.525","Text":"If we don\u0027t return a valid value here,"},{"Start":"04:20.525 ","End":"04:23.600","Text":"we got to return something here as well."},{"Start":"04:23.600 ","End":"04:30.005","Text":"We\u0027ve been asked in this to return a 0 in the catch block,"},{"Start":"04:30.005 ","End":"04:32.795","Text":"if we do have an exception caught."},{"Start":"04:32.795 ","End":"04:35.619","Text":"That would be a except for the fact we\u0027ve got to print a message."},{"Start":"04:35.619 ","End":"04:38.375","Text":"Now, I\u0027m not able to print the message here."},{"Start":"04:38.375 ","End":"04:40.220","Text":"If I try to do this,"},{"Start":"04:40.220 ","End":"04:41.930","Text":"what would happen is I\u0027d get"},{"Start":"04:41.930 ","End":"04:45.650","Text":"a error from the compiler saying this line will never be reached,"},{"Start":"04:45.650 ","End":"04:48.140","Text":"because remember, return does 2 things."},{"Start":"04:48.140 ","End":"04:51.035","Text":"It returns a value 0 in this case,"},{"Start":"04:51.035 ","End":"04:53.120","Text":"but it also exits the function."},{"Start":"04:53.120 ","End":"04:57.980","Text":"It never get to this line and the compiler would complain. I\u0027m going to type it here."},{"Start":"04:57.980 ","End":"05:01.910","Text":"I\u0027m going to move it up in a second just to"},{"Start":"05:01.910 ","End":"05:06.665","Text":"prove that it is necessary to have it before the return."},{"Start":"05:06.665 ","End":"05:10.850","Text":"The message I\u0027m supposed to print is invalid."},{"Start":"05:10.850 ","End":"05:16.080","Text":"Lookup value 0-7 only."},{"Start":"05:16.080 ","End":"05:17.960","Text":"That should be it."},{"Start":"05:17.960 ","End":"05:20.780","Text":"Now let\u0027s see if it compiles."},{"Start":"05:20.780 ","End":"05:22.580","Text":"It has found 2 errors."},{"Start":"05:22.580 ","End":"05:26.165","Text":"First one is missing return statement,"},{"Start":"05:26.165 ","End":"05:28.970","Text":"and the second one is unreachable statements."},{"Start":"05:28.970 ","End":"05:30.950","Text":"This is the one that I was saying."},{"Start":"05:30.950 ","End":"05:34.250","Text":"It\u0027s unreachable because it\u0027s after the return."},{"Start":"05:34.250 ","End":"05:38.150","Text":"Let\u0027s move that up above the return."},{"Start":"05:38.150 ","End":"05:40.775","Text":"Let\u0027s see if it\u0027s happier with that."},{"Start":"05:40.775 ","End":"05:43.190","Text":"It\u0027s got rid of both errors now."},{"Start":"05:43.190 ","End":"05:47.450","Text":"As I said, you have to get the order right because you can\u0027t"},{"Start":"05:47.450 ","End":"05:51.350","Text":"execute something after a return statement, it will never be reached."},{"Start":"05:51.350 ","End":"05:53.225","Text":"That\u0027s what the compiler was complaining about it."},{"Start":"05:53.225 ","End":"05:55.265","Text":"We\u0027re done now we just need to test."},{"Start":"05:55.265 ","End":"05:58.789","Text":"In part D, we\u0027re asked to use various values."},{"Start":"05:58.789 ","End":"06:03.690","Text":"The first one of which is 1."},{"Start":"06:03.690 ","End":"06:06.655","Text":"I\u0027ll look up from index 1."},{"Start":"06:06.655 ","End":"06:12.920","Text":"We get a value in the array at position 2 because we were counting from 0,"},{"Start":"06:12.920 ","End":"06:15.050","Text":"so the second element in that array."},{"Start":"06:15.050 ","End":"06:17.870","Text":"Great, that\u0027s what we expected."},{"Start":"06:17.870 ","End":"06:20.420","Text":"Let\u0027s try position 0,"},{"Start":"06:20.420 ","End":"06:22.235","Text":"which is a valid position."},{"Start":"06:22.235 ","End":"06:23.645","Text":"It just gives us 0,"},{"Start":"06:23.645 ","End":"06:25.775","Text":"fine, that\u0027s what we expected as well."},{"Start":"06:25.775 ","End":"06:27.685","Text":"Then Part D3,"},{"Start":"06:27.685 ","End":"06:29.310","Text":"we are asked to put in 7,"},{"Start":"06:29.310 ","End":"06:31.650","Text":"that\u0027s the last element in the array."},{"Start":"06:31.650 ","End":"06:34.940","Text":"We get a last value in that lookup table."},{"Start":"06:34.940 ","End":"06:36.230","Text":"This lookup table, by the way,"},{"Start":"06:36.230 ","End":"06:41.330","Text":"is returning conversion from kilojoules to calories,"},{"Start":"06:41.330 ","End":"06:45.955","Text":"so different measures in different units."},{"Start":"06:45.955 ","End":"06:49.420","Text":"Last one is to test it with 8,"},{"Start":"06:49.420 ","End":"06:53.675","Text":"which is obviously an error because there isn\u0027t a ninth element in the array,"},{"Start":"06:53.675 ","End":"06:56.000","Text":"so we would normally get an error here,"},{"Start":"06:56.000 ","End":"06:58.430","Text":"but with luck, we won\u0027t."},{"Start":"06:58.430 ","End":"07:03.765","Text":"We get a message printed to the console which is invalid lookup value 0-7,"},{"Start":"07:03.765 ","End":"07:05.720","Text":"but we don\u0027t actually get a runtime error."},{"Start":"07:05.720 ","End":"07:09.380","Text":"We\u0027ve caught the error, and our programs continued on as"},{"Start":"07:09.380 ","End":"07:14.075","Text":"normal and it\u0027s exited as normal without generating any error messages."},{"Start":"07:14.075 ","End":"07:16.745","Text":"We did get a value returned of 0,"},{"Start":"07:16.745 ","End":"07:20.884","Text":"which gets returned if it\u0027s an invalid index."},{"Start":"07:20.884 ","End":"07:23.000","Text":"That\u0027s basically done what we wanted to."},{"Start":"07:23.000 ","End":"07:25.385","Text":"That\u0027s very simple example."},{"Start":"07:25.385 ","End":"07:30.560","Text":"Let me just prove that this exception here would catch all exceptions."},{"Start":"07:30.560 ","End":"07:34.985","Text":"But, the one we were looking for was actually an index out"},{"Start":"07:34.985 ","End":"07:41.225","Text":"of bounds exception which also exists in Java."},{"Start":"07:41.225 ","End":"07:43.130","Text":"Let\u0027s see if it still works."},{"Start":"07:43.130 ","End":"07:46.910","Text":"If I do that, I\u0027m going to try with 8 again"},{"Start":"07:46.910 ","End":"07:50.910","Text":"or 9 I could try is obviously not a 10th one either."},{"Start":"07:50.910 ","End":"07:56.240","Text":"Let\u0027s try 9. I still get the same message and it still worked as I expected to."},{"Start":"07:56.240 ","End":"08:00.845","Text":"Now what would happen if I didn\u0027t have the try-catch block here?"},{"Start":"08:00.845 ","End":"08:02.690","Text":"Well, let\u0027s try it."},{"Start":"08:02.690 ","End":"08:04.310","Text":"Take out a try,"},{"Start":"08:04.310 ","End":"08:07.910","Text":"take out a catch that I can take all of this out,"},{"Start":"08:07.910 ","End":"08:09.530","Text":"it won\u0027t return a 0,"},{"Start":"08:09.530 ","End":"08:11.210","Text":"it will return a value,"},{"Start":"08:11.210 ","End":"08:12.980","Text":"or it will give me an error message."},{"Start":"08:12.980 ","End":"08:16.280","Text":"Let\u0027s see if that does do what we expect."},{"Start":"08:16.280 ","End":"08:20.590","Text":"Carry on working as normal for valid values."},{"Start":"08:20.590 ","End":"08:23.015","Text":"I put 1 in, returns me the value."},{"Start":"08:23.015 ","End":"08:27.230","Text":"But, let\u0027s try an 8 and see what happens this time."},{"Start":"08:27.230 ","End":"08:29.870","Text":"It actually takes me back to the editor,"},{"Start":"08:29.870 ","End":"08:34.445","Text":"but you see that down the bottom here it says index 8 out of bounds for length."},{"Start":"08:34.445 ","End":"08:36.350","Text":"It\u0027s told me what sort of error is."},{"Start":"08:36.350 ","End":"08:39.655","Text":"And it says array index out of bounds exception."},{"Start":"08:39.655 ","End":"08:42.725","Text":"If I look behind in the terminal window,"},{"Start":"08:42.725 ","End":"08:45.500","Text":"the output from the terminal comes up here,"},{"Start":"08:45.500 ","End":"08:50.450","Text":"but runtime error messages come up at the bottom here in red, in blue jay."},{"Start":"08:50.450 ","End":"08:53.750","Text":"You can see it\u0027s giving me the same message"},{"Start":"08:53.750 ","End":"08:57.965","Text":"really essentially they\u0027re saying it\u0027s an out-of-bounds exception."},{"Start":"08:57.965 ","End":"09:01.760","Text":"We\u0027ve managed to catch with our previous code"},{"Start":"09:01.760 ","End":"09:06.740","Text":"the error and exit gracefully and even returned as a value of 0,"},{"Start":"09:06.740 ","End":"09:09.260","Text":"and printed a friendly warning message to"},{"Start":"09:09.260 ","End":"09:13.055","Text":"the console without try and catch blocks without it."},{"Start":"09:13.055 ","End":"09:14.900","Text":"This is what we would\u0027ve expected."},{"Start":"09:14.900 ","End":"09:18.600","Text":"That\u0027s it for this one. I\u0027ll see you soon for the next one."}],"ID":29288},{"Watched":false,"Name":"Exercise 2","Duration":"10m 54s","ChapterTopicVideoID":28095,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.670","Text":"Hello again, in this next exercise,"},{"Start":"00:02.670 ","End":"00:05.820","Text":"we\u0027ve been asked to create a new method called addHexNumbers."},{"Start":"00:05.820 ","End":"00:09.825","Text":"It takes 2 strings as arguments and returns a string,"},{"Start":"00:09.825 ","End":"00:12.270","Text":"which is the sum of the 2 arguments."},{"Start":"00:12.270 ","End":"00:16.125","Text":"All of that is interpreted and returned as hex."},{"Start":"00:16.125 ","End":"00:19.440","Text":"In Part A, we\u0027re asked to use the codepad to experiment"},{"Start":"00:19.440 ","End":"00:23.010","Text":"with Integer.parseInt using 2 parameters."},{"Start":"00:23.010 ","End":"00:26.025","Text":"The first is a string containing a number to be parsed,"},{"Start":"00:26.025 ","End":"00:28.920","Text":"and the second is an int for the number base, i.e."},{"Start":"00:28.920 ","End":"00:30.180","Text":"we use 2 for binary,"},{"Start":"00:30.180 ","End":"00:33.330","Text":"8 for octal and 16 for hex."},{"Start":"00:33.330 ","End":"00:34.650","Text":"In Part A2,"},{"Start":"00:34.650 ","End":"00:38.505","Text":"we are asked to experiment with Integer.toHexstring in codepad."},{"Start":"00:38.505 ","End":"00:41.195","Text":"That takes 1 parameter, an integer,"},{"Start":"00:41.195 ","End":"00:45.305","Text":"which it converts to a string representation in hex."},{"Start":"00:45.305 ","End":"00:50.653","Text":"In Part B, we use exception handling to trap any number format exceptions,"},{"Start":"00:50.653 ","End":"00:56.360","Text":"and to print in B1 a message saying please enter digits only, and in B2,"},{"Start":"00:56.360 ","End":"00:59.600","Text":"the value that\u0027s generated the exception using"},{"Start":"00:59.600 ","End":"01:03.935","Text":"the getMessage method that\u0027s available to all exceptions."},{"Start":"01:03.935 ","End":"01:06.050","Text":"In Part C, if there\u0027s no exception,"},{"Start":"01:06.050 ","End":"01:11.090","Text":"we return a string representation of the 2 numbers added together in hex."},{"Start":"01:11.090 ","End":"01:12.320","Text":"Otherwise, we return"},{"Start":"01:12.320 ","End":"01:18.000","Text":"the literal string error and in Part D we test with the following values,"},{"Start":"01:18.000 ","End":"01:22.905","Text":"strings of 23 and 79 return a string of 9c,"},{"Start":"01:22.905 ","End":"01:24.585","Text":"strings of 23,"},{"Start":"01:24.585 ","End":"01:29.040","Text":"and e returns the string 31, and finally,"},{"Start":"01:29.040 ","End":"01:34.380","Text":"strings of 23 and g returns error and prints,"},{"Start":"01:34.380 ","End":"01:36.845","Text":"please enter digits only."},{"Start":"01:36.845 ","End":"01:44.440","Text":"We\u0027ve been asked first in the exercise to create a method called addHexNumbers,"},{"Start":"01:44.440 ","End":"01:47.055","Text":"and it takes 2 strings."},{"Start":"01:47.055 ","End":"01:50.170","Text":"I\u0027m going to call my first one num1."},{"Start":"01:50.170 ","End":"01:53.930","Text":"Then you need a comma to separate multiple parameters,"},{"Start":"01:53.930 ","End":"01:56.420","Text":"and you put the datatype again,"},{"Start":"01:56.420 ","End":"02:00.275","Text":"it could be a different datatype because this is both strings."},{"Start":"02:00.275 ","End":"02:02.825","Text":"But I still need a datatype there."},{"Start":"02:02.825 ","End":"02:07.880","Text":"There\u0027s my method, but I also need a return value or return datatype,"},{"Start":"02:07.880 ","End":"02:11.180","Text":"and in this case it\u0027s a string we\u0027ve been asked to create."},{"Start":"02:11.180 ","End":"02:14.205","Text":"Now, in Part A1,"},{"Start":"02:14.205 ","End":"02:19.410","Text":"we\u0027ve been asked to use the codepad to experiment with Integer.parseInt."},{"Start":"02:19.410 ","End":"02:21.585","Text":"Let\u0027s do that,"},{"Start":"02:21.585 ","End":"02:27.615","Text":"Integer.parseInt gives us a integer back from a string,"},{"Start":"02:27.615 ","End":"02:34.595","Text":"and we can even tell it what number base to use when it\u0027s interpreting that string."},{"Start":"02:34.595 ","End":"02:37.015","Text":"If for example,"},{"Start":"02:37.015 ","End":"02:43.280","Text":"I use the number 10 in binary base 2,"},{"Start":"02:43.280 ","End":"02:50.215","Text":"we\u0027d expect that to be the number 2 plus 10 is 1 log 2 and 0 log of 1."},{"Start":"02:50.215 ","End":"02:55.040","Text":"When I run Integer.parseInt with 10 as the string that\u0027s input,"},{"Start":"02:55.040 ","End":"02:58.085","Text":"and a base of 2, the number I get back is 2."},{"Start":"02:58.085 ","End":"03:01.135","Text":"Now, if I change that to base 10,"},{"Start":"03:01.135 ","End":"03:03.700","Text":"then 10 is the number 10,"},{"Start":"03:03.700 ","End":"03:04.775","Text":"and that\u0027s what I get."},{"Start":"03:04.775 ","End":"03:07.220","Text":"I get an integer of 10."},{"Start":"03:07.220 ","End":"03:10.895","Text":"But what if I put it in hexadecimal base 16,"},{"Start":"03:10.895 ","End":"03:17.165","Text":"10 is the number 16 because it\u0027s 1 log of 16 and 0 units."},{"Start":"03:17.165 ","End":"03:21.740","Text":"That\u0027s Integer.parseInt a nice easy way of us interpreting"},{"Start":"03:21.740 ","End":"03:27.470","Text":"a string no matter what the number base is and then turning it into an integer."},{"Start":"03:27.470 ","End":"03:32.840","Text":"I can now do calculations with these numbers once I\u0027ve converted them into an integer."},{"Start":"03:32.840 ","End":"03:34.850","Text":"That\u0027s Part A1,"},{"Start":"03:34.850 ","End":"03:39.500","Text":"what about Integer.toHexString Part 2,"},{"Start":"03:39.500 ","End":"03:45.620","Text":"so Integer.toHexString, and then a pair of brackets."},{"Start":"03:45.620 ","End":"03:48.605","Text":"It says here takes 1 parameter, an integer,"},{"Start":"03:48.605 ","End":"03:52.340","Text":"which will convert to a string representation in hex."},{"Start":"03:52.340 ","End":"03:54.145","Text":"If I put 2 here,"},{"Start":"03:54.145 ","End":"03:59.205","Text":"it returns me a string containing 2."},{"Start":"03:59.205 ","End":"04:02.375","Text":"If I put 234,"},{"Start":"04:02.375 ","End":"04:07.400","Text":"it converts 234 decimal into ea,"},{"Start":"04:07.400 ","End":"04:10.070","Text":"which is the hexadecimal representation,"},{"Start":"04:10.070 ","End":"04:12.410","Text":"but it also is converted into a string."},{"Start":"04:12.410 ","End":"04:16.515","Text":"That\u0027s a nice, easy method to play around with."},{"Start":"04:16.515 ","End":"04:21.020","Text":"It should be enough now for us to work out how to write this method,"},{"Start":"04:21.020 ","End":"04:24.260","Text":"which takes the 2 strings as arguments and returns"},{"Start":"04:24.260 ","End":"04:27.830","Text":"a string which is the sum of those 2 arguments,"},{"Start":"04:27.830 ","End":"04:30.320","Text":"all interpreted and returned as hex."},{"Start":"04:30.320 ","End":"04:36.420","Text":"If we were going to add the number 5 to the number 6,"},{"Start":"04:36.420 ","End":"04:41.510","Text":"clearly that\u0027s not going to be a problem in integer terms it\u0027s 11."},{"Start":"04:41.510 ","End":"04:46.460","Text":"But what if we wanted to convert that to a hexadecimal representation?"},{"Start":"04:46.460 ","End":"04:51.335","Text":"Well, let\u0027s try putting that expression inside function here,"},{"Start":"04:51.335 ","End":"04:56.065","Text":"and we get B, which is indeed 11 in hexadecimal."},{"Start":"04:56.065 ","End":"05:00.000","Text":"That\u0027s great. We can crack on with our code now."},{"Start":"05:00.000 ","End":"05:02.415","Text":"What we\u0027re going to do is,"},{"Start":"05:02.415 ","End":"05:07.490","Text":"let\u0027s get the return statement in shape first of all,"},{"Start":"05:07.490 ","End":"05:11.765","Text":"as far as we can before we deal with the exception handling."},{"Start":"05:11.765 ","End":"05:16.580","Text":"What we want to do is we want to return the first number,"},{"Start":"05:16.580 ","End":"05:19.550","Text":"which is num1, and we want to turn it into an integer,"},{"Start":"05:19.550 ","End":"05:21.530","Text":"and when we add it to num2."},{"Start":"05:21.530 ","End":"05:24.860","Text":"But, the number we need to interpret as"},{"Start":"05:24.860 ","End":"05:28.805","Text":"a hexadecimal number so we\u0027re going to put 16 there."},{"Start":"05:28.805 ","End":"05:34.820","Text":"Then we\u0027re going to do the same thing for the other parameter num2,"},{"Start":"05:34.820 ","End":"05:40.175","Text":"convert it, and interpret it as a hexadecimal number."},{"Start":"05:40.175 ","End":"05:44.120","Text":"If we add those 2 numbers together and return it,"},{"Start":"05:44.120 ","End":"05:46.595","Text":"we will have done what we\u0027ve been asked to do."},{"Start":"05:46.595 ","End":"05:50.120","Text":"But actually, this will be an integer that it"},{"Start":"05:50.120 ","End":"05:54.800","Text":"returns because this generates an integer and it add it to another integer,"},{"Start":"05:54.800 ","End":"05:56.570","Text":"we\u0027re returning therefore an integer,"},{"Start":"05:56.570 ","End":"05:58.970","Text":"but we actually wanted to return a string."},{"Start":"05:58.970 ","End":"06:01.730","Text":"What we\u0027ve got to do is we\u0027ve got to pass"},{"Start":"06:01.730 ","End":"06:07.590","Text":"this whole expression here to Integer.toHexString."},{"Start":"06:09.160 ","End":"06:16.115","Text":"What that will do is it\u0027ll take the resulting integer in here,"},{"Start":"06:16.115 ","End":"06:18.815","Text":"and it will convert that to a string,"},{"Start":"06:18.815 ","End":"06:21.635","Text":"and into a hex string at that,"},{"Start":"06:21.635 ","End":"06:24.710","Text":"and then that\u0027s what\u0027s going to be returned."},{"Start":"06:24.710 ","End":"06:28.820","Text":"This should compile now and thus without any errors."},{"Start":"06:28.820 ","End":"06:31.400","Text":"But, it still doesn\u0027t do what we want it to do because it doesn\u0027t"},{"Start":"06:31.400 ","End":"06:34.355","Text":"do the exception handling side of things."},{"Start":"06:34.355 ","End":"06:37.955","Text":"We want to wrap this around, try and catch."},{"Start":"06:37.955 ","End":"06:46.065","Text":"Because the number that we send to Integer.toHexString might not be a number,"},{"Start":"06:46.065 ","End":"06:51.740","Text":"it might be just be a random collection of characters so it will fail,"},{"Start":"06:51.740 ","End":"06:54.080","Text":"and quite rightly it will fail."},{"Start":"06:54.080 ","End":"06:56.705","Text":"If we do a try and catch block,"},{"Start":"06:56.705 ","End":"06:59.400","Text":"we can try and get around that."},{"Start":"06:59.980 ","End":"07:07.430","Text":"The catch here is going to be a number format exception."},{"Start":"07:07.430 ","End":"07:09.965","Text":"Let\u0027s put that down there."},{"Start":"07:09.965 ","End":"07:13.235","Text":"What are we going to do when we catch this exception?"},{"Start":"07:13.235 ","End":"07:15.965","Text":"We\u0027re going to output it says in B1,"},{"Start":"07:15.965 ","End":"07:19.400","Text":"a message saying please enter digits only."},{"Start":"07:19.400 ","End":"07:21.915","Text":"We still need to return something,"},{"Start":"07:21.915 ","End":"07:24.560","Text":"and it tells us in Part C what to do."},{"Start":"07:24.560 ","End":"07:28.480","Text":"But let\u0027s also finish off Part B first."},{"Start":"07:28.480 ","End":"07:30.995","Text":"I\u0027ll just indent one I\u0027ve got here."},{"Start":"07:30.995 ","End":"07:33.770","Text":"What we need to also do in Part B2 is"},{"Start":"07:33.770 ","End":"07:38.205","Text":"to output the value that\u0027s generated the exception,"},{"Start":"07:38.205 ","End":"07:39.900","Text":"using the getMessage methods."},{"Start":"07:39.900 ","End":"07:43.535","Text":"You remember up here we had this parameter e,"},{"Start":"07:43.535 ","End":"07:48.679","Text":"e turns out to be very handy and contains all sorts of things,"},{"Start":"07:48.679 ","End":"07:53.270","Text":"one of which is a method called getMessage."},{"Start":"07:53.270 ","End":"07:55.850","Text":"If we just say e.getMessage,"},{"Start":"07:55.850 ","End":"08:00.800","Text":"we will return some further detail on what it actually"},{"Start":"08:00.800 ","End":"08:05.570","Text":"was that caused that exception to be thrown."},{"Start":"08:05.570 ","End":"08:09.260","Text":"That\u0027s us done on Part B2,"},{"Start":"08:09.260 ","End":"08:11.810","Text":"sort of, we\u0027ll come back to it in a second."},{"Start":"08:11.810 ","End":"08:13.130","Text":"It says in Part C,"},{"Start":"08:13.130 ","End":"08:14.930","Text":"if there\u0027s no exception to return"},{"Start":"08:14.930 ","End":"08:19.025","Text":"a string representation of the 2 numbers, which we have done."},{"Start":"08:19.025 ","End":"08:21.815","Text":"But if there is an exception,"},{"Start":"08:21.815 ","End":"08:26.060","Text":"we return the literal string error."},{"Start":"08:26.060 ","End":"08:27.800","Text":"If I try to compile,"},{"Start":"08:27.800 ","End":"08:31.205","Text":"this gives me an error because it was returning a value for the try block,"},{"Start":"08:31.205 ","End":"08:33.800","Text":"but it\u0027s not returning a value for the catch block,"},{"Start":"08:33.800 ","End":"08:37.460","Text":"and it\u0027s quite rightly saying there\u0027s a missing return statements,"},{"Start":"08:37.460 ","End":"08:41.135","Text":"so I don\u0027t need to return something and what I\u0027m going to return"},{"Start":"08:41.135 ","End":"08:45.890","Text":"is a string containing the characters error."},{"Start":"08:45.890 ","End":"08:48.995","Text":"That should now compile and thus,"},{"Start":"08:48.995 ","End":"08:52.115","Text":"and so I can now finally move on to Part D,"},{"Start":"08:52.115 ","End":"08:54.250","Text":"which is to test this."},{"Start":"08:54.250 ","End":"08:56.965","Text":"Let\u0027s generate an object,"},{"Start":"08:56.965 ","End":"08:58.505","Text":"so we can run our method."},{"Start":"08:58.505 ","End":"09:00.740","Text":"Here we go with addHexNumbers."},{"Start":"09:00.740 ","End":"09:04.300","Text":"The first one has asked us to add is 23,"},{"Start":"09:04.300 ","End":"09:06.410","Text":"and to add that to 79,"},{"Start":"09:06.410 ","End":"09:08.420","Text":"remember you got to express these as strings,"},{"Start":"09:08.420 ","End":"09:12.810","Text":"so you got to put quotes around them in this workbench a dialogue."},{"Start":"09:12.810 ","End":"09:18.065","Text":"If we run that, we also get string back, which is 9c."},{"Start":"09:18.065 ","End":"09:20.210","Text":"If you add these 2 characters together,"},{"Start":"09:20.210 ","End":"09:22.700","Text":"the 2 and the 7, you get 9."},{"Start":"09:22.700 ","End":"09:25.590","Text":"If you add the 3 and the 9 together, you get 12,"},{"Start":"09:25.590 ","End":"09:27.510","Text":"which is C in hexadecimal,"},{"Start":"09:27.510 ","End":"09:29.190","Text":"so 9c sounds right."},{"Start":"09:29.190 ","End":"09:33.000","Text":"That\u0027s great, and that\u0027s what we\u0027re expecting in Part D1."},{"Start":"09:33.000 ","End":"09:39.145","Text":"In Part D2, we\u0027re expecting to get the hexadecimal characters 31,"},{"Start":"09:39.145 ","End":"09:41.685","Text":"and we put in 23,"},{"Start":"09:41.685 ","End":"09:44.415","Text":"and e, and we do."},{"Start":"09:44.415 ","End":"09:45.960","Text":"That\u0027s fine as well."},{"Start":"09:45.960 ","End":"09:49.560","Text":"Now, we\u0027re going to try and cause the exception."},{"Start":"09:49.560 ","End":"09:54.290","Text":"If we put 23 in here and G in here,"},{"Start":"09:54.290 ","End":"09:55.850","Text":"or H or I or J,"},{"Start":"09:55.850 ","End":"10:00.290","Text":"or anything which is not a valid hexadecimal character."},{"Start":"10:00.290 ","End":"10:04.185","Text":"It should return the string error,"},{"Start":"10:04.185 ","End":"10:06.965","Text":"and we should see a message over here in the console,"},{"Start":"10:06.965 ","End":"10:10.324","Text":"and it does, and we\u0027ve got 2 messages in the console."},{"Start":"10:10.324 ","End":"10:14.945","Text":"This one we generated using just a literal string,"},{"Start":"10:14.945 ","End":"10:19.055","Text":"but this one is what\u0027s coming back from getMessage,"},{"Start":"10:19.055 ","End":"10:22.655","Text":"and that\u0027s what these 2 lines here we\u0027re doing."},{"Start":"10:22.655 ","End":"10:25.190","Text":"That getMessage goes a little bit further detail"},{"Start":"10:25.190 ","End":"10:27.200","Text":"on what it was that actually generated it."},{"Start":"10:27.200 ","End":"10:28.940","Text":"It was that character I typed in,"},{"Start":"10:28.940 ","End":"10:32.900","Text":"which is g, which it cannot convert into an integer."},{"Start":"10:32.900 ","End":"10:35.630","Text":"It\u0027s obviously not a number."},{"Start":"10:35.630 ","End":"10:39.350","Text":"Another useful trap of an error here,"},{"Start":"10:39.350 ","End":"10:41.780","Text":"so my program is still running quite happily."},{"Start":"10:41.780 ","End":"10:45.905","Text":"It\u0027s not completely bombed the whole program and system out,"},{"Start":"10:45.905 ","End":"10:51.055","Text":"and I got an error message instead of crashing with a runtime error,"},{"Start":"10:51.055 ","End":"10:55.580","Text":"so that\u0027s it for this one and I\u0027ll see you in the next one."}],"ID":29289},{"Watched":false,"Name":"Exercise 3","Duration":"13m 29s","ChapterTopicVideoID":28088,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.490","Text":"Hello, welcome back. In this exercise,"},{"Start":"00:02.490 ","End":"00:05.640","Text":"we\u0027ve been asked to create a new method called divide numbers."},{"Start":"00:05.640 ","End":"00:10.350","Text":"Again, it takes 2 strings as arguments and returns a string."},{"Start":"00:10.350 ","End":"00:15.435","Text":"In Part A, we\u0027re asked to return as a string representation in decimal,"},{"Start":"00:15.435 ","End":"00:19.170","Text":"the first argument divided by the second argument."},{"Start":"00:19.170 ","End":"00:23.235","Text":"We interpret the inputs as ints in decimal."},{"Start":"00:23.235 ","End":"00:26.400","Text":"In Part B, we use exception handling to trap"},{"Start":"00:26.400 ","End":"00:28.860","Text":"an arithmetic exception returning"},{"Start":"00:28.860 ","End":"00:33.075","Text":"the string divide by 0 not allowed in integer arithmetic."},{"Start":"00:33.075 ","End":"00:36.680","Text":"In Part B2, a number format exception returning"},{"Start":"00:36.680 ","End":"00:40.925","Text":"the string inputs must be numbers in decimal only."},{"Start":"00:40.925 ","End":"00:43.610","Text":"In Part C, we create another method called"},{"Start":"00:43.610 ","End":"00:47.150","Text":"test divide with no arguments or return values,"},{"Start":"00:47.150 ","End":"00:50.210","Text":"and see when we use Scanner to get 2 integers from"},{"Start":"00:50.210 ","End":"00:54.170","Text":"the keyboard and C2 we call the divide numbers method,"},{"Start":"00:54.170 ","End":"00:59.855","Text":"passing the 2 numbers that we were input from the keyboard in Part 1 to the method."},{"Start":"00:59.855 ","End":"01:04.405","Text":"In Part C3, we output the return string to the screen."},{"Start":"01:04.405 ","End":"01:10.985","Text":"In C4, we continue doing this until the 2 inputs are entered as minus 1 and minus 1."},{"Start":"01:10.985 ","End":"01:16.270","Text":"In Part C, we run test divide and test with the input values of 12"},{"Start":"01:16.270 ","End":"01:21.490","Text":"and 3 to get the output for 3.14 and 10, which should output."},{"Start":"01:21.490 ","End":"01:25.450","Text":"Inputs must be numbers in decimal only and 10 and"},{"Start":"01:25.450 ","End":"01:31.135","Text":"0 outputs divide by 0 not allowed in integer arithmetic."},{"Start":"01:31.135 ","End":"01:32.755","Text":"Finally, in Part D,"},{"Start":"01:32.755 ","End":"01:37.180","Text":"we\u0027re asked to remove 1 of the catch blocks and to run an appropriate test again,"},{"Start":"01:37.180 ","End":"01:42.860","Text":"and to consider what difference the exception block makes to the flow of the program."},{"Start":"01:42.860 ","End":"01:46.929","Text":"Let\u0027s create then the method called divide numbers."},{"Start":"01:46.929 ","End":"01:50.170","Text":"This time, we\u0027re also taking 2 strings"},{"Start":"01:50.170 ","End":"01:53.725","Text":"in the numbers that we\u0027re going to divide 1 by the other,"},{"Start":"01:53.725 ","End":"01:57.470","Text":"so let\u0027s call them num1 and num2 again."},{"Start":"01:57.470 ","End":"02:02.460","Text":"We return this time as well a string,"},{"Start":"02:02.460 ","End":"02:07.895","Text":"and we\u0027ve been asked to interpret these inputs as integers and decimal."},{"Start":"02:07.895 ","End":"02:12.050","Text":"We all need to do is to return the string representation in decimal."},{"Start":"02:12.050 ","End":"02:16.230","Text":"The first argument divided by the second argument is Part A."},{"Start":"02:16.230 ","End":"02:18.725","Text":"Let\u0027s think about how we would do that."},{"Start":"02:18.725 ","End":"02:22.640","Text":"If we were just simply going to do num1 divided by num2,"},{"Start":"02:22.640 ","End":"02:25.040","Text":"we\u0027d get an error because these are strings,"},{"Start":"02:25.040 ","End":"02:26.780","Text":"and you can\u0027t divide strings."},{"Start":"02:26.780 ","End":"02:29.164","Text":"We\u0027re going to need to convert this to an integer,"},{"Start":"02:29.164 ","End":"02:34.010","Text":"and we know how to do that integer.parseint as we did before."},{"Start":"02:34.010 ","End":"02:36.530","Text":"If we just put 1 parameter,"},{"Start":"02:36.530 ","End":"02:39.305","Text":"it assumes that it\u0027s a decimal."},{"Start":"02:39.305 ","End":"02:42.620","Text":"We could put comma 10 if we wanted to match"},{"Start":"02:42.620 ","End":"02:46.335","Text":"the previous exercise that we did but it would have no effect."},{"Start":"02:46.335 ","End":"02:50.705","Text":"It assumes if you only put 1 parameter that it\u0027s going to be in decimal."},{"Start":"02:50.705 ","End":"02:55.610","Text":"That\u0027s the first part that will divide 1 integer by another,"},{"Start":"02:55.610 ","End":"02:58.505","Text":"interpreting num1 as an integer and num2 as an integer."},{"Start":"02:58.505 ","End":"03:01.445","Text":"However, we\u0027ve been told to return a string."},{"Start":"03:01.445 ","End":"03:04.445","Text":"This is going to return an integer."},{"Start":"03:04.445 ","End":"03:10.700","Text":"We need to convert the integer that\u0027s been calculated into a string."},{"Start":"03:10.700 ","End":"03:13.235","Text":"We don\u0027t want to convert it into a hex string,"},{"Start":"03:13.235 ","End":"03:14.525","Text":"just into a string."},{"Start":"03:14.525 ","End":"03:20.990","Text":"You\u0027d probably guess that integer.toString will do the job."},{"Start":"03:20.990 ","End":"03:22.730","Text":"Let\u0027s compile that,"},{"Start":"03:22.730 ","End":"03:25.825","Text":"and it seems to be happy with that, no errors."},{"Start":"03:25.825 ","End":"03:27.750","Text":"The second part,"},{"Start":"03:27.750 ","End":"03:32.540","Text":"Part B is to use exception handling to trap an arithmetic exception,"},{"Start":"03:32.540 ","End":"03:36.950","Text":"and we return the string divide by 0 not allowed in integer arithmetic."},{"Start":"03:36.950 ","End":"03:40.975","Text":"In Part 2, a number format exception returns a string."},{"Start":"03:40.975 ","End":"03:44.795","Text":"Inputs must be numbers in decimal only."},{"Start":"03:44.795 ","End":"03:48.094","Text":"We have to return something in every case."},{"Start":"03:48.094 ","End":"03:49.970","Text":"The first part,"},{"Start":"03:49.970 ","End":"03:51.770","Text":"which is the bit for the try block,"},{"Start":"03:51.770 ","End":"03:53.420","Text":"we\u0027re going to try and do this."},{"Start":"03:53.420 ","End":"03:55.385","Text":"If it\u0027s not able to do this,"},{"Start":"03:55.385 ","End":"03:57.290","Text":"it will generate an exception,"},{"Start":"03:57.290 ","End":"04:00.650","Text":"and there are many reasons why it could cause an exception 2 of them are in"},{"Start":"04:00.650 ","End":"04:04.495","Text":"an arithmetic exception and a number format exception."},{"Start":"04:04.495 ","End":"04:08.450","Text":"We\u0027ll catch the first one and the type is"},{"Start":"04:08.450 ","End":"04:14.555","Text":"an arithmetic exception and I\u0027ll just finish the blocks here before we put any code in."},{"Start":"04:14.555 ","End":"04:19.835","Text":"Catch this one and it\u0027s a number format exception."},{"Start":"04:19.835 ","End":"04:22.955","Text":"Now, we\u0027ve got our blocks sorted out."},{"Start":"04:22.955 ","End":"04:25.580","Text":"We just need to return the relevant values."},{"Start":"04:25.580 ","End":"04:29.210","Text":"If we have an arithmetic exception,"},{"Start":"04:29.210 ","End":"04:39.620","Text":"we say divide by 0 not allowed in integer arithmetic, just an end there."},{"Start":"04:39.620 ","End":"04:42.380","Text":"Then in this part here,"},{"Start":"04:42.380 ","End":"04:47.180","Text":"we want to say if there has been an exception and it\u0027s a number format exception,"},{"Start":"04:47.180 ","End":"04:52.910","Text":"inputs must be numbers and decimal only."},{"Start":"04:52.910 ","End":"04:55.375","Text":"That\u0027s routine done."},{"Start":"04:55.375 ","End":"05:01.250","Text":"If I were to put 2 strings into this function that look like integers,"},{"Start":"05:01.250 ","End":"05:03.079","Text":"it would be fine."},{"Start":"05:03.079 ","End":"05:06.740","Text":"But if I put in numbers that were in hexadecimal, say odd,"},{"Start":"05:06.740 ","End":"05:10.145","Text":"weren\u0027t even numbers, there\u0027s any random combination of symbols,"},{"Start":"05:10.145 ","End":"05:12.420","Text":"I would throw a number format exception."},{"Start":"05:12.420 ","End":"05:14.600","Text":"This method would throw a number format exception because it"},{"Start":"05:14.600 ","End":"05:17.300","Text":"can\u0027t parse it as an integer."},{"Start":"05:17.300 ","End":"05:20.150","Text":"Then this bit also would not be able to turn it"},{"Start":"05:20.150 ","End":"05:22.660","Text":"into a string because this would not return anything,"},{"Start":"05:22.660 ","End":"05:24.020","Text":"the program would have ended."},{"Start":"05:24.020 ","End":"05:28.460","Text":"We\u0027re going to write test function to"},{"Start":"05:28.460 ","End":"05:32.944","Text":"test this and that\u0027s in Part C. It\u0027s called test divide."},{"Start":"05:32.944 ","End":"05:35.390","Text":"It doesn\u0027t take any parameters in,"},{"Start":"05:35.390 ","End":"05:38.180","Text":"so we leave empty brackets and doesn\u0027t return anything,"},{"Start":"05:38.180 ","End":"05:39.980","Text":"so we put void beginning."},{"Start":"05:39.980 ","End":"05:45.120","Text":"What we\u0027re going to do is to use the Scanner class again."},{"Start":"05:45.120 ","End":"05:53.960","Text":"We say Scanner equals new Scanner and then we pass to the Scanner System.in,"},{"Start":"05:53.960 ","End":"05:55.505","Text":"we\u0027ve done this a few times now."},{"Start":"05:55.505 ","End":"05:58.010","Text":"Scanner, by the way,"},{"Start":"05:58.010 ","End":"06:00.565","Text":"needs to have a line at the top."},{"Start":"06:00.565 ","End":"06:05.990","Text":"Sorry, I\u0027m giving it an identifying what\u0027s the area there. Let\u0027s call it in."},{"Start":"06:05.990 ","End":"06:07.520","Text":"At the top of your file,"},{"Start":"06:07.520 ","End":"06:10.460","Text":"you should have a line that imports the library the scanner is in,"},{"Start":"06:10.460 ","End":"06:15.185","Text":"and I\u0027ve already got it; import java.util.scanner, that needs to be there."},{"Start":"06:15.185 ","End":"06:21.095","Text":"Now we have an object called in from which we can take keyboard inputs."},{"Start":"06:21.095 ","End":"06:27.360","Text":"I need to also create a couple of variables to store my input in."},{"Start":"06:27.360 ","End":"06:32.179","Text":"Let\u0027s call those unimaginatively num1 and num2."},{"Start":"06:32.179 ","End":"06:40.035","Text":"Let\u0027s get the 2 numbers now in from the keyboard and we can use the in objects."},{"Start":"06:40.035 ","End":"06:43.250","Text":"There is a method called next which gets"},{"Start":"06:43.250 ","End":"06:47.179","Text":"the next string from the keyboard and do the same for num2."},{"Start":"06:47.179 ","End":"06:49.639","Text":"Now we\u0027ve got 2 numbers from the keyboard."},{"Start":"06:49.639 ","End":"06:55.905","Text":"I want to do this repeatedly while some condition is true."},{"Start":"06:55.905 ","End":"07:00.860","Text":"What I want to do is I want to keep doing it while the input is not 2 minus 1."},{"Start":"07:00.860 ","End":"07:03.920","Text":"So what I\u0027m going to do is this is a string, remember,"},{"Start":"07:03.920 ","End":"07:12.395","Text":"so I can\u0027t compare num1 and say not equals to minus 1 because num1 is a string."},{"Start":"07:12.395 ","End":"07:14.345","Text":"I can\u0027t do this."},{"Start":"07:14.345 ","End":"07:18.380","Text":"I have to use the equals method that\u0027s built into"},{"Start":"07:18.380 ","End":"07:22.910","Text":"every string and put inside the round brackets,"},{"Start":"07:22.910 ","End":"07:25.280","Text":"and the thing that is supposed to be equal to."},{"Start":"07:25.280 ","End":"07:26.450","Text":"Now, in my case,"},{"Start":"07:26.450 ","End":"07:27.995","Text":"I want it to be not equal to."},{"Start":"07:27.995 ","End":"07:32.420","Text":"So I\u0027ve put the not symbol in front so that expression will evaluate to keep"},{"Start":"07:32.420 ","End":"07:38.455","Text":"going while we don\u0027t have number 1 containing minus 1."},{"Start":"07:38.455 ","End":"07:41.090","Text":"But of course we want to do it for both of them."},{"Start":"07:41.090 ","End":"07:48.675","Text":"So I need the logical AND operator and do the same thing with num2."},{"Start":"07:48.675 ","End":"07:51.720","Text":"There\u0027s my while loop sorted out."},{"Start":"07:51.720 ","End":"07:55.100","Text":"Now, I\u0027ve already got 2 numbers from the keyboard,"},{"Start":"07:55.100 ","End":"07:59.645","Text":"so all I\u0027ve got to do now is to divide them by calling the call divide,"},{"Start":"07:59.645 ","End":"08:01.175","Text":"divide numbers method,"},{"Start":"08:01.175 ","End":"08:06.020","Text":"and I\u0027m going to parse them these 2 variables in my function called num1 and num2."},{"Start":"08:06.020 ","End":"08:08.090","Text":"They can have the same name as the parameter,"},{"Start":"08:08.090 ","End":"08:13.550","Text":"so it won\u0027t make any difference because these variables are local to this function here."},{"Start":"08:13.550 ","End":"08:16.850","Text":"But I do need to return the string somewhere so I"},{"Start":"08:16.850 ","End":"08:20.120","Text":"could put that and define a string called message,"},{"Start":"08:20.120 ","End":"08:24.185","Text":"and it will store whatever comes back from divide numbers into message."},{"Start":"08:24.185 ","End":"08:26.735","Text":"But actually, I don\u0027t have to store it."},{"Start":"08:26.735 ","End":"08:29.585","Text":"I can use it as an input to another function,"},{"Start":"08:29.585 ","End":"08:36.560","Text":"and our friend system.output.printin is actually a built-in function in Java."},{"Start":"08:36.560 ","End":"08:40.130","Text":"Instead of assigning it into a variable,"},{"Start":"08:40.130 ","End":"08:42.680","Text":"I can just parse it on to"},{"Start":"08:42.680 ","End":"08:47.180","Text":"the print function and it will take whatever value comes out and print it."},{"Start":"08:47.180 ","End":"08:48.875","Text":"So whatever string comes back,"},{"Start":"08:48.875 ","End":"08:51.470","Text":"it\u0027s going to be printed to the screen."},{"Start":"08:51.470 ","End":"08:53.705","Text":"I don\u0027t have to bother with creating a variable."},{"Start":"08:53.705 ","End":"08:58.085","Text":"Now, if I don\u0027t get 2 more inputs from the keyboard,"},{"Start":"08:58.085 ","End":"08:59.915","Text":"this loop will never end."},{"Start":"08:59.915 ","End":"09:03.520","Text":"So I do need to do that again. There we go."},{"Start":"09:03.520 ","End":"09:08.250","Text":"So now I\u0027ve got the next 2 numbers and if they\u0027re minus 1 and minus 1,"},{"Start":"09:08.250 ","End":"09:10.925","Text":"the program should end."},{"Start":"09:10.925 ","End":"09:14.605","Text":"Let\u0027s see if that compiles and it does."},{"Start":"09:14.605 ","End":"09:19.000","Text":"Let\u0027s have it go at running test divide which might use"},{"Start":"09:19.000 ","End":"09:23.589","Text":"and therefore test our divide numbers function. There\u0027s the program."},{"Start":"09:23.589 ","End":"09:27.185","Text":"You\u0027ll see BlueJ has the input window opened up at the bottom."},{"Start":"09:27.185 ","End":"09:31.420","Text":"I can put both numbers on 1 line if I want to or I can press"},{"Start":"09:31.420 ","End":"09:35.580","Text":"12 for the first number and 3 for the next number,"},{"Start":"09:35.580 ","End":"09:38.560","Text":"and I do get the output for 12 divided by 3,"},{"Start":"09:38.560 ","End":"09:40.000","Text":"of course being 4."},{"Start":"09:40.000 ","End":"09:45.545","Text":"Now let\u0027s try it with 3.14 and 10."},{"Start":"09:45.545 ","End":"09:51.050","Text":"Now that has failed because 3.14 is not an integer."},{"Start":"09:51.050 ","End":"09:53.930","Text":"So parse intuitive failed and it would\u0027ve caused"},{"Start":"09:53.930 ","End":"09:57.980","Text":"the runtime exception and therefore the program would\u0027ve ended."},{"Start":"09:57.980 ","End":"10:02.899","Text":"But we had a catch block which caught that runtime error and outputs this message."},{"Start":"10:02.899 ","End":"10:07.790","Text":"Input must be numbers in decimal only and it\u0027s actually not outputting the message in"},{"Start":"10:07.790 ","End":"10:13.340","Text":"the subroutine that\u0027s being returned by divide numbers,"},{"Start":"10:13.340 ","End":"10:18.500","Text":"and the little loop in test divide is printing out that message that came back."},{"Start":"10:18.500 ","End":"10:21.570","Text":"So it either prints out the number that comes back,"},{"Start":"10:21.570 ","End":"10:22.910","Text":"so it printed 4 here,"},{"Start":"10:22.910 ","End":"10:24.680","Text":"or it prints an error message."},{"Start":"10:24.680 ","End":"10:26.570","Text":"I\u0027ll do another legitimate number,"},{"Start":"10:26.570 ","End":"10:28.630","Text":"16 and 4,"},{"Start":"10:28.630 ","End":"10:31.760","Text":"and you\u0027ll see how it gets 4 back because that\u0027s legitimate."},{"Start":"10:31.760 ","End":"10:38.225","Text":"But again, if I put something that has a fractional part in it and divide it by anything,"},{"Start":"10:38.225 ","End":"10:39.800","Text":"it\u0027ll give me an error because it only"},{"Start":"10:39.800 ","End":"10:42.695","Text":"wants decimal numbers and that\u0027s what I was asked to do."},{"Start":"10:42.695 ","End":"10:45.180","Text":"That\u0027s the D1 and D2."},{"Start":"10:45.180 ","End":"10:46.860","Text":"Let\u0027s try D3 now."},{"Start":"10:46.860 ","End":"10:53.960","Text":"We put in 10 as the first number and 0 as the second number and gives us as we expect,"},{"Start":"10:53.960 ","End":"10:58.505","Text":"divide by 0 not allowed in integer arithmetic error."},{"Start":"10:58.505 ","End":"11:02.240","Text":"That\u0027s fantastic there. I should suppose test whether it ends properly,"},{"Start":"11:02.240 ","End":"11:04.250","Text":"so minus 1 and minus 1,"},{"Start":"11:04.250 ","End":"11:06.835","Text":"and it does end."},{"Start":"11:06.835 ","End":"11:09.155","Text":"That\u0027s fantastic."},{"Start":"11:09.155 ","End":"11:12.890","Text":"1 thing I didn\u0027t add was 1 test divide ends."},{"Start":"11:12.890 ","End":"11:15.740","Text":"I should output the message program ending and I would\u0027ve"},{"Start":"11:15.740 ","End":"11:18.950","Text":"seen program ending displayed there."},{"Start":"11:18.950 ","End":"11:20.420","Text":"So let me just add that."},{"Start":"11:20.420 ","End":"11:22.160","Text":"If I put in here,"},{"Start":"11:22.160 ","End":"11:26.585","Text":"you\u0027ll see that when the program ends legitimately,"},{"Start":"11:26.585 ","End":"11:30.440","Text":"you\u0027ll see the message program ending."},{"Start":"11:30.440 ","End":"11:36.980","Text":"So let me just prove that works by quickly creating an object and running it again."},{"Start":"11:36.980 ","End":"11:39.540","Text":"I\u0027ll do a legitimate 1, 12 [inaudible] 4."},{"Start":"11:39.540 ","End":"11:42.600","Text":"Let\u0027s do minus 1 and minus 1 to end."},{"Start":"11:42.600 ","End":"11:44.010","Text":"It does say program ending."},{"Start":"11:44.010 ","End":"11:46.865","Text":"You\u0027ll see the blue bars stopped here in the Workbench"},{"Start":"11:46.865 ","End":"11:51.455","Text":"and it\u0027s basically telling me that the program has stopped and is ended."},{"Start":"11:51.455 ","End":"11:53.180","Text":"So that\u0027s great."},{"Start":"11:53.180 ","End":"11:54.320","Text":"That does what I want it to do,"},{"Start":"11:54.320 ","End":"11:58.850","Text":"but it does say remove 1 of the catch blocks and run an appropriate test."},{"Start":"11:58.850 ","End":"12:02.690","Text":"Again, what difference does the exception block make to the flow of the program?"},{"Start":"12:02.690 ","End":"12:08.045","Text":"So let\u0027s take out this second exception up here and"},{"Start":"12:08.045 ","End":"12:13.610","Text":"just comment that out each of those lines, and then recompile."},{"Start":"12:13.610 ","End":"12:16.985","Text":"If I try and put in a number that\u0027s not a number now,"},{"Start":"12:16.985 ","End":"12:21.650","Text":"the number format exception is not caught and we\u0027ll see what happens."},{"Start":"12:21.650 ","End":"12:26.154","Text":"Let\u0027s try that again and just divide."},{"Start":"12:26.154 ","End":"12:29.925","Text":"So I do 12 and 3, that works."},{"Start":"12:29.925 ","End":"12:34.165","Text":"3.14 and 10,"},{"Start":"12:34.165 ","End":"12:38.210","Text":"and I this time get a error which stops the program."},{"Start":"12:38.210 ","End":"12:40.115","Text":"You see I\u0027ve come back into the editor,"},{"Start":"12:40.115 ","End":"12:42.110","Text":"and in this window here,"},{"Start":"12:42.110 ","End":"12:46.490","Text":"I\u0027ve got my error messages in this area of the console."},{"Start":"12:46.490 ","End":"12:49.460","Text":"The program has stopped as well,"},{"Start":"12:49.460 ","End":"12:50.885","Text":"but it never printed."},{"Start":"12:50.885 ","End":"12:57.575","Text":"The program ended because it never got to this last line here legitimately."},{"Start":"12:57.575 ","End":"13:02.420","Text":"It crashed and burned basically in this function and the whole program stopped."},{"Start":"13:02.420 ","End":"13:06.260","Text":"I think that shows you the difference that it makes to the control flow,"},{"Start":"13:06.260 ","End":"13:07.700","Text":"having a catch block."},{"Start":"13:07.700 ","End":"13:10.565","Text":"Catch blocks allow me to keep on running,"},{"Start":"13:10.565 ","End":"13:14.135","Text":"and because I took this one out when it hit this runtime error,"},{"Start":"13:14.135 ","End":"13:17.880","Text":"the program stopped and it broke me out of this loop"},{"Start":"13:17.880 ","End":"13:22.490","Text":"and I wasn\u0027t ever able to end it legitimately by putting minus 1 and minus 1,"},{"Start":"13:22.490 ","End":"13:26.165","Text":"and so we never saw the program ending message."},{"Start":"13:26.165 ","End":"13:27.875","Text":"That\u0027s it for this one."},{"Start":"13:27.875 ","End":"13:30.300","Text":"I\u0027ll see you for the next one."}],"ID":29290},{"Watched":false,"Name":"Exercise 4","Duration":"9m 29s","ChapterTopicVideoID":28089,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.730","Text":"Hello, welcome back. In this exercise,"},{"Start":"00:02.730 ","End":"00:03.900","Text":"we\u0027ve been asked to make use of"},{"Start":"00:03.900 ","End":"00:08.175","Text":"the lookupCalories method we created in an earlier exercise."},{"Start":"00:08.175 ","End":"00:11.445","Text":"In Part A, we create a new method called enterCalories,"},{"Start":"00:11.445 ","End":"00:12.968","Text":"which takes no arguments,"},{"Start":"00:12.968 ","End":"00:14.550","Text":"and returns no values."},{"Start":"00:14.550 ","End":"00:18.585","Text":"In Part B, we add the following line as the first line in the method,"},{"Start":"00:18.585 ","End":"00:23.740","Text":"and the line is Scanner in=new Scanner (System.in)."},{"Start":"00:23.750 ","End":"00:27.060","Text":"Then in Part C we create a try block."},{"Start":"00:27.060 ","End":"00:31.845","Text":"In D, we use a suitable Scanner method to get a single integer,"},{"Start":"00:31.845 ","End":"00:35.790","Text":"num, from the keyboard inside the try block."},{"Start":"00:35.790 ","End":"00:38.220","Text":"In Part E, we pass num to"},{"Start":"00:38.220 ","End":"00:42.985","Text":"the lookupCalories method and output num followed by the message,"},{"Start":"00:42.985 ","End":"00:46.080","Text":"\"kJ in kcal is\","},{"Start":"00:46.080 ","End":"00:49.895","Text":"followed by the value returned from lookupCalories."},{"Start":"00:49.895 ","End":"00:51.890","Text":"In Part F, after the try block,"},{"Start":"00:51.890 ","End":"00:55.115","Text":"we create a finally block which contains 2 lines."},{"Start":"00:55.115 ","End":"00:56.480","Text":"We output the message,"},{"Start":"00:56.480 ","End":"00:58.355","Text":"\"Closing scanner object\","},{"Start":"00:58.355 ","End":"01:01.315","Text":"and we then close the Scanner object,"},{"Start":"01:01.315 ","End":"01:03.975","Text":"in, using the close method."},{"Start":"01:03.975 ","End":"01:06.705","Text":"In Part G, after the finally block,"},{"Start":"01:06.705 ","End":"01:09.660","Text":"we output the message, \"Ending program\"."},{"Start":"01:09.660 ","End":"01:14.205","Text":"Then we test in Part H with values of: 5 which returns 1.194458,"},{"Start":"01:14.205 ","End":"01:18.323","Text":"8 which returns 0,"},{"Start":"01:18.323 ","End":"01:19.770","Text":"and displays a message,"},{"Start":"01:19.770 ","End":"01:22.845","Text":"e which fails to return a value."},{"Start":"01:22.845 ","End":"01:24.720","Text":"We\u0027re asked in Part 4,"},{"Start":"01:24.720 ","End":"01:28.095","Text":"why Test 2 emits \"Ending program\","},{"Start":"01:28.095 ","End":"01:30.855","Text":"but Test 3 doesn\u0027t."},{"Start":"01:30.855 ","End":"01:38.700","Text":"This method has to make use of the lookupCalories method we created earlier on."},{"Start":"01:38.700 ","End":"01:42.265","Text":"This one is called enterCalories."},{"Start":"01:42.265 ","End":"01:44.720","Text":"It takes no arguments,"},{"Start":"01:44.720 ","End":"01:46.400","Text":"and returns no values."},{"Start":"01:46.400 ","End":"01:49.745","Text":"It\u0027s basically going to allow us to enter something on the keyboard,"},{"Start":"01:49.745 ","End":"01:54.475","Text":"and then send that value to the lookupCalories subroutine,"},{"Start":"01:54.475 ","End":"01:56.720","Text":"function, method, whatever you want to call it."},{"Start":"01:56.720 ","End":"01:58.910","Text":"First things first,"},{"Start":"01:58.910 ","End":"02:03.950","Text":"we have to create a Scanner object."},{"Start":"02:03.950 ","End":"02:06.580","Text":"We\u0027re going to call it in."},{"Start":"02:06.580 ","End":"02:17.110","Text":"As usual, we create it by passing System.in to Scanner."},{"Start":"02:17.720 ","End":"02:20.310","Text":"That\u0027s Part B then."},{"Start":"02:20.310 ","End":"02:26.765","Text":"Now we\u0027ve been asked to create a try block, and within it,"},{"Start":"02:26.765 ","End":"02:31.880","Text":"to use a suitable Scanner method to get a single integer,"},{"Start":"02:31.880 ","End":"02:35.015","Text":"num, from the keyboard inside the try block."},{"Start":"02:35.015 ","End":"02:38.000","Text":"Let\u0027s create an integer called num."},{"Start":"02:38.000 ","End":"02:41.090","Text":"From the in object,"},{"Start":"02:41.090 ","End":"02:45.290","Text":"we can get the next integer using nextInt."},{"Start":"02:45.290 ","End":"02:49.260","Text":"That will store it into the variable int."},{"Start":"02:49.730 ","End":"02:56.130","Text":"Now we can pass num to the lookupCalories method."},{"Start":"02:56.130 ","End":"03:01.920","Text":"So lookupCalories, num,"},{"Start":"03:01.920 ","End":"03:04.065","Text":"will return us a value."},{"Start":"03:04.065 ","End":"03:07.930","Text":"The num is the thing that we just typed in the keyboard."},{"Start":"03:08.000 ","End":"03:10.160","Text":"Once we\u0027ve passed it,"},{"Start":"03:10.160 ","End":"03:13.605","Text":"we output num followed by the message,"},{"Start":"03:13.605 ","End":"03:16.050","Text":"\"kJ in kcal is\","},{"Start":"03:16.050 ","End":"03:20.360","Text":"and then the value that we get back from lookupCalories, which is here."},{"Start":"03:20.360 ","End":"03:26.190","Text":"Let\u0027s put System out println, in."},{"Start":"03:26.870 ","End":"03:32.445","Text":"The first part of the message we\u0027re going to put num out."},{"Start":"03:32.445 ","End":"03:41.730","Text":"Then we\u0027re going to put \"kJ in kcal is\","},{"Start":"03:41.730 ","End":"03:49.770","Text":"and then after that we put the thing that we get back from the lookupCalories function."},{"Start":"03:50.210 ","End":"03:56.190","Text":"That will do something and it\u0027s in a try block."},{"Start":"03:56.740 ","End":"04:01.070","Text":"This will, if it has an exception handling code in,"},{"Start":"04:01.070 ","End":"04:02.870","Text":"which it does up here, lookupCalories."},{"Start":"04:02.870 ","End":"04:04.795","Text":"If lookupCalories fails,"},{"Start":"04:04.795 ","End":"04:07.695","Text":"the exception will be dealt in here,"},{"Start":"04:07.695 ","End":"04:09.470","Text":"but without jumping ahead too much,"},{"Start":"04:09.470 ","End":"04:10.970","Text":"what if it doesn\u0027t?"},{"Start":"04:10.970 ","End":"04:14.315","Text":"What if another exception happens?"},{"Start":"04:14.315 ","End":"04:15.830","Text":"Where will that get caught?"},{"Start":"04:15.830 ","End":"04:17.120","Text":"I need a catch block,"},{"Start":"04:17.120 ","End":"04:21.725","Text":"but I\u0027ve actually been asked to create a finally block,"},{"Start":"04:21.725 ","End":"04:23.330","Text":"not a catch block."},{"Start":"04:23.330 ","End":"04:25.895","Text":"I mention a catch,"},{"Start":"04:25.895 ","End":"04:34.455","Text":"so finally is something that happens in a method even if an exception has been triggered."},{"Start":"04:34.455 ","End":"04:35.960","Text":"Whether tries happened,"},{"Start":"04:35.960 ","End":"04:37.635","Text":"or catches happened,"},{"Start":"04:37.635 ","End":"04:42.130","Text":"it is going to go into finally, no matter what."},{"Start":"04:42.130 ","End":"04:45.820","Text":"All we\u0027ve been asked to do in finally is to output"},{"Start":"04:45.820 ","End":"04:51.195","Text":"a message saying, \"Closing scanner object\"."},{"Start":"04:51.195 ","End":"04:56.725","Text":"Then we\u0027re actually going to use the close method, in."},{"Start":"04:56.725 ","End":"05:00.925","Text":"It\u0027s polite to close resources that we\u0027re not using."},{"Start":"05:00.925 ","End":"05:04.240","Text":"They do get eventually closed anyway by the system using"},{"Start":"05:04.240 ","End":"05:07.725","Text":"garbage cleanup code that\u0027s built into Java,"},{"Start":"05:07.725 ","End":"05:10.220","Text":"but it\u0027s actually good practice to do this to free up"},{"Start":"05:10.220 ","End":"05:12.965","Text":"the memory for the Virtual Machine when you\u0027re done with it."},{"Start":"05:12.965 ","End":"05:16.175","Text":"That\u0027s what we\u0027ve been asked to do."},{"Start":"05:16.175 ","End":"05:19.415","Text":"Then at the end of the finally block,"},{"Start":"05:19.415 ","End":"05:24.265","Text":"System.out.println, \"Ending program\"."},{"Start":"05:24.265 ","End":"05:30.015","Text":"That\u0027s going to happen right at the end of this method, enterCalories."},{"Start":"05:30.015 ","End":"05:32.093","Text":"We\u0027re going to test this now;"},{"Start":"05:32.093 ","End":"05:33.990","Text":"seems we have an error."},{"Start":"05:33.990 ","End":"05:36.975","Text":"Just missing a closing bracket there,"},{"Start":"05:36.975 ","End":"05:41.240","Text":"but notice there\u0027s no error around the try-and-catch."},{"Start":"05:41.240 ","End":"05:42.470","Text":"There isn\u0027t a catch,"},{"Start":"05:42.470 ","End":"05:44.315","Text":"but it\u0027s fine with it."},{"Start":"05:44.315 ","End":"05:46.415","Text":"If we got rid of the finally,"},{"Start":"05:46.415 ","End":"05:48.770","Text":"let\u0027s comment that out and see what happens."},{"Start":"05:48.770 ","End":"05:50.720","Text":"Would we get an error? It\u0027s saying,"},{"Start":"05:50.720 ","End":"05:54.780","Text":"try without catch, finally or resource declarations."},{"Start":"05:54.780 ","End":"05:59.690","Text":"It\u0027s happy to let me not have a catch because there is a finally,"},{"Start":"05:59.690 ","End":"06:01.700","Text":"but the operation is not exactly the same,"},{"Start":"06:01.700 ","End":"06:02.995","Text":"which we\u0027ll see in a moment."},{"Start":"06:02.995 ","End":"06:04.535","Text":"We\u0027ve got that now,"},{"Start":"06:04.535 ","End":"06:08.705","Text":"let\u0027s try the values that we need to test with."},{"Start":"06:08.705 ","End":"06:11.225","Text":"In Part H,"},{"Start":"06:11.225 ","End":"06:14.585","Text":"the method is called enterCalories."},{"Start":"06:14.585 ","End":"06:19.980","Text":"The value we want to enter is 5,"},{"Start":"06:19.980 ","End":"06:21.420","Text":"and it gives me the message,"},{"Start":"06:21.420 ","End":"06:25.035","Text":"\"5 kJ in kcal is 1.19\"."},{"Start":"06:25.035 ","End":"06:27.080","Text":"That\u0027s what we expected."},{"Start":"06:27.080 ","End":"06:28.460","Text":"Let\u0027s run it again."},{"Start":"06:28.460 ","End":"06:30.020","Text":"This is not in a loop,"},{"Start":"06:30.020 ","End":"06:34.295","Text":"so I have to answer it after I input the data each time."},{"Start":"06:34.295 ","End":"06:36.410","Text":"The next one is 8."},{"Start":"06:36.410 ","End":"06:40.273","Text":"Now we know 8 is outside the range,"},{"Start":"06:40.273 ","End":"06:42.400","Text":"so it should be 0-7."},{"Start":"06:42.400 ","End":"06:44.450","Text":"It\u0027s returning me an error message,"},{"Start":"06:44.450 ","End":"06:47.270","Text":"but it\u0027s still showing me this message,"},{"Start":"06:47.270 ","End":"06:49.550","Text":"\"Closing scanner object\","},{"Start":"06:49.550 ","End":"06:52.009","Text":"and it\u0027s still ending the program gracefully."},{"Start":"06:52.009 ","End":"06:54.860","Text":"That seems to work quite nicely."},{"Start":"06:54.860 ","End":"06:58.580","Text":"Let\u0027s try the final one."},{"Start":"06:58.580 ","End":"07:02.195","Text":"Let\u0027s enter something that is not a number."},{"Start":"07:02.195 ","End":"07:05.510","Text":"Now it\u0027s actually ended the program,"},{"Start":"07:05.510 ","End":"07:07.280","Text":"and it\u0027s on this line that it\u0027s ended,"},{"Start":"07:07.280 ","End":"07:09.365","Text":"but what we\u0027ll see is,"},{"Start":"07:09.365 ","End":"07:12.980","Text":"it still ran the closing scanner object code."},{"Start":"07:12.980 ","End":"07:14.795","Text":"It ran the finally part,"},{"Start":"07:14.795 ","End":"07:16.850","Text":"as we expect it to."},{"Start":"07:16.850 ","End":"07:19.640","Text":"It did trigger an exception,"},{"Start":"07:19.640 ","End":"07:20.870","Text":"and the exceptions appeared here."},{"Start":"07:20.870 ","End":"07:22.370","Text":"It was never caught by anything,"},{"Start":"07:22.370 ","End":"07:24.020","Text":"which is why we see it down here,"},{"Start":"07:24.020 ","End":"07:26.935","Text":"but it also ended the program."},{"Start":"07:26.935 ","End":"07:30.165","Text":"That answers our question in Part 2;"},{"Start":"07:30.165 ","End":"07:32.940","Text":"why did Test 2 emit \"Ending program\","},{"Start":"07:32.940 ","End":"07:34.200","Text":"but Test 3 didn\u0027t?"},{"Start":"07:34.200 ","End":"07:37.155","Text":"This test didn\u0027t emit \"Ending program\"."},{"Start":"07:37.155 ","End":"07:41.660","Text":"The reason for that is the exception was thrown here in this line."},{"Start":"07:41.660 ","End":"07:43.760","Text":"It would normally go to a catch block."},{"Start":"07:43.760 ","End":"07:45.560","Text":"Because there isn\u0027t a catch block,"},{"Start":"07:45.560 ","End":"07:47.300","Text":"it goes to the finally,"},{"Start":"07:47.300 ","End":"07:51.860","Text":"and therefore we\u0027ll never get to this part of the code here."},{"Start":"07:51.860 ","End":"07:55.565","Text":"If try had happened and it was successful,"},{"Start":"07:55.565 ","End":"07:58.580","Text":"it would skip to the end of finally and go to here."},{"Start":"07:58.580 ","End":"08:00.955","Text":"If another exception was caught,"},{"Start":"08:00.955 ","End":"08:03.620","Text":"it would do the finally and then go to here."},{"Start":"08:03.620 ","End":"08:05.915","Text":"That\u0027s what happened when we put in 8."},{"Start":"08:05.915 ","End":"08:08.675","Text":"It caught the exception inside lookupCalories,"},{"Start":"08:08.675 ","End":"08:10.865","Text":"and then threw it out to this last line."},{"Start":"08:10.865 ","End":"08:13.235","Text":"We also saw \"Ending program\"."},{"Start":"08:13.235 ","End":"08:16.100","Text":"The only time we don\u0027t see \"Ending program\","},{"Start":"08:16.100 ","End":"08:20.815","Text":"is if there\u0027s a catch that should\u0027ve been caught here and hasn\u0027t been."},{"Start":"08:20.815 ","End":"08:22.760","Text":"It will throw it out."},{"Start":"08:22.760 ","End":"08:23.960","Text":"It will do the finally,"},{"Start":"08:23.960 ","End":"08:25.160","Text":"but it will just end after that."},{"Start":"08:25.160 ","End":"08:26.990","Text":"It won\u0027t do this last line."},{"Start":"08:26.990 ","End":"08:32.355","Text":"There\u0027s a slight subtlety there about the flow of a try-and-catch block"},{"Start":"08:32.355 ","End":"08:37.750","Text":"as opposed to a try-and-finally block that\u0027s been teased out by this particular example."},{"Start":"08:37.750 ","End":"08:41.960","Text":"The exception handled inside lookupCalories, no problem."},{"Start":"08:41.960 ","End":"08:44.270","Text":"It caught it and then it did the finally,"},{"Start":"08:44.270 ","End":"08:45.605","Text":"and then it ended here."},{"Start":"08:45.605 ","End":"08:48.140","Text":"If it executed normally without any problems,"},{"Start":"08:48.140 ","End":"08:52.265","Text":"it would also execute and do finally, and finish here."},{"Start":"08:52.265 ","End":"08:58.175","Text":"The only case where it won\u0027t get to this final line here is if we\u0027ve got a try,"},{"Start":"08:58.175 ","End":"09:01.510","Text":"but no catch for a particular error that can happen,"},{"Start":"09:01.510 ","End":"09:06.135","Text":"and the one that can happen here is if we put in something that\u0027s not a number."},{"Start":"09:06.135 ","End":"09:07.425","Text":"We put in E,"},{"Start":"09:07.425 ","End":"09:10.670","Text":"it\u0027s not caught that exception because we haven\u0027t told it to."},{"Start":"09:10.670 ","End":"09:14.120","Text":"This other one will be picked up here if it\u0027s an invalid value,"},{"Start":"09:14.120 ","End":"09:16.670","Text":"if it\u0027s out of bounds, basically."},{"Start":"09:16.670 ","End":"09:20.780","Text":"A finally happens, but it doesn\u0027t execute this code, it ends."},{"Start":"09:20.780 ","End":"09:23.420","Text":"Normally, it would have just ended here on this line,"},{"Start":"09:23.420 ","End":"09:26.330","Text":"but in this case it did the finally first before it ended."},{"Start":"09:26.330 ","End":"09:30.080","Text":"That\u0027s it for this exercise."}],"ID":29291},{"Watched":false,"Name":"Exercise 5","Duration":"10m 53s","ChapterTopicVideoID":28090,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.080","Text":"Hello again. In this exercise,"},{"Start":"00:02.080 ","End":"00:04.705","Text":"we\u0027re asked to use the keyword throw in Java,"},{"Start":"00:04.705 ","End":"00:08.620","Text":"which we can use to throw an exception under particular conditions."},{"Start":"00:08.620 ","End":"00:11.785","Text":"In Part a, we\u0027re asked to create a method called checkString,"},{"Start":"00:11.785 ","End":"00:16.540","Text":"which returns a Boolean and accepts a single string argument."},{"Start":"00:16.540 ","End":"00:18.460","Text":"In Part b1, inside the method,"},{"Start":"00:18.460 ","End":"00:23.305","Text":"we\u0027re told to use a selection statements to check the argument passed in."},{"Start":"00:23.305 ","End":"00:25.330","Text":"If it\u0027s the literal null,"},{"Start":"00:25.330 ","End":"00:29.065","Text":"we throw a null pointer exception with the message,"},{"Start":"00:29.065 ","End":"00:32.605","Text":"*Invalid input null* with asterisks on either side of it."},{"Start":"00:32.605 ","End":"00:34.750","Text":"If it\u0027s a string of length 0,"},{"Start":"00:34.750 ","End":"00:37.540","Text":"we throw an illegal argument exception with"},{"Start":"00:37.540 ","End":"00:42.510","Text":"the message *invalid input empty string* with asterisks on either side of it."},{"Start":"00:42.510 ","End":"00:43.910","Text":"In Part b3,"},{"Start":"00:43.910 ","End":"00:48.020","Text":"outside both selection statements at the end of the method will return"},{"Start":"00:48.020 ","End":"00:52.820","Text":"the value true if the length of the string is between 8-16 characters long,"},{"Start":"00:52.820 ","End":"00:55.190","Text":"otherwise we return false."},{"Start":"00:55.190 ","End":"00:58.400","Text":"In part c, we\u0027re asked to create a second method called test"},{"Start":"00:58.400 ","End":"01:02.090","Text":"strings with no return value, or arguments."},{"Start":"01:02.090 ","End":"01:06.110","Text":"In part d, we\u0027re asked within the method to create a string array"},{"Start":"01:06.110 ","End":"01:10.385","Text":"called strings and to initialize it with the following values."},{"Start":"01:10.385 ","End":"01:15.370","Text":"Note 1 of the values here is an empty string and 1 is null."},{"Start":"01:15.370 ","End":"01:19.820","Text":"In part e, we\u0027re asked to create a for-loop with an index variable called"},{"Start":"01:19.820 ","End":"01:24.680","Text":"index used to iterate through each element of the strings array."},{"Start":"01:24.680 ","End":"01:28.685","Text":"Within the loop, we create try and catch blocks in part 1."},{"Start":"01:28.685 ","End":"01:33.860","Text":"In part 2 we\u0027re told that in the try block we should call the check string method,"},{"Start":"01:33.860 ","End":"01:39.920","Text":"passing in an element from the strings array using the current value of index."},{"Start":"01:39.920 ","End":"01:43.715","Text":"In part 3, depending on the value returned by check string,"},{"Start":"01:43.715 ","End":"01:48.500","Text":"we display the element value followed by - okay,"},{"Start":"01:48.500 ","End":"01:50.000","Text":"when true is returned,"},{"Start":"01:50.000 ","End":"01:53.545","Text":"or - failed when false is returned."},{"Start":"01:53.545 ","End":"01:54.800","Text":"In the catch block,"},{"Start":"01:54.800 ","End":"01:57.170","Text":"we catch all exceptions and output"},{"Start":"01:57.170 ","End":"02:02.215","Text":"the exception details using getMessage to retrieve the detail."},{"Start":"02:02.215 ","End":"02:04.220","Text":"Outside the loop in part e,"},{"Start":"02:04.220 ","End":"02:08.765","Text":"we\u0027re told to output a message saying Processed space,"},{"Start":"02:08.765 ","End":"02:11.210","Text":"followed by the value of index,"},{"Start":"02:11.210 ","End":"02:14.105","Text":"followed by space of space,"},{"Start":"02:14.105 ","End":"02:16.745","Text":"followed by the size of the array."},{"Start":"02:16.745 ","End":"02:18.280","Text":"In part g,"},{"Start":"02:18.280 ","End":"02:21.785","Text":"we test the code produces 2 failed messages,"},{"Start":"02:21.785 ","End":"02:23.790","Text":"2 okay messages,"},{"Start":"02:23.790 ","End":"02:26.090","Text":"and 2 error messages."},{"Start":"02:26.090 ","End":"02:28.360","Text":"Then we\u0027re asked in part h,"},{"Start":"02:28.360 ","End":"02:33.220","Text":"what advantage throw has given us in this program."},{"Start":"02:33.220 ","End":"02:39.515","Text":"Let\u0027s create the first part which is a method called checkString,"},{"Start":"02:39.515 ","End":"02:45.575","Text":"which takes a string as an argument which are called the parameter n,"},{"Start":"02:45.575 ","End":"02:47.720","Text":"and it returns a Boolean."},{"Start":"02:47.720 ","End":"02:50.180","Text":"Now, inside the method,"},{"Start":"02:50.180 ","End":"02:54.710","Text":"we\u0027re going to use selection statements to check that these arguments are valid."},{"Start":"02:54.710 ","End":"02:58.745","Text":"The in is a valid value that we can deal with."},{"Start":"02:58.745 ","End":"03:03.065","Text":"The first 1 we\u0027re going to check is to see whether n is a null."},{"Start":"03:03.065 ","End":"03:05.810","Text":"In is equal to null,"},{"Start":"03:05.810 ","End":"03:10.430","Text":"then we\u0027re going to throw an exception using throw new,"},{"Start":"03:10.430 ","End":"03:13.525","Text":"which is null pointer exception,"},{"Start":"03:13.525 ","End":"03:16.450","Text":"then we\u0027ll say, why?"},{"Start":"03:16.450 ","End":"03:19.880","Text":"Because it\u0027s an invalid input of null."},{"Start":"03:19.880 ","End":"03:22.205","Text":"But, the stars there are just for emphasis,"},{"Start":"03:22.205 ","End":"03:24.485","Text":"make it stand out when we print it."},{"Start":"03:24.485 ","End":"03:30.530","Text":"Then also if in length is equal to 0,"},{"Start":"03:30.530 ","End":"03:33.950","Text":"it\u0027s an empty string and that\u0027s also invalid input."},{"Start":"03:33.950 ","End":"03:41.310","Text":"But, this time we\u0027ll throw an exception of illegal argument exception."},{"Start":"03:41.310 ","End":"03:42.960","Text":"Could have done it for both,"},{"Start":"03:42.960 ","End":"03:48.675","Text":"but we\u0027ll just try with 2 different ones to see the impact."},{"Start":"03:48.675 ","End":"03:53.480","Text":"This one\u0027s invalid input of empty string."},{"Start":"03:53.480 ","End":"03:56.239","Text":"If neither of them throw an exception,"},{"Start":"03:56.239 ","End":"03:58.460","Text":"then we are okay to return a value."},{"Start":"03:58.460 ","End":"04:02.505","Text":"The value we\u0027re going to return is true or false."},{"Start":"04:02.505 ","End":"04:08.955","Text":"It\u0027s true if the length is at least 8,"},{"Start":"04:08.955 ","End":"04:10.560","Text":"so greater than 7,"},{"Start":"04:10.560 ","End":"04:14.775","Text":"and if the length is not longer than 16,"},{"Start":"04:14.775 ","End":"04:17.025","Text":"so less than 17."},{"Start":"04:17.025 ","End":"04:21.020","Text":"That will return true if both of these things are true,"},{"Start":"04:21.020 ","End":"04:22.670","Text":"otherwise it will return false,"},{"Start":"04:22.670 ","End":"04:24.545","Text":"which is exactly what we want."},{"Start":"04:24.545 ","End":"04:27.845","Text":"Let\u0027s just compile that and see if it\u0027s okay, which it is."},{"Start":"04:27.845 ","End":"04:30.380","Text":"Now we can do part c,"},{"Start":"04:30.380 ","End":"04:32.660","Text":"which is to create a medical testStrings,"},{"Start":"04:32.660 ","End":"04:37.340","Text":"doesn\u0027t return anything and doesn\u0027t take any parameters."},{"Start":"04:37.340 ","End":"04:41.540","Text":"We need to create a string array called strings."},{"Start":"04:41.540 ","End":"04:47.930","Text":"Let me just copy and paste the values that were in the exercise."},{"Start":"04:47.930 ","End":"04:52.850","Text":"Now what we need to do is in part a,"},{"Start":"04:52.850 ","End":"04:54.440","Text":"we\u0027re asked to create a for-loop,"},{"Start":"04:54.440 ","End":"04:58.400","Text":"and we use a variable called index,"},{"Start":"04:58.400 ","End":"05:01.835","Text":"which we use to iterate through the strings array."},{"Start":"05:01.835 ","End":"05:05.315","Text":"While we\u0027re not at the end of that strings array,"},{"Start":"05:05.315 ","End":"05:08.540","Text":"or we don\u0027t need brackets there because it\u0027s an array not a string."},{"Start":"05:08.540 ","End":"05:11.570","Text":"Length is a property of an array."},{"Start":"05:11.570 ","End":"05:12.665","Text":"This is an array."},{"Start":"05:12.665 ","End":"05:15.980","Text":"An individual string has a length"},{"Start":"05:15.980 ","End":"05:19.520","Text":"which we return with a method called length with brackets."},{"Start":"05:19.520 ","End":"05:22.010","Text":"But this is not a method."},{"Start":"05:22.010 ","End":"05:25.565","Text":"This is the whole array\u0027s length and it\u0027s a property of the array."},{"Start":"05:25.565 ","End":"05:29.565","Text":"Hence we don\u0027t need the brackets there."},{"Start":"05:29.565 ","End":"05:35.485","Text":"Index needs to be incremented and as a loop."},{"Start":"05:35.485 ","End":"05:38.860","Text":"In Part e1,"},{"Start":"05:38.860 ","End":"05:42.740","Text":"we\u0027re asked to create try and catch blocks."},{"Start":"05:42.740 ","End":"05:44.900","Text":"Let\u0027s get those in."},{"Start":"05:44.900 ","End":"05:46.865","Text":"Those are try and catch blocks."},{"Start":"05:46.865 ","End":"05:50.180","Text":"What we want to do in the try block is to"},{"Start":"05:50.180 ","End":"05:53.915","Text":"call the check string method that we just created,"},{"Start":"05:53.915 ","End":"05:55.540","Text":"and we\u0027re going to pass to it"},{"Start":"05:55.540 ","End":"06:00.425","Text":"the current string from the array that we\u0027re looking at using index."},{"Start":"06:00.425 ","End":"06:02.410","Text":"Arrays called strings,"},{"Start":"06:02.410 ","End":"06:05.564","Text":"element we\u0027re interested is pointed to by index,"},{"Start":"06:05.564 ","End":"06:10.535","Text":"so that will run the function on this and then that and so on."},{"Start":"06:10.535 ","End":"06:14.630","Text":"That\u0027s that part. Now, depending on the value returned,"},{"Start":"06:14.630 ","End":"06:18.365","Text":"we display this followed by,"},{"Start":"06:18.365 ","End":"06:20.255","Text":"okay if it\u0027s true,"},{"Start":"06:20.255 ","End":"06:23.720","Text":"or this followed by failed if it\u0027s false."},{"Start":"06:23.720 ","End":"06:25.955","Text":"We need an if statement here."},{"Start":"06:25.955 ","End":"06:29.555","Text":"If all of that returns true,"},{"Start":"06:29.555 ","End":"06:33.485","Text":"we output the value we\u0027re currently looking at,"},{"Start":"06:33.485 ","End":"06:36.305","Text":"followed by okay,"},{"Start":"06:36.305 ","End":"06:38.820","Text":"if it\u0027s true, otherwise,"},{"Start":"06:38.820 ","End":"06:41.730","Text":"we return output failed."},{"Start":"06:41.730 ","End":"06:44.805","Text":"Not okay, but failed in this case."},{"Start":"06:44.805 ","End":"06:48.150","Text":"That\u0027s what e3 has asked us to do."},{"Start":"06:48.150 ","End":"06:50.630","Text":"Then in e4, we\u0027ve been asked,"},{"Start":"06:50.630 ","End":"06:55.835","Text":"catch all exceptions and output the exception details using getMessage."},{"Start":"06:55.835 ","End":"07:00.440","Text":"All exceptions will be caught by a type called exception,"},{"Start":"07:00.440 ","End":"07:06.281","Text":"and if we output e.getMessage,"},{"Start":"07:06.281 ","End":"07:10.695","Text":"we\u0027ll see what cause the exception because there\u0027s 2 different causes,"},{"Start":"07:10.695 ","End":"07:12.940","Text":"this one and this one."},{"Start":"07:13.060 ","End":"07:17.615","Text":"Let\u0027s just check that compiles because some errors here,"},{"Start":"07:17.615 ","End":"07:20.435","Text":"let\u0027s just check on those one before going further."},{"Start":"07:20.435 ","End":"07:23.810","Text":"I didn\u0027t declare my index variable."},{"Start":"07:23.810 ","End":"07:26.540","Text":"Let\u0027s do that first."},{"Start":"07:26.540 ","End":"07:32.320","Text":"That\u0027s good now. I just need to do part e now."},{"Start":"07:32.320 ","End":"07:35.750","Text":"It says outside the loop to"},{"Start":"07:35.750 ","End":"07:41.940","Text":"output message saying processed to a newline character first,"},{"Start":"07:41.940 ","End":"07:43.185","Text":"print it on a new line,"},{"Start":"07:43.185 ","End":"07:47.280","Text":"followed by index of,"},{"Start":"07:47.280 ","End":"07:49.190","Text":"put spaces on either side."},{"Start":"07:49.190 ","End":"07:52.160","Text":"Then the length of the strings array."},{"Start":"07:52.160 ","End":"07:55.005","Text":"Again, I don\u0027t need brackets there."},{"Start":"07:55.005 ","End":"07:59.030","Text":"Let\u0027s see if that compiles and see if it does what we want."},{"Start":"07:59.030 ","End":"08:01.640","Text":"We\u0027re expecting to see 2 failed messages,"},{"Start":"08:01.640 ","End":"08:03.244","Text":"2 okay messages,"},{"Start":"08:03.244 ","End":"08:08.310","Text":"and 2 error messages with this set of data. Let\u0027s see what we get."},{"Start":"08:08.310 ","End":"08:12.960","Text":"We\u0027ve got a failed here because it\u0027s too long, 17."},{"Start":"08:12.960 ","End":"08:14.370","Text":"That is what we expect."},{"Start":"08:14.370 ","End":"08:16.605","Text":"Okay here because it\u0027s 8."},{"Start":"08:16.605 ","End":"08:19.365","Text":"Failed here because it\u0027s too short, 4."},{"Start":"08:19.365 ","End":"08:24.195","Text":"Then we get empty string, invalid input, null."},{"Start":"08:24.195 ","End":"08:28.850","Text":"Then this one\u0027s okay because it\u0027s exactly 16 and we\u0027ve processed 6 of 6."},{"Start":"08:28.850 ","End":"08:32.135","Text":"It does look like it\u0027s done what it\u0027s supposed to do."},{"Start":"08:32.135 ","End":"08:34.010","Text":"This one\u0027s too long,"},{"Start":"08:34.010 ","End":"08:36.500","Text":"this one\u0027s okay, this one is too short."},{"Start":"08:36.500 ","End":"08:39.305","Text":"This is an empty string which is invalid input."},{"Start":"08:39.305 ","End":"08:44.045","Text":"This is a null which is also invalid input and it through the relevant exceptions."},{"Start":"08:44.045 ","End":"08:45.910","Text":"This one\u0027s okay."},{"Start":"08:45.910 ","End":"08:50.930","Text":"It carried on running even though it was throwing exceptions in the check string."},{"Start":"08:50.930 ","End":"08:54.260","Text":"That\u0027s because of this little mechanism here,"},{"Start":"08:54.260 ","End":"08:57.740","Text":"is that if checkString threw an exception,"},{"Start":"08:57.740 ","End":"09:01.130","Text":"it would be caught here and the message would be printed,"},{"Start":"09:01.130 ","End":"09:04.415","Text":"the irrelevant message which came from here."},{"Start":"09:04.415 ","End":"09:06.560","Text":"This carried on,"},{"Start":"09:06.560 ","End":"09:07.640","Text":"so at the end of the loop,"},{"Start":"09:07.640 ","End":"09:11.090","Text":"we\u0027d gone through all 6 elements with index had got to 6."},{"Start":"09:11.090 ","End":"09:15.875","Text":"We saw that anyway because we were printing 6 different lines and everything."},{"Start":"09:15.875 ","End":"09:17.960","Text":"It behaved itself very well."},{"Start":"09:17.960 ","End":"09:20.540","Text":"What advantage, it asks us in h,"},{"Start":"09:20.540 ","End":"09:22.880","Text":"has throw given us in this program?"},{"Start":"09:22.880 ","End":"09:27.530","Text":"Well, it\u0027s able to report errors in the input."},{"Start":"09:27.530 ","End":"09:33.100","Text":"What\u0027s interesting here is we didn\u0027t have to return a value."},{"Start":"09:33.100 ","End":"09:34.505","Text":"Normally a function,"},{"Start":"09:34.505 ","End":"09:35.645","Text":"when we try and compile it,"},{"Start":"09:35.645 ","End":"09:40.710","Text":"it will complain if not every path will take us to a return."},{"Start":"09:40.710 ","End":"09:42.230","Text":"It has to return a value."},{"Start":"09:42.230 ","End":"09:45.725","Text":"The only exception if you pardon the pun, is,"},{"Start":"09:45.725 ","End":"09:48.605","Text":"when you throw an exception,"},{"Start":"09:48.605 ","End":"09:51.380","Text":"it doesn\u0027t need to return a value."},{"Start":"09:51.380 ","End":"09:56.140","Text":"It\u0027s perfectly acceptable for it to return no values."},{"Start":"09:56.140 ","End":"09:59.345","Text":"That\u0027s good. It\u0027s exactly what we want really,"},{"Start":"09:59.345 ","End":"10:01.310","Text":"is you want to carry on processing."},{"Start":"10:01.310 ","End":"10:02.810","Text":"But if we receive something,"},{"Start":"10:02.810 ","End":"10:07.535","Text":"but we don\u0027t receive some fake value like minus 1 or 0, or even now,"},{"Start":"10:07.535 ","End":"10:12.890","Text":"we actually would rather it broke out and came into this bit of code,"},{"Start":"10:12.890 ","End":"10:17.780","Text":"didn\u0027t bother trying to process anything and just carried on sweetly"},{"Start":"10:17.780 ","End":"10:20.390","Text":"processing the rest of the data as it is relevant"},{"Start":"10:20.390 ","End":"10:23.000","Text":"because we\u0027re just throwing away invalid values."},{"Start":"10:23.000 ","End":"10:24.530","Text":"We don\u0027t want it to crash the program."},{"Start":"10:24.530 ","End":"10:25.880","Text":"We just don\u0027t want to do anything with them."},{"Start":"10:25.880 ","End":"10:27.815","Text":"We want to ignore them essentially."},{"Start":"10:27.815 ","End":"10:29.000","Text":"But rather than ignore them,"},{"Start":"10:29.000 ","End":"10:32.195","Text":"we\u0027ve displayed a message here on the console,"},{"Start":"10:32.195 ","End":"10:34.640","Text":"but otherwise we\u0027ve carried on as normal."},{"Start":"10:34.640 ","End":"10:38.210","Text":"This is a way of us avoiding"},{"Start":"10:38.210 ","End":"10:44.075","Text":"compiler errors by having code that doesn\u0027t return values in some circumstances."},{"Start":"10:44.075 ","End":"10:50.165","Text":"It\u0027s certainly an advantage of using throw like this to change the flow of a program."},{"Start":"10:50.165 ","End":"10:51.900","Text":"Okay, that\u0027s it for this one,"},{"Start":"10:51.900 ","End":"10:54.280","Text":"see you soon for the next one."}],"ID":29292},{"Watched":false,"Name":"Exercise 6","Duration":"12m 21s","ChapterTopicVideoID":28091,"CourseChapterTopicPlaylistID":277598,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.220","Text":"Hello again. In this question,"},{"Start":"00:02.220 ","End":"00:07.350","Text":"we\u0027re told that compiled Java code runs on a virtual CPU called a Java Virtual Machine,"},{"Start":"00:07.350 ","End":"00:12.720","Text":"a JVM, and it allows the code to be run on different CPUs without recompiling."},{"Start":"00:12.720 ","End":"00:15.600","Text":"The JVM has a certain amount of memory available,"},{"Start":"00:15.600 ","End":"00:18.899","Text":"which it allocates as requested during runtime."},{"Start":"00:18.899 ","End":"00:22.770","Text":"We can use the BlueJ codepad in part a to execute"},{"Start":"00:22.770 ","End":"00:27.450","Text":"these lines which report various amounts of memory in the JVM."},{"Start":"00:27.450 ","End":"00:33.090","Text":"Part a, i, its Runtime.getRuntime.freeMemory, part ii,"},{"Start":"00:33.090 ","End":"00:35.655","Text":"it\u0027s the same but.totalMemory,"},{"Start":"00:35.655 ","End":"00:39.195","Text":"and part iii is the same again, but.maxMemory."},{"Start":"00:39.195 ","End":"00:42.165","Text":"In part b, we\u0027re then asked to create a method called"},{"Start":"00:42.165 ","End":"00:46.070","Text":"JVMinfo with no return value or parameters."},{"Start":"00:46.070 ","End":"00:49.265","Text":"The methods we saw in part a return a long,"},{"Start":"00:49.265 ","End":"00:53.450","Text":"each of them, reporting the relevant amount of memory in bytes."},{"Start":"00:53.450 ","End":"00:56.120","Text":"Inside the JVMinfo method,"},{"Start":"00:56.120 ","End":"00:58.220","Text":"we\u0027re going to use those methods to"},{"Start":"00:58.220 ","End":"01:02.510","Text":"output messages telling us how much free memory is available,"},{"Start":"01:02.510 ","End":"01:06.830","Text":"and how much total memory is currently being used by the JVM,"},{"Start":"01:06.830 ","End":"01:10.385","Text":"but both in megabytes, not bytes."},{"Start":"01:10.385 ","End":"01:16.370","Text":"In part d, we create another method called allocates with no parameters or return values."},{"Start":"01:16.370 ","End":"01:19.820","Text":"In d, i, we use a loop to repeatedly get an int"},{"Start":"01:19.820 ","End":"01:23.360","Text":"from the keyboard and store it into a variable num,"},{"Start":"01:23.360 ","End":"01:27.230","Text":"and which ends when a number below 1 is entered."},{"Start":"01:27.230 ","End":"01:32.015","Text":"Inside the loop, we call the JVMinfo method that we created earlier."},{"Start":"01:32.015 ","End":"01:33.290","Text":"In part d, iii,"},{"Start":"01:33.290 ","End":"01:39.305","Text":"we use try and catch blocks inside the loop to catch an out of memory error."},{"Start":"01:39.305 ","End":"01:40.520","Text":"In part 4,"},{"Start":"01:40.520 ","End":"01:41.945","Text":"inside the try block,"},{"Start":"01:41.945 ","End":"01:46.610","Text":"we attempt to create a new array of strings using the following line,"},{"Start":"01:46.610 ","End":"01:50.425","Text":"which is string and then a pair of square brackets,"},{"Start":"01:50.425 ","End":"01:53.745","Text":"arr equals new string,"},{"Start":"01:53.745 ","End":"01:55.214","Text":"open square bracket,"},{"Start":"01:55.214 ","End":"01:59.660","Text":"num times 1,024 times 1,024,"},{"Start":"01:59.660 ","End":"02:01.535","Text":"close square bracket semicolon."},{"Start":"02:01.535 ","End":"02:02.810","Text":"In the catch block,"},{"Start":"02:02.810 ","End":"02:06.920","Text":"we output to the System.err stream using"},{"Start":"02:06.920 ","End":"02:13.114","Text":"System.err.printLn to send a message saying free memory exceeded,"},{"Start":"02:13.114 ","End":"02:16.730","Text":"maximum available is colon and a space."},{"Start":"02:16.730 ","End":"02:20.540","Text":"Then we output the maximum amount of memory available to"},{"Start":"02:20.540 ","End":"02:25.520","Text":"the JVM using Runtime.getRuntime.maxMemory."},{"Start":"02:25.520 ","End":"02:28.730","Text":"In part e, we use another try block outside"},{"Start":"02:28.730 ","End":"02:32.420","Text":"the while loop that catches all other exceptions using"},{"Start":"02:32.420 ","End":"02:40.015","Text":"the exception type and outputs the message invalid input to System.err stream."},{"Start":"02:40.015 ","End":"02:44.040","Text":"In part f, we test allocate with the following inputs,"},{"Start":"02:44.040 ","End":"02:45.310","Text":"10, 0,"},{"Start":"02:45.310 ","End":"02:47.050","Text":"200, 500,"},{"Start":"02:47.050 ","End":"02:49.190","Text":"and e. In part g,"},{"Start":"02:49.190 ","End":"02:53.435","Text":"we are finally asked why the program continues for test 4,"},{"Start":"02:53.435 ","End":"02:56.735","Text":"but not for test 5."},{"Start":"02:56.735 ","End":"03:01.820","Text":"In part a, we\u0027ve been asked to use the code pad to try a few things out."},{"Start":"03:01.820 ","End":"03:04.940","Text":"The method we want to try out is free memory,"},{"Start":"03:04.940 ","End":"03:09.290","Text":"which belongs to the Runtime.getRuntime object."},{"Start":"03:09.290 ","End":"03:11.150","Text":"This has to do with the JVM."},{"Start":"03:11.150 ","End":"03:17.829","Text":"It returns how many bytes of free memory the JVM has available."},{"Start":"03:17.829 ","End":"03:19.785","Text":"If we use the other one,"},{"Start":"03:19.785 ","End":"03:22.060","Text":"which is called total memory,"},{"Start":"03:22.060 ","End":"03:27.835","Text":"this is how much is already being used plus what is still available,"},{"Start":"03:27.835 ","End":"03:29.470","Text":"so it\u0027s a larger number."},{"Start":"03:29.470 ","End":"03:32.440","Text":"There\u0027s some being used plus there\u0027s a little bit more available."},{"Start":"03:32.440 ","End":"03:36.820","Text":"Then the final figure is the maximum amount that is actually"},{"Start":"03:36.820 ","End":"03:41.560","Text":"available at any one time to this Java Virtual Machine running on this system,"},{"Start":"03:41.560 ","End":"03:44.065","Text":"which is a much bigger number."},{"Start":"03:44.065 ","End":"03:46.030","Text":"You\u0027ve seen how they work."},{"Start":"03:46.030 ","End":"03:48.490","Text":"They\u0027re obviously large unwieldy numbers."},{"Start":"03:48.490 ","End":"03:53.500","Text":"We\u0027d rather divide those down into smaller and measures rather than bytes."},{"Start":"03:53.500 ","End":"03:56.200","Text":"We deal with kilobytes or megabytes or even gigabytes."},{"Start":"03:56.200 ","End":"03:58.105","Text":"We\u0027ve been asked to do it in megabytes,"},{"Start":"03:58.105 ","End":"04:04.170","Text":"so we can now create a method which is called JVMinfo."},{"Start":"04:04.170 ","End":"04:11.090","Text":"It doesn\u0027t return any values and it doesn\u0027t have any parameters."},{"Start":"04:11.090 ","End":"04:15.995","Text":"All it needs to do is to print a message."},{"Start":"04:15.995 ","End":"04:20.365","Text":"First thing is going to say available memory."},{"Start":"04:20.365 ","End":"04:23.750","Text":"Then we\u0027re going to get the amount of free memory,"},{"Start":"04:23.750 ","End":"04:27.545","Text":"but we\u0027re going to divide it by 1,024 to get into kilobytes,"},{"Start":"04:27.545 ","End":"04:31.565","Text":"then 1,024 again, to get into megabytes."},{"Start":"04:31.565 ","End":"04:35.720","Text":"If we just take that entire statement and change the available"},{"Start":"04:35.720 ","End":"04:40.355","Text":"to total and then change the method that we\u0027re getting it from,"},{"Start":"04:40.355 ","End":"04:44.330","Text":"then that is done."},{"Start":"04:44.330 ","End":"04:47.480","Text":"Now we\u0027re ready to do the next part,"},{"Start":"04:47.480 ","End":"04:52.580","Text":"which is part d. We want to create another method,"},{"Start":"04:52.580 ","End":"04:55.055","Text":"but this time called allocate,"},{"Start":"04:55.055 ","End":"04:58.310","Text":"and again has no parameters."},{"Start":"04:58.310 ","End":"05:02.358","Text":"This is going to try and allocate a huge amount of memory,"},{"Start":"05:02.358 ","End":"05:04.520","Text":"and we\u0027re going to catch"},{"Start":"05:04.520 ","End":"05:09.110","Text":"any potential errors if it\u0027s not able to allocate the amount of memory."},{"Start":"05:09.110 ","End":"05:12.530","Text":"We\u0027ve been told to use a loop in d, i,"},{"Start":"05:12.530 ","End":"05:18.290","Text":"to get an int repeatedly from keyboard and store it into a variable num."},{"Start":"05:18.290 ","End":"05:20.480","Text":"We need an int variable called num."},{"Start":"05:20.480 ","End":"05:23.555","Text":"We will need a scanner, pull it in."},{"Start":"05:23.555 ","End":"05:28.265","Text":"It gets its input from system.in a stream."},{"Start":"05:28.265 ","End":"05:30.455","Text":"That should be that."},{"Start":"05:30.455 ","End":"05:33.020","Text":"Now we have to actually get the num."},{"Start":"05:33.020 ","End":"05:38.365","Text":"Num equals in.nextInt,"},{"Start":"05:38.365 ","End":"05:42.410","Text":"a next integer available at the keyboard that has been typed in."},{"Start":"05:42.410 ","End":"05:45.200","Text":"That\u0027s ready to go into a loop."},{"Start":"05:45.200 ","End":"05:51.514","Text":"In the loop, we want to keep going while num is greater than 0."},{"Start":"05:51.514 ","End":"05:53.750","Text":"While it is greater than 0,"},{"Start":"05:53.750 ","End":"05:56.120","Text":"we are going to call"},{"Start":"05:56.120 ","End":"06:02.405","Text":"the JVMinfo method to display the current amount of memory on the screen available."},{"Start":"06:02.405 ","End":"06:04.280","Text":"Then we\u0027ve been asked to use a try,"},{"Start":"06:04.280 ","End":"06:07.010","Text":"and catch block to catch an out of memory error."},{"Start":"06:07.010 ","End":"06:09.440","Text":"We\u0027re expecting an error,"},{"Start":"06:09.440 ","End":"06:11.810","Text":"so we\u0027re going to try and catch it."},{"Start":"06:11.810 ","End":"06:16.325","Text":"The type of error is out of memory."},{"Start":"06:16.325 ","End":"06:22.820","Text":"What goes in the try block is the bit that tries to allocate the memory."},{"Start":"06:22.820 ","End":"06:26.465","Text":"We\u0027ve been given the line that we need to use,"},{"Start":"06:26.465 ","End":"06:33.695","Text":"which is trying to create an array of strings called arr and it is"},{"Start":"06:33.695 ","End":"06:40.880","Text":"huge because we\u0027re multiplying the thing we typed in at the keyboard by 1,024,"},{"Start":"06:40.880 ","End":"06:44.240","Text":"and we\u0027re multiplying that by 1,024 again,"},{"Start":"06:44.240 ","End":"06:52.115","Text":"and a string itself needs about 16 bytes to store all of its data."},{"Start":"06:52.115 ","End":"06:55.140","Text":"Times 16 by num, by 1,024,"},{"Start":"06:55.140 ","End":"06:58.880","Text":"by 1,024 and that\u0027s how many bytes are being requested by this line."},{"Start":"06:58.880 ","End":"07:02.525","Text":"It\u0027ll work for small values of num smallish,"},{"Start":"07:02.525 ","End":"07:06.535","Text":"but it probably will fail as we increase that number."},{"Start":"07:06.535 ","End":"07:09.450","Text":"That\u0027s that part done."},{"Start":"07:09.450 ","End":"07:15.140","Text":"Then we need to in the catch block, report an error."},{"Start":"07:15.140 ","End":"07:19.594","Text":"The error we\u0027re going to report on the system error stream,"},{"Start":"07:19.594 ","End":"07:22.520","Text":"which is different area in the user interface"},{"Start":"07:22.520 ","End":"07:26.680","Text":"of BlueJ appears in red at the bottom of the console window."},{"Start":"07:26.680 ","End":"07:30.575","Text":"These are things we don\u0027t usually want our users to see."},{"Start":"07:30.575 ","End":"07:39.500","Text":"What we want to print his free memory exceeded maximum available is,"},{"Start":"07:39.500 ","End":"07:42.695","Text":"and then we report what\u0027s the maximum amount available."},{"Start":"07:42.695 ","End":"07:47.840","Text":"We know what that is. This method here that we had in our code pad,"},{"Start":"07:47.840 ","End":"07:51.305","Text":"not totalMemory, but maxMemory."},{"Start":"07:51.305 ","End":"07:56.210","Text":"That should report the maximum value."},{"Start":"07:56.210 ","End":"07:58.310","Text":"I could do that."},{"Start":"07:58.310 ","End":"08:03.740","Text":"Divided out into megabytes is what if I wanted to."},{"Start":"08:03.740 ","End":"08:06.275","Text":"That looks like it\u0027s good."},{"Start":"08:06.275 ","End":"08:09.890","Text":"That\u0027s it for the inner part."},{"Start":"08:09.890 ","End":"08:11.495","Text":"Now we\u0027ve been asked to use"},{"Start":"08:11.495 ","End":"08:15.650","Text":"another try block in part e outside the while loop that catches"},{"Start":"08:15.650 ","End":"08:17.630","Text":"all other exceptions using"},{"Start":"08:17.630 ","End":"08:22.840","Text":"the exception type and outputs the message invalid input to a system error."},{"Start":"08:22.840 ","End":"08:27.710","Text":"Really any other error that could occur is because we\u0027re trying to get a number in here,"},{"Start":"08:27.710 ","End":"08:30.350","Text":"and someone could type something in that\u0027s not a number."},{"Start":"08:30.350 ","End":"08:33.175","Text":"We do need to catch that."},{"Start":"08:33.175 ","End":"08:38.570","Text":"Let\u0027s do a try there and the whole thing here."},{"Start":"08:38.570 ","End":"08:41.900","Text":"What I also need to do actually is to indent all"},{"Start":"08:41.900 ","End":"08:46.140","Text":"of this and there\u0027s a shortcut to do that, which is f6."},{"Start":"08:47.720 ","End":"08:53.029","Text":"We\u0027re going to try that and if it works, great."},{"Start":"08:53.029 ","End":"08:55.850","Text":"If it doesn\u0027t, we need to catch it."},{"Start":"08:55.850 ","End":"09:00.695","Text":"We\u0027re going to catch generic exception."},{"Start":"09:00.695 ","End":"09:08.585","Text":"We\u0027re going to put a message out to system error again, saying invalid input."},{"Start":"09:08.585 ","End":"09:10.355","Text":"I think that\u0027s us done."},{"Start":"09:10.355 ","End":"09:12.800","Text":"We are ready to do part f now."},{"Start":"09:12.800 ","End":"09:14.745","Text":"There\u0027s no errors."},{"Start":"09:14.745 ","End":"09:19.345","Text":"Let\u0027s try and run the tests."},{"Start":"09:19.345 ","End":"09:23.745","Text":"We\u0027re running allocate. F, i,"},{"Start":"09:23.745 ","End":"09:28.850","Text":"we\u0027re asked to type 10 and that\u0027ll be a very obvious error."},{"Start":"09:28.850 ","End":"09:33.410","Text":"I\u0027m not coming out of my loop and classic thing to"},{"Start":"09:33.410 ","End":"09:38.640","Text":"happen is because I haven\u0027t read from the keyboard again."},{"Start":"09:38.640 ","End":"09:40.805","Text":"Rookie error there."},{"Start":"09:40.805 ","End":"09:42.545","Text":"Make sure you read."},{"Start":"09:42.545 ","End":"09:47.330","Text":"Make sure you can terminate your loop by putting in whenever you need to put in."},{"Start":"09:47.330 ","End":"09:49.040","Text":"Let\u0027s try that again."},{"Start":"09:49.040 ","End":"09:51.080","Text":"Clear the screen."},{"Start":"09:51.080 ","End":"09:54.530","Text":"It\u0027s telling us it\u0027s got a 118 megabytes available."},{"Start":"09:54.530 ","End":"09:57.110","Text":"The total memory is 126."},{"Start":"09:57.110 ","End":"10:05.760","Text":"That means there\u0027s some data already allocated and that\u0027s what\u0027s left available."},{"Start":"10:05.790 ","End":"10:09.245","Text":"That\u0027s fine, 10 worked okay, 0,"},{"Start":"10:09.245 ","End":"10:13.170","Text":"I would expect to come out of the program with, and it does."},{"Start":"10:13.170 ","End":"10:16.351","Text":"It doesn\u0027t print anything and I\u0027ve got no more inputs,"},{"Start":"10:16.351 ","End":"10:18.095","Text":"so that\u0027s worked as well."},{"Start":"10:18.095 ","End":"10:19.580","Text":"That\u0027s what I expected."},{"Start":"10:19.580 ","End":"10:21.140","Text":"It\u0027s a legitimate quit."},{"Start":"10:21.140 ","End":"10:24.395","Text":"200, I want to see what that 200 does."},{"Start":"10:24.395 ","End":"10:26.665","Text":"Seems to be okay, but it\u0027s less memory available."},{"Start":"10:26.665 ","End":"10:28.665","Text":"Let\u0027s try 500 now,"},{"Start":"10:28.665 ","End":"10:33.040","Text":"and 500 has triggered an exception,"},{"Start":"10:33.040 ","End":"10:36.490","Text":"but the program is still running, which is great."},{"Start":"10:36.490 ","End":"10:38.890","Text":"It tried to allocate the memory."},{"Start":"10:38.890 ","End":"10:43.900","Text":"The amount of memory I needed wasn\u0027t available at the time I tried to"},{"Start":"10:43.900 ","End":"10:49.690","Text":"allocate and so it\u0027s reported error in the error stream here,"},{"Start":"10:49.690 ","End":"10:51.400","Text":"but it\u0027s still allowing me to keep going."},{"Start":"10:51.400 ","End":"10:52.945","Text":"I\u0027m still in the while loop."},{"Start":"10:52.945 ","End":"10:55.990","Text":"Let\u0027s try that last value now of test f,"},{"Start":"10:55.990 ","End":"10:58.384","Text":"iv, which is e. Now,"},{"Start":"10:58.384 ","End":"11:00.440","Text":"that has given me invalid input,"},{"Start":"11:00.440 ","End":"11:05.775","Text":"but it\u0027s actually ended the program and it\u0027s not in the loop anymore."},{"Start":"11:05.775 ","End":"11:12.785","Text":"We\u0027re asked actually to explain why for test 4 here it\u0027s carried on."},{"Start":"11:12.785 ","End":"11:14.705","Text":"So it generated an error but carried on with,"},{"Start":"11:14.705 ","End":"11:16.655","Text":"but for this fifth one,"},{"Start":"11:16.655 ","End":"11:18.905","Text":"it didn\u0027t. It stopped."},{"Start":"11:18.905 ","End":"11:23.900","Text":"You can pretty easily see why we got nested try and catch here."},{"Start":"11:23.900 ","End":"11:29.645","Text":"But this first try is not going to be repeated because it\u0027s not inside the while loop."},{"Start":"11:29.645 ","End":"11:33.110","Text":"Whereas this try and catch is inside the while loop,"},{"Start":"11:33.110 ","End":"11:34.820","Text":"so it\u0027s repeatedly going to try."},{"Start":"11:34.820 ","End":"11:36.350","Text":"So even if it hits an error,"},{"Start":"11:36.350 ","End":"11:38.705","Text":"it will keep going because it\u0027s in the while loop."},{"Start":"11:38.705 ","End":"11:42.870","Text":"Whereas if I get an error outside,"},{"Start":"11:42.870 ","End":"11:45.700","Text":"it\u0027s going to catch it here."},{"Start":"11:45.700 ","End":"11:52.280","Text":"Showing some level of nesting there with the try and catch blocks."},{"Start":"11:52.280 ","End":"11:56.780","Text":"I hope that is straightforward now for you,"},{"Start":"11:56.780 ","End":"11:59.720","Text":"you can have nested try and catch."},{"Start":"11:59.720 ","End":"12:03.950","Text":"You can mix and match your types of exception as well,"},{"Start":"12:03.950 ","End":"12:07.910","Text":"decide whether you want it to continue or you want it to end."},{"Start":"12:07.910 ","End":"12:09.500","Text":"It\u0027s entirely up to you."},{"Start":"12:09.500 ","End":"12:13.505","Text":"Some quite serious areas like memory allocation errors."},{"Start":"12:13.505 ","End":"12:15.589","Text":"If it tries and fails,"},{"Start":"12:15.589 ","End":"12:18.920","Text":"you can try and exit gracefully rather than crash and burn."},{"Start":"12:18.920 ","End":"12:21.930","Text":"Thanks very much for watching. I\u0027ll see you soon."}],"ID":29293}],"Thumbnail":null,"ID":277598},{"Name":"File Input and Output","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"File Paths and Reading","Duration":"7m 56s","ChapterTopicVideoID":28277,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.525","Text":"Hello, and welcome to this video on file input and output."},{"Start":"00:03.525 ","End":"00:04.830","Text":"By the end of this section,"},{"Start":"00:04.830 ","End":"00:09.240","Text":"you\u0027ll be able to describe how files are used as a persistent store of data,"},{"Start":"00:09.240 ","End":"00:12.615","Text":"to explain the differences between the different modes of file access,"},{"Start":"00:12.615 ","End":"00:15.855","Text":"to distinguish between text and binary files,"},{"Start":"00:15.855 ","End":"00:19.035","Text":"and to apply techniques to read and write data."},{"Start":"00:19.035 ","End":"00:21.450","Text":"We\u0027ve reached a point in the course now where many of"},{"Start":"00:21.450 ","End":"00:24.000","Text":"the programming concepts you\u0027ve learned would enable you to write"},{"Start":"00:24.000 ","End":"00:26.910","Text":"some very small functional programs that"},{"Start":"00:26.910 ","End":"00:30.225","Text":"may even be useful in certain limited circumstances."},{"Start":"00:30.225 ","End":"00:33.320","Text":"However, a big issue would be that any data created or"},{"Start":"00:33.320 ","End":"00:38.015","Text":"transformed by our program will be lost when we exited the program."},{"Start":"00:38.015 ","End":"00:41.630","Text":"Remember that a program stores data in memory."},{"Start":"00:41.630 ","End":"00:47.035","Text":"This memory is allocated to our program when requested by the runtime environment."},{"Start":"00:47.035 ","End":"00:51.980","Text":"When the data is no longer needed by our program or when we choose to exit the program,"},{"Start":"00:51.980 ","End":"00:57.170","Text":"the memory is released back to the operating system for other programs to use."},{"Start":"00:57.170 ","End":"00:59.300","Text":"When we switch off the computer,"},{"Start":"00:59.300 ","End":"01:02.060","Text":"the memory contents are lost completely."},{"Start":"01:02.060 ","End":"01:06.335","Text":"Clearly, we need a way of making data to exist outside of the program."},{"Start":"01:06.335 ","End":"01:08.060","Text":"As a user of computers,"},{"Start":"01:08.060 ","End":"01:12.040","Text":"you\u0027ll be familiar with a solution to this problem, the file."},{"Start":"01:12.040 ","End":"01:15.005","Text":"A file is a persistent store of data"},{"Start":"01:15.005 ","End":"01:19.370","Text":"external to the program that created it or is making use of it."},{"Start":"01:19.370 ","End":"01:23.390","Text":"Files are generally created, stored, retrieved,"},{"Start":"01:23.390 ","End":"01:27.920","Text":"and updated by the operating system that your program is running on."},{"Start":"01:27.920 ","End":"01:32.680","Text":"In fact, it\u0027s done by a specific part of the operating system called the file system."},{"Start":"01:32.680 ","End":"01:35.165","Text":"Programs, including the ones you write,"},{"Start":"01:35.165 ","End":"01:39.080","Text":"are able to work with files via the operating system, for example,"},{"Start":"01:39.080 ","End":"01:43.220","Text":"to access data from an existing file or to create a new file,"},{"Start":"01:43.220 ","End":"01:45.515","Text":"or to delete or rename a file."},{"Start":"01:45.515 ","End":"01:49.525","Text":"File-related services are handled by the file system."},{"Start":"01:49.525 ","End":"01:52.395","Text":"To make files easy to locate and manage,"},{"Start":"01:52.395 ","End":"01:54.980","Text":"they\u0027re generally stored in directories or"},{"Start":"01:54.980 ","End":"01:58.355","Text":"folders as they\u0027re called in some operating systems."},{"Start":"01:58.355 ","End":"02:02.480","Text":"The word folder is easier to visualize than directory as we can"},{"Start":"02:02.480 ","End":"02:06.860","Text":"relate to the idea of a physical folder as a container for documents,"},{"Start":"02:06.860 ","End":"02:09.740","Text":"where each single document in the folder relates to"},{"Start":"02:09.740 ","End":"02:15.050","Text":"a single file in a virtual folder or directory on a computer."},{"Start":"02:15.050 ","End":"02:22.130","Text":"A folder can contain files or other folders called subfolders or subdirectories."},{"Start":"02:22.130 ","End":"02:26.345","Text":"Imagine a physical paper folder organizing student work."},{"Start":"02:26.345 ","End":"02:28.640","Text":"We could have an essays folder,"},{"Start":"02:28.640 ","End":"02:32.390","Text":"which contains a red folder with assignments for semester 1,"},{"Start":"02:32.390 ","End":"02:34.640","Text":"a blue folder for semester 2,"},{"Start":"02:34.640 ","End":"02:37.630","Text":"and an orange for semester 3."},{"Start":"02:37.630 ","End":"02:41.105","Text":"Providing the location of a file to a program"},{"Start":"02:41.105 ","End":"02:44.930","Text":"involves providing just enough detail to locate the file,"},{"Start":"02:44.930 ","End":"02:49.885","Text":"including on which drive it exists and in which folder or subfolder."},{"Start":"02:49.885 ","End":"02:53.295","Text":"A relative path means where the file is,"},{"Start":"02:53.295 ","End":"02:56.629","Text":"is relative to the program accessing the file"},{"Start":"02:56.629 ","End":"03:01.385","Text":"and it doesn\u0027t necessarily include every little bit of information."},{"Start":"03:01.385 ","End":"03:04.070","Text":"If it\u0027s in the same folder as the program,"},{"Start":"03:04.070 ","End":"03:08.045","Text":"then a relative path would simply be the name of the file."},{"Start":"03:08.045 ","End":"03:10.835","Text":"Here, we are running a program called MyApp,"},{"Start":"03:10.835 ","End":"03:15.775","Text":"which is accessing a file in the same folder called mydoc.txt."},{"Start":"03:15.775 ","End":"03:21.125","Text":"If our program MyApp was trying to access a file in a subfolder called semester 1,"},{"Start":"03:21.125 ","End":"03:27.710","Text":"then the relative path would be semester1/mydoc.txt."},{"Start":"03:27.710 ","End":"03:30.950","Text":"If our program was stored in a folder called code,"},{"Start":"03:30.950 ","End":"03:33.860","Text":"and at the same level in the folder hierarchy,"},{"Start":"03:33.860 ","End":"03:36.305","Text":"there was another folder called images."},{"Start":"03:36.305 ","End":"03:39.785","Text":"Then we could specify the relative path with 3 dots,"},{"Start":"03:39.785 ","End":"03:43.490","Text":"which basically means go up 1 level in the hierarchy,"},{"Start":"03:43.490 ","End":"03:46.310","Text":"in this case, to project then into"},{"Start":"03:46.310 ","End":"03:51.460","Text":"the images folder where there should be a file called mypic.jpg."},{"Start":"03:51.460 ","End":"03:56.480","Text":"An absolute path gives every single piece of information needed to locate a file."},{"Start":"03:56.480 ","End":"03:58.175","Text":"This would include the drive,"},{"Start":"03:58.175 ","End":"04:01.039","Text":"the subfolders, and the filename."},{"Start":"04:01.039 ","End":"04:02.930","Text":"As a system could be attached to"},{"Start":"04:02.930 ","End":"04:06.620","Text":"multiple storage devices which we call drives or volumes,"},{"Start":"04:06.620 ","End":"04:10.565","Text":"we need to identify the drive where files of interest are stored."},{"Start":"04:10.565 ","End":"04:12.755","Text":"Windows uses a drive letter."},{"Start":"04:12.755 ","End":"04:15.245","Text":"In this example of an absolute path,"},{"Start":"04:15.245 ","End":"04:17.555","Text":"we\u0027re specifying the C drive,"},{"Start":"04:17.555 ","End":"04:19.580","Text":"then it uses subfolder,"},{"Start":"04:19.580 ","End":"04:24.410","Text":"and so on until we get to the filename mydoc.txt."},{"Start":"04:24.410 ","End":"04:27.500","Text":"If our program use this form of absolute path,"},{"Start":"04:27.500 ","End":"04:30.260","Text":"it wouldn\u0027t matter where our program is relative to"},{"Start":"04:30.260 ","End":"04:32.975","Text":"the file it\u0027s trying to access because we have"},{"Start":"04:32.975 ","End":"04:35.690","Text":"all the information we need to locate the file no matter"},{"Start":"04:35.690 ","End":"04:38.905","Text":"which folder our program is stored in."},{"Start":"04:38.905 ","End":"04:41.790","Text":"Other OS is based on Unix,"},{"Start":"04:41.790 ","End":"04:44.785","Text":"username rather than a letter for the volume."},{"Start":"04:44.785 ","End":"04:48.335","Text":"Unix systems also use a forward slash,"},{"Start":"04:48.335 ","End":"04:54.520","Text":"not a backslash like Windows as a separator in the path between folders and files."},{"Start":"04:54.520 ","End":"05:00.200","Text":"Java has a whole class dedicated to dealing with file paths called File."},{"Start":"05:00.200 ","End":"05:06.805","Text":"A File object is created from the class by passing a string with intended path."},{"Start":"05:06.805 ","End":"05:12.145","Text":"In this case, we\u0027re creating an absolute path to a folder called documents."},{"Start":"05:12.145 ","End":"05:14.050","Text":"If this string was valid,"},{"Start":"05:14.050 ","End":"05:19.360","Text":"the resulting file object can then be used to see if a file exists at that path,"},{"Start":"05:19.360 ","End":"05:23.665","Text":"or see whether it\u0027s readable or whether it\u0027s writeable."},{"Start":"05:23.665 ","End":"05:28.265","Text":"It can create files or folders at that path."},{"Start":"05:28.265 ","End":"05:31.220","Text":"It can delete files or folders at that path,"},{"Start":"05:31.220 ","End":"05:32.990","Text":"and many other functions."},{"Start":"05:32.990 ","End":"05:36.380","Text":"Files can be split into 2 broad types;"},{"Start":"05:36.380 ","End":"05:39.290","Text":"text files, and binary files."},{"Start":"05:39.290 ","End":"05:46.145","Text":"A text file is a file containing human-readable content arranged as a sequence of lines."},{"Start":"05:46.145 ","End":"05:51.080","Text":"Text editors are programs that we can use to create text files."},{"Start":"05:51.080 ","End":"05:54.725","Text":"We simply type the text into the editor and save it."},{"Start":"05:54.725 ","End":"05:59.855","Text":"The editors we use to enter source code for programming languages are text editors."},{"Start":"05:59.855 ","End":"06:02.840","Text":"The definition here is valid for source code because"},{"Start":"06:02.840 ","End":"06:05.750","Text":"the programs you create in Java, Python,"},{"Start":"06:05.750 ","End":"06:09.770","Text":"or C are line-based and are human-readable,"},{"Start":"06:09.770 ","End":"06:13.805","Text":"or be unreadable by a peculiar type of human known as a programmer."},{"Start":"06:13.805 ","End":"06:19.765","Text":"They can also be read by computer programs such as compilers or interpreters."},{"Start":"06:19.765 ","End":"06:24.440","Text":"One important feature of text files is this idea of a line of text."},{"Start":"06:24.440 ","End":"06:29.885","Text":"A line will be zero or more characters followed by an end of line marker."},{"Start":"06:29.885 ","End":"06:32.300","Text":"This is the basic structuring principle of"},{"Start":"06:32.300 ","End":"06:34.940","Text":"the text file and it\u0027s common for programs that either"},{"Start":"06:34.940 ","End":"06:40.505","Text":"create text files or read from them to read an entire line at a time within the program."},{"Start":"06:40.505 ","End":"06:44.945","Text":"Python is particularly easy to use when dealing with text files."},{"Start":"06:44.945 ","End":"06:46.520","Text":"With just 3 lines of code,"},{"Start":"06:46.520 ","End":"06:49.970","Text":"you can open a file and display its contents on the console."},{"Start":"06:49.970 ","End":"06:53.210","Text":"But the general idea is applicable to other languages."},{"Start":"06:53.210 ","End":"06:59.270","Text":"First, you open a file at a given path and then hold a reference to it in some variable."},{"Start":"06:59.270 ","End":"07:02.720","Text":"In this case, we\u0027re holding the reference in file."},{"Start":"07:02.720 ","End":"07:05.225","Text":"Then you iterate through each line in the file,"},{"Start":"07:05.225 ","End":"07:08.605","Text":"doing something until you\u0027ve read every line in the file."},{"Start":"07:08.605 ","End":"07:13.190","Text":"A line itself is then read and can be processed inside the loop."},{"Start":"07:13.190 ","End":"07:16.040","Text":"All we\u0027re doing here is to print it out."},{"Start":"07:16.040 ","End":"07:21.515","Text":"The system keeps track of where it is in the file using something called a file pointer."},{"Start":"07:21.515 ","End":"07:23.570","Text":"Here\u0027s the equivalent in Java,"},{"Start":"07:23.570 ","End":"07:26.120","Text":"not much longer I\u0027m making use of a while loop"},{"Start":"07:26.120 ","End":"07:29.215","Text":"rather than enhanced for-loop as the Python example did."},{"Start":"07:29.215 ","End":"07:32.600","Text":"The method hasNextLine returns"},{"Start":"07:32.600 ","End":"07:36.125","Text":"true or false depending on whether there\u0027s another line to read or not."},{"Start":"07:36.125 ","End":"07:41.030","Text":"The method nextLine retrieves the next line from the file."},{"Start":"07:41.030 ","End":"07:43.760","Text":"Again, the system automatically keeps track of"},{"Start":"07:43.760 ","End":"07:46.910","Text":"a file pointer to know where we are in the file."},{"Start":"07:46.910 ","End":"07:48.545","Text":"We\u0027ll pause there for now."},{"Start":"07:48.545 ","End":"07:51.800","Text":"In the next video, we\u0027ll look at writing to files and"},{"Start":"07:51.800 ","End":"07:56.580","Text":"an alternative way to read from files. See you shortly."}],"ID":29802},{"Watched":false,"Name":"Binary Files and Random Access Edits","Duration":"10m 20s","ChapterTopicVideoID":28276,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.790","Text":"Hello, welcome back. In the previous video,"},{"Start":"00:02.790 ","End":"00:07.050","Text":"we looked at file parts and learned how to read whole lines of texts from a text file."},{"Start":"00:07.050 ","End":"00:09.885","Text":"Writing lines of text is just a straightforward,"},{"Start":"00:09.885 ","End":"00:12.420","Text":"but we do have an early decision to make."},{"Start":"00:12.420 ","End":"00:14.685","Text":"When a file is opened for writing,"},{"Start":"00:14.685 ","End":"00:19.410","Text":"it can be opened in 1 of 2 modes; overwrite or append."},{"Start":"00:19.410 ","End":"00:22.470","Text":"Overwrite will always start writing from the beginning of"},{"Start":"00:22.470 ","End":"00:26.534","Text":"the file and anything in the file already will be overwritten."},{"Start":"00:26.534 ","End":"00:28.230","Text":"Append, on the other hand,"},{"Start":"00:28.230 ","End":"00:31.160","Text":"will skip past any existing content to"},{"Start":"00:31.160 ","End":"00:34.250","Text":"the end of the file and carry on writing from there,"},{"Start":"00:34.250 ","End":"00:37.114","Text":"so no content will be overwritten."},{"Start":"00:37.114 ","End":"00:41.450","Text":"Which mode you open the file in is dependent on what you want to do."},{"Start":"00:41.450 ","End":"00:43.280","Text":"Log files, for example,"},{"Start":"00:43.280 ","End":"00:47.165","Text":"might start from a fresh every time a program\u0027s run,"},{"Start":"00:47.165 ","End":"00:50.540","Text":"or might continuously log forever by"},{"Start":"00:50.540 ","End":"00:54.875","Text":"appending any new information to the end of old information."},{"Start":"00:54.875 ","End":"01:01.415","Text":"Here\u0027s an example of a Python program opening a file for writing in append mode."},{"Start":"01:01.415 ","End":"01:07.790","Text":"It then retrieves the current system time and date and formats it as a string."},{"Start":"01:07.790 ","End":"01:13.165","Text":"This string is then written as a single line into the file."},{"Start":"01:13.165 ","End":"01:15.565","Text":"Each time this programs run,"},{"Start":"01:15.565 ","End":"01:18.385","Text":"a new time will be written to the end of the file."},{"Start":"01:18.385 ","End":"01:20.830","Text":"The number of lines in the file will be"},{"Start":"01:20.830 ","End":"01:23.590","Text":"equal to the number of times that the program was run,"},{"Start":"01:23.590 ","End":"01:26.410","Text":"but each line will contain a different time."},{"Start":"01:26.410 ","End":"01:29.320","Text":"One thing that differs between operating systems"},{"Start":"01:29.320 ","End":"01:32.725","Text":"is what constitutes a new line in a text file."},{"Start":"01:32.725 ","End":"01:35.230","Text":"When you type text into a document,"},{"Start":"01:35.230 ","End":"01:40.600","Text":"you press the \"Enter\" key when you want to force the subsequent text onto a new line."},{"Start":"01:40.600 ","End":"01:42.355","Text":"On a Windows machine,"},{"Start":"01:42.355 ","End":"01:44.590","Text":"every time you press the \"Enter\" key,"},{"Start":"01:44.590 ","End":"01:50.090","Text":"you do insert 2 characters into the file to signal the start of a new line;"},{"Start":"01:50.090 ","End":"01:53.580","Text":"the carriage return character ASCII 13,"},{"Start":"01:53.580 ","End":"01:57.055","Text":"and the line feed character ASCII 10."},{"Start":"01:57.055 ","End":"01:59.960","Text":"Although you don\u0027t see the characters themselves,"},{"Start":"01:59.960 ","End":"02:03.175","Text":"they are there inside the file when you save it."},{"Start":"02:03.175 ","End":"02:08.525","Text":"On Unix-based systems there\u0027d be only be 1 character at the end of each line,"},{"Start":"02:08.525 ","End":"02:12.290","Text":"the line feed character, ASCII 10."},{"Start":"02:12.290 ","End":"02:16.580","Text":"This difference is something you sometimes have to take account of when writing"},{"Start":"02:16.580 ","End":"02:21.275","Text":"a program because your program might be running on Windows or Unix,"},{"Start":"02:21.275 ","End":"02:23.150","Text":"and you might not know which."},{"Start":"02:23.150 ","End":"02:24.725","Text":"You\u0027re reliant, therefore,"},{"Start":"02:24.725 ","End":"02:27.890","Text":"on some of the runtime functions that are available that"},{"Start":"02:27.890 ","End":"02:32.435","Text":"report back what constitutes a new line in a plain text document."},{"Start":"02:32.435 ","End":"02:37.075","Text":"The other type of file that can be read or written is a binary file."},{"Start":"02:37.075 ","End":"02:40.340","Text":"A binary file is a non-character-based file,"},{"Start":"02:40.340 ","End":"02:43.775","Text":"which is intended to be read and written only by software,"},{"Start":"02:43.775 ","End":"02:48.485","Text":"and with a structure that\u0027s determined by the author of the file format."},{"Start":"02:48.485 ","End":"02:52.800","Text":"With a binary file, there\u0027s no concept of a line as we have with text files,"},{"Start":"02:52.800 ","End":"02:56.015","Text":"and the structure can be anything a programmer decides."},{"Start":"02:56.015 ","End":"02:58.895","Text":"Hence, binary files are not human-readable."},{"Start":"02:58.895 ","End":"03:02.450","Text":"They are intended to be read or written by software only."},{"Start":"03:02.450 ","End":"03:05.270","Text":"Because binary files are not character-based,"},{"Start":"03:05.270 ","End":"03:08.210","Text":"any byte value can be written to a binary file."},{"Start":"03:08.210 ","End":"03:13.550","Text":"In contrast, a text file can only contain characters from a particular character set."},{"Start":"03:13.550 ","End":"03:17.180","Text":"For example, 7-bit ASCII characters."},{"Start":"03:17.180 ","End":"03:19.605","Text":"Note the careful wording here."},{"Start":"03:19.605 ","End":"03:23.590","Text":"The file structure is determined by the author of the binary file."},{"Start":"03:23.590 ","End":"03:28.178","Text":"Though that could be some custom format designed by the programmer,"},{"Start":"03:28.178 ","End":"03:30.505","Text":"it could also be in a standard format."},{"Start":"03:30.505 ","End":"03:34.495","Text":"For example, MP3 files follow a standard,"},{"Start":"03:34.495 ","End":"03:39.475","Text":"and if you wanted to write a program which recorded sound into an MP3 file,"},{"Start":"03:39.475 ","End":"03:42.910","Text":"it will be writing the file as a binary file,"},{"Start":"03:42.910 ","End":"03:45.880","Text":"but the particular format is well-known and understood,"},{"Start":"03:45.880 ","End":"03:49.600","Text":"so other programs could work with that file too."},{"Start":"03:49.600 ","End":"03:54.880","Text":"Although a recording program may have created 1 particular MP3 file,"},{"Start":"03:54.880 ","End":"03:58.210","Text":"an editor program will be able to read and write"},{"Start":"03:58.210 ","End":"04:02.240","Text":"that file if it supports MP3 file formats."},{"Start":"04:02.240 ","End":"04:05.510","Text":"A media player could subsequently play the contents of"},{"Start":"04:05.510 ","End":"04:09.605","Text":"the file if it supports MP3 file formats as well."},{"Start":"04:09.605 ","End":"04:15.760","Text":"The same goes for other standard file types like JPEG for images or AVI for video."},{"Start":"04:15.760 ","End":"04:20.400","Text":"A binary file will simply be regarded as a sequence of bytes of a certain length,"},{"Start":"04:20.400 ","End":"04:23.000","Text":"and then it\u0027s up to your program to interpret"},{"Start":"04:23.000 ","End":"04:25.970","Text":"those bytes to determine what they represent;"},{"Start":"04:25.970 ","End":"04:29.815","Text":"be that sound or an image or whatever."},{"Start":"04:29.815 ","End":"04:35.180","Text":"Quite often, you\u0027ll hear the term stream when applied to data input and output."},{"Start":"04:35.180 ","End":"04:40.070","Text":"It can help to understand this concept if you visualize a physical stream made"},{"Start":"04:40.070 ","End":"04:45.275","Text":"up of water droplets passing through a particular point on its way somewhere."},{"Start":"04:45.275 ","End":"04:48.470","Text":"A data stream is data rather than"},{"Start":"04:48.470 ","End":"04:52.085","Text":"drops of water passing through a point on its way somewhere."},{"Start":"04:52.085 ","End":"04:54.460","Text":"The somewhere would be into some parts of"},{"Start":"04:54.460 ","End":"04:58.730","Text":"a computer system or out of some part of a computer system."},{"Start":"04:58.730 ","End":"05:02.510","Text":"The keyboard would be an input data stream,"},{"Start":"05:02.510 ","End":"05:05.420","Text":"keys typed on the keyboard are passed 1 at"},{"Start":"05:05.420 ","End":"05:08.285","Text":"a time to the input stream within the operating system."},{"Start":"05:08.285 ","End":"05:10.700","Text":"Characters being sent to the screen and a print"},{"Start":"05:10.700 ","End":"05:13.850","Text":"statement are making use of the output stream,"},{"Start":"05:13.850 ","End":"05:17.420","Text":"which ultimately ends up in your console display as"},{"Start":"05:17.420 ","End":"05:19.880","Text":"even an error stream which prints errors in"},{"Start":"05:19.880 ","End":"05:23.645","Text":"a particular place that\u0027s intended to be viewed by programmers."},{"Start":"05:23.645 ","End":"05:28.310","Text":"String-based input and output means that data passing through a particular point"},{"Start":"05:28.310 ","End":"05:33.370","Text":"can be read and output through various streams interchangeably."},{"Start":"05:33.370 ","End":"05:35.840","Text":"Rather than reading from a keyboard,"},{"Start":"05:35.840 ","End":"05:39.665","Text":"we might read from a file or a network connection,"},{"Start":"05:39.665 ","End":"05:44.135","Text":"or a hardware device streaming video or audio data into the system."},{"Start":"05:44.135 ","End":"05:46.730","Text":"Rather than output to the screen,"},{"Start":"05:46.730 ","End":"05:52.720","Text":"we could output to a file or to a network connection or another hardware device."},{"Start":"05:52.720 ","End":"05:56.135","Text":"When we\u0027re talking about a file stream and a program,"},{"Start":"05:56.135 ","End":"05:59.585","Text":"we usually mean bytes being written to an output stream."},{"Start":"05:59.585 ","End":"06:02.315","Text":"In other words, saving data to a binary file"},{"Start":"06:02.315 ","End":"06:05.740","Text":"or bytes being read from an input stream i.e.,"},{"Start":"06:05.740 ","End":"06:08.475","Text":"reading data from a binary file."},{"Start":"06:08.475 ","End":"06:13.115","Text":"Text files are generally accessed 1 line at a time from the beginning of the file."},{"Start":"06:13.115 ","End":"06:15.770","Text":"We call this serial access,"},{"Start":"06:15.770 ","End":"06:18.320","Text":"where one thing follows the next in series,"},{"Start":"06:18.320 ","End":"06:20.569","Text":"in this case, a series of lines."},{"Start":"06:20.569 ","End":"06:24.125","Text":"For binary files, it would be a series of bytes,"},{"Start":"06:24.125 ","End":"06:25.490","Text":"1 after the other."},{"Start":"06:25.490 ","End":"06:29.840","Text":"Serial access was the way that files tended to be accessed in the early days,"},{"Start":"06:29.840 ","End":"06:35.070","Text":"because data for computer systems was stored on tape with the data written"},{"Start":"06:35.070 ","End":"06:41.075","Text":"in a continuous sequence until it was all written out onto the tape."},{"Start":"06:41.075 ","End":"06:43.805","Text":"To search for data on a tape,"},{"Start":"06:43.805 ","End":"06:46.730","Text":"you have to read from the beginning of the tape until you"},{"Start":"06:46.730 ","End":"06:52.680","Text":"encounter whatever it is that you\u0027re looking for or reach the end of the tape."},{"Start":"06:52.810 ","End":"06:55.850","Text":"If you were writing data to a tape,"},{"Start":"06:55.850 ","End":"06:59.075","Text":"and wanted to append rather than overwrite,"},{"Start":"06:59.075 ","End":"07:02.560","Text":"you\u0027d have to read past all the existing data,"},{"Start":"07:02.560 ","End":"07:08.000","Text":"and then start writing when you reach the end of the previous data."},{"Start":"07:08.000 ","End":"07:13.445","Text":"This makes serial access slow for reading and slow for writing."},{"Start":"07:13.445 ","End":"07:18.710","Text":"Another way of accessing a file is so-called random access."},{"Start":"07:18.710 ","End":"07:22.355","Text":"Hard drives and optical drives use random access."},{"Start":"07:22.355 ","End":"07:25.340","Text":"Basically, you can access any specific part of the data"},{"Start":"07:25.340 ","End":"07:28.280","Text":"you\u0027re storing without reading all the other data first,"},{"Start":"07:28.280 ","End":"07:30.415","Text":"as you must do with serial media."},{"Start":"07:30.415 ","End":"07:32.720","Text":"Think about the Skip button on a CD,"},{"Start":"07:32.720 ","End":"07:34.415","Text":"DVD or Blu-ray player,"},{"Start":"07:34.415 ","End":"07:38.665","Text":"it skips straight ahead to the next track or chapter."},{"Start":"07:38.665 ","End":"07:41.845","Text":"If you\u0027re accessing a particular file on a hard disk,"},{"Start":"07:41.845 ","End":"07:44.320","Text":"you don\u0027t need to read all the files before it."},{"Start":"07:44.320 ","End":"07:47.695","Text":"You just jump straight to the file and access its data."},{"Start":"07:47.695 ","End":"07:50.800","Text":"This relies on opening the file and then being able to"},{"Start":"07:50.800 ","End":"07:54.160","Text":"seek to a specific position within it."},{"Start":"07:54.160 ","End":"07:57.010","Text":"Most high-level languages allow you to do this."},{"Start":"07:57.010 ","End":"08:00.745","Text":"You simply specify the position and bytes that you want to seek to,"},{"Start":"08:00.745 ","End":"08:03.220","Text":"and then you carry on reading from there."},{"Start":"08:03.220 ","End":"08:09.250","Text":"Here\u0027s an example of a Python program which opens a file for binary access."},{"Start":"08:09.250 ","End":"08:12.490","Text":"We seek 16 bytes into the file,"},{"Start":"08:12.490 ","End":"08:15.415","Text":"i.e., we skip over the first 16 bytes."},{"Start":"08:15.415 ","End":"08:18.069","Text":"Then we read from that position onwards."},{"Start":"08:18.069 ","End":"08:25.780","Text":"Here we\u0027re reading 1,024 bytes into something called myDat and then we print myDat out."},{"Start":"08:25.780 ","End":"08:29.974","Text":"An issue with seeking is knowing how the data is organized."},{"Start":"08:29.974 ","End":"08:33.815","Text":"A sequel often involved a calculation of some sort."},{"Start":"08:33.815 ","End":"08:35.450","Text":"For example,"},{"Start":"08:35.450 ","End":"08:41.285","Text":"audio and video binary files are organized in blocks of sound samples or pixels."},{"Start":"08:41.285 ","End":"08:46.000","Text":"If we know the size of the block and how many seconds of audio or video it relates to,"},{"Start":"08:46.000 ","End":"08:51.275","Text":"we can multiply the number of seconds by the size of each 1 second block."},{"Start":"08:51.275 ","End":"08:58.750","Text":"For example, if we wanted to play a video file 1 minute and 21 seconds into the video,"},{"Start":"08:58.750 ","End":"09:03.085","Text":"assuming each second of video is 100,000 bytes,"},{"Start":"09:03.085 ","End":"09:06.667","Text":"we\u0027d need to seek to byte position 81 times"},{"Start":"09:06.667 ","End":"09:12.995","Text":"100,000 in the file to start playing it from 1 minute and 21 seconds in."},{"Start":"09:12.995 ","End":"09:17.285","Text":"1 issue that can arise with binary files is that for simple calculations,"},{"Start":"09:17.285 ","End":"09:19.970","Text":"every block needs to be the same size."},{"Start":"09:19.970 ","End":"09:24.140","Text":"It might be that a block can\u0027t be completely filled with data."},{"Start":"09:24.140 ","End":"09:27.755","Text":"For example, we don\u0027t have a full second of audio or video."},{"Start":"09:27.755 ","End":"09:30.680","Text":"This is fixed by something called padding."},{"Start":"09:30.680 ","End":"09:32.780","Text":"We simply insert a load of"},{"Start":"09:32.780 ","End":"09:38.735","Text":"0s or some other harmless value on the end of our data to fill up the rest of the block."},{"Start":"09:38.735 ","End":"09:40.770","Text":"For strings, for example,"},{"Start":"09:40.770 ","End":"09:44.675","Text":"we can just pad them out with spaces so they\u0027re always the same length."},{"Start":"09:44.675 ","End":"09:49.205","Text":"In this example, we want every string to be cut 16 characters long,"},{"Start":"09:49.205 ","End":"09:51.965","Text":"but this one\u0027s only 11 characters long."},{"Start":"09:51.965 ","End":"09:58.235","Text":"We can add 5 spaces at the end to pad it out to 16 characters."},{"Start":"09:58.235 ","End":"10:01.925","Text":"That\u0027s it for this topic. Thanks for watching."},{"Start":"10:01.925 ","End":"10:04.460","Text":"In this section, we learned how to describe how"},{"Start":"10:04.460 ","End":"10:07.115","Text":"files are used as a persistent store of data,"},{"Start":"10:07.115 ","End":"10:11.105","Text":"to explain the differences between the different modes of file access,"},{"Start":"10:11.105 ","End":"10:14.135","Text":"to distinguish between text and binary files,"},{"Start":"10:14.135 ","End":"10:17.330","Text":"and to apply techniques to read and write data."},{"Start":"10:17.330 ","End":"10:20.670","Text":"Thanks, again, for watching and see you soon."}],"ID":29803},{"Watched":false,"Name":"Exercise 1","Duration":"9m 44s","ChapterTopicVideoID":28377,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.080 ","End":"00:03.660","Text":"Hello and welcome to this first exercise in which we\u0027ve been asked to"},{"Start":"00:03.660 ","End":"00:07.005","Text":"create a new project and within a class called filesApp."},{"Start":"00:07.005 ","End":"00:12.420","Text":"We add a line to import the java.io classes and then in Part A,"},{"Start":"00:12.420 ","End":"00:15.540","Text":"we create a method called right times table which accepts"},{"Start":"00:15.540 ","End":"00:19.410","Text":"a single int argument n and returns a Boolean."},{"Start":"00:19.410 ","End":"00:21.480","Text":"In Part B within a try block,"},{"Start":"00:21.480 ","End":"00:22.890","Text":"we add the following lines;"},{"Start":"00:22.890 ","End":"00:28.880","Text":"FileWriter outfile equals new FileWriter ( \"table.Txt\");"},{"Start":"00:28.880 ","End":"00:34.670","Text":"and then outfile.write and then in quotes,"},{"Start":"00:34.670 ","End":"00:41.870","Text":"(\"Here is times table:\"); In Part C,"},{"Start":"00:41.870 ","End":"00:46.685","Text":"we add a loop that iterates a variable multiplier from 1-12,"},{"Start":"00:46.685 ","End":"00:48.020","Text":"and in Part D,"},{"Start":"00:48.020 ","End":"00:52.430","Text":"we output to the file the contents of the multiplier variable,"},{"Start":"00:52.430 ","End":"00:54.680","Text":"followed by the string x,"},{"Start":"00:54.680 ","End":"00:57.695","Text":"followed by the contents of the variable n,"},{"Start":"00:57.695 ","End":"01:00.720","Text":"followed by the string equals."},{"Start":"01:00.720 ","End":"01:06.005","Text":"In Part E, we then output the product of multiplier and n,"},{"Start":"01:06.005 ","End":"01:08.195","Text":"followed by the literal ."},{"Start":"01:08.195 ","End":"01:10.360","Text":"In Part F, outside the loop,"},{"Start":"01:10.360 ","End":"01:13.420","Text":"we close the file using outfile.close."},{"Start":"01:13.420 ","End":"01:16.510","Text":"In Part G is the last line of the try-block,"},{"Start":"01:16.510 ","End":"01:19.645","Text":"where to return the Boolean value true."},{"Start":"01:19.645 ","End":"01:24.970","Text":"In Part H, we add a catch block which captures an IO exception using"},{"Start":"01:24.970 ","End":"01:28.070","Text":"Get-Message to output any error message detail and"},{"Start":"01:28.070 ","End":"01:31.524","Text":"then finally returning the Boolean value false."},{"Start":"01:31.524 ","End":"01:34.420","Text":"In Part I, we test the program by running it and"},{"Start":"01:34.420 ","End":"01:37.330","Text":"check the contents of the file table.text,"},{"Start":"01:37.330 ","End":"01:40.270","Text":"which would have been saved in the same folder as your project."},{"Start":"01:40.270 ","End":"01:41.710","Text":"Then finally in Part J,"},{"Start":"01:41.710 ","End":"01:47.200","Text":"we change the file permissions of the file table.text to read only and run"},{"Start":"01:47.200 ","End":"01:49.840","Text":"the program again and check that false is"},{"Start":"01:49.840 ","End":"01:54.500","Text":"returned and we check the terminal for the output message."},{"Start":"01:54.500 ","End":"01:57.770","Text":"Once you\u0027ve created your class,"},{"Start":"01:57.770 ","End":"02:02.120","Text":"go ahead and import the java.io classes."},{"Start":"02:02.120 ","End":"02:06.665","Text":"If you want all of them you put wildcard character and asterisks."},{"Start":"02:06.665 ","End":"02:08.870","Text":"If you know these specific classes,"},{"Start":"02:08.870 ","End":"02:11.345","Text":"you can list them 1 by 1 on multiple lines."},{"Start":"02:11.345 ","End":"02:14.200","Text":"I\u0027ll just do it for everything."},{"Start":"02:14.200 ","End":"02:18.260","Text":"So I\u0027ll put a long card character there and then we"},{"Start":"02:18.260 ","End":"02:22.819","Text":"need to start by adding our method called writeTimestable."},{"Start":"02:22.819 ","End":"02:29.750","Text":"It returns a Boolean and accepts a single integer and we\u0027re going to give"},{"Start":"02:29.750 ","End":"02:32.390","Text":"that parameter of the name n."},{"Start":"02:32.390 ","End":"02:37.415","Text":"Everything that deals with files is going to be a checked exception."},{"Start":"02:37.415 ","End":"02:42.000","Text":"So we have to put try and catch or it won\u0027t compile."},{"Start":"02:42.160 ","End":"02:48.140","Text":"Remember the syntax from previous exercises is a bit like"},{"Start":"02:48.140 ","End":"02:56.270","Text":"a selection block and then the catch will need to have something in the brackets here,"},{"Start":"02:56.270 ","End":"02:59.860","Text":"which we\u0027ll come to in a moment."},{"Start":"02:59.860 ","End":"03:03.335","Text":"First things first, what is it that we\u0027re trying to do?"},{"Start":"03:03.335 ","End":"03:07.940","Text":"What we\u0027ve been given some lines in Part B to get us started."},{"Start":"03:07.940 ","End":"03:14.460","Text":"We correct a Filewriter object and we\u0027re going to call it outfile"},{"Start":"03:14.460 ","End":"03:20.705","Text":"and as a argument to a FileWriter you give it a string,"},{"Start":"03:20.705 ","End":"03:25.550","Text":"which is going to be the name of the file that you\u0027re going to write this data to."},{"Start":"03:25.550 ","End":"03:28.325","Text":"We\u0027ve been asked to call it table.text,"},{"Start":"03:28.325 ","End":"03:32.240","Text":"and then we\u0027re going to actually write something to the file and so"},{"Start":"03:32.240 ","End":"03:36.410","Text":"FileWriter class has the method right,"},{"Start":"03:36.410 ","End":"03:39.620","Text":"which allows us to write just like a print line statement"},{"Start":"03:39.620 ","End":"03:44.585","Text":"does except it\u0027s individual characters and strings."},{"Start":"03:44.585 ","End":"03:46.130","Text":"If you want to have a new line,"},{"Start":"03:46.130 ","End":"03:50.780","Text":"you need to put a new line character on the end and we have in this example,"},{"Start":"03:50.780 ","End":"03:52.445","Text":"we\u0027ve been asked to do that."},{"Start":"03:52.445 ","End":"03:59.865","Text":"Here is the times table and force a new line character at the end there."},{"Start":"03:59.865 ","End":"04:02.690","Text":"There\u0027s Part A,"},{"Start":"04:02.690 ","End":"04:05.780","Text":"and B, and C,"},{"Start":"04:05.780 ","End":"04:10.970","Text":"we\u0027ve been asked to add a loop that iterates a variable multiplier from 1-12,"},{"Start":"04:10.970 ","End":"04:14.240","Text":"so it sounds like a for-loop would be the right thing to do there"},{"Start":"04:14.240 ","End":"04:18.875","Text":"and the variable is going to be called multiplier."},{"Start":"04:18.875 ","End":"04:26.690","Text":"We\u0027re going to start it at 1 and we\u0027re going to keep going until we hit 12, in fact,"},{"Start":"04:26.690 ","End":"04:29.120","Text":"less than or equal to 12,"},{"Start":"04:29.120 ","End":"04:36.155","Text":"would be what we need and then increment it by 1 each time around the loop."},{"Start":"04:36.155 ","End":"04:40.730","Text":"There\u0027s a for-loop and what we want to do is output to the file"},{"Start":"04:40.730 ","End":"04:46.325","Text":"the contents or the multiply variable followed by X with a space either side,"},{"Start":"04:46.325 ","End":"04:48.050","Text":"followed by the contents of variable n,"},{"Start":"04:48.050 ","End":"04:51.195","Text":"followed by the string equals with the space either side."},{"Start":"04:51.195 ","End":"04:54.625","Text":"We again use outfile.write."},{"Start":"04:54.625 ","End":"04:58.685","Text":"This time is going to write something different each time around the loop."},{"Start":"04:58.685 ","End":"05:02.810","Text":"The first thing it\u0027s going to output is"},{"Start":"05:02.810 ","End":"05:06.560","Text":"the multiplier variable and it will convert that to"},{"Start":"05:06.560 ","End":"05:11.344","Text":"a string for us and then we want to have an x."},{"Start":"05:11.344 ","End":"05:17.595","Text":"Then contents are variable n and then"},{"Start":"05:17.595 ","End":"05:25.490","Text":"equals so all of that will be written to a file and the next,"},{"Start":"05:25.490 ","End":"05:26.840","Text":"I could do it all on 1 line,"},{"Start":"05:26.840 ","End":"05:28.715","Text":"but I\u0027m going to do on a separate line."},{"Start":"05:28.715 ","End":"05:32.030","Text":"The next part in Part E is to output a product of"},{"Start":"05:32.030 ","End":"05:36.180","Text":"multiplier and n followed by a new line character."},{"Start":"05:36.190 ","End":"05:39.720","Text":"You need to put this in brackets actually,"},{"Start":"05:39.720 ","End":"05:45.740","Text":"so it does the calculation first and then converts it into a string."},{"Start":"05:45.740 ","End":"05:48.965","Text":"Otherwise, it might think you\u0027re trying to add 2 strings together."},{"Start":"05:48.965 ","End":"05:54.125","Text":"Multiplier times n and"},{"Start":"05:54.125 ","End":"06:00.020","Text":"we\u0027re going to finish off with a new line character."},{"Start":"06:00.020 ","End":"06:03.380","Text":"Outside of the loop,"},{"Start":"06:03.380 ","End":"06:10.070","Text":"we\u0027ve been asked to close a file in Part F using outfile.close."},{"Start":"06:10.070 ","End":"06:12.380","Text":"Now you may find it all works fine,"},{"Start":"06:12.380 ","End":"06:16.940","Text":"but it\u0027s a good practice to close files after you\u0027ve done what you need to do with them."},{"Start":"06:16.940 ","End":"06:20.015","Text":"It may be that it doesn\u0027t work at all and"},{"Start":"06:20.015 ","End":"06:23.360","Text":"the file is not correctly written unless you close the file,"},{"Start":"06:23.360 ","End":"06:24.695","Text":"so you should be doing it."},{"Start":"06:24.695 ","End":"06:27.890","Text":"This could be something done in finally close as well."},{"Start":"06:27.890 ","End":"06:32.030","Text":"If you\u0027re dealing with try and catch because you know that\u0027s always going to happen."},{"Start":"06:32.030 ","End":"06:38.030","Text":"But in this case we\u0027ve been asked to just do it in a try-block. That\u0027s it."},{"Start":"06:38.030 ","End":"06:41.000","Text":"Apart from the fact that we do need to return the value,"},{"Start":"06:41.000 ","End":"06:44.000","Text":"so we were going to return true in this case,"},{"Start":"06:44.000 ","End":"06:51.217","Text":"true meaning we successfully wrote the information to the file."},{"Start":"06:51.217 ","End":"06:57.980","Text":"We\u0027ve been told to use a catch clause to catch an IO exception."},{"Start":"06:57.980 ","End":"07:07.015","Text":"Call argument e and we\u0027ve been told to print out the error message details,"},{"Start":"07:07.015 ","End":"07:12.260","Text":"which we can get from e.getMessage."},{"Start":"07:13.670 ","End":"07:19.210","Text":"That\u0027s straightforward enough and then all we need to do now is return false."},{"Start":"07:19.210 ","End":"07:26.835","Text":"We\u0027re signaling back to the calling code that this operation was not successful."},{"Start":"07:26.835 ","End":"07:28.725","Text":"So let\u0027s see,"},{"Start":"07:28.725 ","End":"07:31.308","Text":"I think I\u0027m missing something here."},{"Start":"07:31.308 ","End":"07:36.360","Text":"I\u0027m missing the plus sign there and see if that compiles,"},{"Start":"07:36.360 ","End":"07:37.830","Text":"still not an issue."},{"Start":"07:37.830 ","End":"07:46.775","Text":"Now, so this is capitalization inherits FileWriter with a capital W,"},{"Start":"07:46.775 ","End":"07:49.690","Text":"and that seems to have done the trick."},{"Start":"07:49.690 ","End":"07:54.310","Text":"We now have to run it to see if that works."},{"Start":"07:54.310 ","End":"08:02.090","Text":"Create a new instance of this class and then choose right times table, pass it a number."},{"Start":"08:02.090 ","End":"08:04.180","Text":"We say we want to see the 7 times table,"},{"Start":"08:04.180 ","End":"08:06.085","Text":"pass it a 7."},{"Start":"08:06.085 ","End":"08:08.470","Text":"Click on \"Okay\" and I get true,"},{"Start":"08:08.470 ","End":"08:10.750","Text":"which is an indication that it\u0027s worked."},{"Start":"08:10.750 ","End":"08:12.310","Text":"If you look over into this window,"},{"Start":"08:12.310 ","End":"08:15.310","Text":"you\u0027ll see that it has actually created this file here"},{"Start":"08:15.310 ","End":"08:18.475","Text":"table and dot.text and if I double-click on it,"},{"Start":"08:18.475 ","End":"08:20.140","Text":"it will open up the file and there we go."},{"Start":"08:20.140 ","End":"08:23.845","Text":"It\u0027s created the output that we expect it to."},{"Start":"08:23.845 ","End":"08:26.890","Text":"The first line there was hard-coded and"},{"Start":"08:26.890 ","End":"08:30.960","Text":"then these are the lines that were generated inside the loop."},{"Start":"08:30.960 ","End":"08:37.190","Text":"I have been asked in Part J to change the permissions on this."},{"Start":"08:37.190 ","End":"08:39.154","Text":"If you\u0027ve got a properties in Windows,"},{"Start":"08:39.154 ","End":"08:43.580","Text":"you can right-click on the file and then click on"},{"Start":"08:43.580 ","End":"08:49.160","Text":"this button here under attribute similar system for other operating systems,"},{"Start":"08:49.160 ","End":"08:51.710","Text":"I\u0027ve marked it now as a read-only file."},{"Start":"08:51.710 ","End":"08:56.980","Text":"If I try and run this code again and I\u0027ll do a different times Table 3,"},{"Start":"08:56.980 ","End":"08:59.630","Text":"I expect to see an error because"},{"Start":"08:59.630 ","End":"09:03.305","Text":"the operating system shouldn\u0027t allow me to write to this file."},{"Start":"09:03.305 ","End":"09:05.180","Text":"I get a false back,"},{"Start":"09:05.180 ","End":"09:10.340","Text":"which is what I expect and I also see a message telling me what the error"},{"Start":"09:10.340 ","End":"09:12.650","Text":"was and it\u0027s passed back the details of table or"},{"Start":"09:12.650 ","End":"09:15.620","Text":"text and what it couldn\u0027t do in this case,"},{"Start":"09:15.620 ","End":"09:16.760","Text":"it couldn\u0027t access the file,"},{"Start":"09:16.760 ","End":"09:18.860","Text":"is denied by the operating system."},{"Start":"09:18.860 ","End":"09:22.730","Text":"Therefore, we get an exception and"},{"Start":"09:22.730 ","End":"09:27.400","Text":"I\u0027m able to see the details of the exception using Get-Message."},{"Start":"09:27.400 ","End":"09:29.320","Text":"Nice gentle start,"},{"Start":"09:29.320 ","End":"09:33.260","Text":"and we are showing that you can create a file and write contents to"},{"Start":"09:33.260 ","End":"09:37.520","Text":"it and catch any issues that occur."},{"Start":"09:37.520 ","End":"09:40.370","Text":"But that works as we expect it to,"},{"Start":"09:40.370 ","End":"09:45.510","Text":"so I\u0027ll see you again shortly for the next exercise. Thanks for watching."}],"ID":29804},{"Watched":false,"Name":"Exercise 2","Duration":"13m 42s","ChapterTopicVideoID":28372,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.030","Text":"Hello everyone. In this exercise,"},{"Start":"00:03.030 ","End":"00:08.115","Text":"we\u0027ve been told that files can be appended to using a FileWriter arguments set to true,"},{"Start":"00:08.115 ","End":"00:12.060","Text":"and then in part a, we use the code path to run some code"},{"Start":"00:12.060 ","End":"00:16.575","Text":"and at relevant points check the folder you saved your project in to see the impact."},{"Start":"00:16.575 ","End":"00:20.265","Text":"In part a, 1, we import java.io.star."},{"Start":"00:20.265 ","End":"00:26.910","Text":"In part 2, we create a string called nl and that\u0027s equal to System.getProperty brackets,"},{"Start":"00:26.910 ","End":"00:29.415","Text":"line separator in quotes."},{"Start":"00:29.415 ","End":"00:33.410","Text":"In part 3, my file equals new file,"},{"Start":"00:33.410 ","End":"00:36.005","Text":"and then in quotes, testfile.txt."},{"Start":"00:36.005 ","End":"00:41.270","Text":"Part 4 FileWriter fw equals new FileWriter,"},{"Start":"00:41.270 ","End":"00:43.355","Text":"my file comma true."},{"Start":"00:43.355 ","End":"00:47.982","Text":"In part 5, fw.write some stuff in quotes,"},{"Start":"00:47.982 ","End":"00:52.708","Text":"part 6, fw.write some more stuff in quotes,"},{"Start":"00:52.708 ","End":"00:55.385","Text":"part 7, fw.close,"},{"Start":"00:55.385 ","End":"00:57.425","Text":"part 8, FileWriter,"},{"Start":"00:57.425 ","End":"01:00.515","Text":"fw2 equals new FileWriter,"},{"Start":"01:00.515 ","End":"01:02.855","Text":"myfile comma true in brackets,"},{"Start":"01:02.855 ","End":"01:08.220","Text":"and part 9, fw2.right, nl plus,"},{"Start":"01:08.220 ","End":"01:10.545","Text":"and then a little string asterisk,"},{"Start":"01:10.545 ","End":"01:13.005","Text":"END in capital letters asterisk,"},{"Start":"01:13.005 ","End":"01:17.655","Text":"and then finally part 10, fw2.close."},{"Start":"01:17.655 ","End":"01:21.530","Text":"In part b we create a method called right times table append based on"},{"Start":"01:21.530 ","End":"01:26.690","Text":"the previous exercise with an int argument n and boolean return value,"},{"Start":"01:26.690 ","End":"01:30.935","Text":"which appends a times table to a file table.txt,"},{"Start":"01:30.935 ","End":"01:33.605","Text":"rather than overwrite the file each time."},{"Start":"01:33.605 ","End":"01:37.040","Text":"In part c, we adapt the code to ensure you use"},{"Start":"01:37.040 ","End":"01:42.335","Text":"end-of-line characters relevant to any operating system that the program might be run on."},{"Start":"01:42.335 ","End":"01:49.154","Text":"Then part d is about testing the code in d,1 we delete table.txt file, if it exists."},{"Start":"01:49.154 ","End":"01:55.155","Text":"In part d,2, we run writeTimesTableAppend with an argument of 9, d,3,"},{"Start":"01:55.155 ","End":"01:58.935","Text":"run writeTimesTableAppend again with an argument of 4,"},{"Start":"01:58.935 ","End":"02:04.725","Text":"and we check the file contains 9 times table followed by the 4 times table."},{"Start":"02:04.725 ","End":"02:07.360","Text":"In d, 4, we run writeTimesTable,"},{"Start":"02:07.360 ","End":"02:10.940","Text":"which was from the previous exercise with an argument of 3,"},{"Start":"02:10.940 ","End":"02:16.280","Text":"and we\u0027re asked in part 5 what has happened to the contents of the file and why?"},{"Start":"02:16.280 ","End":"02:18.170","Text":"Finally, in part 6,"},{"Start":"02:18.170 ","End":"02:21.890","Text":"we run writeTimesTableAppend with an argument of 6,"},{"Start":"02:21.890 ","End":"02:25.860","Text":"and check the contents are what we expect."},{"Start":"02:25.910 ","End":"02:32.240","Text":"In part a, we\u0027re going to make use of the code part to experiment with"},{"Start":"02:32.240 ","End":"02:35.300","Text":"a couple of methods and some familiar ones that we\u0027ve"},{"Start":"02:35.300 ","End":"02:38.675","Text":"already used and just see what changes might be."},{"Start":"02:38.675 ","End":"02:44.520","Text":"The first thing we\u0027re going to do in this part of part a, 2,"},{"Start":"02:44.520 ","End":"02:49.865","Text":"is we\u0027re going to define a string which contains the line separator,"},{"Start":"02:49.865 ","End":"02:53.825","Text":"this varies from operating system to operating system."},{"Start":"02:53.825 ","End":"02:59.090","Text":"It\u0027s best practice to actually know what it is when you\u0027re writing to"},{"Start":"02:59.090 ","End":"03:04.880","Text":"files and make sure that you use it rather than just slash n,"},{"Start":"03:04.880 ","End":"03:10.430","Text":"which we might put in strings when we\u0027re outputting to the screen because in a file,"},{"Start":"03:10.430 ","End":"03:16.910","Text":"Microsoft based operating systems expect carriage return and line feed,"},{"Start":"03:16.910 ","End":"03:21.860","Text":"whereas Unix files only expect a line feed."},{"Start":"03:21.860 ","End":"03:24.065","Text":"We\u0027ll see that in a moment."},{"Start":"03:24.065 ","End":"03:25.400","Text":"We\u0027ve got that character,"},{"Start":"03:25.400 ","End":"03:26.780","Text":"we can have a look at what\u0027s in it,"},{"Start":"03:26.780 ","End":"03:30.875","Text":"and you\u0027ll see it\u0027s slash r slash n,"},{"Start":"03:30.875 ","End":"03:36.050","Text":"pair of symbols that\u0027s why it\u0027s a string carriage return and line feed."},{"Start":"03:36.050 ","End":"03:39.080","Text":"That\u0027s what constitutes a new line in"},{"Start":"03:39.080 ","End":"03:43.205","Text":"a file on the system that I\u0027m running on, which is Windows."},{"Start":"03:43.205 ","End":"03:46.453","Text":"Part 3 is to create a file,"},{"Start":"03:46.453 ","End":"03:49.275","Text":"we\u0027ve done this already."},{"Start":"03:49.275 ","End":"03:51.725","Text":"There is a change coming up though."},{"Start":"03:51.725 ","End":"03:55.160","Text":"This file we\u0027re going to call testfile.txt,"},{"Start":"03:55.160 ","End":"03:56.480","Text":"and that\u0027s just a path."},{"Start":"03:56.480 ","End":"03:59.120","Text":"We haven\u0027t actually done anything with it but what we are going to do"},{"Start":"03:59.120 ","End":"04:01.990","Text":"now is we\u0027re going to create a FileWriter object,"},{"Start":"04:01.990 ","End":"04:05.985","Text":"and we\u0027re going to call the object fw,"},{"Start":"04:05.985 ","End":"04:09.285","Text":"and we\u0027re going to create it"},{"Start":"04:09.285 ","End":"04:16.190","Text":"from this file path"},{"Start":"04:16.190 ","End":"04:21.080","Text":"of test file.txt."},{"Start":"04:21.080 ","End":"04:24.624","Text":"No, I don\u0027t want to put txt on.txt in there,"},{"Start":"04:24.624 ","End":"04:27.490","Text":"I want to put my file in there,"},{"Start":"04:27.490 ","End":"04:30.050","Text":"because it\u0027s the path I\u0027ve already typed in."},{"Start":"04:30.050 ","End":"04:34.205","Text":"The change now is I want to add a parameter onto the end,"},{"Start":"04:34.205 ","End":"04:35.899","Text":"which we haven\u0027t seen before,"},{"Start":"04:35.899 ","End":"04:39.815","Text":"which the value of this argument is true,"},{"Start":"04:39.815 ","End":"04:42.680","Text":"and basically, this is the thing that will allow us to"},{"Start":"04:42.680 ","End":"04:45.505","Text":"append to a file rather than overwrite the file."},{"Start":"04:45.505 ","End":"04:47.200","Text":"You see we\u0027ve got a file now,"},{"Start":"04:47.200 ","End":"04:51.635","Text":"that it\u0027s been created by that line called testfile it currently has nothing in it."},{"Start":"04:51.635 ","End":"04:53.780","Text":"If you append to a file that has nothing in it,"},{"Start":"04:53.780 ","End":"04:56.360","Text":"you obviously add to the first line of the file."},{"Start":"04:56.360 ","End":"04:58.790","Text":"That\u0027s what we\u0027re going to do now,"},{"Start":"04:58.790 ","End":"05:02.660","Text":"we\u0027re going to write something to the file,"},{"Start":"05:02.660 ","End":"05:04.835","Text":"it doesn\u0027t matter what it is,"},{"Start":"05:04.835 ","End":"05:09.265","Text":"and then we\u0027re going to write something slightly different to the file,"},{"Start":"05:09.265 ","End":"05:11.370","Text":"and then we\u0027re going to close a file."},{"Start":"05:11.370 ","End":"05:14.330","Text":"You notice, if you look on the side there,"},{"Start":"05:14.330 ","End":"05:16.310","Text":"that the size of the file is still 0,"},{"Start":"05:16.310 ","End":"05:18.500","Text":"we haven\u0027t actually written anything to it yet."},{"Start":"05:18.500 ","End":"05:22.100","Text":"It\u0027s only when you close the file that those changes get written,"},{"Start":"05:22.100 ","End":"05:25.805","Text":"and you\u0027ve see now the file is updated and if I have a look inside it,"},{"Start":"05:25.805 ","End":"05:28.055","Text":"you\u0027ll see it\u0027s got what I wrote in."},{"Start":"05:28.055 ","End":"05:30.890","Text":"Now, they\u0027re all on 1 line because I"},{"Start":"05:30.890 ","End":"05:34.100","Text":"haven\u0027t put in a new line character in line in the string,"},{"Start":"05:34.100 ","End":"05:41.825","Text":"and what I should really use anyway is my combination of characters slash r and slash n,"},{"Start":"05:41.825 ","End":"05:45.560","Text":"which would be a new line on a windows system."},{"Start":"05:45.560 ","End":"05:49.655","Text":"Let\u0027s see how we do that because it\u0027s going to come up,"},{"Start":"05:49.655 ","End":"05:52.610","Text":"we\u0027re going to create another FileWriter object"},{"Start":"05:52.610 ","End":"05:56.005","Text":"now because we\u0027ve closed the previous one,"},{"Start":"05:56.005 ","End":"06:00.630","Text":"and this one is we\u0027re going to call fw2, same path again."},{"Start":"06:00.630 ","End":"06:03.873","Text":"It\u0027s going to be my file and we also want to append,"},{"Start":"06:03.873 ","End":"06:06.005","Text":"so we put true in there."},{"Start":"06:06.005 ","End":"06:10.320","Text":"I missed capital W there."},{"Start":"06:11.050 ","End":"06:17.540","Text":"That\u0027s now open the file again for appending to,"},{"Start":"06:17.540 ","End":"06:19.970","Text":"because it\u0027s already got some content in now,"},{"Start":"06:19.970 ","End":"06:22.910","Text":"when we write a new line to it,"},{"Start":"06:22.910 ","End":"06:25.685","Text":"as I\u0027m going to do in part 9 here,"},{"Start":"06:25.685 ","End":"06:28.820","Text":"we will write it to the end of the file,"},{"Start":"06:28.820 ","End":"06:32.270","Text":"and I\u0027m actually going to write a new line character in first or"},{"Start":"06:32.270 ","End":"06:36.320","Text":"characters that I stored into the string nl,"},{"Start":"06:36.320 ","End":"06:43.650","Text":"and then I\u0027m going to write whatever I want to write in the part 9 here."},{"Start":"06:43.650 ","End":"06:44.970","Text":"Let\u0027s do that,"},{"Start":"06:44.970 ","End":"06:47.660","Text":"and then let\u0027s close it again it won\u0027t have changed"},{"Start":"06:47.660 ","End":"06:50.750","Text":"until I close it and see what\u0027s happened."},{"Start":"06:50.750 ","End":"06:52.725","Text":"Let\u0027s open that file again,"},{"Start":"06:52.725 ","End":"06:54.415","Text":"and what you see is,"},{"Start":"06:54.415 ","End":"06:56.750","Text":"it\u0027s not broken onto a new line."},{"Start":"06:56.750 ","End":"06:58.760","Text":"There\u0027s no slash n character here,"},{"Start":"06:58.760 ","End":"07:01.745","Text":"but because nl contain this string,"},{"Start":"07:01.745 ","End":"07:05.336","Text":"and it\u0027s appropriate for this operating system that I\u0027m running on,"},{"Start":"07:05.336 ","End":"07:09.578","Text":"it\u0027s currently written onto a new line and the new string."},{"Start":"07:09.578 ","End":"07:14.240","Text":"The important thing here is it\u0027s appended added on to the end of what was already there,"},{"Start":"07:14.240 ","End":"07:18.080","Text":"remember we close the file and then we reopened it and we\u0027re"},{"Start":"07:18.080 ","End":"07:22.975","Text":"appending to the file because of this argument here."},{"Start":"07:22.975 ","End":"07:27.685","Text":"That\u0027s it. We needed to really demonstrate."},{"Start":"07:27.685 ","End":"07:31.190","Text":"Let\u0027s get into the code itself now,"},{"Start":"07:31.190 ","End":"07:37.830","Text":"and we really just going to take what we had earlier and we\u0027re going to adapt it."},{"Start":"07:37.830 ","End":"07:41.170","Text":"Let\u0027s take the method exactly,"},{"Start":"07:41.170 ","End":"07:44.290","Text":"complain because we\u0027ve already got one called that,"},{"Start":"07:44.290 ","End":"07:48.145","Text":"but we\u0027re going to call it TimesTableAppend,"},{"Start":"07:48.145 ","End":"07:50.255","Text":"as we\u0027ve been asked to in part b,"},{"Start":"07:50.255 ","End":"07:54.080","Text":"and still has an integer argument and a boolean return value."},{"Start":"07:54.080 ","End":"07:56.453","Text":"We\u0027ll still need a try and catch,"},{"Start":"07:56.453 ","End":"08:00.130","Text":"we\u0027ll still need a FileWriter object but this time,"},{"Start":"08:00.130 ","End":"08:07.770","Text":"we are going to put on the end here through because we want it to append,"},{"Start":"08:07.770 ","End":"08:10.295","Text":"and that\u0027s what this argument here means."},{"Start":"08:10.295 ","End":"08:14.480","Text":"We want to append to the file rather than overwrite it."},{"Start":"08:14.480 ","End":"08:16.618","Text":"That stays the same."},{"Start":"08:16.618 ","End":"08:18.590","Text":"Now there is a slash n here,"},{"Start":"08:18.590 ","End":"08:22.520","Text":"we\u0027ve been asked in part c to adapt it so that"},{"Start":"08:22.520 ","End":"08:27.065","Text":"we use end-of-line characters that are irrelevant to the operating system."},{"Start":"08:27.065 ","End":"08:28.970","Text":"Here we should really"},{"Start":"08:28.970 ","End":"08:37.915","Text":"put system.getProperty, line.separator."},{"Start":"08:37.915 ","End":"08:42.140","Text":"Now that could get very tedious typing that in all the time."},{"Start":"08:42.140 ","End":"08:46.385","Text":"Maybe a better thing to do is to take all of that,"},{"Start":"08:46.385 ","End":"08:48.215","Text":"store it into a variable,"},{"Start":"08:48.215 ","End":"08:50.830","Text":"to assume it\u0027s going to be a string not single character,"},{"Start":"08:50.830 ","End":"08:53.280","Text":"and then we\u0027ll get a good name."},{"Start":"08:53.280 ","End":"08:56.520","Text":"Let\u0027s call it nl again for new line,"},{"Start":"08:56.520 ","End":"08:58.610","Text":"and this time here at the end,"},{"Start":"08:58.610 ","End":"09:02.060","Text":"we just put nl rather than inside the string,"},{"Start":"09:02.060 ","End":"09:03.140","Text":"put a slash n,"},{"Start":"09:03.140 ","End":"09:05.840","Text":"because slash n will work for Unix systems."},{"Start":"09:05.840 ","End":"09:09.170","Text":"No problem, but if you write it to a Window system and it\u0027s"},{"Start":"09:09.170 ","End":"09:13.172","Text":"expecting to find character return line feed at the end of each line,"},{"Start":"09:13.172 ","End":"09:18.645","Text":"it won\u0027t if we just put a slash n. We\u0027ll just do that one more time down here,"},{"Start":"09:18.645 ","End":"09:22.190","Text":"nl rather than slash n,"},{"Start":"09:22.190 ","End":"09:24.830","Text":"and there are no other changes to make,"},{"Start":"09:24.830 ","End":"09:29.180","Text":"as all we needed to do to make it append was to change this value here,"},{"Start":"09:29.180 ","End":"09:32.380","Text":"and we\u0027ll do the other little change that we\u0027ve been asked to do."},{"Start":"09:32.380 ","End":"09:34.980","Text":"We need to take that quote out there."},{"Start":"09:34.980 ","End":"09:38.840","Text":"Let\u0027s see if this does what we expect it to do."},{"Start":"09:38.840 ","End":"09:40.470","Text":"Some other issue here."},{"Start":"09:40.470 ","End":"09:42.260","Text":"Yes, I do need quotes actually,"},{"Start":"09:42.260 ","End":"09:44.875","Text":"because it\u0027s a system property we\u0027re looking for,"},{"Start":"09:44.875 ","End":"09:48.190","Text":"and a thing it\u0027s looking for is line.separator."},{"Start":"09:48.190 ","End":"09:53.665","Text":"That\u0027s that, and now let\u0027s try and do what we need to do in the test,"},{"Start":"09:53.665 ","End":"09:57.860","Text":"actually it does say to delete the table.txt file I don\u0027t have one."},{"Start":"09:57.860 ","End":"09:59.750","Text":"That\u0027s fine I don\u0027t need to delete it."},{"Start":"09:59.750 ","End":"10:07.895","Text":"Now, I\u0027m going to create the file and write to it using wrightTimesTableAppend."},{"Start":"10:07.895 ","End":"10:11.530","Text":"I\u0027m going to pass the argument 9 to it,"},{"Start":"10:11.530 ","End":"10:14.100","Text":"and it\u0027s return true and it\u0027s created it,"},{"Start":"10:14.100 ","End":"10:18.290","Text":"and you can see that it already has contents I can see 1 kilobyte there,"},{"Start":"10:18.290 ","End":"10:19.820","Text":"so let\u0027s just have a look at that."},{"Start":"10:19.820 ","End":"10:25.490","Text":"My empty file was created and then it had written to it a 9-times table,"},{"Start":"10:25.490 ","End":"10:29.300","Text":"and then in part d, 3,"},{"Start":"10:29.300 ","End":"10:32.330","Text":"sorry, we we run it again,"},{"Start":"10:32.330 ","End":"10:34.090","Text":"but with an argument of 4."},{"Start":"10:34.090 ","End":"10:38.110","Text":"WriteTimesTableAppend, argument 4,"},{"Start":"10:38.110 ","End":"10:39.900","Text":"we get true back again,"},{"Start":"10:39.900 ","End":"10:43.200","Text":"and we still have our 9 times table there,"},{"Start":"10:43.200 ","End":"10:46.400","Text":"and we\u0027ve got 4 times table after it."},{"Start":"10:46.400 ","End":"10:48.943","Text":"That\u0027s what we would expect."},{"Start":"10:48.943 ","End":"10:54.425","Text":"But now we\u0027ve been asked to run writeTimesTable in part 4,"},{"Start":"10:54.425 ","End":"10:57.935","Text":"not writeTimesTableAppend, but writeTimesTable."},{"Start":"10:57.935 ","End":"11:02.670","Text":"If we do that with an argument of 3, we get true back."},{"Start":"11:02.670 ","End":"11:05.353","Text":"It did something, and it\u0027s happy."},{"Start":"11:05.353 ","End":"11:07.760","Text":"But what\u0027s happened is that,"},{"Start":"11:07.760 ","End":"11:12.200","Text":"what was in there previously has now been erased,"},{"Start":"11:12.200 ","End":"11:14.750","Text":"and we\u0027re asked why in part 5."},{"Start":"11:14.750 ","End":"11:18.740","Text":"Well, this is the difference between appending an overwriting."},{"Start":"11:18.740 ","End":"11:20.960","Text":"If you overwrite a file,"},{"Start":"11:20.960 ","End":"11:22.850","Text":"it doesn\u0027t matter what\u0027s there,"},{"Start":"11:22.850 ","End":"11:28.508","Text":"it assumes it\u0027s going to be overwritten and it will overwrite from the first line onwards"},{"Start":"11:28.508 ","End":"11:31.340","Text":"whereas append scans to"},{"Start":"11:31.340 ","End":"11:34.790","Text":"the end of the file and then adds the content on to the end of the file."},{"Start":"11:34.790 ","End":"11:38.975","Text":"There will be a file pointer with over write, it\u0027s just set to 0,"},{"Start":"11:38.975 ","End":"11:43.280","Text":"always at the beginning of the file with appended set at read"},{"Start":"11:43.280 ","End":"11:48.155","Text":"through and then sets the file pointer to the end of the existing content."},{"Start":"11:48.155 ","End":"11:52.120","Text":"This is why we\u0027ve wiped out the contents of the table file,"},{"Start":"11:52.120 ","End":"11:56.201","Text":"and if we were to run just to prove again,"},{"Start":"11:56.201 ","End":"12:00.225","Text":"writeTimesTableAppend with 6 this time,"},{"Start":"12:00.225 ","End":"12:03.140","Text":"you will see that this will not destroy."},{"Start":"12:03.140 ","End":"12:05.227","Text":"We\u0027ve already seen this, but we\u0027ll see it again,"},{"Start":"12:05.227 ","End":"12:10.190","Text":"but 3 times table that we just wrote to it using writeTimesTable is still there."},{"Start":"12:10.190 ","End":"12:16.775","Text":"Then writeTimesTableAppend routine has written to the end of that without overwriting."},{"Start":"12:16.775 ","End":"12:18.260","Text":"Nice simple one there,"},{"Start":"12:18.260 ","End":"12:20.045","Text":"but it\u0027s illustrated a couple of concepts."},{"Start":"12:20.045 ","End":"12:25.385","Text":"One, this idea of the new line character which goes into a file."},{"Start":"12:25.385 ","End":"12:28.865","Text":"The second one, is idea of appending or overwriting."},{"Start":"12:28.865 ","End":"12:30.905","Text":"If we look back at table again,"},{"Start":"12:30.905 ","End":"12:37.265","Text":"you\u0027ll see here that it says Unix line feed because when we wrote to this file with"},{"Start":"12:37.265 ","End":"12:46.250","Text":"just the writeTimesTable routine it was using our old style slash n on its own,"},{"Start":"12:46.250 ","End":"12:53.109","Text":"and that\u0027s why it\u0027s saying new line character in here is just asky 10 on its own,"},{"Start":"12:53.109 ","End":"12:56.000","Text":"there\u0027s no carriage return on line feed."},{"Start":"12:56.000 ","End":"13:00.845","Text":"That\u0027s what it\u0027s done and it\u0027s interpreting it as a Unix file."},{"Start":"13:00.845 ","End":"13:02.495","Text":"Whereas if we look at,"},{"Start":"13:02.495 ","End":"13:05.180","Text":"if I was to delete this file,"},{"Start":"13:05.180 ","End":"13:08.300","Text":"and run writeTimesTableAppend because we\u0027ve"},{"Start":"13:08.300 ","End":"13:13.115","Text":"correctly written a carriage return and a line feed when we open this one,"},{"Start":"13:13.115 ","End":"13:16.070","Text":"you\u0027ll see it says windows character return line feed is"},{"Start":"13:16.070 ","End":"13:20.105","Text":"reporting that each line ends with a carriage return line feed,"},{"Start":"13:20.105 ","End":"13:21.890","Text":"not a problem most of the time,"},{"Start":"13:21.890 ","End":"13:24.545","Text":"but sometimes when you\u0027re moving files between systems,"},{"Start":"13:24.545 ","End":"13:27.005","Text":"this little problem can bite you."},{"Start":"13:27.005 ","End":"13:33.290","Text":"The best practice is to make use of the system property here of line separator up to"},{"Start":"13:33.290 ","End":"13:36.740","Text":"find out what the new line characters"},{"Start":"13:36.740 ","End":"13:39.860","Text":"are on the operating system you\u0027re running your program on."},{"Start":"13:39.860 ","End":"13:42.930","Text":"That\u0027s it for this one, see you in the next one."}],"ID":29805},{"Watched":false,"Name":"Exercise 3","Duration":"15m 31s","ChapterTopicVideoID":28373,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.949","Text":"Hello, welcome back. In this exercise,"},{"Start":"00:02.949 ","End":"00:06.130","Text":"we\u0027re told that the Java file class allows the programmer to create"},{"Start":"00:06.130 ","End":"00:09.370","Text":"file paths that can be used to work with files and folders,"},{"Start":"00:09.370 ","End":"00:12.385","Text":"and that copes with differences between operating systems."},{"Start":"00:12.385 ","End":"00:15.040","Text":"In part a, we want to more used the code path to run"},{"Start":"00:15.040 ","End":"00:18.810","Text":"some code and check the folder we save that project in,"},{"Start":"00:18.810 ","End":"00:20.905","Text":"to see the impact of the code."},{"Start":"00:20.905 ","End":"00:24.250","Text":"A, i, we again import java.io.star."},{"Start":"00:24.250 ","End":"00:27.480","Text":"In A, ii, we run file.separator,"},{"Start":"00:27.480 ","End":"00:30.310","Text":"in A, iii file.separatorChar."},{"Start":"00:30.310 ","End":"00:33.130","Text":"In part A, iii, we say file,"},{"Start":"00:33.130 ","End":"00:35.305","Text":"my folder equals new file,"},{"Start":"00:35.305 ","End":"00:38.725","Text":"then in brackets and in quotes, test folder."},{"Start":"00:38.725 ","End":"00:42.430","Text":"Then in part 5, we run my folder.exists,"},{"Start":"00:42.430 ","End":"00:46.840","Text":"in part 6, my folder.mkdir, make directory."},{"Start":"00:46.840 ","End":"00:48.555","Text":"Then part 7,"},{"Start":"00:48.555 ","End":"00:51.200","Text":"my folder.exists once more."},{"Start":"00:51.200 ","End":"00:53.090","Text":"In part 8, we say file,"},{"Start":"00:53.090 ","End":"00:55.490","Text":"my file equals new file,"},{"Start":"00:55.490 ","End":"00:58.695","Text":"brackets and quotes, testfile.txt."},{"Start":"00:58.695 ","End":"01:00.300","Text":"Then we run in part 9,"},{"Start":"01:00.300 ","End":"01:04.620","Text":"myfile.exists, in part 10, myfile.create new file."},{"Start":"01:04.620 ","End":"01:08.560","Text":"Then once more in part 11, myfile.exists."},{"Start":"01:08.560 ","End":"01:13.250","Text":"In part b, we create a method called create file folder that returns"},{"Start":"01:13.250 ","End":"01:18.590","Text":"a boolean and accepts 2 strings named folder and filename as arguments."},{"Start":"01:18.590 ","End":"01:21.245","Text":"Within the method, we create a new folder."},{"Start":"01:21.245 ","End":"01:26.550","Text":"In part c, ii, we create a new path to the new folder using the folder parameter,"},{"Start":"01:26.550 ","End":"01:28.410","Text":"and in part c, ii,"},{"Start":"01:28.410 ","End":"01:31.710","Text":"we use mkdir method to create a new folder."},{"Start":"01:31.710 ","End":"01:34.984","Text":"If the value returned back from mkdir is false,"},{"Start":"01:34.984 ","End":"01:38.435","Text":"we exit from the method by returning false."},{"Start":"01:38.435 ","End":"01:42.844","Text":"In part d, we create a new file inside the folder we just created."},{"Start":"01:42.844 ","End":"01:46.205","Text":"In part d, i, we construct another new path"},{"Start":"01:46.205 ","End":"01:50.195","Text":"using both the folder parameter and a method from part a,"},{"Start":"01:50.195 ","End":"01:54.229","Text":"ii or iii and the filename parameter."},{"Start":"01:54.229 ","End":"01:55.550","Text":"In part d3,"},{"Start":"01:55.550 ","End":"01:58.130","Text":"we use the Create New File method to create"},{"Start":"01:58.130 ","End":"02:01.715","Text":"a new file with a given name in the given folder."},{"Start":"02:01.715 ","End":"02:03.620","Text":"In part d, iii,"},{"Start":"02:03.620 ","End":"02:06.895","Text":"if the value returned from Create New File is true,"},{"Start":"02:06.895 ","End":"02:10.230","Text":"we return true, otherwise we return false."},{"Start":"02:10.230 ","End":"02:15.415","Text":"Then part e is about testing the code with a series of folder and filenames,"},{"Start":"02:15.415 ","End":"02:18.050","Text":"and we check that the files and folders have been"},{"Start":"02:18.050 ","End":"02:20.980","Text":"created inside the current BlueJ project folder,"},{"Start":"02:20.980 ","End":"02:25.845","Text":"part e, i, we do it with folder1 and the filename file1.txt."},{"Start":"02:25.845 ","End":"02:30.375","Text":"E2 is folder 2 and the filename file1.txt,"},{"Start":"02:30.375 ","End":"02:34.260","Text":"and then e, iii is the folder name folder1 again,"},{"Start":"02:34.260 ","End":"02:37.080","Text":"and the filename file1.txt."},{"Start":"02:37.080 ","End":"02:39.830","Text":"Again. In part a,"},{"Start":"02:39.830 ","End":"02:43.340","Text":"we\u0027re going to use the coat pad to try out some of"},{"Start":"02:43.340 ","End":"02:47.255","Text":"the commands that we need to work with files."},{"Start":"02:47.255 ","End":"02:48.590","Text":"First thing we\u0027re going to do,"},{"Start":"02:48.590 ","End":"02:52.625","Text":"is to import the java IO class libraries."},{"Start":"02:52.625 ","End":"02:57.365","Text":"Let me do that using that line in part a, i."},{"Start":"02:57.365 ","End":"02:59.150","Text":"Then we\u0027re going to try a few things out."},{"Start":"02:59.150 ","End":"03:03.525","Text":"File is the class that deals with paths."},{"Start":"03:03.525 ","End":"03:08.315","Text":"Let\u0027s see what file separator does."},{"Start":"03:08.315 ","End":"03:11.015","Text":"I\u0027m always spelling separator."},{"Start":"03:11.015 ","End":"03:15.545","Text":"It\u0027s separate its A-R-A tall."},{"Start":"03:15.545 ","End":"03:19.505","Text":"We do that and it returns us a string,"},{"Start":"03:19.505 ","End":"03:24.350","Text":"which is the backslash character on Windows because that\u0027s what\u0027s used"},{"Start":"03:24.350 ","End":"03:29.689","Text":"in a path to separate a folder from the filename."},{"Start":"03:29.689 ","End":"03:31.290","Text":"That\u0027s what it is in Windows,"},{"Start":"03:31.290 ","End":"03:37.715","Text":"and because it\u0027s a backslash and strings in Java include escape codes with a slash."},{"Start":"03:37.715 ","End":"03:39.830","Text":"When we want to actually use a slash,"},{"Start":"03:39.830 ","End":"03:41.930","Text":"it\u0027s a double slash."},{"Start":"03:41.930 ","End":"03:46.115","Text":"You wouldn\u0027t see that in Unix and Linux are in a Mac."},{"Start":"03:46.115 ","End":"03:47.945","Text":"It\u0027ll be a forward slash,"},{"Start":"03:47.945 ","End":"03:52.040","Text":"but my machines or PC is using a double backslash because"},{"Start":"03:52.040 ","End":"03:53.630","Text":"a single backslash would cause it"},{"Start":"03:53.630 ","End":"03:57.980","Text":"some issues given that it\u0027s used for the escape character."},{"Start":"03:57.980 ","End":"04:00.260","Text":"That\u0027s file separator."},{"Start":"04:00.260 ","End":"04:03.125","Text":"If I do the same thing but put char on the end,"},{"Start":"04:03.125 ","End":"04:06.610","Text":"there is a different method in part a,"},{"Start":"04:06.610 ","End":"04:09.380","Text":"iii that returns exactly the same thing, but as a char."},{"Start":"04:09.380 ","End":"04:11.690","Text":"That char as you expect to be 1 character,"},{"Start":"04:11.690 ","End":"04:13.100","Text":"but as I said, when it outputs,"},{"Start":"04:13.100 ","End":"04:14.585","Text":"they say he\u0027s 1 character,"},{"Start":"04:14.585 ","End":"04:18.335","Text":"but it\u0027s showing you that this is an escape character in front of it."},{"Start":"04:18.335 ","End":"04:21.575","Text":"Although it appears like 2 characters, it\u0027s only 1."},{"Start":"04:21.575 ","End":"04:24.425","Text":"We can get back from file.separator,"},{"Start":"04:24.425 ","End":"04:28.385","Text":"either a string or a char for use file separated char,"},{"Start":"04:28.385 ","End":"04:34.764","Text":"which is the character that is being used to limit a folder and a file."},{"Start":"04:34.764 ","End":"04:37.890","Text":"As I said on Windows, it\u0027s a backslash."},{"Start":"04:37.890 ","End":"04:41.070","Text":"That\u0027s fine. What about part 4?"},{"Start":"04:41.070 ","End":"04:46.130","Text":"I\u0027m going to use File and we\u0027re going to create from the file class,"},{"Start":"04:46.130 ","End":"04:50.500","Text":"let me call it my folder using new file,"},{"Start":"04:50.500 ","End":"04:53.030","Text":"and then in quotes,"},{"Start":"04:53.030 ","End":"04:55.890","Text":"we can create some test folder."},{"Start":"04:55.890 ","End":"04:58.345","Text":"Let\u0027s see what that does."},{"Start":"04:58.345 ","End":"05:03.770","Text":"I happened to have open over here the current working directory."},{"Start":"05:03.770 ","End":"05:08.180","Text":"It\u0027s done nothing at the moment because all it\u0027s done is constructed a file path."},{"Start":"05:08.180 ","End":"05:17.600","Text":"If I now do my folder.exists is what I\u0027ve been asked to do before."},{"Start":"05:17.600 ","End":"05:21.350","Text":"Does that folder exists and it returns me false because it doesn\u0027t"},{"Start":"05:21.350 ","End":"05:24.500","Text":"exist but if I now try and create a folder,"},{"Start":"05:24.500 ","End":"05:28.885","Text":"in my folder is something that allows me to create a path,"},{"Start":"05:28.885 ","End":"05:31.039","Text":"and then I can work with files."},{"Start":"05:31.039 ","End":"05:37.360","Text":"My folder.mkdir, make a directory or a folder,"},{"Start":"05:37.360 ","End":"05:40.365","Text":"returns true, which means it\u0027s a success."},{"Start":"05:40.365 ","End":"05:42.770","Text":"You\u0027ll see over here is a folder has now"},{"Start":"05:42.770 ","End":"05:45.740","Text":"appeared in my current directory called Test folder."},{"Start":"05:45.740 ","End":"05:47.780","Text":"It\u0027s empty because nothing in there,"},{"Start":"05:47.780 ","End":"05:49.190","Text":"but it was able to create it,"},{"Start":"05:49.190 ","End":"05:52.160","Text":"and so if I do, my folder.exists, again,"},{"Start":"05:52.160 ","End":"05:54.080","Text":"it will now return true,"},{"Start":"05:54.080 ","End":"05:56.660","Text":"whereas it previously we saw return false"},{"Start":"05:56.660 ","End":"05:59.510","Text":"because we haven\u0027t actually created the folder yet."},{"Start":"05:59.510 ","End":"06:03.605","Text":"The same thing will happen for files that we do a, viii."},{"Start":"06:03.605 ","End":"06:09.785","Text":"We\u0027re going to create a new path this time we\u0027ll call it my file rather than my folder."},{"Start":"06:09.785 ","End":"06:12.800","Text":"We need to give the file a name."},{"Start":"06:12.800 ","End":"06:17.060","Text":"Let\u0027s call it test file.txt,"},{"Start":"06:17.060 ","End":"06:19.855","Text":"as it\u0027s asked me to do in part 8."},{"Start":"06:19.855 ","End":"06:22.850","Text":"Again, we can see if that file exists."},{"Start":"06:22.850 ","End":"06:26.330","Text":"That path is not called my folder, It\u0027s called my file."},{"Start":"06:26.330 ","End":"06:28.880","Text":"Does it exist? No, it doesn\u0027t exist."},{"Start":"06:28.880 ","End":"06:34.280","Text":"Then we\u0027ll actually try and create it using part 10 there."},{"Start":"06:34.280 ","End":"06:40.955","Text":"As you can see, we just say myfile.create a new file."},{"Start":"06:40.955 ","End":"06:43.660","Text":"See what that does for us."},{"Start":"06:43.660 ","End":"06:48.350","Text":"You\u0027ll see that it has created a file called testfile.txt."},{"Start":"06:48.350 ","End":"06:51.995","Text":"I\u0027ve got my file extension hidden for known files,"},{"Start":"06:51.995 ","End":"06:53.540","Text":"the files actually empty."},{"Start":"06:53.540 ","End":"06:57.155","Text":"You can see it\u0027s 0 kilobytes because we\u0027ve never written anything to a file."},{"Start":"06:57.155 ","End":"07:01.190","Text":"We\u0027ve managed to create an empty text file called testfile."},{"Start":"07:01.190 ","End":"07:03.470","Text":"Again, if I do myfile.exist,"},{"Start":"07:03.470 ","End":"07:04.925","Text":"it will now say true,"},{"Start":"07:04.925 ","End":"07:06.770","Text":"because it does exist."},{"Start":"07:06.770 ","End":"07:12.105","Text":"We now know enough to go ahead and create program."},{"Start":"07:12.105 ","End":"07:15.250","Text":"Let\u0027s go to part b,"},{"Start":"07:15.250 ","End":"07:19.535","Text":"which has to create a method called create file folder,"},{"Start":"07:19.535 ","End":"07:23.000","Text":"because we\u0027re going to create both a file and a folder,"},{"Start":"07:23.000 ","End":"07:27.240","Text":"it returns a Boolean."},{"Start":"07:27.240 ","End":"07:32.610","Text":"It accepts as 2 parameters in fact,"},{"Start":"07:32.610 ","End":"07:39.150","Text":"a string called folder and another string called filename."},{"Start":"07:39.150 ","End":"07:43.255","Text":"With this 1 method, we can create both a file and a folder"},{"Start":"07:43.255 ","End":"07:48.205","Text":"and the file gets created in the folder that we specified to create."},{"Start":"07:48.205 ","End":"07:53.050","Text":"Part C, we create a new path to the new folder using the folder parameter."},{"Start":"07:53.050 ","End":"07:56.125","Text":"Well we know how to do that we just do encode per a moment ago."},{"Start":"07:56.125 ","End":"07:59.830","Text":"Let\u0027s call it, use the same names if you want."},{"Start":"07:59.830 ","End":"08:03.295","Text":"MyFolder let me use,"},{"Start":"08:03.295 ","End":"08:08.020","Text":"new and say it\u0027s from the file class that we\u0027re going to create it."},{"Start":"08:08.020 ","End":"08:15.760","Text":"Then we give it a path because all we want to give it is the name folder,"},{"Start":"08:15.760 ","End":"08:18.100","Text":"wherever is passed in into folder,"},{"Start":"08:18.100 ","End":"08:20.755","Text":"that should be all we need to do."},{"Start":"08:20.755 ","End":"08:25.795","Text":"But to create the folders we saw before in part C2,"},{"Start":"08:25.795 ","End":"08:28.285","Text":"we use mkdir,"},{"Start":"08:28.285 ","End":"08:34.975","Text":"so myFolder.mkdir should now"},{"Start":"08:34.975 ","End":"08:40.885","Text":"create me a folder with a name whatever was passed in into folder."},{"Start":"08:40.885 ","End":"08:47.440","Text":"Great, but I want to check whether it has actually successfully created it."},{"Start":"08:47.440 ","End":"08:48.730","Text":"In part 2,"},{"Start":"08:48.730 ","End":"08:52.839","Text":"it says if the value returned back from mkdir is false,"},{"Start":"08:52.839 ","End":"08:56.185","Text":"we exit from the method by returning false."},{"Start":"08:56.185 ","End":"08:58.060","Text":"I need to check it."},{"Start":"08:58.060 ","End":"09:00.684","Text":"If I say if it\u0027s true,"},{"Start":"09:00.684 ","End":"09:03.550","Text":"then I go ahead and do something."},{"Start":"09:03.550 ","End":"09:09.370","Text":"If not, I\u0027m going to return false because it will never get to"},{"Start":"09:09.370 ","End":"09:16.480","Text":"this line unless it wasn\u0027t able to create the directory."},{"Start":"09:16.480 ","End":"09:21.010","Text":"It will never get to this line because I\u0027m going to have a return true in here somewhere."},{"Start":"09:21.010 ","End":"09:23.560","Text":"That will be, I could pronounce it,"},{"Start":"09:23.560 ","End":"09:25.120","Text":"but I don\u0027t need to."},{"Start":"09:25.120 ","End":"09:28.630","Text":"That would have both created the folder and"},{"Start":"09:28.630 ","End":"09:32.155","Text":"checked if it was successfully created and if it wasn\u0027t,"},{"Start":"09:32.155 ","End":"09:33.940","Text":"it will return false."},{"Start":"09:33.940 ","End":"09:38.290","Text":"If it was, I can now create the file inside that folder."},{"Start":"09:38.290 ","End":"09:42.790","Text":"So this is part D. In D1 I need to create another path."},{"Start":"09:42.790 ","End":"09:45.130","Text":"I can\u0027t reuse my folder path,"},{"Start":"09:45.130 ","End":"09:48.330","Text":"I have to create a new path for the file,"},{"Start":"09:48.330 ","End":"09:50.790","Text":"so I\u0027ll call it myFile."},{"Start":"09:50.790 ","End":"09:53.310","Text":"This time it\u0027s more complex."},{"Start":"09:53.310 ","End":"09:55.275","Text":"What\u0027s got to go inside these brackets?"},{"Start":"09:55.275 ","End":"09:59.130","Text":"It\u0027s not just the name of the file because I want to create"},{"Start":"09:59.130 ","End":"10:04.105","Text":"the file in the folder I just created so I put folder."},{"Start":"10:04.105 ","End":"10:07.675","Text":"Then I need the file separator,"},{"Start":"10:07.675 ","End":"10:09.535","Text":"which is this for Windows."},{"Start":"10:09.535 ","End":"10:11.245","Text":"Actually, it\u0027s that for Windows,"},{"Start":"10:11.245 ","End":"10:16.540","Text":"it would be this if I was on Unix or a Mac but actually,"},{"Start":"10:16.540 ","End":"10:18.115","Text":"it\u0027s bad practice to do that."},{"Start":"10:18.115 ","End":"10:20.440","Text":"I don\u0027t know what operating system I\u0027m running on,"},{"Start":"10:20.440 ","End":"10:25.510","Text":"but I can get the correct string returned to me by"},{"Start":"10:25.510 ","End":"10:30.865","Text":"just running the file separator method."},{"Start":"10:30.865 ","End":"10:32.995","Text":"Not method actually is an attribute."},{"Start":"10:32.995 ","End":"10:35.630","Text":"I can just get it from there."},{"Start":"10:36.120 ","End":"10:40.030","Text":"That would either put a forward slash or backslash."},{"Start":"10:40.030 ","End":"10:42.940","Text":"Then I need to say what a file name is."},{"Start":"10:42.940 ","End":"10:45.174","Text":"Now I have a path."},{"Start":"10:45.174 ","End":"10:50.950","Text":"From that, I can create myFile as I did encode pad earlier."},{"Start":"10:50.950 ","End":"10:54.655","Text":"So I\u0027m going to use the create newFile method, but this time,"},{"Start":"10:54.655 ","End":"10:59.980","Text":"I use myFile.createNewFile, not myFolder.mkdir."},{"Start":"10:59.980 ","End":"11:05.005","Text":"That will do the job but again,"},{"Start":"11:05.005 ","End":"11:11.995","Text":"I should check whether it\u0027s successful and this will return true if it was successful."},{"Start":"11:11.995 ","End":"11:13.900","Text":"If it was successful,"},{"Start":"11:13.900 ","End":"11:16.045","Text":"I want to return true."},{"Start":"11:16.045 ","End":"11:18.490","Text":"Again, if it\u0027s not,"},{"Start":"11:18.490 ","End":"11:20.005","Text":"I want to return false."},{"Start":"11:20.005 ","End":"11:23.440","Text":"Actually, only 2 para brackets here."},{"Start":"11:23.440 ","End":"11:27.805","Text":"Don\u0027t need it or make it clear if I do put para brackets there."},{"Start":"11:27.805 ","End":"11:30.700","Text":"If it was successfully able to create a file,"},{"Start":"11:30.700 ","End":"11:35.200","Text":"I return true, otherwise I return false."},{"Start":"11:35.200 ","End":"11:38.935","Text":"It\u0027s going to return false anyway down here."},{"Start":"11:38.935 ","End":"11:42.100","Text":"If I don\u0027t return true here,"},{"Start":"11:42.100 ","End":"11:47.005","Text":"it will drop out of this part of the code and then it will drop out in the if statement."},{"Start":"11:47.005 ","End":"11:49.270","Text":"I can actually remove that false."},{"Start":"11:49.270 ","End":"11:51.760","Text":"It will do exactly the same job."},{"Start":"11:51.760 ","End":"11:56.995","Text":"Now this will not compile because I haven\u0027t caught this,"},{"Start":"11:56.995 ","End":"11:59.305","Text":"it\u0027s a checked exception."},{"Start":"11:59.305 ","End":"12:04.105","Text":"I will need to put all of this inside a try and catch block."},{"Start":"12:04.105 ","End":"12:05.725","Text":"Let\u0027s go ahead and do that."},{"Start":"12:05.725 ","End":"12:08.245","Text":"I\u0027m going to indent it first of all."},{"Start":"12:08.245 ","End":"12:17.280","Text":"Then up here, I can put my try and down here I can put my catch."},{"Start":"12:17.280 ","End":"12:24.040","Text":"I need to type of IOException and I can Print out."},{"Start":"12:24.330 ","End":"12:27.775","Text":"That looks like enough"},{"Start":"12:27.775 ","End":"12:32.290","Text":"and possibly miss the bracket out somewhere. Let\u0027s just check that out."},{"Start":"12:32.290 ","End":"12:38.980","Text":"Yes, I also need to return false if it ends up in here as well."},{"Start":"12:38.980 ","End":"12:42.355","Text":"There\u0027s lots of different ways I could get out of this."},{"Start":"12:42.355 ","End":"12:46.075","Text":"First of all, if the folder was successful,"},{"Start":"12:46.075 ","End":"12:47.905","Text":"it then tries to create a file."},{"Start":"12:47.905 ","End":"12:49.180","Text":"If that was successful,"},{"Start":"12:49.180 ","End":"12:52.465","Text":"I return true but only if both those things have happened."},{"Start":"12:52.465 ","End":"12:57.205","Text":"If I was not able to create a folder,"},{"Start":"12:57.205 ","End":"12:58.930","Text":"I would return false,"},{"Start":"12:58.930 ","End":"13:00.760","Text":"if I was able to create the folder,"},{"Start":"13:00.760 ","End":"13:04.840","Text":"but not the file, I would also return false because it will end up down here."},{"Start":"13:04.840 ","End":"13:07.885","Text":"If any other thing that wasn\u0027t caught was triggered,"},{"Start":"13:07.885 ","End":"13:12.415","Text":"another IOException, it would come down to here and also return false."},{"Start":"13:12.415 ","End":"13:15.610","Text":"Let\u0027s see what happens when we run all this."},{"Start":"13:15.610 ","End":"13:19.465","Text":"I need to create a new object and then from it,"},{"Start":"13:19.465 ","End":"13:21.700","Text":"I want to create createFileFolder."},{"Start":"13:21.700 ","End":"13:24.370","Text":"In the first part of the test, E1,"},{"Start":"13:24.370 ","End":"13:34.390","Text":"I\u0027ve been asked to use a folder named folder1 and then the filename, file1.txt."},{"Start":"13:34.390 ","End":"13:36.625","Text":"Copy that because I\u0027ll need it again."},{"Start":"13:36.625 ","End":"13:39.475","Text":"Let\u0027s see if it\u0027s able to create a folder called folder1,"},{"Start":"13:39.475 ","End":"13:44.290","Text":"and within it a file called file1.txt. It\u0027s return true."},{"Start":"13:44.290 ","End":"13:46.900","Text":"It\u0027s indicated that it was able to do it but actually,"},{"Start":"13:46.900 ","End":"13:48.550","Text":"I can see folder1 here."},{"Start":"13:48.550 ","End":"13:49.825","Text":"If I go inside it,"},{"Start":"13:49.825 ","End":"13:52.465","Text":"I can also see file1."},{"Start":"13:52.465 ","End":"13:55.660","Text":"Although it\u0027s empty, it\u0027s got a size of 0."},{"Start":"13:55.660 ","End":"14:01.299","Text":"If I open it, you\u0027ll see that it is empty indeed. That\u0027s great."},{"Start":"14:01.299 ","End":"14:09.550","Text":"Let\u0027s test it again with another value or pair of values, so createFileFolder."},{"Start":"14:09.550 ","End":"14:13.900","Text":"This time we\u0027re going to create folder2 same file name as before."},{"Start":"14:13.900 ","End":"14:17.680","Text":"It shouldn\u0027t complain about that because they\u0027re in separate folders. There we go."},{"Start":"14:17.680 ","End":"14:21.955","Text":"We\u0027ve got folder2 and within another file called file1.txt."},{"Start":"14:21.955 ","End":"14:26.920","Text":"That\u0027s great but the final test, part E3,"},{"Start":"14:26.920 ","End":"14:32.920","Text":"we\u0027re going to give the same folder name and filename as we already gave in part 1,"},{"Start":"14:32.920 ","End":"14:38.800","Text":"so we should get false coming back and it won\u0027t attempt to go any further."},{"Start":"14:38.800 ","End":"14:40.660","Text":"Let\u0027s try that."},{"Start":"14:40.660 ","End":"14:47.200","Text":"I want to put folder1 again in here and then file1.txt there."},{"Start":"14:47.200 ","End":"14:49.615","Text":"It does return false,"},{"Start":"14:49.615 ","End":"14:52.254","Text":"and it hasn\u0027t tried to create another file."},{"Start":"14:52.254 ","End":"14:54.235","Text":"It\u0027s already got file1 there."},{"Start":"14:54.235 ","End":"14:57.845","Text":"That logic seems to have all worked out."},{"Start":"14:57.845 ","End":"15:01.805","Text":"We were able to create a new folder given"},{"Start":"15:01.805 ","End":"15:06.980","Text":"a name and also a file within that folder with a given name."},{"Start":"15:06.980 ","End":"15:11.240","Text":"This will work across different operating systems"},{"Start":"15:11.240 ","End":"15:16.100","Text":"because the file separator is correctly determined at run-time."},{"Start":"15:16.100 ","End":"15:19.550","Text":"It\u0027s either the forward slash if we\u0027re running on"},{"Start":"15:19.550 ","End":"15:23.389","Text":"a Unix style system or a backslash for Windows,"},{"Start":"15:23.389 ","End":"15:27.335","Text":"we correctly return true or false depending on the success of all of this."},{"Start":"15:27.335 ","End":"15:29.780","Text":"That\u0027s it for this 1. Thanks very much for watching."},{"Start":"15:29.780 ","End":"15:32.250","Text":"See you shortly for the next 1."}],"ID":29806},{"Watched":false,"Name":"Exercise 4","Duration":"14m 3s","ChapterTopicVideoID":28374,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.710","Text":"Hi there, welcome back."},{"Start":"00:01.710 ","End":"00:06.090","Text":"In this exercise, we\u0027ve been told a scanner can be used to get input from a keyboard,"},{"Start":"00:06.090 ","End":"00:07.535","Text":"or from a file."},{"Start":"00:07.535 ","End":"00:11.415","Text":"In Part a, we use the code pad to try some code."},{"Start":"00:11.415 ","End":"00:15.068","Text":"First part being import java.io.star,"},{"Start":"00:15.068 ","End":"00:18.120","Text":"and then import java.util.Scanner."},{"Start":"00:18.120 ","End":"00:23.880","Text":"On the next line we put File fileToOpen = new File(\"README.txt\"),"},{"Start":"00:23.880 ","End":"00:26.460","Text":"and read me is in capital letters."},{"Start":"00:26.460 ","End":"00:32.430","Text":"Then in Part 4 we use Scanner in = new Scanner(fileToOpen)."},{"Start":"00:32.430 ","End":"00:34.540","Text":"Then from Part 5 onwards,"},{"Start":"00:34.540 ","End":"00:35.994","Text":"we run a number of methods."},{"Start":"00:35.994 ","End":"00:38.765","Text":"The first one being in.hasNext(),"},{"Start":"00:38.765 ","End":"00:40.865","Text":"then we do in.next(),"},{"Start":"00:40.865 ","End":"00:43.925","Text":"3 times from Part 6-8."},{"Start":"00:43.925 ","End":"00:51.235","Text":"Then in.nextLine() twice, and then in.close(), and in.next."},{"Start":"00:51.235 ","End":"00:54.620","Text":"In Part b, we create a new method called count words,"},{"Start":"00:54.620 ","End":"00:59.405","Text":"which returns an int and accepts a string argument called filename."},{"Start":"00:59.405 ","End":"01:03.860","Text":"In Part c, we create a path to the file passed in as filename,"},{"Start":"01:03.860 ","End":"01:05.300","Text":"and in Part d,"},{"Start":"01:05.300 ","End":"01:09.110","Text":"we create a scanner object using the path we created in"},{"Start":"01:09.110 ","End":"01:14.090","Text":"Part c. In Part e we create a string variable data,"},{"Start":"01:14.090 ","End":"01:16.265","Text":"and an int variable count,"},{"Start":"01:16.265 ","End":"01:18.395","Text":"which is initialized to 0."},{"Start":"01:18.395 ","End":"01:23.710","Text":"In Part f, using a while loop which terminates when there are no more tokens to read,"},{"Start":"01:23.710 ","End":"01:27.480","Text":"we read the next token into data using next(),"},{"Start":"01:27.480 ","End":"01:31.307","Text":"we force all characters inside data to lowercase,"},{"Start":"01:31.307 ","End":"01:37.318","Text":"we replace all characters apart from lowercase letters with the empty string,"},{"Start":"01:37.318 ","End":"01:40.748","Text":"we increment count if data contains a word, i.e."},{"Start":"01:40.748 ","End":"01:42.780","Text":"doesn\u0027t contain the empty string,"},{"Start":"01:42.780 ","End":"01:44.480","Text":"and in Part g,"},{"Start":"01:44.480 ","End":"01:45.859","Text":"when the loop terminates,"},{"Start":"01:45.859 ","End":"01:49.370","Text":"we close the file and return count."},{"Start":"01:49.370 ","End":"01:55.328","Text":"In Part h, we return minus 1 only if an exception occurred,"},{"Start":"01:55.328 ","End":"01:58.490","Text":"and then Part i and j is about testing,"},{"Start":"01:58.490 ","End":"02:03.605","Text":"i is to test with README.txt as the input filename,"},{"Start":"02:03.605 ","End":"02:05.945","Text":"and we expect 52 to be returned."},{"Start":"02:05.945 ","End":"02:10.205","Text":"Part j, where we test with missing.txt as the input filename,"},{"Start":"02:10.205 ","End":"02:12.365","Text":"and that should return minus 1."},{"Start":"02:12.365 ","End":"02:13.610","Text":"Finally, in Part k,"},{"Start":"02:13.610 ","End":"02:17.780","Text":"we create our own text file with a few tests words and it across several lines,"},{"Start":"02:17.780 ","End":"02:22.360","Text":"and we check the number of words counted by the program is correct."},{"Start":"02:22.360 ","End":"02:28.655","Text":"Let\u0027s mess around in code pad as we\u0027ve been asked to do in Part a."},{"Start":"02:28.655 ","End":"02:34.100","Text":"We need to import java.io in order to do anything with files at"},{"Start":"02:34.100 ","End":"02:39.620","Text":"all but this time we also need to import Scanner,"},{"Start":"02:39.620 ","End":"02:42.335","Text":"which is part of java.util,"},{"Start":"02:42.335 ","End":"02:46.200","Text":"so that we can manipulate individual,"},{"Start":"02:46.200 ","End":"02:48.510","Text":"I\u0027ve got two full stops there,"},{"Start":"02:48.510 ","End":"02:53.315","Text":"so we can manipulate individual words and lines in a file."},{"Start":"02:53.315 ","End":"02:57.350","Text":"The next part is very similar to something we\u0027ve already done in"},{"Start":"02:57.350 ","End":"03:01.040","Text":"codepad previous exercise. Let\u0027s do that."},{"Start":"03:01.040 ","End":"03:04.890","Text":"It looks like we\u0027re creating a path,"},{"Start":"03:04.890 ","End":"03:06.889","Text":"and then from that path,"},{"Start":"03:06.889 ","End":"03:13.025","Text":"we are going to create a Scanner passing path to the scanner object."},{"Start":"03:13.025 ","End":"03:16.160","Text":"That\u0027s done, now we can use Scanner,"},{"Start":"03:16.160 ","End":"03:18.559","Text":"and we\u0027ve got it in here,"},{"Start":"03:18.559 ","End":"03:20.765","Text":"to do various things."},{"Start":"03:20.765 ","End":"03:23.810","Text":"The first thing we can do is use hasNext(),"},{"Start":"03:23.810 ","End":"03:28.820","Text":"and what that does is tell us if we have another token available, so it\u0027s true."},{"Start":"03:28.820 ","End":"03:31.920","Text":"That means there is something to read from the stream,"},{"Start":"03:31.920 ","End":"03:36.740","Text":"and the stream is coming from a file and we\u0027ve used it previously from a keyboard."},{"Start":"03:36.740 ","End":"03:42.124","Text":"That\u0027s what hasNext() does and it will keep doing that for as long as I\u0027ve got words."},{"Start":"03:42.124 ","End":"03:44.440","Text":"I haven\u0027t actually read anything from the stream yet,"},{"Start":"03:44.440 ","End":"03:47.690","Text":"to read a word from the stream or read a token,"},{"Start":"03:47.690 ","End":"03:49.880","Text":"I should say, from the stream, could be anything,"},{"Start":"03:49.880 ","End":"03:51.710","Text":"not necessarily words,"},{"Start":"03:51.710 ","End":"03:54.825","Text":"I use next to [inaudible] for keyboard,"},{"Start":"03:54.825 ","End":"03:58.250","Text":"and that has read a massive long line of dashes."},{"Start":"03:58.250 ","End":"04:02.870","Text":"We\u0027re just using the README file that comes with every Bluejay project in which"},{"Start":"04:02.870 ","End":"04:07.850","Text":"the first line of is a long secrets of hyphens."},{"Start":"04:07.850 ","End":"04:11.450","Text":"That\u0027s why we got one whole long string in."},{"Start":"04:11.450 ","End":"04:12.920","Text":"There is no spaces in there."},{"Start":"04:12.920 ","End":"04:16.850","Text":"By default, when a scanner scans it\u0027s looking for spaces."},{"Start":"04:16.850 ","End":"04:20.016","Text":"Let\u0027s do in.next() again,"},{"Start":"04:20.016 ","End":"04:23.150","Text":"we\u0027ve been asked to do it 3 times in 6,"},{"Start":"04:23.150 ","End":"04:25.145","Text":"7, and 8 of Part a."},{"Start":"04:25.145 ","End":"04:26.750","Text":"Let\u0027s do it again."},{"Start":"04:26.750 ","End":"04:28.400","Text":"We get the word this."},{"Start":"04:28.400 ","End":"04:31.415","Text":"We get the word is,"},{"Start":"04:31.415 ","End":"04:33.298","Text":"and I could carry on doing that if I wanted to,"},{"Start":"04:33.298 ","End":"04:37.948","Text":"and I would basically get individual words one at a time,"},{"Start":"04:37.948 ","End":"04:41.480","Text":"because the scanner\u0027s picking up on the space between the words,"},{"Start":"04:41.480 ","End":"04:45.410","Text":"and it\u0027s saying, here\u0027s a new token and here\u0027s the next tokens and so on."},{"Start":"04:45.410 ","End":"04:49.550","Text":"But in Number 9 here of Part a,"},{"Start":"04:49.550 ","End":"04:51.200","Text":"it\u0027s a slightly different one."},{"Start":"04:51.200 ","End":"04:53.450","Text":"It says, in.next line,"},{"Start":"04:53.450 ","End":"04:58.790","Text":"and unsurprisingly that we\u0027ll get a whole line because I read the first couple of words."},{"Start":"04:58.790 ","End":"05:01.160","Text":"This is from this first line,"},{"Start":"05:01.160 ","End":"05:03.260","Text":"I only got those individual words out,"},{"Start":"05:03.260 ","End":"05:05.345","Text":"it\u0027s going to read the rest of the line,"},{"Start":"05:05.345 ","End":"05:06.740","Text":"when I do next line here."},{"Start":"05:06.740 ","End":"05:08.195","Text":"If I did next line again,"},{"Start":"05:08.195 ","End":"05:10.726","Text":"I\u0027d get a whole of the following line,"},{"Start":"05:10.726 ","End":"05:13.340","Text":"and the whole of the next line, and so on."},{"Start":"05:13.340 ","End":"05:15.590","Text":"I\u0027ve done that a couple of times."},{"Start":"05:15.590 ","End":"05:20.023","Text":"Then Part 11, I\u0027m told to close the scanner object,"},{"Start":"05:20.023 ","End":"05:22.640","Text":"and because it\u0027s using a file,"},{"Start":"05:22.640 ","End":"05:25.475","Text":"it will actually close the file as well."},{"Start":"05:25.475 ","End":"05:27.290","Text":"Let\u0027s do that."},{"Start":"05:27.290 ","End":"05:32.165","Text":"Then I do in Part 12 one final in.next(),"},{"Start":"05:32.165 ","End":"05:35.810","Text":"and it generates an error because it\u0027s basically saying,"},{"Start":"05:35.810 ","End":"05:38.870","Text":"\"You\u0027ve closed this, I can\u0027t do a next on something"},{"Start":"05:38.870 ","End":"05:42.620","Text":"that\u0027s been closed,\" and that\u0027s exactly what we would expect."},{"Start":"05:42.620 ","End":"05:46.744","Text":"Good, we\u0027ve worked out how to use a few of these methods,"},{"Start":"05:46.744 ","End":"05:49.565","Text":"should be enough now to work out"},{"Start":"05:49.565 ","End":"05:53.645","Text":"how to write the code that we need to write in Part b onwards."},{"Start":"05:53.645 ","End":"05:57.485","Text":"Let\u0027s create our methods called countWords."},{"Start":"05:57.485 ","End":"05:59.450","Text":"It returns an int,"},{"Start":"05:59.450 ","End":"06:03.900","Text":"and accepts a string argument called filename."},{"Start":"06:04.490 ","End":"06:09.120","Text":"There we go. There\u0027s our method declaration."},{"Start":"06:09.120 ","End":"06:14.630","Text":"Then we\u0027re going to create a path to the file passed in as filename."},{"Start":"06:14.630 ","End":"06:16.989","Text":"We create path using file,"},{"Start":"06:16.989 ","End":"06:21.900","Text":"it\u0027s called the path infile because it\u0027s the input file."},{"Start":"06:21.900 ","End":"06:24.705","Text":"We say, new file,"},{"Start":"06:24.705 ","End":"06:26.430","Text":"I\u0027ve done this before,"},{"Start":"06:26.430 ","End":"06:28.310","Text":"and we don\u0027t need to quotes,"},{"Start":"06:28.310 ","End":"06:29.900","Text":"we need just filename,"},{"Start":"06:29.900 ","End":"06:32.840","Text":"because this is going to be sent to us as an argument."},{"Start":"06:32.840 ","End":"06:35.290","Text":"There\u0027s the path constructed,"},{"Start":"06:35.290 ","End":"06:36.900","Text":"and that\u0027s Part c done."},{"Start":"06:36.900 ","End":"06:41.140","Text":"In part d, we create a scanner object using the path we\u0027ve just created."},{"Start":"06:41.140 ","End":"06:44.680","Text":"We did that in the codepad just a moment ago."},{"Start":"06:44.680 ","End":"06:47.480","Text":"We give it a name, I\u0027ll call it in,"},{"Start":"06:48.120 ","End":"06:51.190","Text":"and it\u0027s new Scanner,"},{"Start":"06:51.190 ","End":"06:57.520","Text":"but I need to pass to it a path which is stored in infile here,"},{"Start":"06:57.520 ","End":"07:00.595","Text":"and that uses the filename that has been passed in."},{"Start":"07:00.595 ","End":"07:06.020","Text":"This will open whatever file has been passed in and it\u0027s in a scanner object now,"},{"Start":"07:06.020 ","End":"07:08.986","Text":"so we can create individual tokens from it,"},{"Start":"07:08.986 ","End":"07:11.530","Text":"just like we would from a keyboard."},{"Start":"07:11.530 ","End":"07:14.595","Text":"That\u0027s Part D done."},{"Start":"07:14.595 ","End":"07:19.905","Text":"Then we\u0027ll go to create a couple of variables in Part E. First of all,"},{"Start":"07:19.905 ","End":"07:22.785","Text":"a string variable called data,"},{"Start":"07:22.785 ","End":"07:27.480","Text":"and then another one called count,"},{"Start":"07:27.480 ","End":"07:32.310","Text":"which is an int, and we\u0027ll set that to 0."},{"Start":"07:32.310 ","End":"07:36.150","Text":"That\u0027s Part E. Now we need a while loop for Part"},{"Start":"07:36.150 ","End":"07:42.240","Text":"F. What is the conditions for this while loop?"},{"Start":"07:42.240 ","End":"07:47.280","Text":"We want to terminate when there are not any more tokens to read."},{"Start":"07:47.280 ","End":"07:50.430","Text":"While there are tokens to read,"},{"Start":"07:50.430 ","End":"07:56.970","Text":"we can use in.hasNext to tell us if it has a next token."},{"Start":"07:56.970 ","End":"07:59.850","Text":"If it doesn\u0027t, it will end."},{"Start":"07:59.850 ","End":"08:01.215","Text":"That\u0027s all we need to do."},{"Start":"08:01.215 ","End":"08:04.155","Text":"If there\u0027s not any more tokens,"},{"Start":"08:04.155 ","End":"08:07.455","Text":"it will drop out to the bottom here at the end of the while loop."},{"Start":"08:07.455 ","End":"08:12.030","Text":"While there is data still to read from this file,"},{"Start":"08:12.030 ","End":"08:17.790","Text":"in F1 we need to read the next token into data using next."},{"Start":"08:17.790 ","End":"08:24.165","Text":"Data equals n.next will do the job there."},{"Start":"08:24.165 ","End":"08:26.115","Text":"That\u0027s read the next token in."},{"Start":"08:26.115 ","End":"08:28.950","Text":"The first word in the file will be read in."},{"Start":"08:28.950 ","End":"08:35.430","Text":"Then what we\u0027re going to do is make sure we force everything to lowercase."},{"Start":"08:35.430 ","End":"08:38.805","Text":"We\u0027ve done this before several times in different exercises."},{"Start":"08:38.805 ","End":"08:48.855","Text":"Let me do that by saying data equals data.toLowerCase and we store it back into data."},{"Start":"08:48.855 ","End":"08:53.985","Text":"Then we do the same again with replaceAll."},{"Start":"08:53.985 ","End":"08:57.205","Text":"Once I\u0027ve done this several times now,"},{"Start":"08:57.205 ","End":"09:01.575","Text":"we\u0027re going to pass it a regular expression to say,"},{"Start":"09:01.575 ","End":"09:10.755","Text":"everything that is not a lowercase letter should be replaced with an empty string."},{"Start":"09:10.755 ","End":"09:18.195","Text":"I just gets rid of anything in this string that\u0027s not a letter from a-z in lowercase,"},{"Start":"09:18.195 ","End":"09:22.785","Text":"and there should be only lowercase letters or all other symbols are going to disappear."},{"Start":"09:22.785 ","End":"09:26.160","Text":"We\u0027ve already extracted, remember the spaces from this,"},{"Start":"09:26.160 ","End":"09:30.389","Text":"because that\u0027s what scanner will do when it does next."},{"Start":"09:30.389 ","End":"09:34.575","Text":"Now we\u0027ve got up to Part 4."},{"Start":"09:34.575 ","End":"09:38.340","Text":"What we want to do is we want to increment count,"},{"Start":"09:38.340 ","End":"09:43.180","Text":"but only if data contains a word or it doesn\u0027t contain the empty string."},{"Start":"09:43.940 ","End":"09:46.905","Text":"Remember data is a string,"},{"Start":"09:46.905 ","End":"09:51.135","Text":"we use equals rather than the double equal sign."},{"Start":"09:51.135 ","End":"09:53.865","Text":"If data equals that,"},{"Start":"09:53.865 ","End":"09:56.040","Text":"we do one thing otherwise you do"},{"Start":"09:56.040 ","End":"09:59.190","Text":"another thing but an easier way of saying this is to say,"},{"Start":"09:59.190 ","End":"10:03.705","Text":"if data is not equal to an empty string,"},{"Start":"10:03.705 ","End":"10:08.505","Text":"then add one to the count, otherwise do nothing."},{"Start":"10:08.505 ","End":"10:13.845","Text":"It only adds one to account if there was something there rather than nothing there."},{"Start":"10:13.845 ","End":"10:16.410","Text":"For example, that first line in the read me file was"},{"Start":"10:16.410 ","End":"10:19.365","Text":"a whole lot of hyphens that will get replaced with nothing."},{"Start":"10:19.365 ","End":"10:25.410","Text":"We don\u0027t want to count that as a word so we skip it basically doing this line."},{"Start":"10:25.410 ","End":"10:31.440","Text":"That\u0027s it for the loop but what it does say is when the loop terminates,"},{"Start":"10:31.440 ","End":"10:35.355","Text":"close the file and return count."},{"Start":"10:35.355 ","End":"10:37.575","Text":"Let\u0027s do that."},{"Start":"10:37.575 ","End":"10:41.325","Text":"We say in.close because the file"},{"Start":"10:41.325 ","End":"10:46.965","Text":"is being opened by the scanner because we passed it in file here,"},{"Start":"10:46.965 ","End":"10:53.220","Text":"so it\u0027s the scanner we\u0027re closing effectively and now we\u0027ll close the file and turn."},{"Start":"10:53.220 ","End":"10:57.255","Text":"Then we return count simply"},{"Start":"10:57.255 ","End":"11:02.490","Text":"because that should contain the number of words we\u0027ve encountered."},{"Start":"11:02.490 ","End":"11:08.260","Text":"That would be it but for the fact that we need to do a try and catch block,"},{"Start":"11:08.260 ","End":"11:12.510","Text":"we need to return minus 1 in the case of an exception."},{"Start":"11:12.510 ","End":"11:19.690","Text":"Let\u0027s try block in here and then a catch block afterwards."},{"Start":"11:19.850 ","End":"11:26.070","Text":"I can catch any type of exception with an exception and"},{"Start":"11:26.070 ","End":"11:32.505","Text":"you might as well print out what the error message is if any."},{"Start":"11:32.505 ","End":"11:35.730","Text":"I\u0027m going to return minus 1 here."},{"Start":"11:35.730 ","End":"11:40.005","Text":"If we weren\u0027t able to find a file or access the file,"},{"Start":"11:40.005 ","End":"11:42.795","Text":"then we signal back to the calling code that this is not"},{"Start":"11:42.795 ","End":"11:45.690","Text":"a count because you could have a file with nothing in it,"},{"Start":"11:45.690 ","End":"11:49.335","Text":"but you can\u0027t have a file with minus 1 words in it."},{"Start":"11:49.335 ","End":"11:52.290","Text":"We\u0027re signaling back to the calling code that it didn\u0027t work."},{"Start":"11:52.290 ","End":"11:56.955","Text":"If there is a number greater than minus 1 returned,"},{"Start":"11:56.955 ","End":"11:59.190","Text":"then that\u0027s the number of words we found in the file"},{"Start":"11:59.190 ","End":"12:02.320","Text":"at zero empty or we found some words in it."},{"Start":"12:02.630 ","End":"12:08.340","Text":"I think that\u0027s us done, and it compiles."},{"Start":"12:08.340 ","End":"12:10.470","Text":"Let\u0027s try and test that out."},{"Start":"12:10.470 ","End":"12:16.800","Text":"Now, we need to create an instance and we can call"},{"Start":"12:16.800 ","End":"12:24.615","Text":"the countWords method with README.txt as the file name and Part I,"},{"Start":"12:24.615 ","End":"12:30.840","Text":"it says that should return 52 and when I press it does return 52."},{"Start":"12:30.840 ","End":"12:33.045","Text":"Thank goodness."},{"Start":"12:33.045 ","End":"12:36.750","Text":"The last little bit here is to test"},{"Start":"12:36.750 ","End":"12:40.200","Text":"with missing.txt and that file doesn\u0027t exist in this folder."},{"Start":"12:40.200 ","End":"12:44.475","Text":"You can see here there is a README file but there\u0027s no missing.txt."},{"Start":"12:44.475 ","End":"12:49.020","Text":"If we try and count words on a file that\u0027s not actually there,"},{"Start":"12:49.020 ","End":"12:51.720","Text":"we should see minus 1. Yes, we do."},{"Start":"12:51.720 ","End":"12:56.580","Text":"We also see the exceptions picked up that that file\u0027s not there."},{"Start":"12:56.580 ","End":"12:58.680","Text":"That\u0027s great. Let\u0027s try one more file."},{"Start":"12:58.680 ","End":"13:00.870","Text":"If I open the README file and"},{"Start":"13:00.870 ","End":"13:03.450","Text":"just write some words and save it with a different filename,"},{"Start":"13:03.450 ","End":"13:11.894","Text":"I can just test it out across several lines."},{"Start":"13:11.894 ","End":"13:13.995","Text":"In a blank line there,"},{"Start":"13:13.995 ","End":"13:17.639","Text":"put some random characters on some other lines,"},{"Start":"13:17.639 ","End":"13:19.620","Text":"just to see if it counts those."},{"Start":"13:19.620 ","End":"13:21.240","Text":"I would expect 1, 2,"},{"Start":"13:21.240 ","End":"13:23.475","Text":"3, 4, 5, 6, 7,"},{"Start":"13:23.475 ","End":"13:28.110","Text":"8, 9, 10 words from this file."},{"Start":"13:28.110 ","End":"13:34.980","Text":"Let\u0027s call it test.txt and see whether we get the result we expect."},{"Start":"13:34.980 ","End":"13:38.790","Text":"Great. Nice compact little program that,"},{"Start":"13:38.790 ","End":"13:42.300","Text":"and it just shows you the power of all of the library code that you get with"},{"Start":"13:42.300 ","End":"13:44.610","Text":"most programming languages that let you do things"},{"Start":"13:44.610 ","End":"13:47.430","Text":"like extract individual words or whole lines,"},{"Start":"13:47.430 ","End":"13:52.140","Text":"tell you when there are more words left or more tokens left in the stream."},{"Start":"13:52.140 ","End":"13:53.970","Text":"It\u0027s a really small amount of code,"},{"Start":"13:53.970 ","End":"13:56.760","Text":"but we\u0027ve actually done quite a lot there and lots"},{"Start":"13:56.760 ","End":"14:00.000","Text":"of useful techniques for you to use in your own programs."},{"Start":"14:00.000 ","End":"14:01.380","Text":"Thanks very much for watching,"},{"Start":"14:01.380 ","End":"14:03.700","Text":"and I\u0027ll see you in the next one."}],"ID":29807},{"Watched":false,"Name":"Exercise 5 part a","Duration":"8m 48s","ChapterTopicVideoID":28375,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.520","Text":"Hello, welcome back. In this exercise,"},{"Start":"00:02.520 ","End":"00:06.360","Text":"we\u0027re told that a common file related scenario is to open 1 file,"},{"Start":"00:06.360 ","End":"00:07.665","Text":"read data from it,"},{"Start":"00:07.665 ","End":"00:10.005","Text":"and write output to a different file."},{"Start":"00:10.005 ","End":"00:14.745","Text":"The file read from can then be deleted and the output file can replace it."},{"Start":"00:14.745 ","End":"00:18.960","Text":"In Part a, we use as ever the codepad to try out some commands,"},{"Start":"00:18.960 ","End":"00:21.960","Text":"first one being import java.io.*,"},{"Start":"00:21.960 ","End":"00:27.780","Text":"the second 1 being File file1 equals new file results.txt."},{"Start":"00:27.780 ","End":"00:29.400","Text":"Then we do a similar thing,"},{"Start":"00:29.400 ","End":"00:33.360","Text":"File file2 equals new file temp.txt,"},{"Start":"00:33.360 ","End":"00:38.085","Text":"then file1.create new file and file2.create new file."},{"Start":"00:38.085 ","End":"00:44.435","Text":"In Part 6, we say PrintWriter out equals new PrintWriter file2."},{"Start":"00:44.435 ","End":"00:51.740","Text":"Then in Part 7, out.printline \"Adele\",100, no spaces."},{"Start":"00:51.740 ","End":"00:55.145","Text":"Then we do out.close in Part 8,"},{"Start":"00:55.145 ","End":"01:03.750","Text":"and then file1.delete and file2.renameTo file1 inside brackets."},{"Start":"01:03.750 ","End":"01:07.985","Text":"In Part b, we\u0027re told to create a method called highscore,"},{"Start":"01:07.985 ","End":"01:10.265","Text":"which returns a Boolean and accepts"},{"Start":"01:10.265 ","End":"01:15.175","Text":"a string argument name followed by an int argument score."},{"Start":"01:15.175 ","End":"01:19.715","Text":"This method will add the score a name to the file highscores.txt,"},{"Start":"01:19.715 ","End":"01:22.025","Text":"as long as it\u0027s 1 of the top 10 scores,"},{"Start":"01:22.025 ","End":"01:27.215","Text":"or it will always add the score if there are less than 10 scores currently in the file."},{"Start":"01:27.215 ","End":"01:29.945","Text":"Each line of the file contains a name,"},{"Start":"01:29.945 ","End":"01:32.360","Text":"a single comma, and a score."},{"Start":"01:32.360 ","End":"01:34.355","Text":"For example, Billy,95."},{"Start":"01:34.355 ","End":"01:37.010","Text":"No spaces around the comma."},{"Start":"01:37.010 ","End":"01:40.595","Text":"In Part c, we are told to declare a number of variables,"},{"Start":"01:40.595 ","End":"01:43.235","Text":"a string called line with no initial value,"},{"Start":"01:43.235 ","End":"01:47.690","Text":"a string array called temp with no size or initial values,"},{"Start":"01:47.690 ","End":"01:51.740","Text":"A boolean called newHighScore with initial value of false."},{"Start":"01:51.740 ","End":"01:55.040","Text":"An int called count with initial value of 0,"},{"Start":"01:55.040 ","End":"01:59.500","Text":"a file path called scores to the file highscores.txt,"},{"Start":"01:59.500 ","End":"02:03.975","Text":"and a file path called newScores to the file temp.txt."},{"Start":"02:03.975 ","End":"02:05.430","Text":"In Part d,"},{"Start":"02:05.430 ","End":"02:08.540","Text":"if the file high scores.txt doesn\u0027t exist,"},{"Start":"02:08.540 ","End":"02:10.595","Text":"we create a new file with that name,"},{"Start":"02:10.595 ","End":"02:14.060","Text":"then we create a scanner object in from"},{"Start":"02:14.060 ","End":"02:20.440","Text":"the scores path and a PrintWriter object out from the new scores path."},{"Start":"02:20.440 ","End":"02:23.330","Text":"In Part p, we create a while loop which"},{"Start":"02:23.330 ","End":"02:26.690","Text":"terminates when there are no more lines to read from the input file,"},{"Start":"02:26.690 ","End":"02:30.665","Text":"highscores.txt, or when count has reached 10."},{"Start":"02:30.665 ","End":"02:37.250","Text":"Inside that loop, we read a whole line from the input file into the string line."},{"Start":"02:37.250 ","End":"02:40.610","Text":"In Part g, we split the line string on"},{"Start":"02:40.610 ","End":"02:45.170","Text":"the comma character and store the result into the temp array."},{"Start":"02:45.170 ","End":"02:48.740","Text":"In Part h, if highscore is still false and"},{"Start":"02:48.740 ","End":"02:53.150","Text":"score is greater than the value in temp 1, we do the following."},{"Start":"02:53.150 ","End":"02:56.270","Text":"Firstly, we output name followed by a comma"},{"Start":"02:56.270 ","End":"03:00.140","Text":"and then score as a line in the temp txt file,"},{"Start":"03:00.140 ","End":"03:02.660","Text":"then we set new highscore to true,"},{"Start":"03:02.660 ","End":"03:04.550","Text":"then we increment count,"},{"Start":"03:04.550 ","End":"03:07.130","Text":"and if count is still less than 10,"},{"Start":"03:07.130 ","End":"03:14.290","Text":"we output the contents of line to temp.txt and increment count again."},{"Start":"03:14.290 ","End":"03:20.180","Text":"Otherwise, i.e if highscore was not false or score was not greater than temp 1,"},{"Start":"03:20.180 ","End":"03:26.250","Text":"we output the contents of line to temp.txt and increment count."},{"Start":"03:26.250 ","End":"03:29.015","Text":"Then in Part j outside the while loop,"},{"Start":"03:29.015 ","End":"03:36.140","Text":"we check if count is 0 or both newHighScore is false and count is less than 10."},{"Start":"03:36.140 ","End":"03:41.630","Text":"If so, we output name followed by a comma and then score,"},{"Start":"03:41.630 ","End":"03:43.925","Text":"as a line in the temp.txt file,"},{"Start":"03:43.925 ","End":"03:47.615","Text":"and then we set newHighScore to true."},{"Start":"03:47.615 ","End":"03:51.170","Text":"In Part k, we close the scanner and PrintWriter objects,"},{"Start":"03:51.170 ","End":"03:52.610","Text":"and hence the files,"},{"Start":"03:52.610 ","End":"03:58.660","Text":"Part l, we rename the temp.txt file to highscores.txt."},{"Start":"03:58.660 ","End":"04:00.060","Text":"In Part l3,"},{"Start":"04:00.060 ","End":"04:02.745","Text":"we return the value true."},{"Start":"04:02.745 ","End":"04:04.915","Text":"In Part m, otherwise,"},{"Start":"04:04.915 ","End":"04:08.345","Text":"if this part didn\u0027t run, we return false."},{"Start":"04:08.345 ","End":"04:11.135","Text":"We finally test the program in Part n,"},{"Start":"04:11.135 ","End":"04:14.135","Text":"adding a score of 10 and the name of tensing"},{"Start":"04:14.135 ","End":"04:18.710","Text":"should add the score to the newly created file highscores.txt,"},{"Start":"04:18.710 ","End":"04:21.830","Text":"adding a score of 90 and the name of Topi,"},{"Start":"04:21.830 ","End":"04:26.275","Text":"should add this score above the previous 1 in highscores.txt."},{"Start":"04:26.275 ","End":"04:29.810","Text":"Part 3, adding a score of 40 and the name of Joe,"},{"Start":"04:29.810 ","End":"04:33.590","Text":"should appear in the middle of the previous 2 in highscores.txt."},{"Start":"04:33.590 ","End":"04:39.695","Text":"In Part 4, we add more scores of below 10 until there are 10 names in the file."},{"Start":"04:39.695 ","End":"04:44.770","Text":"Part 5, we add another score which is below the lowest current score in the file,"},{"Start":"04:44.770 ","End":"04:48.890","Text":"and we check that false is returned and the file doesn\u0027t change."},{"Start":"04:48.890 ","End":"04:51.500","Text":"Then finally, in Part 6,"},{"Start":"04:51.500 ","End":"04:53.900","Text":"we add another score which is above 10,"},{"Start":"04:53.900 ","End":"04:57.990","Text":"and check that the bottom score drops out of the file."},{"Start":"04:58.000 ","End":"05:02.915","Text":"Let\u0027s start in the codepad as we\u0027ve been asked to do in Part a,"},{"Start":"05:02.915 ","End":"05:10.040","Text":"and try out some of the commands that we\u0027re going to need to use in this exercise."},{"Start":"05:10.040 ","End":"05:13.960","Text":"First, we\u0027re going to create some file paths,"},{"Start":"05:13.960 ","End":"05:19.685","Text":"file1 points to something called results.txt,"},{"Start":"05:19.685 ","End":"05:21.305","Text":"and we should see,"},{"Start":"05:21.305 ","End":"05:24.440","Text":"I\u0027ve got a window open there on the right-hand side that"},{"Start":"05:24.440 ","End":"05:27.960","Text":"shows you the output of these commands,"},{"Start":"05:27.960 ","End":"05:31.565","Text":"so we should see files being created and so on."},{"Start":"05:31.565 ","End":"05:37.075","Text":"That\u0027s file1 and file2 is called temp.txt."},{"Start":"05:37.075 ","End":"05:38.670","Text":"We haven\u0027t created any files yet,"},{"Start":"05:38.670 ","End":"05:43.285","Text":"all we\u0027ve got is a path to actually create 1 as we do in Part 4,"},{"Start":"05:43.285 ","End":"05:48.800","Text":"we use the path file1 in this case,"},{"Start":"05:48.800 ","End":"05:50.930","Text":"and we say create new file,"},{"Start":"05:50.930 ","End":"05:54.560","Text":"so we would expect in this case a file called"},{"Start":"05:54.560 ","End":"06:00.680","Text":"results.txt to be created because that\u0027s the path we created up here."},{"Start":"06:00.680 ","End":"06:03.440","Text":"Let\u0027s see if that does it, and there we go."},{"Start":"06:03.440 ","End":"06:06.935","Text":"We\u0027ve got a file called results.txt of size 0."},{"Start":"06:06.935 ","End":"06:09.590","Text":"There\u0027s nothing in it, but it has created the file,"},{"Start":"06:09.590 ","End":"06:12.395","Text":"we could do the same thing for file2,"},{"Start":"06:12.395 ","End":"06:15.290","Text":"and the other file can get created too."},{"Start":"06:15.290 ","End":"06:16.940","Text":"Nothing much new there,"},{"Start":"06:16.940 ","End":"06:20.690","Text":"but what we\u0027re going to do now is in Part 6,"},{"Start":"06:20.690 ","End":"06:24.320","Text":"we\u0027re going to create a PrintWriter object."},{"Start":"06:24.320 ","End":"06:27.920","Text":"This basically works like printline,"},{"Start":"06:27.920 ","End":"06:31.190","Text":"so instead of outputting to the screen,"},{"Start":"06:31.190 ","End":"06:33.425","Text":"we output to a file."},{"Start":"06:33.425 ","End":"06:36.630","Text":"This is the syntax for it,"},{"Start":"06:36.650 ","End":"06:40.790","Text":"and pass it path,"},{"Start":"06:40.790 ","End":"06:43.715","Text":"which is the file that we want to do the writing to."},{"Start":"06:43.715 ","End":"06:47.075","Text":"In this case, we are going to be writing to the temp.txt file,"},{"Start":"06:47.075 ","End":"06:52.105","Text":"and it\u0027s complained there because I\u0027ve missed a capital W here."},{"Start":"06:52.105 ","End":"06:54.885","Text":"PrintWriter with a capital W,"},{"Start":"06:54.885 ","End":"06:56.925","Text":"so that\u0027s done it now."},{"Start":"06:56.925 ","End":"07:00.014","Text":"What we should see is that this file,"},{"Start":"07:00.014 ","End":"07:02.780","Text":"file2 should have some content in it,"},{"Start":"07:02.780 ","End":"07:05.660","Text":"but actually we have to write it in there first of all,"},{"Start":"07:05.660 ","End":"07:09.050","Text":"so Part 7 is to actually do the writing,"},{"Start":"07:09.050 ","End":"07:12.305","Text":"so we\u0027ve called our object out,"},{"Start":"07:12.305 ","End":"07:15.510","Text":"and so when we do out printline,"},{"Start":"07:15.510 ","End":"07:19.115","Text":"and we write whatever we want to write in there."},{"Start":"07:19.115 ","End":"07:21.890","Text":"It should be output into the file."},{"Start":"07:21.890 ","End":"07:27.185","Text":"You still don\u0027t see anything happen here until you close the file."},{"Start":"07:27.185 ","End":"07:33.180","Text":"Let\u0027s do that and see if it does the job, and there you go,"},{"Start":"07:33.180 ","End":"07:35.570","Text":"you now see that it\u0027s 1 kilobyte in size,"},{"Start":"07:35.570 ","End":"07:37.430","Text":"it won\u0027t actually be 1 kilobyte in size,"},{"Start":"07:37.430 ","End":"07:39.200","Text":"but it\u0027s the minimum size it reports,"},{"Start":"07:39.200 ","End":"07:42.530","Text":"and you\u0027ll see we\u0027ve actually written that line to the file."},{"Start":"07:42.530 ","End":"07:45.560","Text":"The last couple of commands at 9 and 10 then,"},{"Start":"07:45.560 ","End":"07:49.670","Text":"will cause us to delete a file because it\u0027s"},{"Start":"07:49.670 ","End":"07:54.745","Text":"file 1 we\u0027re saying to delete the file that it\u0027s going to delete it\u0027s results.txt,"},{"Start":"07:54.745 ","End":"07:57.470","Text":"it didn\u0027t have anything in it anyway, so no loss."},{"Start":"07:57.470 ","End":"08:02.070","Text":"But that\u0027s just to show you that it can work, the files disappeared."},{"Start":"08:02.070 ","End":"08:06.285","Text":"Then if we do that final command file2,"},{"Start":"08:06.285 ","End":"08:09.010","Text":"and we say renameTo,"},{"Start":"08:09.010 ","End":"08:11.405","Text":"then supply it with a path,"},{"Start":"08:11.405 ","End":"08:14.510","Text":"which is the name that we\u0027re going to rename it to,"},{"Start":"08:14.510 ","End":"08:17.180","Text":"because we\u0027re now giving it a path of file1,"},{"Start":"08:17.180 ","End":"08:20.305","Text":"what it will do is file2 is temp.txt,"},{"Start":"08:20.305 ","End":"08:22.640","Text":"but it\u0027s going to rename it to a different path,"},{"Start":"08:22.640 ","End":"08:25.970","Text":"which is this one, file1 which is results.txt."},{"Start":"08:25.970 ","End":"08:29.210","Text":"This file temp.txt which does have some content in it,"},{"Start":"08:29.210 ","End":"08:32.880","Text":"should now be renamed to results.txt."},{"Start":"08:32.880 ","End":"08:34.050","Text":"Let\u0027s see if that happens,"},{"Start":"08:34.050 ","End":"08:35.760","Text":"and yes, it does seem to happen,"},{"Start":"08:35.760 ","End":"08:39.860","Text":"just to prove it had the content that we expected in, it\u0027s there."},{"Start":"08:39.860 ","End":"08:42.680","Text":"That\u0027s it for Part a,"},{"Start":"08:42.680 ","End":"08:44.270","Text":"the next bit is quite long,"},{"Start":"08:44.270 ","End":"08:45.905","Text":"so I\u0027ll do that in a separate video."},{"Start":"08:45.905 ","End":"08:48.840","Text":"I\u0027ll see you in a moment for that one."}],"ID":29808},{"Watched":false,"Name":"Exercise 5 part b","Duration":"16m 59s","ChapterTopicVideoID":28378,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.105","Text":"Let\u0027s get started on part B,"},{"Start":"00:03.105 ","End":"00:06.490","Text":"where we create a method called highScore."},{"Start":"00:06.490 ","End":"00:12.075","Text":"It returns the Boolean and accepts two arguments."},{"Start":"00:12.075 ","End":"00:17.330","Text":"The first one is a string called name,"},{"Start":"00:17.330 ","End":"00:22.260","Text":"the second one is an int called score."},{"Start":"00:22.270 ","End":"00:27.935","Text":"Purpose of this method is to write the score to a file and the name with it."},{"Start":"00:27.935 ","End":"00:31.451","Text":"As long as it\u0027s one of the top 10 scores in a file."},{"Start":"00:31.451 ","End":"00:32.960","Text":"If the file doesn\u0027t exist,"},{"Start":"00:32.960 ","End":"00:35.250","Text":"we have to create it."},{"Start":"00:35.250 ","End":"00:39.695","Text":"In part C, we\u0027re going to declare our bunch of variables."},{"Start":"00:39.695 ","End":"00:45.260","Text":"I\u0027m assuming that because this is a file based operation,"},{"Start":"00:45.260 ","End":"00:47.995","Text":"we\u0027re going to need try and catch blocks."},{"Start":"00:47.995 ","End":"00:51.005","Text":"Let me put those in first."},{"Start":"00:51.005 ","End":"00:58.600","Text":"I\u0027m just going to catch all exceptions in a generic Exception block."},{"Start":"00:58.600 ","End":"01:02.340","Text":"If it fails, I\u0027m going to return false"},{"Start":"01:02.340 ","End":"01:11.872","Text":"and I\u0027ll obviously print out what the failure was."},{"Start":"01:11.872 ","End":"01:16.255","Text":"These have basic blocks created."},{"Start":"01:16.255 ","End":"01:19.495","Text":"I haven\u0027t returned the value in all cases so it\u0027s"},{"Start":"01:19.495 ","End":"01:23.020","Text":"presumably complaining about that missing return statements,"},{"Start":"01:23.020 ","End":"01:24.910","Text":"so that\u0027s fine but I can go ahead and create"},{"Start":"01:24.910 ","End":"01:28.135","Text":"my variables as I\u0027ve been asked to in part C now."},{"Start":"01:28.135 ","End":"01:31.660","Text":"We got a string called line,"},{"Start":"01:31.660 ","End":"01:33.360","Text":"no initial value,"},{"Start":"01:33.360 ","End":"01:36.190","Text":"a string array called temp."},{"Start":"01:36.190 ","End":"01:41.165","Text":"A string and a pair of square brackets called temp,"},{"Start":"01:41.165 ","End":"01:45.355","Text":"and a Boolean called newHighScore,"},{"Start":"01:45.355 ","End":"01:50.210","Text":"which we initialize to false."},{"Start":"01:50.210 ","End":"01:52.294","Text":"An int code counts,"},{"Start":"01:52.294 ","End":"01:54.920","Text":"which is initialized to 0."},{"Start":"01:54.920 ","End":"01:59.030","Text":"Then we got some files paths here."},{"Start":"01:59.030 ","End":"02:08.595","Text":"So File scores equals new File path."},{"Start":"02:08.595 ","End":"02:14.833","Text":"The file itself is going to be called highscores.txt."},{"Start":"02:14.833 ","End":"02:20.123","Text":"That\u0027s constructed high path called scores."},{"Start":"02:20.123 ","End":"02:22.810","Text":"There\u0027s going to be another one called"},{"Start":"02:25.130 ","End":"02:29.955","Text":"temp then we call it newScores, that\u0027s the path name."},{"Start":"02:29.955 ","End":"02:36.125","Text":"But the file name is temp.txt."},{"Start":"02:36.125 ","End":"02:40.100","Text":"Easy to get mixed up between a filename and a path."},{"Start":"02:40.100 ","End":"02:43.055","Text":"Remember, a path includes a lot of other information,"},{"Start":"02:43.055 ","End":"02:46.070","Text":"including potentially the drive and everything else."},{"Start":"02:46.070 ","End":"02:52.655","Text":"This abstract thing here of a file is incorporating all of that information for us."},{"Start":"02:52.655 ","End":"02:54.380","Text":"In this case, we\u0027re doing it very simply."},{"Start":"02:54.380 ","End":"02:56.840","Text":"It\u0027s in the local folder."},{"Start":"02:56.840 ","End":"02:59.196","Text":"So those are our file paths."},{"Start":"02:59.196 ","End":"03:00.415","Text":"Then part 7,"},{"Start":"03:00.415 ","End":"03:07.803","Text":"we need to create a scanner object called in."},{"Start":"03:07.803 ","End":"03:10.701","Text":"Let\u0027s call it in because we\u0027re getting data in from a file."},{"Start":"03:10.701 ","End":"03:13.460","Text":"That means we\u0027re going to create a new scanner object."},{"Start":"03:13.460 ","End":"03:17.990","Text":"To it we pass a path of the file that we\u0027re interested in."},{"Start":"03:17.990 ","End":"03:22.245","Text":"The file we\u0027re interested in is scores,"},{"Start":"03:22.245 ","End":"03:25.895","Text":"which will point is too highscores.txt."},{"Start":"03:25.895 ","End":"03:29.480","Text":"Then we need to create a PrintWriter object."},{"Start":"03:29.480 ","End":"03:32.500","Text":"We did one of those in the code pad."},{"Start":"03:32.500 ","End":"03:35.680","Text":"We\u0027re going to call this out,"},{"Start":"03:36.480 ","End":"03:39.860","Text":"so with the syntax."},{"Start":"03:40.850 ","End":"03:44.758","Text":"We also have to pass this a path."},{"Start":"03:44.758 ","End":"03:46.643","Text":"This one\u0027s going to be called newScores."},{"Start":"03:46.643 ","End":"03:49.612","Text":"This is where we write high scores."},{"Start":"03:49.612 ","End":"03:52.720","Text":"We\u0027re reading existing ones from here and then we\u0027re going to write"},{"Start":"03:52.720 ","End":"03:57.275","Text":"the new ones and the existing ones in this file here."},{"Start":"03:57.275 ","End":"04:00.075","Text":"That\u0027s part C done."},{"Start":"04:00.075 ","End":"04:01.970","Text":"In part D,"},{"Start":"04:01.970 ","End":"04:06.580","Text":"we\u0027ve got to create a file if it doesn\u0027t exist."},{"Start":"04:06.580 ","End":"04:09.370","Text":"How would I know that?"},{"Start":"04:09.370 ","End":"04:12.850","Text":"Highscores.txt, we have the path in scores,"},{"Start":"04:12.850 ","End":"04:16.250","Text":"so we say scores.exists."},{"Start":"04:16.700 ","End":"04:19.150","Text":"Obviously we want the opposite of that."},{"Start":"04:19.150 ","End":"04:20.970","Text":"We want to know if it doesn\u0027t exist,"},{"Start":"04:20.970 ","End":"04:23.755","Text":"so I could say scores.exists equals false,"},{"Start":"04:23.755 ","End":"04:26.905","Text":"or I could just say not scores exists."},{"Start":"04:26.905 ","End":"04:28.915","Text":"If it doesn\u0027t exist,"},{"Start":"04:28.915 ","End":"04:36.840","Text":"we want to create it and we create a file by giving a path and saying createNewFile."},{"Start":"04:40.070 ","End":"04:43.580","Text":"Hopefully those lines are good and we still just got one error"},{"Start":"04:43.580 ","End":"04:46.715","Text":"which is about a return value, so that\u0027s fine."},{"Start":"04:46.715 ","End":"04:50.555","Text":"We\u0027ve now done part D. Now,"},{"Start":"04:50.555 ","End":"04:57.202","Text":"we need to create a while loop and do a whole bunch of things inside the while loop."},{"Start":"04:57.202 ","End":"05:03.260","Text":"We\u0027re starting on the while loop\u0027s opening line part E. What is"},{"Start":"05:03.260 ","End":"05:07.010","Text":"the clause here that we\u0027ve got to put"},{"Start":"05:07.010 ","End":"05:11.240","Text":"inside our brackets that decides when the loop should terminate."},{"Start":"05:11.240 ","End":"05:14.750","Text":"It says there are no more lines to be read from"},{"Start":"05:14.750 ","End":"05:18.605","Text":"highscores.txt or in count has reached 10."},{"Start":"05:18.605 ","End":"05:21.920","Text":"Count must be less than 10."},{"Start":"05:21.920 ","End":"05:25.130","Text":"Now it\u0027s easy to fall in the trap of saying all here,"},{"Start":"05:25.130 ","End":"05:29.460","Text":"but remember this is when it terminates the loop so it\u0027s and."},{"Start":"05:29.460 ","End":"05:32.045","Text":"These things both have to be true."},{"Start":"05:32.045 ","End":"05:34.235","Text":"If one or the other is not true,"},{"Start":"05:34.235 ","End":"05:36.245","Text":"then the loop will terminate."},{"Start":"05:36.245 ","End":"05:41.570","Text":"Count is less than 10 and the other part is,"},{"Start":"05:41.570 ","End":"05:43.490","Text":"do we still have lines to read?"},{"Start":"05:43.490 ","End":"05:50.880","Text":"We know that that can be got from the hasNext method inside a scanner,"},{"Start":"05:50.880 ","End":"05:52.580","Text":"so that should do the trick."},{"Start":"05:52.580 ","End":"05:55.400","Text":"While we have less than 10 lines in"},{"Start":"05:55.400 ","End":"06:00.200","Text":"the file and there are still lines to read from the file,"},{"Start":"06:00.200 ","End":"06:02.480","Text":"then we do what\u0027s in this while loop."},{"Start":"06:02.480 ","End":"06:05.120","Text":"The next part, part F inside the loop,"},{"Start":"06:05.120 ","End":"06:09.845","Text":"we read a whole line from the input file into the string line."},{"Start":"06:09.845 ","End":"06:12.350","Text":"Line is going to receive a value."},{"Start":"06:12.350 ","End":"06:13.955","Text":"How do we get that?"},{"Start":"06:13.955 ","End":"06:15.950","Text":"Well, we\u0027ve done this many times before,"},{"Start":"06:15.950 ","End":"06:18.410","Text":"so we say where it\u0027s coming from,"},{"Start":"06:18.410 ","End":"06:20.390","Text":"in in this case,"},{"Start":"06:20.390 ","End":"06:25.280","Text":"and then we get a whole line by saying next line."},{"Start":"06:25.280 ","End":"06:27.035","Text":"If we just did next on its own,"},{"Start":"06:27.035 ","End":"06:29.120","Text":"it would get a single token."},{"Start":"06:29.120 ","End":"06:31.460","Text":"In this case, we want to get a whole line,"},{"Start":"06:31.460 ","End":"06:37.295","Text":"so we now have an entire line inside the string variable line."},{"Start":"06:37.295 ","End":"06:41.090","Text":"That is part F done."},{"Start":"06:41.090 ","End":"06:44.060","Text":"In part G, we want to split the line on"},{"Start":"06:44.060 ","End":"06:51.035","Text":"the comma character because the line will be of the format someone\u0027s name,"},{"Start":"06:51.035 ","End":"06:54.080","Text":"for example, and then a comma and then a number."},{"Start":"06:54.080 ","End":"06:55.690","Text":"This we call a delimiter,"},{"Start":"06:55.690 ","End":"06:59.225","Text":"so we know that the name is the first part and the delimiter,"},{"Start":"06:59.225 ","End":"07:01.846","Text":"and then the score is the next part."},{"Start":"07:01.846 ","End":"07:03.572","Text":"We need to split that out."},{"Start":"07:03.572 ","End":"07:10.355","Text":"It\u0027s already nice string manipulation function called split."},{"Start":"07:10.355 ","End":"07:15.350","Text":"What we\u0027re going to do is we\u0027ve already got an array called temp that we created,"},{"Start":"07:15.350 ","End":"07:17.518","Text":"which is an array of strings,"},{"Start":"07:17.518 ","End":"07:19.325","Text":"and that\u0027s exactly what we need."},{"Start":"07:19.325 ","End":"07:23.555","Text":"We need to take line and we need to split it,"},{"Start":"07:23.555 ","End":"07:27.450","Text":"but the thing we\u0027re going to split it on has to be supplied,"},{"Start":"07:27.450 ","End":"07:29.465","Text":"and it\u0027s the comma that we\u0027re going to split on."},{"Start":"07:29.465 ","End":"07:31.385","Text":"We now have an array temp."},{"Start":"07:31.385 ","End":"07:34.130","Text":"It should have two elements in it."},{"Start":"07:34.130 ","End":"07:38.285","Text":"The first element will contain the thing up to the comma,"},{"Start":"07:38.285 ","End":"07:39.913","Text":"so the person\u0027s name,"},{"Start":"07:39.913 ","End":"07:43.430","Text":"and then the second element will contain the thing after the comma,"},{"Start":"07:43.430 ","End":"07:45.845","Text":"which will be the score."},{"Start":"07:45.845 ","End":"07:48.960","Text":"That\u0027s part G done."},{"Start":"07:48.960 ","End":"07:54.035","Text":"Now we are on part H. Has an if statement involved,"},{"Start":"07:54.035 ","End":"07:58.600","Text":"so if highScore is false,"},{"Start":"08:00.050 ","End":"08:05.550","Text":"I want to say not highScores or I could say highScore equals equals false."},{"Start":"08:05.550 ","End":"08:08.700","Text":"While that isn\u0027t a highScore already,"},{"Start":"08:08.700 ","End":"08:13.484","Text":"and score, sorry, I need a score there."},{"Start":"08:13.484 ","End":"08:15.740","Text":"Both of these things need to be true."},{"Start":"08:15.740 ","End":"08:24.188","Text":"If we haven\u0027t already had a highSchool and score is greater than the value in temp 1,"},{"Start":"08:24.188 ","End":"08:30.285","Text":"now I can\u0027t just do that because"},{"Start":"08:30.285 ","End":"08:37.865","Text":"the thing in temp 1 is a string and I can\u0027t compare a integer to a string."},{"Start":"08:37.865 ","End":"08:38.930","Text":"I have to convert"},{"Start":"08:38.930 ","End":"08:46.550","Text":"this string value that\u0027s stored in temp 1 into an integer and I know how to do that."},{"Start":"08:46.550 ","End":"08:56.837","Text":"I use integer.parseInt and I pass it a string I\u0027m interested in."},{"Start":"08:56.837 ","End":"09:02.330","Text":"It should then treat that as an integer from there onwards."},{"Start":"09:02.330 ","End":"09:05.930","Text":"That looks like it should do the trick."},{"Start":"09:05.930 ","End":"09:07.940","Text":"Why it\u0027s complaining there?"},{"Start":"09:07.940 ","End":"09:17.707","Text":"Yeah. It\u0027s called newHighScore, not just highScore."},{"Start":"09:17.707 ","End":"09:20.170","Text":"It\u0027s the Boolean we\u0027ve set here,"},{"Start":"09:20.170 ","End":"09:21.910","Text":"and by default, it\u0027s false."},{"Start":"09:21.910 ","End":"09:23.590","Text":"We don\u0027t have a new high score until we"},{"Start":"09:23.590 ","End":"09:25.750","Text":"find one and that\u0027s what this block here is doing."},{"Start":"09:25.750 ","End":"09:28.300","Text":"When we don\u0027t already have a high score,"},{"Start":"09:28.300 ","End":"09:33.640","Text":"because we don\u0027t have to add it twice in the file if it\u0027s higher than 2 scores."},{"Start":"09:33.640 ","End":"09:37.510","Text":"It\u0027s higher because score is greater than the value."},{"Start":"09:37.510 ","End":"09:43.105","Text":"If it is we can go on and do h part 1."},{"Start":"09:43.105 ","End":"09:49.810","Text":"We output name followed by a comma and then score as a line in temp.txt."},{"Start":"09:49.810 ","End":"09:55.855","Text":"We want to do out.printLn and we\u0027ll do"},{"Start":"09:55.855 ","End":"10:04.345","Text":"name plus a comma plus score."},{"Start":"10:04.345 ","End":"10:07.945","Text":"Now, we have the reverse problem of what we did the line above."},{"Start":"10:07.945 ","End":"10:14.990","Text":"We want to convert score into a string,"},{"Start":"10:15.120 ","End":"10:24.085","Text":"so we can say Integer.toString(Score)."},{"Start":"10:24.085 ","End":"10:31.240","Text":"That should convert this to a string because it\u0027s an integer,"},{"Start":"10:31.240 ","End":"10:33.670","Text":"a moment and we want to convert it into a string,"},{"Start":"10:33.670 ","End":"10:38.380","Text":"and so that will all go out on 1 line in the output file."},{"Start":"10:38.380 ","End":"10:43.160","Text":"Then we set newHighScore to true."},{"Start":"10:46.200 ","End":"10:51.100","Text":"Then finally, we increment count."},{"Start":"10:51.100 ","End":"10:55.885","Text":"But we are asked to do 1 more thing inside this if block."},{"Start":"10:55.885 ","End":"11:01.070","Text":"We are asked to check if count is still less than 10."},{"Start":"11:02.640 ","End":"11:08.860","Text":"We output the contents of line to temp.txt and increment count again."},{"Start":"11:08.860 ","End":"11:16.796","Text":"We say out.println(line) and"},{"Start":"11:16.796 ","End":"11:20.170","Text":"then we increment count again."},{"Start":"11:20.170 ","End":"11:25.510","Text":"What\u0027s happening there is we found a new high score but so long as"},{"Start":"11:25.510 ","End":"11:32.170","Text":"the score that we\u0027ve found that this is higher than is not the tenth score in the table,"},{"Start":"11:32.170 ","End":"11:33.670","Text":"we want to keep this line."},{"Start":"11:33.670 ","End":"11:36.100","Text":"We don\u0027t want to discard it completely. We do want to keep it."},{"Start":"11:36.100 ","End":"11:39.475","Text":"We just want to put a new high score above it and then put"},{"Start":"11:39.475 ","End":"11:43.900","Text":"the old score that this was higher than below it."},{"Start":"11:43.900 ","End":"11:47.875","Text":"If we don\u0027t do this, it will skip the score. We don\u0027t want to skip it."},{"Start":"11:47.875 ","End":"11:52.570","Text":"We just want it to move down by 1 unless it happens to be the tenth 1,"},{"Start":"11:52.570 ","End":"11:55.330","Text":"in which case it will get skipped."},{"Start":"11:55.330 ","End":"11:59.365","Text":"That spot h done."},{"Start":"11:59.365 ","End":"12:04.825","Text":"Then in part j, it says otherwise."},{"Start":"12:04.825 ","End":"12:09.250","Text":"High scores, not false,"},{"Start":"12:09.250 ","End":"12:12.970","Text":"or it wasn\u0027t a bigger number."},{"Start":"12:12.970 ","End":"12:18.925","Text":"What we still have to do is output the line as before."},{"Start":"12:18.925 ","End":"12:23.930","Text":"We\u0027re going to do exactly the same as what we\u0027ve got up here."},{"Start":"12:24.030 ","End":"12:28.420","Text":"We might as well copy that in here."},{"Start":"12:28.420 ","End":"12:35.290","Text":"It has to happen in 2 places for the reason I just gave a moment ago."},{"Start":"12:35.290 ","End":"12:41.215","Text":"We don\u0027t replace this score just because we\u0027ve got a new high score and we don\u0027t replace"},{"Start":"12:41.215 ","End":"12:48.565","Text":"this score even if there is another high score so long as it\u0027s less than 10 in the file."},{"Start":"12:48.565 ","End":"12:55.315","Text":"We\u0027re copying the lines from 1 file into another and we\u0027re inserting a new line."},{"Start":"12:55.315 ","End":"12:57.040","Text":"If it\u0027s a high score,"},{"Start":"12:57.040 ","End":"13:00.250","Text":"we\u0027re not actually getting rid of anything."},{"Start":"13:00.250 ","End":"13:02.845","Text":"That\u0027s why we keep it here."},{"Start":"13:02.845 ","End":"13:04.720","Text":"We\u0027re nearly done now."},{"Start":"13:04.720 ","End":"13:09.280","Text":"Where we are is part j."},{"Start":"13:09.280 ","End":"13:11.155","Text":"Outside the while loop,"},{"Start":"13:11.155 ","End":"13:14.320","Text":"we check if count is 0."},{"Start":"13:14.320 ","End":"13:18.654","Text":"This is the outside of the loop."},{"Start":"13:18.654 ","End":"13:25.150","Text":"If count is 0 or both,"},{"Start":"13:25.150 ","End":"13:28.270","Text":"new high score is false and count is less than 10."},{"Start":"13:28.270 ","End":"13:30.085","Text":"We need some brackets here."},{"Start":"13:30.085 ","End":"13:33.100","Text":"Not new high score."},{"Start":"13:33.100 ","End":"13:36.234","Text":"I could\u0027ve put new high score equals false."},{"Start":"13:36.234 ","End":"13:43.105","Text":"I want to know that both of these 2 things are true and count is less than 10."},{"Start":"13:43.105 ","End":"13:47.140","Text":"If it\u0027s count of 0,"},{"Start":"13:47.140 ","End":"13:49.960","Text":"the only way it can be a count of 0 is if we had an empty file."},{"Start":"13:49.960 ","End":"13:51.430","Text":"If it\u0027s an empty file,"},{"Start":"13:51.430 ","End":"13:56.455","Text":"we write the scoring or if it wasn\u0027t a high score,"},{"Start":"13:56.455 ","End":"13:59.215","Text":"but there were less than 10 scores in the file,"},{"Start":"13:59.215 ","End":"14:03.865","Text":"then we also have got to add something in."},{"Start":"14:03.865 ","End":"14:09.340","Text":"What we\u0027re going to do is output name followed by a comma,"},{"Start":"14:09.340 ","End":"14:11.350","Text":"followed by a score,"},{"Start":"14:11.350 ","End":"14:14.035","Text":"and set new high score to true."},{"Start":"14:14.035 ","End":"14:16.570","Text":"We\u0027re going to need these 2 lines here,"},{"Start":"14:16.570 ","End":"14:20.005","Text":"so we might as well copy them and the new high score"},{"Start":"14:20.005 ","End":"14:23.770","Text":"equals true or false a little bit code later on to run."},{"Start":"14:23.770 ","End":"14:26.530","Text":"That\u0027s why that\u0027s there."},{"Start":"14:26.530 ","End":"14:28.990","Text":"Nearly done now. Part k,"},{"Start":"14:28.990 ","End":"14:31.765","Text":"we are going to close."},{"Start":"14:31.765 ","End":"14:34.495","Text":"This is outside the loop."},{"Start":"14:34.495 ","End":"14:38.440","Text":"We\u0027re going to close the scanner and printwriter object."},{"Start":"14:38.440 ","End":"14:46.345","Text":"In was one of them, the scanner object and out was the other one."},{"Start":"14:46.345 ","End":"14:50.330","Text":"Actually no, that was the printwriter, that was the scanner."},{"Start":"14:50.490 ","End":"14:57.295","Text":"There we go with those lines and what\u0027s left to do,"},{"Start":"14:57.295 ","End":"14:59.815","Text":"if there\u0027s been a new high score set,"},{"Start":"14:59.815 ","End":"15:02.440","Text":"we delete the high scores file,"},{"Start":"15:02.440 ","End":"15:06.145","Text":"rename the txt file to high score txts and return the value true."},{"Start":"15:06.145 ","End":"15:09.280","Text":"Otherwise we return the value false."},{"Start":"15:09.280 ","End":"15:13.885","Text":"If new high score,"},{"Start":"15:13.885 ","End":"15:18.230","Text":"we do one thing, otherwise we do another thing."},{"Start":"15:19.140 ","End":"15:24.580","Text":"The other thing is to return false."},{"Start":"15:24.580 ","End":"15:26.965","Text":"Here we\u0027ll return true,"},{"Start":"15:26.965 ","End":"15:33.010","Text":"but we need to do the file deletion and renaming first."},{"Start":"15:33.010 ","End":"15:36.430","Text":"To delete the highscores.txt file,"},{"Start":"15:36.430 ","End":"15:46.100","Text":"we\u0027re going to say inputfile.delete,"},{"Start":"15:46.350 ","End":"15:48.820","Text":"and then we\u0027re going to say"},{"Start":"15:48.820 ","End":"15:51.170","Text":"the output file"},{"Start":"15:55.920 ","End":"16:00.070","Text":"gets renamed to,"},{"Start":"16:00.070 ","End":"16:07.375","Text":"and then what was the path for the input file?"},{"Start":"16:07.375 ","End":"16:10.190","Text":"It was scores."},{"Start":"16:10.560 ","End":"16:13.102","Text":"I think actually I cant do,"},{"Start":"16:13.102 ","End":"16:14.815","Text":"end up delete here."},{"Start":"16:14.815 ","End":"16:18.025","Text":"I need to do scores.delete."},{"Start":"16:18.025 ","End":"16:21.190","Text":"These 2 objects are obviously for the scanner and the printwriter."},{"Start":"16:21.190 ","End":"16:23.215","Text":"They\u0027re not the paths that I\u0027m interested in."},{"Start":"16:23.215 ","End":"16:26.110","Text":"So I want to delete a thing at that path, scores,"},{"Start":"16:26.110 ","End":"16:29.035","Text":"which is highscores.text,"},{"Start":"16:29.035 ","End":"16:31.900","Text":"and I want to rename new scores."},{"Start":"16:31.900 ","End":"16:37.420","Text":"I should have new scores there to what scores path was,"},{"Start":"16:37.420 ","End":"16:38.785","Text":"then I return true."},{"Start":"16:38.785 ","End":"16:42.100","Text":"Otherwise I return false."},{"Start":"16:42.100 ","End":"16:45.640","Text":"Let\u0027s just check the last few inevitable errors"},{"Start":"16:45.640 ","End":"16:49.930","Text":"dismissing a second bracket there and that\u0027s all compiled."},{"Start":"16:49.930 ","End":"16:51.460","Text":"All that remains is to test,"},{"Start":"16:51.460 ","End":"16:52.630","Text":"so that\u0027s quite long as well,"},{"Start":"16:52.630 ","End":"16:54.610","Text":"so we\u0027ll do that in the next video."},{"Start":"16:54.610 ","End":"16:59.570","Text":"I\u0027ll see you shortly for a testing part n."}],"ID":29809},{"Watched":false,"Name":"Exercise 5 part c","Duration":"7m 31s","ChapterTopicVideoID":28381,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.110 ","End":"00:04.335","Text":"Now let\u0027s test what we\u0027ve written."},{"Start":"00:04.335 ","End":"00:08.590","Text":"The first test that is going to be N1,"},{"Start":"00:08.590 ","End":"00:15.690","Text":"with a score of 10 and a name of tensing, see what I did there?"},{"Start":"00:16.460 ","End":"00:19.170","Text":"Let\u0027s see what happens."},{"Start":"00:19.170 ","End":"00:21.480","Text":"We\u0027ve fallen the first hurdle,"},{"Start":"00:21.480 ","End":"00:24.060","Text":"we\u0027ve got highscores.txt cannot be found."},{"Start":"00:24.060 ","End":"00:26.989","Text":"That\u0027s right because there is no file yet, hasn\u0027t been created,"},{"Start":"00:26.989 ","End":"00:30.260","Text":"so something\u0027s happened in the creation of the file."},{"Start":"00:30.260 ","End":"00:37.470","Text":"Let\u0027s have a look at the stack trace here and see which line it was 68,"},{"Start":"00:37.470 ","End":"00:41.610","Text":"so it failed here on this line."},{"Start":"00:41.610 ","End":"00:45.425","Text":"Obviously the reason is I\u0027ve got this the wrong way round."},{"Start":"00:45.425 ","End":"00:53.040","Text":"These lines should happen before this scanner object is created,"},{"Start":"00:53.040 ","End":"00:56.765","Text":"because the whole point here is if the file doesn\u0027t exist,"},{"Start":"00:56.765 ","End":"00:58.355","Text":"we need to create it."},{"Start":"00:58.355 ","End":"01:02.905","Text":"Because we\u0027re then going to create a scanner object from it."},{"Start":"01:02.905 ","End":"01:05.490","Text":"We can\u0027t do it the other way round,"},{"Start":"01:05.490 ","End":"01:07.170","Text":"we must check the file exists,"},{"Start":"01:07.170 ","End":"01:09.020","Text":"if it doesn\u0027t exist, we create it,"},{"Start":"01:09.020 ","End":"01:11.060","Text":"then we can create a scanner object."},{"Start":"01:11.060 ","End":"01:14.165","Text":"Even if it\u0027s an empty file, it will still work."},{"Start":"01:14.165 ","End":"01:16.880","Text":"That\u0027s what\u0027s caused that problem and"},{"Start":"01:16.880 ","End":"01:19.760","Text":"obviously it will still compile because it compiled before,"},{"Start":"01:19.760 ","End":"01:23.264","Text":"but hopefully we\u0027ll be able to make the error message go away now."},{"Start":"01:23.264 ","End":"01:25.835","Text":"Let\u0027s clear everything on here."},{"Start":"01:25.835 ","End":"01:29.110","Text":"Let\u0027s do exactly what we did before,"},{"Start":"01:29.110 ","End":"01:30.904","Text":"and this time it\u0027s worked."},{"Start":"01:30.904 ","End":"01:33.800","Text":"I get true back and you\u0027ll also see it\u0027s created a file"},{"Start":"01:33.800 ","End":"01:37.775","Text":"here called high scores and it\u0027s got something in it, we can see the size."},{"Start":"01:37.775 ","End":"01:39.200","Text":"Let\u0027s actually have a look at in it,"},{"Start":"01:39.200 ","End":"01:46.335","Text":"and it\u0027s written our name and the score in separated with a comma, which is great."},{"Start":"01:46.335 ","End":"01:49.140","Text":"Let\u0027s then do the next test N2,"},{"Start":"01:49.140 ","End":"01:57.280","Text":"add another score in of 94, Topi."},{"Start":"01:58.100 ","End":"02:00.795","Text":"That returns true as well,"},{"Start":"02:00.795 ","End":"02:03.570","Text":"and we still got a high scores file there."},{"Start":"02:03.570 ","End":"02:07.360","Text":"You\u0027ll notice that Topi has gone above Tensing,"},{"Start":"02:07.360 ","End":"02:09.740","Text":"which is good, but we\u0027ve got lots of space here."},{"Start":"02:09.740 ","End":"02:13.595","Text":"We\u0027re in our tenth scores in this file."},{"Start":"02:13.595 ","End":"02:16.685","Text":"I think subsequent tests will make us do that."},{"Start":"02:16.685 ","End":"02:19.990","Text":"Let\u0027s put in Part 3,"},{"Start":"02:19.990 ","End":"02:24.955","Text":"which is Joe and 40."},{"Start":"02:24.955 ","End":"02:26.825","Text":"That\u0027s worked as well."},{"Start":"02:26.825 ","End":"02:30.755","Text":"I can show you. Yes, Joe is gone between the 2 that we had there."},{"Start":"02:30.755 ","End":"02:38.135","Text":"Now, it said File 4 to add more scores of below 10 until there are 10 names in the file."},{"Start":"02:38.135 ","End":"02:40.205","Text":"I\u0027m just going to do a whole load now."},{"Start":"02:40.205 ","End":"02:42.995","Text":"We\u0027ve got 3, so we\u0027re going to do 7 more."},{"Start":"02:42.995 ","End":"02:47.180","Text":"Just make up some gibberish names."},{"Start":"02:47.180 ","End":"02:49.040","Text":"Just so I can do this quickly."},{"Start":"02:49.040 ","End":"02:51.605","Text":"Score of 9, 8,"},{"Start":"02:51.605 ","End":"02:54.730","Text":"7 for the next 1,"},{"Start":"02:54.730 ","End":"02:56.824","Text":"6 for the next one."},{"Start":"02:56.824 ","End":"02:58.280","Text":"How many exactly we\u0027ve gotten there?"},{"Start":"02:58.280 ","End":"03:00.770","Text":"Let\u0027s have a quick look, got 1, 2,"},{"Start":"03:00.770 ","End":"03:02.150","Text":"3, 4, 5,"},{"Start":"03:02.150 ","End":"03:03.470","Text":"6, 7,"},{"Start":"03:03.470 ","End":"03:06.075","Text":"so I need 3 more 1,"},{"Start":"03:06.075 ","End":"03:10.440","Text":"2 and 3 for the final one,"},{"Start":"03:10.440 ","End":"03:12.780","Text":"and put name in there."},{"Start":"03:12.780 ","End":"03:16.840","Text":"Let\u0027s have a look at our file now see what we\u0027ve got in there. There we go."},{"Start":"03:16.840 ","End":"03:18.985","Text":"We got 10 names, 1, 2, 3, 4,"},{"Start":"03:18.985 ","End":"03:21.475","Text":"5, 6, 7, 8, 9, 10,"},{"Start":"03:21.475 ","End":"03:25.780","Text":"and the last lowest score is a person called Last,"},{"Start":"03:25.780 ","End":"03:28.145","Text":"and they have a score of 2."},{"Start":"03:28.145 ","End":"03:31.840","Text":"We\u0027re going to add in Part 5 another score which is"},{"Start":"03:31.840 ","End":"03:35.500","Text":"below the lowest score in the file and check up false is"},{"Start":"03:35.500 ","End":"03:38.890","Text":"returned on the file is unchanged because it shouldn\u0027t"},{"Start":"03:38.890 ","End":"03:42.670","Text":"add a score that\u0027s lower than 2 because we\u0027ve already got 10,"},{"Start":"03:42.670 ","End":"03:44.950","Text":"and 2 is already in there."},{"Start":"03:44.950 ","End":"03:48.275","Text":"Let\u0027s try that to see if it works."},{"Start":"03:48.275 ","End":"03:51.820","Text":"Even lower. We call this lower."},{"Start":"03:51.820 ","End":"03:56.660","Text":"We\u0027ll give them a mark of 1 and we do get false back,"},{"Start":"03:56.660 ","End":"04:01.465","Text":"and it hasn\u0027t changed the file, we just checked that."},{"Start":"04:01.465 ","End":"04:03.590","Text":"You\u0027ll notice temp is still there."},{"Start":"04:03.590 ","End":"04:05.870","Text":"We haven\u0027t seen temp all of these times."},{"Start":"04:05.870 ","End":"04:08.210","Text":"Last is the last one but you\u0027ll see"},{"Start":"04:08.210 ","End":"04:12.185","Text":"that temp also contains the same as what we had there before."},{"Start":"04:12.185 ","End":"04:15.575","Text":"It was hidden all this time because it was happening so quickly."},{"Start":"04:15.575 ","End":"04:17.270","Text":"We were creating it,"},{"Start":"04:17.270 ","End":"04:21.410","Text":"and then we were deleting high scores and replacing it with temp,"},{"Start":"04:21.410 ","End":"04:23.585","Text":"which was then renamed to high scores."},{"Start":"04:23.585 ","End":"04:25.310","Text":"You wouldn\u0027t even see this happening."},{"Start":"04:25.310 ","End":"04:28.205","Text":"Windows wouldn\u0027t detect it happening quickly enough."},{"Start":"04:28.205 ","End":"04:30.080","Text":"We just never saw temp."},{"Start":"04:30.080 ","End":"04:36.890","Text":"We\u0027ve seen it now for the first time because we failed to rename it to high scores."},{"Start":"04:36.890 ","End":"04:39.770","Text":"We didn\u0027t need to basically because there were no new high scores."},{"Start":"04:39.770 ","End":"04:46.340","Text":"But I suppose we should check another score above 10 let\u0027s say."},{"Start":"04:46.340 ","End":"04:48.380","Text":"It could be any of the ones above 2,"},{"Start":"04:48.380 ","End":"04:51.350","Text":"and it should drop the rest below."},{"Start":"04:51.350 ","End":"04:55.920","Text":"Let\u0027s have another person called Bill,"},{"Start":"04:55.920 ","End":"05:01.610","Text":"and let\u0027s give them a score of 20 and that should move everyone down 1."},{"Start":"05:01.610 ","End":"05:06.845","Text":"It has successfully added them and we still got our high scores file there."},{"Start":"05:06.845 ","End":"05:10.555","Text":"You can see Bill has been inserted between Joe and tensing,"},{"Start":"05:10.555 ","End":"05:16.865","Text":"and the one we had earlier with a score of 2 Last has now dropped out a file."},{"Start":"05:16.865 ","End":"05:18.640","Text":"That\u0027s all worked as we expect,"},{"Start":"05:18.640 ","End":"05:22.355","Text":"so actually quite a complex program in the end,"},{"Start":"05:22.355 ","End":"05:23.885","Text":"lots going on there."},{"Start":"05:23.885 ","End":"05:26.900","Text":"We pass it a name and we pass it a score and it returns a"},{"Start":"05:26.900 ","End":"05:29.960","Text":"true or false depending on whether it added it to the file."},{"Start":"05:29.960 ","End":"05:36.170","Text":"It\u0027s opening one file for reading and then another file for writing called temp."},{"Start":"05:36.170 ","End":"05:38.960","Text":"It creates the file if it doesn\u0027t already"},{"Start":"05:38.960 ","End":"05:42.845","Text":"exist here so that the scanner can successfully work."},{"Start":"05:42.845 ","End":"05:46.295","Text":"Then while we\u0027ve got less than 10 scores,"},{"Start":"05:46.295 ","End":"05:49.010","Text":"actually while we\u0027ve read less than 10 lines,"},{"Start":"05:49.010 ","End":"05:51.800","Text":"there is still another line to read."},{"Start":"05:51.800 ","End":"05:58.360","Text":"We do this processing in which we split the line into 2 halves based on the comma."},{"Start":"05:58.360 ","End":"06:01.160","Text":"The name will go in the first part of this array,"},{"Start":"06:01.160 ","End":"06:03.350","Text":"the score and the second part,"},{"Start":"06:03.350 ","End":"06:08.705","Text":"we convert that second part into an integer and we compare it to score."},{"Start":"06:08.705 ","End":"06:12.560","Text":"If we haven\u0027t already had a high score around this loop,"},{"Start":"06:12.560 ","End":"06:15.385","Text":"and the score is greater than"},{"Start":"06:15.385 ","End":"06:19.070","Text":"the score that was passed in is greater than normally currently looking at."},{"Start":"06:19.070 ","End":"06:22.520","Text":"Then we write it out to the file and the current position same,"},{"Start":"06:22.520 ","End":"06:25.265","Text":"we\u0027ve gotten new high score increments our count,"},{"Start":"06:25.265 ","End":"06:29.645","Text":"we also output the original line so long as it says,"},{"Start":"06:29.645 ","End":"06:31.130","Text":"and we haven\u0027t gone over 10."},{"Start":"06:31.130 ","End":"06:34.355","Text":"Otherwise we just output the original line."},{"Start":"06:34.355 ","End":"06:37.805","Text":"That keeps happening until we read to the end of the file."},{"Start":"06:37.805 ","End":"06:40.490","Text":"The file can have 0 lines or 10 lines,"},{"Start":"06:40.490 ","End":"06:41.870","Text":"it could have more than 10 lines,"},{"Start":"06:41.870 ","End":"06:43.820","Text":"but will always work."},{"Start":"06:43.820 ","End":"06:46.265","Text":"Then at the end of this while loop,"},{"Start":"06:46.265 ","End":"06:48.860","Text":"we see if we had an empty file,"},{"Start":"06:48.860 ","End":"06:50.060","Text":"if we had an empty file,"},{"Start":"06:50.060 ","End":"06:51.545","Text":"then we write our line in."},{"Start":"06:51.545 ","End":"06:54.200","Text":"Because by definition, an empty file,"},{"Start":"06:54.200 ","End":"06:56.630","Text":"whatever we pass in must be a high score."},{"Start":"06:56.630 ","End":"06:59.330","Text":"Also if we\u0027ve got less than 10 scores,"},{"Start":"06:59.330 ","End":"07:00.720","Text":"it doesn\u0027t matter what the score is,"},{"Start":"07:00.720 ","End":"07:02.915","Text":"it still going to be a high score as well."},{"Start":"07:02.915 ","End":"07:04.400","Text":"Then we close the files,"},{"Start":"07:04.400 ","End":"07:05.780","Text":"if we don\u0027t do this,"},{"Start":"07:05.780 ","End":"07:07.650","Text":"nothing will be written."},{"Start":"07:07.650 ","End":"07:13.250","Text":"The final bit we do is we do this little trick where we delete the original file and then"},{"Start":"07:13.250 ","End":"07:20.195","Text":"rename temporary file to the name that we had before and we return true or false."},{"Start":"07:20.195 ","End":"07:24.200","Text":"Quite a long and complex program that does illustrate pretty"},{"Start":"07:24.200 ","End":"07:28.235","Text":"much everything you need to know with basic text file handling."},{"Start":"07:28.235 ","End":"07:31.920","Text":"Thanks for watching and I\u0027ll see you for the next one."}],"ID":29810},{"Watched":false,"Name":"Exercise 6 part a","Duration":"7m 41s","ChapterTopicVideoID":28376,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.820","Text":"Hello, welcome. In this exercise,"},{"Start":"00:02.820 ","End":"00:06.330","Text":"we\u0027ve been told that files can be written as binary with random access,"},{"Start":"00:06.330 ","End":"00:10.875","Text":"which allows code to seek to a particular place in the file for reading or writing."},{"Start":"00:10.875 ","End":"00:14.235","Text":"Then in part A, we run some code in the codepad,"},{"Start":"00:14.235 ","End":"00:18.030","Text":"firstly being import.java.io.*,"},{"Start":"00:18.030 ","End":"00:20.909","Text":"then RandomAccessFile file"},{"Start":"00:20.909 ","End":"00:30.570","Text":"= new RandomAccessFile (\u0027\u0027test.bin\", \"rw\")."},{"Start":"00:30.570 ","End":"00:33.840","Text":"Then we run a method onfile,"},{"Start":"00:33.840 ","End":"00:39.650","Text":"file.writeUTF and in brackets and some quotes we have (\"Here are some characters\")."},{"Start":"00:39.650 ","End":"00:50.145","Text":"Then file.writeInt(1234), and then file.writeFloat(3.14F)."},{"Start":"00:50.145 ","End":"00:51.960","Text":"Then we run a series of methods."},{"Start":"00:51.960 ","End":"00:55.665","Text":"First one being file.length(), then file.seek(0),"},{"Start":"00:55.665 ","End":"01:01.245","Text":"then file.readUTF(), file.readInt(),"},{"Start":"01:01.245 ","End":"01:04.335","Text":"file.readFloat(), and then file.readUTF() again."},{"Start":"01:04.335 ","End":"01:07.950","Text":"In part B, we\u0027re asked to create a method called addStock"},{"Start":"01:07.950 ","End":"01:11.355","Text":"which returns a boolean and takes in 3 arguments,"},{"Start":"01:11.355 ","End":"01:12.915","Text":"a String called name,"},{"Start":"01:12.915 ","End":"01:14.730","Text":"an int called quantity,"},{"Start":"01:14.730 ","End":"01:16.815","Text":"and a float called price."},{"Start":"01:16.815 ","End":"01:19.290","Text":"Then in part C, we create a new file called"},{"Start":"01:19.290 ","End":"01:23.130","Text":"stocks.bin for read and write with random access."},{"Start":"01:23.130 ","End":"01:25.950","Text":"In part D, we use a loop and"},{"Start":"01:25.950 ","End":"01:31.773","Text":"the string concat method to add spaces to the end of the contents of name,"},{"Start":"01:31.773 ","End":"01:34.920","Text":"while name contains less than 6 characters."},{"Start":"01:34.920 ","End":"01:39.280","Text":"For example, an input of \"IBM\" becomes \"IBM\" with 3 spaces after it."},{"Start":"01:39.860 ","End":"01:43.920","Text":"In part E, we\u0027re told to seek to the end of the file,"},{"Start":"01:43.920 ","End":"01:45.885","Text":"and write name, quantity,"},{"Start":"01:45.885 ","End":"01:52.350","Text":"and price to the file in that order and using their appropriate data types."},{"Start":"01:52.350 ","End":"01:56.550","Text":"We then close the file and return true or false depending on"},{"Start":"01:56.550 ","End":"02:00.510","Text":"the method\u0027s success in part F. In part G,"},{"Start":"02:00.510 ","End":"02:03.015","Text":"we create a new method called getStock,"},{"Start":"02:03.015 ","End":"02:09.000","Text":"which returns a String and accepts a single int argument called position."},{"Start":"02:09.000 ","End":"02:14.610","Text":"The method seeks to the stock in the file pointed at by position using"},{"Start":"02:14.610 ","End":"02:22.740","Text":"the formula,(position-1)*16 given that each stock will take up 16 bytes in a file,"},{"Start":"02:22.740 ","End":"02:28.065","Text":"and it\u0027s desirable to count from 1 rather than 0."},{"Start":"02:28.065 ","End":"02:34.350","Text":"The method we are told in part G should return the stock name and value"},{"Start":"02:34.350 ","End":"02:40.500","Text":"read from the file as a String by multiplying the quantity by the price in this form,"},{"Start":"02:40.500 ","End":"02:47.334","Text":"where it\u0027s \"IBM value is 13391.0\" as an example."},{"Start":"02:47.334 ","End":"02:51.730","Text":"An exception should return the value \"error\"."},{"Start":"02:51.860 ","End":"02:56.100","Text":"Then the testing comes in part J and K. Ji,"},{"Start":"02:56.100 ","End":"02:58.720","Text":"we test with IBM, 100, 133.91."},{"Start":"03:00.920 ","End":"03:03.465","Text":"In the second test of J,"},{"Start":"03:03.465 ","End":"03:06.687","Text":"we do JET2.L, 100, 1061.00."},{"Start":"03:06.687 ","End":"03:14.602","Text":"In part iii,"},{"Start":"03:14.602 ","End":"03:16.740","Text":"GLEN.L, 200, 377.65."},{"Start":"03:16.740 ","End":"03:24.390","Text":"In part iv, ASL.F, 200, 120.40."},{"Start":"03:24.390 ","End":"03:25.500","Text":"In part v,"},{"Start":"03:25.500 ","End":"03:32.250","Text":"REE.MC, 300, 19.02."},{"Start":"03:32.250 ","End":"03:37.996","Text":"In part K, we test with the following inputs for getStock: 4"},{"Start":"03:37.996 ","End":"03:44.110","Text":"should return \"ASL.F value is 24080.0\","},{"Start":"03:44.110 ","End":"03:47.940","Text":"5 should return \"REE.MC value is 5706.0\","},{"Start":"03:47.940 ","End":"03:51.585","Text":"and 6 should return \"error\"."},{"Start":"03:51.585 ","End":"03:55.095","Text":"We\u0027re going to have a go at doing some of these things in"},{"Start":"03:55.095 ","End":"03:59.340","Text":"codepad before we write the code that we need to write."},{"Start":"03:59.340 ","End":"04:03.105","Text":"As usual, we\u0027ve got to import the java.io."},{"Start":"04:03.105 ","End":"04:07.590","Text":"libraries, and then we\u0027ve come across a new object,"},{"Start":"04:07.590 ","End":"04:12.165","Text":"this time called RandomAccessFile or class, I should say."},{"Start":"04:12.165 ","End":"04:17.820","Text":"It\u0027s got a slightly different syntax than the FileWriter and so on that we\u0027ve been using."},{"Start":"04:17.820 ","End":"04:22.310","Text":"What we see is we need to put in not only a filename,"},{"Start":"04:22.310 ","End":"04:26.400","Text":"but we say how we\u0027re opening the file,"},{"Start":"04:26.400 ","End":"04:29.580","Text":"whether it\u0027s just for reading or whether it\u0027s for reading and writing."},{"Start":"04:29.580 ","End":"04:32.414","Text":"In this case, we\u0027re going to open it for reading and writing."},{"Start":"04:32.414 ","End":"04:37.160","Text":"That first bit done of A part I and II."},{"Start":"04:37.160 ","End":"04:39.200","Text":"Now, we\u0027re going to actually write to the file,"},{"Start":"04:39.200 ","End":"04:42.200","Text":"but this time it\u0027s not writing texts characters."},{"Start":"04:42.200 ","End":"04:43.726","Text":"We can write texts, and in fact,"},{"Start":"04:43.726 ","End":"04:45.200","Text":"this first one is going to."},{"Start":"04:45.200 ","End":"04:48.140","Text":"But we\u0027re writing binary values to the file,"},{"Start":"04:48.140 ","End":"04:49.460","Text":"so we can write anything we like."},{"Start":"04:49.460 ","End":"04:50.765","Text":"It doesn\u0027t have to be text,"},{"Start":"04:50.765 ","End":"04:52.070","Text":"but this first part,"},{"Start":"04:52.070 ","End":"04:54.540","Text":"we\u0027re going to write some text."},{"Start":"04:55.420 ","End":"04:58.795","Text":"The words \"here are some characters\"."},{"Start":"04:58.795 ","End":"05:02.715","Text":"We can see that our file test.bin has been created."},{"Start":"05:02.715 ","End":"05:05.445","Text":"It doesn\u0027t look like there\u0027s anything been written to it yet."},{"Start":"05:05.445 ","End":"05:09.270","Text":"Let\u0027s carry on and do the file clause and see if"},{"Start":"05:09.270 ","End":"05:13.860","Text":"that does what we want it to do, file.writeInt."},{"Start":"05:13.860 ","End":"05:18.495","Text":"We\u0027re going to write an integer value of 1234."},{"Start":"05:18.495 ","End":"05:22.770","Text":"There\u0027s another one called writeFloat,"},{"Start":"05:22.770 ","End":"05:26.400","Text":"and we\u0027re going to write the value of 3.14."},{"Start":"05:26.400 ","End":"05:28.053","Text":"If we don\u0027t put F afterwards,"},{"Start":"05:28.053 ","End":"05:32.136","Text":"it might complain that it will assume it\u0027s a double if it\u0027s got a decimal point in,"},{"Start":"05:32.136 ","End":"05:36.195","Text":"so put an F there to force it to a float."},{"Start":"05:36.195 ","End":"05:40.230","Text":"All of that looks good. There\u0027s another little method here called length."},{"Start":"05:40.230 ","End":"05:42.495","Text":"Let\u0027s see what that does for us."},{"Start":"05:42.495 ","End":"05:47.430","Text":"It returns the length of the file, presumably in bytes."},{"Start":"05:47.430 ","End":"05:52.260","Text":"It\u0027s 34 bytes long. That\u0027s interesting. 34 bytes."},{"Start":"05:52.260 ","End":"05:56.610","Text":"I suppose these characters here are some characters plus the int that we wrote plus"},{"Start":"05:56.610 ","End":"06:01.290","Text":"the floating point number that we wrote all add up to 34 characters."},{"Start":"06:01.290 ","End":"06:02.700","Text":"Sounds plausible."},{"Start":"06:02.700 ","End":"06:08.338","Text":"Now, a command of file.seek(0),"},{"Start":"06:08.338 ","End":"06:12.840","Text":"that is seeking to a position in bytes within the file."},{"Start":"06:12.840 ","End":"06:15.420","Text":"If we seek into position 0,"},{"Start":"06:15.420 ","End":"06:18.135","Text":"we\u0027re seeking to the beginning of the file."},{"Start":"06:18.135 ","End":"06:24.480","Text":"If we now execute A,viii file.readUTF(),"},{"Start":"06:24.480 ","End":"06:28.410","Text":"what we\u0027ll see is the beginning of the file,"},{"Start":"06:28.410 ","End":"06:30.780","Text":"and that\u0027s what we wrote at the beginning of the file."},{"Start":"06:30.780 ","End":"06:33.975","Text":"UTF is a way of encoding Strings."},{"Start":"06:33.975 ","End":"06:37.560","Text":"We\u0027ve seen here are some characters [inaudible] back to us."},{"Start":"06:37.560 ","End":"06:41.910","Text":"If we do number ix now, file.readInt."},{"Start":"06:41.910 ","End":"06:47.250","Text":"Do we get the integer back that we wrote in? Yes, we do."},{"Start":"06:47.250 ","End":"06:52.995","Text":"Then get a float back as well using readFloat(), and we get that back."},{"Start":"06:52.995 ","End":"06:56.355","Text":"Now, what if we try and read UTF again?"},{"Start":"06:56.355 ","End":"06:58.650","Text":"What do you expect will happen?"},{"Start":"06:58.650 ","End":"07:01.860","Text":"What will happen? We get an exception."},{"Start":"07:01.860 ","End":"07:04.485","Text":"What it gives us is its end of file exception."},{"Start":"07:04.485 ","End":"07:09.118","Text":"Basically, we\u0027re trying to read our position when there\u0027s no more data,"},{"Start":"07:09.118 ","End":"07:13.640","Text":"so it\u0027s given us this exception to say basically you\u0027ve read past the end of the file,"},{"Start":"07:13.640 ","End":"07:15.755","Text":"which is exactly what we expected."},{"Start":"07:15.755 ","End":"07:19.400","Text":"Because we put in the string, then an int,"},{"Start":"07:19.400 ","End":"07:22.655","Text":"then a float, and we\u0027ve taken out a string,"},{"Start":"07:22.655 ","End":"07:23.870","Text":"an int, and a float."},{"Start":"07:23.870 ","End":"07:25.190","Text":"If we try and take out another string,"},{"Start":"07:25.190 ","End":"07:26.735","Text":"there\u0027s nothing left to read."},{"Start":"07:26.735 ","End":"07:28.790","Text":"All that makes perfect sense."},{"Start":"07:28.790 ","End":"07:33.650","Text":"That\u0027s the end of part A. I\u0027ll see you in a moment in the next video,"},{"Start":"07:33.650 ","End":"07:36.380","Text":"where we actually implement the methods that we need to"},{"Start":"07:36.380 ","End":"07:40.260","Text":"create in part B-I, so see you in a moment."}],"ID":29811},{"Watched":false,"Name":"Exercise 6 part b","Duration":"7m 41s","ChapterTopicVideoID":28379,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.170 ","End":"00:04.050","Text":"Let\u0027s create our method now called addStock,"},{"Start":"00:04.050 ","End":"00:06.495","Text":"which returns a Boolean."},{"Start":"00:06.495 ","End":"00:09.705","Text":"We have 3 arguments."},{"Start":"00:09.705 ","End":"00:13.110","Text":"The first 1 is a string called name,"},{"Start":"00:13.110 ","End":"00:18.390","Text":"second 1 is an int called quantity,"},{"Start":"00:18.390 ","End":"00:23.490","Text":"and the final 1 is a float called price."},{"Start":"00:23.490 ","End":"00:25.785","Text":"Those are opening line."},{"Start":"00:25.785 ","End":"00:29.055","Text":"As ever, this is going to be file-related,"},{"Start":"00:29.055 ","End":"00:33.960","Text":"so we\u0027re going to need to try and catch block so much we\u0027ll put that in upfront,"},{"Start":"00:33.960 ","End":"00:40.000","Text":"and I\u0027m going to catch all of mine in 1 generic exception block."},{"Start":"00:40.340 ","End":"00:42.985","Text":"If we output,"},{"Start":"00:42.985 ","End":"00:46.625","Text":"stack trace that will tell us pretty much everything that\u0027s gone wrong."},{"Start":"00:46.625 ","End":"00:50.525","Text":"Also we actually later on are going to return a value of false."},{"Start":"00:50.525 ","End":"00:53.000","Text":"We\u0027ll do that in here now."},{"Start":"00:53.000 ","End":"00:57.630","Text":"Inside the try block is where we\u0027re going to start"},{"Start":"00:57.630 ","End":"01:01.640","Text":"with Part C. We\u0027re going to create a new file called stock,"},{"Start":"01:01.640 ","End":"01:07.100","Text":"Stock being for read and write with random axis."},{"Start":"01:07.100 ","End":"01:12.440","Text":"What we want is a random access file, give it a name,"},{"Start":"01:12.440 ","End":"01:19.505","Text":"and a coal f equals new Random Access file."},{"Start":"01:19.505 ","End":"01:24.830","Text":"We pass it in a filename that we want to access, so if there isn\u0027t one,"},{"Start":"01:24.830 ","End":"01:27.140","Text":"it will create it, stock.bin,"},{"Start":"01:27.140 ","End":"01:30.425","Text":"and it\u0027s open for read and write."},{"Start":"01:30.425 ","End":"01:33.035","Text":"We need to add RW there."},{"Start":"01:33.035 ","End":"01:35.380","Text":"That will open the file,"},{"Start":"01:35.380 ","End":"01:39.560","Text":"and then what we need to do is in part D,"},{"Start":"01:39.560 ","End":"01:43.385","Text":"use a loop and the string concat method to add spaces"},{"Start":"01:43.385 ","End":"01:47.869","Text":"to the end of the contents of name while it contains less than 6 characters."},{"Start":"01:47.869 ","End":"01:49.385","Text":"This is called padding,"},{"Start":"01:49.385 ","End":"01:51.635","Text":"very common thing that you want to do."},{"Start":"01:51.635 ","End":"01:59.325","Text":"While the name\u0027s length is less than 6,"},{"Start":"01:59.325 ","End":"02:08.040","Text":"we are going to add to the end of this string name another space."},{"Start":"02:08.040 ","End":"02:14.350","Text":"It can do that easily by saying name equals name.concat, and then space."},{"Start":"02:14.350 ","End":"02:18.370","Text":"That will just concatenate one single-space on the end of name,"},{"Start":"02:18.370 ","End":"02:21.850","Text":"and it\u0027ll keep doing that while name is less than 6 characters."},{"Start":"02:21.850 ","End":"02:26.365","Text":"This will have the effect of padding name to 6 characters."},{"Start":"02:26.365 ","End":"02:29.065","Text":"We\u0027ll find out why that\u0027s important in a moment."},{"Start":"02:29.065 ","End":"02:34.930","Text":"Now what we need to do is seek to the end of the file and write name,"},{"Start":"02:34.930 ","End":"02:39.890","Text":"quantity and price to fall in that order and using their appropriate datatypes."},{"Start":"02:39.890 ","End":"02:41.525","Text":"If you want to seek,"},{"Start":"02:41.525 ","End":"02:46.700","Text":"we do f.seek and we say where in bytes we want to seek to."},{"Start":"02:46.700 ","End":"02:51.090","Text":"Because we can get the length of the file from f.length,"},{"Start":"02:51.740 ","End":"02:54.780","Text":"then that\u0027s where we want to seek to,"},{"Start":"02:54.780 ","End":"02:56.915","Text":"we want to seek to the end of the file."},{"Start":"02:56.915 ","End":"03:00.875","Text":"That will now put us at the end of the file to stop us overwriting anything."},{"Start":"03:00.875 ","End":"03:04.710","Text":"Now we can write out the individual elements."},{"Start":"03:05.440 ","End":"03:12.350","Text":"The first one we want to do is write a UTF value,"},{"Start":"03:12.350 ","End":"03:16.055","Text":"unicode text format string."},{"Start":"03:16.055 ","End":"03:19.220","Text":"We are just going to write name,"},{"Start":"03:19.220 ","End":"03:21.350","Text":"and then we do the same again."},{"Start":"03:21.350 ","End":"03:24.290","Text":"But we\u0027re not writing a UTF value,"},{"Start":"03:24.290 ","End":"03:28.200","Text":"this time we\u0027re going to write quantity,"},{"Start":"03:29.720 ","End":"03:35.165","Text":"and then finally we\u0027re going to write the price."},{"Start":"03:35.165 ","End":"03:39.980","Text":"Do you see appropriate right method for the datatype we\u0027re using."},{"Start":"03:39.980 ","End":"03:44.195","Text":"That now should have done what we want it to."},{"Start":"03:44.195 ","End":"03:48.770","Text":"We\u0027re on Part F, we close the file, f.dot close."},{"Start":"03:48.770 ","End":"03:53.105","Text":"We return true in this case"},{"Start":"03:53.105 ","End":"03:59.240","Text":"because we\u0027ve done what we\u0027ve been asked to do and we haven\u0027t had any exceptions."},{"Start":"03:59.240 ","End":"04:01.430","Text":"If there has been an exception,"},{"Start":"04:01.430 ","End":"04:06.165","Text":"then I already put into the catch block a return false here."},{"Start":"04:06.165 ","End":"04:10.400","Text":"That\u0027s our method done doesn\u0027t look like there\u0027s any errors,"},{"Start":"04:10.400 ","End":"04:20.090","Text":"and so we can now create the other partner method to this in part G called getStock."},{"Start":"04:20.090 ","End":"04:22.130","Text":"This returns a string."},{"Start":"04:22.130 ","End":"04:28.115","Text":"It takes as parameter of position that we\u0027re looking for in the file."},{"Start":"04:28.115 ","End":"04:30.330","Text":"Actually this is not positioned in bytes,"},{"Start":"04:30.330 ","End":"04:32.840","Text":"this is a position as in the number of the stock."},{"Start":"04:32.840 ","End":"04:34.955","Text":"The first one is a first stock,"},{"Start":"04:34.955 ","End":"04:37.600","Text":"second one is a second stock and so on."},{"Start":"04:37.600 ","End":"04:42.620","Text":"What we have been asked to do is to, first of all,"},{"Start":"04:42.620 ","End":"04:46.900","Text":"we need to create another random access file object,"},{"Start":"04:46.900 ","End":"04:48.480","Text":"put a f again,"},{"Start":"04:48.480 ","End":"04:52.235","Text":"and the file we\u0027re looking to open is called stock.bin."},{"Start":"04:52.235 ","End":"04:54.395","Text":"We can actually open it just for read"},{"Start":"04:54.395 ","End":"04:57.710","Text":"only because we\u0027re not going to write anything to it in this method,"},{"Start":"04:57.710 ","End":"05:00.890","Text":"we\u0027re just going to read from it, and be safe for just to open it in read mode."},{"Start":"05:00.890 ","End":"05:03.740","Text":"There we\u0027ve got the first opening line."},{"Start":"05:03.740 ","End":"05:08.825","Text":"Of course it\u0027s going to complain because I haven\u0027t done any try and catch blocks,"},{"Start":"05:08.825 ","End":"05:11.960","Text":"so let\u0027s copy something from there."},{"Start":"05:11.960 ","End":"05:15.680","Text":"Let\u0027s put out try and catch blocks in to make it happy."},{"Start":"05:15.680 ","End":"05:18.035","Text":"There\u0027s the first block."},{"Start":"05:18.035 ","End":"05:19.840","Text":"We\u0027re not going to return false here,"},{"Start":"05:19.840 ","End":"05:24.575","Text":"we\u0027re going to return error because it\u0027s a string rather than a Boolean."},{"Start":"05:24.575 ","End":"05:29.425","Text":"We do want to return that something has failed."},{"Start":"05:29.425 ","End":"05:33.150","Text":"If we go back to now part H,"},{"Start":"05:33.150 ","End":"05:37.940","Text":"we\u0027ve got to seek to the stock in the file pointer that by position,"},{"Start":"05:37.940 ","End":"05:41.495","Text":"bearing in mind that each stock will take up 16 bytes in the file."},{"Start":"05:41.495 ","End":"05:46.625","Text":"If we did f.seek position times 16,"},{"Start":"05:46.625 ","End":"05:51.260","Text":"because there are 16 bytes that would seek to the relevant place."},{"Start":"05:51.260 ","End":"05:56.720","Text":"But actually we need to subtract 1 if we\u0027re going to start counting from 1."},{"Start":"05:56.720 ","End":"06:00.035","Text":"Because it actually starts at 0 in the file,"},{"Start":"06:00.035 ","End":"06:02.045","Text":"so we do position minus 1,"},{"Start":"06:02.045 ","End":"06:07.965","Text":"then multiply by 16 for the number of bytes each stock is going to take up."},{"Start":"06:07.965 ","End":"06:09.800","Text":"It\u0027s remarkably straightforward."},{"Start":"06:09.800 ","End":"06:12.650","Text":"All we need to do now is to return the value."},{"Start":"06:12.650 ","End":"06:15.725","Text":"But there is quite a bit we need to do there."},{"Start":"06:15.725 ","End":"06:18.460","Text":"We need to return first of all,"},{"Start":"06:18.460 ","End":"06:21.660","Text":"the UTF name,"},{"Start":"06:21.660 ","End":"06:23.510","Text":"so we\u0027ll return that."},{"Start":"06:23.510 ","End":"06:28.920","Text":"Then we\u0027re going to return literal string value is,"},{"Start":"06:28.920 ","End":"06:35.660","Text":"then we\u0027re going to calculate the quantity times the price of the stock,"},{"Start":"06:35.660 ","End":"06:37.160","Text":"because one is an integer, one is a float."},{"Start":"06:37.160 ","End":"06:40.895","Text":"We\u0027ve got to use different methods to get them out and then multiply them together,"},{"Start":"06:40.895 ","End":"06:45.515","Text":"so f.readInt will be the first one."},{"Start":"06:45.515 ","End":"06:52.140","Text":"When I multiply that by f.readFloat,"},{"Start":"06:52.140 ","End":"06:55.165","Text":"and put all that in brackets."},{"Start":"06:55.165 ","End":"07:00.605","Text":"What we need to do is we\u0027re going to have to convert that to a string or a complaint."},{"Start":"07:00.605 ","End":"07:06.183","Text":"Float object has a method called toString seen before."},{"Start":"07:06.183 ","End":"07:10.190","Text":"That will calculate a number which looks like a float,"},{"Start":"07:10.190 ","End":"07:13.265","Text":"but it will convert it into a string."},{"Start":"07:13.265 ","End":"07:15.605","Text":"You can multiply integers by floats."},{"Start":"07:15.605 ","End":"07:19.040","Text":"It shouldn\u0027t complain about that at all that\u0027s absolutely fine."},{"Start":"07:19.040 ","End":"07:23.360","Text":"That then we\u0027ll return a string,"},{"Start":"07:23.360 ","End":"07:24.860","Text":"which is the name of the stock,"},{"Start":"07:24.860 ","End":"07:28.220","Text":"followed by value is and followed by calculation,"},{"Start":"07:28.220 ","End":"07:29.960","Text":"which is turned into a string."},{"Start":"07:29.960 ","End":"07:32.720","Text":"That\u0027s what we\u0027ve been asked to do."},{"Start":"07:32.720 ","End":"07:35.270","Text":"That should be asked done."},{"Start":"07:35.270 ","End":"07:38.390","Text":"Let\u0027s get to the testing in the next video."},{"Start":"07:38.390 ","End":"07:42.210","Text":"I\u0027ll pause just there, and I\u0027ll see you again in a moment."}],"ID":29812},{"Watched":false,"Name":"Exercise 6 part c","Duration":"8m 6s","ChapterTopicVideoID":28380,"CourseChapterTopicPlaylistID":280619,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.105","Text":"Let\u0027s do the tests in part J,"},{"Start":"00:03.105 ","End":"00:05.625","Text":"create our object,"},{"Start":"00:05.625 ","End":"00:08.850","Text":"and then run a method called addStock."},{"Start":"00:08.850 ","End":"00:13.740","Text":"The first 1 we\u0027re going to add is IBM with"},{"Start":"00:13.740 ","End":"00:20.070","Text":"a quantity of 100 and a price of 133.91,"},{"Start":"00:20.070 ","End":"00:25.155","Text":"put an F on there to stop it complaining about doubles losing precision."},{"Start":"00:25.155 ","End":"00:29.130","Text":"It\u0027s giving me a true back so it looks like it\u0027s done something,"},{"Start":"00:29.130 ","End":"00:32.415","Text":"and I can see the file stock has been created."},{"Start":"00:32.415 ","End":"00:38.180","Text":"We\u0027ll have a look inside that in a moment once we\u0027ve done all these little exercises."},{"Start":"00:38.180 ","End":"00:42.170","Text":"Let\u0027s do the next test, J2."},{"Start":"00:42.170 ","End":"00:47.805","Text":"The next 1 is JET2.L and 100 rows,"},{"Start":"00:47.805 ","End":"00:50.985","Text":"and the price is 1061.00."},{"Start":"00:50.985 ","End":"00:53.490","Text":"That\u0027s happy with that as well."},{"Start":"00:53.490 ","End":"00:57.983","Text":"The third 1, GLEN.L,"},{"Start":"00:57.983 ","End":"01:02.170","Text":"quality 200, price, 377.65."},{"Start":"01:02.240 ","End":"01:04.620","Text":"Happy with that."},{"Start":"01:04.620 ","End":"01:07.530","Text":"It\u0027s reporting it\u0027s happy whether it\u0027s written it to the file or not,"},{"Start":"01:07.530 ","End":"01:09.390","Text":"we\u0027ll find out in a moment."},{"Start":"01:09.390 ","End":"01:15.026","Text":"ASL.F. Some of these are already 1,"},{"Start":"01:15.026 ","End":"01:16.292","Text":"2, 3, 4, 5,"},{"Start":"01:16.292 ","End":"01:19.360","Text":"6 characters in the case of GLEN and JET2."},{"Start":"01:19.360 ","End":"01:20.960","Text":"This one\u0027s shorter."},{"Start":"01:20.960 ","End":"01:22.680","Text":"I need 5 characters."},{"Start":"01:22.680 ","End":"01:23.970","Text":"We want to pad it to 6,"},{"Start":"01:23.970 ","End":"01:25.765","Text":"we\u0027ll see if that\u0027s worked as well."},{"Start":"01:25.765 ","End":"01:32.015","Text":"This 1 we have 200 off at 120.40."},{"Start":"01:32.015 ","End":"01:39.025","Text":"Then the final 1 is going to be REE.MC."},{"Start":"01:39.025 ","End":"01:42.260","Text":"Again, that\u0027s already 6 so that won\u0027t need any padding."},{"Start":"01:42.260 ","End":"01:47.130","Text":"300 of those at 19.02."},{"Start":"01:48.340 ","End":"01:52.804","Text":"There is a testing done for j."},{"Start":"01:52.804 ","End":"01:55.265","Text":"We\u0027re going to have a look inside this file in a moment."},{"Start":"01:55.265 ","End":"02:00.230","Text":"I\u0027ll show you how, but we can test whether it\u0027s worked or not by running"},{"Start":"02:00.230 ","End":"02:05.795","Text":"the tests in k. Let\u0027s now get stock."},{"Start":"02:05.795 ","End":"02:08.210","Text":"We\u0027re going to give a position each time."},{"Start":"02:08.210 ","End":"02:13.940","Text":"Stock number 4 is test number 4 in j,"},{"Start":"02:13.940 ","End":"02:18.290","Text":"so we should get back the fourth 1 that we typed in."},{"Start":"02:18.290 ","End":"02:24.930","Text":"We do see ASL.F and the value we get back is 24080.0."},{"Start":"02:25.330 ","End":"02:29.128","Text":"We know that\u0027s fine because that\u0027s 200 if you double it,"},{"Start":"02:29.128 ","End":"02:31.085","Text":"and then add 2 zeros onto the end,"},{"Start":"02:31.085 ","End":"02:33.210","Text":"that\u0027s the number you get."},{"Start":"02:33.210 ","End":"02:35.805","Text":"That\u0027s worked fine."},{"Start":"02:35.805 ","End":"02:41.340","Text":"to get stock again for position 5,"},{"Start":"02:41.340 ","End":"02:46.305","Text":"which last 1 of our tests in J, and we do."},{"Start":"02:46.305 ","End":"02:50.015","Text":"I\u0027ll assume that that calculation is right as well."},{"Start":"02:50.015 ","End":"02:52.100","Text":"It looks like it\u0027s doing what it should do."},{"Start":"02:52.100 ","End":"02:55.015","Text":"Then let\u0027s have a look at the test in Part 3,"},{"Start":"02:55.015 ","End":"02:57.960","Text":"which is stock number 6."},{"Start":"02:57.960 ","End":"03:01.050","Text":"We don\u0027t have a 6th stock,"},{"Start":"03:01.050 ","End":"03:04.335","Text":"so it should give us an error."},{"Start":"03:04.335 ","End":"03:07.370","Text":"It does, it returns as an error and it hasn\u0027t reported to the user,"},{"Start":"03:07.370 ","End":"03:14.070","Text":"but it has in the error console here is reported that there is an end of file exception."},{"Start":"03:14.070 ","End":"03:17.195","Text":"It\u0027s basically saying you\u0027ve tried to read off the end of the file,"},{"Start":"03:17.195 ","End":"03:20.540","Text":"and it\u0027s been reported back to the calling code as"},{"Start":"03:20.540 ","End":"03:24.665","Text":"a string code of error as we expected it to do."},{"Start":"03:24.665 ","End":"03:28.110","Text":"Now, it\u0027d be nice to have a look inside this file stock.BIN."},{"Start":"03:28.110 ","End":"03:32.735","Text":"If you were to right-click on it and say \"Open with\","},{"Start":"03:32.735 ","End":"03:35.254","Text":"and then try and get it to open with Notepad."},{"Start":"03:35.254 ","End":"03:37.805","Text":"You can do that, but it won\u0027t make a lot of sense."},{"Start":"03:37.805 ","End":"03:40.385","Text":"You can actually make out some of the text here."},{"Start":"03:40.385 ","End":"03:45.470","Text":"But, it doesn\u0027t really make sense because obviously Notepad is intended for text files."},{"Start":"03:45.470 ","End":"03:46.730","Text":"This is not a text file,"},{"Start":"03:46.730 ","End":"03:48.200","Text":"it\u0027s a binary file."},{"Start":"03:48.200 ","End":"03:50.330","Text":"If you want to work with binary files,"},{"Start":"03:50.330 ","End":"03:52.475","Text":"you need something called a Hex viewer."},{"Start":"03:52.475 ","End":"03:56.390","Text":"If you are in Windows and you have Windows PowerShell,"},{"Start":"03:56.390 ","End":"04:00.755","Text":"you can view a hex file with something called Format Hex."},{"Start":"04:00.755 ","End":"04:02.600","Text":"Just say format-hex,"},{"Start":"04:02.600 ","End":"04:06.060","Text":"and then the name of the file that you\u0027re looking into."},{"Start":"04:06.060 ","End":"04:07.440","Text":"What it will do, oops,"},{"Start":"04:07.440 ","End":"04:08.580","Text":"mine 1 is not called that,"},{"Start":"04:08.580 ","End":"04:11.045","Text":"what is the file called?"},{"Start":"04:11.045 ","End":"04:13.865","Text":"It\u0027s called stock, not stocks."},{"Start":"04:13.865 ","End":"04:17.435","Text":"That\u0027s why it\u0027s giving me an error, stock.bin."},{"Start":"04:17.435 ","End":"04:20.675","Text":"It says it\u0027s being used by another process."},{"Start":"04:20.675 ","End":"04:26.210","Text":"What I have missed out from my code was I failed to close the file"},{"Start":"04:26.210 ","End":"04:29.120","Text":"and it\u0027s still open and it\u0027s exactly what you\u0027d see"},{"Start":"04:29.120 ","End":"04:32.200","Text":"because this environment still has the file open."},{"Start":"04:32.200 ","End":"04:34.680","Text":"What I can do is I can shut a virtual machine down,"},{"Start":"04:34.680 ","End":"04:36.605","Text":"reset it, clicking on there."},{"Start":"04:36.605 ","End":"04:39.335","Text":"If I now try and view the file,"},{"Start":"04:39.335 ","End":"04:40.685","Text":"it does show me it."},{"Start":"04:40.685 ","End":"04:42.050","Text":"It\u0027s just something to bear in mind,"},{"Start":"04:42.050 ","End":"04:45.260","Text":"if you do fail to close the file,"},{"Start":"04:45.260 ","End":"04:49.040","Text":"the system may still have it open and other programs are not able to access it."},{"Start":"04:49.040 ","End":"04:51.470","Text":"It\u0027s actually quite a useful error in some ways."},{"Start":"04:51.470 ","End":"04:53.450","Text":"Let\u0027s have a look at the file itself now."},{"Start":"04:53.450 ","End":"04:55.940","Text":"We\u0027re seeing a very strange hex display here."},{"Start":"04:55.940 ","End":"04:59.690","Text":"What we\u0027re seeing is the first 16 bytes of the file on that line,"},{"Start":"04:59.690 ","End":"05:02.690","Text":"the next 16 bytes on that line, and so on."},{"Start":"05:02.690 ","End":"05:07.730","Text":"I deliberately chose this data to be exactly 16."},{"Start":"05:07.730 ","End":"05:09.245","Text":"We can see on a line,"},{"Start":"05:09.245 ","End":"05:15.645","Text":"a string uses 1 byte per character for a normal ASCII character,"},{"Start":"05:15.645 ","End":"05:20.205","Text":"but it always has 2 bytes at the beginning to tell you how long the string is."},{"Start":"05:20.205 ","End":"05:23.009","Text":"I, the letter I is 49,"},{"Start":"05:23.009 ","End":"05:26.910","Text":"B is 42, M is 4D in hex."},{"Start":"05:26.910 ","End":"05:29.630","Text":"Then spaces are 32 decimal,"},{"Start":"05:29.630 ","End":"05:30.830","Text":"which are 20 in hex."},{"Start":"05:30.830 ","End":"05:34.010","Text":"You\u0027re seeing 3 spaces here. Here\u0027s our padding."},{"Start":"05:34.010 ","End":"05:35.420","Text":"We\u0027ve got IBM here,"},{"Start":"05:35.420 ","End":"05:38.375","Text":"3 characters and then 3 spaces there,"},{"Start":"05:38.375 ","End":"05:43.385","Text":"another 3 characters plus 2 bytes at the beginning that says how long the string is."},{"Start":"05:43.385 ","End":"05:46.070","Text":"It\u0027s basically 6 bytes long."},{"Start":"05:46.070 ","End":"05:48.950","Text":"That\u0027s why we\u0027re using 8 bytes."},{"Start":"05:48.950 ","End":"05:55.215","Text":"All of these first 8 bytes here for the name of the share stock."},{"Start":"05:55.215 ","End":"05:59.115","Text":"Then the next 4 bytes are an integer,"},{"Start":"05:59.115 ","End":"06:03.210","Text":"which is the quantity that we have,"},{"Start":"06:03.210 ","End":"06:08.535","Text":"so 64 in hexadecimal in decimal is 100,"},{"Start":"06:08.535 ","End":"06:10.710","Text":"so we had a quantity of 100."},{"Start":"06:10.710 ","End":"06:15.500","Text":"Then this last number here is a floating point representation"},{"Start":"06:15.500 ","End":"06:21.005","Text":"of whatever the price was, 133.91 for IBM."},{"Start":"06:21.005 ","End":"06:27.890","Text":"You can see that that whole first 16 bytes is the first share that we put in."},{"Start":"06:27.890 ","End":"06:33.350","Text":"It\u0027s a mixture of text and an integer and a floating point number."},{"Start":"06:33.350 ","End":"06:35.120","Text":"The second 1 is exactly the same."},{"Start":"06:35.120 ","End":"06:37.865","Text":"You\u0027ll see here, the string is 6 long."},{"Start":"06:37.865 ","End":"06:39.680","Text":"We\u0027ve got JET2,"},{"Start":"06:39.680 ","End":"06:42.170","Text":"which takes up exactly 6 characters,"},{"Start":"06:42.170 ","End":"06:45.383","Text":"so there\u0027s no JET2.L,"},{"Start":"06:45.383 ","End":"06:48.305","Text":"and so there is no need for any padding."},{"Start":"06:48.305 ","End":"06:50.855","Text":"There wasn\u0027t for the next 1, GLEN.L."},{"Start":"06:50.855 ","End":"06:52.310","Text":"There was for this 1 here."},{"Start":"06:52.310 ","End":"06:55.870","Text":"You\u0027ll see there\u0027s a padding character over 20 there."},{"Start":"06:55.870 ","End":"06:58.530","Text":"There wasn\u0027t any for that last 1."},{"Start":"06:58.530 ","End":"07:02.415","Text":"The only ones that needed any padding was the first 1 and this 1 here."},{"Start":"07:02.415 ","End":"07:05.360","Text":"All the rest happened to be exactly 6 long,"},{"Start":"07:05.360 ","End":"07:07.340","Text":"including the dot anyway."},{"Start":"07:07.340 ","End":"07:09.560","Text":"They all have slightly different quantities."},{"Start":"07:09.560 ","End":"07:13.025","Text":"They all have different prices and we see them stored there."},{"Start":"07:13.025 ","End":"07:16.100","Text":"This is really getting to the crux of what"},{"Start":"07:16.100 ","End":"07:19.640","Text":"the difference is between a binary file and a text file."},{"Start":"07:19.640 ","End":"07:23.610","Text":"Text file is intended to be read by humans. A binary file isn\u0027t."},{"Start":"07:23.610 ","End":"07:27.940","Text":"It\u0027s intended to be read by other programs generally or machines."},{"Start":"07:27.940 ","End":"07:33.050","Text":"We can look at the binary file by looking at it in a hex editor or in"},{"Start":"07:33.050 ","End":"07:35.570","Text":"a hex view like this 1 and we will"},{"Start":"07:35.570 ","End":"07:38.375","Text":"be able to make some sense of it if we know the structure."},{"Start":"07:38.375 ","End":"07:40.190","Text":"The structure was really important here,"},{"Start":"07:40.190 ","End":"07:43.040","Text":"that\u0027s why we multiplied each stock by 16,"},{"Start":"07:43.040 ","End":"07:48.870","Text":"to seek to the next position in the file and then the next position and so on and so on."},{"Start":"07:48.870 ","End":"07:52.670","Text":"We were able to locate immediately using random access,"},{"Start":"07:52.670 ","End":"07:55.940","Text":"whichever line we were interested in without having to read"},{"Start":"07:55.940 ","End":"07:59.885","Text":"all the previous ones as we\u0027ve done with text files up until now."},{"Start":"07:59.885 ","End":"08:03.253","Text":"That\u0027s it for the testing part of this exercise,"},{"Start":"08:03.253 ","End":"08:04.580","Text":"and the exercise itself."},{"Start":"08:04.580 ","End":"08:06.930","Text":"Thank you very much for watching."}],"ID":29813}],"Thumbnail":null,"ID":280619},{"Name":"Recursion","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Recursion Basic Concepts","Duration":"8m ","ChapterTopicVideoID":28398,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.065","Text":"Hello, welcome to this video on recursion by the end of this section,"},{"Start":"00:04.065 ","End":"00:07.109","Text":"you\u0027ll be able to explain the core concepts of recursion."},{"Start":"00:07.109 ","End":"00:10.410","Text":"Implement recursion within a typical programming language,"},{"Start":"00:10.410 ","End":"00:16.545","Text":"and evaluate circumstances under which recursion or iteration are more appropriate."},{"Start":"00:16.545 ","End":"00:20.220","Text":"All around us in the natural world are examples of entities that seem to be"},{"Start":"00:20.220 ","End":"00:24.780","Text":"repeating smaller patterns to make a larger overall entity."},{"Start":"00:24.780 ","End":"00:28.260","Text":"Ice crystals have smaller crystals that all appear"},{"Start":"00:28.260 ","End":"00:33.480","Text":"similar and combine together to produce the overall ice crystal shape."},{"Start":"00:33.480 ","End":"00:38.440","Text":"Here we can see a smaller shape interleaved with a large shape,"},{"Start":"00:38.440 ","End":"00:44.030","Text":"repeating in a circular fashion around the center of the crystal."},{"Start":"00:44.030 ","End":"00:49.480","Text":"These smaller repeated parts of the crystal results in the final crystal."},{"Start":"00:49.480 ","End":"00:52.130","Text":"A particular type of broccoli is made up of"},{"Start":"00:52.130 ","End":"00:55.445","Text":"smaller pieces that are all the same basic shape."},{"Start":"00:55.445 ","End":"01:00.060","Text":"The small pieces make larger pieces and"},{"Start":"01:00.060 ","End":"01:05.435","Text":"these larger pieces again repeat around the central stem to make up the overall broccoli."},{"Start":"01:05.435 ","End":"01:09.140","Text":"Even man-made entities can be made up of a basic piece,"},{"Start":"01:09.140 ","End":"01:12.050","Text":"which is a smaller version of the overall entity."},{"Start":"01:12.050 ","End":"01:14.620","Text":"A classic example is the Russian doll,"},{"Start":"01:14.620 ","End":"01:21.620","Text":"each of which can be opened and each one contains another smaller doll."},{"Start":"01:21.620 ","End":"01:26.090","Text":"The process repeating until we reach a point where"},{"Start":"01:26.090 ","End":"01:30.365","Text":"the tiniest doll can\u0027t be opened and so we can go no further."},{"Start":"01:30.365 ","End":"01:34.009","Text":"These examples all illustrate the basic concept of recursion,"},{"Start":"01:34.009 ","End":"01:37.370","Text":"where something is defined in terms of itself."},{"Start":"01:37.370 ","End":"01:44.450","Text":"In this case, each doll refers to another smaller doll until we reach the last one."},{"Start":"01:44.450 ","End":"01:48.530","Text":"A more formal definition of recursion would be"},{"Start":"01:48.530 ","End":"01:51.950","Text":"the determination of a succession of elements by"},{"Start":"01:51.950 ","End":"01:55.610","Text":"operation on 1 or more proceeding elements."},{"Start":"01:55.610 ","End":"01:57.920","Text":"We can see how that fits with the snow crystal,"},{"Start":"01:57.920 ","End":"02:00.205","Text":"the broccoli, and the Russian doll."},{"Start":"02:00.205 ","End":"02:04.460","Text":"Recursion turns out to be a technique that can be applied to computer programs,"},{"Start":"02:04.460 ","End":"02:07.880","Text":"specifically the idea of the recursive function,"},{"Start":"02:07.880 ","End":"02:13.865","Text":"a function definition which refers to itself in the function body."},{"Start":"02:13.865 ","End":"02:16.670","Text":"Let\u0027s look at one of the most basic examples of this."},{"Start":"02:16.670 ","End":"02:20.170","Text":"A recursive function to calculate factorials."},{"Start":"02:20.170 ","End":"02:24.993","Text":"3 factorial means the result of multiplying 3 by 2 by 1,"},{"Start":"02:24.993 ","End":"02:32.285","Text":"4 factorial means the result of multiplying 4 by 3 by 2 by 1, and so on."},{"Start":"02:32.285 ","End":"02:37.775","Text":"We could define a recursive function to calculate factorials like this."},{"Start":"02:37.775 ","End":"02:43.655","Text":"This looks perfectly normal at first glance until you think about this line here."},{"Start":"02:43.655 ","End":"02:48.680","Text":"The function is actually calling itself, or be it,"},{"Start":"02:48.680 ","End":"02:52.520","Text":"with a slightly different argument of n minus 1 rather"},{"Start":"02:52.520 ","End":"02:56.630","Text":"than n. This might seem slightly baffling at first."},{"Start":"02:56.630 ","End":"03:01.790","Text":"How can we define something which it refers to itself in the definition?"},{"Start":"03:01.790 ","End":"03:05.495","Text":"If we look at the line which is recursively calling the function,"},{"Start":"03:05.495 ","End":"03:14.405","Text":"we return a value which is n times the value we get back from calling the function again."},{"Start":"03:14.405 ","End":"03:17.375","Text":"But with an argument of n minus 1."},{"Start":"03:17.375 ","End":"03:21.810","Text":"The 3, we would expect this to happen."},{"Start":"03:21.850 ","End":"03:28.040","Text":"Then, because we\u0027re calling factorial again and with an argument of 2,"},{"Start":"03:28.040 ","End":"03:30.180","Text":"we can\u0027t immediately return a value,"},{"Start":"03:30.180 ","End":"03:34.040","Text":"we first need to get back a value from here."},{"Start":"03:34.040 ","End":"03:38.225","Text":"We call factorial again with the argument 2."},{"Start":"03:38.225 ","End":"03:40.490","Text":"When we get to the recursive line again,"},{"Start":"03:40.490 ","End":"03:43.730","Text":"we still can\u0027t return a value because we"},{"Start":"03:43.730 ","End":"03:47.800","Text":"need to make another recursive call to factorial here."},{"Start":"03:47.800 ","End":"03:53.270","Text":"However this time, we don\u0027t need to make any more recursive calls because we hit"},{"Start":"03:53.270 ","End":"03:55.730","Text":"a part of the function definition where we"},{"Start":"03:55.730 ","End":"03:58.835","Text":"check whether n is equal to 1 in this case it is."},{"Start":"03:58.835 ","End":"04:03.695","Text":"No further function calls are made and the Number 1 is returned instead."},{"Start":"04:03.695 ","End":"04:05.990","Text":"Finally, we have a return value,"},{"Start":"04:05.990 ","End":"04:10.205","Text":"which in turn can be used as a return value for the previous call here."},{"Start":"04:10.205 ","End":"04:14.060","Text":"We can now finally return a value to the original calling code."},{"Start":"04:14.060 ","End":"04:17.750","Text":"The detail of how it works involves understanding how parameters and"},{"Start":"04:17.750 ","End":"04:21.950","Text":"return values make use of a data structure called a stack."},{"Start":"04:21.950 ","End":"04:23.900","Text":"When a function calls another function,"},{"Start":"04:23.900 ","End":"04:27.335","Text":"it saves its current state onto the stack."},{"Start":"04:27.335 ","End":"04:29.465","Text":"When the other function returns,"},{"Start":"04:29.465 ","End":"04:33.065","Text":"restores its previous state from the stack."},{"Start":"04:33.065 ","End":"04:34.760","Text":"With a recursive routine,"},{"Start":"04:34.760 ","End":"04:37.025","Text":"the function\u0027s calling itself,"},{"Start":"04:37.025 ","End":"04:39.560","Text":"but the stack is being used in the same way as it would"},{"Start":"04:39.560 ","End":"04:43.175","Text":"be for any function calling another function."},{"Start":"04:43.175 ","End":"04:46.235","Text":"Say we wanted to find 3 factorial."},{"Start":"04:46.235 ","End":"04:48.320","Text":"When we make the initial call to a function,"},{"Start":"04:48.320 ","End":"04:54.980","Text":"the argument value of 3 is passed into the parameter n because n is not 1,"},{"Start":"04:54.980 ","End":"04:57.155","Text":"we skip the if block."},{"Start":"04:57.155 ","End":"05:02.030","Text":"Instead, return n times factorial n minus 1."},{"Start":"05:02.030 ","End":"05:04.765","Text":"Because we have to make another call to the function,"},{"Start":"05:04.765 ","End":"05:08.030","Text":"we save the current state onto the stack."},{"Start":"05:08.030 ","End":"05:10.670","Text":"In this case, the value of n is the only thing that needs"},{"Start":"05:10.670 ","End":"05:14.225","Text":"saving and so 3 appears on the stack."},{"Start":"05:14.225 ","End":"05:22.235","Text":"We then make the call to the factorial function again but with a value of 3 minus 1."},{"Start":"05:22.235 ","End":"05:25.340","Text":"We enter the factorial function as before,"},{"Start":"05:25.340 ","End":"05:27.615","Text":"but this time the argument is 2."},{"Start":"05:27.615 ","End":"05:31.195","Text":"Parameter n takes on the value 2,"},{"Start":"05:31.195 ","End":"05:34.110","Text":"which again is not 1."},{"Start":"05:34.110 ","End":"05:36.315","Text":"The else block runs."},{"Start":"05:36.315 ","End":"05:40.420","Text":"Once again, we make a recursive call to factorial,"},{"Start":"05:40.420 ","End":"05:45.575","Text":"but we put a current value of n onto the stack first here."},{"Start":"05:45.575 ","End":"05:52.210","Text":"Now we\u0027ve made the call with 1 as the argument and so parameter n takes on the value 1."},{"Start":"05:52.210 ","End":"05:55.320","Text":"This time n is equal to 1,"},{"Start":"05:55.320 ","End":"05:58.730","Text":"so we can return an actual value this time"},{"Start":"05:58.730 ","End":"06:03.350","Text":"1 and this return value also goes on the stack."},{"Start":"06:03.350 ","End":"06:07.360","Text":"Now we can use a stack to restore the previous values of n,"},{"Start":"06:07.360 ","End":"06:09.755","Text":"and so return a final value."},{"Start":"06:09.755 ","End":"06:16.795","Text":"First, we multiply 2 that restored previous value of n by the return value 1,"},{"Start":"06:16.795 ","End":"06:19.135","Text":"which of course gives us 2."},{"Start":"06:19.135 ","End":"06:22.040","Text":"The result goes back on the stack again."},{"Start":"06:22.040 ","End":"06:25.385","Text":"Now we can multiply that value by 3,"},{"Start":"06:25.385 ","End":"06:30.590","Text":"the restored first value of n before we made any recursive calls."},{"Start":"06:30.590 ","End":"06:33.950","Text":"We are now left with our final return value on"},{"Start":"06:33.950 ","End":"06:37.625","Text":"the stack which we can return to the calling code."},{"Start":"06:37.625 ","End":"06:43.325","Text":"This very simple program illustrates the basic principle behind recursive functions."},{"Start":"06:43.325 ","End":"06:47.435","Text":"In particular, 2 different parts of execution that the code could take one"},{"Start":"06:47.435 ","End":"06:52.745","Text":"which we call the recursive case and one which we call the base case."},{"Start":"06:52.745 ","End":"06:57.545","Text":"The base case is the path of execution where no further recursive calls are made."},{"Start":"06:57.545 ","End":"07:01.055","Text":"The recursive case is the path of execution when"},{"Start":"07:01.055 ","End":"07:04.945","Text":"the function calls itself with a new argument."},{"Start":"07:04.945 ","End":"07:06.680","Text":"In our factorial example,"},{"Start":"07:06.680 ","End":"07:10.490","Text":"the base case was executed when the argument passed in was 1,"},{"Start":"07:10.490 ","End":"07:13.465","Text":"resulting in the Value 1 being returned."},{"Start":"07:13.465 ","End":"07:15.510","Text":"For any other value of argument,"},{"Start":"07:15.510 ","End":"07:17.780","Text":"the recursive case was called where"},{"Start":"07:17.780 ","End":"07:20.975","Text":"the function code itself with an argument of n minus 1."},{"Start":"07:20.975 ","End":"07:25.910","Text":"The final result is only returned once all the function calls complete."},{"Start":"07:25.910 ","End":"07:29.570","Text":"Each function call feeds a value forward into"},{"Start":"07:29.570 ","End":"07:34.070","Text":"the next call and this repeats until the base case."},{"Start":"07:34.070 ","End":"07:39.590","Text":"The base case is the key to the correct functioning of a recursive function."},{"Start":"07:39.590 ","End":"07:42.755","Text":"Without it, the function would run forever,"},{"Start":"07:42.755 ","End":"07:47.900","Text":"or more accurately it would keep running until memory runs out."},{"Start":"07:47.900 ","End":"07:53.240","Text":"We\u0027ll pause there and we\u0027ll be back shortly to have a look at a couple of examples of"},{"Start":"07:53.240 ","End":"07:56.000","Text":"recursion and the difference between"},{"Start":"07:56.000 ","End":"08:00.570","Text":"recursion and decomposition. I\u0027ll see you shortly for that one."}],"ID":29814},{"Watched":false,"Name":"Divide and Conquer","Duration":"8m 10s","ChapterTopicVideoID":28399,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.650","Text":"Hello, welcome back."},{"Start":"00:01.650 ","End":"00:05.520","Text":"In the previous video, we introduced the idea of recursion."},{"Start":"00:05.520 ","End":"00:08.070","Text":"We\u0027ll now look at some further examples of"},{"Start":"00:08.070 ","End":"00:11.400","Text":"where recursion might benefit us as programmers."},{"Start":"00:11.400 ","End":"00:13.080","Text":"Lost on the surface,"},{"Start":"00:13.080 ","End":"00:16.470","Text":"recursion might seem similar to the idea of decomposition of"},{"Start":"00:16.470 ","End":"00:20.235","Text":"breaking down a problem into smaller problems that can be solved."},{"Start":"00:20.235 ","End":"00:26.955","Text":"Recursion is different because each subproblem is of the same type as the others."},{"Start":"00:26.955 ","End":"00:32.430","Text":"For this reason, recursion lends itself to a technique known as divide and conquer."},{"Start":"00:32.430 ","End":"00:34.875","Text":"For example, in string parsing,"},{"Start":"00:34.875 ","End":"00:38.369","Text":"let\u0027s say we wanted to know if a particular string was a palindrome,"},{"Start":"00:38.369 ","End":"00:42.510","Text":"ie, reads the same way backwards and forwards."},{"Start":"00:42.510 ","End":"00:47.105","Text":"We want to return true or false depending on whether it is a palindrome."},{"Start":"00:47.105 ","End":"00:53.120","Text":"We can do this using recursion on successively smaller parts of the original string."},{"Start":"00:53.120 ","End":"00:56.300","Text":"It\u0027s the same problem being solved each time,"},{"Start":"00:56.300 ","End":"00:59.460","Text":"but with a slightly different input."},{"Start":"00:59.680 ","End":"01:05.390","Text":"After removing any spaces and other symbols and converting all the letters to lowercase,"},{"Start":"01:05.390 ","End":"01:11.270","Text":"we can compare the first and last characters to see if they\u0027re the same."},{"Start":"01:11.270 ","End":"01:14.330","Text":"If at any point they are not,"},{"Start":"01:14.330 ","End":"01:16.655","Text":"we immediately return false,"},{"Start":"01:16.655 ","End":"01:19.070","Text":"and therefore the function ends."},{"Start":"01:19.070 ","End":"01:23.105","Text":"But whilst the first and last characters are the same,"},{"Start":"01:23.105 ","End":"01:29.470","Text":"we recursively call the function with the first and last characters removed."},{"Start":"01:29.470 ","End":"01:35.675","Text":"We keep doing it until we have exactly one character or zero characters."},{"Start":"01:35.675 ","End":"01:37.235","Text":"If we reach that point,"},{"Start":"01:37.235 ","End":"01:39.965","Text":"we\u0027ve determined that the first and last character of"},{"Start":"01:39.965 ","End":"01:43.980","Text":"every substring we checked was the same."},{"Start":"01:47.320 ","End":"01:50.660","Text":"In this example, we have no more characters,"},{"Start":"01:50.660 ","End":"01:52.370","Text":"we\u0027ve reached the base case,"},{"Start":"01:52.370 ","End":"01:54.215","Text":"and we can return true,"},{"Start":"01:54.215 ","End":"01:57.410","Text":"which propagates back to the calling code and we\u0027ve"},{"Start":"01:57.410 ","End":"02:01.080","Text":"confirmed that the string is a palindrome."},{"Start":"02:01.080 ","End":"02:03.575","Text":"An example of divide and conquer,"},{"Start":"02:03.575 ","End":"02:09.290","Text":"where we gradually reduce the size of the string to 1 or 0 characters."},{"Start":"02:09.290 ","End":"02:14.230","Text":"Another example of divide and conquer using recursion would be a merge sort."},{"Start":"02:14.230 ","End":"02:18.875","Text":"In a merge sort an unsorted list of items, say integers,"},{"Start":"02:18.875 ","End":"02:24.770","Text":"is sorted by first breaking down the list into individual items recursively and then"},{"Start":"02:24.770 ","End":"02:31.055","Text":"merging back together the individual parts which are sorted as they are merged."},{"Start":"02:31.055 ","End":"02:34.430","Text":"Each time we break down the list into two halves,"},{"Start":"02:34.430 ","End":"02:37.295","Text":"a left half and a right half."},{"Start":"02:37.295 ","End":"02:39.980","Text":"Then we split those lists in half again,"},{"Start":"02:39.980 ","End":"02:42.560","Text":"once again with a left half and a right half,"},{"Start":"02:42.560 ","End":"02:45.590","Text":"and we keep doing that until we can\u0027t split any"},{"Start":"02:45.590 ","End":"02:50.945","Text":"further and we\u0027ve reached the base case of the recursive breaking down."},{"Start":"02:50.945 ","End":"02:54.680","Text":"Then we recursively merge back from the bottom,"},{"Start":"02:54.680 ","End":"02:57.905","Text":"swapping the lower of the leftmost values,"},{"Start":"02:57.905 ","End":"03:01.355","Text":"left side first until we can\u0027t go any further,"},{"Start":"03:01.355 ","End":"03:04.115","Text":"and then the same on the right side."},{"Start":"03:04.115 ","End":"03:06.815","Text":"Here we start with two single values,"},{"Start":"03:06.815 ","End":"03:09.845","Text":"87 on the left and 45 on the right."},{"Start":"03:09.845 ","End":"03:12.500","Text":"Because 45 is lower than 87,"},{"Start":"03:12.500 ","End":"03:16.555","Text":"we swap them when we merge these two halves back together."},{"Start":"03:16.555 ","End":"03:20.925","Text":"Then 12 and 89 merge back together in that order."},{"Start":"03:20.925 ","End":"03:22.970","Text":"We do the same again,"},{"Start":"03:22.970 ","End":"03:26.090","Text":"putting whatever is the lowest of the left-most value in"},{"Start":"03:26.090 ","End":"03:30.380","Text":"each half into the leftmost slot of the merged pair."},{"Start":"03:30.380 ","End":"03:33.140","Text":"Because 12 is less than 45,"},{"Start":"03:33.140 ","End":"03:34.535","Text":"it will go first,"},{"Start":"03:34.535 ","End":"03:40.955","Text":"and because 45 is less than the only remaining parts of the right-hand off 89,"},{"Start":"03:40.955 ","End":"03:42.815","Text":"it will go in next."},{"Start":"03:42.815 ","End":"03:44.945","Text":"Now, only two values are left,"},{"Start":"03:44.945 ","End":"03:46.790","Text":"the lowest goes in first,"},{"Start":"03:46.790 ","End":"03:48.305","Text":"which is 87,"},{"Start":"03:48.305 ","End":"03:51.515","Text":"and then all that\u0027s left goes on the end."},{"Start":"03:51.515 ","End":"03:53.360","Text":"Now we can\u0027t go any further on"},{"Start":"03:53.360 ","End":"03:57.870","Text":"this left-hand branch until we deal with the right-hand one."},{"Start":"04:01.820 ","End":"04:05.020","Text":"Now we can merge the final two halves back"},{"Start":"04:05.020 ","End":"04:09.070","Text":"together once more following exactly the same process as before,"},{"Start":"04:09.070 ","End":"04:13.252","Text":"just with different sized halves than before."},{"Start":"04:13.252 ","End":"04:15.880","Text":"Comparing the left-most value of each half"},{"Start":"04:15.880 ","End":"04:19.240","Text":"and putting the lower value into the merged list."},{"Start":"04:19.240 ","End":"04:23.380","Text":"We only have to inspect two values each time and put the lower of"},{"Start":"04:23.380 ","End":"04:28.530","Text":"the two into the merged list,"},{"Start":"04:28.530 ","End":"04:31.720","Text":"until all the items are placed back and now the whole of"},{"Start":"04:31.720 ","End":"04:36.235","Text":"the original list has been recursively broken down and recursively merged,"},{"Start":"04:36.235 ","End":"04:38.020","Text":"but is now sorted."},{"Start":"04:38.020 ","End":"04:40.630","Text":"A good example of the power of recursion,"},{"Start":"04:40.630 ","End":"04:45.320","Text":"executing the same code repeatedly on different instances of"},{"Start":"04:45.320 ","End":"04:51.980","Text":"the data and producing an overall result at the end when all the results are combined."},{"Start":"04:51.980 ","End":"04:55.820","Text":"You may have noticed that some of these problems could have been completed without"},{"Start":"04:55.820 ","End":"04:59.890","Text":"using recursion and using iteration instead, and you\u0027re right."},{"Start":"04:59.890 ","End":"05:01.910","Text":"A simple so-called linear"},{"Start":"05:01.910 ","End":"05:06.590","Text":"recursive routine usually follows the form where the base case is"},{"Start":"05:06.590 ","End":"05:10.040","Text":"equivalent to the terminating condition for a loop and"},{"Start":"05:10.040 ","End":"05:14.165","Text":"the code inside the loop is the recursive step."},{"Start":"05:14.165 ","End":"05:17.660","Text":"It\u0027s often quite straightforward to implement an algorithm with"},{"Start":"05:17.660 ","End":"05:21.620","Text":"a loop instead of using a recursive function call."},{"Start":"05:21.620 ","End":"05:24.215","Text":"It might then be a fair question to ask,"},{"Start":"05:24.215 ","End":"05:28.025","Text":"when would you use iteration and when would you use recursion?"},{"Start":"05:28.025 ","End":"05:31.520","Text":"Recursion depth is one potential factor, ie,"},{"Start":"05:31.520 ","End":"05:35.945","Text":"how many times might a recursive function call itself?"},{"Start":"05:35.945 ","End":"05:38.945","Text":"Remember, each time a function calls another function,"},{"Start":"05:38.945 ","End":"05:43.685","Text":"we need to restore the function state when the called function returns."},{"Start":"05:43.685 ","End":"05:49.430","Text":"We do this by putting a bunch of items on the stack in what\u0027s known as a stack frame."},{"Start":"05:49.430 ","End":"05:52.130","Text":"This includes the values of local variables and"},{"Start":"05:52.130 ","End":"05:55.325","Text":"parameters at the time the new function call is made."},{"Start":"05:55.325 ","End":"05:58.310","Text":"These all have to be restored from the stack once"},{"Start":"05:58.310 ","End":"06:02.920","Text":"the recursive function call returns having hit a base case."},{"Start":"06:02.920 ","End":"06:06.755","Text":"With many levels deep of recursive function calling,"},{"Start":"06:06.755 ","End":"06:10.520","Text":"this could result in lots of dates ending up on the stack and it\u0027s"},{"Start":"06:10.520 ","End":"06:15.425","Text":"possible for the stack to eventually become full causing a runtime error."},{"Start":"06:15.425 ","End":"06:18.920","Text":"Recursion depth is one factor to be aware of"},{"Start":"06:18.920 ","End":"06:22.715","Text":"when choosing between recursion or iteration."},{"Start":"06:22.715 ","End":"06:25.490","Text":"But there\u0027s no arguing that recursive code is usually"},{"Start":"06:25.490 ","End":"06:28.640","Text":"much more elegant than an iterative equivalent."},{"Start":"06:28.640 ","End":"06:30.635","Text":"Once you understand recursion,"},{"Start":"06:30.635 ","End":"06:35.200","Text":"that can make recursive code much easier to read and follow."},{"Start":"06:35.200 ","End":"06:38.865","Text":"It does make it more maintainable therefore."},{"Start":"06:38.865 ","End":"06:40.940","Text":"Having said that, if you were developing"},{"Start":"06:40.940 ","End":"06:44.975","Text":"a novel algorithm and not just implementing a standard one,"},{"Start":"06:44.975 ","End":"06:48.485","Text":"recursive code can be tricky to trace when debugging."},{"Start":"06:48.485 ","End":"06:51.148","Text":"We saw the values being swapped in the merge sort,"},{"Start":"06:51.148 ","End":"06:54.131","Text":"was quite a complicated sequence of operations"},{"Start":"06:54.131 ","End":"06:57.476","Text":"even though what was being done was completely consistent."},{"Start":"06:57.476 ","End":"06:59.600","Text":"You wouldn\u0027t be able to easily spot how"},{"Start":"06:59.600 ","End":"07:02.645","Text":"the values would change through different recursive calls."},{"Start":"07:02.645 ","End":"07:07.549","Text":"This is arguably much easier to trace when code is using loops rather than recursion."},{"Start":"07:07.549 ","End":"07:11.480","Text":"For a well-understood standard recursive algorithm like merge sort,"},{"Start":"07:11.480 ","End":"07:14.120","Text":"it\u0027s not an issue because the design\u0027s already been proven to"},{"Start":"07:14.120 ","End":"07:17.420","Text":"work and implemented in various programming languages."},{"Start":"07:17.420 ","End":"07:20.761","Text":"For a novel algorithm that you\u0027re developing,"},{"Start":"07:20.761 ","End":"07:25.350","Text":"recursion can make development and debugging tricky."},{"Start":"07:25.350 ","End":"07:29.570","Text":"But some algorithms or data structures due to their very nature lend"},{"Start":"07:29.570 ","End":"07:34.220","Text":"themselves to recursive techniques in a way that iteration would not."},{"Start":"07:34.220 ","End":"07:39.140","Text":"The merge sort we saw was one of them and the binary search algorithms another."},{"Start":"07:39.140 ","End":"07:41.810","Text":"The divide and conquer strategy in general would be"},{"Start":"07:41.810 ","End":"07:45.650","Text":"something that recursion would be well-suited to."},{"Start":"07:45.650 ","End":"07:47.930","Text":"That\u0027s it for this video."},{"Start":"07:47.930 ","End":"07:51.769","Text":"In this section, we learned how to explain the core concepts of recursion,"},{"Start":"07:51.769 ","End":"07:54.800","Text":"to implement recursion within a typical programming language,"},{"Start":"07:54.800 ","End":"08:01.235","Text":"and to evaluate circumstances under which recursion or iteration are more appropriate."},{"Start":"08:01.235 ","End":"08:06.890","Text":"Please complete all of the exercises for this topic to consolidate your understanding,"},{"Start":"08:06.890 ","End":"08:11.020","Text":"and I\u0027ll see you soon. Thanks for watching."}],"ID":29815},{"Watched":false,"Name":"Exercise 1","Duration":"5m 14s","ChapterTopicVideoID":28400,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.310","Text":"Hello and welcome to this first exercise on recursion."},{"Start":"00:03.310 ","End":"00:05.560","Text":"Initially, we\u0027ve been asked to create a new project"},{"Start":"00:05.560 ","End":"00:07.975","Text":"and within a class called RecursionApp,"},{"Start":"00:07.975 ","End":"00:11.380","Text":"we then create a method called factorial in"},{"Start":"00:11.380 ","End":"00:15.445","Text":"which we take an int parameter and return an int."},{"Start":"00:15.445 ","End":"00:18.880","Text":"In part b we\u0027re then asked that the method should use"},{"Start":"00:18.880 ","End":"00:22.645","Text":"the following algorithm to return the factorial value."},{"Start":"00:22.645 ","End":"00:28.570","Text":"If n equals 1 then return 1 else return factorial n"},{"Start":"00:28.570 ","End":"00:31.290","Text":"times n minus 1 in brackets end"},{"Start":"00:31.290 ","End":"00:35.725","Text":"if in part c we are then asked to test the method with the following values."},{"Start":"00:35.725 ","End":"00:37.495","Text":"1 should return 1,"},{"Start":"00:37.495 ","End":"00:39.505","Text":"3 should return 6,"},{"Start":"00:39.505 ","End":"00:43.030","Text":"and 5 should return 120."},{"Start":"00:43.030 ","End":"00:47.040","Text":"I\u0027ve created our class RecursionApp and then we\u0027ll"},{"Start":"00:47.040 ","End":"00:50.680","Text":"go ahead and do as we\u0027ve been told in part 1a to create"},{"Start":"00:50.680 ","End":"00:54.490","Text":"a method which returns an int called"},{"Start":"00:54.490 ","End":"00:59.710","Text":"factorial and accepts 1 parameter which is also an int let\u0027s call"},{"Start":"00:59.710 ","End":"01:04.060","Text":"it n. Inside the method then we\u0027re told in part"},{"Start":"01:04.060 ","End":"01:09.310","Text":"1b the method uses this algorithm to return the factorial value."},{"Start":"01:09.310 ","End":"01:15.790","Text":"If n is 1 we return 1 otherwise we return factorial n times n minus 1."},{"Start":"01:15.790 ","End":"01:20.275","Text":"The base case here is when we have a value of"},{"Start":"01:20.275 ","End":"01:27.070","Text":"1 we can simply return the value 1."},{"Start":"01:27.070 ","End":"01:31.490","Text":"But the recursive case is interesting bit."},{"Start":"01:31.490 ","End":"01:39.125","Text":"For any value other than 1 of n we need to return a value."},{"Start":"01:39.125 ","End":"01:44.180","Text":"But what we return is n times n minus"},{"Start":"01:44.180 ","End":"01:50.530","Text":"1 which we pass on to a factorial function."},{"Start":"01:51.380 ","End":"01:56.420","Text":"This is the recursive bit here where we\u0027re calling the factorial function again"},{"Start":"01:56.420 ","End":"02:00.860","Text":"very strange looking code when you aren\u0027t used to recursion."},{"Start":"02:00.860 ","End":"02:04.730","Text":"But actually, that is our program that will do what we need to"},{"Start":"02:04.730 ","End":"02:10.163","Text":"do and what we\u0027ll see if it does and we\u0027ll just talk through the results in a moment,"},{"Start":"02:10.163 ","End":"02:14.760","Text":"so let\u0027s try it with 1 which we expect to return 1."},{"Start":"02:14.920 ","End":"02:18.455","Text":"It says in part c1."},{"Start":"02:18.455 ","End":"02:22.250","Text":"It does indeed return 1 because we saw"},{"Start":"02:22.250 ","End":"02:25.910","Text":"from our base case that it just returns the number 1."},{"Start":"02:25.910 ","End":"02:29.210","Text":"Let\u0027s look at something a little bit more interesting 3."},{"Start":"02:29.210 ","End":"02:35.310","Text":"3 would be 3 times 2 times 1 which should give us 6 and it does."},{"Start":"02:35.310 ","End":"02:39.660","Text":"Then let\u0027s try the final one 5 which should"},{"Start":"02:39.660 ","End":"02:44.655","Text":"return 5 times 4 times 3 times 2 times 1 which is 120."},{"Start":"02:44.655 ","End":"02:46.860","Text":"It does indeed return 120."},{"Start":"02:46.860 ","End":"02:49.515","Text":"Now almost seems like magic."},{"Start":"02:49.515 ","End":"02:55.040","Text":"Little code here and you wonder why it returns the values that it\u0027s returning."},{"Start":"02:55.040 ","End":"02:59.240","Text":"Maybe a little bit easier to see if you actually print out"},{"Start":"02:59.240 ","End":"03:04.695","Text":"the value of n each time a call is made. Let us do that."},{"Start":"03:04.695 ","End":"03:08.180","Text":"Then you get a sense of what the value of n is."},{"Start":"03:08.180 ","End":"03:11.255","Text":"Because although we put in 5 to start with,"},{"Start":"03:11.255 ","End":"03:14.550","Text":"when we call this function because n is not"},{"Start":"03:14.550 ","End":"03:17.960","Text":"equal to 1 is equal to 5 which it\u0027s going to go into this part."},{"Start":"03:17.960 ","End":"03:20.890","Text":"It will then call factorial again."},{"Start":"03:20.890 ","End":"03:24.650","Text":"It can\u0027t return this value n times factorial until its"},{"Start":"03:24.650 ","End":"03:29.045","Text":"first called factorial with this new value of n minus 1."},{"Start":"03:29.045 ","End":"03:32.630","Text":"4 the next time around it will then return"},{"Start":"03:32.630 ","End":"03:36.485","Text":"a value which gets multiplied by n but circular."},{"Start":"03:36.485 ","End":"03:42.090","Text":"It can\u0027t even do that because it has to call n again with the value 4."},{"Start":"03:42.090 ","End":"03:47.645","Text":"Then ethical again with a value of 3 and 2 and finally it will get to 1."},{"Start":"03:47.645 ","End":"03:50.105","Text":"Then it will get to the base case here."},{"Start":"03:50.105 ","End":"03:52.970","Text":"It can return a number without calling factorial"},{"Start":"03:52.970 ","End":"03:56.390","Text":"again and it will unravel and we\u0027ll finally get a result"},{"Start":"03:56.390 ","End":"04:01.050","Text":"which will be 5 times 4 times 3 times 2 times 1 just to prove"},{"Start":"04:01.050 ","End":"04:06.505","Text":"that that\u0027s just run it again with 5 and just see those values propagating through."},{"Start":"04:06.505 ","End":"04:10.910","Text":"You might get a sense of what\u0027s going on from that."},{"Start":"04:10.910 ","End":"04:15.470","Text":"Let\u0027s put in 5 and you\u0027ll see that it\u0027s returned to a 120."},{"Start":"04:15.470 ","End":"04:18.110","Text":"But how it was able to return 120"},{"Start":"04:18.110 ","End":"04:20.750","Text":"was each time the function was called it was called with"},{"Start":"04:20.750 ","End":"04:23.720","Text":"a different value of n. We started off"},{"Start":"04:23.720 ","End":"04:26.915","Text":"with the value 5 which is we were looking for the answer for."},{"Start":"04:26.915 ","End":"04:32.180","Text":"But it calls itself again without us asking it to it was part of the code."},{"Start":"04:32.180 ","End":"04:34.730","Text":"It called it with the value 4 then it"},{"Start":"04:34.730 ","End":"04:37.265","Text":"called it with a value 3 then it called it with a value 2."},{"Start":"04:37.265 ","End":"04:40.070","Text":"It actually called it with a value 1 as well."},{"Start":"04:40.070 ","End":"04:43.765","Text":"But we just never saw it printed because we didn\u0027t print anything in here."},{"Start":"04:43.765 ","End":"04:46.935","Text":"When it\u0027s called with 1 it just returns 1."},{"Start":"04:46.935 ","End":"04:50.150","Text":"In every other case, it\u0027s going to call the function again."},{"Start":"04:50.150 ","End":"04:53.360","Text":"That\u0027s why we have this recursion going on"},{"Start":"04:53.360 ","End":"04:56.885","Text":"calling itself with different values each time."},{"Start":"04:56.885 ","End":"04:59.990","Text":"That\u0027s it for the first one nice and simple code-wise"},{"Start":"04:59.990 ","End":"05:03.920","Text":"but it illustrates all the contexts we need to know for recursion."},{"Start":"05:03.920 ","End":"05:10.025","Text":"The more you look at this type of stuff the more it starts to sink in and make sense."},{"Start":"05:10.025 ","End":"05:12.230","Text":"That\u0027s the first straightforward exercise."},{"Start":"05:12.230 ","End":"05:15.600","Text":"We\u0027ll be back soon for the next one. See you then."}],"ID":29816},{"Watched":false,"Name":"Exercise 2","Duration":"8m 36s","ChapterTopicVideoID":28401,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.400","Text":"Hello again. In this next exercise,"},{"Start":"00:02.400 ","End":"00:05.415","Text":"we\u0027ve been asked to create a method called Fibonacci recursive."},{"Start":"00:05.415 ","End":"00:09.570","Text":"It accepts an int argument n and returns an int,"},{"Start":"00:09.570 ","End":"00:13.139","Text":"which is the nth term in the Fibonacci sequence."},{"Start":"00:13.139 ","End":"00:21.015","Text":"In part a, we\u0027re told that the base case should return 0 for n=0 and 1 for n=1."},{"Start":"00:21.015 ","End":"00:26.595","Text":"In part b, we\u0027re then asked to make sure that the recursive case returns the sum"},{"Start":"00:26.595 ","End":"00:32.550","Text":"of recursive calls with arguments of n minus 1 and n minus 2."},{"Start":"00:32.550 ","End":"00:36.840","Text":"Finally, in part c, we test with value 0 which will returns 0."},{"Start":"00:36.840 ","End":"00:38.715","Text":"One should return 1."},{"Start":"00:38.715 ","End":"00:40.545","Text":"5 should return 5,"},{"Start":"00:40.545 ","End":"00:43.320","Text":"and 8 should return 21."},{"Start":"00:43.320 ","End":"00:46.760","Text":"Let\u0027s start with the return value,"},{"Start":"00:46.760 ","End":"00:49.025","Text":"which we\u0027re told is an int."},{"Start":"00:49.025 ","End":"00:52.700","Text":"The name of the method,"},{"Start":"00:52.700 ","End":"00:56.030","Text":"which is Fibonacci recursive."},{"Start":"00:56.030 ","End":"00:57.950","Text":"The parameter is a single parameter,"},{"Start":"00:57.950 ","End":"00:59.345","Text":"which is again an int."},{"Start":"00:59.345 ","End":"01:01.855","Text":"We\u0027re going to call it n once more."},{"Start":"01:01.855 ","End":"01:04.570","Text":"In part a,"},{"Start":"01:04.570 ","End":"01:09.950","Text":"we\u0027re told that the base case should return 0 for n=0,"},{"Start":"01:09.950 ","End":"01:12.335","Text":"and it should return 1 not for n=1."},{"Start":"01:12.335 ","End":"01:14.565","Text":"That\u0027s relatively straightforward."},{"Start":"01:14.565 ","End":"01:17.900","Text":"But we do need to have a selection statement here."},{"Start":"01:17.900 ","End":"01:19.790","Text":"We will be thinking about what we can have here."},{"Start":"01:19.790 ","End":"01:22.460","Text":"We could do an orbit and simpler way of doing this is"},{"Start":"01:22.460 ","End":"01:25.400","Text":"to say if n is less than or equal to 1."},{"Start":"01:25.400 ","End":"01:27.140","Text":"If it\u0027s one it will come into here."},{"Start":"01:27.140 ","End":"01:29.645","Text":"If it\u0027s 0 will also come into here."},{"Start":"01:29.645 ","End":"01:35.505","Text":"Then what we\u0027re going to do is we\u0027re going to return because if n is 1 we will return 1."},{"Start":"01:35.505 ","End":"01:37.680","Text":"If n is 0 will return 0."},{"Start":"01:37.680 ","End":"01:39.720","Text":"That\u0027s what we were asked to do."},{"Start":"01:39.720 ","End":"01:42.845","Text":"The base case is very straightforward."},{"Start":"01:42.845 ","End":"01:46.040","Text":"The recursive case is also very straightforward."},{"Start":"01:46.040 ","End":"01:51.245","Text":"Well, this time we\u0027ve got to call a function twice."},{"Start":"01:51.245 ","End":"02:01.900","Text":"What we are asked to return is Fibonacci recursive and then n minus 1."},{"Start":"02:01.900 ","End":"02:03.945","Text":"Then we add to that."},{"Start":"02:03.945 ","End":"02:10.520","Text":"The result we get back from Fibonacci recursive n minus 2."},{"Start":"02:10.520 ","End":"02:13.010","Text":"That reads really nicely."},{"Start":"02:13.010 ","End":"02:16.670","Text":"That\u0027s exactly how Fibonacci number is calculated."},{"Start":"02:16.670 ","End":"02:19.895","Text":"It\u0027s the previous 2 terms added together."},{"Start":"02:19.895 ","End":"02:22.955","Text":"If the term is n, n minus 1,"},{"Start":"02:22.955 ","End":"02:25.910","Text":"and n minus 2, the values we get back"},{"Start":"02:25.910 ","End":"02:28.805","Text":"from this function are exactly what we\u0027re looking for."},{"Start":"02:28.805 ","End":"02:32.720","Text":"Let\u0027s see if that works suspiciously short."},{"Start":"02:32.720 ","End":"02:35.000","Text":"But let\u0027s see if it actually does the job."},{"Start":"02:35.000 ","End":"02:39.580","Text":"The first value we\u0027ve been asked to check in part c1 is 0."},{"Start":"02:39.580 ","End":"02:44.240","Text":"We expect to get back 0 in the base case, which we do."},{"Start":"02:44.240 ","End":"02:48.280","Text":"We\u0027ll try the other base case value, which is one."},{"Start":"02:48.280 ","End":"02:51.120","Text":"Which should return 1. It does."},{"Start":"02:51.120 ","End":"02:56.295","Text":"Then part 3, asks us to try it with the value 5."},{"Start":"02:56.295 ","End":"03:01.675","Text":"The 5th term should be 5 in the Fibonacci sequence. It is."},{"Start":"03:01.675 ","End":"03:05.720","Text":"We are asked to also to try the eighth term."},{"Start":"03:05.720 ","End":"03:09.435","Text":"That should be 21. That works as well."},{"Start":"03:09.435 ","End":"03:11.625","Text":"All works but again,"},{"Start":"03:11.625 ","End":"03:13.860","Text":"it seems a bit like magic."},{"Start":"03:13.860 ","End":"03:16.595","Text":"Be nice to see what\u0027s going on a little bit."},{"Start":"03:16.595 ","End":"03:21.180","Text":"Let\u0027s put some code in here just to have a little look at what\u0027s going on."},{"Start":"03:21.180 ","End":"03:27.570","Text":"It is very difficult to trace what\u0027s going on inside the recursive code."},{"Start":"03:27.570 ","End":"03:31.200","Text":"But we will do our best to try and understand this."},{"Start":"03:32.320 ","End":"03:40.140","Text":"n minus 1 will print out the contents off and will also print out a context of n minus 2."},{"Start":"03:40.140 ","End":"03:42.120","Text":"Just label those."},{"Start":"03:42.120 ","End":"03:46.365","Text":"We know what we\u0027re going to call the function here."},{"Start":"03:46.365 ","End":"03:49.280","Text":"What values and here we\u0027re going to use for"},{"Start":"03:49.280 ","End":"03:53.075","Text":"these 2 function calls will see them printed out on the screen."},{"Start":"03:53.075 ","End":"03:55.730","Text":"In fact, let\u0027s just indent it a little bit."},{"Start":"03:55.730 ","End":"03:57.920","Text":"I\u0027ll put a tab character in front of that."},{"Start":"03:57.920 ","End":"04:01.325","Text":"What we also want to do is let\u0027s print"},{"Start":"04:01.325 ","End":"04:06.350","Text":"the value of n when we\u0027re in the base case as well."},{"Start":"04:06.350 ","End":"04:10.850","Text":"We\u0027ll print a special message to say we\u0027re in the base case as well."},{"Start":"04:10.850 ","End":"04:13.730","Text":"Let\u0027s use the tab,"},{"Start":"04:13.730 ","End":"04:15.305","Text":"indent it a little bit."},{"Start":"04:15.305 ","End":"04:17.120","Text":"We\u0027ll say where n is."},{"Start":"04:17.120 ","End":"04:19.685","Text":"Actually, let\u0027s say base case first."},{"Start":"04:19.685 ","End":"04:23.540","Text":"What we might want to do is actually right at the top of the function,"},{"Start":"04:23.540 ","End":"04:29.265","Text":"let\u0027s print out what we are calling the function with."},{"Start":"04:29.265 ","End":"04:33.570","Text":"Let\u0027s use n right at the top as well."},{"Start":"04:33.570 ","End":"04:37.725","Text":"We\u0027ll see a whole bunch of output now when we run method."},{"Start":"04:37.725 ","End":"04:39.195","Text":"Took on an error here."},{"Start":"04:39.195 ","End":"04:42.010","Text":"You\u0027ve got this missing bracket there."},{"Start":"04:42.800 ","End":"04:45.930","Text":"Let\u0027s have a go that."},{"Start":"04:45.930 ","End":"04:49.205","Text":"We try it with some of the smaller numbers."},{"Start":"04:49.205 ","End":"04:53.105","Text":"Let\u0027s try it first of all with, for example 2."},{"Start":"04:53.105 ","End":"04:55.755","Text":"What we get back is the following,"},{"Start":"04:55.755 ","End":"04:58.715","Text":"we\u0027ve got the result of 1 which we expected."},{"Start":"04:58.715 ","End":"05:02.495","Text":"What we\u0027ve done is we\u0027ve called with n is 2."},{"Start":"05:02.495 ","End":"05:09.720","Text":"Then the recursive calls pass on the value 1 and 0 because remember, n minus 1."},{"Start":"05:09.720 ","End":"05:12.540","Text":"Which is 2 minus 1 is 1,"},{"Start":"05:12.540 ","End":"05:14.430","Text":"2 minus 2 is 0."},{"Start":"05:14.430 ","End":"05:16.440","Text":"We calling with 1 and 0."},{"Start":"05:16.440 ","End":"05:20.360","Text":"When we make those function calls we hit the base case."},{"Start":"05:20.360 ","End":"05:22.970","Text":"Because n is 1. Therefore it\u0027s going to return 1."},{"Start":"05:22.970 ","End":"05:27.910","Text":"The second function call here would return n is 0."},{"Start":"05:27.910 ","End":"05:31.805","Text":"Base case is 0, so the value it returns a 0."},{"Start":"05:31.805 ","End":"05:34.475","Text":"If we add 1 and 0 together,"},{"Start":"05:34.475 ","End":"05:36.555","Text":"we get the result of 1."},{"Start":"05:36.555 ","End":"05:39.625","Text":"That\u0027s why we received 1 here."},{"Start":"05:39.625 ","End":"05:47.295","Text":"As we progressively increase the value of n we\u0027ll see more function calls being made."},{"Start":"05:47.295 ","End":"05:49.370","Text":"Because it\u0027s more recursion going on."},{"Start":"05:49.370 ","End":"05:54.350","Text":"Let\u0027s try that. We\u0027re seeing multiple calls here."},{"Start":"05:54.350 ","End":"05:56.180","Text":"We get the result of 2."},{"Start":"05:56.180 ","End":"06:00.110","Text":"When we supply 3, that\u0027s the argument."},{"Start":"06:00.110 ","End":"06:04.820","Text":"We are then making a call of 2 and 1."},{"Start":"06:04.820 ","End":"06:09.975","Text":"Then we\u0027re making another call of 1 and 0."},{"Start":"06:09.975 ","End":"06:12.920","Text":"I should\u0027ve put a space in here between these 2."},{"Start":"06:12.920 ","End":"06:17.870","Text":"Then we will see that the base case is hit for the value 1 here,"},{"Start":"06:17.870 ","End":"06:20.760","Text":"1 there, and 0 here."},{"Start":"06:20.760 ","End":"06:23.600","Text":"That\u0027s where we\u0027re getting 1,0 and 1 back."},{"Start":"06:23.600 ","End":"06:26.045","Text":"When all the return values are added together,"},{"Start":"06:26.045 ","End":"06:28.880","Text":"we get 1 plus 1 which is 2."},{"Start":"06:28.880 ","End":"06:31.790","Text":"Only the base case is going to return an actual number."},{"Start":"06:31.790 ","End":"06:33.830","Text":"The recursive case is going to make"},{"Start":"06:33.830 ","End":"06:38.375","Text":"another call which will indirectly eventually return a number."},{"Start":"06:38.375 ","End":"06:42.530","Text":"But the base case is the bit that actually returns a number of 1 or 0."},{"Start":"06:42.530 ","End":"06:44.525","Text":"It can only be 1 or 0 in this code."},{"Start":"06:44.525 ","End":"06:47.915","Text":"That\u0027s what we get when we resupplied 3 is the argument."},{"Start":"06:47.915 ","End":"06:50.705","Text":"If we look at 5 it\u0027s the same again."},{"Start":"06:50.705 ","End":"06:54.820","Text":"Same argument where it starts to progressively get harder to follow what\u0027s going on."},{"Start":"06:54.820 ","End":"06:57.305","Text":"It\u0027s a huge amount of output here."},{"Start":"06:57.305 ","End":"07:04.860","Text":"What we\u0027re seeing here is when we make this call of n is 5 we\u0027re doing 2 calls here."},{"Start":"07:04.860 ","End":"07:08.720","Text":"1 where we were calling recursively with a value 4,"},{"Start":"07:08.720 ","End":"07:11.600","Text":"1 when we\u0027re calling recursively with the value 3."},{"Start":"07:11.600 ","End":"07:13.720","Text":"We\u0027re seeing all the results here."},{"Start":"07:13.720 ","End":"07:19.550","Text":"All the way down to here of our core with a value of 4."},{"Start":"07:19.550 ","End":"07:22.895","Text":"All of these multiple recursive calls take place."},{"Start":"07:22.895 ","End":"07:26.270","Text":"Then these are the values returned in the base case."},{"Start":"07:26.270 ","End":"07:30.650","Text":"We get 1 plus 1 plus 1 we obvious ignore 0,"},{"Start":"07:30.650 ","End":"07:34.095","Text":"so we get 3 returned from this part."},{"Start":"07:34.095 ","End":"07:36.280","Text":"Then here\u0027s the n is 3-bit."},{"Start":"07:36.280 ","End":"07:38.420","Text":"You see over here we\u0027ve done n minus 2 and we\u0027re"},{"Start":"07:38.420 ","End":"07:42.000","Text":"calling with the value 3. Here\u0027s the n is 3-bit."},{"Start":"07:42.430 ","End":"07:46.845","Text":"We\u0027re getting a value of 1 here and a value of 1 here."},{"Start":"07:46.845 ","End":"07:48.535","Text":"2 returned here,"},{"Start":"07:48.535 ","End":"07:52.835","Text":"3 returned here from all these base cases, 1, 2, 3."},{"Start":"07:52.835 ","End":"07:56.375","Text":"That\u0027s why we get the results back of 5."},{"Start":"07:56.375 ","End":"07:59.510","Text":"We could keep going with this and it gets progressively more and"},{"Start":"07:59.510 ","End":"08:02.870","Text":"more difficult to follow the return values."},{"Start":"08:02.870 ","End":"08:07.460","Text":"That does really show the beauty of recursion."},{"Start":"08:07.460 ","End":"08:08.930","Text":"But also one of the problems."},{"Start":"08:08.930 ","End":"08:12.215","Text":"The beauty is the code is very short indeed."},{"Start":"08:12.215 ","End":"08:16.280","Text":"If I take my print statements back out we\u0027re just left with"},{"Start":"08:16.280 ","End":"08:21.050","Text":"this very simple code, very elegant code."},{"Start":"08:21.050 ","End":"08:24.020","Text":"But if you ever have a problem with something like this,"},{"Start":"08:24.020 ","End":"08:26.480","Text":"trying to trace through the values as we make"},{"Start":"08:26.480 ","End":"08:30.590","Text":"these multiple function calls recursively can be quite tricky."},{"Start":"08:30.590 ","End":"08:33.950","Text":"That is a disadvantage of recursion."},{"Start":"08:33.950 ","End":"08:37.470","Text":"That\u0027s it for this one. See you soon for the next one."}],"ID":29817},{"Watched":false,"Name":"Exercise 3","Duration":"5m 45s","ChapterTopicVideoID":28402,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:01.860","Text":"Hello again and welcome back."},{"Start":"00:01.860 ","End":"00:03.240","Text":"In this next exercise,"},{"Start":"00:03.240 ","End":"00:06.900","Text":"we\u0027ve been told that Green Bottles is a children\u0027s folk song in which"},{"Start":"00:06.900 ","End":"00:11.385","Text":"the number of bottles in each verse decreases until there are no more bottles."},{"Start":"00:11.385 ","End":"00:15.210","Text":"In part a, we create a method called greenBottles which takes"},{"Start":"00:15.210 ","End":"00:19.500","Text":"an int parameter and returns an int, and then in part b,"},{"Start":"00:19.500 ","End":"00:24.420","Text":"we output to the screen the following lines using recursion to initiate"},{"Start":"00:24.420 ","End":"00:29.895","Text":"the next verse each time until the song finishes on 0 bottles."},{"Start":"00:29.895 ","End":"00:32.255","Text":"The first line is number,"},{"Start":"00:32.255 ","End":"00:33.575","Text":"and then literal string,"},{"Start":"00:33.575 ","End":"00:37.040","Text":"space, green bottles, comma hanging on the wall comma."},{"Start":"00:37.040 ","End":"00:39.480","Text":"Then the next line is number again,"},{"Start":"00:39.480 ","End":"00:41.720","Text":"and then literal string, space,"},{"Start":"00:41.720 ","End":"00:42.740","Text":"green bottles comma,"},{"Start":"00:42.740 ","End":"00:44.485","Text":"hanging on the wall comma again,"},{"Start":"00:44.485 ","End":"00:46.725","Text":"and then the 2 final lines,"},{"Start":"00:46.725 ","End":"00:50.990","Text":"a literal string, and if 1 green bottle should accidentally fall,"},{"Start":"00:50.990 ","End":"00:53.900","Text":"3 dots and a new line character,"},{"Start":"00:53.900 ","End":"00:58.955","Text":"and then another final line of there\u0027d be an a space is a literal string."},{"Start":"00:58.955 ","End":"01:00.620","Text":"Finally, in part c,"},{"Start":"01:00.620 ","End":"01:06.960","Text":"we test that an input of 10 produces the correct output of 10 verses of the song."},{"Start":"01:06.960 ","End":"01:08.970","Text":"Start with part a then,"},{"Start":"01:08.970 ","End":"01:16.107","Text":"and we\u0027ve got to create a method which takes an int and returns an int."},{"Start":"01:16.107 ","End":"01:20.125","Text":"Start with int and method\u0027s called greenBottles,"},{"Start":"01:20.125 ","End":"01:23.780","Text":"suitable name for our parameters presumably"},{"Start":"01:23.780 ","End":"01:27.410","Text":"bottles because that\u0027s what we\u0027re going to be counting down,"},{"Start":"01:27.410 ","End":"01:30.950","Text":"and then we\u0027re getting very few instructions for this 1."},{"Start":"01:30.950 ","End":"01:35.060","Text":"We\u0027re not been told about a base case or recursive case."},{"Start":"01:35.060 ","End":"01:39.770","Text":"You should be able to figure out from part b that if it says it will"},{"Start":"01:39.770 ","End":"01:45.185","Text":"continue until we finish on 0 bottles or no bottles,"},{"Start":"01:45.185 ","End":"01:47.960","Text":"then the base case is going to be 0,"},{"Start":"01:47.960 ","End":"01:52.220","Text":"so we\u0027re looking for a value of bottles of 0."},{"Start":"01:52.220 ","End":"01:58.520","Text":"It probably safer actually if we say less than 1 rather than 0."},{"Start":"01:58.520 ","End":"02:00.680","Text":"Of course, 0 is less than 1,"},{"Start":"02:00.680 ","End":"02:05.075","Text":"but this will cover if someone puts in a negative number for bottles,"},{"Start":"02:05.075 ","End":"02:08.640","Text":"it would just immediately stop at the base case,"},{"Start":"02:08.640 ","End":"02:15.380","Text":"so just a little bit defensive there is to just put less than 1 rather than equals 0."},{"Start":"02:15.380 ","End":"02:18.425","Text":"What do we need to do in the base case?"},{"Start":"02:18.425 ","End":"02:24.425","Text":"We need to return a value and not recall recursively."},{"Start":"02:24.425 ","End":"02:30.455","Text":"Doesn\u0027t really matter what we return because the value here is not going to be output."},{"Start":"02:30.455 ","End":"02:33.110","Text":"But I do need to print something."},{"Start":"02:33.110 ","End":"02:35.405","Text":"This is for the last line,"},{"Start":"02:35.405 ","End":"02:39.110","Text":"and therefore I need to print no green bottles,"},{"Start":"02:39.110 ","End":"02:41.605","Text":"so let\u0027s go ahead and do that."},{"Start":"02:41.605 ","End":"02:44.490","Text":"There\u0027s the base case done then."},{"Start":"02:44.490 ","End":"02:47.645","Text":"What do we need to do for the recursive case?"},{"Start":"02:47.645 ","End":"02:50.810","Text":"There\u0027s a lot more output than we\u0027ve seen in the previous examples,"},{"Start":"02:50.810 ","End":"02:54.260","Text":"but actually it\u0027s very simple."},{"Start":"02:54.260 ","End":"02:56.900","Text":"We want an output that is lines,"},{"Start":"02:56.900 ","End":"02:58.490","Text":"so let\u0027s do those first."},{"Start":"02:58.490 ","End":"03:00.800","Text":"We need a number, first of all,"},{"Start":"03:00.800 ","End":"03:04.070","Text":"and that number is available to us in bottles,"},{"Start":"03:04.070 ","End":"03:07.220","Text":"and then we say the rest of the line."},{"Start":"03:07.220 ","End":"03:11.360","Text":"Let\u0027s just copy that because we\u0027ll repeat that twice."},{"Start":"03:11.360 ","End":"03:13.985","Text":"Then the next line is similar,"},{"Start":"03:13.985 ","End":"03:18.200","Text":"but actually it\u0027s not similar because there\u0027s no variables in this."},{"Start":"03:18.200 ","End":"03:21.040","Text":"It\u0027s just 1 green bottle every time."},{"Start":"03:21.040 ","End":"03:23.490","Text":"That\u0027s the end of each verse,"},{"Start":"03:23.490 ","End":"03:26.460","Text":"and we\u0027ve got \\n on the end here,"},{"Start":"03:26.460 ","End":"03:31.165","Text":"so we\u0027re going to break onto a new line for the new verse."},{"Start":"03:31.165 ","End":"03:33.090","Text":"That\u0027s great."},{"Start":"03:33.090 ","End":"03:35.910","Text":"Then we got 1 line on its own,"},{"Start":"03:35.910 ","End":"03:39.660","Text":"and this presumably starts the next verse."},{"Start":"03:39.660 ","End":"03:44.120","Text":"I think actually you probably wanted to do print random print line there because you want"},{"Start":"03:44.120 ","End":"03:49.720","Text":"the next line to start printing on the same line as this one."},{"Start":"03:49.720 ","End":"03:53.750","Text":"That\u0027s great, but actually we\u0027re not doing anything recursive."},{"Start":"03:53.750 ","End":"03:57.365","Text":"Where do we need to recur and what do we need to do?"},{"Start":"03:57.365 ","End":"04:00.920","Text":"We\u0027re always going to want to call the next part of it,"},{"Start":"04:00.920 ","End":"04:04.465","Text":"and so let\u0027s go to greenBottles again."},{"Start":"04:04.465 ","End":"04:06.555","Text":"But we want 1 less bottle,"},{"Start":"04:06.555 ","End":"04:09.660","Text":"so we just call it with bottles minus 1."},{"Start":"04:09.660 ","End":"04:11.190","Text":"We need to return,"},{"Start":"04:11.190 ","End":"04:14.780","Text":"so that\u0027s the thing that\u0027s going to be returned each time round is 1"},{"Start":"04:14.780 ","End":"04:18.950","Text":"less and 1 less and 1 less until we hit the base case and"},{"Start":"04:18.950 ","End":"04:21.455","Text":"we\u0027ll then drop out the whole thing and then"},{"Start":"04:21.455 ","End":"04:27.020","Text":"stop recurring and we should have our output done."},{"Start":"04:27.020 ","End":"04:33.125","Text":"Let\u0027s see if that works and produces the nursery rhymes we expect."},{"Start":"04:33.125 ","End":"04:34.340","Text":"Sensible enough."},{"Start":"04:34.340 ","End":"04:36.600","Text":"Let\u0027s just have a scroll up,"},{"Start":"04:36.600 ","End":"04:38.640","Text":"10 green bottles hanging on the wall,"},{"Start":"04:38.640 ","End":"04:40.200","Text":"10 green bottles hanging on the wall,"},{"Start":"04:40.200 ","End":"04:42.450","Text":"and if 1 green bottle should accidentally fall,"},{"Start":"04:42.450 ","End":"04:44.940","Text":"and then we currently go to 9 green bottles,"},{"Start":"04:44.940 ","End":"04:47.130","Text":"9 green bottles, and if 1 should fall,"},{"Start":"04:47.130 ","End":"04:54.350","Text":"8 green bottles and so on all the way down to the end where we have a final statement,"},{"Start":"04:54.350 ","End":"04:56.135","Text":"there\u0027d be no green bottles,"},{"Start":"04:56.135 ","End":"04:57.605","Text":"I guess we should say,"},{"Start":"04:57.605 ","End":"04:59.765","Text":"hanging on the wall there as well."},{"Start":"04:59.765 ","End":"05:01.550","Text":"That is us done."},{"Start":"05:01.550 ","End":"05:03.185","Text":"It\u0027s very straightforward."},{"Start":"05:03.185 ","End":"05:06.650","Text":"We weren\u0027t told what the base case was and what the recursive case was."},{"Start":"05:06.650 ","End":"05:09.950","Text":"This one\u0027s really seeing if you could figure out what you\u0027d need"},{"Start":"05:09.950 ","End":"05:13.715","Text":"to put for the base case and what you need to put for the recursive case."},{"Start":"05:13.715 ","End":"05:17.424","Text":"It was pretty straightforward once you figure this bit out."},{"Start":"05:17.424 ","End":"05:19.400","Text":"We could have pretty much returned anything"},{"Start":"05:19.400 ","End":"05:22.040","Text":"here because it\u0027s not being used on this last one,"},{"Start":"05:22.040 ","End":"05:24.665","Text":"is just exiting out this code."},{"Start":"05:24.665 ","End":"05:27.380","Text":"We could\u0027ve put minus 1 if we wanted to,"},{"Start":"05:27.380 ","End":"05:29.680","Text":"but that is it basically,"},{"Start":"05:29.680 ","End":"05:32.330","Text":"so hopefully with more examples,"},{"Start":"05:32.330 ","End":"05:34.925","Text":"simple examples of recursion."},{"Start":"05:34.925 ","End":"05:38.540","Text":"It\u0027s starting to make sense now about"},{"Start":"05:38.540 ","End":"05:42.260","Text":"how you might use this as a technique in programming."},{"Start":"05:42.260 ","End":"05:43.535","Text":"Thanks very much for watching,"},{"Start":"05:43.535 ","End":"05:46.140","Text":"and I\u0027ll see you soon for the next one."}],"ID":29818},{"Watched":false,"Name":"Exercise 4","Duration":"11m 4s","ChapterTopicVideoID":28403,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.670","Text":"Hello again and welcome back to the next exercise,"},{"Start":"00:02.670 ","End":"00:05.970","Text":"in which we are told that a string can be recursively checked at it\u0027s"},{"Start":"00:05.970 ","End":"00:10.800","Text":"2 different ends to see if the string value is a palindrome."},{"Start":"00:10.800 ","End":"00:16.215","Text":"In Part a, we\u0027re asked to create a method which is called is palindrome recursive."},{"Start":"00:16.215 ","End":"00:20.355","Text":"It accepts a string argument and returns a Boolean."},{"Start":"00:20.355 ","End":"00:22.470","Text":"In Part b, within the method,"},{"Start":"00:22.470 ","End":"00:25.410","Text":"we\u0027re supposed to force the string to lowercase."},{"Start":"00:25.410 ","End":"00:32.070","Text":"In Part c, to replace all space characters with an empty string and in Part d,"},{"Start":"00:32.070 ","End":"00:34.565","Text":"to replace all characters within the string that are"},{"Start":"00:34.565 ","End":"00:37.995","Text":"not lowercase letters with an empty string."},{"Start":"00:37.995 ","End":"00:43.130","Text":"Then in Part e, we are asked to check if the length of the string is"},{"Start":"00:43.130 ","End":"00:48.335","Text":"less than or equal to 1 and if it is to return true, otherwise,"},{"Start":"00:48.335 ","End":"00:53.019","Text":"if the first character of the string is the same as the last character of the string,"},{"Start":"00:53.019 ","End":"00:56.150","Text":"we call this palindrome recursive function again,"},{"Start":"00:56.150 ","End":"00:59.135","Text":"but pass it a substring of the original string,"},{"Start":"00:59.135 ","End":"01:04.565","Text":"which extracts all but the first and last characters of the original string."},{"Start":"01:04.565 ","End":"01:06.950","Text":"In Part g, we\u0027re told that otherwise,"},{"Start":"01:06.950 ","End":"01:09.482","Text":"we should return the value false."},{"Start":"01:09.482 ","End":"01:14.188","Text":"Then we are asked to test with the string did Hannah see bees?"},{"Start":"01:14.188 ","End":"01:16.820","Text":"Hannah did. That contains a question mark,"},{"Start":"01:16.820 ","End":"01:19.940","Text":"some spaces and a full stop at the end."},{"Start":"01:19.940 ","End":"01:21.785","Text":"That should return true."},{"Start":"01:21.785 ","End":"01:23.800","Text":"If we remove 1 letter,"},{"Start":"01:23.800 ","End":"01:25.970","Text":"not 1 or the other symbols but a letter,"},{"Start":"01:25.970 ","End":"01:30.560","Text":"then we\u0027ll expect to see the value false returned."},{"Start":"01:30.560 ","End":"01:36.110","Text":"This is the same as the exercise we did previously on palindromes,"},{"Start":"01:36.110 ","End":"01:40.160","Text":"but we\u0027re going to use recursion to do it this time."},{"Start":"01:40.160 ","End":"01:42.260","Text":"We still return a Boolean,"},{"Start":"01:42.260 ","End":"01:48.325","Text":"which time the name is palindrome with recursive stuck on the end."},{"Start":"01:48.325 ","End":"01:50.902","Text":"Should we want to have both of them side by side,"},{"Start":"01:50.902 ","End":"01:52.490","Text":"I suppose we could."},{"Start":"01:52.490 ","End":"01:55.385","Text":"We\u0027ve got a string argument this time."},{"Start":"01:55.385 ","End":"02:00.540","Text":"I\u0027m going to call it phrase because it\u0027s made up of several words."},{"Start":"02:00.710 ","End":"02:06.880","Text":"There\u0027s the start in Part a and we\u0027ve got to do a few things."},{"Start":"02:06.880 ","End":"02:11.860","Text":"If the palindrome has some punctuation characters and uppercase letters and so on."},{"Start":"02:11.860 ","End":"02:14.050","Text":"We need to cope with that situation."},{"Start":"02:14.050 ","End":"02:17.485","Text":"In Part b, we forced the string to lowercase."},{"Start":"02:17.485 ","End":"02:21.400","Text":"Phrase is what we have the string in."},{"Start":"02:21.400 ","End":"02:23.260","Text":"We want to store it back in there,"},{"Start":"02:23.260 ","End":"02:26.725","Text":"but we want to force it to lowercase."},{"Start":"02:26.725 ","End":"02:32.300","Text":"Phrase to lowercase should do the job."},{"Start":"02:32.300 ","End":"02:36.880","Text":"Actually, it looks like go narrow straightway. That\u0027s fine."},{"Start":"02:36.880 ","End":"02:39.010","Text":"It\u0027s just we haven\u0027t got to the return statement yet."},{"Start":"02:39.010 ","End":"02:44.560","Text":"The next part is to remove any empty strings,"},{"Start":"02:44.560 ","End":"02:47.175","Text":"if you remember, is replace all."},{"Start":"02:47.175 ","End":"02:50.276","Text":"You say what it is that you want to replace."},{"Start":"02:50.276 ","End":"02:54.410","Text":"We want to replace spaces with empty strings that would do that job."},{"Start":"02:54.410 ","End":"02:58.010","Text":"Finally, we want to replace all characters within the string,"},{"Start":"02:58.010 ","End":"03:01.175","Text":"that are not lowercase letters with an empty string."},{"Start":"03:01.175 ","End":"03:04.700","Text":"Anything else needs to be replaced and you remember we"},{"Start":"03:04.700 ","End":"03:07.940","Text":"used quite complex regular expression."},{"Start":"03:07.940 ","End":"03:09.590","Text":"There actually wasn\u0027t that complex,"},{"Start":"03:09.590 ","End":"03:11.735","Text":"but what it looked like, was this,"},{"Start":"03:11.735 ","End":"03:16.385","Text":"we said if it\u0027s not lowercase a to lowercase z,"},{"Start":"03:16.385 ","End":"03:24.026","Text":"so anything except for lowercase a to z needs to be replaced with nothing."},{"Start":"03:24.026 ","End":"03:25.670","Text":"With its removal, basically,"},{"Start":"03:25.670 ","End":"03:29.150","Text":"we will result if we put an empty string or"},{"Start":"03:29.150 ","End":"03:33.260","Text":"an empty character in the place of whatever was there before. that should do the job."},{"Start":"03:33.260 ","End":"03:35.165","Text":"What are you going to do actually is I\u0027m going to"},{"Start":"03:35.165 ","End":"03:38.180","Text":"just test here because so much good has gone wrong."},{"Start":"03:38.180 ","End":"03:40.730","Text":"This might be useful to us later anyway,"},{"Start":"03:40.730 ","End":"03:43.760","Text":"let\u0027s print out what we end up with."},{"Start":"03:43.760 ","End":"03:48.790","Text":"I\u0027m just going to need to put a dummy return in here or refuse to compile."},{"Start":"03:48.790 ","End":"03:50.425","Text":"Let\u0027s just put -1 there."},{"Start":"03:50.425 ","End":"03:52.460","Text":"Let\u0027s see what we get back from that."},{"Start":"03:52.460 ","End":"03:54.545","Text":"Once we removed our little errors."},{"Start":"03:54.545 ","End":"03:59.055","Text":"Sorry, that should be an = sign there. What do we got here?"},{"Start":"03:59.055 ","End":"04:03.255","Text":"Return -1, so I need to return, let\u0027s say false."},{"Start":"04:03.255 ","End":"04:06.020","Text":"Let\u0027s have a go at that now and see if we get"},{"Start":"04:06.020 ","End":"04:09.932","Text":"the test string in the form that we want it to be in."},{"Start":"04:09.932 ","End":"04:14.465","Text":"We use the same 1 as we were supposed to do in the Part h,"},{"Start":"04:14.465 ","End":"04:16.820","Text":"we know that this one is a palindrome."},{"Start":"04:16.820 ","End":"04:19.880","Text":"Did Hannah see bees?"},{"Start":"04:19.880 ","End":"04:21.470","Text":"Hannah did."},{"Start":"04:21.470 ","End":"04:22.940","Text":"Lowest return false."},{"Start":"04:22.940 ","End":"04:25.250","Text":"We\u0027re not interested in it for memories of being written the code,"},{"Start":"04:25.250 ","End":"04:27.560","Text":"but you can see the output here."},{"Start":"04:27.560 ","End":"04:29.225","Text":"You can see actually,"},{"Start":"04:29.225 ","End":"04:30.935","Text":"this now raw string."},{"Start":"04:30.935 ","End":"04:32.330","Text":"If you read it backwards,"},{"Start":"04:32.330 ","End":"04:34.280","Text":"you would read did Hannah,"},{"Start":"04:34.280 ","End":"04:37.490","Text":"see bees, Hannah did again."},{"Start":"04:37.490 ","End":"04:40.730","Text":"We know it\u0027s a palindrome, but we want the code to work that out."},{"Start":"04:40.730 ","End":"04:42.823","Text":"Let\u0027s carry on then."},{"Start":"04:42.823 ","End":"04:45.680","Text":"On that first little check and it\u0027s often worth doing"},{"Start":"04:45.680 ","End":"04:49.445","Text":"these stuff when you\u0027ve got a few potentially things that could go wrong,"},{"Start":"04:49.445 ","End":"04:52.910","Text":"check them out early on rather than really late into your code."},{"Start":"04:52.910 ","End":"04:55.910","Text":"Let\u0027s have a look at the main event now."},{"Start":"04:55.910 ","End":"05:02.900","Text":"We\u0027re on Part e. If the length of the string is less than or equal to 1, return true."},{"Start":"05:02.900 ","End":"05:08.960","Text":"If phrase dot length is less than or equal to 1,"},{"Start":"05:08.960 ","End":"05:11.733","Text":"then we return true."},{"Start":"05:11.733 ","End":"05:17.560","Text":"That presumably is the base case because it\u0027s letting us get out of the code."},{"Start":"05:17.560 ","End":"05:21.765","Text":"When we\u0027ve got a phrase length of less than or equal to 1,"},{"Start":"05:21.765 ","End":"05:24.380","Text":"going exactly character left with no characters left,"},{"Start":"05:24.380 ","End":"05:25.430","Text":"it\u0027s going to return true."},{"Start":"05:25.430 ","End":"05:28.700","Text":"That means it must be a palindrome logically."},{"Start":"05:28.700 ","End":"05:32.457","Text":"What is the recursive case going to be then?"},{"Start":"05:32.457 ","End":"05:35.015","Text":"Then we\u0027ve got Part f now,"},{"Start":"05:35.015 ","End":"05:38.300","Text":"if the first character of the string is the same as"},{"Start":"05:38.300 ","End":"05:42.590","Text":"the last character of the string called the IsPalindromeRecursive function again,"},{"Start":"05:42.590 ","End":"05:44.780","Text":"but Parsi, a substring of the original string which"},{"Start":"05:44.780 ","End":"05:49.250","Text":"extracts all but the first and last characters of the original string."},{"Start":"05:49.250 ","End":"05:50.570","Text":"Quite a few things there."},{"Start":"05:50.570 ","End":"05:54.245","Text":"But actually, little subtlety here is that it says,"},{"Start":"05:54.245 ","End":"05:55.550","Text":"if the first character,"},{"Start":"05:55.550 ","End":"05:57.320","Text":"so there\u0027s another f in here,"},{"Start":"05:57.320 ","End":"05:59.710","Text":"because the else will only happen."},{"Start":"05:59.710 ","End":"06:01.520","Text":"if The phrase length is less than 1."},{"Start":"06:01.520 ","End":"06:04.970","Text":"We\u0027ve done nothing to check the actual contents of this."},{"Start":"06:04.970 ","End":"06:07.865","Text":"We have got to have an if statement in here."},{"Start":"06:07.865 ","End":"06:13.015","Text":"What we want to check is if the first character of the string."},{"Start":"06:13.015 ","End":"06:17.480","Text":"We can get characters of strings if you\u0027ll remember from charAt."},{"Start":"06:17.480 ","End":"06:20.630","Text":"And the first character is always going to be character 0."},{"Start":"06:20.630 ","End":"06:27.065","Text":"If that is equal to the last character of the string and you can see the logic there."},{"Start":"06:27.065 ","End":"06:28.975","Text":"For it to be a palindrome,"},{"Start":"06:28.975 ","End":"06:31.085","Text":"we go from the start and the end."},{"Start":"06:31.085 ","End":"06:33.440","Text":"Those characters have always going to be the same if it\u0027s"},{"Start":"06:33.440 ","End":"06:37.445","Text":"a palindrome because it\u0027s going to start and end if it\u0027s backwards with the same thing."},{"Start":"06:37.445 ","End":"06:41.955","Text":"Char at braze dot length"},{"Start":"06:41.955 ","End":"06:44.900","Text":"minus 1 will give us the last character because remember"},{"Start":"06:44.900 ","End":"06:48.004","Text":"we\u0027re counting from 0 when we get an index value back."},{"Start":"06:48.004 ","End":"06:51.800","Text":"Phrase length minus 1 will give"},{"Start":"06:51.800 ","End":"06:56.000","Text":"us last character and we\u0027re comparing it to the first character."},{"Start":"06:56.000 ","End":"06:58.145","Text":"If they are the same,"},{"Start":"06:58.145 ","End":"07:02.404","Text":"then it\u0027s not enough to return and say yet good, this is a palindrome."},{"Start":"07:02.404 ","End":"07:06.290","Text":"We actually need to check the next character as well."},{"Start":"07:06.290 ","End":"07:07.340","Text":"How do we do that?"},{"Start":"07:07.340 ","End":"07:09.920","Text":"We pass it a substring of the original string."},{"Start":"07:09.920 ","End":"07:12.545","Text":"We call is palindrome recursive again."},{"Start":"07:12.545 ","End":"07:15.920","Text":"Do need to use the return keyword always said we get a value"},{"Start":"07:15.920 ","End":"07:21.635","Text":"back is palindrome recursive."},{"Start":"07:21.635 ","End":"07:26.750","Text":"But this time we want to use phrase dot substring."},{"Start":"07:26.750 ","End":"07:30.800","Text":"We don\u0027t want the whole string starting from character 1 and"},{"Start":"07:30.800 ","End":"07:35.215","Text":"then going up to phrase length minus 1."},{"Start":"07:35.215 ","End":"07:38.180","Text":"Because it goes up to and not including that last character,"},{"Start":"07:38.180 ","End":"07:41.065","Text":"I believe, it will miss 1 off."},{"Start":"07:41.065 ","End":"07:43.270","Text":"Again, a phrase length minus 1"},{"Start":"07:43.270 ","End":"07:46.115","Text":"and then we\u0027ll miss 1 off anyway because it\u0027s going to go up to"},{"Start":"07:46.115 ","End":"07:52.610","Text":"but not including that last 1 that I believe will do the trick."},{"Start":"07:52.610 ","End":"07:55.355","Text":"There\u0027s 1 little bit left to do, which is to say,"},{"Start":"07:55.355 ","End":"08:02.105","Text":"if the last part is not we don\u0027t need it there."},{"Start":"08:02.105 ","End":"08:07.094","Text":"We want to do is we return false."},{"Start":"08:07.094 ","End":"08:08.870","Text":"Yes, it is here."},{"Start":"08:08.870 ","End":"08:15.260","Text":"Actually, we return false if we don\u0027t do the recursive call."},{"Start":"08:15.260 ","End":"08:18.560","Text":"There\u0027s 2 base cases here effectively."},{"Start":"08:18.560 ","End":"08:22.475","Text":"The first one is if we are left with only 1 character,"},{"Start":"08:22.475 ","End":"08:24.605","Text":"then we return true and we\u0027re out."},{"Start":"08:24.605 ","End":"08:27.590","Text":"The other time is if we\u0027ve found that"},{"Start":"08:27.590 ","End":"08:32.510","Text":"the first character and the last character of the string are not the same,"},{"Start":"08:32.510 ","End":"08:35.345","Text":"then we return false and we go out as well."},{"Start":"08:35.345 ","End":"08:37.745","Text":"Let\u0027s see if that works."},{"Start":"08:37.745 ","End":"08:39.650","Text":"Inevitably, there\u0027s always an error."},{"Start":"08:39.650 ","End":"08:41.675","Text":"Let me just missing a bracket there."},{"Start":"08:41.675 ","End":"08:45.620","Text":"Let\u0027s try that. Did Hannah see bees?"},{"Start":"08:45.620 ","End":"08:47.840","Text":"Hannah did, and we get true back,"},{"Start":"08:47.840 ","End":"08:51.590","Text":"which is what we expect and because we left that debug line in showing"},{"Start":"08:51.590 ","End":"08:55.460","Text":"us each time phrase when it enters the code,"},{"Start":"08:55.460 ","End":"08:58.910","Text":"when we strip it of the other characters that were not interested in,"},{"Start":"08:58.910 ","End":"09:01.235","Text":"you\u0027ll see that each time around,"},{"Start":"09:01.235 ","End":"09:03.050","Text":"we\u0027re left with a palindrome."},{"Start":"09:03.050 ","End":"09:05.150","Text":"It\u0027s still a palindrome each time around."},{"Start":"09:05.150 ","End":"09:07.850","Text":"We just keep going and keep going and keep going,"},{"Start":"09:07.850 ","End":"09:10.325","Text":"until we\u0027re only left with 1 character."},{"Start":"09:10.325 ","End":"09:12.980","Text":"That\u0027s the middle character in this case."},{"Start":"09:12.980 ","End":"09:16.189","Text":"Or if it didn\u0027t have that middle character here,"},{"Start":"09:16.189 ","End":"09:19.130","Text":"it would be 0 and the last 1,"},{"Start":"09:19.130 ","End":"09:21.470","Text":"and we would be fine."},{"Start":"09:21.470 ","End":"09:23.135","Text":"It would still be a palindrome."},{"Start":"09:23.135 ","End":"09:24.620","Text":"That seems to work."},{"Start":"09:24.620 ","End":"09:31.050","Text":"Let\u0027s try and remove the B from it just to check that it returns"},{"Start":"09:31.050 ","End":"09:37.939","Text":"a false results when we provide it with something which is not a palindrome."},{"Start":"09:37.939 ","End":"09:40.895","Text":"Did Hannah see bees?"},{"Start":"09:40.895 ","End":"09:42.200","Text":"Hannah did."},{"Start":"09:42.200 ","End":"09:45.830","Text":"I could remove any character here that any alphabetic character ball,"},{"Start":"09:45.830 ","End":"09:50.975","Text":"I\u0027m going to remove the S from here."},{"Start":"09:50.975 ","End":"09:53.300","Text":"Did Hannah see bee? Hannah did."},{"Start":"09:53.300 ","End":"09:58.130","Text":"That would no longer be a palindrome just because that one character removed."},{"Start":"09:58.130 ","End":"10:02.090","Text":"We should get false back and we do so that\u0027s great."},{"Start":"10:02.090 ","End":"10:04.590","Text":"We\u0027ll see actually where it breaks."},{"Start":"10:04.590 ","End":"10:10.309","Text":"Because h see bee h is a palindrome."},{"Start":"10:10.309 ","End":"10:13.145","Text":"See bee is where it gets to."},{"Start":"10:13.145 ","End":"10:15.770","Text":"This is no longer a palindrome,"},{"Start":"10:15.770 ","End":"10:20.881","Text":"and therefore it drops out of the code and returns as false as we expect."},{"Start":"10:20.881 ","End":"10:23.420","Text":"We were given quite a lot of the steps there."},{"Start":"10:23.420 ","End":"10:26.000","Text":"But actually, this was a little bit tricky because we have"},{"Start":"10:26.000 ","End":"10:28.940","Text":"these two different instances where we were in"},{"Start":"10:28.940 ","End":"10:32.330","Text":"a base case now in a slightly different parts of the code."},{"Start":"10:32.330 ","End":"10:35.705","Text":"It\u0027s a little bit more complex than what we\u0027ve seen up until now,"},{"Start":"10:35.705 ","End":"10:39.515","Text":"but it\u0027s still got this familiar little thing here of base case."},{"Start":"10:39.515 ","End":"10:43.370","Text":"The recursive case is always feeding a slightly different value and"},{"Start":"10:43.370 ","End":"10:47.735","Text":"calling itself until it eventually unravels."},{"Start":"10:47.735 ","End":"10:52.400","Text":"In this case, when it finds it\u0027s not a palindrome and it returns false."},{"Start":"10:52.400 ","End":"10:56.315","Text":"Or when it\u0027s only left with 1 character or 0 characters,"},{"Start":"10:56.315 ","End":"10:58.610","Text":"then you know, it is a palindrome,"},{"Start":"10:58.610 ","End":"11:00.500","Text":"so it returns true."},{"Start":"11:00.500 ","End":"11:02.320","Text":"That\u0027s it for this one as well,"},{"Start":"11:02.320 ","End":"11:05.160","Text":"and I will see you soon for the next one."}],"ID":29819},{"Watched":false,"Name":"Exercise 5","Duration":"11m 28s","ChapterTopicVideoID":28404,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:02.040","Text":"Hello and welcome back."},{"Start":"00:02.040 ","End":"00:05.400","Text":"In this exercise, we\u0027re told how a binary search works."},{"Start":"00:05.400 ","End":"00:09.270","Text":"It works by dividing a set of sorted numbers into"},{"Start":"00:09.270 ","End":"00:11.340","Text":"2 halves and checking whether"},{"Start":"00:11.340 ","End":"00:15.150","Text":"a target value being searched for is in the middle of the set."},{"Start":"00:15.150 ","End":"00:18.870","Text":"If the middle value is lower than the target value,"},{"Start":"00:18.870 ","End":"00:23.460","Text":"the left-hand half, and the middle value is discarded."},{"Start":"00:23.460 ","End":"00:26.955","Text":"If the middle value is higher than the target value,"},{"Start":"00:26.955 ","End":"00:30.525","Text":"the right-hand half and the middle value is discarded."},{"Start":"00:30.525 ","End":"00:34.145","Text":"This happens repeatedly until the target value is found,"},{"Start":"00:34.145 ","End":"00:36.320","Text":"or there\u0027s only one item left."},{"Start":"00:36.320 ","End":"00:38.225","Text":"When the target value is found,"},{"Start":"00:38.225 ","End":"00:40.670","Text":"the position where it was found is returned,"},{"Start":"00:40.670 ","End":"00:44.005","Text":"otherwise, minus 1 is returned."},{"Start":"00:44.005 ","End":"00:49.730","Text":"In part a, we need to create a new method called binary search recursive,"},{"Start":"00:49.730 ","End":"00:54.035","Text":"which returns an int and takes as arguments an array of ints"},{"Start":"00:54.035 ","End":"00:59.260","Text":"called arr and 3 ints left, right, and target."},{"Start":"00:59.260 ","End":"01:03.290","Text":"In part b, we\u0027re told to use a selection structure to test whether"},{"Start":"01:03.290 ","End":"01:08.655","Text":"right is greater than or equal to left and if it\u0027s not,"},{"Start":"01:08.655 ","End":"01:12.155","Text":"we return minus 1 but if it is,"},{"Start":"01:12.155 ","End":"01:16.070","Text":"we first declare an integer variable middle and assign it"},{"Start":"01:16.070 ","End":"01:20.915","Text":"the value left plus right minus left divided by 2,"},{"Start":"01:20.915 ","End":"01:22.640","Text":"all in brackets, and right,"},{"Start":"01:22.640 ","End":"01:24.815","Text":"minus left is also in brackets."},{"Start":"01:24.815 ","End":"01:26.250","Text":"In Part 2,"},{"Start":"01:26.250 ","End":"01:29.960","Text":"we\u0027re asked to say that if the value of the array element"},{"Start":"01:29.960 ","End":"01:34.295","Text":"pointed at by middle is equal to the value of target,"},{"Start":"01:34.295 ","End":"01:37.150","Text":"we return the value of middle."},{"Start":"01:37.150 ","End":"01:39.260","Text":"In Part 3, otherwise,"},{"Start":"01:39.260 ","End":"01:45.065","Text":"we check if the value of the array element pointed at by middle is greater than target,"},{"Start":"01:45.065 ","End":"01:48.185","Text":"if it is, we call binary search recursive again,"},{"Start":"01:48.185 ","End":"01:50.000","Text":"but with values for left and right,"},{"Start":"01:50.000 ","End":"01:53.375","Text":"that will search only the left-hand half of the array."},{"Start":"01:53.375 ","End":"01:58.640","Text":"Or if the value of the array element pointed at by middle is less than target,"},{"Start":"01:58.640 ","End":"02:01.220","Text":"we call binary search recursive again,"},{"Start":"02:01.220 ","End":"02:03.080","Text":"but with values for left and right,"},{"Start":"02:03.080 ","End":"02:06.860","Text":"that will search only the right hand off of the array."},{"Start":"02:06.860 ","End":"02:10.175","Text":"Finally, we test the method with the following values."},{"Start":"02:10.175 ","End":"02:16.230","Text":"An array of integers 1,2,3,4,5,6,7,8 and integer values of 0,7,"},{"Start":"02:16.230 ","End":"02:18.290","Text":"and 2 should return 1."},{"Start":"02:18.290 ","End":"02:25.070","Text":"An array of integer values 2,4,6,8,10,12,14,16 and integers of 0,7,"},{"Start":"02:25.070 ","End":"02:27.200","Text":"and 3 should return minus 1."},{"Start":"02:27.200 ","End":"02:30.740","Text":"Finally, an array of integers,"},{"Start":"02:33.240 ","End":"02:36.090","Text":"1,2,4,8,16,32,64,128 and values of 0,7,"},{"Start":"02:36.090 ","End":"02:39.200","Text":"and 32 should return 5."},{"Start":"02:39.200 ","End":"02:41.780","Text":"Long question this time."},{"Start":"02:41.780 ","End":"02:47.300","Text":"Part a, we\u0027ve been asked to create a new medical binary search recursive."},{"Start":"02:47.300 ","End":"02:52.310","Text":"It returns an int and we pass it many arguments,"},{"Start":"02:52.310 ","End":"02:59.430","Text":"the first of which is an array of int so it\u0027s an integer array and we call it our arr,"},{"Start":"02:59.430 ","End":"03:04.340","Text":"A-R-R, short for array, and then we\u0027ve got 3 more integers,"},{"Start":"03:04.340 ","End":"03:06.685","Text":"left-right, and target."},{"Start":"03:06.685 ","End":"03:13.655","Text":"That\u0027s the opening line of our method defining what the return,"},{"Start":"03:13.655 ","End":"03:16.580","Text":"type, and parameters are."},{"Start":"03:16.580 ","End":"03:20.540","Text":"Then we\u0027re told that we need to use"},{"Start":"03:20.540 ","End":"03:25.475","Text":"a selection structure to test whether right is greater than or equal to left."},{"Start":"03:25.475 ","End":"03:28.420","Text":"That\u0027s straightforward enough and if it is,"},{"Start":"03:28.420 ","End":"03:31.270","Text":"we do something, if it\u0027s not,"},{"Start":"03:31.270 ","End":"03:33.700","Text":"we return minus 1."},{"Start":"03:33.700 ","End":"03:36.910","Text":"I can put an else here, but actually,"},{"Start":"03:36.910 ","End":"03:41.575","Text":"I\u0027ll never get to this part if there\u0027s a previous return."},{"Start":"03:41.575 ","End":"03:45.910","Text":"I can just put that inside here somewhere is going to be a return as"},{"Start":"03:45.910 ","End":"03:52.225","Text":"well and it\u0027ll therefore never get to this spot if it hits a return beforehand."},{"Start":"03:52.225 ","End":"03:55.840","Text":"It\u0027s entirely up to you which way you do it if you really want to put an else"},{"Start":"03:55.840 ","End":"03:58.930","Text":"then you can but it would work"},{"Start":"03:58.930 ","End":"04:01.750","Text":"exactly the same as what I\u0027ve just done but let\u0027s put it"},{"Start":"04:01.750 ","End":"04:08.300","Text":"in just to make it conventional compared to what you\u0027ve seen so far."},{"Start":"04:08.630 ","End":"04:16.725","Text":"The next part asks us to declare an integer variable,"},{"Start":"04:16.725 ","End":"04:21.655","Text":"so this is inside the upper part of the selection structure in the if part."},{"Start":"04:21.655 ","End":"04:24.773","Text":"The variable is called middle,"},{"Start":"04:24.773 ","End":"04:27.980","Text":"and we assign a value according to this formula,"},{"Start":"04:27.980 ","End":"04:32.555","Text":"left plus double brackets,"},{"Start":"04:32.555 ","End":"04:38.045","Text":"right minus left, close 1 bracket divided by 2, and close another bracket."},{"Start":"04:38.045 ","End":"04:43.010","Text":"The bracket is quite important here because we could subtract left from right first,"},{"Start":"04:43.010 ","End":"04:44.270","Text":"then divide it by 2,"},{"Start":"04:44.270 ","End":"04:45.650","Text":"then add it to left."},{"Start":"04:45.650 ","End":"04:47.855","Text":"If we don\u0027t have the brackets in the right order,"},{"Start":"04:47.855 ","End":"04:53.840","Text":"that might give us an inaccurate result. Part D2."},{"Start":"04:53.840 ","End":"04:56.960","Text":"Now, if the value of the array element pointed at by"},{"Start":"04:56.960 ","End":"05:00.710","Text":"middle is equal to the value of target return the value of middle,"},{"Start":"05:00.710 ","End":"05:04.630","Text":"basically, if you found the value that you\u0027re looking for."},{"Start":"05:04.630 ","End":"05:10.415","Text":"The array, we\u0027re using middle as an index to point at something,"},{"Start":"05:10.415 ","End":"05:13.470","Text":"the middle part, middle element of the array."},{"Start":"05:13.470 ","End":"05:16.010","Text":"If that\u0027s the thing that we\u0027re looking for,"},{"Start":"05:16.010 ","End":"05:17.681","Text":"it\u0027s the target,"},{"Start":"05:17.681 ","End":"05:25.045","Text":"then we\u0027ve found the position of the value we\u0027re looking for."},{"Start":"05:25.045 ","End":"05:29.820","Text":"We can just return that position which will be middle."},{"Start":"05:30.050 ","End":"05:33.360","Text":"That\u0027s D2 done."},{"Start":"05:33.360 ","End":"05:36.860","Text":"If it\u0027s not the case if we haven\u0027t returned middle."},{"Start":"05:36.860 ","End":"05:39.440","Text":"Once again, I don\u0027t actually have to have"},{"Start":"05:39.440 ","End":"05:41.960","Text":"an else because it would never get to this line here."},{"Start":"05:41.960 ","End":"05:43.535","Text":"If I return something else here,"},{"Start":"05:43.535 ","End":"05:47.120","Text":"it would never get there if it had already hit this one."},{"Start":"05:47.120 ","End":"05:48.800","Text":"I don\u0027t really need an else,"},{"Start":"05:48.800 ","End":"05:52.697","Text":"but I\u0027ll put on in and inside that part,"},{"Start":"05:52.697 ","End":"05:55.640","Text":"let\u0027s get rid of this return before it causes managed trouble."},{"Start":"05:55.640 ","End":"05:57.360","Text":"Inside that part,"},{"Start":"05:57.360 ","End":"05:59.480","Text":"we\u0027re saying check if the value of"},{"Start":"05:59.480 ","End":"06:03.215","Text":"the array element pointed at by middle is greater than the target."},{"Start":"06:03.215 ","End":"06:05.375","Text":"In this case, if"},{"Start":"06:05.375 ","End":"06:13.541","Text":"array middle has a value that\u0027s bigger than the target value we\u0027re looking for,"},{"Start":"06:13.541 ","End":"06:18.110","Text":"and what we\u0027re going to do is we\u0027re going to do a recursive call"},{"Start":"06:18.110 ","End":"06:25.250","Text":"to binary search recursive but this time the values we send they are different,"},{"Start":"06:25.250 ","End":"06:27.320","Text":"we\u0027re still dealing with the same array,"},{"Start":"06:27.320 ","End":"06:31.655","Text":"but we only want to search the left-hand half of the array."},{"Start":"06:31.655 ","End":"06:34.640","Text":"That is because if we know that"},{"Start":"06:34.640 ","End":"06:38.311","Text":"the value we\u0027ve just looked at is bigger than the target,"},{"Start":"06:38.311 ","End":"06:41.060","Text":"we need a smaller value to get the target and"},{"Start":"06:41.060 ","End":"06:46.325","Text":"the smaller values in a sorted array will be on the left-hand side of the array."},{"Start":"06:46.325 ","End":"06:51.610","Text":"What we want to do is we want to go from left,"},{"Start":"06:51.610 ","End":"06:54.230","Text":"so where we started from up here,"},{"Start":"06:54.230 ","End":"06:57.905","Text":"but up to wherever we\u0027ve calculated middle to be."},{"Start":"06:57.905 ","End":"07:00.680","Text":"But we don\u0027t actually need the value of middle itself because we\u0027ve already"},{"Start":"07:00.680 ","End":"07:03.695","Text":"determined that it\u0027s not the one that we\u0027re looking for."},{"Start":"07:03.695 ","End":"07:06.125","Text":"Otherwise, we would\u0027ve never got to this line."},{"Start":"07:06.125 ","End":"07:09.875","Text":"Well, obviously it\u0027s target we\u0027re still looking for."},{"Start":"07:09.875 ","End":"07:12.875","Text":"That is the call that we need to make,"},{"Start":"07:12.875 ","End":"07:18.770","Text":"and if we don\u0027t find that array middle is greater than target,"},{"Start":"07:18.770 ","End":"07:21.770","Text":"then array middle must be less than target."},{"Start":"07:21.770 ","End":"07:23.510","Text":"In that case, again,"},{"Start":"07:23.510 ","End":"07:24.770","Text":"we\u0027d never get to this line."},{"Start":"07:24.770 ","End":"07:26.285","Text":"I don\u0027t need an else."},{"Start":"07:26.285 ","End":"07:28.430","Text":"I can put one in just to for"},{"Start":"07:28.430 ","End":"07:32.375","Text":"completeness sake and just make it everything else but I don\u0027t need one there."},{"Start":"07:32.375 ","End":"07:35.990","Text":"Let\u0027s put that block in."},{"Start":"07:35.990 ","End":"07:41.990","Text":"What happens here then is we\u0027re going to search the other half of the array."},{"Start":"07:41.990 ","End":"07:46.050","Text":"Binary search recursive."},{"Start":"07:47.440 ","End":"07:51.350","Text":"Same array again but this time,"},{"Start":"07:51.350 ","End":"07:55.190","Text":"we\u0027re going to go from middle plus 1 because we know it\u0027s not"},{"Start":"07:55.190 ","End":"08:00.590","Text":"the middle value so we skip that one up to the end of the search area,"},{"Start":"08:00.590 ","End":"08:03.830","Text":"which is right, that\u0027s the thing that\u0027s been passed in,"},{"Start":"08:03.830 ","End":"08:06.900","Text":"and then we\u0027re searching for the target value."},{"Start":"08:07.060 ","End":"08:10.670","Text":"Have we got a value no matter what happens?"},{"Start":"08:10.670 ","End":"08:14.050","Text":"Yes, because if we found the value,"},{"Start":"08:14.050 ","End":"08:16.925","Text":"it will return the position that it\u0027s found at."},{"Start":"08:16.925 ","End":"08:22.780","Text":"If right is not greater than or equal to left,"},{"Start":"08:22.780 ","End":"08:24.315","Text":"will return minus 1,"},{"Start":"08:24.315 ","End":"08:26.980","Text":"so we\u0027ve got a base case there."},{"Start":"08:26.990 ","End":"08:31.520","Text":"Here, we\u0027ve got some recursive cases which are basically saying,"},{"Start":"08:31.520 ","End":"08:34.850","Text":"if you didn\u0027t find the value and if we know now that the value"},{"Start":"08:34.850 ","End":"08:38.435","Text":"doesn\u0027t exist anywhere in the array because we\u0027ve gone all the way to the end of it."},{"Start":"08:38.435 ","End":"08:42.395","Text":"Then do a recursive search on a different part of the array,"},{"Start":"08:42.395 ","End":"08:43.805","Text":"half of the array,"},{"Start":"08:43.805 ","End":"08:47.086","Text":"either the left half or the right half,"},{"Start":"08:47.086 ","End":"08:49.565","Text":"and just do it all again."},{"Start":"08:49.565 ","End":"08:53.305","Text":"Let\u0027s see if that works."},{"Start":"08:53.305 ","End":"09:02.915","Text":"The first test we\u0027ve been asked to do is with a set of numbers in E1 which are 1-8."},{"Start":"09:02.915 ","End":"09:06.392","Text":"The bounds for this starting at 0, ending at 7,"},{"Start":"09:06.392 ","End":"09:08.930","Text":"so that will cover all of the elements in this array which is like"},{"Start":"09:08.930 ","End":"09:17.420","Text":"from 0,1,2,3,4,5,6,7 and the target value we are looking forward is 2."},{"Start":"09:17.420 ","End":"09:19.730","Text":"That should return 1,"},{"Start":"09:19.730 ","End":"09:23.886","Text":"which it does because 0,1 is the value 2,"},{"Start":"09:23.886 ","End":"09:26.870","Text":"and that was the target value we were looking for."},{"Start":"09:26.870 ","End":"09:28.790","Text":"That looks like it\u0027s worked."},{"Start":"09:28.790 ","End":"09:34.015","Text":"Let\u0027s do a couple more tests to see if it\u0027s exactly what we want."},{"Start":"09:34.015 ","End":"09:43.400","Text":"This next array is even numbers 2,4,6,8,10,12,14,16."},{"Start":"09:43.400 ","End":"09:45.590","Text":"Same length again, 8 elements,"},{"Start":"09:45.590 ","End":"09:48.950","Text":"so 0-7 are the indices."},{"Start":"09:48.950 ","End":"09:51.080","Text":"What\u0027s the value we\u0027re looking for now?"},{"Start":"09:51.080 ","End":"09:54.320","Text":"We\u0027re looking for 3, which doesn\u0027t exist in this set of"},{"Start":"09:54.320 ","End":"09:58.025","Text":"numbers so we\u0027d expect to see minus 1, which we do."},{"Start":"09:58.025 ","End":"10:06.990","Text":"The final test is with what looks like powers of 2, 1,2,4,8,16,32,64,128."},{"Start":"10:06.990 ","End":"10:13.710","Text":"There\u0027s 8 numbers again,"},{"Start":"10:13.710 ","End":"10:19.600","Text":"so we can between 0 and 7 and the target value this time is 32,"},{"Start":"10:19.600 ","End":"10:25.375","Text":"which we\u0027d expect to find at 0,1,2,3,4,5."},{"Start":"10:25.375 ","End":"10:28.735","Text":"Let\u0027s see if that happens and yeah, we do get 5."},{"Start":"10:28.735 ","End":"10:33.310","Text":"All of that seems to work and this is a very well-known algorithm,"},{"Start":"10:33.310 ","End":"10:37.120","Text":"the binary search algorithm but the way we\u0027re using recursion"},{"Start":"10:37.120 ","End":"10:41.770","Text":"here is to split the array in half if we haven\u0027t found the value,"},{"Start":"10:41.770 ","End":"10:44.200","Text":"that\u0027s the base case when we do find the value or"},{"Start":"10:44.200 ","End":"10:46.780","Text":"we\u0027ve looked at the whole array and we know it\u0027s not there,"},{"Start":"10:46.780 ","End":"10:48.190","Text":"we return minus 1."},{"Start":"10:48.190 ","End":"10:51.115","Text":"The recursive cases are different"},{"Start":"10:51.115 ","End":"10:54.310","Text":"depending on whether we\u0027re looking at the bottom half or the top half."},{"Start":"10:54.310 ","End":"11:00.565","Text":"We adjust the parameters here and recursively call binary search again."},{"Start":"11:00.565 ","End":"11:03.905","Text":"If you understand the binary search algorithm,"},{"Start":"11:03.905 ","End":"11:05.915","Text":"this should be fairly clear."},{"Start":"11:05.915 ","End":"11:07.925","Text":"If not, it\u0027s a little bit trickier."},{"Start":"11:07.925 ","End":"11:10.940","Text":"Have a little play-around, write some print statements here"},{"Start":"11:10.940 ","End":"11:14.960","Text":"and just trace the values through and you\u0027ll see what\u0027s going on."},{"Start":"11:14.960 ","End":"11:17.570","Text":"Particularly if you look at the values left and right,"},{"Start":"11:17.570 ","End":"11:22.160","Text":"and you\u0027ll see where it\u0027s searching in which area is searching of the array."},{"Start":"11:22.160 ","End":"11:24.095","Text":"That\u0027s it for this one."},{"Start":"11:24.095 ","End":"11:28.470","Text":"I will see you shortly for the next one. See you then."}],"ID":29820},{"Watched":false,"Name":"Exercise 6","Duration":"20m 11s","ChapterTopicVideoID":28405,"CourseChapterTopicPlaylistID":282563,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:04.230","Text":"Hey everyone. Welcome back to this final exercise on recursion."},{"Start":"00:04.230 ","End":"00:07.350","Text":"This one, we\u0027re told that a merge sort makes use"},{"Start":"00:07.350 ","End":"00:10.470","Text":"of recursion to break down an unsorted array"},{"Start":"00:10.470 ","End":"00:17.340","Text":"into individual elements and merges left-right pairs to reconstruct the sorted array."},{"Start":"00:17.340 ","End":"00:21.215","Text":"In Part a, we\u0027re told to create a method called merge sort."},{"Start":"00:21.215 ","End":"00:24.795","Text":"Which returns no values but takes"},{"Start":"00:24.795 ","End":"00:30.170","Text":"an integer array arr and the array length len as arguments."},{"Start":"00:30.170 ","End":"00:31.340","Text":"In Part b,"},{"Start":"00:31.340 ","End":"00:37.090","Text":"we\u0027re told that the base case is to return from the function when len is equal to 1."},{"Start":"00:37.090 ","End":"00:39.325","Text":"In Part C then,"},{"Start":"00:39.325 ","End":"00:42.545","Text":"we\u0027re told that the recursive case should do the following."},{"Start":"00:42.545 ","End":"00:44.780","Text":"Declare a local variable middle,"},{"Start":"00:44.780 ","End":"00:48.380","Text":"which is initialized with the value len divided by 2."},{"Start":"00:48.380 ","End":"00:52.535","Text":"Declare an integer array left of size middle,"},{"Start":"00:52.535 ","End":"00:57.695","Text":"and declare an integer array right of size len minus middle."},{"Start":"00:57.695 ","End":"01:04.085","Text":"Then using 2 consecutive for-loops were to copy the values from irr element"},{"Start":"01:04.085 ","End":"01:10.565","Text":"0 up to but not including middle into array left."},{"Start":"01:10.565 ","End":"01:18.435","Text":"Then the values from irr element middle up to len into array right."},{"Start":"01:18.435 ","End":"01:21.020","Text":"Then after the second of the 2 for-loops,"},{"Start":"01:21.020 ","End":"01:23.870","Text":"we make a recursive call to Merge Sort,"},{"Start":"01:23.870 ","End":"01:28.580","Text":"passing it the left array with a length of middle."},{"Start":"01:28.580 ","End":"01:32.510","Text":"Then were to make a second recursive call to Merge Sort,"},{"Start":"01:32.510 ","End":"01:37.880","Text":"passing it the right array with a length of len minus middle."},{"Start":"01:37.880 ","End":"01:43.460","Text":"Then finally we add the following line as the final line of the merge sort method."},{"Start":"01:43.460 ","End":"01:45.185","Text":"Merge brackets."},{"Start":"01:45.185 ","End":"01:47.924","Text":"Irr, left, right,"},{"Start":"01:47.924 ","End":"01:53.210","Text":"middle, len minus middle, close bracket semicolon."},{"Start":"01:53.210 ","End":"01:57.275","Text":"Then in Part D, we are told to create a second method called merge,"},{"Start":"01:57.275 ","End":"02:03.670","Text":"which returns no values but accepts as arguments 3 ints arrays called irr,"},{"Start":"02:03.670 ","End":"02:09.830","Text":"left and right, and 2 ints called left len and right len."},{"Start":"02:09.830 ","End":"02:11.495","Text":"In Part 1 of d,"},{"Start":"02:11.495 ","End":"02:14.615","Text":"inside the method that we\u0027ve just created called merge,"},{"Start":"02:14.615 ","End":"02:19.610","Text":"we create 3 integer index variables lr and a,"},{"Start":"02:19.610 ","End":"02:22.385","Text":"and we initialize all of them to 0."},{"Start":"02:22.385 ","End":"02:27.470","Text":"Then we create a while loop which continues whilst l is less than left,"},{"Start":"02:27.470 ","End":"02:31.055","Text":"len and r is less than, write len."},{"Start":"02:31.055 ","End":"02:37.185","Text":"Inside that loop, we compare the values at left l and right r,"},{"Start":"02:37.185 ","End":"02:42.980","Text":"and we put whichever is the smallest into arra and we increment"},{"Start":"02:42.980 ","End":"02:49.100","Text":"either l or r depending on which was copied to irra."},{"Start":"02:49.100 ","End":"02:52.190","Text":"Also, we increment a."},{"Start":"02:52.190 ","End":"02:54.410","Text":"After the first while loop."},{"Start":"02:54.410 ","End":"03:02.040","Text":"We add another while-loop to copy any remaining items from left to irr using left len."},{"Start":"03:02.040 ","End":"03:08.600","Text":"A final while loop to copy any remaining items from right to arr using right len."},{"Start":"03:08.600 ","End":"03:13.115","Text":"We then create a final method to test no return values are arguments,"},{"Start":"03:13.115 ","End":"03:15.155","Text":"we call that merge Test."},{"Start":"03:15.155 ","End":"03:21.170","Text":"Within it we declare an array initialized with 8 unsorted values of your own choosing."},{"Start":"03:21.170 ","End":"03:23.555","Text":"We call that merge sort function,"},{"Start":"03:23.555 ","End":"03:26.340","Text":"pass it your array and its length."},{"Start":"03:26.340 ","End":"03:29.675","Text":"Then use a loop to output the contents of your array"},{"Start":"03:29.675 ","End":"03:33.835","Text":"to check the arrays now assaulted in ascending order."},{"Start":"03:33.835 ","End":"03:39.410","Text":"Let\u0027s create the first of the 3 methods that make up this exercise."},{"Start":"03:39.410 ","End":"03:41.120","Text":"First one is called merge sort,"},{"Start":"03:41.120 ","End":"03:42.665","Text":"doesn\u0027t return any values,"},{"Start":"03:42.665 ","End":"03:45.665","Text":"but it takes an integer array."},{"Start":"03:45.665 ","End":"03:48.500","Text":"You\u0027re going to call R for array."},{"Start":"03:48.500 ","End":"03:53.045","Text":"And the array length is also passed in"},{"Start":"03:53.045 ","End":"03:57.620","Text":"len and it\u0027s actually not the array length of the whole array."},{"Start":"03:57.620 ","End":"03:58.970","Text":"It might be initially,"},{"Start":"03:58.970 ","End":"04:04.560","Text":"but it might be part of the array afterwards that\u0027s used in the recursive case."},{"Start":"04:04.560 ","End":"04:07.550","Text":"Don\u0027t assume it\u0027s going to be the length of a whole array only will be"},{"Start":"04:07.550 ","End":"04:10.760","Text":"the first time we call it recursively,"},{"Start":"04:10.760 ","End":"04:12.980","Text":"that value len will change."},{"Start":"04:12.980 ","End":"04:14.810","Text":"That\u0027s the first part."},{"Start":"04:14.810 ","End":"04:18.305","Text":"Part a, we go straight into the recursive case now,"},{"Start":"04:18.305 ","End":"04:19.730","Text":"sorry the base case."},{"Start":"04:19.730 ","End":"04:23.225","Text":"If len is equal to 1,"},{"Start":"04:23.225 ","End":"04:29.450","Text":"then we simply return and there\u0027s no values being returned but this gets"},{"Start":"04:29.450 ","End":"04:35.360","Text":"us out of infinite regression so it\u0027s base case is needed,"},{"Start":"04:35.360 ","End":"04:38.805","Text":"otherwise the whole thing will just go on forever."},{"Start":"04:38.805 ","End":"04:40.925","Text":"What is the recursive case?"},{"Start":"04:40.925 ","End":"04:44.840","Text":"The rest of the code is going to go into this recursive block here."},{"Start":"04:44.840 ","End":"04:48.995","Text":"There\u0027s quite a few things we need to do before we make the recursive call."},{"Start":"04:48.995 ","End":"04:51.440","Text":"In the first Part, C1,"},{"Start":"04:51.440 ","End":"04:55.140","Text":"we\u0027re asked to declare a local variable called middle,"},{"Start":"04:55.210 ","End":"05:01.265","Text":"which is initialized with the value len divided by 2."},{"Start":"05:01.265 ","End":"05:06.095","Text":"Obviously finding the midpoint of the array,"},{"Start":"05:06.095 ","End":"05:09.395","Text":"or the part of the array that we\u0027re going to sort."},{"Start":"05:09.395 ","End":"05:11.990","Text":"Then C2, we declare an integer array,"},{"Start":"05:11.990 ","End":"05:14.200","Text":"left off size middle."},{"Start":"05:14.200 ","End":"05:21.090","Text":"Integer array called left it\u0027s the new integer array of size middle."},{"Start":"05:21.090 ","End":"05:23.675","Text":"We calculate the size from middle,"},{"Start":"05:23.675 ","End":"05:26.900","Text":"and we\u0027re creating an array of that size."},{"Start":"05:26.900 ","End":"05:32.765","Text":"We do the same for another array called right but this time,"},{"Start":"05:32.765 ","End":"05:39.665","Text":"the value we need to use is middle minus len."},{"Start":"05:39.665 ","End":"05:43.115","Text":"That\u0027s Part 3 done."},{"Start":"05:43.115 ","End":"05:46.040","Text":"Now, we\u0027re going to copy the values from"},{"Start":"05:46.040 ","End":"05:51.035","Text":"the source array arr into these 2 arrays called left and right."},{"Start":"05:51.035 ","End":"05:54.890","Text":"We\u0027re going to use 2 consecutive for-loops to do this."},{"Start":"05:54.890 ","End":"06:01.730","Text":"The first one is going to copy values from array element 0 up to,"},{"Start":"06:01.730 ","End":"06:03.135","Text":"but not including middles."},{"Start":"06:03.135 ","End":"06:06.195","Text":"We want to go for I is less than middle."},{"Start":"06:06.195 ","End":"06:09.675","Text":"Increment I by 1 each time around the loop."},{"Start":"06:09.675 ","End":"06:17.210","Text":"Then we copy into the array left relevant value."},{"Start":"06:17.210 ","End":"06:24.660","Text":"We say left i equals array i."},{"Start":"06:24.660 ","End":"06:28.370","Text":"Then we do our second for-loop."},{"Start":"06:28.370 ","End":"06:32.935","Text":"We do the same but this time around,"},{"Start":"06:32.935 ","End":"06:41.200","Text":"we copy from middle to the end of the array or to lend into array right."},{"Start":"06:41.610 ","End":"06:46.370","Text":"We say i equals middle."},{"Start":"06:49.050 ","End":"06:54.205","Text":"We do up to the end of whatever it is that\u0027s been caused in the length."},{"Start":"06:54.205 ","End":"06:58.150","Text":"Not to the end of the whole array because we don\u0027t know the size of the whole array,"},{"Start":"06:58.150 ","End":"07:02.320","Text":"but just we\u0027ve been pulse the value in len and we go up to there."},{"Start":"07:02.320 ","End":"07:06.695","Text":"At the end of this little fragment of code,"},{"Start":"07:06.695 ","End":"07:09.350","Text":"we would have copied the whole array or"},{"Start":"07:09.350 ","End":"07:12.980","Text":"the part of the array that we\u0027re interested in into 2 different arrays,"},{"Start":"07:12.980 ","End":"07:15.530","Text":"left and right half into the array,"},{"Start":"07:15.530 ","End":"07:17.630","Text":"left half into the array, right."},{"Start":"07:17.630 ","End":"07:20.855","Text":"Now this logic here could easily trip you up."},{"Start":"07:20.855 ","End":"07:24.680","Text":"We actually want to subtract middle from i"},{"Start":"07:24.680 ","End":"07:29.090","Text":"inside this index variable because we\u0027re not starting at 0,"},{"Start":"07:29.090 ","End":"07:30.500","Text":"we\u0027re starting at middle."},{"Start":"07:30.500 ","End":"07:35.390","Text":"Remember, i is going to go from wherever we"},{"Start":"07:35.390 ","End":"07:40.535","Text":"are up to in the array called arr. That\u0027s fine."},{"Start":"07:40.535 ","End":"07:45.500","Text":"We\u0027ll leave that as i but it\u0027s going to go from the middle point of i up to len."},{"Start":"07:45.500 ","End":"07:52.055","Text":"Whereas we want to write from the 0th element of right up to wherever we get to."},{"Start":"07:52.055 ","End":"07:53.690","Text":"We do I minus middle here."},{"Start":"07:53.690 ","End":"07:57.799","Text":"We could\u0027ve used in another variable j if we wanted to as a pointer into right,"},{"Start":"07:57.799 ","End":"08:00.365","Text":"but we can use I and just subtract middle from it,"},{"Start":"08:00.365 ","End":"08:05.090","Text":"and it will do the same thing so easily contribute up if you get that bit wrong there."},{"Start":"08:05.090 ","End":"08:12.975","Text":"There is up to 0.5 and now what we need to 2 is to 2.5."},{"Start":"08:12.975 ","End":"08:15.335","Text":"Off the second of the 2 for-loops,"},{"Start":"08:15.335 ","End":"08:19.895","Text":"we make a recursive call to Merge Sort."},{"Start":"08:19.895 ","End":"08:21.935","Text":"I\u0027m going to cool Merge so again,"},{"Start":"08:21.935 ","End":"08:24.765","Text":"what we\u0027re gonna do is we\u0027re going to pass it"},{"Start":"08:24.765 ","End":"08:29.400","Text":"the left array and the length we\u0027re going to pass as middle."},{"Start":"08:29.400 ","End":"08:35.120","Text":"We\u0027re saying from this array go up to middle and no further."},{"Start":"08:35.120 ","End":"08:37.970","Text":"Then we\u0027re going to do the same thing again."},{"Start":"08:37.970 ","End":"08:41.320","Text":"We\u0027re going to say do it from right."},{"Start":"08:41.320 ","End":"08:46.225","Text":"With a length of len minus middle."},{"Start":"08:46.225 ","End":"08:49.930","Text":"We progressively half the array and"},{"Start":"08:49.930 ","End":"08:54.280","Text":"sort half of the array until we get down to just 1 element."},{"Start":"08:54.280 ","End":"08:57.010","Text":"We just keep calling it until we\u0027ve only got 1 element."},{"Start":"08:57.010 ","End":"09:00.475","Text":"This is the bit where we\u0027re redoing some recursion."},{"Start":"09:00.475 ","End":"09:03.205","Text":"Then the final part, Part 7,"},{"Start":"09:03.205 ","End":"09:09.430","Text":"is to call a new method which we haven\u0027t written yet called merge."},{"Start":"09:09.430 ","End":"09:11.965","Text":"Pass it our original array."},{"Start":"09:11.965 ","End":"09:16.555","Text":"Then a left array and the right array but then also,"},{"Start":"09:16.555 ","End":"09:19.420","Text":"we\u0027re going to pass it middle and len"},{"Start":"09:19.420 ","End":"09:24.325","Text":"minus middle to tell it how long these sub-arrays are."},{"Start":"09:24.325 ","End":"09:26.920","Text":"That\u0027s that and it will give me"},{"Start":"09:26.920 ","End":"09:29.620","Text":"an error on this line because I haven\u0027t written merged yet."},{"Start":"09:29.620 ","End":"09:33.520","Text":"Let\u0027s go ahead and do Part D,"},{"Start":"09:33.520 ","End":"09:36.580","Text":"which is to create the next pumped."},{"Start":"09:36.580 ","End":"09:44.480","Text":"Part D then is to create method merge returns nothing again,"},{"Start":"09:45.630 ","End":"09:52.250","Text":"but does have number of array arguments that go in."},{"Start":"09:52.250 ","End":"09:56.175","Text":"This one, we\u0027ll call array next one we\u0027ll"},{"Start":"09:56.175 ","End":"10:02.380","Text":"call left and and the next one will call right."},{"Start":"10:02.380 ","End":"10:04.265","Text":"Then what we\u0027ve got,"},{"Start":"10:04.265 ","End":"10:06.710","Text":"which we didn\u0027t have in previous methods,"},{"Start":"10:06.710 ","End":"10:11.480","Text":"where the length of the 2 arrays and equal those left"},{"Start":"10:11.480 ","End":"10:21.250","Text":"len and the right lend giving the datatype pair."},{"Start":"10:23.060 ","End":"10:26.900","Text":"What are we to do in Part D?"},{"Start":"10:26.900 ","End":"10:29.960","Text":"We first of all create some index variables,"},{"Start":"10:29.960 ","End":"10:40.385","Text":"lr and a initialize them all to 0. l is 0 and r is 0 and a is 0."},{"Start":"10:40.385 ","End":"10:44.855","Text":"Then we now do a while loop."},{"Start":"10:44.855 ","End":"10:48.590","Text":"Which continues whilst l is less than left,"},{"Start":"10:48.590 ","End":"10:55.940","Text":"len and r is less than right len hence why we were passed in the left lane and"},{"Start":"10:55.940 ","End":"11:03.858","Text":"right len both of those conditions have to be true for this while loop to continue."},{"Start":"11:03.858 ","End":"11:07.150","Text":"There\u0027s our while loop created."},{"Start":"11:07.150 ","End":"11:13.330","Text":"Now inside the loop we\u0027re told in D3 we compare the values at left l,"},{"Start":"11:13.330 ","End":"11:14.620","Text":"and the right r,"},{"Start":"11:14.620 ","End":"11:17.620","Text":"whichever is the smallest we put into array."},{"Start":"11:17.620 ","End":"11:22.720","Text":"We increment either l or r depending on which was copied into our array."},{"Start":"11:22.720 ","End":"11:25.910","Text":"Then we increment the array pointer, a."},{"Start":"11:25.980 ","End":"11:30.579","Text":"We\u0027re going to need to compare these elements."},{"Start":"11:30.579 ","End":"11:36.070","Text":"Left l, is it greater than right r?"},{"Start":"11:36.070 ","End":"11:37.750","Text":"What we\u0027re saying is we\u0027re comparing"},{"Start":"11:37.750 ","End":"11:41.095","Text":"the leftmost value in each of the arrays, left and right."},{"Start":"11:41.095 ","End":"11:45.775","Text":"That\u0027s got to be the smallest value in the final array."},{"Start":"11:45.775 ","End":"11:49.075","Text":"We need to swap it in the leftmost position if that\u0027s not right."},{"Start":"11:49.075 ","End":"11:53.230","Text":"If this number was bigger than the rightmost number,"},{"Start":"11:53.230 ","End":"11:57.625","Text":"then we want to copy the rightmost number into the left-hand side,"},{"Start":"11:57.625 ","End":"12:00.010","Text":"or vice versa if it\u0027s the other way round."},{"Start":"12:00.010 ","End":"12:10.255","Text":"Array a is going to be the rightmost number if left was greater than right."},{"Start":"12:10.255 ","End":"12:16.885","Text":"Otherwise, we\u0027re going to do the other value,"},{"Start":"12:16.885 ","End":"12:25.990","Text":"array a=left l. We\u0027re using r,"},{"Start":"12:25.990 ","End":"12:27.880","Text":"l, and a as index variables."},{"Start":"12:27.880 ","End":"12:33.940","Text":"We therefore got to increase them each time we write something into the array."},{"Start":"12:33.940 ","End":"12:36.160","Text":"In this case we need to do it for a,"},{"Start":"12:36.160 ","End":"12:38.830","Text":"but we also needed to do it for r here."},{"Start":"12:38.830 ","End":"12:41.410","Text":"In this case here,"},{"Start":"12:41.410 ","End":"12:43.960","Text":"we want to do it for a,"},{"Start":"12:43.960 ","End":"12:51.580","Text":"but we also want to do it for l. So a is incremented and l is incremented."},{"Start":"12:51.580 ","End":"12:54.955","Text":"Now, we could actually shorten this code a little bit"},{"Start":"12:54.955 ","End":"12:59.140","Text":"by using the increment inside these brackets."},{"Start":"12:59.140 ","End":"13:01.285","Text":"Remember this means post-increment."},{"Start":"13:01.285 ","End":"13:03.385","Text":"Use whatever the value of a is,"},{"Start":"13:03.385 ","End":"13:07.495","Text":"and then it will increase a by 1 after this line is run,"},{"Start":"13:07.495 ","End":"13:11.050","Text":"and the same for r. We can actually remove"},{"Start":"13:11.050 ","End":"13:12.760","Text":"these 2 lines and put them inside"},{"Start":"13:12.760 ","End":"13:15.627","Text":"the brackets because the increment will happen afterwards,"},{"Start":"13:15.627 ","End":"13:19.255","Text":"because that\u0027s what a post-increment operator does."},{"Start":"13:19.255 ","End":"13:22.405","Text":"We may as well do that here as well."},{"Start":"13:22.405 ","End":"13:25.090","Text":"Look at that other code, is a little bit clearer."},{"Start":"13:25.090 ","End":"13:29.175","Text":"We can just make our overall code a little bit shorter,"},{"Start":"13:29.175 ","End":"13:32.200","Text":"and therefore probably a little bit more readable."},{"Start":"13:32.200 ","End":"13:35.035","Text":"There\u0027s our while loop done."},{"Start":"13:35.035 ","End":"13:36.880","Text":"After the first while loop,"},{"Start":"13:36.880 ","End":"13:40.270","Text":"we\u0027re told in D4 we add another while loop to copy"},{"Start":"13:40.270 ","End":"13:45.725","Text":"any remaining items from left to array using leftLen."},{"Start":"13:45.725 ","End":"13:50.535","Text":"It\u0027s possible that we haven\u0027t copied all the characters over."},{"Start":"13:50.535 ","End":"13:59.005","Text":"We\u0027ll know when we haven\u0027t because either l or r will equal to leftLen or rightLen."},{"Start":"13:59.005 ","End":"14:00.730","Text":"It doesn\u0027t have to be both of them, because it\u0027s logical and,"},{"Start":"14:00.730 ","End":"14:04.930","Text":"so it could have a character leftover in the left array or the right array."},{"Start":"14:04.930 ","End":"14:08.680","Text":"The easiest way to do that is to assume we\u0027ve got more than 1."},{"Start":"14:08.680 ","End":"14:10.480","Text":"If we haven\u0027t even got 1,"},{"Start":"14:10.480 ","End":"14:12.010","Text":"it will just skip the loop."},{"Start":"14:12.010 ","End":"14:17.545","Text":"We\u0027ll say while left is less than leftLen,"},{"Start":"14:17.545 ","End":"14:24.310","Text":"copy the items from left into a array."},{"Start":"14:24.310 ","End":"14:25.735","Text":"That will do the job."},{"Start":"14:25.735 ","End":"14:32.350","Text":"Any of these stray characters leftover will now be copied into the output array, arr."},{"Start":"14:32.350 ","End":"14:38.035","Text":"Then we do the same thing for the right array."},{"Start":"14:38.035 ","End":"14:47.215","Text":"That is part 4 done and part 5 done. That\u0027s good."},{"Start":"14:47.215 ","End":"14:48.940","Text":"What we need to do now is part E,"},{"Start":"14:48.940 ","End":"14:51.370","Text":"to create a method to test both of these."},{"Start":"14:51.370 ","End":"14:53.200","Text":"We could run a merge sort,"},{"Start":"14:53.200 ","End":"14:55.345","Text":"but we wouldn\u0027t see the output."},{"Start":"14:55.345 ","End":"14:57.505","Text":"We wouldn\u0027t see the array sorted."},{"Start":"14:57.505 ","End":"15:00.400","Text":"Let\u0027s create an array and do the sort,"},{"Start":"15:00.400 ","End":"15:02.635","Text":"and then print it out afterwards."},{"Start":"15:02.635 ","End":"15:06.620","Text":"That\u0027s what we\u0027ve been asked to do here."},{"Start":"15:06.660 ","End":"15:09.340","Text":"Let\u0027s first of all declare our array."},{"Start":"15:09.340 ","End":"15:12.655","Text":"I\u0027ll call it testArray."},{"Start":"15:12.655 ","End":"15:15.730","Text":"Let\u0027s put some values in it."},{"Start":"15:15.730 ","End":"15:17.440","Text":"Put any values you like in,"},{"Start":"15:17.440 ","End":"15:20.770","Text":"but do mix them up so it\u0027s not already sorted."},{"Start":"15:20.770 ","End":"15:22.915","Text":"We put 8 in,"},{"Start":"15:22.915 ","End":"15:27.970","Text":"and put some small numbers at the end so that they get moved all the way up to the top,"},{"Start":"15:27.970 ","End":"15:30.925","Text":"and some large numbers at the front here."},{"Start":"15:30.925 ","End":"15:33.370","Text":"That looks reasonable to test with."},{"Start":"15:33.370 ","End":"15:36.700","Text":"Now we\u0027re going to call our method called mergeSort."},{"Start":"15:36.700 ","End":"15:38.185","Text":"We pass the r array,"},{"Start":"15:38.185 ","End":"15:40.310","Text":"in this case called testArray."},{"Start":"15:40.310 ","End":"15:45.560","Text":"We pass it a length of testArray because we want to sort a full array."},{"Start":"15:45.560 ","End":"15:51.445","Text":"Then we\u0027re going to just do a little loop here to output all the elements of the array."},{"Start":"15:51.445 ","End":"15:54.415","Text":"I\u0027ll output them all on the same line."},{"Start":"15:54.415 ","End":"15:59.650","Text":"The element we\u0027re interested in is testArray i,"},{"Start":"15:59.650 ","End":"16:03.010","Text":"and then we\u0027ll just output a space afterwards."},{"Start":"16:03.010 ","End":"16:06.310","Text":"Let\u0027s see if that compiles first of all."},{"Start":"16:06.310 ","End":"16:08.350","Text":"Got an error here."},{"Start":"16:08.350 ","End":"16:11.140","Text":"Forgot to put the equals sign."},{"Start":"16:11.140 ","End":"16:13.731","Text":"Let\u0027s see if the code works."},{"Start":"16:13.731 ","End":"16:15.310","Text":"There\u0027s an awful lot that can go wrong,"},{"Start":"16:15.310 ","End":"16:19.495","Text":"so I won\u0027t be surprised if there\u0027s another error in here somewhere but let\u0027s have a go."},{"Start":"16:19.495 ","End":"16:22.105","Text":"We do have an error up here."},{"Start":"16:22.105 ","End":"16:24.295","Text":"Let\u0027s just have a look at what that is."},{"Start":"16:24.295 ","End":"16:27.850","Text":"When we were declaring our array called right,"},{"Start":"16:27.850 ","End":"16:31.000","Text":"which was C3,"},{"Start":"16:31.000 ","End":"16:33.925","Text":"it doesn\u0027t like middle minus len,"},{"Start":"16:33.925 ","End":"16:36.730","Text":"it should have been len minus middle."},{"Start":"16:36.730 ","End":"16:40.810","Text":"That\u0027s fine. We can make that correction."},{"Start":"16:40.810 ","End":"16:44.560","Text":"Then see if that now is successful."},{"Start":"16:44.560 ","End":"16:49.090","Text":"Hey presto, we seem to have got a sorted output."},{"Start":"16:49.090 ","End":"16:51.370","Text":"Again, it almost looks like magic, doesn\u0027t it?"},{"Start":"16:51.370 ","End":"16:53.110","Text":"It looks too good to be true."},{"Start":"16:53.110 ","End":"16:54.760","Text":"How can this work?"},{"Start":"16:54.760 ","End":"16:59.755","Text":"As ever, it\u0027s worth probably putting some debug code in."},{"Start":"16:59.755 ","End":"17:07.315","Text":"In fact it probably be good idea to put the sorting steps in here when we do the merge."},{"Start":"17:07.315 ","End":"17:14.620","Text":"Each time we do a merge perhaps we print out the results of the array here."},{"Start":"17:14.620 ","End":"17:17.665","Text":"That might be something we could do."},{"Start":"17:17.665 ","End":"17:20.455","Text":"The array of course, testArray is not available."},{"Start":"17:20.455 ","End":"17:22.855","Text":"It will be called arr,"},{"Start":"17:22.855 ","End":"17:26.095","Text":"up here. That should compile."},{"Start":"17:26.095 ","End":"17:31.090","Text":"What we probably also want to do is when we make our calls,"},{"Start":"17:31.090 ","End":"17:38.395","Text":"maybe we could just put in the length of the array that we\u0027re trying to sort."},{"Start":"17:38.395 ","End":"17:40.810","Text":"That\u0027s really the only thing that changes."},{"Start":"17:40.810 ","End":"17:43.480","Text":"Let\u0027s print a message,"},{"Start":"17:43.480 ","End":"17:45.490","Text":"so this left one."},{"Start":"17:45.490 ","End":"17:50.605","Text":"Then what we\u0027re going to print is the length that we pass in called the middle."},{"Start":"17:50.605 ","End":"17:54.940","Text":"We\u0027ll do the same thing down here before we run"},{"Start":"17:54.940 ","End":"17:59.950","Text":"this merge sort line here but we\u0027ll print right instead of left."},{"Start":"17:59.950 ","End":"18:03.160","Text":"We\u0027ll print len minus middle instead of middle,"},{"Start":"18:03.160 ","End":"18:05.155","Text":"because that\u0027s the value we\u0027re passing in."},{"Start":"18:05.155 ","End":"18:07.500","Text":"Let\u0027s put that in brackets,"},{"Start":"18:07.500 ","End":"18:09.715","Text":"or this print statement will complain."},{"Start":"18:09.715 ","End":"18:12.000","Text":"Let\u0027s have a look at what happens now."},{"Start":"18:12.000 ","End":"18:16.900","Text":"We should get a bit more of an insight into how the recursion is happening,"},{"Start":"18:16.900 ","End":"18:19.480","Text":"what\u0027s taking place. There you go."},{"Start":"18:19.480 ","End":"18:24.280","Text":"You can see that we are successively breaking it down into steps."},{"Start":"18:24.280 ","End":"18:30.205","Text":"The first elements of the array are broken down into just 1 item."},{"Start":"18:30.205 ","End":"18:33.145","Text":"We had 4 item, then 2 item, then 1 item."},{"Start":"18:33.145 ","End":"18:38.710","Text":"Then on the right-hand side we do a merge right of only 1 item."},{"Start":"18:38.710 ","End":"18:43.450","Text":"We\u0027ve got 45 and 87 have been swapped over."},{"Start":"18:43.450 ","End":"18:47.785","Text":"Then we\u0027ll do 12 and 89,"},{"Start":"18:47.785 ","End":"18:53.095","Text":"and merge those with this part here, 45 and 87."},{"Start":"18:53.095 ","End":"18:54.685","Text":"You\u0027ve seen part of it there."},{"Start":"18:54.685 ","End":"18:56.950","Text":"We\u0027re seeing each time we do part of"},{"Start":"18:56.950 ","End":"19:01.780","Text":"the merging we\u0027re seeing the output array here each time,"},{"Start":"19:01.780 ","End":"19:06.985","Text":"and eventually we\u0027ll see a final result there."},{"Start":"19:06.985 ","End":"19:09.790","Text":"You can see the recursive calls taking place."},{"Start":"19:09.790 ","End":"19:12.100","Text":"You can see what length is being used at the end,"},{"Start":"19:12.100 ","End":"19:14.395","Text":"these numbers here, whether it\u0027s left or right,"},{"Start":"19:14.395 ","End":"19:16.120","Text":"and the output array."},{"Start":"19:16.120 ","End":"19:18.610","Text":"It\u0027s not quite sorted at these points here."},{"Start":"19:18.610 ","End":"19:21.385","Text":"Eventually we get a sorted array at the end of it."},{"Start":"19:21.385 ","End":"19:24.850","Text":"The algorithm once again is quite well understood."},{"Start":"19:24.850 ","End":"19:26.230","Text":"Classic algorithm."},{"Start":"19:26.230 ","End":"19:28.790","Text":"The recursion this time is very interesting,"},{"Start":"19:28.790 ","End":"19:30.280","Text":"in that what we\u0027re doing is we\u0027re doing"},{"Start":"19:30.280 ","End":"19:32.500","Text":"a merge sort here and we\u0027re doing a merge sort here,"},{"Start":"19:32.500 ","End":"19:35.260","Text":"and we\u0027re calling the function back again."},{"Start":"19:35.260 ","End":"19:38.485","Text":"We\u0027re also calling a completely different function."},{"Start":"19:38.485 ","End":"19:40.780","Text":"So this definition is not calling itself,"},{"Start":"19:40.780 ","End":"19:42.994","Text":"it\u0027s calling another function here to do the merge,"},{"Start":"19:42.994 ","End":"19:46.060","Text":"but you\u0027ll see that that\u0027s being done recursively as"},{"Start":"19:46.060 ","End":"19:49.870","Text":"well because of the way it all unravels."},{"Start":"19:49.870 ","End":"19:51.580","Text":"It does get called multiple times,"},{"Start":"19:51.580 ","End":"19:53.665","Text":"it doesn\u0027t just get called once."},{"Start":"19:53.665 ","End":"19:57.670","Text":"A very interesting combination of techniques there,"},{"Start":"19:57.670 ","End":"20:00.970","Text":"but similar in some ways to the binary search."},{"Start":"20:00.970 ","End":"20:03.000","Text":"We\u0027re dividing our domain into half each time,"},{"Start":"20:03.000 ","End":"20:07.930","Text":"but this time applied to sorting a set of numbers."},{"Start":"20:07.930 ","End":"20:09.694","Text":"Thanks very much for watching."},{"Start":"20:09.694 ","End":"20:12.080","Text":"I\u0027ll see you soon."}],"ID":29821}],"Thumbnail":null,"ID":282563}]