Wednesday, 3 October 2018

Input Filter class Inherit from StringTokenizer (Solution)

Write a Java class, InputFilter, which inherits from the StringTokenizer class which you are familiar with.

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.

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.

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.


import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;

public class E5tester {
  public static void main(String args[]){
    org.junit.runner.JUnitCore.main("E5tester");
  }

  @Test public void check_constructor_01() {
  InputFilter ifilt = new InputFilter("test", "a", "b");
  }


Solution Output:















For more information contact:
Gmail : techexteam@gmail.com
Facebook : Facebook page