Reputation: 921
I am creating a blackjack simulator to better learn java. I recently completed the free course available at udacity which is an excellent resource, https://www.udacity.com/course/cs046.
So far, I have created a deck of cards and printed it successfully. It consists of 3 classes, Blackjack which holds the main method, deck which creates the deck and card which creates the cards.
Is there a better way to do this? One thing that jumps out at me is how long the deck constructor is. In all the examples I have seen, constructors are much shorter.
Also, I am not making any use of the getter methods in card, so they should either be deleted or used. I am leaning towards deleting them since no other method is going to call these methods once the deck is created.
I have not googled this since I don't want to have the answers for future methods given to me. I am not the first person to have this idea!
Thank you for your feedback.
Card.java
package blackjack;
public class Card {
private final String name;
private final String suit;
private final int value;
public Card(String name, String suit, int value){
this.name = name;
this.suit = suit;
this.value = value;
}
public String getName(){
return name;
}
public String getSuit() {
return suit;
}
public int getValue() {
return value;
}
public String toString(){
return (this.name + " of " + this.suit + " " + this.value);
}
}
Deck.java
package blackjack;
import java.util.*;
public class Deck {
private static int numSuits = 4;
private static int numRanks = 13;
private static int numCards = numSuits * numRanks;
private Card[] cards;
public Deck() {
String suit = null;
String name = null;
int value = 0;
cards = new Card[numCards];
int arrayIndex=0;
for (int i=1; i<=numSuits; i++){
for (int j=1; j <= numRanks; j++){
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 0; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
Card card = new Card (name, suit, value);
cards[arrayIndex] = card;
arrayIndex++;
}
}
}
public void printDeck(){
System.out.println(Arrays.toString(cards));
}
}
Blackjack.java
package blackjack;
import java.util.*;
/**
*
* @author
*/
public class Blackjack {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Deck deck = new Deck();
deck.printDeck();
}
}
Upvotes: 1
Views: 3558
Reputation: 53525
Is there a better way to do this? One thing that jumps out at me is how long the deck constructor is. In all the examples I have seen, constructors are much shorter.
Answer:
You're right, in general constructors should be minimalistic. The reason for that is, that you want the object to be created ASAP, and when the constructor is doing too much work, the chance that "something bad will happen" increases and then you might get a "messed up" object (which is something that is very difficult to debug cause by the time you discover it...). All that said, your constructor has two nested loops that run 52 times and perform 2 actions (+2 comparisons) on each cycle. That's not that much work - so, IMHO that's fine.
Also, I am not making any use of the getter methods in card, so they should either be deleted or used. I am leaning towards deleting them since no other method is going to call these methods once the deck is created.
Answer:
No! all the Card
's fields are private and you won't be able to access them to actually read the cards when you go over Card[] cards
(in class Deck
). Yes, in the future...
Upvotes: 1