aspose file tools*
The moose likes Beginning Java and the fly likes Feedback for Program Creating a Deck of Cards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Feedback for Program Creating a Deck of Cards" Watch "Feedback for Program Creating a Deck of Cards" New topic
Author

Feedback for Program Creating a Deck of Cards

Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 22

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])


Nothing is withheld from us what we have conceived to do.
-Russel Kirsch-
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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.

When you do things right, people won't be sure you've done anything at all.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

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.


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8250
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.


Joanne
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 567
    
    4

I'll have to try that, but StringBuilder isn't thread-safe, according to the doc.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
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.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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.
Steve Ho
Greenhorn

Joined: Dec 13, 2012
Posts: 1
I think



would be a lot neater

Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to the Ranch
Justin Coombs
Greenhorn

Joined: Oct 10, 2012
Posts: 22

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

Joined: Mar 17, 2011
Posts: 8250
    
  23

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
Greenhorn

Joined: Oct 10, 2012
Posts: 22

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?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2366
    
  50
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Feedback for Program Creating a Deck of Cards