Here is the backgound The StringMatcher Interface Were going

Here is the backgound.

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: public int match(String s, int start)
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:

1. 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
2. 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.
3. 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.
4. 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 str from 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.


I already finished 1st and 2nd part and I attached it.

interface stringMatcher public int match (String s, int start); class SCM implements stringMatcher private final char c; public SCM(char c) f this.c public int match (String s, int start) if(s. length() start) return -1; if(s.charAtt (start) this c f return start 1; return -1;

Solution

STR program

public class STR implements StringMatcher {

   private String text;

   public STR(String text) {
       this.text = text;
   }

   @Override
   public int match(String s, int start) {

       int count = 0;
       int length = text.length();
       int lenS = s.length();
  
       if ((start < 0) || start >= s.length()) {
           return -1;
       }
// Comparing both the string length\'s to avoid ARRAYINDEXOUTOFBOUND exception
       if(lenS-start < length)
       {
           return -1;
       }
// Main code for the STR class
       for (int index = start, index1 = 0; index1 < length; index++, index1++) {
           if (s.charAt(index) == text.charAt(index1)) {
               count++;
               if (count == length - 1) {
                   return (index + 2);
               }
           } else {
               return -1;
           }
       }

       return -1;
   }

}

Explanation:

1) Call the above method AS:

STR str = new STR(\"cs11w\");
       System.out.println(str.match(\"cs11wb\", 0));
       System.out.println(str.match(\"stuff_cs11wb\", 0));
       System.out.println(str.match(\"stuff_cs11wb\", 6));

Getting output As: 5, -1 and 11 respecively

SEQ program

Note: I am using RCM program(which you implemented) in SEQ program

public class SEQ implements StringMatcher {

   private STR str;
   private RCM rcm;
   public SEQ(STR str, RCM rcm) {
       this.str = str;
       this.rcm = rcm;
   }
  
   @Override
   public int match(String s, int start) {
      
       int matcherSTR = str.match(s, start);
      
       if(matcherSTR == -1) {
           return -1;
       }
       else {
           return rcm.match(s, matcherSTR);
       }
   }
      
}

Explanation:

Call the above method AS:

SEQ seq = new SEQ(new STR(\"cs11w\"), new RCM(\'b\', \'c\'));
       System.out.println(seq.match(\"cs11wb\", 0));
       System.out.println(seq.match(\"cs11wZ\", 0));
       System.out.println(seq.match(\"stuff_cs11wb_morestuff\", 6));

Getting output As: 6, -1 and 12 respecively

Let me know any concerns. Thank you

Here is the backgound. The StringMatcher Interface We\'re going to tackle this problem by implementing several different classes that satisfy a StringMatcher in
Here is the backgound. The StringMatcher Interface We\'re going to tackle this problem by implementing several different classes that satisfy a StringMatcher in
Here is the backgound. The StringMatcher Interface We\'re going to tackle this problem by implementing several different classes that satisfy a StringMatcher in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site