{"Free":0,"Sample":1,"Paid":2}
[{"Name":"Object Orientation","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Introduction to object orientation","Duration":"6m 32s","ChapterTopicVideoID":28780,"CourseChapterTopicPlaylistID":287464,"HasSubtitles":true,"ThumbnailPath":"https://www.proprep.uk/Images/Videos_Thumbnails/28780.jpeg","UploadDate":"2022-04-10T06:49:48.1630000","DurationForVideoObject":"PT6M32S","Description":null,"MetaTitle":"Introduction to object orientation: Video + Workbook | Proprep","MetaDescription":"Object Oriented Programming - Object Orientation. 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/object-oriented-programming/object-orientation/vid30279","VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:03.310","Text":"Hello, welcome to this video on object orientation."},{"Start":"00:03.310 ","End":"00:06.100","Text":"By the end of this section, you\u0027ll be able to explain the relationship"},{"Start":"00:06.100 ","End":"00:08.980","Text":"between procedural and object-oriented programming,"},{"Start":"00:08.980 ","End":"00:13.075","Text":"to relate the concepts of encapsulation and information hiding,"},{"Start":"00:13.075 ","End":"00:19.405","Text":"and to apply the object-oriented principles to encapsulate data and code."},{"Start":"00:19.405 ","End":"00:22.600","Text":"We previously established all the basic principles"},{"Start":"00:22.600 ","End":"00:25.045","Text":"of programming in a typical high-level language."},{"Start":"00:25.045 ","End":"00:28.210","Text":"We developed small units of code which we eventually package into"},{"Start":"00:28.210 ","End":"00:31.855","Text":"individual subprograms called procedures or functions."},{"Start":"00:31.855 ","End":"00:34.735","Text":"Each sub-program performed 1 specific task,"},{"Start":"00:34.735 ","End":"00:37.540","Text":"and we put several subprograms together to form"},{"Start":"00:37.540 ","End":"00:41.350","Text":"a complete program that achieved some overall objective,"},{"Start":"00:41.350 ","End":"00:44.750","Text":"in this example to create a new account on a website."},{"Start":"00:44.750 ","End":"00:47.120","Text":"Now our goal is to change our worldview from"},{"Start":"00:47.120 ","End":"00:50.110","Text":"procedure-based to an object-oriented outlook."},{"Start":"00:50.110 ","End":"00:52.625","Text":"The first question might be, why?"},{"Start":"00:52.625 ","End":"00:55.235","Text":"What\u0027s wrong with a procedure-based approach?"},{"Start":"00:55.235 ","End":"00:59.540","Text":"Well, nothing. It\u0027s a perfectly acceptable way of developing programs and might still be"},{"Start":"00:59.540 ","End":"01:04.360","Text":"appropriate in some circumstances but there are certainly some issues."},{"Start":"01:04.360 ","End":"01:09.185","Text":"As high-level programming languages began to proliferate in the \u002760s and \u002770s,"},{"Start":"01:09.185 ","End":"01:11.660","Text":"larger more complex systems were being built"},{"Start":"01:11.660 ","End":"01:14.150","Text":"and more and more lines of code were being written."},{"Start":"01:14.150 ","End":"01:15.690","Text":"To minimize development time,"},{"Start":"01:15.690 ","End":"01:20.005","Text":"multiple programmers would work in parallel on different aspects of the code,"},{"Start":"01:20.005 ","End":"01:22.850","Text":"and sometimes there will be issues where changes to one piece of"},{"Start":"01:22.850 ","End":"01:27.155","Text":"seemingly isolated code would affect other parts of the program."},{"Start":"01:27.155 ","End":"01:30.755","Text":"Procedural program was developed in order to solve this problem,"},{"Start":"01:30.755 ","End":"01:34.070","Text":"but it perhaps didn\u0027t go far enough in encouraging techniques that"},{"Start":"01:34.070 ","End":"01:37.460","Text":"would contribute to reliable and reusable code."},{"Start":"01:37.460 ","End":"01:39.680","Text":"One of the issues was that data being used by"},{"Start":"01:39.680 ","End":"01:42.650","Text":"the subprograms in the form of global variables or"},{"Start":"01:42.650 ","End":"01:48.590","Text":"other global data entities such as arrays could be modified by any part of the code."},{"Start":"01:48.590 ","End":"01:52.280","Text":"If one programmer made changes to their code that affected"},{"Start":"01:52.280 ","End":"01:56.120","Text":"some of this global data in a way that other programmers weren\u0027t expecting,"},{"Start":"01:56.120 ","End":"01:58.865","Text":"it could cause the program to have issues."},{"Start":"01:58.865 ","End":"02:02.170","Text":"Imagine this trivial code is part of a game."},{"Start":"02:02.170 ","End":"02:08.010","Text":"The addPoints subroutine adds 10 points to the score at a time."},{"Start":"02:08.010 ","End":"02:13.645","Text":"AddBonusPoints subroutine also adds 10 points at a time,"},{"Start":"02:13.645 ","End":"02:18.220","Text":"and a checkLevelUp subroutine moves you to the next level"},{"Start":"02:18.220 ","End":"02:22.565","Text":"each time the score hits an exact multiple of 100 points."},{"Start":"02:22.565 ","End":"02:26.645","Text":"Any of these subroutines can be called a relevant time in our program,"},{"Start":"02:26.645 ","End":"02:29.840","Text":"maybe from different places in the program as well."},{"Start":"02:29.840 ","End":"02:33.530","Text":"This all works fine initially but if at some point a programmer"},{"Start":"02:33.530 ","End":"02:37.415","Text":"changed the bonus points to a number other than 10,"},{"Start":"02:37.415 ","End":"02:42.935","Text":"it would break the code in checkLevelUp where you go to a new level every 100 points."},{"Start":"02:42.935 ","End":"02:45.170","Text":"For example, if the current score was 90,"},{"Start":"02:45.170 ","End":"02:49.580","Text":"and we added bonus points of 20 we\u0027d go straight to 110,"},{"Start":"02:49.580 ","End":"02:52.370","Text":"and checkLevelUp would not detect the crossing of"},{"Start":"02:52.370 ","End":"02:55.475","Text":"the boundary of 100 and it wouldn\u0027t increase the level."},{"Start":"02:55.475 ","End":"02:58.970","Text":"This is a deliberately simplistic scenario so you get"},{"Start":"02:58.970 ","End":"03:03.500","Text":"the idea and can be fixed relatively easily but it does highlight the issue"},{"Start":"03:03.500 ","End":"03:07.445","Text":"that relatively minor changes in one part of the code can cause issues"},{"Start":"03:07.445 ","End":"03:11.839","Text":"elsewhere when common data is being accessed and modified directly."},{"Start":"03:11.839 ","End":"03:16.385","Text":"This issue was addressed in newer languages developed from the late \u002760s onwards."},{"Start":"03:16.385 ","End":"03:21.290","Text":"These languages attempted to resolve the problem through a concept known as an object."},{"Start":"03:21.290 ","End":"03:25.040","Text":"Object built on the idea of procedural programming by keeping"},{"Start":"03:25.040 ","End":"03:29.090","Text":"the emphasis on subprograms as a means of organizing code,"},{"Start":"03:29.090 ","End":"03:34.760","Text":"but with additional safeguards to prevent shared data causing issues."},{"Start":"03:34.760 ","End":"03:39.110","Text":"This move to objects meant a shift in our thinking when designing programs."},{"Start":"03:39.110 ","End":"03:43.400","Text":"Our physical world can be considered to be made up of objects."},{"Start":"03:43.400 ","End":"03:47.660","Text":"These objects can be categorized or classified into types."},{"Start":"03:47.660 ","End":"03:52.130","Text":"For example, there are animals which we can classify into the groups mammals,"},{"Start":"03:52.130 ","End":"03:54.155","Text":"birds, reptiles, and so on."},{"Start":"03:54.155 ","End":"03:57.110","Text":"We can further classify within each of those groups."},{"Start":"03:57.110 ","End":"03:58.835","Text":"For example, within mammals,"},{"Start":"03:58.835 ","End":"04:00.455","Text":"we could have primates,"},{"Start":"04:00.455 ","End":"04:03.845","Text":"cetaceans, rodents, and so on."},{"Start":"04:03.845 ","End":"04:07.430","Text":"Ultimately, we end up in a situation where we can go no further,"},{"Start":"04:07.430 ","End":"04:10.400","Text":"we\u0027re down to individual animals of a particular type,"},{"Start":"04:10.400 ","End":"04:12.350","Text":"for example, a kangaroo."},{"Start":"04:12.350 ","End":"04:18.575","Text":"We can now refer to an object of type kangaroo or of type koala."},{"Start":"04:18.575 ","End":"04:23.060","Text":"This physical object of a kangaroo in the real world can be"},{"Start":"04:23.060 ","End":"04:28.415","Text":"modeled or represented in a program as a virtual kangaroo object."},{"Start":"04:28.415 ","End":"04:32.240","Text":"We might do this if we were writing a simulation program, for example,"},{"Start":"04:32.240 ","End":"04:37.220","Text":"maybe looking at how kangaroo populations develop under particular circumstances."},{"Start":"04:37.220 ","End":"04:39.080","Text":"Using this object-based approach,"},{"Start":"04:39.080 ","End":"04:41.420","Text":"we can easily change the program so we could add"},{"Start":"04:41.420 ","End":"04:44.390","Text":"koala populations into the simulation as well."},{"Start":"04:44.390 ","End":"04:47.900","Text":"Although koalas and kangaroos might behave differently in some ways,"},{"Start":"04:47.900 ","End":"04:52.715","Text":"the things we\u0027d want to do in a simulation might apply to both types of objects."},{"Start":"04:52.715 ","End":"04:54.290","Text":"For example, in this simulation,"},{"Start":"04:54.290 ","End":"04:55.985","Text":"the loss of food sources,"},{"Start":"04:55.985 ","End":"04:59.425","Text":"introduction of predators, forest fires, and so on."},{"Start":"04:59.425 ","End":"05:03.485","Text":"An object-oriented program is built from objects."},{"Start":"05:03.485 ","End":"05:08.915","Text":"Objects interact with other objects to achieve the desired outcome of the program."},{"Start":"05:08.915 ","End":"05:10.880","Text":"This example relates easily to"},{"Start":"05:10.880 ","End":"05:16.910","Text":"real-world physical objects but objects can be used to model more abstract ideas as well."},{"Start":"05:16.910 ","End":"05:19.070","Text":"For example, here\u0027s a typical arrangement of"},{"Start":"05:19.070 ","End":"05:24.139","Text":"objects used to create a program that uses a graphical user interface."},{"Start":"05:24.139 ","End":"05:27.500","Text":"The objects don\u0027t represent anything we can directly relate to"},{"Start":"05:27.500 ","End":"05:32.755","Text":"the physical world but each performs a useful task in the overall program."},{"Start":"05:32.755 ","End":"05:35.325","Text":"The highlighted object here, controller,"},{"Start":"05:35.325 ","End":"05:39.350","Text":"is used to coordinate the actions of other objects."},{"Start":"05:39.350 ","End":"05:43.370","Text":"Model is responsible for storing and retrieving data,"},{"Start":"05:43.370 ","End":"05:45.265","Text":"and carrying out processes."},{"Start":"05:45.265 ","End":"05:47.200","Text":"View, the final object,"},{"Start":"05:47.200 ","End":"05:52.055","Text":"is responsible for collecting user input and sending output to the screen."},{"Start":"05:52.055 ","End":"05:56.495","Text":"Some of the key ideas in object-oriented programming are as follows."},{"Start":"05:56.495 ","End":"06:00.695","Text":"Objects comprise private data and public processes."},{"Start":"06:00.695 ","End":"06:04.505","Text":"Objects communicate with other objects via messages."},{"Start":"06:04.505 ","End":"06:08.420","Text":"Objects can be extended through a process called inheritance."},{"Start":"06:08.420 ","End":"06:12.800","Text":"Objects can vary behavior through a process called polymorphism,"},{"Start":"06:12.800 ","End":"06:18.245","Text":"and object orientation encourages modularity and reusability."},{"Start":"06:18.245 ","End":"06:21.740","Text":"We\u0027ll explore all of these ideas as we move through this chapter."},{"Start":"06:21.740 ","End":"06:24.770","Text":"In the next video, we\u0027ll look at fundamental concept within"},{"Start":"06:24.770 ","End":"06:28.085","Text":"object orientation known as encapsulation."},{"Start":"06:28.085 ","End":"06:32.040","Text":"Thanks for watching and see you shortly for that next video."}],"ID":30279},{"Watched":false,"Name":"Encapsulation","Duration":"6m 22s","ChapterTopicVideoID":28781,"CourseChapterTopicPlaylistID":287464,"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.395","Text":"Hello, welcome back."},{"Start":"00:01.395 ","End":"00:06.435","Text":"One of the cornerstones of the object oriented philosophy is the idea of encapsulation,"},{"Start":"00:06.435 ","End":"00:11.310","Text":"the linking together of state and behavior within a single object."},{"Start":"00:11.310 ","End":"00:14.790","Text":"We encountered the idea of state earlier in the course."},{"Start":"00:14.790 ","End":"00:19.500","Text":"Variables are used in the procedural programming paradigm to hold state"},{"Start":"00:19.500 ","End":"00:24.435","Text":"so a program is in a particular state at any particular instant in time,"},{"Start":"00:24.435 ","End":"00:27.180","Text":"according to the contents of its variables."},{"Start":"00:27.180 ","End":"00:29.820","Text":"Behavior refers to the operations that can be"},{"Start":"00:29.820 ","End":"00:33.600","Text":"performed by sequences of program instructions."},{"Start":"00:33.600 ","End":"00:37.370","Text":"The idea of encapsulation is objects have"},{"Start":"00:37.370 ","End":"00:43.340","Text":"their own variables and their own code which operates on those variables."},{"Start":"00:43.340 ","End":"00:47.495","Text":"To give an example, let\u0027s say we\u0027re modeling a clock as an object."},{"Start":"00:47.495 ","End":"00:53.345","Text":"We\u0027d need state variables to hold the time in hours, minutes, and seconds."},{"Start":"00:53.345 ","End":"00:58.910","Text":"The behavior of the clock might consist of running the clock or adjusting the time."},{"Start":"00:58.910 ","End":"01:01.670","Text":"Encapsulation encompasses the idea that"},{"Start":"01:01.670 ","End":"01:05.975","Text":"the object\u0027s state and its behavior are inextricably linked."},{"Start":"01:05.975 ","End":"01:09.065","Text":"You can\u0027t separate one from the other."},{"Start":"01:09.065 ","End":"01:11.300","Text":"We can\u0027t run update,"},{"Start":"01:11.300 ","End":"01:15.710","Text":"or set the clock if we have no state variables to store the timing."},{"Start":"01:15.710 ","End":"01:21.605","Text":"The variables are only useful if they\u0027re being accessed and updated by the code."},{"Start":"01:21.605 ","End":"01:25.070","Text":"A related concept is information hiding."},{"Start":"01:25.070 ","End":"01:29.225","Text":"This is mechanisms for protecting state behind an interface."},{"Start":"01:29.225 ","End":"01:33.950","Text":"State changes can only be made via the interface."},{"Start":"01:33.950 ","End":"01:38.450","Text":"Information hiding reinforces the encapsulation."},{"Start":"01:38.450 ","End":"01:42.545","Text":"Not only are we bundling code and data together in an object,"},{"Start":"01:42.545 ","End":"01:48.355","Text":"we don\u0027t make the state information inside the object visible outside."},{"Start":"01:48.355 ","End":"01:50.555","Text":"Using that clock example again,"},{"Start":"01:50.555 ","End":"01:53.840","Text":"the state variables, hours, minutes, and seconds,"},{"Start":"01:53.840 ","End":"01:59.750","Text":"are not directly accessible outside the objects and are hidden away within it."},{"Start":"01:59.750 ","End":"02:04.775","Text":"They can only be accessed by the public interface methods outside."},{"Start":"02:04.775 ","End":"02:09.230","Text":"The variable cannot directly be read or changed."},{"Start":"02:09.230 ","End":"02:13.430","Text":"When the program first runs you\u0027d want to set the initial time of"},{"Start":"02:13.430 ","End":"02:18.620","Text":"the clock because you cannot access the state variables inside the objects."},{"Start":"02:18.620 ","End":"02:23.795","Text":"You have to go through the provided interface method here called set,"},{"Start":"02:23.795 ","End":"02:28.430","Text":"and you pass in the relevant values to set the time."},{"Start":"02:28.430 ","End":"02:31.250","Text":"This is the essence of information hiding."},{"Start":"02:31.250 ","End":"02:36.110","Text":"We don\u0027t allow direct access to the variables to outside code,"},{"Start":"02:36.110 ","End":"02:40.730","Text":"but code within the object can access the variables as"},{"Start":"02:40.730 ","End":"02:45.560","Text":"well as supporting the idea of encapsulation you can see why this might be a good idea."},{"Start":"02:45.560 ","End":"02:48.110","Text":"We can provide some code inside the set which"},{"Start":"02:48.110 ","End":"02:51.245","Text":"checks whether the time values are sensible for example."},{"Start":"02:51.245 ","End":"02:55.175","Text":"For example, you shouldn\u0027t be able to set minutes or seconds to 61,"},{"Start":"02:55.175 ","End":"02:57.935","Text":"or hours to 25."},{"Start":"02:57.935 ","End":"03:03.500","Text":"This type of method is called a set method or more formally, a modifier method."},{"Start":"03:03.500 ","End":"03:07.145","Text":"Because a typical object might need many different set methods,"},{"Start":"03:07.145 ","End":"03:10.295","Text":"we\u0027d usually give it a more complete name than just set,"},{"Start":"03:10.295 ","End":"03:12.635","Text":"for example, set time."},{"Start":"03:12.635 ","End":"03:15.020","Text":"Continuing with this clock example,"},{"Start":"03:15.020 ","End":"03:20.315","Text":"we might provide other interface code to our object that could run the clock or stop it."},{"Start":"03:20.315 ","End":"03:23.270","Text":"The process of running the clock executes the code"},{"Start":"03:23.270 ","End":"03:26.345","Text":"that manages the updating of the state variables."},{"Start":"03:26.345 ","End":"03:32.344","Text":"Although no arguments are passed in as they were with the set method every one second,"},{"Start":"03:32.344 ","End":"03:34.990","Text":"the seconds variable is incremented."},{"Start":"03:34.990 ","End":"03:37.580","Text":"If the variable is going to hit 60,"},{"Start":"03:37.580 ","End":"03:42.590","Text":"the code increments the minutes variable and sets seconds back to zero."},{"Start":"03:42.590 ","End":"03:45.335","Text":"If the minutes variable was going to hit 60,"},{"Start":"03:45.335 ","End":"03:50.165","Text":"the coding increments the hours variable and sets minutes back to zero."},{"Start":"03:50.165 ","End":"03:56.495","Text":"Every 12 hours, the hours variable is reset from 12 to 1."},{"Start":"03:56.495 ","End":"03:59.000","Text":"Like the set method before,"},{"Start":"03:59.000 ","End":"04:02.360","Text":"run is also a modifier method as although it doesn\u0027t"},{"Start":"04:02.360 ","End":"04:06.080","Text":"set the variables to specific values passed in as arguments,"},{"Start":"04:06.080 ","End":"04:10.790","Text":"it is modifying those variables just as a set method would."},{"Start":"04:10.790 ","End":"04:13.655","Text":"Access modifiers determine what sort of"},{"Start":"04:13.655 ","End":"04:17.380","Text":"access is granted to variables and methods within an object."},{"Start":"04:17.380 ","End":"04:21.110","Text":"Variables in an object oriented program should be marked as"},{"Start":"04:21.110 ","End":"04:25.130","Text":"private to prevent any direct external access to them."},{"Start":"04:25.130 ","End":"04:28.640","Text":"Any code used to interface with the object is usually made"},{"Start":"04:28.640 ","End":"04:32.045","Text":"public to allow other parts of the program to access it."},{"Start":"04:32.045 ","End":"04:35.120","Text":"However, there could be code that\u0027s private as well."},{"Start":"04:35.120 ","End":"04:38.878","Text":"This means that the method is not accessible outside the object,"},{"Start":"04:38.878 ","End":"04:43.040","Text":"and is only accessible to other methods inside the same object."},{"Start":"04:43.040 ","End":"04:46.370","Text":"An example could be a private method to update the arms of"},{"Start":"04:46.370 ","End":"04:49.925","Text":"the clock anytime your arms need to be moved."},{"Start":"04:49.925 ","End":"04:52.760","Text":"This doesn\u0027t need to be publicly accessible,"},{"Start":"04:52.760 ","End":"04:56.000","Text":"but could be called directly from inside the object when needed."},{"Start":"04:56.000 ","End":"04:59.395","Text":"For example, when the time is first set using the set method,"},{"Start":"04:59.395 ","End":"05:05.135","Text":"which time the minutes or hours hand needs to move in run when the time changes."},{"Start":"05:05.135 ","End":"05:07.940","Text":"If we did need something outside the method to be"},{"Start":"05:07.940 ","End":"05:10.505","Text":"able to access the data stored inside the object."},{"Start":"05:10.505 ","End":"05:14.810","Text":"We can provide a method as a type of middle man that passes on the values."},{"Start":"05:14.810 ","End":"05:17.450","Text":"For example, maybe some other parts of the program"},{"Start":"05:17.450 ","End":"05:20.570","Text":"wants to know the time being displayed on the clock."},{"Start":"05:20.570 ","End":"05:22.580","Text":"We could provide a get method,"},{"Start":"05:22.580 ","End":"05:28.250","Text":"more formally known as a selector method to return the value of our attributes."},{"Start":"05:28.250 ","End":"05:30.545","Text":"There\u0027s nothing particularly clever about this."},{"Start":"05:30.545 ","End":"05:33.290","Text":"We simply create a method that returns the value of"},{"Start":"05:33.290 ","End":"05:36.470","Text":"the variable which is being stored internally within the object."},{"Start":"05:36.470 ","End":"05:39.090","Text":"The important bit is we\u0027re in control."},{"Start":"05:39.090 ","End":"05:42.005","Text":"We\u0027re not allowing direct access to the variable,"},{"Start":"05:42.005 ","End":"05:44.630","Text":"as we would be if it was a global variable."},{"Start":"05:44.630 ","End":"05:49.985","Text":"It will only ever be accessed through this method and we could if we wanted to,"},{"Start":"05:49.985 ","End":"05:53.045","Text":"introduce further checks before returning the value."},{"Start":"05:53.045 ","End":"05:56.855","Text":"That concludes this short introduction to object orientation."},{"Start":"05:56.855 ","End":"06:00.185","Text":"In this section, we learned how to explain the relationship between"},{"Start":"06:00.185 ","End":"06:03.020","Text":"procedural and object-oriented programming to relate"},{"Start":"06:03.020 ","End":"06:06.745","Text":"the concepts of encapsulation and information hiding,"},{"Start":"06:06.745 ","End":"06:12.080","Text":"and to apply object-oriented principles to encapsulate data and code."},{"Start":"06:12.080 ","End":"06:14.210","Text":"Be sure now to complete the exercises,"},{"Start":"06:14.210 ","End":"06:19.655","Text":"to start to get a feel for how object orientation differs from procedural programming."},{"Start":"06:19.655 ","End":"06:22.920","Text":"Thank you for watching, and see you soon."}],"ID":30280},{"Watched":false,"Name":"Exercise 1","Duration":"8m 4s","ChapterTopicVideoID":28782,"CourseChapterTopicPlaylistID":287464,"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.500","Text":"Hello and welcome to this first exercise on object orientation in which we"},{"Start":"00:04.500 ","End":"00:08.345","Text":"create a new project in BlueJ and a class within called Name."},{"Start":"00:08.345 ","End":"00:11.595","Text":"In part a, we\u0027re asked to create 2 private instance variables"},{"Start":"00:11.595 ","End":"00:14.955","Text":"of string datatype called first and last."},{"Start":"00:14.955 ","End":"00:18.360","Text":"In part b, we create a public method called set first,"},{"Start":"00:18.360 ","End":"00:24.330","Text":"which takes a string argument and stores it into the variable first and returns no value."},{"Start":"00:24.330 ","End":"00:27.740","Text":"In part c, we create a public method called set last,"},{"Start":"00:27.740 ","End":"00:33.515","Text":"which takes a string argument and stores it into the variable last and returns no value."},{"Start":"00:33.515 ","End":"00:36.860","Text":"In part d, we create a public method called getFullName,"},{"Start":"00:36.860 ","End":"00:39.830","Text":"which takes no arguments but returns a string."},{"Start":"00:39.830 ","End":"00:44.089","Text":"When called getFullName should return the contents of first"},{"Start":"00:44.089 ","End":"00:48.875","Text":"and last concatenated with a space between them."},{"Start":"00:48.875 ","End":"00:52.235","Text":"In part e, we test the code by creating an object"},{"Start":"00:52.235 ","End":"00:55.670","Text":"from the class on the BlueJ object workbench."},{"Start":"00:55.670 ","End":"01:00.425","Text":"In e1, we\u0027re asked to call set first with a value Margaret,"},{"Start":"01:00.425 ","End":"01:04.800","Text":"and then call set last with the value Hamilton."},{"Start":"01:04.800 ","End":"01:10.235","Text":"Then finally call getFullName which should return Margaret Hamilton."},{"Start":"01:10.235 ","End":"01:13.250","Text":"In part f, we\u0027re asked to create a second object from"},{"Start":"01:13.250 ","End":"01:16.400","Text":"the same glass and repeat the test from part e,"},{"Start":"01:16.400 ","End":"01:20.180","Text":"but set your own first and last names as the values."},{"Start":"01:20.180 ","End":"01:24.005","Text":"We double-click each of the objects on the object workbench one at a time"},{"Start":"01:24.005 ","End":"01:28.420","Text":"and check the values of the instance variables are what we would expect."},{"Start":"01:28.420 ","End":"01:32.434","Text":"Having created the project and the class,"},{"Start":"01:32.434 ","End":"01:34.490","Text":"we\u0027ve got to create"},{"Start":"01:34.490 ","End":"01:40.160","Text":"2 private instance variables of string datatype called first and last."},{"Start":"01:40.160 ","End":"01:45.575","Text":"The modifier to say whether something is public or private goes first,"},{"Start":"01:45.575 ","End":"01:49.470","Text":"then the datatype, then the identifier."},{"Start":"01:49.470 ","End":"01:52.775","Text":"Obviously, we can have an initial value there."},{"Start":"01:52.775 ","End":"01:56.030","Text":"We haven\u0027t been told anything about initial values in this question,"},{"Start":"01:56.030 ","End":"01:57.695","Text":"so we\u0027ll leave it as it is."},{"Start":"01:57.695 ","End":"02:03.480","Text":"There\u0027s our 2 instance variables, first and last."},{"Start":"02:03.480 ","End":"02:05.440","Text":"That\u0027s part a done."},{"Start":"02:05.440 ","End":"02:07.700","Text":"Then we\u0027re asked to create a method."},{"Start":"02:07.700 ","End":"02:11.315","Text":"When creating a method that happens below"},{"Start":"02:11.315 ","End":"02:17.415","Text":"the instance variables declarations and the first one is called set first."},{"Start":"02:17.415 ","End":"02:19.459","Text":"We\u0027ve been told to create this as public."},{"Start":"02:19.459 ","End":"02:21.380","Text":"If we hadn\u0027t done that in the past,"},{"Start":"02:21.380 ","End":"02:23.765","Text":"it would just default to public anyway."},{"Start":"02:23.765 ","End":"02:28.445","Text":"Now we\u0027re going to explicitly be told whether to make them public or private."},{"Start":"02:28.445 ","End":"02:30.694","Text":"Generally, methods are public,"},{"Start":"02:30.694 ","End":"02:33.920","Text":"but then can be some worker methods that are marked"},{"Start":"02:33.920 ","End":"02:37.590","Text":"private and therefore not accessed outside."},{"Start":"02:37.590 ","End":"02:42.350","Text":"Let\u0027s do this first one called set first."},{"Start":"02:42.350 ","End":"02:45.425","Text":"It takes a string argument and returns nothing."},{"Start":"02:45.425 ","End":"02:52.190","Text":"I need to put void in front there and inside here is the argument."},{"Start":"02:52.190 ","End":"02:55.415","Text":"It hasn\u0027t been told what to call it."},{"Start":"02:55.415 ","End":"03:05.630","Text":"I\u0027ll just call it F for first and then the body of the method is in-between braces."},{"Start":"03:05.630 ","End":"03:12.295","Text":"All I need to do is to store it into the variable first."},{"Start":"03:12.295 ","End":"03:19.620","Text":"That\u0027s easy enough and just say first equals F because that\u0027s what\u0027s been passed into me."},{"Start":"03:19.940 ","End":"03:27.170","Text":"The second method set last is going to be exactly the same thing."},{"Start":"03:27.170 ","End":"03:33.905","Text":"I may as well just copy it from here and just change the contents."},{"Start":"03:33.905 ","End":"03:41.195","Text":"Set last and it also accepts a single string."},{"Start":"03:41.195 ","End":"03:44.615","Text":"I\u0027ll just carry on calling that F because it\u0027s a parameter."},{"Start":"03:44.615 ","End":"03:49.985","Text":"I can reuse that name and then the instance variable called last."},{"Start":"03:49.985 ","End":"03:54.920","Text":"Now we\u0027ll get the value of whatever has been passed in. Nice and easy."},{"Start":"03:54.920 ","End":"03:56.645","Text":"That\u0027s all I\u0027ve been asked to do."},{"Start":"03:56.645 ","End":"03:59.450","Text":"Set the first set last."},{"Start":"03:59.450 ","End":"04:02.750","Text":"Those 2 values that are passed in as arguments to"},{"Start":"04:02.750 ","End":"04:08.270","Text":"these 2 methods will now be stored within the object,"},{"Start":"04:08.270 ","End":"04:11.940","Text":"inside its instance variables."},{"Start":"04:11.940 ","End":"04:16.715","Text":"What I need to do now is to do the final method,"},{"Start":"04:16.715 ","End":"04:19.655","Text":"which is called getFullName."},{"Start":"04:19.655 ","End":"04:22.985","Text":"This one takes no arguments but does return"},{"Start":"04:22.985 ","End":"04:27.580","Text":"a string and this is going to be public again."},{"Start":"04:27.580 ","End":"04:29.895","Text":"It returns a string,"},{"Start":"04:29.895 ","End":"04:33.185","Text":"I\u0027ll put the return data type after the word public,"},{"Start":"04:33.185 ","End":"04:42.465","Text":"and then the name of the method get full name and no arguments."},{"Start":"04:42.465 ","End":"04:45.800","Text":"Then the method body itself,"},{"Start":"04:45.800 ","End":"04:50.330","Text":"I need to return the contents of first and last"},{"Start":"04:50.330 ","End":"04:55.550","Text":"concatenated meaning joined together with a space between them so that\u0027s easy enough."},{"Start":"04:55.550 ","End":"04:58.009","Text":"The keyword I need is return."},{"Start":"04:58.009 ","End":"05:02.375","Text":"The first name is stored in the instance variable called first."},{"Start":"05:02.375 ","End":"05:05.675","Text":"I\u0027m going to concatenate that with a space."},{"Start":"05:05.675 ","End":"05:13.170","Text":"I\u0027m going to concatenate that with the contents of the instance variable called last."},{"Start":"05:13.170 ","End":"05:16.050","Text":"That is it."},{"Start":"05:16.050 ","End":"05:18.660","Text":"I can now test,"},{"Start":"05:18.660 ","End":"05:24.440","Text":"so let\u0027s compile that and test it by creating an object on the workbench."},{"Start":"05:24.440 ","End":"05:28.640","Text":"Just accept me instance name it gives me and I\u0027ve been"},{"Start":"05:28.640 ","End":"05:33.140","Text":"asked first of all to call set first with a value Margaret."},{"Start":"05:33.140 ","End":"05:37.445","Text":"There\u0027s set first. Remember strings,"},{"Start":"05:37.445 ","End":"05:38.600","Text":"if I\u0027m passing them in here,"},{"Start":"05:38.600 ","End":"05:40.280","Text":"I have to go into quotes."},{"Start":"05:40.280 ","End":"05:42.810","Text":"There\u0027s Margaret."},{"Start":"05:42.950 ","End":"05:46.490","Text":"That\u0027s presumably done something,"},{"Start":"05:46.490 ","End":"05:48.500","Text":"but I don\u0027t see any reaction."},{"Start":"05:48.500 ","End":"05:52.385","Text":"Then I set a last name to Hamilton."},{"Start":"05:52.385 ","End":"05:56.160","Text":"Then I\u0027m asked to call getFullName,"},{"Start":"05:56.160 ","End":"05:59.295","Text":"which should return Margaret Hamilton."},{"Start":"05:59.295 ","End":"06:03.935","Text":"Let\u0027s call that and I do indeed get a string returned to me,"},{"Start":"06:03.935 ","End":"06:06.725","Text":"which has Margaret space Hamilton."},{"Start":"06:06.725 ","End":"06:08.900","Text":"I\u0027ve done what I\u0027ve been asked to do."},{"Start":"06:08.900 ","End":"06:11.840","Text":"If I want to actually have a look inside this object,"},{"Start":"06:11.840 ","End":"06:13.120","Text":"I can double-click on it."},{"Start":"06:13.120 ","End":"06:17.410","Text":"I can see the value of these variables first and last."},{"Start":"06:17.410 ","End":"06:19.700","Text":"They do obviously contain Margaret without space,"},{"Start":"06:19.700 ","End":"06:23.180","Text":"Hamilton without space and it was the method"},{"Start":"06:23.180 ","End":"06:27.560","Text":"getFullName that join them together and put a space in-between them,"},{"Start":"06:27.560 ","End":"06:30.500","Text":"returning the overall full name."},{"Start":"06:30.500 ","End":"06:34.130","Text":"I have been asked in part F to create a second object"},{"Start":"06:34.130 ","End":"06:38.875","Text":"from the same class and repeat the tests and set your own name."},{"Start":"06:38.875 ","End":"06:42.150","Text":"I need to come up here and create another object."},{"Start":"06:42.150 ","End":"06:43.520","Text":"We haven\u0027t really done this before."},{"Start":"06:43.520 ","End":"06:49.220","Text":"We\u0027ve never needed to do this except the instance name that it\u0027s given me to name2."},{"Start":"06:49.220 ","End":"06:52.895","Text":"Now, if I look inside here, it has nothing."},{"Start":"06:52.895 ","End":"06:55.835","Text":"There\u0027s no value been set for first and last,"},{"Start":"06:55.835 ","End":"07:00.770","Text":"which is what I would expect if I tried to do getFullName,"},{"Start":"07:00.770 ","End":"07:04.340","Text":"I\u0027d get nullspace null returned to me."},{"Start":"07:04.340 ","End":"07:10.560","Text":"I\u0027m obviously going to test that I can put something else in there."},{"Start":"07:10.630 ","End":"07:20.700","Text":"I\u0027ll put my name and surname."},{"Start":"07:22.370 ","End":"07:27.830","Text":"Therefore, I should see my name come back when I do get fullName and I"},{"Start":"07:27.830 ","End":"07:33.860","Text":"do not affected the contents of this other object here."},{"Start":"07:33.860 ","End":"07:35.435","Text":"That\u0027s the whole point."},{"Start":"07:35.435 ","End":"07:37.925","Text":"The object has its own data."},{"Start":"07:37.925 ","End":"07:39.680","Text":"The code I didn\u0027t have to rewrite,"},{"Start":"07:39.680 ","End":"07:43.280","Text":"it was generated from whatever was in here in the class definition."},{"Start":"07:43.280 ","End":"07:45.290","Text":"The code does the same thing,"},{"Start":"07:45.290 ","End":"07:49.280","Text":"but it\u0027s dealing with different data because it deals with its own data."},{"Start":"07:49.280 ","End":"07:52.205","Text":"That\u0027s really just the point of this first exercise."},{"Start":"07:52.205 ","End":"07:56.300","Text":"As we start to get used to throughout this topic and the use of"},{"Start":"07:56.300 ","End":"08:00.610","Text":"objects and the way we interact with them and define them."},{"Start":"08:00.610 ","End":"08:01.685","Text":"That\u0027s it for this one."},{"Start":"08:01.685 ","End":"08:04.470","Text":"See you soon for the next one."}],"ID":30281},{"Watched":false,"Name":"Exercise 2","Duration":"9m 16s","ChapterTopicVideoID":28783,"CourseChapterTopicPlaylistID":287464,"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.870","Text":"Hello and welcome to this exercise in which we\u0027ve been asked to create a class called"},{"Start":"00:03.870 ","End":"00:07.560","Text":"bank account which will be used to store a bank account balance."},{"Start":"00:07.560 ","End":"00:10.320","Text":"Deposits to and withdrawals from the account can"},{"Start":"00:10.320 ","End":"00:13.245","Text":"only be made with the provided interface methods."},{"Start":"00:13.245 ","End":"00:16.530","Text":"In part a, within the class were told to create a private instance"},{"Start":"00:16.530 ","End":"00:21.240","Text":"variable balance of datatype double with initial value 0."},{"Start":"00:21.240 ","End":"00:25.710","Text":"In part b, we\u0027re asked to create 3 public methods within the bank account class."},{"Start":"00:25.710 ","End":"00:27.300","Text":"The first one deposit,"},{"Start":"00:27.300 ","End":"00:30.795","Text":"except a single double argument, and returns nothing."},{"Start":"00:30.795 ","End":"00:34.845","Text":"The second withdraw accepts a single double argument and returns"},{"Start":"00:34.845 ","End":"00:39.750","Text":"a boolean and get balanced the final one, except no arguments,"},{"Start":"00:39.750 ","End":"00:42.810","Text":"but returns a double In part c,"},{"Start":"00:42.810 ","End":"00:47.180","Text":"then we\u0027re asked for the deposit implementation to take the value passed into"},{"Start":"00:47.180 ","End":"00:52.085","Text":"the method and add it to the current value of the instance variable balance,"},{"Start":"00:52.085 ","End":"00:55.129","Text":"storing the result back into balance."},{"Start":"00:55.129 ","End":"00:58.145","Text":"In part d for the get balanced implementation,"},{"Start":"00:58.145 ","End":"01:02.090","Text":"were told to return the current value of balance."},{"Start":"01:02.090 ","End":"01:05.375","Text":"In part e for the withdrawal implementation,"},{"Start":"01:05.375 ","End":"01:07.700","Text":"were first asked to check if subtracting"},{"Start":"01:07.700 ","End":"01:11.135","Text":"the withdrawal amount from balance would give a negative result."},{"Start":"01:11.135 ","End":"01:14.540","Text":"If it doesn\u0027t, then we subtract the withdrawal amount from"},{"Start":"01:14.540 ","End":"01:19.685","Text":"balance and store the result back into balance and return true."},{"Start":"01:19.685 ","End":"01:25.264","Text":"If would then we simply return false without reducing the buttons."},{"Start":"01:25.264 ","End":"01:28.640","Text":"In part f, we test the code by creating an object from"},{"Start":"01:28.640 ","End":"01:31.505","Text":"the bank account class on the object workbench,"},{"Start":"01:31.505 ","End":"01:37.745","Text":"and then we add 1,955.5 to the account by using the deposit method,"},{"Start":"01:37.745 ","End":"01:40.820","Text":"in Part ii to inspect the instance variable balanced"},{"Start":"01:40.820 ","End":"01:43.970","Text":"by double-clicking the object on the object workbench."},{"Start":"01:43.970 ","End":"01:47.165","Text":"In Part iii, we add another 10.0 to the account"},{"Start":"01:47.165 ","End":"01:50.570","Text":"using deposit again and run get balanced,"},{"Start":"01:50.570 ","End":"01:54.570","Text":"which should return 1,965.50."},{"Start":"01:54.570 ","End":"02:02.550","Text":"In Part iv, we withdraw 1,000.50 using the withdrawal method which should return true."},{"Start":"02:02.550 ","End":"02:05.015","Text":"Then we run get balanced again,"},{"Start":"02:05.015 ","End":"02:08.765","Text":"which should return 965.0."},{"Start":"02:08.765 ","End":"02:13.294","Text":"Finally, we attempt to withdraw 966 using withdrawal,"},{"Start":"02:13.294 ","End":"02:18.080","Text":"which should return false and leave balance unchanged."},{"Start":"02:18.080 ","End":"02:21.260","Text":"We\u0027ve made a new class called bank account."},{"Start":"02:21.260 ","End":"02:24.795","Text":"I\u0027ve just emptied it out from the default BlueJ code."},{"Start":"02:24.795 ","End":"02:27.800","Text":"I\u0027m going to create in part a,"},{"Start":"02:27.800 ","End":"02:31.130","Text":"a private instance variable called balance."},{"Start":"02:31.130 ","End":"02:35.645","Text":"It\u0027s of type double with initial value 0."},{"Start":"02:35.645 ","End":"02:38.180","Text":"Don\u0027t need a double here, it\u0027s overkill,"},{"Start":"02:38.180 ","End":"02:42.050","Text":"but it just helps avoid annoying little things"},{"Start":"02:42.050 ","End":"02:46.205","Text":"like having to put an f on after values if we were to use the float."},{"Start":"02:46.205 ","End":"02:47.870","Text":"That\u0027s part a done."},{"Start":"02:47.870 ","End":"02:52.670","Text":"part b to create 3 public methods within this class."},{"Start":"02:52.670 ","End":"02:55.070","Text":"The first one is called deposit."},{"Start":"02:55.070 ","End":"03:00.815","Text":"Let\u0027s mark its modifier as public and it returns nothing."},{"Start":"03:00.815 ","End":"03:02.930","Text":"We need to put void."},{"Start":"03:02.930 ","End":"03:05.780","Text":"Name is deposit as we said,"},{"Start":"03:05.780 ","End":"03:14.165","Text":"and it accepts a single double argument type first and then the name for the parameter."},{"Start":"03:14.165 ","End":"03:17.735","Text":"We\u0027ll call it amount."},{"Start":"03:17.735 ","End":"03:20.670","Text":"In the next part,"},{"Start":"03:20.670 ","End":"03:26.130","Text":"Part I, it asks us to create a method called withdrawal."},{"Start":"03:26.360 ","End":"03:33.140","Text":"This time we return a boolean and again it\u0027s a double and I can use"},{"Start":"03:33.140 ","End":"03:39.695","Text":"the same name for the parameter because it will only apply with inside these braces here."},{"Start":"03:39.695 ","End":"03:43.235","Text":"Then the final method is called get balance."},{"Start":"03:43.235 ","End":"03:48.160","Text":"Once again, public and it returns a double,"},{"Start":"03:48.160 ","End":"03:52.845","Text":"and then we are done with part b."},{"Start":"03:52.845 ","End":"03:54.540","Text":"Now we\u0027re going to fill in,"},{"Start":"03:54.540 ","End":"03:58.380","Text":"in part c implementation for deposit."},{"Start":"03:58.380 ","End":"04:01.310","Text":"What is deposit actually going to do?"},{"Start":"04:01.310 ","End":"04:05.555","Text":"Well, we\u0027ve been told to take the value passed in,"},{"Start":"04:05.555 ","End":"04:07.940","Text":"which is going to be stored in amount,"},{"Start":"04:07.940 ","End":"04:12.150","Text":"and add it to the current value of balance."},{"Start":"04:12.150 ","End":"04:17.945","Text":"That\u0027s easier, we say balance has stored into it what it already has,"},{"Start":"04:17.945 ","End":"04:22.640","Text":"the current balance plus the amount being passed in,"},{"Start":"04:22.640 ","End":"04:25.130","Text":"and that\u0027s it done."},{"Start":"04:25.130 ","End":"04:27.965","Text":"There is no return to do."},{"Start":"04:27.965 ","End":"04:29.900","Text":"We\u0027ve finished with that method."},{"Start":"04:29.900 ","End":"04:32.180","Text":"The next method withdraw."},{"Start":"04:32.180 ","End":"04:36.140","Text":"Actually, for the next method we\u0027ll do get balanced as we\u0027ve been asked to do that."},{"Start":"04:36.140 ","End":"04:38.210","Text":"First, in part d,"},{"Start":"04:38.210 ","End":"04:40.670","Text":"we just return the current value of balance,"},{"Start":"04:40.670 ","End":"04:41.960","Text":"so that\u0027s easy enough."},{"Start":"04:41.960 ","End":"04:45.095","Text":"We just say return"},{"Start":"04:45.095 ","End":"04:52.310","Text":"balance and that part\u0027s done because I haven\u0027t got some brackets here."},{"Start":"04:52.310 ","End":"04:53.330","Text":"It\u0027s giving me an error message."},{"Start":"04:53.330 ","End":"04:55.610","Text":"That\u0027s better. That\u0027s great."},{"Start":"04:55.610 ","End":"04:59.120","Text":"That\u0027s that part done. The withdraws the final one."},{"Start":"04:59.120 ","End":"05:02.600","Text":"That\u0027s a bit more tricky than these previous 2, but not too tricky."},{"Start":"05:02.600 ","End":"05:05.360","Text":"All we have to do is check if subtracting"},{"Start":"05:05.360 ","End":"05:10.700","Text":"the withdrawal amount from balance would give a negative result."},{"Start":"05:10.700 ","End":"05:13.070","Text":"We need an if statement here."},{"Start":"05:13.070 ","End":"05:17.340","Text":"We need to say if balance minus the amount."},{"Start":"05:17.340 ","End":"05:19.250","Text":"That\u0027s the bit that we\u0027ve been asked to withdraw,"},{"Start":"05:19.250 ","End":"05:20.870","Text":"but we\u0027re not withdrawing it yet."},{"Start":"05:20.870 ","End":"05:24.065","Text":"If it\u0027s greater than or equal to 0,"},{"Start":"05:24.065 ","End":"05:25.670","Text":"then great, we go ahead."},{"Start":"05:25.670 ","End":"05:28.335","Text":"If it\u0027s not, we do something else."},{"Start":"05:28.335 ","End":"05:31.455","Text":"What do we do if it is?"},{"Start":"05:31.455 ","End":"05:36.795","Text":"Then we subtract so it doesn\u0027t cause it to go below 0."},{"Start":"05:36.795 ","End":"05:38.735","Text":"That\u0027s in my if-clause here."},{"Start":"05:38.735 ","End":"05:42.940","Text":"We are okay to subtract the amounts from balance."},{"Start":"05:42.940 ","End":"05:49.420","Text":"We can say balance equals balance minus amount."},{"Start":"05:49.420 ","End":"05:53.180","Text":"I could put here return,"},{"Start":"05:53.180 ","End":"05:54.785","Text":"sorry, an else statement."},{"Start":"05:54.785 ","End":"05:59.315","Text":"But I can actually just put return false."},{"Start":"05:59.315 ","End":"06:02.720","Text":"Because if I put return true here,"},{"Start":"06:02.720 ","End":"06:08.284","Text":"when I have been allowed to subtract the amount from the balance,"},{"Start":"06:08.284 ","End":"06:13.190","Text":"it will never get to that line here underneath where it says return false."},{"Start":"06:13.190 ","End":"06:14.870","Text":"I could have an else statement,"},{"Start":"06:14.870 ","End":"06:18.350","Text":"but I don\u0027t actually need it because return true will exit"},{"Start":"06:18.350 ","End":"06:22.490","Text":"this method to the calling code and it will never get to this line."},{"Start":"06:22.490 ","End":"06:28.605","Text":"It will only get to this line if balance minus amount takes us to a negative number."},{"Start":"06:28.605 ","End":"06:30.080","Text":"It won\u0027t allow us to withdraw,"},{"Start":"06:30.080 ","End":"06:31.610","Text":"so it doesn\u0027t change balance."},{"Start":"06:31.610 ","End":"06:34.145","Text":"It just returns the value false,"},{"Start":"06:34.145 ","End":"06:38.375","Text":"which is what we\u0027ve been asked to do."},{"Start":"06:38.375 ","End":"06:44.240","Text":"That\u0027s Part 2 and 3 down of e. In part f,"},{"Start":"06:44.240 ","End":"06:50.495","Text":"we\u0027re told to test the code by creating an object from a bank account."},{"Start":"06:50.495 ","End":"06:54.930","Text":"Let\u0027s do that and accept whatever it calls the instance."},{"Start":"06:54.930 ","End":"06:58.265","Text":"Then we\u0027ve been asked to add a certain amount."},{"Start":"06:58.265 ","End":"07:04.935","Text":"Let\u0027s deposit 1,955.50."},{"Start":"07:04.935 ","End":"07:07.480","Text":"Go ahead and do that"},{"Start":"07:07.480 ","End":"07:11.690","Text":"and just have a look at what balance has in there now, and that\u0027s great."},{"Start":"07:11.690 ","End":"07:13.700","Text":"It does seem to have added it to 0,"},{"Start":"07:13.700 ","End":"07:15.770","Text":"which is what it would have had in previously."},{"Start":"07:15.770 ","End":"07:17.540","Text":"The balance is good."},{"Start":"07:17.540 ","End":"07:20.060","Text":"In Part 3, we\u0027re asked to add another 10,"},{"Start":"07:20.060 ","End":"07:25.885","Text":"so let\u0027s deposit 10 more and see what happens."},{"Start":"07:25.885 ","End":"07:30.980","Text":"Let\u0027s run get balanced as it asks us to in Part 3 to see what we get."},{"Start":"07:30.980 ","End":"07:34.430","Text":"We get back 1,956.5,"},{"Start":"07:34.430 ","End":"07:35.540","Text":"which is what we\u0027d expect,"},{"Start":"07:35.540 ","End":"07:39.995","Text":"1,955.50 plus 10 so that\u0027s great."},{"Start":"07:39.995 ","End":"07:49.050","Text":"Now we\u0027ve been asked in Part 4 to withdraw 1,000.50 so let\u0027s do that."},{"Start":"07:49.050 ","End":"07:53.060","Text":"It returns true because it\u0027s happy that it has withdrawn."},{"Start":"07:53.060 ","End":"07:55.760","Text":"It was happy with the amount that was withdrawn."},{"Start":"07:55.760 ","End":"08:02.475","Text":"Let\u0027s see what gets balanced gives us 965 which is what we\u0027d expect."},{"Start":"08:02.475 ","End":"08:06.694","Text":"Then the final test that such attempt to withdraw"},{"Start":"08:06.694 ","End":"08:12.080","Text":"more than we have in the account 966, we\u0027ll do that."},{"Start":"08:12.080 ","End":"08:18.875","Text":"We should get back false and we do balance should be unchanged."},{"Start":"08:18.875 ","End":"08:26.100","Text":"Let\u0027s have a look and it is still 965 as it was previously."},{"Start":"08:26.100 ","End":"08:29.510","Text":"That\u0027s all the testing done and our code seems to work"},{"Start":"08:29.510 ","End":"08:32.660","Text":"and we\u0027ve done something relatively useful there."},{"Start":"08:32.660 ","End":"08:36.470","Text":"We\u0027ve stored a balance and we started with it as 0."},{"Start":"08:36.470 ","End":"08:40.850","Text":"We were able to add and remove amounts by depositing and withdrawing,"},{"Start":"08:40.850 ","End":"08:46.795","Text":"but it would only allow us to withdraw if we were at having a balance of 0 or more."},{"Start":"08:46.795 ","End":"08:54.830","Text":"This is now starting to seem like a useful use of object orientation in that we\u0027re"},{"Start":"08:54.830 ","End":"08:58.760","Text":"storing the balance in a instance"},{"Start":"08:58.760 ","End":"09:02.750","Text":"variable here and only the methods are allowed to change it."},{"Start":"09:02.750 ","End":"09:07.870","Text":"Plus we\u0027re able to do a little bit of logic to decide when it should be changed."},{"Start":"09:07.870 ","End":"09:12.890","Text":"It\u0027s under our terms that really goes to the heart of encapsulation."},{"Start":"09:12.890 ","End":"09:16.600","Text":"That\u0027s it for this one. Thanks for watching. See you soon."}],"ID":30282},{"Watched":false,"Name":"Exercise 3","Duration":"13m 9s","ChapterTopicVideoID":28776,"CourseChapterTopicPlaylistID":287464,"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.370","Text":"Hello and welcome. In this exercise,"},{"Start":"00:02.370 ","End":"00:04.890","Text":"you\u0027ve been asked to create a new class called clock,"},{"Start":"00:04.890 ","End":"00:10.140","Text":"and then to create 3 private integer instance variables within clock called hours,"},{"Start":"00:10.140 ","End":"00:13.185","Text":"minutes, and seconds, with initial values of 12,"},{"Start":"00:13.185 ","End":"00:15.780","Text":"0, and 0 respectively."},{"Start":"00:15.780 ","End":"00:20.205","Text":"We\u0027re then asked to create a public method called set which accepts 3 parameters,"},{"Start":"00:20.205 ","End":"00:23.550","Text":"hours, minutes, and seconds, and returns nothing."},{"Start":"00:23.550 ","End":"00:27.890","Text":"The method should store the values in 3 parameters into the hours,"},{"Start":"00:27.890 ","End":"00:30.905","Text":"minutes, and seconds instance variables."},{"Start":"00:30.905 ","End":"00:34.190","Text":"We then create a private method called display time,"},{"Start":"00:34.190 ","End":"00:37.115","Text":"which returns nothing and has no parameters."},{"Start":"00:37.115 ","End":"00:41.210","Text":"This should output the contents of the instance variables to display the time,"},{"Start":"00:41.210 ","End":"00:44.495","Text":"for example, 12:59:57 seconds."},{"Start":"00:44.495 ","End":"00:48.035","Text":"In Part d, we\u0027re asked to create another public method called run,"},{"Start":"00:48.035 ","End":"00:51.815","Text":"which advances seconds by 1 each time it\u0027s called."},{"Start":"00:51.815 ","End":"00:53.690","Text":"When seconds reaches 60,"},{"Start":"00:53.690 ","End":"00:58.385","Text":"we\u0027re told that we should advance minutes by 1 and then reset seconds to 0."},{"Start":"00:58.385 ","End":"01:00.290","Text":"When minutes reaches 60,"},{"Start":"01:00.290 ","End":"01:04.205","Text":"we advanced hours by 1 and reset minutes to 0."},{"Start":"01:04.205 ","End":"01:06.469","Text":"When hours reaches 13,"},{"Start":"01:06.469 ","End":"01:09.335","Text":"we reset hours to 1."},{"Start":"01:09.335 ","End":"01:11.270","Text":"At the end of the run method,"},{"Start":"01:11.270 ","End":"01:13.730","Text":"we call the display time method."},{"Start":"01:13.730 ","End":"01:16.640","Text":"In Part e, we\u0027re asked to test by creating an object"},{"Start":"01:16.640 ","End":"01:20.000","Text":"from the clock class on the BlueJ object workbench."},{"Start":"01:20.000 ","End":"01:23.690","Text":"We set the output of the terminal window to clear screen on"},{"Start":"01:23.690 ","End":"01:27.590","Text":"method calls by ticking the box if it isn\u0027t already."},{"Start":"01:27.590 ","End":"01:31.010","Text":"Then using set, we set the time to 12 hours,"},{"Start":"01:31.010 ","End":"01:34.385","Text":"59 minutes, 57 seconds in Part 1."},{"Start":"01:34.385 ","End":"01:38.810","Text":"In Part 2, we\u0027re asked to inspect the instance variables to see that they\u0027ve"},{"Start":"01:38.810 ","End":"01:43.795","Text":"been correctly set by double-clicking the object in the object workbench."},{"Start":"01:43.795 ","End":"01:46.970","Text":"In Part 3, we\u0027re told to call the run method 3"},{"Start":"01:46.970 ","End":"01:51.265","Text":"times to see if the time wraps around to 1 o\u0027clock."},{"Start":"01:51.265 ","End":"01:52.960","Text":"Then we\u0027re asked in Part f,"},{"Start":"01:52.960 ","End":"01:56.420","Text":"why display time doesn\u0027t appear in the list"},{"Start":"01:56.420 ","End":"02:00.875","Text":"of methods when you right-click in the object workbench."},{"Start":"02:00.875 ","End":"02:05.015","Text":"Having created the class called clock,"},{"Start":"02:05.015 ","End":"02:07.130","Text":"we aren\u0027t create in Part a,"},{"Start":"02:07.130 ","End":"02:14.575","Text":"3 instance variables which are all private as all instance variables should be."},{"Start":"02:14.575 ","End":"02:24.220","Text":"They are going to be int and the first one is called hours with initial value of 12,"},{"Start":"02:24.220 ","End":"02:31.400","Text":"and the next one is minutes with initial value of 0,"},{"Start":"02:31.400 ","End":"02:41.340","Text":"and the final one is going to be seconds with again an initial value of 0."},{"Start":"02:43.940 ","End":"02:46.935","Text":"There\u0027s our instance variables,"},{"Start":"02:46.935 ","End":"02:48.375","Text":"that\u0027s Part a done."},{"Start":"02:48.375 ","End":"02:52.760","Text":"Now we are asked to create a method which is going to be"},{"Start":"02:52.760 ","End":"02:58.300","Text":"public called set that accepts 3 parameters,"},{"Start":"02:58.300 ","End":"03:02.855","Text":"h, m, and s. We have to give the datatype"},{"Start":"03:02.855 ","End":"03:05.930","Text":"each time in the order that they are going"},{"Start":"03:05.930 ","End":"03:09.770","Text":"to be sent across as arguments, so there they are."},{"Start":"03:09.770 ","End":"03:11.990","Text":"What we\u0027re going to do with h,"},{"Start":"03:11.990 ","End":"03:18.163","Text":"m and s is set the values in our instance variables to whatever was passed in."},{"Start":"03:18.163 ","End":"03:23.595","Text":"So hours presumably is going to get h,"},{"Start":"03:23.595 ","End":"03:29.130","Text":"minutes our instance variable is going to get m,"},{"Start":"03:29.130 ","End":"03:34.025","Text":"and seconds is going to get whatever was passed in as"},{"Start":"03:34.025 ","End":"03:39.800","Text":"s. That term didn\u0027t put void in front here,"},{"Start":"03:39.800 ","End":"03:41.720","Text":"so that\u0027s going to complain."},{"Start":"03:41.720 ","End":"03:43.850","Text":"No validation done in here really,"},{"Start":"03:43.850 ","End":"03:48.740","Text":"I should check that it\u0027s a sensible value for hours, minutes,"},{"Start":"03:48.740 ","End":"03:50.270","Text":"and seconds because obviously,"},{"Start":"03:50.270 ","End":"03:53.915","Text":"hours can\u0027t be more than 12 and a 12-hour clock."},{"Start":"03:53.915 ","End":"03:57.170","Text":"Minutes can\u0027t be more than 59 and seconds can\u0027t be more"},{"Start":"03:57.170 ","End":"04:00.350","Text":"than 59 either but we\u0027re not going to do that."},{"Start":"04:00.350 ","End":"04:02.510","Text":"We\u0027ve not been asked to do that, but you really should."},{"Start":"04:02.510 ","End":"04:05.370","Text":"There\u0027s set done."},{"Start":"04:05.370 ","End":"04:09.785","Text":"Then we\u0027ve been asked to create another one called display time,"},{"Start":"04:09.785 ","End":"04:15.485","Text":"which outputs the contents of the instance variables to display the time."},{"Start":"04:15.485 ","End":"04:17.620","Text":"Let\u0027s go ahead and do that,"},{"Start":"04:17.620 ","End":"04:20.865","Text":"and now this is private."},{"Start":"04:20.865 ","End":"04:27.410","Text":"First time we\u0027ve come across that for a method doesn\u0027t return any values that we\u0027re told"},{"Start":"04:27.410 ","End":"04:30.035","Text":"off it just displays the time and it doesn\u0027t have"},{"Start":"04:30.035 ","End":"04:34.190","Text":"any parameters either so we got empty brackets there."},{"Start":"04:34.190 ","End":"04:37.625","Text":"Literally, all we need to do is output the time,"},{"Start":"04:37.625 ","End":"04:43.295","Text":"and we do that by taking our variables and concatenating them."},{"Start":"04:43.295 ","End":"04:45.790","Text":"It\u0027s going to be hours first,"},{"Start":"04:45.790 ","End":"04:49.005","Text":"and if we separate them with colons,"},{"Start":"04:49.005 ","End":"04:54.890","Text":"then minutes, then finally seconds."},{"Start":"04:54.890 ","End":"04:57.680","Text":"That is display time done."},{"Start":"04:57.680 ","End":"05:00.710","Text":"We just output the current values of hours, minutes,"},{"Start":"05:00.710 ","End":"05:04.845","Text":"and seconds on a single line separated by colons."},{"Start":"05:04.845 ","End":"05:06.865","Text":"Then in Part d,"},{"Start":"05:06.865 ","End":"05:10.880","Text":"we\u0027re asked to create another method called run."},{"Start":"05:10.880 ","End":"05:14.720","Text":"This is the most important one within a clock."},{"Start":"05:14.720 ","End":"05:20.405","Text":"She runs a clock so it\u0027s a public and again,"},{"Start":"05:20.405 ","End":"05:27.350","Text":"it doesn\u0027t return a value but it doesn\u0027t accept either any arguments."},{"Start":"05:27.350 ","End":"05:31.030","Text":"It\u0027s literally just going to advance the time by 1 second."},{"Start":"05:31.030 ","End":"05:34.940","Text":"Let\u0027s have a look at what we need to have in there."},{"Start":"05:34.940 ","End":"05:37.760","Text":"When seconds reaches 60,"},{"Start":"05:37.760 ","End":"05:39.140","Text":"we advanced minutes by 1,"},{"Start":"05:39.140 ","End":"05:42.500","Text":"but we first of all got to advance the time."},{"Start":"05:42.500 ","End":"05:47.510","Text":"We can say seconds equals seconds plus 1 or shortcut would be just use"},{"Start":"05:47.510 ","End":"05:51.890","Text":"the increment operator so that will store back for seconds into seconds,"},{"Start":"05:51.890 ","End":"05:53.735","Text":"but with one added onto it."},{"Start":"05:53.735 ","End":"05:58.495","Text":"We are now asked to check if in Part 1"},{"Start":"05:58.495 ","End":"06:03.545","Text":"seconds has reached 60 so we need an if statement there."},{"Start":"06:03.545 ","End":"06:06.035","Text":"If seconds has reached 60,"},{"Start":"06:06.035 ","End":"06:08.120","Text":"what do we do?"},{"Start":"06:08.120 ","End":"06:11.440","Text":"We advanced minutes by 1,"},{"Start":"06:11.440 ","End":"06:15.950","Text":"so we use the increment operator on minutes to increment it by"},{"Start":"06:15.950 ","End":"06:21.065","Text":"1 and we should put seconds back to 0 when we do that."},{"Start":"06:21.065 ","End":"06:24.274","Text":"Then when minutes reaches 60,"},{"Start":"06:24.274 ","End":"06:26.950","Text":"we advanced hours by 1."},{"Start":"06:26.950 ","End":"06:29.085","Text":"Let\u0027s do a check now."},{"Start":"06:29.085 ","End":"06:32.330","Text":"This is going to be a nested check inside here."},{"Start":"06:32.330 ","End":"06:35.795","Text":"Because there\u0027s no point checking it every second."},{"Start":"06:35.795 ","End":"06:40.145","Text":"We only want to check it when we increase minutes."},{"Start":"06:40.145 ","End":"06:44.310","Text":"If minutes equals 60,"},{"Start":"06:44.310 ","End":"06:47.295","Text":"we advance hours by 1,"},{"Start":"06:47.295 ","End":"06:50.330","Text":"and then we\u0027re going to do a final check and we\u0027ve obviously got to"},{"Start":"06:50.330 ","End":"06:54.335","Text":"make minutes go back down to 0 as well."},{"Start":"06:54.335 ","End":"07:00.290","Text":"We\u0027re going to do one final check to see if hours has hit 13 in Part 3."},{"Start":"07:00.290 ","End":"07:05.030","Text":"If hours reached 13,"},{"Start":"07:05.030 ","End":"07:08.725","Text":"then we reset hours to 1."},{"Start":"07:08.725 ","End":"07:11.475","Text":"Looks like Part d is done,"},{"Start":"07:11.475 ","End":"07:18.080","Text":"we add a second to the clock every time we run the clock and if seconds hit 60,"},{"Start":"07:18.080 ","End":"07:21.030","Text":"we increase minutes by 1,"},{"Start":"07:21.030 ","End":"07:23.430","Text":"and put seconds back to 0,"},{"Start":"07:23.430 ","End":"07:27.140","Text":"and we do a second check to see if minutes has hit 60."},{"Start":"07:27.140 ","End":"07:30.445","Text":"If it does, we advanced hours by 1 and"},{"Start":"07:30.445 ","End":"07:37.080","Text":"reset minutes to 0 and then we finally do a check if hours has hit 13,"},{"Start":"07:37.080 ","End":"07:40.200","Text":"we reset hours to 1."},{"Start":"07:40.200 ","End":"07:46.775","Text":"One final thing we\u0027ve got to do here in Part 4 is to call the display time method."},{"Start":"07:46.775 ","End":"07:49.055","Text":"After we\u0027ve done all that checks,"},{"Start":"07:49.055 ","End":"07:50.995","Text":"none of this might have run,"},{"Start":"07:50.995 ","End":"07:55.565","Text":"could be none of this run, but we still need to display the current time."},{"Start":"07:55.565 ","End":"08:00.350","Text":"Let\u0027s call the display time method here,"},{"Start":"08:00.350 ","End":"08:03.200","Text":"no parameter is needed because it uses it\u0027s"},{"Start":"08:03.200 ","End":"08:08.750","Text":"own instance variables to get the time and just outputs it to the terminal."},{"Start":"08:08.750 ","End":"08:11.150","Text":"That\u0027s us done then."},{"Start":"08:11.150 ","End":"08:19.365","Text":"Let\u0027s test as we\u0027ve been asked to do in Part e. Let\u0027s see what happens."},{"Start":"08:19.365 ","End":"08:22.490","Text":"We\u0027re going to use set first of all,"},{"Start":"08:22.490 ","End":"08:29.115","Text":"let\u0027s create an instance of the clock so we can then set a time."},{"Start":"08:29.115 ","End":"08:36.305","Text":"The time we\u0027ve been asked to set is 12:59:57 seconds."},{"Start":"08:36.305 ","End":"08:38.615","Text":"We have a look inside the clock."},{"Start":"08:38.615 ","End":"08:42.695","Text":"We can see that those values have been set and so that\u0027s great."},{"Start":"08:42.695 ","End":"08:50.600","Text":"Then we can call the run method 3 times to see if time wraps around to 1."},{"Start":"08:50.600 ","End":"08:51.890","Text":"I\u0027m just going to check here."},{"Start":"08:51.890 ","End":"08:56.735","Text":"Then I\u0027ve got clear screen at method called set, which I have,"},{"Start":"08:56.735 ","End":"08:59.780","Text":"it\u0027s got a tick next to it because that\u0027s what I\u0027ve been asked to do beginning in"},{"Start":"08:59.780 ","End":"09:04.190","Text":"Part e. Let\u0027s call the run method 3 times."},{"Start":"09:04.190 ","End":"09:05.570","Text":"Here\u0027s the first time."},{"Start":"09:05.570 ","End":"09:07.625","Text":"It\u0027s now 12:59:58,"},{"Start":"09:07.625 ","End":"09:11.305","Text":"we saw it as 57 just before we run it."},{"Start":"09:11.305 ","End":"09:13.740","Text":"Now 59 the second time,"},{"Start":"09:13.740 ","End":"09:17.085","Text":"and now the third time, it\u0027s hit 1."},{"Start":"09:17.085 ","End":"09:21.400","Text":"That\u0027s basically wrapped down to what we want it to do."},{"Start":"09:21.400 ","End":"09:28.615","Text":"It doesn\u0027t look great and that\u0027s because we expect with time to see a leading 0 in front."},{"Start":"09:28.615 ","End":"09:31.030","Text":"If you wanted to fix that,"},{"Start":"09:31.030 ","End":"09:33.790","Text":"it\u0027s relatively straightforward there is"},{"Start":"09:33.790 ","End":"09:37.855","Text":"another version of print within the system out library called print f,"},{"Start":"09:37.855 ","End":"09:40.540","Text":"which is a formatted print."},{"Start":"09:40.540 ","End":"09:44.180","Text":"What that does is allows us to put leading 0s and things like that in."},{"Start":"09:44.180 ","End":"09:50.380","Text":"I\u0027ll show you the format for a formatted print."},{"Start":"09:50.380 ","End":"09:52.480","Text":"Next, let\u0027s just take all this up."},{"Start":"09:52.480 ","End":"09:59.855","Text":"What we want to do is put placeholders in for the values that we\u0027re going to"},{"Start":"09:59.855 ","End":"10:08.450","Text":"print and %2d means I want to pay 2 digit integer to be displayed."},{"Start":"10:08.450 ","End":"10:10.070","Text":"If I want a leading 0,"},{"Start":"10:10.070 ","End":"10:13.440","Text":"then I put a 0 in front, so 2 digits,"},{"Start":"10:13.440 ","End":"10:16.835","Text":"and that means if only 1 is available,"},{"Start":"10:16.835 ","End":"10:18.410","Text":"it will put a 0 in front,"},{"Start":"10:18.410 ","End":"10:21.350","Text":"if it\u0027s 2, then it\u0027ll display the 2 digits."},{"Start":"10:21.350 ","End":"10:23.360","Text":"Then any other texts I want to print."},{"Start":"10:23.360 ","End":"10:24.830","Text":"I just want to print a colon."},{"Start":"10:24.830 ","End":"10:26.700","Text":"It will print the colon."},{"Start":"10:26.700 ","End":"10:31.000","Text":"Then I just repeat that again %02d,"},{"Start":"10:31.550 ","End":"10:39.465","Text":"and that will begin for the minutes and then a final one %02d again."},{"Start":"10:39.465 ","End":"10:42.360","Text":"I\u0027ve got now a formatted string."},{"Start":"10:42.360 ","End":"10:45.240","Text":"It\u0027s saying print an integer to"},{"Start":"10:45.240 ","End":"10:51.305","Text":"2 places and if it is only 1 digit and put a 0 in front of it,"},{"Start":"10:51.305 ","End":"10:52.939","Text":"and then print a colon,"},{"Start":"10:52.939 ","End":"10:55.625","Text":"and then print another 2 digits,"},{"Start":"10:55.625 ","End":"10:58.340","Text":"and then a colon and then another 2 digits."},{"Start":"10:58.340 ","End":"11:01.220","Text":"Now what am I actually supposed to print and this is the nice thing"},{"Start":"11:01.220 ","End":"11:04.460","Text":"about print f. Print on its own only"},{"Start":"11:04.460 ","End":"11:07.580","Text":"accepts 1 argument and we have to concatenate anything"},{"Start":"11:07.580 ","End":"11:11.405","Text":"together if we want to print multiple things on 1 line."},{"Start":"11:11.405 ","End":"11:16.325","Text":"With print f, it accepts multiple arguments and in this case,"},{"Start":"11:16.325 ","End":"11:20.450","Text":"take each of these arguments to mean 1 of these placeholders."},{"Start":"11:20.450 ","End":"11:23.585","Text":"In our case, we want to put powers,"},{"Start":"11:23.585 ","End":"11:28.640","Text":"minutes and seconds because those are the values we want to print but we"},{"Start":"11:28.640 ","End":"11:33.560","Text":"want to print them to 2 digits with a 0 in front if necessary."},{"Start":"11:33.560 ","End":"11:36.785","Text":"So let\u0027s just see if that does what we want."},{"Start":"11:36.785 ","End":"11:42.705","Text":"I create a new clock and run it."},{"Start":"11:42.705 ","End":"11:49.010","Text":"You\u0027ll see it\u0027s 12:00:01 is I would expect it\u0027s now got leading 0s,"},{"Start":"11:49.010 ","End":"11:51.550","Text":"that\u0027s much more like a clock."},{"Start":"11:51.550 ","End":"11:53.610","Text":"We\u0027ve been asked in the final question,"},{"Start":"11:53.610 ","End":"11:58.550","Text":"why does display time not appear in the list of"},{"Start":"11:58.550 ","End":"12:00.920","Text":"methods when you right-click in"},{"Start":"12:00.920 ","End":"12:03.890","Text":"Part f. It asks us because you\u0027ll notice here we\u0027ve got run,"},{"Start":"12:03.890 ","End":"12:07.010","Text":"we got set, but we don\u0027t"},{"Start":"12:07.010 ","End":"12:10.400","Text":"see display time even though we know it\u0027s there and we know it\u0027s working,"},{"Start":"12:10.400 ","End":"12:15.410","Text":"we\u0027ve just actually modified it just now to use print f. Why does it not appear in here?"},{"Start":"12:15.410 ","End":"12:18.200","Text":"Well, if you look at the code is very obvious,"},{"Start":"12:18.200 ","End":"12:22.280","Text":"it\u0027s marked as a private method."},{"Start":"12:22.280 ","End":"12:31.970","Text":"That\u0027s why it doesn\u0027t appear in this list here when we right-click on it."},{"Start":"12:31.970 ","End":"12:33.560","Text":"This is public, so it does,"},{"Start":"12:33.560 ","End":"12:35.255","Text":"this is public so it does."},{"Start":"12:35.255 ","End":"12:39.380","Text":"However, anything that\u0027s private is not going to appear in there."},{"Start":"12:39.380 ","End":"12:44.060","Text":"That\u0027s right because we don\u0027t need to ever run display time."},{"Start":"12:44.060 ","End":"12:48.320","Text":"Display time is automatically called anytime the clock is run,"},{"Start":"12:48.320 ","End":"12:51.635","Text":"because if the seconds increase or any of the other digits increase,"},{"Start":"12:51.635 ","End":"12:53.900","Text":"we need to display the updated time."},{"Start":"12:53.900 ","End":"12:55.610","Text":"If we\u0027re just setting the time,"},{"Start":"12:55.610 ","End":"12:58.115","Text":"we don\u0027t want to display it."},{"Start":"12:58.115 ","End":"13:00.060","Text":"That\u0027s part of runs job."},{"Start":"13:00.060 ","End":"13:02.105","Text":"We could display it if we wanted to,"},{"Start":"13:02.105 ","End":"13:05.300","Text":"but that\u0027s just left to run here."},{"Start":"13:05.300 ","End":"13:10.140","Text":"That\u0027s it for this exercise and I\u0027ll see you shortly for the next one."}],"ID":30283},{"Watched":false,"Name":"Exercise 4","Duration":"8m 22s","ChapterTopicVideoID":28777,"CourseChapterTopicPlaylistID":287464,"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.700","Text":"Hello, welcome to this next exercise."},{"Start":"00:02.700 ","End":"00:04.980","Text":"We\u0027ve been asked to create another class within"},{"Start":"00:04.980 ","End":"00:08.325","Text":"the same project as before called Clock24."},{"Start":"00:08.325 ","End":"00:13.230","Text":"In part a, to produce a modified version of the clock from the previous exercise with"},{"Start":"00:13.230 ","End":"00:18.705","Text":"a modified run method that keeps 24-hour time instead of 12-hour time."},{"Start":"00:18.705 ","End":"00:24.435","Text":"We are asked to add a private string attribute called location in part b."},{"Start":"00:24.435 ","End":"00:28.275","Text":"In part c, to add a public method called set location,"},{"Start":"00:28.275 ","End":"00:31.425","Text":"which accepts a string parameter and returns no value."},{"Start":"00:31.425 ","End":"00:36.290","Text":"It takes the value passed in and stores it in location."},{"Start":"00:36.290 ","End":"00:39.970","Text":"In part d, we\u0027re told to modify the display time method."},{"Start":"00:39.970 ","End":"00:42.380","Text":"It shows the value of location before"},{"Start":"00:42.380 ","End":"00:46.895","Text":"the time and prints a new line character at the end of the time."},{"Start":"00:46.895 ","End":"00:52.238","Text":"In part e, we\u0027re asked to test by creating 3 different objects from the Clock24 class."},{"Start":"00:52.238 ","End":"00:57.560","Text":"We turn off the clear screen at method call option in the BlueJ terminal window."},{"Start":"00:57.560 ","End":"01:02.840","Text":"Then we set 1 clock to a time of 9:30 and location New York,"},{"Start":"01:02.840 ","End":"01:08.345","Text":"another to a time of 13:30, and location London."},{"Start":"01:08.345 ","End":"01:13.340","Text":"The final 1 has a time of 21:30 and location Tokyo."},{"Start":"01:13.340 ","End":"01:17.960","Text":"We use the run method on each of the 3 clocks to"},{"Start":"01:17.960 ","End":"01:23.105","Text":"see if the time is correctly kept for each of the 3 individual clocks."},{"Start":"01:23.105 ","End":"01:26.975","Text":"I\u0027ve created my class called Clock24,"},{"Start":"01:26.975 ","End":"01:31.115","Text":"and we\u0027ve been told to create a modified version of"},{"Start":"01:31.115 ","End":"01:35.240","Text":"the clock from the previous exercise with a modified run method."},{"Start":"01:35.240 ","End":"01:38.315","Text":"I may as well take everything from that class,"},{"Start":"01:38.315 ","End":"01:44.540","Text":"and just paste it in here and change that modified run method in my new class."},{"Start":"01:44.540 ","End":"01:47.150","Text":"I\u0027m just going to take that and paste it in here,"},{"Start":"01:47.150 ","End":"01:48.920","Text":"so there we go."},{"Start":"01:48.920 ","End":"01:55.430","Text":"The only change I\u0027ve been asked to make is to keep 24-hour time rather than 12-hour time."},{"Start":"01:55.430 ","End":"01:59.030","Text":"Literally, the only change I need to make is down here."},{"Start":"01:59.030 ","End":"02:02.090","Text":"When I hit 13, I want to allow it to hit 13."},{"Start":"02:02.090 ","End":"02:06.590","Text":"In fact, I want it to go all the way up to 24, but no further."},{"Start":"02:06.590 ","End":"02:09.410","Text":"It goes to 24 and when it hits 24,"},{"Start":"02:09.410 ","End":"02:12.185","Text":"it goes back to 1 this time."},{"Start":"02:12.185 ","End":"02:20.210","Text":"So 23:59:59 should take me back to 0,"},{"Start":"02:20.210 ","End":"02:23.115","Text":"in fact, not to 1."},{"Start":"02:23.115 ","End":"02:28.860","Text":"That is what a 24-hour clock would display the time as."},{"Start":"02:28.860 ","End":"02:30.980","Text":"That\u0027s part a."},{"Start":"02:30.980 ","End":"02:33.455","Text":"That\u0027s literally all I need to do for part a."},{"Start":"02:33.455 ","End":"02:38.315","Text":"That will keep 24-hour time rather than 12-hour time."},{"Start":"02:38.315 ","End":"02:43.070","Text":"I\u0027ve been asked in part b though to add a new attribute,"},{"Start":"02:43.070 ","End":"02:45.335","Text":"instance variable, whatever you want to call it,"},{"Start":"02:45.335 ","End":"02:49.130","Text":"called location, and that is a string."},{"Start":"02:49.130 ","End":"02:54.080","Text":"Let\u0027s do that. Private string location,"},{"Start":"02:54.080 ","End":"02:57.960","Text":"there\u0027s no initial value specified."},{"Start":"02:57.960 ","End":"03:03.725","Text":"Then I need to create a method in part c that allows me to set a location."},{"Start":"03:03.725 ","End":"03:06.290","Text":"That\u0027s a public method."},{"Start":"03:06.290 ","End":"03:09.260","Text":"It doesn\u0027t return any,"},{"Start":"03:09.260 ","End":"03:13.635","Text":"but it\u0027s called set location."},{"Start":"03:13.635 ","End":"03:16.500","Text":"Obviously, I need to pass in a string,"},{"Start":"03:16.500 ","End":"03:18.810","Text":"so I call my string"},{"Start":"03:18.810 ","End":"03:25.130","Text":"s. It\u0027s perfectly acceptable here because we\u0027re just using it as a parameter."},{"Start":"03:25.130 ","End":"03:28.790","Text":"All I need to do is into location,"},{"Start":"03:28.790 ","End":"03:31.160","Text":"store that value that has been passed in."},{"Start":"03:31.160 ","End":"03:35.030","Text":"That\u0027s all I need to do in this method,"},{"Start":"03:35.030 ","End":"03:37.325","Text":"is just store that value away."},{"Start":"03:37.325 ","End":"03:40.445","Text":"I suppose I could do some validation here to check"},{"Start":"03:40.445 ","End":"03:45.090","Text":"that it\u0027s below a certain length and things like that,"},{"Start":"03:45.090 ","End":"03:48.175","Text":"but I\u0027m just going to leave it as it is."},{"Start":"03:48.175 ","End":"03:50.195","Text":"We\u0027ve got that done."},{"Start":"03:50.195 ","End":"03:55.085","Text":"The last thing to do is part d before we start testing in part e"},{"Start":"03:55.085 ","End":"04:00.240","Text":"and that\u0027s to modify display time so it shows the value of location."},{"Start":"04:00.240 ","End":"04:03.455","Text":"Where is location that we\u0027re printing the time for?"},{"Start":"04:03.455 ","End":"04:06.740","Text":"But it also prints a new line character at the end of the line."},{"Start":"04:06.740 ","End":"04:10.400","Text":"I\u0027m going to stick with my formatted print here because it\u0027d be neat."},{"Start":"04:10.400 ","End":"04:12.965","Text":"I have to have done it, and specify that in the question."},{"Start":"04:12.965 ","End":"04:18.360","Text":"A formatted print for a string is percent"},{"Start":"04:18.360 ","End":"04:25.215","Text":"s. I\u0027m going to print the string and then say time is."},{"Start":"04:25.215 ","End":"04:30.590","Text":"Then I have to add in here location because"},{"Start":"04:30.590 ","End":"04:36.145","Text":"I\u0027ve now got 4 different placeholders in here for the time."},{"Start":"04:36.145 ","End":"04:40.345","Text":"The first one is the location that I\u0027m looking for."},{"Start":"04:40.345 ","End":"04:42.230","Text":"No errors there."},{"Start":"04:42.230 ","End":"04:45.830","Text":"Let\u0027s have a go to running this, testing it."},{"Start":"04:45.830 ","End":"04:50.150","Text":"Clock24, I\u0027m going to create my first new clock."},{"Start":"04:50.150 ","End":"04:55.335","Text":"I\u0027ve been asked to create 3 new objects from this class."},{"Start":"04:55.335 ","End":"05:00.465","Text":"The first one is a time of 9:30 and location of New York."},{"Start":"05:00.465 ","End":"05:09.488","Text":"I accept the instance name and set the time to 9:30,"},{"Start":"05:09.488 ","End":"05:16.150","Text":"and set the location to New York."},{"Start":"05:16.150 ","End":"05:20.450","Text":"I will need quotes around that because it\u0027s a string."},{"Start":"05:20.720 ","End":"05:23.740","Text":"I\u0027m done with my first object."},{"Start":"05:23.740 ","End":"05:30.115","Text":"That\u0027s part e1 and another clock with a time of 13:30 and location London."},{"Start":"05:30.115 ","End":"05:36.445","Text":"I now need to create a new clock and set its time."},{"Start":"05:36.445 ","End":"05:41.990","Text":"That is 1:30, in the 24-hour clock,13:30."},{"Start":"05:44.930 ","End":"05:51.050","Text":"The location for that object should be set as London."},{"Start":"05:52.980 ","End":"05:57.840","Text":"Then finally one more clock, the third one,"},{"Start":"05:57.840 ","End":"06:01.760","Text":"which is going to be set to a time of"},{"Start":"06:01.760 ","End":"06:12.550","Text":"21:30 and a location set of Tokyo."},{"Start":"06:12.550 ","End":"06:17.345","Text":"I can look inside all these objects to see what they contain."},{"Start":"06:17.345 ","End":"06:20.960","Text":"This one\u0027s 9:30 and New York."},{"Start":"06:20.960 ","End":"06:24.890","Text":"This one\u0027s 13:30 and London."},{"Start":"06:24.890 ","End":"06:28.520","Text":"Just make that a bit bigger so we can see it, 13:30 and London."},{"Start":"06:28.520 ","End":"06:32.660","Text":"This final one is 21:30 and Tokyo."},{"Start":"06:32.660 ","End":"06:38.685","Text":"We wouldn\u0027t have been able to store that previously because our clock would reset at 12,"},{"Start":"06:38.685 ","End":"06:41.190","Text":"going to 13 would go back to 1."},{"Start":"06:41.190 ","End":"06:43.280","Text":"Let\u0027s see now what happens."},{"Start":"06:43.280 ","End":"06:44.800","Text":"I was asked to just check whether"},{"Start":"06:44.800 ","End":"06:46.670","Text":"the clear screening method call is off"},{"Start":"06:46.670 ","End":"06:49.010","Text":"because I want to see them all at once and it is for me."},{"Start":"06:49.010 ","End":"06:50.800","Text":"Let\u0027s do them one at a time."},{"Start":"06:50.800 ","End":"06:52.145","Text":"If I run this clock,"},{"Start":"06:52.145 ","End":"06:57.095","Text":"I see New York time is 9:30 and 1 second because I\u0027ve advanced that clock by 1 second."},{"Start":"06:57.095 ","End":"06:59.195","Text":"If I run this clock,"},{"Start":"06:59.195 ","End":"07:03.770","Text":"I see London time is 13:30 and 1 second."},{"Start":"07:03.770 ","End":"07:06.095","Text":"If I advance this clock,"},{"Start":"07:06.095 ","End":"07:11.735","Text":"then I see Tokyo time is 21:30 and 1 second."},{"Start":"07:11.735 ","End":"07:16.100","Text":"It\u0027s advancing all of the clocks independently."},{"Start":"07:16.100 ","End":"07:18.830","Text":"Each time I run the individual clock,"},{"Start":"07:18.830 ","End":"07:21.830","Text":"it\u0027s keeping track of its own instance variables."},{"Start":"07:21.830 ","End":"07:25.250","Text":"I\u0027m able to display the time now with the location,"},{"Start":"07:25.250 ","End":"07:28.805","Text":"I probably should have printed a new line at the end of the whole string as well."},{"Start":"07:28.805 ","End":"07:30.830","Text":"I forced it onto a new line each time,"},{"Start":"07:30.830 ","End":"07:34.085","Text":"it would\u0027ve been a slightly nicer output. But you get the idea."},{"Start":"07:34.085 ","End":"07:36.860","Text":"I\u0027ve got now 3 different objects,"},{"Start":"07:36.860 ","End":"07:39.920","Text":"all created from this one set of code."},{"Start":"07:39.920 ","End":"07:44.150","Text":"Is actually relatively useful because I have"},{"Start":"07:44.150 ","End":"07:48.200","Text":"got 3 different clocks all telling the time in different time zones,"},{"Start":"07:48.200 ","End":"07:51.440","Text":"and I know which time zone I\u0027m talking about because it\u0027s stored"},{"Start":"07:51.440 ","End":"07:56.045","Text":"the location and the times are all independent of each other."},{"Start":"07:56.045 ","End":"07:57.605","Text":"Each time I run the clock,"},{"Start":"07:57.605 ","End":"07:59.585","Text":"I only advance that clock."},{"Start":"07:59.585 ","End":"08:02.990","Text":"If I do 3, one after another, they were all in sync,"},{"Start":"08:02.990 ","End":"08:07.265","Text":"but with their respective time differences intact."},{"Start":"08:07.265 ","End":"08:13.980","Text":"Another good example where these objects can be useful in a program."},{"Start":"08:13.980 ","End":"08:15.960","Text":"Hopefully, it\u0027s now really starting to sink in,"},{"Start":"08:15.960 ","End":"08:19.305","Text":"this whole idea of object orientation."},{"Start":"08:19.305 ","End":"08:22.830","Text":"Thanks for watching and see you in the next one."}],"ID":30284},{"Watched":false,"Name":"Exercise 5","Duration":"8m 40s","ChapterTopicVideoID":28778,"CourseChapterTopicPlaylistID":287464,"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":"Welcome back. In this exercise,"},{"Start":"00:02.460 ","End":"00:04.890","Text":"we\u0027ve been asked to create a new class called TeamMember,"},{"Start":"00:04.890 ","End":"00:09.330","Text":"which will be used to store the name of a person and a single task assigned to them."},{"Start":"00:09.330 ","End":"00:14.190","Text":"In Part A, we\u0027re asked to make sure that we have 2 string instance variables,"},{"Start":"00:14.190 ","End":"00:16.275","Text":"name, and task,"},{"Start":"00:16.275 ","End":"00:20.810","Text":"and in Part B to create a method setName that allows the name variable"},{"Start":"00:20.810 ","End":"00:25.850","Text":"to be set to a string passed in as a parameter and returns no value."},{"Start":"00:25.850 ","End":"00:28.445","Text":"In Part C, we\u0027re asked to create another method,"},{"Start":"00:28.445 ","End":"00:31.010","Text":"addTask that allows a task to be passed in as"},{"Start":"00:31.010 ","End":"00:34.865","Text":"a string and stores it into the task instance variable,"},{"Start":"00:34.865 ","End":"00:37.900","Text":"but only if there\u0027s not been a task previously set,"},{"Start":"00:37.900 ","End":"00:40.895","Text":"i.e., task is equal to null."},{"Start":"00:40.895 ","End":"00:43.160","Text":"If a task is successfully added,"},{"Start":"00:43.160 ","End":"00:44.900","Text":"the method should return true,"},{"Start":"00:44.900 ","End":"00:47.570","Text":"otherwise, it should return false."},{"Start":"00:47.570 ","End":"00:51.065","Text":"In Part D, we\u0027re asked to create a method called getTask,"},{"Start":"00:51.065 ","End":"00:53.900","Text":"which copies the task previously stored into"},{"Start":"00:53.900 ","End":"01:00.430","Text":"a temporary variable and then stores null into the task instance variable,"},{"Start":"01:00.430 ","End":"01:05.930","Text":"and finally returns the contents of the name variable concatenated with a colon"},{"Start":"01:05.930 ","End":"01:11.060","Text":"and space concatenated with the previously stored task from the temporary variable."},{"Start":"01:11.060 ","End":"01:14.795","Text":"For example, Bob: pack orders."},{"Start":"01:14.795 ","End":"01:17.960","Text":"We test them by creating an object from the class."},{"Start":"01:17.960 ","End":"01:20.780","Text":"Then you setName to set the name to Bob,"},{"Start":"01:20.780 ","End":"01:24.740","Text":"addTasks to set a task of deliveries should return true."},{"Start":"01:24.740 ","End":"01:29.540","Text":"addTask again to set a task of pack orders which should return false,"},{"Start":"01:29.540 ","End":"01:35.810","Text":"use getTask which should return the string Bob: deliveries."},{"Start":"01:35.810 ","End":"01:39.470","Text":"Then create another object from a class with a name Sara and a task of"},{"Start":"01:39.470 ","End":"01:44.150","Text":"sales and run getTask on this second object twice,"},{"Start":"01:44.150 ","End":"01:47.465","Text":"which should return Sara: sales the first time,"},{"Start":"01:47.465 ","End":"01:51.335","Text":"and then Sara: null the second time."},{"Start":"01:51.335 ","End":"01:56.795","Text":"The first thing we need to do is to create our 2 instance variables,"},{"Start":"01:56.795 ","End":"02:01.055","Text":"both strings and no initial value specified,"},{"Start":"02:01.055 ","End":"02:02.600","Text":"so I would just leave them as they are."},{"Start":"02:02.600 ","End":"02:05.075","Text":"The first one is called name,"},{"Start":"02:05.075 ","End":"02:09.695","Text":"and the second one is called task."},{"Start":"02:09.695 ","End":"02:12.235","Text":"That\u0027s Part A done."},{"Start":"02:12.235 ","End":"02:17.180","Text":"Then we are asked in Part B to create a method called setName,"},{"Start":"02:17.180 ","End":"02:19.235","Text":"which returns no value."},{"Start":"02:19.235 ","End":"02:24.990","Text":"Methods generally aren going to be public and returns no value,"},{"Start":"02:24.990 ","End":"02:27.465","Text":"so void setName,"},{"Start":"02:27.465 ","End":"02:33.740","Text":"and then we have a parameter which is the name that we\u0027re going to store into name,"},{"Start":"02:33.740 ","End":"02:37.480","Text":"so it\u0027s going to be a string because name is a string."},{"Start":"02:37.480 ","End":"02:41.420","Text":"Let\u0027s just call it S for string,"},{"Start":"02:41.420 ","End":"02:46.730","Text":"and then all we need to do inside here is say name=s."},{"Start":"02:46.890 ","End":"02:49.180","Text":"That\u0027s done."},{"Start":"02:49.180 ","End":"02:52.045","Text":"We\u0027ve done very similar things in previous exercises."},{"Start":"02:52.045 ","End":"02:55.675","Text":"The more important method here is addTask,"},{"Start":"02:55.675 ","End":"02:58.030","Text":"which also accepts a string,"},{"Start":"02:58.030 ","End":"03:00.195","Text":"but this time returns a Boolean."},{"Start":"03:00.195 ","End":"03:05.324","Text":"That\u0027s the necessary coding."},{"Start":"03:05.324 ","End":"03:13.545","Text":"Boolean addTask string and let\u0027s call it t for task,"},{"Start":"03:13.545 ","End":"03:15.470","Text":"so what are we to do here?"},{"Start":"03:15.470 ","End":"03:21.560","Text":"We have to check first of all if there\u0027s not been a task previously set."},{"Start":"03:21.560 ","End":"03:23.765","Text":"When you create a string,"},{"Start":"03:23.765 ","End":"03:26.450","Text":"initially it points to null,"},{"Start":"03:26.450 ","End":"03:28.384","Text":"has a null reference basically,"},{"Start":"03:28.384 ","End":"03:31.010","Text":"meaning the string\u0027s value has never been set."},{"Start":"03:31.010 ","End":"03:36.860","Text":"We\u0027re going to compare task to null to see if it is null."},{"Start":"03:36.860 ","End":"03:41.550","Text":"If it is, then we\u0027re okay to write something to task,"},{"Start":"03:41.550 ","End":"03:44.645","Text":"otherwise we leave it as it is because it\u0027s already been set."},{"Start":"03:44.645 ","End":"03:46.640","Text":"Simple as this."},{"Start":"03:46.640 ","End":"03:50.210","Text":"If task double equals null,"},{"Start":"03:50.210 ","End":"03:58.190","Text":"then we can pass in the new value and store it into tasks,"},{"Start":"03:58.190 ","End":"04:05.420","Text":"so we say task=t because that\u0027s the name of our parameter and then we can return true"},{"Start":"04:05.420 ","End":"04:13.115","Text":"because that\u0027s what we\u0027ve been asked to do because we\u0027ve successfully added this task."},{"Start":"04:13.115 ","End":"04:15.185","Text":"Otherwise we should return false,"},{"Start":"04:15.185 ","End":"04:18.680","Text":"so we just need to put a return false here."},{"Start":"04:18.680 ","End":"04:22.755","Text":"It will only get to this line if it didn\u0027t get to this line."},{"Start":"04:22.755 ","End":"04:25.965","Text":"That does what we want it to do."},{"Start":"04:25.965 ","End":"04:28.710","Text":"Now we\u0027re on to Part D,"},{"Start":"04:28.710 ","End":"04:33.830","Text":"getTask returns a contents of the name variable plus"},{"Start":"04:33.830 ","End":"04:39.485","Text":"some concatenation and it stores null into the task variable."},{"Start":"04:39.485 ","End":"04:41.000","Text":"But we need to first of all,"},{"Start":"04:41.000 ","End":"04:43.340","Text":"create a temporary variable."},{"Start":"04:43.340 ","End":"04:46.640","Text":"Let\u0027s create our method first of all, public."},{"Start":"04:46.640 ","End":"04:48.890","Text":"It\u0027s going to return a string."},{"Start":"04:48.890 ","End":"04:51.500","Text":"It\u0027s called getTask."},{"Start":"04:51.500 ","End":"04:55.400","Text":"Get methods generally don\u0027t have parameters that we could do,"},{"Start":"04:55.400 ","End":"04:57.590","Text":"but most of the time they don\u0027t."},{"Start":"04:57.590 ","End":"04:59.060","Text":"What we need to do is first of all,"},{"Start":"04:59.060 ","End":"05:02.915","Text":"create a temporary variable because our task is a string,"},{"Start":"05:02.915 ","End":"05:06.680","Text":"it\u0027s going to need to be a string datatype,"},{"Start":"05:06.680 ","End":"05:11.125","Text":"and we\u0027re going to store into it the old value of task,"},{"Start":"05:11.125 ","End":"05:14.975","Text":"and so that\u0027s what Part 1 of D has asked us to do,"},{"Start":"05:14.975 ","End":"05:19.580","Text":"copies the task previously stored into a temporary variable and then we\u0027re going to store"},{"Start":"05:19.580 ","End":"05:26.030","Text":"null into task or a null reference, I should say."},{"Start":"05:26.030 ","End":"05:29.215","Text":"A string is an object actually as well."},{"Start":"05:29.215 ","End":"05:31.415","Text":"When an object points to null,"},{"Start":"05:31.415 ","End":"05:34.850","Text":"it means that it\u0027s not been set to anything,"},{"Start":"05:34.850 ","End":"05:37.130","Text":"so a string when it doesn\u0027t have a value,"},{"Start":"05:37.130 ","End":"05:40.385","Text":"we can give it a reference to null."},{"Start":"05:40.385 ","End":"05:42.190","Text":"That\u0027s that part done."},{"Start":"05:42.190 ","End":"05:46.980","Text":"D2, D3, it returns the contents of variable."},{"Start":"05:46.980 ","End":"05:49.029","Text":"We\u0027re going to use the return keyword,"},{"Start":"05:49.029 ","End":"05:51.010","Text":"the variable name,"},{"Start":"05:51.010 ","End":"05:59.800","Text":"and concatenate that with a colon and a space and concatenate that with temporary verb,"},{"Start":"05:59.800 ","End":"06:03.040","Text":"not task because task has been overwritten now,"},{"Start":"06:03.040 ","End":"06:07.450","Text":"but the value that task had when we first came in here."},{"Start":"06:07.450 ","End":"06:10.000","Text":"That\u0027s done now."},{"Start":"06:10.000 ","End":"06:13.945","Text":"We can try this out first of all by creating"},{"Start":"06:13.945 ","End":"06:20.005","Text":"an object and then E1 asks us to setName to Bob."},{"Start":"06:20.005 ","End":"06:22.540","Text":"The strings we need to put it in quotes."},{"Start":"06:22.540 ","End":"06:25.970","Text":"There we go, and then we are asked in Part 2 to"},{"Start":"06:25.970 ","End":"06:30.095","Text":"use addTasks to set a task of deliveries."},{"Start":"06:30.095 ","End":"06:36.170","Text":"Let\u0027s do that, and then we have been asked in Part 3, we got true back,"},{"Start":"06:36.170 ","End":"06:38.735","Text":"which is what we expect because it allowed us to set the value,"},{"Start":"06:38.735 ","End":"06:43.745","Text":"in Part 3 we\u0027ve been asked to use addTask again with a different value,"},{"Start":"06:43.745 ","End":"06:47.395","Text":"this time of pack orders."},{"Start":"06:47.395 ","End":"06:49.880","Text":"That returns as false as we expected."},{"Start":"06:49.880 ","End":"06:51.304","Text":"If we have a look inside,"},{"Start":"06:51.304 ","End":"06:53.660","Text":"pack orders was not put into task,"},{"Start":"06:53.660 ","End":"06:57.380","Text":"deliveries was the one that was in there before and it hasn\u0027t overwritten it."},{"Start":"06:57.380 ","End":"07:00.140","Text":"Which is what we were expecting."},{"Start":"07:00.140 ","End":"07:03.860","Text":"If we do getTask now as we\u0027ve been asked in Part 4,"},{"Start":"07:03.860 ","End":"07:08.960","Text":"and we should see both of those fields putting into a single string and return to us,"},{"Start":"07:08.960 ","End":"07:11.300","Text":"that\u0027s fine and that\u0027s what we expected."},{"Start":"07:11.300 ","End":"07:12.785","Text":"If we have a look inside now,"},{"Start":"07:12.785 ","End":"07:15.920","Text":"the task has been set to null as we would expect."},{"Start":"07:15.920 ","End":"07:20.390","Text":"Then the last little bit to check is to create another object from the class,"},{"Start":"07:20.390 ","End":"07:23.365","Text":"this time with a name of Sara,"},{"Start":"07:23.365 ","End":"07:28.935","Text":"and we\u0027re going to set Sara as a task as well."},{"Start":"07:28.935 ","End":"07:31.280","Text":"Set the name first,"},{"Start":"07:31.280 ","End":"07:36.160","Text":"and then set a task of sales,"},{"Start":"07:36.160 ","End":"07:39.030","Text":"and then we\u0027ve been asked to run. That\u0027s true. That\u0027s good, it was able to do it."},{"Start":"07:39.030 ","End":"07:44.550","Text":"Then we\u0027ve been asked to run getTask twice,"},{"Start":"07:44.550 ","End":"07:48.950","Text":"so we should see Sara: sales the first time and Sara: null a second time."},{"Start":"07:48.950 ","End":"07:51.095","Text":"Let\u0027s do that."},{"Start":"07:51.095 ","End":"07:52.352","Text":"getTask one, yeah,"},{"Start":"07:52.352 ","End":"07:56.235","Text":"we get Sara: sales back and then getTask again,"},{"Start":"07:56.235 ","End":"07:59.990","Text":"we get Sara: null as we\u0027d expect because we\u0027d already done"},{"Start":"07:59.990 ","End":"08:04.385","Text":"the task and we therefore set tasks to null."},{"Start":"08:04.385 ","End":"08:09.175","Text":"Again, another example of where we might use objects in a program,"},{"Start":"08:09.175 ","End":"08:13.579","Text":"you can imagine this being part of some sort of scheduling, program,"},{"Start":"08:13.579 ","End":"08:18.110","Text":"scheduling jobs for people in a team working in a store or something,"},{"Start":"08:18.110 ","End":"08:20.270","Text":"so you\u0027re starting to get a sense of again,"},{"Start":"08:20.270 ","End":"08:24.710","Text":"how an object could be used to represent entities."},{"Start":"08:24.710 ","End":"08:29.870","Text":"In this case, staff members who belong to a team and allocate tasks to them,"},{"Start":"08:29.870 ","End":"08:35.335","Text":"which then can be carried out and then new tasks can be assigned."},{"Start":"08:35.335 ","End":"08:38.945","Text":"We\u0027re going to extend this in the next exercise, but that\u0027s it for now."},{"Start":"08:38.945 ","End":"08:41.460","Text":"See you shortly for the next one."}],"ID":30285},{"Watched":false,"Name":"Exercise 6","Duration":"9m 47s","ChapterTopicVideoID":28779,"CourseChapterTopicPlaylistID":287464,"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.299","Text":"Hello, welcome back. In the previous exercise,"},{"Start":"00:03.299 ","End":"00:08.024","Text":"we created a class for a team member to whom we could assign a task."},{"Start":"00:08.024 ","End":"00:11.070","Text":"In this exercise, we are asked to modify that code"},{"Start":"00:11.070 ","End":"00:14.670","Text":"so that multiple tasks can be assigned to a team member."},{"Start":"00:14.670 ","End":"00:20.235","Text":"In Part A, we\u0027re asked to change the instance variable task into an array of 5 strings."},{"Start":"00:20.235 ","End":"00:24.075","Text":"In Part B, we\u0027re asked to create a new instance variable index,"},{"Start":"00:24.075 ","End":"00:26.670","Text":"which is used as an index into the array and"},{"Start":"00:26.670 ","End":"00:29.700","Text":"also keeps track of how many tasks have been assigned."},{"Start":"00:29.700 ","End":"00:32.200","Text":"In Part C, we\u0027re asked to modify addTask,"},{"Start":"00:32.200 ","End":"00:35.880","Text":"so it allows multiple tasks to be set and stored in the array,"},{"Start":"00:35.880 ","End":"00:39.170","Text":"but only up to a limit of 5 tasks in total."},{"Start":"00:39.170 ","End":"00:41.150","Text":"If it\u0027s able to store a task,"},{"Start":"00:41.150 ","End":"00:42.860","Text":"the method should return true."},{"Start":"00:42.860 ","End":"00:45.725","Text":"Otherwise, it should return false."},{"Start":"00:45.725 ","End":"00:48.260","Text":"In Part D, we modify getTask,"},{"Start":"00:48.260 ","End":"00:53.180","Text":"so it only removes and returns the most recently added task in the array."},{"Start":"00:53.180 ","End":"00:55.865","Text":"In Part 1, we update index,"},{"Start":"00:55.865 ","End":"00:58.220","Text":"if it\u0027s not 0 already."},{"Start":"00:58.220 ","End":"01:02.435","Text":"In Part 2, we mark the item to be removed as null."},{"Start":"01:02.435 ","End":"01:08.390","Text":"In Part 3, we return the current item concatenated with a name and a colon,"},{"Start":"01:08.390 ","End":"01:11.825","Text":"for example, Bob: pack orders."},{"Start":"01:11.825 ","End":"01:14.990","Text":"In Part 4, if there are no tasks in the array,"},{"Start":"01:14.990 ","End":"01:21.130","Text":"we simply return name and colon space null concatenated."},{"Start":"01:21.130 ","End":"01:25.730","Text":"We then test by creating an object from the class and use setName"},{"Start":"01:25.730 ","End":"01:30.200","Text":"to set the name to Bob and add task to set a task of deliveries,"},{"Start":"01:30.200 ","End":"01:32.225","Text":"which should return true."},{"Start":"01:32.225 ","End":"01:35.525","Text":"Add task again to set the task of pack orders,"},{"Start":"01:35.525 ","End":"01:37.310","Text":"which should return true."},{"Start":"01:37.310 ","End":"01:39.950","Text":"Then again to set the task of sales,"},{"Start":"01:39.950 ","End":"01:41.750","Text":"which should again return true."},{"Start":"01:41.750 ","End":"01:46.330","Text":"Once more to set a task of warehouse which should also return true."},{"Start":"01:46.330 ","End":"01:49.280","Text":"Again to add customer service,"},{"Start":"01:49.280 ","End":"01:51.065","Text":"which should return true."},{"Start":"01:51.065 ","End":"01:54.645","Text":"Then one more time to set a task of cleaning,"},{"Start":"01:54.645 ","End":"01:57.050","Text":"which should return this time false."},{"Start":"01:57.050 ","End":"01:58.865","Text":"Then we use getTask,"},{"Start":"01:58.865 ","End":"02:03.935","Text":"which should return the string Bob: space customer service and again,"},{"Start":"02:03.935 ","End":"02:07.400","Text":"which should return Bob: space warehouse."},{"Start":"02:07.400 ","End":"02:13.080","Text":"We continue on until Bob: null is returned."},{"Start":"02:13.080 ","End":"02:18.950","Text":"Let\u0027s go ahead and modify the previous exercise called TeamMember."},{"Start":"02:18.950 ","End":"02:23.990","Text":"The first thing we\u0027ve been asked to do in Part A is to change"},{"Start":"02:23.990 ","End":"02:30.365","Text":"the instance variable task from a single variable into an array of 5 Strings."},{"Start":"02:30.365 ","End":"02:35.105","Text":"To do that, we need to put brackets there to say it\u0027s a string array."},{"Start":"02:35.105 ","End":"02:37.385","Text":"The name is still task,"},{"Start":"02:37.385 ","End":"02:38.945","Text":"or we could call it tasks,"},{"Start":"02:38.945 ","End":"02:42.065","Text":"would make more sense because it\u0027s plural."},{"Start":"02:42.065 ","End":"02:46.255","Text":"Then we size the array by saying new String."},{"Start":"02:46.255 ","End":"02:47.840","Text":"Then the size of the array,"},{"Start":"02:47.840 ","End":"02:49.925","Text":"we\u0027ve been told to make 5."},{"Start":"02:49.925 ","End":"02:53.335","Text":"Now we have an array of 5 strings,"},{"Start":"02:53.335 ","End":"02:55.665","Text":"and the array is called tasks."},{"Start":"02:55.665 ","End":"02:58.610","Text":"That\u0027s the modification we need to make there."},{"Start":"02:58.610 ","End":"03:05.305","Text":"We also need to create an index variable and that\u0027s going to be an int."},{"Start":"03:05.305 ","End":"03:13.955","Text":"It will start at 0 because that\u0027d be the first item in the array. That\u0027s fine."},{"Start":"03:13.955 ","End":"03:17.075","Text":"We\u0027re going to get some errors now because we\u0027ve changed some of their name around us."},{"Start":"03:17.075 ","End":"03:20.070","Text":"That\u0027s fine. We\u0027ll sort those out as we go through."},{"Start":"03:20.720 ","End":"03:29.730","Text":"Part C asks us to modify addTask that allows multiple tasks to a limit of 5."},{"Start":"03:29.730 ","End":"03:35.585","Text":"AddTask now still passing a String of t. This time,"},{"Start":"03:35.585 ","End":"03:39.635","Text":"we don\u0027t ask to see if task is equal to null."},{"Start":"03:39.635 ","End":"03:42.485","Text":"We actually just need to look at index."},{"Start":"03:42.485 ","End":"03:47.335","Text":"If index is less than 5,"},{"Start":"03:47.335 ","End":"03:54.790","Text":"maybe it\u0027s better to say task.length in case we change the size of my array future,"},{"Start":"03:54.790 ","End":"03:56.030","Text":"but if that\u0027s the case,"},{"Start":"03:56.030 ","End":"03:58.085","Text":"then we can add a value."},{"Start":"03:58.085 ","End":"04:04.710","Text":"Otherwise, we don\u0027t want to add a value and we\u0027ll just return false."},{"Start":"04:04.710 ","End":"04:08.300","Text":"The only change we need to make here then is,"},{"Start":"04:08.300 ","End":"04:09.680","Text":"well, a couple of changes,"},{"Start":"04:09.680 ","End":"04:14.825","Text":"but the first one is tasks and then where,"},{"Start":"04:14.825 ","End":"04:17.555","Text":"so we\u0027re using an index for that purpose."},{"Start":"04:17.555 ","End":"04:20.704","Text":"Start at 0 and then as we add items,"},{"Start":"04:20.704 ","End":"04:23.380","Text":"it will go to 1, 2, 3, and 4."},{"Start":"04:23.380 ","End":"04:28.579","Text":"We just pass in the value t and that gets stored in the current location."},{"Start":"04:28.579 ","End":"04:32.495","Text":"One other thing we need to do is to increment index."},{"Start":"04:32.495 ","End":"04:36.490","Text":"The next time we write it to the next element in the array,"},{"Start":"04:36.490 ","End":"04:39.010","Text":"or when index hits 5,"},{"Start":"04:39.010 ","End":"04:42.635","Text":"we stop because we don\u0027t want to go any further."},{"Start":"04:42.635 ","End":"04:48.210","Text":"There\u0027s not enough room. That is that part done."},{"Start":"04:48.210 ","End":"04:52.640","Text":"Part D, we now need to modify."},{"Start":"04:52.640 ","End":"04:57.500","Text":"It only removes and returns the most recently added task in the array."},{"Start":"04:57.500 ","End":"04:59.990","Text":"How do we do that?"},{"Start":"04:59.990 ","End":"05:02.690","Text":"We still need the temp variable,"},{"Start":"05:02.690 ","End":"05:09.470","Text":"but what we need to do first is we need to just check if index is greater than 0."},{"Start":"05:09.470 ","End":"05:12.140","Text":"Because if it\u0027s not, we\u0027re going to try"},{"Start":"05:12.140 ","End":"05:15.155","Text":"and go off the front of the array because an error."},{"Start":"05:15.155 ","End":"05:16.580","Text":"Let\u0027s indent all of these."},{"Start":"05:16.580 ","End":"05:17.990","Text":"We might get rid of some of them."},{"Start":"05:17.990 ","End":"05:19.355","Text":"There\u0027s our if statement."},{"Start":"05:19.355 ","End":"05:22.195","Text":"If index is greater than 0,"},{"Start":"05:22.195 ","End":"05:26.550","Text":"we are going to subtract 1 from index."},{"Start":"05:26.550 ","End":"05:28.920","Text":"Otherwise, it will go to minus 1."},{"Start":"05:28.920 ","End":"05:32.270","Text":"We don\u0027t want to do that. Then this bit"},{"Start":"05:32.270 ","End":"05:36.965","Text":"stays the same except for the array is called tasks."},{"Start":"05:36.965 ","End":"05:41.090","Text":"We need to give a position which is 1 at index."},{"Start":"05:41.090 ","End":"05:44.050","Text":"That\u0027s why we incremented it first."},{"Start":"05:44.050 ","End":"05:45.800","Text":"Once we\u0027ve incremented it,"},{"Start":"05:45.800 ","End":"05:49.040","Text":"we\u0027re looking at that element because this index contains where you\u0027re"},{"Start":"05:49.040 ","End":"05:52.910","Text":"going to write the next tasks to the array."},{"Start":"05:52.910 ","End":"05:54.005","Text":"When we taking it out,"},{"Start":"05:54.005 ","End":"05:55.730","Text":"we subtract 1 first,"},{"Start":"05:55.730 ","End":"05:57.950","Text":"then we take it out."},{"Start":"05:57.950 ","End":"06:06.295","Text":"Then we have the value in temp and we can store into current position null."},{"Start":"06:06.295 ","End":"06:13.700","Text":"Then we can return name and the colon and space and temp as before."},{"Start":"06:13.700 ","End":"06:22.760","Text":"The only other change we need to make is here."},{"Start":"06:22.760 ","End":"06:25.430","Text":"Actually, we don\u0027t need an else because we don\u0027t ever get here."},{"Start":"06:25.430 ","End":"06:28.085","Text":"If this wasn\u0027t true,"},{"Start":"06:28.085 ","End":"06:35.210","Text":"we\u0027re going to return name plus a colon and a space."},{"Start":"06:35.210 ","End":"06:38.525","Text":"We can just put the word null because it\u0027s going to be a string anyway."},{"Start":"06:38.525 ","End":"06:43.680","Text":"That will do the job that we\u0027re looking to do."},{"Start":"06:43.960 ","End":"06:47.145","Text":"All good apart from this,"},{"Start":"06:47.145 ","End":"06:49.865","Text":"I think we\u0027ve got an S there."},{"Start":"06:49.865 ","End":"06:53.420","Text":"We changed the name of our array from task to tasks,"},{"Start":"06:53.420 ","End":"06:55.550","Text":"was just a variable not an array initially."},{"Start":"06:55.550 ","End":"06:57.845","Text":"That\u0027s got rid of that error now."},{"Start":"06:57.845 ","End":"07:02.160","Text":"Let\u0027s create an object."},{"Start":"07:02.160 ","End":"07:08.270","Text":"There we go, created an object and a name Bob again, on that object."},{"Start":"07:08.270 ","End":"07:13.970","Text":"We\u0027ll set a name and we\u0027re going to add a whole bunch of tasks."},{"Start":"07:13.970 ","End":"07:17.570","Text":"The first one is deliveries."},{"Start":"07:17.570 ","End":"07:20.795","Text":"Next one, and that\u0027s okay, it\u0027s good."},{"Start":"07:20.795 ","End":"07:23.900","Text":"Pack orders and that\u0027s also good."},{"Start":"07:23.900 ","End":"07:30.535","Text":"Then sales, and then warehouse."},{"Start":"07:30.535 ","End":"07:34.625","Text":"Finally, customer service."},{"Start":"07:34.625 ","End":"07:37.535","Text":"Let\u0027s just have a look inside."},{"Start":"07:37.535 ","End":"07:39.560","Text":"It\u0027s allowed us to set all of those."},{"Start":"07:39.560 ","End":"07:41.165","Text":"Let\u0027s have a look inside."},{"Start":"07:41.165 ","End":"07:43.910","Text":"You\u0027ll see here, because you\u0027ve got an array,"},{"Start":"07:43.910 ","End":"07:46.250","Text":"we can\u0027t directly see the value but if you click on this,"},{"Start":"07:46.250 ","End":"07:48.425","Text":"it\u0027s saying that there\u0027s a reference to an array."},{"Start":"07:48.425 ","End":"07:51.080","Text":"We click on there and we can see our array."},{"Start":"07:51.080 ","End":"07:56.705","Text":"It\u0027s got the 5 values in the order that we put them in, from index 0-4."},{"Start":"07:56.705 ","End":"07:59.995","Text":"That\u0027s great, that\u0027s what we expect."},{"Start":"07:59.995 ","End":"08:01.880","Text":"I suppose we expect,"},{"Start":"08:01.880 ","End":"08:06.890","Text":"when we add the next one in Part 7 of cleaning,"},{"Start":"08:06.890 ","End":"08:12.225","Text":"it should refuse to do that one because it\u0027s too many tasks."},{"Start":"08:12.225 ","End":"08:14.355","Text":"We do get false back."},{"Start":"08:14.355 ","End":"08:22.295","Text":"Just to check that we haven\u0027t overwritten that last element, and that\u0027s right."},{"Start":"08:22.295 ","End":"08:24.350","Text":"It\u0027s just got customer service in there."},{"Start":"08:24.350 ","End":"08:26.375","Text":"All of that looks good."},{"Start":"08:26.375 ","End":"08:28.610","Text":"If we run getTask,"},{"Start":"08:28.610 ","End":"08:30.950","Text":"it should be the last task we added,"},{"Start":"08:30.950 ","End":"08:33.310","Text":"will come out for Bob."},{"Start":"08:33.310 ","End":"08:36.230","Text":"That was it, customer service as we expected."},{"Start":"08:36.230 ","End":"08:39.290","Text":"Do it again and we\u0027ll get another one,"},{"Start":"08:39.290 ","End":"08:43.055","Text":"which was warehouse, which was the last but 1."},{"Start":"08:43.055 ","End":"08:44.570","Text":"If we look in the array,"},{"Start":"08:44.570 ","End":"08:51.095","Text":"we\u0027ll see that we\u0027ve got null written into those last and we\u0027ve got 3 other values here."},{"Start":"08:51.095 ","End":"08:53.120","Text":"If I keep going,"},{"Start":"08:53.120 ","End":"08:55.760","Text":"getTask and getTask again,"},{"Start":"08:55.760 ","End":"08:59.555","Text":"eventually, I\u0027ll get a last task which was that one."},{"Start":"08:59.555 ","End":"09:01.055","Text":"The first one we set."},{"Start":"09:01.055 ","End":"09:03.965","Text":"I\u0027d try and do it again. It\u0027ll be null."},{"Start":"09:03.965 ","End":"09:06.200","Text":"I could do that as many times as I like and it will keep"},{"Start":"09:06.200 ","End":"09:09.290","Text":"returning null because there\u0027s no other values."},{"Start":"09:09.290 ","End":"09:14.360","Text":"There\u0027s no other tasks assigned and there\u0027s no other values to get. That\u0027s it."},{"Start":"09:14.360 ","End":"09:17.615","Text":"We\u0027ve done a few exercise now, very similar,"},{"Start":"09:17.615 ","End":"09:22.665","Text":"all just trying to get used to the idea of an object and how we might have"},{"Start":"09:22.665 ","End":"09:28.430","Text":"set and get methods and other methods which might not use get and set as prefixes,"},{"Start":"09:28.430 ","End":"09:30.020","Text":"but they do the same thing really,"},{"Start":"09:30.020 ","End":"09:33.560","Text":"just to change the instance variable sometimes with"},{"Start":"09:33.560 ","End":"09:39.410","Text":"some logic behind them or directly in the case of name, we just change the name."},{"Start":"09:39.410 ","End":"09:42.305","Text":"But often there\u0027ll be some logic there as well."},{"Start":"09:42.305 ","End":"09:44.120","Text":"That\u0027s it for these exercises."},{"Start":"09:44.120 ","End":"09:47.870","Text":"I\u0027ll see you in the next topic. Thanks for watching."}],"ID":30286}],"Thumbnail":null,"ID":287464},{"Name":"Classes","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Classes and Objects","Duration":"5m 36s","ChapterTopicVideoID":29485,"CourseChapterTopicPlaylistID":294471,"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.615","Text":"Hello and welcome to this video on classes."},{"Start":"00:03.615 ","End":"00:05.130","Text":"By the end of this section,"},{"Start":"00:05.130 ","End":"00:08.265","Text":"you\u0027ll be able to distinguish between an object and class,"},{"Start":"00:08.265 ","End":"00:11.205","Text":"create constructor methods for a class,"},{"Start":"00:11.205 ","End":"00:15.105","Text":"apply the concept of overloading to constructor methods,"},{"Start":"00:15.105 ","End":"00:20.000","Text":"and apply the static keyword to create class attributes and methods."},{"Start":"00:20.000 ","End":"00:25.430","Text":"Previously, we became familiar with the concepts of encapsulation and information hiding,"},{"Start":"00:25.430 ","End":"00:28.970","Text":"the act of binding up code with data and only allowing"},{"Start":"00:28.970 ","End":"00:33.245","Text":"access to the data via provided methods."},{"Start":"00:33.245 ","End":"00:38.845","Text":"Although we\u0027d been using classes as a mechanism to contain and build our programs,"},{"Start":"00:38.845 ","End":"00:41.300","Text":"we didn\u0027t really explain what a class is nor did"},{"Start":"00:41.300 ","End":"00:45.200","Text":"we formally define how it relates to an object."},{"Start":"00:45.200 ","End":"00:49.025","Text":"Going back to the example of the classification of animals,"},{"Start":"00:49.025 ","End":"00:51.815","Text":"we can see where class gets its name."},{"Start":"00:51.815 ","End":"00:54.650","Text":"When putting an animal into a place in the diagram,"},{"Start":"00:54.650 ","End":"00:58.925","Text":"we\u0027re categorizing or classifying where to place the animal."},{"Start":"00:58.925 ","End":"01:03.210","Text":"Hence, the word class from classify."},{"Start":"01:03.250 ","End":"01:07.295","Text":"A kangaroo is one particular class of animal."},{"Start":"01:07.295 ","End":"01:09.260","Text":"From the kangaroo class,"},{"Start":"01:09.260 ","End":"01:13.300","Text":"we can make objects, individual kangaroos."},{"Start":"01:13.300 ","End":"01:19.200","Text":"We could create 1 kangaroo or 5 or 100,"},{"Start":"01:19.200 ","End":"01:26.015","Text":"all will have their own data values and methods to operate on those data values."},{"Start":"01:26.015 ","End":"01:30.095","Text":"For example, the kangaroo might have a size."},{"Start":"01:30.095 ","End":"01:32.315","Text":"As the kangaroo grows,"},{"Start":"01:32.315 ","End":"01:34.460","Text":"its size would increase."},{"Start":"01:34.460 ","End":"01:37.940","Text":"Each kangaroo\u0027s size would be stored internally for"},{"Start":"01:37.940 ","End":"01:42.835","Text":"that individual and wouldn\u0027t affect any other individual."},{"Start":"01:42.835 ","End":"01:46.014","Text":"We create each individual from the same class,"},{"Start":"01:46.014 ","End":"01:50.945","Text":"but a kangaroo\u0027s can all have slightly different attributes, so can vary."},{"Start":"01:50.945 ","End":"01:52.865","Text":"In general terms then,"},{"Start":"01:52.865 ","End":"01:59.739","Text":"the relationship between a class and an object is that an object is created from a class."},{"Start":"01:59.739 ","End":"02:03.760","Text":"We could define a class as a template used"},{"Start":"02:03.760 ","End":"02:07.735","Text":"to create objects consisting of state and behavior,"},{"Start":"02:07.735 ","End":"02:12.084","Text":"and an object as an instance of a class."},{"Start":"02:12.084 ","End":"02:14.770","Text":"To make sense of these definitions,"},{"Start":"02:14.770 ","End":"02:18.070","Text":"think of 1 kangaroo object in our example,"},{"Start":"02:18.070 ","End":"02:21.905","Text":"which was an instance of the kangaroo class."},{"Start":"02:21.905 ","End":"02:25.085","Text":"One of its attributes was size,"},{"Start":"02:25.085 ","End":"02:27.845","Text":"and another might have been color."},{"Start":"02:27.845 ","End":"02:31.285","Text":"Its behaviors could have been jump,"},{"Start":"02:31.285 ","End":"02:37.020","Text":"sleep, eat, or drink."},{"Start":"02:37.030 ","End":"02:42.140","Text":"We\u0027ve previously used the word instance variables instead of using"},{"Start":"02:42.140 ","End":"02:48.289","Text":"attributes because we\u0027re familiar with the term variable from procedural programming."},{"Start":"02:48.289 ","End":"02:53.000","Text":"We could also have used the word field, property, or state."},{"Start":"02:53.000 ","End":"02:56.495","Text":"You\u0027ll often hear these terms used interchangeably."},{"Start":"02:56.495 ","End":"03:00.170","Text":"Similarly, we might hear the word behavior, or method,"},{"Start":"03:00.170 ","End":"03:04.540","Text":"or function when we\u0027re talking about the organizational units of code."},{"Start":"03:04.540 ","End":"03:09.605","Text":"Which word we use is dependent on the high level language or specific contexts."},{"Start":"03:09.605 ","End":"03:11.884","Text":"For example, when designing classes,"},{"Start":"03:11.884 ","End":"03:14.360","Text":"we tend to talk about behaviors."},{"Start":"03:14.360 ","End":"03:17.480","Text":"In Java, we tend to use the word method,"},{"Start":"03:17.480 ","End":"03:18.980","Text":"and in C++,"},{"Start":"03:18.980 ","End":"03:21.035","Text":"you might say member function."},{"Start":"03:21.035 ","End":"03:26.990","Text":"The process of creating a new object from a class is called instantiation."},{"Start":"03:26.990 ","End":"03:31.235","Text":"We\u0027re creating an instance of the class."},{"Start":"03:31.235 ","End":"03:34.985","Text":"The process is straightforward and simply involves the use"},{"Start":"03:34.985 ","End":"03:38.930","Text":"of the word new to create a new object."},{"Start":"03:38.930 ","End":"03:42.050","Text":"We\u0027re creating an object from the kangaroo class,"},{"Start":"03:42.050 ","End":"03:46.960","Text":"so need to specify that after the new keyword."},{"Start":"03:46.960 ","End":"03:49.130","Text":"We\u0027ll need to store a reference to"},{"Start":"03:49.130 ","End":"03:52.730","Text":"the newly created object so that we can make use of it."},{"Start":"03:52.730 ","End":"03:59.215","Text":"Here we\u0027ve created a variable with the identifier skippy to store the object reference."},{"Start":"03:59.215 ","End":"04:02.570","Text":"In Java and C++, you also need to define the type"},{"Start":"04:02.570 ","End":"04:05.450","Text":"of class when declaring the variable itself hence,"},{"Start":"04:05.450 ","End":"04:09.350","Text":"this Java code has kangaroo before skippy,"},{"Start":"04:09.350 ","End":"04:11.855","Text":"as well as after new."},{"Start":"04:11.855 ","End":"04:16.265","Text":"This makes sense as when declaring any variables in Java or C++,"},{"Start":"04:16.265 ","End":"04:18.935","Text":"we need to specify the datatype."},{"Start":"04:18.935 ","End":"04:22.730","Text":"For primitives, this would be int, char, float, etc."},{"Start":"04:22.730 ","End":"04:26.120","Text":"But for an object, it\u0027s the name of the class."},{"Start":"04:26.120 ","End":"04:31.160","Text":"By convention, we normally name classes with a capital letter at the beginning"},{"Start":"04:31.160 ","End":"04:36.215","Text":"so we can distinguish a class for many objects that were created from that class."},{"Start":"04:36.215 ","End":"04:37.730","Text":"As you can see here,"},{"Start":"04:37.730 ","End":"04:39.890","Text":"we have a capital K for kangaroo,"},{"Start":"04:39.890 ","End":"04:46.750","Text":"but skippy has a lowercase s. In interactive teaching environments such as BlueJ,"},{"Start":"04:46.750 ","End":"04:51.011","Text":"objects can be created from a class in the object workbench."},{"Start":"04:51.011 ","End":"04:54.095","Text":"This invokes the new keyword when a class"},{"Start":"04:54.095 ","End":"04:57.904","Text":"is right-clicked and the new menu option is selected."},{"Start":"04:57.904 ","End":"05:01.805","Text":"Any values needed to create the object can then be provided"},{"Start":"05:01.805 ","End":"05:05.750","Text":"interactively by typing them into the textboxes."},{"Start":"05:05.750 ","End":"05:09.690","Text":"Our kangaroo class didn\u0027t have any values passed to it when created,"},{"Start":"05:09.690 ","End":"05:11.255","Text":"but in this example,"},{"Start":"05:11.255 ","End":"05:13.895","Text":"we\u0027re creating an object from the student class"},{"Start":"05:13.895 ","End":"05:16.970","Text":"and passing in values to the object when it is created to"},{"Start":"05:16.970 ","End":"05:19.310","Text":"set the students first and last"},{"Start":"05:19.310 ","End":"05:23.075","Text":"names and the year in which they are enrolling onto a course."},{"Start":"05:23.075 ","End":"05:24.770","Text":"We\u0027ll pause there now,"},{"Start":"05:24.770 ","End":"05:26.300","Text":"and in the next video,"},{"Start":"05:26.300 ","End":"05:29.210","Text":"we\u0027ll look at a standard form for visually representing"},{"Start":"05:29.210 ","End":"05:32.510","Text":"classes called a UML class diagram."},{"Start":"05:32.510 ","End":"05:36.210","Text":"Thanks very much for watching and see you shortly."}],"ID":31092},{"Watched":false,"Name":"Constructors","Duration":"6m 26s","ChapterTopicVideoID":29484,"CourseChapterTopicPlaylistID":294471,"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.390","Text":"Hello, welcome back. When starting to"},{"Start":"00:03.390 ","End":"00:06.225","Text":"develop a program in an object-oriented programming language,"},{"Start":"00:06.225 ","End":"00:10.350","Text":"one of the first jobs to do is identify what goes into a class."},{"Start":"00:10.350 ","End":"00:15.510","Text":"A standard notation for object-oriented design called Unified Modelling Language,"},{"Start":"00:15.510 ","End":"00:18.630","Text":"or UML, allows us to communicate designs without"},{"Start":"00:18.630 ","End":"00:23.085","Text":"making specific reference to any particular programming language."},{"Start":"00:23.085 ","End":"00:28.035","Text":"The most basic use of UML is to show what a class consists of."},{"Start":"00:28.035 ","End":"00:33.270","Text":"The class diagram consists of 3 boxes within a single rectangle."},{"Start":"00:33.270 ","End":"00:36.075","Text":"The top one contains the name of the class,"},{"Start":"00:36.075 ","End":"00:39.145","Text":"and the middle one contains the attributes."},{"Start":"00:39.145 ","End":"00:44.150","Text":"A symbol indicates whether each individual attribute is public or private."},{"Start":"00:44.150 ","End":"00:47.405","Text":"Minus means private, plus means public."},{"Start":"00:47.405 ","End":"00:50.119","Text":"Here we see the class has 3 attributes,"},{"Start":"00:50.119 ","End":"00:51.365","Text":"2 of which are ints,"},{"Start":"00:51.365 ","End":"00:53.060","Text":"and one of which is a string,"},{"Start":"00:53.060 ","End":"00:56.285","Text":"and all are private, as we\u0027d expect."},{"Start":"00:56.285 ","End":"01:02.030","Text":"The symbols also apply to methods which occupy the bottom part of the UML diagram."},{"Start":"01:02.030 ","End":"01:04.909","Text":"Here we see 4 methods, all public."},{"Start":"01:04.909 ","End":"01:07.280","Text":"The first takes 2 parameters,"},{"Start":"01:07.280 ","End":"01:08.810","Text":"and returns no value."},{"Start":"01:08.810 ","End":"01:11.385","Text":"The second, and third, take no parameters,"},{"Start":"01:11.385 ","End":"01:13.020","Text":"and return no values."},{"Start":"01:13.020 ","End":"01:15.800","Text":"The final one takes no parameters,"},{"Start":"01:15.800 ","End":"01:19.480","Text":"but returns a single value of type Boolean."},{"Start":"01:19.480 ","End":"01:24.815","Text":"1 special method every class has is called a Constructor."},{"Start":"01:24.815 ","End":"01:28.250","Text":"A constructor is a method within a class that\u0027s automatically"},{"Start":"01:28.250 ","End":"01:32.075","Text":"called when an object is instantiated from the class."},{"Start":"01:32.075 ","End":"01:35.380","Text":"An object exist in the memory of the computer system it\u0027s running on,"},{"Start":"01:35.380 ","End":"01:38.630","Text":"so it stands to reason that one thing that needs to happen when the objects"},{"Start":"01:38.630 ","End":"01:42.260","Text":"created is for memory to be reserved for the instance variables,"},{"Start":"01:42.260 ","End":"01:44.870","Text":"and in the other data the object requires to function."},{"Start":"01:44.870 ","End":"01:48.215","Text":"The constructor is responsible for that task,"},{"Start":"01:48.215 ","End":"01:51.500","Text":"and this basic form of the constructor is automatically taken"},{"Start":"01:51.500 ","End":"01:54.925","Text":"care of without a programmer having to write any code."},{"Start":"01:54.925 ","End":"01:57.890","Text":"But a constructor can also be used to initialize"},{"Start":"01:57.890 ","End":"02:02.230","Text":"the object typically by setting attribute values to defaults,"},{"Start":"02:02.230 ","End":"02:04.970","Text":"or to value is being passed in as arguments."},{"Start":"02:04.970 ","End":"02:08.510","Text":"If the programmer doesn\u0027t explicitly create a constructor,"},{"Start":"02:08.510 ","End":"02:10.894","Text":"a default constructor will be executed,"},{"Start":"02:10.894 ","End":"02:14.555","Text":"which simply does the job of reserving memory."},{"Start":"02:14.555 ","End":"02:19.310","Text":"However, it is usual for a programmer to provide their own implementation for"},{"Start":"02:19.310 ","End":"02:22.040","Text":"the constructor to in effect setup any"},{"Start":"02:22.040 ","End":"02:26.225","Text":"object created from the class so it\u0027s ready to use."},{"Start":"02:26.225 ","End":"02:28.575","Text":"In our clock example previously,"},{"Start":"02:28.575 ","End":"02:33.335","Text":"we created a clock and it already had a default time of 12 o\u0027clock."},{"Start":"02:33.335 ","End":"02:37.385","Text":"We then have the option later of running this set method,"},{"Start":"02:37.385 ","End":"02:39.470","Text":"which we could use to set the time,"},{"Start":"02:39.470 ","End":"02:41.780","Text":"to a time we actually wanted."},{"Start":"02:41.780 ","End":"02:46.940","Text":"We could instead have implemented a constructor method which received"},{"Start":"02:46.940 ","End":"02:52.849","Text":"a set of parameters to set the individual values of hours, minutes, and seconds."},{"Start":"02:52.849 ","End":"02:58.190","Text":"Any clock being created from the class could then have its instance variables set on"},{"Start":"02:58.190 ","End":"03:00.320","Text":"the same line as when the clock was"},{"Start":"03:00.320 ","End":"03:04.625","Text":"instantiated rather than having to do 2 separate operations,"},{"Start":"03:04.625 ","End":"03:06.215","Text":"1 to create the clock,"},{"Start":"03:06.215 ","End":"03:08.060","Text":"and another to set the time."},{"Start":"03:08.060 ","End":"03:11.555","Text":"The syntax for the constructor varies according to the language,"},{"Start":"03:11.555 ","End":"03:14.240","Text":"but generally follows these 2 rules."},{"Start":"03:14.240 ","End":"03:18.175","Text":"Firstly, the method has the same name as the class."},{"Start":"03:18.175 ","End":"03:23.195","Text":"Secondly, there\u0027s no return value coming back from the method."},{"Start":"03:23.195 ","End":"03:29.810","Text":"In this example, note how the constructor for the class clock has the name clock."},{"Start":"03:29.810 ","End":"03:35.210","Text":"Notice also there\u0027s no return value datatype between public,"},{"Start":"03:35.210 ","End":"03:37.580","Text":"and clock, as we\u0027d normally expect."},{"Start":"03:37.580 ","End":"03:40.160","Text":"Not even the keyword void."},{"Start":"03:40.160 ","End":"03:44.990","Text":"Constructors can\u0027t return a value because the memory address where the object has been"},{"Start":"03:44.990 ","End":"03:49.970","Text":"stored is automatically returned so that we can reference the object in our code."},{"Start":"03:49.970 ","End":"03:55.210","Text":"Here, our reference to our newly created object is being stored in my clock,"},{"Start":"03:55.210 ","End":"03:58.295","Text":"and we can\u0027t expect a return value to be returned,"},{"Start":"03:58.295 ","End":"04:00.740","Text":"as well as that memory reference."},{"Start":"04:00.740 ","End":"04:02.630","Text":"When creating our objects,"},{"Start":"04:02.630 ","End":"04:07.400","Text":"we can now pass values inside the brackets after the class name."},{"Start":"04:07.400 ","End":"04:09.200","Text":"But when creating an object,"},{"Start":"04:09.200 ","End":"04:11.045","Text":"it\u0027s sometimes not possible to provide"},{"Start":"04:11.045 ","End":"04:15.035","Text":"all the details needed by the object at the time of creation."},{"Start":"04:15.035 ","End":"04:19.580","Text":"For example, a student class could contain an attribute for the student\u0027s name,"},{"Start":"04:19.580 ","End":"04:21.110","Text":"and their year of enrollment,"},{"Start":"04:21.110 ","End":"04:23.690","Text":"which could be set by a constructor."},{"Start":"04:23.690 ","End":"04:25.985","Text":"But some students, we might know"},{"Start":"04:25.985 ","End":"04:32.795","Text":"what their term time addresses is at the time we create the object."},{"Start":"04:32.795 ","End":"04:36.490","Text":"Our constructor could instead take 3 arguments."},{"Start":"04:36.490 ","End":"04:39.380","Text":"Which version of the constructor should we use?"},{"Start":"04:39.380 ","End":"04:40.840","Text":"One with 2 arguments,"},{"Start":"04:40.840 ","End":"04:42.275","Text":"or one with 3?"},{"Start":"04:42.275 ","End":"04:43.670","Text":"If we made it 3,"},{"Start":"04:43.670 ","End":"04:45.620","Text":"we could send an empty string for"},{"Start":"04:45.620 ","End":"04:50.030","Text":"the last argument for students who don\u0027t yet have a term time address."},{"Start":"04:50.030 ","End":"04:53.375","Text":"But it turns out there\u0027s a simpler, and better solution."},{"Start":"04:53.375 ","End":"04:56.390","Text":"We can have more than one constructor."},{"Start":"04:56.390 ","End":"04:59.300","Text":"This process is called overloading,"},{"Start":"04:59.300 ","End":"05:03.740","Text":"and it applies to all types of methods, not just constructors."},{"Start":"05:03.740 ","End":"05:09.770","Text":"Here we appear to have 2 methods inside a class that have the same name Student."},{"Start":"05:09.770 ","End":"05:15.025","Text":"It also happens to be the same as the name of the class so that makes it a constructor."},{"Start":"05:15.025 ","End":"05:17.365","Text":"If we have 2 constructors,"},{"Start":"05:17.365 ","End":"05:20.045","Text":"how does the program know which one to run?"},{"Start":"05:20.045 ","End":"05:23.150","Text":"The answer is, through the arguments."},{"Start":"05:23.150 ","End":"05:26.765","Text":"The compiler, when it encounters the new keyword,"},{"Start":"05:26.765 ","End":"05:30.770","Text":"checks the number and types of each of the arguments,"},{"Start":"05:30.770 ","End":"05:36.530","Text":"and selects the appropriate method that has the same number of types and arguments."},{"Start":"05:36.530 ","End":"05:39.235","Text":"They must be in the same order too."},{"Start":"05:39.235 ","End":"05:41.585","Text":"Here the first line would compile fine,"},{"Start":"05:41.585 ","End":"05:46.460","Text":"because we provided a constructor that takes a string followed by an integer."},{"Start":"05:46.460 ","End":"05:49.550","Text":"But the second line wouldn\u0027t compile because there\u0027s"},{"Start":"05:49.550 ","End":"05:54.110","Text":"no constructor found that takes an int followed by a string."},{"Start":"05:54.110 ","End":"05:57.800","Text":"We can provide as many different implementations of the method as we want,"},{"Start":"05:57.800 ","End":"06:01.685","Text":"so long as they have differing numbers or types of arguments."},{"Start":"06:01.685 ","End":"06:05.354","Text":"Here we see 1 constructor with no arguments,"},{"Start":"06:05.354 ","End":"06:07.605","Text":"the 2nd one with a single string,"},{"Start":"06:07.605 ","End":"06:10.305","Text":"the 3rd with a string and an int,"},{"Start":"06:10.305 ","End":"06:12.675","Text":"the 4th with 2 strings,"},{"Start":"06:12.675 ","End":"06:17.180","Text":"and then the final one with a string and int and a string."},{"Start":"06:17.180 ","End":"06:19.235","Text":"We\u0027ll pause one final time here,"},{"Start":"06:19.235 ","End":"06:20.435","Text":"and in the last video,"},{"Start":"06:20.435 ","End":"06:23.300","Text":"we\u0027ll talk about static attributes and methods."},{"Start":"06:23.300 ","End":"06:26.730","Text":"Thanks again for watching, and see you shortly."}],"ID":31093},{"Watched":false,"Name":"Class Attributes and Methods","Duration":"4m 38s","ChapterTopicVideoID":29483,"CourseChapterTopicPlaylistID":294471,"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.870","Text":"Hello, welcome back. As well as the access modifiers,"},{"Start":"00:03.870 ","End":"00:06.075","Text":"private and public that we\u0027ve come across."},{"Start":"00:06.075 ","End":"00:09.135","Text":"There\u0027s another modifier called static."},{"Start":"00:09.135 ","End":"00:14.760","Text":"The static keyword allows us to turn off the ability for classes to generate"},{"Start":"00:14.760 ","End":"00:20.445","Text":"multiple instances of variables in each individual object being created."},{"Start":"00:20.445 ","End":"00:26.010","Text":"We call that a single kangaroo class is used to create all kangaroo objects."},{"Start":"00:26.010 ","End":"00:29.505","Text":"Each object will have its own data."},{"Start":"00:29.505 ","End":"00:34.230","Text":"If we wanted a single variable to be shared across all objects made from"},{"Start":"00:34.230 ","End":"00:39.170","Text":"that class we can do so by marking it as static."},{"Start":"00:39.170 ","End":"00:41.840","Text":"For example, say we wanted to count the number of"},{"Start":"00:41.840 ","End":"00:44.615","Text":"kangaroos that have been created from the class."},{"Start":"00:44.615 ","End":"00:51.155","Text":"We can do so by adding a static variable to the kangaroo class called kangarooCount."},{"Start":"00:51.155 ","End":"00:53.915","Text":"Only 1 copy of this variable exists,"},{"Start":"00:53.915 ","End":"00:56.825","Text":"no matter how many kangaroos we create."},{"Start":"00:56.825 ","End":"01:01.610","Text":"Anytime we reference the value of this variable from any of the kangaroos,"},{"Start":"01:01.610 ","End":"01:04.040","Text":"we\u0027re talking about the same variable."},{"Start":"01:04.040 ","End":"01:07.700","Text":"It no longer belongs to each individual object."},{"Start":"01:07.700 ","End":"01:10.180","Text":"It belongs to the class."},{"Start":"01:10.180 ","End":"01:13.490","Text":"KangarooCount can therefore be increased each time"},{"Start":"01:13.490 ","End":"01:17.785","Text":"a kangaroo is created by a line in the constructor method."},{"Start":"01:17.785 ","End":"01:22.745","Text":"At any point, we could then find out how many kangaroos there are."},{"Start":"01:22.745 ","End":"01:25.715","Text":"Because this variable belongs to the class,"},{"Start":"01:25.715 ","End":"01:31.100","Text":"we refer to it as being a class variable or a class attribute."},{"Start":"01:31.100 ","End":"01:35.390","Text":"A class attribute is an attribute within a class that\u0027s not"},{"Start":"01:35.390 ","End":"01:39.994","Text":"duplicated for any objects instantiated from the class."},{"Start":"01:39.994 ","End":"01:46.475","Text":"All objects access the single copy of this class attribute through the class itself."},{"Start":"01:46.475 ","End":"01:48.995","Text":"Methods can also be static,"},{"Start":"01:48.995 ","End":"01:53.075","Text":"meaning that they belong to a class rather than an object."},{"Start":"01:53.075 ","End":"01:58.000","Text":"For example, the print line method in Java is a class method."},{"Start":"01:58.000 ","End":"02:03.305","Text":"You don\u0027t have to create an object first in order to print something using print line,"},{"Start":"02:03.305 ","End":"02:07.100","Text":"because it\u0027s a static method and it belongs to the class."},{"Start":"02:07.100 ","End":"02:09.950","Text":"We simply put the class name in front."},{"Start":"02:09.950 ","End":"02:14.196","Text":"Because Java classes come bundled in containers called packages."},{"Start":"02:14.196 ","End":"02:23.810","Text":"Because"},{"Start":"02:23.810 ","End":"02:24.770","Text":"Java classes come"},{"Start":"02:24.770 ","End":"02:27.439","Text":"bundled in containers called packages,"},{"Start":"02:27.439 ","End":"02:31.234","Text":"there\u0027s a reference to the package in the notation as well,"},{"Start":"02:31.234 ","End":"02:33.980","Text":"to say the print line method."},{"Start":"02:33.980 ","End":"02:36.155","Text":"Inside the outer class,"},{"Start":"02:36.155 ","End":"02:39.785","Text":"which is part of this system package."},{"Start":"02:39.785 ","End":"02:45.590","Text":"There\u0027s a special static method in many object oriented languages called Main."},{"Start":"02:45.590 ","End":"02:49.910","Text":"Main\u0027s role is to define the starting point for the program."},{"Start":"02:49.910 ","End":"02:52.490","Text":"Because the program could contain code in"},{"Start":"02:52.490 ","End":"02:58.115","Text":"many different classes and execution needs to start from a single defined point."},{"Start":"02:58.115 ","End":"03:02.675","Text":"The reason it needs to be a static is that in object oriented programs,"},{"Start":"03:02.675 ","End":"03:06.905","Text":"code is contained in object methods and when we\u0027re starting the program,"},{"Start":"03:06.905 ","End":"03:09.425","Text":"we wouldn\u0027t have created any objects yet."},{"Start":"03:09.425 ","End":"03:11.540","Text":"Making a method static,"},{"Start":"03:11.540 ","End":"03:13.640","Text":"which means it belongs to a class and therefore"},{"Start":"03:13.640 ","End":"03:16.115","Text":"doesn\u0027t need an object to be instantiated,"},{"Start":"03:16.115 ","End":"03:18.355","Text":"gets around this problem."},{"Start":"03:18.355 ","End":"03:20.540","Text":"A main function should exist in"},{"Start":"03:20.540 ","End":"03:24.980","Text":"any Java program we want to run outside our development environment."},{"Start":"03:24.980 ","End":"03:28.340","Text":"For example, this main method would be"},{"Start":"03:28.340 ","End":"03:32.210","Text":"the automatic entry point for the program and it can then be"},{"Start":"03:32.210 ","End":"03:36.170","Text":"responsible for kicking off the rest of the program by creating"},{"Start":"03:36.170 ","End":"03:40.925","Text":"a class and then executing a method within that class."},{"Start":"03:40.925 ","End":"03:43.115","Text":"If you don\u0027t provide a main,"},{"Start":"03:43.115 ","End":"03:44.600","Text":"you won\u0027t be able to execute"},{"Start":"03:44.600 ","End":"03:48.260","Text":"your program outside your programming development environment."},{"Start":"03:48.260 ","End":"03:54.890","Text":"Only 1 is needed in any of the classes that make up your program."},{"Start":"03:54.890 ","End":"04:02.000","Text":"Static attributes and methods in UML class diagrams are shown by being underlined."},{"Start":"04:02.000 ","End":"04:04.984","Text":"Here there are no static methods,"},{"Start":"04:04.984 ","End":"04:09.445","Text":"but there\u0027s a single static attribute, kangarooCounts."},{"Start":"04:09.445 ","End":"04:11.900","Text":"That\u0027s it for this topic."},{"Start":"04:11.900 ","End":"04:16.420","Text":"In this section, we learned how to distinguish between an object and a class,"},{"Start":"04:16.420 ","End":"04:19.010","Text":"to create constructor methods for a class,"},{"Start":"04:19.010 ","End":"04:23.240","Text":"to apply the concept of overloading to construct a methods,"},{"Start":"04:23.240 ","End":"04:29.120","Text":"and to apply the static keyword to create class attributes and methods."},{"Start":"04:29.120 ","End":"04:32.072","Text":"Be sure to complete all the exercises now"},{"Start":"04:32.072 ","End":"04:35.465","Text":"to embed everything we\u0027ve learned in these videos."},{"Start":"04:35.465 ","End":"04:39.330","Text":"Thank you very much for watching. I will see you soon."}],"ID":31094},{"Watched":false,"Name":"Exercise 1","Duration":"4m 41s","ChapterTopicVideoID":29469,"CourseChapterTopicPlaylistID":294471,"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.410","Text":"Hello, and welcome to this exercise in which we create"},{"Start":"00:02.410 ","End":"00:05.575","Text":"a new project and within it a class called Student."},{"Start":"00:05.575 ","End":"00:08.560","Text":"The class should have two string attributes first and last,"},{"Start":"00:08.560 ","End":"00:10.915","Text":"and an int attribute year."},{"Start":"00:10.915 ","End":"00:15.055","Text":"We then create a constructor in part b that accepts two string parameters named"},{"Start":"00:15.055 ","End":"00:20.095","Text":"firstname and lastname and one int parameter named enrollmentYear."},{"Start":"00:20.095 ","End":"00:23.860","Text":"It sets the instance values to the values passed in."},{"Start":"00:23.860 ","End":"00:28.225","Text":"We then create a selector method called get details that returns the first name,"},{"Start":"00:28.225 ","End":"00:29.905","Text":"last name, and enrollment year,"},{"Start":"00:29.905 ","End":"00:33.145","Text":"all concatenated together with spaces in-between."},{"Start":"00:33.145 ","End":"00:38.740","Text":"We then test by creating objects and run and get details using as input values,"},{"Start":"00:38.740 ","End":"00:48.800","Text":"Ben Chen and 2022 in part 1 and Anita Gill and 2023 for part 2."},{"Start":"00:48.980 ","End":"00:55.640","Text":"First thing to do is to create the string attributes, first and last."},{"Start":"00:55.640 ","End":"00:57.710","Text":"We\u0027re not setting initial values."},{"Start":"00:57.710 ","End":"00:59.675","Text":"We can do them both on the same line."},{"Start":"00:59.675 ","End":"01:03.620","Text":"Then int for the attribute called year."},{"Start":"01:03.620 ","End":"01:04.910","Text":"Now we can do part b,"},{"Start":"01:04.910 ","End":"01:06.485","Text":"which is to create a constructor."},{"Start":"01:06.485 ","End":"01:13.295","Text":"Remember a constructor has the same name as the class itself."},{"Start":"01:13.295 ","End":"01:17.675","Text":"Then we can put parameters inside these brackets here."},{"Start":"01:17.675 ","End":"01:22.880","Text":"Then the code that goes into the constructor inside these braces."},{"Start":"01:22.880 ","End":"01:25.775","Text":"There is no return value for a constructor."},{"Start":"01:25.775 ","End":"01:32.750","Text":"But we do put a public or private modifier for the constructor method itself."},{"Start":"01:32.750 ","End":"01:36.345","Text":"Clearly we need to be a public method."},{"Start":"01:36.345 ","End":"01:40.820","Text":"Now we put the names of the parameters that we\u0027ve been given and their data types."},{"Start":"01:40.820 ","End":"01:44.735","Text":"The first one is a string called firstname."},{"Start":"01:44.735 ","End":"01:48.145","Text":"Second is also a string called lastname."},{"Start":"01:48.145 ","End":"01:53.945","Text":"The final one is an int called enrollmentYear."},{"Start":"01:53.945 ","End":"01:59.360","Text":"There\u0027s our initial opening line to define the constructor."},{"Start":"01:59.360 ","End":"02:01.100","Text":"Now what we need to do is set"},{"Start":"02:01.100 ","End":"02:05.270","Text":"the instance variables to the values that have been passed in."},{"Start":"02:05.270 ","End":"02:09.440","Text":"First is going to be obviously firstname."},{"Start":"02:09.440 ","End":"02:13.950","Text":"Last is going to be lastname"},{"Start":"02:14.140 ","End":"02:22.190","Text":"and year is going to come from enrollmentYear."},{"Start":"02:23.300 ","End":"02:26.070","Text":"That\u0027s that part done."},{"Start":"02:26.070 ","End":"02:28.580","Text":"Then we\u0027ve been asked to create in Part c,"},{"Start":"02:28.580 ","End":"02:33.510","Text":"a select method called get details that returns firstname,"},{"Start":"02:33.510 ","End":"02:36.330","Text":"lastname and enrollment year concatenated together."},{"Start":"02:36.330 ","End":"02:38.420","Text":"That\u0027s going to be a string,"},{"Start":"02:38.420 ","End":"02:40.835","Text":"and it\u0027s also public."},{"Start":"02:40.835 ","End":"02:43.800","Text":"String is the return value public."},{"Start":"02:43.800 ","End":"02:51.060","Text":"Then the name of the method is getDetails, no parameters needed."},{"Start":"02:51.060 ","End":"02:54.005","Text":"A single line of code is all we need."},{"Start":"02:54.005 ","End":"03:01.550","Text":"We return all the different attributes concatenated with spaces."},{"Start":"03:01.550 ","End":"03:04.955","Text":"This last one, although it\u0027s a Integer,"},{"Start":"03:04.955 ","End":"03:10.265","Text":"Java knows that we\u0027re concatenating strings together here and we\u0027re returning a string."},{"Start":"03:10.265 ","End":"03:15.570","Text":"Will do the conversion of this integer into a string for us."},{"Start":"03:15.880 ","End":"03:18.015","Text":"That\u0027s the code done."},{"Start":"03:18.015 ","End":"03:24.220","Text":"Let\u0027s do part D to test what we\u0027ve got to put the test data in."},{"Start":"03:26.240 ","End":"03:29.860","Text":"There\u0027s the first set of data."},{"Start":"03:30.920 ","End":"03:33.715","Text":"If we want to get details on it,"},{"Start":"03:33.715 ","End":"03:37.400","Text":"we should get all those fields back and we do."},{"Start":"03:37.410 ","End":"03:42.260","Text":"Let\u0027s do the same thing again for the other student."},{"Start":"03:44.180 ","End":"03:49.495","Text":"Presume we get different details back for that student\u0027s same method,"},{"Start":"03:49.495 ","End":"03:51.700","Text":"but running it on a different object."},{"Start":"03:51.700 ","End":"03:55.395","Text":"Therefore, we get the attribute values from that object."},{"Start":"03:55.395 ","End":"03:57.330","Text":"They were different values."},{"Start":"03:57.330 ","End":"04:02.530","Text":"We can see those if we inspect by double-clicking in each of the objects,"},{"Start":"04:02.530 ","End":"04:05.795","Text":"but we created them from the same class."},{"Start":"04:05.795 ","End":"04:10.450","Text":"The method returned the values of the instance variables,"},{"Start":"04:10.450 ","End":"04:15.180","Text":"which got their values from the constructor."},{"Start":"04:15.180 ","End":"04:16.775","Text":"When we created the object,"},{"Start":"04:16.775 ","End":"04:21.035","Text":"values were passed into these parameters and we stored those away."},{"Start":"04:21.035 ","End":"04:25.610","Text":"That\u0027s a nice straightforward starting one on this topic."},{"Start":"04:25.610 ","End":"04:30.290","Text":"The next one, we\u0027ll show you some slight variation"},{"Start":"04:30.290 ","End":"04:35.300","Text":"on constructor in terms of the very carefully chose parameter names here,"},{"Start":"04:35.300 ","End":"04:40.470","Text":"we will be not so careful on the next exercise. See you shortly for that one."}],"ID":31095},{"Watched":false,"Name":"Exercise 2","Duration":"8m 26s","ChapterTopicVideoID":29468,"CourseChapterTopicPlaylistID":294471,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.140 ","End":"00:03.240","Text":"Hello welcome back. In this exercise,"},{"Start":"00:03.240 ","End":"00:05.700","Text":"we\u0027ve been asked to create a new class called car."},{"Start":"00:05.700 ","End":"00:08.505","Text":"Within that class, we should have 3 string attributes,"},{"Start":"00:08.505 ","End":"00:11.490","Text":"manufacturer, model, and registration."},{"Start":"00:11.490 ","End":"00:13.860","Text":"In Part B, we\u0027re asked to create a constructor that"},{"Start":"00:13.860 ","End":"00:16.170","Text":"accepts parameters for the car\u0027s manufacturer model,"},{"Start":"00:16.170 ","End":"00:20.055","Text":"and registration, and sets the instance variables to those values."},{"Start":"00:20.055 ","End":"00:25.170","Text":"In Part C, we create an alternative constructor that accepts only 2 parameters for"},{"Start":"00:25.170 ","End":"00:28.260","Text":"manufacturer model and sets the instance variables to"},{"Start":"00:28.260 ","End":"00:31.770","Text":"those values and sets registration to unknown."},{"Start":"00:31.770 ","End":"00:35.580","Text":"In Part D, we create another constructor that accepts no parameters at"},{"Start":"00:35.580 ","End":"00:39.580","Text":"all and sets all 3 attributes in your object to unknown."},{"Start":"00:39.580 ","End":"00:42.950","Text":"We then test by creating 3 objects in Part 1,"},{"Start":"00:42.950 ","End":"00:45.065","Text":"we use the arguments Ford,"},{"Start":"00:45.065 ","End":"00:50.540","Text":"Mustang and MUS T4NG and in Part 2,"},{"Start":"00:50.540 ","End":"00:54.780","Text":"we use the arguments Neo and Futuro."},{"Start":"00:54.780 ","End":"00:58.340","Text":"In Part 3, we give no arguments at all."},{"Start":"00:58.340 ","End":"01:00.245","Text":"Then in Part 4,"},{"Start":"01:00.245 ","End":"01:02.540","Text":"we double-click on each object to confirm"},{"Start":"01:02.540 ","End":"01:06.095","Text":"the contents of the attributes are what we would expect."},{"Start":"01:06.095 ","End":"01:09.875","Text":"First thing to do is to create the attributes,"},{"Start":"01:09.875 ","End":"01:11.675","Text":"all 3 as strings."},{"Start":"01:11.675 ","End":"01:15.327","Text":"First one\u0027s called manufacturer,"},{"Start":"01:15.327 ","End":"01:17.090","Text":"second one model."},{"Start":"01:17.090 ","End":"01:20.880","Text":"Final one, registration."},{"Start":"01:22.820 ","End":"01:29.210","Text":"The innovation in this exercise is we\u0027re going to create multiple constructors,"},{"Start":"01:29.210 ","End":"01:33.575","Text":"but we\u0027re also going to use the same parameter names and"},{"Start":"01:33.575 ","End":"01:38.475","Text":"instance variable names just to see how the syntax for that work."},{"Start":"01:38.475 ","End":"01:40.730","Text":"The first one, first constructor,"},{"Start":"01:40.730 ","End":"01:44.750","Text":"remember constructors always have the same name as the class."},{"Start":"01:44.750 ","End":"01:47.765","Text":"We might unpublic and there\u0027s no return value,"},{"Start":"01:47.765 ","End":"01:50.030","Text":"then any parameters that we need."},{"Start":"01:50.030 ","End":"01:53.180","Text":"We can have multiple different versions of"},{"Start":"01:53.180 ","End":"01:57.990","Text":"a constructor with a different number of parameters,"},{"Start":"01:59.930 ","End":"02:07.085","Text":"but maybe different types of parameters as well with the same number of parameters."},{"Start":"02:07.085 ","End":"02:10.160","Text":"For example, 3 strings or 1 int,"},{"Start":"02:10.160 ","End":"02:12.595","Text":"2 strings would be different constructors."},{"Start":"02:12.595 ","End":"02:15.440","Text":"In this one, we\u0027ve been asked to create."},{"Start":"02:15.440 ","End":"02:19.220","Text":"First of all, a constructor that accepts parameters for manufacturer model"},{"Start":"02:19.220 ","End":"02:23.285","Text":"and registration and sets the instance variables to all those values."},{"Start":"02:23.285 ","End":"02:28.240","Text":"I can choose any parameter name I like."},{"Start":"02:28.240 ","End":"02:30.735","Text":"I haven\u0027t been told what to use."},{"Start":"02:30.735 ","End":"02:38.105","Text":"I\u0027m going to use exactly the same parameter names as I have instance variable names."},{"Start":"02:38.105 ","End":"02:40.730","Text":"Which leads to an interesting question,"},{"Start":"02:40.730 ","End":"02:43.580","Text":"which is how does it know which one I\u0027m referring to?"},{"Start":"02:43.580 ","End":"02:50.060","Text":"The object variables or the parameters because they\u0027ve got the same names,"},{"Start":"02:50.060 ","End":"02:51.890","Text":"manufacturer, manufacturer and model,"},{"Start":"02:51.890 ","End":"02:54.620","Text":"model, registration and registration."},{"Start":"02:54.620 ","End":"02:57.725","Text":"The answer is the handy keyword this."},{"Start":"02:57.725 ","End":"02:59.000","Text":"When we\u0027re referring to this,"},{"Start":"02:59.000 ","End":"03:05.195","Text":"we mean this object and its methods and its instance variables."},{"Start":"03:05.195 ","End":"03:09.090","Text":"When I say this dot manufacturer,"},{"Start":"03:09.160 ","End":"03:16.780","Text":"I\u0027m saying this instance variable for this object."},{"Start":"03:16.780 ","End":"03:22.760","Text":"I can have therefore the same name for the parameter here, manufacturer."},{"Start":"03:22.760 ","End":"03:25.490","Text":"If I had left this off,"},{"Start":"03:25.490 ","End":"03:29.600","Text":"I would just get null because manufacturer doesn\u0027t have a value."},{"Start":"03:29.600 ","End":"03:34.445","Text":"I\u0027m just saying, set the value of this to what it already is,"},{"Start":"03:34.445 ","End":"03:37.295","Text":"which is going to be null. I don\u0027t want that."},{"Start":"03:37.295 ","End":"03:42.215","Text":"I want to say this dot manufacturer is referring to the instance variable."},{"Start":"03:42.215 ","End":"03:46.520","Text":"Manufacturer here is referring to the parameter."},{"Start":"03:46.520 ","End":"03:50.514","Text":"A very subtle but very important difference."},{"Start":"03:50.514 ","End":"03:54.960","Text":"The same thing I\u0027m going to do here for model."},{"Start":"03:54.960 ","End":"04:01.615","Text":"Of course I could choose a different name for a parameter if I wanted to,"},{"Start":"04:01.615 ","End":"04:05.920","Text":"just as we did in the previous exercise."},{"Start":"04:05.920 ","End":"04:10.810","Text":"But why bother when there\u0027s usually an obvious name, for instance,"},{"Start":"04:10.810 ","End":"04:15.220","Text":"variable, it\u0027s going to be the same obvious name for its parameter surely."},{"Start":"04:15.220 ","End":"04:18.580","Text":"I think it does make sense to use,"},{"Start":"04:18.580 ","End":"04:20.880","Text":"make use of the, this keyword where you need to."},{"Start":"04:20.880 ","End":"04:24.160","Text":"Sensible parameter names are going to probably you\u0027re going to be the same as"},{"Start":"04:24.160 ","End":"04:29.065","Text":"the sensible instance variable names that we thought up with in the first place."},{"Start":"04:29.065 ","End":"04:31.720","Text":"There\u0027s our first constructor done."},{"Start":"04:31.720 ","End":"04:35.440","Text":"We can do another one which is exactly the same,"},{"Start":"04:35.440 ","End":"04:40.785","Text":"but has a different number of parameters."},{"Start":"04:40.785 ","End":"04:44.690","Text":"Let us just copy that line and remove that one we don\u0027t want."},{"Start":"04:44.690 ","End":"04:48.650","Text":"We\u0027ve been told that we want another constructor."},{"Start":"04:48.650 ","End":"04:50.915","Text":"In Part C,"},{"Start":"04:50.915 ","End":"04:54.380","Text":"only accepts 2 parameters for manufacturing model and"},{"Start":"04:54.380 ","End":"04:58.145","Text":"sets the instance variable to those values and then registration to unknown."},{"Start":"04:58.145 ","End":"05:02.945","Text":"That\u0027s easy enough. We just get rid of this third parameter here."},{"Start":"05:02.945 ","End":"05:04.970","Text":"We still got this constructor here,"},{"Start":"05:04.970 ","End":"05:07.099","Text":"notice, but now, there\u0027s 2 constructors."},{"Start":"05:07.099 ","End":"05:10.445","Text":"One has 3 parameters, one has 2."},{"Start":"05:10.445 ","End":"05:12.725","Text":"Really all we need to do now,"},{"Start":"05:12.725 ","End":"05:14.465","Text":"I can copy this as well,"},{"Start":"05:14.465 ","End":"05:17.735","Text":"is I can set these values here,"},{"Start":"05:17.735 ","End":"05:23.170","Text":"except I can\u0027t set registration to a parameter because I haven\u0027t got one."},{"Start":"05:23.170 ","End":"05:25.825","Text":"I\u0027m going to set it to unknown."},{"Start":"05:25.825 ","End":"05:29.390","Text":"I can actually leave this off here if I wanted to,"},{"Start":"05:29.390 ","End":"05:32.090","Text":"because there\u0027s obviously no clash now between"},{"Start":"05:32.090 ","End":"05:37.250","Text":"a parameter value and the name of the instance variable."},{"Start":"05:37.250 ","End":"05:40.565","Text":"All that seems to compile as well."},{"Start":"05:40.565 ","End":"05:44.870","Text":"Let\u0027s do the final one and that accepts no parameters at all."},{"Start":"05:44.870 ","End":"05:46.640","Text":"Sometimes, I\u0027ll just type that out."},{"Start":"05:46.640 ","End":"05:49.970","Text":"Public car, no parameters."},{"Start":"05:49.970 ","End":"05:59.530","Text":"What I need to do is set the instance variables to unknown, all 3 of them."},{"Start":"06:00.530 ","End":"06:04.255","Text":"There\u0027s my 3 constructors,"},{"Start":"06:04.255 ","End":"06:06.710","Text":"as I\u0027ve been asked to do, but,"},{"Start":"06:06.710 ","End":"06:10.850","Text":"and then missed my equals sign and then what I need to do now is"},{"Start":"06:10.850 ","End":"06:17.045","Text":"to test by creating 3 objects from this car class."},{"Start":"06:17.045 ","End":"06:21.155","Text":"The first one, I\u0027ve been asked to use the one"},{"Start":"06:21.155 ","End":"06:26.510","Text":"that has 3 arguments. I chose the wrong one there."},{"Start":"06:26.510 ","End":"06:28.940","Text":"One that has 3 arguments is this one."},{"Start":"06:28.940 ","End":"06:30.050","Text":"This one has no arguments,"},{"Start":"06:30.050 ","End":"06:31.580","Text":"this one has 2 arguments."},{"Start":"06:31.580 ","End":"06:34.745","Text":"Choose the one that uses 3 arguments."},{"Start":"06:34.745 ","End":"06:40.950","Text":"I\u0027m going to put in Ford and the registration."},{"Start":"06:40.950 ","End":"06:45.140","Text":"It seems to have happily accepted that created the object."},{"Start":"06:45.140 ","End":"06:47.495","Text":"Let\u0027s do the second one."},{"Start":"06:47.495 ","End":"06:49.945","Text":"This one uses 2."},{"Start":"06:49.945 ","End":"06:57.110","Text":"The model is also a manufacturer is Neo,"},{"Start":"06:57.110 ","End":"07:00.170","Text":"the model number is Futura."},{"Start":"07:00.170 ","End":"07:05.990","Text":"Then let\u0027s do the final one where we set nothing and just creates the object."},{"Start":"07:05.990 ","End":"07:08.480","Text":"There\u0027s no parameters to put in there at all."},{"Start":"07:08.480 ","End":"07:09.710","Text":"Now, if we look inside these,"},{"Start":"07:09.710 ","End":"07:14.570","Text":"here\u0027s the one that we created with 3 arguments passed in."},{"Start":"07:14.570 ","End":"07:17.785","Text":"It\u0027s successfully written all the arguments in."},{"Start":"07:17.785 ","End":"07:19.595","Text":"Then the second one,"},{"Start":"07:19.595 ","End":"07:23.510","Text":"whereas it\u0027s written 2 in and then the final one we didn\u0027t have an argument for,"},{"Start":"07:23.510 ","End":"07:27.440","Text":"but it\u0027s also written a value for that of unknown as we expected."},{"Start":"07:27.440 ","End":"07:31.545","Text":"This final one should have unknown written tool 3."},{"Start":"07:31.545 ","End":"07:35.200","Text":"If we had done nothing in that final one,"},{"Start":"07:35.200 ","End":"07:37.165","Text":"which we can do,"},{"Start":"07:37.165 ","End":"07:39.895","Text":"the values would not be unknown,"},{"Start":"07:39.895 ","End":"07:41.090","Text":"they would just be null."},{"Start":"07:41.090 ","End":"07:42.820","Text":"Let\u0027s just prove that."},{"Start":"07:42.820 ","End":"07:45.310","Text":"Let\u0027s create a new car again,"},{"Start":"07:45.310 ","End":"07:48.050","Text":"no parameters, and open it up."},{"Start":"07:48.050 ","End":"07:55.620","Text":"Sure enough, we do have null set to all of the values for those 3 fields."},{"Start":"07:55.620 ","End":"07:59.125","Text":"That was a fairly straightforward exercise again,"},{"Start":"07:59.125 ","End":"08:03.880","Text":"this time all we were doing was creating multiple constructors,"},{"Start":"08:03.880 ","End":"08:06.220","Text":"but we\u0027re also making use of the this keyword."},{"Start":"08:06.220 ","End":"08:08.860","Text":"We could distinguish between the instance variables of"},{"Start":"08:08.860 ","End":"08:12.560","Text":"this object and the parameters that were being passed in,"},{"Start":"08:12.560 ","End":"08:14.960","Text":"which happened to have the same names."},{"Start":"08:14.960 ","End":"08:17.690","Text":"Because it\u0027s convenient quite often to use the same name."},{"Start":"08:17.690 ","End":"08:19.315","Text":"Rather than think of another one,"},{"Start":"08:19.315 ","End":"08:23.630","Text":"and we can distinguish between the 2 things by using this keyword."},{"Start":"08:23.630 ","End":"08:26.700","Text":"That\u0027s it for this one. Thanks very much for watching."}],"ID":31096},{"Watched":false,"Name":"Exercise 3","Duration":"6m 13s","ChapterTopicVideoID":29467,"CourseChapterTopicPlaylistID":294471,"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.160","Text":"Hello again. In this exercise,"},{"Start":"00:02.160 ","End":"00:04.035","Text":"we create a new class called Customer."},{"Start":"00:04.035 ","End":"00:06.015","Text":"In Part A, we create 3 attributes,"},{"Start":"00:06.015 ","End":"00:07.260","Text":"a string called fullName,"},{"Start":"00:07.260 ","End":"00:08.790","Text":"a boolean called hasLogin,"},{"Start":"00:08.790 ","End":"00:11.670","Text":"and an int called noOfPurchases."},{"Start":"00:11.670 ","End":"00:14.760","Text":"In Part B, we create 3 different constructors."},{"Start":"00:14.760 ","End":"00:18.638","Text":"The first one takes 1 argument and the parameter should be called fullName,"},{"Start":"00:18.638 ","End":"00:22.320","Text":"it should use this to set a fullName attribute to whatever has been passed in"},{"Start":"00:22.320 ","End":"00:26.745","Text":"and set hasLogin to false and noOfPurchases to 0."},{"Start":"00:26.745 ","End":"00:29.270","Text":"The second constructor takes 2 arguments"},{"Start":"00:29.270 ","End":"00:31.865","Text":"and the parameters should be called fullName and hasLogin."},{"Start":"00:31.865 ","End":"00:34.580","Text":"It should set the fullName and hasLogin attributes to the values"},{"Start":"00:34.580 ","End":"00:37.835","Text":"passed in and noOfPurchases to 0."},{"Start":"00:37.835 ","End":"00:40.970","Text":"The final constructor takes 3 arguments and"},{"Start":"00:40.970 ","End":"00:44.960","Text":"the parameters should be called fullName and hasLogin and noOfPurchases."},{"Start":"00:44.960 ","End":"00:48.005","Text":"It should set all the instance variables to the values passed"},{"Start":"00:48.005 ","End":"00:52.430","Text":"in and then we test by creating 3 objects from the class as follows."},{"Start":"00:52.430 ","End":"00:54.200","Text":"The first one with 1 argument,"},{"Start":"00:54.200 ","End":"00:56.660","Text":"Bill Booker, the second with 2 arguments,"},{"Start":"00:56.660 ","End":"00:58.220","Text":"Bob Baker and true,"},{"Start":"00:58.220 ","End":"00:59.990","Text":"the final one with 3 arguments,"},{"Start":"00:59.990 ","End":"01:02.780","Text":"Beryl Brown, true, and 3."},{"Start":"01:02.780 ","End":"01:05.150","Text":"We then double-click each object to see if"},{"Start":"01:05.150 ","End":"01:09.710","Text":"the constructors have correctly set the attributes the values we expect."},{"Start":"01:09.710 ","End":"01:14.600","Text":"First things first, to create the 3 attributes."},{"Start":"01:14.600 ","End":"01:18.090","Text":"One is a string called fullName,"},{"Start":"01:18.110 ","End":"01:23.070","Text":"second is a boolean called hasLogin,"},{"Start":"01:23.070 ","End":"01:29.655","Text":"and final one is an int called noOfPurchases."},{"Start":"01:29.655 ","End":"01:33.345","Text":"Then I\u0027m going to create 3 different constructors b1."},{"Start":"01:33.345 ","End":"01:35.684","Text":"The first one takes 1 argument."},{"Start":"01:35.684 ","End":"01:39.630","Text":"Constructor name obviously will be customer."},{"Start":"01:39.630 ","End":"01:45.050","Text":"It needs to have the public modifier in front and then in the brackets,"},{"Start":"01:45.050 ","End":"01:49.520","Text":"we have whatever it is that the parameters are going to be."},{"Start":"01:49.520 ","End":"01:51.500","Text":"I\u0027ll just create 3 copies now,"},{"Start":"01:51.500 ","End":"01:53.525","Text":"save myself some typing."},{"Start":"01:53.525 ","End":"01:55.090","Text":"The first one there,"},{"Start":"01:55.090 ","End":"01:59.464","Text":"it takes 1 argument and the parameter is going to be called fullName,"},{"Start":"01:59.464 ","End":"02:03.800","Text":"so that\u0027s going to be a string because fullName itself is a string."},{"Start":"02:03.800 ","End":"02:07.260","Text":"Let\u0027s keep exactly the same capitalization."},{"Start":"02:07.260 ","End":"02:12.105","Text":"That should have been a N, but I\u0027ve not done that so let\u0027s just leave it as it is."},{"Start":"02:12.105 ","End":"02:19.415","Text":"I need to set fullName my instance variable to whatever has been passed in,"},{"Start":"02:19.415 ","End":"02:22.715","Text":"but this will cause me a problem."},{"Start":"02:22.715 ","End":"02:31.970","Text":"I can\u0027t do that. I need to put this in front to distinguish between the parameter and"},{"Start":"02:31.970 ","End":"02:37.400","Text":"the instance variable and then the other 2 attributes I set"},{"Start":"02:37.400 ","End":"02:43.800","Text":"to false in the case of hasLogin and noOfPurchases to 0."},{"Start":"02:43.800 ","End":"02:45.485","Text":"That\u0027s great."},{"Start":"02:45.485 ","End":"02:49.505","Text":"I\u0027ll copy these because presumably I\u0027ll need them down here at some point."},{"Start":"02:49.505 ","End":"02:53.310","Text":"The Part 2, 2 arguments."},{"Start":"02:53.310 ","End":"02:56.205","Text":"Parameters, fullName, and hasLogin,"},{"Start":"02:56.205 ","End":"03:00.520","Text":"so fullName, and hasLogin."},{"Start":"03:00.520 ","End":"03:07.520","Text":"I need to make sure that I put the datatype in front of those so fullName is a string,"},{"Start":"03:07.520 ","End":"03:10.700","Text":"hasLogin is a boolean,"},{"Start":"03:10.700 ","End":"03:15.205","Text":"and fullName has its value,"},{"Start":"03:15.205 ","End":"03:19.740","Text":"hasLogin has its value,"},{"Start":"03:19.740 ","End":"03:25.160","Text":"and noOfPurchases is going to be set to 0 as we did in"},{"Start":"03:25.160 ","End":"03:31.215","Text":"the previous one and then the final one in Part B3, 3 arguments."},{"Start":"03:31.215 ","End":"03:37.895","Text":"Let\u0027s copy this here so it\u0027d be a bit of typing into the brackets down here and then"},{"Start":"03:37.895 ","End":"03:45.220","Text":"the final argument is the int noOfPurchases."},{"Start":"03:45.220 ","End":"03:48.755","Text":"Again, I can copy these lines here."},{"Start":"03:48.755 ","End":"03:56.775","Text":"The only one I need to change there is the last one so that I have"},{"Start":"03:56.775 ","End":"04:01.410","Text":"the parameter being passed and stored"},{"Start":"04:01.410 ","End":"04:06.650","Text":"into the instance variable and that\u0027s why I need to use this keyword,"},{"Start":"04:06.650 ","End":"04:11.600","Text":"because the parameter has the same name as the instance variable and it won\u0027t"},{"Start":"04:11.600 ","End":"04:14.060","Text":"know which one I\u0027m talking about so assume I\u0027m talking about"},{"Start":"04:14.060 ","End":"04:17.360","Text":"the instance variable and I won\u0027t have the correct value in it."},{"Start":"04:17.360 ","End":"04:20.045","Text":"That looks good."},{"Start":"04:20.045 ","End":"04:25.550","Text":"I\u0027m missing s here and therefore it\u0027s complaining."},{"Start":"04:25.550 ","End":"04:29.220","Text":"Let\u0027s try and create our 3 objects."},{"Start":"04:29.810 ","End":"04:33.345","Text":"The first one, Bill Booker,"},{"Start":"04:33.345 ","End":"04:37.865","Text":"1 argument is going to be just this one here with just fullName,"},{"Start":"04:37.865 ","End":"04:44.900","Text":"so let\u0027s put the value in and see what is"},{"Start":"04:44.900 ","End":"04:52.025","Text":"stored in there and we get false correctly set to hasLogin and noOfPurchases to 0."},{"Start":"04:52.025 ","End":"04:54.035","Text":"That\u0027s what we expect."},{"Start":"04:54.035 ","End":"04:58.055","Text":"Let\u0027s do the test in C2,"},{"Start":"04:58.055 ","End":"05:02.865","Text":"which is 2 parameters or 2 arguments this time."},{"Start":"05:02.865 ","End":"05:05.120","Text":"In this case,"},{"Start":"05:05.120 ","End":"05:07.399","Text":"we\u0027ve got Bob Baker set as fullName,"},{"Start":"05:07.399 ","End":"05:11.675","Text":"true as the value we passed in and noOfPurchases was set to"},{"Start":"05:11.675 ","End":"05:16.325","Text":"0 because the constructor did that for us and let\u0027s do the final one."},{"Start":"05:16.325 ","End":"05:21.330","Text":"In this case, we pass in 3 arguments."},{"Start":"05:21.330 ","End":"05:24.045","Text":"We need to choose that constructor,"},{"Start":"05:24.045 ","End":"05:31.755","Text":"Beryl Brown, and has a login and has made 3 purchases."},{"Start":"05:31.755 ","End":"05:34.685","Text":"If we look inside this object,"},{"Start":"05:34.685 ","End":"05:37.760","Text":"we see that the attributes have all been set correctly."},{"Start":"05:37.760 ","End":"05:41.071","Text":"Just exactly the same as the previous exercise, really,"},{"Start":"05:41.071 ","End":"05:44.615","Text":"just a few more options in this one and one of these,"},{"Start":"05:44.615 ","End":"05:48.210","Text":"we got basically different datatypes."},{"Start":"05:48.210 ","End":"05:51.180","Text":"We\u0027ve got a string and a boolean, and an int,"},{"Start":"05:51.180 ","End":"05:52.930","Text":"or just a string and a boolean,"},{"Start":"05:52.930 ","End":"05:54.740","Text":"or just a string."},{"Start":"05:54.740 ","End":"05:59.510","Text":"None of our constructors have got no values because that wouldn\u0027t really make sense."},{"Start":"05:59.510 ","End":"06:03.480","Text":"We at least need to give the customer a name."},{"Start":"06:03.480 ","End":"06:06.050","Text":"That\u0027s the only slight change on the previous exercise,"},{"Start":"06:06.050 ","End":"06:10.400","Text":"pretty straightforward and hopefully getting the idea of constructors now."},{"Start":"06:10.400 ","End":"06:13.380","Text":"Thanks very much for watching. See you in the next one."}],"ID":31097},{"Watched":false,"Name":"Exercise 4","Duration":"9m 25s","ChapterTopicVideoID":29466,"CourseChapterTopicPlaylistID":294471,"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.280","Text":"Hello, welcome to this exercise in which we\u0027ve been"},{"Start":"00:02.280 ","End":"00:04.800","Text":"asked to create a new class called WageSlip,"},{"Start":"00:04.800 ","End":"00:07.695","Text":"based on the class diagram that\u0027s been given."},{"Start":"00:07.695 ","End":"00:11.400","Text":"In part a, one of the constructors should accept a name,"},{"Start":"00:11.400 ","End":"00:13.575","Text":"hours worked, and an hourly rate."},{"Start":"00:13.575 ","End":"00:18.510","Text":"It should set tax rate to 0.2 and the other attributes to the values passed in."},{"Start":"00:18.510 ","End":"00:23.580","Text":"The other constructor takes an extra parameter used to set the value of tax rate."},{"Start":"00:23.580 ","End":"00:27.270","Text":"All 4 values passed in is stored into the relevant attribute."},{"Start":"00:27.270 ","End":"00:29.970","Text":"In part b, we implement get gross pay."},{"Start":"00:29.970 ","End":"00:34.425","Text":"It returns the hourly rate multiplied by the number of hours worked."},{"Start":"00:34.425 ","End":"00:37.470","Text":"In part c, we implement getNetPay."},{"Start":"00:37.470 ","End":"00:42.965","Text":"It returns the gross pay with tax deducted according to the set tax rate."},{"Start":"00:42.965 ","End":"00:46.280","Text":"For example, for a gross pay figure of 1,000,"},{"Start":"00:46.280 ","End":"00:49.370","Text":"a default tax rate of 0.2 would deduct"},{"Start":"00:49.370 ","End":"00:54.230","Text":"20 per cent from the gross pay resulting in a net pay of 800."},{"Start":"00:54.230 ","End":"00:58.700","Text":"In part d, we test by creating 4 different objects and run"},{"Start":"00:58.700 ","End":"01:04.040","Text":"getNetPay and get gross pay on each with the following values in Part 1,"},{"Start":"01:04.040 ","End":"01:07.110","Text":"Elly Ash, 25, and 15,"},{"Start":"01:07.110 ","End":"01:10.980","Text":"Part 2, Karen Lopez, 35,"},{"Start":"01:10.980 ","End":"01:14.220","Text":"and 15, Part 3, Ben Green,"},{"Start":"01:14.220 ","End":"01:18.270","Text":"35, 25, and 0.4."},{"Start":"01:18.270 ","End":"01:20.670","Text":"In Part 4 Ken Li,"},{"Start":"01:20.670 ","End":"01:25.390","Text":"35, 25, and 0.45."},{"Start":"01:25.550 ","End":"01:29.195","Text":"We\u0027ve been presented with a class diagram this time,"},{"Start":"01:29.195 ","End":"01:33.140","Text":"and we\u0027re going to create the class based on this diagram."},{"Start":"01:33.140 ","End":"01:37.650","Text":"It\u0027s called WageSlip and has 4 attributes."},{"Start":"01:37.650 ","End":"01:40.430","Text":"Attributes are going to be private,"},{"Start":"01:40.430 ","End":"01:43.115","Text":"that\u0027s what the minus sign means."},{"Start":"01:43.115 ","End":"01:46.850","Text":"We have a string called name,"},{"Start":"01:46.850 ","End":"01:51.375","Text":"then we have an int called hours,"},{"Start":"01:51.375 ","End":"01:54.835","Text":"a double called hourly rate."},{"Start":"01:54.835 ","End":"01:58.590","Text":"Once again, double is overkill for this,"},{"Start":"01:58.590 ","End":"02:03.470","Text":"but it saves us a bit of typing and so on and so for this demo,"},{"Start":"02:03.470 ","End":"02:04.550","Text":"it\u0027s not a problem."},{"Start":"02:04.550 ","End":"02:09.200","Text":"We also have another double called tax rate."},{"Start":"02:09.200 ","End":"02:12.506","Text":"Again, probably should be a float rather than a double,"},{"Start":"02:12.506 ","End":"02:15.335","Text":"but just to make it easy for ourselves, we\u0027re going to call it a double."},{"Start":"02:15.335 ","End":"02:17.282","Text":"There\u0027s our attributes."},{"Start":"02:17.282 ","End":"02:19.925","Text":"What we need now is constructor."},{"Start":"02:19.925 ","End":"02:21.895","Text":"In fact, there\u0027s 2 constructors."},{"Start":"02:21.895 ","End":"02:24.575","Text":"In part a, it tells us a little bit more."},{"Start":"02:24.575 ","End":"02:26.810","Text":"One of the constructors accepts a name,"},{"Start":"02:26.810 ","End":"02:28.520","Text":"hours worked and an hourly rate,"},{"Start":"02:28.520 ","End":"02:30.410","Text":"and then we have a default tax rate."},{"Start":"02:30.410 ","End":"02:32.720","Text":"The other one takes the tax rate."},{"Start":"02:32.720 ","End":"02:34.985","Text":"There are 4 parameters for that one."},{"Start":"02:34.985 ","End":"02:40.085","Text":"Let\u0027s start with the one with just 3 parameters."},{"Start":"02:40.085 ","End":"02:42.950","Text":"There\u0027s no return value because it\u0027s a constructor."},{"Start":"02:42.950 ","End":"02:45.680","Text":"It\u0027s called WageSlip."},{"Start":"02:45.950 ","End":"02:48.680","Text":"The first parameter is a string."},{"Start":"02:48.680 ","End":"02:50.840","Text":"We can give it a name because it\u0027s going to take"},{"Start":"02:50.840 ","End":"02:53.255","Text":"in the name of the person on the wage slip."},{"Start":"02:53.255 ","End":"02:56.930","Text":"Let\u0027s call it name. The next one is an int,"},{"Start":"02:56.930 ","End":"02:59.495","Text":"which we presume is going to be the hours."},{"Start":"02:59.495 ","End":"03:02.495","Text":"The next one is the double,"},{"Start":"03:02.495 ","End":"03:05.480","Text":"which is going to be the hourly rate."},{"Start":"03:05.480 ","End":"03:07.840","Text":"For this particular constructor,"},{"Start":"03:07.840 ","End":"03:13.130","Text":"so all we need to do is to file away the values in the relevant places."},{"Start":"03:13.130 ","End":"03:16.595","Text":"This name equals name,"},{"Start":"03:16.595 ","End":"03:21.330","Text":"this hours equals hours,"},{"Start":"03:21.620 ","End":"03:26.420","Text":"this hourly rate equals hourly rate,"},{"Start":"03:26.420 ","End":"03:31.040","Text":"but the only one that we haven\u0027t got a value for is tax rate and"},{"Start":"03:31.040 ","End":"03:36.695","Text":"we\u0027ve been told in the question in part a to set that to 0.2."},{"Start":"03:36.695 ","End":"03:39.240","Text":"There\u0027s our constructor done."},{"Start":"03:39.240 ","End":"03:43.295","Text":"We can just copy and paste that for our second constructor."},{"Start":"03:43.295 ","End":"03:45.374","Text":"The only difference, it\u0027s complaining here,"},{"Start":"03:45.374 ","End":"03:49.880","Text":"it\u0027s saying we\u0027ve already got constructor with these parameters and types."},{"Start":"03:49.880 ","End":"03:54.905","Text":"We need an extra one so they can distinguish between the 2 different constructors,"},{"Start":"03:54.905 ","End":"03:57.040","Text":"and that it\u0027s going to be a double again."},{"Start":"03:57.040 ","End":"04:00.290","Text":"That will contain the tax rate."},{"Start":"04:00.290 ","End":"04:02.060","Text":"So let\u0027s call it tax rate."},{"Start":"04:02.060 ","End":"04:05.300","Text":"Then we can get rid of default here."},{"Start":"04:05.300 ","End":"04:09.215","Text":"We can just say this tax rate,"},{"Start":"04:09.215 ","End":"04:15.460","Text":"just copy, that equals tax rate."},{"Start":"04:15.460 ","End":"04:20.960","Text":"There\u0027s our constructors done just to check and seems to be fine."},{"Start":"04:20.960 ","End":"04:25.190","Text":"All we need to do now is to implement in part b and c, 2 methods."},{"Start":"04:25.190 ","End":"04:27.020","Text":"There\u0027s only 2 methods."},{"Start":"04:27.020 ","End":"04:30.260","Text":"One\u0027s called getPaidGross, one\u0027s called getPayNets."},{"Start":"04:30.260 ","End":"04:31.430","Text":"These are both public as well."},{"Start":"04:31.430 ","End":"04:35.975","Text":"You can see the plus sign next to them on the UML class diagram."},{"Start":"04:35.975 ","End":"04:38.915","Text":"They both return doubles."},{"Start":"04:38.915 ","End":"04:40.910","Text":"Straightforward enough."},{"Start":"04:40.910 ","End":"04:44.315","Text":"Public gets gross pay."},{"Start":"04:44.315 ","End":"04:51.170","Text":"No parameters is going to simply return how much the gross pay is and we"},{"Start":"04:51.170 ","End":"04:58.470","Text":"calculate the pay obviously by multiplying the hours by the hourly rate."},{"Start":"04:58.470 ","End":"05:01.785","Text":"That\u0027s it. That\u0027s gross pay done."},{"Start":"05:01.785 ","End":"05:04.595","Text":"Not much harder for net pay."},{"Start":"05:04.595 ","End":"05:14.030","Text":"We just need to subtract the text part of the pay and then we\u0027ve got the net pay."},{"Start":"05:14.030 ","End":"05:16.715","Text":"We\u0027re going to use actually,"},{"Start":"05:16.715 ","End":"05:24.240","Text":"we can call the method getGrossPay to get the gross pay first."},{"Start":"05:24.850 ","End":"05:27.755","Text":"That will return as the gross pay."},{"Start":"05:27.755 ","End":"05:31.224","Text":"Better put some brackets around these things as well."},{"Start":"05:31.224 ","End":"05:35.405","Text":"What I\u0027m going to do is from that we want to subtract,"},{"Start":"05:35.405 ","End":"05:42.620","Text":"getGrossPay again multiply by the tax rate."},{"Start":"05:42.620 ","End":"05:47.075","Text":"This part is the bit that we\u0027re going to"},{"Start":"05:47.075 ","End":"05:50.870","Text":"subtract as tax and we subtract it from the gross pay."},{"Start":"05:50.870 ","End":"05:54.140","Text":"I\u0027m actually calling the getGrossPay method here."},{"Start":"05:54.140 ","End":"05:56.135","Text":"If I wanted to be a bit more efficient,"},{"Start":"05:56.135 ","End":"05:59.615","Text":"I could have stored this in a variable to save myself calling it twice,"},{"Start":"05:59.615 ","End":"06:02.025","Text":"but I\u0027m just going to leave it as it is."},{"Start":"06:02.025 ","End":"06:04.010","Text":"It\u0027s a very simple demo program."},{"Start":"06:04.010 ","End":"06:06.205","Text":"If you were doing this millions of times a second,"},{"Start":"06:06.205 ","End":"06:08.270","Text":"you probably would want to store it into"},{"Start":"06:08.270 ","End":"06:12.260","Text":"a variable because it would improve the performance slightly."},{"Start":"06:12.260 ","End":"06:15.350","Text":"There\u0027s our finished methods."},{"Start":"06:15.350 ","End":"06:17.480","Text":"What we haven\u0027t done is said the return value."},{"Start":"06:17.480 ","End":"06:22.100","Text":"We need to put double in front of both of these."},{"Start":"06:22.100 ","End":"06:23.840","Text":"Then it will stop complaining."},{"Start":"06:23.840 ","End":"06:25.175","Text":"Now I can test."},{"Start":"06:25.175 ","End":"06:29.623","Text":"The test in part d is to create 4 different objects,"},{"Start":"06:29.623 ","End":"06:32.330","Text":"2 with tax rate set, 2 without."},{"Start":"06:32.330 ","End":"06:34.340","Text":"The first 2 are the ones without."},{"Start":"06:34.340 ","End":"06:36.515","Text":"There\u0027s only 3 parameters."},{"Start":"06:36.515 ","End":"06:43.325","Text":"The first person\u0027s name and their pay is 25,"},{"Start":"06:43.325 ","End":"06:45.940","Text":"and their hourly rate is 15."},{"Start":"06:45.940 ","End":"06:48.605","Text":"We can put these in either order, actually,"},{"Start":"06:48.605 ","End":"06:51.635","Text":"in our code, it\u0027s our choice, but obviously,"},{"Start":"06:51.635 ","End":"06:55.040","Text":"the order that they\u0027re received in determines which"},{"Start":"06:55.040 ","End":"06:59.920","Text":"parameter and then ultimately which instance variable it\u0027s going to end up in."},{"Start":"06:59.920 ","End":"07:02.655","Text":"There\u0027s that one done."},{"Start":"07:02.655 ","End":"07:05.420","Text":"Let\u0027s see what happens if we run getGrossPay."},{"Start":"07:05.420 ","End":"07:08.900","Text":"We\u0027re expecting 375 and that\u0027s what we get."},{"Start":"07:08.900 ","End":"07:12.860","Text":"That\u0027s what we\u0027re expecting 300 for net pay."},{"Start":"07:12.860 ","End":"07:19.920","Text":"The tax comes out to £75 at 20 per cent of 375."},{"Start":"07:21.400 ","End":"07:24.200","Text":"That\u0027s great. That seems to work."},{"Start":"07:24.200 ","End":"07:26.695","Text":"Let\u0027s do the second one."},{"Start":"07:26.695 ","End":"07:29.100","Text":"This is a different name."},{"Start":"07:29.100 ","End":"07:32.805","Text":"Same hourly rate,"},{"Start":"07:32.805 ","End":"07:35.970","Text":"but different number of hours,"},{"Start":"07:35.970 ","End":"07:37.820","Text":"so expect a different number here."},{"Start":"07:37.820 ","End":"07:40.040","Text":"The tax is going to be the same as well,"},{"Start":"07:40.040 ","End":"07:45.560","Text":"but we expect the gross and the net to be different from the previous one."},{"Start":"07:45.560 ","End":"07:53.540","Text":"We get 525 as we expect for the gross and 420 as we expect from the net."},{"Start":"07:53.540 ","End":"07:59.800","Text":"The next one we\u0027re going to do in Part 3 is going to take 4 parameters."},{"Start":"07:59.800 ","End":"08:06.695","Text":"For this person, we\u0027ve got higher number of hours and a higher hourly rate."},{"Start":"08:06.695 ","End":"08:09.730","Text":"The tax rate is also higher."},{"Start":"08:09.730 ","End":"08:13.020","Text":"So 40 per cent tax for this person."},{"Start":"08:13.020 ","End":"08:19.395","Text":"The gross pay is 875, as we expect."},{"Start":"08:19.395 ","End":"08:24.290","Text":"The net pay should be 40 per cent lower and that\u0027s 525."},{"Start":"08:24.290 ","End":"08:27.245","Text":"That\u0027s correct. Let\u0027s just try the final one now."},{"Start":"08:27.245 ","End":"08:29.990","Text":"Again, 4 parameters."},{"Start":"08:29.990 ","End":"08:37.750","Text":"This one is going to be the same hours and the same hourly rate, 35 and 25."},{"Start":"08:37.750 ","End":"08:40.250","Text":"So the gross figure should be the same,"},{"Start":"08:40.250 ","End":"08:42.245","Text":"but the tax rate is going to be different."},{"Start":"08:42.245 ","End":"08:48.520","Text":"45 per cent tax for this person so we expect a gross of 875 again."},{"Start":"08:48.520 ","End":"08:55.490","Text":"We get that. Then we expect net to be slightly different, which it is."},{"Start":"08:55.490 ","End":"08:58.100","Text":"That\u0027s all work fine."},{"Start":"08:58.100 ","End":"09:03.275","Text":"We were able to figure out what the UML diagram is asking us to do."},{"Start":"09:03.275 ","End":"09:07.520","Text":"We were able to create 2 but 4 instances of"},{"Start":"09:07.520 ","End":"09:11.920","Text":"this object to using 1 constructor to using another."},{"Start":"09:11.920 ","End":"09:16.910","Text":"It\u0027s actually relatively useful as a class definition."},{"Start":"09:16.910 ","End":"09:19.055","Text":"With just very short amount of code,"},{"Start":"09:19.055 ","End":"09:21.800","Text":"we were able to do something relatively useful."},{"Start":"09:21.800 ","End":"09:24.810","Text":"That\u0027s it for this one. Thanks for watching."}],"ID":31098},{"Watched":false,"Name":"Exercise 5","Duration":"16m 11s","ChapterTopicVideoID":29471,"CourseChapterTopicPlaylistID":294471,"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.570","Text":"Hello. Welcome to this video on this exercise in which we\u0027ve been asked to create"},{"Start":"00:03.570 ","End":"00:08.235","Text":"a new class called kangaroo based on the class diagram that\u0027s been given."},{"Start":"00:08.235 ","End":"00:12.990","Text":"In part a, we use a constructor to set the values of the attributes size, color,"},{"Start":"00:12.990 ","End":"00:18.525","Text":"and age to values passed in at instantiation and asleep to false."},{"Start":"00:18.525 ","End":"00:19.890","Text":"In part b,"},{"Start":"00:19.890 ","End":"00:23.085","Text":"we\u0027re asked to note how count is underlined and is used"},{"Start":"00:23.085 ","End":"00:26.895","Text":"to keep track of how many kangaroo objects have been instantiated."},{"Start":"00:26.895 ","End":"00:32.700","Text":"We add code to the constructor that will keep count of the number of kangaroos created."},{"Start":"00:32.700 ","End":"00:36.885","Text":"In part c, we\u0027re asked to implement the jump method to display the message"},{"Start":"00:36.885 ","End":"00:41.220","Text":"\"Jumping:\" concatenated with the values of the 2 parameters passed in."},{"Start":"00:41.220 ","End":"00:43.755","Text":"For example, \"Jumping: 2 high,"},{"Start":"00:43.755 ","End":"00:46.320","Text":"direction F\" for forward."},{"Start":"00:46.320 ","End":"00:49.070","Text":"In part d, we implement sleep and"},{"Start":"00:49.070 ","End":"00:53.530","Text":"awake methods to set the asleep attribute to the appropriate value."},{"Start":"00:53.530 ","End":"00:57.230","Text":"In part e, we implement the isAsleep method so it"},{"Start":"00:57.230 ","End":"01:01.190","Text":"returns a value indicating whether the kangaroo is currently asleep or not."},{"Start":"01:01.190 ","End":"01:04.760","Text":"In part f, we implement display status so"},{"Start":"01:04.760 ","End":"01:08.060","Text":"that a message is displayed indicating its age, color,"},{"Start":"01:08.060 ","End":"01:11.240","Text":"and size, and whether the kangaroo is asleep or awake,"},{"Start":"01:11.240 ","End":"01:13.450","Text":"for example, this is a red kangaroo,"},{"Start":"01:13.450 ","End":"01:15.175","Text":"size 1, age 3."},{"Start":"01:15.175 ","End":"01:17.765","Text":"The kangaroo is currently sleeping."},{"Start":"01:17.765 ","End":"01:21.440","Text":"We then test by creating 3 kangaroo objects on"},{"Start":"01:21.440 ","End":"01:25.331","Text":"the object workbench with the following values at instantiation,1,"},{"Start":"01:25.331 ","End":"01:32.040","Text":"\"red\", 3, 2, \"gray\", 5, 3, \"brown\", 4."},{"Start":"01:32.040 ","End":"01:35.745","Text":"In part 4, we run display status on all 3 objects,"},{"Start":"01:35.745 ","End":"01:36.900","Text":"and in part 5,"},{"Start":"01:36.900 ","End":"01:40.340","Text":"we run the sleep method on 1 of the kangaroos and confirm"},{"Start":"01:40.340 ","End":"01:44.420","Text":"the effect by running is asleep and display status."},{"Start":"01:44.420 ","End":"01:49.189","Text":"We then run getCount and confirm that 3 is returned."},{"Start":"01:49.189 ","End":"01:54.460","Text":"In part h, we\u0027re asked to modify the getCount method so it\u0027s a class method."},{"Start":"01:54.460 ","End":"01:57.155","Text":"When creating objects in the object workbench,"},{"Start":"01:57.155 ","End":"02:01.770","Text":"what has now happened to the getCount method we are asked."},{"Start":"02:01.770 ","End":"02:04.795","Text":"Then finally, we\u0027re also asked in part i,"},{"Start":"02:04.795 ","End":"02:10.340","Text":"how we can run getCount from within the BlueJ environment."},{"Start":"02:10.880 ","End":"02:18.115","Text":"In this question, we\u0027ve been asked to create the class based on the class diagram,"},{"Start":"02:18.115 ","End":"02:22.230","Text":"and there are 5 attributes."},{"Start":"02:22.230 ","End":"02:25.710","Text":"First 1 is an int,"},{"Start":"02:25.710 ","End":"02:28.215","Text":"which is called size,"},{"Start":"02:28.215 ","End":"02:34.785","Text":"next 1 is a string called color,"},{"Start":"02:34.785 ","End":"02:42.420","Text":"and then we\u0027ve got an int which is age,"},{"Start":"02:42.420 ","End":"02:52.090","Text":"and then a boolean used to track whether the animal is asleep or not called asleep,"},{"Start":"02:52.130 ","End":"02:55.425","Text":"and then finally a count."},{"Start":"02:55.425 ","End":"03:03.135","Text":"Now count has been underlined so that means something."},{"Start":"03:03.135 ","End":"03:10.015","Text":"What it means is we have to mark it as a static variable,"},{"Start":"03:10.015 ","End":"03:13.240","Text":"and that means it will persist."},{"Start":"03:13.240 ","End":"03:16.190","Text":"For the class as long as the program is running,"},{"Start":"03:16.190 ","End":"03:18.925","Text":"you don\u0027t even need to create any objects,"},{"Start":"03:18.925 ","End":"03:22.390","Text":"and this variable will exist but it\u0027ll be"},{"Start":"03:22.390 ","End":"03:26.890","Text":"shared amongst all objects that are created from this class."},{"Start":"03:26.890 ","End":"03:33.820","Text":"We can use it to keep track of how many kangaroo objects we\u0027ve instantiated."},{"Start":"03:33.820 ","End":"03:37.155","Text":"That\u0027s very exciting so we\u0027re not done yet."},{"Start":"03:37.155 ","End":"03:39.135","Text":"Let\u0027s have a look at the next part,"},{"Start":"03:39.135 ","End":"03:41.100","Text":"then we\u0027ve got to create a constructor,"},{"Start":"03:41.100 ","End":"03:45.425","Text":"and the constructor is obviously going to be called kangaroo."},{"Start":"03:45.425 ","End":"03:51.215","Text":"Like all constructors, it should be marked as public,"},{"Start":"03:51.215 ","End":"03:53.660","Text":"and there\u0027s no return value."},{"Start":"03:53.660 ","End":"03:57.455","Text":"Now the attributes that are passed in,"},{"Start":"03:57.455 ","End":"04:00.000","Text":"the arguments are passed in,"},{"Start":"04:00.000 ","End":"04:02.705","Text":"the first 1 is an int,"},{"Start":"04:02.705 ","End":"04:05.990","Text":"and that is going to be size."},{"Start":"04:05.990 ","End":"04:09.435","Text":"Then we\u0027ve got 1 which is the color,"},{"Start":"04:09.435 ","End":"04:12.520","Text":"and that\u0027s a string, not an int."},{"Start":"04:14.120 ","End":"04:17.390","Text":"Then we\u0027ve got another int,"},{"Start":"04:17.390 ","End":"04:20.510","Text":"which is the age of the kangaroo."},{"Start":"04:20.510 ","End":"04:24.790","Text":"That is the only constructor we\u0027ve been asked to create."},{"Start":"04:24.790 ","End":"04:27.300","Text":"Let\u0027s set all the values,"},{"Start":"04:27.300 ","End":"04:33.335","Text":"and the only 1 we haven\u0027t been given a value for is asleep,"},{"Start":"04:33.335 ","End":"04:40.320","Text":"and we\u0027d been told to make that false."},{"Start":"04:40.320 ","End":"04:45.645","Text":"Every kangaroo we create is not asleep,"},{"Start":"04:45.645 ","End":"04:50.210","Text":"it\u0027s awake, so that\u0027s that part done."},{"Start":"04:50.210 ","End":"04:55.445","Text":"Now, coming back to the static variable we created up here,"},{"Start":"04:55.445 ","End":"04:59.330","Text":"we were told in part b to use it to"},{"Start":"04:59.330 ","End":"05:03.139","Text":"keep track of how many kangaroo objects have been instantiated."},{"Start":"05:03.139 ","End":"05:06.530","Text":"Well, how do you do that? Every time we create an object,"},{"Start":"05:06.530 ","End":"05:07.850","Text":"the constructor is going to run."},{"Start":"05:07.850 ","End":"05:09.490","Text":"There is only 1 constructor,"},{"Start":"05:09.490 ","End":"05:13.160","Text":"and all we have to do in there is to increment the count variable,"},{"Start":"05:13.160 ","End":"05:15.735","Text":"count has already been created,"},{"Start":"05:15.735 ","End":"05:22.414","Text":"it belongs to the whole class and it will not exist for each instance of a kangaroo."},{"Start":"05:22.414 ","End":"05:24.290","Text":"It won\u0027t have that value,"},{"Start":"05:24.290 ","End":"05:27.755","Text":"it will apply collectively to all the kangaroos."},{"Start":"05:27.755 ","End":"05:30.380","Text":"Therefore, anytime we create a kangaroo,"},{"Start":"05:30.380 ","End":"05:33.470","Text":"that will get bumped up so we\u0027ll be keeping track of"},{"Start":"05:33.470 ","End":"05:38.374","Text":"how many objects effectively we\u0027ve instantiated using this class."},{"Start":"05:38.374 ","End":"05:40.640","Text":"That\u0027s as simple as that,"},{"Start":"05:40.640 ","End":"05:42.260","Text":"that\u0027s part b done,"},{"Start":"05:42.260 ","End":"05:46.255","Text":"and then what we\u0027ve been asked to do is implement jump."},{"Start":"05:46.255 ","End":"05:50.565","Text":"Another public method, obviously,"},{"Start":"05:50.565 ","End":"05:58.865","Text":"and all it does is display a message based on the parameters that are passed in."},{"Start":"05:58.865 ","End":"06:03.215","Text":"We have from the diagram an int,"},{"Start":"06:03.215 ","End":"06:06.565","Text":"which is the height,"},{"Start":"06:06.565 ","End":"06:11.625","Text":"and a char, which is the direction,"},{"Start":"06:11.625 ","End":"06:18.065","Text":"and all we need to do is to print a message system.out.println."},{"Start":"06:18.065 ","End":"06:23.180","Text":"What we\u0027re going to print is jumping,"},{"Start":"06:23.180 ","End":"06:27.260","Text":"followed by height,"},{"Start":"06:27.260 ","End":"06:32.550","Text":"and then we say which direction it\u0027s going in,"},{"Start":"06:32.550 ","End":"06:35.760","Text":"and we use the direction for that."},{"Start":"06:35.760 ","End":"06:39.500","Text":"Rather than do some fancy graphics and animation,"},{"Start":"06:39.500 ","End":"06:42.650","Text":"we\u0027re just printing out what these 2 things are"},{"Start":"06:42.650 ","End":"06:47.060","Text":"just to show you that the kangaroo can actually do things."},{"Start":"06:47.060 ","End":"06:52.110","Text":"That\u0027s part c done and part d,"},{"Start":"06:52.110 ","End":"06:54.470","Text":"I seem to have forgotten something here,"},{"Start":"06:54.470 ","End":"06:56.810","Text":"I\u0027ve forgotten to put the return type."},{"Start":"06:56.810 ","End":"06:57.980","Text":"There is no return type,"},{"Start":"06:57.980 ","End":"06:59.855","Text":"so I need to put void there."},{"Start":"06:59.855 ","End":"07:06.160","Text":"Part d, I need to implement sleep and awake methods."},{"Start":"07:06.160 ","End":"07:10.265","Text":"Once again, they\u0027re going to be public."},{"Start":"07:10.265 ","End":"07:16.875","Text":"Sleep is simply going to set the asleep attribute to true,"},{"Start":"07:16.875 ","End":"07:23.730","Text":"and awake is obviously going to do the opposite of awake, what you want to call it."},{"Start":"07:23.730 ","End":"07:27.660","Text":"Awake makes it false."},{"Start":"07:27.660 ","End":"07:34.242","Text":"That\u0027s that done, and then we have a method called isAsleep."},{"Start":"07:34.242 ","End":"07:38.095","Text":"It returns a Boolean and there are no parameters."},{"Start":"07:38.095 ","End":"07:40.090","Text":"This is essentially asking a question,"},{"Start":"07:40.090 ","End":"07:42.175","Text":"very common naming methodology, this,"},{"Start":"07:42.175 ","End":"07:45.445","Text":"because it reads quite well in the code, isAsleep."},{"Start":"07:45.445 ","End":"07:47.920","Text":"We\u0027re asking a question, is it asleep?"},{"Start":"07:47.920 ","End":"07:50.860","Text":"If is asleep, it will return true."},{"Start":"07:50.860 ","End":"07:58.570","Text":"All we need to do is check the value of the asleep variable."},{"Start":"07:58.570 ","End":"08:04.975","Text":"We can actually just return the contents of that variable, asleep."},{"Start":"08:04.975 ","End":"08:08.050","Text":"If it contains true,"},{"Start":"08:08.050 ","End":"08:09.790","Text":"that means it\u0027s asleep,"},{"Start":"08:09.790 ","End":"08:11.500","Text":"if it contains false,"},{"Start":"08:11.500 ","End":"08:14.290","Text":"that means it\u0027s not asleep, i.e. it\u0027s awake."},{"Start":"08:14.290 ","End":"08:16.660","Text":"That\u0027s exactly what we wanted there."},{"Start":"08:16.660 ","End":"08:21.355","Text":"Nice naming and this all logically works."},{"Start":"08:21.355 ","End":"08:24.370","Text":"All we got to do now is to implement"},{"Start":"08:24.370 ","End":"08:29.020","Text":"the display test status so we can then do some testing."},{"Start":"08:29.020 ","End":"08:33.170","Text":"Again, there\u0027s no return value specified."},{"Start":"08:33.330 ","End":"08:38.305","Text":"Display status, no parameters either,"},{"Start":"08:38.305 ","End":"08:40.060","Text":"and then what we do need though,"},{"Start":"08:40.060 ","End":"08:42.010","Text":"is the code inside,"},{"Start":"08:42.010 ","End":"08:47.005","Text":"which indicates its age and so on."},{"Start":"08:47.005 ","End":"08:49.705","Text":"Just another print statement."},{"Start":"08:49.705 ","End":"08:53.545","Text":"A color, and then I need to set size,"},{"Start":"08:53.545 ","End":"08:56.709","Text":"and then age, and then finally,"},{"Start":"08:56.709 ","End":"08:58.990","Text":"whether it\u0027s asleep or not."},{"Start":"08:58.990 ","End":"09:05.215","Text":"What we probably want to do there is to on the same line."},{"Start":"09:05.215 ","End":"09:09.775","Text":"Let\u0027s make this into system out print rather than print line."},{"Start":"09:09.775 ","End":"09:13.450","Text":"Let\u0027s put a full stop after what we\u0027ve already got,"},{"Start":"09:13.450 ","End":"09:18.730","Text":"and then let\u0027s do an if statement; if isAsleep."},{"Start":"09:18.730 ","End":"09:20.215","Text":"Then we print 1 thing,"},{"Start":"09:20.215 ","End":"09:22.165","Text":"otherwise, we print another thing."},{"Start":"09:22.165 ","End":"09:25.870","Text":"What we\u0027re going to print is and we do print line,"},{"Start":"09:25.870 ","End":"09:29.995","Text":"so it does have carriage return at the end or a line fit."},{"Start":"09:29.995 ","End":"09:33.250","Text":"What we want to do is print."},{"Start":"09:33.250 ","End":"09:41.305","Text":"In this case, kangaroo is currently sleeping,"},{"Start":"09:41.305 ","End":"09:45.115","Text":"otherwise, we say it\u0027s awake."},{"Start":"09:45.115 ","End":"09:48.340","Text":"It looks like we are done now."},{"Start":"09:48.340 ","End":"09:52.540","Text":"Let\u0027s just see if that compiles, which seems to."},{"Start":"09:52.540 ","End":"10:00.560","Text":"We can attempt to test with the test data that we\u0027ve been presented with."},{"Start":"10:01.020 ","End":"10:05.230","Text":"First 1 is Size 1,"},{"Start":"10:05.230 ","End":"10:08.950","Text":"color red, Page 3."},{"Start":"10:08.950 ","End":"10:10.675","Text":"Then let\u0027s create them all,"},{"Start":"10:10.675 ","End":"10:12.565","Text":"and we\u0027ll come and test them in a moment."},{"Start":"10:12.565 ","End":"10:16.165","Text":"Second 1 is Size 2,"},{"Start":"10:16.165 ","End":"10:21.115","Text":"color gray, Age 5."},{"Start":"10:21.115 ","End":"10:24.880","Text":"The final 1 is Size 3,"},{"Start":"10:24.880 ","End":"10:28.225","Text":"brown, and Age 4."},{"Start":"10:28.225 ","End":"10:31.780","Text":"Let\u0027s run display status to see what we get."},{"Start":"10:31.780 ","End":"10:34.090","Text":"That\u0027s great. Red kangaroo,"},{"Start":"10:34.090 ","End":"10:35.290","Text":"Size 1, Age 3,"},{"Start":"10:35.290 ","End":"10:37.585","Text":"and the kangaroo is awake."},{"Start":"10:37.585 ","End":"10:40.015","Text":"What do we get for the next 1?"},{"Start":"10:40.015 ","End":"10:41.350","Text":"That\u0027s as we expect,"},{"Start":"10:41.350 ","End":"10:43.480","Text":"we get a gray kangaroo, Size 2,"},{"Start":"10:43.480 ","End":"10:47.605","Text":"Age 5, and it\u0027s awake."},{"Start":"10:47.605 ","End":"10:50.005","Text":"Then the final 1,"},{"Start":"10:50.005 ","End":"10:51.625","Text":"we get brown kangaroo,"},{"Start":"10:51.625 ","End":"10:53.920","Text":"Size 3, Age 4."},{"Start":"10:53.920 ","End":"10:56.725","Text":"That\u0027s what we expect. They\u0027re all awake because,"},{"Start":"10:56.725 ","End":"10:58.645","Text":"by default, they\u0027re all awake."},{"Start":"10:58.645 ","End":"11:02.575","Text":"Now, let\u0027s run asleep method on,"},{"Start":"11:02.575 ","End":"11:04.910","Text":"let\u0027s say, the first 1."},{"Start":"11:05.280 ","End":"11:11.050","Text":"Sleep, and then let\u0027s run display status on it."},{"Start":"11:11.050 ","End":"11:14.664","Text":"That\u0027s changed the status to say it\u0027s currently sleeping."},{"Start":"11:14.664 ","End":"11:17.185","Text":"What about getCount?"},{"Start":"11:17.185 ","End":"11:19.195","Text":"Let\u0027s run getCount,"},{"Start":"11:19.195 ","End":"11:23.900","Text":"except we don\u0027t seem to have a getCount here."},{"Start":"11:27.600 ","End":"11:30.265","Text":"In fact, we haven\u0027t done getCount,"},{"Start":"11:30.265 ","End":"11:33.775","Text":"we need to double-click on the object."},{"Start":"11:33.775 ","End":"11:36.490","Text":"If you click on Show Static Fields,"},{"Start":"11:36.490 ","End":"11:41.065","Text":"it will show us the class variable for kangaroo count,"},{"Start":"11:41.065 ","End":"11:42.445","Text":"which is a static,"},{"Start":"11:42.445 ","End":"11:45.865","Text":"has the Value 3 because we created 3 kangaroos."},{"Start":"11:45.865 ","End":"11:48.024","Text":"If I was to create another 1,"},{"Start":"11:48.024 ","End":"11:51.910","Text":"you\u0027d see that that would go up by 1."},{"Start":"11:51.910 ","End":"11:55.990","Text":"Let\u0027s do 1, red 1."},{"Start":"11:55.990 ","End":"11:57.895","Text":"That\u0027s kangaroo 4."},{"Start":"11:57.895 ","End":"12:01.270","Text":"If I now click on Kangaroo 4, I can see it\u0027s details."},{"Start":"12:01.270 ","End":"12:03.100","Text":"But if I look at show static fields,"},{"Start":"12:03.100 ","End":"12:06.400","Text":"it\u0027s got a count of 4 now because we\u0027ve got 4 kangaroos."},{"Start":"12:06.400 ","End":"12:10.645","Text":"But if I did the same on any of the objects and showed the static field,"},{"Start":"12:10.645 ","End":"12:14.960","Text":"it would say 4 for all of them because there is only 1"},{"Start":"12:14.960 ","End":"12:19.500","Text":"variable or class variable called count,"},{"Start":"12:19.500 ","End":"12:24.280","Text":"and it\u0027s shared amongst all 4 of these objects."},{"Start":"12:24.290 ","End":"12:30.280","Text":"That is Part 6 done and all the other"},{"Start":"12:30.280 ","End":"12:32.950","Text":"tests taking Part G. But what we\u0027ve been asked to do"},{"Start":"12:32.950 ","End":"12:35.965","Text":"in Part H is to modify the getCount method."},{"Start":"12:35.965 ","End":"12:38.035","Text":"We haven\u0027t actually done a getCount method,"},{"Start":"12:38.035 ","End":"12:40.375","Text":"so probably we should implement that."},{"Start":"12:40.375 ","End":"12:45.700","Text":"It was in the UML diagram public and it\u0027s going to return an int,"},{"Start":"12:45.700 ","End":"12:48.820","Text":"is equal to getCount."},{"Start":"12:48.820 ","End":"12:55.300","Text":"All it does obviously is going to return the value of count."},{"Start":"12:55.300 ","End":"12:57.115","Text":"That will work."},{"Start":"12:57.115 ","End":"13:00.145","Text":"If we create 2 kangaroos,"},{"Start":"13:00.145 ","End":"13:02.305","Text":"it will give us a count of 2."},{"Start":"13:02.305 ","End":"13:05.410","Text":"If we create 3, it\u0027ll create a count of 3."},{"Start":"13:05.410 ","End":"13:10.720","Text":"If I look at that and I can show my static field says 1,"},{"Start":"13:10.720 ","End":"13:15.400","Text":"and I can run my method called getCount, which will show 1."},{"Start":"13:15.400 ","End":"13:19.615","Text":"That\u0027s fine. That shows up on the object."},{"Start":"13:19.615 ","End":"13:23.440","Text":"The method itself belongs to every object,"},{"Start":"13:23.440 ","End":"13:25.435","Text":"and I have a copy of it for every object."},{"Start":"13:25.435 ","End":"13:32.230","Text":"Now what I\u0027ve been asked to do is to modify getCount so that it\u0027s a class method."},{"Start":"13:32.230 ","End":"13:39.130","Text":"I do that by putting static in front of the return value."},{"Start":"13:39.130 ","End":"13:43.540","Text":"I\u0027m basically saying this is a public static method called getCount,"},{"Start":"13:43.540 ","End":"13:45.250","Text":"which returns an integer."},{"Start":"13:45.250 ","End":"13:48.160","Text":"The effect of that will be,"},{"Start":"13:48.160 ","End":"13:51.355","Text":"if I right-click on here now,"},{"Start":"13:51.355 ","End":"13:54.610","Text":"getCount belongs to the class,"},{"Start":"13:54.610 ","End":"13:56.560","Text":"not to objects made from that class."},{"Start":"13:56.560 ","End":"14:02.839","Text":"If I create a kangaroo,"},{"Start":"14:02.839 ","End":"14:06.400","Text":"red, [inaudible] 1, red, 1."},{"Start":"14:06.400 ","End":"14:08.650","Text":"Previously, just a moment ago,"},{"Start":"14:08.650 ","End":"14:13.315","Text":"I could see getCount in here on the object context menu."},{"Start":"14:13.315 ","End":"14:16.180","Text":"I can no longer see it in here,"},{"Start":"14:16.180 ","End":"14:20.410","Text":"and instead, it appears up here on the class."},{"Start":"14:20.410 ","End":"14:24.490","Text":"That makes sense because I\u0027ve defined this"},{"Start":"14:24.490 ","End":"14:29.950","Text":"now as a class method rather than an object method,"},{"Start":"14:29.950 ","End":"14:33.550","Text":"so really subtle difference between the 2 things."},{"Start":"14:33.550 ","End":"14:39.205","Text":"Basically, the objects are not getting their own copy of the method called getCount."},{"Start":"14:39.205 ","End":"14:42.700","Text":"There\u0027s a single getCount method and it"},{"Start":"14:42.700 ","End":"14:47.095","Text":"belongs to the class because I\u0027ve put static in front of it."},{"Start":"14:47.095 ","End":"14:54.040","Text":"Just like I\u0027ve put static in front of this variable and it makes into a class variable,"},{"Start":"14:54.040 ","End":"14:56.230","Text":"if I put static in front of a method,"},{"Start":"14:56.230 ","End":"15:01.000","Text":"it means only 1 method will exist for all objects"},{"Start":"15:01.000 ","End":"15:07.260","Text":"and you won\u0027t be able to use the object name and then the name of the method to run it,"},{"Start":"15:07.260 ","End":"15:11.210","Text":"you have to use the class name and the name of"},{"Start":"15:11.210 ","End":"15:15.680","Text":"the method in order to run this particular method called getCount."},{"Start":"15:15.680 ","End":"15:18.950","Text":"It\u0027s a real subtle differences there between"},{"Start":"15:18.950 ","End":"15:23.100","Text":"static when used with variables and with methods."},{"Start":"15:23.100 ","End":"15:27.650","Text":"It seems pretty obvious what it does for the variables."},{"Start":"15:27.650 ","End":"15:29.030","Text":"But for a method,"},{"Start":"15:29.030 ","End":"15:32.670","Text":"it appears in the workbench."},{"Start":"15:32.670 ","End":"15:34.430","Text":"If it\u0027s not static,"},{"Start":"15:34.430 ","End":"15:36.305","Text":"it\u0027ll appear in the list as everything else."},{"Start":"15:36.305 ","End":"15:37.640","Text":"None of these are static."},{"Start":"15:37.640 ","End":"15:39.575","Text":"But if it is a static method,"},{"Start":"15:39.575 ","End":"15:44.450","Text":"it will only appear when we right-click on the class in this class for you up here,"},{"Start":"15:44.450 ","End":"15:48.690","Text":"which we\u0027ve never seen before we came across static."},{"Start":"15:49.320 ","End":"15:51.580","Text":"This exercise is done."},{"Start":"15:51.580 ","End":"15:53.140","Text":"Hopefully, that was straightforward."},{"Start":"15:53.140 ","End":"15:55.790","Text":"Really what we\u0027ve introduced is this idea of"},{"Start":"15:55.790 ","End":"15:59.660","Text":"static variables which belong to the class and"},{"Start":"15:59.660 ","End":"16:06.830","Text":"static methods which also belong to the class and further work there on UML diagrams,"},{"Start":"16:06.830 ","End":"16:08.870","Text":"reading them and interpreting them."},{"Start":"16:08.870 ","End":"16:11.370","Text":"Thanks for watching. See you soon."}],"ID":31099},{"Watched":false,"Name":"Exercise 6","Duration":"8m 26s","ChapterTopicVideoID":29470,"CourseChapterTopicPlaylistID":294471,"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.455","Text":"Hello, welcome back."},{"Start":"00:01.455 ","End":"00:05.010","Text":"In this exercise, we\u0027re asked to create a class called SimLauncher,"},{"Start":"00:05.010 ","End":"00:09.195","Text":"which will make use of the Kangaroo class which we created in the previous exercise."},{"Start":"00:09.195 ","End":"00:13.680","Text":"In part A, we\u0027re asked to create a public static method called main,"},{"Start":"00:13.680 ","End":"00:20.190","Text":"which returns no value but accepts an array of Strings with a parameter name, args."},{"Start":"00:20.190 ","End":"00:22.755","Text":"In part B inside the main method,"},{"Start":"00:22.755 ","End":"00:27.345","Text":"we\u0027re asked to create 3 instances of the Kangaroo class called kanga1,"},{"Start":"00:27.345 ","End":"00:29.835","Text":"kanga2, and kanga3."},{"Start":"00:29.835 ","End":"00:36.315","Text":"Again, inside the main method in part C we\u0027re asked to run the sleep method on kanga1."},{"Start":"00:36.315 ","End":"00:38.340","Text":"Then once more inside main,"},{"Start":"00:38.340 ","End":"00:44.000","Text":"we\u0027re asked to run the display status method on each of the 3 Kangaroo objects."},{"Start":"00:44.000 ","End":"00:47.960","Text":"In part E, we\u0027re asked to use the class method getCount,"},{"Start":"00:47.960 ","End":"00:52.570","Text":"to display a message saying how many kangaroos there are in the simulation."},{"Start":"00:52.570 ","End":"00:54.900","Text":"In part F, we\u0027re asked for,"},{"Start":"00:54.900 ","End":"00:58.640","Text":"first make sure we\u0027ve activated the terminal window and Bluej and then"},{"Start":"00:58.640 ","End":"01:02.855","Text":"to right click on the SimLauncher class and select the main method."},{"Start":"01:02.855 ","End":"01:04.790","Text":"We\u0027re asked the question then,"},{"Start":"01:04.790 ","End":"01:10.740","Text":"what happens and why did no object get created on the object workbench?"},{"Start":"01:12.170 ","End":"01:19.850","Text":"The final exercise is going to be called SimLauncher, class called SimLauncher."},{"Start":"01:19.850 ","End":"01:24.820","Text":"In it, we have to create a static method with a special name, main."},{"Start":"01:24.820 ","End":"01:29.540","Text":"This is a public method and it returns no value,"},{"Start":"01:29.540 ","End":"01:33.490","Text":"so static follows public,"},{"Start":"01:33.490 ","End":"01:35.415","Text":"the return value,"},{"Start":"01:35.415 ","End":"01:38.630","Text":"which is void in this case and then the name of the method,"},{"Start":"01:38.630 ","End":"01:40.790","Text":"just as we would do for any other method."},{"Start":"01:40.790 ","End":"01:45.825","Text":"We\u0027ve been told that the parameter is an array of Strings,"},{"Start":"01:45.825 ","End":"01:48.330","Text":"so we put that as String and then"},{"Start":"01:48.330 ","End":"01:52.205","Text":"an empty pair of brackets and then whatever we want to call our parameter."},{"Start":"01:52.205 ","End":"01:57.545","Text":"Traditionally it\u0027s called args for arguments as a way of passing in values to a program."},{"Start":"01:57.545 ","End":"01:59.000","Text":"We\u0027re not going to do anything with it,"},{"Start":"01:59.000 ","End":"02:05.805","Text":"but that\u0027s the standard signature for a main method in Java,"},{"Start":"02:05.805 ","End":"02:10.580","Text":"very similar to what it looks like for C as well."},{"Start":"02:10.580 ","End":"02:14.090","Text":"That\u0027s something we haven\u0027t done before and we\u0027ll"},{"Start":"02:14.090 ","End":"02:17.225","Text":"see what the implication is of that in a moment."},{"Start":"02:17.225 ","End":"02:19.115","Text":"Then in part B, we\u0027re asked,"},{"Start":"02:19.115 ","End":"02:20.360","Text":"inside this method,"},{"Start":"02:20.360 ","End":"02:23.375","Text":"to create 3 instances of the Kangaroo class."},{"Start":"02:23.375 ","End":"02:27.245","Text":"Now, there is no code in here to do with the Kangaroo class,"},{"Start":"02:27.245 ","End":"02:30.410","Text":"but it is in the project that we\u0027ve been working in,"},{"Start":"02:30.410 ","End":"02:36.980","Text":"so it will have access to that class and to create instances of a class,"},{"Start":"02:36.980 ","End":"02:40.145","Text":"we simply need to say what the class name is,"},{"Start":"02:40.145 ","End":"02:41.960","Text":"in this case, Kangaroo."},{"Start":"02:41.960 ","End":"02:46.590","Text":"We need to give it an identifier and we\u0027ve been given the names,"},{"Start":"02:46.590 ","End":"02:48.090","Text":"kanga1 in first case."},{"Start":"02:48.090 ","End":"02:50.210","Text":"Then to create an instance of an object,"},{"Start":"02:50.210 ","End":"02:51.518","Text":"we use the keyword,"},{"Start":"02:51.518 ","End":"02:54.875","Text":"new, and then again give the name of the object."},{"Start":"02:54.875 ","End":"02:57.560","Text":"Then any arguments we want to"},{"Start":"02:57.560 ","End":"03:01.355","Text":"pass in when we\u0027re creating this thought will go to the constructor."},{"Start":"03:01.355 ","End":"03:05.690","Text":"We may as well use the values that we used previously."},{"Start":"03:05.690 ","End":"03:13.575","Text":"There was an integer of size and then color and then age."},{"Start":"03:13.575 ","End":"03:17.870","Text":"There\u0027s our first one done and we can just"},{"Start":"03:17.870 ","End":"03:21.760","Text":"copy that line twice to create 2 more instances,"},{"Start":"03:21.760 ","End":"03:25.260","Text":"it\u0027s complaining because there\u0027s already one called kanga1."},{"Start":"03:25.270 ","End":"03:31.700","Text":"Now it\u0027s happier, these will all have the same sizes and colors,"},{"Start":"03:31.700 ","End":"03:33.475","Text":"so let\u0027s change them."},{"Start":"03:33.475 ","End":"03:41.925","Text":"That\u0027s gray, I\u0027m going to change that one to brown and let\u0027s change this ages as well."},{"Start":"03:41.925 ","End":"03:46.895","Text":"We\u0027ve now got 3 different kangaroos and it\u0027s not complaining."},{"Start":"03:46.895 ","End":"03:52.050","Text":"Each one has got a different instance name."},{"Start":"03:52.050 ","End":"03:55.790","Text":"Reference to the instance is stored in this name."},{"Start":"03:55.790 ","End":"03:59.435","Text":"We can do stuff with these instances"},{"Start":"03:59.435 ","End":"04:03.665","Text":"by just referring to the name and then using dot notation."},{"Start":"04:03.665 ","End":"04:06.260","Text":"We can run, for example,"},{"Start":"04:06.260 ","End":"04:11.190","Text":"the sleep method and we\u0027ve asked to do it on kanga1 in part C,"},{"Start":"04:11.190 ","End":"04:17.980","Text":"so kanga1 is the name of the object and we want to make it sleep,"},{"Start":"04:17.980 ","End":"04:19.470","Text":"so we call the sleep method,"},{"Start":"04:19.470 ","End":"04:20.675","Text":"there are no parameters,"},{"Start":"04:20.675 ","End":"04:23.390","Text":"so we just close the brackets and it\u0027s as simple as that,"},{"Start":"04:23.390 ","End":"04:26.330","Text":"so make kanga1 go to sleep."},{"Start":"04:26.330 ","End":"04:32.595","Text":"We can then do a display status on kanga1 and in fact,"},{"Start":"04:32.595 ","End":"04:35.820","Text":"all of the kangaroos,"},{"Start":"04:35.820 ","End":"04:40.440","Text":"if we just change the identifier."},{"Start":"04:40.440 ","End":"04:42.750","Text":"This will do it for kangaroo 2,"},{"Start":"04:42.750 ","End":"04:44.760","Text":"this will do it for kangaroo 3."},{"Start":"04:44.760 ","End":"04:47.120","Text":"That\u0027s part D done."},{"Start":"04:47.120 ","End":"04:49.580","Text":"Now the only slightly tricky thing"},{"Start":"04:49.580 ","End":"04:52.565","Text":"here that you might have been wondering how do I do this,"},{"Start":"04:52.565 ","End":"04:56.810","Text":"is we\u0027re using a class method this time, getCount."},{"Start":"04:56.810 ","End":"05:01.225","Text":"It\u0027s not kanga1.getCount because it doesn\u0027t exist inside the object,"},{"Start":"05:01.225 ","End":"05:03.445","Text":"it exists inside the class."},{"Start":"05:03.445 ","End":"05:07.625","Text":"The syntax is going to be slightly different and we\u0027ve been asked to print a message,"},{"Start":"05:07.625 ","End":"05:10.550","Text":"so let\u0027s start with printing the message first,"},{"Start":"05:10.550 ","End":"05:14.870","Text":"so system out print line and we\u0027re going to say,"},{"Start":"05:14.870 ","End":"05:18.130","Text":"how many kangaroos there are."},{"Start":"05:18.130 ","End":"05:19.990","Text":"Then what we want to do,"},{"Start":"05:19.990 ","End":"05:22.280","Text":"is we give the name of the class,"},{"Start":"05:22.280 ","End":"05:26.840","Text":"not the name of the object and this is the Kangaroo class that we\u0027re interested"},{"Start":"05:26.840 ","End":"05:31.530","Text":"in and the Kangaroo class has a method called getCount,"},{"Start":"05:31.530 ","End":"05:33.680","Text":"it\u0027s a class method because we marked as"},{"Start":"05:33.680 ","End":"05:38.300","Text":"static in the previous exercise at the end of the exercise."},{"Start":"05:38.300 ","End":"05:43.520","Text":"We don\u0027t put kanga1 or kanga2 or kanga3.getCount,"},{"Start":"05:43.520 ","End":"05:49.130","Text":"we put Kangaroo because that\u0027s the name of the class that contains this static method,"},{"Start":"05:49.130 ","End":"05:52.325","Text":"which itself has a class variable,"},{"Start":"05:52.325 ","End":"05:57.255","Text":"which keeps track of how many kangaroos we\u0027ve instantiated."},{"Start":"05:57.255 ","End":"06:00.405","Text":"That\u0027s it, simple as that."},{"Start":"06:00.405 ","End":"06:02.580","Text":"That part E done,"},{"Start":"06:02.580 ","End":"06:05.870","Text":"so we can now just go ahead and run this."},{"Start":"06:05.870 ","End":"06:12.185","Text":"Now what we see is a slight change in the Bluej window up here,"},{"Start":"06:12.185 ","End":"06:17.495","Text":"where it\u0027s showing us an arrow going to Kangaroo and what this is saying is,"},{"Start":"06:17.495 ","End":"06:19.475","Text":"the class SimLauncher,"},{"Start":"06:19.475 ","End":"06:23.325","Text":"is making use of the class Kangaroo."},{"Start":"06:23.325 ","End":"06:26.060","Text":"When we want to do something like this,"},{"Start":"06:26.060 ","End":"06:28.190","Text":"we don\u0027t need to create a new SimLauncher,"},{"Start":"06:28.190 ","End":"06:35.860","Text":"we can simply go straight to main String args and run it and the program will run."},{"Start":"06:35.860 ","End":"06:37.790","Text":"We can give some arguments,"},{"Start":"06:37.790 ","End":"06:38.930","Text":"we don\u0027t need to for this program,"},{"Start":"06:38.930 ","End":"06:42.280","Text":"so just click on \"Okay\" there and low and behold,"},{"Start":"06:42.280 ","End":"06:48.020","Text":"all of that code inside main runs and we see the output from all of it."},{"Start":"06:48.020 ","End":"06:51.050","Text":"We\u0027re told there\u0027s a red kangaroo, of size that,"},{"Start":"06:51.050 ","End":"06:53.240","Text":"age that and it\u0027s sleeping and you remember"},{"Start":"06:53.240 ","End":"06:58.235","Text":"we called the sleep method on that first object kanga1."},{"Start":"06:58.235 ","End":"07:00.545","Text":"The other 2 were awake,"},{"Start":"07:00.545 ","End":"07:02.030","Text":"so that\u0027s why we\u0027re seeing awake,"},{"Start":"07:02.030 ","End":"07:03.890","Text":"because they are awake by default."},{"Start":"07:03.890 ","End":"07:07.650","Text":"There\u0027s where the class method ran and we got"},{"Start":"07:07.650 ","End":"07:12.150","Text":"the value 3 back from it and we printed this text around it."},{"Start":"07:12.150 ","End":"07:16.640","Text":"Very simple exercise, but actually"},{"Start":"07:16.640 ","End":"07:23.075","Text":"showing a lot of what we\u0027ve learned in this topic in the previous exercises."},{"Start":"07:23.075 ","End":"07:29.540","Text":"First of all, creating instances of a class and then we have an object,"},{"Start":"07:29.540 ","End":"07:35.360","Text":"so we\u0027ve got 3 objects here and the object reference is stored in kanga1,"},{"Start":"07:35.360 ","End":"07:36.890","Text":"kanga2, and kanga3,"},{"Start":"07:36.890 ","End":"07:40.940","Text":"so we can then make use of all the methods available"},{"Start":"07:40.940 ","End":"07:45.305","Text":"in those objects and these display status and sleep are the ones that we\u0027ve used."},{"Start":"07:45.305 ","End":"07:47.600","Text":"Then finally the class method,"},{"Start":"07:47.600 ","End":"07:50.450","Text":"getCount, which belongs to Kangaroo,"},{"Start":"07:50.450 ","End":"07:52.700","Text":"so the syntax\u0027s difference could be referring to"},{"Start":"07:52.700 ","End":"07:56.510","Text":"the class name rather than the object names and we\u0027re able"},{"Start":"07:56.510 ","End":"08:02.720","Text":"to keep track because we had a static variable inside that class definition as well."},{"Start":"08:02.720 ","End":"08:05.120","Text":"It meant that we\u0027re keeping track of"},{"Start":"08:05.120 ","End":"08:09.289","Text":"the total number of kangaroos because there is only 1 variable"},{"Start":"08:09.289 ","End":"08:13.025","Text":"that\u0027s called count and we get access to it"},{"Start":"08:13.025 ","End":"08:17.255","Text":"through this static method here called getCount."},{"Start":"08:17.255 ","End":"08:19.805","Text":"That\u0027s it for this one, nice and easy,"},{"Start":"08:19.805 ","End":"08:23.150","Text":"but it does tie together quite a lot of what we\u0027ve done in this chapter."},{"Start":"08:23.150 ","End":"08:26.160","Text":"Thanks very much for watching, and see you soon."}],"ID":31100}],"Thumbnail":null,"ID":294471},{"Name":"Inheritance","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"The Concept of Inheritance","Duration":"5m 48s","ChapterTopicVideoID":29475,"CourseChapterTopicPlaylistID":294470,"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":"Hello, Welcome to this video on inheritance."},{"Start":"00:03.540 ","End":"00:07.365","Text":"By the end of this section, you\u0027ll be able to explain the concept of inheritance,"},{"Start":"00:07.365 ","End":"00:09.990","Text":"create subclasses from a base class,"},{"Start":"00:09.990 ","End":"00:15.765","Text":"override inherited methods, and assess when overriding is appropriate."},{"Start":"00:15.765 ","End":"00:18.060","Text":"We\u0027ve now seen the relationship between"},{"Start":"00:18.060 ","End":"00:20.550","Text":"classes and objects and I\u0027m beginning to get feel for how"},{"Start":"00:20.550 ","End":"00:22.680","Text":"these concepts might help us model"},{"Start":"00:22.680 ","End":"00:26.355","Text":"some problem that we want to represent in a computer program."},{"Start":"00:26.355 ","End":"00:30.585","Text":"Classes let us create objects which retain their own data,"},{"Start":"00:30.585 ","End":"00:33.240","Text":"but can all behave in similar ways through"},{"Start":"00:33.240 ","End":"00:36.780","Text":"the behaviors that have been implemented in the class."},{"Start":"00:36.780 ","End":"00:39.155","Text":"This particular Kangaroo is green,"},{"Start":"00:39.155 ","End":"00:42.335","Text":"but another might be red and have a different size,"},{"Start":"00:42.335 ","End":"00:44.825","Text":"but both have the ability to jump."},{"Start":"00:44.825 ","End":"00:46.190","Text":"This ability to produce"},{"Start":"00:46.190 ","End":"00:50.839","Text":"multiple independent objects from the same template class is useful."},{"Start":"00:50.839 ","End":"00:54.710","Text":"But it would be nice if we could customize one of the objects to have"},{"Start":"00:54.710 ","End":"00:59.315","Text":"some additional attributes and methods that the other object doesn\u0027t have."},{"Start":"00:59.315 ","End":"01:01.775","Text":"We call this specializing."},{"Start":"01:01.775 ","End":"01:04.130","Text":"Looking at the example of the Kangaroo, again,"},{"Start":"01:04.130 ","End":"01:07.940","Text":"a Kangaroo is a specialization of a marsupial."},{"Start":"01:07.940 ","End":"01:10.970","Text":"In other words, it\u0027s a type of marsupial,"},{"Start":"01:10.970 ","End":"01:13.100","Text":"which itself is a type of mammal,"},{"Start":"01:13.100 ","End":"01:15.035","Text":"which is a type of animal."},{"Start":"01:15.035 ","End":"01:17.900","Text":"Although it has some characteristics in common with"},{"Start":"01:17.900 ","End":"01:21.530","Text":"other marsupials namely it keeps its newborns in a pouch,"},{"Start":"01:21.530 ","End":"01:25.475","Text":"a Kangaroo is also very different to other marsupials."},{"Start":"01:25.475 ","End":"01:29.300","Text":"One of its specializations is it grows very large."},{"Start":"01:29.300 ","End":"01:32.180","Text":"The other is its impressive jumping ability."},{"Start":"01:32.180 ","End":"01:35.690","Text":"A Kangaroo is clearly very different to a Koala,"},{"Start":"01:35.690 ","End":"01:38.135","Text":"even though both are marsupials."},{"Start":"01:38.135 ","End":"01:40.685","Text":"Far back in evolutionary time,"},{"Start":"01:40.685 ","End":"01:43.985","Text":"they would have had a common marsupial ancestor."},{"Start":"01:43.985 ","End":"01:50.210","Text":"From this idea, we get the concept in object-oriented programming of inheritance."},{"Start":"01:50.210 ","End":"01:53.195","Text":"Inheritance involves a hierarchy of inheritance,"},{"Start":"01:53.195 ","End":"01:54.695","Text":"just like this diagram."},{"Start":"01:54.695 ","End":"01:57.425","Text":"Getting more and more specialists as we move"},{"Start":"01:57.425 ","End":"02:00.985","Text":"down the levels until we can specialize no further."},{"Start":"02:00.985 ","End":"02:04.880","Text":"Using inheritance mechanism in object-oriented programming,"},{"Start":"02:04.880 ","End":"02:08.960","Text":"we can take a class and produce from it a child class,"},{"Start":"02:08.960 ","End":"02:13.535","Text":"which inherits all the attributes and methods of the parent class."},{"Start":"02:13.535 ","End":"02:17.120","Text":"If the parent class has 3 attributes and 5 methods,"},{"Start":"02:17.120 ","End":"02:21.170","Text":"that child class will have those same attributes and methods as well."},{"Start":"02:21.170 ","End":"02:23.540","Text":"But the child class could also have"},{"Start":"02:23.540 ","End":"02:28.490","Text":"some additional attributes or methods that don\u0027t exist in the parent class."},{"Start":"02:28.490 ","End":"02:31.880","Text":"Likewise, another different child class,"},{"Start":"02:31.880 ","End":"02:33.200","Text":"say the one in green,"},{"Start":"02:33.200 ","End":"02:36.680","Text":"could also inherit from the same parent class and add"},{"Start":"02:36.680 ","End":"02:42.200","Text":"its own additional methods or attributes that don\u0027t exist in the parent class."},{"Start":"02:42.200 ","End":"02:47.600","Text":"It now varies from both the parent class and its sibling class."},{"Start":"02:47.600 ","End":"02:49.460","Text":"Human beings, of course,"},{"Start":"02:49.460 ","End":"02:51.290","Text":"inherit from 2 parents,"},{"Start":"02:51.290 ","End":"02:54.125","Text":"something which is called multiple inheritance."},{"Start":"02:54.125 ","End":"02:58.430","Text":"Such a mechanism also exists in object-oriented."},{"Start":"02:58.430 ","End":"03:01.700","Text":"Multiple inheritance means that a child class"},{"Start":"03:01.700 ","End":"03:06.095","Text":"inherits all the methods and attributes for more than one class."},{"Start":"03:06.095 ","End":"03:09.815","Text":"All the methods and attributes here in parent class 1"},{"Start":"03:09.815 ","End":"03:14.555","Text":"and parent class 2 will be inherited by the child class."},{"Start":"03:14.555 ","End":"03:19.070","Text":"In practice, multiple inheritance is not often used,"},{"Start":"03:19.070 ","End":"03:22.315","Text":"and in fact, some languages don\u0027t even support it."},{"Start":"03:22.315 ","End":"03:27.970","Text":"However, it\u0027s possible to have multiple levels of inheritance within a hierarchy."},{"Start":"03:27.970 ","End":"03:32.170","Text":"Here, child class 3 inherits from child class 1,"},{"Start":"03:32.170 ","End":"03:35.180","Text":"which in turn inherits from the parent class."},{"Start":"03:35.180 ","End":"03:38.520","Text":"Likewise, child class 4 inherits from"},{"Start":"03:38.520 ","End":"03:43.305","Text":"child class 2 which also inherits from the parent class."},{"Start":"03:43.305 ","End":"03:46.510","Text":"Child classes 3 and 4 will have some traits in"},{"Start":"03:46.510 ","End":"03:51.340","Text":"common since they inherit them both from the red parent class."},{"Start":"03:51.340 ","End":"03:53.530","Text":"But that also differ based on"},{"Start":"03:53.530 ","End":"03:59.090","Text":"whatever additional attributes and methods were added in child class 1 and 2."},{"Start":"03:59.090 ","End":"04:06.310","Text":"You can think of child 3 and 4 as cousins who have parents that are siblings,"},{"Start":"04:06.310 ","End":"04:08.585","Text":"and therefore share a grandparent."},{"Start":"04:08.585 ","End":"04:13.655","Text":"Because the idea of parent and child is really easy to grasp and understand,"},{"Start":"04:13.655 ","End":"04:18.050","Text":"it\u0027d be tempting to use the word grandchild class for these ones at the bottom."},{"Start":"04:18.050 ","End":"04:21.080","Text":"But that can get quickly out of hand as we keep going down"},{"Start":"04:21.080 ","End":"04:25.025","Text":"the hierarchy as we\u0027d need great-grandchildren and so on."},{"Start":"04:25.025 ","End":"04:31.565","Text":"Instead, we tend to use the words base class and subclass to keep the language simple."},{"Start":"04:31.565 ","End":"04:35.320","Text":"The equivalent using that terminology would be"},{"Start":"04:35.320 ","End":"04:40.815","Text":"base class from which subclass 1 and subclass 2 inherit."},{"Start":"04:40.815 ","End":"04:44.710","Text":"Subclass 3 inherits from subclass 1,"},{"Start":"04:44.710 ","End":"04:48.055","Text":"and therefore can be said to be a descendant class."},{"Start":"04:48.055 ","End":"04:52.755","Text":"Likewise, subclass 4 inherits from subclass 2."},{"Start":"04:52.755 ","End":"04:59.085","Text":"We can therefore say that subclass 2 is an ancestor class to subclass 4."},{"Start":"04:59.085 ","End":"05:03.620","Text":"One final variation you might come across is rather than saying base class,"},{"Start":"05:03.620 ","End":"05:08.795","Text":"we could instead use the word superclass to refer to a base class."},{"Start":"05:08.795 ","End":"05:16.405","Text":"Let\u0027s round up all those terms together to clarify."},{"Start":"05:16.405 ","End":"05:19.520","Text":"Base class, parent class, superclass,"},{"Start":"05:19.520 ","End":"05:26.705","Text":"ancestor class mean a class from which attributes and behaviors are inherited."},{"Start":"05:26.705 ","End":"05:30.770","Text":"Subclass, child class, descendant class,"},{"Start":"05:30.770 ","End":"05:36.635","Text":"refer to a class which inherits attributes and behaviors from another class."},{"Start":"05:36.635 ","End":"05:39.245","Text":"We\u0027ll pause there and in the next video,"},{"Start":"05:39.245 ","End":"05:42.290","Text":"look at the detail of how inheritance works using"},{"Start":"05:42.290 ","End":"05:45.485","Text":"examples in UML and in a high-level language."},{"Start":"05:45.485 ","End":"05:49.260","Text":"Thanks very much for watching and see you shortly."}],"ID":31081},{"Watched":false,"Name":"Inheritance Example","Duration":"6m 41s","ChapterTopicVideoID":29474,"CourseChapterTopicPlaylistID":294470,"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.070","Text":"Hello, welcome back."},{"Start":"00:02.070 ","End":"00:05.730","Text":"UML notation can not only show the details of a class,"},{"Start":"00:05.730 ","End":"00:09.465","Text":"but can also show the relationships between classes."},{"Start":"00:09.465 ","End":"00:14.895","Text":"Aligned with an empty arrowhead at the end is showing an inheritance relationship."},{"Start":"00:14.895 ","End":"00:18.030","Text":"It\u0027s saying that the class being pointed to is"},{"Start":"00:18.030 ","End":"00:24.255","Text":"an ancestor class from which the descendant class inherits attributes and behaviors."},{"Start":"00:24.255 ","End":"00:27.660","Text":"In this example, we\u0027re model-ling a computer game in"},{"Start":"00:27.660 ","End":"00:31.605","Text":"which there are a number of different types of vehicle available to a player."},{"Start":"00:31.605 ","End":"00:35.640","Text":"The base class is called Vehicle and 2 subclasses,"},{"Start":"00:35.640 ","End":"00:39.775","Text":"Car and Boat, are inheriting from the vehicle class."},{"Start":"00:39.775 ","End":"00:45.710","Text":"We\u0027re saying in this diagram that car is a type of vehicle and boat is a type of vehicle,"},{"Start":"00:45.710 ","End":"00:50.165","Text":"but a car class is not the same as a boat class."},{"Start":"00:50.165 ","End":"00:53.480","Text":"However, even though a car is not the same as a boat,"},{"Start":"00:53.480 ","End":"00:56.210","Text":"the vehicle has a number of attributes that\u0027ll be"},{"Start":"00:56.210 ","End":"01:00.755","Text":"inherited by the car and boat subclasses."},{"Start":"01:00.755 ","End":"01:05.990","Text":"These attributes have been put here because they are common to all types of vehicle."},{"Start":"01:05.990 ","End":"01:11.000","Text":"Again, every vehicle has a length, a color,"},{"Start":"01:11.000 ","End":"01:13.610","Text":"a damage indicator to still have damaged"},{"Start":"01:13.610 ","End":"01:15.950","Text":"the vehicle is when it\u0027s been involved in collisions and so"},{"Start":"01:15.950 ","End":"01:21.070","Text":"on and a speed attribute to say how fast the vehicle is moving."},{"Start":"01:21.070 ","End":"01:24.680","Text":"Notice we don\u0027t see a plus or minus sign next to"},{"Start":"01:24.680 ","End":"01:28.465","Text":"the attributes as we\u0027ve seen previously but hash symbol."},{"Start":"01:28.465 ","End":"01:32.000","Text":"This is an indicator in UML that an attribute or behavior is"},{"Start":"01:32.000 ","End":"01:37.175","Text":"protected as halfway house between private and public."},{"Start":"01:37.175 ","End":"01:41.345","Text":"Protected attributes can be accessed and modified by subclasses,"},{"Start":"01:41.345 ","End":"01:46.129","Text":"but not by external classes outside the inheritance hierarchy."},{"Start":"01:46.129 ","End":"01:49.535","Text":"The vehicle base class also has methods,"},{"Start":"01:49.535 ","End":"01:51.680","Text":"all of which have been marked public,"},{"Start":"01:51.680 ","End":"01:56.725","Text":"so they\u0027ll also be available in the subclasses and will be public there too."},{"Start":"01:56.725 ","End":"01:59.800","Text":"The first is a constructor."},{"Start":"01:59.800 ","End":"02:03.590","Text":"It\u0027s used to set up the length and color of the vehicle."},{"Start":"02:03.590 ","End":"02:07.370","Text":"All vehicles will need their length and colors set up when they\u0027re created."},{"Start":"02:07.370 ","End":"02:09.740","Text":"It makes sense to put this here."},{"Start":"02:09.740 ","End":"02:12.140","Text":"All vehicles will also need to move,"},{"Start":"02:12.140 ","End":"02:14.270","Text":"so movement has been provided."},{"Start":"02:14.270 ","End":"02:17.230","Text":"All vehicles will collide with other objects."},{"Start":"02:17.230 ","End":"02:19.940","Text":"It makes sense to put collide in here."},{"Start":"02:19.940 ","End":"02:24.680","Text":"Similarly, repairs needs to take place and"},{"Start":"02:24.680 ","End":"02:30.350","Text":"the vehicle might need to increase its speed to make it go faster."},{"Start":"02:30.350 ","End":"02:34.850","Text":"Where it gets interesting is the implementation of the subclasses."},{"Start":"02:34.850 ","End":"02:36.950","Text":"The car class, as we know,"},{"Start":"02:36.950 ","End":"02:40.190","Text":"will inherit all 4 attributes from the vehicle class."},{"Start":"02:40.190 ","End":"02:44.435","Text":"But we\u0027ll also have an additional attribute called automatic."},{"Start":"02:44.435 ","End":"02:48.680","Text":"This is used to store whether the vehicle has an automatic or manual gearbox."},{"Start":"02:48.680 ","End":"02:51.080","Text":"Not all vehicles need to have this attribute,"},{"Start":"02:51.080 ","End":"02:54.170","Text":"so it makes sense not to store it in the base class."},{"Start":"02:54.170 ","End":"02:56.900","Text":"This is an example of us specializing."},{"Start":"02:56.900 ","End":"03:00.020","Text":"We have 1 slight customization which makes"},{"Start":"03:00.020 ","End":"03:04.445","Text":"the car class different to both vehicle and boat."},{"Start":"03:04.445 ","End":"03:07.490","Text":"The boat class also has an extra attribute,"},{"Start":"03:07.490 ","End":"03:12.245","Text":"sales, which stores the number of sales that the boat has."},{"Start":"03:12.245 ","End":"03:15.350","Text":"Clearly, not all types of vehicle would have a sale."},{"Start":"03:15.350 ","End":"03:19.100","Text":"It makes sense again to not put this in the base class,"},{"Start":"03:19.100 ","End":"03:23.435","Text":"but to create a new customized class boat which contains this attribute,"},{"Start":"03:23.435 ","End":"03:27.145","Text":"as well as all the other attributes in the vehicle class."},{"Start":"03:27.145 ","End":"03:30.730","Text":"The subclasses may still need to have their own constructors."},{"Start":"03:30.730 ","End":"03:32.230","Text":"In the case of the car,"},{"Start":"03:32.230 ","End":"03:37.705","Text":"it\u0027s constructor would need to set up the value of the automatic attribute."},{"Start":"03:37.705 ","End":"03:39.445","Text":"In the case of the boat,"},{"Start":"03:39.445 ","End":"03:42.459","Text":"it\u0027s constructor would need to set the number of sales."},{"Start":"03:42.459 ","End":"03:44.345","Text":"In both cases,"},{"Start":"03:44.345 ","End":"03:48.250","Text":"each subclass would also be responsible for calling the constructor in"},{"Start":"03:48.250 ","End":"03:53.005","Text":"the base class and would need to pass on any arguments that it needs."},{"Start":"03:53.005 ","End":"03:57.370","Text":"In this case, to set up the length and color attributes."},{"Start":"03:57.370 ","End":"04:02.994","Text":"Let\u0027s now look at how this might work in a typical high-level language."},{"Start":"04:02.994 ","End":"04:09.720","Text":"In Java, the keyword extends is used to facilitate inheritance."},{"Start":"04:09.720 ","End":"04:12.455","Text":"The subclass name is given first,"},{"Start":"04:12.455 ","End":"04:18.745","Text":"then the keyword extends and then the name of the parent class being inherited from."},{"Start":"04:18.745 ","End":"04:22.160","Text":"Inside the class definition we can add"},{"Start":"04:22.160 ","End":"04:25.220","Text":"any new attributes and method this class is to have."},{"Start":"04:25.220 ","End":"04:26.660","Text":"In our car example,"},{"Start":"04:26.660 ","End":"04:29.510","Text":"we needed an extra attribute to determine whether the"},{"Start":"04:29.510 ","End":"04:33.245","Text":"car\u0027s automatic or not so we\u0027ve defined it here."},{"Start":"04:33.245 ","End":"04:38.135","Text":"Any new methods to be added into car would then follow below."},{"Start":"04:38.135 ","End":"04:41.030","Text":"Basically, this class looks like any other class,"},{"Start":"04:41.030 ","End":"04:43.970","Text":"but the extends keyword means it"},{"Start":"04:43.970 ","End":"04:47.764","Text":"magically acquires all the attributes and methods in vehicle,"},{"Start":"04:47.764 ","End":"04:52.630","Text":"as well as having anything defined inside the car class itself."},{"Start":"04:52.630 ","End":"04:55.685","Text":"Building on this same example in Java,"},{"Start":"04:55.685 ","End":"04:59.735","Text":"we can add a constructor method to the car class."},{"Start":"04:59.735 ","End":"05:02.900","Text":"This constructor can make a call to the constructor of"},{"Start":"05:02.900 ","End":"05:07.054","Text":"the base class using the super keyword,"},{"Start":"05:07.054 ","End":"05:12.125","Text":"which has to be the first line inside this subclass constructor."},{"Start":"05:12.125 ","End":"05:15.770","Text":"Super can be used to pass on the 2 arguments,"},{"Start":"05:15.770 ","End":"05:18.125","Text":"passed in to parameters,"},{"Start":"05:18.125 ","End":"05:23.000","Text":"length and color to the constructor of vehicle."},{"Start":"05:23.000 ","End":"05:27.710","Text":"Vehicles constructor will then take care of storing away those values."},{"Start":"05:27.710 ","End":"05:30.140","Text":"In the second line here,"},{"Start":"05:30.140 ","End":"05:35.510","Text":"the value being passed into the parameter automatic is stored away"},{"Start":"05:35.510 ","End":"05:41.300","Text":"instead in an instance variable in the object being created from the car class."},{"Start":"05:41.300 ","End":"05:43.520","Text":"Because we\u0027ve used the same name for"},{"Start":"05:43.520 ","End":"05:47.525","Text":"the parameter as we have for this instance variable,"},{"Start":"05:47.525 ","End":"05:52.595","Text":"we use this to distinguish between the 2 identifiers."},{"Start":"05:52.595 ","End":"05:55.160","Text":"Going back to our UML diagram,"},{"Start":"05:55.160 ","End":"05:58.535","Text":"any new methods present in the child class are listed"},{"Start":"05:58.535 ","End":"06:02.465","Text":"in the bottom rectangle for each child class."},{"Start":"06:02.465 ","End":"06:06.080","Text":"The car might have a method to engage"},{"Start":"06:06.080 ","End":"06:10.325","Text":"the forward gear and another method to engage reverse."},{"Start":"06:10.325 ","End":"06:15.335","Text":"Similarly, the boat might need a method that raises the sails."},{"Start":"06:15.335 ","End":"06:19.505","Text":"These methods only belong to these specific subclasses."},{"Start":"06:19.505 ","End":"06:24.275","Text":"Even though a boat and a car have many similarities that they all inherit from vehicle,"},{"Start":"06:24.275 ","End":"06:29.705","Text":"only a boat can raise a sale and only a car can go into reverse gear."},{"Start":"06:29.705 ","End":"06:32.540","Text":"We\u0027ll pause there and in the next video,"},{"Start":"06:32.540 ","End":"06:37.025","Text":"we\u0027ll look at a special type of method called an overwritten method."},{"Start":"06:37.025 ","End":"06:40.620","Text":"See you soon for that 1. Thanks for watching."}],"ID":31082},{"Watched":false,"Name":"Overriding Edits","Duration":"9m 22s","ChapterTopicVideoID":29473,"CourseChapterTopicPlaylistID":294470,"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.640","Text":"Hello, welcome back. In the previous video,"},{"Start":"00:02.640 ","End":"00:07.065","Text":"we saw how a class can inherit attributes and methods from a parent class."},{"Start":"00:07.065 ","End":"00:12.000","Text":"It can then add its own methods and attributes that only exist in the subclass."},{"Start":"00:12.000 ","End":"00:15.000","Text":"Here the car class extends what it inherits from"},{"Start":"00:15.000 ","End":"00:18.855","Text":"the vehicle class by adding an automatic attribute,"},{"Start":"00:18.855 ","End":"00:21.900","Text":"and 2 new methods forward and reverse."},{"Start":"00:21.900 ","End":"00:24.705","Text":"Similarly, boat adds and attributes sales,"},{"Start":"00:24.705 ","End":"00:26.895","Text":"and method raise sales."},{"Start":"00:26.895 ","End":"00:30.660","Text":"Both subclasses have their own constructor methods too."},{"Start":"00:30.660 ","End":"00:34.685","Text":"An alternative to adding totally new methods in the subclasses"},{"Start":"00:34.685 ","End":"00:38.825","Text":"is to add methods which replace methods in the base class."},{"Start":"00:38.825 ","End":"00:41.060","Text":"Notice that in this particular case,"},{"Start":"00:41.060 ","End":"00:46.955","Text":"the 2 new methods declared in the subclasses already exist in the base class."},{"Start":"00:46.955 ","End":"00:51.905","Text":"This is a particular form of specialization known as method overriding."},{"Start":"00:51.905 ","End":"00:55.460","Text":"The method move inside vehicle is being"},{"Start":"00:55.460 ","End":"00:59.510","Text":"replaced inside car with a different implementation."},{"Start":"00:59.510 ","End":"01:04.310","Text":"This allows a car object to behave differently to a generic vehicle object."},{"Start":"01:04.310 ","End":"01:07.070","Text":"Cars have a particular physics around how they move,"},{"Start":"01:07.070 ","End":"01:08.660","Text":"including how they corner,"},{"Start":"01:08.660 ","End":"01:12.290","Text":"how they respond to the terrain they\u0027re driving on, and so on."},{"Start":"01:12.290 ","End":"01:16.430","Text":"Similarly, a boat also moves differently to a generic vehicle."},{"Start":"01:16.430 ","End":"01:19.250","Text":"For one thing, it can\u0027t move at all unless it\u0027s on water,"},{"Start":"01:19.250 ","End":"01:23.315","Text":"and then the movement itself might be affected by the state of the water."},{"Start":"01:23.315 ","End":"01:26.686","Text":"For example, are there waves or is the water calm?"},{"Start":"01:26.686 ","End":"01:30.530","Text":"It makes it a lot of sense to replace some of the methods we\u0027ve inherited,"},{"Start":"01:30.530 ","End":"01:35.945","Text":"and to replace them with more specialized ones more appropriate to the subclass."},{"Start":"01:35.945 ","End":"01:41.390","Text":"To do so, we simply define the method again in the subclass."},{"Start":"01:41.390 ","End":"01:44.570","Text":"But to do that, we\u0027ve got to be careful to make sure that"},{"Start":"01:44.570 ","End":"01:50.015","Text":"the method signature is the same as the method we\u0027re inheriting from the base class."},{"Start":"01:50.015 ","End":"01:52.205","Text":"A method\u0027s signature, remember,"},{"Start":"01:52.205 ","End":"01:54.290","Text":"is the name of a method,"},{"Start":"01:54.290 ","End":"01:56.285","Text":"the number of parameters,"},{"Start":"01:56.285 ","End":"01:58.535","Text":"and the types of those parameters."},{"Start":"01:58.535 ","End":"02:05.780","Text":"In this case, our move method in the car class doesn\u0027t have any parameters,"},{"Start":"02:05.780 ","End":"02:10.040","Text":"which is the same as the inherited method in the vehicle class,"},{"Start":"02:10.040 ","End":"02:12.260","Text":"so the signature does match,"},{"Start":"02:12.260 ","End":"02:14.495","Text":"and an override can happen."},{"Start":"02:14.495 ","End":"02:17.465","Text":"Looking at the same example as source code,"},{"Start":"02:17.465 ","End":"02:22.595","Text":"we just put our new method into the subclass just as we would for any subclass method."},{"Start":"02:22.595 ","End":"02:25.790","Text":"Anytime we call move on car objects, now,"},{"Start":"02:25.790 ","End":"02:29.315","Text":"we\u0027re not calling the move method inherited from the base class,"},{"Start":"02:29.315 ","End":"02:32.435","Text":"but the move method defined here."},{"Start":"02:32.435 ","End":"02:35.470","Text":"An optional annotation can be used that will cause"},{"Start":"02:35.470 ","End":"02:38.995","Text":"a compiler error if the intention was to override."},{"Start":"02:38.995 ","End":"02:41.590","Text":"But the signature of the method being defined doesn\u0027t"},{"Start":"02:41.590 ","End":"02:44.815","Text":"match any method in an ancestor class."},{"Start":"02:44.815 ","End":"02:48.070","Text":"It\u0027s good practice to use it as it makes it clear for"},{"Start":"02:48.070 ","End":"02:51.790","Text":"someone reading the code that this is an overridden method."},{"Start":"02:51.790 ","End":"02:54.355","Text":"But just to reiterate, it\u0027s optional."},{"Start":"02:54.355 ","End":"02:56.530","Text":"The override will happen anyway,"},{"Start":"02:56.530 ","End":"03:00.150","Text":"even if at override is emitted,"},{"Start":"03:00.150 ","End":"03:03.290","Text":"so long as these signatures match."},{"Start":"03:03.390 ","End":"03:10.555","Text":"In Java, every class automatically inherits from an ultimate superclass called object."},{"Start":"03:10.555 ","End":"03:13.015","Text":"Object has a number of methods that can be"},{"Start":"03:13.015 ","End":"03:16.850","Text":"overwritten and often are in order to make them useful."},{"Start":"03:16.850 ","End":"03:21.320","Text":"For example, equals is used to compare one object to another."},{"Start":"03:21.320 ","End":"03:24.590","Text":"The default implementation for equals will just compare"},{"Start":"03:24.590 ","End":"03:28.145","Text":"2 objects to determine whether they\u0027re the same object,"},{"Start":"03:28.145 ","End":"03:31.535","Text":"or if they live in the same memory address."},{"Start":"03:31.535 ","End":"03:36.140","Text":"In this case, equals would return true because my Object 1,"},{"Start":"03:36.140 ","End":"03:42.125","Text":"and Object 2 refer to the same memory location where the object has been stored."},{"Start":"03:42.125 ","End":"03:45.260","Text":"But what you usually want to know is due to"},{"Start":"03:45.260 ","End":"03:48.800","Text":"particular objects contain the same data values."},{"Start":"03:48.800 ","End":"03:54.740","Text":"Say we had 2 completely different objects stored in different memory locations."},{"Start":"03:54.740 ","End":"03:57.170","Text":"We might want to check that some of the attributes"},{"Start":"03:57.170 ","End":"04:00.305","Text":"in one of those objects match the other."},{"Start":"04:00.305 ","End":"04:04.730","Text":"To do that, you will need to override the default equals method"},{"Start":"04:04.730 ","End":"04:08.795","Text":"that every object will have inherited from the object superclass,"},{"Start":"04:08.795 ","End":"04:12.425","Text":"and provide a suitable replacement in the subclass."},{"Start":"04:12.425 ","End":"04:16.879","Text":"Let\u0027s consider an example where we\u0027ve modeled a house in a class."},{"Start":"04:16.879 ","End":"04:19.730","Text":"The class has an attribute for the door number,"},{"Start":"04:19.730 ","End":"04:21.695","Text":"and another for the postcode."},{"Start":"04:21.695 ","End":"04:23.300","Text":"With these 2 items,"},{"Start":"04:23.300 ","End":"04:25.850","Text":"we can identify a particular house,"},{"Start":"04:25.850 ","End":"04:28.550","Text":"and compare it to any other houses we might be handling in"},{"Start":"04:28.550 ","End":"04:32.533","Text":"our program to see whether they have a match."},{"Start":"04:32.533 ","End":"04:35.240","Text":"In our overridden equals method here,"},{"Start":"04:35.240 ","End":"04:39.410","Text":"we\u0027re comparing the attributes of the object being passed in,"},{"Start":"04:39.410 ","End":"04:43.160","Text":"and the attributes of this particular object."},{"Start":"04:43.160 ","End":"04:47.180","Text":"There is a slight complication because we have to have"},{"Start":"04:47.180 ","End":"04:53.370","Text":"a method signature for equals that has a parameter of type object."},{"Start":"04:53.830 ","End":"04:58.048","Text":"Because the class we\u0027re dealing with is actually a house,"},{"Start":"04:58.048 ","End":"05:02.880","Text":"we\u0027re forced to declare a temporary house object"},{"Start":"05:02.880 ","End":"05:08.330","Text":"and then we cast the object passed in into a house object."},{"Start":"05:08.330 ","End":"05:11.526","Text":"It\u0027s a bit awkward, but it\u0027s necessary."},{"Start":"05:11.526 ","End":"05:15.230","Text":"The signature matches and we can override"},{"Start":"05:15.230 ","End":"05:20.645","Text":"the default equals method inherited from a superclass."},{"Start":"05:20.645 ","End":"05:23.300","Text":"Another method inherited from objects,"},{"Start":"05:23.300 ","End":"05:27.350","Text":"and very often overridden in subclasses is toString."},{"Start":"05:27.350 ","End":"05:33.065","Text":"toString is supposed to return a string representation of the object."},{"Start":"05:33.065 ","End":"05:35.555","Text":"It\u0027s up to you to decide what that means."},{"Start":"05:35.555 ","End":"05:39.860","Text":"But in this example, it will probably make sense to return the door number followed"},{"Start":"05:39.860 ","End":"05:44.720","Text":"by the postcode with some literal strings in-between explaining what\u0027s, what."},{"Start":"05:44.720 ","End":"05:47.660","Text":"The default toString method would generally"},{"Start":"05:47.660 ","End":"05:50.405","Text":"return the address that the object\u0027s stored at,"},{"Start":"05:50.405 ","End":"05:52.520","Text":"and the name of the class."},{"Start":"05:52.520 ","End":"05:56.330","Text":"That\u0027s not particularly useful so it\u0027s fairly obvious why it\u0027s so"},{"Start":"05:56.330 ","End":"06:00.935","Text":"common to override the toString method in our subclasses."},{"Start":"06:00.935 ","End":"06:03.320","Text":"A question you might be asking is,"},{"Start":"06:03.320 ","End":"06:08.574","Text":"is it still possible to access the method in the superclass that you\u0027ve overridden?"},{"Start":"06:08.574 ","End":"06:10.515","Text":"The answer is yes,"},{"Start":"06:10.515 ","End":"06:12.350","Text":"and the mechanism for it is"},{"Start":"06:12.350 ","End":"06:17.900","Text":"the super keyword which we came across when dealing with constructors."},{"Start":"06:17.900 ","End":"06:20.705","Text":"In this example, we\u0027re using a base class called"},{"Start":"06:20.705 ","End":"06:24.710","Text":"item to model items we have for sale in an online store."},{"Start":"06:24.710 ","End":"06:29.795","Text":"Maybe one specific item we sell that we need a specialist class for is a pen."},{"Start":"06:29.795 ","End":"06:32.270","Text":"Pens can be of many different colors,"},{"Start":"06:32.270 ","End":"06:35.720","Text":"and can either be normal pens or retractable pens."},{"Start":"06:35.720 ","End":"06:39.110","Text":"Details of any item can be displayed on the screen using"},{"Start":"06:39.110 ","End":"06:43.580","Text":"the display details method in the base class."},{"Start":"06:43.580 ","End":"06:49.880","Text":"Here you can see the overridden display details method in the pen subclass,"},{"Start":"06:49.880 ","End":"06:56.794","Text":"and this line here makes the call to display details in the item class."},{"Start":"06:56.794 ","End":"07:03.005","Text":"The final question you might have is when might it be appropriate to use overriding?"},{"Start":"07:03.005 ","End":"07:06.200","Text":"If you\u0027re specializing a class using inheritance,"},{"Start":"07:06.200 ","End":"07:10.160","Text":"it could involve just adding another attribute in the subclass."},{"Start":"07:10.160 ","End":"07:15.430","Text":"That doesn\u0027t need overriding as we\u0027re just adding another piece of state information."},{"Start":"07:15.430 ","End":"07:19.370","Text":"When you want to specialize the behavior in your class,"},{"Start":"07:19.370 ","End":"07:22.115","Text":"you may need to override."},{"Start":"07:22.115 ","End":"07:24.530","Text":"Overloading, where you provide"},{"Start":"07:24.530 ","End":"07:28.265","Text":"multiple different versions of a method with the same name,"},{"Start":"07:28.265 ","End":"07:33.980","Text":"but different signatures can provide a mechanism for specializing behavior."},{"Start":"07:33.980 ","End":"07:36.860","Text":"But it\u0027s really only appropriate where there might be"},{"Start":"07:36.860 ","End":"07:40.220","Text":"different types of data that we\u0027re passing into an object."},{"Start":"07:40.220 ","End":"07:43.250","Text":"For example, when passing 2 integers to a method,"},{"Start":"07:43.250 ","End":"07:44.495","Text":"we might do one thing."},{"Start":"07:44.495 ","End":"07:46.130","Text":"When passing into floats,"},{"Start":"07:46.130 ","End":"07:48.350","Text":"we might do something slightly different."},{"Start":"07:48.350 ","End":"07:51.968","Text":"That\u0027s quite a restrictive form of specializing."},{"Start":"07:51.968 ","End":"07:53.375","Text":"When that\u0027s not enough,"},{"Start":"07:53.375 ","End":"07:56.747","Text":"and the behavior needs specializing,"},{"Start":"07:56.747 ","End":"08:00.650","Text":"overriding will allow behavior to be specialized,"},{"Start":"08:00.650 ","End":"08:06.095","Text":"but does have the restriction that it needs to happen within an inheritance hierarchy."},{"Start":"08:06.095 ","End":"08:09.589","Text":"Overloading doesn\u0027t have that restriction."},{"Start":"08:09.589 ","End":"08:16.385","Text":"One final consideration is that overriding has one major advantage over overloading."},{"Start":"08:16.385 ","End":"08:22.145","Text":"With overridden methods, behavior can change whilst the program\u0027s running."},{"Start":"08:22.145 ","End":"08:25.490","Text":"With overloading, you\u0027ve made the decision about how"},{"Start":"08:25.490 ","End":"08:29.915","Text":"the program is going to work at the time you compile the program."},{"Start":"08:29.915 ","End":"08:34.760","Text":"Overriding can provide more flexibility by delaying the decision about"},{"Start":"08:34.760 ","End":"08:39.800","Text":"which method is going to be executed to the actual time of execution."},{"Start":"08:39.800 ","End":"08:44.920","Text":"The decision is made by the runtime environment whilst the program is running,"},{"Start":"08:44.920 ","End":"08:49.130","Text":"and could be as a result of user actions."},{"Start":"08:49.130 ","End":"08:51.915","Text":"This is quite a powerful mechanism,"},{"Start":"08:51.915 ","End":"08:57.355","Text":"and we\u0027ll look at the details of this in the next topic; polymorphism."},{"Start":"08:57.355 ","End":"09:01.959","Text":"In this section, we learned how to explain the concept of inheritance,"},{"Start":"09:01.959 ","End":"09:04.960","Text":"to create subclasses from base class,"},{"Start":"09:04.960 ","End":"09:11.080","Text":"override inherited methods, and assess when overriding is appropriate."},{"Start":"09:11.080 ","End":"09:13.435","Text":"Thanks for watching, and as ever,"},{"Start":"09:13.435 ","End":"09:15.685","Text":"be sure to complete the exercises,"},{"Start":"09:15.685 ","End":"09:21.710","Text":"and watch the solution videos to embed what we\u0027ve covered here. See you next time."}],"ID":31083},{"Watched":false,"Name":"Exercise 1","Duration":"9m 31s","ChapterTopicVideoID":29472,"CourseChapterTopicPlaylistID":294470,"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.315","Text":"Hello. Welcome to this exercise on inheritance."},{"Start":"00:03.315 ","End":"00:08.340","Text":"We\u0027ve firstly been asked to create a new project and within a class called person."},{"Start":"00:08.340 ","End":"00:15.090","Text":"Then in part a, we\u0027re asked to create 3 private attributes: name, age, and birthplace."},{"Start":"00:15.090 ","End":"00:18.000","Text":"In part b, we\u0027re asked to create a constructor,"},{"Start":"00:18.000 ","End":"00:23.100","Text":"which sets the values of each of those attributes to the values passed in as arguments."},{"Start":"00:23.100 ","End":"00:26.780","Text":"In part c, we add a static main method to this class,"},{"Start":"00:26.780 ","End":"00:30.170","Text":"which creates a person object called person1,"},{"Start":"00:30.170 ","End":"00:32.270","Text":"using the name: Ray,"},{"Start":"00:32.270 ","End":"00:36.325","Text":"age: 78, and place of birth: London."},{"Start":"00:36.325 ","End":"00:39.740","Text":"In part d, we\u0027re told to put the second line of"},{"Start":"00:39.740 ","End":"00:45.310","Text":"the main method as system.out.println(person1)."},{"Start":"00:45.310 ","End":"00:48.140","Text":"In part e, we\u0027re told to run the main method"},{"Start":"00:48.140 ","End":"00:51.445","Text":"and note what appears in the terminal output."},{"Start":"00:51.445 ","End":"00:54.215","Text":"In part f, we should override"},{"Start":"00:54.215 ","End":"00:59.090","Text":"the toString method that this class automatically inherits from the object class."},{"Start":"00:59.090 ","End":"01:04.160","Text":"The method should return the attributes within the class as a concatenated string."},{"Start":"01:04.160 ","End":"01:06.335","Text":"For example, name: Ray,"},{"Start":"01:06.335 ","End":"01:09.140","Text":"age: 78, place of birth: London."},{"Start":"01:09.140 ","End":"01:11.390","Text":"Finally, in part g,"},{"Start":"01:11.390 ","End":"01:13.510","Text":"we\u0027re asked to run the main method again,"},{"Start":"01:13.510 ","End":"01:17.570","Text":"and we\u0027re asked what is printed and why."},{"Start":"01:17.570 ","End":"01:20.695","Text":"Having created the person class,"},{"Start":"01:20.695 ","End":"01:24.320","Text":"we can go ahead and do as part a asks us to do,"},{"Start":"01:24.320 ","End":"01:27.800","Text":"which is to create 3 private attributes: string,"},{"Start":"01:27.800 ","End":"01:28.940","Text":"age, and birthplace."},{"Start":"01:28.940 ","End":"01:31.220","Text":"We haven\u0027t been given data types for those,"},{"Start":"01:31.220 ","End":"01:36.890","Text":"but I guess it\u0027s fairly obvious that the name would have to be a string,"},{"Start":"01:36.890 ","End":"01:41.405","Text":"the age would have to be an int,"},{"Start":"01:41.405 ","End":"01:48.360","Text":"and birthplace would also have to be a string."},{"Start":"01:48.370 ","End":"01:52.865","Text":"Obviously, we\u0027ve marked all of these as private,"},{"Start":"01:52.865 ","End":"01:56.075","Text":"as we should, if we haven\u0027t been told anything else."},{"Start":"01:56.075 ","End":"01:58.850","Text":"We have actually been told to use private attributes here."},{"Start":"01:58.850 ","End":"02:02.254","Text":"In part b, we create a constructor."},{"Start":"02:02.254 ","End":"02:07.574","Text":"We\u0027re going to set each of these attributes the values that get parsed in as arguments."},{"Start":"02:07.574 ","End":"02:13.355","Text":"As public for the constructor, no return value."},{"Start":"02:13.355 ","End":"02:17.140","Text":"We don\u0027t need to put void or anything for the constructor."},{"Start":"02:17.140 ","End":"02:21.260","Text":"It has the same name as the class, which is person."},{"Start":"02:21.260 ","End":"02:25.096","Text":"We\u0027re going to get a string, which is going to be the name,"},{"Start":"02:25.096 ","End":"02:28.730","Text":"an int, which is going to be the age,"},{"Start":"02:28.730 ","End":"02:31.730","Text":"and the string, which is going to be the birthplace."},{"Start":"02:31.730 ","End":"02:34.580","Text":"Because we\u0027ve used the same names for"},{"Start":"02:34.580 ","End":"02:37.910","Text":"the parameters as we are for the instance variables,"},{"Start":"02:37.910 ","End":"02:42.945","Text":"we\u0027ll make use of this to distinguish between the 2."},{"Start":"02:42.945 ","End":"02:46.250","Text":"That\u0027s the name of the parameter, name."},{"Start":"02:46.250 ","End":"02:51.320","Text":"This says we\u0027re using an instance variable called age."},{"Start":"02:51.320 ","End":"02:54.150","Text":"We want to set it to the parameter age."},{"Start":"02:54.150 ","End":"02:58.890","Text":"Finally, the same for birthplace."},{"Start":"02:58.890 ","End":"03:02.645","Text":"That\u0027s the constructor done, part b."},{"Start":"03:02.645 ","End":"03:07.880","Text":"Now we came across main previously in previous topic."},{"Start":"03:07.880 ","End":"03:13.129","Text":"We know that main is the starting point for a program."},{"Start":"03:13.129 ","End":"03:15.199","Text":"It has a particular signature."},{"Start":"03:15.199 ","End":"03:19.025","Text":"It\u0027s public, it\u0027s a static method,"},{"Start":"03:19.025 ","End":"03:24.330","Text":"and it returns nothing called main with a lowercase"},{"Start":"03:24.330 ","End":"03:31.655","Text":"m. It accepts an array of strings,"},{"Start":"03:31.655 ","End":"03:33.575","Text":"which we can call it anything we like."},{"Start":"03:33.575 ","End":"03:36.035","Text":"Convention is to call it args."},{"Start":"03:36.035 ","End":"03:39.480","Text":"That\u0027s the method."},{"Start":"03:41.720 ","End":"03:44.560","Text":"Word method there as well. Don\u0027t need that."},{"Start":"03:44.560 ","End":"03:47.110","Text":"Public static, void, main, string, args,"},{"Start":"03:47.110 ","End":"03:51.190","Text":"that is the method that starts off a program,"},{"Start":"03:51.190 ","End":"03:52.900","Text":"if you want to create a stand-alone program,"},{"Start":"03:52.900 ","End":"03:57.960","Text":"which we\u0027ve been asked to create this method in this particular exercise."},{"Start":"03:57.960 ","End":"04:00.890","Text":"We\u0027ve gone ahead and done that."},{"Start":"04:00.890 ","End":"04:05.814","Text":"Inside here, we\u0027re going to create a person object called person1."},{"Start":"04:05.814 ","End":"04:09.125","Text":"We\u0027d been given some values."},{"Start":"04:09.125 ","End":"04:13.615","Text":"To create an object outside the workbench,"},{"Start":"04:13.615 ","End":"04:16.585","Text":"we say the name of the object type,"},{"Start":"04:16.585 ","End":"04:18.640","Text":"which in this case is person."},{"Start":"04:18.640 ","End":"04:22.025","Text":"We have an identifier for the object."},{"Start":"04:22.025 ","End":"04:26.570","Text":"Then we can create a new object from that class,"},{"Start":"04:26.570 ","End":"04:29.075","Text":"person, which we have to stay it again."},{"Start":"04:29.075 ","End":"04:32.490","Text":"Then we parse in the values that we want to work with."},{"Start":"04:32.490 ","End":"04:37.545","Text":"In this case, Ray, 78, London."},{"Start":"04:37.545 ","End":"04:42.290","Text":"That would have created an object for us from the person class,"},{"Start":"04:42.290 ","End":"04:48.915","Text":"and has a reference to that object in a person1 variable."},{"Start":"04:48.915 ","End":"04:53.000","Text":"That\u0027s great. Now, we\u0027ve been asked simply"},{"Start":"04:53.000 ","End":"04:56.750","Text":"to type in and try out something for this next bit."},{"Start":"04:56.750 ","End":"04:58.430","Text":"In the second line,"},{"Start":"04:58.430 ","End":"05:04.280","Text":"we are going to simply println(person1)."},{"Start":"05:04.280 ","End":"05:08.275","Text":"Now, person1 is an object."},{"Start":"05:08.275 ","End":"05:13.050","Text":"What does it print out if it prints out the object?"},{"Start":"05:13.050 ","End":"05:14.715","Text":"We\u0027re about to find out."},{"Start":"05:14.715 ","End":"05:19.610","Text":"Let\u0027s run it as we\u0027ve been asked to do in part e. Remember,"},{"Start":"05:19.610 ","End":"05:21.530","Text":"we don\u0027t need to create anything on the workbench."},{"Start":"05:21.530 ","End":"05:24.725","Text":"This is associated with a class because it\u0027s a static method."},{"Start":"05:24.725 ","End":"05:28.730","Text":"We click here. We don\u0027t need to provide any arguments."},{"Start":"05:28.730 ","End":"05:32.750","Text":"We get a response. We get an output."},{"Start":"05:32.750 ","End":"05:34.970","Text":"It says Person with a capital P,"},{"Start":"05:34.970 ","End":"05:36.815","Text":"which happens to be the name of the class."},{"Start":"05:36.815 ","End":"05:42.770","Text":"Then it says at, and it\u0027s got a load of numbers with a character."},{"Start":"05:42.770 ","End":"05:45.440","Text":"This looks like a hex number,"},{"Start":"05:45.440 ","End":"05:46.930","Text":"and it\u0027s quite a large one."},{"Start":"05:46.930 ","End":"05:49.370","Text":"This is actually the memory address where"},{"Start":"05:49.370 ","End":"05:52.370","Text":"this particular object that was created at"},{"Start":"05:52.370 ","End":"05:55.625","Text":"the time we ran main happened to be living in memory."},{"Start":"05:55.625 ","End":"05:57.510","Text":"It no longer exists."},{"Start":"05:57.510 ","End":"05:59.015","Text":"Every time we run this,"},{"Start":"05:59.015 ","End":"06:03.830","Text":"we\u0027ll get a different address returned because it"},{"Start":"06:03.830 ","End":"06:08.780","Text":"requests the space from the run time each time it runs."},{"Start":"06:08.780 ","End":"06:12.245","Text":"The operating system will say, okay, here\u0027s an address."},{"Start":"06:12.245 ","End":"06:14.090","Text":"You can store your object there."},{"Start":"06:14.090 ","End":"06:17.090","Text":"That\u0027s why it\u0027s a different one each time."},{"Start":"06:17.090 ","End":"06:19.460","Text":"Because every time the program runs,"},{"Start":"06:19.460 ","End":"06:21.590","Text":"it then creates the object,"},{"Start":"06:21.590 ","End":"06:24.020","Text":"finishes, and then destroys the object."},{"Start":"06:24.020 ","End":"06:25.760","Text":"It doesn\u0027t exist anymore."},{"Start":"06:25.760 ","End":"06:27.170","Text":"When you create another one,"},{"Start":"06:27.170 ","End":"06:30.040","Text":"it creates it in a different location."},{"Start":"06:30.040 ","End":"06:33.080","Text":"We\u0027ve found out what it does."},{"Start":"06:33.080 ","End":"06:36.139","Text":"Now we\u0027d been asked in part f to override"},{"Start":"06:36.139 ","End":"06:41.150","Text":"the toString method that this class automatically inherits from the object class."},{"Start":"06:41.150 ","End":"06:43.670","Text":"We\u0027ll come to this later,"},{"Start":"06:43.670 ","End":"06:47.315","Text":"but you can actually see that all objects,"},{"Start":"06:47.315 ","End":"06:48.455","Text":"no matter what their type,"},{"Start":"06:48.455 ","End":"06:52.115","Text":"inherit from a default object in Java called object."},{"Start":"06:52.115 ","End":"06:56.060","Text":"Object has a few standard methods that quite often,"},{"Start":"06:56.060 ","End":"06:57.185","Text":"you want to override."},{"Start":"06:57.185 ","End":"06:59.180","Text":"One is called toString."},{"Start":"06:59.180 ","End":"07:02.480","Text":"That\u0027s what we\u0027ve been asked to override here. We\u0027re going to do that."},{"Start":"07:02.480 ","End":"07:05.850","Text":"We\u0027ll see what the effect of that would be."},{"Start":"07:06.100 ","End":"07:12.230","Text":"Public. It does return something,"},{"Start":"07:12.230 ","End":"07:17.260","Text":"it returns a string, but it doesn\u0027t take any parameters."},{"Start":"07:17.260 ","End":"07:20.805","Text":"That is what we need to provide initially."},{"Start":"07:20.805 ","End":"07:22.685","Text":"Then what is it that we\u0027re returning?"},{"Start":"07:22.685 ","End":"07:24.410","Text":"We\u0027re going to return a string."},{"Start":"07:24.410 ","End":"07:27.020","Text":"We can return anything we like, but basically,"},{"Start":"07:27.020 ","End":"07:30.470","Text":"we\u0027re returning a string representation of this object."},{"Start":"07:30.470 ","End":"07:32.314","Text":"What does the object contain?"},{"Start":"07:32.314 ","End":"07:35.055","Text":"We\u0027ve been given a format to use."},{"Start":"07:35.055 ","End":"07:38.000","Text":"Name, a colon, and a space,"},{"Start":"07:38.000 ","End":"07:41.690","Text":"followed by the name attribute."},{"Start":"07:41.690 ","End":"07:44.735","Text":"Then we have a comma and a space,"},{"Start":"07:44.735 ","End":"07:49.055","Text":"and then age, followed by the age."},{"Start":"07:49.055 ","End":"07:54.730","Text":"Then finally, a place of birth."},{"Start":"07:57.370 ","End":"08:03.055","Text":"That will be birthplace attributes."},{"Start":"08:03.055 ","End":"08:08.750","Text":"We\u0027re simply going to return all of these attributes"},{"Start":"08:08.750 ","End":"08:15.665","Text":"with the field names here in response to a call to the toString method."},{"Start":"08:15.665 ","End":"08:22.550","Text":"Because toString already exists in an object or objects,"},{"Start":"08:22.550 ","End":"08:27.200","Text":"because all objects inherit it from the Java object class,"},{"Start":"08:27.200 ","End":"08:30.275","Text":"this is going to be an overriding method."},{"Start":"08:30.275 ","End":"08:34.235","Text":"We\u0027re already making use of inheritance and overriding,"},{"Start":"08:34.235 ","End":"08:38.145","Text":"even though we haven\u0027t used the magic extends keyword yet."},{"Start":"08:38.145 ","End":"08:40.985","Text":"Let\u0027s see what happens now."},{"Start":"08:40.985 ","End":"08:44.130","Text":"When we now run main,"},{"Start":"08:44.130 ","End":"08:47.395","Text":"instead of seeing person and an address,"},{"Start":"08:47.395 ","End":"08:54.005","Text":"we see all field values and field names in-between with commas."},{"Start":"08:54.005 ","End":"08:57.065","Text":"It\u0027s done exactly what we wanted it to do."},{"Start":"08:57.065 ","End":"08:59.510","Text":"We\u0027ll see in the next exercise,"},{"Start":"08:59.510 ","End":"09:03.000","Text":"how can we create multiple instances of a person."},{"Start":"09:03.000 ","End":"09:08.780","Text":"We can actually see the inherited methods and override another one."},{"Start":"09:08.780 ","End":"09:10.760","Text":"We\u0027ll look at that in the next exercise."},{"Start":"09:10.760 ","End":"09:12.380","Text":"But that\u0027s this one done."},{"Start":"09:12.380 ","End":"09:16.125","Text":"That was a very brief introduction to inheritance."},{"Start":"09:16.125 ","End":"09:21.755","Text":"We\u0027ve overridden something that all objects inherit, the object class."},{"Start":"09:21.755 ","End":"09:25.585","Text":"One of the methods within the object class is toString."},{"Start":"09:25.585 ","End":"09:28.804","Text":"We simply overrode it in this exercise."},{"Start":"09:28.804 ","End":"09:31.800","Text":"Thanks for watching. See you shortly for the next one."}],"ID":31084},{"Watched":false,"Name":"Exercise 2","Duration":"13m 41s","ChapterTopicVideoID":29482,"CourseChapterTopicPlaylistID":294470,"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.300","Text":"Hello, welcome to this exercise in which we\u0027ve been asked to make use of"},{"Start":"00:03.300 ","End":"00:08.610","Text":"the person class we created in the previous exercise to create 2 instances,"},{"Start":"00:08.610 ","End":"00:12.240","Text":"person 1 and person 2 of a Person on"},{"Start":"00:12.240 ","End":"00:18.930","Text":"the object workbench using the same attribute values for each of Ray 78 and London."},{"Start":"00:18.930 ","End":"00:22.080","Text":"In part B, we\u0027re told to right-click on \"Person"},{"Start":"00:22.080 ","End":"00:26.385","Text":"1\" and to select the Inherited from object option,"},{"Start":"00:26.385 ","End":"00:30.720","Text":"which then presents a sub menu from which you should select the equals method."},{"Start":"00:30.720 ","End":"00:36.225","Text":"In part B,2 we\u0027re told to type person 2 into the dialogue box,"},{"Start":"00:36.225 ","End":"00:41.460","Text":"which will compare the person 1 object to the person 2 object for equality."},{"Start":"00:41.460 ","End":"00:46.805","Text":"In part 3, we\u0027re asked what is returned and why."},{"Start":"00:46.805 ","End":"00:51.470","Text":"In part C, we provide a new implementation in person of"},{"Start":"00:51.470 ","End":"00:57.830","Text":"the equals method that overrides the default version inherited from the object class."},{"Start":"00:57.830 ","End":"01:02.210","Text":"In part 1, we\u0027re told that the method should return true if the value"},{"Start":"01:02.210 ","End":"01:06.605","Text":"stored in the instance variables are all the same in both objects,"},{"Start":"01:06.605 ","End":"01:08.885","Text":"otherwise it returns false."},{"Start":"01:08.885 ","End":"01:12.470","Text":"In part 2, we\u0027re told to note that you\u0027ll need to declare"},{"Start":"01:12.470 ","End":"01:15.935","Text":"the equals method to accept an argument of type object"},{"Start":"01:15.935 ","End":"01:19.430","Text":"and then use a type cast to create a temporary object"},{"Start":"01:19.430 ","End":"01:23.915","Text":"of type Person to enable the code to compile correctly."},{"Start":"01:23.915 ","End":"01:29.100","Text":"In part D, we test by creating 3 instances of Person."},{"Start":"01:29.100 ","End":"01:32.475","Text":"Person 1 with values Ray 78 and London,"},{"Start":"01:32.475 ","End":"01:35.955","Text":"person 2 with values Ray 78 and London again,"},{"Start":"01:35.955 ","End":"01:38.250","Text":"person 3 with values Dave,"},{"Start":"01:38.250 ","End":"01:40.305","Text":"78, and London."},{"Start":"01:40.305 ","End":"01:45.740","Text":"In part 4, we then compare person 1 to person 2 using equals."},{"Start":"01:45.740 ","End":"01:48.035","Text":"It should now return true."},{"Start":"01:48.035 ","End":"01:53.010","Text":"In part 5, we compare person 2 to person 1 using equals again,"},{"Start":"01:53.010 ","End":"01:55.710","Text":"and that should also return true."},{"Start":"01:55.710 ","End":"01:57.570","Text":"In part 6,"},{"Start":"01:57.570 ","End":"02:01.025","Text":"we compare person 3 to person 1 using equals."},{"Start":"02:01.025 ","End":"02:04.620","Text":"This time, it should return false."},{"Start":"02:05.660 ","End":"02:11.330","Text":"We\u0027re going to make use, as we\u0027ve been asked to of the person class in this exercise."},{"Start":"02:11.330 ","End":"02:13.362","Text":"We create 2 instances;"},{"Start":"02:13.362 ","End":"02:15.020","Text":"person 1 and person 2,"},{"Start":"02:15.020 ","End":"02:17.425","Text":"so we do that by clicking on \u0027\u0027New person\u0027\u0027,"},{"Start":"02:17.425 ","End":"02:19.565","Text":"and we\u0027re making use of the workbench here,"},{"Start":"02:19.565 ","End":"02:22.070","Text":"rather than the main method which was creating"},{"Start":"02:22.070 ","End":"02:24.830","Text":"a person class previously in the previous exercise."},{"Start":"02:24.830 ","End":"02:28.655","Text":"I\u0027m going to use exactly the same values as we did before."},{"Start":"02:28.655 ","End":"02:33.710","Text":"In fact, we\u0027re going to use them for both objects."},{"Start":"02:33.710 ","End":"02:35.285","Text":"Here\u0027s person 1."},{"Start":"02:35.285 ","End":"02:37.580","Text":"I can give that instance any name I like,"},{"Start":"02:37.580 ","End":"02:39.590","Text":"and by default it\u0027s given it person 1,"},{"Start":"02:39.590 ","End":"02:42.235","Text":"and then I want to do exactly the same thing again,"},{"Start":"02:42.235 ","End":"02:46.565","Text":"and I\u0027m going to give it the same values."},{"Start":"02:46.565 ","End":"02:51.215","Text":"Both these objects contain the same data."},{"Start":"02:51.215 ","End":"02:55.595","Text":"When we do part B now we\u0027ve been asked to right-click on person"},{"Start":"02:55.595 ","End":"03:01.470","Text":"1 and select the \u0027\u0027Inherited\u0027\u0027 from object menu,"},{"Start":"03:01.470 ","End":"03:06.440","Text":"and then we can see a whole load of methods here that the object class,"},{"Start":"03:06.440 ","End":"03:08.180","Text":"which every object in Java,"},{"Start":"03:08.180 ","End":"03:10.345","Text":"as we said, is built from."},{"Start":"03:10.345 ","End":"03:15.455","Text":"We\u0027ve inherited all of the methods within object with a capital O,"},{"Start":"03:15.455 ","End":"03:17.990","Text":"and we saw that toString in"},{"Start":"03:17.990 ","End":"03:23.180","Text":"the previous exercise is one of them and we have redefined it in that previous exercise."},{"Start":"03:23.180 ","End":"03:26.330","Text":"Therefore, it appears as redefined in person,"},{"Start":"03:26.330 ","End":"03:28.160","Text":"and we can actually see it over here."},{"Start":"03:28.160 ","End":"03:31.520","Text":"String toString is the new implementation that we"},{"Start":"03:31.520 ","End":"03:35.630","Text":"did that overrode the one that was in the object class."},{"Start":"03:35.630 ","End":"03:41.150","Text":"Now, we have been asked to use the equals method,"},{"Start":"03:41.150 ","End":"03:43.400","Text":"which is the one at the top here."},{"Start":"03:43.400 ","End":"03:47.375","Text":"When we compare one thing for equality to another,"},{"Start":"03:47.375 ","End":"03:51.560","Text":"we simply put inside the brackets here you\u0027ll notice there\u0027s a pair of brackets here,"},{"Start":"03:51.560 ","End":"03:53.330","Text":"a reference to the other object."},{"Start":"03:53.330 ","End":"03:55.955","Text":"We\u0027ve been asked to type in person 2,"},{"Start":"03:55.955 ","End":"04:00.360","Text":"so we\u0027re seeing whether person 1 is equal to person 2."},{"Start":"04:00.360 ","End":"04:02.280","Text":"It either is or it isn\u0027t,"},{"Start":"04:02.280 ","End":"04:04.470","Text":"so let\u0027s see what happens."},{"Start":"04:04.470 ","End":"04:06.405","Text":"We get false."},{"Start":"04:06.405 ","End":"04:08.775","Text":"We\u0027ve compared person 1 to person 2,"},{"Start":"04:08.775 ","End":"04:10.565","Text":"and we get false."},{"Start":"04:10.565 ","End":"04:12.740","Text":"Now, why do we get false?"},{"Start":"04:12.740 ","End":"04:16.340","Text":"Now, the previous exercise should give you a little clue to this."},{"Start":"04:16.340 ","End":"04:20.329","Text":"What it\u0027s actually doing is it\u0027s not comparing the fields,"},{"Start":"04:20.329 ","End":"04:23.705","Text":"the attributes inside these 2 objects to each other."},{"Start":"04:23.705 ","End":"04:28.070","Text":"It\u0027s comparing the memory addresses for these 2 objects."},{"Start":"04:28.070 ","End":"04:31.310","Text":"It\u0027s saying, is this the same object as the previous one?"},{"Start":"04:31.310 ","End":"04:33.110","Text":"Obviously they\u0027re not the same object."},{"Start":"04:33.110 ","End":"04:38.090","Text":"They are 2 different objects and they\u0027d live in different memory locations."},{"Start":"04:38.090 ","End":"04:41.120","Text":"If we had 2 objects and they both referred to"},{"Start":"04:41.120 ","End":"04:45.125","Text":"the same object in the same memory location,"},{"Start":"04:45.125 ","End":"04:46.595","Text":"then they would be equal."},{"Start":"04:46.595 ","End":"04:48.100","Text":"But clearly these 2 aren\u0027t."},{"Start":"04:48.100 ","End":"04:49.930","Text":"We create one-person object,"},{"Start":"04:49.930 ","End":"04:52.060","Text":"and then we create another person object,"},{"Start":"04:52.060 ","End":"04:54.290","Text":"and they live in different memory locations."},{"Start":"04:54.290 ","End":"04:58.565","Text":"Therefore, when we try and compare them with the built in"},{"Start":"04:58.565 ","End":"05:04.655","Text":"equals method that we get for free when we inherit from the object class,"},{"Start":"05:04.655 ","End":"05:06.575","Text":"it gives us false."},{"Start":"05:06.575 ","End":"05:10.310","Text":"In the next part of the exercise in part C,"},{"Start":"05:10.310 ","End":"05:14.045","Text":"we\u0027ve been asked to provide a new implementation in person"},{"Start":"05:14.045 ","End":"05:18.455","Text":"of the equals method that overrides that default one that we inherited."},{"Start":"05:18.455 ","End":"05:21.845","Text":"Let\u0027s do that and see what happens."},{"Start":"05:21.845 ","End":"05:24.090","Text":"We do just what we did before,"},{"Start":"05:24.090 ","End":"05:27.080","Text":"notice here we overrode the toString method."},{"Start":"05:27.080 ","End":"05:31.280","Text":"Now we\u0027re going to override the equals method,"},{"Start":"05:31.280 ","End":"05:38.425","Text":"and it returns a Boolean to say whether it is equal or not, true or false."},{"Start":"05:38.425 ","End":"05:40.704","Text":"It\u0027s called equals."},{"Start":"05:40.704 ","End":"05:42.970","Text":"Now the tricky bit here,"},{"Start":"05:42.970 ","End":"05:46.450","Text":"and we\u0027ve been given a bit of a clue in part 2,"},{"Start":"05:46.450 ","End":"05:50.515","Text":"is, what is the type that we\u0027re expecting here?"},{"Start":"05:50.515 ","End":"05:55.510","Text":"Well, we\u0027re expecting an object with a capital O,"},{"Start":"05:55.510 ","End":"05:58.960","Text":"because we\u0027re comparing one object to another and it has to have"},{"Start":"05:58.960 ","End":"06:05.085","Text":"the same signature as the default method that we\u0027re overriding,"},{"Start":"06:05.085 ","End":"06:07.900","Text":"and that uses an object with a capital O."},{"Start":"06:07.900 ","End":"06:15.070","Text":"Then we can give the parameter any name we want to so I can call it obj."},{"Start":"06:15.070 ","End":"06:21.600","Text":"Now I have a reference to an object which is stored in this thing obj."},{"Start":"06:21.600 ","End":"06:26.045","Text":"But what I\u0027m going to do is I actually want to do a cast,"},{"Start":"06:26.045 ","End":"06:28.190","Text":"so it\u0027s not of type object,"},{"Start":"06:28.190 ","End":"06:30.290","Text":"it\u0027s of type person."},{"Start":"06:30.290 ","End":"06:34.645","Text":"I create a new temporary Person object,"},{"Start":"06:34.645 ","End":"06:36.945","Text":"and I can call it whatever I want."},{"Start":"06:36.945 ","End":"06:39.540","Text":"Call it person with a lowercase p,"},{"Start":"06:39.540 ","End":"06:47.610","Text":"and what I say is it\u0027s equal and has the same content as obj,"},{"Start":"06:47.610 ","End":"06:49.350","Text":"which is what\u0027s been parsed in."},{"Start":"06:49.350 ","End":"06:57.715","Text":"But I\u0027m going to cast it to a person type."},{"Start":"06:57.715 ","End":"07:01.490","Text":"I\u0027ve got a temporary thing called person with"},{"Start":"07:01.490 ","End":"07:06.595","Text":"a lowercase p. It comes from a person class,"},{"Start":"07:06.595 ","End":"07:11.780","Text":"and it gets its values from whatever was parsed in,"},{"Start":"07:11.780 ","End":"07:13.910","Text":"which was parsed in as an object,"},{"Start":"07:13.910 ","End":"07:16.464","Text":"but we\u0027ve cast it to a person."},{"Start":"07:16.464 ","End":"07:19.355","Text":"Now we\u0027ve got a reference to a person,"},{"Start":"07:19.355 ","End":"07:22.290","Text":"and we can do things with the fields that were parsed in,"},{"Start":"07:22.290 ","End":"07:27.060","Text":"and what we need to do is to compare all of the values of name, age,"},{"Start":"07:27.060 ","End":"07:29.855","Text":"birthplace for the 2 objects, and if they\u0027re the same,"},{"Start":"07:29.855 ","End":"07:34.850","Text":"then we say good equals and return true."},{"Start":"07:34.850 ","End":"07:37.430","Text":"That\u0027s now very simple."},{"Start":"07:37.430 ","End":"07:42.319","Text":"What we do is we say if and that reference is called person."},{"Start":"07:42.319 ","End":"07:47.255","Text":"If person.name equals name,"},{"Start":"07:47.255 ","End":"07:48.530","Text":"the one we\u0027ve got stored in,"},{"Start":"07:48.530 ","End":"07:51.780","Text":"and there needs to be a double equals here by the way,"},{"Start":"07:52.810 ","End":"08:00.860","Text":"and person.age equals age and"},{"Start":"08:00.860 ","End":"08:09.878","Text":"person.birthplace equals birthplace,"},{"Start":"08:09.878 ","End":"08:12.290","Text":"if all of those things are true,"},{"Start":"08:12.290 ","End":"08:15.605","Text":"then we return true,"},{"Start":"08:15.605 ","End":"08:20.060","Text":"if they\u0027re not true, we return false."},{"Start":"08:20.060 ","End":"08:24.675","Text":"It will never get to this line if it got to this part,"},{"Start":"08:24.675 ","End":"08:27.255","Text":"so I don\u0027t need to do anything more."},{"Start":"08:27.255 ","End":"08:29.150","Text":"That should be it,"},{"Start":"08:29.150 ","End":"08:31.085","Text":"although it\u0027s complaining about something here."},{"Start":"08:31.085 ","End":"08:33.095","Text":"That\u0027s double equals,"},{"Start":"08:33.095 ","End":"08:35.965","Text":"classic beginner\u0027s error that you might make."},{"Start":"08:35.965 ","End":"08:37.440","Text":"That\u0027s us done,"},{"Start":"08:37.440 ","End":"08:39.440","Text":"so let me just talk that through one more time"},{"Start":"08:39.440 ","End":"08:42.420","Text":"because it\u0027s a little bit tricky to get your head around."},{"Start":"08:42.420 ","End":"08:46.865","Text":"The thing that we have to override has this signature."},{"Start":"08:46.865 ","End":"08:49.640","Text":"It\u0027s expecting an object to be"},{"Start":"08:49.640 ","End":"08:52.640","Text":"inside these brackets and we can give it whatever name we like,"},{"Start":"08:52.640 ","End":"08:55.445","Text":"and I\u0027ve called it obj for the parameter."},{"Start":"08:55.445 ","End":"08:58.970","Text":"It returns a Boolean and it has to be called equals."},{"Start":"08:58.970 ","End":"09:01.240","Text":"If I don\u0027t do exactly that,"},{"Start":"09:01.240 ","End":"09:05.450","Text":"it won\u0027t spot that I\u0027m overriding something."},{"Start":"09:05.450 ","End":"09:07.190","Text":"Now I have overridden it."},{"Start":"09:07.190 ","End":"09:11.660","Text":"If I just tried to say person was of type object,"},{"Start":"09:11.660 ","End":"09:15.245","Text":"an object doesn\u0027t have these fields name,"},{"Start":"09:15.245 ","End":"09:21.094","Text":"age and birthplace, but a person does have these fields: name,"},{"Start":"09:21.094 ","End":"09:22.570","Text":"age, and birthplace."},{"Start":"09:22.570 ","End":"09:32.580","Text":"That\u0027s why here I\u0027m casting to a person class the object that\u0027s been parsed in."},{"Start":"09:32.580 ","End":"09:35.210","Text":"I can refer to it as a person now,"},{"Start":"09:35.210 ","End":"09:38.630","Text":"and I give it the identifier person and I can pick up those fields."},{"Start":"09:38.630 ","End":"09:40.070","Text":"If I don\u0027t do that,"},{"Start":"09:40.070 ","End":"09:42.780","Text":"I will get a compiler error."},{"Start":"09:42.800 ","End":"09:46.595","Text":"If I put person here instead of object,"},{"Start":"09:46.595 ","End":"09:48.215","Text":"I wouldn\u0027t get a compiler error."},{"Start":"09:48.215 ","End":"09:52.540","Text":"But it just wouldn\u0027t override what I wanted to override,"},{"Start":"09:52.540 ","End":"09:54.890","Text":"so some real subtleties there."},{"Start":"09:54.890 ","End":"09:59.450","Text":"Now let\u0027s see if it actually does what we want it to do by running it."},{"Start":"09:59.450 ","End":"10:02.990","Text":"In the final part where we do the testing,"},{"Start":"10:02.990 ","End":"10:08.330","Text":"we\u0027ve got to create 3 instances of person. Let\u0027s do that."},{"Start":"10:08.330 ","End":"10:10.520","Text":"It\u0027s exactly as we did before,"},{"Start":"10:10.520 ","End":"10:14.400","Text":"Ray 78 and is born in London,"},{"Start":"10:14.400 ","End":"10:17.470","Text":"and so is person 2."},{"Start":"10:21.140 ","End":"10:25.320","Text":"But person 3 we\u0027re going to make different."},{"Start":"10:25.320 ","End":"10:27.920","Text":"We\u0027re going to make that Dave,"},{"Start":"10:27.920 ","End":"10:32.690","Text":"given the same date of birth of same age and same place of birth."},{"Start":"10:32.690 ","End":"10:37.760","Text":"They are containing the same values for these 2 objects,"},{"Start":"10:37.760 ","End":"10:39.845","Text":"but different ones for this one."},{"Start":"10:39.845 ","End":"10:43.985","Text":"Let\u0027s see what happens so we can do what we did before."},{"Start":"10:43.985 ","End":"10:49.040","Text":"In part 4, we\u0027ve been asked to compare person 1 to person 2 using equals."},{"Start":"10:49.040 ","End":"10:51.425","Text":"It should now return true."},{"Start":"10:51.425 ","End":"10:53.285","Text":"Let\u0027s see what happens."},{"Start":"10:53.285 ","End":"10:58.410","Text":"Boolean equals, and we can compare person 1 to person 2."},{"Start":"10:58.410 ","End":"11:00.270","Text":"We got false last time,"},{"Start":"11:00.270 ","End":"11:02.430","Text":"this time hopefully we get true,"},{"Start":"11:02.430 ","End":"11:04.210","Text":"and we do, we get true."},{"Start":"11:04.210 ","End":"11:08.955","Text":"If we do that in reverse order if we compare person 2 to person"},{"Start":"11:08.955 ","End":"11:16.400","Text":"1 as we\u0027ve been asked to do in part 5 there of D,"},{"Start":"11:16.400 ","End":"11:18.415","Text":"we also get true."},{"Start":"11:18.415 ","End":"11:21.265","Text":"But if we do part 6,"},{"Start":"11:21.265 ","End":"11:23.935","Text":"compare person 3 to person 1,"},{"Start":"11:23.935 ","End":"11:25.600","Text":"see if they\u0027re equal,"},{"Start":"11:25.600 ","End":"11:28.780","Text":"we get false which is what we expect because we have"},{"Start":"11:28.780 ","End":"11:32.145","Text":"different values in these fields here."},{"Start":"11:32.145 ","End":"11:34.810","Text":"These 2 are the same, but this one wasn\u0027t."},{"Start":"11:34.810 ","End":"11:38.605","Text":"Therefore, this is not the same person as that person."},{"Start":"11:38.605 ","End":"11:42.165","Text":"What we have done is we\u0027ve overridden"},{"Start":"11:42.165 ","End":"11:46.825","Text":"yet another method that was inherited from the object class."},{"Start":"11:46.825 ","End":"11:48.410","Text":"Previously we did toString,"},{"Start":"11:48.410 ","End":"11:51.770","Text":"this time we\u0027ve done equals and a little bit more tricky to understand"},{"Start":"11:51.770 ","End":"11:55.400","Text":"equals because of this casting that we\u0027ve got to do here."},{"Start":"11:55.400 ","End":"11:58.865","Text":"Because object is what it\u0027s looking for in here."},{"Start":"11:58.865 ","End":"12:03.530","Text":"But this is person class rather than an object,"},{"Start":"12:03.530 ","End":"12:07.460","Text":"although it has the methods that we get from an object,"},{"Start":"12:07.460 ","End":"12:09.140","Text":"because it inherits from it."},{"Start":"12:09.140 ","End":"12:13.820","Text":"But we want to treat it as a new type of class which has new fields in it,"},{"Start":"12:13.820 ","End":"12:14.930","Text":"which we do have here."},{"Start":"12:14.930 ","End":"12:18.080","Text":"We\u0027ve inherited these methods like equals,"},{"Start":"12:18.080 ","End":"12:19.865","Text":"and to override it,"},{"Start":"12:19.865 ","End":"12:22.400","Text":"we can provide a new implementation of equals,"},{"Start":"12:22.400 ","End":"12:26.360","Text":"but it must have the same signature here or what would"},{"Start":"12:26.360 ","End":"12:31.775","Text":"happen is if we went into this menu here and went to this part,"},{"Start":"12:31.775 ","End":"12:36.080","Text":"it wouldn\u0027t show redefined in person here if"},{"Start":"12:36.080 ","End":"12:40.535","Text":"we had put some other type inside the brackets here,"},{"Start":"12:40.535 ","End":"12:46.805","Text":"because it needs these types to determine whether this signature is equivalent,"},{"Start":"12:46.805 ","End":"12:52.460","Text":"and whether therefore we\u0027re redefining something and overriding it,"},{"Start":"12:52.460 ","End":"12:56.930","Text":"rather than creating a completely new method of a different type."},{"Start":"12:56.930 ","End":"13:00.290","Text":"Real subtlety there in the difference"},{"Start":"13:00.290 ","End":"13:03.860","Text":"between having person here and having object there,"},{"Start":"13:03.860 ","End":"13:07.115","Text":"but it has to be object always if you want to"},{"Start":"13:07.115 ","End":"13:11.480","Text":"override the method that comes inside the object class."},{"Start":"13:11.480 ","End":"13:17.030","Text":"Then we cast afterwards to whatever the type we want it to be."},{"Start":"13:17.030 ","End":"13:24.710","Text":"Then we can do the logic of the rest of the method as we require. That\u0027s it for this one."},{"Start":"13:24.710 ","End":"13:26.000","Text":"In the next exercise,"},{"Start":"13:26.000 ","End":"13:29.120","Text":"we\u0027ll start creating our own classes,"},{"Start":"13:29.120 ","End":"13:30.710","Text":"not just based on object,"},{"Start":"13:30.710 ","End":"13:33.185","Text":"but based on a completely new class,"},{"Start":"13:33.185 ","End":"13:35.690","Text":"and then we can override methods within"},{"Start":"13:35.690 ","End":"13:39.110","Text":"that class as well and extend that class as well,"},{"Start":"13:39.110 ","End":"13:42.150","Text":"so see you shortly for those."}],"ID":31085},{"Watched":false,"Name":"Exercise 3","Duration":"10m 33s","ChapterTopicVideoID":29481,"CourseChapterTopicPlaylistID":294470,"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.940","Text":"Hello welcome back. In this exercise,"},{"Start":"00:02.940 ","End":"00:09.840","Text":"we\u0027ve been asked to create a class called item and are given a UML class diagram."},{"Start":"00:09.840 ","End":"00:14.969","Text":"In part a, we\u0027re told to implement all the methods and specifically"},{"Start":"00:14.969 ","End":"00:19.844","Text":"display details should print to the console a message as follows."},{"Start":"00:19.844 ","End":"00:24.450","Text":"An example is given description blue pen in stock 40,"},{"Start":"00:24.450 ","End":"00:30.315","Text":"and that will depend on the contents of the description and quantity attributes."},{"Start":"00:30.315 ","End":"00:35.070","Text":"In part b, we\u0027re told to implement a child class SpecialItem,"},{"Start":"00:35.070 ","End":"00:37.005","Text":"which inherits from item,"},{"Start":"00:37.005 ","End":"00:41.285","Text":"but has an additional attribute called deposit, a double,"},{"Start":"00:41.285 ","End":"00:44.120","Text":"which is used to store the value of a deposit that should be paid"},{"Start":"00:44.120 ","End":"00:47.630","Text":"for specially ordered nonstop items."},{"Start":"00:47.630 ","End":"00:50.275","Text":"The constructor for SpecialItem,"},{"Start":"00:50.275 ","End":"00:51.749","Text":"we\u0027re told in part c,"},{"Start":"00:51.749 ","End":"00:56.030","Text":"should take an argument for description plus another argument which contains"},{"Start":"00:56.030 ","End":"01:01.585","Text":"the deposit value and should always set quantity to -1."},{"Start":"01:01.585 ","End":"01:07.354","Text":"In part d, we\u0027re told to implement a new version of display details within SpecialItem,"},{"Start":"01:07.354 ","End":"01:10.220","Text":"which instead of displaying the quantity in stock,"},{"Start":"01:10.220 ","End":"01:11.968","Text":"displays the message,"},{"Start":"01:11.968 ","End":"01:14.105","Text":"Description: engraved pen."},{"Start":"01:14.105 ","End":"01:21.940","Text":"This special item requires a deposit before ordering of 515, as an example."},{"Start":"01:21.940 ","End":"01:27.440","Text":"In part e, we\u0027re told to test the code by creating 2 objects as follows,"},{"Start":"01:27.440 ","End":"01:32.959","Text":"an object of type Item with the description \"blue pen\" and quantity 40,"},{"Start":"01:32.959 ","End":"01:36.110","Text":"in part ii, an object of type SpecialItem with"},{"Start":"01:36.110 ","End":"01:40.840","Text":"the description \"engraved pen\" and deposit value 515."},{"Start":"01:40.840 ","End":"01:43.310","Text":"In part iii, we\u0027re told to run"},{"Start":"01:43.310 ","End":"01:48.900","Text":"display details on each object to see if we get the expected output."},{"Start":"01:50.480 ","End":"01:55.835","Text":"We\u0027re going to go ahead and implement the class based on the class diagram."},{"Start":"01:55.835 ","End":"01:58.640","Text":"This time we\u0027re seeing hash symbol,"},{"Start":"01:58.640 ","End":"02:02.450","Text":"which previously we\u0027d seen a minus there meaning private."},{"Start":"02:02.450 ","End":"02:04.585","Text":"The hash means protected,"},{"Start":"02:04.585 ","End":"02:11.180","Text":"which means members of child classes can access things that are"},{"Start":"02:11.180 ","End":"02:14.750","Text":"protected in the parent class or"},{"Start":"02:14.750 ","End":"02:20.410","Text":"ancestor class so long as they have been marked as protected and not private."},{"Start":"02:20.410 ","End":"02:26.360","Text":"We have been told to mark this first one description as protected,"},{"Start":"02:26.360 ","End":"02:29.085","Text":"and that is of data type string."},{"Start":"02:29.085 ","End":"02:31.485","Text":"That\u0027s done."},{"Start":"02:31.485 ","End":"02:35.315","Text":"The next one is an int,"},{"Start":"02:35.315 ","End":"02:38.610","Text":"and that is called quantity."},{"Start":"02:38.610 ","End":"02:42.705","Text":"There\u0027s our attributes done."},{"Start":"02:42.705 ","End":"02:46.369","Text":"In part a, we\u0027re asked to implement all the methods."},{"Start":"02:46.369 ","End":"02:50.975","Text":"We\u0027re told what the display details method should do specifically."},{"Start":"02:50.975 ","End":"02:55.175","Text":"But if we just start with the constructor item,"},{"Start":"02:55.175 ","End":"03:01.785","Text":"that\u0027s obviously going to be public and constructors never return values."},{"Start":"03:01.785 ","End":"03:03.780","Text":"That\u0027s as we expect."},{"Start":"03:03.780 ","End":"03:07.820","Text":"What do we need from the item constructor?"},{"Start":"03:07.820 ","End":"03:09.109","Text":"We need a string."},{"Start":"03:09.109 ","End":"03:11.225","Text":"Let\u0027s call that description."},{"Start":"03:11.225 ","End":"03:12.920","Text":"And we need an integer,"},{"Start":"03:12.920 ","End":"03:15.245","Text":"which we\u0027ll call quantity."},{"Start":"03:15.245 ","End":"03:19.865","Text":"Simple enough to set those values."},{"Start":"03:19.865 ","End":"03:23.940","Text":"The instance variable gets its value from"},{"Start":"03:23.940 ","End":"03:30.940","Text":"the first parameter and quantity from the second,"},{"Start":"03:30.940 ","End":"03:32.890","Text":"which happens to have the same names."},{"Start":"03:32.890 ","End":"03:38.110","Text":"That\u0027s fine. Done. As the constructor method done and get"},{"Start":"03:38.110 ","End":"03:41.374","Text":"description and get quantity or"},{"Start":"03:41.374 ","End":"03:44.980","Text":"obviously select a methods we\u0027ve seen this many times before."},{"Start":"03:44.980 ","End":"03:48.969","Text":"It\u0027s very straightforward. Kept description,"},{"Start":"03:48.969 ","End":"03:53.690","Text":"no parameters, but will return a string."},{"Start":"03:56.250 ","End":"04:03.100","Text":"The string, it\u0027s going to return is stored in description."},{"Start":"04:03.100 ","End":"04:08.260","Text":"We\u0027re going to do exactly the same thing for quantity."},{"Start":"04:08.260 ","End":"04:13.085","Text":"I got my brackets in the wrong place there."},{"Start":"04:13.085 ","End":"04:17.825","Text":"This one is going to return an int and is obviously called"},{"Start":"04:17.825 ","End":"04:25.895","Text":"quantity and returns the value of quantity."},{"Start":"04:25.895 ","End":"04:30.840","Text":"The only one left to do is display details."},{"Start":"04:31.540 ","End":"04:37.865","Text":"Once again public returns nothing and takes no parameters."},{"Start":"04:37.865 ","End":"04:42.635","Text":"What this does is print to the screen and message,"},{"Start":"04:42.635 ","End":"04:49.595","Text":"which goes on the lines of description in text with a colon and a space."},{"Start":"04:49.595 ","End":"04:53.180","Text":"Then we actually give the description because we have access to"},{"Start":"04:53.180 ","End":"04:56.859","Text":"that because this is within us. That\u0027s fine."},{"Start":"04:56.859 ","End":"05:02.689","Text":"I can just write description there and then I can say in stock,"},{"Start":"05:02.689 ","End":"05:05.330","Text":"and that\u0027s how many we have in stock,"},{"Start":"05:05.330 ","End":"05:08.705","Text":"which is stored in the quantity."},{"Start":"05:08.705 ","End":"05:12.725","Text":"Attribute. That\u0027s done."},{"Start":"05:12.725 ","End":"05:17.850","Text":"We\u0027ve done part a and don\u0027t seem to have any errors."},{"Start":"05:17.850 ","End":"05:20.810","Text":"We can implement a child class called SpecialItem,"},{"Start":"05:20.810 ","End":"05:22.625","Text":"which inherits from item,"},{"Start":"05:22.625 ","End":"05:25.055","Text":"but has an additional attribute called deposit,"},{"Start":"05:25.055 ","End":"05:27.530","Text":"which is a double that\u0027s used to store the value of"},{"Start":"05:27.530 ","End":"05:31.255","Text":"a deposit for a specialty ordered non-stock items."},{"Start":"05:31.255 ","End":"05:33.200","Text":"You\u0027ll need to create a new class."},{"Start":"05:33.200 ","End":"05:35.900","Text":"I\u0027ve already created mine, it\u0027s empty."},{"Start":"05:35.900 ","End":"05:41.630","Text":"Now the first thing we need to do if we\u0027re going to inherit from another class is to"},{"Start":"05:41.630 ","End":"05:46.805","Text":"use the extends keyword and then we give that name in Java,"},{"Start":"05:46.805 ","End":"05:48.455","Text":"this is the syntax."},{"Start":"05:48.455 ","End":"05:52.700","Text":"We extend it from the parent class,"},{"Start":"05:52.700 ","End":"05:58.205","Text":"which in this case is called item. That\u0027s that."},{"Start":"05:58.205 ","End":"06:02.800","Text":"I\u0027m going to add an extra attribute here called deposit."},{"Start":"06:02.800 ","End":"06:06.890","Text":"I can make that private or protected. Let\u0027s make it private."},{"Start":"06:06.890 ","End":"06:10.445","Text":"Have been told about any other child classes for SpecialItem."},{"Start":"06:10.445 ","End":"06:12.665","Text":"Let\u0027s make it private for now."},{"Start":"06:12.665 ","End":"06:17.680","Text":"It\u0027s a double. It\u0027s called deposit."},{"Start":"06:17.680 ","End":"06:24.215","Text":"Now I can create my constructor which is always public and returns no value,"},{"Start":"06:24.215 ","End":"06:27.124","Text":"but does need an argument,"},{"Start":"06:27.124 ","End":"06:28.745","Text":"single argument this time."},{"Start":"06:28.745 ","End":"06:32.070","Text":"It\u0027s just going to take the description."},{"Start":"06:33.440 ","End":"06:39.275","Text":"You might ask, where is it going to get the quantity from which it inherits from item?"},{"Start":"06:39.275 ","End":"06:43.130","Text":"Or we\u0027re going to just give it a quantity of -1."},{"Start":"06:43.130 ","End":"06:47.165","Text":"To call the constructor from the parent class,"},{"Start":"06:47.165 ","End":"06:55.040","Text":"we use super on its own and then pass inside the brackets whatever the arguments are."},{"Start":"06:55.040 ","End":"07:01.850","Text":"In this case we\u0027re going to pass on description and we\u0027re going to pass on -1."},{"Start":"07:01.850 ","End":"07:06.050","Text":"Whatever was passed into the constructor for SpecialItem gets passed to"},{"Start":"07:06.050 ","End":"07:10.415","Text":"the constructor of item along with the value -1."},{"Start":"07:10.415 ","End":"07:14.210","Text":"So we are now able to create the item that this is"},{"Start":"07:14.210 ","End":"07:18.020","Text":"built on and the fields will have their values,"},{"Start":"07:18.020 ","End":"07:23.540","Text":"but we also need to set a value for the deposit as well."},{"Start":"07:23.540 ","End":"07:26.250","Text":"Really that should go in here,"},{"Start":"07:26.250 ","End":"07:34.865","Text":"and it\u0027s a double so we just set the value of deposit to whatever was passed in."},{"Start":"07:34.865 ","End":"07:43.205","Text":"Now we can implement a new version as we are told to in part d of display details,"},{"Start":"07:43.205 ","End":"07:47.229","Text":"which this time doesn\u0027t display quantity because there is no quantity."},{"Start":"07:47.229 ","End":"07:48.680","Text":"This is a special order item,"},{"Start":"07:48.680 ","End":"07:51.770","Text":"but it does display how much we are"},{"Start":"07:51.770 ","End":"07:56.660","Text":"expected to pay as a deposit for ordering this type of item."},{"Start":"07:56.660 ","End":"08:03.365","Text":"First bits of the same description is whatever it is that we are ordering."},{"Start":"08:03.365 ","End":"08:11.020","Text":"Then we say, this special item requires a deposit before ordering of,"},{"Start":"08:11.020 ","End":"08:13.920","Text":"and then the amount."},{"Start":"08:13.920 ","End":"08:17.085","Text":"We have access to that attribute."},{"Start":"08:17.085 ","End":"08:21.020","Text":"Put the value in the brackets here and it\u0027s"},{"Start":"08:21.020 ","End":"08:25.680","Text":"concatenate it to this string and that value and this string."},{"Start":"08:26.500 ","End":"08:28.805","Text":"Now if we compile,"},{"Start":"08:28.805 ","End":"08:35.825","Text":"we will see that our display now has an arrow pointing up to item saying,"},{"Start":"08:35.825 ","End":"08:40.245","Text":"essentially this inherits from that."},{"Start":"08:40.245 ","End":"08:43.330","Text":"We haven\u0027t seen that before because the first exercise"},{"Start":"08:43.330 ","End":"08:45.790","Text":"that we\u0027re actually making use of inheritance,"},{"Start":"08:45.790 ","End":"08:49.855","Text":"but you\u0027ll get used to that and see that many times in this topic."},{"Start":"08:49.855 ","End":"08:51.670","Text":"Now we can test, first of all,"},{"Start":"08:51.670 ","End":"09:01.995","Text":"by creating the item in part 1 with description of \"blue pen\" and a quantity of 40."},{"Start":"09:01.995 ","End":"09:05.265","Text":"Then we create an item from special item."},{"Start":"09:05.265 ","End":"09:11.480","Text":"This one is going to be an \"engraved pen and there\u0027s a deposit of 5.15."},{"Start":"09:11.480 ","End":"09:14.800","Text":"We\u0027ll run as its our subsidiary in part 3,"},{"Start":"09:14.800 ","End":"09:18.070","Text":"display details on each of the items that start with item."},{"Start":"09:18.070 ","End":"09:20.085","Text":"Run display details,"},{"Start":"09:20.085 ","End":"09:24.618","Text":"and we see is we expect to blue pen with 40 of those in stock,"},{"Start":"09:24.618 ","End":"09:28.880","Text":"but when we run display details from here,"},{"Start":"09:28.880 ","End":"09:31.985","Text":"we will see it says something different."},{"Start":"09:31.985 ","End":"09:34.835","Text":"Engraved pen I spelled that wrong when I was typing in,"},{"Start":"09:34.835 ","End":"09:39.935","Text":"and the special item requires amount of the deposit."},{"Start":"09:39.935 ","End":"09:43.700","Text":"We\u0027ve got display details in both objects,"},{"Start":"09:43.700 ","End":"09:49.895","Text":"but they do different things because one has been overridden."},{"Start":"09:49.895 ","End":"09:55.285","Text":"Item, the parent class or the base class shows the quantity,"},{"Start":"09:55.285 ","End":"09:59.630","Text":"but SpecialItem has a different implementation."},{"Start":"09:59.630 ","End":"10:02.300","Text":"Didn\u0027t need to use any special keywords or anything because Java"},{"Start":"10:02.300 ","End":"10:05.270","Text":"doesn\u0027t need them of override or anything like that,"},{"Start":"10:05.270 ","End":"10:07.100","Text":"but this over-ride,"},{"Start":"10:07.100 ","End":"10:12.665","Text":"the one that was in the base class or the parent class, whatever you want to call it,"},{"Start":"10:12.665 ","End":"10:15.455","Text":"if we look on the menu here,"},{"Start":"10:15.455 ","End":"10:18.080","Text":"if we look at inherited from item,"},{"Start":"10:18.080 ","End":"10:21.020","Text":"we\u0027ll see that display details is there and we\u0027re told"},{"Start":"10:21.020 ","End":"10:24.300","Text":"that it\u0027s re-defined inside SpecialItem."},{"Start":"10:24.300 ","End":"10:26.360","Text":"That\u0027s it for this first exercise."},{"Start":"10:26.360 ","End":"10:28.400","Text":"Nice simple one, and we\u0027ll be building on"},{"Start":"10:28.400 ","End":"10:32.550","Text":"that in the next few videos. Thanks for watching."}],"ID":31086},{"Watched":false,"Name":"Exercise 4","Duration":"18m 5s","ChapterTopicVideoID":29480,"CourseChapterTopicPlaylistID":294470,"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.995","Text":"Hello again. Welcome back."},{"Start":"00:01.995 ","End":"00:05.230","Text":"In this exercise we\u0027ve been told to create a class called pen,"},{"Start":"00:05.230 ","End":"00:09.549","Text":"which inherits from the item class which we created in the previous exercise."},{"Start":"00:09.549 ","End":"00:13.165","Text":"The pen has an additional 2 attributes,"},{"Start":"00:13.165 ","End":"00:15.535","Text":"a string called color,"},{"Start":"00:15.535 ","End":"00:18.990","Text":"and a Boolean called isRetractable."},{"Start":"00:18.990 ","End":"00:22.250","Text":"In part b, we\u0027re told the constructor for pen should take"},{"Start":"00:22.250 ","End":"00:25.810","Text":"3 arguments to set the attribute values for color,"},{"Start":"00:25.810 ","End":"00:28.480","Text":"quantity, and isRetractable."},{"Start":"00:28.480 ","End":"00:30.850","Text":"It should automatically set the description"},{"Start":"00:30.850 ","End":"00:34.480","Text":"attribute to an appropriate value depending on the color attribute."},{"Start":"00:34.480 ","End":"00:36.400","Text":"For example, with a value of blue,"},{"Start":"00:36.400 ","End":"00:38.530","Text":"description will be set to blue pen."},{"Start":"00:38.530 ","End":"00:40.360","Text":"With a value of black,"},{"Start":"00:40.360 ","End":"00:43.055","Text":"description will be set to black pen."},{"Start":"00:43.055 ","End":"00:47.260","Text":"In part c, we\u0027re told to override the display details method within"},{"Start":"00:47.260 ","End":"00:52.120","Text":"the pen class so that it displays whether the pen is retractable or not."},{"Start":"00:52.120 ","End":"00:58.435","Text":"For example, description, black retractable pen in stock 999."},{"Start":"00:58.435 ","End":"01:02.935","Text":"In part d, we\u0027re told to create another class called promo pen,"},{"Start":"01:02.935 ","End":"01:05.845","Text":"which inherits from the pen class."},{"Start":"01:05.845 ","End":"01:08.979","Text":"This class should have an additional attribute message"},{"Start":"01:08.979 ","End":"01:12.200","Text":"which contains a promotional message."},{"Start":"01:12.200 ","End":"01:15.190","Text":"In part f, we\u0027re told the constructor should accept"},{"Start":"01:15.190 ","End":"01:19.480","Text":"an additional argument to set the message attribute."},{"Start":"01:19.480 ","End":"01:23.980","Text":"The display details method should call the display details method from"},{"Start":"01:23.980 ","End":"01:26.000","Text":"the parent class and then add"},{"Start":"01:26.000 ","End":"01:29.645","Text":"an additional line of output showing the promotional message."},{"Start":"01:29.645 ","End":"01:34.220","Text":"For example, description black retractable pen in stock 999,"},{"Start":"01:34.220 ","End":"01:39.385","Text":"message, webshoppy.com for your stationary needs, 24.7."},{"Start":"01:39.385 ","End":"01:44.525","Text":"In part h, we are told to test the code by creating objects as follows,"},{"Start":"01:44.525 ","End":"01:46.850","Text":"a pen object with color blue,"},{"Start":"01:46.850 ","End":"01:50.134","Text":"retractable set to false and quantity 40,"},{"Start":"01:50.134 ","End":"01:52.205","Text":"a pen object with color red,"},{"Start":"01:52.205 ","End":"01:54.305","Text":"retractable set to true,"},{"Start":"01:54.305 ","End":"01:56.060","Text":"and quantity 10,"},{"Start":"01:56.060 ","End":"01:59.045","Text":"a promo pen object with color black,"},{"Start":"01:59.045 ","End":"02:00.935","Text":"retractable set to true,"},{"Start":"02:00.935 ","End":"02:03.095","Text":"quantity set to 999,"},{"Start":"02:03.095 ","End":"02:07.970","Text":"and message set to webshoppy.com for your stationary needs 24/7."},{"Start":"02:07.970 ","End":"02:14.000","Text":"Finally, we run display details on each object to see if we get the expected output."},{"Start":"02:14.000 ","End":"02:19.385","Text":"Once again, we\u0027re going to extend the item class."},{"Start":"02:19.385 ","End":"02:22.760","Text":"Pen is based on item,"},{"Start":"02:22.760 ","End":"02:26.830","Text":"so we have to put extends item."},{"Start":"02:26.830 ","End":"02:32.195","Text":"Then we add in the attributes which pen has"},{"Start":"02:32.195 ","End":"02:37.445","Text":"in addition to the attributes that it\u0027s going to inherit from item,"},{"Start":"02:37.445 ","End":"02:42.060","Text":"1 is a string called color,"},{"Start":"02:42.550 ","End":"02:49.110","Text":"and the other is a Boolean called isRetractable."},{"Start":"02:55.910 ","End":"03:00.185","Text":"The constructor now we have to do in part b,"},{"Start":"03:00.185 ","End":"03:04.310","Text":"it takes 3 arguments to set the attributes for color,"},{"Start":"03:04.310 ","End":"03:07.280","Text":"quantity, and whether it\u0027s retractable or not."},{"Start":"03:07.280 ","End":"03:11.795","Text":"Notice, we do actually need a value for the description as well."},{"Start":"03:11.795 ","End":"03:14.740","Text":"But it says in the question that we\u0027re going to automatically set that."},{"Start":"03:14.740 ","End":"03:16.475","Text":"We won\u0027t take it as an argument."},{"Start":"03:16.475 ","End":"03:21.965","Text":"We\u0027re going to ultimately set it to whatever the pen color is and then the word pen."},{"Start":"03:21.965 ","End":"03:25.730","Text":"Little bit trickier than what we\u0027ve previously done and it will involve as"},{"Start":"03:25.730 ","End":"03:30.710","Text":"calling the constructor or the parent class,"},{"Start":"03:30.710 ","End":"03:33.665","Text":"which in this case would be item."},{"Start":"03:33.665 ","End":"03:36.950","Text":"Let\u0027s have a go at doing that."},{"Start":"03:36.950 ","End":"03:41.205","Text":"It\u0027s a public method called Pen,"},{"Start":"03:41.205 ","End":"03:47.900","Text":"and their return values don\u0027t even need to put point because it\u0027s a constructor."},{"Start":"03:47.900 ","End":"03:52.325","Text":"These are going to be the parameters,"},{"Start":"03:52.325 ","End":"03:59.545","Text":"quantity, color,"},{"Start":"03:59.545 ","End":"04:04.185","Text":"and whether it\u0027s retractable or not."},{"Start":"04:04.185 ","End":"04:08.160","Text":"This is the bit where we call the constructor."},{"Start":"04:08.160 ","End":"04:14.465","Text":"This call using super always has to be the first line of any constructor."},{"Start":"04:14.465 ","End":"04:18.620","Text":"We can\u0027t do lots of logic in here because we can only have 1 line."},{"Start":"04:18.620 ","End":"04:22.660","Text":"But it\u0027s pretty simple what we need to do here in the call to super."},{"Start":"04:22.660 ","End":"04:25.220","Text":"We\u0027re calling the constructor for item."},{"Start":"04:25.220 ","End":"04:28.850","Text":"An item needs a description and a quantity,"},{"Start":"04:28.850 ","End":"04:30.620","Text":"that\u0027s all it needs."},{"Start":"04:30.620 ","End":"04:39.210","Text":"The description is going to be literal string description."},{"Start":"04:39.220 ","End":"04:42.200","Text":"Actually, no, it\u0027s not going to have the literal string description,"},{"Start":"04:42.200 ","End":"04:44.850","Text":"and it\u0027s going to have the coloring,"},{"Start":"04:45.110 ","End":"04:51.920","Text":"so whatever has been passed as color and then the literal string pen,"},{"Start":"04:51.920 ","End":"04:53.390","Text":"so black pen, blue pen,"},{"Start":"04:53.390 ","End":"04:56.240","Text":"red pen, whatever, that\u0027s what we\u0027re going to pass in as"},{"Start":"04:56.240 ","End":"04:59.220","Text":"the description because we\u0027re creating a pen,"},{"Start":"04:59.220 ","End":"05:01.540","Text":"so it\u0027s always going to be some pen."},{"Start":"05:01.540 ","End":"05:06.110","Text":"We just need to know what the color and the quantity gets passed"},{"Start":"05:06.110 ","End":"05:12.740","Text":"through from the argument here that\u0027s been provided already."},{"Start":"05:12.740 ","End":"05:21.200","Text":"There\u0027s a quantity and that\u0027s the call to the constructor of the parent class done."},{"Start":"05:21.200 ","End":"05:23.975","Text":"But we still have to finish the constructor off."},{"Start":"05:23.975 ","End":"05:33.590","Text":"We want to set the values of color and is retractable."},{"Start":"05:43.010 ","End":"05:47.380","Text":"That now is constructed on."},{"Start":"05:48.020 ","End":"05:51.424","Text":"The next part is to override"},{"Start":"05:51.424 ","End":"05:56.645","Text":"display details we\u0027ve been asked to do in part c. Let\u0027s do that."},{"Start":"05:56.645 ","End":"06:04.100","Text":"It\u0027s a public method called display details."},{"Start":"06:04.100 ","End":"06:07.520","Text":"It returns nothing,"},{"Start":"06:07.520 ","End":"06:12.140","Text":"so we put void in front and it takes no arguments,"},{"Start":"06:12.140 ","End":"06:15.810","Text":"so we put empty brackets there."},{"Start":"06:17.180 ","End":"06:22.280","Text":"All it does is, first of all,"},{"Start":"06:22.280 ","End":"06:26.315","Text":"check whether it\u0027s a retractable pen or not because if it is,"},{"Start":"06:26.315 ","End":"06:29.870","Text":"it adds that to the description when it\u0027s printing it out."},{"Start":"06:29.870 ","End":"06:38.730","Text":"Otherwise, it just prints the value of the color and then the word pen."},{"Start":"06:38.730 ","End":"06:41.055","Text":"We need an if statement."},{"Start":"06:41.055 ","End":"06:47.780","Text":"What we need to say is if it is a retractable,"},{"Start":"06:47.780 ","End":"06:49.670","Text":"we don\u0027t need to compare it to true or anything because"},{"Start":"06:49.670 ","End":"06:52.100","Text":"it contains the value true or false."},{"Start":"06:52.100 ","End":"06:54.275","Text":"If it is retractable,"},{"Start":"06:54.275 ","End":"07:00.030","Text":"then what we\u0027re going to display is the following."},{"Start":"07:06.940 ","End":"07:12.840","Text":"So far that\u0027s the same as what the existing method does."},{"Start":"07:15.010 ","End":"07:23.160","Text":"But now we\u0027re adding the color and we\u0027re adding the word retractable pen here."},{"Start":"07:24.610 ","End":"07:30.590","Text":"Whereas previously we were to just say how many were in stock."},{"Start":"07:30.590 ","End":"07:32.990","Text":"We\u0027re now going to say retractable pen,"},{"Start":"07:32.990 ","End":"07:36.240","Text":"and then we\u0027re going to say how many we have in stock."},{"Start":"07:43.100 ","End":"07:48.170","Text":"That\u0027s what we would display if it is a retractable pen."},{"Start":"07:48.170 ","End":"07:58.090","Text":"Otherwise,; p we will display a different message."},{"Start":"07:58.820 ","End":"08:03.955","Text":"Let\u0027s just copy this line here and modify it."},{"Start":"08:03.955 ","End":"08:08.210","Text":"We\u0027re just going to say that this is not a retractable pen,"},{"Start":"08:08.210 ","End":"08:12.310","Text":"so we just remove the word retractable from it."},{"Start":"08:12.310 ","End":"08:14.130","Text":"That does the job."},{"Start":"08:14.130 ","End":"08:15.965","Text":"If it\u0027s a retractable pen,"},{"Start":"08:15.965 ","End":"08:17.315","Text":"it prints this message."},{"Start":"08:17.315 ","End":"08:19.570","Text":"Otherwise, it prints that message."},{"Start":"08:19.570 ","End":"08:26.870","Text":"This value is coming from our own little instance variable here of isRetractable."},{"Start":"08:26.870 ","End":"08:30.905","Text":"We have done what we\u0027ve been asked to do in"},{"Start":"08:30.905 ","End":"08:38.460","Text":"part c. Now we\u0027re going to create another class."},{"Start":"08:38.460 ","End":"08:41.800","Text":"This one\u0027s called promo pen."},{"Start":"08:42.050 ","End":"08:46.490","Text":"You see how pen now we can see that inherits from item."},{"Start":"08:46.490 ","End":"08:51.930","Text":"Now let\u0027s create a new class called PromoPen."},{"Start":"09:00.830 ","End":"09:04.035","Text":"If you can just edit this part out please."},{"Start":"09:04.035 ","End":"09:06.000","Text":"Where I delete this stuff,"},{"Start":"09:06.000 ","End":"09:12.810","Text":"it\u0027s a waste of time for the viewer so you just go from here."},{"Start":"09:12.810 ","End":"09:15.030","Text":"I\u0027ve got PromoPen,"},{"Start":"09:15.030 ","End":"09:19.140","Text":"and now this inherits PromoPen not from item."},{"Start":"09:19.140 ","End":"09:27.090","Text":"I put in here extends pen and this is going to have"},{"Start":"09:27.090 ","End":"09:36.130","Text":"an additional attribute called message."},{"Start":"09:37.460 ","End":"09:40.660","Text":"That\u0027s going to be a string"},{"Start":"09:44.810 ","End":"09:53.160","Text":"and the constructor will need that additional argument to set the message attribute,"},{"Start":"09:53.160 ","End":"09:57.750","Text":"so let\u0027s do that constructor."},{"Start":"09:57.750 ","End":"10:00.720","Text":"It\u0027s going to be called PromoPen."},{"Start":"10:00.720 ","End":"10:02.890","Text":"It\u0027s going to be public."},{"Start":"10:05.630 ","End":"10:11.640","Text":"It\u0027s going to be a whole host of parameters we\u0027re going to need here."},{"Start":"10:11.640 ","End":"10:13.650","Text":"We don\u0027t need description,"},{"Start":"10:13.650 ","End":"10:17.590","Text":"but we do need quantity."},{"Start":"10:19.490 ","End":"10:25.319","Text":"Obviously that\u0027s simple hint. We do need"},{"Start":"10:25.319 ","End":"10:35.460","Text":"a color and we"},{"Start":"10:35.460 ","End":"10:40.150","Text":"need to know whether it\u0027s retractable or not."},{"Start":"10:43.400 ","End":"10:51.495","Text":"Then we need this extra argument to set the message on the promotional pen."},{"Start":"10:51.495 ","End":"10:53.370","Text":"I\u0027m sure you\u0027ve had 1 of these promotional pens."},{"Start":"10:53.370 ","End":"10:57.810","Text":"It\u0027s got phone number or a web address on it and so they"},{"Start":"10:57.810 ","End":"11:03.210","Text":"are ordered from various stationary companies and given out exhibition console on."},{"Start":"11:03.210 ","End":"11:06.435","Text":"There is the constructor."},{"Start":"11:06.435 ","End":"11:14.420","Text":"Now we\u0027re going to call the constructor for the parent class,"},{"Start":"11:14.420 ","End":"11:17.990","Text":"which is in this case going to be pen."},{"Start":"11:17.990 ","End":"11:21.290","Text":"Whatever pen needs we\u0027re going to pass on."},{"Start":"11:21.290 ","End":"11:25.440","Text":"We don\u0027t need to pass on description."},{"Start":"11:26.450 ","End":"11:33.915","Text":"It\u0027s going to put that in there itself from the pen constructor."},{"Start":"11:33.915 ","End":"11:40.270","Text":"But we do need to pass on all the other relevant values that are needed."},{"Start":"11:41.200 ","End":"11:47.700","Text":"We\u0027re going to pass on. Quantity,"},{"Start":"11:48.520 ","End":"11:54.090","Text":"color is retractable."},{"Start":"12:03.230 ","End":"12:07.410","Text":"Now let me just think about getting the right number"},{"Start":"12:07.410 ","End":"12:11.740","Text":"of parameters there seems to be not complaining."},{"Start":"12:13.340 ","End":"12:15.930","Text":"Hopefully that\u0027ll be fine."},{"Start":"12:15.930 ","End":"12:18.480","Text":"Can check back in my class in a moment."},{"Start":"12:18.480 ","End":"12:21.970","Text":"Now I need to set the message"},{"Start":"12:26.780 ","End":"12:29.850","Text":"to whatever has been passed in."},{"Start":"12:29.850 ","End":"12:32.250","Text":"There\u0027s my constructor done just once again,"},{"Start":"12:32.250 ","End":"12:36.480","Text":"I\u0027m going to check whether the parent class has got the right things."},{"Start":"12:36.480 ","End":"12:38.940","Text":"Compiler hasn\u0027t complained, so it looks like I\u0027m good."},{"Start":"12:38.940 ","End":"12:47.055","Text":"But does a pen need just 3 attributes to be passed into the constructor?"},{"Start":"12:47.055 ","End":"12:50.730","Text":"Yes, it does so we could and it will"},{"Start":"12:50.730 ","End":"12:56.535","Text":"call the constructor for item to set the description."},{"Start":"12:56.535 ","End":"13:02.115","Text":"You see here now we\u0027ve got an extra layer of inheritance."},{"Start":"13:02.115 ","End":"13:04.170","Text":"PromoPen inherits from pen,"},{"Start":"13:04.170 ","End":"13:07.120","Text":"which itself inherits from item."},{"Start":"13:07.760 ","End":"13:14.880","Text":"Now we can go ahead and change our display details method,"},{"Start":"13:14.880 ","End":"13:20.145","Text":"which is going to be overwritten from the 1 that\u0027s in pen."},{"Start":"13:20.145 ","End":"13:23.670","Text":"This is slightly different because it\u0027s going to"},{"Start":"13:23.670 ","End":"13:27.270","Text":"call display details from the parent class,"},{"Start":"13:27.270 ","End":"13:29.190","Text":"which we haven\u0027t done before."},{"Start":"13:29.190 ","End":"13:37.080","Text":"Let\u0027s see how we do that so no value is returned back."},{"Start":"13:37.080 ","End":"13:40.005","Text":"Name is display details,"},{"Start":"13:40.005 ","End":"13:41.910","Text":"and there is already 1 of those so this is"},{"Start":"13:41.910 ","End":"13:45.960","Text":"an overwrite of whatever is already there, that it\u0027s inherited."},{"Start":"13:45.960 ","End":"13:50.580","Text":"There was no parameters or arguments needed."},{"Start":"13:50.580 ","End":"13:54.255","Text":"What we\u0027re going to do is we\u0027re going to use super not on its own."},{"Start":"13:54.255 ","End":"13:55.680","Text":"If you just use super on its own,"},{"Start":"13:55.680 ","End":"13:58.920","Text":"it\u0027s calling the constructor of the parent class."},{"Start":"13:58.920 ","End":"14:08.130","Text":"But if you put super and then dot and the name of a method in the parent class,"},{"Start":"14:08.130 ","End":"14:13.815","Text":"it will call the method from that class."},{"Start":"14:13.815 ","End":"14:16.350","Text":"What we\u0027re doing here is we\u0027re"},{"Start":"14:16.350 ","End":"14:20.579","Text":"inside display details is not a recursive call calling itself."},{"Start":"14:20.579 ","End":"14:25.335","Text":"What it\u0027s actually doing is calling the display details method that we are"},{"Start":"14:25.335 ","End":"14:30.900","Text":"overwriting here that\u0027s actually in the pen class,"},{"Start":"14:30.900 ","End":"14:32.775","Text":"which is this parent class."},{"Start":"14:32.775 ","End":"14:38.340","Text":"This is going to run display details from pen."},{"Start":"14:38.340 ","End":"14:43.140","Text":"But we are the display details method inside PromoPen."},{"Start":"14:43.140 ","End":"14:44.760","Text":"It\u0027s going to do a little bit more,"},{"Start":"14:44.760 ","End":"14:50.789","Text":"which is just to print the additional thing which is a message."},{"Start":"14:50.789 ","End":"14:54.240","Text":"All we\u0027re going to do it\u0027s we\u0027re going to add on a new line"},{"Start":"14:54.240 ","End":"15:01.515","Text":"the message preceded with a literal saying,"},{"Start":"15:01.515 ","End":"15:09.780","Text":"message, so message plus whatever it is that\u0027s contained in the message attribute."},{"Start":"15:09.780 ","End":"15:13.710","Text":"That\u0027s all we needed to do that and let\u0027s"},{"Start":"15:13.710 ","End":"15:17.174","Text":"go ahead and test what we\u0027ve done. What is going on here?"},{"Start":"15:17.174 ","End":"15:28.120","Text":"You\u0027ve got a bit of an error because I put 2 brackets in."},{"Start":"15:30.050 ","End":"15:32.295","Text":"That sorted out."},{"Start":"15:32.295 ","End":"15:38.440","Text":"Now we\u0027re going to create what we\u0027ve been asked to create in our testing."},{"Start":"15:39.140 ","End":"15:44.340","Text":"First of all, we are supposed to create a pen which is"},{"Start":"15:44.340 ","End":"15:49.020","Text":"blue and retractable and quantity set to 40,"},{"Start":"15:49.020 ","End":"15:54.053","Text":"so 40, color blue,"},{"Start":"15:54.053 ","End":"15:58.455","Text":"and retractable set to false."},{"Start":"15:58.455 ","End":"16:01.200","Text":"There\u0027s our first pen."},{"Start":"16:01.200 ","End":"16:03.465","Text":"Then another pen,"},{"Start":"16:03.465 ","End":"16:06.690","Text":"which is going to be red."},{"Start":"16:06.690 ","End":"16:08.940","Text":"Again, we\u0027ve got a quantity first,"},{"Start":"16:08.940 ","End":"16:12.270","Text":"which is 10 red,"},{"Start":"16:12.270 ","End":"16:20.175","Text":"and this time it is retractable and then finally we\u0027re going to do a PromoPen."},{"Start":"16:20.175 ","End":"16:23.415","Text":"We\u0027re going to create it from this class here."},{"Start":"16:23.415 ","End":"16:33.022","Text":"That began a stock 999 with color"},{"Start":"16:33.022 ","End":"16:39.930","Text":"black and retractable set"},{"Start":"16:39.930 ","End":"16:45.490","Text":"to true and the message we\u0027ve been given."},{"Start":"16:58.070 ","End":"17:01.890","Text":"Let\u0027s see what we get and run"},{"Start":"17:01.890 ","End":"17:05.160","Text":"display details on each of those objects to see what we expect."},{"Start":"17:05.160 ","End":"17:11.100","Text":"We expect this to be a blue pen and it\u0027s not"},{"Start":"17:11.100 ","End":"17:13.890","Text":"retractable and we\u0027ve got 40 in stock so let\u0027s see if that"},{"Start":"17:13.890 ","End":"17:18.165","Text":"works and yet seem to give us a description we expect."},{"Start":"17:18.165 ","End":"17:21.360","Text":"Then the next 1 was a red pen and it\u0027s"},{"Start":"17:21.360 ","End":"17:25.245","Text":"retractable on its modified the message that\u0027s displayed."},{"Start":"17:25.245 ","End":"17:29.250","Text":"We had tenants stock, that\u0027s right and then this last 1,"},{"Start":"17:29.250 ","End":"17:32.130","Text":"we\u0027ve inherited everything from pen,"},{"Start":"17:32.130 ","End":"17:36.540","Text":"but we\u0027ve overwritten display details to display the message as well."},{"Start":"17:36.540 ","End":"17:40.110","Text":"That seems to have worked as well."},{"Start":"17:40.110 ","End":"17:45.320","Text":"2 levels of inheritance here and we\u0027ve"},{"Start":"17:45.320 ","End":"17:49.160","Text":"overwritten display details here and"},{"Start":"17:49.160 ","End":"17:53.580","Text":"here to do something slightly different to the parent class,"},{"Start":"17:53.580 ","End":"17:57.570","Text":"ultimate ancestor class, which is item there."},{"Start":"17:57.570 ","End":"17:59.835","Text":"Relatively straightforward that one,"},{"Start":"17:59.835 ","End":"18:03.225","Text":"we get a bit more tricky in the next couple."},{"Start":"18:03.225 ","End":"18:05.920","Text":"Thanks for watching. See you then."}],"ID":31087},{"Watched":false,"Name":"Exercise 5 Part 1","Duration":"8m 54s","ChapterTopicVideoID":29479,"CourseChapterTopicPlaylistID":294470,"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.340","Text":"Hello, welcome to this exercise where we are asked to create a class called clothing,"},{"Start":"00:05.340 ","End":"00:09.735","Text":"which inherits from the item class we\u0027ve used in previous exercises."},{"Start":"00:09.735 ","End":"00:15.240","Text":"The clothing class is shown in a UML class diagram and we are asked to"},{"Start":"00:15.240 ","End":"00:17.610","Text":"note that the attributes and methods inherited from"},{"Start":"00:17.610 ","End":"00:21.225","Text":"Item are not shown in that class diagram."},{"Start":"00:21.225 ","End":"00:26.085","Text":"We\u0027re then told in Part A to implement all the methods in the clothing class."},{"Start":"00:26.085 ","End":"00:29.280","Text":"The implementation of display details should"},{"Start":"00:29.280 ","End":"00:32.835","Text":"show the size and color of the item as follows."},{"Start":"00:32.835 ","End":"00:35.820","Text":"An example was given description: t-shirt,"},{"Start":"00:35.820 ","End":"00:38.220","Text":"size: medium, color: white."},{"Start":"00:38.220 ","End":"00:40.930","Text":"In stock 7\"."},{"Start":"00:40.960 ","End":"00:45.980","Text":"In Part B, we\u0027re told to create another class, custom t-shirt,"},{"Start":"00:45.980 ","End":"00:51.245","Text":"which inherits from the clothing class and adds an extra attribute called text."},{"Start":"00:51.245 ","End":"00:55.700","Text":"We\u0027re told to ensure that the constructor for custom t-shirt accepts"},{"Start":"00:55.700 ","End":"01:00.275","Text":"an argument for the text attribute and initializes it to this value."},{"Start":"01:00.275 ","End":"01:02.810","Text":"There\u0027s no need for a description argument."},{"Start":"01:02.810 ","End":"01:08.860","Text":"Ensure the constructor always sets the description attribute to custom t-shirt."},{"Start":"01:08.860 ","End":"01:15.755","Text":"In part 2, we\u0027re told to implement an extra selector method for the new attribute."},{"Start":"01:15.755 ","End":"01:21.020","Text":"In part 3, we\u0027re told that display details for custom t-shirt should call"},{"Start":"01:21.020 ","End":"01:24.890","Text":"the display details method from the parent class and should"},{"Start":"01:24.890 ","End":"01:28.925","Text":"then output on a separate line that customized text,"},{"Start":"01:28.925 ","End":"01:33.015","Text":"e.g., \"Customization: Climate Action Now!\""},{"Start":"01:33.015 ","End":"01:35.069","Text":"Looking at the diagram,"},{"Start":"01:35.069 ","End":"01:42.390","Text":"we\u0027ve got 2 new attributes on top of the ones that we are going to get from Item."},{"Start":"01:42.390 ","End":"01:46.160","Text":"Let\u0027s, first of all, extend an item,"},{"Start":"01:46.160 ","End":"01:49.025","Text":"which is the definition for clothing."},{"Start":"01:49.025 ","End":"01:56.160","Text":"Then add these 2 extra fields and we\u0027re told to make them protected,"},{"Start":"01:56.160 ","End":"01:58.110","Text":"so let\u0027s do that."},{"Start":"01:58.110 ","End":"02:08.200","Text":"String, size, and string color."},{"Start":"02:08.200 ","End":"02:12.710","Text":"Now, let\u0027s implement the extra methods we need."},{"Start":"02:12.710 ","End":"02:15.020","Text":"I\u0027ll come to the constructor in a moment."},{"Start":"02:15.020 ","End":"02:18.770","Text":"Let\u0027s get size, get color first."},{"Start":"02:18.770 ","End":"02:22.680","Text":"Straightforward, get size return to string,"},{"Start":"02:22.680 ","End":"02:24.000","Text":"it\u0027s going to be public,"},{"Start":"02:24.000 ","End":"02:28.650","Text":"and that\u0027s just going to return the contents of"},{"Start":"02:28.650 ","End":"02:33.800","Text":"the size and now the method\u0027s going to be the same."},{"Start":"02:33.800 ","End":"02:38.239","Text":"This is such common activity to implement."},{"Start":"02:38.239 ","End":"02:39.610","Text":"Select a method,"},{"Start":"02:39.610 ","End":"02:42.335","Text":"some IDs will do this automatically for you."},{"Start":"02:42.335 ","End":"02:46.943","Text":"Because BlueJ is a learning environment,"},{"Start":"02:46.943 ","End":"02:53.760","Text":"it doesn\u0027t do that for us because it\u0027s practice that you need and when you\u0027re learning."},{"Start":"02:53.760 ","End":"02:57.410","Text":"The more you do it, the more you internalize this concept."},{"Start":"02:57.410 ","End":"03:01.535","Text":"Returning color, we\u0027re returning size,"},{"Start":"03:01.535 ","End":"03:04.105","Text":"I need to say what those types are."},{"Start":"03:04.105 ","End":"03:06.180","Text":"They\u0027re both strings."},{"Start":"03:06.180 ","End":"03:09.575","Text":"I duct the constructor,"},{"Start":"03:09.575 ","End":"03:13.430","Text":"but let\u0027s put the constructor together."},{"Start":"03:13.430 ","End":"03:18.830","Text":"We do need to set the values of the fields that we\u0027ve added,"},{"Start":"03:18.830 ","End":"03:20.380","Text":"size, and color."},{"Start":"03:20.380 ","End":"03:23.910","Text":"We\u0027re going to have to do that in the constructor."},{"Start":"03:23.910 ","End":"03:29.660","Text":"Let\u0027s create a constructor by giving it a name, clothing."},{"Start":"03:29.660 ","End":"03:31.925","Text":"It\u0027s going to take a bunch of strings."},{"Start":"03:31.925 ","End":"03:36.900","Text":"First one is going to be description because we need that."},{"Start":"03:36.910 ","End":"03:42.575","Text":"The second one is going to be size."},{"Start":"03:42.575 ","End":"03:50.190","Text":"Next one\u0027s going to be color and then an int for quantity."},{"Start":"03:50.190 ","End":"03:55.400","Text":"We\u0027re going to need to call the constructor for the parent class and pass"},{"Start":"03:55.400 ","End":"04:00.585","Text":"along the description and the quantity."},{"Start":"04:00.585 ","End":"04:03.600","Text":"Then in here, we can set"},{"Start":"04:03.600 ","End":"04:11.765","Text":"the size and color attributes to the ones that have been passed in."},{"Start":"04:11.765 ","End":"04:15.960","Text":"There is our constructor for clothing"},{"Start":"04:15.960 ","End":"04:21.020","Text":"and the last thing we need to do is to implement the display details class,"},{"Start":"04:21.020 ","End":"04:23.970","Text":"which returns nothing so avoid there."},{"Start":"04:23.970 ","End":"04:28.575","Text":"This is going to display pretty much all the fields that we\u0027ve got."},{"Start":"04:28.575 ","End":"04:31.910","Text":"Description, we have access to"},{"Start":"04:31.910 ","End":"04:35.875","Text":"description even though it\u0027s not defined here because it\u0027s protected."},{"Start":"04:35.875 ","End":"04:44.870","Text":"We inherit access to it and then we have size in the size field,"},{"Start":"04:44.870 ","End":"04:49.779","Text":"then we have color in color field."},{"Start":"04:49.779 ","End":"04:52.715","Text":"Then finally, how many we\u0027ve got in stock."},{"Start":"04:52.715 ","End":"05:00.155","Text":"There is our display details method done."},{"Start":"05:00.155 ","End":"05:04.790","Text":"We\u0027ll check that when we do the testing at the end."},{"Start":"05:04.790 ","End":"05:08.465","Text":"Now we need to create another class called custom t-shirt,"},{"Start":"05:08.465 ","End":"05:14.720","Text":"which inherits from the clothing class and adds an extra attribute called text."},{"Start":"05:14.720 ","End":"05:16.250","Text":"Let\u0027s go ahead and do that."},{"Start":"05:16.250 ","End":"05:18.920","Text":"I\u0027ve already created an empty class here."},{"Start":"05:18.920 ","End":"05:21.515","Text":"Let\u0027s fill it in with some code."},{"Start":"05:21.515 ","End":"05:26.520","Text":"This extends clothing."},{"Start":"05:29.690 ","End":"05:33.600","Text":"But it has an extra attribute now."},{"Start":"05:33.600 ","End":"05:37.140","Text":"Let\u0027s make that private,"},{"Start":"05:37.140 ","End":"05:40.360","Text":"I haven\u0027t been told anything otherwise."},{"Start":"05:41.540 ","End":"05:43.755","Text":"It\u0027s called texts,"},{"Start":"05:43.755 ","End":"05:45.760","Text":"so presumably it\u0027s a string."},{"Start":"05:45.760 ","End":"05:50.665","Text":"Now the constructor for custom t-shirt we\u0027d been told,"},{"Start":"05:50.665 ","End":"05:54.280","Text":"takes an argument for the text attribute and initialize it to this value."},{"Start":"05:54.280 ","End":"05:58.045","Text":"There\u0027s no need for a description argument."},{"Start":"05:58.045 ","End":"06:00.925","Text":"We always need to set that to custom t-shirt."},{"Start":"06:00.925 ","End":"06:05.455","Text":"Let\u0027s do what we need to do here."},{"Start":"06:05.455 ","End":"06:13.100","Text":"Custom t-shirt is the name of the constructor."},{"Start":"06:13.650 ","End":"06:16.330","Text":"It doesn\u0027t return a value,"},{"Start":"06:16.330 ","End":"06:18.610","Text":"but it does need various values."},{"Start":"06:18.610 ","End":"06:22.375","Text":"The first thing is a description."},{"Start":"06:22.375 ","End":"06:24.310","Text":"Actually, we don\u0027t need a description."},{"Start":"06:24.310 ","End":"06:29.005","Text":"We\u0027ve been told that, we need all the other bits though."},{"Start":"06:29.005 ","End":"06:32.065","Text":"We need the size, color,"},{"Start":"06:32.065 ","End":"06:35.595","Text":"and quantity, which is an int."},{"Start":"06:35.595 ","End":"06:44.020","Text":"Then what we need to do is to pass to the parent class a fixed description,"},{"Start":"06:44.020 ","End":"06:47.515","Text":"which is custom t-shirt,"},{"Start":"06:47.515 ","End":"06:50.245","Text":"is always going to be custom t-shirt."},{"Start":"06:50.245 ","End":"06:55.830","Text":"But we\u0027re going to pass on the values of size,"},{"Start":"06:55.830 ","End":"06:59.855","Text":"color, and quantity."},{"Start":"06:59.855 ","End":"07:05.300","Text":"The only thing we haven\u0027t set is text."},{"Start":"07:05.300 ","End":"07:10.665","Text":"We do need that extra attribute up here."},{"Start":"07:10.665 ","End":"07:15.950","Text":"Let\u0027s put that on the end text and then"},{"Start":"07:15.950 ","End":"07:21.650","Text":"here we\u0027re going to say this dot text equals text."},{"Start":"07:21.650 ","End":"07:30.690","Text":"We have been asked to add an extra selector method for this new attribute."},{"Start":"07:30.690 ","End":"07:38.570","Text":"We\u0027ll do that and then the final thing to do is to display or have"},{"Start":"07:38.570 ","End":"07:43.070","Text":"a new version of display details and"},{"Start":"07:43.070 ","End":"07:48.290","Text":"this should call display details from the parent class."},{"Start":"07:48.290 ","End":"07:52.190","Text":"Interestingly, we haven\u0027t done that before so that is done by doing"},{"Start":"07:52.190 ","End":"07:58.840","Text":"this super dot and then the name of the method you want to call in the parent class."},{"Start":"07:58.840 ","End":"08:02.570","Text":"If the parent class of this is clothing,"},{"Start":"08:02.570 ","End":"08:07.229","Text":"this will call the display details method from clothing."},{"Start":"08:07.570 ","End":"08:12.620","Text":"But what we also need to do is to output an extra value"},{"Start":"08:12.620 ","End":"08:21.270","Text":"for a customized text."},{"Start":"08:29.300 ","End":"08:36.605","Text":"We\u0027ve now implemented this class missing the return type there."},{"Start":"08:36.605 ","End":"08:38.420","Text":"We\u0027re returning a string."},{"Start":"08:38.420 ","End":"08:41.195","Text":"I need to make that public string."},{"Start":"08:41.195 ","End":"08:44.990","Text":"That\u0027s it for parts A and B."},{"Start":"08:44.990 ","End":"08:48.680","Text":"In the next video, we\u0027ll do the final class,"},{"Start":"08:48.680 ","End":"08:51.620","Text":"customary, and do all the testing."},{"Start":"08:51.620 ","End":"08:54.000","Text":"See you shortly for that one."}],"ID":31088},{"Watched":false,"Name":"Exercise 5 Part 2","Duration":"9m 27s","ChapterTopicVideoID":29478,"CourseChapterTopicPlaylistID":294470,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:06.045","Text":"Hello. Welcome back to the second part of this exercise in which we create another class,"},{"Start":"00:06.045 ","End":"00:09.825","Text":"CustomHoody which inherits from the clothing class and adds"},{"Start":"00:09.825 ","End":"00:13.995","Text":"an extra 2 attributes: frontText and backText."},{"Start":"00:13.995 ","End":"00:15.975","Text":"In Part 1 of C,"},{"Start":"00:15.975 ","End":"00:18.885","Text":"we are told there\u0027s no need for a description argument."},{"Start":"00:18.885 ","End":"00:24.435","Text":"We ensure that constructor always sets the description attribute to CustomHoody."},{"Start":"00:24.435 ","End":"00:27.350","Text":"In Part 2, we\u0027re told that the constructor will not need"},{"Start":"00:27.350 ","End":"00:31.265","Text":"any extra arguments to set the values of frontText and backText."},{"Start":"00:31.265 ","End":"00:34.790","Text":"Instead, we provide a modifier method, setText,"},{"Start":"00:34.790 ","End":"00:41.165","Text":"which takes 2 arguments and set frontText and backText to the values passed in."},{"Start":"00:41.165 ","End":"00:45.680","Text":"In Part 3, we\u0027re told that setText should ensure that the text is no"},{"Start":"00:45.680 ","End":"00:50.434","Text":"longer than 25 characters for either of the 2 values."},{"Start":"00:50.434 ","End":"00:53.855","Text":"If setText was able to set the text for both attributes,"},{"Start":"00:53.855 ","End":"00:56.060","Text":"it returns the Boolean value true,"},{"Start":"00:56.060 ","End":"00:58.975","Text":"otherwise it returns false."},{"Start":"00:58.975 ","End":"01:02.250","Text":"displayDetails for CustomHoody should call"},{"Start":"01:02.250 ","End":"01:05.233","Text":"the displayDetails method from the parent class,"},{"Start":"01:05.233 ","End":"01:09.305","Text":"and should output on a separate line the customized text."},{"Start":"01:09.305 ","End":"01:13.055","Text":"Let me say that again, [inaudible], the customized text."},{"Start":"01:13.055 ","End":"01:18.230","Text":"For example, customization front: Cranhurst College CS."},{"Start":"01:18.230 ","End":"01:22.580","Text":"Customization back: Class of 2022."},{"Start":"01:22.580 ","End":"01:25.970","Text":"We then test by creating objects as follows."},{"Start":"01:25.970 ","End":"01:29.675","Text":"Clothing, shorts, medium, khaki, 5."},{"Start":"01:29.675 ","End":"01:31.940","Text":"CustomTshirt, small,"},{"Start":"01:31.940 ","End":"01:35.140","Text":"green, 100, climate action now."},{"Start":"01:35.140 ","End":"01:39.280","Text":"A CustomHoody, large, blue, 40."},{"Start":"01:39.280 ","End":"01:43.415","Text":"When we run setText on the object created in Part 3,"},{"Start":"01:43.415 ","End":"01:45.010","Text":"we give it the values,"},{"Start":"01:45.010 ","End":"01:49.565","Text":"Cranhurst College CS and Class of 2022."},{"Start":"01:49.565 ","End":"01:56.214","Text":"We then run display details on all 3 objects to see that the output is what we expect."},{"Start":"01:56.214 ","End":"02:01.265","Text":"Welcome back to this next part where we implement Part C,"},{"Start":"02:01.265 ","End":"02:03.905","Text":"and the new class called CustomHoody."},{"Start":"02:03.905 ","End":"02:10.175","Text":"I\u0027ve created a new class and I\u0027m about to implement it as we\u0027ve been asked to."},{"Start":"02:10.175 ","End":"02:14.645","Text":"There are 2 extra attributes, frontText and backText."},{"Start":"02:14.645 ","End":"02:18.380","Text":"I\u0027m going to presume that strings and that they\u0027re private."},{"Start":"02:18.380 ","End":"02:20.855","Text":"Because they\u0027re both the same types,"},{"Start":"02:20.855 ","End":"02:24.810","Text":"I can just declare those on the same line."},{"Start":"02:24.810 ","End":"02:33.080","Text":"Now my constructor doesn\u0027t need a description argument as the t-shirt;"},{"Start":"02:33.080 ","End":"02:35.450","Text":"custom t-shirt didn\u0027t either."},{"Start":"02:35.450 ","End":"02:40.435","Text":"It doesn\u0027t need any arguments for frontText and backText either."},{"Start":"02:40.435 ","End":"02:43.910","Text":"We\u0027re going to do a separate method for that in Part 2,"},{"Start":"02:43.910 ","End":"02:46.700","Text":"but I do need to create a constructor for it,"},{"Start":"02:46.700 ","End":"02:49.265","Text":"so let\u0027s do that."},{"Start":"02:49.265 ","End":"02:51.665","Text":"It\u0027s going to be public,"},{"Start":"02:51.665 ","End":"02:55.110","Text":"it\u0027s going to be called CustomHoody."},{"Start":"02:55.250 ","End":"02:58.325","Text":"What are all the things that I need?"},{"Start":"02:58.325 ","End":"03:00.545","Text":"I don\u0027t need a description,"},{"Start":"03:00.545 ","End":"03:05.350","Text":"but I do need a color and a size."},{"Start":"03:05.350 ","End":"03:09.635","Text":"A string, make sure I get this in the right order."},{"Start":"03:09.635 ","End":"03:12.620","Text":"Previously, we declared size and color first,"},{"Start":"03:12.620 ","End":"03:14.405","Text":"so I\u0027ll do it in that order again."},{"Start":"03:14.405 ","End":"03:21.600","Text":"Size, string, color, and then int quantity."},{"Start":"03:21.600 ","End":"03:25.335","Text":"The only thing we\u0027re missing is the description."},{"Start":"03:25.335 ","End":"03:31.705","Text":"Then we\u0027re going to call a parent class and pass it a description of"},{"Start":"03:31.705 ","End":"03:41.500","Text":"CustomHoody and then pass on size, color, and quantity."},{"Start":"03:41.500 ","End":"03:45.050","Text":"There are no other attributes to set for now,"},{"Start":"03:45.050 ","End":"03:49.115","Text":"so frontText and backText won\u0027t have values."},{"Start":"03:49.115 ","End":"03:56.300","Text":"What we\u0027ve been asked to do is to provide a method called setText,"},{"Start":"03:56.300 ","End":"03:58.340","Text":"which takes arguments and sets"},{"Start":"03:58.340 ","End":"04:01.775","Text":"frontTexts and backText to those values that are passed in."},{"Start":"04:01.775 ","End":"04:05.630","Text":"Let\u0027s do that. It\u0027s going a bit of a complaint here."},{"Start":"04:05.630 ","End":"04:07.265","Text":"What\u0027s that about?"},{"Start":"04:07.265 ","End":"04:13.400","Text":"It\u0027s expecting a different set of arguments required."},{"Start":"04:13.400 ","End":"04:19.340","Text":"No arguments because I haven\u0027t extended any class yet."},{"Start":"04:19.340 ","End":"04:23.390","Text":"Extends clothing, and that\u0027s why it\u0027s complaining."},{"Start":"04:23.390 ","End":"04:27.155","Text":"That\u0027s going away. Let\u0027s do setText now."},{"Start":"04:27.155 ","End":"04:30.850","Text":"Public setText."},{"Start":"04:30.850 ","End":"04:39.265","Text":"We\u0027d need 2 attributes: frontText and backText."},{"Start":"04:39.265 ","End":"04:45.990","Text":"But what we have been asked to do is to check the length of those."},{"Start":"04:46.400 ","End":"04:55.325","Text":"If frontText.length, is a method built into every string,"},{"Start":"04:55.325 ","End":"05:00.845","Text":"is less than or equal to 25 because it does say"},{"Start":"05:00.845 ","End":"05:07.040","Text":"longer than 25 characters and backText as well."},{"Start":"05:07.040 ","End":"05:14.980","Text":"If it is, we go ahead and change our values and return true."},{"Start":"05:15.130 ","End":"05:21.740","Text":"I\u0027ll just put that in there, otherwise it will return false."},{"Start":"05:21.740 ","End":"05:31.450","Text":"The only change I need to make is to the return value, which is a Boolean."},{"Start":"05:32.990 ","End":"05:37.280","Text":"Final thing to do then is to actually set the values of"},{"Start":"05:37.280 ","End":"05:42.345","Text":"frontText and backText. That seems good."},{"Start":"05:42.345 ","End":"05:48.045","Text":"Last thing to do is to change display details."},{"Start":"05:48.045 ","End":"05:49.715","Text":"We\u0027re going to go ahead and use,"},{"Start":"05:49.715 ","End":"05:51.380","Text":"as we did previously,"},{"Start":"05:51.380 ","End":"05:57.690","Text":"the display details from a parent class,"},{"Start":"05:58.120 ","End":"06:03.620","Text":"but afterwards, we\u0027re going to print the customization."},{"Start":"06:03.620 ","End":"06:06.170","Text":"This could be quite long."},{"Start":"06:06.170 ","End":"06:12.600","Text":"I think I put a new line character in there to split it onto a second line."},{"Start":"06:13.000 ","End":"06:16.920","Text":"That is what we\u0027ve been asked to do."},{"Start":"06:17.450 ","End":"06:21.545","Text":"Let\u0027s test as we\u0027ve been asked to do in Part D,"},{"Start":"06:21.545 ","End":"06:25.895","Text":"and we should now have quite a big hierarchy of classes."},{"Start":"06:25.895 ","End":"06:27.740","Text":"I\u0027ve rearranged these manually."},{"Start":"06:27.740 ","End":"06:30.035","Text":"They\u0027ll probably all be jumbled together on yours,"},{"Start":"06:30.035 ","End":"06:33.485","Text":"but you can just move them around as you see fit."},{"Start":"06:33.485 ","End":"06:37.315","Text":"As you can see, we\u0027ve got 2 levels of inheritance now."},{"Start":"06:37.315 ","End":"06:41.090","Text":"CustomTshirt and CustomHoody both inherit from clothing,"},{"Start":"06:41.090 ","End":"06:43.820","Text":"which itself inherits from item,"},{"Start":"06:43.820 ","End":"06:47.950","Text":"or a special item just inherits from item."},{"Start":"06:47.950 ","End":"06:52.175","Text":"Let\u0027s create an item of clothing. Click on there."},{"Start":"06:52.175 ","End":"06:56.555","Text":"We\u0027ve been asked to create some shorts,"},{"Start":"06:56.555 ","End":"07:05.065","Text":"which we have in stock in medium, color;khaki."},{"Start":"07:05.065 ","End":"07:08.435","Text":"We have 5 of them."},{"Start":"07:08.435 ","End":"07:10.625","Text":"That\u0027s that one done."},{"Start":"07:10.625 ","End":"07:17.530","Text":"Let\u0027s do the customerTshirt next."},{"Start":"07:17.530 ","End":"07:23.160","Text":"Create a new CustomTshirt that has been almost"},{"Start":"07:23.160 ","End":"07:31.050","Text":"to be small and green,"},{"Start":"07:31.050 ","End":"07:38.190","Text":"quantity;100, and got a slogan on there that look like so."},{"Start":"07:38.190 ","End":"07:41.025","Text":"That seems to have done that."},{"Start":"07:41.025 ","End":"07:45.130","Text":"Then the last one, CustomHoody."},{"Start":"07:46.090 ","End":"07:51.920","Text":"We\u0027re going to have a size of a large,"},{"Start":"07:51.920 ","End":"07:54.415","Text":"color of blue,"},{"Start":"07:54.415 ","End":"07:57.590","Text":"and we\u0027re going to have 40 of those."},{"Start":"07:57.590 ","End":"08:02.695","Text":"But this time we\u0027re going to run another method on here, which is setText."},{"Start":"08:02.695 ","End":"08:06.010","Text":"Only CustomHoody has setText."},{"Start":"08:06.010 ","End":"08:07.700","Text":"We\u0027ve got getText and the other one,"},{"Start":"08:07.700 ","End":"08:09.605","Text":"but we don\u0027t have setText."},{"Start":"08:09.605 ","End":"08:18.090","Text":"Let\u0027s do a setText and pass it in these tests values of"},{"Start":"08:18.090 ","End":"08:29.050","Text":"Cranhurst College CS and Class of 2022, pun not intended."},{"Start":"08:30.080 ","End":"08:33.485","Text":"There we go, we get true back."},{"Start":"08:33.485 ","End":"08:35.705","Text":"Now let\u0027s run all these things."},{"Start":"08:35.705 ","End":"08:39.050","Text":"For clothing, when we do display details,"},{"Start":"08:39.050 ","End":"08:42.140","Text":"we get the plain old description,"},{"Start":"08:42.140 ","End":"08:44.345","Text":"including the size and color,"},{"Start":"08:44.345 ","End":"08:46.100","Text":"which we don\u0027t have for a normal item;"},{"Start":"08:46.100 ","End":"08:49.055","Text":"we just have description, and a quantity."},{"Start":"08:49.055 ","End":"08:51.005","Text":"That\u0027s work fine."},{"Start":"08:51.005 ","End":"08:53.990","Text":"If we do it from CustomTshirt,"},{"Start":"08:53.990 ","End":"08:55.595","Text":"we get the same stuff,"},{"Start":"08:55.595 ","End":"08:59.660","Text":"except we didn\u0027t supply a parameter which contain what it was."},{"Start":"08:59.660 ","End":"09:05.055","Text":"We passed in the default value of CustomTshirt to the base class constructor."},{"Start":"09:05.055 ","End":"09:09.590","Text":"We have this customization coming from a different attribute, which is great."},{"Start":"09:09.590 ","End":"09:14.000","Text":"Then the final one is the hoodie,"},{"Start":"09:14.000 ","End":"09:16.430","Text":"which also does the same thing,"},{"Start":"09:16.430 ","End":"09:20.930","Text":"has a different value here and it has a front and back customization."},{"Start":"09:20.930 ","End":"09:24.260","Text":"All of that seems to have worked just as we expected."},{"Start":"09:24.260 ","End":"09:27.420","Text":"Thank you very much for watching. See you in the next one."}],"ID":31089},{"Watched":false,"Name":"Exercise 6 Part 1","Duration":"8m 26s","ChapterTopicVideoID":29477,"CourseChapterTopicPlaylistID":294470,"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.705","Text":"Hello, welcome back."},{"Start":"00:01.705 ","End":"00:02.920","Text":"In this exercise,"},{"Start":"00:02.920 ","End":"00:04.870","Text":"we\u0027ve been asked to create a new project and"},{"Start":"00:04.870 ","End":"00:07.600","Text":"3 classes within it as per the diagram below,"},{"Start":"00:07.600 ","End":"00:09.835","Text":"a vehicle class, a car class,"},{"Start":"00:09.835 ","End":"00:11.455","Text":"and a boat class,"},{"Start":"00:11.455 ","End":"00:15.775","Text":"with the relationship that\u0027s been implied in the diagram."},{"Start":"00:15.775 ","End":"00:20.740","Text":"In Part A, we\u0027re told to implement the vehicle constructor so it has"},{"Start":"00:20.740 ","End":"00:23.080","Text":"a length and color arguments stored into"},{"Start":"00:23.080 ","End":"00:28.570","Text":"the relevant attribute and initial speed and damage values of 0."},{"Start":"00:28.570 ","End":"00:32.575","Text":"In Part B, we\u0027re told a car can be automatic or manual,"},{"Start":"00:32.575 ","End":"00:35.515","Text":"and we implement the constructor so an extra argument"},{"Start":"00:35.515 ","End":"00:38.934","Text":"determines the value of the automatic attribute,"},{"Start":"00:38.934 ","End":"00:43.055","Text":"and another argument sets the top speed attribute."},{"Start":"00:43.055 ","End":"00:48.250","Text":"A boat always has a motor but can have 0 or more sails."},{"Start":"00:48.250 ","End":"00:52.060","Text":"Implement the constructor so that the number of sales is passed"},{"Start":"00:52.060 ","End":"00:56.380","Text":"in as an argument to set the sales attribute."},{"Start":"00:56.380 ","End":"01:03.715","Text":"In Part D, we\u0027re told to implement the collide method so that it adds 10 to damage"},{"Start":"01:03.715 ","End":"01:11.715","Text":"each time it\u0027s called up to a limit of a 100.5 speed while speed is more than 0."},{"Start":"01:11.715 ","End":"01:17.200","Text":"We\u0027ve got to create 3 classes here and because current boat inherit from vehicle,"},{"Start":"01:17.200 ","End":"01:19.210","Text":"we\u0027re going to start on vehicle."},{"Start":"01:19.210 ","End":"01:22.810","Text":"Let\u0027s start by putting the attributes in,"},{"Start":"01:22.810 ","End":"01:25.880","Text":"these are all protected."},{"Start":"01:28.140 ","End":"01:33.110","Text":"We\u0027ve got first of all an int for length,"},{"Start":"01:35.880 ","End":"01:46.400","Text":"string for color, an int for damage,"},{"Start":"01:46.840 ","End":"01:51.125","Text":"and finally, an int for speed."},{"Start":"01:51.125 ","End":"01:54.710","Text":"Then we\u0027d been asked to create a constructor for"},{"Start":"01:54.710 ","End":"01:59.150","Text":"vehicle so that it sets length and color to whatever is passed in,"},{"Start":"01:59.150 ","End":"02:05.210","Text":"and an initial speed and damage values of 0."},{"Start":"02:05.210 ","End":"02:06.995","Text":"Let\u0027s do that and"},{"Start":"02:06.995 ","End":"02:15.390","Text":"public constructor called vehicle which takes in length and color arguments."},{"Start":"02:15.390 ","End":"02:21.010","Text":"Length is an int and color is string."},{"Start":"02:21.890 ","End":"02:28.925","Text":"We set those instance variables to their respective past and values,"},{"Start":"02:28.925 ","End":"02:34.800","Text":"speed to 0 and damage to 0."},{"Start":"02:34.800 ","End":"02:41.760","Text":"That\u0027s vehicle done Part A and then Part B."},{"Start":"02:41.760 ","End":"02:48.515","Text":"Our class car extends vehicle according to that diagram,"},{"Start":"02:48.515 ","End":"02:50.225","Text":"so don\u0027t forget to put that in,"},{"Start":"02:50.225 ","End":"02:54.620","Text":"and what it has that a vehicle doesn\u0027t have"},{"Start":"02:54.620 ","End":"02:59.870","Text":"is an attribute called automatic, which is a Boolean."},{"Start":"02:59.870 ","End":"03:09.530","Text":"This is private. Then it also has int private as well and the constructor has"},{"Start":"03:09.530 ","End":"03:12.470","Text":"to determine the value of"},{"Start":"03:12.470 ","End":"03:20.930","Text":"that automatic attribute and another argument will set the top speed attributes."},{"Start":"03:20.930 ","End":"03:22.580","Text":"Let\u0027s do of course,"},{"Start":"03:22.580 ","End":"03:25.565","Text":"still need to get values in for"},{"Start":"03:25.565 ","End":"03:30.940","Text":"the other attributes that are needed for a vehicle and pass those in as well."},{"Start":"03:30.940 ","End":"03:33.090","Text":"We got to collect all of those."},{"Start":"03:33.090 ","End":"03:36.945","Text":"We need the length, we need the color,"},{"Start":"03:36.945 ","End":"03:42.335","Text":"and then these new values of automatic,"},{"Start":"03:42.335 ","End":"03:46.590","Text":"it\u0027s a Boolean, and then finally, the top speed."},{"Start":"03:46.590 ","End":"03:50.935","Text":"Set those values up."},{"Start":"03:50.935 ","End":"03:57.605","Text":"Actually, I have made a mistake here."},{"Start":"03:57.605 ","End":"04:01.580","Text":"I need to call this car not vehicle."},{"Start":"04:01.580 ","End":"04:07.820","Text":"It\u0027s the vehicle class that we\u0027re inheriting from and the constructor"},{"Start":"04:07.820 ","End":"04:13.925","Text":"for vehicle needs to have a set of arguments passed to it,"},{"Start":"04:13.925 ","End":"04:17.720","Text":"which are going to be length and color,"},{"Start":"04:17.720 ","End":"04:26.855","Text":"and then we should set automatic and top speed to whatever has been passed in."},{"Start":"04:26.855 ","End":"04:34.265","Text":"There\u0027s our constructor for car done and that seems to compile no problem."},{"Start":"04:34.265 ","End":"04:39.500","Text":"The final thing is to do that third class, the boat."},{"Start":"04:39.500 ","End":"04:44.750","Text":"Let\u0027s go ahead and create a new class called boat."},{"Start":"04:44.750 ","End":"04:48.800","Text":"A boat has 2 attributes that are different to"},{"Start":"04:48.800 ","End":"04:54.515","Text":"the car that it has sails but it does have a top speed."},{"Start":"04:54.515 ","End":"04:56.930","Text":"But the base class doesn\u0027t have a top speed,"},{"Start":"04:56.930 ","End":"05:00.695","Text":"so we need to implement in this class."},{"Start":"05:00.695 ","End":"05:02.395","Text":"Let\u0027s do that."},{"Start":"05:02.395 ","End":"05:06.245","Text":"These are both going to be private according to the diagram."},{"Start":"05:06.245 ","End":"05:12.590","Text":"Private int sails and private."},{"Start":"05:12.590 ","End":"05:21.965","Text":"We\u0027ll need a constructor that sets the number of sails and the top speed."},{"Start":"05:21.965 ","End":"05:25.565","Text":"Of course, is going to need all of the other parameters as well,"},{"Start":"05:25.565 ","End":"05:29.040","Text":"which were length of the vehicle,"},{"Start":"05:29.040 ","End":"05:31.190","Text":"color of the vehicle,"},{"Start":"05:31.190 ","End":"05:34.415","Text":"and that\u0027s all we need for a constructor."},{"Start":"05:34.415 ","End":"05:40.380","Text":"But again, we need to pass on to the vehicle constructor."},{"Start":"05:41.290 ","End":"05:48.294","Text":"The length and the color."},{"Start":"05:48.294 ","End":"05:55.190","Text":"I had forgotten here also to put a bit of that is"},{"Start":"05:55.190 ","End":"06:03.490","Text":"going to do the inheritance a boat extends a vehicle."},{"Start":"06:03.490 ","End":"06:07.790","Text":"Now we should have a relationship that looks like this."},{"Start":"06:07.790 ","End":"06:10.790","Text":"We\u0027ve got a boat and a car,"},{"Start":"06:10.790 ","End":"06:14.260","Text":"they both inherit from a vehicle."},{"Start":"06:14.260 ","End":"06:19.570","Text":"We\u0027ve been asked to implement a pair of methods here, collide,"},{"Start":"06:19.570 ","End":"06:25.180","Text":"so that it adds 10 to damage each time it\u0027s called up to a limit of 100,"},{"Start":"06:25.180 ","End":"06:29.805","Text":"and it halves the speed where the speed is more than 0."},{"Start":"06:29.805 ","End":"06:35.230","Text":"When damage equals a 100 speed should be set to 0. Where is collide?"},{"Start":"06:35.230 ","End":"06:37.314","Text":"Collide is inside vehicle,"},{"Start":"06:37.314 ","End":"06:40.090","Text":"because all types of vehicle can"},{"Start":"06:40.090 ","End":"06:44.785","Text":"collide whether it\u0027s a generic vehicle or a car or a boat."},{"Start":"06:44.785 ","End":"06:48.260","Text":"Let\u0027s implement collide then."},{"Start":"06:48.980 ","End":"06:51.960","Text":"The method is called collide."},{"Start":"06:51.960 ","End":"06:54.590","Text":"No arguments we\u0027ve been told about."},{"Start":"06:54.590 ","End":"07:02.295","Text":"But what we\u0027re going to do is we\u0027re going to say damage equals damage plus 10,"},{"Start":"07:02.295 ","End":"07:07.205","Text":"and if damage goes over a 100,"},{"Start":"07:07.205 ","End":"07:11.300","Text":"then we need to make damage limited to 100."},{"Start":"07:11.300 ","End":"07:17.130","Text":"We\u0027ll just say damage equals a 100 if it goes above."},{"Start":"07:17.810 ","End":"07:26.320","Text":"We also need to half the speed whilst the speed is more than 0."},{"Start":"07:26.320 ","End":"07:31.125","Text":"The speed is greater than 0,"},{"Start":"07:31.125 ","End":"07:35.745","Text":"speed divided by 2. Should do it."},{"Start":"07:35.745 ","End":"07:38.130","Text":"Actually, that could take us below 0,"},{"Start":"07:38.130 ","End":"07:44.170","Text":"so let\u0027s do that"},{"Start":"07:44.170 ","End":"07:51.105","Text":"but only if speed divided by 2 is greater than 0."},{"Start":"07:51.105 ","End":"07:54.500","Text":"That\u0027s probably better, and let\u0027s put some brackets around this."},{"Start":"07:54.500 ","End":"07:58.255","Text":"This is integer division as well."},{"Start":"07:58.255 ","End":"08:02.900","Text":"If we divide the speed by 2 and it\u0027s still over 0,"},{"Start":"08:02.900 ","End":"08:05.150","Text":"then we divide the speed by 2,"},{"Start":"08:05.150 ","End":"08:07.415","Text":"otherwise we leave it where it is."},{"Start":"08:07.415 ","End":"08:10.997","Text":"That seems to be sensible for collide."},{"Start":"08:10.997 ","End":"08:15.100","Text":"I forgot my void here."},{"Start":"08:15.380 ","End":"08:23.870","Text":"Let\u0027s just pause there and we will continue the other methods in the next video."},{"Start":"08:23.870 ","End":"08:26.160","Text":"See you shortly for that one."}],"ID":31090},{"Watched":false,"Name":"Exercise 6 Part 2","Duration":"16m 43s","ChapterTopicVideoID":29476,"CourseChapterTopicPlaylistID":294470,"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.785","Text":"Hello again, welcome back."},{"Start":"00:01.785 ","End":"00:03.495","Text":"In the previous video,"},{"Start":"00:03.495 ","End":"00:07.680","Text":"for this exercise we had implemented the vehicle,"},{"Start":"00:07.680 ","End":"00:11.340","Text":"car, and boat classes plus the collide method."},{"Start":"00:11.340 ","End":"00:13.680","Text":"Now in Part E, we\u0027re to implement"},{"Start":"00:13.680 ","End":"00:19.110","Text":"the repair method so that it resets damage to 0 when called."},{"Start":"00:19.110 ","End":"00:22.830","Text":"In Part F, we implement the faster method so that"},{"Start":"00:22.830 ","End":"00:26.865","Text":"it increases the value of the current speed attribute by 1,"},{"Start":"00:26.865 ","End":"00:33.980","Text":"as long as it is less than 5 for vehicle and it increases the same attribute by 10,"},{"Start":"00:33.980 ","End":"00:37.825","Text":"so long as it\u0027s less than top speed for car objects."},{"Start":"00:37.825 ","End":"00:41.255","Text":"It increases the value of current speed by 2,"},{"Start":"00:41.255 ","End":"00:45.755","Text":"so long as it\u0027s less than top speed for boat objects."},{"Start":"00:45.755 ","End":"00:49.115","Text":"In Part G, we\u0027re told to implement the move method"},{"Start":"00:49.115 ","End":"00:52.130","Text":"in vehicle so that it displays a message saying,"},{"Start":"00:52.130 ","End":"00:53.915","Text":"vehicle is moving at,"},{"Start":"00:53.915 ","End":"00:56.885","Text":"followed by the speed in miles per hour,"},{"Start":"00:56.885 ","End":"00:59.150","Text":"while damage is less than a 100,"},{"Start":"00:59.150 ","End":"01:03.065","Text":"otherwise it displays vehicle cannot move."},{"Start":"01:03.065 ","End":"01:07.550","Text":"In Part 2, we\u0027re told that we\u0027re to implement moving car,"},{"Start":"01:07.550 ","End":"01:11.015","Text":"so that it displays a message saying car is being driven at,"},{"Start":"01:11.015 ","End":"01:14.060","Text":"followed by the speed and miles per hour,"},{"Start":"01:14.060 ","End":"01:16.325","Text":"while damage is less than a 100."},{"Start":"01:16.325 ","End":"01:19.780","Text":"Otherwise, it displays car cannot be driven."},{"Start":"01:19.780 ","End":"01:25.280","Text":"Then moving boats should be implemented so that it displays a message saying,"},{"Start":"01:25.280 ","End":"01:27.410","Text":"boat is being sailed at,"},{"Start":"01:27.410 ","End":"01:32.870","Text":"followed by the speed and not while damage is less than a 100."},{"Start":"01:32.870 ","End":"01:37.055","Text":"Otherwise, it displays boat cannot be sailed."},{"Start":"01:37.055 ","End":"01:42.935","Text":"We then test, firstly by creating a vehicle length 90 color black."},{"Start":"01:42.935 ","End":"01:49.385","Text":"We run faster, followed by move 5 times and confirm that the speed no longer increases."},{"Start":"01:49.385 ","End":"01:53.870","Text":"We then create a car length 400 color silver,"},{"Start":"01:53.870 ","End":"01:56.990","Text":"automatic, with a top speed of 50."},{"Start":"01:56.990 ","End":"02:00.080","Text":"We run faster followed by move 6 times,"},{"Start":"02:00.080 ","End":"02:03.905","Text":"and confirm that speed no longer increases."},{"Start":"02:03.905 ","End":"02:07.595","Text":"Then we create a boat length 600 color white,"},{"Start":"02:07.595 ","End":"02:10.625","Text":"sails to top speed 10."},{"Start":"02:10.625 ","End":"02:16.925","Text":"We run faster followed by move 5 times and confirm that speed no longer increases."},{"Start":"02:16.925 ","End":"02:23.720","Text":"We run the collide method on any of the objects 10 times and follow up with a move to"},{"Start":"02:23.720 ","End":"02:27.065","Text":"confirm that the damage results in a situation where the vehicle can\u0027t"},{"Start":"02:27.065 ","End":"02:31.085","Text":"move and at the speed has reduced to 0."},{"Start":"02:31.085 ","End":"02:35.510","Text":"We then run the repair method on the same object and attempt to run"},{"Start":"02:35.510 ","End":"02:40.075","Text":"faster and move methods to confirm the vehicle can move again."},{"Start":"02:40.075 ","End":"02:43.250","Text":"Previously done parts A-D of this exercise,"},{"Start":"02:43.250 ","End":"02:45.710","Text":"we\u0027re going to continue from Part E onwards."},{"Start":"02:45.710 ","End":"02:49.370","Text":"Part E, asks us to implement a repair method."},{"Start":"02:49.370 ","End":"02:51.800","Text":"Again, this is going to be in vehicle,"},{"Start":"02:51.800 ","End":"02:56.900","Text":"all types of vehicle can be repaired is what that diagram is saying."},{"Start":"02:56.900 ","End":"03:00.550","Text":"A method repair is very straight forward."},{"Start":"03:00.550 ","End":"03:04.340","Text":"All it does is set damage to 0,"},{"Start":"03:04.340 ","End":"03:06.950","Text":"so there\u0027s no more damage."},{"Start":"03:06.950 ","End":"03:09.515","Text":"This is like a percentage figure,"},{"Start":"03:09.515 ","End":"03:11.865","Text":"not present damage or a 100 percent damage."},{"Start":"03:11.865 ","End":"03:13.255","Text":"A 100 percent damage means,"},{"Start":"03:13.255 ","End":"03:14.750","Text":"it\u0027s completely damaged,"},{"Start":"03:14.750 ","End":"03:16.850","Text":"0 means there\u0027s no damage."},{"Start":"03:16.850 ","End":"03:19.160","Text":"That was straightforward enough."},{"Start":"03:19.160 ","End":"03:23.425","Text":"F is going to be a little bit trickier,"},{"Start":"03:23.425 ","End":"03:26.390","Text":"for vehicle, it does 1 thing for cars,"},{"Start":"03:26.390 ","End":"03:27.485","Text":"it does another thing,"},{"Start":"03:27.485 ","End":"03:28.880","Text":"and boats does another thing."},{"Start":"03:28.880 ","End":"03:32.390","Text":"Not particularly tricky, but we\u0027ve got to do it in 3 places."},{"Start":"03:32.390 ","End":"03:34.990","Text":"F1 we can do in here,"},{"Start":"03:34.990 ","End":"03:42.095","Text":"and that is to increase the value of current speed when faster is called."},{"Start":"03:42.095 ","End":"03:46.055","Text":"Method, doesn\u0027t return anything,"},{"Start":"03:46.055 ","End":"03:51.680","Text":"but it\u0027s called faster and increases"},{"Start":"03:51.680 ","End":"03:59.480","Text":"the speed by 1 as long as it\u0027s less than 5 in this particular case, we\u0027ll vehicle."},{"Start":"03:59.480 ","End":"04:06.410","Text":"If speed is less than 5 to be in brackets,"},{"Start":"04:06.410 ","End":"04:11.270","Text":"then speed can have 1 added to it."},{"Start":"04:11.270 ","End":"04:14.670","Text":"I can put speed plus, plus here."},{"Start":"04:14.980 ","End":"04:21.175","Text":"That\u0027s easy enough for a vehicle."},{"Start":"04:21.175 ","End":"04:24.350","Text":"While it\u0027s less than 5,"},{"Start":"04:24.350 ","End":"04:25.925","Text":"we increase by 1."},{"Start":"04:25.925 ","End":"04:28.475","Text":"Otherwise we don\u0027t do anything."},{"Start":"04:28.475 ","End":"04:32.225","Text":"That is fine for vehicle."},{"Start":"04:32.225 ","End":"04:41.370","Text":"For a car, we need a different increment. It\u0027s 10."},{"Start":"04:41.370 ","End":"04:47.365","Text":"This time we\u0027re going to use the top speed attribute to limit the speed."},{"Start":"04:47.365 ","End":"04:50.210","Text":"Vehicles are always limited to 5,"},{"Start":"04:50.210 ","End":"04:54.290","Text":"whereas the cars and boats can be limited"},{"Start":"04:54.290 ","End":"04:58.100","Text":"by a variable amount depending on what top speed was set too."},{"Start":"04:58.100 ","End":"05:03.110","Text":"Not much harder, but just a little bit slightly different implementation."},{"Start":"05:03.110 ","End":"05:06.860","Text":"Let\u0027s do that 1 in here."},{"Start":"05:06.860 ","End":"05:14.460","Text":"Faster is going to be overriding what\u0027s inherited from vehicle,"},{"Start":"05:14.460 ","End":"05:18.875","Text":"and it\u0027s going to have slightly different code inside it."},{"Start":"05:18.875 ","End":"05:24.812","Text":"We\u0027re going to say if speed is less than top speed,"},{"Start":"05:24.812 ","End":"05:27.440","Text":"then add to speed."},{"Start":"05:27.440 ","End":"05:30.495","Text":"We say we\u0027re going to add 10."},{"Start":"05:30.495 ","End":"05:33.725","Text":"Again, it\u0027s 1 of those tricky bits logic actually,"},{"Start":"05:33.725 ","End":"05:43.005","Text":"we shouldn\u0027t add it if speed plus 10 is less than top speed."},{"Start":"05:43.005 ","End":"05:44.760","Text":"We don\u0027t add it,"},{"Start":"05:44.760 ","End":"05:48.245","Text":"if the addition would make it go higher than top speed."},{"Start":"05:48.245 ","End":"05:51.200","Text":"Otherwise, we do have it."},{"Start":"05:51.200 ","End":"06:00.785","Text":"That\u0027s faster for car and it\u0027s a very similar process for boat, except this time,"},{"Start":"06:00.785 ","End":"06:08.350","Text":"we\u0027re going to say speed plus 2 is less than top speed,"},{"Start":"06:08.350 ","End":"06:13.130","Text":"then we can do the speed increase."},{"Start":"06:13.140 ","End":"06:18.160","Text":"We\u0027re good now for Part F we\u0027ve done,"},{"Start":"06:18.160 ","End":"06:22.540","Text":"and so we\u0027ve got to do now is G and then we can test in Part"},{"Start":"06:22.540 ","End":"06:25.750","Text":"H. We\u0027ve got to implement"},{"Start":"06:25.750 ","End":"06:29.515","Text":"the move method and it\u0027s different again for vehicle, car, and boats."},{"Start":"06:29.515 ","End":"06:33.746","Text":"Let\u0027s go back to the vehicle and start there."},{"Start":"06:33.746 ","End":"06:40.555","Text":"Move simply says, what\u0027s happening to the vehicle."},{"Start":"06:40.555 ","End":"06:46.060","Text":"Presumably and again would actually make the vehicle move on screen."},{"Start":"06:46.060 ","End":"06:49.220","Text":"But we\u0027re just going to display a message."},{"Start":"06:49.370 ","End":"06:53.097","Text":"Public move or it\u0027s going to return I think it\u0027s"},{"Start":"06:53.097 ","End":"07:02.555","Text":"public void move and then we\u0027re going to put here a space and move this up."},{"Start":"07:02.555 ","End":"07:06.380","Text":"What we\u0027re going to do is say the vehicle is moving at"},{"Start":"07:06.380 ","End":"07:13.404","Text":"system print line to the vehicle is moving at."},{"Start":"07:13.404 ","End":"07:17.370","Text":"Then we put the speed attributes."},{"Start":"07:17.370 ","End":"07:25.060","Text":"Then you say miles per hour by default and that small damage is less than a 100,"},{"Start":"07:25.060 ","End":"07:28.220","Text":"otherwise it displays vehicle cannot move."},{"Start":"07:28.220 ","End":"07:32.855","Text":"We\u0027ll need an if block here."},{"Start":"07:32.855 ","End":"07:40.585","Text":"If damage is less than a 100, print that message."},{"Start":"07:40.585 ","End":"07:45.275","Text":"Otherwise, print something else."},{"Start":"07:45.275 ","End":"07:47.615","Text":"Just copy that line, save me typing."},{"Start":"07:47.615 ","End":"07:52.925","Text":"What I want to do is print vehicle cannot."},{"Start":"07:52.925 ","End":"07:56.665","Text":"That\u0027s G1 done."},{"Start":"07:56.665 ","End":"07:59.515","Text":"Got an error here. Let\u0027s see, where the error is."},{"Start":"07:59.515 ","End":"08:03.260","Text":"I\u0027m just missing a bracket there."},{"Start":"08:03.540 ","End":"08:07.555","Text":"That\u0027s better. Then we\u0027ll do part 2 for car."},{"Start":"08:07.555 ","End":"08:11.820","Text":"This time, rather than saying is moving at,"},{"Start":"08:11.820 ","End":"08:15.810","Text":"we say is driven at so trivial difference really."},{"Start":"08:15.810 ","End":"08:22.335","Text":"But just to illustrate that the methods can do something different if we override."},{"Start":"08:22.335 ","End":"08:24.810","Text":"In this case,"},{"Start":"08:24.810 ","End":"08:32.830","Text":"if damage is less than a 100,"},{"Start":"08:32.830 ","End":"08:36.730","Text":"do 1 thing, otherwise,"},{"Start":"08:36.730 ","End":"08:38.905","Text":"we do another thing."},{"Start":"08:38.905 ","End":"08:41.260","Text":"If damage is less than 100,"},{"Start":"08:41.260 ","End":"08:44.155","Text":"we\u0027re going to say system out,"},{"Start":"08:44.155 ","End":"08:49.735","Text":"the time car is being driven at."},{"Start":"08:49.735 ","End":"08:56.575","Text":"Then when we put how fast it\u0027s going and we\u0027re going to say miles per hour."},{"Start":"08:56.575 ","End":"09:02.380","Text":"If it is not able to be driven and just copy that,"},{"Start":"09:02.380 ","End":"09:08.560","Text":"see me typing and we just print car cannot."},{"Start":"09:08.560 ","End":"09:11.395","Text":"That\u0027s 1 done."},{"Start":"09:11.395 ","End":"09:14.590","Text":"Then final 1 is to do the same thing in boats."},{"Start":"09:14.590 ","End":"09:20.230","Text":"We\u0027re going to override what was inherited from vehicle to do something else."},{"Start":"09:20.230 ","End":"09:23.515","Text":"Again, it\u0027s the very trivial change,"},{"Start":"09:23.515 ","End":"09:29.200","Text":"all it\u0027s doing is displaying would not swap the miles per hour."},{"Start":"09:29.200 ","End":"09:33.850","Text":"Because boats then get measured in miles per hour generally,"},{"Start":"09:33.850 ","End":"09:36.290","Text":"in terms of their speeds."},{"Start":"09:37.230 ","End":"09:44.125","Text":"But just to illustrate here that we could be doing something different on-screen."},{"Start":"09:44.125 ","End":"09:49.525","Text":"If this was a nice graphical game of some sort,"},{"Start":"09:49.525 ","End":"09:52.555","Text":"just really to illustrate the point here."},{"Start":"09:52.555 ","End":"10:00.800","Text":"We\u0027re going to print boat is being sailed at rather than moving or driven."},{"Start":"10:00.810 ","End":"10:08.060","Text":"Then we can give this speed and we\u0027re going to say, not."},{"Start":"10:08.760 ","End":"10:16.930","Text":"Now that only happens if damage is less than a 100."},{"Start":"10:16.930 ","End":"10:23.245","Text":"Otherwise, we print an alternative message which says,"},{"Start":"10:23.245 ","End":"10:31.400","Text":"copy that, previously booked cannot be saved."},{"Start":"10:32.700 ","End":"10:38.785","Text":"We are done, now we can test and see what happens."},{"Start":"10:38.785 ","End":"10:45.475","Text":"We first asked to create a vehicle length 90 and color black."},{"Start":"10:45.475 ","End":"10:49.765","Text":"There\u0027s 90, color black."},{"Start":"10:49.765 ","End":"10:53.410","Text":"Assuming this is how we distinguish vehicles by their color."},{"Start":"10:53.410 ","End":"10:57.640","Text":"We\u0027re going to run faster followed by"},{"Start":"10:57.640 ","End":"11:04.105","Text":"move 5 times and confirm the speed no longer increases."},{"Start":"11:04.105 ","End":"11:08.620","Text":"Faster, faster,"},{"Start":"11:08.620 ","End":"11:11.270","Text":"we\u0027re going to move there."},{"Start":"11:11.340 ","End":"11:14.710","Text":"Faster again, move."},{"Start":"11:14.710 ","End":"11:17.905","Text":"Faster again, move."},{"Start":"11:17.905 ","End":"11:20.260","Text":"After this one,"},{"Start":"11:20.260 ","End":"11:28.060","Text":"it shouldn\u0027t go any faster because we limited generic vehicles to 5 miles an hour,"},{"Start":"11:28.060 ","End":"11:30.640","Text":"so yeah, and that seems to work."},{"Start":"11:30.640 ","End":"11:33.490","Text":"It\u0027s not going any faster than 5 miles an hour."},{"Start":"11:33.490 ","End":"11:35.860","Text":"Sounds great, I actually want to turn off"},{"Start":"11:35.860 ","End":"11:40.615","Text":"Clear Screen method call so we can see all evolving on screen."},{"Start":"11:40.615 ","End":"11:44.440","Text":"Now I\u0027m going to do H2,"},{"Start":"11:44.440 ","End":"11:51.130","Text":"which is to create a car length 400 silver automatic with a top speed of 50."},{"Start":"11:51.130 ","End":"12:01.450","Text":"Great, it\u0027s 100 long."},{"Start":"12:01.450 ","End":"12:08.120","Text":"It\u0027s automatic. I\u0027ll put true there and top speed is 50."},{"Start":"12:08.460 ","End":"12:13.030","Text":"Now, I\u0027m going to run faster followed by 6 times."},{"Start":"12:13.030 ","End":"12:16.120","Text":"It can\u0027t go faster than 60 miles an hour."},{"Start":"12:16.120 ","End":"12:18.011","Text":"There\u0027s 10, 20, 30,"},{"Start":"12:18.011 ","End":"12:28.580","Text":"40 and it\u0027s not going above 40."},{"Start":"12:29.250 ","End":"12:35.830","Text":"I should have a less than or equals to there to see if it can hit 50 exactly."},{"Start":"12:35.830 ","End":"12:40.840","Text":"But it seems to be doing the job is not allowing me to go significantly faster."},{"Start":"12:40.840 ","End":"12:43.060","Text":"I should have had a less than in there,"},{"Start":"12:43.060 ","End":"12:45.295","Text":"but rather than recompile it,"},{"Start":"12:45.295 ","End":"12:47.230","Text":"I\u0027m just going to leave it as it is."},{"Start":"12:47.230 ","End":"12:55.090","Text":"Let\u0027s do boat length 600 in part 3, color white."},{"Start":"12:55.090 ","End":"13:03.985","Text":"600, white 2 sales, top speed 10."},{"Start":"13:03.985 ","End":"13:08.845","Text":"Let\u0027s run faster, followed by move 5 times."},{"Start":"13:08.845 ","End":"13:10.600","Text":"Being sailed at,"},{"Start":"13:10.600 ","End":"13:12.610","Text":"we get rather than moving or driven,"},{"Start":"13:12.610 ","End":"13:14.740","Text":"and it says knots."},{"Start":"13:14.740 ","End":"13:22.455","Text":"This doesn\u0027t seem to be working because we\u0027re always staying at 0 knots."},{"Start":"13:22.455 ","End":"13:26.085","Text":"That\u0027s something for us to look into."},{"Start":"13:26.085 ","End":"13:28.583","Text":"Why is that happening?"},{"Start":"13:28.583 ","End":"13:35.980","Text":"It looks like what I\u0027ve done is I haven\u0027t set these attributes up that were passed in,"},{"Start":"13:35.980 ","End":"13:38.710","Text":"and therefore the top speed is always 0."},{"Start":"13:38.710 ","End":"13:42.820","Text":"That\u0027s why it doesn\u0027t do anything so you just need to make sure I fix that."},{"Start":"13:42.820 ","End":"13:47.185","Text":"This sales equals sales."},{"Start":"13:47.185 ","End":"13:54.325","Text":"This top speed is the 1 that was causing our program to fail, equals to speed."},{"Start":"13:54.325 ","End":"13:56.275","Text":"While I\u0027m here,"},{"Start":"13:56.275 ","End":"13:58.990","Text":"I think I\u0027ll make this less than or equals"},{"Start":"13:58.990 ","End":"14:03.505","Text":"2 as well to fix the bug that we saw elsewhere."},{"Start":"14:03.505 ","End":"14:06.475","Text":"Let\u0027s try again with our boat."},{"Start":"14:06.475 ","End":"14:11.200","Text":"Let\u0027s retest part 3 of H. A new boat,"},{"Start":"14:11.200 ","End":"14:15.430","Text":"length 600, color white,"},{"Start":"14:15.430 ","End":"14:20.050","Text":"number of sales to top speed 10 minutes."},{"Start":"14:20.050 ","End":"14:25.930","Text":"Let\u0027s see if I move it increasing,"},{"Start":"14:25.930 ","End":"14:27.730","Text":"yeah, that\u0027s better."},{"Start":"14:27.730 ","End":"14:34.390","Text":"We should hit after 6 runs or 5 runs,"},{"Start":"14:34.390 ","End":"14:37.525","Text":"should hit the maximum."},{"Start":"14:37.525 ","End":"14:39.505","Text":"I should get to 10,"},{"Start":"14:39.505 ","End":"14:43.330","Text":"but no faster and that seems to have worked fine."},{"Start":"14:43.330 ","End":"14:46.870","Text":"Now, what I\u0027m going to do is I\u0027m going to run because I\u0027ve got rid of"},{"Start":"14:46.870 ","End":"14:51.985","Text":"my 2 car and objects that had previously because tried to recompile."},{"Start":"14:51.985 ","End":"14:56.710","Text":"I\u0027m going to collide my boat 10 times and follow up with them,"},{"Start":"14:56.710 ","End":"15:00.505","Text":"move to see if it will move or not."},{"Start":"15:00.505 ","End":"15:08.954","Text":"There\u0027s 1, 2, 3, 4,"},{"Start":"15:08.954 ","End":"15:17.560","Text":"5, 6, 7, 9, and 10."},{"Start":"15:17.560 ","End":"15:20.170","Text":"I\u0027ll have a little look inside and expect to actually"},{"Start":"15:20.170 ","End":"15:24.490","Text":"see what my damages and it does look like it\u0027s gone to a 100."},{"Start":"15:24.490 ","End":"15:28.989","Text":"Now when I run move,"},{"Start":"15:28.989 ","End":"15:31.855","Text":"it should say I can\u0027t move because I\u0027m too damaged."},{"Start":"15:31.855 ","End":"15:33.790","Text":"Let\u0027s see if that works. There we go."},{"Start":"15:33.790 ","End":"15:36.940","Text":"It says boat cannot be sailed because of the damage."},{"Start":"15:36.940 ","End":"15:39.220","Text":"Now let\u0027s try and repair it."},{"Start":"15:39.220 ","End":"15:41.290","Text":"That\u0027s inherited from vehicle,"},{"Start":"15:41.290 ","End":"15:43.030","Text":"all vehicles can be repaired."},{"Start":"15:43.030 ","End":"15:47.720","Text":"Now, let\u0027s try and move it and great it can"},{"Start":"15:47.720 ","End":"15:54.295","Text":"be moved now because it\u0027s no longer damaged, perfect."},{"Start":"15:54.295 ","End":"15:57.350","Text":"That is it for this exercise,"},{"Start":"15:57.350 ","End":"15:59.420","Text":"a very long 1 with the testing."},{"Start":"15:59.420 ","End":"16:03.410","Text":"But we\u0027ve now got something actually relatively complicated,"},{"Start":"16:03.410 ","End":"16:06.410","Text":"lots of different behaviors going on, lots of overriding."},{"Start":"16:06.410 ","End":"16:09.185","Text":"Good illustration of inheritance, I think,"},{"Start":"16:09.185 ","End":"16:12.185","Text":"where we\u0027ve got generic vehicles and we\u0027ve got boats,"},{"Start":"16:12.185 ","End":"16:13.880","Text":"which is a specialized type of vehicle,"},{"Start":"16:13.880 ","End":"16:17.100","Text":"and cars which are specialized type of vehicle."},{"Start":"16:17.100 ","End":"16:19.190","Text":"They have slightly different behaviors."},{"Start":"16:19.190 ","End":"16:20.330","Text":"A boat, his sale,"},{"Start":"16:20.330 ","End":"16:21.350","Text":"the car is driven,"},{"Start":"16:21.350 ","End":"16:23.000","Text":"a vehicle just moves."},{"Start":"16:23.000 ","End":"16:27.665","Text":"This one is always by default limited to 5 miles an hour."},{"Start":"16:27.665 ","End":"16:33.365","Text":"These types of vehicles have a top speed that can be set to whatever we like."},{"Start":"16:33.365 ","End":"16:37.025","Text":"Just illustrates quite a lot of different behaviors there,"},{"Start":"16:37.025 ","End":"16:40.270","Text":"as well as the principles of inheritance."},{"Start":"16:40.270 ","End":"16:43.460","Text":"Thanks for watching. See you again soon."}],"ID":31091}],"Thumbnail":null,"ID":294470},{"Name":"Polymorphism","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Polymorphism Basics","Duration":"10m 13s","ChapterTopicVideoID":29493,"CourseChapterTopicPlaylistID":294472,"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, welcome to this video on polymorphism."},{"Start":"00:03.150 ","End":"00:04.530","Text":"By the end of this section,"},{"Start":"00:04.530 ","End":"00:07.395","Text":"you\u0027ll be able to explain the concept of polymorphism,"},{"Start":"00:07.395 ","End":"00:11.010","Text":"describe the process of polymorphic method dispatch,"},{"Start":"00:11.010 ","End":"00:16.410","Text":"and select and apply appropriate techniques to make use of polymorphism."},{"Start":"00:16.410 ","End":"00:20.640","Text":"We\u0027ve now covered the 2 key principles of object-oriented programming,"},{"Start":"00:20.640 ","End":"00:25.205","Text":"the idea of encapsulation and the idea of inheritance."},{"Start":"00:25.205 ","End":"00:30.395","Text":"The next important concept to cover in our P is polymorphism."},{"Start":"00:30.395 ","End":"00:33.050","Text":"Deriving from the Greek words poly meaning"},{"Start":"00:33.050 ","End":"00:37.805","Text":"many and morph meaning shapes or forms, in computer science,"},{"Start":"00:37.805 ","End":"00:41.540","Text":"polymorphism principally relates to the idea that methods with"},{"Start":"00:41.540 ","End":"00:48.455","Text":"the same name or operations with the same symbol can exhibit different behaviors."},{"Start":"00:48.455 ","End":"00:53.015","Text":"Polymorphism manifest in many different ways in a typical program."},{"Start":"00:53.015 ","End":"00:57.020","Text":"Parametric polymorphism it comes in 2 forms;"},{"Start":"00:57.020 ","End":"01:01.324","Text":"method overloading and operator overloading."},{"Start":"01:01.324 ","End":"01:05.085","Text":"Method overriding is another variant,"},{"Start":"01:05.085 ","End":"01:08.360","Text":"as is subtype polymorphism."},{"Start":"01:08.360 ","End":"01:11.300","Text":"We\u0027ll cover each of these in turn."},{"Start":"01:11.300 ","End":"01:14.660","Text":"In parametric polymorphism, values of"},{"Start":"01:14.660 ","End":"01:17.900","Text":"different datatypes can be handled in a uniform way."},{"Start":"01:17.900 ","End":"01:20.420","Text":"We\u0027ve been witnessing parametric polymorphism"},{"Start":"01:20.420 ","End":"01:23.330","Text":"when using print line without really being aware of it."},{"Start":"01:23.330 ","End":"01:28.400","Text":"Print line can print the value inside a variable no matter what its type,"},{"Start":"01:28.400 ","End":"01:30.425","Text":"so whatever is inside the brackets,"},{"Start":"01:30.425 ","End":"01:35.060","Text":"print line will output to the screen whether that\u0027s a string or an int,"},{"Start":"01:35.060 ","End":"01:37.290","Text":"or a float, doesn\u0027t matter."},{"Start":"01:37.290 ","End":"01:41.075","Text":"Print lines able to deal with it and produce the appropriate output."},{"Start":"01:41.075 ","End":"01:46.640","Text":"It uses the type of the parameter in the brackets to behave in the appropriate way,"},{"Start":"01:46.640 ","End":"01:49.535","Text":"hence the name parametric."},{"Start":"01:49.535 ","End":"01:52.580","Text":"Overloading, which we came across earlier,"},{"Start":"01:52.580 ","End":"01:57.485","Text":"involves providing many implementations of a method with different signatures,"},{"Start":"01:57.485 ","End":"02:00.860","Text":"ie a number and type of arguments."},{"Start":"02:00.860 ","End":"02:03.680","Text":"Each of these methods is going to behave slightly different"},{"Start":"02:03.680 ","End":"02:06.170","Text":"from each other and we can see how this matches"},{"Start":"02:06.170 ","End":"02:09.020","Text":"the definition of polymorphism of many methods"},{"Start":"02:09.020 ","End":"02:12.260","Text":"with the same name exhibiting different behavior."},{"Start":"02:12.260 ","End":"02:15.560","Text":"We\u0027re not showing the entire class definition here,"},{"Start":"02:15.560 ","End":"02:20.240","Text":"but we can see that there should be 3 attributes, name,"},{"Start":"02:20.240 ","End":"02:25.595","Text":"enrollmentYear, and termTimeAddress that all need default values set."},{"Start":"02:25.595 ","End":"02:29.040","Text":"If we pass nothing to the constructor,"},{"Start":"02:29.040 ","End":"02:31.425","Text":"the first method will run here,"},{"Start":"02:31.425 ","End":"02:33.290","Text":"and in this part of the code,"},{"Start":"02:33.290 ","End":"02:37.220","Text":"we\u0027ll need to set default values for all 3 of the attributes,"},{"Start":"02:37.220 ","End":"02:40.490","Text":"name, enrollmentYear, and termTimeAddress."},{"Start":"02:40.490 ","End":"02:44.464","Text":"If we pass a single string parameter,"},{"Start":"02:44.464 ","End":"02:49.520","Text":"then this method will run and the name attribute will"},{"Start":"02:49.520 ","End":"02:54.950","Text":"get its value set and the others will need default values,"},{"Start":"02:54.950 ","End":"02:59.405","Text":"and so on if we pass a string parameter and an int,"},{"Start":"02:59.405 ","End":"03:02.125","Text":"we\u0027d run this method and so on."},{"Start":"03:02.125 ","End":"03:07.090","Text":"Once again, this technique is parametric polymorphism."},{"Start":"03:07.090 ","End":"03:13.915","Text":"A polymorphism is a consequence of the parameters being of different types."},{"Start":"03:13.915 ","End":"03:18.535","Text":"Operator overloading is another example of polymorphism."},{"Start":"03:18.535 ","End":"03:21.484","Text":"For example, consider the plus operator."},{"Start":"03:21.484 ","End":"03:24.063","Text":"With 2 ints it does 1 thing,"},{"Start":"03:24.063 ","End":"03:26.422","Text":"it gives you the result 41."},{"Start":"03:26.422 ","End":"03:29.305","Text":"With 2 floats, it gives you another thing."},{"Start":"03:29.305 ","End":"03:31.300","Text":"It has to deal with a fractional part,"},{"Start":"03:31.300 ","End":"03:34.100","Text":"so it returns 41.0."},{"Start":"03:34.580 ","End":"03:36.820","Text":"With 2 strings,"},{"Start":"03:36.820 ","End":"03:39.520","Text":"it does something completely different because it\u0027s"},{"Start":"03:39.520 ","End":"03:42.520","Text":"concatenating the 2 different strings,"},{"Start":"03:42.520 ","End":"03:44.765","Text":"not summing 2 numbers."},{"Start":"03:44.765 ","End":"03:51.320","Text":"The same symbol has different behaviors depending on the type of data it\u0027s working with."},{"Start":"03:51.320 ","End":"03:55.475","Text":"This fits the general definition of polymorphism, meaning different behavior,"},{"Start":"03:55.475 ","End":"03:57.950","Text":"but with the same symbol in this case,"},{"Start":"03:57.950 ","End":"04:00.665","Text":"rather than the same method name."},{"Start":"04:00.665 ","End":"04:04.445","Text":"The divide operator will also do different things depending on whether you\u0027re"},{"Start":"04:04.445 ","End":"04:08.285","Text":"dealing with a pair of ints or a pair of floats."},{"Start":"04:08.285 ","End":"04:10.175","Text":"Although java doesn\u0027t,"},{"Start":"04:10.175 ","End":"04:12.710","Text":"some high-level languages will let you override"},{"Start":"04:12.710 ","End":"04:16.925","Text":"any operators with your own new behaviors."},{"Start":"04:16.925 ","End":"04:21.965","Text":"Overloading shouldn\u0027t be confused with overriding."},{"Start":"04:21.965 ","End":"04:24.920","Text":"We came across this example of overriding in"},{"Start":"04:24.920 ","End":"04:28.940","Text":"the inheritance topic and what it enabled us to do given"},{"Start":"04:28.940 ","End":"04:32.870","Text":"a generic vehicle base class was to provide a new way of"},{"Start":"04:32.870 ","End":"04:37.640","Text":"moving or increasing speed in the subclasses."},{"Start":"04:37.640 ","End":"04:40.580","Text":"This makes sense because a car moves in a different way to"},{"Start":"04:40.580 ","End":"04:44.000","Text":"a boat and a boat moves in a different way to a generic vehicle."},{"Start":"04:44.000 ","End":"04:47.690","Text":"When moving faster, they both might have different rates of"},{"Start":"04:47.690 ","End":"04:52.690","Text":"acceleration and respond in different ways to the surface on which they\u0027re traveling."},{"Start":"04:52.690 ","End":"04:58.445","Text":"Overriding is another way of implementing polymorphism and 1 of the more powerful ones,"},{"Start":"04:58.445 ","End":"05:01.670","Text":"which we\u0027ll now try and illustrate further, but first,"},{"Start":"05:01.670 ","End":"05:05.540","Text":"we\u0027ll have to cover some related concepts and terms."},{"Start":"05:05.540 ","End":"05:09.500","Text":"When defining a variable in a statically typed language,"},{"Start":"05:09.500 ","End":"05:11.555","Text":"the variable has a type,"},{"Start":"05:11.555 ","End":"05:14.990","Text":"and once it takes on a type, it can\u0027t change."},{"Start":"05:14.990 ","End":"05:19.575","Text":"Here we define 2 variables, an int,"},{"Start":"05:19.575 ","End":"05:21.410","Text":"and a string, and"},{"Start":"05:21.410 ","End":"05:26.525","Text":"then a third int variable which takes on the value of the first variable."},{"Start":"05:26.525 ","End":"05:33.860","Text":"Now, myVar3 and myVar1 both contain the same value, 20."},{"Start":"05:33.860 ","End":"05:39.005","Text":"That\u0027s worked okay because both variables were declared with the same datatype, int."},{"Start":"05:39.005 ","End":"05:43.505","Text":"But if you try to assign an integer variable a string value,"},{"Start":"05:43.505 ","End":"05:45.630","Text":"you\u0027ll of course get an error."},{"Start":"05:45.630 ","End":"05:52.775","Text":"Both of these lines are trying to assign a string into a variable declared as an integer,"},{"Start":"05:52.775 ","End":"05:56.390","Text":"which of course is not allowed by the compiler."},{"Start":"05:56.390 ","End":"05:59.240","Text":"In object-oriented languages, it\u0027s possible to have"},{"Start":"05:59.240 ","End":"06:02.525","Text":"a variable that said to have a polymorphic type."},{"Start":"06:02.525 ","End":"06:05.450","Text":"A variable can hold a reference to an object that\u0027s either of"},{"Start":"06:05.450 ","End":"06:09.830","Text":"the base class type or any of the sub-class types."},{"Start":"06:09.830 ","End":"06:12.280","Text":"We call these subtypes."},{"Start":"06:12.280 ","End":"06:15.845","Text":"Car is a subtype of the vehicle type,"},{"Start":"06:15.845 ","End":"06:20.780","Text":"and boat is also a subtype of the vehicle type."},{"Start":"06:20.780 ","End":"06:28.195","Text":"Here we declare 2 variables to hold object references, myObj1 and myOb2."},{"Start":"06:28.195 ","End":"06:33.864","Text":"MyObj1 at this point is storing a reference to a vehicle object,"},{"Start":"06:33.864 ","End":"06:38.575","Text":"and myObj2 is storing a reference to a car object."},{"Start":"06:38.575 ","End":"06:40.540","Text":"Nothing unusual about that."},{"Start":"06:40.540 ","End":"06:42.910","Text":"Here we can create another variable,"},{"Start":"06:42.910 ","End":"06:50.365","Text":"myObj3 of type vehicle and we can make it reference the object we created earlier,"},{"Start":"06:50.365 ","End":"06:54.864","Text":"myObj1, which is also of type vehicle."},{"Start":"06:54.864 ","End":"06:58.215","Text":"Again, nothing new or special here."},{"Start":"06:58.215 ","End":"07:02.335","Text":"However, this line is doing something interesting."},{"Start":"07:02.335 ","End":"07:10.240","Text":"We\u0027re now changing myObj3 to refer not to a vehicle object but to a car object."},{"Start":"07:10.240 ","End":"07:15.015","Text":"Amazingly, it works without any problems."},{"Start":"07:15.015 ","End":"07:21.565","Text":"Using the vehicle inheritance hierarchy because a car is a type of vehicle,"},{"Start":"07:21.565 ","End":"07:24.460","Text":"we can store a reference to a car in"},{"Start":"07:24.460 ","End":"07:28.090","Text":"a variable that was initially set up to store vehicle objects."},{"Start":"07:28.090 ","End":"07:30.670","Text":"What we\u0027re seeing here is that the type can"},{"Start":"07:30.670 ","End":"07:35.240","Text":"change so long as it\u0027s within an inheritance hierarchy."},{"Start":"07:35.240 ","End":"07:41.330","Text":"The type of myObj1 and myObj3 is"},{"Start":"07:41.330 ","End":"07:48.815","Text":"a dynamic type that could change from a vehicle type to a car type or to a boat type,"},{"Start":"07:48.815 ","End":"07:54.980","Text":"but only in the direction of subclass, not the superclass."},{"Start":"07:54.980 ","End":"07:57.890","Text":"If we did it in the other direction, we\u0027d have a problem."},{"Start":"07:57.890 ","End":"08:04.714","Text":"We can\u0027t say a vehicle is a type of car that doesn\u0027t make sense."},{"Start":"08:04.714 ","End":"08:09.365","Text":"It\u0027s the other way round. A car is a type of vehicle."},{"Start":"08:09.365 ","End":"08:15.560","Text":"This assignment fails because you\u0027re trying to change the type of"},{"Start":"08:15.560 ","End":"08:19.790","Text":"myOb2 from a car which is what it was declared"},{"Start":"08:19.790 ","End":"08:25.135","Text":"as into a vehicle which doesn\u0027t make sense."},{"Start":"08:25.135 ","End":"08:28.535","Text":"To summarize, to make use of type polymorphism,"},{"Start":"08:28.535 ","End":"08:30.380","Text":"we need to declare a variable of"},{"Start":"08:30.380 ","End":"08:35.660","Text":"the base class type and we can then store references to an object of"},{"Start":"08:35.660 ","End":"08:44.630","Text":"the base class type or any of the sub-class types that derive from that base class."},{"Start":"08:44.630 ","End":"08:48.530","Text":"You may hear this expressed in the following ways."},{"Start":"08:48.530 ","End":"08:52.610","Text":"A static type is the type of a variable as declared in"},{"Start":"08:52.610 ","End":"08:57.725","Text":"the source code and encountered by the compiler at compile time."},{"Start":"08:57.725 ","End":"09:03.290","Text":"Whereas a dynamic type is the type that a variable takes on as a result of"},{"Start":"09:03.290 ","End":"09:09.690","Text":"an assignment operation and therefore 1 that can only be known at runtime."},{"Start":"09:09.690 ","End":"09:11.915","Text":"Again from the previous example,"},{"Start":"09:11.915 ","End":"09:15.455","Text":"any variables we declared should be of type vehicle,"},{"Start":"09:15.455 ","End":"09:19.175","Text":"but what\u0027s being stored in those variables could change while the program is"},{"Start":"09:19.175 ","End":"09:24.320","Text":"running and they\u0027re variable could therefore hold a reference to a different subtype."},{"Start":"09:24.320 ","End":"09:27.350","Text":"For example, a car or a boat."},{"Start":"09:27.350 ","End":"09:31.310","Text":"It seems that we\u0027ve escaped the shackles of static typing for"},{"Start":"09:31.310 ","End":"09:37.678","Text":"this very limited scenario where we\u0027re using subtypes in an inheritance hierarchy,"},{"Start":"09:37.678 ","End":"09:42.095","Text":"but it can lead to some confusion because a variable can have 2 types;"},{"Start":"09:42.095 ","End":"09:45.545","Text":"it\u0027s static type as declared in the source code,"},{"Start":"09:45.545 ","End":"09:47.270","Text":"and it\u0027s dynamic type,"},{"Start":"09:47.270 ","End":"09:50.915","Text":"which is what the variable currently holds a reference to."},{"Start":"09:50.915 ","End":"09:54.350","Text":"We\u0027ll pause there and let those concepts sink in."},{"Start":"09:54.350 ","End":"09:58.115","Text":"In the next video, we\u0027ll look at this idea of dynamic types,"},{"Start":"09:58.115 ","End":"10:01.055","Text":"along with the idea of method overriding,"},{"Start":"10:01.055 ","End":"10:05.900","Text":"which together lead to a very powerful form of polymorphism,"},{"Start":"10:05.900 ","End":"10:10.550","Text":"making use of a procedure known as dynamic method dispatch."},{"Start":"10:10.550 ","End":"10:13.710","Text":"See you soon for that 1. Thanks for watching."}],"ID":31101},{"Watched":false,"Name":"Dynamic dispatch","Duration":"9m 58s","ChapterTopicVideoID":29492,"CourseChapterTopicPlaylistID":294472,"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.845","Text":"Hello, welcome back."},{"Start":"00:01.845 ","End":"00:05.850","Text":"We previously introduced the idea that a single polymorphic variable"},{"Start":"00:05.850 ","End":"00:10.260","Text":"can hold any type of object within an inheritance hierarchy."},{"Start":"00:10.260 ","End":"00:15.465","Text":"If we declare a variable myObj with an initial type of vehicle,"},{"Start":"00:15.465 ","End":"00:19.725","Text":"we could store any objects of type vehicle."},{"Start":"00:19.725 ","End":"00:24.210","Text":"The static type of myObj is vehicle because that\u0027s"},{"Start":"00:24.210 ","End":"00:28.371","Text":"what the variable was first declared as in the source code,"},{"Start":"00:28.371 ","End":"00:30.420","Text":"but once the program is running,"},{"Start":"00:30.420 ","End":"00:35.850","Text":"we could subsequently change myObj\u0027s content using an assignment to refer to"},{"Start":"00:35.850 ","End":"00:38.850","Text":"a car object instead because a car is"},{"Start":"00:38.850 ","End":"00:42.600","Text":"a type of vehicle according to our inheritance hierarchy,"},{"Start":"00:42.600 ","End":"00:46.125","Text":"so it makes it a perfectly valid operation."},{"Start":"00:46.125 ","End":"00:51.060","Text":"The dynamic type of myObj is now car."},{"Start":"00:51.060 ","End":"00:54.840","Text":"We could also change myObj\u0027s contents to refer to"},{"Start":"00:54.840 ","End":"00:58.470","Text":"a boat object which again is part of the hierarchy."},{"Start":"00:58.470 ","End":"01:03.840","Text":"This allows us to write much simpler code because we can create a single variable of"},{"Start":"01:03.840 ","End":"01:06.840","Text":"the base class type even if the object being"},{"Start":"01:06.840 ","End":"01:11.235","Text":"contained in the variable is of a different type to the base class."},{"Start":"01:11.235 ","End":"01:15.330","Text":"We could then run the move method on any of the types of vehicle"},{"Start":"01:15.330 ","End":"01:20.475","Text":"currently held in myObj by calling Obj.move."},{"Start":"01:20.475 ","End":"01:25.157","Text":"Because myObj currently references a boat object,"},{"Start":"01:25.157 ","End":"01:28.730","Text":"the move method that\u0027s going to be executed is the one inside"},{"Start":"01:28.730 ","End":"01:33.020","Text":"the boat class which overrides the one in the vehicle class."},{"Start":"01:33.020 ","End":"01:36.230","Text":"Change it to a reference to a car object and then"},{"Start":"01:36.230 ","End":"01:41.360","Text":"the car object\u0027s overridden move method will be called instead."},{"Start":"01:41.360 ","End":"01:44.480","Text":"Whilst it\u0027s unlikely that a program would keep"},{"Start":"01:44.480 ","End":"01:47.190","Text":"changing the type of object in a single variable,"},{"Start":"01:47.190 ","End":"01:50.750","Text":"what\u0027s more likely is if you had an array of objects of"},{"Start":"01:50.750 ","End":"01:53.945","Text":"one particular base class type but then stored"},{"Start":"01:53.945 ","End":"01:58.455","Text":"objects in the array that were of different subtypes."},{"Start":"01:58.455 ","End":"02:01.295","Text":"Let\u0027s consider a different example."},{"Start":"02:01.295 ","End":"02:05.179","Text":"A company has employees that can be part-time or full-time."},{"Start":"02:05.179 ","End":"02:08.030","Text":"Some common attributes and methods can be inherited from"},{"Start":"02:08.030 ","End":"02:11.420","Text":"the employee class but the details aren\u0027t shown here."},{"Start":"02:11.420 ","End":"02:14.570","Text":"One method is implemented in the subclasses"},{"Start":"02:14.570 ","End":"02:17.960","Text":"and will be different for each type of subclass."},{"Start":"02:17.960 ","End":"02:19.610","Text":"For a part-time employee,"},{"Start":"02:19.610 ","End":"02:23.150","Text":"the monthly pay is calculated by multiplying an hourly rate"},{"Start":"02:23.150 ","End":"02:27.560","Text":"by the number of hours that employee worked in a particular month."},{"Start":"02:27.560 ","End":"02:29.450","Text":"For a full-time employee,"},{"Start":"02:29.450 ","End":"02:35.260","Text":"the hours are fixed so monthly pay is simply dependent on the staff member\u0027s pay grade."},{"Start":"02:35.260 ","End":"02:39.230","Text":"Here we have an example of 2 methods with the same name but"},{"Start":"02:39.230 ","End":"02:43.355","Text":"with different behavior, a polymorphic method."},{"Start":"02:43.355 ","End":"02:46.925","Text":"We declared an array of employee objects."},{"Start":"02:46.925 ","End":"02:52.355","Text":"We could store either part-time employees or full-time employees in the array."},{"Start":"02:52.355 ","End":"02:55.010","Text":"When initially declaring the array,"},{"Start":"02:55.010 ","End":"02:57.800","Text":"it will be full of null references because"},{"Start":"02:57.800 ","End":"03:01.050","Text":"the elements haven\u0027t been assigned any values yet,"},{"Start":"03:01.050 ","End":"03:04.190","Text":"but we could start to populate the array with elements"},{"Start":"03:04.190 ","End":"03:08.490","Text":"that are a mixture of part-time and full-time employees."},{"Start":"03:09.010 ","End":"03:14.315","Text":"Eventually we could have filled the array with employees,"},{"Start":"03:14.315 ","End":"03:20.220","Text":"a mixture of part-time and full-time in any combination that we want."},{"Start":"03:20.240 ","End":"03:23.160","Text":"Now comes the interesting bit."},{"Start":"03:23.160 ","End":"03:25.630","Text":"We can iterate through the array accessing"},{"Start":"03:25.630 ","End":"03:29.515","Text":"each element and executing the get monthly pay method."},{"Start":"03:29.515 ","End":"03:33.700","Text":"This will execute a different implementation of the get monthly pay method"},{"Start":"03:33.700 ","End":"03:38.102","Text":"depending on what type of object is stored in the current element,"},{"Start":"03:38.102 ","End":"03:39.730","Text":"so when we start,"},{"Start":"03:39.730 ","End":"03:42.160","Text":"i will be 0 so we access this element."},{"Start":"03:42.160 ","End":"03:44.695","Text":"Because we\u0027ve stored a part-time employee there,"},{"Start":"03:44.695 ","End":"03:49.060","Text":"the get monthly pay for a part-time employee is executed."},{"Start":"03:49.060 ","End":"03:52.255","Text":"The loop continues iterating through the array"},{"Start":"03:52.255 ","End":"03:56.155","Text":"accessing each object and executing the relevant method."},{"Start":"03:56.155 ","End":"04:00.725","Text":"This time it will be the method defined in the full-time class,"},{"Start":"04:00.725 ","End":"04:05.380","Text":"and the next one will be part-time, and so on."},{"Start":"04:05.380 ","End":"04:09.490","Text":"We could even store employee objects in this array and therefore run the get"},{"Start":"04:09.490 ","End":"04:14.620","Text":"monthly pay method from the employee class if it had one."},{"Start":"04:14.620 ","End":"04:20.245","Text":"What we\u0027re seeing here is known as dynamic method dispatch."},{"Start":"04:20.245 ","End":"04:25.180","Text":"This is the process during program execution by which a method call within"},{"Start":"04:25.180 ","End":"04:31.900","Text":"an inheritance hierarchy is correctly selected and executed by the runtime environment."},{"Start":"04:31.900 ","End":"04:35.890","Text":"It gets the name from the fact that the dynamic type of the variable holding"},{"Start":"04:35.890 ","End":"04:40.000","Text":"the object determines which method is going to be executed."},{"Start":"04:40.000 ","End":"04:41.830","Text":"In our example just now,"},{"Start":"04:41.830 ","End":"04:44.770","Text":"it wasn\u0027t a variable but an array of employee objects,"},{"Start":"04:44.770 ","End":"04:48.585","Text":"each element of which you can think of as being a variable."},{"Start":"04:48.585 ","End":"04:54.710","Text":"Dynamic dispatch is also known as dynamic lookup or sometimes dynamic binding."},{"Start":"04:54.710 ","End":"04:59.465","Text":"It\u0027s through this process that method polymorphism is made possible."},{"Start":"04:59.465 ","End":"05:03.791","Text":"Our example was simple as there was only one level of inheritance,"},{"Start":"05:03.791 ","End":"05:06.905","Text":"but with a deeper and more complex inheritance hierarchy,"},{"Start":"05:06.905 ","End":"05:12.035","Text":"the lookup would be more involved but the basic process is the same."},{"Start":"05:12.035 ","End":"05:16.175","Text":"Assume we have the following inheritance hierarchy and we\u0027re trying to run"},{"Start":"05:16.175 ","End":"05:21.935","Text":"display details on the object currently stored in the variable myObj."},{"Start":"05:21.935 ","End":"05:26.885","Text":"That dynamic type of myObj is customTshirt."},{"Start":"05:26.885 ","End":"05:29.330","Text":"We look inside the class definition for"},{"Start":"05:29.330 ","End":"05:33.325","Text":"customTshirt and see if there\u0027s a matching method."},{"Start":"05:33.325 ","End":"05:35.370","Text":"In this case there\u0027s not,"},{"Start":"05:35.370 ","End":"05:38.885","Text":"so we look again for the method in the superclass."},{"Start":"05:38.885 ","End":"05:45.410","Text":"Again there\u0027s no display details method in the superclass so we repeat the process again."},{"Start":"05:45.410 ","End":"05:49.730","Text":"This time we find the method and we can execute it."},{"Start":"05:49.730 ","End":"05:53.855","Text":"When calling display details method for customTshirt objects,"},{"Start":"05:53.855 ","End":"05:59.495","Text":"the display details method for the item class is executed."},{"Start":"05:59.495 ","End":"06:03.275","Text":"If we look at the same process for customHoody,"},{"Start":"06:03.275 ","End":"06:06.800","Text":"we immediately find a match for display details in"},{"Start":"06:06.800 ","End":"06:13.280","Text":"the customHoody class and need look no further as we can execute the method found."},{"Start":"06:13.280 ","End":"06:17.600","Text":"If we had a clothing object in myObj, again,"},{"Start":"06:17.600 ","End":"06:22.175","Text":"we\u0027d find no method inside the class definition for clothing,"},{"Start":"06:22.175 ","End":"06:23.570","Text":"so we\u0027d look again in"},{"Start":"06:23.570 ","End":"06:29.875","Text":"its superclass and find the display details method there and could execute it."},{"Start":"06:29.875 ","End":"06:33.590","Text":"To summarize, methods being executed are searched"},{"Start":"06:33.590 ","End":"06:37.700","Text":"for in the subclass being accessed and we move up through"},{"Start":"06:37.700 ","End":"06:40.580","Text":"the inheritance hierarchy in the direction of"},{"Start":"06:40.580 ","End":"06:46.145","Text":"the ultimate superclass trying to find the method we\u0027re trying to execute."},{"Start":"06:46.145 ","End":"06:51.365","Text":"One question you might have is what if you don\u0027t need a certain method in a base class"},{"Start":"06:51.365 ","End":"06:57.335","Text":"in an inheritance hierarchy because the method is always going to be overwritten?"},{"Start":"06:57.335 ","End":"07:01.640","Text":"For example, if we had a display details method in clothing,"},{"Start":"07:01.640 ","End":"07:04.130","Text":"we\u0027d never get to the display details method in"},{"Start":"07:04.130 ","End":"07:07.550","Text":"item through the dynamic dispatch process."},{"Start":"07:07.550 ","End":"07:13.505","Text":"All subclasses would find a match before ever getting to item\u0027s display details method."},{"Start":"07:13.505 ","End":"07:19.085","Text":"Maybe we don\u0027t even need to define a display details method in Item."},{"Start":"07:19.085 ","End":"07:21.754","Text":"Going back to an earlier example,"},{"Start":"07:21.754 ","End":"07:26.800","Text":"we don\u0027t show the employee class having a getMonthlyPay method."},{"Start":"07:26.800 ","End":"07:29.540","Text":"It wouldn\u0027t make sense to run and getMonthlyPay in"},{"Start":"07:29.540 ","End":"07:32.210","Text":"the base class of employee because we need to"},{"Start":"07:32.210 ","End":"07:35.374","Text":"know whether they are a full-time or part-time employee"},{"Start":"07:35.374 ","End":"07:37.970","Text":"before we can calculate their pay."},{"Start":"07:37.970 ","End":"07:41.990","Text":"It\u0027d be good therefore if we could just leave it to the subclasses to"},{"Start":"07:41.990 ","End":"07:46.160","Text":"implement the method and to have some way of checking that they do."},{"Start":"07:46.160 ","End":"07:51.545","Text":"In Java there is a way of doing this with the abstract keyword."},{"Start":"07:51.545 ","End":"07:55.985","Text":"An abstract method doesn\u0027t have an implementation."},{"Start":"07:55.985 ","End":"07:58.935","Text":"It\u0027s declared without a method body."},{"Start":"07:58.935 ","End":"08:01.970","Text":"Here we\u0027re defining getMonthlyPay as"},{"Start":"08:01.970 ","End":"08:05.385","Text":"a method that returns a double but takes no parameters,"},{"Start":"08:05.385 ","End":"08:10.970","Text":"but you\u0027ll notice there are no braces and no code inside the braces,"},{"Start":"08:10.970 ","End":"08:13.295","Text":"we just end with a semicolon."},{"Start":"08:13.295 ","End":"08:18.290","Text":"What this is signaling to the compiler is here is a method signature but"},{"Start":"08:18.290 ","End":"08:21.200","Text":"the actual implementation of the code for this method will"},{"Start":"08:21.200 ","End":"08:24.725","Text":"be in any subclasses that inherit from this method."},{"Start":"08:24.725 ","End":"08:28.820","Text":"If it isn\u0027t implemented in the subclasses that extend this class,"},{"Start":"08:28.820 ","End":"08:31.340","Text":"there will be a compile-time error."},{"Start":"08:31.340 ","End":"08:34.250","Text":"This forces the programmer to create a method"},{"Start":"08:34.250 ","End":"08:37.550","Text":"in the subclass that does whatever is needed."},{"Start":"08:37.550 ","End":"08:42.800","Text":"What this is signaling to the compiler is here is a method signature but"},{"Start":"08:42.800 ","End":"08:46.220","Text":"the actual implementation of the code for this method will be"},{"Start":"08:46.220 ","End":"08:50.545","Text":"in any subclasses that inherit from this class."},{"Start":"08:50.545 ","End":"08:54.980","Text":"An abstract class can be made up of a mixture of regular methods like"},{"Start":"08:54.980 ","End":"08:59.588","Text":"this constructor method and abstract methods,"},{"Start":"08:59.588 ","End":"09:02.270","Text":"but if there are any abstract methods in a class,"},{"Start":"09:02.270 ","End":"09:05.885","Text":"the whole class has to be declared as abstract."},{"Start":"09:05.885 ","End":"09:09.320","Text":"To summarize, an abstract method is a method that is"},{"Start":"09:09.320 ","End":"09:12.830","Text":"to declared with a signature but without a body."},{"Start":"09:12.830 ","End":"09:16.190","Text":"An abstract class is a class that\u0027s not intended to be"},{"Start":"09:16.190 ","End":"09:20.975","Text":"instantiated but to act as a superclass for other classes."},{"Start":"09:20.975 ","End":"09:25.639","Text":"It may or may not contain abstract methods."},{"Start":"09:25.639 ","End":"09:32.675","Text":"In UML diagrams, abstract classes or abstract methods are shown in italics."},{"Start":"09:32.675 ","End":"09:35.090","Text":"That\u0027s it for this topic."},{"Start":"09:35.090 ","End":"09:39.095","Text":"In this section we learned how to explain the concept of polymorphism,"},{"Start":"09:39.095 ","End":"09:42.740","Text":"describe the process of polymorphic method dispatch,"},{"Start":"09:42.740 ","End":"09:48.330","Text":"select, and apply appropriate techniques to make use of polymorphism."},{"Start":"09:48.340 ","End":"09:51.380","Text":"Be sure to work through the exercises."},{"Start":"09:51.380 ","End":"09:55.640","Text":"Many of the examples in this video are developed in those exercises."},{"Start":"09:55.640 ","End":"09:57.780","Text":"Thanks very much for watching."}],"ID":31102},{"Watched":false,"Name":"Exercise 1","Duration":"15m 36s","ChapterTopicVideoID":29491,"CourseChapterTopicPlaylistID":294472,"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.155","Text":"Hello everyone. Welcome to this video solution on polymorphism."},{"Start":"00:04.155 ","End":"00:05.850","Text":"In this exercise, we\u0027ve been asked,"},{"Start":"00:05.850 ","End":"00:10.350","Text":"first of all to create a new project and three classes within it as follows."},{"Start":"00:10.350 ","End":"00:15.900","Text":"We\u0027re given a UML diagram showing some relationships and 3 classes."},{"Start":"00:15.900 ","End":"00:20.970","Text":"One of the attributes in one of the classes is underlined."},{"Start":"00:20.970 ","End":"00:25.889","Text":"In part a, we\u0027re then told to implement all the selector methods within employee."},{"Start":"00:25.889 ","End":"00:29.310","Text":"The constructor for employee we\u0027re told should store the argument"},{"Start":"00:29.310 ","End":"00:32.430","Text":"into name and use count to keep track of"},{"Start":"00:32.430 ","End":"00:36.720","Text":"how many employees there are in total by incrementing count each time"},{"Start":"00:36.720 ","End":"00:42.299","Text":"the constructor is called and setting the instance variable ID to that number,"},{"Start":"00:42.299 ","End":"00:47.540","Text":"hence ensuring each employee has a different automatically generated ID."},{"Start":"00:47.540 ","End":"00:51.350","Text":"In part c, we\u0027re told that the constructor for FullTimeEmployee"},{"Start":"00:51.350 ","End":"00:55.130","Text":"has an extra argument which should be used to set the pay grade."},{"Start":"00:55.130 ","End":"00:57.830","Text":"The constructor for PartTimeEmployee has"},{"Start":"00:57.830 ","End":"01:01.730","Text":"an extra argument which should be used to set the hourly pay rate,"},{"Start":"01:01.730 ","End":"01:04.425","Text":"we\u0027re told in part d. In part e,"},{"Start":"01:04.425 ","End":"01:07.970","Text":"get monthly pay for FullTimeEmployee should return"},{"Start":"01:07.970 ","End":"01:13.040","Text":"a value which is 800 multiplied by the pay grade."},{"Start":"01:13.040 ","End":"01:18.815","Text":"Part f, get monthly pay for PartTimeEmployee should return a value which is hours,"},{"Start":"01:18.815 ","End":"01:21.080","Text":"multiplied by hourly rate."},{"Start":"01:21.080 ","End":"01:24.530","Text":"Then in part g, we\u0027re told that set hours should accept"},{"Start":"01:24.530 ","End":"01:30.035","Text":"a single int argument which is stored into the hours instance variable."},{"Start":"01:30.035 ","End":"01:32.195","Text":"We then test as follows."},{"Start":"01:32.195 ","End":"01:35.240","Text":"In part 1, we create an object from the base class on"},{"Start":"01:35.240 ","End":"01:38.595","Text":"the BlueJ workbench with the argument \"Bob\"."},{"Start":"01:38.595 ","End":"01:43.665","Text":"Then an object from FullTimeEmployee with the arguments \"Stu\" and 1,"},{"Start":"01:43.665 ","End":"01:49.100","Text":"and then an object from FullTimeEmployee again with arguments \"Petra\" and 2."},{"Start":"01:49.100 ","End":"01:54.534","Text":"Then another one from PartTimeEmployee with arguments \"Jo\""},{"Start":"01:54.534 ","End":"02:00.795","Text":"10.5 and then we run the setHours on that object with an argument of 10."},{"Start":"02:00.795 ","End":"02:05.340","Text":"Then we create an object from PartTimeEmployee with the arguments \"Jinnie\" and"},{"Start":"02:05.340 ","End":"02:11.505","Text":"11.5 and again run the setHours on that object with an argument of 20."},{"Start":"02:11.505 ","End":"02:16.625","Text":"Then we run getCount to see that 5 is returned for number of employees."},{"Start":"02:16.625 ","End":"02:20.510","Text":"We run get monthly pay on each respective object,"},{"Start":"02:20.510 ","End":"02:22.690","Text":"which return 800,"},{"Start":"02:22.690 ","End":"02:26.810","Text":"1,600, 105, and 230."},{"Start":"02:26.810 ","End":"02:33.020","Text":"We begin by putting the fields into the employee class, the attributes."},{"Start":"02:33.020 ","End":"02:37.639","Text":"We got two protected attributes, one called ID,"},{"Start":"02:37.639 ","End":"02:40.315","Text":"which is an int,"},{"Start":"02:40.315 ","End":"02:45.675","Text":"and one called name, which is a string."},{"Start":"02:45.675 ","End":"02:50.185","Text":"Then we have a private attribute called count,"},{"Start":"02:50.185 ","End":"02:54.520","Text":"but it\u0027s underlined so it\u0027s got to be a static class variable,"},{"Start":"02:54.520 ","End":"02:56.895","Text":"and that\u0027s an int."},{"Start":"02:56.895 ","End":"02:59.035","Text":"Once we\u0027ve got our attributes,"},{"Start":"02:59.035 ","End":"03:04.715","Text":"we can implement some of these selector methods."},{"Start":"03:04.715 ","End":"03:11.205","Text":"GetID is obviously going to return the ID which is an int,"},{"Start":"03:11.205 ","End":"03:16.365","Text":"so it\u0027s public int getID."},{"Start":"03:16.365 ","End":"03:21.000","Text":"You simply need to put return ID."},{"Start":"03:21.000 ","End":"03:25.905","Text":"I might as well just copy that for the other selectors."},{"Start":"03:25.905 ","End":"03:28.245","Text":"I\u0027ll just change the names."},{"Start":"03:28.245 ","End":"03:33.135","Text":"I\u0027ve got getName and getCount."},{"Start":"03:33.135 ","End":"03:35.490","Text":"There\u0027s my selector methods."},{"Start":"03:35.490 ","End":"03:42.860","Text":"It\u0027s traditional to put the constructor above all of those so let\u0027s put it here."},{"Start":"03:42.920 ","End":"03:45.225","Text":"I got this the wrong way round,"},{"Start":"03:45.225 ","End":"03:47.475","Text":"I need a int ID."},{"Start":"03:47.475 ","End":"03:49.380","Text":"Let\u0027s do the constructor,"},{"Start":"03:49.380 ","End":"03:50.844","Text":"which is part b."},{"Start":"03:50.844 ","End":"03:53.890","Text":"We\u0027ve got a few things to do here now,"},{"Start":"03:53.890 ","End":"03:58.375","Text":"which are first of all to keep track of"},{"Start":"03:58.375 ","End":"04:04.505","Text":"the number of employees we\u0027ve created from this object or any of its sub types."},{"Start":"04:04.505 ","End":"04:12.036","Text":"Our method has to be public and our return value has to be called employee."},{"Start":"04:12.036 ","End":"04:14.710","Text":"It only accepts one argument,"},{"Start":"04:14.710 ","End":"04:17.475","Text":"which is the name."},{"Start":"04:17.475 ","End":"04:21.610","Text":"This is business of increasing count each time we create an employee."},{"Start":"04:21.610 ","End":"04:23.300","Text":"If we start at 0,"},{"Start":"04:23.300 ","End":"04:28.140","Text":"we probably should set at 0 up here."},{"Start":"04:28.810 ","End":"04:32.060","Text":"Should be done by default anyway,"},{"Start":"04:32.060 ","End":"04:33.410","Text":"but it\u0027s good practice to do it."},{"Start":"04:33.410 ","End":"04:36.110","Text":"Then the first employee will be 1,"},{"Start":"04:36.110 ","End":"04:38.765","Text":"because we\u0027re going to store into"},{"Start":"04:38.765 ","End":"04:44.310","Text":"ID whatever the current value of count is and therefore,"},{"Start":"04:44.310 ","End":"04:47.190","Text":"the first one would be 1, the subsequent employee would be 2, and so on."},{"Start":"04:47.190 ","End":"04:50.760","Text":"They will have a unique ID. That\u0027s simple enough."},{"Start":"04:50.760 ","End":"04:54.950","Text":"Then we just store away into the instance variable,"},{"Start":"04:54.950 ","End":"04:57.476","Text":"the name of the person that\u0027s been passed in."},{"Start":"04:57.476 ","End":"05:02.000","Text":"So we have an employee with a name and they\u0027re automatically assigned an ID and we\u0027re"},{"Start":"05:02.000 ","End":"05:07.535","Text":"keeping track of how many employees we have using this static variable count here."},{"Start":"05:07.535 ","End":"05:09.900","Text":"That\u0027s all good."},{"Start":"05:09.900 ","End":"05:11.435","Text":"We\u0027ve got a problem here."},{"Start":"05:11.435 ","End":"05:14.645","Text":"That\u0027s because I\u0027m saying I\u0027m returning in int."},{"Start":"05:14.645 ","End":"05:17.180","Text":"But obviously I\u0027m returning a string."},{"Start":"05:17.180 ","End":"05:19.115","Text":"Whilst I\u0027m copying, I\u0027m pasting there."},{"Start":"05:19.115 ","End":"05:22.370","Text":"That\u0027s Part b done."},{"Start":"05:22.370 ","End":"05:27.510","Text":"Part c is the constructor for FullTimeEmployee."},{"Start":"05:27.510 ","End":"05:32.015","Text":"I\u0027ve already created my empty classes here, but there\u0027s no code."},{"Start":"05:32.015 ","End":"05:37.990","Text":"The first thing obviously we\u0027ve got to do is extend the employee object"},{"Start":"05:38.180 ","End":"05:47.330","Text":"and add any fields that we need FullTimeEmployee to have,"},{"Start":"05:47.330 ","End":"05:51.160","Text":"which are not in the base employee, and there is only one,"},{"Start":"05:51.160 ","End":"05:54.110","Text":"and we\u0027ve been asked to make it private and it\u0027s"},{"Start":"05:54.110 ","End":"05:59.015","Text":"an int and it\u0027s called grade because it\u0027s going to be the pay grade."},{"Start":"05:59.015 ","End":"06:00.995","Text":"That\u0027s fine."},{"Start":"06:00.995 ","End":"06:05.150","Text":"Then we create the constructor to just take"},{"Start":"06:05.150 ","End":"06:08.922","Text":"in that extra argument and use it to set the grade."},{"Start":"06:08.922 ","End":"06:10.145","Text":"Let\u0027s do that."},{"Start":"06:10.145 ","End":"06:15.545","Text":"Public, no return value, FullTimeEmployee,"},{"Start":"06:15.545 ","End":"06:17.605","Text":"that\u0027s the name of the class,"},{"Start":"06:17.605 ","End":"06:23.905","Text":"and we can accept a string called name."},{"Start":"06:23.905 ","End":"06:29.055","Text":"We\u0027re going to set name to that."},{"Start":"06:29.055 ","End":"06:38.310","Text":"However, we have to call the constructor of the parent class to do it."},{"Start":"06:38.310 ","End":"06:41.000","Text":"Actually, that\u0027s not what we need to do here."},{"Start":"06:41.000 ","End":"06:45.960","Text":"What we need to do here is to store away the pay grade."},{"Start":"06:49.990 ","End":"06:57.370","Text":"Our second parameter is an int called grade"},{"Start":"06:57.740 ","End":"07:07.610","Text":"and I store away whatever was passed in into this variable here, instance variable grade."},{"Start":"07:07.610 ","End":"07:11.275","Text":"That\u0027s the constructor for FullTimeEmployee."},{"Start":"07:11.275 ","End":"07:13.760","Text":"Let\u0027s do the one for PartTimeEmployee."},{"Start":"07:13.760 ","End":"07:16.940","Text":"Once again, I\u0027ve clicked an empty class to save us time."},{"Start":"07:16.940 ","End":"07:22.610","Text":"But I do need to add extends, employee setup,"},{"Start":"07:22.610 ","End":"07:24.885","Text":"this inheritance relationship,"},{"Start":"07:24.885 ","End":"07:27.585","Text":"and now the extra fields,"},{"Start":"07:27.585 ","End":"07:32.460","Text":"they\u0027re two, they\u0027re both private."},{"Start":"07:32.460 ","End":"07:37.035","Text":"One is hourly rate and that\u0027s a double."},{"Start":"07:37.035 ","End":"07:39.327","Text":"It could have been a float."},{"Start":"07:39.327 ","End":"07:41.050","Text":"But that\u0027s fine."},{"Start":"07:41.050 ","End":"07:43.360","Text":"It just saves us a bit of typing."},{"Start":"07:43.360 ","End":"07:47.995","Text":"The other one is an integer called hours."},{"Start":"07:47.995 ","End":"07:51.010","Text":"Presumably, this is the number of hours that they work and how much"},{"Start":"07:51.010 ","End":"07:54.190","Text":"they get paid per hour is the hourly rate."},{"Start":"07:54.190 ","End":"07:58.989","Text":"That\u0027s the first bit done of this class."},{"Start":"07:58.989 ","End":"08:00.910","Text":"Now we need to do the constructor,"},{"Start":"08:00.910 ","End":"08:06.085","Text":"as we\u0027ve been asked in part D. As usual,"},{"Start":"08:06.085 ","End":"08:07.450","Text":"it\u0027s going to be public,"},{"Start":"08:07.450 ","End":"08:16.120","Text":"same name as the class for the name of the constructor and whatever parameters we need."},{"Start":"08:16.120 ","End":"08:19.810","Text":"We know we need a name of the employee."},{"Start":"08:19.810 ","End":"08:22.629","Text":"In the case of a part-time employee,"},{"Start":"08:22.629 ","End":"08:29.020","Text":"we need to know what their hourly rate is so we can store that away."},{"Start":"08:29.020 ","End":"08:38.545","Text":"Then we simply call the constructor of the base class with that."},{"Start":"08:38.545 ","End":"08:44.365","Text":"Then we use this hourly rate"},{"Start":"08:44.365 ","End":"08:50.125","Text":"to take in the value stored in the parameter, hourlyRate."},{"Start":"08:50.125 ","End":"08:55.345","Text":"That\u0027s the constructor done for the part-time employee as well."},{"Start":"08:55.345 ","End":"09:01.090","Text":"Then now we\u0027re going to move on to creating the getMonthlyPay method."},{"Start":"09:01.090 ","End":"09:03.940","Text":"We\u0027ll start with the full-time employee."},{"Start":"09:03.940 ","End":"09:07.990","Text":"We return, according to that UML diagram,"},{"Start":"09:07.990 ","End":"09:10.000","Text":"a double to public."},{"Start":"09:10.000 ","End":"09:12.250","Text":"We return a double."},{"Start":"09:12.250 ","End":"09:14.665","Text":"The method is called getMonthlyPay."},{"Start":"09:14.665 ","End":"09:17.050","Text":"No parameters needed."},{"Start":"09:17.050 ","End":"09:20.995","Text":"All we\u0027re going to do is return a fixed number of 800."},{"Start":"09:20.995 ","End":"09:24.700","Text":"Not great practice. Would probably have that as a constant somewhere."},{"Start":"09:24.700 ","End":"09:28.780","Text":"Then we multiply that by their pay grade."},{"Start":"09:28.780 ","End":"09:31.630","Text":"That\u0027s it. That\u0027s what they get paid every"},{"Start":"09:31.630 ","End":"09:34.825","Text":"month is whatever their pay grade is times 800."},{"Start":"09:34.825 ","End":"09:38.545","Text":"That works fine for full-time employee."},{"Start":"09:38.545 ","End":"09:41.500","Text":"For a part-time employee,"},{"Start":"09:41.500 ","End":"09:43.315","Text":"we\u0027re told in part F,"},{"Start":"09:43.315 ","End":"09:46.180","Text":"we simply return a value which is"},{"Start":"09:46.180 ","End":"09:50.425","Text":"their hourly rate multiplied by the number of hours they work."},{"Start":"09:50.425 ","End":"09:52.645","Text":"Let\u0027s do that."},{"Start":"09:52.645 ","End":"09:55.225","Text":"This again is going to be a double."},{"Start":"09:55.225 ","End":"09:57.820","Text":"It has the same name,"},{"Start":"09:57.820 ","End":"09:59.245","Text":"this is really important,"},{"Start":"09:59.245 ","End":"10:06.135","Text":"and the same number and type of arguments as we did for the part-time person."},{"Start":"10:06.135 ","End":"10:09.815","Text":"I\u0027ve got monthly pay, not monthly rate."},{"Start":"10:09.815 ","End":"10:14.410","Text":"This time though, the implementation is different for a part-time worker."},{"Start":"10:14.410 ","End":"10:16.165","Text":"We\u0027re not interested in their pay grade."},{"Start":"10:16.165 ","End":"10:22.555","Text":"We simply multiply the number of hours they worked by their hourly rate,"},{"Start":"10:22.555 ","End":"10:26.620","Text":"and that will give us their pay figure."},{"Start":"10:26.620 ","End":"10:31.825","Text":"We\u0027ve done everything we\u0027ve been asked to do up to part F."},{"Start":"10:31.825 ","End":"10:37.150","Text":"The only thing we haven\u0027t done now before we can start testing is part G,"},{"Start":"10:37.150 ","End":"10:43.165","Text":"where we need to set the hours of the person so that know how many hours they\u0027ve worked."},{"Start":"10:43.165 ","End":"10:47.260","Text":"We can\u0027t do that when we create the employee because they haven\u0027t worked yet, presumably."},{"Start":"10:47.260 ","End":"10:48.550","Text":"At some later stage,"},{"Start":"10:48.550 ","End":"10:50.920","Text":"we can say how many hours they worked in a month."},{"Start":"10:50.920 ","End":"10:54.085","Text":"Therefore, we can calculate the pay rate."},{"Start":"10:54.085 ","End":"10:58.390","Text":"You can\u0027t always parse in arguments to a constructor because some of"},{"Start":"10:58.390 ","End":"11:03.475","Text":"those items might not be known at that point."},{"Start":"11:03.475 ","End":"11:07.615","Text":"This is where set methods come in, or modifier methods."},{"Start":"11:07.615 ","End":"11:09.730","Text":"We\u0027re going to set the hours,"},{"Start":"11:09.730 ","End":"11:13.600","Text":"but we have to parse in a value for how many hours they worked."},{"Start":"11:13.600 ","End":"11:15.220","Text":"That\u0027s going to be an integer."},{"Start":"11:15.220 ","End":"11:19.300","Text":"If they work part hours in our system, it won\u0027t work."},{"Start":"11:19.300 ","End":"11:21.895","Text":"It\u0027s always going to be whole number of hours."},{"Start":"11:21.895 ","End":"11:26.650","Text":"All we do is store away into hours."},{"Start":"11:26.650 ","End":"11:28.975","Text":"We\u0027re now able to test."},{"Start":"11:28.975 ","End":"11:32.050","Text":"Let\u0027s see if this all works as we expect."},{"Start":"11:32.050 ","End":"11:34.240","Text":"In the test, we\u0027ve been asked to,"},{"Start":"11:34.240 ","End":"11:37.510","Text":"first of all, in H1 or Part 1,"},{"Start":"11:37.510 ","End":"11:42.580","Text":"to create an object of type base class,"},{"Start":"11:42.580 ","End":"11:43.960","Text":"which is employee,"},{"Start":"11:43.960 ","End":"11:48.250","Text":"and parse in the value there of Bob."},{"Start":"11:48.250 ","End":"11:49.870","Text":"It seems to do that."},{"Start":"11:49.870 ","End":"11:52.240","Text":"It hasn\u0027t really complained or anything."},{"Start":"11:52.240 ","End":"11:53.695","Text":"It\u0027s not going to be a lot in there."},{"Start":"11:53.695 ","End":"11:56.020","Text":"Just Bob and an ID."},{"Start":"11:56.020 ","End":"11:59.110","Text":"It\u0027s created it as Employee Number 1,"},{"Start":"11:59.110 ","End":"12:01.105","Text":"which is what we would expect."},{"Start":"12:01.105 ","End":"12:04.285","Text":"Then in Part 2,"},{"Start":"12:04.285 ","End":"12:06.895","Text":"we create an object from full time."},{"Start":"12:06.895 ","End":"12:10.165","Text":"We call that person Stu,"},{"Start":"12:10.165 ","End":"12:12.280","Text":"and we give him a grade of 1."},{"Start":"12:12.280 ","End":"12:13.575","Text":"Seems to have worked."},{"Start":"12:13.575 ","End":"12:15.580","Text":"Part 3, do it again,"},{"Start":"12:15.580 ","End":"12:21.820","Text":"but different name and a different pay grade of 2. It\u0027s done."},{"Start":"12:21.820 ","End":"12:25.795","Text":"Part 4, we create a part-time employee."},{"Start":"12:25.795 ","End":"12:28.780","Text":"We call him Jo."},{"Start":"12:28.780 ","End":"12:32.020","Text":"Their hourly rate is 10.5."},{"Start":"12:32.020 ","End":"12:34.360","Text":"But that\u0027s not going to be enough on its own,"},{"Start":"12:34.360 ","End":"12:37.287","Text":"we then have to set how many hours they worked."},{"Start":"12:37.287 ","End":"12:40.405","Text":"We\u0027re going to set hours here on that object."},{"Start":"12:40.405 ","End":"12:45.030","Text":"We have been told to say 10 is the number of hours they worked,"},{"Start":"12:45.030 ","End":"12:46.710","Text":"so we give it the argument."},{"Start":"12:46.710 ","End":"12:48.360","Text":"That\u0027s Part 4 done."},{"Start":"12:48.360 ","End":"12:49.725","Text":"Finally, Part 5."},{"Start":"12:49.725 ","End":"12:52.110","Text":"One more part-time worker,"},{"Start":"12:52.110 ","End":"12:55.155","Text":"this time called Jinnie."},{"Start":"12:55.155 ","End":"12:59.745","Text":"Their hourly rate is 11.5."},{"Start":"12:59.745 ","End":"13:06.580","Text":"Their hours have to be set to 20."},{"Start":"13:06.840 ","End":"13:10.600","Text":"That\u0027s all of the objects created."},{"Start":"13:10.600 ","End":"13:15.370","Text":"We\u0027ve been asked to run getCount to see that 5 is returned for the number of employees."},{"Start":"13:15.370 ","End":"13:17.320","Text":"Let\u0027s see if that worked."},{"Start":"13:17.320 ","End":"13:21.740","Text":"We could run it on any of the objects, actually."},{"Start":"13:24.060 ","End":"13:27.475","Text":"No count. We can run it on here, but it appears on this menu."},{"Start":"13:27.475 ","End":"13:29.170","Text":"If we run it on 1 of the other ones,"},{"Start":"13:29.170 ","End":"13:30.805","Text":"we\u0027d have to go into sub-menu"},{"Start":"13:30.805 ","End":"13:34.450","Text":"for employee because it\u0027s obviously inherited it from employee."},{"Start":"13:34.450 ","End":"13:35.860","Text":"Just to prove that it works."},{"Start":"13:35.860 ","End":"13:37.180","Text":"There we go. There\u0027s 5."},{"Start":"13:37.180 ","End":"13:39.700","Text":"If I did it on another object,"},{"Start":"13:39.700 ","End":"13:41.740","Text":"it would also give us 5."},{"Start":"13:41.740 ","End":"13:43.990","Text":"If I did it on the base class there,"},{"Start":"13:43.990 ","End":"13:45.670","Text":"I don\u0027t have to go into a sub-menu,"},{"Start":"13:45.670 ","End":"13:47.800","Text":"but it also gives us 5."},{"Start":"13:47.800 ","End":"13:50.875","Text":"That\u0027s great. That seems to work fine."},{"Start":"13:50.875 ","End":"13:55.495","Text":"Now what about the monthly pay test in part J?"},{"Start":"13:55.495 ","End":"13:59.635","Text":"We can\u0027t get any pay out of this object here."},{"Start":"13:59.635 ","End":"14:01.900","Text":"This is an object which we don\u0027t know if it\u0027s"},{"Start":"14:01.900 ","End":"14:04.075","Text":"a full-time employee or a part-time employee."},{"Start":"14:04.075 ","End":"14:10.165","Text":"We don\u0027t actually get the option to run the getMonthlyPay,"},{"Start":"14:10.165 ","End":"14:11.755","Text":"but we can for the others."},{"Start":"14:11.755 ","End":"14:16.270","Text":"For that 1st object, we get 800."},{"Start":"14:16.270 ","End":"14:20.720","Text":"For the 2nd one, we get 1,600."},{"Start":"14:21.030 ","End":"14:25.660","Text":"Third one, we get 105."},{"Start":"14:25.660 ","End":"14:29.350","Text":"The final one, we get 230."},{"Start":"14:29.350 ","End":"14:31.255","Text":"They\u0027re all exactly as we expected."},{"Start":"14:31.255 ","End":"14:32.650","Text":"Why do we get those values?"},{"Start":"14:32.650 ","End":"14:35.139","Text":"Well, we can see if we inspect the object,"},{"Start":"14:35.139 ","End":"14:42.200","Text":"this is someone who worked for 10 hours at an hourly rate of 10.5."},{"Start":"14:42.570 ","End":"14:47.260","Text":"Therefore, that person gets paid 105."},{"Start":"14:47.260 ","End":"14:50.245","Text":"That was the object called partTime1."},{"Start":"14:50.245 ","End":"14:52.165","Text":"If I run on partTime1,"},{"Start":"14:52.165 ","End":"14:55.525","Text":"getMonthlyPay again, you\u0027ll see it does give me 105."},{"Start":"14:55.525 ","End":"15:01.405","Text":"Now what\u0027s interesting about this one is the getMonthlyPay method,"},{"Start":"15:01.405 ","End":"15:02.980","Text":"a different one runs,"},{"Start":"15:02.980 ","End":"15:08.665","Text":"depending on whether it was a full-time employee or a part-time employee."},{"Start":"15:08.665 ","End":"15:18.835","Text":"In this case, it\u0027s just a fixed calculation of 800 times the pay grade."},{"Start":"15:18.835 ","End":"15:22.240","Text":"This one, it was 800 times the pay grade again,"},{"Start":"15:22.240 ","End":"15:23.905","Text":"which gave us 800,"},{"Start":"15:23.905 ","End":"15:26.035","Text":"because they were Pay Grade 1."},{"Start":"15:26.035 ","End":"15:30.310","Text":"Nice little general starting exercise. Lots of classes."},{"Start":"15:30.310 ","End":"15:33.755","Text":"We\u0027re going to need to use these in the next couple of exercises."},{"Start":"15:33.755 ","End":"15:36.630","Text":"I\u0027ll see you shortly for those."}],"ID":31103},{"Watched":false,"Name":"Exercise 2","Duration":"9m 35s","ChapterTopicVideoID":29490,"CourseChapterTopicPlaylistID":294472,"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. Welcome to this exercise in which we\u0027ve been told to change"},{"Start":"00:03.150 ","End":"00:08.330","Text":"the employee class from the previous exercise into an abstract class."},{"Start":"00:08.330 ","End":"00:12.150","Text":"Then, in Part A, we\u0027re told on the object workbench, once again,"},{"Start":"00:12.150 ","End":"00:14.609","Text":"to attempt to create a part-time employee,"},{"Start":"00:14.609 ","End":"00:18.720","Text":"a full-time employee, and an employee from the base class."},{"Start":"00:18.720 ","End":"00:22.590","Text":"We\u0027re then asked the question what has changed and why?"},{"Start":"00:22.590 ","End":"00:26.850","Text":"In Part B, we\u0027re told to add the @override annotation before"},{"Start":"00:26.850 ","End":"00:31.630","Text":"the getMonthlyPay method in either the full-time or part-time employee class,"},{"Start":"00:31.630 ","End":"00:35.000","Text":"and we\u0027re again asked what happens and why?"},{"Start":"00:35.000 ","End":"00:40.280","Text":"In Part C, we\u0027re told to implement a dummy getMonthlyPay method in the relevant class,"},{"Start":"00:40.280 ","End":"00:42.895","Text":"which always returns the value zero."},{"Start":"00:42.895 ","End":"00:47.795","Text":"We then add @override annotation before getMonthlyPay"},{"Start":"00:47.795 ","End":"00:53.555","Text":"in the 2 relevant places and confirm there are no compiler error messages."},{"Start":"00:53.555 ","End":"00:55.700","Text":"Finally, we\u0027re told to remove"},{"Start":"00:55.700 ","End":"01:00.095","Text":"the dummy getMonthlyPay method and replace it with an abstract method."},{"Start":"01:00.095 ","End":"01:03.710","Text":"Once again, we confirm there are no compiler error messages,"},{"Start":"01:03.710 ","End":"01:05.300","Text":"and we\u0027re asked the question,"},{"Start":"01:05.300 ","End":"01:10.350","Text":"how is an abstract class different to an abstract method?"},{"Start":"01:10.350 ","End":"01:12.500","Text":"In the opening part of the question,"},{"Start":"01:12.500 ","End":"01:13.655","Text":"we\u0027ve been told here to make"},{"Start":"01:13.655 ","End":"01:18.665","Text":"this employee class from that previous exercise, an abstract class."},{"Start":"01:18.665 ","End":"01:22.910","Text":"Go into the class definition and literally all we\u0027ve got to"},{"Start":"01:22.910 ","End":"01:27.110","Text":"do is put the keyword abstract in the right place,"},{"Start":"01:27.110 ","End":"01:33.740","Text":"which is after the access modifier and before the keyword class."},{"Start":"01:33.740 ","End":"01:35.510","Text":"If we compile now,"},{"Start":"01:35.510 ","End":"01:37.040","Text":"don\u0027t get any errors."},{"Start":"01:37.040 ","End":"01:38.330","Text":"Click on \"Close\","},{"Start":"01:38.330 ","End":"01:41.900","Text":"you\u0027ll see that the blue jay display has changed,"},{"Start":"01:41.900 ","End":"01:43.880","Text":"so as an abstract,"},{"Start":"01:43.880 ","End":"01:47.495","Text":"the word abstract within these little angle braces,"},{"Start":"01:47.495 ","End":"01:52.835","Text":"and these 2 classes are now grayed out because we need to recompile the whole project"},{"Start":"01:52.835 ","End":"01:58.715","Text":"because it recognizes that the class that has been inherited from has changed."},{"Start":"01:58.715 ","End":"02:00.500","Text":"If we click on \"Compile\","},{"Start":"02:00.500 ","End":"02:04.615","Text":"that hashing goes away and all is good again."},{"Start":"02:04.615 ","End":"02:08.705","Text":"In Part 2A it asks us to create,"},{"Start":"02:08.705 ","End":"02:11.915","Text":"again as we did in the testing for the previous exercise,"},{"Start":"02:11.915 ","End":"02:14.210","Text":"a part-time employee and a full-time employee."},{"Start":"02:14.210 ","End":"02:17.330","Text":"Let\u0027s do that, and also an employee."},{"Start":"02:17.330 ","End":"02:21.935","Text":"Now if I go to create an object of type employee,"},{"Start":"02:21.935 ","End":"02:24.095","Text":"you\u0027ll notice there\u0027s a problem."},{"Start":"02:24.095 ","End":"02:28.400","Text":"There\u0027s no option here for me to create a new object as there is with these,"},{"Start":"02:28.400 ","End":"02:31.110","Text":"so the new doesn\u0027t appear."},{"Start":"02:31.110 ","End":"02:34.525","Text":"There\u0027s no constructor that appears alongside it."},{"Start":"02:34.525 ","End":"02:38.720","Text":"That\u0027s right because that\u0027s exactly what abstract does."},{"Start":"02:38.720 ","End":"02:41.705","Text":"As we\u0027ve been asked later in the question to explain,"},{"Start":"02:41.705 ","End":"02:45.910","Text":"it means you cannot instantiate an object of this type."},{"Start":"02:45.910 ","End":"02:49.085","Text":"That\u0027s why the workbench doesn\u0027t allow me to do it."},{"Start":"02:49.085 ","End":"02:51.440","Text":"That\u0027s Part A done."},{"Start":"02:51.440 ","End":"02:57.095","Text":"Part B says to add the override annotation before they"},{"Start":"02:57.095 ","End":"03:04.035","Text":"getMonthlyPay method in either of these 2 classes and to see what happens."},{"Start":"03:04.035 ","End":"03:13.130","Text":"Annotations are something that you can put into programs that are optional."},{"Start":"03:13.130 ","End":"03:14.780","Text":"But they\u0027re basically signals to"},{"Start":"03:14.780 ","End":"03:19.710","Text":"the compiler and can be useful in generating warnings and so on."},{"Start":"03:19.710 ","End":"03:23.615","Text":"If I try and compile this now with the @override line,"},{"Start":"03:23.615 ","End":"03:27.890","Text":"because I\u0027m overriding something is what I\u0027m saying."},{"Start":"03:27.890 ","End":"03:32.105","Text":"Because you\u0027ll notice that getMonthlyPay has a different implementation"},{"Start":"03:32.105 ","End":"03:36.845","Text":"in full-time employee and part-time employee."},{"Start":"03:36.845 ","End":"03:40.400","Text":"Obviously, we multiply hours by hourly rate and this"},{"Start":"03:40.400 ","End":"03:45.050","Text":"1 and we do just the pay grade times 800 in the other 1."},{"Start":"03:45.050 ","End":"03:49.025","Text":"You\u0027ll see that there\u0027s an error message for both cases."},{"Start":"03:49.025 ","End":"03:51.980","Text":"It\u0027s the same error message and it says method does not"},{"Start":"03:51.980 ","End":"03:56.690","Text":"override or implement a method from a supertype,"},{"Start":"03:56.690 ","End":"03:57.980","Text":"which is basically saying,"},{"Start":"03:57.980 ","End":"04:01.100","Text":"you are telling me that you\u0027re overriding with this method,"},{"Start":"04:01.100 ","End":"04:03.470","Text":"getMonthlyPay, which is what it appears to"},{"Start":"04:03.470 ","End":"04:06.350","Text":"be doing because I\u0027ve got a different getMonthlyPay over here."},{"Start":"04:06.350 ","End":"04:08.660","Text":"But it\u0027s complaining."},{"Start":"04:08.660 ","End":"04:13.279","Text":"What it\u0027s saying is there\u0027s nothing in the base class"},{"Start":"04:13.279 ","End":"04:18.365","Text":"called getMonthlyPay that you\u0027re actually overriding,"},{"Start":"04:18.365 ","End":"04:24.845","Text":"so that takes us to Part C and we can implement a dummy,"},{"Start":"04:24.845 ","End":"04:27.260","Text":"getMonthlyPay, which doesn\u0027t really do anything."},{"Start":"04:27.260 ","End":"04:31.130","Text":"It just returns 0 every time,"},{"Start":"04:31.130 ","End":"04:35.090","Text":"but it will at least make the warning go away hopefully,"},{"Start":"04:35.090 ","End":"04:37.040","Text":"and we will see that happening."},{"Start":"04:37.040 ","End":"04:39.575","Text":"GetMonthlyPay returns double;"},{"Start":"04:39.575 ","End":"04:49.335","Text":"has to have the same signature to be an override, no arguments."},{"Start":"04:49.335 ","End":"04:52.245","Text":"We\u0027ll just return 0."},{"Start":"04:52.245 ","End":"04:59.450","Text":"That is in the base class in employee and it\u0027s compiled fine."},{"Start":"04:59.450 ","End":"05:04.805","Text":"Now does this error message go away if we try and compile it?"},{"Start":"05:04.805 ","End":"05:07.310","Text":"It does not. Why not?"},{"Start":"05:07.310 ","End":"05:10.760","Text":"Double getMonthlyPay; have I spelled the same way?"},{"Start":"05:10.760 ","End":"05:13.850","Text":"Does it have the same method signature?"},{"Start":"05:13.850 ","End":"05:17.135","Text":"Double getMonthlyPay."},{"Start":"05:17.135 ","End":"05:21.510","Text":"Double getMonthlyPay does appear to."},{"Start":"05:21.860 ","End":"05:24.890","Text":"Well, let\u0027s try and recompile the whole project;"},{"Start":"05:24.890 ","End":"05:26.420","Text":"that\u0027s made it go away."},{"Start":"05:26.420 ","End":"05:32.180","Text":"There\u0027s something about the previous conditions that existed while I was checking it,"},{"Start":"05:32.180 ","End":"05:36.410","Text":"but you\u0027ll see now that this @override is absolutely fine."},{"Start":"05:36.410 ","End":"05:39.020","Text":"It\u0027s happy with it. Still compile without it."},{"Start":"05:39.020 ","End":"05:43.925","Text":"But you remember before we couldn\u0027t even get it to compile because it would say, hang on."},{"Start":"05:43.925 ","End":"05:46.280","Text":"You told me you\u0027re overriding this thing."},{"Start":"05:46.280 ","End":"05:53.070","Text":"But I can\u0027t find the reference to it in the base class."},{"Start":"05:53.070 ","End":"05:55.380","Text":"That\u0027s good. That seems to work."},{"Start":"05:55.380 ","End":"06:00.110","Text":"Now we have used the annotation override in the relevant places."},{"Start":"06:00.110 ","End":"06:01.670","Text":"When you\u0027re reading this code,"},{"Start":"06:01.670 ","End":"06:05.465","Text":"it\u0027s easier to see that this is a method thus been"},{"Start":"06:05.465 ","End":"06:11.225","Text":"overridden and there\u0027s going to be a getMonthlyPay in the base class as well."},{"Start":"06:11.225 ","End":"06:15.500","Text":"However, it\u0027s a bit rubbish in this particular example"},{"Start":"06:15.500 ","End":"06:19.920","Text":"because we don\u0027t want a dummy function if we can avoid it."},{"Start":"06:19.920 ","End":"06:21.740","Text":"Part D moves us on to that."},{"Start":"06:21.740 ","End":"06:25.850","Text":"It says, remove the dummy, getMonthlyPay method,"},{"Start":"06:25.850 ","End":"06:28.490","Text":"and replace it with an abstract method."},{"Start":"06:28.490 ","End":"06:33.305","Text":"An abstract method is 1 that doesn\u0027t have a method body."},{"Start":"06:33.305 ","End":"06:37.010","Text":"There is no code associated with an abstract method."},{"Start":"06:37.010 ","End":"06:40.435","Text":"We simply take away all of those,"},{"Start":"06:40.435 ","End":"06:43.790","Text":"the curly brackets and everything inside them."},{"Start":"06:43.790 ","End":"06:47.000","Text":"Keep the signature as it is."},{"Start":"06:47.000 ","End":"06:50.240","Text":"But what we do is we put abstract in front of it,"},{"Start":"06:50.240 ","End":"06:53.195","Text":"just very similar to the class definition."},{"Start":"06:53.195 ","End":"06:59.485","Text":"We\u0027re saying this method is abstract as well,"},{"Start":"06:59.485 ","End":"07:02.295","Text":"and we don\u0027t get any errors there."},{"Start":"07:02.295 ","End":"07:04.415","Text":"Now we don\u0027t have a dummy method."},{"Start":"07:04.415 ","End":"07:09.725","Text":"What we have is a signature of a method called getMonthlyPay,"},{"Start":"07:09.725 ","End":"07:11.300","Text":"which returns a double."},{"Start":"07:11.300 ","End":"07:15.990","Text":"But it doesn\u0027t actually have an implementation in here."},{"Start":"07:15.990 ","End":"07:20.645","Text":"It will still compile and it will still work as we expect it to."},{"Start":"07:20.645 ","End":"07:24.155","Text":"I cannot instantiate employee object."},{"Start":"07:24.155 ","End":"07:29.075","Text":"I can though instantiate a full-time employee or a part-time employee,"},{"Start":"07:29.075 ","End":"07:31.700","Text":"which inherits from employee."},{"Start":"07:31.700 ","End":"07:35.435","Text":"The answer to the question that we posed there is,"},{"Start":"07:35.435 ","End":"07:39.620","Text":"how is an abstract class different to an abstract method?"},{"Start":"07:39.620 ","End":"07:43.160","Text":"Well, an abstract class cannot be instantiated."},{"Start":"07:43.160 ","End":"07:46.025","Text":"That\u0027s what we saw when we tried to do new here."},{"Start":"07:46.025 ","End":"07:47.900","Text":"You just simply can\u0027t do it."},{"Start":"07:47.900 ","End":"07:57.480","Text":"Whereas a abstract method is 1 that\u0027s going to be implemented later on in a subclass."},{"Start":"07:57.480 ","End":"08:00.860","Text":"The abstract method here, getMonthlyPay,"},{"Start":"08:00.860 ","End":"08:06.620","Text":"was implemented in this subclass and this was its implementation."},{"Start":"08:06.620 ","End":"08:09.965","Text":"It had a different implementation in"},{"Start":"08:09.965 ","End":"08:15.065","Text":"this subclass here where we multiplied hours by hourly rate."},{"Start":"08:15.065 ","End":"08:18.475","Text":"Few things in that exercise that we\u0027ve covered;"},{"Start":"08:18.475 ","End":"08:22.035","Text":"1 is this idea of an abstract class,"},{"Start":"08:22.035 ","End":"08:25.655","Text":"1 that you can\u0027t instantiate objects from;"},{"Start":"08:25.655 ","End":"08:29.225","Text":"2 is this override annotation,"},{"Start":"08:29.225 ","End":"08:31.820","Text":"which is just good practice,"},{"Start":"08:31.820 ","End":"08:34.460","Text":"basically forces the compiler to"},{"Start":"08:34.460 ","End":"08:38.540","Text":"check that we\u0027ve got the right signatures and everything,"},{"Start":"08:38.540 ","End":"08:40.910","Text":"so that if we think we\u0027re overriding,"},{"Start":"08:40.910 ","End":"08:42.680","Text":"we are actually overriding,"},{"Start":"08:42.680 ","End":"08:44.660","Text":"it will do that check for us."},{"Start":"08:44.660 ","End":"08:48.304","Text":"Then finally, this idea of an abstract method,"},{"Start":"08:48.304 ","End":"08:56.220","Text":"which is a method that has to be implemented in 1 of the child classes,"},{"Start":"08:56.220 ","End":"09:00.525","Text":"and we mark it here with its signature."},{"Start":"09:00.525 ","End":"09:02.535","Text":"What does it return?"},{"Start":"09:02.535 ","End":"09:05.150","Text":"What is it called? What are its arguments?"},{"Start":"09:05.150 ","End":"09:07.610","Text":"But we put the word abstract in front,"},{"Start":"09:07.610 ","End":"09:11.600","Text":"and that means we don\u0027t have to provide an implementation,"},{"Start":"09:11.600 ","End":"09:14.375","Text":"a dummy implementation in here."},{"Start":"09:14.375 ","End":"09:17.345","Text":"That\u0027s seems silly and it is silly."},{"Start":"09:17.345 ","End":"09:20.390","Text":"This function of marketing something as"},{"Start":"09:20.390 ","End":"09:25.130","Text":"abstract has been provided in the language and in many object-oriented languages,"},{"Start":"09:25.130 ","End":"09:31.125","Text":"similar concepts exist so that we don\u0027t have to do these dummy methods."},{"Start":"09:31.125 ","End":"09:35.700","Text":"That\u0027s it for this 1. Thanks very much for watching and see you in the next 1."}],"ID":31104},{"Watched":false,"Name":"Exercise 3","Duration":"14m 57s","ChapterTopicVideoID":29489,"CourseChapterTopicPlaylistID":294472,"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, welcome back. In this exercise,"},{"Start":"00:02.670 ","End":"00:05.400","Text":"we\u0027ve been asked to create a new class AppLaunch"},{"Start":"00:05.400 ","End":"00:07.770","Text":"within the project we used in the previous exercise."},{"Start":"00:07.770 ","End":"00:09.090","Text":"Then in part A,"},{"Start":"00:09.090 ","End":"00:13.065","Text":"within app launch to create a public static method main,"},{"Start":"00:13.065 ","End":"00:18.645","Text":"which accepts an argument of a string array and returns no values."},{"Start":"00:18.645 ","End":"00:22.560","Text":"In part B, we\u0027re told to create 3 variables inside the main method,"},{"Start":"00:22.560 ","End":"00:24.240","Text":"emp1, emp2,"},{"Start":"00:24.240 ","End":"00:27.555","Text":"and emp3 of type employee."},{"Start":"00:27.555 ","End":"00:32.690","Text":"In part C, we\u0027re told to instantiate an object of type part-time employee with"},{"Start":"00:32.690 ","End":"00:38.665","Text":"arguments dot and 10.5 and assigned to it the emp1 variable."},{"Start":"00:38.665 ","End":"00:41.300","Text":"In part D, we instantiate an object of"},{"Start":"00:41.300 ","End":"00:45.560","Text":"type full-time employee with arguments of dash and 1,"},{"Start":"00:45.560 ","End":"00:49.045","Text":"and assigned to it the emp2 variable."},{"Start":"00:49.045 ","End":"00:50.685","Text":"In part E, then,"},{"Start":"00:50.685 ","End":"00:54.905","Text":"we add 2 lines of code using system out print line to output"},{"Start":"00:54.905 ","End":"01:00.295","Text":"a monthly wages using the 2 objects stored in emp1 and emp2."},{"Start":"01:00.295 ","End":"01:03.895","Text":"In part F, we assign the contents of emp1, 2, and 3,"},{"Start":"01:03.895 ","End":"01:07.595","Text":"and output the monthly wages of the objects stored in emp3."},{"Start":"01:07.595 ","End":"01:09.240","Text":"In part G,"},{"Start":"01:09.240 ","End":"01:12.450","Text":"we assign the contents of emp2 to emp3,"},{"Start":"01:12.450 ","End":"01:16.870","Text":"and output a monthly wages of the objects stored in emp3 and then in part H,"},{"Start":"01:16.870 ","End":"01:19.805","Text":"we compile our code and run the main method."},{"Start":"01:19.805 ","End":"01:22.070","Text":"We should see an output of 0,"},{"Start":"01:22.070 ","End":"01:25.765","Text":"800, 0, and 800."},{"Start":"01:25.765 ","End":"01:30.230","Text":"In part I, we\u0027re asked how it was possible for emp3 to be"},{"Start":"01:30.230 ","End":"01:34.940","Text":"assigned a value of emp1 or emp2 when they\u0027re both different types."},{"Start":"01:34.940 ","End":"01:36.440","Text":"In part J,"},{"Start":"01:36.440 ","End":"01:38.885","Text":"we\u0027re told that dots hours are currently 0."},{"Start":"01:38.885 ","End":"01:41.060","Text":"We should attempt to add a line to run"},{"Start":"01:41.060 ","End":"01:45.605","Text":"the set hours method for emp1 where dot is currently stored,"},{"Start":"01:45.605 ","End":"01:47.815","Text":"we use an argument of 10."},{"Start":"01:47.815 ","End":"01:49.240","Text":"In part K,"},{"Start":"01:49.240 ","End":"01:53.345","Text":"we\u0027re asked the question what compiler error is generated and why."},{"Start":"01:53.345 ","End":"01:55.700","Text":"Then in part L, finally,"},{"Start":"01:55.700 ","End":"01:58.940","Text":"we\u0027re told that this problem can be fixed by moving an attribute or"},{"Start":"01:58.940 ","End":"02:02.875","Text":"a method in 1 of the classes to a different location."},{"Start":"02:02.875 ","End":"02:07.520","Text":"We\u0027re told to implement the changes that will enable the program to successfully compile."},{"Start":"02:07.520 ","End":"02:15.600","Text":"We then test by running main and checking that it produces an output of 105 and 800."},{"Start":"02:16.100 ","End":"02:19.950","Text":"Once we\u0027ve created the new class put"},{"Start":"02:19.950 ","End":"02:23.885","Text":"AppLaunch truthfulness existing project from the previous exercise,"},{"Start":"02:23.885 ","End":"02:26.920","Text":"we can create our main method."},{"Start":"02:26.920 ","End":"02:32.465","Text":"Remember, there\u0027s an entry point for all Java programs called Main,"},{"Start":"02:32.465 ","End":"02:34.310","Text":"but it has this signature,"},{"Start":"02:34.310 ","End":"02:43.025","Text":"public static returns nothing so void main and the argument is an array of strings,"},{"Start":"02:43.025 ","End":"02:45.020","Text":"which you can call it anything you like,"},{"Start":"02:45.020 ","End":"02:47.205","Text":"but should call it arcs."},{"Start":"02:47.205 ","End":"02:51.710","Text":"There is our method declared and then in part B,"},{"Start":"02:51.710 ","End":"02:54.805","Text":"we\u0027ve been asked to create 3 variables inside this method,"},{"Start":"02:54.805 ","End":"02:56.770","Text":"emp1, 2 and 3,"},{"Start":"02:56.770 ","End":"03:00.020","Text":"and they\u0027re all of the same type, which is employee."},{"Start":"03:00.020 ","End":"03:03.890","Text":"Here, we\u0027re saying that we\u0027ve gotten a reference to"},{"Start":"03:03.890 ","End":"03:09.570","Text":"an employee class and we\u0027re going to store them in 3 variables,"},{"Start":"03:09.570 ","End":"03:12.600","Text":"emp1, emp2 and emp3,"},{"Start":"03:12.600 ","End":"03:16.480","Text":"emp being an abbreviation for employee, presumably."},{"Start":"03:16.480 ","End":"03:19.715","Text":"We can do that even though we were told,"},{"Start":"03:19.715 ","End":"03:25.895","Text":"you cannot instantiate objects from an employee class,"},{"Start":"03:25.895 ","End":"03:30.769","Text":"you can still declare variables that hold employees strangely,"},{"Start":"03:30.769 ","End":"03:36.005","Text":"and we\u0027ll see how and why throughout the rest of this exercise."},{"Start":"03:36.005 ","End":"03:37.490","Text":"That\u0027s part B done."},{"Start":"03:37.490 ","End":"03:41.360","Text":"Now, what we\u0027re going to do is I\u0027m going to instantiate an object of"},{"Start":"03:41.360 ","End":"03:47.778","Text":"type part-time employee and we\u0027re going to store it and assign it to the emp1 variable."},{"Start":"03:47.778 ","End":"03:50.145","Text":"Emp1 has already been declared,"},{"Start":"03:50.145 ","End":"03:55.775","Text":"but we want to create an object and store that object or a reference to our object"},{"Start":"03:55.775 ","End":"04:01.960","Text":"into emp1 and the object is of type part-time employee."},{"Start":"04:01.960 ","End":"04:04.070","Text":"To create a part-time employee,"},{"Start":"04:04.070 ","End":"04:09.110","Text":"we need to send some arguments in and we\u0027d been given some arguments here."},{"Start":"04:09.110 ","End":"04:15.095","Text":"The name is dot and the hourly pay rate is 10.5,"},{"Start":"04:15.095 ","End":"04:17.795","Text":"so it seems to be no complaints there."},{"Start":"04:17.795 ","End":"04:21.530","Text":"We can move on to part D and in this case,"},{"Start":"04:21.530 ","End":"04:24.385","Text":"we are assigning to emp2."},{"Start":"04:24.385 ","End":"04:29.060","Text":"This time, it\u0027s a full-time employee, name of dash,"},{"Start":"04:29.060 ","End":"04:36.005","Text":"pay grade of 1 so the constructor is different because the object we\u0027re creating it from"},{"Start":"04:36.005 ","End":"04:37.715","Text":"is different and they have"},{"Start":"04:37.715 ","End":"04:42.830","Text":"different constructors and we\u0027re assigning them to different variables,"},{"Start":"04:42.830 ","End":"04:46.225","Text":"but they\u0027re of the same type, curiously."},{"Start":"04:46.225 ","End":"04:48.590","Text":"We\u0027ll see what implications that has,"},{"Start":"04:48.590 ","End":"04:50.425","Text":"as I said in a moment."},{"Start":"04:50.425 ","End":"04:53.080","Text":"Now, in part E,"},{"Start":"04:53.080 ","End":"04:55.580","Text":"we\u0027re simply going to print out"},{"Start":"04:55.580 ","End":"04:59.090","Text":"the monthly wages of both of"},{"Start":"04:59.090 ","End":"05:02.930","Text":"these employees using whatever means we have available and you remember,"},{"Start":"05:02.930 ","End":"05:05.740","Text":"there\u0027s a method called Get monthly pay,"},{"Start":"05:05.740 ","End":"05:10.920","Text":"so what we\u0027re going to do is system.out.println,"},{"Start":"05:10.920 ","End":"05:19.405","Text":"and then the name of the variable and then we can use dot notation to call a method and"},{"Start":"05:19.405 ","End":"05:23.285","Text":"our methods get monthly pay and that will print out"},{"Start":"05:23.285 ","End":"05:28.800","Text":"a monthly pay of dot and then we can do the same thing."},{"Start":"05:28.800 ","End":"05:30.290","Text":"I\u0027ll copy this line."},{"Start":"05:30.290 ","End":"05:38.665","Text":"See me typing for employee 2 and then that will print out the monthly pay of employee 2."},{"Start":"05:38.665 ","End":"05:44.355","Text":"That\u0027s part E done and then part F says,"},{"Start":"05:44.355 ","End":"05:48.690","Text":"assign the contents of emp1 to emp3."},{"Start":"05:48.690 ","End":"05:50.760","Text":"That\u0027s easy enough. We just say,"},{"Start":"05:50.760 ","End":"05:54.740","Text":"emp3 equals emp1 and then,"},{"Start":"05:54.740 ","End":"06:00.665","Text":"we\u0027re going to output the monthly wages of the object stored in emp3,"},{"Start":"06:00.665 ","End":"06:07.820","Text":"so we\u0027ll copy this line above and change the reference in it to emp3."},{"Start":"06:07.820 ","End":"06:10.685","Text":"What do we think that will print out?"},{"Start":"06:10.685 ","End":"06:17.810","Text":"Then G is the same pretty much as these previous 2 lines except this time,"},{"Start":"06:17.810 ","End":"06:24.430","Text":"we\u0027re going to get emp3s value from emp2."},{"Start":"06:24.430 ","End":"06:26.490","Text":"You may think,"},{"Start":"06:26.490 ","End":"06:28.290","Text":"you know what\u0027s going to be outputs,"},{"Start":"06:28.290 ","End":"06:32.990","Text":"but let\u0027s check that in part H. It tells us,"},{"Start":"06:32.990 ","End":"06:36.365","Text":"we should run the main method and we should see 0,"},{"Start":"06:36.365 ","End":"06:38.390","Text":"800, 0, 800."},{"Start":"06:38.390 ","End":"06:42.665","Text":"Let\u0027s see if we get that and we do. That\u0027s great."},{"Start":"06:42.665 ","End":"06:45.380","Text":"We\u0027ve basically been able to instantiate a bunch of"},{"Start":"06:45.380 ","End":"06:50.750","Text":"objects from the part-time employee and full-time employee class."},{"Start":"06:50.750 ","End":"06:56.675","Text":"The third employee, we never created an object for them,"},{"Start":"06:56.675 ","End":"07:03.330","Text":"but we were able to assign the value of emp1 or emp2 to there."},{"Start":"07:03.330 ","End":"07:06.930","Text":"We could therefore make use of the methods in there."},{"Start":"07:06.930 ","End":"07:10.050","Text":"Basically, we\u0027re getting a copy of a reference to"},{"Start":"07:10.050 ","End":"07:16.740","Text":"this object or this object and when we get into employee 3,"},{"Start":"07:16.740 ","End":"07:19.850","Text":"we can do everything we can do with a normal object."},{"Start":"07:19.850 ","End":"07:23.790","Text":"We didn\u0027t have to instantiate a new object here,"},{"Start":"07:23.790 ","End":"07:27.270","Text":"we could simply use a reference to an existing object."},{"Start":"07:27.270 ","End":"07:30.270","Text":"Because they\u0027re all of type employee,"},{"Start":"07:30.270 ","End":"07:36.224","Text":"it didn\u0027t even matter if they were a full-time employee or a part-time employee,"},{"Start":"07:36.224 ","End":"07:40.200","Text":"both of these assignments worked as we expected."},{"Start":"07:40.200 ","End":"07:44.415","Text":"Now the issue here, we\u0027ve been asked about,"},{"Start":"07:44.415 ","End":"07:47.429","Text":"how is it possible for emp3 to be assigned"},{"Start":"07:47.429 ","End":"07:53.036","Text":"the value of emp1 or emp2 because they\u0027re both different types,"},{"Start":"07:53.036 ","End":"07:54.245","Text":"1 is a part-time employee,"},{"Start":"07:54.245 ","End":"07:55.490","Text":"1 is a full-time employee?"},{"Start":"07:55.490 ","End":"07:57.990","Text":"Well, they\u0027re not really different types, are they?"},{"Start":"07:57.990 ","End":"08:02.320","Text":"The variable itself is of type employee."},{"Start":"08:02.320 ","End":"08:09.255","Text":"What we have got though is we\u0027ve got different objects being referenced by that variable."},{"Start":"08:09.255 ","End":"08:15.290","Text":"What we\u0027re seeing here is a polymorphic variable and it\u0027s showing really"},{"Start":"08:15.290 ","End":"08:22.625","Text":"that these variables can contain a subtype of employee as well as an employee."},{"Start":"08:22.625 ","End":"08:27.555","Text":"In fact, they can\u0027t contain an employee because we can\u0027t instantiate an employee."},{"Start":"08:27.555 ","End":"08:33.375","Text":"But any subtypes of employee can be assigned to any of these variables."},{"Start":"08:33.375 ","End":"08:39.410","Text":"All we\u0027re really seeing here is this idea of a polymorphic variable,"},{"Start":"08:39.410 ","End":"08:40.960","Text":"but we\u0027re going to get a little bit further now."},{"Start":"08:40.960 ","End":"08:46.695","Text":"In part J, you\u0027ll notice that this part-time employer we never set the hours for them."},{"Start":"08:46.695 ","End":"08:52.230","Text":"Now if we do try and set the hours as we\u0027ve been asked to do in part J,"},{"Start":"08:52.230 ","End":"08:56.110","Text":"we just say emp1.setHours."},{"Start":"08:56.110 ","End":"08:58.085","Text":"Should be perfectly legitimate."},{"Start":"08:58.085 ","End":"09:01.050","Text":"Set it to 10 hours."},{"Start":"09:01.050 ","End":"09:03.450","Text":"When we compile,"},{"Start":"09:03.450 ","End":"09:07.985","Text":"we get an error, and we\u0027re asked what error is generated."},{"Start":"09:07.985 ","End":"09:10.635","Text":"Well, that\u0027s easy enough. We just look at this."},{"Start":"09:10.635 ","End":"09:14.840","Text":"It says, cannot find symbol method setHours(int)."},{"Start":"09:14.840 ","End":"09:18.340","Text":"If I normally had seen this error you\u0027d think,"},{"Start":"09:18.340 ","End":"09:21.380","Text":"I just didn\u0027t define this or I\u0027ve misspelled,"},{"Start":"09:21.380 ","End":"09:23.415","Text":"I put a lowercase h or something,"},{"Start":"09:23.415 ","End":"09:27.510","Text":"or the wrong type of parameter has been passed in."},{"Start":"09:27.510 ","End":"09:32.260","Text":"But in this case, it\u0027s much more subtle than that."},{"Start":"09:32.260 ","End":"09:36.720","Text":"What\u0027s going on is the compiler needs to know that"},{"Start":"09:36.720 ","End":"09:43.540","Text":"all subtypes of employee actually have a setHours method."},{"Start":"09:43.540 ","End":"09:46.650","Text":"If they don\u0027t, it will complain, which is what it is doing."},{"Start":"09:46.650 ","End":"09:50.910","Text":"It\u0027s saying, well, I don\u0027t know what setHours is"},{"Start":"09:50.910 ","End":"09:55.635","Text":"because that doesn\u0027t exist inside the employee class."},{"Start":"09:55.635 ","End":"09:58.360","Text":"You told me to declare a load of variables to"},{"Start":"09:58.360 ","End":"10:05.195","Text":"instantiate employee objects into or subtypes."},{"Start":"10:05.195 ","End":"10:07.510","Text":"It\u0027s let me do that and it\u0027s been fine up until now."},{"Start":"10:07.510 ","End":"10:10.930","Text":"But what it cannot do is it cannot pick up"},{"Start":"10:10.930 ","End":"10:14.640","Text":"the setHours method because that only"},{"Start":"10:14.640 ","End":"10:18.894","Text":"exists inside a part-time employee class definition."},{"Start":"10:18.894 ","End":"10:22.320","Text":"It doesn\u0027t exist in this one and more importantly,"},{"Start":"10:22.320 ","End":"10:26.835","Text":"it doesn\u0027t exist in the employee class."},{"Start":"10:26.835 ","End":"10:30.270","Text":"It\u0027s basically saying, if you\u0027re going to use"},{"Start":"10:30.270 ","End":"10:35.435","Text":"this method I want you to have that in the base class."},{"Start":"10:35.435 ","End":"10:38.433","Text":"It can be overridden in subclasses,"},{"Start":"10:38.433 ","End":"10:41.320","Text":"but it has to have 1 or I\u0027m not going to allow"},{"Start":"10:41.320 ","End":"10:45.870","Text":"this style of program where you\u0027ve got a base class."},{"Start":"10:45.870 ","End":"10:48.365","Text":"With this clearing 3 variables to"},{"Start":"10:48.365 ","End":"10:52.325","Text":"store either a part-time employee or a full-time employee,"},{"Start":"10:52.325 ","End":"10:53.855","Text":"yes, I\u0027ll allow you to do that."},{"Start":"10:53.855 ","End":"10:57.220","Text":"But what you can\u0027t do is have a setHours method or"},{"Start":"10:57.220 ","End":"11:01.655","Text":"any method only belongs to 1 of those subclasses."},{"Start":"11:01.655 ","End":"11:06.165","Text":"If this method was in the base class, it\u0027s absolutely fine."},{"Start":"11:06.165 ","End":"11:09.450","Text":"The fix is relatively simple."},{"Start":"11:09.450 ","End":"11:15.570","Text":"Part L, it asks us to fix this by moving in an attribute and a method,"},{"Start":"11:15.570 ","End":"11:18.440","Text":"so it\u0027s pretty easy to do that."},{"Start":"11:18.440 ","End":"11:24.750","Text":"What we need to do is we need to go back to our part-time employee and we need to"},{"Start":"11:24.750 ","End":"11:32.380","Text":"remove the hours attribute from here because we\u0027re going to put it into the base class."},{"Start":"11:32.380 ","End":"11:38.090","Text":"Then we\u0027re going to also move setHours into the base class as well."},{"Start":"11:38.090 ","End":"11:40.495","Text":"Let\u0027s recompile that."},{"Start":"11:40.495 ","End":"11:42.810","Text":"This is complaining because it doesn\u0027t know where hours is,"},{"Start":"11:42.810 ","End":"11:44.490","Text":"and it will resolve that in a moment."},{"Start":"11:44.490 ","End":"11:46.715","Text":"We go back to employee now."},{"Start":"11:46.715 ","End":"11:49.810","Text":"We can implement a new method inside"},{"Start":"11:49.810 ","End":"11:54.760","Text":"employee that allows us to set the hours, but of course,"},{"Start":"11:54.760 ","End":"11:59.184","Text":"we need to also create a variable in here called"},{"Start":"11:59.184 ","End":"12:04.550","Text":"hours or there\u0027ll be no variable to store this data into."},{"Start":"12:04.550 ","End":"12:10.295","Text":"It has to be protected because the subclasses are going to want to have access to it."},{"Start":"12:10.295 ","End":"12:13.930","Text":"In particular, in the getMonthlyPay calculation,"},{"Start":"12:13.930 ","End":"12:16.945","Text":"they\u0027ll need to have access so they can calculate that."},{"Start":"12:16.945 ","End":"12:18.890","Text":"It turns out it\u0027s as simple as that."},{"Start":"12:18.890 ","End":"12:25.275","Text":"We move this setHours method into the base class employee."},{"Start":"12:25.275 ","End":"12:28.710","Text":"Therefore, it could be called from full-time employee,"},{"Start":"12:28.710 ","End":"12:30.495","Text":"but it wouldn\u0027t make a lot of sense,"},{"Start":"12:30.495 ","End":"12:33.450","Text":"but it can be also called from"},{"Start":"12:33.450 ","End":"12:36.245","Text":"the part-time employee and that does make a lot more sense."},{"Start":"12:36.245 ","End":"12:38.075","Text":"Let\u0027s recompile everything."},{"Start":"12:38.075 ","End":"12:40.845","Text":"Now our error has gone away."},{"Start":"12:40.845 ","End":"12:43.805","Text":"If I go back to app launch you will see that"},{"Start":"12:43.805 ","End":"12:47.400","Text":"this line was causing an error before and it\u0027s no"},{"Start":"12:47.400 ","End":"12:54.885","Text":"longer causing an error because an employee class does have a method called setHours,"},{"Start":"12:54.885 ","End":"13:01.080","Text":"therefore both of the subclasses also have that method."},{"Start":"13:01.080 ","End":"13:03.375","Text":"If we run this program now,"},{"Start":"13:03.375 ","End":"13:07.020","Text":"it should produce some slightly different output based on"},{"Start":"13:07.020 ","End":"13:10.730","Text":"the fact that our part-time worker is now not working 0 hours,"},{"Start":"13:10.730 ","End":"13:13.150","Text":"but is working 10 hours."},{"Start":"13:13.150 ","End":"13:16.085","Text":"Lo and behold, it has output what we expect."},{"Start":"13:16.085 ","End":"13:18.140","Text":"It\u0027s just repeating it twice because remember,"},{"Start":"13:18.140 ","End":"13:22.950","Text":"we\u0027ve got that other variable emp3 in there that\u0027s"},{"Start":"13:22.950 ","End":"13:28.440","Text":"just copying the reference to the other 2 objects."},{"Start":"13:28.440 ","End":"13:31.055","Text":"That seems to have worked,"},{"Start":"13:31.055 ","End":"13:35.025","Text":"but it\u0027s quite a lot going on there to unpick."},{"Start":"13:35.025 ","End":"13:39.125","Text":"What essentially is happening is that at runtime,"},{"Start":"13:39.125 ","End":"13:45.140","Text":"it needs to know which methods to run from which object."},{"Start":"13:45.140 ","End":"13:48.815","Text":"This is called dynamic binding."},{"Start":"13:48.815 ","End":"13:50.490","Text":"At the time it runs,"},{"Start":"13:50.490 ","End":"13:53.805","Text":"it works out which method needs to run."},{"Start":"13:53.805 ","End":"13:57.200","Text":"Because the employee variable,"},{"Start":"13:57.200 ","End":"13:58.800","Text":"all these variables here,"},{"Start":"13:58.800 ","End":"14:03.142","Text":"can contain either a part-time employee or a full-time employee,"},{"Start":"14:03.142 ","End":"14:07.935","Text":"it doesn\u0027t know which getMonthlyPay to run until it runs."},{"Start":"14:07.935 ","End":"14:09.825","Text":"At this point here,"},{"Start":"14:09.825 ","End":"14:13.849","Text":"and because we\u0027re doing a reference to a part-time employee,"},{"Start":"14:13.849 ","End":"14:18.015","Text":"it multiplies by hourly rate."},{"Start":"14:18.015 ","End":"14:22.960","Text":"This one here, same variable notice, the emp3,"},{"Start":"14:22.960 ","End":"14:26.930","Text":"this has got reference to a full-time employee and it"},{"Start":"14:26.930 ","End":"14:31.230","Text":"will actually just multiply 800 by the pay grade which is 1."},{"Start":"14:31.230 ","End":"14:35.570","Text":"That\u0027s why we see 800 appear at the output there."},{"Start":"14:35.570 ","End":"14:43.865","Text":"That is really the essence of dynamic binding being shown there and polymorphic methods."},{"Start":"14:43.865 ","End":"14:48.620","Text":"Dynamic methods being selected"},{"Start":"14:48.620 ","End":"14:53.705","Text":"correctly at runtime given the hierarchy that you\u0027re working within."},{"Start":"14:53.705 ","End":"14:57.570","Text":"That\u0027s it for this exercise. I\u0027ll see you for the next 1."}],"ID":31105},{"Watched":false,"Name":"Exercise 4 Part 1","Duration":"11m 58s","ChapterTopicVideoID":29488,"CourseChapterTopicPlaylistID":294472,"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.885","Text":"Hello, welcome to this exercise in which we\u0027re told a venue holds"},{"Start":"00:03.885 ","End":"00:08.010","Text":"events and needs a computer program to manage bookings."},{"Start":"00:08.010 ","End":"00:10.020","Text":"The venue can cope with a different number of"},{"Start":"00:10.020 ","End":"00:12.510","Text":"attendees depending on the type of event and"},{"Start":"00:12.510 ","End":"00:16.800","Text":"staffing levels for the event also depend on the type of event."},{"Start":"00:16.800 ","End":"00:20.610","Text":"We\u0027re then given an instruction to create a new project,"},{"Start":"00:20.610 ","End":"00:23.175","Text":"and within it classes as follows."},{"Start":"00:23.175 ","End":"00:26.625","Text":"We\u0027re given 4 classes and relationship."},{"Start":"00:26.625 ","End":"00:32.340","Text":"We\u0027re told to note the italics and capital letters on the diagram."},{"Start":"00:32.340 ","End":"00:38.100","Text":"In part B, we\u0027re told that the constructor for an event requires a date as a string,"},{"Start":"00:38.100 ","End":"00:39.600","Text":"the name of the Booker,"},{"Start":"00:39.600 ","End":"00:42.605","Text":"and the number of people who will attend the event."},{"Start":"00:42.605 ","End":"00:50.070","Text":"We start having already created a project and I\u0027ve created a new class event here."},{"Start":"00:50.070 ","End":"00:54.980","Text":"We would directed in part a to note the italics and capital letters."},{"Start":"00:54.980 ","End":"00:57.320","Text":"Start with the italics, Italics,"},{"Start":"00:57.320 ","End":"01:00.050","Text":"you\u0027ll notice event is italicized."},{"Start":"01:00.050 ","End":"01:04.250","Text":"What we\u0027re saying there is something in italics and UML signifies"},{"Start":"01:04.250 ","End":"01:10.120","Text":"that that field or that class or that method is abstract."},{"Start":"01:10.120 ","End":"01:14.915","Text":"We just simply need to put the word abstract in front"},{"Start":"01:14.915 ","End":"01:19.760","Text":"of the class definition to mark this as an abstract class."},{"Start":"01:19.760 ","End":"01:25.445","Text":"That means we cannot instantiate event objects or the objects"},{"Start":"01:25.445 ","End":"01:31.655","Text":"are derived from subtypes from event obviously but the subtypes party,"},{"Start":"01:31.655 ","End":"01:36.485","Text":"concert, and meeting other things that we can actually instantiate into objects."},{"Start":"01:36.485 ","End":"01:40.385","Text":"That is first little bit covered and we\u0027ll come back to"},{"Start":"01:40.385 ","End":"01:45.830","Text":"some abstract methods later down but let\u0027s get our attributes sorted."},{"Start":"01:45.830 ","End":"01:53.405","Text":"First of all, we\u0027ve got protected attributes of two strings called Date and Booker,"},{"Start":"01:53.405 ","End":"01:56.870","Text":"and 2 ints, 1 called attendees, 1 called staff."},{"Start":"01:56.870 ","End":"02:02.785","Text":"Let\u0027s do those protected string date."},{"Start":"02:02.785 ","End":"02:08.685","Text":"I might as well just copy that protected string Booker,"},{"Start":"02:08.685 ","End":"02:11.985","Text":"a person who books the event, presumably."},{"Start":"02:11.985 ","End":"02:16.335","Text":"Then we\u0027ve got an int of"},{"Start":"02:16.335 ","End":"02:22.665","Text":"attendees and same again for staff."},{"Start":"02:22.665 ","End":"02:24.800","Text":"There\u0027s our attributes then."},{"Start":"02:24.800 ","End":"02:27.920","Text":"Now we\u0027ve got a number set of attributes here,"},{"Start":"02:27.920 ","End":"02:30.500","Text":"and we were asked also to note the capital letters."},{"Start":"02:30.500 ","End":"02:34.295","Text":"Capital letters by convention signify in"},{"Start":"02:34.295 ","End":"02:40.415","Text":"an identifier for an attribute or variable, signifies a constant."},{"Start":"02:40.415 ","End":"02:42.380","Text":"A value that is not going to change,"},{"Start":"02:42.380 ","End":"02:45.630","Text":"and you\u0027ll notice also these have been made into"},{"Start":"02:45.840 ","End":"02:51.680","Text":"publicly accessible attributes because they don\u0027t change,"},{"Start":"02:51.680 ","End":"02:52.730","Text":"so it\u0027s not an issue."},{"Start":"02:52.730 ","End":"02:56.945","Text":"We\u0027re not going to have any problems by making them accessible outside the class."},{"Start":"02:56.945 ","End":"03:01.565","Text":"You often find that with constants that they are public accessible."},{"Start":"03:01.565 ","End":"03:03.830","Text":"Now how do we make a constant in Java?"},{"Start":"03:03.830 ","End":"03:09.675","Text":"We actually just say it\u0027s static and final."},{"Start":"03:09.675 ","End":"03:14.600","Text":"We can only create one of these things and it cannot be changed basically,"},{"Start":"03:14.600 ","End":"03:17.210","Text":"you\u0027ll notice these fields have been underlined as well."},{"Start":"03:17.210 ","End":"03:19.835","Text":"These attributes have been underlined."},{"Start":"03:19.835 ","End":"03:24.980","Text":"That\u0027s all we need to do and will go with the capitalization convention."},{"Start":"03:24.980 ","End":"03:28.140","Text":"We\u0027ve also got initial value specified here."},{"Start":"03:28.450 ","End":"03:35.220","Text":"PARTY_MAX ATTENDEES, that is going to be set to 300."},{"Start":"03:35.220 ","End":"03:40.445","Text":"Anytime we refer to this constant party max attendees,"},{"Start":"03:40.445 ","End":"03:44.985","Text":"we\u0027re going to retrieve a value of 300."},{"Start":"03:44.985 ","End":"03:47.985","Text":"That\u0027s that first one done."},{"Start":"03:47.985 ","End":"03:50.430","Text":"Let\u0027s do the others."},{"Start":"03:50.430 ","End":"03:52.335","Text":"I haven\u0027t put the datatype here,"},{"Start":"03:52.335 ","End":"03:54.585","Text":"so that\u0027s why it\u0027s complaining."},{"Start":"03:54.585 ","End":"03:56.940","Text":"The second one already been defined,"},{"Start":"03:56.940 ","End":"04:02.355","Text":"so CONCERT MAX gets a different value of 600,"},{"Start":"04:02.355 ","End":"04:07.860","Text":"and the final one of meeting Max is 80."},{"Start":"04:07.860 ","End":"04:12.070","Text":"Then we have PARTY STAFF RATIO."},{"Start":"04:12.170 ","End":"04:15.705","Text":"Once again, let\u0027s copy that."},{"Start":"04:15.705 ","End":"04:25.605","Text":"That is one star for every 15 attendees and for Concert it\u0027s 10."},{"Start":"04:25.605 ","End":"04:28.380","Text":"For meeting, we don\u0027t have the same thing but"},{"Start":"04:28.380 ","End":"04:33.260","Text":"a slightly different name here is obviously used in a slight different ways."},{"Start":"04:33.260 ","End":"04:35.150","Text":"We\u0027ll see later on."},{"Start":"04:35.150 ","End":"04:39.855","Text":"We have a meeting staff limit."},{"Start":"04:39.855 ","End":"04:47.520","Text":"There\u0027s some limit we use to decide on staffing and that is set to 50."},{"Start":"04:47.520 ","End":"04:51.440","Text":"Obviously the advantage of having these constants is if we decide at"},{"Start":"04:51.440 ","End":"04:56.075","Text":"some point that the maximum attendees for a constant need to change,"},{"Start":"04:56.075 ","End":"04:59.690","Text":"we can just change it this 1 line here than any other references"},{"Start":"04:59.690 ","End":"05:03.810","Text":"to Concert Max Attendees will be referring to that new value."},{"Start":"05:03.810 ","End":"05:05.405","Text":"That\u0027s why we use constants."},{"Start":"05:05.405 ","End":"05:07.340","Text":"That\u0027s the first part done."},{"Start":"05:07.340 ","End":"05:10.610","Text":"Then we\u0027ve got all of our attributes in place."},{"Start":"05:10.610 ","End":"05:13.010","Text":"We\u0027ve got some methods as well that we\u0027ve got to"},{"Start":"05:13.010 ","End":"05:16.120","Text":"cover there in italics as well apart from event."},{"Start":"05:16.120 ","End":"05:17.990","Text":"Let\u0027s deal with those."},{"Start":"05:17.990 ","End":"05:21.035","Text":"We\u0027ve got attendees check,"},{"Start":"05:21.035 ","End":"05:25.480","Text":"which returns a boolean and accepts no parameters."},{"Start":"05:25.480 ","End":"05:28.080","Text":"What do we need to do here?"},{"Start":"05:28.080 ","End":"05:33.830","Text":"This is not a method that we need to provide an implementation for."},{"Start":"05:33.830 ","End":"05:35.210","Text":"It\u0027s an abstract method,"},{"Start":"05:35.210 ","End":"05:38.110","Text":"as we said, italics means it\u0027s abstract."},{"Start":"05:38.110 ","End":"05:45.200","Text":"We need to mark it at the beginning with that notation to say that\u0027s what it is."},{"Start":"05:45.200 ","End":"05:48.290","Text":"The same applies to staff check."},{"Start":"05:48.290 ","End":"05:49.625","Text":"We\u0027ll just copy that."},{"Start":"05:49.625 ","End":"05:52.100","Text":"That just returns a boolean as well,"},{"Start":"05:52.100 ","End":"05:54.930","Text":"so we literally just got changed the name."},{"Start":"05:56.030 ","End":"06:02.415","Text":"We have another one which returns nothing,"},{"Start":"06:02.415 ","End":"06:05.255","Text":"so we put void in here,"},{"Start":"06:05.255 ","End":"06:08.920","Text":"and that is called assigned staff."},{"Start":"06:12.200 ","End":"06:16.900","Text":"Then we have one which returns a string."},{"Start":"06:19.110 ","End":"06:21.700","Text":"Remember it\u0027s capitalists for strings,"},{"Start":"06:21.700 ","End":"06:26.865","Text":"it\u0027s a class that is returned or an object."},{"Start":"06:26.865 ","End":"06:29.520","Text":"That\u0027s called summary."},{"Start":"06:29.520 ","End":"06:33.090","Text":"We\u0027ve covered off all of the 4 that were in"},{"Start":"06:33.090 ","End":"06:37.500","Text":"italics and they were basically abstract methods."},{"Start":"06:37.500 ","End":"06:42.765","Text":"They\u0027re going to be implemented in the subclasses."},{"Start":"06:42.765 ","End":"06:48.400","Text":"We don\u0027t have to worry about that for the moment but let\u0027s just finish"},{"Start":"06:48.400 ","End":"06:54.450","Text":"off by creating the other classes and linking them to this event,"},{"Start":"06:54.450 ","End":"06:57.015","Text":"and we\u0027ll come back and do the constructor."},{"Start":"06:57.015 ","End":"07:03.455","Text":"Party doesn\u0027t need anything else apart from at the moment,"},{"Start":"07:03.455 ","End":"07:07.775","Text":"it needs to say that it\u0027s related to event."},{"Start":"07:07.775 ","End":"07:10.160","Text":"We\u0027ve got extend event."},{"Start":"07:10.160 ","End":"07:12.215","Text":"It will need a constructor,"},{"Start":"07:12.215 ","End":"07:16.580","Text":"so we\u0027ll have to do the constructor and pass it on"},{"Start":"07:16.580 ","End":"07:23.310","Text":"to the relevant values onto the constructor for the base class."},{"Start":"07:23.310 ","End":"07:28.485","Text":"As we know, format for a constructor is so"},{"Start":"07:28.485 ","End":"07:36.950","Text":"public party than the attributes and the constructor for an event requires a date."},{"Start":"07:36.950 ","End":"07:43.705","Text":"That\u0027s a string. It requires the name of a Booker,"},{"Start":"07:43.705 ","End":"07:44.980","Text":"which is a string as well,"},{"Start":"07:44.980 ","End":"07:51.375","Text":"and integer saying how many people are going to attend the event."},{"Start":"07:51.375 ","End":"07:54.760","Text":"Those things, we\u0027re simply going to pass on to"},{"Start":"07:54.760 ","End":"08:00.550","Text":"the constructor for the base class. We have to do here."},{"Start":"08:00.550 ","End":"08:04.855","Text":"That is for the moment class done"},{"Start":"08:04.855 ","End":"08:09.570","Text":"but it is complaining about it\u0027s not an abstract class itself."},{"Start":"08:09.570 ","End":"08:11.864","Text":"Why have to implement methods?"},{"Start":"08:11.864 ","End":"08:16.090","Text":"I was supposed to implement a method for summary and I haven\u0027t done it,"},{"Start":"08:16.090 ","End":"08:17.575","Text":"which is why it\u0027s complaining,"},{"Start":"08:17.575 ","End":"08:19.120","Text":"and it\u0027s going to do that for all of"},{"Start":"08:19.120 ","End":"08:23.540","Text":"the methods that I haven\u0027t implemented yet until I implement."},{"Start":"08:23.540 ","End":"08:26.975","Text":"Let me just go back here and finish off part B,"},{"Start":"08:26.975 ","End":"08:31.745","Text":"which is to do the constructor that is going to be made use of."},{"Start":"08:31.745 ","End":"08:38.090","Text":"Constructors normally declared before any abstract methods, before any methods."},{"Start":"08:38.090 ","End":"08:42.005","Text":"In fact, the first method declared traditionally in a class."},{"Start":"08:42.005 ","End":"08:46.970","Text":"Let\u0027s create our constructor public event,"},{"Start":"08:46.970 ","End":"08:52.865","Text":"same as the previous thing that we wrote in party."},{"Start":"08:52.865 ","End":"08:58.235","Text":"String, which is the date, string,"},{"Start":"08:58.235 ","End":"09:00.560","Text":"which is the booker,"},{"Start":"09:00.560 ","End":"09:06.840","Text":"and int which is the attendees."},{"Start":"09:09.700 ","End":"09:13.100","Text":"There we go."},{"Start":"09:13.100 ","End":"09:23.010","Text":"All we need to do is to set the instance variables to the relevant parameter."},{"Start":"09:24.350 ","End":"09:27.360","Text":"This completed then."},{"Start":"09:27.360 ","End":"09:29.570","Text":"We\u0027ve got a constructor."},{"Start":"09:29.570 ","End":"09:32.449","Text":"We\u0027ve got the abstract methods that are in italics."},{"Start":"09:32.449 ","End":"09:36.270","Text":"We\u0027ve got the constants defined as attributes,"},{"Start":"09:36.270 ","End":"09:39.283","Text":"and then initial values have been given to those constants."},{"Start":"09:39.283 ","End":"09:43.925","Text":"They\u0027ll never be able to be changed because we put final, hence they\u0027re constants."},{"Start":"09:43.925 ","End":"09:46.460","Text":"Then we\u0027ve got four attributes."},{"Start":"09:46.460 ","End":"09:49.820","Text":"That pool of our objects are going to contain."},{"Start":"09:49.820 ","End":"09:53.825","Text":"Some subclasses, objects made from them."},{"Start":"09:53.825 ","End":"09:55.940","Text":"That is complete,"},{"Start":"09:55.940 ","End":"09:59.195","Text":"so we\u0027re gonna do the same thing to create"},{"Start":"09:59.195 ","End":"10:04.880","Text":"our concert class and our meeting class that there\u0027s a slight change for concert."},{"Start":"10:04.880 ","End":"10:07.114","Text":"You\u0027ll notice from a UML diagram,"},{"Start":"10:07.114 ","End":"10:09.905","Text":"it\u0027s empty for party a meeting."},{"Start":"10:09.905 ","End":"10:15.465","Text":"Concert has an extra field which is private."},{"Start":"10:15.465 ","End":"10:18.020","Text":"We\u0027re going to have to put that in."},{"Start":"10:18.020 ","End":"10:20.000","Text":"It\u0027s a string,"},{"Start":"10:20.000 ","End":"10:26.200","Text":"and it\u0027s the name of the artist who is having a constant."},{"Start":"10:26.200 ","End":"10:28.760","Text":"The person that books it would be their agent,"},{"Start":"10:28.760 ","End":"10:32.020","Text":"and the artists that the person actually doing the concert."},{"Start":"10:32.020 ","End":"10:35.555","Text":"Alongside that, we will need a constructor,"},{"Start":"10:35.555 ","End":"10:37.595","Text":"just the same as we previously had."},{"Start":"10:37.595 ","End":"10:43.860","Text":"In fact, I can just copy it from party and change the name."},{"Start":"10:46.060 ","End":"10:48.800","Text":"It\u0027s going to do exactly the same job,"},{"Start":"10:48.800 ","End":"10:50.630","Text":"so that\u0027s that done."},{"Start":"10:50.630 ","End":"10:58.700","Text":"Because I haven\u0027t written extends event saying you don\u0027t need any parameters."},{"Start":"10:58.700 ","End":"11:01.145","Text":"You do if it\u0027s of type event,"},{"Start":"11:01.145 ","End":"11:06.575","Text":"so that error will go away but the error that still remains is the one I said previously."},{"Start":"11:06.575 ","End":"11:11.530","Text":"I haven\u0027t implemented any of those methods that I\u0027ve marked as abstract."},{"Start":"11:11.530 ","End":"11:18.635","Text":"It\u0027s going to complain about that but that\u0027s the concert shell definition done."},{"Start":"11:18.635 ","End":"11:23.180","Text":"The final one is to do the meeting"},{"Start":"11:23.180 ","End":"11:29.330","Text":"and paste the constructor again from party."},{"Start":"11:29.330 ","End":"11:31.250","Text":"It\u0027s going to be the same thing."},{"Start":"11:31.250 ","End":"11:39.725","Text":"Simply going to pass on the relevant values to the constructor for the base class."},{"Start":"11:39.725 ","End":"11:46.460","Text":"Again, I need to put extends event for that to work."},{"Start":"11:46.460 ","End":"11:51.485","Text":"We\u0027ll pause there and we\u0027ve done part A and part B of this exercise."},{"Start":"11:51.485 ","End":"11:57.470","Text":"We\u0027ll go ahead and implementing the behaviors in the subclasses in the next video."},{"Start":"11:57.470 ","End":"11:59.640","Text":"I\u0027ll see you shortly for that one."}],"ID":31106},{"Watched":false,"Name":"Exercise 4 Part 2","Duration":"10m 25s","ChapterTopicVideoID":29487,"CourseChapterTopicPlaylistID":294472,"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.475","Text":"Hello, welcome back. Continuing on with the exercise in Part c,"},{"Start":"00:04.475 ","End":"00:06.310","Text":"we\u0027re told to implement the behaviors in"},{"Start":"00:06.310 ","End":"00:09.785","Text":"the subclasses according to the following criteria."},{"Start":"00:09.785 ","End":"00:14.260","Text":"In Part 1, we\u0027re told the attendees check returns true if the number of"},{"Start":"00:14.260 ","End":"00:16.060","Text":"attendees is less than or equal to"},{"Start":"00:16.060 ","End":"00:19.765","Text":"the maximum number of attendees for that type of event."},{"Start":"00:19.765 ","End":"00:21.565","Text":"For example, for a party,"},{"Start":"00:21.565 ","End":"00:23.995","Text":"250 would return true,"},{"Start":"00:23.995 ","End":"00:26.890","Text":"but 301 would return false."},{"Start":"00:26.890 ","End":"00:29.635","Text":"In Part 2, we\u0027re told that staff check returns"},{"Start":"00:29.635 ","End":"00:33.070","Text":"true if the number of staff currently assigned is greater"},{"Start":"00:33.070 ","End":"00:35.935","Text":"than or equal to the number of attendees"},{"Start":"00:35.935 ","End":"00:39.475","Text":"divided by the staffing ratio for the relevant event."},{"Start":"00:39.475 ","End":"00:42.610","Text":"For example, for a party with a 150 people,"},{"Start":"00:42.610 ","End":"00:46.545","Text":"a value of 10 assigned to staff would return true."},{"Start":"00:46.545 ","End":"00:52.159","Text":"Note, meetings only ever need 1 staff member unless there are more than 50 attendees,"},{"Start":"00:52.159 ","End":"00:54.950","Text":"in which case there should be 2 staff members."},{"Start":"00:54.950 ","End":"01:00.220","Text":"This threshold value of 50 is held in a meeting staff limit."},{"Start":"01:00.220 ","End":"01:03.620","Text":"In Part 3, we\u0027re told that assigned staff sets"},{"Start":"01:03.620 ","End":"01:08.390","Text":"the staff attribute to the correct amount for the type of event being held."},{"Start":"01:08.390 ","End":"01:12.140","Text":"For example, for a concert with 600 people,"},{"Start":"01:12.140 ","End":"01:15.170","Text":"staff would be set to 60."},{"Start":"01:15.170 ","End":"01:17.420","Text":"Then in Part 4, finally,"},{"Start":"01:17.420 ","End":"01:21.260","Text":"we\u0027re told that set artist is a modifier which takes"},{"Start":"01:21.260 ","End":"01:27.360","Text":"a string argument and uses it to set the value of the artist attribute."},{"Start":"01:27.590 ","End":"01:36.200","Text":"Previously, we created the base class and the structure for all of the subclasses."},{"Start":"01:36.200 ","End":"01:39.710","Text":"Now, we\u0027re going to go ahead and implement"},{"Start":"01:39.710 ","End":"01:43.430","Text":"some of the behaviors, everything but summary."},{"Start":"01:43.430 ","End":"01:45.530","Text":"In Part C, we need to do,"},{"Start":"01:45.530 ","End":"01:48.815","Text":"first of all, C1 the attendee\u0027s check."},{"Start":"01:48.815 ","End":"01:53.600","Text":"Now, we can use the override annotation here because we are"},{"Start":"01:53.600 ","End":"01:58.775","Text":"actually overriding something that was declared as abstract in the base class."},{"Start":"01:58.775 ","End":"02:03.260","Text":"If I was to now put the definition in for attendees,"},{"Start":"02:03.260 ","End":"02:06.180","Text":"check if they\u0027re quite happy with that."},{"Start":"02:06.180 ","End":"02:10.009","Text":"Attendee\u0027s check returns a Boolean, takes normal arguments."},{"Start":"02:10.009 ","End":"02:16.100","Text":"What it does is return true or false depending on whether"},{"Start":"02:16.100 ","End":"02:22.760","Text":"the attendees are greater than the maximum number that can attend this type of event."},{"Start":"02:22.760 ","End":"02:26.585","Text":"For a party, we want to check at"},{"Start":"02:26.585 ","End":"02:34.235","Text":"attendees is less than or equal to the limit,"},{"Start":"02:34.235 ","End":"02:36.140","Text":"and if we look at the event class,"},{"Start":"02:36.140 ","End":"02:38.570","Text":"we can see what the maximum is."},{"Start":"02:38.570 ","End":"02:42.410","Text":"We don\u0027t want the value 300 that\u0027d be stored into party max attendees."},{"Start":"02:42.410 ","End":"02:44.900","Text":"We use the name of the constant,"},{"Start":"02:44.900 ","End":"02:46.680","Text":"and we compare it to that."},{"Start":"02:46.680 ","End":"02:50.450","Text":"It\u0027s less than or equal to party max attendees,"},{"Start":"02:50.450 ","End":"02:53.485","Text":"we\u0027re good, and we can return true."},{"Start":"02:53.485 ","End":"02:56.265","Text":"Otherwise, we return false."},{"Start":"02:56.265 ","End":"02:58.895","Text":"I don\u0027t need an else here."},{"Start":"02:58.895 ","End":"03:01.430","Text":"As we\u0027ve done before,"},{"Start":"03:01.430 ","End":"03:08.090","Text":"return is going to exit if the attendees is less than or equal to party max attendees."},{"Start":"03:08.090 ","End":"03:09.380","Text":"It will just end there."},{"Start":"03:09.380 ","End":"03:11.210","Text":"If it ever gets to this line,"},{"Start":"03:11.210 ","End":"03:13.355","Text":"that\u0027s because this was not true."},{"Start":"03:13.355 ","End":"03:15.365","Text":"I should return false."},{"Start":"03:15.365 ","End":"03:17.930","Text":"That\u0027s attendee\u0027s check done."},{"Start":"03:17.930 ","End":"03:22.760","Text":"In Part 2, we do something similar where we do a staff check."},{"Start":"03:22.760 ","End":"03:26.630","Text":"Again, we\u0027re overriding something in the base class,"},{"Start":"03:26.630 ","End":"03:29.615","Text":"so we can put that annotation in."},{"Start":"03:29.615 ","End":"03:33.940","Text":"Staff check also returns a Boolean."},{"Start":"03:34.010 ","End":"03:38.495","Text":"The job of staff check is to"},{"Start":"03:38.495 ","End":"03:42.980","Text":"see that we\u0027ve got the relevant number of staff at the event."},{"Start":"03:42.980 ","End":"03:46.385","Text":"What is the relevant number of staff?"},{"Start":"03:46.385 ","End":"03:55.250","Text":"Well, if the number of staff we\u0027ve currently got is greater than or equal"},{"Start":"03:55.250 ","End":"03:59.930","Text":"to the number of attendees divided"},{"Start":"03:59.930 ","End":"04:05.485","Text":"by the party to staff ratio constant,"},{"Start":"04:05.485 ","End":"04:08.643","Text":"and the same as before,"},{"Start":"04:08.643 ","End":"04:10.700","Text":"otherwise, we return false."},{"Start":"04:10.700 ","End":"04:12.040","Text":"It would never get to this line,"},{"Start":"04:12.040 ","End":"04:15.455","Text":"if that previous line had returned true,"},{"Start":"04:15.455 ","End":"04:18.605","Text":"execute this bit. That\u0027s fine."},{"Start":"04:18.605 ","End":"04:21.290","Text":"If we\u0027ve got the right number of staff,"},{"Start":"04:21.290 ","End":"04:22.970","Text":"then we return true."},{"Start":"04:22.970 ","End":"04:25.295","Text":"Otherwise, we return false."},{"Start":"04:25.295 ","End":"04:30.560","Text":"Then assigned staff is like the complimentary method to this."},{"Start":"04:30.560 ","End":"04:35.910","Text":"This is where we can assign a number of staff to the event."},{"Start":"04:35.910 ","End":"04:40.580","Text":"We may as well calculate that because we know what staff ratio we do need,"},{"Start":"04:40.580 ","End":"04:43.255","Text":"and it\u0027s different for each subtype."},{"Start":"04:43.255 ","End":"04:47.150","Text":"For a party, it returns nothing."},{"Start":"04:47.150 ","End":"04:50.415","Text":"It\u0027s called assigned staff."},{"Start":"04:50.415 ","End":"04:55.730","Text":"For our party, the amount of staff we want to assign is going to"},{"Start":"04:55.730 ","End":"05:02.855","Text":"be attendees divided by party staff ratio."},{"Start":"05:02.855 ","End":"05:05.780","Text":"Now, these methods are pretty unsophisticated,"},{"Start":"05:05.780 ","End":"05:08.900","Text":"and they\u0027re not really doing much as dividing by different constants."},{"Start":"05:08.900 ","End":"05:14.740","Text":"But this is really just to illustrate the examples in a very simple way,"},{"Start":"05:14.740 ","End":"05:19.160","Text":"so we keep the code simple so that these concepts of"},{"Start":"05:19.160 ","End":"05:24.470","Text":"polymorphism subclasses are the ones that you\u0027re concentrating on,"},{"Start":"05:24.470 ","End":"05:27.670","Text":"not filling in lots of complex code."},{"Start":"05:27.670 ","End":"05:31.290","Text":"That\u0027s assigned staff done."},{"Start":"05:31.290 ","End":"05:35.780","Text":"The final one doesn\u0027t apply to this class."},{"Start":"05:35.780 ","End":"05:39.650","Text":"In Part 4, that only applies to the concept class,"},{"Start":"05:39.650 ","End":"05:40.840","Text":"and we\u0027ll come back to that."},{"Start":"05:40.840 ","End":"05:47.060","Text":"Let\u0027s go ahead and fill in the same methods for the concert class."},{"Start":"05:47.060 ","End":"05:50.240","Text":"What I\u0027m going to do is, I\u0027m just going to copy what I\u0027ve got here."},{"Start":"05:50.240 ","End":"05:51.845","Text":"Because as I\u0027ve just said,"},{"Start":"05:51.845 ","End":"05:53.480","Text":"we\u0027ve kept it quite simple,"},{"Start":"05:53.480 ","End":"05:57.125","Text":"this exercise, and they\u0027re all doing very similar things."},{"Start":"05:57.125 ","End":"06:00.380","Text":"But we\u0027re using different constants here."},{"Start":"06:00.380 ","End":"06:05.360","Text":"Concert, we replace that constant there with word"},{"Start":"06:05.360 ","End":"06:12.310","Text":"constant and same here concept and the same concept."},{"Start":"06:12.310 ","End":"06:18.290","Text":"But there was this one other method in a concert which was set artists,"},{"Start":"06:18.290 ","End":"06:20.270","Text":"which we do in part C4,"},{"Start":"06:20.270 ","End":"06:22.235","Text":"and that\u0027s simple enough."},{"Start":"06:22.235 ","End":"06:31.025","Text":"We just say, give me a string and I will set that value into the instance variable,"},{"Start":"06:31.025 ","End":"06:35.985","Text":"artist, and that\u0027s part border."},{"Start":"06:35.985 ","End":"06:41.630","Text":"Let\u0027s just finish off by doing the meeting."},{"Start":"06:41.630 ","End":"06:44.990","Text":"The meeting has a slightly different behavior"},{"Start":"06:44.990 ","End":"06:50.615","Text":"because I can again copy everything from party,"},{"Start":"06:50.615 ","End":"06:52.805","Text":"and change the constants."},{"Start":"06:52.805 ","End":"06:57.020","Text":"Here, constant for meeting applies,"},{"Start":"06:57.020 ","End":"07:01.205","Text":"but the staff checks are going to be quite different."},{"Start":"07:01.205 ","End":"07:07.550","Text":"I\u0027m going to follow those out and replace them with a completely different code."},{"Start":"07:07.550 ","End":"07:15.900","Text":"Because you\u0027ll notice that the question asks for something fairly different."},{"Start":"07:15.900 ","End":"07:20.930","Text":"For staff check, the check is something like this."},{"Start":"07:20.930 ","End":"07:26.435","Text":"There\u0027s actually 2 parts of this."},{"Start":"07:26.435 ","End":"07:35.010","Text":"If the number of attendees greater than the meeting staff limit."},{"Start":"07:35.010 ","End":"07:38.525","Text":"There\u0027s a difference, and we don\u0027t talk about staff ratios here. There\u0027s a limit."},{"Start":"07:38.525 ","End":"07:41.810","Text":"If there were more than 50, I think it was people at the meeting,"},{"Start":"07:41.810 ","End":"07:43.745","Text":"we need 2 staff."},{"Start":"07:43.745 ","End":"07:46.340","Text":"If that is true, if there\u0027s more,"},{"Start":"07:46.340 ","End":"07:55.715","Text":"then the limit number of people and staff has been set to 2, then we\u0027re good."},{"Start":"07:55.715 ","End":"07:57.710","Text":"We can return true."},{"Start":"07:57.710 ","End":"08:00.200","Text":"Now this is going to be a little bit more complex."},{"Start":"08:00.200 ","End":"08:06.690","Text":"I do need my curly brackets here because I\u0027m going to have an else-if here."},{"Start":"08:07.390 ","End":"08:12.365","Text":"If attendees is greater than the meeting staff limit and the staff is 2,"},{"Start":"08:12.365 ","End":"08:13.805","Text":"then I return true."},{"Start":"08:13.805 ","End":"08:17.190","Text":"If it\u0027s not, 2 of those things have to be true,"},{"Start":"08:17.190 ","End":"08:19.777","Text":"so that could be one that\u0027s truer than the other."},{"Start":"08:19.777 ","End":"08:21.438","Text":"I need a further else,"},{"Start":"08:21.438 ","End":"08:22.751","Text":"someone do an else."},{"Start":"08:22.751 ","End":"08:29.150","Text":"If attendees is less than or"},{"Start":"08:29.150 ","End":"08:36.830","Text":"equal to meeting staff limit and the staff is 1,"},{"Start":"08:36.830 ","End":"08:39.500","Text":"then that\u0027s also good,"},{"Start":"08:39.500 ","End":"08:40.910","Text":"and I can return true."},{"Start":"08:40.910 ","End":"08:45.800","Text":"The only other possible condition would be,"},{"Start":"08:45.800 ","End":"08:49.100","Text":"either the staff has set to not 1 or not 2,"},{"Start":"08:49.100 ","End":"08:53.120","Text":"and it doesn\u0027t matter what the limit would be, that would be a problem."},{"Start":"08:53.120 ","End":"08:57.435","Text":"In that case, we return false."},{"Start":"08:57.435 ","End":"09:01.970","Text":"I could put just to keep the syntax clear,"},{"Start":"09:01.970 ","End":"09:03.525","Text":"I can put that as a final,"},{"Start":"09:03.525 ","End":"09:09.920","Text":"else C and see how those 3 flow and relate to each other."},{"Start":"09:10.230 ","End":"09:16.385","Text":"If the attendees is greater than the limit and the staff is 2,"},{"Start":"09:16.385 ","End":"09:17.735","Text":"then we return true."},{"Start":"09:17.735 ","End":"09:21.170","Text":"If the attendees is less than or equal to the limit and staff is 1,"},{"Start":"09:21.170 ","End":"09:22.420","Text":"then we also return true."},{"Start":"09:22.420 ","End":"09:24.995","Text":"These are the 2 checks that pass."},{"Start":"09:24.995 ","End":"09:27.820","Text":"Otherwise, we fail the check."},{"Start":"09:27.820 ","End":"09:30.725","Text":"That\u0027s for checking the staff."},{"Start":"09:30.725 ","End":"09:32.900","Text":"What about automatically assigning the staff?"},{"Start":"09:32.900 ","End":"09:36.385","Text":"Well, it\u0027s going to be very similar logic."},{"Start":"09:36.385 ","End":"09:43.900","Text":"If attendees is greater than meeting staff limit,"},{"Start":"09:43.900 ","End":"09:45.725","Text":"then we set staff to 2."},{"Start":"09:45.725 ","End":"09:49.955","Text":"Otherwise, we set staff to 1."},{"Start":"09:49.955 ","End":"09:53.430","Text":"That is done?"},{"Start":"09:53.430 ","End":"09:55.070","Text":"We got an error here."},{"Start":"09:55.070 ","End":"09:56.690","Text":"Sorry, it\u0027s not double equals, is it?"},{"Start":"09:56.690 ","End":"09:58.085","Text":"I\u0027m assigning a value,"},{"Start":"09:58.085 ","End":"10:01.534","Text":"not comparing values. Just have that."},{"Start":"10:01.534 ","End":"10:06.030","Text":"So that is that done."},{"Start":"10:06.030 ","End":"10:11.845","Text":"We\u0027ve completed everything in Part C for meeting,"},{"Start":"10:11.845 ","End":"10:15.855","Text":"concert, and party."},{"Start":"10:15.855 ","End":"10:19.220","Text":"In part D, we\u0027ll just finish off the final behavior,"},{"Start":"10:19.220 ","End":"10:23.045","Text":"which is summary, and we\u0027re good to start testing after that."},{"Start":"10:23.045 ","End":"10:25.650","Text":"Thanks for watching. See you in the next one."}],"ID":31107},{"Watched":false,"Name":"Exercise 4 Part 3","Duration":"9m 49s","ChapterTopicVideoID":29486,"CourseChapterTopicPlaylistID":294472,"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. Welcome back for this final part of the exercise."},{"Start":"00:03.510 ","End":"00:07.410","Text":"In part d, we\u0027re told that the behavior for summary is as follows."},{"Start":"00:07.410 ","End":"00:08.745","Text":"In part I, we\u0027re told,"},{"Start":"00:08.745 ","End":"00:10.080","Text":"for a party,"},{"Start":"00:10.080 ","End":"00:16.470","Text":"summary returns a string with a booker\u0027s name followed by \u0027\u0027\u0027s party.\u0027\u0027,"},{"Start":"00:16.470 ","End":"00:18.780","Text":"concatenated with the date,"},{"Start":"00:18.780 ","End":"00:21.000","Text":"concatenated with the number of attendees."},{"Start":"00:21.000 ","End":"00:28.140","Text":"For example, \"Abigail\u0027s party 10/2/2022.150 invited.\""},{"Start":"00:28.140 ","End":"00:30.360","Text":"In part II we\u0027re told, that for a concert,"},{"Start":"00:30.360 ","End":"00:36.645","Text":"summary returns \"Concert: \" concatenated with the artist,"},{"Start":"00:36.645 ","End":"00:38.565","Text":"concatenated with the date,"},{"Start":"00:38.565 ","End":"00:41.085","Text":"concatenated with the number of attendees."},{"Start":"00:41.085 ","End":"00:46.290","Text":"For example, \"Concert: Codepunks 11/2/2022."},{"Start":"00:46.290 ","End":"00:48.720","Text":"500 capacity crowd."},{"Start":"00:48.720 ","End":"00:51.215","Text":"In part III we\u0027re told, for a meeting,"},{"Start":"00:51.215 ","End":"00:57.140","Text":"summary returns booker concatenated with \" Meeting \" concatenated with a date."},{"Start":"00:57.140 ","End":"01:04.085","Text":"For example, \"Planning Committee Meeting 12/2/2022.\""},{"Start":"01:04.085 ","End":"01:11.180","Text":"In part e, we then test by creating objects on the BlueJ object workbench as follows."},{"Start":"01:11.180 ","End":"01:15.355","Text":"In part I, a party with arguments \"10/2/2022\","},{"Start":"01:15.355 ","End":"01:17.620","Text":"\"Abigail\", and 150."},{"Start":"01:17.620 ","End":"01:23.610","Text":"In part II, a concert with arguments \"11/2/2022\","},{"Start":"01:23.610 ","End":"01:26.265","Text":"\"A. Gent\", and 500."},{"Start":"01:26.265 ","End":"01:33.025","Text":"We use set artist to set the artist attribute to \"Codepunks\" on that object."},{"Start":"01:33.025 ","End":"01:37.275","Text":"In part III, a meeting with arguments \"12/2/2022\","},{"Start":"01:37.275 ","End":"01:41.635","Text":"and \"Planning Committee\", and 81."},{"Start":"01:41.635 ","End":"01:44.510","Text":"We\u0027re told to check that the output from"},{"Start":"01:44.510 ","End":"01:49.410","Text":"summary for each of those objects matches parts di-diii."},{"Start":"01:50.150 ","End":"01:54.920","Text":"Then we run assignStaff for each object and inspect"},{"Start":"01:54.920 ","End":"01:59.165","Text":"them to check if the correct number of staff are allocated,"},{"Start":"01:59.165 ","End":"02:00.395","Text":"which would be 10,"},{"Start":"02:00.395 ","End":"02:04.030","Text":"50, and 2 respectively."},{"Start":"02:04.030 ","End":"02:08.990","Text":"To complete this class and every other class,"},{"Start":"02:08.990 ","End":"02:11.960","Text":"we just need to implement the summary method."},{"Start":"02:11.960 ","End":"02:18.515","Text":"For party, we return just the booker\u0027s name followed by that person\u0027s party,"},{"Start":"02:18.515 ","End":"02:20.900","Text":"concatenate with a date, and so on."},{"Start":"02:20.900 ","End":"02:24.110","Text":"It\u0027s going to look something like this."},{"Start":"02:24.110 ","End":"02:26.195","Text":"Again, it\u0027s an override."},{"Start":"02:26.195 ","End":"02:29.375","Text":"We can add that annotation first."},{"Start":"02:29.375 ","End":"02:34.215","Text":"Then it is called public, returns,"},{"Start":"02:34.215 ","End":"02:41.025","Text":"String, and it\u0027s called summary, no parameters."},{"Start":"02:41.025 ","End":"02:44.595","Text":"All we\u0027re going to do is return one big long string."},{"Start":"02:44.595 ","End":"02:50.210","Text":"We say the name of the booker and then \u0027\u0027\u0027s party,"},{"Start":"02:50.210 ","End":"02:53.210","Text":"we\u0027re assuming the person is booking their own party."},{"Start":"02:53.210 ","End":"02:58.615","Text":"Then we have a date from the date attribute."},{"Start":"02:58.615 ","End":"03:02.295","Text":"Then we say how many people were invited."},{"Start":"03:02.295 ","End":"03:04.460","Text":"This is just showing we can have"},{"Start":"03:04.460 ","End":"03:07.760","Text":"slightly different behaviors for all of the different types of events."},{"Start":"03:07.760 ","End":"03:11.345","Text":"Now, this uses the word invited rather than attendees."},{"Start":"03:11.345 ","End":"03:15.350","Text":"Concept has the name of the artist in there and things like that."},{"Start":"03:15.350 ","End":"03:23.990","Text":"We\u0027re just illustrating different behaviors on the same method in different sub-types."},{"Start":"03:23.990 ","End":"03:27.810","Text":"That\u0027s the one for party."},{"Start":"03:27.810 ","End":"03:29.880","Text":"Now we should get no more errors."},{"Start":"03:29.880 ","End":"03:33.440","Text":"We don\u0027t because it\u0027s now implemented every one of"},{"Start":"03:33.440 ","End":"03:37.740","Text":"the abstract methods that were in the base class."},{"Start":"03:37.740 ","End":"03:44.385","Text":"We can do the same for meeting and implement that method."},{"Start":"03:44.385 ","End":"03:48.120","Text":"First one I just copy and paste that,"},{"Start":"03:48.120 ","End":"03:50.865","Text":"and then just change what\u0027s in it."},{"Start":"03:50.865 ","End":"03:53.775","Text":"For the meeting,"},{"Start":"03:53.775 ","End":"03:56.825","Text":"it\u0027s slightly different format."},{"Start":"03:56.825 ","End":"04:02.160","Text":"We say booker and then just the word Meeting,"},{"Start":"04:02.160 ","End":"04:04.665","Text":"with capitals, M,"},{"Start":"04:04.665 ","End":"04:06.455","Text":"the date, and the full stop,"},{"Start":"04:06.455 ","End":"04:08.225","Text":"and we don\u0027t need anything else."},{"Start":"04:08.225 ","End":"04:10.740","Text":"Very brief for a meeting."},{"Start":"04:10.740 ","End":"04:14.250","Text":"Don\u0027t need to know how many attendees there are."},{"Start":"04:14.250 ","End":"04:16.890","Text":"That\u0027s that implementation done."},{"Start":"04:16.890 ","End":"04:19.635","Text":"The only one we\u0027ve got to do is concert."},{"Start":"04:19.635 ","End":"04:25.250","Text":"Concert is slight exception in comparison to the others."},{"Start":"04:25.250 ","End":"04:29.915","Text":"Here, we\u0027re not really interested in the booker,"},{"Start":"04:29.915 ","End":"04:34.040","Text":"we are interested in the artists though."},{"Start":"04:34.040 ","End":"04:36.290","Text":"This one is slightly different."},{"Start":"04:36.290 ","End":"04:39.035","Text":"We say concert,"},{"Start":"04:39.035 ","End":"04:42.080","Text":"to distinguish this from other types of event,"},{"Start":"04:42.080 ","End":"04:45.485","Text":"and then we say who the artist is,"},{"Start":"04:45.485 ","End":"04:48.380","Text":"and then we have a space,"},{"Start":"04:48.380 ","End":"04:49.920","Text":"and then the date."},{"Start":"04:49.920 ","End":"04:56.860","Text":"Then we say put full stop and a space just to move it on from the next field."},{"Start":"04:56.860 ","End":"05:01.170","Text":"Then we say how many attendees we have, concert goers,"},{"Start":"05:01.170 ","End":"05:04.820","Text":"and we say a capacity crowd"},{"Start":"05:04.820 ","End":"05:07.925","Text":"because obviously we don\u0027t know that we\u0027re going to sell every ticket,"},{"Start":"05:07.925 ","End":"05:10.885","Text":"but there will be, say a 500 capacity."},{"Start":"05:10.885 ","End":"05:17.810","Text":"That\u0027s the implementation for all of the sub-types done now."},{"Start":"05:17.810 ","End":"05:21.814","Text":"If I was to go back to the main display,"},{"Start":"05:21.814 ","End":"05:25.550","Text":"I would see that everything\u0027s filled in and it\u0027s all good."},{"Start":"05:25.550 ","End":"05:27.260","Text":"Now I can test,"},{"Start":"05:27.260 ","End":"05:29.690","Text":"as I\u0027ve been asked to do in part e,"},{"Start":"05:29.690 ","End":"05:33.630","Text":"by creating objects on the workbench."},{"Start":"05:33.630 ","End":"05:36.015","Text":"The first one, part I,"},{"Start":"05:36.015 ","End":"05:38.320","Text":"is going to be a party,"},{"Start":"05:39.020 ","End":"05:42.250","Text":"10/2/2022, there is a date,"},{"Start":"05:42.250 ","End":"05:44.885","Text":"data type in Java and most languages,"},{"Start":"05:44.885 ","End":"05:47.660","Text":"just easier for us to use strings at the moment."},{"Start":"05:47.660 ","End":"05:49.350","Text":"Nothing particularly complex about it,"},{"Start":"05:49.350 ","End":"05:51.440","Text":"we\u0027re just trying to keep it as simple as possible."},{"Start":"05:51.440 ","End":"05:53.855","Text":"The name of the booker is Abigail,"},{"Start":"05:53.855 ","End":"05:57.910","Text":"and the number of attendees is 150."},{"Start":"05:57.910 ","End":"06:00.795","Text":"There\u0027s her party booked."},{"Start":"06:00.795 ","End":"06:02.880","Text":"Now we\u0027re going to do the second one now,"},{"Start":"06:02.880 ","End":"06:04.575","Text":"part II of e,"},{"Start":"06:04.575 ","End":"06:07.185","Text":"which is a concert."},{"Start":"06:07.185 ","End":"06:09.910","Text":"Let\u0027s create a new concept."},{"Start":"06:10.340 ","End":"06:13.260","Text":"The date is 11/2/2022,"},{"Start":"06:13.260 ","End":"06:20.265","Text":"the booker is A. Gent,"},{"Start":"06:20.265 ","End":"06:24.455","Text":"and number of attendees is 500."},{"Start":"06:24.455 ","End":"06:26.690","Text":"Now, after we run that,"},{"Start":"06:26.690 ","End":"06:31.190","Text":"we\u0027ve been told to run set artist here in e,"},{"Start":"06:31.190 ","End":"06:35.940","Text":"ii, to set the artist attribute."},{"Start":"06:37.520 ","End":"06:43.235","Text":"This is the only object that will have a set artist method available."},{"Start":"06:43.235 ","End":"06:47.510","Text":"Just prove that by setting it."},{"Start":"06:47.510 ","End":"06:48.740","Text":"If I look on here,"},{"Start":"06:48.740 ","End":"06:51.770","Text":"there is no set artist because that was just in"},{"Start":"06:51.770 ","End":"06:57.200","Text":"the concert object and it wasn\u0027t in the base class."},{"Start":"06:57.200 ","End":"07:00.440","Text":"The final one then in part III,"},{"Start":"07:00.440 ","End":"07:05.070","Text":"we\u0027re going to create a new meeting."},{"Start":"07:05.890 ","End":"07:10.985","Text":"The values are 12/2/22,"},{"Start":"07:10.985 ","End":"07:13.575","Text":"a booker is the Planning Committee,"},{"Start":"07:13.575 ","End":"07:16.585","Text":"and the number of attendees, it\u0027s 81,"},{"Start":"07:16.585 ","End":"07:20.725","Text":"which is more than this maximum for a meeting."},{"Start":"07:20.725 ","End":"07:22.600","Text":"We can check that in a moment."},{"Start":"07:22.600 ","End":"07:28.060","Text":"Let\u0027s check that the output from summary is what we expect for all of those."},{"Start":"07:28.060 ","End":"07:32.800","Text":"Summary for the party object generates that."},{"Start":"07:32.800 ","End":"07:35.215","Text":"Okay, great."},{"Start":"07:35.215 ","End":"07:37.780","Text":"That\u0027s what we expect."},{"Start":"07:37.780 ","End":"07:44.885","Text":"Summary for a concert objects produces that,"},{"Start":"07:44.885 ","End":"07:46.980","Text":"which is great again."},{"Start":"07:46.980 ","End":"07:50.385","Text":"Summary for the meeting object,"},{"Start":"07:50.385 ","End":"07:52.725","Text":"the simplest of the largest code,"},{"Start":"07:52.725 ","End":"07:56.085","Text":"what the meeting is, and the date."},{"Start":"07:56.085 ","End":"08:01.395","Text":"Different behaviors for different sub-types of event."},{"Start":"08:01.395 ","End":"08:03.980","Text":"That\u0027s what we\u0027re looking to check."},{"Start":"08:03.980 ","End":"08:08.875","Text":"Let\u0027s run assignStaff for each of those objects if we get the right numbers."},{"Start":"08:08.875 ","End":"08:13.700","Text":"This will be different behavior as well because it depends on what type of event it was."},{"Start":"08:13.700 ","End":"08:17.970","Text":"AssignStaff, assignStaff, and assignStaff."},{"Start":"08:17.970 ","End":"08:19.580","Text":"If I look inside now,"},{"Start":"08:19.580 ","End":"08:23.030","Text":"I\u0027ve got 10 staff assigned."},{"Start":"08:23.030 ","End":"08:27.830","Text":"That would be right, is a ratio of 15, 1:15."},{"Start":"08:27.830 ","End":"08:29.890","Text":"That\u0027s the right number for that."},{"Start":"08:29.890 ","End":"08:33.005","Text":"For a concert, it was 1 for 10, I think."},{"Start":"08:33.005 ","End":"08:35.120","Text":"If we look at the concert,"},{"Start":"08:35.120 ","End":"08:38.525","Text":"yeah, that\u0027s worked out as well, 50."},{"Start":"08:38.525 ","End":"08:39.859","Text":"Then for a meeting,"},{"Start":"08:39.859 ","End":"08:46.515","Text":"if it was over 40 people than we generated staff of 2."},{"Start":"08:46.515 ","End":"08:49.200","Text":"Let\u0027s have a look inside here."},{"Start":"08:49.200 ","End":"08:51.685","Text":"It was staff of 2."},{"Start":"08:51.685 ","End":"08:56.640","Text":"But that\u0027s over the limit for a meeting."},{"Start":"08:56.640 ","End":"08:59.090","Text":"If we run attendees check,"},{"Start":"08:59.090 ","End":"09:01.580","Text":"they should overturn true apart from that meeting."},{"Start":"09:01.580 ","End":"09:04.295","Text":"Let\u0027s do it in order. Attendees check, true."},{"Start":"09:04.295 ","End":"09:06.050","Text":"Basically means it\u0027s okay."},{"Start":"09:06.050 ","End":"09:07.940","Text":"Attendees check, true."},{"Start":"09:07.940 ","End":"09:09.215","Text":"That\u0027s okay as well."},{"Start":"09:09.215 ","End":"09:13.270","Text":"Attendees check here, and it\u0027s false."},{"Start":"09:13.270 ","End":"09:16.230","Text":"Quite a lot of code to do,"},{"Start":"09:16.230 ","End":"09:17.690","Text":"not a lot, it seems,"},{"Start":"09:17.690 ","End":"09:20.660","Text":"but as you\u0027ll see in a subsequent exercise,"},{"Start":"09:20.660 ","End":"09:26.085","Text":"is really starts to do some fairly useful stuff."},{"Start":"09:26.085 ","End":"09:28.520","Text":"It does, again, illustrate this concept of"},{"Start":"09:28.520 ","End":"09:34.250","Text":"abstract methods and classes and having slightly different behaviors in"},{"Start":"09:34.250 ","End":"09:38.045","Text":"the sub-classes when we\u0027ve got methods that are"},{"Start":"09:38.045 ","End":"09:42.350","Text":"seemingly do very similar things but behave slightly differently."},{"Start":"09:42.350 ","End":"09:44.960","Text":"That\u0027s the essence of polymorphism."},{"Start":"09:44.960 ","End":"09:48.289","Text":"We\u0027ll build on this one in the next couple of exercises."},{"Start":"09:48.289 ","End":"09:50.850","Text":"Thanks for watching and see you for those."}],"ID":31108},{"Watched":false,"Name":"Exercise 5","Duration":"17m 19s","ChapterTopicVideoID":29496,"CourseChapterTopicPlaylistID":294472,"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.759","Text":"Hello, welcome back. In this exercise,"},{"Start":"00:02.759 ","End":"00:06.164","Text":"we\u0027ve been told to make use of the classes event, party,"},{"Start":"00:06.164 ","End":"00:10.260","Text":"concert, and meeting, which we created in the previous exercise."},{"Start":"00:10.260 ","End":"00:13.410","Text":"In part a, we\u0027re told that in the existing project,"},{"Start":"00:13.410 ","End":"00:17.700","Text":"we create a new class called Event Manager and within it,"},{"Start":"00:17.700 ","End":"00:20.875","Text":"a standard Java main method."},{"Start":"00:20.875 ","End":"00:29.165","Text":"Inside main, we declare an array of event objects of size 7 and call it events."},{"Start":"00:29.165 ","End":"00:33.860","Text":"In part c, we\u0027re told to instantiate 3 objects and store references to"},{"Start":"00:33.860 ","End":"00:38.525","Text":"them in the first 3 locations of the array as follows."},{"Start":"00:38.525 ","End":"00:42.800","Text":"First one is a party with arguments 10/2/22,"},{"Start":"00:42.800 ","End":"00:45.475","Text":"Abigail, and 150,"},{"Start":"00:45.475 ","End":"00:47.710","Text":"and that will go in events 0,"},{"Start":"00:47.710 ","End":"00:50.855","Text":"a concert with arguments 11/2/22,"},{"Start":"00:50.855 ","End":"00:54.665","Text":"A. Gent 500 in events 1,"},{"Start":"00:54.665 ","End":"00:58.175","Text":"and then a meeting with arguments 12/2/22,"},{"Start":"00:58.175 ","End":"01:04.230","Text":"Planning Committee 81 in events 2."},{"Start":"01:04.230 ","End":"01:07.580","Text":"Then in part d, we\u0027re told to use a while loop to output"},{"Start":"01:07.580 ","End":"01:12.500","Text":"the details of each element stored in the array using the summary method."},{"Start":"01:12.500 ","End":"01:18.725","Text":"The loop ends when a null object is encountered or the end of the array is reached."},{"Start":"01:18.725 ","End":"01:22.550","Text":"In part e, we\u0027re told to test by running the main method and to note"},{"Start":"01:22.550 ","End":"01:28.165","Text":"the issue with the output for the concert and we\u0027re asked why that\u0027s happened."},{"Start":"01:28.165 ","End":"01:29.870","Text":"In part f,"},{"Start":"01:29.870 ","End":"01:33.140","Text":"we\u0027re told to attempt to rectify the problem by adding"},{"Start":"01:33.140 ","End":"01:37.099","Text":"a line after the creation of the concert object."},{"Start":"01:37.099 ","End":"01:41.090","Text":"What\u0027s the error message you see and why does it occur?"},{"Start":"01:41.090 ","End":"01:44.990","Text":"You\u0027re asked at the end of part f. In part g,"},{"Start":"01:44.990 ","End":"01:48.200","Text":"were told to fix the problem by adding a line of code creating"},{"Start":"01:48.200 ","End":"01:53.150","Text":"a new object called temp concert of type concert."},{"Start":"01:53.150 ","End":"01:54.545","Text":"On the same line,"},{"Start":"01:54.545 ","End":"01:57.230","Text":"we assign the current value of events 1 to"},{"Start":"01:57.230 ","End":"02:02.855","Text":"this new object and you\u0027re reminded that you might need to use a cast."},{"Start":"02:02.855 ","End":"02:07.670","Text":"In part h, we are told to use the temp concert object to fix the problem with"},{"Start":"02:07.670 ","End":"02:12.815","Text":"the missing information spotted in part e. Then in part i,"},{"Start":"02:12.815 ","End":"02:18.700","Text":"were told to run main again to confirm the program now works as expected."},{"Start":"02:18.700 ","End":"02:26.149","Text":"We\u0027re in the existing project for the previous exercise where you had the event party,"},{"Start":"02:26.149 ","End":"02:30.260","Text":"concert, and meeting classes and now we\u0027re going to create"},{"Start":"02:30.260 ","End":"02:34.505","Text":"a main method inside this new class I\u0027ve created called Event Manager."},{"Start":"02:34.505 ","End":"02:37.490","Text":"This doesn\u0027t inherit from anything."},{"Start":"02:37.490 ","End":"02:40.249","Text":"It\u0027s going to make use of the other classes"},{"Start":"02:40.249 ","End":"02:45.170","Text":"and so what we need to do is provide a method inside called main."},{"Start":"02:45.170 ","End":"02:48.920","Text":"We know that there\u0027s a signature for these main methods now: public,"},{"Start":"02:48.920 ","End":"02:55.340","Text":"static, void, meaning there\u0027s only one of these main methods."},{"Start":"02:55.340 ","End":"02:58.639","Text":"It\u0027s accessible outside the class,"},{"Start":"02:58.639 ","End":"03:05.010","Text":"doesn\u0027t return anything, has an array of strings as an argument."},{"Start":"03:05.680 ","End":"03:08.670","Text":"Inside this main method then,"},{"Start":"03:08.670 ","End":"03:14.650","Text":"we\u0027ve been asked in part b to declare an array and this"},{"Start":"03:14.650 ","End":"03:17.095","Text":"is going to look a bit unusual because it\u0027s an array of"},{"Start":"03:17.095 ","End":"03:20.845","Text":"event objects and it\u0027s called events."},{"Start":"03:20.845 ","End":"03:22.750","Text":"Well, I suppose it wouldn\u0027t look unusual,"},{"Start":"03:22.750 ","End":"03:25.885","Text":"but when you start to think about it and dig into it a bit,"},{"Start":"03:25.885 ","End":"03:29.235","Text":"there\u0027s some slightly strange behavior."},{"Start":"03:29.235 ","End":"03:33.790","Text":"The type of the array is event."},{"Start":"03:33.790 ","End":"03:36.655","Text":"It\u0027s an array of events."},{"Start":"03:36.655 ","End":"03:42.250","Text":"We call it events and we use the new keyword to"},{"Start":"03:42.250 ","End":"03:48.660","Text":"allocate space for 7 events."},{"Start":"03:48.660 ","End":"03:51.190","Text":"A week\u0027s worth of events presumably."},{"Start":"03:51.190 ","End":"03:54.200","Text":"It can be 365 if we wanted to implement it that way,"},{"Start":"03:54.200 ","End":"03:56.210","Text":"one for every day of the year."},{"Start":"03:56.210 ","End":"04:02.285","Text":"That\u0027s part b and now we need to instantiate some objects in part c,"},{"Start":"04:02.285 ","End":"04:08.510","Text":"3 of them, and store references to them in the first 3 locations of the array."},{"Start":"04:08.510 ","End":"04:10.490","Text":"This is familiar from the previous exercise,"},{"Start":"04:10.490 ","End":"04:15.440","Text":"part c1 is the same arguments we used previously,"},{"Start":"04:15.440 ","End":"04:19.250","Text":"but this time we\u0027re going to store the object into"},{"Start":"04:19.250 ","End":"04:24.245","Text":"the array so what we need to do here is give the name of the array."},{"Start":"04:24.245 ","End":"04:27.755","Text":"We\u0027re hard-coding the index,"},{"Start":"04:27.755 ","End":"04:29.630","Text":"so the very first element of the array,"},{"Start":"04:29.630 ","End":"04:32.225","Text":"we\u0027re going to store its first object,"},{"Start":"04:32.225 ","End":"04:37.190","Text":"and the object we\u0027re going to create is a party."},{"Start":"04:37.190 ","End":"04:41.615","Text":"You say new party and we pass all the values to the constructor;"},{"Start":"04:41.615 ","End":"04:45.125","Text":"10/2/22, name of the booker,"},{"Start":"04:45.125 ","End":"04:47.665","Text":"and the number of attendees."},{"Start":"04:47.665 ","End":"04:55.925","Text":"That has created an object of type party and stored it into an array element,"},{"Start":"04:55.925 ","End":"05:00.635","Text":"events 0, but the array is of type event,"},{"Start":"05:00.635 ","End":"05:03.275","Text":"which is the interesting thing about this exercise."},{"Start":"05:03.275 ","End":"05:08.345","Text":"We\u0027re going to do the same thing for c2 with"},{"Start":"05:08.345 ","End":"05:15.735","Text":"a different type of object and this one is going to be a concert."},{"Start":"05:15.735 ","End":"05:17.555","Text":"So date goes first,"},{"Start":"05:17.555 ","End":"05:19.894","Text":"then the name of the booker,"},{"Start":"05:19.894 ","End":"05:23.995","Text":"then the number of attendees. We\u0027re done."},{"Start":"05:23.995 ","End":"05:27.080","Text":"Now we\u0027ve created another object and we\u0027ve stored it into"},{"Start":"05:27.080 ","End":"05:30.050","Text":"the events array that we created up here,"},{"Start":"05:30.050 ","End":"05:35.570","Text":"but this is into the second element of the array and then the final one is going to"},{"Start":"05:35.570 ","End":"05:38.420","Text":"be the same thing again but"},{"Start":"05:38.420 ","End":"05:43.224","Text":"a different element and this time we\u0027re going to create a meeting."},{"Start":"05:43.224 ","End":"05:50.370","Text":"The arguments for this were slightly different date."},{"Start":"05:50.370 ","End":"05:55.170","Text":"The title is planning committee."},{"Start":"05:55.600 ","End":"06:00.909","Text":"We have 81 attendees."},{"Start":"06:00.909 ","End":"06:04.275","Text":"That\u0027s nice and straightforward,"},{"Start":"06:04.275 ","End":"06:05.840","Text":"nothing tricky about that."},{"Start":"06:05.840 ","End":"06:08.390","Text":"But what we then been asked to do in part d is to"},{"Start":"06:08.390 ","End":"06:12.050","Text":"create a while loop and this is why we have stored them into"},{"Start":"06:12.050 ","End":"06:15.380","Text":"an array because you can now start using loops to iterate through the elements in"},{"Start":"06:15.380 ","End":"06:20.660","Text":"the way and run methods on the object that\u0027s stored in there."},{"Start":"06:20.660 ","End":"06:23.195","Text":"Now if they\u0027re all the same type of object,"},{"Start":"06:23.195 ","End":"06:24.560","Text":"that will do the same thing."},{"Start":"06:24.560 ","End":"06:28.325","Text":"But these are all the same base class,"},{"Start":"06:28.325 ","End":"06:31.400","Text":"but there are different sub-types so that behavior can be"},{"Start":"06:31.400 ","End":"06:34.789","Text":"different even though we\u0027re running the same methods,"},{"Start":"06:34.789 ","End":"06:38.585","Text":"so this is where we\u0027re seeing some polymorphism happening,"},{"Start":"06:38.585 ","End":"06:45.320","Text":"particularly interesting bit where it\u0027s deciding at run time which method to run."},{"Start":"06:45.320 ","End":"06:51.109","Text":"Let\u0027s then look at what we need to do for a while loop,"},{"Start":"06:51.109 ","End":"06:55.730","Text":"so while loop syntax is like this. I\u0027m sure you remember."},{"Start":"06:55.730 ","End":"06:59.464","Text":"If you\u0027ve come to this chapter from a different language,"},{"Start":"06:59.464 ","End":"07:01.550","Text":"it\u0027s not that different in other languages."},{"Start":"07:01.550 ","End":"07:05.915","Text":"We need to start by having an index variable,"},{"Start":"07:05.915 ","End":"07:07.550","Text":"so I\u0027ll declare that there,"},{"Start":"07:07.550 ","End":"07:11.840","Text":"and what we want do is we want to keep going."},{"Start":"07:11.840 ","End":"07:15.095","Text":"We still have elements to look at."},{"Start":"07:15.095 ","End":"07:24.890","Text":"While i is less than and every Java array has a property called length,"},{"Start":"07:24.890 ","End":"07:26.180","Text":"which tells us how long it is."},{"Start":"07:26.180 ","End":"07:31.025","Text":"It\u0027s a property, or an attribute rather than a function."},{"Start":"07:31.025 ","End":"07:33.560","Text":"We just put that, we don\u0027t need brackets after it."},{"Start":"07:33.560 ","End":"07:36.905","Text":"If we are not at the end of the array,"},{"Start":"07:36.905 ","End":"07:41.135","Text":"which would be element 6, counting from 0."},{"Start":"07:41.135 ","End":"07:42.605","Text":"We can keep going,"},{"Start":"07:42.605 ","End":"07:52.115","Text":"but we also need to check that the current event we\u0027re looking at is a not null."},{"Start":"07:52.115 ","End":"07:57.770","Text":"Because if we don\u0027t assign a value to the array element,"},{"Start":"07:57.770 ","End":"08:02.495","Text":"it will just show null and we\u0027ll get a null pointer error potentially."},{"Start":"08:02.495 ","End":"08:03.965","Text":"We don\u0027t want that to happen."},{"Start":"08:03.965 ","End":"08:07.160","Text":"We just basically stop if we encounter"},{"Start":"08:07.160 ","End":"08:12.455","Text":"an empty array element which hasn\u0027t had an object put into it,"},{"Start":"08:12.455 ","End":"08:17.195","Text":"because at this point we\u0027ve created an array with space for 7 events,"},{"Start":"08:17.195 ","End":"08:19.820","Text":"but we\u0027ve only created 3 of them."},{"Start":"08:19.820 ","End":"08:24.325","Text":"All the subsequent ones will contain a null reference."},{"Start":"08:24.325 ","End":"08:29.650","Text":"There is the loop done and there\u0027s very little left to do now,"},{"Start":"08:29.650 ","End":"08:32.080","Text":"all we want to do is print out"},{"Start":"08:32.080 ","End":"08:37.700","Text":"the details of the event and we have a method that returns all the details."},{"Start":"08:37.700 ","End":"08:39.590","Text":"We could have done this and probably should have done"},{"Start":"08:39.590 ","End":"08:41.690","Text":"this with a 2 string method as well,"},{"Start":"08:41.690 ","End":"08:43.925","Text":"but here we go, we\u0027ve done it now."},{"Start":"08:43.925 ","End":"08:51.455","Text":"What we want to do is say the event that we\u0027re currently looking at on that event,"},{"Start":"08:51.455 ","End":"08:57.845","Text":"run the summary method and that will be printed out."},{"Start":"08:57.845 ","End":"09:01.640","Text":"Then of course, we need to increment i otherwise would be in"},{"Start":"09:01.640 ","End":"09:06.815","Text":"an infinite loop and will always be printing the first element of the array."},{"Start":"09:06.815 ","End":"09:09.170","Text":"That is our part d done,"},{"Start":"09:09.170 ","End":"09:12.590","Text":"and we can now run this and as we\u0027ve been asked to do in part e,"},{"Start":"09:12.590 ","End":"09:15.140","Text":"with luck, this will all magically work and will"},{"Start":"09:15.140 ","End":"09:18.620","Text":"have different types of objects stored into this array."},{"Start":"09:18.620 ","End":"09:22.055","Text":"Let\u0027s see what happens. Compiles fine."},{"Start":"09:22.055 ","End":"09:26.045","Text":"You see all these arrows here are basically saying"},{"Start":"09:26.045 ","End":"09:31.160","Text":"the event manager makes use of all of these other classes."},{"Start":"09:31.160 ","End":"09:34.730","Text":"It\u0027s not part of the inheritance hierarchy as these are,"},{"Start":"09:34.730 ","End":"09:36.920","Text":"but it does make use of all of"},{"Start":"09:36.920 ","End":"09:40.895","Text":"these other classes and that\u0027s why we\u0027ve got these dashed arrows here."},{"Start":"09:40.895 ","End":"09:45.935","Text":"Let\u0027s go ahead and run the main method and see what happens. There we go."},{"Start":"09:45.935 ","End":"09:51.350","Text":"It\u0027s printed all of the details from summary as we expect,"},{"Start":"09:51.350 ","End":"09:55.700","Text":"but we\u0027re asked the question to note"},{"Start":"09:55.700 ","End":"10:00.455","Text":"in part E what the issue is with the output for the concert and what\u0027s happened."},{"Start":"10:00.455 ","End":"10:04.535","Text":"The concert has a value here null."},{"Start":"10:04.535 ","End":"10:07.145","Text":"The object itself is not null,"},{"Start":"10:07.145 ","End":"10:12.845","Text":"but something we\u0027re using in that method has been set to null."},{"Start":"10:12.845 ","End":"10:19.850","Text":"We might be able to spot what\u0027s happened if you look at the summary method,"},{"Start":"10:19.850 ","End":"10:23.075","Text":"one of the things that we print here,"},{"Start":"10:23.075 ","End":"10:26.120","Text":"which we don\u0027t do for the other methods is the name of"},{"Start":"10:26.120 ","End":"10:29.165","Text":"the artists during the concert and you\u0027ll remember,"},{"Start":"10:29.165 ","End":"10:33.560","Text":"we\u0027re supposed to set the artist name and we haven\u0027t done that."},{"Start":"10:33.560 ","End":"10:36.140","Text":"Therefore, it prints null,"},{"Start":"10:36.140 ","End":"10:38.270","Text":"but there\u0027s a further problem."},{"Start":"10:38.270 ","End":"10:41.840","Text":"Let\u0027s try and do what part f says."},{"Start":"10:41.840 ","End":"10:47.825","Text":"Attempt to rectify it by adding a line after the creation of the concert objects."},{"Start":"10:47.825 ","End":"10:53.240","Text":"Here we should be able to say events 1,"},{"Start":"10:53.240 ","End":"10:57.905","Text":"because that\u0027s where we stored our concert object."},{"Start":"10:57.905 ","End":"11:07.670","Text":"Events 1.set artist and we can just set the artist name as we did previously."},{"Start":"11:07.670 ","End":"11:10.445","Text":"If we compile that,"},{"Start":"11:10.445 ","End":"11:17.420","Text":"we get an issue and it says cannot find symbol method set artist."},{"Start":"11:17.420 ","End":"11:22.115","Text":"Now the problem here is that we can\u0027t,"},{"Start":"11:22.115 ","End":"11:25.310","Text":"as we found in a previous exercise,"},{"Start":"11:25.310 ","End":"11:29.000","Text":"run on a generic event,"},{"Start":"11:29.000 ","End":"11:30.860","Text":"which could be of any of these types,"},{"Start":"11:30.860 ","End":"11:33.575","Text":"party, concert, or meeting."},{"Start":"11:33.575 ","End":"11:41.945","Text":"We can\u0027t execute a method that only exists in one of the sub-types."},{"Start":"11:41.945 ","End":"11:45.035","Text":"If it\u0027s going to exist, it has to exist in all of them."},{"Start":"11:45.035 ","End":"11:52.970","Text":"Previously, we fix this problem by moving an attribute and a method into the base class."},{"Start":"11:52.970 ","End":"11:57.800","Text":"Now, that doesn\u0027t really seem very good in terms of a solution here,"},{"Start":"11:57.800 ","End":"12:01.895","Text":"because why should we have an artist\u0027s field"},{"Start":"12:01.895 ","End":"12:06.230","Text":"in all of the other classes when there is no artists,"},{"Start":"12:06.230 ","End":"12:09.050","Text":"it don\u0027t really sit right, does it?"},{"Start":"12:09.050 ","End":"12:15.170","Text":"There is another way of doing this and it\u0027s directed us to that in part"},{"Start":"12:15.170 ","End":"12:23.990","Text":"g. It says fix it by creating a new object called temp concert of type concert."},{"Start":"12:23.990 ","End":"12:27.710","Text":"What we can do here is let\u0027s comment this out."},{"Start":"12:27.710 ","End":"12:29.495","Text":"That\u0027s what we were trying to do,"},{"Start":"12:29.495 ","End":"12:33.620","Text":"but we cannot because of the issue that we\u0027ve previously come"},{"Start":"12:33.620 ","End":"12:38.120","Text":"across around sub-types and the base class."},{"Start":"12:38.120 ","End":"12:42.035","Text":"If we have something that is of type event,"},{"Start":"12:42.035 ","End":"12:46.400","Text":"which is all of these other classes are sub-types of that type."},{"Start":"12:46.400 ","End":"12:49.040","Text":"We can do that so long as they\u0027ve all got the same methods,"},{"Start":"12:49.040 ","End":"12:53.090","Text":"but this one is different in the concert class."},{"Start":"12:53.090 ","End":"12:54.500","Text":"You only exist in the concert class."},{"Start":"12:54.500 ","End":"12:56.330","Text":"What we can do is we can get around that."},{"Start":"12:56.330 ","End":"13:06.360","Text":"We can create a new temporary object and we can call it tempConcert."},{"Start":"13:07.720 ","End":"13:11.690","Text":"Then how do we get a value of that?"},{"Start":"13:11.690 ","End":"13:17.870","Text":"Well, we want to get a value of what\u0027s already stored in events 1."},{"Start":"13:17.870 ","End":"13:20.720","Text":"Why don\u0027t we do that and we\u0027ve seen that in a previous exercise,"},{"Start":"13:20.720 ","End":"13:22.220","Text":"you can actually do that."},{"Start":"13:22.220 ","End":"13:27.140","Text":"Events 1 refers to an object which is what we created"},{"Start":"13:27.140 ","End":"13:31.670","Text":"with these attributes and it\u0027s currently stored in events"},{"Start":"13:31.670 ","End":"13:35.689","Text":"1 and we can take a copy of that and put it into"},{"Start":"13:35.689 ","End":"13:41.660","Text":"this new object we\u0027ve created called tempConcert and because that\u0027s of type concert,"},{"Start":"13:41.660 ","End":"13:43.640","Text":"we should be able to execute methods."},{"Start":"13:43.640 ","End":"13:45.920","Text":"However, there\u0027s still a little problem here."},{"Start":"13:45.920 ","End":"13:48.395","Text":"If we look at this, if we try and compile it,"},{"Start":"13:48.395 ","End":"13:53.675","Text":"it says incompatible types event cannot be converted concert."},{"Start":"13:53.675 ","End":"13:57.080","Text":"But fortunately there\u0027s a compiler doing its job."},{"Start":"13:57.080 ","End":"13:59.090","Text":"That\u0027s what it\u0027s there for is to check that you don\u0027t"},{"Start":"13:59.090 ","End":"14:01.640","Text":"assign something of one type to another."},{"Start":"14:01.640 ","End":"14:05.000","Text":"But as we know with strings and so on,"},{"Start":"14:05.000 ","End":"14:08.180","Text":"we can actually do something called a cast where"},{"Start":"14:08.180 ","End":"14:11.300","Text":"we temporarily say to the compiler, hey, look,"},{"Start":"14:11.300 ","End":"14:15.365","Text":"I know this is not really a concert if you declared it in events,"},{"Start":"14:15.365 ","End":"14:17.540","Text":"but can you just do it for me, please anyway,"},{"Start":"14:17.540 ","End":"14:23.540","Text":"so we do the cast and that\u0027s just by putting the type we want it"},{"Start":"14:23.540 ","End":"14:30.035","Text":"to be in brackets and if it can translate it into a concert, it will."},{"Start":"14:30.035 ","End":"14:32.225","Text":"The compiler goes away happy,"},{"Start":"14:32.225 ","End":"14:35.360","Text":"and you\u0027ll see now it does compile."},{"Start":"14:35.360 ","End":"14:38.555","Text":"Just to summarize what we\u0027ve done there."},{"Start":"14:38.555 ","End":"14:42.620","Text":"We had one of these elements was of type concert."},{"Start":"14:42.620 ","End":"14:45.080","Text":"The other one was a party, the other one was a meeting."},{"Start":"14:45.080 ","End":"14:47.585","Text":"There was one method called"},{"Start":"14:47.585 ","End":"14:51.650","Text":"set artists that was only in concert and wasn\u0027t in party, or meeting,"},{"Start":"14:51.650 ","End":"14:53.510","Text":"or the base class therefore,"},{"Start":"14:53.510 ","End":"14:59.120","Text":"if we try and run set artist on an event, it would complain."},{"Start":"14:59.120 ","End":"15:01.790","Text":"Whereas we\u0027ve now created"},{"Start":"15:01.790 ","End":"15:07.295","Text":"a new temporary object called tempConcert and we\u0027ve placed the value,"},{"Start":"15:07.295 ","End":"15:12.470","Text":"or we\u0027ve actually placed a reference to Events 1 into that objects."},{"Start":"15:12.470 ","End":"15:21.185","Text":"We now have a way of running the code inside a concert object."},{"Start":"15:21.185 ","End":"15:22.550","Text":"Now we can just,"},{"Start":"15:22.550 ","End":"15:24.530","Text":"on the second line,"},{"Start":"15:24.530 ","End":"15:25.970","Text":"as it says in part h,"},{"Start":"15:25.970 ","End":"15:29.840","Text":"use the temp concert object to fix the problem with the missing information."},{"Start":"15:29.840 ","End":"15:32.630","Text":"What we should be able to do if you say"},{"Start":"15:32.630 ","End":"15:37.865","Text":"tempConcert and now we can run set artists because we\u0027re working on"},{"Start":"15:37.865 ","End":"15:42.170","Text":"object in terms of this line but it\u0027s actually"},{"Start":"15:42.170 ","End":"15:48.980","Text":"referring to something in the events array because of this reference here."},{"Start":"15:48.980 ","End":"15:53.660","Text":"Let\u0027s see what happens and now our error has gone away."},{"Start":"15:53.660 ","End":"15:56.675","Text":"Let\u0027s see if when we run main again,"},{"Start":"15:56.675 ","End":"15:58.535","Text":"we don\u0027t see null this time,"},{"Start":"15:58.535 ","End":"16:02.280","Text":"but we see the artist\u0027s name that we just set."},{"Start":"16:02.760 ","End":"16:07.180","Text":"Yes, we do. It\u0027s done what we wanted to."},{"Start":"16:07.180 ","End":"16:12.230","Text":"Little trick we did here in that we didn\u0027t really want to"},{"Start":"16:12.230 ","End":"16:17.210","Text":"move the set artist method into the base class because it"},{"Start":"16:17.210 ","End":"16:20.210","Text":"doesn\u0027t fit with any other type of event and it\u0027s"},{"Start":"16:20.210 ","End":"16:23.225","Text":"right to put it in the specialized class"},{"Start":"16:23.225 ","End":"16:26.750","Text":"of concert but it then presented"},{"Start":"16:26.750 ","End":"16:31.280","Text":"this problem of us not being able to run that line that we wanted to."},{"Start":"16:31.280 ","End":"16:33.080","Text":"Now look at what we\u0027ve got here."},{"Start":"16:33.080 ","End":"16:36.780","Text":"We were doing something interesting here it doesn\u0027t look particularly interesting,"},{"Start":"16:36.780 ","End":"16:38.585","Text":"there\u0027s a very simple piece of code."},{"Start":"16:38.585 ","End":"16:40.310","Text":"What it\u0027s doing is it\u0027s running"},{"Start":"16:40.310 ","End":"16:45.710","Text":"a different summary depending on what was stored at that a particular element."},{"Start":"16:45.710 ","End":"16:50.900","Text":"The inside of the loop is the same no matter what type of object we\u0027re dealing"},{"Start":"16:50.900 ","End":"16:56.150","Text":"with so long as they\u0027re all from the same parent class, it will work."},{"Start":"16:56.150 ","End":"17:00.320","Text":"Obviously there was a summary in the parent class and we overrode it."},{"Start":"17:00.320 ","End":"17:04.110","Text":"Therefore we had different behavior in a child classes."},{"Start":"17:04.110 ","End":"17:12.230","Text":"Really getting into the crux now of polymorphic methods and the method binding,"},{"Start":"17:12.230 ","End":"17:15.955","Text":"or method lookup that happens during run-time."},{"Start":"17:15.955 ","End":"17:19.760","Text":"That\u0027s it for this one. Thanks for watching. See you in the next one."}],"ID":31109},{"Watched":false,"Name":"Exercise 6 Part 1","Duration":"12m 49s","ChapterTopicVideoID":29495,"CourseChapterTopicPlaylistID":294472,"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 everyone. In this exercise,"},{"Start":"00:03.150 ","End":"00:05.070","Text":"we\u0027re told to build on the previous 1,"},{"Start":"00:05.070 ","End":"00:09.225","Text":"to build more functionality into the EventManager application."},{"Start":"00:09.225 ","End":"00:15.285","Text":"We\u0027re going to allow users to book and store events interactively using keyboard input."},{"Start":"00:15.285 ","End":"00:17.190","Text":"In Part a, we\u0027re told to edit"},{"Start":"00:17.190 ","End":"00:22.740","Text":"the existing EventManager class so it looks like the following code."},{"Start":"00:22.740 ","End":"00:25.625","Text":"In Part b, we\u0027re told to create a new method"},{"Start":"00:25.625 ","End":"00:28.970","Text":"inside the EventManager class called displayOptions,"},{"Start":"00:28.970 ","End":"00:32.135","Text":"which returns no values and accepts no arguments."},{"Start":"00:32.135 ","End":"00:35.120","Text":"Within it, we print out the options below."},{"Start":"00:35.120 ","End":"00:37.805","Text":"Option 1, list booked events,"},{"Start":"00:37.805 ","End":"00:39.575","Text":"Option 2, book a new event,"},{"Start":"00:39.575 ","End":"00:41.720","Text":"Option 3, check attendance."},{"Start":"00:41.720 ","End":"00:45.210","Text":"Option 9, exit program."},{"Start":"00:45.210 ","End":"00:49.280","Text":"In Part c, we\u0027re told to create a new method called menu,"},{"Start":"00:49.280 ","End":"00:51.379","Text":"which does the following."},{"Start":"00:51.379 ","End":"00:54.890","Text":"Create an object from the Scanner class,"},{"Start":"00:54.890 ","End":"00:58.250","Text":"add import java.util.Scanner at the top of"},{"Start":"00:58.250 ","End":"01:04.000","Text":"the class by adding as a first-line of the method and then there\u0027s a line of code."},{"Start":"01:04.000 ","End":"01:05.690","Text":"In Part 2,"},{"Start":"01:05.690 ","End":"01:07.250","Text":"we\u0027re told to call displayOptions,"},{"Start":"01:07.250 ","End":"01:09.055","Text":"to display the initial options."},{"Start":"01:09.055 ","End":"01:13.430","Text":"In Part 3, we\u0027re told to use a while loop to take input from the keyboard using"},{"Start":"01:13.430 ","End":"01:18.910","Text":"keyboard.next to either quit the loop or run the listEvents method."},{"Start":"01:18.910 ","End":"01:21.485","Text":"In Part 4, we\u0027re told to use equals,"},{"Start":"01:21.485 ","End":"01:23.120","Text":"not a double equals,"},{"Start":"01:23.120 ","End":"01:27.440","Text":"and not equal to sign when comparing strings in Java."},{"Start":"01:27.440 ","End":"01:31.760","Text":"In Part 5, we\u0027re told to call displayOptions inside the loop to"},{"Start":"01:31.760 ","End":"01:36.830","Text":"display the options each time and then check again for keyboard inputs."},{"Start":"01:36.830 ","End":"01:38.990","Text":"Then finally in Part d, we\u0027re told to run"},{"Start":"01:38.990 ","End":"01:42.230","Text":"the main method in EventManager to see if the code"},{"Start":"01:42.230 ","End":"01:48.235","Text":"so far works as intended by choosing first Option 1 and then Option 9."},{"Start":"01:48.235 ","End":"01:51.050","Text":"The first thing we need to do is we\u0027ve been asked in"},{"Start":"01:51.050 ","End":"01:53.810","Text":"Part a is to edit the existing EventManager class,"},{"Start":"01:53.810 ","End":"01:57.845","Text":"which look like this when we\u0027ve completed the previous exercise."},{"Start":"01:57.845 ","End":"02:01.340","Text":"We\u0027re going to reorganize this so that we can use it as the basis of"},{"Start":"02:01.340 ","End":"02:03.020","Text":"our application because there\u0027s some things we want to"},{"Start":"02:03.020 ","End":"02:05.810","Text":"keep and there\u0027s some things we want to remove."},{"Start":"02:05.810 ","End":"02:10.415","Text":"First of all, we\u0027ll remove these lines here which aren\u0027t needed."},{"Start":"02:10.415 ","End":"02:15.680","Text":"They were there just to create some objects so that we could test things."},{"Start":"02:15.680 ","End":"02:22.695","Text":"This part is going to move into its own new method called listEvents."},{"Start":"02:22.695 ","End":"02:29.520","Text":"Let\u0027s take that out and then move it outside main into its own method."},{"Start":"02:29.520 ","End":"02:33.990","Text":"It\u0027s public void listEvents."},{"Start":"02:33.990 ","End":"02:39.470","Text":"Then the code itself will go inside and intent that"},{"Start":"02:39.470 ","End":"02:45.110","Text":"so it\u0027s a bit neater and that pretty much is that part done."},{"Start":"02:45.110 ","End":"02:53.000","Text":"We\u0027ve got something that will allow us to list the contents of the events per array,"},{"Start":"02:53.000 ","End":"02:56.600","Text":"printing out each element of that array."},{"Start":"02:56.600 ","End":"02:58.430","Text":"Well, not just the element of the array,"},{"Start":"02:58.430 ","End":"03:01.985","Text":"a summary of what\u0027s contained in the object."},{"Start":"03:01.985 ","End":"03:04.175","Text":"That part done."},{"Start":"03:04.175 ","End":"03:09.410","Text":"Now, the array itself has to be moved outside"},{"Start":"03:09.410 ","End":"03:15.305","Text":"the main method to the top so that everything in the class can access it."},{"Start":"03:15.305 ","End":"03:20.180","Text":"Actually, that\u0027s the most important change of all."},{"Start":"03:20.180 ","End":"03:26.810","Text":"Then finally, what we need to do is main should not be really used to contain"},{"Start":"03:26.810 ","End":"03:29.840","Text":"an entire application unless it\u0027s a very simple things that beginners"},{"Start":"03:29.840 ","End":"03:33.455","Text":"often put everything into main and it\u0027s understandable at the beginning,"},{"Start":"03:33.455 ","End":"03:37.310","Text":"but you\u0027ll quickly run into problems because of the fact that"},{"Start":"03:37.310 ","End":"03:42.200","Text":"main is a static and lots of your other things aren\u0027t going to be statics."},{"Start":"03:42.200 ","End":"03:45.380","Text":"If you\u0027re sharing data like we are here, for example,"},{"Start":"03:45.380 ","End":"03:48.725","Text":"in this array, you\u0027ll quickly run into problems."},{"Start":"03:48.725 ","End":"03:50.630","Text":"That\u0027s why we\u0027ve moved it out."},{"Start":"03:50.630 ","End":"03:57.920","Text":"Main really should only be to kick off another bit of code in your overall application."},{"Start":"03:57.920 ","End":"04:03.810","Text":"In this case, we are going to create an EventManager object."},{"Start":"04:03.810 ","End":"04:06.230","Text":"From this class that we\u0027re defining,"},{"Start":"04:06.230 ","End":"04:08.000","Text":"we\u0027re going to create an object."},{"Start":"04:08.000 ","End":"04:13.445","Text":"Then we can run a piece of code in there which runs everything else."},{"Start":"04:13.445 ","End":"04:16.145","Text":"You\u0027ll see in the question,"},{"Start":"04:16.145 ","End":"04:19.685","Text":"it actually gives you the code that you need to write."},{"Start":"04:19.685 ","End":"04:24.305","Text":"It\u0027s got EventManager because that\u0027s the name of the class."},{"Start":"04:24.305 ","End":"04:26.510","Text":"Then we\u0027ve got the object name,"},{"Start":"04:26.510 ","End":"04:32.190","Text":"and then we create an instance of the class EventManager."},{"Start":"04:32.190 ","End":"04:36.280","Text":"Everything we\u0027re subsequently going to write will be"},{"Start":"04:36.280 ","End":"04:40.560","Text":"now accessible through this object called app."},{"Start":"04:40.560 ","End":"04:44.385","Text":"The next line here is app.menu."},{"Start":"04:44.385 ","End":"04:47.200","Text":"We haven\u0027t actually implemented that yet."},{"Start":"04:47.200 ","End":"04:54.505","Text":"But this menu method will actually do all of the main stuff in this program."},{"Start":"04:54.505 ","End":"04:58.180","Text":"Basically, this main is the entry point for the program and it"},{"Start":"04:58.180 ","End":"05:01.780","Text":"creates an object of type EventManager,"},{"Start":"05:01.780 ","End":"05:07.040","Text":"which we call app and then it kicks off the method called menu within apps."},{"Start":"05:07.040 ","End":"05:09.280","Text":"It\u0027s complaining at the moment because it can\u0027t find it,"},{"Start":"05:09.280 ","End":"05:13.140","Text":"but we\u0027ll fill that in pretty soon."},{"Start":"05:13.140 ","End":"05:16.850","Text":"That is the first part done."},{"Start":"05:16.850 ","End":"05:18.800","Text":"The only thing we haven\u0027t actually added is,"},{"Start":"05:18.800 ","End":"05:21.560","Text":"we\u0027ve got another variable."},{"Start":"05:21.560 ","End":"05:25.325","Text":"You\u0027ll notice here called EventPointer,"},{"Start":"05:25.325 ","End":"05:27.605","Text":"which is an int,"},{"Start":"05:27.605 ","End":"05:32.475","Text":"and it is set to zero."},{"Start":"05:32.475 ","End":"05:36.485","Text":"We use this as an index into this array"},{"Start":"05:36.485 ","End":"05:41.270","Text":"to say where we are writing the next event to basically."},{"Start":"05:41.270 ","End":"05:44.330","Text":"That\u0027s Part a done."},{"Start":"05:44.330 ","End":"05:49.895","Text":"Now, we\u0027re asked to create a new method inside the EventManager class."},{"Start":"05:49.895 ","End":"05:51.760","Text":"Not inside the main method,"},{"Start":"05:51.760 ","End":"05:53.325","Text":"not inside listEvents,"},{"Start":"05:53.325 ","End":"05:57.320","Text":"but inside the overall class that we\u0027ve been working in previously."},{"Start":"05:57.320 ","End":"06:02.235","Text":"This is called displayOptions."},{"Start":"06:02.235 ","End":"06:06.660","Text":"Then displayOptions takes no arguments,"},{"Start":"06:06.660 ","End":"06:11.175","Text":"it\u0027s public and it returns nothing."},{"Start":"06:11.175 ","End":"06:14.640","Text":"What we need to do is print out the options."},{"Start":"06:14.640 ","End":"06:21.900","Text":"The first 1 says option number 1 is to list booked events."},{"Start":"06:21.900 ","End":"06:25.085","Text":"Let\u0027s copy this slide. It\u0027s very similar to all the others,"},{"Start":"06:25.085 ","End":"06:27.940","Text":"2, is to book a new event,"},{"Start":"06:27.940 ","End":"06:30.780","Text":"3 is to check attendance,"},{"Start":"06:30.780 ","End":"06:35.550","Text":"and 9 is to exit the program."},{"Start":"06:35.550 ","End":"06:39.960","Text":"That\u0027s display options done very straightforward."},{"Start":"06:39.960 ","End":"06:42.960","Text":"That\u0027s Part b closed off."},{"Start":"06:42.960 ","End":"06:47.255","Text":"Then finally, we need to create this method called menu,"},{"Start":"06:47.255 ","End":"06:54.049","Text":"which does the core things within this particular application."},{"Start":"06:54.049 ","End":"06:56.315","Text":"Let\u0027s go ahead and do that."},{"Start":"06:56.315 ","End":"07:02.520","Text":"First of all, we\u0027ve been told we\u0027re going to use the Scanner class in a menu."},{"Start":"07:02.520 ","End":"07:05.220","Text":"If I need a Scanner class,"},{"Start":"07:05.220 ","End":"07:10.145","Text":"and let\u0027s just do the opening line."},{"Start":"07:10.145 ","End":"07:13.365","Text":"Menu, takes no options."},{"Start":"07:13.365 ","End":"07:15.210","Text":"I\u0027m going to create a Scanner class."},{"Start":"07:15.210 ","End":"07:16.560","Text":"If I need to Scanner class,"},{"Start":"07:16.560 ","End":"07:19.820","Text":"I have to import that from the Java library that contains it."},{"Start":"07:19.820 ","End":"07:23.005","Text":"The very first line is import"},{"Start":"07:23.005 ","End":"07:31.755","Text":"java.util.Scanner with a capital S. Now I can make use of that."},{"Start":"07:31.755 ","End":"07:34.760","Text":"I won\u0027t get an error on this next line where"},{"Start":"07:34.760 ","End":"07:39.500","Text":"I try and create a new object from the Scanner class."},{"Start":"07:39.500 ","End":"07:43.385","Text":"I\u0027m going to call it keyb for keyboard."},{"Start":"07:43.385 ","End":"07:48.060","Text":"It\u0027s going to be a new Scanner and it uses system"},{"Start":"07:48.060 ","End":"07:53.810","Text":"in order to get the input stream from the keyboard."},{"Start":"07:53.810 ","End":"07:56.215","Text":"That part done,"},{"Start":"07:56.215 ","End":"08:03.790","Text":"and then 1 I\u0027ve been told to do is use that to take keyboard input."},{"Start":"08:03.790 ","End":"08:07.990","Text":"But I\u0027m going to display the options available for first."},{"Start":"08:07.990 ","End":"08:13.660","Text":"It just prints out a menu of options before it takes any keyboard input."},{"Start":"08:13.660 ","End":"08:16.265","Text":"Then I\u0027m going to use a while loop."},{"Start":"08:16.265 ","End":"08:18.315","Text":"It says in Part 3,"},{"Start":"08:18.315 ","End":"08:24.730","Text":"and I\u0027m going to use keyboard next to either quit the loop or run the listEvents method."},{"Start":"08:24.730 ","End":"08:28.510","Text":"Keyboard.next or keyb,"},{"Start":"08:28.510 ","End":"08:34.300","Text":"I\u0027ve called it, will return a token from the input."},{"Start":"08:34.300 ","End":"08:37.130","Text":"I need to store that somewhere."},{"Start":"08:37.980 ","End":"08:41.665","Text":"Let\u0027s call it choice."},{"Start":"08:41.665 ","End":"08:45.140","Text":"I\u0027m going to set up a variable here,"},{"Start":"08:45.140 ","End":"08:47.555","Text":"do that along the same line for 1-2."},{"Start":"08:47.555 ","End":"08:51.140","Text":"Now choice has a value which it\u0027s"},{"Start":"08:51.140 ","End":"08:55.810","Text":"reading from the keyboard and I can use that in a while loop."},{"Start":"08:55.810 ","End":"09:04.470","Text":"Let\u0027s fill out the code once we\u0027ve created the block."},{"Start":"09:04.470 ","End":"09:07.385","Text":"What do I need in my while loop?"},{"Start":"09:07.385 ","End":"09:12.600","Text":"I either quit the loop or run the listEvents method."},{"Start":"09:12.600 ","End":"09:19.370","Text":"What I need to do is I need to see whether the input equals 9."},{"Start":"09:19.370 ","End":"09:21.415","Text":"Because if it does equal 9,"},{"Start":"09:21.415 ","End":"09:23.610","Text":"then I want to quit."},{"Start":"09:23.610 ","End":"09:27.930","Text":"But actually, while it\u0027s anything but 9,"},{"Start":"09:27.930 ","End":"09:29.880","Text":"I can keep going."},{"Start":"09:29.880 ","End":"09:34.820","Text":"I do a not there while the choice is not equal to 9."},{"Start":"09:34.820 ","End":"09:37.100","Text":"Just to reiterate strings,"},{"Start":"09:37.100 ","End":"09:40.235","Text":"you can\u0027t just compare them with equals and not equals."},{"Start":"09:40.235 ","End":"09:42.905","Text":"You have to use the equals method because we\u0027re saying,"},{"Start":"09:42.905 ","End":"09:44.915","Text":"do they contain the same thing?"},{"Start":"09:44.915 ","End":"09:47.300","Text":"Not do they live in the same memory location,"},{"Start":"09:47.300 ","End":"09:49.520","Text":"but do they contain the same thing?"},{"Start":"09:49.520 ","End":"09:56.155","Text":"While the choice is not equal to 9,"},{"Start":"09:56.155 ","End":"09:59.150","Text":"I want to go around the loop."},{"Start":"09:59.150 ","End":"10:02.745","Text":"Otherwise, I quit the loop."},{"Start":"10:02.745 ","End":"10:11.050","Text":"What I need to do inside the loop then is I need to check whether choices,"},{"Start":"10:11.050 ","End":"10:13.030","Text":"1 of the other options, for example,"},{"Start":"10:13.030 ","End":"10:15.505","Text":"listEvents, so I can do that."},{"Start":"10:15.505 ","End":"10:18.230","Text":"If same thing again,"},{"Start":"10:18.230 ","End":"10:26.990","Text":"choice equals 1."},{"Start":"10:26.990 ","End":"10:28.385","Text":"If it is 1,"},{"Start":"10:28.385 ","End":"10:30.920","Text":"then I do something."},{"Start":"10:30.920 ","End":"10:37.450","Text":"In this case, what I want to do is if I haven\u0027t entered anything else,"},{"Start":"10:37.450 ","End":"10:39.954","Text":"but 1, it will just do nothing."},{"Start":"10:39.954 ","End":"10:49.010","Text":"But what I do need to do at the end here is I need to display the options again."},{"Start":"10:50.370 ","End":"10:55.330","Text":"That the user knows what options they have to type and then"},{"Start":"10:55.330 ","End":"10:59.395","Text":"I need to get input again inside the loop,"},{"Start":"10:59.395 ","End":"11:03.545","Text":"because previously, I was getting outside the loop."},{"Start":"11:03.545 ","End":"11:06.180","Text":"I need to get it here as well,"},{"Start":"11:06.180 ","End":"11:10.300","Text":"so it gives me the option to get another input."},{"Start":"11:10.300 ","End":"11:16.315","Text":"Otherwise, I\u0027ll have an infinite loop and that should do it."},{"Start":"11:16.315 ","End":"11:19.135","Text":"We could test that now,"},{"Start":"11:19.135 ","End":"11:21.370","Text":"run the main method in EventManager,"},{"Start":"11:21.370 ","End":"11:22.615","Text":"as it says in Part d,"},{"Start":"11:22.615 ","End":"11:27.700","Text":"to see the code works by choosing Option 1 and then Option 9."},{"Start":"11:27.700 ","End":"11:30.335","Text":"I need to run the main method."},{"Start":"11:30.335 ","End":"11:33.560","Text":"There\u0027s my menu options is waiting for some keyboard input,"},{"Start":"11:33.560 ","End":"11:35.135","Text":"which I type at the bottom."},{"Start":"11:35.135 ","End":"11:39.290","Text":"The only 1 that should work at the moment is to list booked events."},{"Start":"11:39.290 ","End":"11:43.460","Text":"It did something, but I can\u0027t see any events so"},{"Start":"11:43.460 ","End":"11:48.170","Text":"presumably it did try to print and if I do 9, it should quit,"},{"Start":"11:48.170 ","End":"11:57.695","Text":"which it has so 1 thing I could add into my listEvents is a little thing"},{"Start":"11:57.695 ","End":"12:02.045","Text":"at the top so that I know it actually got into it"},{"Start":"12:02.045 ","End":"12:08.190","Text":"and probably nice friendly list of what it\u0027s doing."},{"Start":"12:08.190 ","End":"12:10.755","Text":"That would do the job,"},{"Start":"12:10.755 ","End":"12:13.470","Text":"blank lines in as well."},{"Start":"12:13.470 ","End":"12:17.420","Text":"If there are events, it will print the events,"},{"Start":"12:17.420 ","End":"12:20.240","Text":"but it will also print if there are events,"},{"Start":"12:20.240 ","End":"12:21.320","Text":"it will print the events."},{"Start":"12:21.320 ","End":"12:24.065","Text":"Otherwise it would just print the word events."},{"Start":"12:24.065 ","End":"12:29.990","Text":"That will make the user a little bit more reassured that something has actually happened."},{"Start":"12:29.990 ","End":"12:33.395","Text":"To do that again, 1, list booked events and there it say\u0027s events."},{"Start":"12:33.395 ","End":"12:34.865","Text":"We know that options worked,"},{"Start":"12:34.865 ","End":"12:36.950","Text":"9, quits the program."},{"Start":"12:36.950 ","End":"12:38.870","Text":"In the subsequent exercises,"},{"Start":"12:38.870 ","End":"12:42.080","Text":"we\u0027ll actually start filling in these methods here."},{"Start":"12:42.080 ","End":"12:44.675","Text":"We do actually add some events."},{"Start":"12:44.675 ","End":"12:46.700","Text":"When we list them, we can see them."},{"Start":"12:46.700 ","End":"12:50.100","Text":"Thanks for watching. I\u0027ll see you soon for the next 1."}],"ID":31110},{"Watched":false,"Name":"Exercise 6 Part 2","Duration":"15m 45s","ChapterTopicVideoID":29494,"CourseChapterTopicPlaylistID":294472,"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.425","Text":"Hello, Welcome back."},{"Start":"00:01.425 ","End":"00:03.749","Text":"In this next part of the exercise,"},{"Start":"00:03.749 ","End":"00:06.810","Text":"we\u0027re told to implement a selector method in"},{"Start":"00:06.810 ","End":"00:10.365","Text":"the base class which returns the date attribute."},{"Start":"00:10.365 ","End":"00:13.310","Text":"Then to adapt but constructor in the concert class,"},{"Start":"00:13.310 ","End":"00:19.200","Text":"so it accepts an extra argument to set the value of the artist attribute."},{"Start":"00:19.200 ","End":"00:23.520","Text":"In part G were told to implement a new method, book event,"},{"Start":"00:23.520 ","End":"00:27.555","Text":"which asks the user for all the data required for an event and"},{"Start":"00:27.555 ","End":"00:31.605","Text":"adds it to the next available slot in the array."},{"Start":"00:31.605 ","End":"00:35.895","Text":"In part 1, we were told to declare 4 string variables, date, Booker,"},{"Start":"00:35.895 ","End":"00:40.365","Text":"type, and artist, and 1 int attendees."},{"Start":"00:40.365 ","End":"00:44.083","Text":"In part 2 to print a prompt for each item of input,"},{"Start":"00:44.083 ","End":"00:46.310","Text":"for example, enter the date."},{"Start":"00:46.310 ","End":"00:47.870","Text":"Then in part 3,"},{"Start":"00:47.870 ","End":"00:53.090","Text":"we\u0027re told to use a scanner object and next to get a string from the keyboard or"},{"Start":"00:53.090 ","End":"00:59.870","Text":"next-int to get an integer and also to set the delimiter for scanner to new line."},{"Start":"00:59.870 ","End":"01:03.520","Text":"Then some example code is given to us."},{"Start":"01:03.520 ","End":"01:09.980","Text":"In part 4, we\u0027re told that the input for type determines what objects should be"},{"Start":"01:09.980 ","End":"01:17.335","Text":"created and stored in the array and can only be party, concert, or meeting."},{"Start":"01:17.335 ","End":"01:21.035","Text":"In part 5, we\u0027re told that for a type of concert,"},{"Start":"01:21.035 ","End":"01:23.825","Text":"you need to take input for the artist\u0027s name,"},{"Start":"01:23.825 ","End":"01:26.544","Text":"as well as all the other inputs."},{"Start":"01:26.544 ","End":"01:28.880","Text":"Then in part 6,"},{"Start":"01:28.880 ","End":"01:33.095","Text":"we\u0027re told to use a vent pointer as an index for the array."},{"Start":"01:33.095 ","End":"01:35.720","Text":"Part H is for testing."},{"Start":"01:35.720 ","End":"01:41.285","Text":"We test by adding an event of each type interactively using menu option two,"},{"Start":"01:41.285 ","End":"01:45.535","Text":"and then run menu option one to see the results."},{"Start":"01:45.535 ","End":"01:47.150","Text":"We\u0027ve been asked to in part E,"},{"Start":"01:47.150 ","End":"01:53.510","Text":"to implement the selector method or a selector method because we don\u0027t have one for date."},{"Start":"01:53.510 ","End":"01:56.600","Text":"We really should have select two methods for all of these things,"},{"Start":"01:56.600 ","End":"02:00.365","Text":"but we\u0027ve just been asked to do one for dates will do that."},{"Start":"02:00.365 ","End":"02:07.920","Text":"It\u0027s going to be public and it\u0027s going to return a string."},{"Start":"02:07.920 ","End":"02:11.525","Text":"It\u0027s going to be called Get date."},{"Start":"02:11.525 ","End":"02:19.455","Text":"No parameters and all it\u0027s going to do is return the date, field or attribute."},{"Start":"02:19.455 ","End":"02:21.595","Text":"That\u0027s that done,"},{"Start":"02:21.595 ","End":"02:23.625","Text":"so that\u0027s very simple,"},{"Start":"02:23.625 ","End":"02:31.550","Text":"part E. Then we\u0027ve also been asked to adapt the constructor for the concert class."},{"Start":"02:31.550 ","End":"02:33.910","Text":"Let\u0027s do that now."},{"Start":"02:33.910 ","End":"02:38.570","Text":"You recall we had problems previously because set artist"},{"Start":"02:38.570 ","End":"02:43.510","Text":"here had to be run separately when creating a concert event."},{"Start":"02:43.510 ","End":"02:48.710","Text":"Otherwise you get void printed in the summary here and we had"},{"Start":"02:48.710 ","End":"02:54.815","Text":"some issues around having to build a temporary object and so on in the previous exercise."},{"Start":"02:54.815 ","End":"02:59.555","Text":"Now we can get around that by improving the design of this object"},{"Start":"02:59.555 ","End":"03:05.015","Text":"so that it has a slightly different constructor than the other types of objects."},{"Start":"03:05.015 ","End":"03:06.680","Text":"For this type of object,"},{"Start":"03:06.680 ","End":"03:12.105","Text":"what we need to do is we need to add an extra argument,"},{"Start":"03:12.105 ","End":"03:14.580","Text":"which is going to be the artist."},{"Start":"03:14.580 ","End":"03:17.985","Text":"Then in the constructor,"},{"Start":"03:17.985 ","End":"03:20.470","Text":"we pass to the super-class,"},{"Start":"03:20.470 ","End":"03:23.525","Text":"the date of the event, the name of the event, attendees."},{"Start":"03:23.525 ","End":"03:27.905","Text":"But then here we can run set artist"},{"Start":"03:27.905 ","End":"03:34.265","Text":"to set the artist to whatever it is that was passed in."},{"Start":"03:34.265 ","End":"03:38.010","Text":"Quick change, but it means that we\u0027ve avoided some problems"},{"Start":"03:38.010 ","End":"03:41.090","Text":"and often you will find this with your class designs."},{"Start":"03:41.090 ","End":"03:42.230","Text":"If they\u0027re not quite right,"},{"Start":"03:42.230 ","End":"03:46.160","Text":"they\u0027re going to cause you some issue somewhere down the line so they need tweaking."},{"Start":"03:46.160 ","End":"03:49.985","Text":"What we\u0027ve done to this particular class is we\u0027ve added"},{"Start":"03:49.985 ","End":"03:57.200","Text":"an extra attribute to the constructor so we can set artist correctly."},{"Start":"03:57.200 ","End":"03:59.705","Text":"That\u0027s Part E and F done,"},{"Start":"03:59.705 ","End":"04:04.640","Text":"and they\u0027re just a prelude really to being able to do part G. We\u0027re"},{"Start":"04:04.640 ","End":"04:10.480","Text":"going to have a new method now in event manager called book event."},{"Start":"04:10.480 ","End":"04:14.480","Text":"We\u0027ve done the menu and we\u0027ve done the display options,"},{"Start":"04:14.480 ","End":"04:17.720","Text":"which prints the available options in the menu."},{"Start":"04:17.720 ","End":"04:23.580","Text":"Then what we need to do then is to do this method called book event."},{"Start":"04:24.290 ","End":"04:32.280","Text":"It\u0027s going to be public void and it\u0027s called book event."},{"Start":"04:32.280 ","End":"04:35.945","Text":"What do we need to do in book event?"},{"Start":"04:35.945 ","End":"04:38.030","Text":"We need to do quite a few things."},{"Start":"04:38.030 ","End":"04:41.650","Text":"Actually, we can use keyboard input here."},{"Start":"04:41.650 ","End":"04:46.760","Text":"What we\u0027ve got to do is to declare some string variables,"},{"Start":"04:46.760 ","End":"04:52.940","Text":"first of all, and part one and that is four string variables."},{"Start":"04:52.940 ","End":"04:56.150","Text":"We\u0027re going to hold temporarily"},{"Start":"04:56.150 ","End":"05:00.905","Text":"all these things as we get them from the keyboard and declare that on one line."},{"Start":"05:00.905 ","End":"05:08.990","Text":"Then there\u0027s an int variable called attendees and these don\u0027t need to persist."},{"Start":"05:08.990 ","End":"05:12.050","Text":"These are just local to this book event thing,"},{"Start":"05:12.050 ","End":"05:16.470","Text":"so they won\u0027t be seen outside this method."},{"Start":"05:16.820 ","End":"05:18.875","Text":"What we need to do now,"},{"Start":"05:18.875 ","End":"05:23.360","Text":"then is to print a prompt for each item of input."},{"Start":"05:23.360 ","End":"05:27.790","Text":"For example, date for enter the date for date."},{"Start":"05:27.790 ","End":"05:33.680","Text":"We then need to use the scanner object and it\u0027s next or"},{"Start":"05:33.680 ","End":"05:39.330","Text":"next int methods to get the actual input from the user."},{"Start":"05:39.330 ","End":"05:40.640","Text":"Let\u0027s do that first."},{"Start":"05:40.640 ","End":"05:46.115","Text":"Let\u0027s set up our scanner object and we\u0027re helpfully given some code here."},{"Start":"05:46.115 ","End":"05:53.135","Text":"Scanner, we call it keyboard, cause new scanner."},{"Start":"05:53.135 ","End":"05:59.070","Text":"This previously in the menu option part."},{"Start":"05:59.070 ","End":"06:02.282","Text":"Now we\u0027ve got our keyboard,"},{"Start":"06:02.282 ","End":"06:04.475","Text":"w e can read stuff from it,"},{"Start":"06:04.475 ","End":"06:11.490","Text":"but we\u0027ve been told to use delimiter method."},{"Start":"06:11.490 ","End":"06:18.275","Text":"Here, what this does is it sets up the delimiter character which says when something,"},{"Start":"06:18.275 ","End":"06:20.045","Text":"a token has been read."},{"Start":"06:20.045 ","End":"06:22.370","Text":"If you don\u0027t do that by default,"},{"Start":"06:22.370 ","End":"06:24.214","Text":"when it encounters a space,"},{"Start":"06:24.214 ","End":"06:28.370","Text":"it thinks that it\u0027s going to get a new bit of input after the space."},{"Start":"06:28.370 ","End":"06:34.820","Text":"We\u0027re using the return key to signal that and if we hadn\u0027t done this, it would work."},{"Start":"06:34.820 ","End":"06:37.580","Text":"But only for one word input,"},{"Start":"06:37.580 ","End":"06:39.140","Text":"because anytime it sees a space,"},{"Start":"06:39.140 ","End":"06:41.300","Text":"it thinks that\u0027s a new piece of inputs."},{"Start":"06:41.300 ","End":"06:45.305","Text":"You get some strange if you leave the delimiter as a space."},{"Start":"06:45.305 ","End":"06:48.050","Text":"This will fix that problem."},{"Start":"06:48.050 ","End":"06:55.300","Text":"We can actually get the input into a variable now,"},{"Start":"06:55.340 ","End":"06:58.110","Text":"we\u0027ve already set up our variables."},{"Start":"06:58.110 ","End":"07:02.255","Text":"The first pit of data we will need is the date,"},{"Start":"07:02.255 ","End":"07:07.955","Text":"so let\u0027s ask the user for that input."},{"Start":"07:07.955 ","End":"07:10.270","Text":"That\u0027s will print a prompt."},{"Start":"07:10.270 ","End":"07:12.340","Text":"I\u0027ll just put a slash in front of that,"},{"Start":"07:12.340 ","End":"07:15.280","Text":"so breaks onto a new line before it prints it."},{"Start":"07:15.280 ","End":"07:18.820","Text":"Now I\u0027m going to actually get the input into date,"},{"Start":"07:18.820 ","End":"07:27.910","Text":"the variable I\u0027ve just set up earlier by using keyboard.next that reads the next token,"},{"Start":"07:27.910 ","End":"07:34.214","Text":"which will be everything up to when the \"Enter\" key was pressed into date."},{"Start":"07:34.214 ","End":"07:36.837","Text":"That part done,"},{"Start":"07:36.837 ","End":"07:39.805","Text":"then I want to do the same thing,"},{"Start":"07:39.805 ","End":"07:45.148","Text":"but I want to get the person who\u0027s booking the event."},{"Start":"07:45.148 ","End":"07:48.070","Text":"Changed the prompt to event booker and we\u0027re going to"},{"Start":"07:48.070 ","End":"07:52.367","Text":"store that piece of data into the variable booker."},{"Start":"07:52.367 ","End":"07:53.725","Text":"We\u0027ve got the date,"},{"Start":"07:53.725 ","End":"07:55.330","Text":"we got the name of the booker."},{"Start":"07:55.330 ","End":"07:59.725","Text":"Then we can repeat this again for attendees,"},{"Start":"07:59.725 ","End":"08:03.250","Text":"except this is going to be an integer."},{"Start":"08:03.250 ","End":"08:08.544","Text":"If we wanted, we could take the input as a string and convert it to a integer,"},{"Start":"08:08.544 ","End":"08:13.045","Text":"but it\u0027s easier just to use the nextInt method."},{"Start":"08:13.045 ","End":"08:16.870","Text":"It\u0027s going to do that automatically for us,"},{"Start":"08:16.870 ","End":"08:20.200","Text":"and then we store that into attendees."},{"Start":"08:20.200 ","End":"08:23.995","Text":"That\u0027s attendees input as well."},{"Start":"08:23.995 ","End":"08:28.870","Text":"Now, the next part is going to be trickier because we want to ask"},{"Start":"08:28.870 ","End":"08:34.300","Text":"what type of event is going to be created."},{"Start":"08:34.300 ","End":"08:36.249","Text":"Is it a party, is it a concert,"},{"Start":"08:36.249 ","End":"08:37.375","Text":"or is it a meeting?"},{"Start":"08:37.375 ","End":"08:40.000","Text":"We need to prompt the user for that, type the input,"},{"Start":"08:40.000 ","End":"08:44.155","Text":"and then do different things depending on what you are typing."},{"Start":"08:44.155 ","End":"08:48.070","Text":"Let\u0027s ask them first of all, what\u0027s the event,"},{"Start":"08:48.070 ","End":"08:53.725","Text":"a party, and then we get that into type."},{"Start":"08:53.725 ","End":"08:57.190","Text":"That\u0027s why we have the type variable there declared at the top."},{"Start":"08:57.190 ","End":"08:59.395","Text":"Let\u0027s get that from the keyboard."},{"Start":"08:59.395 ","End":"09:03.790","Text":"Now what we need to do is we need an if/else structure."},{"Start":"09:03.790 ","End":"09:06.340","Text":"We could do this as a switch structure as well."},{"Start":"09:06.340 ","End":"09:09.380","Text":"I\u0027ll keep it simple and just keep it as an if/else and else/if."},{"Start":"09:13.170 ","End":"09:16.990","Text":"Remember we\u0027re not allowed to use double equals for strings,"},{"Start":"09:16.990 ","End":"09:18.715","Text":"so we use the equals method."},{"Start":"09:18.715 ","End":"09:21.714","Text":"If the type equals party,"},{"Start":"09:21.714 ","End":"09:23.125","Text":"then we do one thing."},{"Start":"09:23.125 ","End":"09:27.505","Text":"Otherwise, we check if the type equals,"},{"Start":"09:27.505 ","End":"09:29.620","Text":"let\u0027s say meeting actually."},{"Start":"09:29.620 ","End":"09:31.150","Text":"It\u0027s slightly out of order,"},{"Start":"09:31.150 ","End":"09:33.280","Text":"but it would do the same thing."},{"Start":"09:33.280 ","End":"09:37.300","Text":"Then the final one is if the type equals"},{"Start":"09:37.300 ","End":"09:38.650","Text":"a concept because we need"},{"Start":"09:38.650 ","End":"09:42.280","Text":"some more input for that one that it\u0027s a different thing again,"},{"Start":"09:42.280 ","End":"09:45.700","Text":"but all this follows a very similar format."},{"Start":"09:45.700 ","End":"09:49.465","Text":"We\u0027ve got our blocks set out."},{"Start":"09:49.465 ","End":"09:52.225","Text":"If the type equals a party,"},{"Start":"09:52.225 ","End":"09:54.190","Text":"what we want to do is we want to create"},{"Start":"09:54.190 ","End":"09:59.305","Text":"a new event of type party and store into the array."},{"Start":"09:59.305 ","End":"10:02.005","Text":"The array is called events."},{"Start":"10:02.005 ","End":"10:07.825","Text":"Where we going to store it is the location pointed to by the event pointer."},{"Start":"10:07.825 ","End":"10:11.365","Text":"It starts at zero and will proceed up to the end of the array."},{"Start":"10:11.365 ","End":"10:13.499","Text":"That part done,"},{"Start":"10:13.499 ","End":"10:19.105","Text":"and now we need to instantiate an object and store it into that location in the array."},{"Start":"10:19.105 ","End":"10:23.470","Text":"We use the new keyword to do that because this is a party,"},{"Start":"10:23.470 ","End":"10:28.105","Text":"we\u0027re going to create a party object and store it at that location."},{"Start":"10:28.105 ","End":"10:34.780","Text":"We obviously pass on the details that we\u0027ve collected from the keyboard."},{"Start":"10:34.780 ","End":"10:37.645","Text":"That does the job."},{"Start":"10:37.645 ","End":"10:40.660","Text":"What we also need to do is to increment"},{"Start":"10:40.660 ","End":"10:46.870","Text":"the event pointer so that we don\u0027t keep overwriting the same location all the time."},{"Start":"10:46.870 ","End":"10:50.140","Text":"That is what we need to do for a party."},{"Start":"10:50.140 ","End":"10:53.380","Text":"It\u0027s exactly the same for a meeting,"},{"Start":"10:53.380 ","End":"10:55.649","Text":"so we\u0027ll just copy and paste that in there,"},{"Start":"10:55.649 ","End":"11:01.038","Text":"except obviously, we\u0027re not creating an object of type party,"},{"Start":"11:01.038 ","End":"11:04.089","Text":"we\u0027re creating an object of type meeting."},{"Start":"11:04.089 ","End":"11:07.360","Text":"Date booker and attendees\u0027 information is passed on"},{"Start":"11:07.360 ","End":"11:10.536","Text":"to the constructor for our meeting and"},{"Start":"11:10.536 ","End":"11:18.865","Text":"meeting objects have different functionality than party objects as do concerts."},{"Start":"11:18.865 ","End":"11:21.310","Text":"I will copy this in,"},{"Start":"11:21.310 ","End":"11:26.875","Text":"but I\u0027ve got some extra information to collect for a concert."},{"Start":"11:26.875 ","End":"11:30.505","Text":"Concert does have these attributes,"},{"Start":"11:30.505 ","End":"11:32.905","Text":"but it has an extra one."},{"Start":"11:32.905 ","End":"11:34.540","Text":"Now, you notice,"},{"Start":"11:34.540 ","End":"11:38.130","Text":"remember we changed the constructor in"},{"Start":"11:38.130 ","End":"11:42.306","Text":"part F to accept an extra argument, which is artist."},{"Start":"11:42.306 ","End":"11:47.130","Text":"But I only need to get an artist if I\u0027m creating an event of type concert,"},{"Start":"11:47.130 ","End":"11:50.644","Text":"so I also need to get that from the keyboard."},{"Start":"11:50.644 ","End":"11:55.540","Text":"I\u0027ll say artist equals keyboard.next,"},{"Start":"11:55.540 ","End":"11:58.120","Text":"and it will get that information."},{"Start":"11:58.120 ","End":"12:03.850","Text":"However, I also need to tell the user what to type in."},{"Start":"12:03.850 ","End":"12:06.310","Text":"Otherwise, it would just be presented with"},{"Start":"12:06.310 ","End":"12:09.220","Text":"a prompt and the user wouldn\u0027t know what to type in."},{"Start":"12:09.220 ","End":"12:17.485","Text":"Enter name of artist and that sorts that out."},{"Start":"12:17.485 ","End":"12:23.455","Text":"We have done what we need to do for booking the events,"},{"Start":"12:23.455 ","End":"12:27.775","Text":"so let\u0027s see if it works."},{"Start":"12:27.775 ","End":"12:31.630","Text":"Let\u0027s compile all this."},{"Start":"12:31.630 ","End":"12:33.100","Text":"I got a few issues here,"},{"Start":"12:33.100 ","End":"12:34.585","Text":"so let\u0027s see what\u0027s going on."},{"Start":"12:34.585 ","End":"12:36.430","Text":"Of course, I missed a bracket there."},{"Start":"12:36.430 ","End":"12:39.850","Text":"It\u0027ll be the same for these other ones because"},{"Start":"12:39.850 ","End":"12:44.230","Text":"there\u0027s the bracket for equals and there\u0027s the bracket for the if."},{"Start":"12:44.230 ","End":"12:47.860","Text":"That\u0027s done."},{"Start":"12:47.860 ","End":"12:52.263","Text":"Now, event pointer, it cannot find."},{"Start":"12:52.263 ","End":"12:54.550","Text":"Did I create an event pointer?"},{"Start":"12:54.550 ","End":"12:57.295","Text":"I did, but I use a E,"},{"Start":"12:57.295 ","End":"13:00.835","Text":"so that\u0027s why that is causing a problem."},{"Start":"13:00.835 ","End":"13:05.920","Text":"Now that\u0027s compiled and there\u0027s no errors for that."},{"Start":"13:05.920 ","End":"13:09.055","Text":"Let\u0027s see if that works."},{"Start":"13:09.055 ","End":"13:14.785","Text":"If I compile everything just to check that everything is being rebuilt,"},{"Start":"13:14.785 ","End":"13:17.680","Text":"I can go ahead and run the program."},{"Start":"13:17.680 ","End":"13:22.150","Text":"I get the events and I get prompt here."},{"Start":"13:22.150 ","End":"13:24.100","Text":"I\u0027m being requested to type something in,"},{"Start":"13:24.100 ","End":"13:26.785","Text":"so I\u0027m going to use 2 to bookevent."},{"Start":"13:26.785 ","End":"13:29.005","Text":"One thing I haven\u0027t done,"},{"Start":"13:29.005 ","End":"13:30.625","Text":"so let\u0027s just stop that,"},{"Start":"13:30.625 ","End":"13:32.710","Text":"is I haven\u0027t added to"},{"Start":"13:32.710 ","End":"13:41.860","Text":"the menu options code the bit that will actually run menu book event."},{"Start":"13:41.860 ","End":"13:45.370","Text":"Let\u0027s add this here."},{"Start":"13:45.370 ","End":"13:48.445","Text":"If choice = 2,"},{"Start":"13:48.445 ","End":"13:50.500","Text":"then I run bookevent,"},{"Start":"13:50.500 ","End":"13:53.845","Text":"so make sure we do that."},{"Start":"13:53.845 ","End":"13:59.770","Text":"Otherwise, it\u0027s not going to run as we want it to. Let\u0027s try that again."},{"Start":"13:59.770 ","End":"14:02.200","Text":"Now let\u0027s try option 2,"},{"Start":"14:02.200 ","End":"14:04.660","Text":"and it\u0027s now prompting me for the date."},{"Start":"14:04.660 ","End":"14:08.365","Text":"Let\u0027s type in any old date,"},{"Start":"14:08.365 ","End":"14:12.145","Text":"the name of the booker,"},{"Start":"14:12.145 ","End":"14:16.945","Text":"and attendees, and say it\u0027s a party."},{"Start":"14:16.945 ","End":"14:20.150","Text":"It\u0027s going to take me back to the menu, so it looks like it\u0027s done."},{"Start":"14:20.150 ","End":"14:23.075","Text":"Sometimes if I list the booked events,"},{"Start":"14:23.075 ","End":"14:27.260","Text":"I can see that the party has been printed."},{"Start":"14:27.260 ","End":"14:29.105","Text":"That looks like it\u0027s worked."},{"Start":"14:29.105 ","End":"14:30.740","Text":"A little issue here with the menu."},{"Start":"14:30.740 ","End":"14:35.885","Text":"I should probably print a blank line before I print the menu. That\u0027s fine. It was well."},{"Start":"14:35.885 ","End":"14:38.600","Text":"Let\u0027s add another one just to check. It wasn\u0027t a one off."},{"Start":"14:38.600 ","End":"14:41.526","Text":"Let\u0027s try getting a concert, for example."},{"Start":"14:41.526 ","End":"14:45.210","Text":"Let\u0027s enter the date for the concert."},{"Start":"14:46.470 ","End":"14:50.605","Text":"The agent is going to book the concert."},{"Start":"14:50.605 ","End":"14:53.065","Text":"We have 500 people."},{"Start":"14:53.065 ","End":"14:56.482","Text":"It\u0027s a concert."},{"Start":"14:56.482 ","End":"14:59.470","Text":"We can have the artists we had before."},{"Start":"14:59.470 ","End":"15:02.545","Text":"It seems to be okay, no errors."},{"Start":"15:02.545 ","End":"15:04.390","Text":"Let\u0027s list the events again."},{"Start":"15:04.390 ","End":"15:08.725","Text":"You can see now that we\u0027ve got 2 events and"},{"Start":"15:08.725 ","End":"15:15.160","Text":"different behavior coming back from the summary method because this one says invited,"},{"Start":"15:15.160 ","End":"15:18.370","Text":"this one says capacity crowd,"},{"Start":"15:18.370 ","End":"15:22.257","Text":"this one says the word concert and has the artist\u0027s name in front of it as well."},{"Start":"15:22.257 ","End":"15:25.240","Text":"So you can see different behavior happening on these methods."},{"Start":"15:25.240 ","End":"15:27.655","Text":"There\u0027s some polymorphism happening there."},{"Start":"15:27.655 ","End":"15:33.255","Text":"We can also see that some of the inherited functionality works"},{"Start":"15:33.255 ","End":"15:40.320","Text":"absolutely fine because they\u0027re all subtypes of an event parent class."},{"Start":"15:40.320 ","End":"15:42.830","Text":"That\u0027s it for this one. Thanks for watching."},{"Start":"15:42.830 ","End":"15:46.200","Text":"See you for the next part of the exercise shortly."}],"ID":31111},{"Watched":false,"Name":"Exercise 6 Part 3","Duration":"12m 38s","ChapterTopicVideoID":29497,"CourseChapterTopicPlaylistID":294472,"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.330","Text":"Hello. Welcome back to this final part of the solution video."},{"Start":"00:03.330 ","End":"00:04.815","Text":"In part i,"},{"Start":"00:04.815 ","End":"00:07.890","Text":"we implement a new method called dateFree,"},{"Start":"00:07.890 ","End":"00:11.055","Text":"which accepts a string argument and returns a boolean."},{"Start":"00:11.055 ","End":"00:13.380","Text":"This method should loop through all the elements in the array,"},{"Start":"00:13.380 ","End":"00:15.990","Text":"checking the date attribute of each event and"},{"Start":"00:15.990 ","End":"00:19.005","Text":"that it doesn\u0027t equal the argument passed in."},{"Start":"00:19.005 ","End":"00:20.790","Text":"If no matching date is found,"},{"Start":"00:20.790 ","End":"00:22.305","Text":"it returns true;"},{"Start":"00:22.305 ","End":"00:24.660","Text":"otherwise, it returns false."},{"Start":"00:24.660 ","End":"00:27.510","Text":"We adapt the bookEvent method so that it only"},{"Start":"00:27.510 ","End":"00:30.900","Text":"allows booking to be made if the proposed date is free;"},{"Start":"00:30.900 ","End":"00:35.085","Text":"otherwise, it displays a message saying \"Date not available\"."},{"Start":"00:35.085 ","End":"00:40.470","Text":"In part k, we\u0027re told to implement the check attendance menu option to iterate through"},{"Start":"00:40.470 ","End":"00:46.205","Text":"all the events checking the attendees do not exceed the limit for the event type."},{"Start":"00:46.205 ","End":"00:49.745","Text":"A warning message is displayed if so,"},{"Start":"00:49.745 ","End":"00:52.640","Text":"followed by the details of the event."},{"Start":"00:52.640 ","End":"00:58.660","Text":"We output \"Attendance checks completed\" before the code exits."},{"Start":"00:58.660 ","End":"01:02.270","Text":"Then we test finally in part l, first of all,"},{"Start":"01:02.270 ","End":"01:05.888","Text":"with a party with arguments \"10/2/22\","},{"Start":"01:05.888 ","End":"01:08.510","Text":"\"Abigail\", and 150."},{"Start":"01:08.510 ","End":"01:12.095","Text":"Then a concert with arguments \"11/2/22\","},{"Start":"01:12.095 ","End":"01:15.049","Text":"\"A. Gent\", 500, and \"Codepunks\"."},{"Start":"01:15.049 ","End":"01:18.545","Text":"Then a meeting with arguments \"12/2/22\","},{"Start":"01:18.545 ","End":"01:21.575","Text":"\"Planning Committee\", 81."},{"Start":"01:21.575 ","End":"01:25.340","Text":"Then a meeting with arguments \"12/2/22\","},{"Start":"01:25.340 ","End":"01:27.455","Text":"\"Clash\", and 20,"},{"Start":"01:27.455 ","End":"01:30.935","Text":"which should fail because of the date."},{"Start":"01:30.935 ","End":"01:34.925","Text":"We finally then run the check attendees menu option,"},{"Start":"01:34.925 ","End":"01:41.445","Text":"which should highlight the third event as exceeding the attendee limit for meetings."},{"Start":"01:41.445 ","End":"01:43.995","Text":"Let\u0027s implement dateFree then."},{"Start":"01:43.995 ","End":"01:47.685","Text":"What we need to return is a boolean,"},{"Start":"01:47.685 ","End":"01:51.500","Text":"and we need to pass it a string which"},{"Start":"01:51.500 ","End":"01:56.300","Text":"contains the date that we\u0027re looking to check against,"},{"Start":"01:56.300 ","End":"01:58.205","Text":"see if that date is free."},{"Start":"01:58.205 ","End":"02:05.055","Text":"The code is going to be very similar to what we\u0027ve already had for listing the events."},{"Start":"02:05.055 ","End":"02:09.950","Text":"We could actually steal that code and adapt it."},{"Start":"02:09.950 ","End":"02:14.255","Text":"Let\u0027s have a look at the top here where we list the events and"},{"Start":"02:14.255 ","End":"02:19.445","Text":"just take that service a bit of typing and we\u0027ll adapt as we need to."},{"Start":"02:19.445 ","End":"02:21.785","Text":"We do need an index variable."},{"Start":"02:21.785 ","End":"02:25.250","Text":"We don\u0027t need to print out a message saying events,"},{"Start":"02:25.250 ","End":"02:30.250","Text":"but we do need to keep checking through the array whilst we"},{"Start":"02:30.250 ","End":"02:36.125","Text":"haven\u0027t got to the end or we haven\u0027t hit a null element."},{"Start":"02:36.125 ","End":"02:40.414","Text":"We\u0027re not looking to print a summary."},{"Start":"02:40.414 ","End":"02:47.345","Text":"What we are looking to do though is we\u0027re supposed to check whether getDate"},{"Start":"02:47.345 ","End":"02:54.620","Text":"returns a date which is equal to the one that has been passed into this method."},{"Start":"02:54.620 ","End":"02:56.390","Text":"We\u0027re going to say,"},{"Start":"02:56.390 ","End":"03:02.690","Text":"if date.equals and then"},{"Start":"03:02.690 ","End":"03:07.625","Text":"we\u0027re going to pass through it events i and getDate method."},{"Start":"03:07.625 ","End":"03:10.790","Text":"If those things are true,"},{"Start":"03:10.790 ","End":"03:12.650","Text":"if it does equal,"},{"Start":"03:12.650 ","End":"03:20.880","Text":"then we\u0027re going to return false because the date is not free."},{"Start":"03:21.410 ","End":"03:23.885","Text":"At the end here,"},{"Start":"03:23.885 ","End":"03:25.370","Text":"outside the while loop,"},{"Start":"03:25.370 ","End":"03:33.635","Text":"we got to the end without finding a date that matches the one that\u0027s stored in date,"},{"Start":"03:33.635 ","End":"03:37.130","Text":"then we can return true because the data is free."},{"Start":"03:37.130 ","End":"03:40.460","Text":"We iterate through each element in the array until we get to"},{"Start":"03:40.460 ","End":"03:43.635","Text":"the end or we hit a null object,"},{"Start":"03:43.635 ","End":"03:45.260","Text":"and then it checks that"},{"Start":"03:45.260 ","End":"03:53.870","Text":"the date of"},{"Start":"03:53.870 ","End":"03:55.955","Text":"the current object we\u0027re looking at."},{"Start":"03:55.955 ","End":"03:59.030","Text":"Then if it does find that they are equal,"},{"Start":"03:59.030 ","End":"04:03.440","Text":"then there\u0027s been a clash of dates and we return false because the date is not free."},{"Start":"04:03.440 ","End":"04:06.200","Text":"Otherwise, we get to the end and we never found one,"},{"Start":"04:06.200 ","End":"04:09.085","Text":"we can return true."},{"Start":"04:09.085 ","End":"04:11.115","Text":"That seems to compile okay."},{"Start":"04:11.115 ","End":"04:12.950","Text":"We\u0027ll check that out in a moment,"},{"Start":"04:12.950 ","End":"04:14.735","Text":"see if it works."},{"Start":"04:14.735 ","End":"04:20.120","Text":"Now what we want to do is we want to adapt the book event methods so it"},{"Start":"04:20.120 ","End":"04:26.405","Text":"uses dateFree and it only allows a booking to be made if the day is free."},{"Start":"04:26.405 ","End":"04:28.775","Text":"That should be straightforward enough."},{"Start":"04:28.775 ","End":"04:31.655","Text":"When we get the date up here,"},{"Start":"04:31.655 ","End":"04:35.360","Text":"we need to run dateFree on it."},{"Start":"04:35.360 ","End":"04:39.530","Text":"If dateFree, I\u0027ll pass it,"},{"Start":"04:39.530 ","End":"04:42.200","Text":"the date that was just typed in at the keyboard."},{"Start":"04:42.200 ","End":"04:43.670","Text":"If the date is free,"},{"Start":"04:43.670 ","End":"04:46.310","Text":"we\u0027ll get true; otherwise, we\u0027ll get false."},{"Start":"04:46.310 ","End":"04:51.020","Text":"If it is true, we want to run all of the rest of this code."},{"Start":"04:51.020 ","End":"04:56.930","Text":"Now my indents are going to be out because I just put this if in."},{"Start":"04:56.930 ","End":"05:02.914","Text":"That\u0027s a cool feature in BlueJ to indent everything that you\u0027ve highlighted."},{"Start":"05:02.914 ","End":"05:05.975","Text":"F6 or you can go up to that menu option there."},{"Start":"05:05.975 ","End":"05:11.630","Text":"Now this code only runs if we know that the date is free."},{"Start":"05:11.630 ","End":"05:13.855","Text":"If the date isn\u0027t free,"},{"Start":"05:13.855 ","End":"05:17.415","Text":"it will drop out to the end here."},{"Start":"05:17.415 ","End":"05:19.905","Text":"I\u0027ll just line up my brackets."},{"Start":"05:19.905 ","End":"05:22.010","Text":"If the date wasn\u0027t free,"},{"Start":"05:22.010 ","End":"05:27.030","Text":"then I want to print a message saying something to that effect."},{"Start":"05:27.030 ","End":"05:30.025","Text":"We\u0027ve told the user that we haven\u0027t made the booking,"},{"Start":"05:30.025 ","End":"05:32.590","Text":"tell the user that that date\u0027s already been used."},{"Start":"05:32.590 ","End":"05:35.295","Text":"That\u0027s part j done."},{"Start":"05:35.295 ","End":"05:38.270","Text":"Just check that it compiles, looks like it does."},{"Start":"05:38.270 ","End":"05:43.550","Text":"Now the last thing to do is to implement the check attendance menu option."},{"Start":"05:43.550 ","End":"05:44.990","Text":"We go through all the events,"},{"Start":"05:44.990 ","End":"05:50.605","Text":"checking the attendees do not exceed the limit for the event type. Let\u0027s do that."},{"Start":"05:50.605 ","End":"05:56.210","Text":"Again, we can use the method that we have here too."},{"Start":"05:56.210 ","End":"06:05.010","Text":"In fact, it\u0027d be better if I use the list events method and adapt to that."},{"Start":"06:05.010 ","End":"06:10.430","Text":"We\u0027ll use that as the basis of attendance check method."},{"Start":"06:10.430 ","End":"06:13.190","Text":"You don\u0027t want to print events at the beginning."},{"Start":"06:13.190 ","End":"06:15.460","Text":"This is going be slightly different."},{"Start":"06:15.460 ","End":"06:21.410","Text":"While we don\u0027t hit the end of the array or hit a null event,"},{"Start":"06:21.410 ","End":"06:27.275","Text":"we want to check and print only if there\u0027s a problem."},{"Start":"06:27.275 ","End":"06:31.820","Text":"We say, if events i and then we run"},{"Start":"06:31.820 ","End":"06:38.075","Text":"the attendees check method on the object of interest."},{"Start":"06:38.075 ","End":"06:43.485","Text":"If that returns a false,"},{"Start":"06:43.485 ","End":"06:48.545","Text":"we know we\u0027ve had a problem and we need to print a warning."},{"Start":"06:48.545 ","End":"06:50.210","Text":"This won\u0027t print a warning,"},{"Start":"06:50.210 ","End":"06:54.480","Text":"but it will print a summary."},{"Start":"06:54.480 ","End":"06:57.460","Text":"Before that, we should print the warning."},{"Start":"06:57.460 ","End":"07:01.290","Text":"We need to know what the warning is for."},{"Start":"07:02.180 ","End":"07:06.860","Text":"That then is attendance check implemented and"},{"Start":"07:06.860 ","End":"07:09.050","Text":"actually should at the end print"},{"Start":"07:09.050 ","End":"07:13.130","Text":"a message saying the attendance checks have been completed."},{"Start":"07:13.130 ","End":"07:14.930","Text":"Otherwise, we\u0027ll have the same issue as before"},{"Start":"07:14.930 ","End":"07:17.495","Text":"with events you don\u0027t know whether it\u0027s run or not."},{"Start":"07:17.495 ","End":"07:23.235","Text":"It should be println [inaudible] as well."},{"Start":"07:23.235 ","End":"07:26.544","Text":"We\u0027re ready to test."},{"Start":"07:26.544 ","End":"07:32.900","Text":"We\u0027ve obviously got to put this into the menu as well or it won\u0027t ever run anything."},{"Start":"07:32.900 ","End":"07:35.300","Text":"Let\u0027s make sure we do that as well."},{"Start":"07:35.300 ","End":"07:37.775","Text":"Let\u0027s extend what we got here."},{"Start":"07:37.775 ","End":"07:42.085","Text":"Obviously, we could have any number of option that you could add,"},{"Start":"07:42.085 ","End":"07:44.915","Text":"methods to check the staffing,"},{"Start":"07:44.915 ","End":"07:46.280","Text":"to delete events,"},{"Start":"07:46.280 ","End":"07:50.765","Text":"amend events, and so on."},{"Start":"07:50.765 ","End":"07:58.705","Text":"3 would run the attendance check."},{"Start":"07:58.705 ","End":"08:01.045","Text":"If we type 3,"},{"Start":"08:01.045 ","End":"08:04.205","Text":"it will run the attendance check method."},{"Start":"08:04.205 ","End":"08:11.300","Text":"Finally, contest everything with the test data that we\u0027ve been given in part"},{"Start":"08:11.300 ","End":"08:20.920","Text":"l. Let\u0027s put in a party and the date is going to be 10/2/22,"},{"Start":"08:20.920 ","End":"08:23.055","Text":"Abigail, and 150,"},{"Start":"08:23.055 ","End":"08:24.975","Text":"which is a party."},{"Start":"08:24.975 ","End":"08:28.760","Text":"That should be fine for a party, 150 are allowed."},{"Start":"08:28.760 ","End":"08:36.710","Text":"Then a concert on a different date, 11/2/22."},{"Start":"08:36.710 ","End":"08:39.680","Text":"Whoops, sorry, I didn\u0027t choose the right option there."},{"Start":"08:39.680 ","End":"08:44.315","Text":"Book a new event, 11/2/22."},{"Start":"08:44.315 ","End":"08:52.800","Text":"The booker is the A. Gent and the attendees is 500 and it\u0027s a concert."},{"Start":"08:52.800 ","End":"08:57.365","Text":"I\u0027ll be prompted for the name of the artist."},{"Start":"08:57.365 ","End":"09:00.150","Text":"That seems to have done something."},{"Start":"09:00.150 ","End":"09:03.270","Text":"Then part 3 meetings."},{"Start":"09:03.270 ","End":"09:07.420","Text":"I need to book a new event for a meeting."},{"Start":"09:07.580 ","End":"09:12.810","Text":"The date is the 12/2/22."},{"Start":"09:12.810 ","End":"09:14.630","Text":"We shouldn\u0027t have any clashes so far."},{"Start":"09:14.630 ","End":"09:16.180","Text":"We went one on the 10th, one on the 11th,"},{"Start":"09:16.180 ","End":"09:17.878","Text":"one on the 12th,"},{"Start":"09:17.878 ","End":"09:20.525","Text":"so we should have no problems there."},{"Start":"09:20.525 ","End":"09:23.570","Text":"Planning committee is 81,"},{"Start":"09:23.570 ","End":"09:27.210","Text":"so we know that\u0027s a problem for meetings."},{"Start":"09:27.910 ","End":"09:31.430","Text":"I\u0027m just going to list what I\u0027ve got so far just to check"},{"Start":"09:31.430 ","End":"09:34.280","Text":"that it\u0027s done what I\u0027ve expected, and it has."},{"Start":"09:34.280 ","End":"09:37.805","Text":"So party went in first and concert."},{"Start":"09:37.805 ","End":"09:41.060","Text":"It doesn\u0027t print the number of attendees for"},{"Start":"09:41.060 ","End":"09:44.960","Text":"planning committee meeting because it doesn\u0027t do it for meetings at all."},{"Start":"09:44.960 ","End":"09:51.945","Text":"That was a different behavior for a summary event method in meetings."},{"Start":"09:51.945 ","End":"09:58.430","Text":"The last thing to do then is to try and put in a meeting with a clashing date,"},{"Start":"09:58.430 ","End":"10:07.910","Text":"which is Number 4 here in part l. We\u0027re going to put in a new event option 2, 12/2."},{"Start":"10:07.910 ","End":"10:11.590","Text":"We know there\u0027s something going on then already."},{"Start":"10:11.590 ","End":"10:15.710","Text":"It should refuse to enter this and I won\u0027t go into the rest of the stuff."},{"Start":"10:15.710 ","End":"10:16.895","Text":"Yes. It does say,"},{"Start":"10:16.895 ","End":"10:21.250","Text":"\"Date already booked for another event\" and throws me back to the menu."},{"Start":"10:21.250 ","End":"10:23.050","Text":"That seems to work."},{"Start":"10:23.050 ","End":"10:29.810","Text":"A final check to do is to check whether the attendance limits have been broken."},{"Start":"10:29.810 ","End":"10:32.285","Text":"It does give me a warning"},{"Start":"10:32.285 ","End":"10:36.170","Text":"that there\u0027s an event that exceeds the attendance limit and it\u0027s"},{"Start":"10:36.170 ","End":"10:39.380","Text":"the planning committee meeting that it shows us a summary for"},{"Start":"10:39.380 ","End":"10:43.430","Text":"and then the message attendance check completed."},{"Start":"10:43.430 ","End":"10:48.830","Text":"If I run 1, I can see that those 3 events are the only ones booked at the moment,"},{"Start":"10:48.830 ","End":"10:51.080","Text":"and 9, I can exit."},{"Start":"10:51.080 ","End":"10:53.675","Text":"That\u0027s it for this exercise."},{"Start":"10:53.675 ","End":"10:56.010","Text":"There was an awful lot of steps to this,"},{"Start":"10:56.010 ","End":"11:00.290","Text":"but what we have shown is we have got the ability to"},{"Start":"11:00.290 ","End":"11:05.195","Text":"create objects of different types on-demand from the keyboard."},{"Start":"11:05.195 ","End":"11:07.670","Text":"There\u0027s no way of the compiler knowing what we\u0027re"},{"Start":"11:07.670 ","End":"11:11.060","Text":"going to enter at the keyboard before this program runs."},{"Start":"11:11.060 ","End":"11:15.600","Text":"It should really bring it home to you now that this is happening at run-time,"},{"Start":"11:15.600 ","End":"11:21.480","Text":"it\u0027s choosing which object to create at run-time."},{"Start":"11:21.480 ","End":"11:23.225","Text":"As you can see here,"},{"Start":"11:23.225 ","End":"11:26.240","Text":"we\u0027re creating either a party or a meeting or a concert."},{"Start":"11:26.240 ","End":"11:31.730","Text":"But the methods that are common to all subtypes can,"},{"Start":"11:31.730 ","End":"11:34.760","Text":"for example, get the date and check whether the date is free."},{"Start":"11:34.760 ","End":"11:39.830","Text":"It doesn\u0027t depend on whether it\u0027s a party or a meeting or a concert."},{"Start":"11:39.830 ","End":"11:43.130","Text":"It works exactly the same way for all events."},{"Start":"11:43.130 ","End":"11:46.865","Text":"Similarly, the attendees check works the same way for all the events."},{"Start":"11:46.865 ","End":"11:52.820","Text":"It correctly picks up the right method from that particular object that it\u0027s dealing"},{"Start":"11:52.820 ","End":"11:58.490","Text":"with at that instance in time in the events array and runs the correct methods."},{"Start":"11:58.490 ","End":"12:02.143","Text":"We\u0027re seeing polymorphism in the attendees checking and"},{"Start":"12:02.143 ","End":"12:06.380","Text":"we\u0027re seeing polymorphism in the booking of events."},{"Start":"12:06.380 ","End":"12:08.570","Text":"Even when we display events,"},{"Start":"12:08.570 ","End":"12:11.780","Text":"we\u0027re seeing polymorphism because we\u0027re displaying"},{"Start":"12:11.780 ","End":"12:16.014","Text":"a different summary depending on the type of event,"},{"Start":"12:16.014 ","End":"12:20.510","Text":"but they\u0027re all stored in an array of type event,"},{"Start":"12:20.510 ","End":"12:22.535","Text":"which was the parent class."},{"Start":"12:22.535 ","End":"12:25.715","Text":"Hopefully, that all makes sense and that was"},{"Start":"12:25.715 ","End":"12:28.640","Text":"building up to something relatively complex which you"},{"Start":"12:28.640 ","End":"12:34.655","Text":"can carry on developing if you\u0027d like to delete events or edit events and so on."},{"Start":"12:34.655 ","End":"12:38.310","Text":"Thanks very much for watching. I\u0027ll see you soon."}],"ID":31112}],"Thumbnail":null,"ID":294472},{"Name":"Collections","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"ArrayLists","Duration":"5m 59s","ChapterTopicVideoID":29546,"CourseChapterTopicPlaylistID":294562,"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.135","Text":"Hello, welcome to this video on collections."},{"Start":"00:03.135 ","End":"00:04.470","Text":"By the end of this section,"},{"Start":"00:04.470 ","End":"00:07.635","Text":"you\u0027ll be able to describe the purpose of collection classes,"},{"Start":"00:07.635 ","End":"00:11.625","Text":"explain the process of auto boxing and unboxing,"},{"Start":"00:11.625 ","End":"00:15.765","Text":"relate the concept of generics to collection classes,"},{"Start":"00:15.765 ","End":"00:21.310","Text":"and evaluate which collection class is appropriate to a given situation."},{"Start":"00:21.440 ","End":"00:25.040","Text":"We\u0027ve been using arrays for some time in the course now"},{"Start":"00:25.040 ","End":"00:28.085","Text":"and it may have occurred to you that they have some issues."},{"Start":"00:28.085 ","End":"00:33.905","Text":"For example, the size of the array needs to be known at compile time."},{"Start":"00:33.905 ","End":"00:37.055","Text":"Elements in the array could be empty,"},{"Start":"00:37.055 ","End":"00:40.060","Text":"therefore wasting memory space."},{"Start":"00:40.060 ","End":"00:48.425","Text":"Locating or removing data can be slow and the code to implement it could be complicated."},{"Start":"00:48.425 ","End":"00:52.429","Text":"Whilst it\u0027s important to know how arrays work in practice,"},{"Start":"00:52.429 ","End":"00:58.055","Text":"you might want to use other more convenient data structures to do similar jobs."},{"Start":"00:58.055 ","End":"01:04.100","Text":"Most modern object-oriented languages provide something called collection classes,"},{"Start":"01:04.100 ","End":"01:08.705","Text":"which allow us to work with collections of individual class instances."},{"Start":"01:08.705 ","End":"01:12.065","Text":"If we define a collection as a group of"},{"Start":"01:12.065 ","End":"01:16.246","Text":"individual objects gathered together as a single unit,"},{"Start":"01:16.246 ","End":"01:20.390","Text":"then a collection class is a template for"},{"Start":"01:20.390 ","End":"01:26.405","Text":"an abstract datatype which can be used to group individual objects into a single unit."},{"Start":"01:26.405 ","End":"01:33.890","Text":"In a nutshell, a collection is a container object used to store other objects."},{"Start":"01:33.890 ","End":"01:36.320","Text":"When using a collection class,"},{"Start":"01:36.320 ","End":"01:40.730","Text":"you don\u0027t need to know the details of how the underlying code for the collection works."},{"Start":"01:40.730 ","End":"01:45.488","Text":"You simply make use of the features of the collection through its provided methods,"},{"Start":"01:45.488 ","End":"01:50.360","Text":"hence the use of the word abstract in the definition here,"},{"Start":"01:50.360 ","End":"01:56.210","Text":"because we\u0027re hiding away unnecessary details of the implementation so the programmer can"},{"Start":"01:56.210 ","End":"02:02.450","Text":"just focus on making use of the collection class to perform whatever function is needed."},{"Start":"02:02.450 ","End":"02:08.795","Text":"An example of collection classes in Java would be an ArrayList,"},{"Start":"02:08.795 ","End":"02:12.080","Text":"a LinkedList, a Stack,"},{"Start":"02:12.080 ","End":"02:15.590","Text":"a HashMap, or a HashSet."},{"Start":"02:15.590 ","End":"02:18.215","Text":"We\u0027ll look a few of the most commonly used."},{"Start":"02:18.215 ","End":"02:20.950","Text":"Firstly, the ArrayList."},{"Start":"02:20.950 ","End":"02:24.130","Text":"An ArrayList can be used as an Array might,"},{"Start":"02:24.130 ","End":"02:26.585","Text":"but with more flexibility."},{"Start":"02:26.585 ","End":"02:29.430","Text":"Compare the declaration of an array of"},{"Start":"02:29.430 ","End":"02:37.220","Text":"strings with the declaration of an ArrayList containing strings."},{"Start":"02:37.220 ","End":"02:44.135","Text":"Notice how for the ArrayList we put the type between a less than and greater than symbol,"},{"Start":"02:44.135 ","End":"02:47.185","Text":"commonly called angle braces."},{"Start":"02:47.185 ","End":"02:51.695","Text":"Note also we don\u0027t need to specify a size in square brackets."},{"Start":"02:51.695 ","End":"02:54.380","Text":"In fact, there are no square brackets and"},{"Start":"02:54.380 ","End":"02:59.605","Text":"instead an empty pair of angle braces here known as a diamond."},{"Start":"02:59.605 ","End":"03:03.575","Text":"What we\u0027ve done in this second line is to create"},{"Start":"03:03.575 ","End":"03:07.445","Text":"an ArrayList data structure called words,"},{"Start":"03:07.445 ","End":"03:10.520","Text":"which will be used to store strings."},{"Start":"03:10.520 ","End":"03:15.260","Text":"It hasn\u0027t got a size specified because an ArrayList will grow as"},{"Start":"03:15.260 ","End":"03:20.060","Text":"items are added to it and can shrink as items are removed."},{"Start":"03:20.060 ","End":"03:23.630","Text":"Whereas the string array we declared in"},{"Start":"03:23.630 ","End":"03:29.615","Text":"this first line would be fixed in size at 10 elements."},{"Start":"03:29.615 ","End":"03:34.345","Text":"We can add elements to our ArrayList using the add method."},{"Start":"03:34.345 ","End":"03:40.165","Text":"The list grows automatically as we successively call add."},{"Start":"03:40.165 ","End":"03:43.015","Text":"An ArrayList method called size,"},{"Start":"03:43.015 ","End":"03:48.080","Text":"returns the current size of the list as an integer."},{"Start":"03:48.620 ","End":"03:51.665","Text":"Another method get,"},{"Start":"03:51.665 ","End":"03:56.845","Text":"returns the value stored at a particular index."},{"Start":"03:56.845 ","End":"04:02.695","Text":"Note how we\u0027re using round brackets here, not square brackets,"},{"Start":"04:02.695 ","End":"04:06.505","Text":"as we\u0027re making a method call and passing"},{"Start":"04:06.505 ","End":"04:12.980","Text":"3 as an argument to specify the index we\u0027re interested in."},{"Start":"04:12.980 ","End":"04:17.645","Text":"Note the datatype used here, string,"},{"Start":"04:17.645 ","End":"04:25.270","Text":"was carefully chosen for this initial example because it\u0027s an object, not a primitive."},{"Start":"04:25.270 ","End":"04:29.320","Text":"In fact, the syntax for the ArrayList will not allow"},{"Start":"04:29.320 ","End":"04:34.225","Text":"primitive datatypes such as ints or floats to be used here."},{"Start":"04:34.225 ","End":"04:40.720","Text":"Only objects, either ones created from classes defined in the language like string or"},{"Start":"04:40.720 ","End":"04:47.815","Text":"from our own user-defined classes can be contained in an ArrayList."},{"Start":"04:47.815 ","End":"04:54.445","Text":"Attempting to use primitive types in these braces will cause a compile-time error."},{"Start":"04:54.445 ","End":"05:01.280","Text":"Fortunately, there\u0027s a way around this which is to use, so-called wrapper classes."},{"Start":"05:01.280 ","End":"05:06.245","Text":"These allow us to send primitive values as if they were objects."},{"Start":"05:06.245 ","End":"05:09.816","Text":"For an int and char respectively,"},{"Start":"05:09.816 ","End":"05:15.650","Text":"the wrapper class is Integer and Character with a capital I and"},{"Start":"05:15.650 ","End":"05:22.445","Text":"capital C. The full list of Java wrapper classes is as follows."},{"Start":"05:22.445 ","End":"05:26.150","Text":"Handily, apart from int and char,"},{"Start":"05:26.150 ","End":"05:27.935","Text":"they have the same name,"},{"Start":"05:27.935 ","End":"05:34.160","Text":"but with a capital letter initially and that\u0027s"},{"Start":"05:34.160 ","End":"05:37.190","Text":"consistent with the convention that class names start with"},{"Start":"05:37.190 ","End":"05:40.910","Text":"a capital letter to easily distinguish them from objects,"},{"Start":"05:40.910 ","End":"05:43.040","Text":"or in this case from primitives."},{"Start":"05:43.040 ","End":"05:45.530","Text":"We\u0027ll pause there and in the next video,"},{"Start":"05:45.530 ","End":"05:48.290","Text":"we\u0027ll look at a mechanism for automatically converting"},{"Start":"05:48.290 ","End":"05:51.290","Text":"primitives into their wrapper classes and"},{"Start":"05:51.290 ","End":"05:57.260","Text":"for automatically retrieving primitive values from wrapper classes."},{"Start":"05:57.260 ","End":"06:00.300","Text":"Thanks for watching and see you soon."}],"ID":31158},{"Watched":false,"Name":"Autoboxing and Generic Types","Duration":"4m 31s","ChapterTopicVideoID":29547,"CourseChapterTopicPlaylistID":294562,"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.985","Text":"Hello, welcome back. In the previous video,"},{"Start":"00:02.985 ","End":"00:10.630","Text":"we saw there were wrapper classes for putting primitive values into objects."},{"Start":"00:10.640 ","End":"00:16.680","Text":"Java can automatically put primitives into the correct wrapper class."},{"Start":"00:16.680 ","End":"00:21.120","Text":"For example, suppose you create an ArrayList of characters."},{"Start":"00:21.120 ","End":"00:24.510","Text":"To add a single character to the list,"},{"Start":"00:24.510 ","End":"00:27.420","Text":"you should really create a new object of"},{"Start":"00:27.420 ","End":"00:33.060","Text":"type character and pass that to the add method of the ArrayList."},{"Start":"00:33.060 ","End":"00:36.765","Text":"But through the process of autoboxing,"},{"Start":"00:36.765 ","End":"00:42.920","Text":"just putting a primitive inside the brackets of the add method will"},{"Start":"00:42.920 ","End":"00:45.890","Text":"result in an automatic conversion from"},{"Start":"00:45.890 ","End":"00:49.610","Text":"the primitive type to the corresponding wrapper class."},{"Start":"00:49.610 ","End":"00:55.915","Text":"In this case, a char gets converted into a character object,"},{"Start":"00:55.915 ","End":"00:59.305","Text":"and that\u0027s what we mean by autoboxing."},{"Start":"00:59.305 ","End":"01:02.690","Text":"The reverse process is known as unboxing."},{"Start":"01:02.690 ","End":"01:07.790","Text":"We have a method here that requires an int argument."},{"Start":"01:07.790 ","End":"01:11.075","Text":"If the value passed in is an even number,"},{"Start":"01:11.075 ","End":"01:13.265","Text":"the method returns true."},{"Start":"01:13.265 ","End":"01:19.145","Text":"Here we declare an object which is an instance of the integer class,"},{"Start":"01:19.145 ","End":"01:23.695","Text":"and we store into the object the value 8."},{"Start":"01:23.695 ","End":"01:29.055","Text":"You\u0027d expect this method to call inside the print statements to produce an error,"},{"Start":"01:29.055 ","End":"01:32.960","Text":"because the isEven method is expecting to receive"},{"Start":"01:32.960 ","End":"01:39.499","Text":"an int and instead is being sent an object of type integer."},{"Start":"01:39.499 ","End":"01:43.235","Text":"But thanks to the unboxing process, there is no error,"},{"Start":"01:43.235 ","End":"01:46.430","Text":"and the value from inside the myInt object is"},{"Start":"01:46.430 ","End":"01:52.370","Text":"extracted and the resulting int is sent to the isEven method,"},{"Start":"01:52.370 ","End":"01:56.900","Text":"which happily accepts it and returns true given"},{"Start":"01:56.900 ","End":"02:01.760","Text":"that 8 was passed in and that is an even number."},{"Start":"02:01.760 ","End":"02:06.110","Text":"The syntax behind collections involves angle brackets,"},{"Start":"02:06.110 ","End":"02:08.165","Text":"which we haven\u0027t come across before,"},{"Start":"02:08.165 ","End":"02:10.535","Text":"so it\u0027s worth looking at this briefly."},{"Start":"02:10.535 ","End":"02:15.020","Text":"As we\u0027ve seen the text inside the angle brackets when creating an ArrayList"},{"Start":"02:15.020 ","End":"02:19.890","Text":"is being used to indicate the type of object we\u0027re storing in the ArrayList."},{"Start":"02:19.890 ","End":"02:25.655","Text":"We can also use the same mechanism to create our own classes that can deal with any type."},{"Start":"02:25.655 ","End":"02:27.500","Text":"When defining this class,"},{"Start":"02:27.500 ","End":"02:32.405","Text":"we\u0027re giving the type an identifier T inside angle bracket."},{"Start":"02:32.405 ","End":"02:36.860","Text":"We can now store a value of type T inside"},{"Start":"02:36.860 ","End":"02:41.870","Text":"the instance variable called item using a constructor."},{"Start":"02:41.870 ","End":"02:46.580","Text":"We can return the value of item using the get method."},{"Start":"02:46.580 ","End":"02:51.620","Text":"This mechanism enables us to define methods or classes that accept types as"},{"Start":"02:51.620 ","End":"02:54.590","Text":"a parameter alongside whatever data is being"},{"Start":"02:54.590 ","End":"02:58.160","Text":"passed into the method or class as a parameter."},{"Start":"02:58.160 ","End":"03:03.274","Text":"This means that the methods or classes can be made generic to all types."},{"Start":"03:03.274 ","End":"03:09.140","Text":"In fact, we call this feature in Java generic types or just generics."},{"Start":"03:09.140 ","End":"03:13.519","Text":"This feature has been provided so that when a collection class is defined,"},{"Start":"03:13.519 ","End":"03:17.060","Text":"it\u0027s possible to use with any type of object."},{"Start":"03:17.060 ","End":"03:22.385","Text":"Imagine if the developers of Java had to create 1 ArrayList that work with ints,"},{"Start":"03:22.385 ","End":"03:24.755","Text":"and a different one that worked with chars,"},{"Start":"03:24.755 ","End":"03:27.745","Text":"and a different one for floats, and so on."},{"Start":"03:27.745 ","End":"03:31.790","Text":"If instead the collection could work on all object types,"},{"Start":"03:31.790 ","End":"03:34.175","Text":"even user-defined ones,"},{"Start":"03:34.175 ","End":"03:39.530","Text":"but with a specific type being used to put into angle brackets,"},{"Start":"03:39.530 ","End":"03:42.470","Text":"that makes things a lot more manageable."},{"Start":"03:42.470 ","End":"03:45.350","Text":"This is exactly what they did when they implemented"},{"Start":"03:45.350 ","End":"03:49.460","Text":"collections to make use of generic types."},{"Start":"03:49.460 ","End":"03:52.430","Text":"To see some more examples of this in use,"},{"Start":"03:52.430 ","End":"03:57.110","Text":"this first ArrayList stores a collection of strings."},{"Start":"03:57.110 ","End":"04:01.535","Text":"The second one, a collection of vehicles."},{"Start":"04:01.535 ","End":"04:07.040","Text":"The mechanism by which we add an item to the collection is exactly the same,"},{"Start":"04:07.040 ","End":"04:09.200","Text":"whether we\u0027re adding a string to"},{"Start":"04:09.200 ","End":"04:14.225","Text":"the word collection or a vehicle to the vehicles collection,"},{"Start":"04:14.225 ","End":"04:18.140","Text":"because add is built to take any type."},{"Start":"04:18.140 ","End":"04:22.340","Text":"The mechanism by which we add an item to the collection is the same."},{"Start":"04:22.340 ","End":"04:23.765","Text":"We will again pause there,"},{"Start":"04:23.765 ","End":"04:25.025","Text":"and in the next video,"},{"Start":"04:25.025 ","End":"04:28.895","Text":"we\u0027ll discuss different ways of moving through the elements in a collection."},{"Start":"04:28.895 ","End":"04:31.950","Text":"Thank you very much for watching and see you soon."}],"ID":31159},{"Watched":false,"Name":"Iterating Through Collections","Duration":"3m 35s","ChapterTopicVideoID":29548,"CourseChapterTopicPlaylistID":294562,"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.330","Text":"Hello, welcome back. The whole point of"},{"Start":"00:03.330 ","End":"00:06.510","Text":"collections is to gather together a series of objects."},{"Start":"00:06.510 ","End":"00:09.960","Text":"It\u0027s natural to want to move through each object in the collection,"},{"Start":"00:09.960 ","End":"00:12.090","Text":"either retrieving details from it,"},{"Start":"00:12.090 ","End":"00:14.325","Text":"or doing some processing on it."},{"Start":"00:14.325 ","End":"00:18.510","Text":"The traditional form of for loop is used to change the value of"},{"Start":"00:18.510 ","End":"00:23.295","Text":"an integer variable across some fixed range of values in a predictable way."},{"Start":"00:23.295 ","End":"00:28.175","Text":"This integer value is often used as an index into an array,"},{"Start":"00:28.175 ","End":"00:32.330","Text":"so that successive elements of the array can be processed."},{"Start":"00:32.330 ","End":"00:37.670","Text":"We can also use this style of for loop with an ArrayLists get method."},{"Start":"00:37.670 ","End":"00:39.754","Text":"But in many modern languages,"},{"Start":"00:39.754 ","End":"00:45.920","Text":"there\u0027s also an enhanced for loop which is used to process elements in a collection."},{"Start":"00:45.920 ","End":"00:50.285","Text":"The syntax for such a loop in Java is like this."},{"Start":"00:50.285 ","End":"00:52.564","Text":"Each time around the loop,"},{"Start":"00:52.564 ","End":"00:57.680","Text":"the variable takes a new value on from the collection."},{"Start":"00:57.680 ","End":"01:02.480","Text":"If we had a collection of strings called somewords,"},{"Start":"01:02.480 ","End":"01:09.040","Text":"a string variable item takes on the value of the first item in the collection,"},{"Start":"01:09.040 ","End":"01:11.295","Text":"and then on the next time around the loop,"},{"Start":"01:11.295 ","End":"01:14.255","Text":"the second item and the third,"},{"Start":"01:14.255 ","End":"01:18.320","Text":"and so on, until we get to the end of the collection."},{"Start":"01:18.320 ","End":"01:22.175","Text":"The inside of the loop is where the processing can take place."},{"Start":"01:22.175 ","End":"01:24.755","Text":"In this case, all we do is print out the string."},{"Start":"01:24.755 ","End":"01:27.380","Text":"We don\u0027t need to specify the size of"},{"Start":"01:27.380 ","End":"01:30.650","Text":"the collection or increment variables or anything else,"},{"Start":"01:30.650 ","End":"01:32.585","Text":"it\u0027s all done automatically,"},{"Start":"01:32.585 ","End":"01:35.675","Text":"so the code is nice and compact."},{"Start":"01:35.675 ","End":"01:40.730","Text":"An alternative to using an enhanced for loop is an iterator,"},{"Start":"01:40.730 ","End":"01:46.915","Text":"an object whose functionality allows iteration over all the elements of a collection."},{"Start":"01:46.915 ","End":"01:50.225","Text":"The enhanced for loop is nice and simple,"},{"Start":"01:50.225 ","End":"01:51.950","Text":"but it has some restrictions,"},{"Start":"01:51.950 ","End":"01:54.365","Text":"so sometimes it may be necessary to use"},{"Start":"01:54.365 ","End":"01:59.885","Text":"an iterator object which is a more flexible way of iterating through collections."},{"Start":"01:59.885 ","End":"02:02.525","Text":"An iterator works with generic types,"},{"Start":"02:02.525 ","End":"02:08.094","Text":"so it can iterate over any type of object even custom ones that you\u0027ve created."},{"Start":"02:08.094 ","End":"02:11.930","Text":"Let\u0027s say we had a class called contact,"},{"Start":"02:11.930 ","End":"02:16.085","Text":"which stores the name and the email address of a contact for an address book,"},{"Start":"02:16.085 ","End":"02:24.780","Text":"we can store a series of contacts into a collection of type contact called addresses."},{"Start":"02:25.360 ","End":"02:29.510","Text":"We can create an iterator object now"},{"Start":"02:29.510 ","End":"02:34.145","Text":"using the iterator method against the identifier for the collection,"},{"Start":"02:34.145 ","End":"02:36.230","Text":"in this case, addresses."},{"Start":"02:36.230 ","End":"02:40.130","Text":"Note there\u0027s no new keyword here."},{"Start":"02:40.130 ","End":"02:44.725","Text":"Now we can make use of that iterator instance iter,"},{"Start":"02:44.725 ","End":"02:51.750","Text":"to first of all, see if there\u0027s any more objects in the collection using hasNext."},{"Start":"02:51.760 ","End":"02:55.970","Text":"We can then get the next item from the collection and"},{"Start":"02:55.970 ","End":"03:00.210","Text":"put it into a variable so we can do something with it."},{"Start":"03:01.300 ","End":"03:06.290","Text":"The processing might involve inspecting the contents of the current element in"},{"Start":"03:06.290 ","End":"03:11.600","Text":"the collection or using 1 of its values in a correct calculation,"},{"Start":"03:11.600 ","End":"03:15.815","Text":"but 1 thing that an iterator can do which an enhanced for loop cannot,"},{"Start":"03:15.815 ","End":"03:18.500","Text":"is to remove elements from the collection."},{"Start":"03:18.500 ","End":"03:22.465","Text":"If you need to remove elements from a collection whilst they\u0027re iterating,"},{"Start":"03:22.465 ","End":"03:27.000","Text":"you must use an iterator not an enhanced for loop."},{"Start":"03:27.000 ","End":"03:30.020","Text":"We\u0027ll pause there once again and in the next video,"},{"Start":"03:30.020 ","End":"03:33.575","Text":"we\u0027ll look at another commonly used collection, the HashMap."},{"Start":"03:33.575 ","End":"03:35.910","Text":"Thanks for watching and see you then."}],"ID":31160},{"Watched":false,"Name":"HashMaps","Duration":"5m 17s","ChapterTopicVideoID":29549,"CourseChapterTopicPlaylistID":294562,"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.750","Text":"Hello again. Another common abstract datatype"},{"Start":"00:03.750 ","End":"00:07.680","Text":"that\u0027s provided as a collection class is a HashMap."},{"Start":"00:07.680 ","End":"00:12.870","Text":"A map is a collection that\u0027s comprised of key value pairs."},{"Start":"00:12.870 ","End":"00:17.835","Text":"The values are accessed by using the key as a locator."},{"Start":"00:17.835 ","End":"00:22.880","Text":"A HashMap is a specific implementation of a map that"},{"Start":"00:22.880 ","End":"00:28.850","Text":"uses a hash code to determine the storage location for values."},{"Start":"00:28.850 ","End":"00:32.180","Text":"Maps differ from lists in a couple of ways."},{"Start":"00:32.180 ","End":"00:34.520","Text":"Firstly, lists are ordered, i.e.,"},{"Start":"00:34.520 ","End":"00:37.010","Text":"put the things into the list one after the other,"},{"Start":"00:37.010 ","End":"00:38.990","Text":"and there\u0027s a beginning and an end."},{"Start":"00:38.990 ","End":"00:43.880","Text":"You generally process list items in the order that they were stored."},{"Start":"00:43.880 ","End":"00:47.285","Text":"There is no particular order in a map."},{"Start":"00:47.285 ","End":"00:50.045","Text":"The second difference is that with a map,"},{"Start":"00:50.045 ","End":"00:53.060","Text":"you\u0027re storing 2 things into the collection,"},{"Start":"00:53.060 ","End":"00:56.165","Text":"a key and a value."},{"Start":"00:56.165 ","End":"01:01.210","Text":"The value is the thing that you want to store and"},{"Start":"01:01.210 ","End":"01:07.540","Text":"a unique key is associated with each value so you can locate it."},{"Start":"01:07.540 ","End":"01:12.040","Text":"You can think of the key as a word in an index of"},{"Start":"01:12.040 ","End":"01:17.050","Text":"a book and the value is the page with the content that you\u0027re looking for."},{"Start":"01:17.050 ","End":"01:20.455","Text":"In fact, a map is also sometimes called a dictionary,"},{"Start":"01:20.455 ","End":"01:23.830","Text":"which makes sense as you\u0027re looking up words in a dictionary so"},{"Start":"01:23.830 ","End":"01:28.810","Text":"the word is the key and the definition of the word is the value."},{"Start":"01:28.810 ","End":"01:32.740","Text":"If we stored a bunch of computer science acronyms as"},{"Start":"01:32.740 ","End":"01:37.840","Text":"keys and then the expanded form of the acronym as the value,"},{"Start":"01:37.840 ","End":"01:42.950","Text":"a HashMap might look something like this."},{"Start":"01:43.790 ","End":"01:52.015","Text":"The put method is used to store the key and to retrieve a particular value,"},{"Start":"01:52.015 ","End":"01:55.795","Text":"you need to supply the key and if the key is found,"},{"Start":"01:55.795 ","End":"01:59.210","Text":"the relevant value is returned."},{"Start":"02:02.760 ","End":"02:05.364","Text":"As we stated previously,"},{"Start":"02:05.364 ","End":"02:09.580","Text":"a HashMap\u0027s a particular implementation of a map that makes use of"},{"Start":"02:09.580 ","End":"02:15.370","Text":"a hash code to determine where the value is going to be stored in the data structure."},{"Start":"02:15.370 ","End":"02:21.840","Text":"Hash codes are generated by using an algorithm to turn an input into an integer."},{"Start":"02:21.840 ","End":"02:24.220","Text":"For example, if we had a string,"},{"Start":"02:24.220 ","End":"02:30.275","Text":"we could generate a hash code for it by passing it through this hashing algorithm,"},{"Start":"02:30.275 ","End":"02:34.700","Text":"which might just take the ASCII value of each character in the string"},{"Start":"02:34.700 ","End":"02:40.075","Text":"and adding up all those ASCII values to produce a single integer."},{"Start":"02:40.075 ","End":"02:42.260","Text":"So for this input string,"},{"Start":"02:42.260 ","End":"02:48.290","Text":"65 plus 66 plus 66 plus 65 would give us 262"},{"Start":"02:48.290 ","End":"02:54.340","Text":"and we could then use this integer value as a locator for the item we want to store."},{"Start":"02:54.340 ","End":"02:57.770","Text":"Assuming we had an infinite number of memory locations,"},{"Start":"02:57.770 ","End":"03:03.560","Text":"we could use the memory location 262 to store this particular item."},{"Start":"03:03.560 ","End":"03:07.145","Text":"In practice, the algorithms usually much more complex than this,"},{"Start":"03:07.145 ","End":"03:09.395","Text":"but the general principle is the same."},{"Start":"03:09.395 ","End":"03:13.915","Text":"You put a value into an algorithm and get an integer out."},{"Start":"03:13.915 ","End":"03:18.275","Text":"Then you use the integer to decide where to store the value."},{"Start":"03:18.275 ","End":"03:21.385","Text":"With a HashMap the key is the thing,"},{"Start":"03:21.385 ","End":"03:26.350","Text":"pass through the hashing algorithm and then we use the integer that\u0027s returned to"},{"Start":"03:26.350 ","End":"03:31.690","Text":"decide where to store the value that\u0027s associated with that key."},{"Start":"03:31.690 ","End":"03:34.150","Text":"We\u0027ve previously seen that every object will have"},{"Start":"03:34.150 ","End":"03:37.270","Text":"inherited from the object superclass and we\u0027re"},{"Start":"03:37.270 ","End":"03:43.780","Text":"responsible for overriding certain methods like equals and to string in the subclass."},{"Start":"03:43.780 ","End":"03:46.580","Text":"This is also true for hashCode."},{"Start":"03:46.580 ","End":"03:50.830","Text":"We should override the hashCode method so the 2 items that we regard"},{"Start":"03:50.830 ","End":"03:55.770","Text":"as equal generate the same hashCode integer value."},{"Start":"03:55.770 ","End":"04:01.640","Text":"In this example, 2 different song objects are the same only if"},{"Start":"04:01.640 ","End":"04:08.335","Text":"both the song name and the song artist attributes are the same for both objects."},{"Start":"04:08.335 ","End":"04:14.375","Text":"A suitable hashCode for the song class would be like this,"},{"Start":"04:14.375 ","End":"04:23.390","Text":"where we call the hashCode method on the name and artist attributes separately then"},{"Start":"04:23.390 ","End":"04:26.900","Text":"we add together those 2 integers returned by"},{"Start":"04:26.900 ","End":"04:32.270","Text":"the strings class is hashCode method and we get a single integer,"},{"Start":"04:32.270 ","End":"04:34.265","Text":"which we can then return."},{"Start":"04:34.265 ","End":"04:40.075","Text":"Now, if song name and artists are the same for 2 different song objects,"},{"Start":"04:40.075 ","End":"04:42.880","Text":"the hashCode will also be the same,"},{"Start":"04:42.880 ","End":"04:44.545","Text":"which is what we want it."},{"Start":"04:44.545 ","End":"04:48.565","Text":"The most commonly used methods with a HashMap are the following."},{"Start":"04:48.565 ","End":"04:52.300","Text":"One particularly interesting one is keyset,"},{"Start":"04:52.300 ","End":"04:56.740","Text":"which returns a set containing all the keys"},{"Start":"04:56.740 ","End":"05:01.270","Text":"currently in the HashMap and this can be useful for searching through,"},{"Start":"05:01.270 ","End":"05:03.970","Text":"for example, for validating whether something"},{"Start":"05:03.970 ","End":"05:07.160","Text":"is in the HashMap before trying to retrieve it."},{"Start":"05:07.160 ","End":"05:10.540","Text":"We\u0027ll pause there one final time and in the last video,"},{"Start":"05:10.540 ","End":"05:14.875","Text":"we\u0027ll look at sets and specifically a HashSet."},{"Start":"05:14.875 ","End":"05:17.720","Text":"Thanks for watching and see you soon."}],"ID":31161},{"Watched":false,"Name":"HashSets","Duration":"3m 20s","ChapterTopicVideoID":29550,"CourseChapterTopicPlaylistID":294562,"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.825","Text":"Hello, welcome back. We previously saw how"},{"Start":"00:03.825 ","End":"00:08.040","Text":"a HashMap can be used to store key-value pairs efficiently."},{"Start":"00:08.040 ","End":"00:13.230","Text":"A different type of abstract datatype is a set,"},{"Start":"00:13.230 ","End":"00:17.865","Text":"an unordered collection of values with no duplicates."},{"Start":"00:17.865 ","End":"00:25.875","Text":"A HashSet is a set that internally uses hashing to store the individual set values."},{"Start":"00:25.875 ","End":"00:28.650","Text":"Sets are used in a different way to maps."},{"Start":"00:28.650 ","End":"00:30.645","Text":"There\u0027s no key involved."},{"Start":"00:30.645 ","End":"00:33.280","Text":"We don\u0027t usually want to get values from the set,"},{"Start":"00:33.280 ","End":"00:36.210","Text":"we simply want to store things in the set and"},{"Start":"00:36.210 ","End":"00:39.945","Text":"check whether a particular value is contained in the set."},{"Start":"00:39.945 ","End":"00:44.285","Text":"There\u0027s no order, so there\u0027s no need for an index."},{"Start":"00:44.285 ","End":"00:51.130","Text":"You put items into a HashSet just like you would for an ArrayList by using add."},{"Start":"00:51.130 ","End":"00:54.875","Text":"To check if a particular value is in a set,"},{"Start":"00:54.875 ","End":"00:57.505","Text":"you use the contains method."},{"Start":"00:57.505 ","End":"01:01.085","Text":"As hashing is being used to store the values in the set,"},{"Start":"01:01.085 ","End":"01:04.910","Text":"you must override the default hashCode method in"},{"Start":"01:04.910 ","End":"01:09.605","Text":"your own class definitions for HashSets to work properly."},{"Start":"01:09.605 ","End":"01:11.930","Text":"As we saw with HashMaps,"},{"Start":"01:11.930 ","End":"01:15.650","Text":"a hashing algorithm of some sort must be used on any of"},{"Start":"01:15.650 ","End":"01:17.810","Text":"the individual attributes in"},{"Start":"01:17.810 ","End":"01:23.880","Text":"your custom class that contribute to item identity and equality."},{"Start":"01:24.970 ","End":"01:27.560","Text":"In addition to the ones we\u0027ve already seen,"},{"Start":"01:27.560 ","End":"01:32.870","Text":"there are a number of other useful methods in the HashSet collection class."},{"Start":"01:32.870 ","End":"01:37.669","Text":"There\u0027s quite a bit of overlap with the HashMap and ArrayLists collections,"},{"Start":"01:37.669 ","End":"01:41.210","Text":"but each has its own particular uses and which"},{"Start":"01:41.210 ","End":"01:44.945","Text":"1 you use will be dependent on your requirements."},{"Start":"01:44.945 ","End":"01:48.695","Text":"Arraylists are suitable when things need to be"},{"Start":"01:48.695 ","End":"01:52.730","Text":"ordered and when things need to grow or shrink,"},{"Start":"01:52.730 ","End":"01:56.570","Text":"and if an index would be useful."},{"Start":"01:56.570 ","End":"02:02.780","Text":"For a HashMap, if you have a very large collection and you want to look up things,"},{"Start":"02:02.780 ","End":"02:07.430","Text":"then a HashMap might be a way to go especially if you have"},{"Start":"02:07.430 ","End":"02:12.960","Text":"something that\u0027s very obvious for key that you might want to search on."},{"Start":"02:13.540 ","End":"02:20.330","Text":"Finally, a HashSet might be useful in circumstances where you do not want"},{"Start":"02:20.330 ","End":"02:23.270","Text":"to allow duplicate values and you\u0027re not"},{"Start":"02:23.270 ","End":"02:26.495","Text":"interested specifically in the values themselves,"},{"Start":"02:26.495 ","End":"02:31.710","Text":"just whether a particular value is within the set."},{"Start":"02:32.230 ","End":"02:38.060","Text":"1 final practical point to use any of the collection classes we\u0027ve looked at,"},{"Start":"02:38.060 ","End":"02:41.360","Text":"you will need to import the relevant library."},{"Start":"02:41.360 ","End":"02:44.690","Text":"Fortunately, they\u0027re all in the same package, java.util,"},{"Start":"02:44.690 ","End":"02:49.925","Text":"and then you just specify the name of the relevant class."},{"Start":"02:49.925 ","End":"02:52.580","Text":"That\u0027s it for this topic."},{"Start":"02:52.580 ","End":"02:57.035","Text":"In this section, we learned how to describe the purpose of collection classes,"},{"Start":"02:57.035 ","End":"03:01.120","Text":"explain the process of autoboxing and unboxing,"},{"Start":"03:01.120 ","End":"03:05.840","Text":"to relate the concepts of generics to collection classes,"},{"Start":"03:05.840 ","End":"03:12.075","Text":"and to evaluate when a collection class is appropriate to a given situation."},{"Start":"03:12.075 ","End":"03:14.720","Text":"Go ahead and complete the exercises"},{"Start":"03:14.720 ","End":"03:18.140","Text":"now to get to grips with everything we\u0027ve covered here."},{"Start":"03:18.140 ","End":"03:20.670","Text":"Thank you very much for watching."}],"ID":31162},{"Watched":false,"Name":"Exercise 1","Duration":"13m 15s","ChapterTopicVideoID":29551,"CourseChapterTopicPlaylistID":294562,"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.940","Text":"Hello, Welcome to this exercise on collections."},{"Start":"00:02.940 ","End":"00:06.195","Text":"We\u0027re first asked to create a new project and within it,"},{"Start":"00:06.195 ","End":"00:08.415","Text":"a class called CollectionsApp."},{"Start":"00:08.415 ","End":"00:12.450","Text":"In part A, we\u0027re then told within a new method, polite numbers,"},{"Start":"00:12.450 ","End":"00:15.210","Text":"which returns no values and accepts no arguments,"},{"Start":"00:15.210 ","End":"00:20.475","Text":"to declare an ArrayList called myList to contain integers."},{"Start":"00:20.475 ","End":"00:27.165","Text":"In part B, we\u0027re told to use the add method to add these numbers 1 at a time to myList,"},{"Start":"00:27.165 ","End":"00:30.375","Text":"and the first set of numbers is 3, 5, and 6."},{"Start":"00:30.375 ","End":"00:33.945","Text":"We\u0027re told in part 1 to use the new keyword."},{"Start":"00:33.945 ","End":"00:36.690","Text":"In part 2, the numbers are 6, 7,"},{"Start":"00:36.690 ","End":"00:40.035","Text":"and 9, and we\u0027re told to use autoboxing."},{"Start":"00:40.035 ","End":"00:43.010","Text":"In part C, we\u0027re told to output the contents of"},{"Start":"00:43.010 ","End":"00:45.920","Text":"myList using a single print line statement,"},{"Start":"00:45.920 ","End":"00:51.065","Text":"and use the object workbench to test that the numbers you expect are in myList."},{"Start":"00:51.065 ","End":"00:55.640","Text":"In part D, we use the methods size and get to store"},{"Start":"00:55.640 ","End":"01:01.070","Text":"the last element of myList into a new int variable lastInt."},{"Start":"01:01.070 ","End":"01:06.110","Text":"We then output the contents of the lastInt variable on the next line."},{"Start":"01:06.110 ","End":"01:11.440","Text":"Use the object workbench again to test that the output is what you expect."},{"Start":"01:11.440 ","End":"01:16.070","Text":"Then in part F, we remove one of the occurrences of 6 from myList"},{"Start":"01:16.070 ","End":"01:20.610","Text":"using the remove method and output using a single print line."},{"Start":"01:20.610 ","End":"01:23.720","Text":"Again, we test by creating an object on"},{"Start":"01:23.720 ","End":"01:27.815","Text":"the object workbench and running the polite numbers method."},{"Start":"01:27.815 ","End":"01:29.390","Text":"Finally, in part H,"},{"Start":"01:29.390 ","End":"01:34.040","Text":"we double-click on the object created on the workbench so we can inspect its contents."},{"Start":"01:34.040 ","End":"01:38.345","Text":"We\u0027re asked why you cannot see myList,"},{"Start":"01:38.345 ","End":"01:44.835","Text":"and then to move myList so it can be seen on the object workbench."},{"Start":"01:44.835 ","End":"01:48.695","Text":"I\u0027ve created the class collections app, and within it,"},{"Start":"01:48.695 ","End":"01:54.710","Text":"I\u0027m going to create a method called polite numbers,"},{"Start":"01:54.710 ","End":"02:01.000","Text":"which is going to contain some numbers in an ArrayList."},{"Start":"02:01.400 ","End":"02:06.229","Text":"That\u0027s the opening line of method definition."},{"Start":"02:06.229 ","End":"02:09.655","Text":"Now I need to create an ArrayList,"},{"Start":"02:09.655 ","End":"02:13.665","Text":"and I\u0027m going to call it myList,"},{"Start":"02:13.665 ","End":"02:16.635","Text":"and it\u0027s going to contain integers."},{"Start":"02:16.635 ","End":"02:24.225","Text":"The syntax here then is ArrayList is what I\u0027m creating it from,"},{"Start":"02:24.225 ","End":"02:28.370","Text":"and then there\u0027s this generics syntax where I\u0027ve put"},{"Start":"02:28.370 ","End":"02:34.640","Text":"a typeInt in this case it\u0027s going to be of type integer."},{"Start":"02:34.640 ","End":"02:38.375","Text":"Then I give my ArrayList a name."},{"Start":"02:38.375 ","End":"02:40.850","Text":"I\u0027m just going to call mine myList."},{"Start":"02:40.850 ","End":"02:47.410","Text":"Then I create an object from this class ArrayList,"},{"Start":"02:47.410 ","End":"02:51.015","Text":"and I can put the diamond here,"},{"Start":"02:51.015 ","End":"02:54.480","Text":"and it\u0027s a constructor method,"},{"Start":"02:54.480 ","End":"02:56.940","Text":"so I need to put some brackets afterwards."},{"Start":"02:56.940 ","End":"03:01.445","Text":"There\u0027s the opening line that will create my ArrayList,"},{"Start":"03:01.445 ","End":"03:03.080","Text":"which is going to contain integers."},{"Start":"03:03.080 ","End":"03:04.850","Text":"It\u0027s going to be called myList."},{"Start":"03:04.850 ","End":"03:13.820","Text":"The reason it\u0027s giving me an error is I need to import the ArrayList code from java.util."},{"Start":"03:13.820 ","End":"03:15.515","Text":"Once I\u0027ve done that,"},{"Start":"03:15.515 ","End":"03:17.525","Text":"the error goes away."},{"Start":"03:17.525 ","End":"03:21.990","Text":"So I\u0027ve created myList now,"},{"Start":"03:21.990 ","End":"03:23.595","Text":"which I\u0027ve called myList,"},{"Start":"03:23.595 ","End":"03:25.450","Text":"and in part B,"},{"Start":"03:25.450 ","End":"03:32.180","Text":"I\u0027m asked to use the add method to add some numbers to the list one at a time,"},{"Start":"03:32.180 ","End":"03:36.920","Text":"so I give the name of the list and then I use"},{"Start":"03:36.920 ","End":"03:42.635","Text":"the add keyword or method name actually, it\u0027s not keyword."},{"Start":"03:42.635 ","End":"03:45.035","Text":"Now inside the brackets,"},{"Start":"03:45.035 ","End":"03:47.810","Text":"I\u0027m going to give a value,"},{"Start":"03:47.810 ","End":"03:51.695","Text":"but because these are integers, actually,"},{"Start":"03:51.695 ","End":"03:56.355","Text":"I\u0027m supposed to be passing objects to the add method."},{"Start":"03:56.355 ","End":"04:00.493","Text":"I can do it this way by saying new integer,"},{"Start":"04:00.493 ","End":"04:03.110","Text":"and then whatever is the integer I want to pass in."},{"Start":"04:03.110 ","End":"04:07.315","Text":"This creates an object of type integer"},{"Start":"04:07.315 ","End":"04:12.660","Text":"and passes to the constructor of that object, the number 3."},{"Start":"04:12.660 ","End":"04:17.695","Text":"This will create an integer object with 3 inside it,"},{"Start":"04:17.695 ","End":"04:19.180","Text":"and I\u0027m going to do"},{"Start":"04:19.180 ","End":"04:23.575","Text":"exactly the same thing for the next 3 numbers that we\u0027re supposed to put in,"},{"Start":"04:23.575 ","End":"04:27.030","Text":"but change the number itself,"},{"Start":"04:27.030 ","End":"04:30.090","Text":"so I\u0027ve got 3, 5, and 6."},{"Start":"04:30.090 ","End":"04:32.680","Text":"That\u0027s part B1 done,"},{"Start":"04:32.680 ","End":"04:38.385","Text":"I\u0027ve used the new keyword to create a new integer object with given values,"},{"Start":"04:38.385 ","End":"04:41.205","Text":"so those values will be stored inside the object."},{"Start":"04:41.205 ","End":"04:45.595","Text":"Then that object is added to this list, myList."},{"Start":"04:45.595 ","End":"04:48.200","Text":"In part 2 of B,"},{"Start":"04:48.200 ","End":"04:50.675","Text":"I\u0027ve been asked to use the autoboxing,"},{"Start":"04:50.675 ","End":"04:56.015","Text":"so all that means is you can\u0027t actually treat primitives like"},{"Start":"04:56.015 ","End":"05:02.165","Text":"integers and chars and so on as objects because they\u0027re not objects, they\u0027re primitives."},{"Start":"05:02.165 ","End":"05:09.085","Text":"But Java does you a favor by trying to interpret when it\u0027s doing things like this,"},{"Start":"05:09.085 ","End":"05:15.170","Text":"anything that can be converted into a wrapper class as that wrapper-type object."},{"Start":"05:15.170 ","End":"05:21.105","Text":"This 6 will be converted into an integer object,"},{"Start":"05:21.105 ","End":"05:23.370","Text":"and it will contain the number 6."},{"Start":"05:23.370 ","End":"05:29.615","Text":"This is the process of autoboxing that Java does for you."},{"Start":"05:29.615 ","End":"05:33.230","Text":"It\u0027s important for you to be aware that it\u0027s happening,"},{"Start":"05:33.230 ","End":"05:36.240","Text":"but basically, it\u0027s doing more work for you."},{"Start":"05:36.240 ","End":"05:40.610","Text":"We don\u0027t need to do this thing for primitives,"},{"Start":"05:40.610 ","End":"05:46.285","Text":"Java will take care of it for us through this autoboxing process."},{"Start":"05:46.285 ","End":"05:48.540","Text":"That looks good so far,"},{"Start":"05:48.540 ","End":"05:51.195","Text":"so we\u0027ve got a list of things now,"},{"Start":"05:51.195 ","End":"05:54.140","Text":"and so it also says in part C to use"},{"Start":"05:54.140 ","End":"06:00.770","Text":"a single print line statement to output the contents of the entire list."},{"Start":"06:00.770 ","End":"06:03.100","Text":"Now, how can you do that?"},{"Start":"06:03.100 ","End":"06:06.710","Text":"Well, are we able to concatenate a lot of things"},{"Start":"06:06.710 ","End":"06:11.863","Text":"together each object\u0027s value basically from myList?"},{"Start":"06:11.863 ","End":"06:14.690","Text":"Well, we don\u0027t have to do anything as complex as that,"},{"Start":"06:14.690 ","End":"06:17.150","Text":"we can actually just put myList."},{"Start":"06:17.150 ","End":"06:19.565","Text":"If we had done this with an array,"},{"Start":"06:19.565 ","End":"06:24.230","Text":"this would just give us the address of the array but with myList,"},{"Start":"06:24.230 ","End":"06:25.550","Text":"because it\u0027s an object,"},{"Start":"06:25.550 ","End":"06:29.780","Text":"and the object contains a method called toString,"},{"Start":"06:29.780 ","End":"06:33.960","Text":"and myList has overwritten or ArrayLists have overwritten it,"},{"Start":"06:33.960 ","End":"06:39.170","Text":"and this will actually print the contents of the list if we\u0027ve done everything right."},{"Start":"06:39.170 ","End":"06:40.835","Text":"Let\u0027s just see that. Like I said,"},{"Start":"06:40.835 ","End":"06:42.875","Text":"this wouldn\u0027t work for an array,"},{"Start":"06:42.875 ","End":"06:48.495","Text":"but it does work for ArrayLists because toString has been overwritten."},{"Start":"06:48.495 ","End":"06:51.785","Text":"Let\u0027s have a look at what happens, and there we go."},{"Start":"06:51.785 ","End":"06:54.230","Text":"We\u0027ve got the output that we expected."},{"Start":"06:54.230 ","End":"06:59.960","Text":"Those are the numbers that we put into our list, those 6 numbers."},{"Start":"06:59.960 ","End":"07:02.420","Text":"That\u0027s part C done."},{"Start":"07:02.420 ","End":"07:06.365","Text":"Part D is to use the methods size,"},{"Start":"07:06.365 ","End":"07:14.930","Text":"and get to store the last element of myList into a new int variable called lastInt."},{"Start":"07:14.930 ","End":"07:18.185","Text":"That\u0027s easy enough. Here,"},{"Start":"07:18.185 ","End":"07:21.910","Text":"we\u0027re going to declare a new int called myInt,"},{"Start":"07:21.910 ","End":"07:29.240","Text":"and we\u0027re going to get the value that\u0027s stored into one of these objects in the list by"},{"Start":"07:29.240 ","End":"07:37.590","Text":"using get and get expects a index into the list."},{"Start":"07:37.590 ","End":"07:40.820","Text":"If you imagine the list like an array that has indexes,"},{"Start":"07:40.820 ","End":"07:44.645","Text":"the 6th item in the array because we start from 0,"},{"Start":"07:44.645 ","End":"07:48.485","Text":"would be retrieved using that line there."},{"Start":"07:48.485 ","End":"07:51.450","Text":"That would work, but it\u0027s not a great way of doing things,"},{"Start":"07:51.450 ","End":"07:56.860","Text":"we really should take the size of the list and use that as the basis for this."},{"Start":"07:56.860 ","End":"07:59.090","Text":"If we just do it properly,"},{"Start":"07:59.090 ","End":"08:03.240","Text":"we should say myList.size,"},{"Start":"08:03.240 ","End":"08:06.455","Text":"and that gives us the size of the list,"},{"Start":"08:06.455 ","End":"08:09.335","Text":"but that will actually return a 6."},{"Start":"08:09.335 ","End":"08:13.925","Text":"If I try and get the 7th member of that list,"},{"Start":"08:13.925 ","End":"08:15.545","Text":"it will give me a runtime error,"},{"Start":"08:15.545 ","End":"08:19.745","Text":"so I want to subtract 1 from that or should I say,"},{"Start":"08:19.745 ","End":"08:23.845","Text":"that will give me the correct result and no errors."},{"Start":"08:23.845 ","End":"08:26.135","Text":"I can try and print that out now,"},{"Start":"08:26.135 ","End":"08:32.630","Text":"just to see that I am getting it into my variable called myInt."},{"Start":"08:32.630 ","End":"08:34.595","Text":"Let\u0027s see if that\u0027s worked,"},{"Start":"08:34.595 ","End":"08:36.410","Text":"just by running that,"},{"Start":"08:36.410 ","End":"08:38.680","Text":"and see what the output is."},{"Start":"08:38.680 ","End":"08:44.880","Text":"There we go, it has retrieved into an integer the 6th item,"},{"Start":"08:44.880 ","End":"08:46.915","Text":"which I got using index 5,"},{"Start":"08:46.915 ","End":"08:49.715","Text":"stored it into an integer variable,"},{"Start":"08:49.715 ","End":"08:56.630","Text":"and then I was able to output the value of that integer variable and notice I was able to"},{"Start":"08:56.630 ","End":"09:04.070","Text":"assign something that came from an object into an integer variable."},{"Start":"09:04.070 ","End":"09:07.045","Text":"This is an example of unboxing,"},{"Start":"09:07.045 ","End":"09:09.495","Text":"which Java also does for us."},{"Start":"09:09.495 ","End":"09:14.495","Text":"If it\u0027s of type integer and I\u0027m trying to assign it to an int,"},{"Start":"09:14.495 ","End":"09:16.550","Text":"it won\u0027t complain and say they are"},{"Start":"09:16.550 ","End":"09:19.250","Text":"different types and int is not the same thing as an integer,"},{"Start":"09:19.250 ","End":"09:22.490","Text":"it will just get the value out from"},{"Start":"09:22.490 ","End":"09:27.035","Text":"that integer object and store it into the int variable here."},{"Start":"09:27.035 ","End":"09:31.880","Text":"That all works as we expect and we can see how things now working."},{"Start":"09:31.880 ","End":"09:36.740","Text":"Final thing to do then is to do part F,"},{"Start":"09:36.740 ","End":"09:39.785","Text":"which is to remove one of the occurrences of 6,"},{"Start":"09:39.785 ","End":"09:42.140","Text":"from myList, you notice we\u0027ve got 2 in there,"},{"Start":"09:42.140 ","End":"09:45.665","Text":"so I didn\u0027t mean to remove the line here,"},{"Start":"09:45.665 ","End":"09:51.180","Text":"but actually to use the remove method that ArrayLists have,"},{"Start":"09:51.180 ","End":"09:56.615","Text":"and this is simple as putting myList.remove,"},{"Start":"09:56.615 ","End":"09:59.735","Text":"and then in brackets the position that I want to remove."},{"Start":"09:59.735 ","End":"10:01.190","Text":"This will be 0,"},{"Start":"10:01.190 ","End":"10:03.990","Text":"1, 2, 3, 4,"},{"Start":"10:03.990 ","End":"10:07.680","Text":"5, so I could either remove 2 or I could remove 3,"},{"Start":"10:07.680 ","End":"10:09.330","Text":"it won\u0027t make any difference."},{"Start":"10:09.330 ","End":"10:11.085","Text":"Let\u0027s remove 2,"},{"Start":"10:11.085 ","End":"10:19.470","Text":"and then let\u0027s output the list again just to see that that\u0027s what."},{"Start":"10:20.020 ","End":"10:26.855","Text":"I don\u0027t want to output my int I want to output myList."},{"Start":"10:26.855 ","End":"10:30.260","Text":"Let\u0027s see what\u0027s happened now, and there we go."},{"Start":"10:30.260 ","End":"10:32.450","Text":"You can see that the list was printed,"},{"Start":"10:32.450 ","End":"10:35.630","Text":"the original list, then the int that I extracted,"},{"Start":"10:35.630 ","End":"10:41.260","Text":"and then the list was printed again after I removed one of the elements from the list,"},{"Start":"10:41.260 ","End":"10:43.290","Text":"and you can see that I\u0027ve only got 1,"},{"Start":"10:43.290 ","End":"10:46.865","Text":"6 in there now, and that\u0027s great."},{"Start":"10:46.865 ","End":"10:50.900","Text":"I\u0027ve been asked in part G to test by running that method,"},{"Start":"10:50.900 ","End":"10:57.315","Text":"and then double-click on the object in part H so that you can inspect the contents."},{"Start":"10:57.315 ","End":"10:59.870","Text":"There\u0027s no myList in here."},{"Start":"10:59.870 ","End":"11:01.520","Text":"In fact, there\u0027s nothing in here."},{"Start":"11:01.520 ","End":"11:05.000","Text":"We\u0027re being asked the question, why?"},{"Start":"11:05.000 ","End":"11:10.100","Text":"Well, that\u0027s because it\u0027s declared inside the method here,"},{"Start":"11:10.100 ","End":"11:16.710","Text":"so the way that the lifetime of this method works is that these will be local variables,"},{"Start":"11:16.710 ","End":"11:18.210","Text":"the things that are in here,"},{"Start":"11:18.210 ","End":"11:21.810","Text":"and this object here is local as well."},{"Start":"11:22.030 ","End":"11:25.924","Text":"When a method exits,"},{"Start":"11:25.924 ","End":"11:28.160","Text":"any local variables and objects are destroyed,"},{"Start":"11:28.160 ","End":"11:29.765","Text":"so that\u0027s why we don\u0027t see it."},{"Start":"11:29.765 ","End":"11:31.955","Text":"If I wanted it to persist,"},{"Start":"11:31.955 ","End":"11:37.980","Text":"I could just keep it outside the code and declare at the top here this way,"},{"Start":"11:37.980 ","End":"11:42.005","Text":"any other methods inside this class could also access it."},{"Start":"11:42.005 ","End":"11:48.005","Text":"But that means that it should be visible to me on the object workbench."},{"Start":"11:48.005 ","End":"11:52.220","Text":"Let\u0027s see if that makes a difference this time."},{"Start":"11:52.220 ","End":"11:56.670","Text":"If I run it and then run that method itself,"},{"Start":"11:56.670 ","End":"11:58.380","Text":"and now have a look inside,"},{"Start":"11:58.380 ","End":"12:02.780","Text":"you\u0027ll see I\u0027ve got an ArrayList of integers called myList,"},{"Start":"12:02.780 ","End":"12:05.480","Text":"and this is a reference here to something,"},{"Start":"12:05.480 ","End":"12:09.305","Text":"so here\u0027s our ArrayList and it\u0027s telling us some properties,"},{"Start":"12:09.305 ","End":"12:11.195","Text":"the size and something else."},{"Start":"12:11.195 ","End":"12:13.250","Text":"Then if I double-click on that reference,"},{"Start":"12:13.250 ","End":"12:16.865","Text":"I see the actual elements themselves."},{"Start":"12:16.865 ","End":"12:19.850","Text":"The first one you\u0027ll see is the number 3."},{"Start":"12:19.850 ","End":"12:22.490","Text":"The next one is the number 5 and so on,"},{"Start":"12:22.490 ","End":"12:25.385","Text":"just like a list on the side here."},{"Start":"12:25.385 ","End":"12:28.430","Text":"We\u0027re seeing the ArrayList itself now."},{"Start":"12:28.430 ","End":"12:32.630","Text":"Interesting, we\u0027ve only got 5 elements in our array,"},{"Start":"12:32.630 ","End":"12:35.295","Text":"but it\u0027s created 10 in this ArrayList,"},{"Start":"12:35.295 ","End":"12:37.620","Text":"and that\u0027s just for efficiency reasons."},{"Start":"12:37.620 ","End":"12:40.940","Text":"Java just automatically creates lists of size 10,"},{"Start":"12:40.940 ","End":"12:43.805","Text":"and then when it needs to increase them, it does."},{"Start":"12:43.805 ","End":"12:46.010","Text":"That\u0027s just for efficiency reasons really,"},{"Start":"12:46.010 ","End":"12:48.030","Text":"so don\u0027t worry too much about that."},{"Start":"12:48.030 ","End":"12:51.180","Text":"But you can see how the fields have been stored away,"},{"Start":"12:51.180 ","End":"12:54.710","Text":"these attributes have been stored away inside the object,"},{"Start":"12:54.710 ","End":"12:58.505","Text":"and we have access to that because we now"},{"Start":"12:58.505 ","End":"13:02.900","Text":"declared this list outside the method, therefore,"},{"Start":"13:02.900 ","End":"13:06.920","Text":"it lives for as long as any objects are in existence,"},{"Start":"13:06.920 ","End":"13:09.395","Text":"and because we\u0027re using the object workbench here,"},{"Start":"13:09.395 ","End":"13:12.035","Text":"we\u0027re able to see the values after we\u0027ve run it."},{"Start":"13:12.035 ","End":"13:13.880","Text":"Thanks for watching, that\u0027s it for this one,"},{"Start":"13:13.880 ","End":"13:16.650","Text":"and see you shortly for the next one."}],"ID":31163},{"Watched":false,"Name":"Exercise 2","Duration":"13m 5s","ChapterTopicVideoID":29552,"CourseChapterTopicPlaylistID":294562,"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 everyone. Welcome to this exercise in which we"},{"Start":"00:02.730 ","End":"00:05.625","Text":"first been asked to create a new class within"},{"Start":"00:05.625 ","End":"00:11.745","Text":"the existing project called contact and the class looks like so."},{"Start":"00:11.745 ","End":"00:14.535","Text":"We\u0027re then told to implement the attributes"},{"Start":"00:14.535 ","End":"00:19.860","Text":"constructor and accessor methods according to that class diagram."},{"Start":"00:19.860 ","End":"00:23.134","Text":"In Part B, we\u0027re told that the toString method override"},{"Start":"00:23.134 ","End":"00:27.020","Text":"the toString method inherited from the object class that all Java classes"},{"Start":"00:27.020 ","End":"00:29.300","Text":"inherit and should be used to return"},{"Start":"00:29.300 ","End":"00:34.700","Text":"the attribute values as a single string and an example is given."},{"Start":"00:34.700 ","End":"00:36.815","Text":"Part C then says to create"},{"Start":"00:36.815 ","End":"00:41.615","Text":"a new public method addressBook within the collectionsApp class,"},{"Start":"00:41.615 ","End":"00:44.585","Text":"which returns no values and takes no arguments."},{"Start":"00:44.585 ","End":"00:47.060","Text":"In Part D, we create an ArrayList called"},{"Start":"00:47.060 ","End":"00:50.555","Text":"contacts to hold an arbitrary number of contacts."},{"Start":"00:50.555 ","End":"00:52.850","Text":"We ensure that that\u0027s declared outside"},{"Start":"00:52.850 ","End":"00:57.170","Text":"the address book method so it can be inspected on the object workbench."},{"Start":"00:57.170 ","End":"01:03.920","Text":"In Part E within address book we put 3 contacts into the contacts ArrayList using"},{"Start":"01:03.920 ","End":"01:10.705","Text":"the add method as follows and 3 sets of values are given in Parts 1,"},{"Start":"01:10.705 ","End":"01:12.060","Text":"2, and 3."},{"Start":"01:12.060 ","End":"01:16.205","Text":"In Part F, we\u0027re told to use a for each style for-loop to"},{"Start":"01:16.205 ","End":"01:20.840","Text":"output each contact in contexts on a new line and"},{"Start":"01:20.840 ","End":"01:25.100","Text":"then finally in Part G we\u0027re told to run the address book method and see if"},{"Start":"01:25.100 ","End":"01:30.145","Text":"the contacts are added to the ArrayList and output to the terminal window."},{"Start":"01:30.145 ","End":"01:32.450","Text":"I\u0027ve created the class called"},{"Start":"01:32.450 ","End":"01:36.650","Text":"contact within the existing project that we\u0027re working on in"},{"Start":"01:36.650 ","End":"01:39.965","Text":"the previous exercise and then I can go ahead and"},{"Start":"01:39.965 ","End":"01:44.730","Text":"implement the class according to the class diagram."},{"Start":"01:44.730 ","End":"01:46.965","Text":"We\u0027ve got 3 attributes,"},{"Start":"01:46.965 ","End":"01:49.970","Text":"name, email and mobile,"},{"Start":"01:49.970 ","End":"01:52.380","Text":"and they\u0027re all strings."},{"Start":"01:53.320 ","End":"01:59.045","Text":"I can just copy that and change the attribute name."},{"Start":"01:59.045 ","End":"02:01.080","Text":"We\u0027ve got name, email,"},{"Start":"02:01.080 ","End":"02:03.040","Text":"and mobile as I said."},{"Start":"02:03.040 ","End":"02:11.465","Text":"Then I need to do a constructor to set those values as it says in Part"},{"Start":"02:11.465 ","End":"02:16.295","Text":"A. Constructor is public name of it"},{"Start":"02:16.295 ","End":"02:24.550","Text":"is contact and it\u0027s going to take 3 parameters."},{"Start":"02:26.300 ","End":"02:36.765","Text":"We simply set those attribute values to what\u0027s been passed in and once we\u0027ve done that,"},{"Start":"02:36.765 ","End":"02:41.990","Text":"we can do our select to methods which have"},{"Start":"02:41.990 ","End":"02:47.090","Text":"been specified in the UML diagram there as getName,"},{"Start":"02:47.090 ","End":"02:48.995","Text":"getEmail, and getMobile,"},{"Start":"02:48.995 ","End":"02:54.830","Text":"which do an obvious thing which is return the respective attribute as a string."},{"Start":"02:54.830 ","End":"03:04.080","Text":"That\u0027s going to be public and it returns a string and getName is the first one,"},{"Start":"03:04.080 ","End":"03:11.355","Text":"no parameters and it simply returns the name field."},{"Start":"03:11.355 ","End":"03:13.295","Text":"That\u0027s the first one."},{"Start":"03:13.295 ","End":"03:21.900","Text":"I\u0027ll just copy that for the other 2 and change the attribute name and method name."},{"Start":"03:23.390 ","End":"03:26.485","Text":"That select a method\u0027s done."},{"Start":"03:26.485 ","End":"03:35.735","Text":"Then the final thing to do in Part B is to override the toString method,"},{"Start":"03:35.735 ","End":"03:40.850","Text":"which we inherit from the object class and it doesn\u0027t say extends here."},{"Start":"03:40.850 ","End":"03:44.360","Text":"But every object in Java inherits from"},{"Start":"03:44.360 ","End":"03:48.340","Text":"the object class and that will have a method in there called toString,"},{"Start":"03:48.340 ","End":"03:52.985","Text":"which we want to overwrite to do some new behavior."},{"Start":"03:52.985 ","End":"03:56.345","Text":"I don\u0027t have to put this override annotationing."},{"Start":"03:56.345 ","End":"04:00.920","Text":"But I\u0027m going to just check that I\u0027ve got the right format,"},{"Start":"04:00.920 ","End":"04:05.250","Text":"so toString is the name of the method,"},{"Start":"04:05.250 ","End":"04:08.015","Text":"it doesn\u0027t take any parameters,"},{"Start":"04:08.015 ","End":"04:11.340","Text":"but it does return a strings."},{"Start":"04:14.380 ","End":"04:17.755","Text":"It needs to actually return something."},{"Start":"04:17.755 ","End":"04:20.045","Text":"What is it that we\u0027re going to return?"},{"Start":"04:20.045 ","End":"04:26.750","Text":"We\u0027re going to return all of the field values and with some string literals in"},{"Start":"04:26.750 ","End":"04:33.860","Text":"front of them so the name and a space followed by the name field,"},{"Start":"04:33.860 ","End":"04:37.175","Text":"and then mobile,"},{"Start":"04:37.175 ","End":"04:43.795","Text":"and the email field."},{"Start":"04:43.795 ","End":"04:45.710","Text":"If we form that correctly,"},{"Start":"04:45.710 ","End":"04:49.385","Text":"that should all compile and we should get a result."},{"Start":"04:49.385 ","End":"04:52.565","Text":"I\u0027ve got a bracket in there which I don\u0027t need so take that out,"},{"Start":"04:52.565 ","End":"04:55.510","Text":"and that should sort the air out."},{"Start":"04:55.510 ","End":"05:00.724","Text":"We\u0027ve done Parts A and B then we\u0027ve implemented contact class,"},{"Start":"05:00.724 ","End":"05:04.520","Text":"which stores some contact details into these attributes"},{"Start":"05:04.520 ","End":"05:09.020","Text":"here and returns them using these selector methods."},{"Start":"05:09.020 ","End":"05:12.800","Text":"We\u0027ve overwritten the toString method to get"},{"Start":"05:12.800 ","End":"05:15.410","Text":"a string representation of what\u0027s stored"},{"Start":"05:15.410 ","End":"05:19.420","Text":"in this object when we create an object from this class."},{"Start":"05:19.420 ","End":"05:23.684","Text":"What we\u0027re gonna do now is going to create a method inside"},{"Start":"05:23.684 ","End":"05:27.000","Text":"the collectionsApp class that we were working"},{"Start":"05:27.000 ","End":"05:31.125","Text":"on previously and we\u0027re going to call it address book."},{"Start":"05:31.125 ","End":"05:32.900","Text":"Let\u0027s go ahead and do that."},{"Start":"05:32.900 ","End":"05:35.360","Text":"Just create some space here and"},{"Start":"05:35.360 ","End":"05:41.285","Text":"so the address book returns no values and takes no arguments."},{"Start":"05:41.285 ","End":"05:44.270","Text":"In order to call it, we need to make it public."},{"Start":"05:44.270 ","End":"05:49.550","Text":"It returns no values and takes no arguments."},{"Start":"05:49.550 ","End":"05:56.030","Text":"Code body itself is going to create an ArrayList,"},{"Start":"05:56.030 ","End":"06:02.090","Text":"first of all, called context to actually hold the contacts themselves."},{"Start":"06:02.090 ","End":"06:05.855","Text":"Now we\u0027ve been told we\u0027ve got to declare this outside"},{"Start":"06:05.855 ","End":"06:10.445","Text":"the address book method so we can have access to it, as we said before,"},{"Start":"06:10.445 ","End":"06:14.015","Text":"if you create anything inside a method,"},{"Start":"06:14.015 ","End":"06:17.295","Text":"it\u0027s created inside the method and then as soon as we get"},{"Start":"06:17.295 ","End":"06:20.655","Text":"to the closing brace of the code,"},{"Start":"06:20.655 ","End":"06:24.125","Text":"it\u0027s destroyed so you can\u0027t see it on the object workbench,"},{"Start":"06:24.125 ","End":"06:26.480","Text":"which is useful especially when we were learning,"},{"Start":"06:26.480 ","End":"06:28.895","Text":"but it\u0027s also useful in development time when you\u0027re"},{"Start":"06:28.895 ","End":"06:32.030","Text":"developing code and debugging to be able to see those values."},{"Start":"06:32.030 ","End":"06:36.410","Text":"Let\u0027s create our contacts list."},{"Start":"06:36.410 ","End":"06:45.920","Text":"The syntax is as follows we\u0027re creating an ArrayList and it\u0027s of type contact,"},{"Start":"06:45.920 ","End":"06:48.245","Text":"the one that we just created."},{"Start":"06:48.245 ","End":"06:50.375","Text":"To create an ArrayList,"},{"Start":"06:50.375 ","End":"06:52.190","Text":"we need to give it a name first of all,"},{"Start":"06:52.190 ","End":"06:55.645","Text":"so as a school contacts with an S,"},{"Start":"06:55.645 ","End":"07:03.105","Text":"it\u0027s a new ArrayList,"},{"Start":"07:03.105 ","End":"07:07.840","Text":"empty angle braces, the brackets there are,"},{"Start":"07:07.840 ","End":"07:11.735","Text":"this is a constructor effectively we\u0027re calling a constructor."},{"Start":"07:11.735 ","End":"07:15.085","Text":"There\u0027s no arguments that go to the constructor."},{"Start":"07:15.085 ","End":"07:19.330","Text":"We are actually creating a type which is contact,"},{"Start":"07:19.330 ","End":"07:23.200","Text":"but that will all happen automatically during the compilation process."},{"Start":"07:23.200 ","End":"07:28.210","Text":"That\u0027s fine we\u0027ve created our ArrayList now called contacts."},{"Start":"07:28.210 ","End":"07:30.145","Text":"Now we can actually do stuff with it."},{"Start":"07:30.145 ","End":"07:35.110","Text":"We\u0027ve been asked in Part E to do is to put 3 contacts into"},{"Start":"07:35.110 ","End":"07:40.570","Text":"the ArrayList using the add method and we\u0027ve been given the details."},{"Start":"07:40.570 ","End":"07:41.935","Text":"Now to do this,"},{"Start":"07:41.935 ","End":"07:50.510","Text":"we could create an object temporarily and store it into an object variable,"},{"Start":"07:50.510 ","End":"07:52.205","Text":"but we don\u0027t really need to do that."},{"Start":"07:52.205 ","End":"07:55.700","Text":"What we can do is inside the brackets for add,"},{"Start":"07:55.700 ","End":"08:02.670","Text":"we can create the object and then that will pass on a reference to the ArrayList."},{"Start":"08:02.670 ","End":"08:05.285","Text":"Just to see how that works in practice,"},{"Start":"08:05.285 ","End":"08:12.280","Text":"we calling here the constructor for contact by putting new."},{"Start":"08:15.590 ","End":"08:18.710","Text":"The first one is this."},{"Start":"08:18.710 ","End":"08:20.945","Text":"Once we\u0027ve given it these attributes,"},{"Start":"08:20.945 ","End":"08:23.750","Text":"it will pass them through to the constructor."},{"Start":"08:23.750 ","End":"08:29.480","Text":"The object will be created and it will return a reference to the calling code,"},{"Start":"08:29.480 ","End":"08:33.380","Text":"which is this line and that reference is the address of"},{"Start":"08:33.380 ","End":"08:37.880","Text":"this object and that\u0027s added the ArrayList called contacts."},{"Start":"08:37.880 ","End":"08:40.715","Text":"Quite a few different steps going on there."},{"Start":"08:40.715 ","End":"08:43.535","Text":"But it should all make sense,"},{"Start":"08:43.535 ","End":"08:49.460","Text":"especially once you run this thing and see what has actually happened as a result."},{"Start":"08:49.460 ","End":"08:55.940","Text":"I think I\u0027ve got too many brackets there now and that does this."},{"Start":"08:55.940 ","End":"08:58.400","Text":"There\u0027s our first contact."},{"Start":"08:58.400 ","End":"09:03.340","Text":"I just copy that and change the details for the other people and of course,"},{"Start":"09:03.340 ","End":"09:05.510","Text":"you probably wouldn\u0027t have code like this."},{"Start":"09:05.510 ","End":"09:08.120","Text":"These would come from variable\u0027s type to"},{"Start":"09:08.120 ","End":"09:11.630","Text":"the keyboard or from a graphical user interface or something."},{"Start":"09:11.630 ","End":"09:14.825","Text":"But this is just to get the idea."},{"Start":"09:14.825 ","End":"09:20.615","Text":"We had just covering this concept for the first time."},{"Start":"09:20.615 ","End":"09:23.840","Text":"Just a different phone number I can put anything in"},{"Start":"09:23.840 ","End":"09:27.995","Text":"here and different phone number for this person."},{"Start":"09:27.995 ","End":"09:32.440","Text":"Penalty, email address."},{"Start":"09:32.480 ","End":"09:38.480","Text":"Those are our 3 contacts being created here,"},{"Start":"09:38.480 ","End":"09:45.129","Text":"objects created using new and then a reference is past 2 contacts,"},{"Start":"09:45.129 ","End":"09:49.045","Text":"ArrayList and stored in there and it added in that order."},{"Start":"09:49.045 ","End":"09:51.130","Text":"Then in Part F,"},{"Start":"09:51.130 ","End":"09:54.760","Text":"we\u0027ve been asked to use a for each style loop to"},{"Start":"09:54.760 ","End":"10:01.000","Text":"output the contact each of those contexts on a new line."},{"Start":"10:01.000 ","End":"10:09.235","Text":"That\u0027s straightforward enough the syntax for each loop is as follows."},{"Start":"10:09.235 ","End":"10:13.450","Text":"We say for then we need brackets,"},{"Start":"10:13.450 ","End":"10:15.355","Text":"and then there\u0027s a body."},{"Start":"10:15.355 ","End":"10:21.980","Text":"Now the syntax for this is,"},{"Start":"10:21.980 ","End":"10:30.935","Text":"so we say, what is the type of element that we\u0027re iterating through and it\u0027s a contact."},{"Start":"10:30.935 ","End":"10:36.740","Text":"What do you want to call it in this loop and we\u0027ll just call it item,"},{"Start":"10:36.740 ","End":"10:40.310","Text":"you can call it anything you like this useful."},{"Start":"10:40.310 ","End":"10:44.210","Text":"What is the collection that we\u0027re iterating through?"},{"Start":"10:44.210 ","End":"10:47.945","Text":"In our case, it\u0027s contacts."},{"Start":"10:47.945 ","End":"10:52.475","Text":"We\u0027re going to create an object called item,"},{"Start":"10:52.475 ","End":"10:56.225","Text":"which takes on new values each time around this loop."},{"Start":"10:56.225 ","End":"11:04.050","Text":"Item is of type contact and comes from the ArrayList called contact."},{"Start":"11:04.050 ","End":"11:07.475","Text":"Every time around a loop, item will have a different value."},{"Start":"11:07.475 ","End":"11:10.760","Text":"We don\u0027t need to do any maintenance of any pointers or anything like that."},{"Start":"11:10.760 ","End":"11:12.560","Text":"It will be done for us."},{"Start":"11:12.560 ","End":"11:15.440","Text":"I can just do what I want with it in this case,"},{"Start":"11:15.440 ","End":"11:18.880","Text":"all I\u0027ve got to do is to print it out."},{"Start":"11:18.920 ","End":"11:22.790","Text":"That will do is assuming that"},{"Start":"11:22.790 ","End":"11:29.210","Text":"the contacts object print something useful using its toString method,"},{"Start":"11:29.210 ","End":"11:36.945","Text":"which of course we overrode and this therefore should do something useful for us."},{"Start":"11:36.945 ","End":"11:42.965","Text":"The only thing left to do is Part G to run it and see if it does what we expect."},{"Start":"11:42.965 ","End":"11:46.550","Text":"Let\u0027s go ahead and do that and there\u0027s address book."},{"Start":"11:46.550 ","End":"11:52.895","Text":"It does print all of the details of all of the contacts that we added"},{"Start":"11:52.895 ","End":"12:00.080","Text":"to the ArrayList and the reason it did it is because key bit here is here."},{"Start":"12:00.080 ","End":"12:04.910","Text":"It created objects using the values passed in and"},{"Start":"12:04.910 ","End":"12:09.455","Text":"they were storing away into these attributes and the various values passed in."},{"Start":"12:09.455 ","End":"12:13.250","Text":"But when we were asked to run the toString method,"},{"Start":"12:13.250 ","End":"12:20.275","Text":"it returned all those fields with the literal text in front of it and"},{"Start":"12:20.275 ","End":"12:28.160","Text":"the only place where that would happen will be in this line here."},{"Start":"12:28.160 ","End":"12:31.610","Text":"We\u0027re not saying item and then dot toString,"},{"Start":"12:31.610 ","End":"12:33.530","Text":"but that\u0027s what\u0027s happening by default."},{"Start":"12:33.530 ","End":"12:38.075","Text":"We\u0027re not getting individual fields and concatenating them together."},{"Start":"12:38.075 ","End":"12:40.745","Text":"That\u0027s done by the toString method."},{"Start":"12:40.745 ","End":"12:43.940","Text":"Hence, this very short piece of code here,"},{"Start":"12:43.940 ","End":"12:49.250","Text":"both adds a load of items to an ArrayList and then"},{"Start":"12:49.250 ","End":"12:54.720","Text":"print out all of them and we could have 3,000."},{"Start":"12:54.720 ","End":"12:58.430","Text":"This code here doesn\u0027t change so nice compact way of"},{"Start":"12:58.430 ","End":"13:02.510","Text":"iterating through items inside a connection."},{"Start":"13:02.510 ","End":"13:05.610","Text":"That\u0027s it for this one. Thanks for watching. See you soon."}],"ID":31164},{"Watched":false,"Name":"Exercise 3 Part 1","Duration":"8m 37s","ChapterTopicVideoID":29553,"CourseChapterTopicPlaylistID":294562,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.000 ","End":"00:06.090","Text":"Hello again. We\u0027re told in this exercise to modify the address book method we created"},{"Start":"00:06.090 ","End":"00:11.940","Text":"in the previous exercise to allow a user to input new contexts and search for them."},{"Start":"00:11.940 ","End":"00:15.180","Text":"In Part A, we are told to move the code to display"},{"Start":"00:15.180 ","End":"00:19.770","Text":"all the contacts into a new method called listContacts."},{"Start":"00:19.770 ","End":"00:22.560","Text":"In Part B, we\u0027re told to use a Scanner object and"},{"Start":"00:22.560 ","End":"00:25.980","Text":"do...while loop to take a command from the user."},{"Start":"00:25.980 ","End":"00:28.800","Text":"\"Add\" allows a user to enter the details for"},{"Start":"00:28.800 ","End":"00:31.890","Text":"a new contact and add them to the contacts list."},{"Start":"00:31.890 ","End":"00:35.490","Text":"\"List\" displays all current contacts."},{"Start":"00:35.490 ","End":"00:38.025","Text":"\"Exit\" ends the program."},{"Start":"00:38.025 ","End":"00:42.560","Text":"In Part C then, we\u0027re told to run the program and add 2 contacts,"},{"Start":"00:42.560 ","End":"00:46.660","Text":"followed by list command and exit command."},{"Start":"00:46.660 ","End":"00:50.760","Text":"This is the address book method that we had previously."},{"Start":"00:50.760 ","End":"00:54.950","Text":"The first thing we need to do is to move out the code that"},{"Start":"00:54.950 ","End":"00:59.513","Text":"displays or contacts into its own method,"},{"Start":"00:59.513 ","End":"01:01.597","Text":"as what we\u0027ve been asked to do in Part A,"},{"Start":"01:01.597 ","End":"01:04.175","Text":"so that\u0027s straightforward enough."},{"Start":"01:04.175 ","End":"01:08.210","Text":"We assume that it\u0027s going to be public as"},{"Start":"01:08.210 ","End":"01:12.055","Text":"you could be private because it\u0027s a helper method,"},{"Start":"01:12.055 ","End":"01:16.010","Text":"so it\u0027s only used inside the address book code itself."},{"Start":"01:16.010 ","End":"01:21.800","Text":"It doesn\u0027t return anything and is called whatever we want."},{"Start":"01:21.800 ","End":"01:24.245","Text":"Let\u0027s call it listContacts,"},{"Start":"01:24.245 ","End":"01:27.590","Text":"and then we can simply paste in"},{"Start":"01:27.590 ","End":"01:33.065","Text":"that code that we\u0027ve just cut out from the address book method."},{"Start":"01:33.065 ","End":"01:36.514","Text":"Just check that that doesn\u0027t give me any errors and that looks fine,"},{"Start":"01:36.514 ","End":"01:38.495","Text":"so that\u0027s Part A done."},{"Start":"01:38.495 ","End":"01:44.530","Text":"Now, we\u0027re going to use a scanner object to get input from the user."},{"Start":"01:44.530 ","End":"01:47.060","Text":"We\u0027re going to use a do...while loop to do that."},{"Start":"01:47.060 ","End":"01:49.235","Text":"So let\u0027s, first of all,"},{"Start":"01:49.235 ","End":"01:50.870","Text":"create a Scanner object."},{"Start":"01:50.870 ","End":"01:55.730","Text":"We\u0027re using it to take keyboard input so much we\u0027ll call it keyboard."},{"Start":"01:55.730 ","End":"01:59.990","Text":"We need to create that from the Scanner class and"},{"Start":"01:59.990 ","End":"02:04.900","Text":"the Scanner class needs to be told where that input is coming from,"},{"Start":"02:04.900 ","End":"02:08.045","Text":"so we pass a reference to System.in."},{"Start":"02:08.045 ","End":"02:11.870","Text":"It\u0027s going to complain because it doesn\u0027t know what a scanner is"},{"Start":"02:11.870 ","End":"02:15.620","Text":"because we have to import class."},{"Start":"02:15.620 ","End":"02:17.056","Text":"So let\u0027s do that,"},{"Start":"02:17.056 ","End":"02:20.840","Text":"and that should make that particular error go away,"},{"Start":"02:20.840 ","End":"02:22.775","Text":"which it seems to have done."},{"Start":"02:22.775 ","End":"02:28.130","Text":"As before, we probably want to set the delimiter to"},{"Start":"02:28.130 ","End":"02:34.100","Text":"be the new line character to avoid any issues,"},{"Start":"02:34.100 ","End":"02:38.670","Text":"so that should take care of that."},{"Start":"02:38.670 ","End":"02:40.610","Text":"The issues would be, by default,"},{"Start":"02:40.610 ","End":"02:42.755","Text":"it\u0027s split on spaces,"},{"Start":"02:42.755 ","End":"02:45.695","Text":"each new bit of input from the keyboard."},{"Start":"02:45.695 ","End":"02:49.390","Text":"So if you put a full name into contexts,"},{"Start":"02:49.390 ","End":"02:52.305","Text":"it wouldn\u0027t work as you\u0027d want it to."},{"Start":"02:52.305 ","End":"02:55.860","Text":"So that\u0027s the first couple of lines done."},{"Start":"02:55.860 ","End":"02:59.795","Text":"Now we need to create a do...while loop."},{"Start":"02:59.795 ","End":"03:02.015","Text":"Do...while loops are really handy."},{"Start":"03:02.015 ","End":"03:04.370","Text":"Don\u0027t have them in all languages."},{"Start":"03:04.370 ","End":"03:08.810","Text":"The syntax for them is similar to a while loop,"},{"Start":"03:08.810 ","End":"03:11.855","Text":"but the condition is at the end."},{"Start":"03:11.855 ","End":"03:16.520","Text":"So what it allows us to do is to not have"},{"Start":"03:16.520 ","End":"03:22.165","Text":"some code outside the loop and inside the loop and just saves us a couple of lines."},{"Start":"03:22.165 ","End":"03:30.050","Text":"The first thing we want to do inside the loop is to prompt the user to type something in."},{"Start":"03:30.050 ","End":"03:34.535","Text":"I\u0027m going to break it onto a blank line as well."},{"Start":"03:34.535 ","End":"03:37.800","Text":"Let\u0027s put a slash in front of it,"},{"Start":"03:37.800 ","End":"03:41.720","Text":"and then they enter whatever it is that they want to do,"},{"Start":"03:41.720 ","End":"03:45.920","Text":"and then we\u0027re going to need a variable to store their choice into."},{"Start":"03:45.920 ","End":"03:48.385","Text":"Let\u0027s call that choice,"},{"Start":"03:48.385 ","End":"03:55.160","Text":"and that will get whatever has been typed on the keyboard."},{"Start":"03:55.160 ","End":"03:58.925","Text":"Use keyboard.next to do that,"},{"Start":"03:58.925 ","End":"04:03.245","Text":"and I\u0027ll need to create my string choice up here."},{"Start":"04:03.245 ","End":"04:05.720","Text":"But condition, I need to put it at the end here,"},{"Start":"04:05.720 ","End":"04:08.690","Text":"is, when is this loop going to end?"},{"Start":"04:08.690 ","End":"04:17.540","Text":"I want it to be running until I get exit."},{"Start":"04:17.540 ","End":"04:19.595","Text":"So what I\u0027m going to need to do is going to say,"},{"Start":"04:19.595 ","End":"04:23.465","Text":"while choice is not equal to exit,"},{"Start":"04:23.465 ","End":"04:25.475","Text":"I keep going around this loop."},{"Start":"04:25.475 ","End":"04:28.840","Text":"Otherwise, I exit the loop."},{"Start":"04:28.840 ","End":"04:31.420","Text":"That should do it,"},{"Start":"04:31.420 ","End":"04:33.320","Text":"and this should be on 1 line, I think,"},{"Start":"04:33.320 ","End":"04:36.576","Text":"and that\u0027s why it\u0027s complaining,"},{"Start":"04:36.576 ","End":"04:39.060","Text":"equals, there we go."},{"Start":"04:39.060 ","End":"04:41.600","Text":"That\u0027s the kind of core bit."},{"Start":"04:41.600 ","End":"04:44.570","Text":"It gets something from the keyboard,"},{"Start":"04:44.570 ","End":"04:46.643","Text":"stores into the choice variable,"},{"Start":"04:46.643 ","End":"04:48.514","Text":"so long as that\u0027s not exit,"},{"Start":"04:48.514 ","End":"04:50.585","Text":"it will keep going around that loop."},{"Start":"04:50.585 ","End":"04:53.555","Text":"What about the actual commands themselves?"},{"Start":"04:53.555 ","End":"04:57.110","Text":"We\u0027ve always already got the listContacts."},{"Start":"04:57.110 ","End":"05:00.680","Text":"So we might as well add that."},{"Start":"05:00.680 ","End":"05:06.125","Text":"Let\u0027s do that. The choice,"},{"Start":"05:06.125 ","End":"05:10.440","Text":"he pulls the list."},{"Start":"05:10.820 ","End":"05:17.685","Text":"We\u0027re going to run a listContact method."},{"Start":"05:17.685 ","End":"05:24.163","Text":"The important one is the \"Add\", which is,"},{"Start":"05:24.163 ","End":"05:31.190","Text":"I\u0027ll use the same line there, choice.equals."},{"Start":"05:31.190 ","End":"05:32.615","Text":"Contents can be different."},{"Start":"05:32.615 ","End":"05:37.580","Text":"It can be \"add\", and to add something,"},{"Start":"05:37.580 ","End":"05:40.805","Text":"we\u0027re going to need to get more things from the keyboard."},{"Start":"05:40.805 ","End":"05:43.580","Text":"So let\u0027s create some variables to store those."},{"Start":"05:43.580 ","End":"05:46.219","Text":"We need the name, the email,"},{"Start":"05:46.219 ","End":"05:50.865","Text":"and the mobile number."},{"Start":"05:50.865 ","End":"05:56.220","Text":"We need a prompt to say what it is that should be entered."},{"Start":"05:57.350 ","End":"06:00.375","Text":"Let\u0027s prompt the user for that."},{"Start":"06:00.375 ","End":"06:03.090","Text":"First of all, we want their name,"},{"Start":"06:03.090 ","End":"06:08.020","Text":"and then we\u0027ll store that into the name variable,"},{"Start":"06:08.810 ","End":"06:11.545","Text":"whatever was typed in,"},{"Start":"06:11.545 ","End":"06:16.735","Text":"and all the same thing for all those other fields that we\u0027re going to store away."},{"Start":"06:16.735 ","End":"06:19.753","Text":"So we got name."},{"Start":"06:19.753 ","End":"06:21.660","Text":"Let\u0027s get email."},{"Start":"06:21.660 ","End":"06:24.657","Text":"Let\u0027s store that into email."},{"Start":"06:24.657 ","End":"06:30.275","Text":"Then finally, let\u0027s get the mobile number."},{"Start":"06:30.275 ","End":"06:33.623","Text":"All of our fields entered at the keyboard,"},{"Start":"06:33.623 ","End":"06:39.167","Text":"and we now need to create an object and add it to the ArrayList,"},{"Start":"06:39.167 ","End":"06:42.080","Text":"and we\u0027ve already done that so it\u0027s doing the same thing,"},{"Start":"06:42.080 ","End":"06:44.180","Text":"but using variables this time."},{"Start":"06:44.180 ","End":"06:48.335","Text":"The ArrayList is called Contacts."},{"Start":"06:48.335 ","End":"06:51.835","Text":"The method we used to add is called Add,"},{"Start":"06:51.835 ","End":"06:55.294","Text":"and the thing we want to add is an object."},{"Start":"06:55.294 ","End":"07:00.250","Text":"So we need to create a new object of type Contact,"},{"Start":"07:00.250 ","End":"07:03.680","Text":"and then pass in the values that we want"},{"Start":"07:03.680 ","End":"07:09.150","Text":"to store away in that particular object instance."},{"Start":"07:09.150 ","End":"07:11.730","Text":"That\u0027s going to be name, email,"},{"Start":"07:11.730 ","End":"07:17.740","Text":"mobile because those are the things we\u0027ve just collected from the user."},{"Start":"07:17.740 ","End":"07:20.168","Text":"That should be it."},{"Start":"07:20.168 ","End":"07:21.750","Text":"We can do Part C,"},{"Start":"07:21.750 ","End":"07:23.265","Text":"which is the test."},{"Start":"07:23.265 ","End":"07:25.920","Text":"There doesn\u0027t seem to be any errors there."},{"Start":"07:25.920 ","End":"07:28.935","Text":"So let\u0027s see what happens when we test this."},{"Start":"07:28.935 ","End":"07:32.290","Text":"Aviv, could you just go back a little bit?"},{"Start":"07:32.290 ","End":"07:35.630","Text":"That bit at the beginning,"},{"Start":"07:35.630 ","End":"07:37.415","Text":"as I just want to clear this output."},{"Start":"07:37.415 ","End":"07:39.950","Text":"Let\u0027s see what happens when we run this."},{"Start":"07:39.950 ","End":"07:45.515","Text":"Create a new object from collections and run the address book method."},{"Start":"07:45.515 ","End":"07:47.795","Text":"So it prompted for our command,"},{"Start":"07:47.795 ","End":"07:51.424","Text":"so let\u0027s try list first of all to see if the existing things,"},{"Start":"07:51.424 ","End":"07:55.790","Text":"so we\u0027d hardcoded those contacts into our programs. So they\u0027re there."},{"Start":"07:55.790 ","End":"07:58.255","Text":"Let\u0027s try and add one now."},{"Start":"07:58.255 ","End":"08:02.996","Text":"It\u0027s tough to think of names when you\u0027ve got so many in there."},{"Start":"08:02.996 ","End":"08:05.285","Text":"Let\u0027s just put anything in."},{"Start":"08:05.285 ","End":"08:07.890","Text":"Email\u0027s next."},{"Start":"08:07.890 ","End":"08:09.710","Text":"So we\u0027ve added something,"},{"Start":"08:09.710 ","End":"08:11.650","Text":"and it\u0027s asking us for another command now."},{"Start":"08:11.650 ","End":"08:15.010","Text":"If we list, we should be able to see the one that we\u0027ve added."},{"Start":"08:15.010 ","End":"08:17.190","Text":"Thankfully, it is,"},{"Start":"08:17.190 ","End":"08:19.845","Text":"and it\u0027s the last one there."},{"Start":"08:19.845 ","End":"08:26.735","Text":"That\u0027s the basics of this done up to Part C. We\u0027ll break"},{"Start":"08:26.735 ","End":"08:30.770","Text":"for the next video where we\u0027ll develop the last"},{"Start":"08:30.770 ","End":"08:35.660","Text":"bit where we try and find a contact and we use an iterator object to do that."},{"Start":"08:35.660 ","End":"08:38.070","Text":"I\u0027ll see you in a moment for that one."}],"ID":31165},{"Watched":false,"Name":"Exercise 3 Part 2","Duration":"9m 7s","ChapterTopicVideoID":29554,"CourseChapterTopicPlaylistID":294562,"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.055","Text":"Continuing on from the previous part,"},{"Start":"00:05.055 ","End":"00:08.445","Text":"where we finished by running the program and adding 2 contacts."},{"Start":"00:08.445 ","End":"00:13.770","Text":"We are now asked to develop the program so a new command of Find allows a user to find"},{"Start":"00:13.770 ","End":"00:16.490","Text":"a contact by entering their name and either"},{"Start":"00:16.490 ","End":"00:20.030","Text":"display that contact\u0027s details or contact not found."},{"Start":"00:20.030 ","End":"00:22.925","Text":"We\u0027re told to use an iterator object to iterate"},{"Start":"00:22.925 ","End":"00:26.165","Text":"through the contacts rather than a for-loop,"},{"Start":"00:26.165 ","End":"00:27.530","Text":"and then in Part E,"},{"Start":"00:27.530 ","End":"00:30.485","Text":"we\u0027re told to test by adding new contacts,"},{"Start":"00:30.485 ","End":"00:32.089","Text":"Anita and Andrew,"},{"Start":"00:32.089 ","End":"00:38.194","Text":"and attempt to find Anita\u0027s details in the contact list using the Find Command."},{"Start":"00:38.194 ","End":"00:41.720","Text":"So having reached the point where we can add contacts and list them,"},{"Start":"00:41.720 ","End":"00:50.585","Text":"we want to impart the Find Contacts so we add another if-statement here to"},{"Start":"00:50.585 ","End":"00:59.560","Text":"pick up the new command and if that is picked up,"},{"Start":"00:59.560 ","End":"01:02.620","Text":"then we\u0027ve got to do a number of things,"},{"Start":"01:02.620 ","End":"01:05.289","Text":"but to get some input from the keyboard,"},{"Start":"01:05.289 ","End":"01:09.770","Text":"so let\u0027s create a variable for that."},{"Start":"01:11.160 ","End":"01:14.080","Text":"This is the person we\u0027re searching for,"},{"Start":"01:14.080 ","End":"01:16.015","Text":"is going to be sold in there."},{"Start":"01:16.015 ","End":"01:19.240","Text":"Then we can ask for a name to be found,"},{"Start":"01:19.240 ","End":"01:24.026","Text":"and if that is going to be very similar to the adding contacts."},{"Start":"01:24.026 ","End":"01:27.840","Text":"We might as well use that."},{"Start":"01:27.840 ","End":"01:29.890","Text":"Now we\u0027ve got it."},{"Start":"01:29.890 ","End":"01:32.920","Text":"We can look for it but we want to"},{"Start":"01:32.920 ","End":"01:38.140","Text":"create an iterator object so that\u0027s the tricky bit in this one."},{"Start":"01:38.140 ","End":"01:41.480","Text":"So rather than using an enhanced for loop,"},{"Start":"01:41.480 ","End":"01:43.888","Text":"we\u0027re going to use this new type of object,"},{"Start":"01:43.888 ","End":"01:47.060","Text":"which allows us to iterate through collections."},{"Start":"01:47.060 ","End":"01:50.075","Text":"To declare it, we say,"},{"Start":"01:50.075 ","End":"01:54.635","Text":"\"What sort of object we\u0027re going to be iterating through?\""},{"Start":"01:54.635 ","End":"01:56.015","Text":"So in our case,"},{"Start":"01:56.015 ","End":"01:57.485","Text":"it\u0027s a contact,"},{"Start":"01:57.485 ","End":"02:00.320","Text":"and then we say what the name of it,"},{"Start":"02:00.320 ","End":"02:02.285","Text":"what this object should be called."},{"Start":"02:02.285 ","End":"02:04.700","Text":"We can just call it IT for iterator,"},{"Start":"02:04.700 ","End":"02:07.355","Text":"and then this is the tricky bit."},{"Start":"02:07.355 ","End":"02:12.710","Text":"We have to say what is the list that we\u0027re operating on."},{"Start":"02:12.710 ","End":"02:20.240","Text":"From that list, there should be a method with a i called iterator that will"},{"Start":"02:20.240 ","End":"02:28.275","Text":"return the iterator object and store a reference to it in IT."},{"Start":"02:28.275 ","End":"02:32.140","Text":"So that is the tricky bit really is to create"},{"Start":"02:32.140 ","End":"02:36.160","Text":"this iterator object and using it is relatively straightforward."},{"Start":"02:36.160 ","End":"02:41.530","Text":"We can have a while loop now and be saying that while loop."},{"Start":"02:41.530 ","End":"02:45.265","Text":"So inside this while loop, we\u0027re going to say,"},{"Start":"02:45.265 ","End":"02:50.035","Text":"use the iterator object called it to find out"},{"Start":"02:50.035 ","End":"02:54.295","Text":"if there is anything else left to iterate through,"},{"Start":"02:54.295 ","End":"02:56.740","Text":"and it\u0027s a hasNext method."},{"Start":"02:56.740 ","End":"03:01.840","Text":"Similar thing exists in a scanner you may have used before in a previous program."},{"Start":"03:01.840 ","End":"03:07.550","Text":"If there is still another item in the collection to be got,"},{"Start":"03:07.550 ","End":"03:09.365","Text":"then keep going around the while loop."},{"Start":"03:09.365 ","End":"03:12.605","Text":"Otherwise dropout the end of the while loop."},{"Start":"03:12.605 ","End":"03:15.335","Text":"That\u0027s the first bit done."},{"Start":"03:15.335 ","End":"03:20.570","Text":"Now what we need to do is we actually need to get the object out and store it"},{"Start":"03:20.570 ","End":"03:27.290","Text":"temporarily into temporary objects so that we can inspect it and do things with it."},{"Start":"03:27.290 ","End":"03:30.890","Text":"The object is going to be of type contact."},{"Start":"03:30.890 ","End":"03:36.365","Text":"We can just call our object temp for temporary."},{"Start":"03:36.365 ","End":"03:39.605","Text":"We want to and get it,"},{"Start":"03:39.605 ","End":"03:44.145","Text":"we use it.next because that was our iterator name,"},{"Start":"03:44.145 ","End":"03:47.390","Text":"and we\u0027re going to get the next thing from the iterator."},{"Start":"03:47.390 ","End":"03:52.335","Text":"So that now stores current object into temp,"},{"Start":"03:52.335 ","End":"03:55.245","Text":"and now we can do stuff with temp."},{"Start":"03:55.245 ","End":"04:00.000","Text":"In our case, what we want to do is we want to check"},{"Start":"04:00.000 ","End":"04:06.680","Text":"if the name that\u0027s stored into that object that we just got."},{"Start":"04:06.680 ","End":"04:11.825","Text":"We can get that name using the getName method that we implemented."},{"Start":"04:11.825 ","End":"04:20.240","Text":"We want to know if it contains the search name that we just put in."},{"Start":"04:20.240 ","End":"04:21.695","Text":"You could use equals here,"},{"Start":"04:21.695 ","End":"04:24.220","Text":"then you\u0027d have to have an exact match."},{"Start":"04:24.220 ","End":"04:26.450","Text":"But contains is often better when"},{"Start":"04:26.450 ","End":"04:31.190","Text":"you\u0027re searching for strings because this will also match on substrings."},{"Start":"04:31.190 ","End":"04:33.980","Text":"So just to explain that again."},{"Start":"04:33.980 ","End":"04:37.055","Text":"We got, using the iterator,"},{"Start":"04:37.055 ","End":"04:41.375","Text":"an object of type contact and stored it into temp."},{"Start":"04:41.375 ","End":"04:44.330","Text":"From temp now, we can get the name that was"},{"Start":"04:44.330 ","End":"04:47.300","Text":"stored in the current object that we\u0027re looking at in this loop,"},{"Start":"04:47.300 ","End":"04:49.955","Text":"and we\u0027re going to check whether it contains"},{"Start":"04:49.955 ","End":"04:53.930","Text":"the name that we\u0027re searching for, search name."},{"Start":"04:53.930 ","End":"04:58.025","Text":"If we have found that person,"},{"Start":"04:58.025 ","End":"05:02.995","Text":"then we can print out their details,"},{"Start":"05:02.995 ","End":"05:07.295","Text":"and that\u0027s easy enough because we just have to say temp."},{"Start":"05:07.295 ","End":"05:12.365","Text":"Remember, it will print out using the 2 string method,"},{"Start":"05:12.365 ","End":"05:15.370","Text":"the current details of that contact."},{"Start":"05:15.370 ","End":"05:17.255","Text":"So that\u0027s all we have to do."},{"Start":"05:17.255 ","End":"05:21.695","Text":"However, just slightly more complex than this because"},{"Start":"05:21.695 ","End":"05:26.660","Text":"we\u0027ve been told that if they don\u0027t find that person,"},{"Start":"05:26.660 ","End":"05:28.400","Text":"then we need to print a message,"},{"Start":"05:28.400 ","End":"05:31.355","Text":"and we got to choose quite carefully where that happens."},{"Start":"05:31.355 ","End":"05:36.665","Text":"Because we can only print that message if it\u0027s checked everything and not found it."},{"Start":"05:36.665 ","End":"05:38.705","Text":"So what we probably should do is create"},{"Start":"05:38.705 ","End":"05:44.030","Text":"a Boolean variable and use that as a flag to say whether it\u0027s been found or not."},{"Start":"05:44.030 ","End":"05:48.780","Text":"So we start off by defaulting to not found,"},{"Start":"05:48.780 ","End":"05:50.675","Text":"and if it is found,"},{"Start":"05:50.675 ","End":"05:53.210","Text":"then we\u0027ll say that we have found it,"},{"Start":"05:53.210 ","End":"05:54.695","Text":"and if it\u0027s not,"},{"Start":"05:54.695 ","End":"05:58.580","Text":"then we\u0027ll check for that at the end of our loop here."},{"Start":"05:58.580 ","End":"06:02.855","Text":"After we\u0027ve done all of the objects in this collection,"},{"Start":"06:02.855 ","End":"06:04.920","Text":"we can check whether it\u0027s found or not,"},{"Start":"06:04.920 ","End":"06:06.530","Text":"and if it wasn\u0027t,"},{"Start":"06:06.530 ","End":"06:09.004","Text":"if it was not found,"},{"Start":"06:09.004 ","End":"06:13.370","Text":"then we can print a suitable message."},{"Start":"06:13.370 ","End":"06:20.371","Text":"System out.print line, and we\u0027ll just say contact."},{"Start":"06:20.371 ","End":"06:23.195","Text":"That should be it."},{"Start":"06:23.195 ","End":"06:26.315","Text":"So we\u0027ve implemented our new Find method,"},{"Start":"06:26.315 ","End":"06:31.503","Text":"which creates a string temporary one for keyboard input to be got."},{"Start":"06:31.503 ","End":"06:35.540","Text":"it stores that into search name, that keyboard input."},{"Start":"06:35.540 ","End":"06:38.720","Text":"Then it creates an iterator object,"},{"Start":"06:38.720 ","End":"06:40.395","Text":"which we\u0027ve called IT,"},{"Start":"06:40.395 ","End":"06:42.410","Text":"and it\u0027s iterating through"},{"Start":"06:42.410 ","End":"06:46.220","Text":"the contacts ArrayList that we\u0027ve created and put these addresses"},{"Start":"06:46.220 ","End":"06:52.070","Text":"into a Boolean flag variable here just to see whether the person has been found or not."},{"Start":"06:52.070 ","End":"06:56.675","Text":"Then we use the iterator\u0027s hasNext method which will control the loop."},{"Start":"06:56.675 ","End":"07:00.680","Text":"It\u0027ll keep looping until hasNext returns false."},{"Start":"07:00.680 ","End":"07:05.045","Text":"Then we have a temporary object which we use to"},{"Start":"07:05.045 ","End":"07:10.175","Text":"retrieve current object using the iterator from the collection,"},{"Start":"07:10.175 ","End":"07:17.210","Text":"and then we compare that to what was typed in using contains rather than equals,"},{"Start":"07:17.210 ","End":"07:19.175","Text":"which is a bit more flexible,"},{"Start":"07:19.175 ","End":"07:23.990","Text":"and that then will either print out a record"},{"Start":"07:23.990 ","End":"07:29.390","Text":"or continue on until the collection has all been iterated through."},{"Start":"07:29.390 ","End":"07:30.875","Text":"If we never found it,"},{"Start":"07:30.875 ","End":"07:32.840","Text":"we print contact not found."},{"Start":"07:32.840 ","End":"07:34.145","Text":"So that\u0027s the theory."},{"Start":"07:34.145 ","End":"07:35.660","Text":"Let\u0027s see if this actually worked."},{"Start":"07:35.660 ","End":"07:38.745","Text":"So let\u0027s run that now see what happens."},{"Start":"07:38.745 ","End":"07:41.630","Text":"So run the address book in the test."},{"Start":"07:41.630 ","End":"07:46.490","Text":"We\u0027ve been told to type in a couple more contacts so let\u0027s do that."},{"Start":"07:46.490 ","End":"07:49.415","Text":"Let\u0027s add Anita."},{"Start":"07:49.415 ","End":"07:51.355","Text":"Look up her number as usual,"},{"Start":"07:51.355 ","End":"07:53.938","Text":"and then let\u0027s add another person,"},{"Start":"07:53.938 ","End":"07:56.550","Text":"Andrew, their email,"},{"Start":"07:56.550 ","End":"07:58.905","Text":"and a number, and then we\u0027re done."},{"Start":"07:58.905 ","End":"08:02.360","Text":"Now let\u0027s list our contacts just so that we know who we\u0027ve got in there."},{"Start":"08:02.360 ","End":"08:05.840","Text":"So these were the 3 that we hard-coded and then these are the two"},{"Start":"08:05.840 ","End":"08:09.740","Text":"that we just added so we\u0027re now going to try and find the person."},{"Start":"08:09.740 ","End":"08:13.100","Text":"The person we\u0027re going to look for is Anita,"},{"Start":"08:13.100 ","End":"08:15.780","Text":"and with luck, we will see her details."},{"Start":"08:15.780 ","End":"08:18.810","Text":"Brilliant. it has shown her details."},{"Start":"08:18.810 ","End":"08:21.920","Text":"Actually, this will work if we put A-N as well for"},{"Start":"08:21.920 ","End":"08:26.215","Text":"Andrew and Anita because they both contain A-N."},{"Start":"08:26.215 ","End":"08:30.855","Text":"Interesting, that hasn\u0027t worked because the command was wrong."},{"Start":"08:30.855 ","End":"08:34.370","Text":"That\u0027s what name to find is an. There we go."},{"Start":"08:34.370 ","End":"08:38.857","Text":"So Anita and Andrew had been found because they both contain A-N."},{"Start":"08:38.857 ","End":"08:42.560","Text":"But if I tried to find A-N-D,"},{"Start":"08:42.560 ","End":"08:44.945","Text":"you would only find Andrew."},{"Start":"08:44.945 ","End":"08:47.554","Text":"Then that\u0027s a difference between contain an equals."},{"Start":"08:47.554 ","End":"08:49.100","Text":"It\u0027s not looking for an exact match,"},{"Start":"08:49.100 ","End":"08:51.980","Text":"but it\u0027s looking if these consecutive characters with"},{"Start":"08:51.980 ","End":"08:56.090","Text":"the same case are found in the string we\u0027re searching through."},{"Start":"08:56.090 ","End":"08:58.550","Text":"That appears to have worked."},{"Start":"08:58.550 ","End":"09:01.655","Text":"We used the iterator method for it so slightly different way"},{"Start":"09:01.655 ","End":"09:05.330","Text":"of iterating through a collection than the for-each loop."},{"Start":"09:05.330 ","End":"09:08.070","Text":"Thanks for watching. See you in the next one."}],"ID":31166},{"Watched":false,"Name":"Exercise 4","Duration":"9m 22s","ChapterTopicVideoID":29555,"CourseChapterTopicPlaylistID":294562,"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.930","Text":"Hello, welcome. In this first part of the exercise,"},{"Start":"00:03.930 ","End":"00:06.720","Text":"we\u0027re told a hashCode is an integer value that is"},{"Start":"00:06.720 ","End":"00:10.695","Text":"generated using an algorithm applied to some input."},{"Start":"00:10.695 ","End":"00:14.100","Text":"With objects, it can be used to identify an object."},{"Start":"00:14.100 ","End":"00:18.090","Text":"When a user-defined class overrides the equals method,"},{"Start":"00:18.090 ","End":"00:21.150","Text":"it should also override the hashCode method so"},{"Start":"00:21.150 ","End":"00:25.350","Text":"the objects that are equal produce identical hashCode values."},{"Start":"00:25.350 ","End":"00:31.020","Text":"We\u0027re then told to create a new project and within it a new class called Song,"},{"Start":"00:31.020 ","End":"00:36.240","Text":"as follows, and a UML Class Diagram is given for Song."},{"Start":"00:36.240 ","End":"00:39.215","Text":"In Part b, we\u0027re told to implement all the methods,"},{"Start":"00:39.215 ","End":"00:42.620","Text":"including an overwritten 2 string method that returns"},{"Start":"00:42.620 ","End":"00:46.490","Text":"the 3 fields separated by commas and a single space,"},{"Start":"00:46.490 ","End":"00:48.415","Text":"and an example is given."},{"Start":"00:48.415 ","End":"00:49.910","Text":"Part c, we\u0027re told that"},{"Start":"00:49.910 ","End":"00:54.500","Text":"the overridden equals method should compare the attribute values for"},{"Start":"00:54.500 ","End":"01:01.535","Text":"name and artist and should return true only if both match in the object being compared."},{"Start":"01:01.535 ","End":"01:04.580","Text":"Then we\u0027re told to create 3 objects from this new class on"},{"Start":"01:04.580 ","End":"01:07.775","Text":"the object workbench using the following data."},{"Start":"01:07.775 ","End":"01:11.500","Text":"Firstly, this, then that,"},{"Start":"01:11.500 ","End":"01:15.385","Text":"and then a third duplicate set of values."},{"Start":"01:15.385 ","End":"01:21.160","Text":"We then run tests by creating objects with the same values as before in part d,"},{"Start":"01:21.160 ","End":"01:24.730","Text":"and then check that song 1 and 3 are equal,"},{"Start":"01:24.730 ","End":"01:27.550","Text":"song 1 and 2 are not equal,"},{"Start":"01:27.550 ","End":"01:31.734","Text":"song 2 and 3 are also not equal."},{"Start":"01:31.734 ","End":"01:36.205","Text":"We then are told to run the hashCode method on each of the objects,"},{"Start":"01:36.205 ","End":"01:39.220","Text":"and we\u0027re asked if the integer value returned is the"},{"Start":"01:39.220 ","End":"01:44.180","Text":"same for any of the 3 objects, and why."},{"Start":"01:44.780 ","End":"01:48.370","Text":"Having created the new project and new class,"},{"Start":"01:48.370 ","End":"01:51.330","Text":"we can start to implement the attributes."},{"Start":"01:51.330 ","End":"01:53.090","Text":"We got 2 strings,"},{"Start":"01:53.090 ","End":"01:56.890","Text":"but a private name and artist."},{"Start":"01:56.890 ","End":"02:02.145","Text":"We also have an int called \"year.\""},{"Start":"02:02.145 ","End":"02:04.250","Text":"There\u0027s our attributes done,"},{"Start":"02:04.250 ","End":"02:07.840","Text":"and we can create our constructor now."},{"Start":"02:07.840 ","End":"02:15.690","Text":"That\u0027s going to take in all those attributes and set them so should be pretty,"},{"Start":"02:15.690 ","End":"02:19.350","Text":"used to doing this by now and that\u0027s done."},{"Start":"02:19.350 ","End":"02:22.850","Text":"What we\u0027ve got to do now is our accessor methods."},{"Start":"02:22.850 ","End":"02:26.075","Text":"Select the methods, whatever you want to call them, get this,"},{"Start":"02:26.075 ","End":"02:29.160","Text":"just to return those fields,"},{"Start":"02:29.160 ","End":"02:31.125","Text":"so 2 of them return strings,"},{"Start":"02:31.125 ","End":"02:33.360","Text":"and 1 of them returns an integer."},{"Start":"02:33.360 ","End":"02:39.845","Text":"We\u0027ve been asked also to make sure we override the 2 string method."},{"Start":"02:39.845 ","End":"02:47.410","Text":"To simply concatenate all of the field values with a space and a comma in between."},{"Start":"02:47.410 ","End":"02:49.580","Text":"That\u0027s straightforward enough."},{"Start":"02:49.580 ","End":"02:55.370","Text":"We should probably put the override annotation above"},{"Start":"02:55.370 ","End":"03:01.245","Text":"just to check that we\u0027ve got the right signature and it seems to be right."},{"Start":"03:01.245 ","End":"03:05.040","Text":"What we\u0027re going to return is the following,"},{"Start":"03:05.040 ","End":"03:07.850","Text":"and because this last field is an integer,"},{"Start":"03:07.850 ","End":"03:12.730","Text":"we need to convert it to a string for this expression to work."},{"Start":"03:12.730 ","End":"03:17.245","Text":"That should do that job and no errors so far."},{"Start":"03:17.245 ","End":"03:23.120","Text":"Then the part c, we\u0027re asked to override the equals method."},{"Start":"03:23.120 ","End":"03:29.705","Text":"Both of these come down from the object class that every Java object inherits from."},{"Start":"03:29.705 ","End":"03:33.230","Text":"It\u0027s our job to override them if necessary."},{"Start":"03:33.230 ","End":"03:39.390","Text":"For equals, what we\u0027re going to do is return a Boolean."},{"Start":"03:39.390 ","End":"03:43.835","Text":"The parameter you use is it\u0027s an object,"},{"Start":"03:43.835 ","End":"03:45.770","Text":"it\u0027s of type object."},{"Start":"03:45.770 ","End":"03:48.755","Text":"Call it whatever you like. I\u0027ll just call mine o."},{"Start":"03:48.755 ","End":"03:52.460","Text":"The only tricky bit about this is you need to do"},{"Start":"03:52.460 ","End":"03:57.410","Text":"a cost to whatever your class is going to be."},{"Start":"03:57.410 ","End":"04:01.595","Text":"In our case, it\u0027s going to be a Song."},{"Start":"04:01.595 ","End":"04:06.200","Text":"We say call it temp for temporary."},{"Start":"04:06.200 ","End":"04:14.765","Text":"We say that that is whatever\u0027s being passed in but cast into a song object."},{"Start":"04:14.765 ","End":"04:16.745","Text":"Now we\u0027ve got in temp,"},{"Start":"04:16.745 ","End":"04:18.440","Text":"whatever has been passed in,"},{"Start":"04:18.440 ","End":"04:23.945","Text":"and we\u0027re then going to just compare some of the fields within the 2,"},{"Start":"04:23.945 ","End":"04:26.585","Text":"what we\u0027ve got stored away."},{"Start":"04:26.585 ","End":"04:30.810","Text":"We\u0027ve got stored away in name, something,"},{"Start":"04:30.810 ","End":"04:34.910","Text":"and we want to make sure that what\u0027s been passed in is the same thing."},{"Start":"04:34.910 ","End":"04:36.995","Text":"That\u0027s temp.name."},{"Start":"04:36.995 ","End":"04:40.985","Text":"Remember strings, you can\u0027t compare with a double equals sign."},{"Start":"04:40.985 ","End":"04:44.330","Text":"We want to make sure that the name fields match on"},{"Start":"04:44.330 ","End":"04:47.540","Text":"the object passed in and what we\u0027ve got stored away name,"},{"Start":"04:47.540 ","End":"04:56.090","Text":"and we also want to make sure that the artist attribute match is what\u0027s been passed in."},{"Start":"04:56.090 ","End":"04:58.220","Text":"We can do this."},{"Start":"04:58.220 ","End":"04:59.570","Text":"We don\u0027t need to do an if statement,"},{"Start":"04:59.570 ","End":"05:06.110","Text":"basically it will return true only if the name is equal and the artist is equal"},{"Start":"05:06.110 ","End":"05:08.510","Text":"on both the object passed in and"},{"Start":"05:08.510 ","End":"05:13.355","Text":"these instance variables within the object and we\u0027re currently operating with."},{"Start":"05:13.355 ","End":"05:15.140","Text":"Otherwise, it will return false."},{"Start":"05:15.140 ","End":"05:16.490","Text":"Could use an if statement if you want to,"},{"Start":"05:16.490 ","End":"05:19.220","Text":"but you should be able to understand that by now."},{"Start":"05:19.220 ","End":"05:23.750","Text":"The logic will return true only in the case that it\u0027s supposed to."},{"Start":"05:23.750 ","End":"05:32.180","Text":"That is it for part c. We\u0027re going to test this now using the object workbench,"},{"Start":"05:32.180 ","End":"05:36.785","Text":"and we\u0027re going to create 3 Songs specific order."},{"Start":"05:36.785 ","End":"05:44.680","Text":"The first one is called Yesterday by the Beatles,"},{"Start":"05:44.680 ","End":"05:48.195","Text":"and its year is 1965,"},{"Start":"05:48.195 ","End":"05:51.280","Text":"and that\u0027s going to be whatever the default name is."},{"Start":"05:51.280 ","End":"05:54.530","Text":"Song 1. In the object workbench,"},{"Start":"05:54.530 ","End":"05:55.640","Text":"you can specify a name,"},{"Start":"05:55.640 ","End":"05:57.500","Text":"but it\u0027s the thing you usually just"},{"Start":"05:57.500 ","End":"05:59.750","Text":"accept what it gives and it\u0027s usually something sensible,"},{"Start":"05:59.750 ","End":"06:02.645","Text":"as you see it\u0027s given song 2 hear for example."},{"Start":"06:02.645 ","End":"06:08.645","Text":"Song 2 is also called \"Yesterday\" but by a different artist."},{"Start":"06:08.645 ","End":"06:10.235","Text":"Then song 3,"},{"Start":"06:10.235 ","End":"06:15.555","Text":"we\u0027re going to use exactly the same details as we did when we created song 1,"},{"Start":"06:15.555 ","End":"06:18.640","Text":"Yesterday again, and Beatles Again,"},{"Start":"06:18.640 ","End":"06:19.880","Text":"the year is the same."},{"Start":"06:19.880 ","End":"06:23.020","Text":"Again, we don\u0027t actually use the year and the comparison."},{"Start":"06:23.020 ","End":"06:25.340","Text":"Would be ignored if I put different year,"},{"Start":"06:25.340 ","End":"06:32.135","Text":"it still work but let\u0027s keep it the same as exactly the same as the first object some 1."},{"Start":"06:32.135 ","End":"06:34.625","Text":"There\u0027s our 3 objects."},{"Start":"06:34.625 ","End":"06:41.900","Text":"Now we\u0027re going to do Part e. We\u0027re going to check whether song 1 and song 3 are equal."},{"Start":"06:41.900 ","End":"06:43.295","Text":"Here\u0027s song 1."},{"Start":"06:43.295 ","End":"06:46.410","Text":"You go to, equals here,"},{"Start":"06:46.410 ","End":"06:53.776","Text":"and then you just put the reference to the other object and song 3 and song"},{"Start":"06:53.776 ","End":"06:55.370","Text":"1 because they\u0027ve got the same artist and"},{"Start":"06:55.370 ","End":"07:01.170","Text":"the same name should give us true, and it does."},{"Start":"07:01.170 ","End":"07:05.010","Text":"Goodness. Song 1 and song 2 are not equal."},{"Start":"07:05.010 ","End":"07:06.915","Text":"Let\u0027s try that now."},{"Start":"07:06.915 ","End":"07:08.810","Text":"Song 2 into here,"},{"Start":"07:08.810 ","End":"07:09.950","Text":"we should get false,"},{"Start":"07:09.950 ","End":"07:12.560","Text":"which we do, which is great, and then we can,"},{"Start":"07:12.560 ","End":"07:18.795","Text":"just for completeness, check Song 2 and song 3 because they\u0027re also not equal."},{"Start":"07:18.795 ","End":"07:22.740","Text":"That\u0027s false as well, they\u0027re not equal."},{"Start":"07:22.740 ","End":"07:25.730","Text":"These 2 songs are the same."},{"Start":"07:25.730 ","End":"07:28.320","Text":"This, and this is not the same."},{"Start":"07:28.320 ","End":"07:30.920","Text":"This and this is therefore not the same either."},{"Start":"07:30.920 ","End":"07:33.950","Text":"It seems to have worked as we expect."},{"Start":"07:33.950 ","End":"07:35.960","Text":"Now the real tests that we\u0027re doing in"},{"Start":"07:35.960 ","End":"07:40.250","Text":"this particular exercise is with another method called hashCode."},{"Start":"07:40.250 ","End":"07:42.230","Text":"Now, you won\u0027t see hashCode here,"},{"Start":"07:42.230 ","End":"07:46.095","Text":"but it\u0027s been inherited from object, and here it is."},{"Start":"07:46.095 ","End":"07:48.000","Text":"It will return a number,"},{"Start":"07:48.000 ","End":"07:52.880","Text":"which is like a numeric representation of this object."},{"Start":"07:52.880 ","End":"07:56.255","Text":"What it\u0027s done, is given us this value,"},{"Start":"07:56.255 ","End":"07:57.680","Text":"the integer value always."},{"Start":"07:57.680 ","End":"07:59.240","Text":"That\u0027s the one for song 1."},{"Start":"07:59.240 ","End":"08:02.835","Text":"Let\u0027s do the same for song 2,"},{"Start":"08:02.835 ","End":"08:05.805","Text":"and we get a different number."},{"Start":"08:05.805 ","End":"08:07.545","Text":"Let\u0027s do song 3."},{"Start":"08:07.545 ","End":"08:11.180","Text":"Now we probably expect to get the same one for song 3,"},{"Start":"08:11.180 ","End":"08:13.745","Text":"given that they\u0027ve got the same data in,"},{"Start":"08:13.745 ","End":"08:17.330","Text":"but you find that it\u0027s a different value."},{"Start":"08:17.330 ","End":"08:19.580","Text":"We returned from song 1, song 2,"},{"Start":"08:19.580 ","End":"08:25.305","Text":"and song 3 hashCodes using the inbuilt method hashCode."},{"Start":"08:25.305 ","End":"08:29.360","Text":"It returns us an integer and it\u0027s different for all 3 objects."},{"Start":"08:29.360 ","End":"08:35.795","Text":"The reason for that is by default and objects hashCode method"},{"Start":"08:35.795 ","End":"08:39.920","Text":"hashes the memory address that the objects stored"},{"Start":"08:39.920 ","End":"08:44.210","Text":"at and returns that if you want it to hash anything else,"},{"Start":"08:44.210 ","End":"08:48.875","Text":"you have to tell it and you have to override the hashCode method."},{"Start":"08:48.875 ","End":"08:50.930","Text":"What we should really have done is taken"},{"Start":"08:50.930 ","End":"08:56.345","Text":"the song name and the artist\u0027s name and hashed those,"},{"Start":"08:56.345 ","End":"09:00.080","Text":"and then as a combined hashCode,"},{"Start":"09:00.080 ","End":"09:02.445","Text":"we should have return that."},{"Start":"09:02.445 ","End":"09:07.955","Text":"That\u0027s exactly what we\u0027re going to do in the next exercise because without doing that,"},{"Start":"09:07.955 ","End":"09:13.985","Text":"you\u0027ll get unexpected behavior for those collections that require this to work."},{"Start":"09:13.985 ","End":"09:16.490","Text":"For example, a set or a map."},{"Start":"09:16.490 ","End":"09:19.100","Text":"See you shortly for the next exercise where we\u0027ll fix"},{"Start":"09:19.100 ","End":"09:23.070","Text":"that problem and use a different collection. See you then."}],"ID":31167},{"Watched":false,"Name":"Exercise 5","Duration":"9m 8s","ChapterTopicVideoID":29544,"CourseChapterTopicPlaylistID":294562,"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.190","Text":"Hi, everyone. In this exercise,"},{"Start":"00:02.190 ","End":"00:05.445","Text":"we\u0027ve been told to create a new class called HallOfFame,"},{"Start":"00:05.445 ","End":"00:08.910","Text":"which makes use of the Song class to store songs"},{"Start":"00:08.910 ","End":"00:12.915","Text":"that have been inducted into the Music Industry Hall of Fame."},{"Start":"00:12.915 ","End":"00:19.140","Text":"Within the class, we create a HashSet collection of Song objects called songSet,"},{"Start":"00:19.140 ","End":"00:25.880","Text":"and then create a method that returns no values and accepts no arguments called testSet."},{"Start":"00:25.880 ","End":"00:30.710","Text":"Within it, add the following data to songSet."},{"Start":"00:30.710 ","End":"00:35.960","Text":"Once we\u0027ve added all of that data at the end of the method in Part C,"},{"Start":"00:35.960 ","End":"00:40.775","Text":"we\u0027re told to print out songSet on 1 line."},{"Start":"00:40.775 ","End":"00:42.740","Text":"Sets are not supposed to contain"},{"Start":"00:42.740 ","End":"00:46.685","Text":"duplicate values and we\u0027re asked if there are any duplicates songs."},{"Start":"00:46.685 ","End":"00:49.134","Text":"If so, why that\u0027s happened?"},{"Start":"00:49.134 ","End":"00:55.130","Text":"In Part D, we\u0027re told to override the hashCode method of the song class so that it adds"},{"Start":"00:55.130 ","End":"00:57.590","Text":"together the hashCode value of the name"},{"Start":"00:57.590 ","End":"01:01.600","Text":"attribute and the hashCode value of the artist attribute."},{"Start":"01:01.600 ","End":"01:05.840","Text":"We run the code again and compare the output to what we saw in"},{"Start":"01:05.840 ","End":"01:11.480","Text":"Part C. Having created a new class HallOfFame,"},{"Start":"01:11.480 ","End":"01:14.660","Text":"we need to in Part A create hashSet,"},{"Start":"01:14.660 ","End":"01:19.460","Text":"collection of songs objects and we\u0027re going to call it songSet."},{"Start":"01:19.760 ","End":"01:24.800","Text":"Market as private as we should, most things in."},{"Start":"01:24.800 ","End":"01:32.465","Text":"I had an object and it\u0027s going to have as its type Song,"},{"Start":"01:32.465 ","End":"01:35.405","Text":"this class that we just created."},{"Start":"01:35.405 ","End":"01:39.710","Text":"Then we\u0027re going to call it the songSet as we\u0027ve been told."},{"Start":"01:39.710 ","End":"01:46.280","Text":"To create it, we simply need to use new and the HashSet."},{"Start":"01:46.280 ","End":"01:50.240","Text":"Again, we\u0027re getting into the names and so on in a moment."},{"Start":"01:50.240 ","End":"01:52.340","Text":"There\u0027s my opening line."},{"Start":"01:52.340 ","End":"01:55.370","Text":"It\u0027s going to complain because it doesn\u0027t know what a HashSet is."},{"Start":"01:55.370 ","End":"02:04.580","Text":"That\u0027s because I need to import that from Java.util.HashSet,"},{"Start":"02:04.580 ","End":"02:10.770","Text":"the error has gone away now."},{"Start":"02:10.770 ","End":"02:14.030","Text":"We\u0027ve declared this structure,"},{"Start":"02:14.030 ","End":"02:16.370","Text":"this collection, and it\u0027s called songSet."},{"Start":"02:16.370 ","End":"02:18.380","Text":"It\u0027s there for storing songs."},{"Start":"02:18.380 ","End":"02:21.920","Text":"It\u0027s from the collection called HashSet."},{"Start":"02:21.920 ","End":"02:24.455","Text":"We\u0027ll find out what that does in a moment."},{"Start":"02:24.455 ","End":"02:26.185","Text":"In Part B,"},{"Start":"02:26.185 ","End":"02:31.700","Text":"we have got to create a method that returns no values and accepts no arguments."},{"Start":"02:31.700 ","End":"02:33.440","Text":"We\u0027re going to call it testSet."},{"Start":"02:33.440 ","End":"02:36.815","Text":"Then inside there, we\u0027re going to"},{"Start":"02:36.815 ","End":"02:43.535","Text":"add this data that we\u0027ve been given Part B from 1-7 to the songSet."},{"Start":"02:43.535 ","End":"02:48.080","Text":"How do we add? Well, it\u0027s the same as we do for an array list with the song list,"},{"Start":"02:48.080 ","End":"02:51.829","Text":"songSets, the name of the collection."},{"Start":"02:51.829 ","End":"02:54.305","Text":"We\u0027re going to add something to it."},{"Start":"02:54.305 ","End":"02:55.790","Text":"The thing we want to add,"},{"Start":"02:55.790 ","End":"02:57.530","Text":"we put in brackets."},{"Start":"02:57.530 ","End":"03:04.110","Text":"Now, we need to create an object that will then be added to the songSet collection."},{"Start":"03:04.110 ","End":"03:06.060","Text":"To create a song,"},{"Start":"03:06.060 ","End":"03:09.665","Text":"we do what we do for every object."},{"Start":"03:09.665 ","End":"03:15.605","Text":"You say new, and then you pass to it the values to its constructor."},{"Start":"03:15.605 ","End":"03:21.995","Text":"If the first song is Yesterday, the year 65,"},{"Start":"03:21.995 ","End":"03:25.175","Text":"then that\u0027s what I need to do for that first one,"},{"Start":"03:25.175 ","End":"03:30.005","Text":"so that seems to have compiled a case now Error Reporting."},{"Start":"03:30.005 ","End":"03:32.030","Text":"If I just copy that line,"},{"Start":"03:32.030 ","End":"03:35.660","Text":"then save me having any issues brackets and so on."},{"Start":"03:35.660 ","End":"03:42.210","Text":"I can just put in all the other data for all these other songs."},{"Start":"03:42.590 ","End":"03:44.910","Text":"We\u0027ve typed all of those in."},{"Start":"03:44.910 ","End":"03:48.440","Text":"Now, there\u0027s actually a duplicate on the end,"},{"Start":"03:48.440 ","End":"03:50.900","Text":"which is Yesterday again."},{"Start":"03:50.900 ","End":"03:52.520","Text":"We\u0027ve got the same song,"},{"Start":"03:52.520 ","End":"03:53.990","Text":"not Leona Lewis version,"},{"Start":"03:53.990 ","End":"03:55.685","Text":"but the Beatles version."},{"Start":"03:55.685 ","End":"03:56.930","Text":"We\u0027ve added it twice,"},{"Start":"03:56.930 ","End":"03:59.255","Text":"so we\u0027ve attempted to add it twice."},{"Start":"03:59.255 ","End":"04:04.100","Text":"Let\u0027s see what happens when we compile that. That\u0027s worked fine."},{"Start":"04:04.100 ","End":"04:09.735","Text":"We\u0027re going to print out the entire set on 1 line."},{"Start":"04:09.735 ","End":"04:13.860","Text":"We just need to give the name. It will do that."},{"Start":"04:13.860 ","End":"04:18.445","Text":"Because the 2 string method has been overridden,"},{"Start":"04:18.445 ","End":"04:21.605","Text":"and it should display a 1 line the entire set."},{"Start":"04:21.605 ","End":"04:25.840","Text":"Let\u0027s see what happens when we run that."},{"Start":"04:25.840 ","End":"04:28.170","Text":"You create a new HallOfFame object."},{"Start":"04:28.170 ","End":"04:31.035","Text":"We run the test set method in there."},{"Start":"04:31.035 ","End":"04:33.195","Text":"We see our set."},{"Start":"04:33.195 ","End":"04:35.450","Text":"We\u0027ve got Yesterday, Beatles,"},{"Start":"04:35.450 ","End":"04:36.950","Text":"1965, Sweet Dreams,"},{"Start":"04:36.950 ","End":"04:39.170","Text":"Eurythmics, 1983, and so on."},{"Start":"04:39.170 ","End":"04:41.525","Text":"All of those will go in there."},{"Start":"04:41.525 ","End":"04:43.560","Text":"What we see,"},{"Start":"04:43.560 ","End":"04:45.960","Text":"is we see the same thing at the end."},{"Start":"04:45.960 ","End":"04:48.830","Text":"It\u0027s put in everything we\u0027ve asked it to."},{"Start":"04:48.830 ","End":"04:53.530","Text":"But the whole point of a set is you\u0027re not allowed to contain duplicate values."},{"Start":"04:53.530 ","End":"04:56.060","Text":"We have got a duplicate value here,"},{"Start":"04:56.060 ","End":"04:58.145","Text":"which is Yesterday\u0027s Beatles."},{"Start":"04:58.145 ","End":"04:59.990","Text":"Why has that happened?"},{"Start":"04:59.990 ","End":"05:06.630","Text":"Well, it\u0027s because we didn\u0027t override the hashCode method of the song class."},{"Start":"05:06.630 ","End":"05:09.725","Text":"It doesn\u0027t know that they\u0027re the same thing."},{"Start":"05:09.725 ","End":"05:11.615","Text":"Even though it has equals,"},{"Start":"05:11.615 ","End":"05:17.240","Text":"it actually uses the hashCode to determine if these things should be stored in"},{"Start":"05:17.240 ","End":"05:19.610","Text":"a particular way or in a particular place and if they\u0027re the"},{"Start":"05:19.610 ","End":"05:22.940","Text":"same and we didn\u0027t do what we were supposed to do,"},{"Start":"05:22.940 ","End":"05:24.460","Text":"so it hasn\u0027t worked correctly."},{"Start":"05:24.460 ","End":"05:27.530","Text":"Let\u0027s see if we fix that problem,"},{"Start":"05:27.530 ","End":"05:32.365","Text":"if it by implementing the override of hashCode,"},{"Start":"05:32.365 ","End":"05:34.280","Text":"if that problem goes away."},{"Start":"05:34.280 ","End":"05:38.765","Text":"We\u0027re going to need to go into song to fix this."},{"Start":"05:38.765 ","End":"05:40.745","Text":"What we\u0027re going to need to do,"},{"Start":"05:40.745 ","End":"05:42.275","Text":"is we need to override,"},{"Start":"05:42.275 ","End":"05:45.235","Text":"as we said, the hashCode method."},{"Start":"05:45.235 ","End":"05:48.070","Text":"As we\u0027re told in Part D,"},{"Start":"05:48.610 ","End":"05:51.320","Text":"we need to add together"},{"Start":"05:51.320 ","End":"05:56.380","Text":"the hashCode variable name attribute and the hashCode value of the artist attribute."},{"Start":"05:56.380 ","End":"06:00.020","Text":"That\u0027s pretty straightforward, although the code does read a bit"},{"Start":"06:00.020 ","End":"06:04.400","Text":"strange and I\u0027ll just explain that once we got it in."},{"Start":"06:04.400 ","End":"06:07.085","Text":"We don\u0027t take any arguments."},{"Start":"06:07.085 ","End":"06:12.170","Text":"But what we do is we return an integer and we\u0027re going to"},{"Start":"06:12.170 ","End":"06:18.110","Text":"use to get that integer artist.hashCode."},{"Start":"06:18.110 ","End":"06:26.145","Text":"We\u0027re going to run that. Then we\u0027re going to add that to name."},{"Start":"06:26.145 ","End":"06:29.975","Text":"What that will give us is 2 different integer values,"},{"Start":"06:29.975 ","End":"06:31.925","Text":"1 coming from here and 1 coming from there."},{"Start":"06:31.925 ","End":"06:36.580","Text":"It will add them together and return that to the calling code."},{"Start":"06:36.580 ","End":"06:39.730","Text":"That\u0027s what we\u0027ve been asked to do. It does look a bit strange."},{"Start":"06:39.730 ","End":"06:42.380","Text":"Because we\u0027re calling hashCode from within hashCode,"},{"Start":"06:42.380 ","End":"06:45.690","Text":"so is this a recursive thing going on here?"},{"Start":"06:45.690 ","End":"06:48.740","Text":"If it is, why wasn\u0027t carry on forever?"},{"Start":"06:48.740 ","End":"06:51.125","Text":"Because there\u0027s no base case."},{"Start":"06:51.125 ","End":"06:57.875","Text":"Well, it\u0027s actually not recursive because this hashCode is for the object song."},{"Start":"06:57.875 ","End":"07:01.640","Text":"There\u0027s a different hashCode operating on artists and"},{"Start":"07:01.640 ","End":"07:06.480","Text":"name because this is for hashCode on a string."},{"Start":"07:06.480 ","End":"07:09.725","Text":"Strings have their own way of"},{"Start":"07:09.725 ","End":"07:14.060","Text":"calculating hash and that\u0027s been implemented and that\u0027s not what we\u0027re overriding."},{"Start":"07:14.060 ","End":"07:19.020","Text":"We\u0027re overriding the hashCode for this object, the song."},{"Start":"07:19.020 ","End":"07:21.935","Text":"Let\u0027s just look a little bit strange,"},{"Start":"07:21.935 ","End":"07:24.560","Text":"hopefully that should make sense that we\u0027re not"},{"Start":"07:24.560 ","End":"07:28.325","Text":"overriding this hashCode and this is completely different."},{"Start":"07:28.325 ","End":"07:33.645","Text":"We\u0027re overriding the one that comes in this object song."},{"Start":"07:33.645 ","End":"07:36.430","Text":"All we\u0027ve done is implement the hashCode."},{"Start":"07:36.430 ","End":"07:42.185","Text":"Let\u0027s see what happens if we compile everything and do exactly what we did before."},{"Start":"07:42.185 ","End":"07:46.175","Text":"Do we see the Beatles again twice?"},{"Start":"07:46.175 ","End":"07:47.885","Text":"Hopefully, we won\u0027t."},{"Start":"07:47.885 ","End":"07:50.020","Text":"Let\u0027s try that."},{"Start":"07:50.020 ","End":"07:52.560","Text":"There\u0027s our first occurrence of Beatles."},{"Start":"07:52.560 ","End":"07:54.140","Text":"If I scroll through,"},{"Start":"07:54.140 ","End":"07:56.885","Text":"you\u0027ll see that we\u0027ve got closed bracket here."},{"Start":"07:56.885 ","End":"08:03.135","Text":"No second Beatles dataset found or song found."},{"Start":"08:03.135 ","End":"08:05.370","Text":"It\u0027s fixed the problem."},{"Start":"08:05.370 ","End":"08:09.425","Text":"What looks like a very strange and esoteric problem,"},{"Start":"08:09.425 ","End":"08:14.605","Text":"just because we didn\u0027t implement the hashCode as we were supposed to."},{"Start":"08:14.605 ","End":"08:18.320","Text":"When you are told in the documentation that you have to"},{"Start":"08:18.320 ","End":"08:21.890","Text":"override hashCode when you override equals,"},{"Start":"08:21.890 ","End":"08:23.810","Text":"this is why if you don\u0027t,"},{"Start":"08:23.810 ","End":"08:25.910","Text":"you\u0027ll have some issues."},{"Start":"08:25.910 ","End":"08:29.735","Text":"They might look a bit obscure and you might wonder why they\u0027ve happened."},{"Start":"08:29.735 ","End":"08:32.389","Text":"In this case, it\u0027s pretty obvious,"},{"Start":"08:32.389 ","End":"08:35.090","Text":"we shouldn\u0027t have been able to put 2 things into a set."},{"Start":"08:35.090 ","End":"08:39.840","Text":"A set by definition is an unordered collection of items."},{"Start":"08:39.840 ","End":"08:43.445","Text":"You\u0027ll notice these aren\u0027t exactly in the order that we put them in,"},{"Start":"08:43.445 ","End":"08:47.450","Text":"but it shouldn\u0027t allow duplicates in a set."},{"Start":"08:47.450 ","End":"08:49.130","Text":"Previously, it was."},{"Start":"08:49.130 ","End":"08:53.015","Text":"Now it\u0027s not because we fixed it by implementing"},{"Start":"08:53.015 ","End":"08:58.815","Text":"an overridden hashCode method for the song object."},{"Start":"08:58.815 ","End":"09:00.420","Text":"That\u0027s it for this one."},{"Start":"09:00.420 ","End":"09:06.245","Text":"We\u0027ll build on that to do 1 final exercise on collections using HashMaps."},{"Start":"09:06.245 ","End":"09:08.850","Text":"See you for that one soon."}],"ID":31168},{"Watched":false,"Name":"Exercise 6","Duration":"11m 27s","ChapterTopicVideoID":29545,"CourseChapterTopicPlaylistID":294562,"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.680","Text":"Hello, welcome back."},{"Start":"00:01.680 ","End":"00:02.940","Text":"In this exercise,"},{"Start":"00:02.940 ","End":"00:05.565","Text":"we\u0027re told a HashMap can be used to efficiently look up"},{"Start":"00:05.565 ","End":"00:09.630","Text":"information in a collection of objects by using a key-value pair."},{"Start":"00:09.630 ","End":"00:13.095","Text":"Items are put into the HashMap using the put method,"},{"Start":"00:13.095 ","End":"00:14.790","Text":"which requires a key,"},{"Start":"00:14.790 ","End":"00:16.635","Text":"and the item to be stored."},{"Start":"00:16.635 ","End":"00:20.015","Text":"Values are retrieved using the get method with a key."},{"Start":"00:20.015 ","End":"00:25.475","Text":"The HashMap can be searched for a particular key using the containsKey method."},{"Start":"00:25.475 ","End":"00:27.650","Text":"In part A, we\u0027re told within"},{"Start":"00:27.650 ","End":"00:33.020","Text":"the existing HallOfFameclass to create a new collection called songLibrary,"},{"Start":"00:33.020 ","End":"00:39.485","Text":"which can store a collection of Song Objects indexed using a String key."},{"Start":"00:39.485 ","End":"00:46.505","Text":"In part B, we\u0027re told we will use a music genre as the key to each of our songs."},{"Start":"00:46.505 ","End":"00:49.925","Text":"Within a new method called testMap we`re to add"},{"Start":"00:49.925 ","End":"00:54.035","Text":"the following data to the songLibrary collection."},{"Start":"00:54.035 ","End":"00:56.270","Text":"Once we\u0027ve added those items of data,"},{"Start":"00:56.270 ","End":"01:02.380","Text":"we\u0027re to test by outputting the contents of the songLibrary collection on a single line."},{"Start":"01:02.380 ","End":"01:07.510","Text":"We\u0027re then told in part D to use a while loop to take input from the keyboard,"},{"Start":"01:07.510 ","End":"01:11.920","Text":"prompting the user to enter a genre or the keyword \"quit\" to end."},{"Start":"01:11.920 ","End":"01:15.190","Text":"If a song from that genre is found in the HashMap,"},{"Start":"01:15.190 ","End":"01:16.930","Text":"we output the details,"},{"Start":"01:16.930 ","End":"01:20.885","Text":"otherwise we output the message \"Genre not found.\""},{"Start":"01:20.885 ","End":"01:24.490","Text":"In part E we\u0027re told to test that the code works by searching for"},{"Start":"01:24.490 ","End":"01:27.910","Text":"some genres present in the HashMap,"},{"Start":"01:27.910 ","End":"01:31.949","Text":"and with at least 1 other that is not."},{"Start":"01:31.949 ","End":"01:35.620","Text":"Then finally, we\u0027re asked what the flaw is in using"},{"Start":"01:35.620 ","End":"01:39.055","Text":"the genre as a key in this particular exercise,"},{"Start":"01:39.055 ","End":"01:41.780","Text":"and how we might solve it."},{"Start":"01:41.780 ","End":"01:44.405","Text":"In this next exercise,"},{"Start":"01:44.405 ","End":"01:49.265","Text":"then we\u0027ve been first asked to create a new collection,"},{"Start":"01:49.265 ","End":"01:51.935","Text":"this time a HashMap."},{"Start":"01:51.935 ","End":"01:54.560","Text":"We\u0027re going to use that to store songs,"},{"Start":"01:54.560 ","End":"01:57.875","Text":"and we\u0027re going to index on a string,"},{"Start":"01:57.875 ","End":"02:01.700","Text":"which will be the key to access the particular song."},{"Start":"02:01.700 ","End":"02:06.730","Text":"Let\u0027s start by creating our hash."},{"Start":"02:06.730 ","End":"02:10.430","Text":"The tricky bit with a HashMap is there\u0027s"},{"Start":"02:10.430 ","End":"02:15.005","Text":"2 things to go in here for the generics specifier."},{"Start":"02:15.005 ","End":"02:19.405","Text":"First 1 is the type of the key,"},{"Start":"02:19.405 ","End":"02:22.370","Text":"and the next is the type of whatever"},{"Start":"02:22.370 ","End":"02:27.065","Text":"the object is that you\u0027re trying to store away in this HashMap."},{"Start":"02:27.065 ","End":"02:30.670","Text":"For us, the key is going to be a String,"},{"Start":"02:30.670 ","End":"02:34.905","Text":"and the object is going to be a song."},{"Start":"02:34.905 ","End":"02:36.710","Text":"That looks a bit different to what we\u0027ve seen"},{"Start":"02:36.710 ","End":"02:39.155","Text":"previously when we declared a nice collection types."},{"Start":"02:39.155 ","End":"02:40.790","Text":"Because there\u0027s 2 different types,"},{"Start":"02:40.790 ","End":"02:42.740","Text":"and they\u0027re separated by comma."},{"Start":"02:42.740 ","End":"02:46.920","Text":"We said we\u0027d call our HashMap songLibrary,"},{"Start":"02:46.920 ","End":"02:49.065","Text":"and we create that as usual."},{"Start":"02:49.065 ","End":"02:56.235","Text":"I do in new, and then the name of the class so that should do it for us."},{"Start":"02:56.235 ","End":"02:58.955","Text":"Now, it\u0027s complaining, can\u0027t find HashMap."},{"Start":"02:58.955 ","End":"03:05.690","Text":"Guess what? You need to do the import top to say we\u0027re going to use a HashMap,"},{"Start":"03:05.690 ","End":"03:07.730","Text":"and that should go away."},{"Start":"03:07.730 ","End":"03:14.030","Text":"Great. Now we can create the data inside it."},{"Start":"03:14.030 ","End":"03:16.070","Text":"It\u0027s very similar to what we did with"},{"Start":"03:16.070 ","End":"03:19.355","Text":"previously when we were just using a set, a HashSet."},{"Start":"03:19.355 ","End":"03:24.285","Text":"Let\u0027s create a new method called testMap,"},{"Start":"03:24.285 ","End":"03:30.200","Text":"and inside it we\u0027re going to do very similar things to what we did above."},{"Start":"03:30.200 ","End":"03:33.815","Text":"This time we\u0027re called songLibrary,"},{"Start":"03:33.815 ","End":"03:38.805","Text":"and we use put rather than add."},{"Start":"03:38.805 ","End":"03:41.985","Text":"Because we need 2 fields here."},{"Start":"03:41.985 ","End":"03:44.940","Text":"The first field is going to be the key."},{"Start":"03:44.940 ","End":"03:48.695","Text":"Then the second field is going to be the song."},{"Start":"03:48.695 ","End":"03:51.460","Text":"That we\u0027ve been given data here in part B1,"},{"Start":"03:51.460 ","End":"03:53.225","Text":"the key is \"Rock\","},{"Start":"03:53.225 ","End":"03:54.720","Text":"so it\u0027s just a string."},{"Start":"03:54.720 ","End":"03:56.445","Text":"Just put that in quotes,"},{"Start":"03:56.445 ","End":"04:02.245","Text":"and then a comma to say this is the next thing that we\u0027re putting in to the library,"},{"Start":"04:02.245 ","End":"04:04.540","Text":"not the key but the value."},{"Start":"04:04.540 ","End":"04:06.550","Text":"We\u0027re going put an object in there,"},{"Start":"04:06.550 ","End":"04:07.600","Text":"which is a Song Object."},{"Start":"04:07.600 ","End":"04:10.705","Text":"We need to create that just as we did previously,"},{"Start":"04:10.705 ","End":"04:13.710","Text":"and paste it all into here."},{"Start":"04:13.710 ","End":"04:19.135","Text":"Actually, probably easier for me to just take all that data from there."},{"Start":"04:19.135 ","End":"04:21.115","Text":"Just do that for the first 1."},{"Start":"04:21.115 ","End":"04:30.705","Text":"That would put a single item into this HashMap with the key of \"Rock\"."},{"Start":"04:30.705 ","End":"04:36.500","Text":"I just do the same thing now for those other data items with different keys of reggae,"},{"Start":"04:36.500 ","End":"04:39.270","Text":"folk, country, rock, and so on."},{"Start":"04:39.380 ","End":"04:45.720","Text":"That\u0027s our 6 different items put into the songLibrary."},{"Start":"04:45.720 ","End":"04:49.530","Text":"To test it, we\u0027d been told in part C,"},{"Start":"04:49.530 ","End":"04:50.815","Text":"just the output,"},{"Start":"04:50.815 ","End":"04:53.750","Text":"and songLibrary on a single line."},{"Start":"04:53.750 ","End":"04:56.930","Text":"Let\u0027s do that just to see that it\u0027s done."},{"Start":"04:56.930 ","End":"04:58.820","Text":"What we expect it to have done."},{"Start":"04:58.820 ","End":"05:03.530","Text":"The output is slightly different than it is for a HashSet."},{"Start":"05:03.530 ","End":"05:05.680","Text":"We\u0027ll see that in a sec."},{"Start":"05:05.680 ","End":"05:07.815","Text":"TestMap was our method,"},{"Start":"05:07.815 ","End":"05:10.330","Text":"and you\u0027ll see now we\u0027ve got actually braces,"},{"Start":"05:10.330 ","End":"05:13.834","Text":"and we have a key."},{"Start":"05:13.834 ","End":"05:16.265","Text":"The name of the key is specified,"},{"Start":"05:16.265 ","End":"05:17.510","Text":"then an equal sign,"},{"Start":"05:17.510 ","End":"05:19.805","Text":"and then the actual key value."},{"Start":"05:19.805 ","End":"05:23.690","Text":"We\u0027ve overwritten clearly the 2 string method"},{"Start":"05:23.690 ","End":"05:28.285","Text":"for this type of object to display the format in this way."},{"Start":"05:28.285 ","End":"05:30.435","Text":"There is the data."},{"Start":"05:30.435 ","End":"05:34.340","Text":"We\u0027ve got the first key there was yet Rock."},{"Start":"05:34.340 ","End":"05:35.815","Text":"The next key is Hip-Hop,"},{"Start":"05:35.815 ","End":"05:39.720","Text":"the next key is Electronic pop, and so on."},{"Start":"05:39.720 ","End":"05:41.805","Text":"We got Folk there, and Reggae there."},{"Start":"05:41.805 ","End":"05:44.980","Text":"Notice again, not in the order that we"},{"Start":"05:44.980 ","End":"05:48.655","Text":"inserted it into the set because sets have no order."},{"Start":"05:48.655 ","End":"05:54.605","Text":"It\u0027s whatever is convenient to the algorithm that puts it in is the order it goes in."},{"Start":"05:54.605 ","End":"05:59.920","Text":"That\u0027s the end of the setName marked by the closing brace."},{"Start":"05:59.920 ","End":"06:04.360","Text":"We know it\u0027s added all the songs into this HashMap."},{"Start":"06:04.360 ","End":"06:07.480","Text":"Now we can move on to the next part where we\u0027ve been"},{"Start":"06:07.480 ","End":"06:12.345","Text":"told to use a while loop to take input."},{"Start":"06:12.345 ","End":"06:14.535","Text":"We can type in a genre,"},{"Start":"06:14.535 ","End":"06:16.125","Text":"and get it to search for it."},{"Start":"06:16.125 ","End":"06:18.465","Text":"Just to show how this works."},{"Start":"06:18.465 ","End":"06:21.480","Text":"Let\u0027s go ahead, and do all that stuff."},{"Start":"06:21.480 ","End":"06:26.560","Text":"We need to get a scanner object to take keyboard input,"},{"Start":"06:26.560 ","End":"06:27.940","Text":"I\u0027ve done this a few times now."},{"Start":"06:27.940 ","End":"06:30.415","Text":"We should get the idea, again,"},{"Start":"06:30.415 ","End":"06:32.905","Text":"we haven\u0027t got a scanner available,"},{"Start":"06:32.905 ","End":"06:38.305","Text":"so we need to import it from the relevant library."},{"Start":"06:38.305 ","End":"06:41.955","Text":"To make that error message go away, which it now has."},{"Start":"06:41.955 ","End":"06:51.430","Text":"Then we can set the delimiter by using the useDelimiter method to a new line."},{"Start":"06:51.430 ","End":"06:56.440","Text":"Then you need a string to store whatever has been typed in."},{"Start":"06:56.440 ","End":"07:00.325","Text":"Then we can do a loop of course we can do while loop."},{"Start":"07:00.325 ","End":"07:01.570","Text":"That\u0027s the best way to do it."},{"Start":"07:01.570 ","End":"07:03.310","Text":"Do it to actually get the data."},{"Start":"07:03.310 ","End":"07:08.156","Text":"We want it to keep going while they don\u0027t type \"quit\"."},{"Start":"07:08.156 ","End":"07:11.310","Text":"As long as the genre is not \"quit\","},{"Start":"07:11.310 ","End":"07:14.565","Text":"this will keep going round inside this loop."},{"Start":"07:14.565 ","End":"07:17.335","Text":"Science had not been initialized, and had a value yet."},{"Start":"07:17.335 ","End":"07:18.700","Text":"That\u0027s perfectly right."},{"Start":"07:18.700 ","End":"07:21.250","Text":"We\u0027re going to prompt the user,"},{"Start":"07:21.250 ","End":"07:26.125","Text":"first of all, probably good to put a line break in before."},{"Start":"07:26.125 ","End":"07:28.120","Text":"Once they\u0027ve done that,"},{"Start":"07:28.120 ","End":"07:30.504","Text":"we get the input,"},{"Start":"07:30.504 ","End":"07:32.350","Text":"store it into choice,"},{"Start":"07:32.350 ","End":"07:38.225","Text":"then there\u0027s a simple case of checking whether it\u0027s already in the library or not."},{"Start":"07:38.225 ","End":"07:42.620","Text":"I shall also actually check that they haven\u0027t typed in \"quit\","},{"Start":"07:42.620 ","End":"07:46.010","Text":"or in other words, I will try and search for \"quit\"."},{"Start":"07:46.010 ","End":"07:49.840","Text":"We only do this if they haven\u0027t typed in \"quit\"."},{"Start":"07:49.840 ","End":"07:58.160","Text":"Then I do a further check to see if the songLibrary contains this particular key sets."},{"Start":"07:58.160 ","End":"08:00.710","Text":"It contains key not just contains,"},{"Start":"08:00.710 ","End":"08:03.995","Text":"and whatever was typed in at the keyboard."},{"Start":"08:03.995 ","End":"08:06.560","Text":"If it does contain that key,"},{"Start":"08:06.560 ","End":"08:09.755","Text":"then I can retrieve the data,"},{"Start":"08:09.755 ","End":"08:11.465","Text":"and I can display it."},{"Start":"08:11.465 ","End":"08:13.820","Text":"Otherwise, I display a genre not found."},{"Start":"08:13.820 ","End":"08:15.680","Text":"If I have found it,"},{"Start":"08:15.680 ","End":"08:17.540","Text":"I can just print it out."},{"Start":"08:17.540 ","End":"08:20.210","Text":"Obviously, you could do some other processing here if you wanted to."},{"Start":"08:20.210 ","End":"08:25.205","Text":"I\u0027m going to use get on the songLibrary,"},{"Start":"08:25.205 ","End":"08:27.005","Text":"but I have to pass it now,"},{"Start":"08:27.005 ","End":"08:32.345","Text":"a key which is what was stored into the choice variable,"},{"Start":"08:32.345 ","End":"08:34.170","Text":"because I`ve typed here the keyboard."},{"Start":"08:34.170 ","End":"08:36.965","Text":"If it can find it, it will print it."},{"Start":"08:36.965 ","End":"08:41.405","Text":"Well, we know it can find it because it wouldn\u0027t be in this line if it couldn\u0027t find it."},{"Start":"08:41.405 ","End":"08:43.550","Text":"But if it doesn\u0027t find it,"},{"Start":"08:43.550 ","End":"08:48.630","Text":"we print a message saying \"not found\"."},{"Start":"08:49.390 ","End":"08:52.325","Text":"That should do it for us."},{"Start":"08:52.325 ","End":"08:54.535","Text":"Let\u0027s have a test of it."},{"Start":"08:54.535 ","End":"08:57.875","Text":"We create a new HallOfFame object,"},{"Start":"08:57.875 ","End":"08:59.810","Text":"and then we run map."},{"Start":"08:59.810 ","End":"09:03.800","Text":"We still got debug code in there above. Just ignore that."},{"Start":"09:03.800 ","End":"09:05.840","Text":"We\u0027re going to enter a genre,"},{"Start":"09:05.840 ","End":"09:07.730","Text":"and we know for a fact that Rock is in there,"},{"Start":"09:07.730 ","End":"09:09.035","Text":"so I start Rock,"},{"Start":"09:09.035 ","End":"09:12.890","Text":"and it does return the data that we\u0027re looking for."},{"Start":"09:12.890 ","End":"09:19.120","Text":"Let\u0027s do Electronic pop that should be in there, and it is."},{"Start":"09:19.120 ","End":"09:20.960","Text":"I do 1 more Folk,"},{"Start":"09:20.960 ","End":"09:22.790","Text":"that should be in there, and that\u0027s in there."},{"Start":"09:22.790 ","End":"09:25.880","Text":"As well let\u0027s try a genre that we know is not in there."},{"Start":"09:25.880 ","End":"09:27.905","Text":"We can put in jazz in there,"},{"Start":"09:27.905 ","End":"09:31.875","Text":"and it\u0027s told us correctly that it\u0027s not found."},{"Start":"09:31.875 ","End":"09:36.185","Text":"That seems to have done exactly what we wanted it to."},{"Start":"09:36.185 ","End":"09:38.570","Text":"Now, the final question,"},{"Start":"09:38.570 ","End":"09:39.860","Text":"part F, says,"},{"Start":"09:39.860 ","End":"09:44.270","Text":"what\u0027s the flaw in using the genre as a key in this particular exercise,"},{"Start":"09:44.270 ","End":"09:45.320","Text":"and how can we solve it?"},{"Start":"09:45.320 ","End":"09:47.915","Text":"Well, clearly, the problem is,"},{"Start":"09:47.915 ","End":"09:55.374","Text":"you can only have 1 song associated with a genre which is clearly not very practical."},{"Start":"09:55.374 ","End":"09:57.590","Text":"How could we fix that?"},{"Start":"09:57.590 ","End":"09:59.944","Text":"What can we do that would improve this program,"},{"Start":"09:59.944 ","End":"10:02.540","Text":"or we could not use the genre?"},{"Start":"10:02.540 ","End":"10:07.310","Text":"It\u0027s probably a bad field to use as a search field."},{"Start":"10:07.310 ","End":"10:10.790","Text":"Maybe the song name would be better to use as the key."},{"Start":"10:10.790 ","End":"10:13.760","Text":"We would duplicate some data because we\u0027d be storing it twice,"},{"Start":"10:13.760 ","End":"10:16.930","Text":"once as the key, and then again with all the other data."},{"Start":"10:16.930 ","End":"10:21.485","Text":"That would work, and it would locate the song quickly, and accurately."},{"Start":"10:21.485 ","End":"10:26.150","Text":"The only issue there is if the song occurs twice for different artists,"},{"Start":"10:26.150 ","End":"10:30.095","Text":"like The Beatles Yesterday or Leona Lewis Yesterday."},{"Start":"10:30.095 ","End":"10:34.160","Text":"We couldn\u0027t do that because we can\u0027t use the same key twice."},{"Start":"10:34.160 ","End":"10:37.535","Text":"We could fix that by putting,"},{"Start":"10:37.535 ","End":"10:40.295","Text":"if we detect that the key already exists,"},{"Start":"10:40.295 ","End":"10:42.320","Text":"we can append a number to the end,"},{"Start":"10:42.320 ","End":"10:44.675","Text":"or we can append the artist\u0027s name to the end."},{"Start":"10:44.675 ","End":"10:47.720","Text":"But maybe it\u0027s really a suggestion there that this is"},{"Start":"10:47.720 ","End":"10:51.125","Text":"not the right thing to use for this particular problem."},{"Start":"10:51.125 ","End":"10:53.180","Text":"Maybe we should use some other type of collection."},{"Start":"10:53.180 ","End":"10:55.295","Text":"For example, an ArrayList,"},{"Start":"10:55.295 ","End":"10:58.415","Text":"which won\u0027t be as efficient as a HashMap,"},{"Start":"10:58.415 ","End":"11:02.270","Text":"but it will allow us to store multiple songs with the same name."},{"Start":"11:02.270 ","End":"11:05.940","Text":"There is actually a type of map called a multimap,"},{"Start":"11:05.940 ","End":"11:11.195","Text":"and that can return a collection in response to a key rather than a single item."},{"Start":"11:11.195 ","End":"11:15.665","Text":"If it does allow us to do this thing, then we might want to do."},{"Start":"11:15.665 ","End":"11:17.465","Text":"But I just think it highlights,"},{"Start":"11:17.465 ","End":"11:19.415","Text":"you\u0027ve got to choose the right type of"},{"Start":"11:19.415 ","End":"11:24.665","Text":"data structure collection for the particular problem that you\u0027re trying to solve."},{"Start":"11:24.665 ","End":"11:28.230","Text":"Thanks very much for watching.1 See you again soon."}],"ID":31169}],"Thumbnail":null,"ID":294562},{"Name":"Design Principles","TopicPlaylistFirstVideoID":0,"Duration":null,"Videos":[{"Watched":false,"Name":"Coupling and Cohesion","Duration":"5m 27s","ChapterTopicVideoID":29604,"CourseChapterTopicPlaylistID":294565,"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.995","Text":"Hello and welcome to this video on Object Oriented Design Principles."},{"Start":"00:04.995 ","End":"00:06.420","Text":"By the end of this section,"},{"Start":"00:06.420 ","End":"00:11.235","Text":"you\u0027ll be able to explain the key features of well-designed object oriented programs,"},{"Start":"00:11.235 ","End":"00:15.470","Text":"to distinguish between aggregation and composition and to"},{"Start":"00:15.470 ","End":"00:20.300","Text":"evaluate when composition or inheritance is more appropriate."},{"Start":"00:20.300 ","End":"00:23.540","Text":"It can be quite a daunting task when you\u0027re starting"},{"Start":"00:23.540 ","End":"00:26.465","Text":"out with a blank sheet of paper on a new design."},{"Start":"00:26.465 ","End":"00:30.140","Text":"If good design principles are part of your habits,"},{"Start":"00:30.140 ","End":"00:32.870","Text":"then there\u0027s a better chance that your end product will be more"},{"Start":"00:32.870 ","End":"00:36.170","Text":"straightforward to develop and maintain."},{"Start":"00:36.170 ","End":"00:39.300","Text":"When designing an object-oriented program,"},{"Start":"00:39.300 ","End":"00:43.745","Text":"some of the fundamental design principles are the following."},{"Start":"00:43.745 ","End":"00:47.435","Text":"Avoid code duplication."},{"Start":"00:47.435 ","End":"00:50.450","Text":"Encapsulate what varies."},{"Start":"00:50.450 ","End":"00:54.200","Text":"Prefer aggregation over inheritance,"},{"Start":"00:54.200 ","End":"00:59.150","Text":"and program to an interface not an implementation."},{"Start":"00:59.150 ","End":"01:01.730","Text":"Will look briefly at what these mean and how to"},{"Start":"01:01.730 ","End":"01:05.330","Text":"incorporate these ideas into our own designs."},{"Start":"01:05.330 ","End":"01:10.505","Text":"Many of these principles relate to the ideas of coupling and cohesion,"},{"Start":"01:10.505 ","End":"01:15.230","Text":"which we\u0027ll consider first before examining each of these points here."},{"Start":"01:15.230 ","End":"01:20.030","Text":"Coupling. A well-designed system will consist of units of code that"},{"Start":"01:20.030 ","End":"01:25.130","Text":"are largely independent and communicate through a small and well-defined interface."},{"Start":"01:25.130 ","End":"01:28.885","Text":"Such a system is said to have loose coupling."},{"Start":"01:28.885 ","End":"01:36.694","Text":"Cohesion. How well a unit of code maps to a particular logical task or entity?"},{"Start":"01:36.694 ","End":"01:38.885","Text":"Each unit of code should have"},{"Start":"01:38.885 ","End":"01:43.910","Text":"a clearly defined responsibility in a highly cohesive system."},{"Start":"01:43.910 ","End":"01:50.405","Text":"In object-oriented programming, the object is the fundamental code unit."},{"Start":"01:50.405 ","End":"01:55.910","Text":"Our objective is that there\u0027s a very loose coupling between different objects."},{"Start":"01:55.910 ","End":"01:58.370","Text":"The internals of each object should be able to"},{"Start":"01:58.370 ","End":"02:02.090","Text":"vary without affecting the overall operation of the system."},{"Start":"02:02.090 ","End":"02:06.020","Text":"Each object has its own job and any communication between"},{"Start":"02:06.020 ","End":"02:10.100","Text":"objects is through the provided public methods."},{"Start":"02:10.100 ","End":"02:16.535","Text":"Thinking smaller, a unit of code might be a single method within a class."},{"Start":"02:16.535 ","End":"02:18.785","Text":"Alternatively, going the other way,"},{"Start":"02:18.785 ","End":"02:23.990","Text":"a unit of code could be a whole package of many classes."},{"Start":"02:23.990 ","End":"02:28.790","Text":"With methods, the methods should perform a single task."},{"Start":"02:28.790 ","End":"02:34.040","Text":"A class should model a single logical entity."},{"Start":"02:34.040 ","End":"02:39.380","Text":"A package should relate to a single type of functionality."},{"Start":"02:39.380 ","End":"02:42.500","Text":"The Java package, java.time is"},{"Start":"02:42.500 ","End":"02:46.385","Text":"a package of classes for dealing with anything time-related."},{"Start":"02:46.385 ","End":"02:51.145","Text":"It\u0027s clear that this is a package that relates to a single area of functionality, time."},{"Start":"02:51.145 ","End":"02:53.570","Text":"Time is a complex concept."},{"Start":"02:53.570 ","End":"02:56.660","Text":"There are different ways of recording and interpreting time in"},{"Start":"02:56.660 ","End":"03:02.465","Text":"the human world using different calendars and different time zones."},{"Start":"03:02.465 ","End":"03:07.175","Text":"For example, even though most of the world works on the Gregorian calendar,"},{"Start":"03:07.175 ","End":"03:12.325","Text":"with 365 days in a year and a leap year every 4 years,"},{"Start":"03:12.325 ","End":"03:17.300","Text":"most countries write dates with the day first followed by the month."},{"Start":"03:17.300 ","End":"03:22.460","Text":"Whereas in the US, the month is stated before the day."},{"Start":"03:22.460 ","End":"03:25.640","Text":"This seemingly small detail needs attention,"},{"Start":"03:25.640 ","End":"03:30.380","Text":"or a program using dates might produce incorrect results."},{"Start":"03:30.380 ","End":"03:33.410","Text":"It gets even more complicated than that."},{"Start":"03:33.410 ","End":"03:37.535","Text":"Some regions of the world and some religious calendars might have"},{"Start":"03:37.535 ","End":"03:42.455","Text":"a lunar based month or might use a different reference here."},{"Start":"03:42.455 ","End":"03:47.620","Text":"When trying to calculate the difference in days between 2 candidates,"},{"Start":"03:47.620 ","End":"03:52.744","Text":"it can be surprisingly complex when taking into account different types of calendar,"},{"Start":"03:52.744 ","End":"03:56.750","Text":"time zones, treatment of leap years, and so on."},{"Start":"03:56.750 ","End":"04:00.020","Text":"The java.time package can provide classes that"},{"Start":"04:00.020 ","End":"04:03.845","Text":"deal with all that complexity and variation for you."},{"Start":"04:03.845 ","End":"04:06.995","Text":"But if you don\u0027t want that level of flexibility,"},{"Start":"04:06.995 ","End":"04:10.055","Text":"you can simply use a local date class,"},{"Start":"04:10.055 ","End":"04:15.665","Text":"which always assumes the Gregorian calendar and doesn\u0027t bother with the precise time,"},{"Start":"04:15.665 ","End":"04:17.603","Text":"in hours, minutes, and seconds,"},{"Start":"04:17.603 ","End":"04:19.715","Text":"it just stores a date."},{"Start":"04:19.715 ","End":"04:26.315","Text":"The details of how the actual data is stored internally within the object is irrelevant."},{"Start":"04:26.315 ","End":"04:28.895","Text":"This class simply does 1 job,"},{"Start":"04:28.895 ","End":"04:31.160","Text":"which is to store a date."},{"Start":"04:31.160 ","End":"04:34.250","Text":"If you want to know the day of the week,"},{"Start":"04:34.250 ","End":"04:36.950","Text":"there\u0027s a method that will do that."},{"Start":"04:36.950 ","End":"04:42.215","Text":"If you want to know how long a particular month is in days,"},{"Start":"04:42.215 ","End":"04:44.875","Text":"there\u0027s a method for that too."},{"Start":"04:44.875 ","End":"04:50.780","Text":"These methods perform 1 logical task as we\u0027d expect them to,"},{"Start":"04:50.780 ","End":"04:54.425","Text":"so the java.time package is highly cohesive."},{"Start":"04:54.425 ","End":"04:57.775","Text":"All the classes within it deal with time."},{"Start":"04:57.775 ","End":"05:02.165","Text":"The local date class is also highly cohesive."},{"Start":"05:02.165 ","End":"05:05.480","Text":"The only thing it tries to do is store a date,"},{"Start":"05:05.480 ","End":"05:10.969","Text":"and each method within local date is also highly cohesive."},{"Start":"05:10.969 ","End":"05:15.425","Text":"Each method performs 1 task only. Will pause there."},{"Start":"05:15.425 ","End":"05:16.685","Text":"In the next video,"},{"Start":"05:16.685 ","End":"05:19.550","Text":"we\u0027ll use this new knowledge of cohesion to help us"},{"Start":"05:19.550 ","End":"05:23.570","Text":"understand some of the design principles we saw earlier."},{"Start":"05:23.570 ","End":"05:26.940","Text":"Thanks for watching and see you soon."}],"ID":31186},{"Watched":false,"Name":"Refactoring","Duration":"6m 20s","ChapterTopicVideoID":29605,"CourseChapterTopicPlaylistID":294565,"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, welcome back. It may seem obvious that"},{"Start":"00:03.510 ","End":"00:07.635","Text":"code duplication is not a desirable feature of our programs."},{"Start":"00:07.635 ","End":"00:14.070","Text":"But less obvious is that code duplication causes issues with maintenance."},{"Start":"00:14.070 ","End":"00:18.014","Text":"Code duplication can also lead to bugs."},{"Start":"00:18.014 ","End":"00:21.915","Text":"If you find that there is duplication somewhere in your code,"},{"Start":"00:21.915 ","End":"00:26.885","Text":"this is an indicator that a bad design decision has been made somewhere along the line."},{"Start":"00:26.885 ","End":"00:29.210","Text":"You should look again at your design,"},{"Start":"00:29.210 ","End":"00:33.035","Text":"and see how you can modify it to remove the duplication."},{"Start":"00:33.035 ","End":"00:37.175","Text":"It can be difficult to get all aspects of a design correct,"},{"Start":"00:37.175 ","End":"00:42.320","Text":"especially when requirements change or very loosely defined in the first place."},{"Start":"00:42.320 ","End":"00:45.740","Text":"It\u0027s common therefore when making changes to code,"},{"Start":"00:45.740 ","End":"00:49.130","Text":"to talk about refactoring."},{"Start":"00:49.130 ","End":"00:52.790","Text":"This is the restructuring of an existing design,"},{"Start":"00:52.790 ","End":"00:58.070","Text":"in order to improve it when making a modification or extension."},{"Start":"00:58.070 ","End":"01:03.595","Text":"The refactoring might start with something as simple as renaming attributes or classes,"},{"Start":"01:03.595 ","End":"01:06.590","Text":"to make their role in the design clearer."},{"Start":"01:06.590 ","End":"01:10.070","Text":"Or moving things from 1 class to another,"},{"Start":"01:10.070 ","End":"01:12.995","Text":"where they might conceptually be a better fit."},{"Start":"01:12.995 ","End":"01:14.675","Text":"Or at the extremes,"},{"Start":"01:14.675 ","End":"01:19.370","Text":"a complete rigging of the relationships between classes."},{"Start":"01:19.370 ","End":"01:24.080","Text":"Sometimes we refactor just to improve an existing design,"},{"Start":"01:24.080 ","End":"01:28.595","Text":"ready to accommodate any changing requirements in the future."},{"Start":"01:28.595 ","End":"01:30.950","Text":"To give a simple example,"},{"Start":"01:30.950 ","End":"01:34.310","Text":"here we have code that displays a menu of options,"},{"Start":"01:34.310 ","End":"01:37.355","Text":"and then processes the user\u0027s choice."},{"Start":"01:37.355 ","End":"01:41.840","Text":"It works, but it would benefit from some refactoring."},{"Start":"01:41.840 ","End":"01:46.490","Text":"The loop contains code to display options,"},{"Start":"01:46.490 ","End":"01:51.664","Text":"and then also processes the option choices."},{"Start":"01:51.664 ","End":"01:55.760","Text":"It will be better if the lines in red were moved to"},{"Start":"01:55.760 ","End":"02:00.410","Text":"a separate method and then that method could be called from inside the loop."},{"Start":"02:00.410 ","End":"02:03.950","Text":"It can also be called outside the loop, for example,"},{"Start":"02:03.950 ","End":"02:07.415","Text":"when the program first loads or in a help page."},{"Start":"02:07.415 ","End":"02:10.040","Text":"This avoids code duplication."},{"Start":"02:10.040 ","End":"02:13.370","Text":"If a new menu option is to be added to the program,"},{"Start":"02:13.370 ","End":"02:17.795","Text":"then the method that displays all these options can be edited."},{"Start":"02:17.795 ","End":"02:20.690","Text":"Of course, we\u0027d still have to write the code in"},{"Start":"02:20.690 ","End":"02:23.855","Text":"the loop to perform that new option down here."},{"Start":"02:23.855 ","End":"02:26.150","Text":"But the principle of 1 method,"},{"Start":"02:26.150 ","End":"02:28.195","Text":"1 task is valid."},{"Start":"02:28.195 ","End":"02:31.165","Text":"This method should process the options,"},{"Start":"02:31.165 ","End":"02:34.924","Text":"and the other 1 should display the available options."},{"Start":"02:34.924 ","End":"02:37.280","Text":"To improve the code further,"},{"Start":"02:37.280 ","End":"02:41.450","Text":"we can move the processing commands out to a separate method 2."},{"Start":"02:41.450 ","End":"02:47.660","Text":"Now, this loop is the central core of the program and it performs 1 function,"},{"Start":"02:47.660 ","End":"02:52.060","Text":"to keep accepting keyboard input until quit is entered."},{"Start":"02:52.060 ","End":"02:54.800","Text":"The 2 subprograms being called,"},{"Start":"02:54.800 ","End":"02:59.855","Text":"display the options and process the options."},{"Start":"02:59.855 ","End":"03:02.900","Text":"Again, they perform 1 function only."},{"Start":"03:02.900 ","End":"03:07.505","Text":"Their responsibilities are clear and distinct from each other."},{"Start":"03:07.505 ","End":"03:10.558","Text":"1 displays the other processes."},{"Start":"03:10.558 ","End":"03:13.490","Text":"By refactoring in this way,"},{"Start":"03:13.490 ","End":"03:16.025","Text":"we\u0027ve not only made the code more cohesive,"},{"Start":"03:16.025 ","End":"03:20.070","Text":"we\u0027ve decoupled the 2 parts of code as well."},{"Start":"03:20.260 ","End":"03:26.165","Text":"Objects should be designed in such a way that any code that might be subject to change,"},{"Start":"03:26.165 ","End":"03:28.700","Text":"should be in embedded inside the object."},{"Start":"03:28.700 ","End":"03:31.850","Text":"Any external code that may use the object,"},{"Start":"03:31.850 ","End":"03:35.345","Text":"will not be affected by any internal changes."},{"Start":"03:35.345 ","End":"03:38.015","Text":"Say we had a music player class,"},{"Start":"03:38.015 ","End":"03:41.525","Text":"which gives us the ability to play MP3 files."},{"Start":"03:41.525 ","End":"03:44.240","Text":"We\u0027ve made in the future, wants to provide the ability to"},{"Start":"03:44.240 ","End":"03:47.435","Text":"support playback of different music file formats."},{"Start":"03:47.435 ","End":"03:51.500","Text":"The right thing to do is hide away all the code and data within the object,"},{"Start":"03:51.500 ","End":"03:55.700","Text":"and allow access to the features of the object through provided public methods."},{"Start":"03:55.700 ","End":"03:58.895","Text":"For example, play, pause, stop, etc."},{"Start":"03:58.895 ","End":"04:02.915","Text":"You might have some other code which provides a graphical user interface,"},{"Start":"04:02.915 ","End":"04:06.245","Text":"with a familiar buttons for play pause and so on."},{"Start":"04:06.245 ","End":"04:08.480","Text":"Clicking on these buttons,"},{"Start":"04:08.480 ","End":"04:11.030","Text":"calls a relevant method in the class."},{"Start":"04:11.030 ","End":"04:13.400","Text":"The file format being used,"},{"Start":"04:13.400 ","End":"04:15.995","Text":"should be irrelevant to any external code,"},{"Start":"04:15.995 ","End":"04:17.630","Text":"like the GUI code."},{"Start":"04:17.630 ","End":"04:22.835","Text":"That\u0027s a detail taken care of internally by the music player class."},{"Start":"04:22.835 ","End":"04:25.340","Text":"Then 1 day when we develop some code to"},{"Start":"04:25.340 ","End":"04:27.650","Text":"allow a different type of music file to be played,"},{"Start":"04:27.650 ","End":"04:29.915","Text":"like a web file or a FLAC file."},{"Start":"04:29.915 ","End":"04:32.270","Text":"The external code, for example,"},{"Start":"04:32.270 ","End":"04:33.890","Text":"the user interface code,"},{"Start":"04:33.890 ","End":"04:35.555","Text":"doesn\u0027t have to be changed."},{"Start":"04:35.555 ","End":"04:37.955","Text":"It just carries on working as before."},{"Start":"04:37.955 ","End":"04:41.300","Text":"That\u0027s a sign of good design."},{"Start":"04:41.300 ","End":"04:44.915","Text":"Another practical way of planning for variation through"},{"Start":"04:44.915 ","End":"04:49.355","Text":"encapsulation is using enumerations."},{"Start":"04:49.355 ","End":"04:55.220","Text":"An enumerated type is a custom data type that allows a variable to take on a value,"},{"Start":"04:55.220 ","End":"05:00.905","Text":"from a member of a set of predefined constants."},{"Start":"05:00.905 ","End":"05:04.790","Text":"Enumerations allow us to store an ordered set of values,"},{"Start":"05:04.790 ","End":"05:08.120","Text":"that won\u0027t be modified or added to during runtime."},{"Start":"05:08.120 ","End":"05:12.125","Text":"This feature is useful when we have a small set of allowed values,"},{"Start":"05:12.125 ","End":"05:15.160","Text":"that in the future we might want to add to."},{"Start":"05:15.160 ","End":"05:19.145","Text":"The compiler can then be used to check that no values outside the set,"},{"Start":"05:19.145 ","End":"05:22.190","Text":"have been used in the program by mistake."},{"Start":"05:22.190 ","End":"05:27.155","Text":"For example, if we wanted to store a set of commands for a robot,"},{"Start":"05:27.155 ","End":"05:28.820","Text":"we could use the following."},{"Start":"05:28.820 ","End":"05:31.340","Text":"Then somewhere in our program,"},{"Start":"05:31.340 ","End":"05:33.960","Text":"we could use this."},{"Start":"05:34.180 ","End":"05:38.705","Text":"If we tried to put the following line into our program,"},{"Start":"05:38.705 ","End":"05:42.170","Text":"we\u0027d get an error signaled by the compiler."},{"Start":"05:42.170 ","End":"05:48.215","Text":"Because upwards has not been defined within our enum commands."},{"Start":"05:48.215 ","End":"05:53.420","Text":"We could also write our own validation code to check that any data input by the user,"},{"Start":"05:53.420 ","End":"05:57.470","Text":"matches a valid input from a set of allowed values."},{"Start":"05:57.470 ","End":"06:01.520","Text":"We\u0027ve encapsulated what might vary in the future,"},{"Start":"06:01.520 ","End":"06:04.940","Text":"according to changing requirements of the program by"},{"Start":"06:04.940 ","End":"06:09.200","Text":"embedding the allowed commands into the enumeration."},{"Start":"06:09.200 ","End":"06:12.275","Text":"We\u0027ll pause again there and when we\u0027ll come back,"},{"Start":"06:12.275 ","End":"06:15.710","Text":"we\u0027ll look at the idea of aggregation of classes,"},{"Start":"06:15.710 ","End":"06:18.740","Text":"and why it should be preferred over inheritance."},{"Start":"06:18.740 ","End":"06:21.210","Text":"Thanks for watching. See you soon."}],"ID":31187},{"Watched":false,"Name":"Associations","Duration":"6m 14s","ChapterTopicVideoID":29606,"CourseChapterTopicPlaylistID":294565,"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":"Hi everyone. Welcome back."},{"Start":"00:02.110 ","End":"00:04.420","Text":"In any object-oriented program,"},{"Start":"00:04.420 ","End":"00:09.325","Text":"we\u0027d expect objects created from different classes to interact with each other."},{"Start":"00:09.325 ","End":"00:15.400","Text":"We create links between the objects by providing methods to pass values between them."},{"Start":"00:15.400 ","End":"00:18.490","Text":"These links are known as associations."},{"Start":"00:18.490 ","End":"00:21.955","Text":"When showing links between classes in UML,"},{"Start":"00:21.955 ","End":"00:26.125","Text":"we draw lines between the 2 classes to show the association."},{"Start":"00:26.125 ","End":"00:28.900","Text":"A simple arrowhead on the line shows"},{"Start":"00:28.900 ","End":"00:35.800","Text":"a dependency relationship where 1 class is making use of facilities in the other class."},{"Start":"00:35.800 ","End":"00:39.220","Text":"In this example, the user interface might be used to"},{"Start":"00:39.220 ","End":"00:42.580","Text":"choose a song from a list of available songs."},{"Start":"00:42.580 ","End":"00:45.425","Text":"Then the music player loads relevant file,"},{"Start":"00:45.425 ","End":"00:48.860","Text":"decodes it, and plays out the audio."},{"Start":"00:48.860 ","End":"00:54.605","Text":"We can identify this dependency relationship with Uses-a,"},{"Start":"00:54.605 ","End":"00:57.275","Text":"user interface, Uses-a,"},{"Start":"00:57.275 ","End":"00:59.920","Text":"music player in this example."},{"Start":"00:59.920 ","End":"01:06.440","Text":"As we now align with an empty triangle on the end shows an inheritance association."},{"Start":"01:06.440 ","End":"01:13.955","Text":"This relationship is known as a relationship or an, is-a relationship."},{"Start":"01:13.955 ","End":"01:17.285","Text":"A kangaroo is a marsupial."},{"Start":"01:17.285 ","End":"01:21.120","Text":"A marsupial is a mammal."},{"Start":"01:21.530 ","End":"01:24.965","Text":"The relationship between 2 or more classes,"},{"Start":"01:24.965 ","End":"01:30.560","Text":"known as aggregation, is shown with a diamond symbol on the end of the line."},{"Start":"01:30.560 ","End":"01:36.540","Text":"This type of association is known as a has-a relationship."},{"Start":"01:37.480 ","End":"01:41.000","Text":"In a previous topic, we saw how collection classes"},{"Start":"01:41.000 ","End":"01:44.180","Text":"allow us to group together objects of a similar type."},{"Start":"01:44.180 ","End":"01:48.200","Text":"A more general principle in programming called aggregation,"},{"Start":"01:48.200 ","End":"01:52.880","Text":"is about bringing together parts to create a larger whole."},{"Start":"01:52.880 ","End":"01:58.070","Text":"In the real-world, we often talk about aggregates in mining or construction."},{"Start":"01:58.070 ","End":"02:01.850","Text":"For example, concrete is made up of a mixture of sand,"},{"Start":"02:01.850 ","End":"02:05.465","Text":"small stones, water, and cement."},{"Start":"02:05.465 ","End":"02:09.935","Text":"Cement itself is an aggregate of lime and silica."},{"Start":"02:09.935 ","End":"02:13.040","Text":"All these individual ingredients brought together into a"},{"Start":"02:13.040 ","End":"02:17.975","Text":"collective makes for the thing we called concrete."},{"Start":"02:17.975 ","End":"02:24.800","Text":"Soil is also an aggregate made up of decomposing or decomposed organic materials,"},{"Start":"02:24.800 ","End":"02:28.070","Text":"minerals, gases, and water."},{"Start":"02:28.070 ","End":"02:31.580","Text":"Again, these ingredients make up the thing we call soil."},{"Start":"02:31.580 ","End":"02:33.695","Text":"In object oriented programming,"},{"Start":"02:33.695 ","End":"02:36.050","Text":"we\u0027re bringing together different classes,"},{"Start":"02:36.050 ","End":"02:40.790","Text":"which are the ingredients we use to create a larger unit of code."},{"Start":"02:40.790 ","End":"02:46.250","Text":"There are 2 distinct types of aggregation in object oriented programming."},{"Start":"02:46.250 ","End":"02:50.810","Text":"Plain old aggregation is a whole part relationship or"},{"Start":"02:50.810 ","End":"02:55.985","Text":"association in which a part can be associated with more than 1 whole."},{"Start":"02:55.985 ","End":"02:59.675","Text":"This is aggregation with shareable parts."},{"Start":"02:59.675 ","End":"03:04.700","Text":"Composition is a whole part relationship or association in"},{"Start":"03:04.700 ","End":"03:09.710","Text":"which the parts of a whole cannot be shared with any other whole."},{"Start":"03:09.710 ","End":"03:11.885","Text":"A book has a page."},{"Start":"03:11.885 ","End":"03:15.500","Text":"The page cannot be in 2 different books."},{"Start":"03:15.500 ","End":"03:18.845","Text":"It\u0027s part of the book and cannot be shared."},{"Start":"03:18.845 ","End":"03:21.920","Text":"Another way to say this is the book is"},{"Start":"03:21.920 ","End":"03:26.315","Text":"composed of pages which cannot be shared with other books."},{"Start":"03:26.315 ","End":"03:28.670","Text":"This makes it a composition."},{"Start":"03:28.670 ","End":"03:32.195","Text":"A composition is drawn with a filled-in diagram."},{"Start":"03:32.195 ","End":"03:35.630","Text":"The degree of the relationship or how many of the items are"},{"Start":"03:35.630 ","End":"03:40.369","Text":"involved is either shown by a number or a range of numbers,"},{"Start":"03:40.369 ","End":"03:44.075","Text":"or an asterix meaning 0 or more."},{"Start":"03:44.075 ","End":"03:46.475","Text":"Let\u0027s look at another example."},{"Start":"03:46.475 ","End":"03:50.360","Text":"A clock is made up of hands or face,"},{"Start":"03:50.360 ","End":"03:54.215","Text":"and the electromechanical mechanism that moves the hands."},{"Start":"03:54.215 ","End":"03:59.525","Text":"We can consider these 3 components to be an essential part of a clock."},{"Start":"03:59.525 ","End":"04:02.300","Text":"The parts themselves have no real use,"},{"Start":"04:02.300 ","End":"04:04.670","Text":"when not associated together as a whole."},{"Start":"04:04.670 ","End":"04:08.665","Text":"A clock is clearly a composition."},{"Start":"04:08.665 ","End":"04:15.800","Text":"Hence a solid diamond is used in the diagram showing the association between the parts."},{"Start":"04:15.800 ","End":"04:20.840","Text":"But the clock could also use a battery to power it."},{"Start":"04:20.840 ","End":"04:24.590","Text":"But because the battery can be removed and used elsewhere,"},{"Start":"04:24.590 ","End":"04:31.850","Text":"it has a use outside the context of the clock so it\u0027s drawn with an empty diamond,"},{"Start":"04:31.850 ","End":"04:34.310","Text":"meaning plain old aggregation,"},{"Start":"04:34.310 ","End":"04:37.205","Text":"rather than composition which we have here."},{"Start":"04:37.205 ","End":"04:40.520","Text":"You might argue that a battery is an essential part of"},{"Start":"04:40.520 ","End":"04:44.015","Text":"the clock and therefore should be drawn with a solid diamond."},{"Start":"04:44.015 ","End":"04:50.215","Text":"But remember the hollow diamond is indicating that this is a shareable part."},{"Start":"04:50.215 ","End":"04:53.705","Text":"Also, the clock is still a clock without the battery."},{"Start":"04:53.705 ","End":"04:56.149","Text":"It just won\u0027t be able to move its hands."},{"Start":"04:56.149 ","End":"05:00.080","Text":"Looking at another example, imagine a university."},{"Start":"05:00.080 ","End":"05:07.085","Text":"The university is composed of faculties which cannot be part of other universities."},{"Start":"05:07.085 ","End":"05:09.995","Text":"This is a solid diamond symbol."},{"Start":"05:09.995 ","End":"05:14.615","Text":"Another way of saying this is a university, has a faculty."},{"Start":"05:14.615 ","End":"05:16.820","Text":"But if we look at the bottom,"},{"Start":"05:16.820 ","End":"05:24.169","Text":"the relationship of a lecturer to 2 specific faculties is shown with a hollow diamond."},{"Start":"05:24.169 ","End":"05:27.830","Text":"What we\u0027re showing here is that a lecturer can teach in"},{"Start":"05:27.830 ","End":"05:30.485","Text":"both computer science and"},{"Start":"05:30.485 ","End":"05:35.825","Text":"electronics faculties and doesn\u0027t belong exclusively to either of them."},{"Start":"05:35.825 ","End":"05:39.275","Text":"Lecturer is a shareable part."},{"Start":"05:39.275 ","End":"05:44.705","Text":"The other type of relationship we\u0027re showing here is the is-a relationship."},{"Start":"05:44.705 ","End":"05:52.549","Text":"Computer science is a faculty and electronics is a faculty."},{"Start":"05:52.549 ","End":"05:57.080","Text":"They are specializations of the general faculty type."},{"Start":"05:57.080 ","End":"06:02.260","Text":"Hence, this relationship inheritance is shown by the open arrow head."},{"Start":"06:02.260 ","End":"06:04.520","Text":"We\u0027ll pause there 1 final time."},{"Start":"06:04.520 ","End":"06:05.795","Text":"In the next video,"},{"Start":"06:05.795 ","End":"06:11.150","Text":"we\u0027ll relate this knowledge of aggregation to 1 of our most important design principles."},{"Start":"06:11.150 ","End":"06:14.880","Text":"Thanks very much for watching and see you shortly."}],"ID":31188},{"Watched":false,"Name":"Composition vs Inheritance","Duration":"4m 58s","ChapterTopicVideoID":29607,"CourseChapterTopicPlaylistID":294565,"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.685","Text":"Hello again, welcome back."},{"Start":"00:02.685 ","End":"00:06.090","Text":"Coming back to one of our original design principles,"},{"Start":"00:06.090 ","End":"00:10.170","Text":"these principles about knowing when it\u0027s appropriate to use inheritance and"},{"Start":"00:10.170 ","End":"00:14.940","Text":"when using aggregation instead might lead to a better design."},{"Start":"00:14.940 ","End":"00:20.130","Text":"Inheritance has its advantages but generally these are only apply if there\u0027s"},{"Start":"00:20.130 ","End":"00:26.355","Text":"a really natural and close relationship between the superclass and sub classes."},{"Start":"00:26.355 ","End":"00:31.425","Text":"We can reuse the common code in the base class in the subclasses."},{"Start":"00:31.425 ","End":"00:33.600","Text":"We can then specialize the behavior in"},{"Start":"00:33.600 ","End":"00:38.175","Text":"those sub classes to only change the behavior that should be different."},{"Start":"00:38.175 ","End":"00:42.060","Text":"We, of course, benefit from polymorphic methods."},{"Start":"00:42.060 ","End":"00:45.080","Text":"But in practice this situation of"},{"Start":"00:45.080 ","End":"00:47.870","Text":"a really natural and close relationship between"},{"Start":"00:47.870 ","End":"00:52.160","Text":"super class and sub classes doesn\u0027t often arise."},{"Start":"00:52.160 ","End":"00:55.205","Text":"Then we have to accept the other downsides,"},{"Start":"00:55.205 ","End":"00:59.690","Text":"which is that the inheritance itself leads to quite tight coupling"},{"Start":"00:59.690 ","End":"01:04.040","Text":"of the code in the child and ancestor classes and that"},{"Start":"01:04.040 ","End":"01:06.830","Text":"making polymorphic method calls involves"},{"Start":"01:06.830 ","End":"01:09.770","Text":"some runtime decision making and therefore"},{"Start":"01:09.770 ","End":"01:13.580","Text":"there could be an impact on the performance of the code."},{"Start":"01:13.580 ","End":"01:17.570","Text":"However, there\u0027s no denying that there can be some advantages in"},{"Start":"01:17.570 ","End":"01:21.730","Text":"some situations when using inheritance."},{"Start":"01:21.730 ","End":"01:25.700","Text":"Basically, if it meets the is a test and"},{"Start":"01:25.700 ","End":"01:30.140","Text":"the vast majority of the methods and data in the base class are going to be"},{"Start":"01:30.140 ","End":"01:33.575","Text":"used then there may well be a case for"},{"Start":"01:33.575 ","End":"01:38.645","Text":"inheritance otherwise aggregation is usually the way to go."},{"Start":"01:38.645 ","End":"01:42.770","Text":"Hence we prefer aggregation over inheritance"},{"Start":"01:42.770 ","End":"01:47.180","Text":"unless there\u0027s strong evidence to suggest otherwise."},{"Start":"01:47.180 ","End":"01:53.735","Text":"Aggregation is much more likely to result in reusable code for other projects."},{"Start":"01:53.735 ","End":"01:56.980","Text":"It\u0027s also likely to lead to the development of code that has"},{"Start":"01:56.980 ","End":"02:01.600","Text":"looser coupling given that there are no superclasses to worry about."},{"Start":"02:01.600 ","End":"02:06.460","Text":"This final principle is about the idea that classes and methods should be"},{"Start":"02:06.460 ","End":"02:08.320","Text":"designed so that we don\u0027t worry about"},{"Start":"02:08.320 ","End":"02:12.070","Text":"the internal details of how a method has been implemented."},{"Start":"02:12.070 ","End":"02:14.170","Text":"In fact, put more strongly,"},{"Start":"02:14.170 ","End":"02:17.065","Text":"we shouldn\u0027t allow our methods to make"},{"Start":"02:17.065 ","End":"02:21.145","Text":"assumptions about how something has been implemented."},{"Start":"02:21.145 ","End":"02:24.400","Text":"Interactions between classes should be based on"},{"Start":"02:24.400 ","End":"02:29.710","Text":"carefully selected public methods and return values received from them."},{"Start":"02:29.710 ","End":"02:34.030","Text":"Here, the user interface class could be designed to play"},{"Start":"02:34.030 ","End":"02:39.605","Text":"a song by calling a method set filename in the music player class,"},{"Start":"02:39.605 ","End":"02:42.365","Text":"which could change the file name attribute."},{"Start":"02:42.365 ","End":"02:50.450","Text":"Play inside music player then loads the file by using filename when called and plays it."},{"Start":"02:50.450 ","End":"02:53.780","Text":"The trouble with this is that if we decide to change"},{"Start":"02:53.780 ","End":"02:58.040","Text":"the way that the music player class works internally, for example,"},{"Start":"02:58.040 ","End":"03:02.075","Text":"it uses an array list of filenames so we can have a whole list of songs,"},{"Start":"03:02.075 ","End":"03:07.595","Text":"we\u0027ll have broken the code and we\u0027ll need to change the user interface class as well."},{"Start":"03:07.595 ","End":"03:11.030","Text":"If we had designed it from the start to have no knowledge of"},{"Start":"03:11.030 ","End":"03:14.870","Text":"the internal workings of music player we\u0027d have a better design."},{"Start":"03:14.870 ","End":"03:19.220","Text":"For example, the music player could have provided a request method to which"},{"Start":"03:19.220 ","End":"03:23.420","Text":"we pass a song name and if it\u0027s able to insert"},{"Start":"03:23.420 ","End":"03:27.530","Text":"the song internally into a queue and returns true to"},{"Start":"03:27.530 ","End":"03:32.825","Text":"the user interface saying it was able to fulfill the request."},{"Start":"03:32.825 ","End":"03:38.485","Text":"The play method keeps track of which songs currently played in the queue."},{"Start":"03:38.485 ","End":"03:42.213","Text":"Where classes have dependencies on other classes"},{"Start":"03:42.213 ","End":"03:45.424","Text":"stick into this principle of programming to the interface,"},{"Start":"03:45.424 ","End":"03:47.440","Text":"not the implementation,"},{"Start":"03:47.440 ","End":"03:54.560","Text":"makes it easier to simplify code to eliminate unwanted or unneeded dependencies."},{"Start":"03:54.560 ","End":"03:59.495","Text":"This will make for code that\u0027s much easier to reuse in other projects."},{"Start":"03:59.495 ","End":"04:04.400","Text":"There are some languages that provides special features to encourage this design."},{"Start":"04:04.400 ","End":"04:09.729","Text":"In Java there\u0027s a type of class called an interface that can be used for this purpose."},{"Start":"04:09.729 ","End":"04:13.025","Text":"However, all languages, even procedural languages,"},{"Start":"04:13.025 ","End":"04:15.950","Text":"can benefit from this idea that we write our code"},{"Start":"04:15.950 ","End":"04:19.553","Text":"based on values passed into code units i.e."},{"Start":"04:19.553 ","End":"04:24.440","Text":"methods or functions and we respond to any values returned"},{"Start":"04:24.440 ","End":"04:30.140","Text":"without relying on any knowledge of the internal workings of the code."},{"Start":"04:30.140 ","End":"04:33.845","Text":"That\u0027s it for our overview of design principles."},{"Start":"04:33.845 ","End":"04:37.700","Text":"In this section we learned how to explain the key features of"},{"Start":"04:37.700 ","End":"04:41.390","Text":"well designed object oriented programs to distinguish"},{"Start":"04:41.390 ","End":"04:44.870","Text":"between aggregation and composition and"},{"Start":"04:44.870 ","End":"04:49.940","Text":"evaluate when composition or inheritance is more appropriate."},{"Start":"04:49.940 ","End":"04:54.440","Text":"As ever be sure to complete the exercises and to watch the video solutions."},{"Start":"04:54.440 ","End":"04:58.590","Text":"Thank you very much for watching and see you soon."}],"ID":31189},{"Watched":false,"Name":"Exercise 1","Duration":"12m 41s","ChapterTopicVideoID":29608,"CourseChapterTopicPlaylistID":294565,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.020 ","End":"00:04.095","Text":"Hello and welcome to this exercise on design principles."},{"Start":"00:04.095 ","End":"00:05.340","Text":"We\u0027ve been asked, first of all,"},{"Start":"00:05.340 ","End":"00:07.935","Text":"to create a new project called Aggregation,"},{"Start":"00:07.935 ","End":"00:12.540","Text":"and then in Part a to create a new class called Page as follows."},{"Start":"00:12.540 ","End":"00:14.805","Text":"In Part 1, we add 2 attributes,"},{"Start":"00:14.805 ","End":"00:18.345","Text":"a String called content and an int called pageNumber."},{"Start":"00:18.345 ","End":"00:23.310","Text":"We then create a constructor which sets the 2 attributes to the values passed in,"},{"Start":"00:23.310 ","End":"00:26.805","Text":"and then we create an overridden method toString,"},{"Start":"00:26.805 ","End":"00:29.010","Text":"which returns a newline character,"},{"Start":"00:29.010 ","End":"00:31.530","Text":"concatenated with the content attribute,"},{"Start":"00:31.530 ","End":"00:33.915","Text":"concatenated with another newline,"},{"Start":"00:33.915 ","End":"00:39.525","Text":"and then the pageNumber attribute followed by 1 final newline character."},{"Start":"00:39.525 ","End":"00:45.040","Text":"In Part b, we\u0027re asked to test the Page class on the BlueJ object workbench,"},{"Start":"00:45.040 ","End":"00:51.170","Text":"by creating an object with the content attributes set to Test Page and page number as 1,"},{"Start":"00:51.170 ","End":"00:54.020","Text":"and then we run the toString method to see"},{"Start":"00:54.020 ","End":"00:57.805","Text":"the output includes the content and page number."},{"Start":"00:57.805 ","End":"01:01.880","Text":"In Part c, we\u0027re asked to create a new class called Book,"},{"Start":"01:01.880 ","End":"01:05.900","Text":"which has a single String attribute called title,"},{"Start":"01:05.900 ","End":"01:08.810","Text":"and a constructor which sets its value."},{"Start":"01:08.810 ","End":"01:11.165","Text":"In Part 2, we\u0027re asked to add"},{"Start":"01:11.165 ","End":"01:16.615","Text":"an ArrayList called pages which stores a collection of Page objects."},{"Start":"01:16.615 ","End":"01:20.615","Text":"In Part 3, we\u0027re asked to add a method called addPage,"},{"Start":"01:20.615 ","End":"01:24.770","Text":"which accepts a Page object as a parameter and then uses"},{"Start":"01:24.770 ","End":"01:31.540","Text":"the add method of the ArrayList pages to add the passed in page to the book."},{"Start":"01:31.540 ","End":"01:34.290","Text":"In Part 4, we\u0027re asked to add a method called"},{"Start":"01:34.290 ","End":"01:37.775","Text":"readBook which first outputs the title of the book,"},{"Start":"01:37.775 ","End":"01:39.815","Text":"followed by a newline character,"},{"Start":"01:39.815 ","End":"01:45.620","Text":"and then the entire contents of the pages collection using a loop."},{"Start":"01:45.620 ","End":"01:53.430","Text":"Then we test on the object workbench by first creating pages with the following content."},{"Start":"01:56.060 ","End":"01:58.974","Text":"Then still on the object workbench,"},{"Start":"01:58.974 ","End":"02:02.650","Text":"we create a Book object with the title Treasure Island,"},{"Start":"02:02.650 ","End":"02:06.630","Text":"and use the addPage method of the book object to add"},{"Start":"02:06.630 ","End":"02:11.040","Text":"each page you created in Part d to the book in the correct order."},{"Start":"02:11.040 ","End":"02:14.910","Text":"If the first page object was called Page 1, for example,"},{"Start":"02:14.910 ","End":"02:17.835","Text":"then add that first to the book using addPage,"},{"Start":"02:17.835 ","End":"02:20.355","Text":"then Page 2, and so on."},{"Start":"02:20.355 ","End":"02:24.100","Text":"We then run the readBook method to see whether the output in"},{"Start":"02:24.100 ","End":"02:29.660","Text":"the terminal window matches what you\u0027d expect for a 3-page book."},{"Start":"02:29.660 ","End":"02:36.555","Text":"We finally asked what relationship is there between the book and page?"},{"Start":"02:36.555 ","End":"02:38.460","Text":"In making that interpretation,"},{"Start":"02:38.460 ","End":"02:42.105","Text":"we ignore the arrow drawn by BlueJ."},{"Start":"02:42.105 ","End":"02:50.070","Text":"I\u0027ve already created the new project and the page class and in Part a1,"},{"Start":"02:50.070 ","End":"02:52.265","Text":"I\u0027ve been asked to add 2 attributes."},{"Start":"02:52.265 ","End":"02:56.975","Text":"The first one is a String called content,"},{"Start":"02:56.975 ","End":"03:01.033","Text":"where I\u0027m going to store the content for the page,"},{"Start":"03:01.033 ","End":"03:06.190","Text":"and then the second one is an int called pageNumber,"},{"Start":"03:06.190 ","End":"03:08.130","Text":"where I store the page number."},{"Start":"03:08.130 ","End":"03:10.030","Text":"To do a2,"},{"Start":"03:10.030 ","End":"03:15.005","Text":"I need to create a constructor which is obviously called Page,"},{"Start":"03:15.005 ","End":"03:18.109","Text":"and takes 2 arguments."},{"Start":"03:18.109 ","End":"03:20.290","Text":"The first one is a String,"},{"Start":"03:20.290 ","End":"03:24.440","Text":"and we\u0027ll call that content and second one is an int,"},{"Start":"03:24.440 ","End":"03:26.570","Text":"which we\u0027ll just call pageNumber."},{"Start":"03:26.570 ","End":"03:32.875","Text":"Then to set those 2 attributes as usual,"},{"Start":"03:32.875 ","End":"03:34.748","Text":"we do the following."},{"Start":"03:34.748 ","End":"03:37.290","Text":"And that\u0027s the second part done."},{"Start":"03:37.290 ","End":"03:41.750","Text":"Final bit we\u0027ve got to do for this very straightforward little class is"},{"Start":"03:41.750 ","End":"03:46.560","Text":"to add a override of the toString method,"},{"Start":"03:46.560 ","End":"03:49.220","Text":"so I\u0027ll put the override annotation here just to check that I\u0027ve got"},{"Start":"03:49.220 ","End":"03:54.200","Text":"the right signature and it\u0027s a String that is returned."},{"Start":"03:54.200 ","End":"04:02.010","Text":"The method is called toString and that seems to be fine once I add the return keyword."},{"Start":"04:02.980 ","End":"04:06.410","Text":"As the instructions tell us,"},{"Start":"04:06.410 ","End":"04:08.510","Text":"we want to return a newline character,"},{"Start":"04:08.510 ","End":"04:13.280","Text":"first of all, to break onto a new line, presumably."},{"Start":"04:13.280 ","End":"04:17.105","Text":"Then the content attribute,"},{"Start":"04:17.105 ","End":"04:25.355","Text":"another newline, followed by pageNumber and 1 final newline character."},{"Start":"04:25.355 ","End":"04:29.885","Text":"That\u0027s that method done and we can test."},{"Start":"04:29.885 ","End":"04:34.420","Text":"Got page numbers spelled wrong here and I can now test that."},{"Start":"04:34.420 ","End":"04:42.000","Text":"Let\u0027s create a page with any more content in test page will do us and pageNumber of 1."},{"Start":"04:42.000 ","End":"04:46.473","Text":"If I run the toString method which has been overridden,"},{"Start":"04:46.473 ","End":"04:51.305","Text":"as we know, there\u0027ll be a toString in here as well and it\u0027s been redefined as we\u0027re told."},{"Start":"04:51.305 ","End":"04:54.740","Text":"I seem to get back something sensible."},{"Start":"04:54.740 ","End":"04:56.975","Text":"There\u0027s the test page, there\u0027s a page number,"},{"Start":"04:56.975 ","End":"04:59.240","Text":"and a load of newline characters."},{"Start":"04:59.240 ","End":"05:01.480","Text":"That seems to have done the job."},{"Start":"05:01.480 ","End":"05:04.610","Text":"Now, I need to proceed to create,"},{"Start":"05:04.610 ","End":"05:06.335","Text":"as it tells us in Part c,"},{"Start":"05:06.335 ","End":"05:08.030","Text":"a new class called Book."},{"Start":"05:08.030 ","End":"05:10.370","Text":"I\u0027ve got an empty one here."},{"Start":"05:10.370 ","End":"05:16.040","Text":"Book is going to have a String attribute called title,"},{"Start":"05:16.040 ","End":"05:19.385","Text":"a constructor to set the value of title,"},{"Start":"05:19.385 ","End":"05:21.325","Text":"so let\u0027s do that."},{"Start":"05:21.325 ","End":"05:24.646","Text":"We\u0027ve done in Part c1 then."},{"Start":"05:24.646 ","End":"05:27.664","Text":"Then we\u0027d been asked to add an ArrayList called pages,"},{"Start":"05:27.664 ","End":"05:31.280","Text":"which is going to store a collection of Page objects."},{"Start":"05:31.280 ","End":"05:33.140","Text":"We just created the page class,"},{"Start":"05:33.140 ","End":"05:36.065","Text":"so we\u0027re going to store a collection of those."},{"Start":"05:36.065 ","End":"05:39.935","Text":"The way to do that is to do the following."},{"Start":"05:39.935 ","End":"05:43.160","Text":"We\u0027re going to create an ArrayList."},{"Start":"05:43.160 ","End":"05:47.630","Text":"It\u0027s going to store objects of type Page,"},{"Start":"05:47.630 ","End":"05:56.198","Text":"and it\u0027s going to be called pages maybe created by using new ArrayList."},{"Start":"05:56.198 ","End":"05:57.620","Text":"That should do the job."},{"Start":"05:57.620 ","End":"06:01.115","Text":"Of course, that won\u0027t compile because it doesn\u0027t know anything about"},{"Start":"06:01.115 ","End":"06:06.269","Text":"array lists unless we import the correct class,"},{"Start":"06:06.269 ","End":"06:14.735","Text":"so java.util.ArrayList, and that should compile now without any problems."},{"Start":"06:14.735 ","End":"06:19.960","Text":"Now, we need to add a method in Part 3 called addPage."},{"Start":"06:19.960 ","End":"06:21.514","Text":"This might look unusual,"},{"Start":"06:21.514 ","End":"06:24.545","Text":"done a lot of this in the exercises previously."},{"Start":"06:24.545 ","End":"06:27.193","Text":"It accepts an object as a parameter,"},{"Start":"06:27.193 ","End":"06:29.270","Text":"and 1 of our own objects in this case,"},{"Start":"06:29.270 ","End":"06:35.910","Text":"and then uses the add method of the ArrayList pages to add in a page to the book."},{"Start":"06:35.910 ","End":"06:37.700","Text":"What we want to do here is,"},{"Start":"06:37.700 ","End":"06:41.000","Text":"first of all, obviously, we\u0027ve got to make it available."},{"Start":"06:41.000 ","End":"06:42.965","Text":"It doesn\u0027t return anything."},{"Start":"06:42.965 ","End":"06:45.125","Text":"There\u0027s an argument there, maybe for it to"},{"Start":"06:45.125 ","End":"06:48.530","Text":"return true or false depending on whether it\u0027s successful,"},{"Start":"06:48.530 ","End":"06:50.480","Text":"but we won\u0027t do that now."},{"Start":"06:50.480 ","End":"06:53.750","Text":"What we want to do is we want to add something of"},{"Start":"06:53.750 ","End":"06:59.165","Text":"type Page and then we\u0027ll give the parameter the name, Page."},{"Start":"06:59.165 ","End":"07:04.865","Text":"We\u0027ve got to been passed in a page and we want to add that to the ArrayList,"},{"Start":"07:04.865 ","End":"07:08.495","Text":"so we need to use pages.add,"},{"Start":"07:08.495 ","End":"07:11.750","Text":"and the thing passed in is called page,"},{"Start":"07:11.750 ","End":"07:13.310","Text":"so it\u0027s as simple as that."},{"Start":"07:13.310 ","End":"07:17.845","Text":"Whatever is passed in will be stored into this ArrayList."},{"Start":"07:17.845 ","End":"07:20.685","Text":"That is Part 3 done."},{"Start":"07:20.685 ","End":"07:28.030","Text":"Part 4 is going to involve us creating a method called readBook,"},{"Start":"07:28.030 ","End":"07:31.635","Text":"so a book is no use unless we can read it."},{"Start":"07:31.635 ","End":"07:34.080","Text":"No parameters specified,"},{"Start":"07:34.080 ","End":"07:36.565","Text":"so let\u0027s just do that then."},{"Start":"07:36.565 ","End":"07:39.360","Text":"What we have to do is, first of all,"},{"Start":"07:39.360 ","End":"07:44.280","Text":"output the title field and let\u0027s put a newline in front"},{"Start":"07:44.280 ","End":"07:49.765","Text":"of that as well just for good measure and a blank line maybe after it as well."},{"Start":"07:49.765 ","End":"07:51.430","Text":"That puts out the title."},{"Start":"07:51.430 ","End":"07:54.655","Text":"Now, what we need to do is we need to,"},{"Start":"07:54.655 ","End":"07:56.200","Text":"lowercase t there,"},{"Start":"07:56.200 ","End":"08:00.714","Text":"we need to create a loop which will loop through the collection,"},{"Start":"08:00.714 ","End":"08:03.030","Text":"giving us the page each time."},{"Start":"08:03.030 ","End":"08:07.030","Text":"We can just use a simple for-loop here,1 off simple for-loop,"},{"Start":"08:07.030 ","End":"08:11.980","Text":"but a for-loop that will use Page objects."},{"Start":"08:11.980 ","End":"08:14.435","Text":"That syntax looks like this."},{"Start":"08:14.435 ","End":"08:16.815","Text":"For each type of Page objects,"},{"Start":"08:16.815 ","End":"08:20.915","Text":"which we will call page in pages,"},{"Start":"08:20.915 ","End":"08:25.160","Text":"we want to just output that page."},{"Start":"08:25.160 ","End":"08:29.645","Text":"Because we overrode the toString method,"},{"Start":"08:29.645 ","End":"08:32.855","Text":"we should see if we just put the page in here,"},{"Start":"08:32.855 ","End":"08:34.250","Text":"the content of the page,"},{"Start":"08:34.250 ","End":"08:36.365","Text":"and the page number below it."},{"Start":"08:36.365 ","End":"08:38.465","Text":"It\u0027s as simple as that."},{"Start":"08:38.465 ","End":"08:41.445","Text":"That all seems to compile fine,"},{"Start":"08:41.445 ","End":"08:44.870","Text":"and we can now test as we\u0027ve been asked to do by going to"},{"Start":"08:44.870 ","End":"08:51.545","Text":"the object workbench and creating pages with the content in."},{"Start":"08:51.545 ","End":"08:55.069","Text":"Let\u0027s do that. New page"},{"Start":"08:55.069 ","End":"08:58.970","Text":"giving it the instance name for this object of Page 1 will accept that."},{"Start":"08:58.970 ","End":"09:01.880","Text":"Now, I need to put in the content,"},{"Start":"09:01.880 ","End":"09:04.540","Text":"so I\u0027m just going to copy and paste."},{"Start":"09:04.540 ","End":"09:09.330","Text":"Mind to save it being typed and that\u0027s Page 1,"},{"Start":"09:09.330 ","End":"09:13.805","Text":"so there\u0027s our first page created. Let\u0027s create another one."},{"Start":"09:13.805 ","End":"09:19.250","Text":"Again, I\u0027m going to copy and paste the content and that will be Page 2,"},{"Start":"09:19.250 ","End":"09:21.395","Text":"and then 1 final page."},{"Start":"09:21.395 ","End":"09:23.285","Text":"Not very long book this one,"},{"Start":"09:23.285 ","End":"09:25.751","Text":"but you get the idea."},{"Start":"09:25.751 ","End":"09:27.270","Text":"This will be Page 3."},{"Start":"09:27.270 ","End":"09:31.290","Text":"Let\u0027s try spacing there, Page 3."},{"Start":"09:31.290 ","End":"09:34.680","Text":"We\u0027ve got our pages."},{"Start":"09:34.680 ","End":"09:38.323","Text":"We\u0027ve created pages, but we haven\u0027t created a book yet,"},{"Start":"09:38.323 ","End":"09:42.990","Text":"and that\u0027s what we\u0027re going to do now in Part e. We\u0027re going to"},{"Start":"09:42.990 ","End":"09:48.135","Text":"create a book and we\u0027re going to give it the title, Treasure Island."},{"Start":"09:48.135 ","End":"09:49.625","Text":"There\u0027s our book now,"},{"Start":"09:49.625 ","End":"09:51.740","Text":"but this book is going to be empty."},{"Start":"09:51.740 ","End":"09:54.065","Text":"It doesn\u0027t have any pages in it yet,"},{"Start":"09:54.065 ","End":"09:59.300","Text":"so we need to add those pages using the method that we created called addPage."},{"Start":"09:59.300 ","End":"10:02.375","Text":"We just put a reference to the page here,"},{"Start":"10:02.375 ","End":"10:04.715","Text":"so our first page was called Page 1,"},{"Start":"10:04.715 ","End":"10:07.490","Text":"the name of this object over here, you can see."},{"Start":"10:07.490 ","End":"10:09.340","Text":"We\u0027ll add that page,"},{"Start":"10:09.340 ","End":"10:12.765","Text":"and then we\u0027ll add Page 2,"},{"Start":"10:12.765 ","End":"10:16.350","Text":"and then we\u0027ll add Page 3."},{"Start":"10:16.350 ","End":"10:21.635","Text":"We have now a book that consists of 3 pages."},{"Start":"10:21.635 ","End":"10:26.420","Text":"The last thing for us to do then is to run the readBook method"},{"Start":"10:26.420 ","End":"10:30.793","Text":"in here and see what output that produces."},{"Start":"10:30.793 ","End":"10:32.660","Text":"That seems to make sense."},{"Start":"10:32.660 ","End":"10:36.635","Text":"The title of the book is up here and then we\u0027ve got a newline afterwards."},{"Start":"10:36.635 ","End":"10:40.490","Text":"Then we\u0027ve got the first page and that was the text we had content,"},{"Start":"10:40.490 ","End":"10:42.245","Text":"and obviously it\u0027d be a lot more in a real book,"},{"Start":"10:42.245 ","End":"10:44.780","Text":"and then the page number, and a break,"},{"Start":"10:44.780 ","End":"10:49.170","Text":"the second page number found on page number."},{"Start":"10:49.170 ","End":"10:53.015","Text":"That makes sense and you can see how this"},{"Start":"10:53.015 ","End":"10:57.475","Text":"might actually work for an e-book type application."},{"Start":"10:57.475 ","End":"11:00.470","Text":"In Part h, the final question, we\u0027ve been asked,"},{"Start":"11:00.470 ","End":"11:05.240","Text":"what relationship is there between the book and the page?"},{"Start":"11:05.240 ","End":"11:10.820","Text":"Now, this arrow BlueJ always draws this arrow if 1 class is making use of another one."},{"Start":"11:10.820 ","End":"11:12.935","Text":"Really, in a UML diagram,"},{"Start":"11:12.935 ","End":"11:15.470","Text":"this would have a diamond on it."},{"Start":"11:15.470 ","End":"11:21.230","Text":"It\u0027s an aggregation because we\u0027re making up this book from these pages,"},{"Start":"11:21.230 ","End":"11:26.075","Text":"but it\u0027s actually a stronger form of aggregation called composition"},{"Start":"11:26.075 ","End":"11:32.290","Text":"because the book is composed of pages and they\u0027re not shared with any other objects."},{"Start":"11:32.290 ","End":"11:34.615","Text":"These pages belong to this book."},{"Start":"11:34.615 ","End":"11:37.630","Text":"I could create other pages and put them in another book,"},{"Start":"11:37.630 ","End":"11:40.390","Text":"but these particular pages belong to this book."},{"Start":"11:40.390 ","End":"11:46.650","Text":"It would be a diamond with the diamond shape filled in in solid."},{"Start":"11:46.650 ","End":"11:49.330","Text":"This is a good example of aggregation."},{"Start":"11:49.330 ","End":"11:52.120","Text":"This very simple example."},{"Start":"11:52.120 ","End":"11:56.230","Text":"The pages don\u0027t really have any meaning as individual pages."},{"Start":"11:56.230 ","End":"11:58.885","Text":"They\u0027re part of a book and"},{"Start":"11:58.885 ","End":"12:02.500","Text":"the structure we\u0027ve got here for our pages and books looks right."},{"Start":"12:02.500 ","End":"12:08.250","Text":"We could develop this further so that the content that\u0027s being stored here,"},{"Start":"12:08.250 ","End":"12:10.910","Text":"we could store some other content like an illustration,"},{"Start":"12:10.910 ","End":"12:15.035","Text":"for example, and that would be part of each individual page."},{"Start":"12:15.035 ","End":"12:17.040","Text":"The book method we could develop further,"},{"Start":"12:17.040 ","End":"12:18.470","Text":"so we\u0027ve got some more methods."},{"Start":"12:18.470 ","End":"12:21.800","Text":"For example, a method that tells us how many pages we\u0027ve got"},{"Start":"12:21.800 ","End":"12:26.180","Text":"or a method that reads from a particular page to the end of the book."},{"Start":"12:26.180 ","End":"12:33.215","Text":"It does show this very simple starting exercise how already design is important,"},{"Start":"12:33.215 ","End":"12:38.035","Text":"and if you get it right, it\u0027s much easier to write a well-written program."},{"Start":"12:38.035 ","End":"12:42.060","Text":"That\u0027s it for this one. Thanks for watching and see you soon."}],"ID":31190},{"Watched":false,"Name":"Exercise 2","Duration":"9m 28s","ChapterTopicVideoID":29609,"CourseChapterTopicPlaylistID":294565,"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.940","Text":"Hello, Welcome to this exercise in which we\u0027ve been asked to"},{"Start":"00:02.940 ","End":"00:06.000","Text":"remain in the existing project in Bluej and to"},{"Start":"00:06.000 ","End":"00:11.925","Text":"type a series of commands into the Code Pad and note the effect of those commands."},{"Start":"00:11.925 ","End":"00:15.900","Text":"Firstly, import java.time.*;"},{"Start":"00:15.900 ","End":"00:23.265","Text":"and LocalDate date1 equals LocalDate.of 2022,05,21."},{"Start":"00:23.265 ","End":"00:29.150","Text":"LocalDate date2 equals LocalDate.of 2022,05,21,"},{"Start":"00:29.150 ","End":"00:38.130","Text":"and LocalDate date3 equals LocalDate.of 2022,05,33."},{"Start":"00:38.130 ","End":"00:45.651","Text":"LocalDate date3 equals a LocalDate.of 2022,05,31."},{"Start":"00:45.651 ","End":"00:50.125","Text":"Date 4 is l. LocalDate.now,"},{"Start":"00:50.125 ","End":"00:55.123","Text":"we\u0027re asked to use date1.equals(date2),"},{"Start":"00:55.123 ","End":"01:01.470","Text":"date1.equals(date3) date1.getMonth."},{"Start":"01:01.470 ","End":"01:09.075","Text":"Data3.getDayOfMonth, date4.getDayOfWeek, date."},{"Start":"01:09.075 ","End":"01:12.855","Text":"3.isAfter(date1)."},{"Start":"01:12.855 ","End":"01:23.025","Text":"Date1.is Before(date4) Date1.is LeapYear, and date4.toString."},{"Start":"01:23.025 ","End":"01:28.670","Text":"In part b, we then asked which particular OO design principle,"},{"Start":"01:28.670 ","End":"01:32.665","Text":"the LocalDate class exhibits."},{"Start":"01:32.665 ","End":"01:35.630","Text":"I\u0027ve got a project open and I need the"},{"Start":"01:35.630 ","End":"01:38.555","Text":"project open because you can\u0027t use a Code Pad without it."},{"Start":"01:38.555 ","End":"01:43.025","Text":"I\u0027m going to proceed and start typing these commands in."},{"Start":"01:43.025 ","End":"01:47.390","Text":"Clearly we\u0027ve got to import a library first of all,"},{"Start":"01:47.390 ","End":"01:48.755","Text":"before we can do anything."},{"Start":"01:48.755 ","End":"01:53.510","Text":"This one\u0027s called java.time has led of classes inside it."},{"Start":"01:53.510 ","End":"01:57.335","Text":"I\u0027m going to make use of one of those classes in Part 2,"},{"Start":"01:57.335 ","End":"01:59.815","Text":"which is called a LocalDate."},{"Start":"01:59.815 ","End":"02:09.930","Text":"I create an object called date1 from LocalDate where I use it as I say, LocalDate.of."},{"Start":"02:09.950 ","End":"02:15.304","Text":"I put a date in the year first than a month,"},{"Start":"02:15.304 ","End":"02:17.660","Text":"and then a day within the month."},{"Start":"02:17.660 ","End":"02:21.505","Text":"That will create an object called date1."},{"Start":"02:21.505 ","End":"02:24.320","Text":"I\u0027m going to do the same thing again."},{"Start":"02:24.320 ","End":"02:27.065","Text":"I\u0027m going to call it date2 remember Up arrow"},{"Start":"02:27.065 ","End":"02:30.695","Text":"in the Code Pad gets me the previous command."},{"Start":"02:30.695 ","End":"02:33.080","Text":"That\u0027s the same date as previously,"},{"Start":"02:33.080 ","End":"02:35.930","Text":"so we will need that in a moment. Just accept that."},{"Start":"02:35.930 ","End":"02:39.215","Text":"I\u0027ve merely created 2 objects now,"},{"Start":"02:39.215 ","End":"02:41.625","Text":"date1 and date2 here\u0027s date3."},{"Start":"02:41.625 ","End":"02:44.670","Text":"Let\u0027s see what happens for this one."},{"Start":"02:44.670 ","End":"02:47.080","Text":"Now the day,"},{"Start":"02:47.080 ","End":"02:50.675","Text":"here\u0027s a slightly unusual one on purpose,"},{"Start":"02:50.675 ","End":"02:54.695","Text":"because we want to see that you cannot create"},{"Start":"02:54.695 ","End":"02:58.680","Text":"an object if the date doesn\u0027t look like a valid date."},{"Start":"02:58.680 ","End":"03:02.780","Text":"In this case, it\u0027s told us that the day of month"},{"Start":"03:02.780 ","End":"03:07.705","Text":"values are going to go from 1-28 or 1-31."},{"Start":"03:07.705 ","End":"03:11.715","Text":"Obviously 33 is outside that range."},{"Start":"03:11.715 ","End":"03:16.130","Text":"Internally this object does some validation for us."},{"Start":"03:16.130 ","End":"03:20.315","Text":"If it cannot create an object of that type with these values,"},{"Start":"03:20.315 ","End":"03:23.255","Text":"it won\u0027t create it and it\u0027ll throw an exception."},{"Start":"03:23.255 ","End":"03:24.980","Text":"Really in our application,"},{"Start":"03:24.980 ","End":"03:29.420","Text":"we should have to try and catch block to hide"},{"Start":"03:29.420 ","End":"03:34.400","Text":"that error message and get the user to type in a valid date."},{"Start":"03:34.400 ","End":"03:36.905","Text":"That was Part 4."},{"Start":"03:36.905 ","End":"03:40.880","Text":"Part 5 is to create yet another date object."},{"Start":"03:40.880 ","End":"03:44.130","Text":"This one\u0027s called date4 this time."},{"Start":"03:44.130 ","End":"03:46.345","Text":"Actually still date3,"},{"Start":"03:46.345 ","End":"03:50.390","Text":"but this time with a legitimate date of"},{"Start":"03:50.390 ","End":"03:55.955","Text":"31 because there are 31 days in May and it\u0027s not complained about that."},{"Start":"03:55.955 ","End":"04:00.290","Text":"The next due date4 this one is different."},{"Start":"04:00.290 ","End":"04:04.430","Text":"This is actually going to create from the now method."},{"Start":"04:04.430 ","End":"04:09.050","Text":"It\u0027s going to create a date object that contains today\u0027s date."},{"Start":"04:09.050 ","End":"04:13.245","Text":"Date4 will contain today\u0027s date."},{"Start":"04:13.245 ","End":"04:17.975","Text":"Let\u0027s see what happens when we run some of the other methods here."},{"Start":"04:17.975 ","End":"04:27.170","Text":"Number 7, we\u0027ve been asked to compare date1 to date2 using the equals method,"},{"Start":"04:27.170 ","End":"04:30.470","Text":"which works just the same way as it does for most objects,"},{"Start":"04:30.470 ","End":"04:32.490","Text":"compares the 2 dates."},{"Start":"04:32.490 ","End":"04:35.935","Text":"Date1 equals date2,"},{"Start":"04:35.935 ","End":"04:40.550","Text":"and we get true returned because those 2 days are obviously the same,"},{"Start":"04:40.550 ","End":"04:43.400","Text":"the 21st of May 2022."},{"Start":"04:43.400 ","End":"04:46.190","Text":"That\u0027s unsurprising and similarly,"},{"Start":"04:46.190 ","End":"04:50.270","Text":"it should be unsurprising when we run this one that we get"},{"Start":"04:50.270 ","End":"04:55.520","Text":"false because this date is not equal to that date."},{"Start":"04:55.520 ","End":"04:58.145","Text":"So far so good."},{"Start":"04:58.145 ","End":"05:06.020","Text":"Then Number 10 there is to get the day of month from Date3. Let\u0027s do that."},{"Start":"05:06.020 ","End":"05:10.030","Text":"Day, month."},{"Start":"05:10.030 ","End":"05:11.884","Text":"That returns 31,"},{"Start":"05:11.884 ","End":"05:14.630","Text":"an integer, which is the 31st day of the month."},{"Start":"05:14.630 ","End":"05:17.030","Text":"If that date was the 31st of May."},{"Start":"05:17.030 ","End":"05:21.275","Text":"What about day of week one does so it\u0027s slightly different."},{"Start":"05:21.275 ","End":"05:25.505","Text":"It returns a enumeration, which is Tuesday,"},{"Start":"05:25.505 ","End":"05:29.840","Text":"because each of the days of the week in this library are given effectively"},{"Start":"05:29.840 ","End":"05:35.690","Text":"an integer number in sequence starting from Monday and ending on Sunday."},{"Start":"05:35.690 ","End":"05:37.700","Text":"Because that\u0027s defined in a library,"},{"Start":"05:37.700 ","End":"05:39.484","Text":"it comes up as Tuesday,"},{"Start":"05:39.484 ","End":"05:41.360","Text":"but there\u0027ll be a number associated with it,"},{"Start":"05:41.360 ","End":"05:43.195","Text":"probably Number 2,"},{"Start":"05:43.195 ","End":"05:44.975","Text":"given it\u0027s the second day of the week,"},{"Start":"05:44.975 ","End":"05:46.610","Text":"if you start from Monday."},{"Start":"05:46.610 ","End":"05:50.090","Text":"Then let\u0027s try Number 12,"},{"Start":"05:50.090 ","End":"05:58.595","Text":"which is date3 is after date4 what we\u0027re checking here is to see to compare the 2 dates."},{"Start":"05:58.595 ","End":"06:04.595","Text":"Because the 31st is after today,"},{"Start":"06:04.595 ","End":"06:06.815","Text":"I\u0027m doing this on the 22nd,"},{"Start":"06:06.815 ","End":"06:15.780","Text":"that today\u0027s date it has returned true because the 31st is after the 22nd."},{"Start":"06:15.780 ","End":"06:24.785","Text":"The next one is before date1 is before date4 that before today\u0027s date."},{"Start":"06:24.785 ","End":"06:27.020","Text":"Date1 is the 21st,"},{"Start":"06:27.020 ","End":"06:32.375","Text":"so yes is before today\u0027s date, a couple of days ago."},{"Start":"06:32.375 ","End":"06:35.600","Text":"What about date1.isLeapYear?"},{"Start":"06:35.600 ","End":"06:43.580","Text":"That returns false because 2022 is not a leap year, 2020 was."},{"Start":"06:43.580 ","End":"06:48.440","Text":"Finally, date4.toString,"},{"Start":"06:48.440 ","End":"06:52.550","Text":"you will see returns today\u0027s date,"},{"Start":"06:52.550 ","End":"06:55.470","Text":"which is the 22nd."},{"Start":"06:55.470 ","End":"07:01.400","Text":"All of those methods very useful will come from the LocalDate object."},{"Start":"07:01.400 ","End":"07:04.430","Text":"We\u0027ve been asked to in part b,"},{"Start":"07:04.430 ","End":"07:07.205","Text":"which particular design principle,"},{"Start":"07:07.205 ","End":"07:10.160","Text":"LocalDate as a class exhibits?"},{"Start":"07:10.160 ","End":"07:15.540","Text":"It\u0027s a really good example of encapsulation done very well."},{"Start":"07:15.540 ","End":"07:19.730","Text":"All of the date and time classes are good examples of encapsulation."},{"Start":"07:19.730 ","End":"07:24.485","Text":"We don\u0027t know how this date is actually stored internally,"},{"Start":"07:24.485 ","End":"07:27.125","Text":"inside LocalDate, and actually we don\u0027t care."},{"Start":"07:27.125 ","End":"07:32.716","Text":"We\u0027ve also got direct access to this value wherever it\u0027s stored"},{"Start":"07:32.716 ","End":"07:38.705","Text":"toString is a method that\u0027s provided to us that returns the value as a string."},{"Start":"07:38.705 ","End":"07:41.555","Text":"Same for day of week or day of month."},{"Start":"07:41.555 ","End":"07:45.590","Text":"But they\u0027re all selected methods that have been provided to us."},{"Start":"07:45.590 ","End":"07:50.060","Text":"They\u0027re the ones that we gain access to those internals from."},{"Start":"07:50.060 ","End":"07:55.460","Text":"But it\u0027s completely consistent with the principle of encapsulation."},{"Start":"07:55.460 ","End":"08:00.200","Text":"There\u0027s actually a lot of complexity behind these classes that\u0027s being hidden from us."},{"Start":"08:00.200 ","End":"08:02.240","Text":"For example, the is leap year."},{"Start":"08:02.240 ","End":"08:06.605","Text":"It\u0027s actually quite complex to work out when it\u0027s a leap year,"},{"Start":"08:06.605 ","End":"08:09.060","Text":"every 4 years, but actually,"},{"Start":"08:09.060 ","End":"08:12.380","Text":"it\u0027s going to be on a boundary of every 400 years as well."},{"Start":"08:12.380 ","End":"08:14.368","Text":"I don\u0027t know if you\u0027re aware of that."},{"Start":"08:14.368 ","End":"08:16.640","Text":"There\u0027s not a straightforward as it looks."},{"Start":"08:16.640 ","End":"08:19.910","Text":"Similarly things like is before and here\u0027s after It\u0027s actually quite"},{"Start":"08:19.910 ","End":"08:24.380","Text":"complex to work out how this works because it could straddle a month and so on."},{"Start":"08:24.380 ","End":"08:27.110","Text":"All of that complexity hidden away from you,"},{"Start":"08:27.110 ","End":"08:29.255","Text":"you don\u0027t really care how it\u0027s implemented."},{"Start":"08:29.255 ","End":"08:31.355","Text":"It either works or it doesn\u0027t work."},{"Start":"08:31.355 ","End":"08:34.100","Text":"We just need to know what formats the answer in."},{"Start":"08:34.100 ","End":"08:35.930","Text":"If it fits, for example,"},{"Start":"08:35.930 ","End":"08:37.520","Text":"is leap year the format,"},{"Start":"08:37.520 ","End":"08:42.170","Text":"we return a Boolean and we expect it to be true if it\u0027s a leap year,"},{"Start":"08:42.170 ","End":"08:45.305","Text":"false if it\u0027s not, that\u0027s all we care about."},{"Start":"08:45.305 ","End":"08:48.170","Text":"Designing your classes so they hide"},{"Start":"08:48.170 ","End":"08:51.560","Text":"the implementation details and provide us a clear interface."},{"Start":"08:51.560 ","End":"08:55.955","Text":"That\u0027s a key aspect of good design in object-oriented,"},{"Start":"08:55.955 ","End":"08:59.180","Text":"but getting it right is not very easy."},{"Start":"08:59.180 ","End":"09:01.850","Text":"In fact, the developers of Java themselves have had"},{"Start":"09:01.850 ","End":"09:06.050","Text":"several goals at getting time and date done well."},{"Start":"09:06.050 ","End":"09:10.400","Text":"They\u0027ve had to have several goes over the last 20 years to get that just right."},{"Start":"09:10.400 ","End":"09:15.950","Text":"It\u0027s not just beginners that struggle is actually tricky thing to design classes well,"},{"Start":"09:15.950 ","End":"09:19.730","Text":"so that encapsulation provides you with all the features that you"},{"Start":"09:19.730 ","End":"09:24.295","Text":"need and it\u0027s easy to understand and it\u0027s a nice clear interface."},{"Start":"09:24.295 ","End":"09:26.255","Text":"That\u0027s it for this one. Thanks for watching."},{"Start":"09:26.255 ","End":"09:28.680","Text":"See you shortly for the next one."}],"ID":31191},{"Watched":false,"Name":"Exercise 3 Part 1","Duration":"7m 30s","ChapterTopicVideoID":29610,"CourseChapterTopicPlaylistID":294565,"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.585","Text":"Hi everyone and welcome to this exercise in which we\u0027ve been asked to create"},{"Start":"00:03.585 ","End":"00:06.885","Text":"a new project called Hotels and 2 classes"},{"Start":"00:06.885 ","End":"00:09.015","Text":"within it as follows and we\u0027re given"},{"Start":"00:09.015 ","End":"00:13.980","Text":"a UML diagram with 2 classes and a relationship between them."},{"Start":"00:13.980 ","End":"00:18.179","Text":"In part a, we\u0027re told that a property contains rooms,"},{"Start":"00:18.179 ","End":"00:23.985","Text":"each of which has a room number and an indication of how many beds the room has."},{"Start":"00:23.985 ","End":"00:30.030","Text":"We add attributes and the constructor for Room to set the 2 attributes."},{"Start":"00:30.030 ","End":"00:32.150","Text":"In part b, we are told after adding"},{"Start":"00:32.150 ","End":"00:35.360","Text":"the selector methods to override the toString method for"},{"Start":"00:35.360 ","End":"00:40.775","Text":"Room to return a string that concatenates the room number and number of beds."},{"Start":"00:40.775 ","End":"00:44.515","Text":"For example, Room 11 has 1 bed."},{"Start":"00:44.515 ","End":"00:48.530","Text":"In part c, we\u0027re told that the Property class should use"},{"Start":"00:48.530 ","End":"00:53.570","Text":"an Array List collection called rooms to store its rooms."},{"Start":"00:53.570 ","End":"00:57.260","Text":"In part d, we\u0027re told that rooms are added to objects"},{"Start":"00:57.260 ","End":"01:01.295","Text":"created from the Property class using the addRoom method,"},{"Start":"01:01.295 ","End":"01:05.360","Text":"which creates an object from the Room class with the first argument"},{"Start":"01:05.360 ","End":"01:10.175","Text":"used to set the room number and the second to set the number of beds."},{"Start":"01:10.175 ","End":"01:17.125","Text":"In part 2, we\u0027re told it adds the created room to the collection called rooms,"},{"Start":"01:17.125 ","End":"01:21.035","Text":"and in part 3, we\u0027re told that addRoom updates the numberOfRooms"},{"Start":"01:21.035 ","End":"01:26.515","Text":"attribute to correctly reflect the number of rooms at the property."},{"Start":"01:26.515 ","End":"01:30.905","Text":"In part e, we\u0027re told that after adding the selector methods,"},{"Start":"01:30.905 ","End":"01:35.405","Text":"we should override the toString method for this class to return"},{"Start":"01:35.405 ","End":"01:40.820","Text":"a string that concatenates the property name and number of rooms in brackets."},{"Start":"01:40.820 ","End":"01:45.520","Text":"For example, Regent Hotel, 5 rooms."},{"Start":"01:46.030 ","End":"01:49.895","Text":"Let\u0027s start by creating a Room."},{"Start":"01:49.895 ","End":"01:52.745","Text":"The room has 2 attributes."},{"Start":"01:52.745 ","End":"01:55.460","Text":"One is called roomNumber,"},{"Start":"01:55.460 ","End":"01:58.670","Text":"and that\u0027s an int, as is."},{"Start":"01:58.670 ","End":"02:03.320","Text":"The other attribute, which is called beds,"},{"Start":"02:03.320 ","End":"02:06.935","Text":"which stores the number of beds this room has."},{"Start":"02:06.935 ","End":"02:12.275","Text":"Then the constructor is used to set those 2 attributes."},{"Start":"02:12.275 ","End":"02:14.725","Text":"So let\u0027s do that."},{"Start":"02:14.725 ","End":"02:17.930","Text":"That\u0027s done, and then we\u0027ve got"},{"Start":"02:17.930 ","End":"02:22.690","Text":"the selector methods to right which just return those 2 attributes."},{"Start":"02:22.690 ","End":"02:26.825","Text":"So the final thing to do then is to override"},{"Start":"02:26.825 ","End":"02:31.310","Text":"the toString method as we\u0027ve been asked in part b to do,"},{"Start":"02:31.310 ","End":"02:35.330","Text":"which is going to concatenate the room number and the number of beds."},{"Start":"02:35.330 ","End":"02:41.270","Text":"Let\u0027s put an annotation in there and write our method."},{"Start":"02:41.270 ","End":"02:45.740","Text":"It\u0027s going to put room and then a space,"},{"Start":"02:45.740 ","End":"02:50.515","Text":"capital, and then the room number, there we go."},{"Start":"02:50.515 ","End":"02:58.270","Text":"That is the Room class done and we move on to the Property class in"},{"Start":"02:58.270 ","End":"03:06.795","Text":"part c. What that has is an Array List which is used to store rooms."},{"Start":"03:06.795 ","End":"03:11.655","Text":"So a property contains rooms."},{"Start":"03:11.655 ","End":"03:15.395","Text":"We\u0027re going to start by putting an Array List,"},{"Start":"03:15.395 ","End":"03:17.125","Text":"import int at the top."},{"Start":"03:17.125 ","End":"03:19.195","Text":"Otherwise we can\u0027t have an Array List,"},{"Start":"03:19.195 ","End":"03:24.170","Text":"and then I can put in my Array List and it\u0027s of"},{"Start":"03:24.170 ","End":"03:31.025","Text":"type Room because it\u0027s going to be storing rooms and we\u0027re going to call it rooms."},{"Start":"03:31.025 ","End":"03:32.855","Text":"That will do the trick,"},{"Start":"03:32.855 ","End":"03:37.220","Text":"and we\u0027ll send it to have the other attribute,"},{"Start":"03:37.220 ","End":"03:41.420","Text":"which is the name of the property and"},{"Start":"03:41.420 ","End":"03:45.560","Text":"we\u0027re also going to keep track of how many rooms we have in the properties."},{"Start":"03:45.560 ","End":"03:47.725","Text":"It starts off with no rooms."},{"Start":"03:47.725 ","End":"03:54.755","Text":"We need to keep track of how many it has at any one time and that\u0027s attributes done."},{"Start":"03:54.755 ","End":"03:59.495","Text":"In part d, we\u0027re told that rooms are"},{"Start":"03:59.495 ","End":"04:04.970","Text":"added to objects created from the Property class using the addRoom method."},{"Start":"04:04.970 ","End":"04:07.775","Text":"We need to implement this addRoom method."},{"Start":"04:07.775 ","End":"04:12.934","Text":"addRoom will take 2 arguments;"},{"Start":"04:12.934 ","End":"04:19.775","Text":"an integer that says the room number and another one says the number of beds."},{"Start":"04:19.775 ","End":"04:26.840","Text":"From it, we\u0027re going to create a Room object of type room,"},{"Start":"04:26.840 ","End":"04:29.450","Text":"and we\u0027ll call it room,"},{"Start":"04:29.450 ","End":"04:31.595","Text":"and to do that,"},{"Start":"04:31.595 ","End":"04:34.370","Text":"we need to create a new room and pass to"},{"Start":"04:34.370 ","End":"04:38.398","Text":"it the attributes that the constructor for a room will need,"},{"Start":"04:38.398 ","End":"04:41.795","Text":"that\u0027s what\u0027s the room number and how many beds does it have."},{"Start":"04:41.795 ","End":"04:45.560","Text":"Now we have a reference to that object stored in room."},{"Start":"04:45.560 ","End":"04:50.830","Text":"We can add that to the collection called Rooms."},{"Start":"04:50.830 ","End":"04:52.385","Text":"That\u0027s easy enough to do."},{"Start":"04:52.385 ","End":"04:55.430","Text":"We just say rooms.add,"},{"Start":"04:55.430 ","End":"04:58.085","Text":"and there\u0027s our reference there, room."},{"Start":"04:58.085 ","End":"04:59.735","Text":"It\u0027s as simple as that."},{"Start":"04:59.735 ","End":"05:06.080","Text":"That\u0027s D2 done and D3 would be straightforward as well."},{"Start":"05:06.080 ","End":"05:07.880","Text":"We can do it 2 ways."},{"Start":"05:07.880 ","End":"05:11.570","Text":"We can either just say rooms plus,"},{"Start":"05:11.570 ","End":"05:13.745","Text":"plus, that\u0027d be one way of doing it."},{"Start":"05:13.745 ","End":"05:16.280","Text":"Probably the easiest and the right way to do it."},{"Start":"05:16.280 ","End":"05:21.560","Text":"We could also use the size method built into Array Lists,"},{"Start":"05:21.560 ","End":"05:24.510","Text":"which would return us an integer that"},{"Start":"05:24.510 ","End":"05:27.710","Text":"tells us the size of that Array List and obviously,"},{"Start":"05:27.710 ","End":"05:29.250","Text":"that\u0027s the number of rooms we have as well,"},{"Start":"05:29.250 ","End":"05:32.495","Text":"so we can put that into number of rooms as well."},{"Start":"05:32.495 ","End":"05:34.565","Text":"But I\u0027m just going to leave it as it is."},{"Start":"05:34.565 ","End":"05:37.130","Text":"There\u0027s our addRoom method done,"},{"Start":"05:37.130 ","End":"05:38.960","Text":"and it seems to be happy enough."},{"Start":"05:38.960 ","End":"05:42.890","Text":"I need to say what it returns and haven\u0027t been told anything,"},{"Start":"05:42.890 ","End":"05:45.380","Text":"probably a good thing to have done is"},{"Start":"05:45.380 ","End":"05:48.290","Text":"to return a Boolean to say whether it\u0027s successful or not,"},{"Start":"05:48.290 ","End":"05:49.760","Text":"but I\u0027m just going to leave it as void."},{"Start":"05:49.760 ","End":"05:55.010","Text":"Let\u0027s now implement the constructor and the selector methods."},{"Start":"05:55.010 ","End":"05:57.350","Text":"I should have done the constructor first really."},{"Start":"05:57.350 ","End":"06:00.875","Text":"All it needs for the constructor is to set"},{"Start":"06:00.875 ","End":"06:06.810","Text":"its name and now I\u0027m going to add my selector methods."},{"Start":"06:07.700 ","End":"06:16.225","Text":"Final thing to do is to add the override to the toString method."},{"Start":"06:16.225 ","End":"06:19.240","Text":"I put my annotation in, good practice to do that."},{"Start":"06:19.240 ","End":"06:21.475","Text":"Then the definition itself,"},{"Start":"06:21.475 ","End":"06:23.755","Text":"as far as public,"},{"Start":"06:23.755 ","End":"06:25.555","Text":"it returns a string."},{"Start":"06:25.555 ","End":"06:29.035","Text":"It\u0027s called toString, no parameters,"},{"Start":"06:29.035 ","End":"06:32.870","Text":"and then return the value,"},{"Start":"06:32.870 ","End":"06:36.910","Text":"and it\u0027s told me to concatenate the property name."},{"Start":"06:36.910 ","End":"06:41.215","Text":"That\u0027s the name and then the number of rooms in brackets."},{"Start":"06:41.215 ","End":"06:44.380","Text":"I\u0027m going to put a space and then an opening"},{"Start":"06:44.380 ","End":"06:49.120","Text":"bracket and then I\u0027m going to put a numberOfRooms."},{"Start":"06:49.120 ","End":"06:51.965","Text":"Then I\u0027m going to close the bracket."},{"Start":"06:51.965 ","End":"06:55.675","Text":"I put the word rooms in front to it as well."},{"Start":"06:55.675 ","End":"06:59.870","Text":"I think I\u0027ll just assume that it\u0027s always going to have more than one room."},{"Start":"06:59.870 ","End":"07:06.035","Text":"That\u0027s that method done."},{"Start":"07:06.035 ","End":"07:08.840","Text":"I\u0027m going to close my bracket there, so it\u0027s complaining."},{"Start":"07:08.840 ","End":"07:13.520","Text":"But that seems to have completed"},{"Start":"07:13.520 ","End":"07:21.765","Text":"the selector methods for property and the addRoom method as well."},{"Start":"07:21.765 ","End":"07:25.280","Text":"I\u0027ll pause there and we\u0027ll come back shortly and test"},{"Start":"07:25.280 ","End":"07:28.730","Text":"all of this by putting it together in another class."},{"Start":"07:28.730 ","End":"07:31.230","Text":"Thanks for watching, I\u0027ll see you shortly."}],"ID":31192},{"Watched":false,"Name":"Exercise 3 Part 2","Duration":"11m 11s","ChapterTopicVideoID":29611,"CourseChapterTopicPlaylistID":294565,"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.660","Text":"Hello. Welcome back to this next part of the exercise in which"},{"Start":"00:03.660 ","End":"00:07.515","Text":"we\u0027ve been told to create another class called HotelsApp,"},{"Start":"00:07.515 ","End":"00:11.550","Text":"which we\u0027ll use to test our room and property classes."},{"Start":"00:11.550 ","End":"00:16.635","Text":"In Part g, we\u0027re told inside the class to declare a HashMap called properties,"},{"Start":"00:16.635 ","End":"00:21.795","Text":"which has a string key and is used to store property objects."},{"Start":"00:21.795 ","End":"00:28.165","Text":"In Part h, we\u0027re told within the HotelsApp class to create a method called start,"},{"Start":"00:28.165 ","End":"00:31.340","Text":"which will be used to start our application."},{"Start":"00:31.340 ","End":"00:38.250","Text":"Inside start, we create an instance of a property with a name of Regent Hotel,"},{"Start":"00:38.250 ","End":"00:43.360","Text":"and we store a reference to the object in a variable called hotel."},{"Start":"00:43.360 ","End":"00:45.405","Text":"In Part i,"},{"Start":"00:45.405 ","End":"00:48.560","Text":"we\u0027re asked using the addRoom method to add"},{"Start":"00:48.560 ","End":"00:52.435","Text":"rooms to the object referenced by hotel as follows."},{"Start":"00:52.435 ","End":"00:54.990","Text":"Part 1, Room 1 with 1 bed,"},{"Start":"00:54.990 ","End":"00:57.585","Text":"Part 2, Room 2 with 1 bed,"},{"Start":"00:57.585 ","End":"01:01.920","Text":"Part 3 Room 3 with 1 bed, Part 4,"},{"Start":"01:01.920 ","End":"01:04.080","Text":"Room 11 with 2 beds,"},{"Start":"01:04.080 ","End":"01:05.400","Text":"and Part 5,"},{"Start":"01:05.400 ","End":"01:08.575","Text":"room 12 with 2 beds."},{"Start":"01:08.575 ","End":"01:13.490","Text":"In Part j, we\u0027re asked to add the hotel object we just created to"},{"Start":"01:13.490 ","End":"01:20.755","Text":"the properties collection using put with a key being the name of the hotel."},{"Start":"01:20.755 ","End":"01:25.415","Text":"In Part k, we\u0027re asked to create a new instance of a property,"},{"Start":"01:25.415 ","End":"01:28.490","Text":"this time with a name of Crummy Inn and"},{"Start":"01:28.490 ","End":"01:31.790","Text":"store a reference to this new object in the variable hotel,"},{"Start":"01:31.790 ","End":"01:33.470","Text":"which has already been declared."},{"Start":"01:33.470 ","End":"01:39.765","Text":"Now again, add rooms to the object referenced by hotel as follows; Part 1,"},{"Start":"01:39.765 ","End":"01:42.060","Text":"Room 1 with 1 bed, Part 2,"},{"Start":"01:42.060 ","End":"01:43.740","Text":"Room 2 with 2 beds,"},{"Start":"01:43.740 ","End":"01:46.785","Text":"Part 3, Room 3 with 3 beds."},{"Start":"01:46.785 ","End":"01:49.220","Text":"Then in Part l we\u0027re again,"},{"Start":"01:49.220 ","End":"01:56.015","Text":"asked to add the hotel object we just created to the properties collection using put,"},{"Start":"01:56.015 ","End":"01:59.945","Text":"with the key being the name of the hotel."},{"Start":"01:59.945 ","End":"02:03.800","Text":"In Part m, we test by adding a line to print"},{"Start":"02:03.800 ","End":"02:09.505","Text":"properties and run the start method on the object workbench."},{"Start":"02:09.505 ","End":"02:11.365","Text":"In Part n,"},{"Start":"02:11.365 ","End":"02:16.675","Text":"we\u0027re asked to explain the output we see generated."},{"Start":"02:16.675 ","End":"02:19.690","Text":"We\u0027ve created HotelsApp,"},{"Start":"02:19.690 ","End":"02:25.685","Text":"the new class that we\u0027ve been asked to do in Part f. In Part g,"},{"Start":"02:25.685 ","End":"02:30.635","Text":"we\u0027ve all been asked to declare a HashMap called properties,"},{"Start":"02:30.635 ","End":"02:34.490","Text":"which has a string key and is used to store property objects."},{"Start":"02:34.490 ","End":"02:40.980","Text":"For HashMaps, we have the type as HashMap."},{"Start":"02:40.980 ","End":"02:43.430","Text":"Then within the angle brackets here,"},{"Start":"02:43.430 ","End":"02:47.675","Text":"we say what the type is of the key,"},{"Start":"02:47.675 ","End":"02:51.320","Text":"which we have just been told is a string."},{"Start":"02:51.320 ","End":"02:56.585","Text":"Then you have the type of the thing that you\u0027re storing,"},{"Start":"02:56.585 ","End":"03:00.410","Text":"which in this case is going to be a property."},{"Start":"03:00.410 ","End":"03:06.290","Text":"Then we\u0027ve been asked to give it the name properties so that follows,"},{"Start":"03:06.290 ","End":"03:10.075","Text":"the identifier for this is properties."},{"Start":"03:10.075 ","End":"03:14.810","Text":"Then we create it. That will do the job."},{"Start":"03:14.810 ","End":"03:19.640","Text":"We obviously have to add the HashMap by importing"},{"Start":"03:19.640 ","End":"03:25.820","Text":"it and that will make any errors once I add my g here, go away."},{"Start":"03:25.820 ","End":"03:30.545","Text":"We\u0027ve created the HashMap"},{"Start":"03:30.545 ","End":"03:36.950","Text":"called properties that stores a property object or a series of property objects."},{"Start":"03:36.950 ","End":"03:41.520","Text":"We can locate those using a string key."},{"Start":"03:41.520 ","End":"03:45.530","Text":"We\u0027ll see how that works as we progress through the exercise."},{"Start":"03:45.530 ","End":"03:49.520","Text":"Now in Part h,"},{"Start":"03:49.520 ","End":"03:52.825","Text":"we create a method called start."},{"Start":"03:52.825 ","End":"04:00.435","Text":"Not told if it returns anything so we assume it\u0027s void, no parameters."},{"Start":"04:00.435 ","End":"04:03.860","Text":"This is where we\u0027re going to actually locate our code,"},{"Start":"04:03.860 ","End":"04:06.995","Text":"start building this application and test it initially."},{"Start":"04:06.995 ","End":"04:12.695","Text":"We\u0027re going to create now an instance of a property with a name, region,"},{"Start":"04:12.695 ","End":"04:15.680","Text":"hotel, and we\u0027re going to store a reference to that object"},{"Start":"04:15.680 ","End":"04:19.580","Text":"that\u0027s created in a variable called Hotel."},{"Start":"04:19.580 ","End":"04:26.240","Text":"It\u0027s a property object that we\u0027re creating of 1 of the property class."},{"Start":"04:26.240 ","End":"04:32.225","Text":"We\u0027re going to store a reference to it in this variable called hotel."},{"Start":"04:32.225 ","End":"04:37.275","Text":"As ever, we need to create it using a new keyword and"},{"Start":"04:37.275 ","End":"04:43.875","Text":"supply it with what arguments are needed."},{"Start":"04:43.875 ","End":"04:49.325","Text":"In this case, we\u0027ll just need a single argument which is the name of the hotel."},{"Start":"04:49.325 ","End":"04:52.490","Text":"We\u0027ve been told region hotel,"},{"Start":"04:52.490 ","End":"04:54.530","Text":"that\u0027s Part h done."},{"Start":"04:54.530 ","End":"04:56.090","Text":"Now Part I,"},{"Start":"04:56.090 ","End":"05:01.125","Text":"we\u0027re asked to use the addRoom method to add rooms to the property."},{"Start":"05:01.125 ","End":"05:04.905","Text":"We\u0027ve obviously stored a reference to the property in hotel."},{"Start":"05:04.905 ","End":"05:08.715","Text":"That\u0027s simple enough, we just say hotel.addRoom,"},{"Start":"05:08.715 ","End":"05:13.974","Text":"that\u0027s the method we created inside property class."},{"Start":"05:13.974 ","End":"05:15.680","Text":"In order to add a room,"},{"Start":"05:15.680 ","End":"05:17.749","Text":"we have to pass in the details."},{"Start":"05:17.749 ","End":"05:21.995","Text":"So what\u0027s the room number and how many beds does it have."},{"Start":"05:21.995 ","End":"05:26.231","Text":"We\u0027ll just work our way through Part 1,"},{"Start":"05:26.231 ","End":"05:27.620","Text":"2, 3, 4,"},{"Start":"05:27.620 ","End":"05:30.285","Text":"and 5 of Part I here."},{"Start":"05:30.285 ","End":"05:32.025","Text":"I\u0027ll just copy those."},{"Start":"05:32.025 ","End":"05:36.615","Text":"Room 2 has 1 bed,"},{"Start":"05:36.615 ","End":"05:39.945","Text":"Room 3 also has 1 bed."},{"Start":"05:39.945 ","End":"05:45.210","Text":"Room 11, which presumably is on the first floor,"},{"Start":"05:45.210 ","End":"05:48.020","Text":"it\u0027s conventionally used in hotel rooms."},{"Start":"05:48.020 ","End":"05:53.780","Text":"Room 12, which is the second room on the first floor has also 2 beds."},{"Start":"05:53.780 ","End":"05:57.695","Text":"That\u0027s added rooms to"},{"Start":"05:57.695 ","End":"06:03.560","Text":"this particular property which we\u0027ve stored reference to it in hotel."},{"Start":"06:03.560 ","End":"06:08.660","Text":"Our new property called Regent Hotel has 5 rooms,"},{"Start":"06:08.660 ","End":"06:10.723","Text":"and the rooms are numbered 1,"},{"Start":"06:10.723 ","End":"06:12.475","Text":"2, 3, 11, and 12."},{"Start":"06:12.475 ","End":"06:14.490","Text":"These 3 rooms have 1 bed,"},{"Start":"06:14.490 ","End":"06:16.875","Text":"these 2 rooms have 2 beds."},{"Start":"06:16.875 ","End":"06:19.890","Text":"We\u0027re going to add that now in Part j."},{"Start":"06:19.890 ","End":"06:24.965","Text":"We\u0027ve been asked to add it to the properties collection that we"},{"Start":"06:24.965 ","End":"06:31.890","Text":"defined up here using it\u0027s put to method so we say properties.put."},{"Start":"06:32.780 ","End":"06:37.054","Text":"When we put something into HashMap,"},{"Start":"06:37.054 ","End":"06:39.140","Text":"we have to supply a key."},{"Start":"06:39.140 ","End":"06:42.140","Text":"The thing we want to put it in the HashMap is"},{"Start":"06:42.140 ","End":"06:45.180","Text":"the hotel object that we\u0027ve just created and"},{"Start":"06:45.180 ","End":"06:51.185","Text":"stored a reference to in hotel but the key is actually the name of the hotel."},{"Start":"06:51.185 ","End":"06:56.300","Text":"We\u0027ve got access to the name of the hotel because we\u0027ve provided a method"},{"Start":"06:56.300 ","End":"06:59.210","Text":"called getName and that"},{"Start":"06:59.210 ","End":"07:02.300","Text":"will return us the name of the hotel or I could just type in again,"},{"Start":"07:02.300 ","End":"07:05.555","Text":"region hotel, but this is probably a better way of doing it."},{"Start":"07:05.555 ","End":"07:10.235","Text":"The object itself that I created already contains this text in it."},{"Start":"07:10.235 ","End":"07:12.260","Text":"If I put hotel.getName,"},{"Start":"07:12.260 ","End":"07:13.490","Text":"it gets me the name,"},{"Start":"07:13.490 ","End":"07:20.360","Text":"and so I can locate my hotel using its string name and I\u0027ll get"},{"Start":"07:20.360 ","End":"07:27.485","Text":"returned a hotel object or property object that I can then do something with."},{"Start":"07:27.485 ","End":"07:34.610","Text":"That\u0027s Part j done and I\u0027m going to do the same thing again for another hotel."},{"Start":"07:34.610 ","End":"07:36.260","Text":"Just to test how this works,"},{"Start":"07:36.260 ","End":"07:38.000","Text":"we need at least 2 hotels."},{"Start":"07:38.000 ","End":"07:43.710","Text":"I can reuse the reference here called Hotel and just"},{"Start":"07:43.710 ","End":"07:50.315","Text":"create a new hotel and store a new reference in hotel."},{"Start":"07:50.315 ","End":"07:55.405","Text":"New property, this one\u0027s called Crummy Inn."},{"Start":"07:55.405 ","End":"08:04.050","Text":"I\u0027m going to create some new rooms and add them by using the addRoom method."},{"Start":"08:04.050 ","End":"08:13.590","Text":"Ki, it said Room 1 has 1 bed and Room 2 has 2 beds,"},{"Start":"08:13.590 ","End":"08:16.335","Text":"Room 3 has 3 beds."},{"Start":"08:16.335 ","End":"08:20.224","Text":"That\u0027s our new property created,"},{"Start":"08:20.224 ","End":"08:21.440","Text":"it\u0027s called Crummy Inn."},{"Start":"08:21.440 ","End":"08:24.050","Text":"We\u0027ve re-used this reference variable,"},{"Start":"08:24.050 ","End":"08:25.775","Text":"but we\u0027re referring to a different object."},{"Start":"08:25.775 ","End":"08:27.440","Text":"That one still exists,"},{"Start":"08:27.440 ","End":"08:31.410","Text":"but it\u0027s been stored away into this HashMap."},{"Start":"08:31.410 ","End":"08:39.135","Text":"Now this second hotel that we\u0027ve created here is got to also be added to the collection."},{"Start":"08:39.135 ","End":"08:41.520","Text":"In Part l, we\u0027ve been asked to do that."},{"Start":"08:41.520 ","End":"08:48.050","Text":"Actually I can just copy this exact line here and that will do the job."},{"Start":"08:48.050 ","End":"08:50.225","Text":"Done quite a few things."},{"Start":"08:50.225 ","End":"08:54.455","Text":"We\u0027ve created a property and added 5 rooms to it,"},{"Start":"08:54.455 ","End":"08:58.235","Text":"and then created a different property and added 3 rooms to it."},{"Start":"08:58.235 ","End":"09:03.830","Text":"We can see what we\u0027ve managed to build by"},{"Start":"09:03.830 ","End":"09:10.685","Text":"printing out the properties collection."},{"Start":"09:10.685 ","End":"09:12.695","Text":"Let\u0027s see what that does."},{"Start":"09:12.695 ","End":"09:15.635","Text":"You can see here we\u0027ve got some arrows."},{"Start":"09:15.635 ","End":"09:18.860","Text":"These won\u0027t contain the diamonds that you saw,"},{"Start":"09:18.860 ","End":"09:24.440","Text":"because BlueJ doesn\u0027t support that but you can make sense of what\u0027s going on here."},{"Start":"09:24.440 ","End":"09:31.850","Text":"Let\u0027s create an instance of HotelsApp and run the start method."},{"Start":"09:31.850 ","End":"09:39.215","Text":"We have something up here on the screen which says Crummy Inn equals Crummy Inn 3 rooms,"},{"Start":"09:39.215 ","End":"09:43.610","Text":"region hotels equals region hotel 5 rooms."},{"Start":"09:43.610 ","End":"09:49.555","Text":"What we\u0027re seeing here is as we\u0027ve been asked to explain in Part n,"},{"Start":"09:49.555 ","End":"09:54.595","Text":"is this is displaying the contents of a HashMap."},{"Start":"09:54.595 ","End":"09:58.540","Text":"That\u0027s the default toString method of all HashMaps."},{"Start":"09:58.540 ","End":"10:03.820","Text":"It will just show you the key and then equal sign and then whatever"},{"Start":"10:03.820 ","End":"10:10.485","Text":"the toString returns for the object that\u0027s being stored in this collection."},{"Start":"10:10.485 ","End":"10:14.455","Text":"Because we\u0027re storing a property object,"},{"Start":"10:14.455 ","End":"10:20.110","Text":"it will call the toString method of that individual property,"},{"Start":"10:20.110 ","End":"10:24.115","Text":"which returns the name of the hotel and how many rooms it had."},{"Start":"10:24.115 ","End":"10:28.905","Text":"When we overrode toString in here, that\u0027s what it did."},{"Start":"10:28.905 ","End":"10:31.870","Text":"We\u0027re not seeing the individual details of the rooms,"},{"Start":"10:31.870 ","End":"10:34.750","Text":"so we\u0027ll need something else to do that but rest"},{"Start":"10:34.750 ","End":"10:38.080","Text":"assured they are there and we\u0027ll see that later on."},{"Start":"10:38.080 ","End":"10:39.955","Text":"But that\u0027s a good start."},{"Start":"10:39.955 ","End":"10:42.265","Text":"We\u0027ve got 2 different hotels."},{"Start":"10:42.265 ","End":"10:44.919","Text":"It\u0027s got the correct number of rooms."},{"Start":"10:44.919 ","End":"10:48.985","Text":"The order in which it\u0027s stored doesn\u0027t really matter but we can"},{"Start":"10:48.985 ","End":"10:53.440","Text":"access either of these hotels by using the key,"},{"Start":"10:53.440 ","End":"10:56.155","Text":"which happens to be the name of the hotel."},{"Start":"10:56.155 ","End":"10:59.365","Text":"Then once we\u0027ve got access to it, that property,"},{"Start":"10:59.365 ","End":"11:03.285","Text":"we can manipulate the rooms within it."},{"Start":"11:03.285 ","End":"11:07.555","Text":"We will move on to that in the next exercises."},{"Start":"11:07.555 ","End":"11:11.840","Text":"That\u0027s it for this one. Thanks very much for watching. See you soon."}],"ID":31193},{"Watched":false,"Name":"Exercise 4 Part 1","Duration":"7m 55s","ChapterTopicVideoID":29612,"CourseChapterTopicPlaylistID":294565,"HasSubtitles":true,"ThumbnailPath":null,"UploadDate":null,"DurationForVideoObject":null,"Description":null,"MetaTitle":null,"MetaDescription":null,"Canonical":null,"VideoComments":[],"Subtitles":[{"Start":"00:00.380 ","End":"00:04.170","Text":"Hello everyone and welcome to this exercise in which we\u0027ve been asked to"},{"Start":"00:04.170 ","End":"00:07.830","Text":"build on the project we created in the previous exercise,"},{"Start":"00:07.830 ","End":"00:10.500","Text":"to implement a hotel booking system."},{"Start":"00:10.500 ","End":"00:14.820","Text":"We\u0027re asked to create a new enum by clicking on the \"New Class\" button on"},{"Start":"00:14.820 ","End":"00:20.190","Text":"the left-hand side of the project screen in BlueJ and in the dialogue that comes up,"},{"Start":"00:20.190 ","End":"00:23.745","Text":"selecting \"Enum\" under \"Class Type\"."},{"Start":"00:23.745 ","End":"00:27.780","Text":"We\u0027re to give the enum the name \u0027Command\u0027."},{"Start":"00:27.780 ","End":"00:31.620","Text":"In Part A, we\u0027re then asked to edit the default code generated by"},{"Start":"00:31.620 ","End":"00:35.175","Text":"BlueJ so that the values inside the braces are QUIT,"},{"Start":"00:35.175 ","End":"00:39.360","Text":"BOOK, LIST, MENU and BOOKINGS."},{"Start":"00:39.360 ","End":"00:42.430","Text":"In Part B, we\u0027re asked to compile the enum,"},{"Start":"00:42.430 ","End":"00:45.050","Text":"fixing any errors as appropriate."},{"Start":"00:45.050 ","End":"00:49.460","Text":"In Part C, we\u0027re asked to reopen the HotelsApp class in"},{"Start":"00:49.460 ","End":"00:54.250","Text":"the project and above the start method to add another HashMap,"},{"Start":"00:54.250 ","End":"00:59.505","Text":"called commandWords, which uses a String as a key and"},{"Start":"00:59.505 ","End":"01:05.870","Text":"a Command enumerated type as the value to be retrieved from the collection."},{"Start":"01:05.870 ","End":"01:08.270","Text":"In part D, we\u0027re asked,"},{"Start":"01:08.270 ","End":"01:13.970","Text":"inside the start method to add a series of entries to the HashMap as follows."},{"Start":"01:13.970 ","End":"01:19.360","Text":"In Part 1, the key should be \"quit\" and the Command should be Command.QUIT."},{"Start":"01:19.360 ","End":"01:23.760","Text":"In Part 2, the key is \"book\" and the Command is Command.BOOK."},{"Start":"01:23.760 ","End":"01:29.310","Text":"In Part 3, the key is \"list\" and the Command is Command.LIST."},{"Start":"01:29.310 ","End":"01:30.645","Text":"In Part 4,"},{"Start":"01:30.645 ","End":"01:34.515","Text":"the key is \"Menu\" and the Command is Command.HELP."},{"Start":"01:34.515 ","End":"01:36.300","Text":"In Part 5,"},{"Start":"01:36.300 ","End":"01:41.460","Text":"the Key is \"Exit\" and the Command is Command.QUIT."},{"Start":"01:41.460 ","End":"01:43.105","Text":"In Part E,"},{"Start":"01:43.105 ","End":"01:47.360","Text":"we\u0027re then asked to test that the map is correctly set up by adding"},{"Start":"01:47.360 ","End":"01:52.435","Text":"the line System.out.printLn(commandWords.keySet());"},{"Start":"01:52.435 ","End":"01:57.065","Text":"and running the start method from the object workbench."},{"Start":"01:57.065 ","End":"02:03.050","Text":"Let\u0027s get back into the project that we had previously from the previous exercise."},{"Start":"02:03.050 ","End":"02:04.730","Text":"Then as we\u0027ve been told here,"},{"Start":"02:04.730 ","End":"02:08.100","Text":"we need to create a new enum."},{"Start":"02:08.100 ","End":"02:09.840","Text":"We click on \"New Class\" to do that,"},{"Start":"02:09.840 ","End":"02:12.260","Text":"it\u0027s a type of class in Java,"},{"Start":"02:12.260 ","End":"02:13.870","Text":"is the way it\u0027s implemented."},{"Start":"02:13.870 ","End":"02:18.965","Text":"Then we Select \"Enum\" under \"Class Type\" and then we give it a name."},{"Start":"02:18.965 ","End":"02:21.875","Text":"We\u0027ve been told to call it \u0027Command\u0027."},{"Start":"02:21.875 ","End":"02:24.665","Text":"That will generate this,"},{"Start":"02:24.665 ","End":"02:26.900","Text":"which by default, as usual,"},{"Start":"02:26.900 ","End":"02:32.240","Text":"BlueJ tries to be helpful and sets up an example for you,"},{"Start":"02:32.240 ","End":"02:34.730","Text":"the structure of an enum,"},{"Start":"02:34.730 ","End":"02:38.240","Text":"so we can just delete those values that it\u0027s put in."},{"Start":"02:38.240 ","End":"02:41.826","Text":"It\u0027s enumerated the days of the week there."},{"Start":"02:41.826 ","End":"02:44.930","Text":"We can replace it with the ones that we need to."},{"Start":"02:44.930 ","End":"02:47.617","Text":"In Part A, we\u0027re told what they are."},{"Start":"02:47.617 ","End":"02:49.880","Text":"so we want a command called QUIT,"},{"Start":"02:49.880 ","End":"02:52.065","Text":"another one called BOOK,"},{"Start":"02:52.065 ","End":"02:55.065","Text":"another one called LIST, HELP."},{"Start":"02:55.065 ","End":"02:57.030","Text":"That\u0027s it, we don\u0027t need to put a semicolon,"},{"Start":"02:57.030 ","End":"03:04.140","Text":"the braces contain whatever the individual allowed values are for this enumeration."},{"Start":"03:04.140 ","End":"03:07.440","Text":"Just Click on \"Compile\" to check if it\u0027s fine, which it is."},{"Start":"03:07.440 ","End":"03:10.085","Text":"That\u0027s Part A and B done."},{"Start":"03:10.085 ","End":"03:15.030","Text":"Now, we\u0027re going to go back into the HotelsApp Class,"},{"Start":"03:15.030 ","End":"03:21.125","Text":"which contains the start method and allows us to test what we\u0027ve done."},{"Start":"03:21.125 ","End":"03:26.060","Text":"Let\u0027s get in there. Inside here we\u0027ve been told"},{"Start":"03:26.060 ","End":"03:32.120","Text":"to add another HashMap into this class."},{"Start":"03:32.120 ","End":"03:38.395","Text":"That one is going to be called commandWords."},{"Start":"03:38.395 ","End":"03:40.610","Text":"We know how to do a HashMap,"},{"Start":"03:40.610 ","End":"03:42.560","Text":"same as above, but this time,"},{"Start":"03:42.560 ","End":"03:48.185","Text":"we\u0027re going to map a String which represents the text that\u0027s being typed in."},{"Start":"03:48.185 ","End":"03:54.480","Text":"We\u0027re going to map that to one of our enumerated values in Commands."},{"Start":"03:54.480 ","End":"03:57.435","Text":"What we need to do is to say String,"},{"Start":"03:57.435 ","End":"04:02.520","Text":"Command and we\u0027re going to call this commandWords."},{"Start":"04:02.520 ","End":"04:08.090","Text":"It does mean, of course, that the String that we supply here should be unique,"},{"Start":"04:08.090 ","End":"04:13.820","Text":"so one command maps to one particular command in the enumeration."},{"Start":"04:13.820 ","End":"04:19.520","Text":"That\u0027s done and what we can do now is Part D,"},{"Start":"04:19.520 ","End":"04:24.380","Text":"which is going to be inside the start method."},{"Start":"04:24.380 ","End":"04:25.970","Text":"Inside the start method,"},{"Start":"04:25.970 ","End":"04:29.695","Text":"we\u0027d already set up these test code here for these properties."},{"Start":"04:29.695 ","End":"04:32.170","Text":"Let\u0027s put it down here,"},{"Start":"04:32.170 ","End":"04:33.380","Text":"just make a bit of space."},{"Start":"04:33.380 ","End":"04:37.505","Text":"I\u0027m going to put the commands that we want to"},{"Start":"04:37.505 ","End":"04:43.055","Text":"be triggered when we type in a particular word into that HashMap."},{"Start":"04:43.055 ","End":"04:49.284","Text":"What we\u0027ve got to do is the HashMap is called commandWords and into that,"},{"Start":"04:49.284 ","End":"04:56.535","Text":"we\u0027re going to put something and the thing we put is, first, the key."},{"Start":"04:56.535 ","End":"05:02.070","Text":"This is the text we\u0027re going to use to quit the program."},{"Start":"05:02.070 ","End":"05:04.850","Text":"That\u0027s going to be mapped to a command,"},{"Start":"05:04.850 ","End":"05:09.145","Text":"which is Command.QUIT,"},{"Start":"05:09.145 ","End":"05:13.965","Text":"which is what we created in the enum class."},{"Start":"05:13.965 ","End":"05:17.840","Text":"That should map a text, \"quit\","},{"Start":"05:17.840 ","End":"05:21.665","Text":"to a command we want to perform, which does quitting."},{"Start":"05:21.665 ","End":"05:25.760","Text":"We just carry on doing that for the other ones we\u0027ve been asked to do."},{"Start":"05:25.760 ","End":"05:30.170","Text":"We\u0027ve been asked to do book and the keyword for"},{"Start":"05:30.170 ","End":"05:34.955","Text":"that is \"book\" and the command is close but very similar."},{"Start":"05:34.955 ","End":"05:38.510","Text":"Then we do in Part 3, \"list\","},{"Start":"05:38.510 ","End":"05:45.460","Text":"I\u0027m going to list the hotels that we have available and that maps to the list command."},{"Start":"05:45.460 ","End":"05:48.980","Text":"Then we\u0027re going to put one in for \"help\","},{"Start":"05:48.980 ","End":"05:52.640","Text":"which displays all the available commands."},{"Start":"05:52.640 ","End":"05:58.580","Text":"Just to show you that we can use two different keys to trigger the same command,"},{"Start":"05:58.580 ","End":"06:00.470","Text":"we\u0027re going to use exit as well."},{"Start":"06:00.470 ","End":"06:07.325","Text":"You can exit a program either by typing \u0027exit\u0027 or by typing \u0027quit\u0027,"},{"Start":"06:07.325 ","End":"06:09.980","Text":"but either one is mapped to the Command.QUIT,"},{"Start":"06:09.980 ","End":"06:12.680","Text":"so we do have a little bit of flexibility with the keywords we can use,"},{"Start":"06:12.680 ","End":"06:17.600","Text":"there\u0027s often more than one word to describe an action you want to do and that\u0027s fine."},{"Start":"06:17.600 ","End":"06:21.005","Text":"They can both trigger the same command if we want to,"},{"Start":"06:21.005 ","End":"06:24.215","Text":"but the words themselves, because this is a map."},{"Start":"06:24.215 ","End":"06:28.900","Text":"That\u0027s Part D done."},{"Start":"06:28.900 ","End":"06:31.545","Text":"Got any errors there so far? No, we haven\u0027t."},{"Start":"06:31.545 ","End":"06:39.840","Text":"Now we can simply put in a line to print out what the command words set looks like."},{"Start":"06:39.840 ","End":"06:42.725","Text":"To do that, we do the following."},{"Start":"06:42.725 ","End":"06:49.730","Text":"We\u0027ve been asked to put System.out.printLn(commandWords.keySet));.What that"},{"Start":"06:49.730 ","End":"06:56.870","Text":"does is it returns us set of all the key values that have been put into this structure,"},{"Start":"06:56.870 ","End":"06:59.300","Text":"commandWords into the collection and this"},{"Start":"06:59.300 ","End":"07:02.630","Text":"HashMap and it would just return us all those things."},{"Start":"07:02.630 ","End":"07:06.410","Text":"Presumably that has a two String that will represent"},{"Start":"07:06.410 ","End":"07:10.770","Text":"the values and we can print out on one line that way."},{"Start":"07:10.770 ","End":"07:16.400","Text":"Let\u0027s see if that works by creating and you\u0027ll see now we\u0027ve"},{"Start":"07:16.400 ","End":"07:22.770","Text":"got a line going here because we\u0027re using this enumeration in the HotelsApp."},{"Start":"07:22.770 ","End":"07:25.590","Text":"If we create an object,"},{"Start":"07:25.590 ","End":"07:28.260","Text":"execute the start method,"},{"Start":"07:28.260 ","End":"07:30.290","Text":"we still see the code from before,"},{"Start":"07:30.290 ","End":"07:34.575","Text":"but we now see a list of the available commands,"},{"Start":"07:34.575 ","End":"07:35.820","Text":"help, exit,"},{"Start":"07:35.820 ","End":"07:37.810","Text":"book, quit and list."},{"Start":"07:37.810 ","End":"07:41.495","Text":"Notice they\u0027re not in the order we actually put them in and that\u0027s fine"},{"Start":"07:41.495 ","End":"07:46.265","Text":"because these are unordered and they just have to be unique."},{"Start":"07:46.265 ","End":"07:50.180","Text":"We\u0027ll pause there and in the next video we\u0027ll come"},{"Start":"07:50.180 ","End":"07:53.765","Text":"back to actually using these commands and processing them."},{"Start":"07:53.765 ","End":"07:56.610","Text":"See you shortly for that one."}],"ID":31194},{"Watched":false,"Name":"Exercise 4 Part 2","Duration":"12m 12s","ChapterTopicVideoID":29613,"CourseChapterTopicPlaylistID":294565,"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.430","Text":"Hello, welcome back to the second part of"},{"Start":"00:02.430 ","End":"00:05.085","Text":"this exercise in which we\u0027ve been asked to create"},{"Start":"00:05.085 ","End":"00:08.265","Text":"a new method input property which has no arguments"},{"Start":"00:08.265 ","End":"00:11.849","Text":"and returns a property object reference."},{"Start":"00:11.849 ","End":"00:18.000","Text":"It should prompt the user to enter a property name at the keyboard and store the property"},{"Start":"00:18.000 ","End":"00:24.740","Text":"entered at the keyboard into a string allowing for a property name that includes spaces."},{"Start":"00:24.740 ","End":"00:30.229","Text":"You should then check whether that property exists in the properties collection."},{"Start":"00:30.229 ","End":"00:34.025","Text":"If it doesn\u0027t, it should display a message \"Not found,"},{"Start":"00:34.025 ","End":"00:37.100","Text":"try again\" and loop back to Part 1."},{"Start":"00:37.100 ","End":"00:39.350","Text":"When a valid property is entered,"},{"Start":"00:39.350 ","End":"00:43.330","Text":"the method should return a reference to that property."},{"Start":"00:43.330 ","End":"00:46.190","Text":"In Part G, we\u0027re told to test"},{"Start":"00:46.190 ","End":"00:51.635","Text":"the input property method by adding this line below the booking test code,"},{"Start":"00:51.635 ","End":"00:56.545","Text":"we then start as follows and running the start method."},{"Start":"00:56.545 ","End":"01:03.275","Text":"The code is system out print line found plus input property."},{"Start":"01:03.275 ","End":"01:08.180","Text":"We then check an input of Notel is rejected and"},{"Start":"01:08.180 ","End":"01:12.650","Text":"prompts for more input and we check an input of"},{"Start":"01:12.650 ","End":"01:16.055","Text":"reagent hotel is accepted and returns"},{"Start":"01:16.055 ","End":"01:23.225","Text":"a property object so that the Found message is displayed along with the hotel details."},{"Start":"01:23.225 ","End":"01:29.165","Text":"If we start off by adding below what we\u0027ve written so far, the following,"},{"Start":"01:29.165 ","End":"01:38.770","Text":"just to give us the structure that we need for testing and so on."},{"Start":"01:38.770 ","End":"01:42.960","Text":"We\u0027ve got 1 loop here and inside it,"},{"Start":"01:42.960 ","End":"01:49.955","Text":"we\u0027ve got 2 methods being called process option and choose option."},{"Start":"01:49.955 ","End":"01:53.510","Text":"They happen in that order because we want to process"},{"Start":"01:53.510 ","End":"01:58.400","Text":"the option that was typed in and there hasn\u0027t been 1 typed in until we get to this line."},{"Start":"01:58.400 ","End":"02:02.870","Text":"We\u0027ve set ourselves up with a pre-selected command at the beginning,"},{"Start":"02:02.870 ","End":"02:03.980","Text":"which is the help command,"},{"Start":"02:03.980 ","End":"02:05.150","Text":"which is useful,"},{"Start":"02:05.150 ","End":"02:06.575","Text":"the first time we run this,"},{"Start":"02:06.575 ","End":"02:10.970","Text":"it will print out list of the allowed commands,"},{"Start":"02:10.970 ","End":"02:13.070","Text":"which are all of these ones here."},{"Start":"02:13.070 ","End":"02:21.440","Text":"That\u0027s the loop done that we\u0027ve been asked to do in Part F. Now inside the class,"},{"Start":"02:21.440 ","End":"02:23.645","Text":"but outside this method,"},{"Start":"02:23.645 ","End":"02:28.325","Text":"we need to create a new method called process option,"},{"Start":"02:28.325 ","End":"02:32.000","Text":"and that takes an argument of type commands."},{"Start":"02:32.000 ","End":"02:36.110","Text":"We need to give it a name. I\u0027ll just call it CMD for the parameter."},{"Start":"02:36.110 ","End":"02:41.750","Text":"What does this do? It has to have going into G_1, a switch structure."},{"Start":"02:41.750 ","End":"02:45.305","Text":"Switch is are very handy selection structure"},{"Start":"02:45.305 ","End":"02:48.750","Text":"that for this circumstance works really well."},{"Start":"02:48.750 ","End":"02:51.095","Text":"We switch on something,"},{"Start":"02:51.095 ","End":"02:53.404","Text":"and depending on the value of that something,"},{"Start":"02:53.404 ","End":"02:56.525","Text":"we run different blocks of code."},{"Start":"02:56.525 ","End":"02:59.359","Text":"We\u0027ve been passed in a command."},{"Start":"02:59.359 ","End":"03:02.179","Text":"One of the enumerations in that list that we created,"},{"Start":"03:02.179 ","End":"03:03.845","Text":"depending on what its value is,"},{"Start":"03:03.845 ","End":"03:08.225","Text":"it will do different block of code that will execute a different block of code."},{"Start":"03:08.225 ","End":"03:11.870","Text":"We\u0027ve been given an example for this."},{"Start":"03:11.870 ","End":"03:15.470","Text":"First cases, the thing that goes alongside switch"},{"Start":"03:15.470 ","End":"03:19.445","Text":"to say for these different values do these actions."},{"Start":"03:19.445 ","End":"03:24.455","Text":"If it\u0027s help command that\u0027s been passed in,"},{"Start":"03:24.455 ","End":"03:26.675","Text":"then we going to do the fine."},{"Start":"03:26.675 ","End":"03:35.700","Text":"We\u0027re just going to display a message followed by what we did earlier commandWords."},{"Start":"03:35.780 ","End":"03:39.770","Text":"We turn all the possible commands as"},{"Start":"03:39.770 ","End":"03:44.165","Text":"a list and then we\u0027ve told the user basically what\u0027s allowable."},{"Start":"03:44.165 ","End":"03:50.375","Text":"We did that, you\u0027ll notice further up here as part of our debug, I believe."},{"Start":"03:50.375 ","End":"03:54.150","Text":"I think I\u0027ve already taken out that\u0027s not there anymore."},{"Start":"03:54.370 ","End":"04:00.330","Text":"What we need to do is to put public and void them."},{"Start":"04:00.330 ","End":"04:08.700","Text":"What we need to do next then is to implement the list command as G_2 ask us to do."},{"Start":"04:08.700 ","End":"04:15.245","Text":"We\u0027ve done this one and each command should have a break after it."},{"Start":"04:15.245 ","End":"04:19.515","Text":"It knows that they help code is from there to there."},{"Start":"04:19.515 ","End":"04:25.505","Text":"Then the next value here is going to be another type of command."},{"Start":"04:25.505 ","End":"04:28.880","Text":"This is the block of code for the list command."},{"Start":"04:28.880 ","End":"04:33.815","Text":"For list, we want to do something similar to the above actually."},{"Start":"04:33.815 ","End":"04:38.315","Text":"But what we\u0027re going to do is print a list of the hotels that are available."},{"Start":"04:38.315 ","End":"04:41.955","Text":"Here we\u0027ll say something like this."},{"Start":"04:41.955 ","End":"04:45.584","Text":"Again, stoking, we command words,"},{"Start":"04:45.584 ","End":"04:53.470","Text":"keys will get the keys from properties which will give us the name of each property."},{"Start":"04:53.470 ","End":"04:55.850","Text":"We don\u0027t want to print out every detail about the property,"},{"Start":"04:55.850 ","End":"04:57.265","Text":"we just want the names."},{"Start":"04:57.265 ","End":"05:02.225","Text":"We now have a list of the available hotels and once again,"},{"Start":"05:02.225 ","End":"05:05.720","Text":"we want to do break here and then want to implement in"},{"Start":"05:05.720 ","End":"05:12.830","Text":"any other commands so far so we\u0027ll just put a default clause in,"},{"Start":"05:12.830 ","End":"05:18.845","Text":"and would say something like system out, print line commands not implemented."},{"Start":"05:18.845 ","End":"05:21.620","Text":"That is the thing that does they\u0027re doing,"},{"Start":"05:21.620 ","End":"05:24.830","Text":"it processes the option that\u0027s been selected,"},{"Start":"05:24.830 ","End":"05:29.610","Text":"but we need to get the option from the keyboard as well."},{"Start":"05:29.610 ","End":"05:34.715","Text":"This is Part H. We\u0027re going to return"},{"Start":"05:34.715 ","End":"05:41.925","Text":"something which has type command and it\u0027s called chooseOption."},{"Start":"05:41.925 ","End":"05:44.835","Text":"Has no parameters,"},{"Start":"05:44.835 ","End":"05:48.080","Text":"public, and it\u0027s expecting a return,"},{"Start":"05:48.080 ","End":"05:51.080","Text":"which is why it\u0027s giving me an error message at the moment."},{"Start":"05:51.080 ","End":"05:53.670","Text":"What going to do in here?"},{"Start":"05:53.670 ","End":"05:57.845","Text":"We need to store whatever is typed in into a variable."},{"Start":"05:57.845 ","End":"06:00.079","Text":"So we\u0027ll call that variable choice,"},{"Start":"06:00.079 ","End":"06:03.080","Text":"and we need a variable called valid."},{"Start":"06:03.080 ","End":"06:04.775","Text":"It tells us in H_1,"},{"Start":"06:04.775 ","End":"06:08.765","Text":"which is a Boolean, and we set that to false."},{"Start":"06:08.765 ","End":"06:13.535","Text":"Then we can create a scanner object, call it keyboard."},{"Start":"06:13.535 ","End":"06:15.920","Text":"Now, we can do the while loop,"},{"Start":"06:15.920 ","End":"06:22.195","Text":"refers to in Part 2 of H. How\u0027s that going to look?"},{"Start":"06:22.195 ","End":"06:27.845","Text":"I should probably tell it to what we need to type first of all."},{"Start":"06:27.845 ","End":"06:31.595","Text":"Let\u0027s do system out print line."},{"Start":"06:31.595 ","End":"06:36.230","Text":"Command first of all and then we can do our while loop."},{"Start":"06:36.230 ","End":"06:43.175","Text":"What goes inside here is going to be as simple as that while not valid, keep looping."},{"Start":"06:43.175 ","End":"06:46.400","Text":"We want them to keep typing something in until they type in something"},{"Start":"06:46.400 ","End":"06:50.825","Text":"sensible that matches 1 of the available commands."},{"Start":"06:50.825 ","End":"06:58.430","Text":"In here we\u0027ll do some code that prints an error message or accepts the command."},{"Start":"06:58.430 ","End":"07:01.130","Text":"What do we have to do? We have to type."},{"Start":"07:01.130 ","End":"07:04.325","Text":"We have to get them to type something in."},{"Start":"07:04.325 ","End":"07:13.565","Text":"Then we use that to get something out of the HashMap which matches what they typed in."},{"Start":"07:13.565 ","End":"07:15.349","Text":"If there is no match,"},{"Start":"07:15.349 ","End":"07:17.435","Text":"then it will be null."},{"Start":"07:17.435 ","End":"07:21.720","Text":"When it\u0027s not equal to null, we\u0027re good."},{"Start":"07:22.550 ","End":"07:25.905","Text":"Otherwise, if it\u0027s is equal to null,"},{"Start":"07:25.905 ","End":"07:28.470","Text":"we need to print an error message."},{"Start":"07:28.470 ","End":"07:30.825","Text":"Let\u0027s do that."},{"Start":"07:30.825 ","End":"07:36.920","Text":"The error message is going to be not valid command and if it is a valid command,"},{"Start":"07:36.920 ","End":"07:38.345","Text":"we\u0027re not going to do anything yet,"},{"Start":"07:38.345 ","End":"07:41.810","Text":"but we do need to set the flag that we\u0027ve used called"},{"Start":"07:41.810 ","End":"07:47.165","Text":"valid to true so that we break out of this while loop."},{"Start":"07:47.165 ","End":"07:50.750","Text":"All this is doing is constantly getting some texts,"},{"Start":"07:50.750 ","End":"07:56.610","Text":"single word text item in and it uses that text as"},{"Start":"07:56.610 ","End":"08:03.110","Text":"a key so we try and get using that key from command words or command."},{"Start":"08:03.110 ","End":"08:04.910","Text":"If we get a command that\u0027s null,"},{"Start":"08:04.910 ","End":"08:08.990","Text":"that means it wasn\u0027t able to find 1 and that means it was an invalid keyword."},{"Start":"08:08.990 ","End":"08:11.150","Text":"If it does find a valid 1,"},{"Start":"08:11.150 ","End":"08:15.432","Text":"then we say valid is true and it will break out of this loop,"},{"Start":"08:15.432 ","End":"08:18.830","Text":"otherwise we print an error message."},{"Start":"08:18.830 ","End":"08:21.260","Text":"Once we\u0027ve broken out of the loop,"},{"Start":"08:21.260 ","End":"08:24.420","Text":"we can return a command."},{"Start":"08:24.420 ","End":"08:26.000","Text":"This is a nice routine."},{"Start":"08:26.000 ","End":"08:28.960","Text":"It will only return as valid commands."},{"Start":"08:28.960 ","End":"08:33.675","Text":"We need to get a command there so we do exactly what we do."},{"Start":"08:33.675 ","End":"08:36.770","Text":"This code done."},{"Start":"08:36.770 ","End":"08:40.100","Text":"We can have a go at running this now."},{"Start":"08:40.100 ","End":"08:41.645","Text":"Let\u0027s see what we get."},{"Start":"08:41.645 ","End":"08:48.020","Text":"Run the start method and still got some of the decode there."},{"Start":"08:48.020 ","End":"08:52.175","Text":"But we\u0027ve seen now that the help is immediately printed."},{"Start":"08:52.175 ","End":"08:57.035","Text":"The commands that are available and now I\u0027ve been asked to type in a command."},{"Start":"08:57.035 ","End":"08:59.210","Text":"Let\u0027s type in list,"},{"Start":"08:59.210 ","End":"09:04.695","Text":"and the list command seems to work and it returns me a list of the available hotels."},{"Start":"09:04.695 ","End":"09:09.515","Text":"We\u0027ve already seen help work because it was set up to try that."},{"Start":"09:09.515 ","End":"09:12.955","Text":"But we\u0027ll do it again just to prove it. There it is."},{"Start":"09:12.955 ","End":"09:17.145","Text":"Help. What about book?"},{"Start":"09:17.145 ","End":"09:20.315","Text":"That does show command not implemented yet."},{"Start":"09:20.315 ","End":"09:26.555","Text":"If I typed in a command that didn\u0027t even exist in the list,"},{"Start":"09:26.555 ","End":"09:28.625","Text":"I\u0027d get a different type of error."},{"Start":"09:28.625 ","End":"09:33.275","Text":"For example, make is not a valid command,"},{"Start":"09:33.275 ","End":"09:38.120","Text":"bookings is not a valid command but the ones we"},{"Start":"09:38.120 ","End":"09:42.920","Text":"have typed up until now are valid commands in the case of book,"},{"Start":"09:42.920 ","End":"09:44.920","Text":"it just hasn\u0027t been implemented yet."},{"Start":"09:44.920 ","End":"09:47.360","Text":"That does what we want to do,"},{"Start":"09:47.360 ","End":"09:49.925","Text":"just trying with exit as well."},{"Start":"09:49.925 ","End":"09:53.755","Text":"Because I can use exit or quit and it has quit the program."},{"Start":"09:53.755 ","End":"09:55.940","Text":"It\u0027s done exactly what I wanted."},{"Start":"09:55.940 ","End":"09:59.080","Text":"But it was quite long-winded and now we\u0027ve done this before."},{"Start":"09:59.080 ","End":"10:01.865","Text":"The question in Part J is,"},{"Start":"10:01.865 ","End":"10:06.950","Text":"why have we broken it up into so many individual pieces rather than just"},{"Start":"10:06.950 ","End":"10:12.560","Text":"write it out as 1 big block up here and start as we thought we\u0027ve previously done."},{"Start":"10:12.560 ","End":"10:15.200","Text":"We\u0027ve really broken it down into these 3 bits."},{"Start":"10:15.200 ","End":"10:18.380","Text":"That part there, that part there and that part there."},{"Start":"10:18.380 ","End":"10:21.950","Text":"We know that a good design principles to follow is we"},{"Start":"10:21.950 ","End":"10:24.920","Text":"should implement methods that just complete 1 task and"},{"Start":"10:24.920 ","End":"10:28.445","Text":"1 task only and to fit the principle of"},{"Start":"10:28.445 ","End":"10:33.975","Text":"high cohesion and that\u0027s pretty much fits this command,"},{"Start":"10:33.975 ","End":"10:37.939","Text":"this method is just processing the various options."},{"Start":"10:37.939 ","End":"10:41.255","Text":"This one\u0027s getting the options from the keyboard."},{"Start":"10:41.255 ","End":"10:44.810","Text":"It\u0027s completely different method and it just has 1 job."},{"Start":"10:44.810 ","End":"10:46.760","Text":"It gets a valid command,"},{"Start":"10:46.760 ","End":"10:49.250","Text":"and if it is valid, returns it."},{"Start":"10:49.250 ","End":"10:51.155","Text":"This one does 1 job."},{"Start":"10:51.155 ","End":"10:54.380","Text":"It processes the command that\u0027s been passed in."},{"Start":"10:54.380 ","End":"11:02.885","Text":"This bit here is just calling those 2 methods 1 after another. Much better."},{"Start":"11:02.885 ","End":"11:05.209","Text":"Even the enumerations themselves,"},{"Start":"11:05.209 ","End":"11:07.175","Text":"you might argue or why do you need those?"},{"Start":"11:07.175 ","End":"11:10.160","Text":"We can just use these strings. There\u0027s a couple of reasons."},{"Start":"11:10.160 ","End":"11:16.445","Text":"One is that the compiler will help us enforce on some checking."},{"Start":"11:16.445 ","End":"11:22.740","Text":"As I showed you before, we find a type to command here that doesn\u0027t actually exist,"},{"Start":"11:22.740 ","End":"11:25.145","Text":"the compiler will complain."},{"Start":"11:25.145 ","End":"11:26.990","Text":"Whereas if this was a string,"},{"Start":"11:26.990 ","End":"11:28.505","Text":"so long as it\u0027s a string,"},{"Start":"11:28.505 ","End":"11:29.780","Text":"it would allow it."},{"Start":"11:29.780 ","End":"11:34.310","Text":"The compiler is doing some work here for us. It makes it better."},{"Start":"11:34.310 ","End":"11:40.370","Text":"Also it\u0027s another very subtle type of encapsulate what varies because we might know"},{"Start":"11:40.370 ","End":"11:43.450","Text":"what commands our system needs when we start writing it but"},{"Start":"11:43.450 ","End":"11:47.255","Text":"there\u0027s every chance that we\u0027re going to add new commands over time."},{"Start":"11:47.255 ","End":"11:50.375","Text":"As those requirements of the program change,"},{"Start":"11:50.375 ","End":"11:54.320","Text":"we can add the new commands in 1 particular place,"},{"Start":"11:54.320 ","End":"11:58.430","Text":"much cleaner than doing it all in 1 block,"},{"Start":"11:58.430 ","End":"12:04.010","Text":"which in previous exercises we\u0027ve done and it\u0027s not so maintainable."},{"Start":"12:04.010 ","End":"12:06.125","Text":"That\u0027s it for this one. Thanks for watching."},{"Start":"12:06.125 ","End":"12:08.720","Text":"See you for the next one where we\u0027ll build even further"},{"Start":"12:08.720 ","End":"12:13.020","Text":"on to this particular exercise. Thanks again."}],"ID":31195},{"Watched":false,"Name":"Exercise 5 Part 1","Duration":"6m 54s","ChapterTopicVideoID":29614,"CourseChapterTopicPlaylistID":294565,"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.515","Text":"Hello again everyone."},{"Start":"00:01.515 ","End":"00:04.590","Text":"We\u0027re again going to build on the previous exercise,"},{"Start":"00:04.590 ","End":"00:10.035","Text":"this time to allow a room booking to be made at any of the properties that are available."},{"Start":"00:10.035 ","End":"00:12.480","Text":"In part a, we\u0027re told to create a new class,"},{"Start":"00:12.480 ","End":"00:15.765","Text":"booking, with 3 private attributes."},{"Start":"00:15.765 ","End":"00:19.170","Text":"A reference to a local date object called date,"},{"Start":"00:19.170 ","End":"00:21.435","Text":"an int called beds,"},{"Start":"00:21.435 ","End":"00:25.379","Text":"and a reference to a property object called property."},{"Start":"00:25.379 ","End":"00:28.230","Text":"In part b, we\u0027re told to add a constructor method to"},{"Start":"00:28.230 ","End":"00:31.965","Text":"the class that sets the 3 attributes."},{"Start":"00:31.965 ","End":"00:36.735","Text":"In part c, we\u0027re then told to override the toString method within Booking"},{"Start":"00:36.735 ","End":"00:41.865","Text":"so that it concatenates date with a date, a comma,"},{"Start":"00:41.865 ","End":"00:44.570","Text":"the property name, a comma,"},{"Start":"00:44.570 ","End":"00:46.955","Text":"and the number of beds, for example,"},{"Start":"00:46.955 ","End":"00:53.615","Text":"Date:2022-05-22, Regent Hotel, 2 beds."},{"Start":"00:53.615 ","End":"00:56.135","Text":"In part d, we\u0027re told to add"},{"Start":"00:56.135 ","End":"01:00.574","Text":"an ArrayList collection inside the Hotels App class called bookings,"},{"Start":"01:00.574 ","End":"01:03.840","Text":"which will hold objects of type booking."},{"Start":"01:03.840 ","End":"01:09.830","Text":"In part e, we\u0027re told to test by adding the following lines of code inside"},{"Start":"01:09.830 ","End":"01:16.270","Text":"the start method immediately below the creation of the first property Regent hotel."},{"Start":"01:16.270 ","End":"01:19.955","Text":"Firstly, booking equals new booking."},{"Start":"01:19.955 ","End":"01:22.730","Text":"Local date of 2022,"},{"Start":"01:22.730 ","End":"01:26.645","Text":"06, 22, 1, Hotel."},{"Start":"01:26.645 ","End":"01:33.805","Text":"Then bookings.addbooking and system.out.printline bookings."},{"Start":"01:33.805 ","End":"01:40.800","Text":"Then we run the start method and check that the booking is displayed."},{"Start":"01:40.910 ","End":"01:45.395","Text":"Once again, in the previous project,"},{"Start":"01:45.395 ","End":"01:48.800","Text":"we\u0027re going to create a new class called booking in this case,"},{"Start":"01:48.800 ","End":"01:52.760","Text":"in part a, we\u0027ve got 3 private attributes this time."},{"Start":"01:52.760 ","End":"01:59.260","Text":"The first one is a reference to a local date object."},{"Start":"01:59.260 ","End":"02:04.385","Text":"We\u0027ll need to import the library to enable us to do that."},{"Start":"02:04.385 ","End":"02:06.560","Text":"It\u0027s just called date."},{"Start":"02:06.560 ","End":"02:13.350","Text":"Then we\u0027ve got in part 2 an integer called beds."},{"Start":"02:13.610 ","End":"02:19.880","Text":"Then finally, a reference to a property object."},{"Start":"02:19.880 ","End":"02:22.625","Text":"We\u0027re just going to call that property."},{"Start":"02:22.625 ","End":"02:26.585","Text":"As I said, it\u0027s going to complain about that lined"},{"Start":"02:26.585 ","End":"02:32.820","Text":"unless I import the library. Let\u0027s do that."},{"Start":"02:33.700 ","End":"02:39.740","Text":"We\u0027re good there. Now we can add a constructor as we\u0027ve been asked to in part b,"},{"Start":"02:39.740 ","End":"02:43.295","Text":"that sets those attributes as usual,"},{"Start":"02:43.295 ","End":"02:48.915","Text":"it\u0027s going to be public, it\u0027s called booking."},{"Start":"02:48.915 ","End":"02:51.660","Text":"Let\u0027s do it in that order,"},{"Start":"02:51.660 ","End":"02:59.554","Text":"as in a local date reference and then an integer."},{"Start":"02:59.554 ","End":"03:04.220","Text":"Then we set those into our own instance variables."},{"Start":"03:04.220 ","End":"03:06.995","Text":"We\u0027ve done part b now."},{"Start":"03:06.995 ","End":"03:09.200","Text":"Then the only thing we need to do in"},{"Start":"03:09.200 ","End":"03:14.255","Text":"this very short class is to override the toString method."},{"Start":"03:14.255 ","End":"03:16.250","Text":"I\u0027ll put an annotation in,"},{"Start":"03:16.250 ","End":"03:19.435","Text":"it\u0027s the optional reading in any way."},{"Start":"03:19.435 ","End":"03:22.490","Text":"We got a public method,"},{"Start":"03:22.490 ","End":"03:28.550","Text":"returns a string called toString and what this is going"},{"Start":"03:28.550 ","End":"03:34.595","Text":"to do is return the date a string literal."},{"Start":"03:34.595 ","End":"03:40.490","Text":"It says date, then the date and a space."},{"Start":"03:40.490 ","End":"03:44.915","Text":"Then we\u0027re going to get the properties name and display that."},{"Start":"03:44.915 ","End":"03:47.620","Text":"We could do it that way."},{"Start":"03:53.690 ","End":"04:01.480","Text":"That is it for booking method and it compiles just fine."},{"Start":"04:01.480 ","End":"04:09.030","Text":"What we now need to do is go back to the hotels app code."},{"Start":"04:09.030 ","End":"04:14.675","Text":"We\u0027re going to add an ArrayList to what we have already, called Bookings,"},{"Start":"04:14.675 ","End":"04:21.335","Text":"and that\u0027s used to store objects of type booking so the class we just created."},{"Start":"04:21.335 ","End":"04:24.875","Text":"We\u0027re good now,"},{"Start":"04:24.875 ","End":"04:27.170","Text":"we\u0027ve done part d,"},{"Start":"04:27.170 ","End":"04:30.820","Text":"and then we can go ahead and check this."},{"Start":"04:30.820 ","End":"04:36.530","Text":"Of course, it\u0027s complaining because I haven\u0027t imported the library code."},{"Start":"04:36.530 ","End":"04:38.855","Text":"It\u0027s happier now."},{"Start":"04:38.855 ","End":"04:44.000","Text":"I can now test as we\u0027ve been asked to do in part"},{"Start":"04:44.000 ","End":"04:50.435","Text":"e. I\u0027m going to add some code into start."},{"Start":"04:50.435 ","End":"04:52.850","Text":"I can actually do that test."},{"Start":"04:52.850 ","End":"04:55.320","Text":"I\u0027m already got start to open here."},{"Start":"04:55.320 ","End":"05:00.820","Text":"It has told me to do the test right after the creation of"},{"Start":"05:00.820 ","End":"05:04.750","Text":"the first property because obviously we can\u0027t create"},{"Start":"05:04.750 ","End":"05:09.735","Text":"a booking unless we\u0027ve got a property to refer to."},{"Start":"05:09.735 ","End":"05:15.960","Text":"We happen to have it in this variable here, hotel."},{"Start":"05:15.960 ","End":"05:19.360","Text":"Why not just make use of that as we\u0027ve been asked to do."},{"Start":"05:19.360 ","End":"05:25.030","Text":"So E1 is to create a temporary object called booking,"},{"Start":"05:25.030 ","End":"05:26.500","Text":"the booking itself,"},{"Start":"05:26.500 ","End":"05:31.230","Text":"then we\u0027re going to create using a local date object,"},{"Start":"05:31.230 ","End":"05:36.250","Text":"so we can just put local date of and then some integers in,"},{"Start":"05:36.250 ","End":"05:38.845","Text":"that will give us a date,"},{"Start":"05:38.845 ","End":"05:42.370","Text":"so that would do it."},{"Start":"05:42.370 ","End":"05:46.355","Text":"Then the number of bedrooms and"},{"Start":"05:46.355 ","End":"05:53.570","Text":"the hotel property that we\u0027re going to do the booking for and that obviously,"},{"Start":"05:53.570 ","End":"05:56.540","Text":"we have that already because we created"},{"Start":"05:56.540 ","End":"06:00.035","Text":"one up here when we were adding rooms to the hotel."},{"Start":"06:00.035 ","End":"06:01.695","Text":"Let\u0027s make use of it."},{"Start":"06:01.695 ","End":"06:07.730","Text":"Then what I can do now is add the thing I\u0027ve just created,"},{"Start":"06:07.730 ","End":"06:10.985","Text":"booking to the collection,"},{"Start":"06:10.985 ","End":"06:12.350","Text":"and then to test."},{"Start":"06:12.350 ","End":"06:14.030","Text":"Finally, I can print it out."},{"Start":"06:14.030 ","End":"06:17.625","Text":"I\u0027ll just print the entire collection out."},{"Start":"06:17.625 ","End":"06:19.995","Text":"Let\u0027s see what we get from that."},{"Start":"06:19.995 ","End":"06:25.410","Text":"I haven\u0027t imported the Java lock time."},{"Start":"06:25.410 ","End":"06:27.990","Text":"That seems to work."},{"Start":"06:27.990 ","End":"06:32.115","Text":"Let\u0027s run the start method and see what happens."},{"Start":"06:32.115 ","End":"06:35.510","Text":"We get the booking which is up here,"},{"Start":"06:35.510 ","End":"06:37.910","Text":"and we\u0027ve obviously got some other debug stuff going on."},{"Start":"06:37.910 ","End":"06:44.060","Text":"But there is the date that we entered for a hotel and a single booking,"},{"Start":"06:44.060 ","End":"06:47.450","Text":"and the rest of it is obviously just the program running."},{"Start":"06:47.450 ","End":"06:48.890","Text":"That seems to have worked."},{"Start":"06:48.890 ","End":"06:51.500","Text":"I\u0027ll just pause there and I\u0027ll see you"},{"Start":"06:51.500 ","End":"06:55.530","Text":"shortly for the next part of the exercise. Thanks for watching."}],"ID":31196},{"Watched":false,"Name":"Exercise 5 Part 2","Duration":"7m 41s","ChapterTopicVideoID":29615,"CourseChapterTopicPlaylistID":294565,"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.430","Text":"Hello, welcome back to the second part of"},{"Start":"00:02.430 ","End":"00:05.085","Text":"this exercise in which we\u0027ve been asked to create"},{"Start":"00:05.085 ","End":"00:08.265","Text":"a new method input property which has no arguments"},{"Start":"00:08.265 ","End":"00:11.849","Text":"and returns a property object reference."},{"Start":"00:11.849 ","End":"00:18.000","Text":"It should prompt the user to enter a property name at the keyboard and store the property"},{"Start":"00:18.000 ","End":"00:24.665","Text":"entered at the keyboard into a string allowing for a property name that includes spaces."},{"Start":"00:24.665 ","End":"00:30.064","Text":"It should then check whether that property exists in the properties collection."},{"Start":"00:30.064 ","End":"00:33.860","Text":"If it doesn\u0027t, it should display a message \"Not found,"},{"Start":"00:33.860 ","End":"00:37.160","Text":"try again\" and loop back to Part I."},{"Start":"00:37.160 ","End":"00:39.650","Text":"When a valid property is entered,"},{"Start":"00:39.650 ","End":"00:43.430","Text":"the method should return a reference to that property."},{"Start":"00:43.430 ","End":"00:49.625","Text":"In Part G we\u0027re told tool to test the inputProperty method by adding this line"},{"Start":"00:49.625 ","End":"00:56.440","Text":"below the booking test code within start as follows and running the start method."},{"Start":"00:56.440 ","End":"01:03.150","Text":"The code is system.out.printline found plus inputProperty."},{"Start":"01:03.150 ","End":"01:10.475","Text":"We then check an input of Notel is rejected and prompts for more input,"},{"Start":"01:10.475 ","End":"01:16.205","Text":"and we check an input of Regent Hotel is accepted and returns"},{"Start":"01:16.205 ","End":"01:23.185","Text":"a Property object so that the found message is displayed along with the hotel details."},{"Start":"01:23.185 ","End":"01:30.410","Text":"Continue on into Part F. Then we\u0027re going to create a new method called inputProperty,"},{"Start":"01:30.410 ","End":"01:37.665","Text":"which returns a reference to a Property object."},{"Start":"01:37.665 ","End":"01:41.405","Text":"We\u0027re going to need to make this method public as well,"},{"Start":"01:41.405 ","End":"01:51.460","Text":"so there\u0027s the return value and the name of the method is inputProperty, no parameters."},{"Start":"01:51.740 ","End":"01:56.915","Text":"We need to get the user to type in something at the keyboard."},{"Start":"01:56.915 ","End":"02:02.880","Text":"We\u0027re going to need to use a scanner as the usual,"},{"Start":"02:02.880 ","End":"02:07.635","Text":"and we\u0027ll need a string to hold the input n,"},{"Start":"02:07.635 ","End":"02:11.615","Text":"and because it\u0027s going to be the name of the property, we\u0027ll call it Name."},{"Start":"02:11.615 ","End":"02:16.820","Text":"We\u0027re going to have a Boolean variable for the validation."},{"Start":"02:16.820 ","End":"02:19.760","Text":"Set that to false initially,"},{"Start":"02:19.760 ","End":"02:23.705","Text":"you should have enough now to get going."},{"Start":"02:23.705 ","End":"02:26.750","Text":"One thing we do need to do actually is to make sure"},{"Start":"02:26.750 ","End":"02:34.085","Text":"the scanner object is using a Delimiter of a newline character."},{"Start":"02:34.085 ","End":"02:38.045","Text":"You can get away with space if it\u0027s a one word,"},{"Start":"02:38.045 ","End":"02:40.400","Text":"that command to sweep and for commands."},{"Start":"02:40.400 ","End":"02:43.955","Text":"But for this particular scenario,"},{"Start":"02:43.955 ","End":"02:46.200","Text":"we need them to be able to type spaces in,"},{"Start":"02:46.200 ","End":"02:49.490","Text":"because obviously hotel can have spaces in the name."},{"Start":"02:49.490 ","End":"02:52.105","Text":"That\u0027s a setup."},{"Start":"02:52.105 ","End":"02:56.600","Text":"We probably also want to have something to hold"},{"Start":"02:56.600 ","End":"03:02.850","Text":"the property itself that we\u0027re going to return the chosen properties."},{"Start":"03:02.850 ","End":"03:07.520","Text":"Let\u0027s collect chosen and we\u0027ll set it to null initially."},{"Start":"03:07.520 ","End":"03:10.960","Text":"It will by default be set to null."},{"Start":"03:10.960 ","End":"03:13.410","Text":"That part done."},{"Start":"03:13.410 ","End":"03:17.820","Text":"Because they\u0027re not returning anything, complain there."},{"Start":"03:17.980 ","End":"03:22.105","Text":"Now, we can do our loop."},{"Start":"03:22.105 ","End":"03:26.990","Text":"We\u0027re going to be a while loop because of a while we don\u0027t"},{"Start":"03:26.990 ","End":"03:32.475","Text":"have valid being equal to true, we keep looping."},{"Start":"03:32.475 ","End":"03:40.450","Text":"We want to ask the user to type in the property name and then get that from the keyboard."},{"Start":"03:40.450 ","End":"03:43.985","Text":"Now, the interesting bit,"},{"Start":"03:43.985 ","End":"03:46.640","Text":"we need to try and check whether,"},{"Start":"03:46.640 ","End":"03:51.020","Text":"as we\u0027ve been asked to in Part 3 of F,"},{"Start":"03:51.020 ","End":"03:56.090","Text":"whether that property actually exists in the properties collection."},{"Start":"03:56.090 ","End":"03:59.405","Text":"A simple way of doing that is to say"},{"Start":"03:59.405 ","End":"04:10.560","Text":"properties contains key and whatever it was that was passed in."},{"Start":"04:12.310 ","End":"04:22.990","Text":"If it does, then we can store a reference to the property object by getting it."},{"Start":"04:23.270 ","End":"04:26.205","Text":"We know it now exists,"},{"Start":"04:26.205 ","End":"04:34.205","Text":"so we get it and we store the reference to that object in chosen,"},{"Start":"04:34.205 ","End":"04:35.930","Text":"which we declared up here."},{"Start":"04:35.930 ","End":"04:39.125","Text":"We can now break out the loop as well."},{"Start":"04:39.125 ","End":"04:42.740","Text":"We say valid is true, and if not,"},{"Start":"04:42.740 ","End":"04:47.715","Text":"we need to tell the user what\u0027s gone wrong,"},{"Start":"04:47.715 ","End":"04:51.105","Text":"and then it will just go around the loop."},{"Start":"04:51.105 ","End":"04:53.780","Text":"Then when we do break out of this,"},{"Start":"04:53.780 ","End":"05:00.770","Text":"we can return object reference that we\u0027ve got stored in chosen."},{"Start":"05:00.770 ","End":"05:05.085","Text":"Just to summarize, we keep going around this loop,"},{"Start":"05:05.085 ","End":"05:07.820","Text":"while we don\u0027t have a valid property name entered."},{"Start":"05:07.820 ","End":"05:13.070","Text":"We know if it\u0027s a valid property name because we find a key in"},{"Start":"05:13.070 ","End":"05:19.070","Text":"the collection properties based on whatever the text was that we typed in here."},{"Start":"05:19.070 ","End":"05:21.395","Text":"Then when we do have that key,"},{"Start":"05:21.395 ","End":"05:27.895","Text":"we know we can go and then fetch the object using Get from the properties ArrayList."},{"Start":"05:27.895 ","End":"05:30.465","Text":"In fact, sorry not Arraylist it\u0027s HashMap,"},{"Start":"05:30.465 ","End":"05:32.330","Text":"and we\u0027ve got a reference to it,"},{"Start":"05:32.330 ","End":"05:36.290","Text":"breakout a loop and return that."},{"Start":"05:36.290 ","End":"05:39.385","Text":"Seems to all compile."},{"Start":"05:39.385 ","End":"05:43.300","Text":"Now, to test it as in Part G,"},{"Start":"05:43.300 ","End":"05:47.510","Text":"we can only test it if we\u0027ve actually got some properties."},{"Start":"05:47.510 ","End":"05:50.870","Text":"We can\u0027t really test this on the object workbench."},{"Start":"05:50.870 ","End":"05:56.810","Text":"To test, we\u0027ll have to put some test code in to here."},{"Start":"05:56.810 ","End":"06:01.055","Text":"We\u0027re already in there but into the start method up here,"},{"Start":"06:01.055 ","End":"06:05.540","Text":"and where it\u0027s told us to put a code is just"},{"Start":"06:05.540 ","End":"06:10.865","Text":"below the booking code that we added in the previous part of the exercise."},{"Start":"06:10.865 ","End":"06:13.489","Text":"Here, for example,"},{"Start":"06:13.489 ","End":"06:17.475","Text":"I\u0027ve just taken out print line statement, had the booking."},{"Start":"06:17.475 ","End":"06:20.630","Text":"The reason why I put it here is we already have a reference to"},{"Start":"06:20.630 ","End":"06:24.590","Text":"a hotel stored in this variable here called hotels,"},{"Start":"06:24.590 ","End":"06:27.590","Text":"so why not just make use of [inaudible] test code."},{"Start":"06:27.590 ","End":"06:29.515","Text":"So that\u0027s exactly what we\u0027re gonna do."},{"Start":"06:29.515 ","End":"06:34.350","Text":"We\u0027ll say, and the we call our method."},{"Start":"06:34.350 ","End":"06:39.410","Text":"Off it will go call inputProperty looping around getting input from us,"},{"Start":"06:39.410 ","End":"06:42.500","Text":"rejecting it and if it\u0027s not valid, and if it is valid,"},{"Start":"06:42.500 ","End":"06:47.450","Text":"it will return an object reference to the print line statement,"},{"Start":"06:47.450 ","End":"06:53.930","Text":"which will just basically consume it and print it out using its toString method."},{"Start":"06:53.930 ","End":"06:56.900","Text":"Let\u0027s see if this works."},{"Start":"06:56.900 ","End":"06:59.635","Text":"I\u0027m just going to run the start method,"},{"Start":"06:59.635 ","End":"07:03.545","Text":"and straightway prompts us for the property name."},{"Start":"07:03.545 ","End":"07:12.075","Text":"We\u0027re going to try Notel as we\u0027ve been asked to in Part 1 and hasn\u0027t found that hotel."},{"Start":"07:12.075 ","End":"07:16.970","Text":"We know there should be at this point a Regent Hotel,"},{"Start":"07:16.970 ","End":"07:19.955","Text":"and it does find that and it returns it."},{"Start":"07:19.955 ","End":"07:22.995","Text":"We see here Found displayed,"},{"Start":"07:22.995 ","End":"07:26.780","Text":"and the toString method here is generating this text,"},{"Start":"07:26.780 ","End":"07:31.010","Text":"and then the start method continues on into the code that we\u0027ve already written."},{"Start":"07:31.010 ","End":"07:33.830","Text":"That looks like it works fine,"},{"Start":"07:33.830 ","End":"07:35.560","Text":"and so we\u0027ll break there again,"},{"Start":"07:35.560 ","End":"07:40.070","Text":"and we\u0027ll do a very similar one in the next video to get date."},{"Start":"07:40.070 ","End":"07:42.450","Text":"Thanks for watching. See you soon."}],"ID":31197},{"Watched":false,"Name":"Exercise 5 Part 3","Duration":"8m 10s","ChapterTopicVideoID":29616,"CourseChapterTopicPlaylistID":294565,"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.940","Text":"Hello. Welcome back to the next part of the exercise."},{"Start":"00:02.940 ","End":"00:06.435","Text":"We are now adding a similar method to the previous one,"},{"Start":"00:06.435 ","End":"00:08.100","Text":"but this time called inputDate,"},{"Start":"00:08.100 ","End":"00:15.165","Text":"which has no arguments but returns a reference to an object of type LocalDate as follows."},{"Start":"00:15.165 ","End":"00:17.880","Text":"We firstly prompt the user to type in an integer from"},{"Start":"00:17.880 ","End":"00:22.590","Text":"1-31 and store the input from the user into an integer variable day."},{"Start":"00:22.590 ","End":"00:25.110","Text":"We then prompt the user to type in an integer from"},{"Start":"00:25.110 ","End":"00:30.360","Text":"1-12 and store the input from the user into an integer variable month."},{"Start":"00:30.360 ","End":"00:34.305","Text":"We then prompt the user to type in an integer representing the year"},{"Start":"00:34.305 ","End":"00:38.465","Text":"and store the input from the user into an integer variable year."},{"Start":"00:38.465 ","End":"00:43.160","Text":"We then use a try...catch block to attempt to create a LocalDate object"},{"Start":"00:43.160 ","End":"00:48.680","Text":"using those values typed in and the of method in LocalDate."},{"Start":"00:48.680 ","End":"00:55.680","Text":"If the date created is on or before today\u0027s date as obtained by LocalDate.now,"},{"Start":"00:55.680 ","End":"01:01.100","Text":"we display a suitable message and force the user to type in another date."},{"Start":"01:01.100 ","End":"01:04.520","Text":"In the catch block, we display the error message,"},{"Start":"01:04.520 ","End":"01:06.740","Text":"\"Date is not valid.\""},{"Start":"01:06.740 ","End":"01:13.659","Text":"Otherwise, we return a reference to the newly created LocalDate object."},{"Start":"01:13.659 ","End":"01:18.740","Text":"In Part I, we then test the chooseDate method by creating"},{"Start":"01:18.740 ","End":"01:23.675","Text":"an object from HotelsApp on the object workbench as follows."},{"Start":"01:23.675 ","End":"01:28.550","Text":"Firstly, we check a date of 31-06-22 is"},{"Start":"01:28.550 ","End":"01:33.964","Text":"rejected and an invalid date message is displayed."},{"Start":"01:33.964 ","End":"01:37.220","Text":"We then check a date in the past is rejected,"},{"Start":"01:37.220 ","End":"01:40.940","Text":"and a suitable message is displayed, and finally,"},{"Start":"01:40.940 ","End":"01:47.989","Text":"we check a valid date after today is accepted and returns a LocalDate object."},{"Start":"01:47.989 ","End":"01:52.850","Text":"Create in Part H a new method called inputDate."},{"Start":"01:52.850 ","End":"01:58.850","Text":"It returns LocalDate reference and doesn\u0027t take any parameters."},{"Start":"01:58.850 ","End":"02:05.690","Text":"So what we need to do is have a set of variables to do Parts I-7."},{"Start":"02:05.690 ","End":"02:09.185","Text":"What I need is the same as before,"},{"Start":"02:09.185 ","End":"02:12.925","Text":"a Scanner object, a Boolean flag variable,"},{"Start":"02:12.925 ","End":"02:16.610","Text":"and this time I need some variables to store the day, month,"},{"Start":"02:16.610 ","End":"02:21.320","Text":"and year integers and also the date object that I\u0027m going to create"},{"Start":"02:21.320 ","End":"02:22.610","Text":"and this one here is"},{"Start":"02:22.610 ","End":"02:26.900","Text":"the ultimate object reference that we\u0027re going to return at the moment."},{"Start":"02:26.900 ","End":"02:29.840","Text":"It would be null, I can actually explicitly say that here if I want to,"},{"Start":"02:29.840 ","End":"02:34.940","Text":"but it should have null in it anyway because it hasn\u0027t been initialized to anything yet."},{"Start":"02:34.940 ","End":"02:38.400","Text":"What I\u0027m going to do now is my while loop,"},{"Start":"02:38.400 ","End":"02:42.445","Text":"so while I don\u0027t have a valid input,"},{"Start":"02:42.445 ","End":"02:46.730","Text":"I\u0027m going to keep looping round so I need to get a prompt for"},{"Start":"02:46.730 ","End":"02:52.670","Text":"the day and store that into the day variable."},{"Start":"02:52.670 ","End":"02:54.710","Text":"Because it\u0027s an integer,"},{"Start":"02:54.710 ","End":"02:58.120","Text":"I use nextInt rather than just the next."},{"Start":"02:58.120 ","End":"03:03.950","Text":"I\u0027ll do the same thing for the month and year."},{"Start":"03:03.950 ","End":"03:07.650","Text":"So I\u0027m just going to paste those and change what I\u0027ve got."},{"Start":"03:07.670 ","End":"03:11.715","Text":"So month, it\u0027s going to be 1-12."},{"Start":"03:11.715 ","End":"03:15.000","Text":"Year is going to be anything from the current year."},{"Start":"03:15.000 ","End":"03:17.390","Text":"I\u0027ll just hardcode 2022 in here,"},{"Start":"03:17.390 ","End":"03:20.030","Text":"but you could be cleverer and extract"},{"Start":"03:20.030 ","End":"03:23.930","Text":"the year from the time now and display that there instead,"},{"Start":"03:23.930 ","End":"03:26.960","Text":"but we\u0027ll just keep it simple for now."},{"Start":"03:26.960 ","End":"03:30.395","Text":"We\u0027ve got all of our inputs."},{"Start":"03:30.395 ","End":"03:35.570","Text":"Now we can attempt to create a date object,"},{"Start":"03:35.570 ","End":"03:38.704","Text":"but it can throw an exception,"},{"Start":"03:38.704 ","End":"03:42.330","Text":"so we\u0027re going to need to do a try and catch block."},{"Start":"03:42.430 ","End":"03:50.020","Text":"With catch, you need to say what type of exception is going to be caught."},{"Start":"03:50.240 ","End":"03:53.420","Text":"So we want a DateTimeException."},{"Start":"03:53.420 ","End":"03:56.135","Text":"If it does cause an exception,"},{"Start":"03:56.135 ","End":"03:58.234","Text":"we\u0027re going to print a message saying,"},{"Start":"03:58.234 ","End":"04:00.110","Text":"\"Date is not valid.\""},{"Start":"04:00.110 ","End":"04:03.340","Text":"That\u0027s what it says in Part 6."},{"Start":"04:03.340 ","End":"04:05.735","Text":"Actually, in the try block,"},{"Start":"04:05.735 ","End":"04:08.630","Text":"we\u0027re going to trigger that exception."},{"Start":"04:08.630 ","End":"04:15.335","Text":"We\u0027re going to attempt to create a date using LocalDate.of method,"},{"Start":"04:15.335 ","End":"04:17.810","Text":"which we used in a previous exercise."},{"Start":"04:17.810 ","End":"04:21.845","Text":"So in here, we\u0027re going to put the [inaudible] is important."},{"Start":"04:21.845 ","End":"04:26.585","Text":"It\u0027s year, month, and then day."},{"Start":"04:26.585 ","End":"04:29.750","Text":"So if it can create this object,"},{"Start":"04:29.750 ","End":"04:31.930","Text":"it will put a reference to it into date,"},{"Start":"04:31.930 ","End":"04:35.095","Text":"otherwise, it will trigger an exception."},{"Start":"04:35.095 ","End":"04:40.595","Text":"Then what we also have to do is check,"},{"Start":"04:40.595 ","End":"04:43.580","Text":"as we\u0027ve been asked to in Part 5,"},{"Start":"04:43.580 ","End":"04:49.615","Text":"if the date to create is on or before today\u0027s date because we don\u0027t want that to happen."},{"Start":"04:49.615 ","End":"04:52.500","Text":"We need an \"if\" statement in here."},{"Start":"04:52.500 ","End":"04:57.495","Text":"First of all, I\u0027ll check it\u0027s not null, just to be safe,"},{"Start":"04:57.495 ","End":"05:03.604","Text":"and then what I also want to do is use the isAfter method,"},{"Start":"05:03.604 ","End":"05:09.750","Text":"and I can get today\u0027s date by putting LocalDate.now."},{"Start":"05:10.430 ","End":"05:15.650","Text":"I\u0027m comparing the date returned from this to"},{"Start":"05:15.650 ","End":"05:21.035","Text":"the date we currently have stored in the date object here."},{"Start":"05:21.035 ","End":"05:23.450","Text":"If that is okay,"},{"Start":"05:23.450 ","End":"05:25.235","Text":"if it returns true,"},{"Start":"05:25.235 ","End":"05:29.270","Text":"then I can do something."},{"Start":"05:29.270 ","End":"05:31.130","Text":"I can say valid, basically,"},{"Start":"05:31.130 ","End":"05:32.795","Text":"and I\u0027ll breakout a loop."},{"Start":"05:32.795 ","End":"05:35.045","Text":"Otherwise, I want to print"},{"Start":"05:35.045 ","End":"05:40.805","Text":"an error message and just explain what\u0027s happened to the user."},{"Start":"05:40.805 ","End":"05:44.490","Text":"I\u0027m going to say, \"Date must be in the future.\""},{"Start":"05:45.230 ","End":"05:49.820","Text":"That looks like we\u0027ve done what we\u0027ve been asked to do in"},{"Start":"05:49.820 ","End":"05:57.440","Text":"Part H. The only thing we haven\u0027t done is to actually return the date object itself,"},{"Start":"05:57.440 ","End":"05:59.945","Text":"so the compiler would complain if I don\u0027t do that."},{"Start":"05:59.945 ","End":"06:01.505","Text":"Outside the while loop,"},{"Start":"06:01.505 ","End":"06:03.095","Text":"once we\u0027ve broken out here,"},{"Start":"06:03.095 ","End":"06:05.575","Text":"we can just return date."},{"Start":"06:05.575 ","End":"06:07.910","Text":"So that all seems to compile."},{"Start":"06:07.910 ","End":"06:12.050","Text":"To check that you\u0027ve got java.time.star rather"},{"Start":"06:12.050 ","End":"06:16.310","Text":"than just.localDate because we need the exception to be picked up,"},{"Start":"06:16.310 ","End":"06:18.360","Text":"or it won\u0027t compile."},{"Start":"06:19.550 ","End":"06:25.575","Text":"We can now do Part I by creating"},{"Start":"06:25.575 ","End":"06:33.025","Text":"HotelsApp object and running the inputDate method that we just wrote."},{"Start":"06:33.025 ","End":"06:36.715","Text":"A bit of a typo there. Type in \"dat\" should be date."},{"Start":"06:36.715 ","End":"06:42.655","Text":"So let\u0027s do Part I-1 where it says user date of 31-6,"},{"Start":"06:42.655 ","End":"06:44.500","Text":"so 31 for the day,"},{"Start":"06:44.500 ","End":"06:46.885","Text":"6 for the month, and 22 for the year."},{"Start":"06:46.885 ","End":"06:50.200","Text":"Obviously, there was no 31st day,"},{"Start":"06:50.200 ","End":"06:52.715","Text":"sorry, in June,"},{"Start":"06:52.715 ","End":"06:54.625","Text":"and this should generate an error."},{"Start":"06:54.625 ","End":"06:57.780","Text":"I don\u0027t get an exception thrown because it caught it,"},{"Start":"06:57.780 ","End":"06:59.641","Text":"and just displayed in the message,"},{"Start":"06:59.641 ","End":"07:01.130","Text":"\"Date is not valid.\""},{"Start":"07:01.130 ","End":"07:04.040","Text":"I do get the exception, but it doesn\u0027t stop my program running."},{"Start":"07:04.040 ","End":"07:06.995","Text":"So let\u0027s try the second date,"},{"Start":"07:06.995 ","End":"07:09.720","Text":"a date in the past."},{"Start":"07:11.560 ","End":"07:18.615","Text":"There is a 31st of March, \u002722."},{"Start":"07:18.615 ","End":"07:21.980","Text":"So it\u0027s after the time of today,"},{"Start":"07:21.980 ","End":"07:28.550","Text":"and so the \"Date must be in the future,\" and asking me again to type in a date,"},{"Start":"07:28.550 ","End":"07:32.135","Text":"so let\u0027s try a valid date after today."},{"Start":"07:32.135 ","End":"07:36.725","Text":"So I\u0027ll choose the 30th of June,"},{"Start":"07:36.725 ","End":"07:39.875","Text":"which at the time of writing is in the future,"},{"Start":"07:39.875 ","End":"07:41.705","Text":"and it\u0027s accepted it,"},{"Start":"07:41.705 ","End":"07:44.666","Text":"and it\u0027s thrown me out of this bit now."},{"Start":"07:44.666 ","End":"07:46.490","Text":"The method has finished executing,"},{"Start":"07:46.490 ","End":"07:50.996","Text":"and it\u0027s returned a LocalDate reference."},{"Start":"07:50.996 ","End":"07:52.250","Text":"If I click on here,"},{"Start":"07:52.250 ","End":"07:53.930","Text":"I can see what\u0027s in there,"},{"Start":"07:53.930 ","End":"07:57.970","Text":"and here is my LocalDate fields."},{"Start":"07:57.970 ","End":"08:00.380","Text":"So I\u0027ve got a date returned,"},{"Start":"08:00.380 ","End":"08:03.100","Text":"and I\u0027ve done what I\u0027ve been asked."},{"Start":"08:03.100 ","End":"08:04.610","Text":"So we\u0027ll pause one more time there,"},{"Start":"08:04.610 ","End":"08:07.610","Text":"and we\u0027ll finish off the exercise in the next video."},{"Start":"08:07.610 ","End":"08:09.364","Text":"Thanks very much for watching."},{"Start":"08:09.364 ","End":"08:11.370","Text":"I\u0027ll see you soon."}],"ID":31198},{"Watched":false,"Name":"Exercise 5 Part 4","Duration":"9m 6s","ChapterTopicVideoID":29617,"CourseChapterTopicPlaylistID":294565,"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.520","Text":"Hi again. Welcome back to this final part of the exercise in which we are asked to now"},{"Start":"00:05.520 ","End":"00:11.460","Text":"remove any debug code within the start method from previous exercises in Part J."},{"Start":"00:11.460 ","End":"00:13.260","Text":"We then in Part K,"},{"Start":"00:13.260 ","End":"00:18.195","Text":"add a new method called make booking with no arguments or return values,"},{"Start":"00:18.195 ","End":"00:20.070","Text":"which calls the input property,"},{"Start":"00:20.070 ","End":"00:22.010","Text":"and input date methods."},{"Start":"00:22.010 ","End":"00:27.315","Text":"Then using the inputted data adds a booking to the bookings collection."},{"Start":"00:27.315 ","End":"00:32.175","Text":"We should assume that the booking will always be for a room with 1 bed."},{"Start":"00:32.175 ","End":"00:36.345","Text":"In Part L, we\u0027re told to add another method called list bookings,"},{"Start":"00:36.345 ","End":"00:39.645","Text":"which again returns no values and takes no arguments."},{"Start":"00:39.645 ","End":"00:42.380","Text":"But whose implementation iterates through"},{"Start":"00:42.380 ","End":"00:47.045","Text":"the bookings collection and outputs each booking on a new line."},{"Start":"00:47.045 ","End":"00:48.350","Text":"In Part M,"},{"Start":"00:48.350 ","End":"00:53.705","Text":"we\u0027re told to add in the necessary code to implement book and bookings commands,"},{"Start":"00:53.705 ","End":"00:59.450","Text":"which respectively enable booking and listing of current bookings."},{"Start":"00:59.450 ","End":"01:02.600","Text":"We then make sure in Part 1 that we add"},{"Start":"01:02.600 ","End":"01:06.964","Text":"the command keywords in the command words collection."},{"Start":"01:06.964 ","End":"01:13.100","Text":"We make sure that the commands all have a case in process commands,"},{"Start":"01:13.100 ","End":"01:17.930","Text":"and also that the commands are listed in the command enum."},{"Start":"01:17.930 ","End":"01:23.495","Text":"Finally, we test by adding at least 2 valid bookings for each of the hotels."},{"Start":"01:23.495 ","End":"01:28.025","Text":"Then run bookings each time to see the impact."},{"Start":"01:28.025 ","End":"01:33.965","Text":"Let\u0027s start by removing some of the debug code we\u0027ve left in from previous exercises."},{"Start":"01:33.965 ","End":"01:35.930","Text":"For example, just now,"},{"Start":"01:35.930 ","End":"01:39.440","Text":"we added this line to run"},{"Start":"01:39.440 ","End":"01:46.235","Text":"the input property code and then print a message based on when it was found."},{"Start":"01:46.235 ","End":"01:51.695","Text":"We can probably remove these bookings and then obviously the printout of those bookings,"},{"Start":"01:51.695 ","End":"01:55.260","Text":"we need to keep the hotels here."},{"Start":"01:55.260 ","End":"02:00.275","Text":"We\u0027ll leave that in because they do need to be set up or we can\u0027t book anything."},{"Start":"02:00.275 ","End":"02:06.815","Text":"But we can remove this line here which prints out a list of the properties."},{"Start":"02:06.815 ","End":"02:09.080","Text":"Again, we need to keep our commands in."},{"Start":"02:09.080 ","End":"02:11.015","Text":"That\u0027s an integral part of the program."},{"Start":"02:11.015 ","End":"02:16.875","Text":"All this stuff is needed and the rest of it we\u0027ve taken out."},{"Start":"02:16.875 ","End":"02:18.485","Text":"That\u0027s Part J done."},{"Start":"02:18.485 ","End":"02:22.235","Text":"Now, we\u0027re going to add a new method called make booking,"},{"Start":"02:22.235 ","End":"02:25.340","Text":"which is just going to call some of the things that we\u0027ve already"},{"Start":"02:25.340 ","End":"02:29.300","Text":"created and won\u0027t return anything."},{"Start":"02:29.300 ","End":"02:32.465","Text":"Probably one other case here really, for something,"},{"Start":"02:32.465 ","End":"02:36.890","Text":"maybe to return a Boolean if it was successful or not."},{"Start":"02:36.890 ","End":"02:38.960","Text":"That probably is more sensible,"},{"Start":"02:38.960 ","End":"02:40.925","Text":"but just for time\u0027s sake,"},{"Start":"02:40.925 ","End":"02:43.535","Text":"I\u0027m not doing that."},{"Start":"02:43.535 ","End":"02:46.820","Text":"Make booking is the name of the method."},{"Start":"02:46.820 ","End":"02:52.770","Text":"All it\u0027s going to do is get the date from the user."},{"Start":"02:52.770 ","End":"02:54.780","Text":"We need to store that somewhere."},{"Start":"02:54.780 ","End":"02:58.620","Text":"Let\u0027s store that in a variable date."},{"Start":"02:58.620 ","End":"03:03.230","Text":"We\u0027ll call our input date method that we created in a previous exercise."},{"Start":"03:03.230 ","End":"03:04.880","Text":"There\u0027s the first bit done."},{"Start":"03:04.880 ","End":"03:08.030","Text":"Then we\u0027re going to do the same for property."},{"Start":"03:08.030 ","End":"03:12.615","Text":"We\u0027re going to create a property object."},{"Start":"03:12.615 ","End":"03:17.930","Text":"We\u0027re going to get that from the user because they\u0027re going to type in a name."},{"Start":"03:17.930 ","End":"03:21.410","Text":"But that\u0027s going to return us a property object."},{"Start":"03:21.410 ","End":"03:23.165","Text":"It\u0027s not just a string we want,"},{"Start":"03:23.165 ","End":"03:26.360","Text":"we want the object associated with the string."},{"Start":"03:26.360 ","End":"03:29.255","Text":"That\u0027s the first bit done."},{"Start":"03:29.255 ","End":"03:31.790","Text":"Now, we can actually make the booking,"},{"Start":"03:31.790 ","End":"03:36.995","Text":"which involves adding a booking to the bookings collection."},{"Start":"03:36.995 ","End":"03:40.260","Text":"We\u0027ll first need to create a booking."},{"Start":"03:40.660 ","End":"03:42.935","Text":"I need to store that somewhere."},{"Start":"03:42.935 ","End":"03:45.100","Text":"Let\u0027s do this then."},{"Start":"03:45.100 ","End":"03:49.535","Text":"Just by default, we\u0027re going to do a booking of"},{"Start":"03:49.535 ","End":"03:57.150","Text":"a 1 bedroom property or 1 bedroom room within a property."},{"Start":"03:57.830 ","End":"04:04.785","Text":"That done, and then we can add it to the bookings collection."},{"Start":"04:04.785 ","End":"04:08.705","Text":"That should be it for make booking."},{"Start":"04:08.705 ","End":"04:11.040","Text":"Sorry, it\u0027s booking here."},{"Start":"04:11.040 ","End":"04:14.120","Text":"Booking is the object we just created."},{"Start":"04:14.120 ","End":"04:18.005","Text":"Bookings is the collection of booking"},{"Start":"04:18.005 ","End":"04:23.345","Text":"that we\u0027ve put together earlier on to hold all the bookings."},{"Start":"04:23.345 ","End":"04:29.660","Text":"That should work now and we can move on to Part L,"},{"Start":"04:29.660 ","End":"04:34.595","Text":"which is going to let us see all of the bookings."},{"Start":"04:34.595 ","End":"04:38.840","Text":"That\u0027s called list bookings."},{"Start":"04:38.840 ","End":"04:43.175","Text":"We simply need an enhanced for loop"},{"Start":"04:43.175 ","End":"04:47.090","Text":"that will print each of the bookings within the booking collections."},{"Start":"04:47.090 ","End":"04:49.300","Text":"We\u0027ve done things like this before."},{"Start":"04:49.300 ","End":"04:54.830","Text":"First thing probably is to print a message saying, these are the bookings."},{"Start":"04:54.830 ","End":"04:56.870","Text":"Then we\u0027ll do our loop."},{"Start":"04:56.870 ","End":"04:59.945","Text":"For each type of booking,"},{"Start":"04:59.945 ","End":"05:02.894","Text":"the variable is called booking and"},{"Start":"05:02.894 ","End":"05:06.770","Text":"the bookings collection that we\u0027re going to iterate through."},{"Start":"05:06.770 ","End":"05:08.270","Text":"For each one of those,"},{"Start":"05:08.270 ","End":"05:11.315","Text":"we\u0027re going to just print out the booking,"},{"Start":"05:11.315 ","End":"05:17.715","Text":"which we\u0027ll call the 2 string method of the booking object."},{"Start":"05:17.715 ","End":"05:19.830","Text":"When that\u0027s done."},{"Start":"05:19.830 ","End":"05:21.390","Text":"It will just stop here."},{"Start":"05:21.390 ","End":"05:28.390","Text":"I\u0027ll probably just print a blank line afterwards as well just for neatness sake."},{"Start":"05:28.390 ","End":"05:35.540","Text":"Now, we\u0027ve done our 2 commands and we\u0027ve got to do Part M,"},{"Start":"05:35.540 ","End":"05:41.135","Text":"which is to add in the code to actually put this into the rest of the program."},{"Start":"05:41.135 ","End":"05:43.320","Text":"There\u0027s a few things we need to do."},{"Start":"05:43.320 ","End":"05:46.775","Text":"Part 1, make sure the commands are in the command words collection."},{"Start":"05:46.775 ","End":"05:49.250","Text":"You\u0027ll notice that we\u0027re getting into programming."},{"Start":"05:49.250 ","End":"05:51.665","Text":"Remember, we put the commands,"},{"Start":"05:51.665 ","End":"05:54.920","Text":"the strings that are associated with the various commands into here."},{"Start":"05:54.920 ","End":"05:56.540","Text":"We\u0027ve got book already,"},{"Start":"05:56.540 ","End":"05:59.210","Text":"which is great, but we don\u0027t have bookings."},{"Start":"05:59.210 ","End":"06:01.205","Text":"We need to add that."},{"Start":"06:01.205 ","End":"06:03.560","Text":"The command it should map to,"},{"Start":"06:03.560 ","End":"06:08.640","Text":"presumably command.booking, except it\u0027s,"},{"Start":"06:08.640 ","End":"06:10.695","Text":"sorry got the case wrong there."},{"Start":"06:10.695 ","End":"06:12.610","Text":"That\u0027s that bit."},{"Start":"06:12.610 ","End":"06:18.095","Text":"Still an error and that\u0027s because as we\u0027re prompted in Part 3 of them there,"},{"Start":"06:18.095 ","End":"06:21.815","Text":"we need to also add to the enumeration"},{"Start":"06:21.815 ","End":"06:26.870","Text":"any new commands that we\u0027re adding as valid commands."},{"Start":"06:26.870 ","End":"06:28.520","Text":"We\u0027ve already got book."},{"Start":"06:28.520 ","End":"06:30.200","Text":"We don\u0027t have bookings."},{"Start":"06:30.200 ","End":"06:35.090","Text":"If we recompile that error that we just had a moment ago,"},{"Start":"06:35.090 ","End":"06:36.758","Text":"it should go away,"},{"Start":"06:36.758 ","End":"06:39.165","Text":"bookings is what I should\u0027ve done."},{"Start":"06:39.165 ","End":"06:42.560","Text":"That\u0027s gone. But we\u0027d still have to make sure"},{"Start":"06:42.560 ","End":"06:46.850","Text":"that in our case statement for process and the commands,"},{"Start":"06:46.850 ","End":"06:50.975","Text":"we have all the relevant entries here as well."},{"Start":"06:50.975 ","End":"06:57.215","Text":"We need a case for book which calls make booking,"},{"Start":"06:57.215 ","End":"06:59.060","Text":"we need a break statement here as well,"},{"Start":"06:59.060 ","End":"07:03.130","Text":"so it doesn\u0027t go off the end."},{"Start":"07:03.130 ","End":"07:07.560","Text":"Then it is same thing for bookings."},{"Start":"07:07.560 ","End":"07:13.669","Text":"That\u0027s going to call a list bookings method"},{"Start":"07:13.669 ","End":"07:17.150","Text":"and make sure that doesn\u0027t fall into the default clause."},{"Start":"07:17.150 ","End":"07:19.475","Text":"We put a break in there."},{"Start":"07:19.475 ","End":"07:24.315","Text":"I think that is everything."},{"Start":"07:24.315 ","End":"07:28.295","Text":"We\u0027ve put the what we needed to in the enumeration."},{"Start":"07:28.295 ","End":"07:31.595","Text":"We\u0027ve got cases, we\u0027ve added the commands."},{"Start":"07:31.595 ","End":"07:38.505","Text":"We should now be able to run our program and create bookings from it. Let\u0027s try it."},{"Start":"07:38.505 ","End":"07:44.565","Text":"Straight in with a list of the commands and we can now type a command."},{"Start":"07:44.565 ","End":"07:48.490","Text":"Let\u0027s see what hotels we\u0027ve got that list."},{"Start":"07:48.490 ","End":"07:50.735","Text":"We\u0027ve got those 2 hotels."},{"Start":"07:50.735 ","End":"07:52.735","Text":"Let\u0027s try bookings."},{"Start":"07:52.735 ","End":"07:54.210","Text":"There\u0027s no bookings."},{"Start":"07:54.210 ","End":"07:56.210","Text":"That\u0027s also fine, but you would expect that."},{"Start":"07:56.210 ","End":"07:57.890","Text":"Now, let\u0027s try and book."},{"Start":"07:57.890 ","End":"08:00.020","Text":"We type in a day."},{"Start":"08:00.020 ","End":"08:05.200","Text":"Let\u0027s try June 2023."},{"Start":"08:05.200 ","End":"08:07.200","Text":"We\u0027re going to put in,"},{"Start":"08:07.200 ","End":"08:09.210","Text":"say, the Crummy Inn."},{"Start":"08:09.210 ","End":"08:12.575","Text":"Let\u0027s see if that\u0027s appeared by putting bookings."},{"Start":"08:12.575 ","End":"08:15.260","Text":"Yes, it seems to have brought that up."},{"Start":"08:15.260 ","End":"08:20.085","Text":"Let\u0027s do another one for Regent Hotel."},{"Start":"08:20.085 ","End":"08:24.930","Text":"Book again, use the same date,"},{"Start":"08:24.930 ","End":"08:31.230","Text":"30th 06 2023, Regent Hotel."},{"Start":"08:31.230 ","End":"08:34.560","Text":"Type bookings again."},{"Start":"08:34.560 ","End":"08:37.810","Text":"It seems to put my bookings in."},{"Start":"08:37.810 ","End":"08:42.665","Text":"So that is the end of this extraordinarily long exercise."},{"Start":"08:42.665 ","End":"08:46.070","Text":"But actually, I think you\u0027ll find it was very easy"},{"Start":"08:46.070 ","End":"08:49.775","Text":"to add those last 2 commands because of the way we built,"},{"Start":"08:49.775 ","End":"08:56.580","Text":"everything else and does show we\u0027ve probably got our overall design about right."},{"Start":"08:56.580 ","End":"08:59.380","Text":"But there\u0027s definitely some improvements we can make."},{"Start":"08:59.380 ","End":"09:02.075","Text":"In the final exercise,"},{"Start":"09:02.075 ","End":"09:03.965","Text":"we\u0027ll take a look at those improvements."},{"Start":"09:03.965 ","End":"09:07.620","Text":"Thanks very much for watching and I\u0027ll see you soon."}],"ID":31199},{"Watched":false,"Name":"Exercise 6 Part 1","Duration":"4m 33s","ChapterTopicVideoID":29602,"CourseChapterTopicPlaylistID":294565,"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.950","Text":"Hello, everyone, and welcome to this exercise in which we\u0027re asked to consider"},{"Start":"00:04.950 ","End":"00:07.080","Text":"the hotel application we built in"},{"Start":"00:07.080 ","End":"00:11.760","Text":"the previous exercises and to answer the following questions."},{"Start":"00:11.760 ","End":"00:15.240","Text":"Part A, we\u0027re asked about the core of the program,"},{"Start":"00:15.240 ","End":"00:17.183","Text":"which is in the start method,"},{"Start":"00:17.183 ","End":"00:18.720","Text":"it currently violates 1 of"},{"Start":"00:18.720 ","End":"00:22.935","Text":"the design principles of well-written object-oriented programs."},{"Start":"00:22.935 ","End":"00:25.605","Text":"How could it be improved?"},{"Start":"00:25.605 ","End":"00:27.160","Text":"In part B,"},{"Start":"00:27.160 ","End":"00:30.020","Text":"we\u0027re told that as it stands when making bookings,"},{"Start":"00:30.020 ","End":"00:32.000","Text":"the program doesn\u0027t check whether there\u0027s"},{"Start":"00:32.000 ","End":"00:35.930","Text":"availability on the particular date the booking is made."},{"Start":"00:35.930 ","End":"00:39.820","Text":"How and where can that be addressed?"},{"Start":"00:39.820 ","End":"00:45.020","Text":"Here\u0027s the start method as it was when we finished the previous exercise."},{"Start":"00:45.020 ","End":"00:51.140","Text":"The issue we have with it is there are 3 different things happening in this method."},{"Start":"00:51.140 ","End":"00:56.930","Text":"We\u0027re setting up the properties at the beginning then we\u0027re setting up the commands,"},{"Start":"00:56.930 ","End":"00:59.780","Text":"and then we do this main part which kicks off"},{"Start":"00:59.780 ","End":"01:03.395","Text":"the program and processes the commands that we input."},{"Start":"01:03.395 ","End":"01:09.950","Text":"As we know, a good design principle is that a method should really only perform 1 task."},{"Start":"01:09.950 ","End":"01:11.510","Text":"We\u0027re doing 3 here."},{"Start":"01:11.510 ","End":"01:14.330","Text":"These logically don\u0027t really belong inside the start method."},{"Start":"01:14.330 ","End":"01:18.650","Text":"It would probably make sense to do a very simple re-factoring job on"},{"Start":"01:18.650 ","End":"01:23.330","Text":"this to remove each of these sections into their own methods."},{"Start":"01:23.330 ","End":"01:26.090","Text":"For example, logically it would make sense to move"},{"Start":"01:26.090 ","End":"01:30.285","Text":"this into a method called create properties."},{"Start":"01:30.285 ","End":"01:33.350","Text":"Similarly, it would make sense to move"},{"Start":"01:33.350 ","End":"01:40.480","Text":"the command words part into its own method called create commands."},{"Start":"01:40.480 ","End":"01:45.770","Text":"That will lead to a much more manageable start method."},{"Start":"01:45.770 ","End":"01:47.800","Text":"Looks very short now."},{"Start":"01:47.800 ","End":"01:51.760","Text":"We could call those 2 methods from inside the start method,"},{"Start":"01:51.760 ","End":"01:58.495","Text":"but even better would be to just create a constructor because we haven\u0027t done so far."},{"Start":"01:58.495 ","End":"02:03.080","Text":"1 of the things you can put into a constructor is set up code like this."},{"Start":"02:03.080 ","End":"02:08.190","Text":"We could put create properties in here and similarly,"},{"Start":"02:08.190 ","End":"02:12.760","Text":"we could put create commands words in here."},{"Start":"02:12.760 ","End":"02:17.105","Text":"That would do exactly the same job as we were doing previously,"},{"Start":"02:17.105 ","End":"02:22.630","Text":"but we haven\u0027t violated that principle of only 1 task for a method."},{"Start":"02:22.630 ","End":"02:24.885","Text":"That\u0027s part A."},{"Start":"02:24.885 ","End":"02:29.750","Text":"Part B we\u0027ve been asked to look at this idea that the program doesn\u0027t"},{"Start":"02:29.750 ","End":"02:34.760","Text":"check if there\u0027s any availability on a particular date that the bookings made."},{"Start":"02:34.760 ","End":"02:39.695","Text":"You really shouldn\u0027t allow someone to make a booking when your hotel is already full."},{"Start":"02:39.695 ","End":"02:44.120","Text":"We can get around that fairly simply by extending"},{"Start":"02:44.120 ","End":"02:52.760","Text":"our make booking method to call a routine that checks whether the room is available."},{"Start":"02:52.760 ","End":"02:56.660","Text":"If we add this code at the bottom,"},{"Start":"02:56.660 ","End":"02:59.705","Text":"this is an example, room is available,"},{"Start":"02:59.705 ","End":"03:02.840","Text":"we pass in the date that we\u0027re looking for availability,"},{"Start":"03:02.840 ","End":"03:06.680","Text":"the number of beds we need in the room that we\u0027re trying to book,"},{"Start":"03:06.680 ","End":"03:09.118","Text":"and which property we\u0027ve booked it in,"},{"Start":"03:09.118 ","End":"03:11.930","Text":"and it would just loop through the collection,"},{"Start":"03:11.930 ","End":"03:15.830","Text":"checking that for the property that we\u0027re looking at."},{"Start":"03:15.830 ","End":"03:19.805","Text":"If on that date for that property,"},{"Start":"03:19.805 ","End":"03:24.695","Text":"there is a room booked and the number of rooms"},{"Start":"03:24.695 ","End":"03:29.630","Text":"available with that many beds is greater than the number booked,"},{"Start":"03:29.630 ","End":"03:33.125","Text":"then we\u0027ve still got availability and there\u0027s a room available on that night"},{"Start":"03:33.125 ","End":"03:36.935","Text":"and we can make a booking that night otherwise,"},{"Start":"03:36.935 ","End":"03:41.905","Text":"we return false and it prints no availability on this date."},{"Start":"03:41.905 ","End":"03:46.400","Text":"There\u0027s a few changes you have to make but again, it\u0027s quite elegant."},{"Start":"03:46.400 ","End":"03:49.520","Text":"Make bookings got 1 job to make the booking."},{"Start":"03:49.520 ","End":"03:51.110","Text":"However, to do its job,"},{"Start":"03:51.110 ","End":"03:52.730","Text":"it might need a helper method."},{"Start":"03:52.730 ","End":"03:56.060","Text":"For example, to see whether a room is available even"},{"Start":"03:56.060 ","End":"03:59.555","Text":"that might need a further helper method or 2."},{"Start":"03:59.555 ","End":"04:03.315","Text":"You\u0027ll notice there\u0027s 1 here called get number rooms with beds."},{"Start":"04:03.315 ","End":"04:06.020","Text":"We\u0027re trying to find out how many 1 bedrooms there are or"},{"Start":"04:06.020 ","End":"04:08.840","Text":"how many 2 bedrooms there are, and so on."},{"Start":"04:08.840 ","End":"04:11.960","Text":"That will be off in the property class because"},{"Start":"04:11.960 ","End":"04:15.280","Text":"that logically belongs in the properties class."},{"Start":"04:15.280 ","End":"04:19.350","Text":"There it is just as a count of how many rooms have that many beds."},{"Start":"04:19.350 ","End":"04:21.770","Text":"Between the 3 of these methods,"},{"Start":"04:21.770 ","End":"04:24.060","Text":"we can solve this problem,"},{"Start":"04:24.060 ","End":"04:26.180","Text":"we\u0027ve been asked to in part B."},{"Start":"04:26.180 ","End":"04:28.640","Text":"That\u0027s it for this one, we\u0027ll come back for"},{"Start":"04:28.640 ","End":"04:31.685","Text":"part C and part D in a moment in the next video."},{"Start":"04:31.685 ","End":"04:33.840","Text":"Thanks for watching. See you then."}],"ID":31200},{"Watched":false,"Name":"Exercise 6 Part 2","Duration":"4m 19s","ChapterTopicVideoID":29603,"CourseChapterTopicPlaylistID":294565,"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.000","Text":"Hello again. Continuing on from the previous exercise,"},{"Start":"00:04.000 ","End":"00:06.975","Text":"we\u0027re asked in Part C about"},{"Start":"00:06.975 ","End":"00:12.210","Text":"an important entity which is missing from the design for this application."},{"Start":"00:12.210 ","End":"00:14.280","Text":"We\u0027re asked what it is."},{"Start":"00:14.280 ","End":"00:15.990","Text":"Then in Part D,"},{"Start":"00:15.990 ","End":"00:19.230","Text":"we\u0027re told that check-in involves allocating a room to"},{"Start":"00:19.230 ","End":"00:22.905","Text":"a guest who has made a booking and arrives at the hotel."},{"Start":"00:22.905 ","End":"00:26.745","Text":"The room number is given to them based on the type of room they booked,"},{"Start":"00:26.745 ","End":"00:28.620","Text":"how many beds that room has,"},{"Start":"00:28.620 ","End":"00:31.905","Text":"and which rooms are available at the time they check in."},{"Start":"00:31.905 ","End":"00:36.450","Text":"We\u0027re asked how might this check-in routine work."},{"Start":"00:36.450 ","End":"00:40.590","Text":"Part C asks us what\u0027s missing."},{"Start":"00:40.590 ","End":"00:46.310","Text":"It should be fairly obvious that the entity that\u0027s missing is the client or customer."},{"Start":"00:46.310 ","End":"00:51.290","Text":"We can\u0027t make a booking in thin air without a customer being involved."},{"Start":"00:51.290 ","End":"00:52.700","Text":"Who is the booking for?"},{"Start":"00:52.700 ","End":"00:55.960","Text":"Who will check in when they arrive?"},{"Start":"00:55.960 ","End":"00:59.630","Text":"We know we have entities for properties of rooms."},{"Start":"00:59.630 ","End":"01:02.375","Text":"The entity for a client is missing,"},{"Start":"01:02.375 ","End":"01:05.120","Text":"and that would be associated with booking."},{"Start":"01:05.120 ","End":"01:07.160","Text":"The sorts of things you\u0027re going to have in"},{"Start":"01:07.160 ","End":"01:10.700","Text":"a client class would be obviously the client\u0027s name,"},{"Start":"01:10.700 ","End":"01:13.460","Text":"their contact details like their email address or telephone,"},{"Start":"01:13.460 ","End":"01:15.200","Text":"should be fairly obvious,"},{"Start":"01:15.200 ","End":"01:19.385","Text":"but it will be very similar to the room type thing."},{"Start":"01:19.385 ","End":"01:24.215","Text":"It\u0027s going to have attributes and not many methods apart from getting set methods,"},{"Start":"01:24.215 ","End":"01:27.470","Text":"we\u0027re just using it as a container for clients,"},{"Start":"01:27.470 ","End":"01:30.290","Text":"and we\u0027d add that then to the booking."},{"Start":"01:30.290 ","End":"01:31.895","Text":"When a booking is made,"},{"Start":"01:31.895 ","End":"01:35.180","Text":"we don\u0027t just add the date and the time,"},{"Start":"01:35.180 ","End":"01:39.620","Text":"we add a client associated with that booking as well."},{"Start":"01:39.620 ","End":"01:44.750","Text":"That\u0027s Part C. Part D says that check-in would involve"},{"Start":"01:44.750 ","End":"01:49.705","Text":"allocating a room to a client who\u0027s made a booking and arrives at the hotel."},{"Start":"01:49.705 ","End":"01:54.050","Text":"Then a room number will be given to them based on the type of room that they\u0027ve booked."},{"Start":"01:54.050 ","End":"01:55.880","Text":"However, many beds it has,"},{"Start":"01:55.880 ","End":"01:58.250","Text":"and whatever room is free,"},{"Start":"01:58.250 ","End":"02:01.100","Text":"you will be allocated that room number."},{"Start":"02:01.100 ","End":"02:07.095","Text":"That code would look something like the following check-in."},{"Start":"02:07.095 ","End":"02:09.935","Text":"Obviously when you check in, you need a date,"},{"Start":"02:09.935 ","End":"02:12.410","Text":"Today\u0027s date that we\u0027re interested in when you\u0027re checking,"},{"Start":"02:12.410 ","End":"02:13.580","Text":"you can\u0027t check in tomorrow,"},{"Start":"02:13.580 ","End":"02:14.975","Text":"you check in today."},{"Start":"02:14.975 ","End":"02:17.435","Text":"We get the date and store that."},{"Start":"02:17.435 ","End":"02:21.325","Text":"We\u0027d find out what property is being checked into."},{"Start":"02:21.325 ","End":"02:23.995","Text":"Then we\u0027d go and look for a free room."},{"Start":"02:23.995 ","End":"02:28.175","Text":"You\u0027d need to write a method called find free room,"},{"Start":"02:28.175 ","End":"02:31.370","Text":"which would iterate through the collection,"},{"Start":"02:31.370 ","End":"02:34.265","Text":"finding a room that hasn\u0027t been booked."},{"Start":"02:34.265 ","End":"02:39.340","Text":"You\u0027d need to add an extra property called occupied."},{"Start":"02:39.340 ","End":"02:42.400","Text":"That would be an attribute within a room."},{"Start":"02:42.400 ","End":"02:44.870","Text":"Then, when someone checks in,"},{"Start":"02:44.870 ","End":"02:47.390","Text":"you set that attribute to true,"},{"Start":"02:47.390 ","End":"02:50.555","Text":"and you can return a Boolean value rule saying,"},{"Start":"02:50.555 ","End":"02:52.715","Text":"we\u0027ve successfully checked someone in."},{"Start":"02:52.715 ","End":"02:55.385","Text":"Otherwise, we say we weren\u0027t able to check them in,"},{"Start":"02:55.385 ","End":"02:58.280","Text":"presumably because someone\u0027s already checked into"},{"Start":"02:58.280 ","End":"03:01.580","Text":"this room and check out would be the reverse."},{"Start":"03:01.580 ","End":"03:04.760","Text":"We could also then provide a method called is occupied,"},{"Start":"03:04.760 ","End":"03:08.325","Text":"which returns the occupied method."},{"Start":"03:08.325 ","End":"03:12.300","Text":"Going back to the check-in routine,"},{"Start":"03:12.300 ","End":"03:16.220","Text":"we basically try and find a free room,"},{"Start":"03:16.220 ","End":"03:19.040","Text":"and then we\u0027ll get a reference to that room in there."},{"Start":"03:19.040 ","End":"03:21.050","Text":"If that room is not null,"},{"Start":"03:21.050 ","End":"03:23.030","Text":"we try and check into that room,"},{"Start":"03:23.030 ","End":"03:26.465","Text":"and we print the room number by getting the room number."},{"Start":"03:26.465 ","End":"03:29.075","Text":"Otherwise, we print an error message."},{"Start":"03:29.075 ","End":"03:32.330","Text":"Because you should only be able to make, say,"},{"Start":"03:32.330 ","End":"03:38.090","Text":"3 bookings for a hotel that only has 3 rooms with 1 bed,"},{"Start":"03:38.090 ","End":"03:41.325","Text":"you shouldn\u0027t get the problem of trying to check in"},{"Start":"03:41.325 ","End":"03:45.540","Text":"4 people because there shouldn\u0027t be 4 bookings on that day."},{"Start":"03:45.540 ","End":"03:47.880","Text":"If we\u0027ve written the rest of our code well."},{"Start":"03:47.880 ","End":"03:51.790","Text":"This is literally all we need to do is provide those extra methods."},{"Start":"03:51.790 ","End":"03:56.070","Text":"Again, I think it shows you the flexibility of the application."},{"Sta