Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2012, 04:06 PM   PM User | #1
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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() { }
    


Last edited by dan-dan; 04-27-2012 at 04:20 PM..
dan-dan is offline   Reply With Quote
Old 04-27-2012, 06:08 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.

Last edited by Fou-Lu; 04-27-2012 at 06:48 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
dan-dan (04-29-2012)
Old 04-29-2012, 05:35 PM   PM User | #3
dan-dan
Regular Coder

 
dan-dan's Avatar
 
Join Date: Aug 2009
Location: England
Posts: 483
Thanks: 22
Thanked 79 Times in 78 Posts
dan-dan is on a distinguished road
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.
dan-dan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:55 AM.


Advertisement
Log in to turn off these ads.