CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Getting object vars after retrieval from vector (http://www.codingforums.com/showthread.php?t=258920)

dan-dan 04-27-2012 04:06 PM

Getting object vars after retrieval from vector
 
I just started out trying to make a simple card game as of last night.

My problem is on line 16 and 17 of the first file. I have successfully created the 52 objects with variables for the cards, and once created they are placed into a Vector (the deck). Just to test it worked I used the below code, but the name is underlined and I get the error name cannot be resolved or is not a field.

Does anyone know why this may be? The objects are deffinately in the Vector, I just can't get to the variables they contain.

Thanks.

Code:

Object ob = (Object) Cards.deck.get(i);
System.out.println(ob.name);

****head.java
PHP Code:

import javax.swing.JOptionPane;

public class ****
head {
    
    public ****
head() {
        
        
int p 0;
        for (
int i 0Cards.allCards.lengthi++) {
            
!= 13 0;
            
Cards card = new Cards();
            
            
card.name Cards.allCards[i];
            
card.cardValue 1;
            
card.imgPath "images\\cards\\" card.name ".gif";
            
Cards.deck.add(card);
            
Object ob = (Object) Cards.deck.get(i);
            
System.out.println(ob.name);
            
p++;
            }
        }
    
    
    public static 
void main(String[] args) {
        ****
head sh = new ****head();
        
    }


Cards.java
PHP Code:

import java.util.Vector;

public class 
Cards {
    
    static 
String[] allCards = { 
        
"A-Hearts""2-Hearts""3-Hearts""4-Hearts""5-Hearts"
        
"6-Hearts""7-Hearts""8-Hearts""9-Hearts""10-Hearts"
        
"J-Hearts""Q-Hearts""K-Hearts",
    
        
"A-Dimonds""2-Dimonds""3-Dimonds""4-Dimonds""5-Dimonds"
        
"6-Dimonds""7-Dimonds""8-Dimonds""9-Dimonds""10-Dimonds"
        
"J-Dimonds""Q-Dimonds""K-Dimonds",
    
        
"A-Clubs""2-Clubs""3-Clubs""4-Clubs""5-Clubs"
        
"6-Clubs""7-Clubs""8-Clubs""9-Clubs""10-Clubs"
        
"J-Clubs""Q-Clubs""K-Clubs",
    
        
"A-Spades""2-Spades""3-Spades""4-Spades""5-Spades"
        
"6-Spades""7-Spades""8-Spades""9-Spades""10-Spades"
        
"J-Spades""Q-Spades""K-Spades" 
        
};
    
    
    static 
String name;
    static 
int cardValue;
    static 
String imgPath;
    
    static 
Vector <Objectdeck = new Vector<Object>(allCards.length);
    
    public 
Cards() { }
    



Fou-Lu 04-27-2012 06:08 PM

It never will. Object is a base type that contains no properties, and your Card doesn't represent what it is.
You'll need to represent a card as an object, and a deck of Card. I'd represent the suit and value as an enum string pair:
PHP Code:


public class Card implements Comparable<Card>
{
    public static 
enum Suit
    
{
        
HEART ("Hearts"),
        
SPADE ("Spades"),
        
CLUB ("Clubs"),
        
DIAMOND ("Diamonds");
        
        private final 
String sSuit;
        
        
Suit(String s)
        {
            
this.sSuit s;
        }
        
        public 
String getSuit()
        {
            return 
this.sSuit;
        }
    }
    
    public static 
enum FaceValue
    
{
        
ACE ("A"),
        
TWO ("2"),
        
THREE ("3"),
        
FOUR ("4"),
        
FIVE ("5"),
        
SIX ("6"),
        
SEVEN ("7"), 
        
EIGHT ("8"),
        
NINE ("9"),
        
TEN ("10"),
        
JACK ("J"),
        
QUEEN ("Q"),
        
KING ("K");
        
        private final 
String sValue;
        
        
FaceValue(String s)
        {
            
this.sValue s;
        }
        
        public 
String getValue()
        {
            return 
this.sValue;
        }
    }

    
    private 
Suit suit;
    private 
FaceValue value;
    
    public 
Card(Suit suitFaceValue value)
    {
        
this.suit suit;
        
this.value value;
        
    }
    
    public 
Card(String suitString value)
    {
        
this.suit Suit.valueOf(suit);
        
this.value FaceValue.valueOf(value);
    }

    public 
Suit getSuit()
    {
        return 
this.suit;
    }

    public 
FaceValue getValue()
    {
        return 
this.value;
    }
    
    public 
int compareTo(Card c)
    {
        
int iResult this.suit.compareTo(c.suit);
        if (
iResult == 0)
        {
            
iResult this.value.compareTo(c.value);
        }
        
        return 
iResult;
    }
    
    public 
String toString()
    {
        return 
this.value.getValue() + "-" this.suit.getSuit();
    }


And the deck as an array list. I like the list since you can shuffle it, but without an override it will accept duplicate values.
PHP Code:

import java.util.Collections;
import java.util.ArrayList;


public class 
Deck extends ArrayList<Card>
{
    private static final 
long serialVersionUID 7465934980544707488L;

    public static 
void main(String... argv)
    {
        
Deck d = new Deck();
        
        
// Populate the deck with one of each suit and face value.
        
for (Card.Suit s Card.Suit.values())
        {
            for (
Card.FaceValue v Card.FaceValue.values())
            {
                
d.add(new Card(sv));
            }
        }
        
        
Collections.shuffle(d);
        
System.out.println("shuffled:");
        for (
Card c d)
        {
            
System.out.println(c);
        }
        
        
Collections.sort(d);
        
System.out.println("sorted:");
        for (
Card c d)
        {
            
System.out.println(c);
        }
    }


I like the enums since you can do a lot with them. It forces specific allowed entries and lets you run a switch on them unlike a string.

Edit:
Oh, btw I should mention simplicity as well. If you don't really care about a Deck or a Card, you can represent it just using the String array you have. That can be thrown into a list and Shuffled as well and print just as a string:
PHP Code:

    public static void main(String[] argv)
    {
        
String[] allCards = { 
                
"A-Hearts""2-Hearts""3-Hearts""4-Hearts""5-Hearts"
                
"6-Hearts""7-Hearts""8-Hearts""9-Hearts""10-Hearts"
                
"J-Hearts""Q-Hearts""K-Hearts",
            
                
"A-Dimonds""2-Dimonds""3-Dimonds""4-Dimonds""5-Dimonds"
                
"6-Dimonds""7-Dimonds""8-Dimonds""9-Dimonds""10-Dimonds"
                
"J-Dimonds""Q-Dimonds""K-Dimonds",
            
                
"A-Clubs""2-Clubs""3-Clubs""4-Clubs""5-Clubs"
                
"6-Clubs""7-Clubs""8-Clubs""9-Clubs""10-Clubs"
                
"J-Clubs""Q-Clubs""K-Clubs",
            
                
"A-Spades""2-Spades""3-Spades""4-Spades""5-Spades"
                
"6-Spades""7-Spades""8-Spades""9-Spades""10-Spades"
                
"J-Spades""Q-Spades""K-Spades" 
                
};
        
java.util.List<Stringcards Arrays.asList(allCards);
        
java.util.Collections.shuffle(cards);
        for (
String s cards)
        {
            
System.out.println(s);
        }
    } 

Depending on exactly what you want to do, sometimes simplicity is for the better.

dan-dan 04-29-2012 05:35 PM

Wow, thanks Fou-Lu! I've tried your code and it works great. I understand what you're saying.

My first attempt actually used a String array and used the Collections.shuffle method, though I dropped it. I'm still struggling with classes and objects so this is pretty difficult understanding the best (and legal) approach.

Thanks again.


All times are GMT +1. The time now is 03:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.