Method calls with arrays (origionally posted in wrong place)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Method calls with arrays (origionally posted in wrong place)

  1. #1
    Join Date
    May 2005
    Posts
    15

    Method calls with arrays (origionally posted in wrong place)

    sry i had origionally posted this in the java developers section of the forum

    alright i was wondineg if it is possible to call methods with an array
    here is why i need this to happen:

    i am calling a series of methods that all have the same return time (boolean)
    and once the return from one of these method becomes true i need to stop and return that
    a "driver" is calling all of these methods

    (it's a poker game)

    it first calls isRoyalFlush....then all the way down to isHighCard
    i realize that they dont' truly have to be seperate methods, but for testing/creating it would be much easier. once they return a true value, i want the search to break and return

    could anyboyd tell me how to, or an alternate way to doing this?

    if that makes no sense:
    public void Play()
    {
    Method[]arr=new Method[/*not sure what goes here*/]; /*maybe like the method names, i'm thinking of somethign like an onClick in javascript for buttons where i would do onClick="isRoyalFlush()"*/
    boolean isType=false;
    for(int i=0;i<9;i++)
    {
    isType=arr[i];
    if(isType)
    //print and do all that stuff
    }
    }
    public boolean isRoyalFlush()//this would be cell 0 in the array
    {

    }
    public boolean isStraightFlush()//cell 1....etc
    {
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    You can't do what you're asking for, just call the methods seperately.

  3. #3
    Join Date
    May 2005
    Posts
    15
    are you sure? b/c i was searching for it and i found
    java.lang.reflect........
    http://www.devx.com/Java/Article/27798/0/page/2

    i just don't understand how it is used, or when to use it

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Using reflection here would be overkill, i think.

    ..but you could have solved the problem using those methods. I your case
    I would have done something like this:

    Code:
    public class Poker {
      public final static int ROYAL_STRAIGHT_FLUSH=9;
      public final static int STRAIGHT_FLUSH=8;
      // etc. etc.
      public final static int HI_CARD=0;
      public int getHand() {
        int handType;
        for (handType=ROYAL_STRAIGHT_FLUSH; handType>=HI_CARD; handType--) {
          switch (handType) {
            case ROYAL_STRAIGHT_FLUSH:
              if (isRoyalFlush()) return handType;
              break;
            case STRAIGHT_FLUSH:
              if (isStraightFlush()) return handType;
              break;
            // etc. etc.
          }
        }
      }
    }
    eschew obfuscation

  5. #5
    Join Date
    Oct 2004
    Posts
    311
    another way to do this:

    Code:
    public final static int ROYAL_STRAIGHT_FLUSH=9;
    public final static int STRAIGHT_FLUSH=8;
    // etc. etc.
    public final static int HI_CARD=0;
    
    int getHand() {
      return isRoyalFlush();
    }
    
    int isRoyalFlush() {
      if (/* hand is royalFlush*/)
      {
        return ROYAL_STRAIGHT_FLUSH;
      }
      else
      {
        return isStraightFlush();
      }
    }
    
    int isStriaghtFlush() {
      if (/* hand is straightFlush*/)
      {
        return STRAIGHT_FLUSH;
      }
      else
      {
        return isFullHouse();
      }
    }
    
    etc.

  6. #6
    Join Date
    May 2005
    Posts
    15
    yes i realize this, but i am trying 2 do it with as little typing as possible (and this looked like a cool way to do it) i realize that it is unnecessary though

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center