Question: IN JAVA programming language You've probably seen text filters before. Programs which can take some input text, and change it around to get new text.

IN JAVA programming language

You've probably seen text filters before. Programs which can take some input text, and change it around to get new text. So for example, we could have a filter that looks for any appearance of the substring MSU, and replaces it with Michigan State University. That way, if you typed in "I am a student at MSU.", you'd see "I am a student at Michigan State 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 "MSU" with "Michigan State 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 MSU","MSU","Michigan State University"); > String s = ""; > while (in.hasMoreTokens()) s = s + in.nextToken() + " "; > System.out.println(s); I am a student at Michigan State University >

You will need to import the following class (but only this class; others are disallowed):

java.util.StringTokenizer

To finish the 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).

All method signatures should match the ones shown exactly.

In short, you are responsible for three versions of the constructor, one new method which performs the text translation, and three overridden methods for reading in string tokens with text replacement. The overridden methods perform the same as the original, except that the strings which are returned should have been text translated already. Hint: figure out how to make use of the parent methods.

The @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).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!