Results 1 to 4 of 4
  1. #1
    Christa912 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Calling a method from another class without losing data?

    So I'm doing a lab for a BlackJack program, and it has a lot of classes so I'm not going to post the code here. (i.e. BlackJackPlayer, BlackJackHand, Decks, Card, BlackJack, BlackJackStrategy, and a gui, etc. ) The problem I'm having is that at one point in BlackJackHand class you have to split the hand, so you have to create two new cards to send to the two new BlackJackHands. But the deck to be used is in the deck class(which has the deal method). I don't see how to get the next card without creating a totally new and useless deck. But I'm obviously confused.
    I'm sorry if I made it sound confusing, also.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,538
    Rep Power
    5

    Default Re: Calling a method from another class without losing data?

    Perhaps a shared instance of Deck might help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calling a method from another class without losing data?

    Why not add just another class, a Table; it 'owns' the players and the deck of cards and everything else refers to a Table object (or it has a public static Table member in the Table class itself (a singleton)).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Christa912 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default Re: Calling a method from another class without losing data?

    Because your supposed to use the code that's already half-written, and just fill in where it says to. Although, its poorly documented.
    "If the BlackJackHand can be split, this method will split the BlackJackHand. The two Cards in the current BlackJackHand become one Card each for two new BlackJackHands. The next two Cards for the two new BlackJackHands are passed in as parameters. You will need to create new BlackJackHands." These are the instructions.
    The split method in the BlackJackHand class looks like this as of now. The deck.deal() can't be used with a completely new instance of a Decks

    public BlackJackHand[] split(Card card1, Card card2)
    {
    BlackJackHand[] split = null;


    if (canSplit())
    {

    BlackJackHand[] hand1 = new BlackJackHand(card1, //deck.deal());
    BlackJackHand[] hand2 = new BlackJackHand(card2, //deck.deal());




    }

    return split;
    }

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 9
    Last Post: 12-02-2011, 01:36 PM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •