WEBVTT 1 00:00:02.110 --> 00:00:04.114 In a previous demo I should you how to create 2 00:00:04.116 --> 00:00:06.001 two objects of the same type 3 00:00:06.005 --> 00:00:07.225 two Bicycle objects. 4 00:00:08.003 --> 00:00:10.001 In this demo I'm going to show you how to create 5 00:00:10.002 --> 00:00:11.116 two different kinds of objects 6 00:00:11.220 --> 00:00:13.119 a Bicycle and Rider object 7 00:00:13.229 --> 00:00:16.009 and how to make those work together. 8 00:00:17.119 --> 00:00:20.116 First I'm going to create a Bicycle object as I did before 9 00:00:21.005 --> 00:00:23.117 not that these menu items called constructors of the 10 00:00:23.118 --> 00:00:25.006 Bicycle class, a constructor 11 00:00:25.009 --> 00:00:28.003 is rather like a method, except that it runs only once 12 00:00:28.005 --> 00:00:30.119 at a point when an object is being created 13 00:00:30.227 --> 00:00:35.000 and it belongs to the class rather than to a particular object. 14 00:00:35.223 --> 00:00:37.221 So as before I'll enter 15 00:00:37.222 --> 00:00:39.228 parameters which provide values for 16 00:00:39.229 --> 00:00:42.227 the wheelDiameter and the numberOfGears 17 00:00:43.007 --> 00:00:45.115 click Ok and the Bicycle object 18 00:00:45.116 --> 00:00:47.226 appears in the object bench. 19 00:00:48.112 --> 00:00:50.007 now I'm going to create a Rider object 20 00:00:50.112 --> 00:00:53.000 I right click on the class and choose a constructor 21 00:00:53.009 --> 00:00:55.112 note that the Rider class has two constructors 22 00:00:55.112 --> 00:00:57.116 and the menu tells us, the first one 23 00:00:57.221 --> 00:00:59.113 has one parameter which means one 24 00:00:59.114 --> 00:01:01.116 piece of information needs to be entered 25 00:01:01.222 --> 00:01:03.229 the second one has two parameters 26 00:01:04.113 --> 00:01:07.005 so I'm going to click on the second constructor 27 00:01:07.112 --> 00:01:09.114 and the dialog box 28 00:01:09.117 --> 00:01:13.006 then prompts me to enter two pieces of information. 29 00:01:14.007 --> 00:01:17.002 The first parameter is a name for this Rider object 30 00:01:17.220 --> 00:01:19.223 and that parameter is of type string 31 00:01:19.224 --> 00:01:22.002 so I need to enter a string in this box 32 00:01:22.009 --> 00:01:23.117 when you enter a string you need 33 00:01:23.118 --> 00:01:26.110 to wrap that in double quotes 34 00:01:27.000 --> 00:01:30.224 so lets give this rider a name 35 00:01:31.007 --> 00:01:33.002 so whats they type of the other parameter 36 00:01:33.007 --> 00:01:35.001 what type is Bicycle 37 00:01:35.113 --> 00:01:38.000 so that means it needs to be a Bicycle object 38 00:01:38.007 --> 00:01:40.008 so I need to put an object in here 39 00:01:40.222 --> 00:01:43.000 well actually I need to put in something that 40 00:01:43.001 --> 00:01:46.007 identifies the Bicycle object that i want this Rider 41 00:01:46.111 --> 00:01:48.114 to be riding. 42 00:01:48.229 --> 00:01:50.222 So I have one Bicycle object here 43 00:01:50.226 --> 00:01:52.007 the one I created earlier 44 00:01:52.115 --> 00:01:53.228 called bicycle one 45 00:01:54.008 --> 00:01:55.227 so I could just type in 46 00:01:56.002 --> 00:01:59.000 the name, the instance name bicycle one 47 00:01:59.117 --> 00:02:01.227 even simplier I can just click 48 00:02:01.228 --> 00:02:04.228 on that object in the object bench in BlueJ 49 00:02:05.004 --> 00:02:08.006 pops that name into the parameter box 50 00:02:08.116 --> 00:02:10.224 and now I can click Ok 51 00:02:11.003 --> 00:02:12.009 and a Rider object 52 00:02:12.113 --> 00:02:15.007 is created and appears in the object bench 53 00:02:15.113 --> 00:02:17.114 I now have two objects in the object bench 54 00:02:17.115 --> 00:02:19.118 that are somehow related to each other 55 00:02:19.228 --> 00:02:23.006 that Rider object is riding that Bicycle object. 56 00:02:23.119 --> 00:02:25.111 Lets see if we can convince ourselves 57 00:02:25.112 --> 00:02:27.111 that this is the case 58 00:02:27.228 --> 00:02:29.223 if I insect the Bicycle object 59 00:02:29.224 --> 00:02:32.229 we can see its properties and see that pedalRpm 60 00:02:33.002 --> 00:02:36.115 for this Bicycle object is currently zero. 61 00:02:37.004 --> 00:02:41.221 I can now call one of the methods of the Rider object 62 00:02:42.001 --> 00:02:45.223 here's its methods Rider has a method called speedUp 63 00:02:46.111 --> 00:02:49.007 which should make the Bicycle that it is riding 64 00:02:49.116 --> 00:02:51.224 speed up, so lets try that 65 00:02:51.229 --> 00:02:54.009 nothing much obviously happens there 66 00:02:54.224 --> 00:02:58.110 lets go back to the Bicycle object and inspect it again 67 00:02:59.113 --> 00:03:02.224 and we can see that pedalRpm has increased to fifty 68 00:03:03.001 --> 00:03:05.226 so calling a method of that Rider object 69 00:03:06.003 --> 00:03:08.008 has had an effect, its changed one of the 70 00:03:08.009 --> 00:03:11.110 properties of that Bicycle object. 71 00:03:12.008 --> 00:03:14.009 That demonstrates the relationship between these 72 00:03:14.110 --> 00:03:16.227 two objects the rider wants to speed up 73 00:03:17.005 --> 00:03:19.000 so how can this happen, well the rider 74 00:03:19.003 --> 00:03:21.002 can pedal the bicycle faster 75 00:03:21.111 --> 00:03:26.005 which in turn changes the pedalRpm property of the bicycle. 76 00:03:27.006 --> 00:03:30.004 Now I'm going to create another Bicycle object 77 00:03:31.223 --> 00:03:34.223 as before call a constructor and enter some 78 00:03:34.226 --> 00:03:36.226 values for the parameters 79 00:03:37.110 --> 00:03:41.224 and that creates a second Bicycle object in the object bench. 80 00:03:43.004 --> 00:03:44.220 Now I'm going to make this rider 81 00:03:44.221 --> 00:03:46.222 change from riding bicycle one 82 00:03:47.111 --> 00:03:49.005 to riding bicycle two 83 00:03:49.228 --> 00:03:53.223 the Rider object has a method called changeBicycle 84 00:03:54.118 --> 00:03:56.110 so lets call that 85 00:03:57.002 --> 00:03:59.112 and the method call dialog 86 00:03:59.116 --> 00:04:02.221 asks for a parameter of type Bicycle 87 00:04:03.005 --> 00:04:05.117 and as before that needs to be something that 88 00:04:05.118 --> 00:04:07.220 identifies the Bicycle object 89 00:04:07.224 --> 00:04:09.227 that I want to change to 90 00:04:10.116 --> 00:04:12.000 and I can just click on the 91 00:04:12.001 --> 00:04:14.004 new Bicycle object in the object bench 92 00:04:14.009 --> 00:04:17.221 and that puts the instance name into this dialog. 93 00:04:18.002 --> 00:04:21.221 I can click Ok and now rider one 94 00:04:21.228 --> 00:04:24.004 is riding bicycle two 95 00:04:24.112 --> 00:04:25.227 to demonstrate this 96 00:04:26.111 --> 00:04:28.227 inspect bicycle two 97 00:04:29.229 --> 00:04:33.115 note pedalRpm is currently zero 98 00:04:34.113 --> 00:04:37.002 close that then call 99 00:04:37.006 --> 00:04:39.114 speedUp and Rider 100 00:04:39.228 --> 00:04:43.115 and inspect bicycle two and bicycle two 101 00:04:43.225 --> 00:04:46.114 has now had its pedalRpm property change 102 00:04:46.117 --> 00:04:48.009 so that relationship now 103 00:04:48.222 --> 00:04:51.118 is between rider one and bicycle two.