CS 211, 004/006
Lab Exercise-5
due Thursday, October 4th at noon


The objective of this lab is to derive new classes from other classes which we are familiar with, in order to see how methods can be overridden to provide specialized functionality.

Overview:

  1. Write a Java class, InputFilter, which inherits from the StringTokenizer class which you are familiar with.
  2. The InputFilter will have new methods and/or extend existing methods which allow the text of input strings to be filtered to produce new results.
  3. Download and use a tester module to ensure that your programs are correct. The programs can also be tested using Dr Java's interactive window.
  4. Prepare the assignment for submission and submit it
This is a lab exercise on which collaboration (including discussing the solution on Piazza or searching online sources) is explicitly permitted. It is still in your interest to understand and prepare your final submission yourself.

Task:

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 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
>

Alternatively, having already written this class, we could just use it as a multi-layer translator to process several filters in a row for a combined effect:

> 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
>

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

To finish the InputFilter class, you will need to implement the following: 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).

Testing:

Download the following (along with the tester jar from before):

Submission:

Submission instructions are as follows.

  1. Let xxx be your lab section number (one of 213-220), and let yyyyyyyy be your GMU userid. Create the directory xxx_yyyyyyyy_E5/
  2. Place all of the .java files that you've created into the directory.
  3. Create the file 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
    userID: dknuth
    G#: 00123456
    Lecture section: 004
    Lab section: 213

  4. compress the folder and its contents into a .zip file, and upload the file to Blackboard.