How do I create multiple instances of a class at runtime?

T

Temp31415

I'm writing a blackjack program in Java by creating a Card class and
creating multiple instances (52 in fact) of the class. I was wondering
if there's a way to create these instances at runtime using a loop
instead of going through and actually creating each in the code.

If anybody has a better idea how to do this that would be great too.

Thank You!
 
J

Joona I Palaste

Temp31415 said:
I'm writing a blackjack program in Java by creating a Card class and
creating multiple instances (52 in fact) of the class. I was wondering
if there's a way to create these instances at runtime using a loop
instead of going through and actually creating each in the code.
If anybody has a better idea how to do this that would be great too.
Thank You!

To create the instaces at runtime using a loop... use a loop.
Try something like:
Card[] deck = new Card[52];
for (int i=0; i<52; i++) {
deck = new Card();
/* now you can call methods on deck to set the suit and the
number */
}

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"This is a personnel commuter."
- Train driver in Scientific American
 
J

Jeff

Is this what you are looking for?

Card[] deck = new Card[52];
for (int i = 0; i < 52; i++) {
deck= new Card();
}

Instead of a simple array you could put them in an
ArrayList or some other java.util data structure...
 
E

Eric Sosman

Temp31415 said:
I'm writing a blackjack program in Java by creating a Card class and
creating multiple instances (52 in fact) of the class. I was wondering
if there's a way to create these instances at runtime using a loop
instead of going through and actually creating each in the code.

If anybody has a better idea how to do this that would be great too.

The word you're looking for is "array."

Card[] deck = new Card[52];
for (int i = 0; i < 52; ++i)
deck = new Card();
 
T

Tito

I'm writing a blackjack program in Java by creating a Card class and
creating multiple instances (52 in fact) of the class. I was wondering
if there's a way to create these instances at runtime using a loop
instead of going through and actually creating each in the code.

What do you think of something like this (Java-pseudocode):
(the nomenclature is in spanish, as I don't know the appropriate english
terms).

class Baraja { // A "baraja" is a collection of 52 different cards.

public class Carta { // A card
...
private Carta() {
<creates a Joker>
}
private Carta(Palo palo, int numero) {
...
}
}

Carta[][] cartas = new Carta[4][13]; // I think this is illegal in Java,
but you catch the idea, don't you?
Carta[] jokers = new Carta[0]; // So you don't like playing with jokers?

public Baraja() {
<initialize card arrays>
}

public Carta getJoker(int i) {
return jokers;
}

public Carta getCard(Palo palo, int numero) {
return cartas[palo][numero]; // cards can also be lazily created here.
}
}

Tito
 
M

Michiel Konstapel

Temp31415 said:
I'm writing a blackjack program in Java by creating a Card class and
creating multiple instances (52 in fact) of the class. I was wondering
if there's a way to create these instances at runtime using a loop
instead of going through and actually creating each in the code.

If anybody has a better idea how to do this that would be great too.

You have 52 cards, composed of 4 suits with 13 cards each. We'll call
the "number" on the card (2, 3, ..., 10, Jack, ...) the value. So, to
create a complete deck:

// we don't have enums, so fake em
public static final int HEARTS = 0;
public static final int DIAMONDS = 1;
public static final int SPADES = 2;
public static final int CLUBS = 3;

public static final int MAX_VALUE = 12; // 0..12 is 13 cards per suit

Card[] deck = new Card[52];
// we map 2, 3, ... 10, Jack, Queen, King, Ace to 0..13
for (int suit = HEARTS; suit < CLUBS; suit++) {
for (int value = 0; value < MAX_VALUE; value++) {
deck = new Card(suit, value);
}
}

A tip: the next step is probably to shuffle the deck. Take a look at
Collections.shuffle(List).
HTH,
Michiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top