Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java classes, and return values

Name: Anonymous 2005-11-13 19:35

i have to create two classes. ones a game and the other is a card class that the game calls from. here is my card class

import java.util.Random;
public class card {
   
    private static Random generator = new Random();
    private int value;
    private int suit;
   
    public card() {
        Dealcard();
    }
   
    public void Dealcard(){
       
        value = generator.nextInt(13)+1;
        suit = generator.nextInt(4)+1;
    }
   
    public int Getvalue() {
       
        return value;
    }
   
    public int Getsuit() {
       
        return suit;
    }
   
    public String toString(){
       
        String facevalue;
        if (value >1 && value < 11)
            facevalue = "";
        else if (value == 1)
            facevalue = "ace";
        else if (value == 11)
            facevalue = "jack";
        else if (value == 12)
            facevalue = "queen";
        else
            facevalue = "king";
       
        String facesuit;
       
        if (suit == 1)
            facesuit = "hearts";
        else if (suit == 2)
            facesuit = "spades";
        else if (suit == 3)
            facesuit = "diamonds";
        else
            facesuit = "clubs";
       

    }
}

the whole point in creating the tostring at the bottom is so i can combine the value and suit and have it be returned. how do i have it be returned?

Name: Anonymous 2005-11-14 5:57

return facevalue + " " + facesuit;

Other advice: learn how to use switch/case statements (honestly they're less useful in Java than other languages because of Java's "interesting" notion of equality, but in your program they would work.

By convention Java symbols use interCaps, so you should say getSuit() and faceValue and whatnot. Classes start with a capital letter, so your class should be called Class.

None of that has any meaning to the compiler, but it's good to stick to conventions.

Oh yeah and /prog/ has a [code] tag.

OH and one more thing, your card dealer seems to be quite capable of dealing the same card twice in a row. You might want to replace it with a Deck class that contains 52 Cards and has a shuffle() method.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List