1
00:00:05,460 --> 00:00:07,620
alright so the changes we made at the

2
00:00:07,620 --> 00:00:09,930
end of the last video enable us to send

3
00:00:09,930 --> 00:00:11,550
the output to a file if you want to

4
00:00:11,550 --> 00:00:13,640
using the technique we saw back in

5
00:00:13,640 --> 00:00:15,720
Section 10 when we were writing to

6
00:00:15,720 --> 00:00:16,830
files

7
00:00:16,830 --> 00:00:18,380
alright so let's make sure that it still

8
00:00:18,380 --> 00:00:20,000
works and that it looks the same as it did

9
00:00:20,000 --> 00:00:22,310
before so now we can send the output to

10
00:00:22,310 --> 00:00:24,320
a file and see how it displays in a

11
00:00:24,320 --> 00:00:26,370
browser so lets actually do that so what

12
00:00:26,370 --> 00:00:29,040
we're going to do is after the

13
00:00:29,040 --> 00:00:32,240
tag the paragraph tag we're going to do

14
00:00:32,240 --> 00:00:39,330
....

15
00:00:39,330 --> 00:00:45,330
....

16
00:00:45,330 --> 00:00:48,030
...

17
00:00:48,030 --> 00:00:51,240
....

18
00:00:51,240 --> 00:00:54,830
...

19
00:00:54,830 --> 00:01:00,830
....

20
00:01:00,830 --> 00:01:02,780
and we'll just open the project pane so

21
00:01:02,780 --> 00:01:03,810
we can see that the file gets

22
00:01:03,810 --> 00:01:05,990
created for us now if you are using

23
00:01:05,990 --> 00:01:07,620
linux or mac do make sure you add the

24
00:01:07,620 --> 00:01:10,380
dot HTML file extension even if you

25
00:01:10,380 --> 00:01:11,540
wouldn't normally do that and I'll

26
00:01:11,540 --> 00:01:13,170
explain why in a minute

27
00:01:13,170 --> 00:01:14,750
now we can send the output of the

28
00:01:14,750 --> 00:01:17,160
display method to a file by adding test

29
00:01:17,160 --> 00:01:20,100
_doc as the named perimeter in the

30
00:01:20,100 --> 00:01:22,350
call to display and you saw me do that on line

31
00:01:22,350 --> 00:01:27,540
69 so let's just run this you can

32
00:01:27,540 --> 00:01:29,490
see that was very quick and then the

33
00:01:29,490 --> 00:01:33,090
file test.HTML appeared now intelliJ

34
00:01:33,090 --> 00:01:35,390
understands about HTML so i can

35
00:01:35,390 --> 00:01:36,810
right-click the test file in the project

36
00:01:36,810 --> 00:01:41,100
pane the context menu their is an open in

37
00:01:41,100 --> 00:01:44,280
browser down here and that's why I

38
00:01:44,280 --> 00:01:45,660
mentioned that you should include the

39
00:01:45,660 --> 00:01:48,180
file extension linux and osx don't

40
00:01:48,180 --> 00:01:49,980
rely on the file extension to decide

41
00:01:49,980 --> 00:01:51,500
what to do with the file but

42
00:01:51,500 --> 00:01:53,030
intelliJ has adopted the windows

43
00:01:53,030 --> 00:01:54,860
approach and does use the file extension

44
00:01:54,860 --> 00:01:57,840
so if the file doesn't have a dot HTM or

45
00:01:57,840 --> 00:02:00,600
dot HTML extension then you won't get

46
00:02:00,600 --> 00:02:02,570
the option to open in your browser

47
00:02:02,570 --> 00:02:04,820
alright so in my case though i'm going

48
00:02:04,820 --> 00:02:06,140
to choose chrome which is the one that I

49
00:02:06,140 --> 00:02:07,610
normally do when i'm recording videos

50
00:02:07,610 --> 00:02:09,860
you can choose the default

51
00:02:09,860 --> 00:02:12,720
browser and we can see that it's

52
00:02:12,720 --> 00:02:14,220
actually popped up now and it's working

53
00:02:14,220 --> 00:02:15,330
quite nicely

54
00:02:15,330 --> 00:02:18,240
alright so that's composition now we've

55
00:02:18,240 --> 00:02:19,060
used both

56
00:02:19,060 --> 00:02:21,340
composition and inheritance in this program

57
00:02:21,340 --> 00:02:23,320
so it's a good example of the

58
00:02:23,320 --> 00:02:25,300
differences between them and when you

59
00:02:25,300 --> 00:02:28,150
use each one so getting back to our code

60
00:02:28,150 --> 00:02:34,090
now our basic classes all inherit from

61
00:02:34,090 --> 00:02:35,920
tagged and when we talk about head for

62
00:02:35,920 --> 00:02:39,090
example would say that head is a tag

63
00:02:39,090 --> 00:02:41,920
similarly body is a tag and so is

64
00:02:41,920 --> 00:02:44,260
doctype although doc type are slightly

65
00:02:44,260 --> 00:02:46,150
different kind of tag it doesn't have a

66
00:02:46,150 --> 00:02:48,430
corresponding end tag so whenever you

67
00:02:48,430 --> 00:02:49,870
have a relationship that can be

68
00:02:49,870 --> 00:02:51,900
described using is a that's an

69
00:02:51,900 --> 00:02:53,110
indication that you should consider

70
00:02:53,110 --> 00:02:55,900
using inheritance to model the objects

71
00:02:55,900 --> 00:02:57,870
composition is used when the

72
00:02:57,870 --> 00:03:00,700
relationship is described as a has a a

73
00:03:00,700 --> 00:03:03,930
document has a doctype and has a body so

74
00:03:03,930 --> 00:03:05,860
composition is more appropriate to model

75
00:03:05,860 --> 00:03:06,970
the document

76
00:03:06,970 --> 00:03:08,430
ok there's another example of

77
00:03:08,430 --> 00:03:10,270
composition being used in our body class

78
00:03:10,270 --> 00:03:13,030
so let's have a look at that HTML

79
00:03:13,030 --> 00:03:14,620
doc object has to contain a

80
00:03:14,620 --> 00:03:18,090
doctype a head tag and a body tag so it's

81
00:03:18,090 --> 00:03:20,500
effectively not an HTML document without

82
00:03:20,500 --> 00:03:21,220
them

83
00:03:21,220 --> 00:03:23,340
so these three components are created

84
00:03:23,340 --> 00:03:25,120
when each instance of the class is

85
00:03:25,120 --> 00:03:28,590
created in the init method you can see

86
00:03:28,590 --> 00:03:29,920
its doing that down here starting on line

87
00:03:29,920 --> 00:03:33,210
47 now body object has any number of

88
00:03:33,210 --> 00:03:36,240
tags and maybe empty that would make for

89
00:03:36,240 --> 00:03:38,320
a very interesting document but it would

90
00:03:38,320 --> 00:03:41,340
actually conform to the specification so

91
00:03:41,340 --> 00:03:43,210
we don't create any objects when body

92
00:03:43,210 --> 00:03:45,550
instances are created instead we just

93
00:03:45,550 --> 00:03:47,860
create an empty list ready to contain

94
00:03:47,860 --> 00:03:50,890
the tags when they're added you can see

95
00:03:50,890 --> 00:03:53,260
that code for that here on line 32 now

96
00:03:53,260 --> 00:03:55,240
when a tag is added to the body by

97
00:03:55,240 --> 00:03:57,040
calling the body classes add_

98
00:03:57,040 --> 00:04:00,120
tag method a new tag objects created then

99
00:04:00,120 --> 00:04:02,800
appended to the list now this is also

100
00:04:02,800 --> 00:04:05,620
composition but now the body class has

101
00:04:05,620 --> 00:04:08,400
its own attributes isn't just composed

102
00:04:08,400 --> 00:04:10,150
of its composite classes

103
00:04:10,150 --> 00:04:11,860
it has an underscore body _

104
00:04:11,860 --> 00:04:14,460
contents data attribute and a method

105
00:04:14,460 --> 00:04:16,329
called add underscore tag

106
00:04:16,329 --> 00:04:18,089
alright so at this point anything else

107
00:04:18,089 --> 00:04:20,260
to say about this wil all be repetition

108
00:04:20,260 --> 00:04:22,650
so it's time for a challenge to

109
00:04:22,650 --> 00:04:24,730
consolidate what we covered to give you

110
00:04:24,730 --> 00:04:27,300
a bit of a chance to apply it at the

111
00:04:27,300 --> 00:04:29,670
moment ahead class doesn't create a

112
00:04:29,670 --> 00:04:31,990
document header with anything in it

113
00:04:31,990 --> 00:04:33,760
so your challenge is to modify the

114
00:04:33,760 --> 00:04:36,310
program so that the head class can

115
00:04:36,310 --> 00:04:39,430
include a title tag and your HTML should

116
00:04:39,430 --> 00:04:40,690
look something like this

117
00:04:40,690 --> 00:04:42,400
although there's no need to include

118
00:04:42,400 --> 00:04:44,320
line-breaks indenting I've just done

119
00:04:44,320 --> 00:04:46,480
that to make it easy to read but the

120
00:04:46,480 --> 00:04:48,670
text for the title will be passed to the

121
00:04:48,670 --> 00:04:51,220
html dot classes init method where

122
00:04:51,220 --> 00:04:53,500
the documents created alright so that's

123
00:04:53,500 --> 00:04:55,600
the challenge see how you go with that

124
00:04:55,600 --> 00:04:57,400
pause the video and I'll see you when

125
00:04:57,400 --> 00:05:01,870
you get back ok so the basic approach

126
00:05:01,870 --> 00:05:03,640
this challenge will probably be the same

127
00:05:03,640 --> 00:05:06,010
for all solutions but you may well have

128
00:05:06,010 --> 00:05:07,600
a different implementation to my

129
00:05:07,600 --> 00:05:10,090
solution as always that's fine

130
00:05:10,090 --> 00:05:12,970
the real test is does it work if you get

131
00:05:12,970 --> 00:05:14,650
the correct HTML produced then

132
00:05:14,650 --> 00:05:15,820
congratulations

133
00:05:15,820 --> 00:05:18,820
alright so if you read through the basic

134
00:05:18,820 --> 00:05:21,850
HTML structure that we looked in the

135
00:05:21,850 --> 00:05:23,740
browser earlier you may have noticed

136
00:05:23,740 --> 00:05:25,000
that the head can contain a few

137
00:05:25,000 --> 00:05:27,160
different tags so you may have made the

138
00:05:27,160 --> 00:05:30,700
head class behave like a body class with

139
00:05:30,700 --> 00:05:32,770
an add_tag method to add new content

140
00:05:32,770 --> 00:05:35,350
to the head element that's fine it's

141
00:05:35,350 --> 00:05:37,360
probably the way you do things so that

142
00:05:37,360 --> 00:05:40,090
the program correctly reflects the HTML

143
00:05:40,090 --> 00:05:42,730
specification the challenge we asked for

144
00:05:42,730 --> 00:05:45,220
a title so for my solution i'm just

145
00:05:45,220 --> 00:05:47,260
going to add a title and nothing more

146
00:05:47,260 --> 00:05:48,880
so the first step is to modify the head

147
00:05:48,880 --> 00:05:53,200
class and we need to modify the init

148
00:05:53,200 --> 00:05:55,540
method to take another argument which

149
00:05:55,540 --> 00:05:57,550
effectively will be the title going to

150
00:05:57,550 --> 00:06:00,850
come up here we're going to put....

151
00:06:00,850 --> 00:06:05,500
....

152
00:06:05,500 --> 00:06:09,160
...

153
00:06:09,160 --> 00:06:13,270
...

154
00:06:13,270 --> 00:06:16,480
....

155
00:06:16,480 --> 00:06:17,740
...

156
00:06:17,740 --> 00:06:20,080
...

157
00:06:20,080 --> 00:06:22,180
....

158
00:06:22,180 --> 00:06:27,340
...

159
00:06:27,340 --> 00:06:31,180
....so this creates a new

160
00:06:31,180 --> 00:06:34,390
tag with the name title and the contents

161
00:06:34,390 --> 00:06:36,400
being the text that was passed in to the

162
00:06:36,400 --> 00:06:39,070
init method now the head contents are

163
00:06:39,070 --> 00:06:41,200
then set to this the string

164
00:06:41,200 --> 00:06:43,450
representation of this new tag now we

165
00:06:43,450 --> 00:06:45,380
can improve that slightly

166
00:06:45,380 --> 00:06:47,170
if the title doesn't contain any text

167
00:06:47,170 --> 00:06:48,770
then we really don't want an empty tag

168
00:06:48,770 --> 00:06:51,580
inside the head so we can give the title

169
00:06:51,580 --> 00:06:53,830
parameter a default value than test

170
00:06:53,830 --> 00:06:56,330
this before creating the tag

171
00:06:56,330 --> 00:06:57,980
let's go ahead and do that i'm going to

172
00:06:57,980 --> 00:07:02,450
put....

173
00:07:02,450 --> 00:07:03,730
...

174
00:07:03,730 --> 00:07:14,360
...

175
00:07:14,360 --> 00:07:15,800
alright next thing we need to do is

176
00:07:15,800 --> 00:07:19,580
modify the HTML dot class now first it

177
00:07:19,580 --> 00:07:21,700
needs a parameter to the init method to

178
00:07:21,700 --> 00:07:24,550
accept the title and we'll use a named

179
00:07:24,550 --> 00:07:26,110
parameter with the default value here as

180
00:07:26,110 --> 00:07:28,790
well that way a document can be created

181
00:07:28,790 --> 00:07:32,270
without providing a title so come

182
00:07:32,270 --> 00:07:37,190
down to the html doc the init Method...

183
00:07:37,190 --> 00:07:39,500
...

184
00:07:39,500 --> 00:07:44,800
...

185
00:07:44,800 --> 00:07:48,790
...now the html .

186
00:07:48,790 --> 00:07:50,420
class doesn't have to do anything with

187
00:07:50,420 --> 00:07:52,600
the title it's just passing it on to the

188
00:07:52,600 --> 00:07:54,610
head class when it creates a new head

189
00:07:54,610 --> 00:07:56,720
object and that should be all we need to

190
00:07:56,720 --> 00:07:59,270
do I'm going to start by making sure we haven't

191
00:07:59,270 --> 00:08:00,890
broken anything so i'm going to run the

192
00:08:00,890 --> 00:08:05,140
program if we don't got any errors that is a

193
00:08:05,140 --> 00:08:07,580
good sign and what i can do is open the

194
00:08:07,580 --> 00:08:09,580
project pane I can also double-click

195
00:08:09,580 --> 00:08:11,980
that if i wanted to open the HTML file

196
00:08:11,980 --> 00:08:13,670
in the default browser but I'm going to

197
00:08:13,670 --> 00:08:15,980
right click it open a browser and choose

198
00:08:15,980 --> 00:08:19,910
chrome again that's it opening up now

199
00:08:19,910 --> 00:08:22,580
in a new tab if we have a look at the

200
00:08:22,580 --> 00:08:26,390
HTML so far so good we haven't past a title

201
00:08:26,390 --> 00:08:28,190
in so we haven't got a title and the

202
00:08:28,190 --> 00:08:29,950
head tags up there on line three

203
00:08:29,950 --> 00:08:32,830
so that's good that's working so let's

204
00:08:32,830 --> 00:08:34,730
see now if it works when we do passing a

205
00:08:34,730 --> 00:08:36,549
title and i'll provide that when we

206
00:08:36,549 --> 00:08:39,789
create the HTML doc instance go back

207
00:08:39,789 --> 00:08:40,700
to the code again

208
00:08:40,700 --> 00:08:44,470
so where we need to change this is on line

209
00:08:44,470 --> 00:08:48,140
67 let's just make that....

210
00:08:48,140 --> 00:08:52,210
....all right we'll run

211
00:08:52,210 --> 00:08:57,340
again in a browser or open in browser and

212
00:08:57,340 --> 00:08:58,720
look at the source

213
00:08:58,720 --> 00:09:01,900
okay for some reason we haven't got a

214
00:09:01,900 --> 00:09:03,070
a title lets just go back and check our

215
00:09:03,070 --> 00:09:08,080
code ok looking at the code

216
00:09:08,080 --> 00:09:09,700
definitely got the title in there and we go

217
00:09:09,700 --> 00:09:12,260
back down I'll go back up i should say

218
00:09:12,260 --> 00:09:13,130
to the head

219
00:09:13,130 --> 00:09:15,860
we've definitely got the title

220
00:09:15,860 --> 00:09:18,700
arguments showing init method and we're

221
00:09:18,700 --> 00:09:21,760
definitely creating a tag and that should all

222
00:09:21,760 --> 00:09:23,470
be ok as far as I'm concerns just try

223
00:09:23,470 --> 00:09:28,010
running it again and go back to browser

224
00:09:28,010 --> 00:09:29,570
what we need to do is need to open it

225
00:09:29,570 --> 00:09:35,440
again open browser Chrome again let's

226
00:09:35,440 --> 00:09:38,360
just look at the page source and it is

227
00:09:38,360 --> 00:09:40,210
....

228
00:09:40,210 --> 00:09:43,720
...

229
00:09:43,720 --> 00:09:46,310
...

230
00:09:46,310 --> 00:09:50,650
...ok so false alarm

231
00:09:50,650 --> 00:09:52,690
that was all working correctly and with

232
00:09:52,690 --> 00:09:55,190
now got the test html containing a

233
00:09:55,190 --> 00:09:58,330
title tag inside the head and by the way

234
00:09:58,330 --> 00:09:59,470
the other way we could have checked for

235
00:09:59,470 --> 00:10:01,880
that is if we just go back and

236
00:10:01,880 --> 00:10:05,870
closed down these view sources depending on

237
00:10:05,870 --> 00:10:07,880
the browser you can actually see the

238
00:10:07,880 --> 00:10:10,570
title in the browser bar but I can't the way

239
00:10:10,570 --> 00:10:11,890
chrome is configured on my

240
00:10:11,890 --> 00:10:13,660
computer right now that would be a

241
00:10:13,660 --> 00:10:15,740
second way of doing it anyway because in

242
00:10:15,740 --> 00:10:17,360
my case I'm still just seeing the URL of

243
00:10:17,360 --> 00:10:21,910
the document but depending on how your

244
00:10:21,910 --> 00:10:24,010
browser is configured you probably see

245
00:10:24,010 --> 00:10:25,810
the title in the browser's address bar

246
00:10:25,810 --> 00:10:26,590
anyway
the title in the browser's address bar

247
00:10:26,590 --> 00:10:28,810
alright so that's it that's composition

248
00:10:28,810 --> 00:10:30,970
in action in the next video we're going

249
00:10:30,970 --> 00:10:32,020
to look at something that's often

250
00:10:32,020 --> 00:10:33,740
described as a weaker form of

251
00:10:33,740 --> 00:10:36,700
composition and that's aggregation see

252
00:10:36,700 --> 00:10:42,450
you in the next video

