Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Card Class (Setting string rank value. 1 = "Ace" etc)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Card Class (Setting string rank value. 1 = "Ace" etc)

    My Current Card Class


    public class Card

    {
    private String suit;
    private int value;

    public void setSuit(String newSuit) //Setting suit
    {
    suit = newSuit;
    }

    public String getSuit() //Getting suit
    {
    return suit;
    }

    public void setValue(int newValue) //Setting value
    {
    value = newValue;
    }

    public int getValue() //Getting value
    {
    return value;
    }

    }

    My Current War2 Class


    public class War2
    {
    public static void main(String[] args)
    {
    int myValue, myValue1, mySuit1, mySuit2;

    Card myCard = new Card();
    Card myCard2 = new Card();

    final int CARDS_SUIT = 4;
    mySuit1 = ((int)(Math.random() * 100) % CARDS_SUIT + 1); //Generating random number for 1st Card Suit

    if (mySuit1 == 1) //Nested if to set random suit to 1st Card
    myCard.setSuit("Spades"); //Setting setSuit in the Card Class
    else
    if (mySuit1 == 2)
    myCard.setSuit("Hearts");
    else
    if (mySuit1 == 3)
    myCard.setSuit("Diamond");
    else
    if (mySuit1 == 4)
    myCard.setSuit("Clubs");

    final int CARDS_SUIT1 = 4;
    mySuit2 = ((int)(Math.random() * 100) % CARDS_SUIT1 + 1); //Generating random number for 2nd Card Suit

    if (mySuit2 == 1) //Nested if to set random suit to 2nd Card
    myCard2.setSuit("Spades"); //Setting setSuit in the Card Class
    else
    if (mySuit2 == 2)
    myCard2.setSuit("Hearts");
    else
    if (mySuit2 == 3)
    myCard2.setSuit("Diamond");
    else
    if (mySuit2 == 4)
    myCard2.setSuit("Clubs");

    final int CARDS_IN_SUIT = 13;
    myValue = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1); //Generating random number for cards
    myValue1 = ((int)(Math.random() * 100) % CARDS_IN_SUIT + 1);

    myCard.setValue(myValue);
    myCard2.setValue(myValue1);

    System.out.println("My Card is the " + myCard.getValue() + " of " + myCard.getSuit()); //Displaying Card Details
    System.out.println("Your Card is the " + myCard2.getValue() + " of " + myCard2.getSuit());

    if (myCard.getValue() > myCard2.getValue()) //Nested if to display Winner or Tie
    System.out.println("I win");
    if (myCard.getValue() < myCard2.getValue())
    System.out.println("You win");
    else if (myCard.getValue() == myCard2.getValue())
    System.out.println("Its a tie");


    }

    }

    My Current Output


    My Card is the 4 of Spades
    Your Card is the 13 of Clubs
    You win

    My Current Problem


    I am trying to get within the Card class setValue() method, besides
    setting the numeric value, also set the string rank value as follows.
    Assuming Ace is the lowest value.

    Numeric value String value for rank

    1 “Ace”
    2 through 10 “2” through “10”
    11 “Jack”
    12 “Queen”
    13 “King”

    And achieve an outcome like this :

    My Card is the Ace of Spades
    Your Card is the 2 of Clubs
    You win

    Can I please get an idea of how to go about it. Confused


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Card Class (Setting string rank value. 1 = "Ace" etc)

    So, for integer values 1, 2, ..., 13 you want to associate Strings with the values

    "Ace" // 1
    "Deuce" // 2
    "Trey" // 3
    "Four"
    .
    .
    .
    "Queen" // 12
    "King" // 13



    How about making an array of Strings with the names that you want to associate with the integer values? Then, somehow, use the int value of the card (its rank) to index into the array?

    If you would rather see "2 of Clubs" and not "Deuce of Clubs" then it's a simple matter of changing the corresponding String array element. None of my card-playing friends would ever say "The 2 of clubs," but maybe your requirements are different.

    The bottom line is:
    If you convert all of the rank values to Strings you don't have to worry about special cases like Ace, Jack, Queen, King.


    Cheers!

    Z
    Last edited by Zaphod_b; October 9th, 2012 at 03:32 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Card Class (Setting string rank value. 1 = "Ace" etc)

    Thanks for reply ... Haven't got to Arrarys yet its the next chapter we are looking at. Is the another way to do it using Characters, Strings and Stringbuilder ?

    Like using valueOf(int i) etc ???

    Thanks

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Card Class (Setting string rank value. 1 = "Ace" etc)

    Quote Originally Posted by ashboi View Post
    Thanks for reply ... Haven't got to Arrarys yet ...
    Then, I guess you can use a modest cascade of "if(){} else{}" statements to translate values to Strings so that you can print Ace, Jack, Queen and King as Strings instead of int values 1, 11, 12, and 13. For the other ranks, you can just print the numerical value.

    I wouldn't use things that haven't been covered in class until I checked with my instructor. By not using arrays yet, maybe the idea is that you will appreciate arrays more when they do get to be covered.

    I mean, since you can't simply print the int returned by getValue(); you have to test for the four special cases to decide what to print.

    Cheers!

    Z
    Last edited by Zaphod_b; October 9th, 2012 at 11:33 PM.

Similar Threads

  1. [SOLVED] Card Program"War" Class help
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 16th, 2012, 08:43 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 10:24 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM

Tags for this Thread