1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

My First Programming Question (Java)

Discussion in 'Programming & Webmastering' started by spy2520, Oct 28, 2009.

  1. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    Ok guys, this isnt for school. I'm just bored and and messing with code.

    I have a card class, i have a face card class that extends card.

    If i have an array of type card that also contains face cards (its a deck of cards), can i access the a parameters that are exclusive to the face card class in the array?
     
  2. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    i just solved my problem another way, but i'd still like to hear different answers.

    Just in case mine is more than necessary.
     
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.88/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    Someone will be along soon with more Java experience than me.
    My suggestion is that you post the code that worked for you, and let everyone give their input as to how you did it and any ideas on improving it.
    That way not only will you get suggestions, but other people can peruse the code in case they want to do something similar. :toast:
     
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,945 (6.25/day)
    Thanks Received:
    3,785
    Location:
    IA, USA
    You have to change the access modifiers on properties in the Face class to "protected" if you want an inherited class to have access to it. "private" will hide it from everything except itself.

    @Kreig: It's the same as C#. XD
     
    Kreij says thanks.
    Crunching for Team TPU
  5. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    I'll post the code in a bit. But what i'm looking at is "public enum faces {king, queen, jack, ace}" and subsequently "public faces face;" as part of FaceCard. Are you saying i should use protected instead of public?
     
  6. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,945 (6.25/day)
    Thanks Received:
    3,785
    Location:
    IA, USA
    Enums are static/const. In order to access an Enumerator, you have to go through the class name. For example, if Faces is in the class "Enumerators", you'd have to access Faces via "Enumerators.Faces". You can access it directly from inside of the Enumerators class. For example, "Enumorators.MyMethod()" could access Faces just by using "Faces".


    If your array is accessible, you can access everything that's accessible about it including the items it contains.
     
    Crunching for Team TPU
  7. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    I'm currently using FaceCard.Faces. But if i wanted to do something as simple as print the array, how could i print the face, suit, and value of every card considering that only 16 cards will have a face. It is easy with corresponding toString() methods, but if i wanted to do anything more than print...i just gotta stare at it for another hour and everything will make sense. Thats how it usually works.
     
  8. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,945 (6.25/day)
    Thanks Received:
    3,785
    Location:
    IA, USA
    You should have a class "Card" which defines the value of the card, the suit of the card, and any other attributes. In "Card" override ".toString()" to output the Card how you want it to be displayed.


    I think you use this to override it:
    Code:
    public override string toString()
    {
    }
     
    Crunching for Team TPU
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.88/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    No. Make the "Card" class Protected so that only derived classes (eg. FaceCards) have access to it. Depending on what you are doing, you may want to derive classes for the face cards from the base card class (eg. public class King extends Card) and put the properties and methods in there for the "King" card.

    When you are creating classes, each subclass should build upon the presceding base class. So you would want a card class that definds everything for a card, then perhaps a face card class that defines the things common to all face cards, and then a class for each individual face card.
     
  10. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    As far as this program goes, all face cards have the same attributes besides the face. Except the ace of course.
     
  11. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.88/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    That's fine, so you have your card base class which has thing common to all cards.
    You have a face class which is derived from card, which has what facce cards have in common.
    You have a regular card class, and an Ace classs derived from cards which define their specific properties and methods.
     
  12. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    i see. Thats alot of classes. So if done properly, i can declare an array of cards that could hold all these types?

    As in a CardDeck class that contains an array of 52 cards.
     
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.88/day)
    Thanks Received:
    5,616
    Location:
    Cheeseland (Wisconsin, USA)
    You can use any type of collection object (array, List, etc.) to hold the 52 cards.
    What game are you thinking about? That can make a difference how many classes you need.
     
  14. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.16/day)
    Thanks Received:
    16
    blackjack for instance. simple as can be.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page