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

Pages: 1-

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-13 20:10

Erm.

Why are you asking this question?  Your GetSuit() and GetValue() are evidence that you know how to return things from a method.  Exactly what problem are you having?

And, as an afterthought, this is probably the worst place to ask a question re: Java programming if you want to avoid a flambe.

Name: Anonymous 2005-11-13 20:26

Your code hurts my eyes

Name: Anonymous 2005-11-13 23:14

maybe hes asking how to return the value of suit?

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.

Name: Anonymous 2005-11-14 21:49

>>5
well, some card games (e.g. blackjack) sometimes use multiple decks to get an approximately even distribution of cards no matter how many are dealt.

Name: Anonymous 2006-11-14 21:31

classes and return values

Name: Anonymous 2006-11-15 4:38

>>1

Are you going to post all your homework here? GTFO.

Name: Anonymous 2009-12-14 17:33

I AM [sup]\\[/sup]POSTING// IN A [sup]\\[/sup]THREAD//

Name: Anonymous 2009-12-14 17:34

I AM \\POSTING// IN A \\THREAD//

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