Java variable/object names

airbornflghtairbornflght Houston, TX Icrontian
edited January 2007 in Internet & Media
is there any way to declare java variables or objects in a way so they are sequentially numbered?

such as:

[PHP]
import java.util.*;
public class Deck
{
ArrayList deck = new ArrayList();
Random rand = new Random();
private final int SPADE=6, HEART=3, DIAMOND=4, CLUB=5;
public Deck()
{
NameMaker namer = new NameMaker("card", 1);
int i = 1;
do
{
int suit=0, val=0;

if(i>=1&&i<=13)
{
suit=HEART;
val=i;
}
if(i>=14&&i<=26)
{
suit=DIAMOND;
val=i-13;
}
if(i>=27&&i<=39)
{
suit=CLUB;
val=i-26;
}
if(i>=40&&i<=52)
{
suit=SPADE;
val=i-39;
}

//I have no clue how to go further...
i++;
}while(i<=52);
}
}
[/PHP]


This is the nameMaker class. I was trying to figure out how to get this to work, but it is evading me. Is this even possible. Once make the cards I want to add them to the ArrayList deck. so that I will have a deck of cards. after that I will be set to go forth.

The project is to make a War card game simulator. So after I get the deck I have to deal out the cards randomly, and I also have a class called stack. Which there will be four of those objects, two for the player. I'm sure all of you know how to play war. This program just has to play the game by itself and output all of the moves that happen.
[PHP]
public class NameMaker
{
private String pre;
int number, start, end;
public NameMaker(String prefix,int s)
{
String pre=prefix;
start=s;
number=start-1;
}

public String getName()
{
number++;
String together=pre+number;
return together;
}
}[/PHP]

would I just be better off hand coding all 52 cards?

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    Are you talking about naming the objects as in:

    Card card1 = new Card();
    Card card2 = new Card();
    .
    .
    .

    ?

    Or do you want to just set a "name" field in the Card class?

    If so, why do you want to do this?

    Also, a comment:

    final variable names are generally all capitalized: HEARTS
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2007
    yeah, I want card1, card2...card52.

    And as I create the objects I want to add them to the deck ArrayList
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    You don't need to name them all:

    [php]
    ArrayList<Card> deck = new ArrayList<Card>(); //this means that when you use deck.get(), you'll get a Card object back
    Card aCard;
    int numCards = 52;

    for(int i=0; i<numCards; i++){
    aCard = new Card(); // or whatever constructor you have
    deck.add(aCard);
    }
    [/php]

    edit:

    you also don't really need to use ArrayList, if you know exactly how big the deck is - you can just use an array of Cards
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2007
    shwaip wrote:
    You don't need to name them all:

    [php]
    ArrayList<Card> deck = new ArrayList<Card>(); //this means that when you use deck.get(), you'll get a Card object back
    Card aCard;
    int numCards = 52;

    for(int i=0; i<numCards; i++){
    aCard = new Card(); // or whatever constructor you have
    deck.add(aCard);
    }
    [/php]

    edit:

    you also don't really need to use ArrayList, if you know exactly how big the deck is - you can just use an array of Cards

    yeh, I was just going to use an array, but my stacks are going to have to be ArrayLists so I decided to just use an ArrayList for the deck also. Thanks shwaip.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    Arrays are faster/more efficient.
  • edited January 2007
    Arrays are faster/more efficient.

    I usually use an ArrayList when i'm building a data structure full of a particular type of object and I don't know how many will be constructed, then convert that list to an object array once it's populated. Think fetching data out of a db and stuffing it into objects, then stuff those objects into a list, then turn the list into an array and return the array.
    public Person[] get People(){
       List<Person> people = new ArrayList<Person>();
       ...
       db code 
       ...
       while(rs.next()){
         Person person = new Person();
         ...
         do stuff
         ...
         people.add(person);
       }
       Person[] _people = people.toArray(new Person[0]);
       return _people;
    }
    
Sign In or Register to comment.