Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: User Defined Methods

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default User Defined Methods

    I have to write a Method that is of the type boolean contains string1, string 2, and Int(N)

    The method must Checks if a given percentage of string2 (given by N) is a substring of string1

    can anyone please help ME???

    All I have so far is

    public static boolean partialmatching(String Str1, String Str2, IntN)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Defined Methods

    I think it's a typo, but you need to declare int data types lower case, and there is a missing space. Also, general convention in java is to name variables/methods starting with lower case letters, then each word is either separated by a underscore or more commonly just capitalized.

    Here's the basic lay-out for the method you want.
    public static boolean partialMatching(String str1, String str2, int n)
    {
         // code goes in here
    }

    Did you want us to help you come up with an algorithm to implement this method, or did you have that part figured out?

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    an algorithm would be nice cause i have this so far
    public static boolean partialmatching(string str1, string str2, Int N)
    {
      int count = 0
        for (int i = 0; i < Str1.length(); i++)
      {
         if (Str2.charAt(i) == Str1.charAt(0))
               for(int j =0; j < Str2.length(); j++)
         {

    i just dont know how to count how many characters from the second string are in the first string and put it into code i am stuck on that part
    Last edited by helloworld922; October 19th, 2009 at 10:17 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Defined Methods

    for(int i = 0; i < str1.length(); i++)
    {
         for (int j= 0; j < str2.length();j++)
         {
              if (i+j >= str1.length())
              {
                   // test wrap-around bit
                   if (str1.charAt(i+j - str1.length()) != str2.charAt(j))
                   {
                        break;
                   }
              }
              else
              {
                   // test not wrap-around bit
                   if (str1.charAt(i+j) != str2.charAt(j))
                   {
                        break;
                   }
              }
         }
    }

    You'll have to figure out how to get this to return what you want, but here's a fairly basic program flow to get you started

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    inside the curlys where you put test wrap around bit in comments what did you mean by that?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Defined Methods

    Oh wow, haha that's the wrong topic to be posting that in. For some reason I thought this was your other topic about substrings

    So the letters in str2 can appear anywhere in str1?

    for (int i = 0; i < str2.length(); i++)
    {
         for (int j = 0; j < str1.length(); j++)
         {
              if (str2.charAt(i) == str1.charAt(j))
              {
                   // found a matching letter, increase the count and replace that letter with a space
                   break;
              }
         }
    }
    return ((double)count)/str2.length();

  7. #7
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    okay sorry I am asking so many questions but I'm stuck on this method for the longest time i greatly appreciate the time your taking to help me

    public static boolean partialmatching(string str1, string str2, int N)
    {
    boolean partialmatch = false;
     
    lastposition = (str2.length() * N) / 100
     
        for (int i = 0; i < Str1.length(); i++)
      	{
         if (Str2.charAt(i) == Str1.charAt(0))
               for(int j =0; j < Str2.length(); j++)
        	      {
    	     if (Str2.charAt(i) == Str1.charAt(j))

    that is what i have came up with so far
    not sure if its right it looks to me as if it is
    but the only thing i need is how can i get the percentage of string2 (given by N) is a substring of string1

    if you can show me how to input into this code and if this code will work please?

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    yes the letters can be found anywhere but as long as their in consecutive order

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Defined Methods

    err... oops. The return ((double)count)/str2.length(); should be the percentage of common letters between str1 and str2. Just compare that with N and return true or false appropriately.

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    what do you think about this code can you look over it I think I might be done
     
    public static boolean partialmatching(string str1, string str2, int N)
    {
    for (int i = 0; i < str2.length(); i++)
    {
         for (int j = 0; j < str1.length(); j++)
         {
              if (str2.charAt(i) == str1.charAt(j))
              	count++;
     
              }
         }
     
    	return (((double)count)/str2.length());
     
    	if ((((double)count)/str2.length()) == N)
    		return true;
    	else
    		return false;
    }

  11. #11
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Methods

    If you can review this code please see what you think about it

    please give me some feedback?

    public static boolean partialmatching(string str1, string str2, int N)
    {
    int count = 0;
     
    for (int i = 0; i < str2.length(); i++)
    {
         for (int j = 0; j < str1.length(); j++)
         {
              if (str2.charAt(i) == str1.charAt(j))
              	count++;
     
              }
         }
     
    	return (((double)count)/str2.length());
     
    	if ((((double)count)/str2.length()) == N)
    		return true;
    	else
    		return false;
    }

  12. #12
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: User Defined Methods

    You have a pre-mature return statement. remove the return (((double)count)/str2.length());

    Also, I think you wanted to check if it matched by atleast that percentage, right?

    If so, you need to change your if conditional to test >= intead of just ==

Similar Threads

  1. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 04:00 AM
  2. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 05:23 AM
  3. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 05:29 AM
  4. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 11:08 AM