I'm a bit of a java newbie,so I'm taking a java class. I'm working on my current assignment that asks me to make a class that holds data types for Card types (spade, diamond, etc) and their numbers (1, 2, 3 Jack, etc). I've managed to work this out, but I'm asked to make the class generate these numbers randomly, in order to choose a random card out of the deck. Below is my current code for my cards class.
public class Card {
private int rank, suit;
public Card(int rank, int suit) {
this.rank = rank; //set methods
this.suit = suit;
}
public String toString(){
String[] suits = {null, "Clubs", "Diamonds", "Hearts", "Spades"}; //Null is never used, replaces 0
String[] ranks = {null, "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; //Null is never used, replaces 0
String drawnCard = ranks[this.rank] + " of " + suits[this.suit];
return drawnCard;
}}
class TestCard
{
public static void main (String args[]){
Card card = new Card(8, 4);
{
System.out.println(card);
}
}
}
The class TestCard is what I currently have, but I'm trying to replace the values inside with randomly assigned values (rank 1-4, suit 1-13). So how exactly do I generate a random value from the arrays in the Card class and call it in the TestCard class?