Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java im having trouble with making a deck of cards.

zyzz28

Coder
Im not sure where to start, any tips help. Thank you in advance.

Write a class(Cards.java)that defines an enumerated type named Rank with values ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king. The main method should do the following:

1. Declare variables highCard, faceCard, card1, and card2 of type Rank.

2. Assign highCard to be an ace, faceCard to be a jack, queen or king (your choice), and card1 and card2 to be two different numbered cards (two through ten - your choice).

3. Print a line, using the highCard and faceCard objects, in the following format:

A blackjack hand: ace and ....

The faceCard variable should be printed instead of the dots.

4. Declare two variables card1 Val and card2Val of type int and assign them the face value of your card1 and card2 objects. Use your card1 and card2 variables and the ordinal method associated with enumerated types. Remember that the face value of two is 2, three is 3, and so on so you need to make a slight adjustment to the ordinal value of the enumerated type.

5. Print two lines, using the card1 and card2 objects and the name method, as follows:

A two card hand: (print card1 and card2)

Hand value: (print the sum of the face values of the two cards)
 
I think the list that you have there is a good place to start. Are you asking how to write the code to get what you want? Have you tried to code this already?

Perhaps @Krusty the Senile can give you a hand. I believe he's familiar with Java!
 
yes, but im just having trouble getting started, this is the first thing I have really tried to do with java. but yes im asking how to write the code to get this. this is a practice question in a java book, however it doesn't show me a way to do it.
 
Sorry, just saw this. Let me get something together ...

As a simple start, it seems that it should look something like this (in a file called Cards.java):

Code:
class Cards {

    enum Rank {
        ACE,
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN,
        JACK,
        QUEEN,
        KING
    }
    
    /**
     * Main method.
     *
     * @param args program arguments.
     */
    public static void main(String[] args) {
        Rank highCard;
        Rank faceCard;
        Rank card1;
        Rank card2;
    }
}

You can start by trying to answer part 3.

I'm not sure what you've covered so far in regards to enums, but it's possible to set or override the toString() value per enum, and even set constructors so you can initialise each enum value. I think this is what you'd need to do to set specific values for each enum value. I think there's a good enough example in the Java Tutorial docs: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Let me know how you go.
 
I am going back and fourth with eclipse, and drjava. I like eclipse better most of time but I still have yet to figure it out good enough to use only it. Thank you for your help. Im taking my first online class, and I have never even looked a line of code before this semester, so its all new to me.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom