Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 13th, 2006, 09:45 PM
Authorized User
 
Join Date: Jul 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ironchef
Default Java is KILLING me!!! I really need help!!!!!

If your reading this...thanks!

ok...so I'm trying to add a method that is supposed to return a value based on the poker rank of a hand of cards.0 for the lowest hand(no hand that is) and 10 for the highest hand(royal flush). I have already defined 5 .java source files(2 enum, 3 class) that are desinged to create a deck.

i have 2 main problems:

1. Which class do I put the method in?
2. How exactly do i design this method?

If some one could help me i would really appreciate it!
Here are the classes:

Suit.java

public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}


Rank.java

public enum Rank {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING, ACE
}


Card.java

public class Card implements Comparable<Card> {

    //compare two cards
    public int compareTo(Card card) {
        if(suit.equals(card.suit)) {
            if(rank.equals(card.rank)) {
                return 0;
            }
            return rank.compareTo(card.rank) < 0 ? -1 : 1;
        } else {
            return suit.compareTo(card.suit) < 0 ? -1 : 1;
        }
    }

    public String toString() {
        return rank + " of " + suit;
    }

    public Card(Rank rank, Suit suit) {
        this.suit = suit;
        this.rank = rank;
    }

    public Suit getSuit() {
        return suit;
    }

    public Rank getRank() {
        return rank;
    }

    private Rank rank;
    private Suit suit;
}


Hand.java

import java.util.Vector;
import java.util.Collections;

public class Hand {

    public Hand sort() {
        Collections.sort(hand);
        return this;
    }

    //add card to hand
    public void add(Card card) {
        hand.add(card);
    }

    public String toString() {
        StringBuilder str = new StringBuilder();
        for(Card card : hand) {
            str.append("\n" + card);
        }
        return str.toString();
    }

    private Vector<Card> hand = new Vector<Card>();
}


CardDeck.java

import java.util.Stack;
import java.util.Collections;

public class CardDeck {
    //create a deck of 52 cards
    public CardDeck() {
        for(Suit suit : Suit.values())
         for(Rank rank : Rank.values())
            deck.push(new Card(rank, suit));
    }

    //deal a hand
    public Hand dealHand(int numCards) {
        Hand hand = new Hand();
        for(int i = 0; i < numCards; i++) {
            hand.add((Card)deck.pop());
        }
        return hand;
    }

    //shuffle deck
    public void shuffle() {
        Collections.shuffle(deck);
    }


    private Stack<Card> deck = new Stack<Card>();
}


if you have any questions here's my e-mail:
[email protected]
IronChef - http://www.freewebs.com/cool_recipes
__________________
<b>IronChef</b> - http://www.freewebs.com/cool_recipes





Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript started killing flash nairaby HTML Code Clinic 1 August 28th, 2007 11:47 PM
Java is killing me!! Pls Help!!!! il nonno Java Basics 7 September 24th, 2006 10:29 PM
Datareader problem, killing me joer80 ADO.NET 1 March 23rd, 2006 06:44 PM
This is killing me RobCarter VB Databases Basics 1 January 18th, 2006 01:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.