java - Design and implement a class called Card that represents a standard playing card -
here issue, have created class incomplete because having issues on 2 levels. might tired...so bear me please.
- i having issue trying resolve having 5 random cards generate , not 1 more important no duplicates.
- i still new every time try add main says variables haven't been initialized. wtf...this biggest program/ first class have had write , stuck.
help please
import java.util.*; public class card { private int suit; // initializes 2 variables card private int value; public card() // constructor of card creates random suit , value card { random num = new random(); suit = num.nextint(4); value = num.nextint(13)+1; } public card(int card_suit, int card_value) // constructor of card takes int representing suit , value { suit = card_suit; value = card_value; } public int getvalue() // returns numeric value of card { return value; } public int getsuit() // returns numeric coded value of card' suit { return suit; } public string getsuitasstring() // return string representing card's suit. // (if card's suit invalid, "??" returned.) { switch ( suit ) { case 3: return "spades"; case 2: return "hearts"; case 1: return "diamonds"; case 0: return "clubs"; default: return "??"; } } public string getvalueasstring() // return string representing card's value. // if card's value i// compares value of card, value, int, of card // returns true if value > other card, false if not invalid, "??" returned. { switch ( value ) { case 1: return "ace"; case 2: return "2"; case 3: return "3"; case 4: return "4"; case 5: return "5"; case 6: return "6"; case 7: return "7"; case 8: return "8"; case 9: return "9"; case 10: return "10"; case 11: return "jack"; case 12: return "queen"; case 13: return "king"; default: return "??"; } } public string tostring() // overrides tostring method, { return getvalueasstring() + " of " + getsuitasstring(); // return string representation of card, such // "10 of hearts" or "queen of spades". } public boolean comparevalue(int cardother) { return value > cardother; } }
imo have 2 options rid of problem-1:
override
equals
method based onsuit_value
combo , every time create object of put inlist
after checking if exist or not. if exists create new 1 else keep , put in list.similar before instead of
list
usemap
keysuit+value -> respective obj
.
Comments
Post a Comment