Results 1 to 19 of 19
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default help on different ways to construct objects...

    all,

    I'm a tad bit disappointed that OOP in Java is so artsy based. I've got an assignment here that's been hammered out a million times already, but I need to ask a brand new question about it. It's a blackjack game that's required.

    My question isn't about how to do it, but rather WHY I would do it one way over the other. So for me, I have a data mindset only...e.g. - just numbers, numbers, numbers. So if I take just the DECK() object, could I produce it like the following? and if I could, why would I want to do it this way?? Opinions please??

    Java Code:
    public class Deck
    {
       public Deck()
       {
          int allCards[] = new int[51];
       }
    }
    I know that's fairly simple. Basically the constructor is nothing more than 52 elements that represent all of the card values. The suits in this example can be determined by finding "mod" values on them. Or other methods would work too.

    I know I *could* do this, but my question for you guys is: *Why* would I do it this way? Would professionals actually appreciate things like this? This is not necessarily a typical OOP way of programming!

    thanks guys!
    Last edited by ajetrumpet; 04-30-2013 at 04:49 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: help on different ways to construct objects...

    First a question for you: if a deck contains 52 cards why have you created an array with a length of 51?

    Of course there are many different ways to write code to achieve the same result. Which is the best way? Who knows, it is open to debate and often vigorously. In your example you have used an array to hold the "cards" whereas others may choose a List or Set. What you choose to do depends upon personal preference and experience. Personally I would also create a Card class rather than representing them with ints.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    And the advantage of a Card class is you can then encapsulate the Card logic and data there (eg the suit and value), whereas you would need to do all that stuff in whatever class owned that array of yours.

    Keep It Simple. If logically simple concepts (like a Card) require lots of comments to explain how you have created it (which it would do if you were going to go down the mod route) then you've probably taken a wrong turn somewhere.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    And the advantage of a Card class is you can then encapsulate the Card logic and data there (eg the suit and value), whereas you would need to do all that stuff in whatever class owned that array of yours.

    Keep It Simple. If logically simple concepts (like a Card) require lots of comments to explain how you have created it (which it would do if you were going to go down the mod route) then you've probably taken a wrong turn somewhere.
    yeah guys, but you see what I wrote *is* the simple way!! Keep it simple? How? Can you give me an example of a CARD and a DECK please? here's one good example of an entire blackjack game, and it's completely OOP in a professional manner (e.g. - the way it should be laid out and implemented):

    http://math.hws.edu/eck/cs124/javano...-8-answer.html

    FYI - an array of 51 ints was a mistake. sorry.

    See this is the problem I have with programmers and OOP - in this example that we are working with (compared to the example I have just given), it turns out that *more code* in more classes equals *simpler*. Can you guys explain to me why that is the case?

    If I were going to think about this problem in a logic sense, the first thing I would do is break down a deck of cards into the smallest "components" I could think of. does that make sense? and I would then start thinking about those components from the ground up. so here's what makes sense, in terms of thinking about things that way:

    => one int PER suit
    => one int PER value
    => one suit and one value PER card
    => two cards PER hand
    => 52 cards PER deck
    => one deck PER game (this is actually optional, it's one deck until it has to be shuffled)

    so do you guys see where I'm going here? This type of thinking is *exactly* in line with the Internet of Things concept. This is exactly how the originals players in the software industry (and the computer industry for that matter!) created the world we have today!

    So the question still remains, *how* can I streamline my thinking this way so it can be intelligently communicated ot other software developers? do you guys see how I'm trying to make my life simple here? please be honest. again, thanks for your help so far!
    Last edited by ajetrumpet; 04-30-2013 at 11:31 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    Always think about what the code would look like handed over to someone else.
    Java is about that.
    Maintenance costs are the bulk of the cost for software.
    And that maintenance is almost never done by the person who wrote the code.
    Breaking the code down into manageable chunks (a network of interacting objects in the case of OO) means the individual parts can be more easily tested in isolation, they can be altered (if you follow the maxim of "code against an interface") without needing major restructuring throughout the code base, they can be maintained in short.

    But even looking at what you defined just there:
    => one int PER suit
    => one int PER value
    => one suit and one value PER card
    => two cards PER hand
    => 52 cards PER deck
    => one deck PER game
    How is that different to the code you linked to?
    (Though I would use an enum for the suits)
    The first three items are properties of Card.
    The next is a property of a Hand.
    The next is a property of a Deck.
    The last is a property of the Game.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    Breaking the code down into manageable chunks (a network of interacting objects in the case of OO) means the individual parts can be more easily tested in isolation
    So in short, a great way to unit test things?

    Quote Originally Posted by Tolls View Post
    How is that different to the code you linked to?
    (Though I would use an enum for the suits)
    The first three items are properties of Card.
    The next is a property of a Hand.
    The next is a property of a Deck.
    The last is a property of the Game.
    Well it's not really different, per se. It's pretty much the same. And actually, the code I linked to *does* use enums for suits. I guess the issue here is that I'm really not sure how easy I can make this type of thing so I can explain to others when I'm working with them. Does that make sense? I want to be able to communicate my thinking clearly to someone else who doesn't necessarily have a high level of intelligence. (You know the type, very high skillsets but can't necessarily talk intelligently to others).

    I know you don't like code requests, but let me ask you...if you were doing this, what kind of constructor method would you use for CARD? Can you give me a short example here? I want to use it just to get an example of someone else's doing and compare that with what they have said to me (like you have).

    Again, this is all about how I eventually think about doing this work and how easily I can tell someone else how I do it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by ajetrumpet View Post
    So in short, a great way to unit test things?
    In that instance yes. But also maintenance, as I said.

    Quote Originally Posted by ajetrumpet View Post
    Well it's not really different, per se. It's pretty much the same. And actually, the code I linked to *does* use enums for suits.
    Not the bit I looked at. It assigns numbers to 4 constants and uses those.
    Quote Originally Posted by ajetrumpet View Post
    I guess the issue here is that I'm really not sure how easy I can make this type of thing so I can explain to others when I'm working with them. Does that make sense? I want to be able to communicate my thinking clearly to someone else who doesn't necessarily have a high level of intelligence. (You know the type, very high skillsets but can't necessarily talk intelligently to others).
    Then write the code in an OO way.
    All someone else needs to know is there are Cards, which have a Suit, and a value.
    52 of them form a Deck, which can be deal()t from.
    In other words, self documenting code.
    Another part of the reason for OO.

    Quote Originally Posted by ajetrumpet View Post
    I know you don't like code requests, but let me ask you...if you were doing this, what kind of constructor method would you use for CARD? Can you give me a short example here? I want to use it just to get an example of someone else's doing and compare that with what they have said to me (like you have).

    Again, this is all about how I eventually think about doing this work and how easily I can tell someone else how I do it.
    There's a perfectly good example up in your link (except it not using enums for the Suits).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    In other words, self documenting code.
    Another part of the reason for OO.
    self-documenting code? explain a bit please? expand on that?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by ajetrumpet View Post
    self-documenting code? explain a bit please? expand on that?
    Give classes sensible names that identify what the clas represents (not 'ClassA' or 'ClassB', but 'Card' or 'Deck').
    Give variables sensible names that identify what the variable is (not 'a', or 'b', but 'card' or 'hand').
    Give methods sensible names that identify what the method does, so when it is called it is obvious in the code what is going on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Tolls,

    Just to expand on what you said earlier here, I want to make sure I'm thinking about this whole thing in the right manner. Part of the problem here with me getting this is that a lot of times I completely refuse to let machines do the work in any given situation even though that's inevitable. I absolutely have to know what is going on. So, let me see if I can sum this up so I can make myself understand something that is important about objects. Then if you could either verify that I'm right or tell me I'm stupid, that would be great!! So:

    If I have a CARD class with this code...

    Java Code:
       private final int suit; 
       private final int value;
    
       public Card() {
          suit = JOKER;
          value = 1;
       }
    ...and I have a DECK class with this code...

    Java Code:
       private Card[] deck;
    
    public Deck() {
             deck = new Card[52];
    
          int cardCt = 0; // How many cards have been created so far.
    
          for ( int suit = 0; suit <= 3; suit++ ) {
             for ( int value = 1; value <= 13; value++ ) {
                deck[cardCt] = new Card(value,suit);
                cardCt++;
             }
          }
    }
    ...then I'm not understanding why we have a class variable of private Card[] deck;, then an instance of it here: deck = new Card[52];, and then yet *another* operation later to create the individual cards ... deck[cardCt] = new Card(value,suit);??

    I want to see an equivalency of operating on an array of primitives that would produce the same 3 lines of code. Can you verify that the following would be such code please?

    Java Code:
    private int[] array;
    
    public method foo {
            array = new int[52];
    
    for (int x = 0; x < 52; x++)
    {
       array[x] = x //so here, since I am working with an array of 
    //primitives instead of an array of objects I don't need to 
    //instantiate new objects!?
    }
    do you follow this? do you see where I'm comparing the manipulation of an object array with an array of primitive ints? Is that comparison even a fair way to think of this? But the bottom line here is, what is the purpose of assigning 52 new Card() objects to the array slots when the array of card objects were already constructed at the first line of the DECK() constructor??

    I know I'm wrong about this, but I need help understanding why. thanks!
    Last edited by ajetrumpet; 04-30-2013 at 09:07 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    This line:
    Java Code:
    deck = new Card[52];
    only creates an array object on the heap, and gives the reference to the deck variable.
    The 52 slots in that array are all null, which is the default, just as 0 is the default for an int array.
    This code:
    Java Code:
    deck[cardCt] = new Card(value,suit);
    creates a Card object on the heap, and sets the value of deck[cardCt] to point to that object (think C pointer, if that helps).
    This is similar to setting the value of an int array to something other than 0.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    This line:
    Java Code:
    deck = new Card[52];
    only creates an array object on the heap, and gives the reference to the deck variable.
    The 52 slots in that array are all null, which is the default, just as 0 is the default for an int array.
    This code:
    Java Code:
    deck[cardCt] = new Card(value,suit);
    creates a Card object on the heap, and sets the value of deck[cardCt] to point to that object (think C pointer, if that helps).
    This is similar to setting the value of an int array to something other than 0.
    Tolls,

    I know all of that. What I was looking for is something from you that would claim that there is an easier way to do things like this! It just seems a bit too redundant and complex, that's all.

    Can code like this be done in any simper way?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    You need 52 cards, so you need to create 52 of them.
    This creation is in a single place and creates objects that know what they are...you don't need any extra calculations to determine what suit a particular card is, for example.

    This is about as simple as it gets...except it still doesn't use enums for the suits.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    You need 52 cards, so you need to create 52 of them.
    This creation is in a single place and creates objects that know what they are...you don't need any extra calculations to determine what suit a particular card is, for example.

    This is about as simple as it gets...except it still doesn't use enums for the suits.
    that's what I figured Tolls. This is about how simple it gets, and it doesn't get any simpler than this. not a problem. that's all I needed! thanks!

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    Don't forget the enums...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    Don't forget the enums...:)
    are those constructed with the STRUCT keyword? I've seen a lot of pseudo-enums, where people try to do something like it just by creating a constant list. you know, kind of like in that academic example I gave! that's not really an enum.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    No.
    They're enums:
    Java Code:
    public enum SomeEnum {
    THIS_IS_ONE,
    THIS_IS_TWO,
    IGNORE_THIS_ONE
    }
    They're full classes with limited instances.
    In the case of Cards they would be Suits.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: help on different ways to construct objects...

    Quote Originally Posted by Tolls View Post
    No.
    They're enums:
    Java Code:
    public enum SomeEnum {
    THIS_IS_ONE,
    THIS_IS_TWO,
    IGNORE_THIS_ONE
    }
    They're full classes with limited instances.
    In the case of Cards they would be Suits.
    and they are referenced like this right?

    Java Code:
    SomeEnum.THIS_IS_ONE

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help on different ways to construct objects...

    Yep.
    It's all in the tutorials.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Simulating objects selecting objects
    By Asef in forum New To Java
    Replies: 7
    Last Post: 10-27-2012, 07:05 PM
  2. Create objects. What happens to objects on close
    By rru96 in forum New To Java
    Replies: 1
    Last Post: 10-13-2012, 09:46 AM
  3. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:00 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
  •