Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The StringMatcher Interface We\'re going to tackle this problem by implementing

ID: 3793384 • Letter: T

Question

The StringMatcher Interface

We're going to tackle this problem by implementing several different classes that satisfy a StringMatcher interface, which specifies a single method:

match should take a string and a start index, and return -1 if the given string, starting from index start, does not match the specified pattern. Otherwise, it should return the index after the specified pattern ends.

We will implement four kinds of string patterns, each as a class that implements the StringMatcher interface:

SCM which stands for "single-character matcher". This is provided for you. It has a single char field, and matches the string with just that one character

RCM which stands for "range-character matcher". You will implement this. It has two char fields and checks that the string has a character in between those two characters at the given start index.

SEQ which stands for "sequence." You will implement this. It has two StringMatcher fields. It checks that the first StringMatcher matches at the given start index, and if it does, also checks that the second StringMatcher matches after the first one.

STR, which stands for "string." You will implement this. It has a single String field str. It checks that the string to match against starts with strfrom the given start index.

Some examples:

For a string pattern like new STR("cs11w"):

For matching the string "cs11wb" starting at index 0, it should return 5 -- the index after w, where the pattern ends

For matching the string "stuff_cs11wb" starting at index 0, it should return -1, since "stuff" is not equal to "cs11w"

For matching the string "stuff_cs11wb" starting at index 6, it should return 11 -- the index after w, where the pattern ends

For a single character pattern like new SCM('c')

For matching the string "cs11wb" starting at index 0, it should return 1 -- the index after c, where the pattern ends

For matching the string "stuff_cs11wb" starting at index 0, it should return -1, since it doesn't match

For matching the string "stuff_cs11wb" starting at index 6, it should return 7 -- the index after c, where the pattern ends

For a range pattern like new RCM('a', 'z'):

For matching the string "good_stuff" starting at index 0, it should return 1 -- the index after g, where the pattern ends (g matches since it is between a and z)

For matching the string "good_stuff" starting at index 5, it should return 6 -- the index after s, where the pattern ends (s matches because it is between a and z)

For matching the string "good_Stuff" starting at index 5, it should return -1, since S is not between a and z

For a sequence pattern like new SEQ(new STR("cs11w"), new RCM('b', 'c'))

For matching the string "cs11wb" starting at index 0, it should return 6 -- the index after b, where the pattern ends

For matching the string "cs11wZ" starting at index 0, it should return -1. The first part of the sequence matches, but not the second

For matching the string "stuff_cs11wb_morestuff" starting at index 6, it should return 12, the index after both patterns are matched

Hints and Tips

You can use < and > to compare char values, which have relatively straightforward meanings for letters and numbers. You can look up an ASCII table (just use Google) if you want to see what the underlying values are.

Remember to use .equals to compare String values, not ==.

Double-check indices and documentation when using charAt() and substring() on Strings, to avoid out-of-bounds indexing.


interface StringMatcher {
public int match(String s, int start);
}

class SCM implements StringMatcher {
private final char c;
public SCM(char c) {
this.c = c;
}
public int match(String s, int start) {
if(s.length() <= start) { return -1; }
if(s.charAt(start) == this.c) { return start + 1; }
return -1;
}
}

class RCM implements StringMatcher {
private final char from, to;
public RCM(char from, char to) { this.from = from; this.to = to; }
public int match(String s, int start) {
return -1;
}
}

class STR implements StringMatcher {
private final String str;
public STR(String str) { this.str = str; }
public int match(String s, int start) {
return -1;
}
}

class SEQ implements StringMatcher {
private final StringMatcher first, second;
public SEQ(StringMatcher first, StringMatcher second) {
this.first = first;
this.second = second;
}
public int match(String s, int start) {
return -1;
}
}

public class Main {
public static void main(String[] args) {
  
StringMatcher ti = new STR("testinput");
StringMatcher RCM('1', '6');
StringMatcher dotTxt = new STR(".txt");
  
// Write some small printout tests of these individually

StringMatcher s = new SEQ(ti, new SEQ(oneToSix, dotTxt));
int m = s.match("testinput1.txt", 0);
System.out.println(m);
  
// You build one that matches the account pattern.
}
}

Explanation / Answer

import java.util.*;
import java.lang.*;
import java.io.*;

interface StringMatcher{
   public int match(String S,int start);
};

class SCM implements StringMatcher{
   private final char c;
   public SCM(char c){
       this.c = c;
   }
   public int match(String S, int start){
       if(S.length() <= start)
       {
           return -1;
       }
       if(S.charAt(start) == this.c)
       {
           return start+1;
       }
       return -1;
   }
};
class RCM implements StringMatcher{
   private final char from,to;
   public RCM(char from , char to){
       this.from = from;
       this.to = to;
   }
   public int match(String S, int start){
       if(S.length() <= start)
       {
           return -1;
       }
       //compare if character at start index is in betwwen from and to
       if(S.charAt(start)>= from && S.charAt(start) <= to)
       {
          return start+1;
       }
   return -1;
   }
};
class STR implements StringMatcher{
   private final String str;
   public STR(String str)
   {
       this.str = str;
   }
   public int match(String S, int start)
   {
       if(S.length() <= start)
       {
           return -1;
       }
       //check if start+str length exceeds length of S
       if(start+str.length() <= S.length()){
          //get the substring from S from start index to start+str.length and compare with str
           if(str.equals((S.substring(start,start+str.length()))))
          {
              return start+str.length();
          }
       }
       return -1;
   }
};
class SEQ implements StringMatcher{
   private final StringMatcher first, second;
   public SEQ(StringMatcher first, StringMatcher second)
   {
       this.first = first;
       this.second = second;
   }
   public int match(String S, int start)
   {
       int index1 = first.match(S,start);
       if(index1 != -1)
       {
           return(second.match(S,index1));
       }
       return -1;
   }
}
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {

StringMatcher ti = new STR("testinput");
StringMatcher RCM('1', '6');
StringMatcher dotTxt = new STR(".txt");
  
  

StringMatcher s = new SEQ(ti, new SEQ(oneToSix, dotTxt));
int m = s.match("testinput1.txt", 0);
System.out.println(m);


StringMatcher ti1 = new STR("cs11w");
m = ti1.match("cs11wb",0);
System.out.println(m);
m = ti1.match("stuff_cs11wb",0);
System.out.println(m);
m = ti1.match("stuff_cs11wb",6);
System.out.println(m);

StringMatcher aToz = new RCM('a','z');
m = aToz.match("good_stuff",0);
System.out.println(m);
m = aToz.match("good_stuff",5);
System.out.println(m);
m = aToz.match("good_Stuff",5);
System.out.println(m);
StringMatcher s1 = new SEQ(ti1,new RCM('b','c'));
m = s1.match("cs11wb",0);
System.out.println(m);
m = s1.match("stuff_cs11wb_morestuff",6);
System.out.println(m);
m = s1.match("cs11wZ",0);
System.out.println(m);


   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote