Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Exercise Help (http://www.go4expert.com/forums/java-exercise-help-t16628/)

hanleyhansen 23Mar2009 05:45

Java Exercise Help
 
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++;
            }
        }
        boolean flush = isFlush();
        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.

hanleyhansen 23Mar2009 07:10

Re: Java Exercise Help
 
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.

hanleyhansen 25Mar2009 04:26

Re: Java Exercise Help
 
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?

hanleyhansen 27Mar2009 18:36

Re: Java Exercise Help
 
Problem Solved!


All times are GMT +5.5. The time now is 15:34.