GMU
, and replaces it
with George Mason University
. That way, if you
typed in "I am a student at GMU
.", you'd see
"I am a student at George Mason University
." come
out the other side. Our goal here is to create a class which
will do this sort of text replacement on our input.
You're already familiar with the Scanner
class.
In this lab, we will be using a similar class, called
the StringTokenizer
. It doesn't have all of the
capabilities of the Scanner
(Scanner
is declared final
so we can't use it to derive other classes), but it does have the capability to read tokens from a string.
This functionality is good,
and we want to keep it. We want to create a class which is
still a StringTokenizer
, but which gives us the special
extra functionality which we need.
Thus, for this lab, you will make InputFilter
,
a new class which is a subclass of StringTokenizer
. As
a result, it will inherit all of the associated functionality
automatically. Additionally, you'll provide it with a replacement
expression. So for example, if we want to replace
"GMU"
with "George Mason University"
,
we would give the InputFilter
both strings. This
would come into play when we try to read in String
data from the StringTokenizer
using nextToken()
and related methods. Below is
a working example of the class:
> InputFilter in = new InputFilter("I am a student at GMU","GMU","George Mason University");
> String s = "";
> while (in.hasMoreTokens()) s = s + in.nextToken() + " ";
> System.out.println(s);
I am a student at George Mason University
>
> String input = "leet hacker";
> InputFilter in1 = new InputFilter("","er","0r");
> InputFilter in2 = new InputFilter("","t","7");
> InputFilter in3 = new InputFilter("","a","4");
> InputFilter in4 = new InputFilter("","e","3");
> InputFilter in5 = new InputFilter("","ck","xx");
> String output = in5.translate( in4.translate ( in3.translate ( in2.translate ( in1.translate( input )))));
> System.out.println( output );
l337 h4xx0r
>
java.util.StringTokenizer
InputFilter
class, you will need to
implement the following:
public InputFilter(String s, String key, String replacement)
A constructor which initializes the InputFilter
from a string, and replaces occurrences of
key
with replacement
.public InputFilter(String s, String delim, String key, String replacement)
same as above, but with the addition of a token delimiter (see documentation for StringTokenizer
).public InputFilter(String s, String delim, boolean returndelim, String key, String replacement)
same as above, but with the addition of a flag indicating whether to return delimiters (see documentation for StringTokenizer
).public String translate( String s )
this call
takes a passed-in string, and using the instance's
key
/replacement
pair, creates and
returns a new string with the text replaced. Hint: look up
String
's replaceAll()
method.@Override public String nextToken()
this overrides
the parent's version, and does nearly the same thing, except
that the output string should have had the text replacement
performed already.@Override public String nextToken(String delim)
same as above, but specifying the token delimiter (see documentation for StringTokenizer
).@Override public Object nextElement()
returns the exact same output as nextToken()
, except that it is cast as an Object
(see documentation for StringTokenizer
).@Override
directive isn't strictly necessary, but
it helps the compiler catch bugs like misspellings (you're telling
it that you're overriding a method, so if you're not, then that
probably means you made a typo, and the compiler will tell you).
Testing:
Download the following (along with the tester jar from before):
Submission instructions are as follows.
xxx_yyyyyyyy_E5/
.java
files that you've
created into the directory.ID.txt
in the format shown below, containing your name, userid, G#, lecture section
and lab section, and add it to the directory.
Full Name: Donald Knuth