1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java Exercise Help

Discussion in 'Java' started by hanleyhansen, Mar 23, 2009.

  1. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I'm working on this Java exercise from one of my Java books. Here's the exercise:

    Write a method called isFlush that takes an array of Cards as a parameter and that returns true if the hand contains a flush, and false otherwise. A flush is a poker hand that contains five or more cards of the same suit.

    Code:
    class Card {
    
        int suit, rank;
        int index = 0;
        Card[] deck = new Card [52];
        
        public Card () {
            this.suit = 0; this.rank = 0;
        }
    
        public Card (int suit, int rank) {
            this.suit = suit; this.rank = rank;
        }
    
        public void main(String[] args) {
            for (; suit <= 3; suit++) {
                for (int rank = 1; rank <= 13; rank++) {
                    deck[index] = new Card (suit, rank);
                    index++;
                }
            }
            [B]boolean flush = isFlush();[/B]
            if (flush = true){
                System.out.println("Congratulations! You have a flush!");
            }
            else {
                System.out.println("Sorry, you do not have a flush.");
            }
        }
        
        public boolean isFlush(Card[] x) {
            int count = 0;
            for(index = 0; index < x.length; index++){
                boolean comp = compareCard(x[index], x[index + 1]);
                if (comp = true){
                    count++;
                }
            }
            if (count >= 5){
                return true;
            }
            else{
                return false;
            }
        }
        
        public boolean compareCard(Card c1, Card c2){
            if (c1.suit != c2.suit){
                return false;
            }
            return true;
        }
    
    }
    The part I put in bold is the part I'm having a problem with. The program looks good but how can I feed the isFlush method a hand of cards from the deck? Also, if you guys see anything else wrong with the code please let me know that way I can fix it before I submit it to my Java instructor.
     
  2. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I think I kind of fixed my problem. Here's my code:

    Code:
    public class Card {
    
        int suit, rank;
        int index = 0;
        Card[] deck = new Card [52];
            
        public Card () {
            this.suit = 0; this.rank = 0;
        }
    
        public Card (int suit, int rank) {
            this.suit = suit; this.rank = rank;
        }
    
        public void main(String[] args) {
            for (suit = 0; suit <= 3; suit++) {
                for (rank = 1; rank <= 13; rank++) {
                    deck[index] = new Card (suit, rank);
                    index++;
                }
            }
            isFlush(deck,6);
        }
    
        public boolean isFlush(Card[] x, int y) {
            int count = 0;
            for(index = 0; index < y; index++){
                boolean comp = compareCard(x[index], x[index + 1]);
                if (comp = true){
                    count++;
                }
            }
            if (count >= 5){
                System.out.println("Congratulations! You have a flush!");
                return true;
            }
            else{
                System.out.println("Sorry, you do not have a flush.");
                return false;
            }
        }
    
        public boolean compareCard(Card c1, Card c2){
            if (c1.suit != c2.suit){
                return false;
            }
            return true;
        }
    
    }
    But now I'm getting this error:

    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Exception in thread "main" Java Result: 1

    I know I have to make my main method static but how can I reference my non static variables from main? Or what other workaround can I use to get this to work? Any help is appreciated.
     
  3. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I've done some rearranging but still can't get the program working. Here's my code:

    Code:
    public class Main {
    
        public static void main(String[] args) {
            Card.fillDeck();
        }
    
    }
    
    class Card {
    
        int suit, rank;
        int index = 0;
        Card[] deck = new Card [52];
    
        public Card () {
            this.suit = 0; this.rank = 0;
        }
    
        public Card (int suit, int rank) {
            this.suit = suit; this.rank = rank;
        }
    
        public boolean isFlush(Card[] x, int y) {
            int count = 0;
            for(index = 0; index < y; index++){
                boolean comp = compareCard(x[index], x[index + 1]);
                if (comp = true){
                    count++;
                }
            }
            if (count >= 5){
                System.out.println("Congratulations! You have a flush!");
                return true;
            }
            else{
                System.out.println("Sorry, you do not have a flush.");
                return false;
            }
        }
    
        public boolean compareCard(Card c1, Card c2){
            if (c1.suit != c2.suit){
                return false;
            }
            return true;
        }
    
        public void fillDeck(){
            for (suit = 0; suit <= 3; suit++) {
                    for (rank = 1; rank <= 13; rank++) {
                    deck[index] = new Card (suit, rank);
                    index++;
                    }
                }
                isFlush(deck,6);
        }
    }
    
    I need a work around to calling a non-static method from main. Can anyone help me?
     
  4. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    Problem Solved!
     

Share This Page