Comments on Assignment 4:
tag and link enumerations
There is a small typo on the handout for this assignment. When discussing
TagEnumeration, it says,
Method hasMoreElements should
always be called first, and calls to hasMoreElements
and nextElement are expected
to alternate. Before hasMoreElements can return true or false, ...
It should read,
Method hasMoreElements should
always be called first, and calls to hasMoreElements
and nextElement are expected
to alternate. Before nextElement can return true or false, ...
We give you some pointers that should help you write TagEnumeration
and LinkEnumeration.
Tags and the names of their parameters may be upper case, lower case, or
a mixture. For example, one can write http, HTTP, Http, HtTp, and they
are all treated the same.
You may want to use a String method that coverts to lower case,
to make "http" easier to check for (and also "src"). However, you should
not
return a lowercased version of a tag. For example, if a tag contains 'http="ABc"
' and you tell the user that the tag was "http="abc" ', this will cause
no problems on a windows or macintosh machine, but it will cause a problem
on a Unix machine, because file and directory names are case sensitive.
A tag or a link may span several lines. Therefore, you would need a loop
that repeatedly reads a line until the end of the tag or link is found.
This pattern happens often in programs like this, so you should get used
to this pattern. Suppose the index (or number) of the character "<"
in a String variable line is in variable i; that is s[i] = '<'
and no '<' appears in s[0..i-1]. At this point, you have to find the
next ">", which is the next part of the tag. You could do the following
1. remove from line the character preceding character i, so that "<"
is the first character in variable line;
2. as long as line does not contain ">" do the following
Read the next line into a variable s;
If s is null (meaning there are no more lines),
then do whatever you have to do to indicate that there are no more tags
and terminate the loop
line= line + s;
We purposefully did not tell you what to do in case that there was no more
lines; this depends on how you write the program.
In both TagEnumeration and LinkEnumeration, it is best to keep methods
hasMoreElements and nextElement as simple as possible. To this end:
Have a variable s that contains the tag (or link) that has been found but
has not yet been given back when executing nextElement. This variable is
null when there are no more links.
Write a method whose purpose is to change variable s to the NEXT tag (or
link) (to null if there is no more). And then call this method in judicious
places. Remember that calls to the methods should always alternate: hasMoreElements(),
nextElement(), hasMoreElements(), nextElement(), ...
In class LinkEnumeration, it is probably best to have a function that extracts
a link from a tag, returning null if there is no link.
Your program doesn't have to worry about errors; assume the input has correct
tags. If there is a missing ">" after a tag, then one can't say what your
program will produce. If a link looks like this: href= "abc>, then there
is a missing ", and you won't know what your program will do. We will test
your program only on input that contains correct tags and links.