In an input consisting of all 'a', 'b', and 'c'

I'm stumped. I don't need the code, please just explain how t

Page 1 of 1

9 Replies - 1413 Views - Last Post: 18 March 2009 - 08:57 PM Rate Topic: -----

#1 patti_xd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-March 09

In an input consisting of all 'a', 'b', and 'c'

Post icon  Posted 18 March 2009 - 12:24 AM

import java.util.*;

public class Matcher
{
	public static void main (String [] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter 5 input strings, press enter after each one.");
		String message1 = sc.next();
		String message2 = sc.next();
		String message3 = sc.next();
		String message4 = sc.next();
		String message5 = sc.next();	
		printMatch(message1);
		printMatch(message2);
		printMatch(message3);
		printMatch(message4);
		printMatch(message5);

		// The statement/loop that calls matchesPattern1(),matchesPattern(2),
		// and matchesPattern3() goes in this method
		
	}
	
	public static boolean matchesPattern1(String message)
	{
		int[] counts = countLetters(message);  //invokes to count each a's, b's and c's
		int n = counts[0]; // n = number of a's
		int m = counts [1]; // m = number of b's
		int total = counts [2];
		if ((n+m)==total) // if total number of a's + b's equal total number of c's its true
			return true;
		else
			return false;
	}

	public static boolean matchesPattern2(String message)
	{
[quote] I have no idea how to start this off, method 1 and 3 took me hours to understand and complete.  

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"?)
aabbcccc and abbbbccccc are valid words in this language, but abaccc and abc are not. 

would it make sense to start from the left and take the last letter to see if its a letter 'c' if it is continue by message.length-1 and see if that last letter is still 'c' and so forth?  If its a 'b' then continue, and but if its an 'a' false?  I'm not quite sure how this would work or if it does.

maybe I did method 1 the wrong way, but it says that "This pattern is identical to the one above, except it imposes a specific ordering requirement on the input string."[/quote]	
	}
	
	public static boolean matchesPattern3(String message)
	{
		int[] counts = countLetters(message);  //invokes to count each a's, b's and c's
		int a = counts[0]; // number of a's
		int c = counts [2];
		if (a > 0 && c > 0 && a==c) 
			return true;
		else
			return false;
	}

	//counts letters in the string
	public static int[] countLetters(String message)
	{
		int[] counts = new int[3];
		for (int i = 0; i < message.length(); i++)
		{if (Character.isLetter(message.charAt(i)))
			counts[message.charAt(i) - 'a']++;
		}
		return counts;
	}

	// prints whether the message matches each pattern or not
	public static void printMatch(String a)
	{
		if (matchesPattern1(a)==true)
			System.out.println("Input matches pattern 1.");
		else
			System.out.println("Input does not match pattern 1.");
		
		
		if (matchesPattern2(a)==true)
			System.out.println("Input matches pattern 2.");
		else
			System.out.println("Input does not match pattern 2.");

		
		
		if (matchesPattern3(a)==true)
			System.out.println("Input matches pattern 3.");
		else
			System.out.println("Input does not match pattern 3.");
		
	}
	// You may define any extra (static) helper methods that you want
	
}


 
 


Is This A Good Question/Topic? 0
  • +

Replies To: In an input consisting of all 'a', 'b', and 'c'

#2 Daegan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 67
  • Joined: 17-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 12:32 AM

Could you please give example inputs and expected outputs?
Was This Post Helpful? 0
  • +
  • -

#3 Daegan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 67
  • Joined: 17-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 12:59 AM

You could try to do a bubblesort method to check if the patterns are right. I had to do something similar to this in my computer science class on doubles. I believe that characters can be sorted in this manner, which would probably make your entire project easier overall.

This post has been edited by Daegan: 18 March 2009 - 01:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 patti_xd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 09:38 AM

View PostDaegan, on 17 Mar, 2009 - 11:32 PM, said:

Could you please give example inputs and expected outputs?


For method matchesPattern2() it returns true if all inputs of 'a' are before 'b' and all inputs of 'b' are before 'c'. Such that, True inputs would be aabbcc, aacc, and bbbcc. Inputs that are False would be aabbccaa, aaccbb, bbaacc, and bbccca. The whole project would tell me which inputs out of five are true for matchesPattern1(),matchesPattern2() and matchesPattern3() as you can see in printMatch().

I was thinking about it all night, and this is what i've gotten so far, but I'm not sure if its close.

public static boolean matchesPattern2(String message)
	{
while (true) {
for (int i = 0; i < message.length(); i++)
{if (Character.isLetter(ch: 'a') == message.charAt(i))
   return true;
	 { if (Character.isLetter(ch: 'b') == message.charAt(i))
	   return true;
			   { if(Character.isLetter(ch: 'c') == message.charAt(i))
					  return true;}
else if (Character.isLetter(ch: 'b') == message.charAt(i))
	  {if (Character.isLetter(ch: 'a') == message.charAt(i))
		 { if (Character.isLetter(ch: 'c') == message.charAt(i))
			   return false;}}
}}


This code doesn't work but if I fixed it up a little, would this be the way to start off the method?

View PostDaegan, on 17 Mar, 2009 - 11:59 PM, said:

You could try to do a bubblesort method to check if the patterns are right. I had to do something similar to this in my computer science class on doubles. I believe that characters can be sorted in this manner, which would probably make your entire project easier overall.


I haven't learned the bubblesort method in class yet, but isn't the method to replace letters in a string? I just need to check if the all 'a's are before 'b's, and all 'b's are before 'c's.
Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 01:25 PM

Is the input string only allowed to contain A's, B's, and C's?
Was This Post Helpful? 0
  • +
  • -

#6 patti_xd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 01:27 PM

View Postbsaunders, on 18 Mar, 2009 - 12:25 PM, said:

Is the input string only allowed to contain A's, B's, and C's?


ohh sorry! yes. input is only a's, b's c's. All lowercase.
Was This Post Helpful? 0
  • +
  • -

#7 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 03:20 PM

Did you write a method that checks if the input string contains only lowercase a's, b's, and c's before proceeding? For example, consider the method containsOnlyABCs:

	/*
	 * private static boolean containsOnlyABCs(String string)
	 *
	 * Return Value: Returns true if the string contains only lowercase a's,
	 *			   b's, and c's. Otherwise, it returns false.
	 *
	 * Description: Determines if the string contains only lowercase a's, b's,
	 *			  and c's.
	 */
	private static boolean containsOnlyABCs(String string)
	{
		int i;

		/* Loop through each character in the string */
		for(i = 0; i < string.length(); i ++) {
			char c;

			c = string.charAt(i);
			/* Is the character not a lowercase a, b, or c? */
			if(c != 'a' && c != 'b' && c != 'c')
				return false; /* The string does not contain only a's, b's, and c's */
		}

		return true;
	}


Also, regarding the second pattern, do you about the collating sequences of character sets? For instance, in the ASCII character set, the letter character 'a' is less than the character 'b', and the character 'b' is less than 'c'. Using that knowledge, you can construct a method that checks if the given string is in sequence:

	/*
	 * private static boolean matchesPattern(String string)
	 *
	 * Return Value: Returns true if the string matches the pattern described below.
	 *				 Otherwise, it returns false.
	 *
	 * Description: Determines if the string contains only lowercase a's, b's,
	 *				and c's, and that the letters are in sequence, i.e, all a's
	 *				come before b's, and all b's come before c's. For example, the
	 *			  string "aaabbaacccc" would yield a return value of false because
	 *				some b's in the string are illegally followed by a's.
	 */
	private static boolean matchesPattern(String string)
	{
		char last;
		int i;

		/* Make sure the string contains only lowercase a's, b's, and c's */
		if(!containsOnlyABCs(string))
			return false;

		/* Get the first character in the string */
		last = string.charAt(0);

		/* Loop through the next characters in the string */
		for(i = 1; i < string.length(); i ++) {
			char next;

			/* Get the next character in the string */
			next = string.charAt(i);

			/* The next character in the string has to be greater than or equal the last
			   character in order for this pattern to match */
			if(next < last)
				return false; /* If the next character is less than the last, the pattern
								 doesn't match. Return false */

			last = next; /* The next character now becomes the last so that it can be
							compared to the "new" next */
		}

		return true;
	}


If you don't understand the last code snippet, please feel free to ask any questions.

This post has been edited by bsaunders: 18 March 2009 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 patti_xd  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 18-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 08:45 PM

View Postbsaunders, on 18 Mar, 2009 - 02:20 PM, said:

Did you write a method that checks if the input string contains only lowercase a's, b's, and c's before proceeding? For example, consider the method containsOnlyABCs:

	/*
	 * private static boolean containsOnlyABCs(String string)
	 *
	 * Return Value: Returns true if the string contains only lowercase a's,
	 *			   b's, and c's. Otherwise, it returns false.
	 *
	 * Description: Determines if the string contains only lowercase a's, b's,
	 *			  and c's.
	 */
	private static boolean containsOnlyABCs(String string)
	{
		int i;

		/* Loop through each character in the string */
		for(i = 0; i < string.length(); i ++) {
			char c;

			c = string.charAt(i);
			/* Is the character not a lowercase a, b, or c? */
			if(c != 'a' && c != 'b' && c != 'c')
				return false; /* The string does not contain only a's, b's, and c's */
		}

		return true;
	}


Also, regarding the second pattern, do you about the collating sequences of character sets? For instance, in the ASCII character set, the letter character 'a' is less than the character 'b', and the character 'b' is less than 'c'. Using that knowledge, you can construct a method that checks if the given string is in sequence:

	/*
	 * private static boolean matchesPattern(String string)
	 *
	 * Return Value: Returns true if the string matches the pattern described below.
	 *				 Otherwise, it returns false.
	 *
	 * Description: Determines if the string contains only lowercase a's, b's,
	 *				and c's, and that the letters are in sequence, i.e, all a's
	 *				come before b's, and all b's come before c's. For example, the
	 *			  string "aaabbaacccc" would yield a return value of false because
	 *				some b's in the string are illegally followed by a's.
	 */
	private static boolean matchesPattern(String string)
	{
		char last;
		int i;

		/* Make sure the string contains only lowercase a's, b's, and c's */
		if(!containsOnlyABCs(string))
			return false;

		/* Get the first character in the string */
		last = string.charAt(0);

		/* Loop through the next characters in the string */
		for(i = 1; i < string.length(); i ++) {
			char next;

			/* Get the next character in the string */
			next = string.charAt(i);

			/* The next character in the string has to be greater than or equal the last
			   character in order for this pattern to match */
			if(next < last)
				return false; /* If the next character is less than the last, the pattern
								 doesn't match. Return false */

			last = next; /* The next character now becomes the last so that it can be
							compared to the "new" next */
		}

		return true;
	}


If you don't understand the last code snippet, please feel free to ask any questions.



Thank you so much! I understand the problem and strings even better now. If you can, can you please explain to me why exactly the character 'a' is less than the character 'b' ? Is it because of their placement in the alphabet, meaning the character 'a's value is 0 and 'b's is 1?
Was This Post Helpful? 0
  • +
  • -

#9 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 08:51 PM

ASCII value, the letter A<B because B comes after A, if you google ASCII chart youll find that other symbols are either heaver or litter.
Was This Post Helpful? 0
  • +
  • -

#10 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: In an input consisting of all 'a', 'b', and 'c'

Posted 18 March 2009 - 08:57 PM

Quote

Thank you so much! I understand the problem and strings even better now. If you can, can you please explain to me why exactly the character 'a' is less than the character 'b' ? Is it because of their placement in the alphabet, meaning the character 'a's value is 0 and 'b's is 1?


Sure. Do you know what a character set is?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1