JAVA PROGRAM DUE AT 6:50 TODAY / /HELP !!!!, HELP ME FIX MY CODE |
JAVA PROGRAM DUE AT 6:50 TODAY / /HELP !!!!, HELP ME FIX MY CODE |
![]()
Post
#1
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() Group: Human Posts: 525 Joined: Nov 2008 Member No: 695,913 ![]() |
Heres the instructions for the program:
CODE The ability to match patterns in text is a critical feature of many programs. For example, pattern-matching is required for a compiler to recognize and correctly translate program source code. In today's lab, you will write several methods that analyze user input to determine whether it matches a few simple patterns. To make things easier for you, we will assume that the user input only consists of sequences of the lowercase letters 'a', 'b', and 'c', with no other characters or spacing permitted. The Matcher.txt file contains a skeleton for today's lab assignment. Use this file to create a new BlueJ or Eclipse project and then complete the following exercises: 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 and m may be equal, but this is not required. Thus, bc, abc, 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. 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 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. 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. 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. 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. heres my code : CODE import java.util.*; public class Matcher { public static void main (String [] args) { Scanner sc = new Scanner(System.in); //for (int z = 1; z <= 5; z++) { System.out.println("Type in a set of characters (a or b or c): "); String input = sc.nextLine(); // The statement/loop that calls matchesPattern1(),matchesPattern(2), // and matchesPattern3() goes in this method matchesPattern1(input); matchesPattern2(input); matchesPattern3(input); } } // 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. public static boolean matchesPattern1(String input) { boolean t1 = false; int len1 = input.length(), charA = 0, charB = 0, charC = 0; char [] characterarray = input.toCharArray(); for (int i = 0; i > len1; i++) { while (characterarray[i] == 'a'){charA = charA + 1;} while (characterarray[i] == 'b'){charB = charB + 1;} while (characterarray[i] == 'c'){charC = charC + 1;} } if (charA + charB == charC){t1 = true;} else {t1 = false;} System.out.println(t1); return t1; } public static boolean matchesPattern2(String input) { boolean t2 = false; int len1 = input.length(), charA = 0, charB = 0, charC = 0; char [] characterarray= input.toCharArray(); for (int i = 0; i > len1; i++) { while (characterarray[i] == 'a' & charB == 0){charA = charA + 1;} while (characterarray[i] == 'b' & charC == 0){charB = charB + 1;} if (characterarray[i] == 'c') { while (characterarray[i] == 'c'){charC = charC + 1;} } else { break; } } while (charA + charB == charC){t2 = true;} System.out.println(t2); return t2; } public static boolean matchesPattern3(String input) { boolean t3 = false; int len1 = input.length(), charA = 0, charB = 0, charC = 0; char [] characterarray= input.toCharArray(); for (int i = 0; i > len1; i++) { while (characterarray[i] == 'a' & charB == 0){charA = charA + 1;} while (characterarray[i] == 'b' & charC == 0){charB = charB + 1;} if (characterarray[i] == 'c') { while (characterarray[i] == 'c'){charC = charC + 1;} } else { break; } } while (charA == charC){t3 = true;} System.out.println(t3); return t3; } // You may define any extra (static) helper methods that you want } //getCount(String s, char c) //getLastposition(String s, char c) i cant get it 2 work so any help re writing this woudl be greatly appreciated. Stay Up -1- Subliminal EDIT STILL ACCEPTING HELP |
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() Group: Human Posts: 525 Joined: Nov 2008 Member No: 695,913 ![]() |
anybody? . . i need this to hand in soon . . help please !!!!
Stay Up -1- Subliminal |
|
|
![]()
Post
#3
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
I don't want to be a jerk, but the problem isn't that hard. You're doing some stuff with nested while loops that isn't really necessary. This can be done with a single for loop over the string.
I wrote a Java version (and a Haskell version, incidentally), but I probably shouldn't just post my code. Here's the algorithm I used for each pattern, though (in pseudocode): CODE matchesPattern1(s): totals[] = {0,0,0} for each character in s: if character is 'a': increment totals[A] by 1 else if character is 'b': increment totals[B] by 1 else if character is 'c': increment totals[C] by 1 return totals[A] = totals[B] == totals[C] matchesPattern2(s): totals[] = {0,0,0} saw[] = {false,false,false} for each character in s: if character is 'a': saw[A] = true if saw[B] or saw[C]: return false else: increment totals[A] by 1 else if character is 'b': saw[B] = true if saw[C]: return false else: increment totals[B] by 1 else if character is 'c': saw[C] = true increment totals[C] by 1 return totals[A] + totals[B] == totals[C] matchesPattern3(s): totals[] = {0,0,0} saw[] = {false,false,false} for each character in s: if character is 'a': saw[A] = true if saw[B] or saw[C]: return false else: increment totals[A] by 1 else if character is 'b': saw[B] = true if saw[C]: return false else if character is 'c': saw[C] = true increment totals[C] by 1 return totals[A] == totals[C] and totals[A] > 0 Translate to Java, and you should be golden. |
|
|
![]()
Post
#4
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 2,648 Joined: Apr 2008 Member No: 639,265 ![]() |
Incidentally, here's the Haskell version of the same program:
CODE matchesPattern1 s = mp s (0, 0, 0)
where mp [] (a, b, c) = a + b == c mp (x:xs) (a, b, c) | x == 'a' = mp xs ((a + 1), b, c) | x == 'b' = mp xs (a, (b + 1), c) | x == 'c' = mp xs (a, b, (c + 1)) matchesPattern2 s = mp s (0, 0, 0) (False, False, False) where mp [] (a, b, c) _ = a + b == c mp (x:xs) (a, b, c) (sa, sb, sc) | x == 'a' = if sb || sc then False else mp xs ((a + 1), b, c) (True, sb, sc) | x == 'b' = if sc then False else mp xs (a, (b + 1), c) (sa, True, sc) | x == 'c' = mp xs (a, b, (c + 1)) (sa, sb, True) matchesPattern3 s = mp s (0, 0, 0) (False, False, False) where mp [] (a, b, c) _ = (a == c) && (a > 0) mp (x:xs) (a, b, c) (sa, sb, sc) | x == 'a' = if sb || sc then False else mp xs ((a + 1), b, c) (True, sb, sc) | x == 'b' = if sc then False else mp xs (a, (b + 1), c) (sa, True, sc) | x == 'c' = mp xs (a, b, (c + 1)) (sa, sb, True) |
|
|
![]()
Post
#5
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() Group: Human Posts: 525 Joined: Nov 2008 Member No: 695,913 ![]() |
thnx man . . .
![]() Stay up -1- Subliminal |
|
|
![]()
Post
#6
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 8,629 Joined: Jan 2007 Member No: 498,468 ![]() |
Topic Closed & Moved
|
|
|
![]() ![]() |