• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Feedback for Program Creating a Deck of Cards

 
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again


After much tutorial-watching and book reading I've come to realize that for me, a lot of stuff goes in one ear and right out the other unless I get my hands dirty in Eclipse and start thinking critically. So I'm tackling the daunting task of learning Java by going into Eclipse and writing out my own little programs without any cheating (looking on the internet), and then bringing the finished product here to get feedback from you guys. That way I'm actually working my brain and "making it stick" then identifying areas of improvement with your guys'/gals' help.


So I created a program that creates a deck of cards...I got stumped for a little bit and had to restart a few times but eventually I got it right and was actually very pleased with how few lines of code it required; I was expecting more work!


The comments are more for my own reflection and self teaching/self reminding, as I'm sure the workings of the program are obvious to you seasoned Java coders.


Could you please provide some feedback about anything I could've done to make this little program more succinct or more readable, any different approach I could have taken...any feedback basically would be really appreciated. Since I'm doing this all on my own I have no other point of reference to learn better, more acceptable ways of writing programs.


One specific question for feedback I would like to ask: "How would I have achieved the same result with a 2 dimensional array?"




Here's my program, consisting of 2 classes

Deck.java


CardTest.java



The output was as I'd hoped; all of the cards being printed out, with none missing. So there's no errors or bugs that I can see, but I'm just asking for your thoughts in general.

EDIT: I just realized that the while loop is unnecessary since the for loops will execute themselves to the end of their arrays without the need for a counter mechanism (but the "c" variable is still needed to plug the completed card strings into deck at index [c])
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when is c != i?
You can get rid of the c variable completely and just use i, right?

Also, you could use a custom object to generate cards. Each card has-a value and a suit.
 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:when is c != i?
You can get rid of the c variable completely and just use i, right?



No, he can't. i goes up to 4 so if he did that his deck will only have four cards.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Coombs wrote:Could you please provide some feedback about anything I could've done to make this little program more succinct or more readable...


Well, I don't know about succinct, but I will say this: StringsAreBad (←click) - especially for the sort of thing you want. An enum would be much better.

I won't go into all the ins and outs, but suffice to say that your 'suits' definition could be as simple as:and because they're fully-fledged objects, you can add behaviour to them.

HIH

Winston
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Winston and would point you to Item 50 (pp.224-6) of "Effective Java," Second Edition, by Joshua Bloch. (See Item 32 in the First Edition.) Also, see Item 51, which warns against the performance impact of contcatenating strings with the "+" operator. Because strings are immutable (meaning they don't ever change after being created, a feature that seems to bother new programmers, but which ultimately shows itself to have some powerful advantages), concatenating them means that each is actually copied to the new string. The performance impact explodes (that is, grows exponentially) as you add more strings to be concatenated, since each "+" operation copies two strings (thus, a second "+" copies the result of the first "+", a third "+" copies all of that, and so on).

Bloch's book is highly regarded here and, after buying and reading it, I can see why. I recommend it to all Java programmers.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Also, see Item 51, which warns against the performance impact of contcatenating strings with the "+" operator.


Whilst you are correct that using + in unoptimised code could have a performance impact, the java compiler is clever enough to recognise this and will optimise the code automatically.
If you were to disassemble the compiled class file with javap you would see that the code in question uses a StringBuilder to create the complete string.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll have to try that, but StringBuilder isn't thread-safe, according to the doc.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:I'll have to try that, but StringBuilder isn't thread-safe, according to the doc.



The StringBuilder will be local to the method so doesn't need to worry about thread safety. If any of the String variables being appended are potentially thread unsafe then you would have the same problem whether you used a StringBuilder or String concatenation.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aj Prieto wrote:

Janeice DelVecchio wrote:when is c != i?
You can get rid of the c variable completely and just use i, right?



No, he can't. i goes up to 4 so if he did that his deck will only have four cards.



I think the outer while loop is unnecessary; it doesn't accomplish anything. Then c becomes unnecessary because the index of deck can be derived.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think



would be a lot neater

Steve
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all of your feedback, it gave me a lot to think about. Since I'm still learning I'm not the sharpest coder, and I realized that this isn't really a functional program to actually try to make a card game out of because the cards have no value. The only thing that defines them is their Strings. This would mean that I can't really introduce much game logic. For example, if I were to try to implement a method to compare a jack to a king to see which card is higher, the cards have no numerical value and so finding out which is higher in a mathematical manner would be impossible. Kinda difficult to play blackjack and count to 21 when each card has no numerical value!

I need to think this over a little bit to figure out what I need to do to make each card have more values other than a string. I think this might have been what Winston was getting at when mentioning enum.

Anyway, thanks again for your feedback.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Justin Coombs wrote:I need to think this over a little bit to figure out what I need to do to make each card have more values other than a string. I think this might have been what Winston was getting at when mentioning enum.


Exactly so, and for the exact reasons you said. Strings are NOT cards, and never will be. A Card class, on the other hand, can be anything you want it to be, and display any way you want to display it. Enums are just a way of defining a restricted value set (such as a Suit or card Value) as a class, and have lots of very useful extras.

HIH. Good luck.

Winston
 
Justin Coombs
Ranch Hand
Posts: 31
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I haven't gotten around to the chapter on enums in the java book I'm reading, but what do you guys think of this revision. It does basically the same thing as the last one, but this time the cards themselves in the array are actually objects

I got stuck on figuring out how to shuffle the objects...any suggestions?

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Collections class has a shuffle() method which shuffles the collection you pass in. The Arrays class has an asList() method which wraps an array in a List wrapper which will allow you to pass in your card array (shouldn't this be called deck) and shuffle it.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic