Results 1 to 10 of 10
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Help with accessing static method

    Hi folks

    I have a very simple class that creates two HashSets (one is a copy of the other). Reason I'm doing this is that the second HashSet will perform some further computations that I'm not attempting at this point in time. Anyway, here's the code:
    Java Code:
    import ou.*;
    import java.util.*;
    
    /**
     * Class Consecutives - A utility class for removing a consecutive pair
     * of integers from a set of integers.
     * 
     * @author (M255 CT) 
     * @version (1.0)
     */
    
    public class Consecutives
    {
    
       /**
        * A class method to return a set containing the integers that are the 
        * elements of the array provided as the argument. The result can be used
        * as the actual argument to the removeConsecutive() method).
        */
       public static Set<Integer> numberSet(int[] integers)   
       {
          Set<Integer> numSet = new HashSet<Integer>();
          for (int eachInteger : integers)
          {
             numSet.add(eachInteger);
          }
          return numSet;
       }
       
       public static Set<Integer> removeConsecutives(int[] numberSet)
      
       {
          Set <Integer>set1 = new HashSet<Integer>();
          set1.addAll(numberSet(numberSet));
          return  set1;
       }
    
    }
    Now, If I create an array if integers using the following:
    Java Code:
    int[] numbers = {1, 17, 6, 2, 5, 9};
    I now want to populate my first HashCode with the array values, then execute the second method with the result of the first, sending the following message:
    Java Code:
    Consecutives.removeConsecutives(Consecutives.numberSet(numbers));
    However, I get the following error:
    Java Code:
    Semantic error: : Static method removeConsecutives( java.util.HashSet ) not understood by class'Consecutives'
    What am I doing wrong? If I send a message directly to the Class method using the array it works, for example sending the message:
    Java Code:
    Consecutives.removeConsecutives(numbers);
    returns the values you would expect.

    I know this will be something very simple but I'm stumped.

    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your calling the removeConsecutives method by passing a HashSet into its parameter, but as written it can only accept an array of ints. As an aside, I'm not really sure what you're trying to do with this method as it doesn't appear to be removing anything.

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Hi Fubarable

    Your right is saying that the method at this point isn't doing anything, I still have to complete the code (will be moving onto that later).

    The method (as you rightly say) method does accept and array of ints as it's argument which is correct. I will need to provide the argument for the method with a variety of ints that are sent directly. It the argument decleration the reason I'm getting the symantic error?

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, you are getting the error because you are not calling the method with an array of ints, that's why.
    Yes, I think I'm getting what your saying. I need the output of numberSet to be cast as an array before the method removeConsecutives will work? Indeed, any argument for removeConsecutives needs to be an array?

    Thanks again

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jazzermonty View Post
    Yes, I think I'm getting what your saying. I need the output of numberSet to be cast as an array before the method removeConsecutives will work?
    No, you can't simply cast a Set as an array. Makes no sense. What you need to decide first is what this method is supposed to do.


    Indeed, any argument for removeConsecutives needs to be an array?
    You are constrained by the method signature which you yourself have created.

  7. #7
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No, you can't simply cast a Set as an array. Makes no sense. What you need to decide first is what this method is supposed to do.




    You are constrained by the method signature which you yourself have created.
    Hi Fubarable

    Think I've got it now, change the method to the following:

    Java Code:
     public static Set<Integer> removeConsecutives(HashSet <Integer> newSet)
      
       {
          Set <Integer>set1 = new HashSet<Integer>();
         for (int anInt : newSet)
         {
            set1.add(anInt);
          }          
          return  set1;
       }
    Which makes sence now that I think about it. Thanks for your help.:)

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It still doesn't make sense to me. The method name states that it removes consecutive elements, and I don't see anything being removed by this method.

  9. #9
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It still doesn't make sense to me. The method name states that it removes consecutive elements, and I don't see anything being removed by this method.
    Yes, that's because I've not written that part yet, as per post #one. Perhaps it's just the way I like to tackle these things. I break them down to the smallest components as possible and build up the method from there. If your interested the method will eventually subtract each int within the set from each other int, and when the answer is one (i.e donating consecutive numbers) this will populate another set. Once this new set has been populated, this will be the mechanisim I will use to remove the elements from the original set. Confused? I know I am.

    Thanks for your help BTW. It did point me in the right direction. I won't mark this post as solved yet and will post the final code once I've completed it (just in case your interested).

    Cheers:D

  10. #10
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Fubarable

    Thanks once again for your help. As promised here is my completed code. Your comments would be very much appriciated.

    Java Code:
     public static Set<Integer> removeConsecutives(HashSet <Integer> newSet)  
       {
         TreeSet <Integer>set1 = new TreeSet<Integer>();
         TreeSet <Integer>set2 = new TreeSet<Integer>(set1);
         for (Integer anInt :newSet)
          {
            set1.add(anInt);         
          }     
          for ( Integer i : set1) 
          {
          if (set1.higher(i) !=null)      
               {           
                  if((set1.higher(i) - i) ==1)
                  {
                     OUDialog.alert("Consecutive Numbers " + i + " and " + set1.higher(i) + " have been removed");
                     set2.add(i);
                     set2.add(set1.higher(i));
                  }             
               }  
           }
          set1.removeAll(set2);
          return  set1;
       }
    Thanks

Similar Threads

  1. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  2. Replies: 4
    Last Post: 05-12-2010, 11:05 AM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •