1.Write a method named matchesPattern1() that accepts a String as input. This method determines whether or not the input String has the following property:
The string has a number of c's equal to the total number of a's and b's.
In other words, every word in this language is of the form:
anbmc(n + m), where n ≥ 0 and m ≥ 0. (n,m,(n+m)都是次方)
n and m may be equal, but this is not required.
Thus, bc, abcc, abccac, aabbcccc and abbbbccccc are all valid words in this language.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbcc, your method should return false.
2.Write a method named matchesPattern2() that accepts a String as input. This method determines whether or not the input String has the following properties:
The string has a number of c's equal to the total number of a's and b's. All a's come before any b's, and all b's come before any c's. (HINT: Can you test for this using one or more boolean variable "flags"?)
In other words, every word in this language is of the form:
anbmc(n + m), where n ≥ 0 and m ≥ 0. (n,m,(n+m)都是次方)
n and m may be equal, but this is not required.
Thus, aabbcccc and abbbbccccc are valid words in this language, but abaccc and abc are not.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbcc, your method should return false.
This pattern is identical to the one above, except it imposes a specific ordering requirement on the input string.
3.Write a method named matchesPattern3() that accepts a String as input. This method determines whether or not the input String has the following properties:
The string has an equal (nonzero) number of a's and c's, and any number of (zero or more) b's. All a's come before any b's, and all b's come before any c's.
In other words, every word in this language is of the form:
anb*cn, where n > 0. (n,*都是次方)
Thus, aabbcc and ac are valid words in this language, but abaccc and aabc are not.
Your method should return a boolean value: true if the input string satisfies these requirements, and false if it does not.
For example, given a String such as aabbaa, your method should return false.
4.Fill in the body of your main() method so that it prompts the user to enter 5 input strings, one at a time. For each input string, your program should compare it to each pattern, and print out whether the string matches each pattern. 用来写的源文件是 // Matcher.java
import java.util.*;
public class Matcher { public static void main (String [] args) { Scanner sc = new Scanner(System.in);
// The statement/loop that calls matchesPattern1(),matchesPattern(2), // and matchesPattern3() goes in this method
}
// Define your pattern-matching methods here. Be sure to make them static! // // Each method should have a header with the following form: // // public static boolean matchesPatternX (String input) // // where 'X' is replaced by 1, 2, or 3.
// You may define any extra (static) helper methods that you want