0

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?

Herp
  • 3
  • 2
  • You can generate random values using the [java.util.Random class](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html). – Henrik Aasted Sørensen Oct 08 '19 at 17:55
  • 1
    Thanks! I think I understand the gisp of it now, and can probably figure the rest out. – Herp Oct 08 '19 at 18:00
  • [Java – Generate random integers in a range](https://www.mkyong.com/java/java-generate-random-integers-in-a-range/) – Abra Oct 08 '19 at 18:03
  • [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – Abra Oct 08 '19 at 18:05
  • And just for the record: consider to learn about java enums: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html ... what you are doing there with your int fields to point into arrays ... that is really the poor mans version of using enums. – GhostCat Oct 08 '19 at 18:18

2 Answers2

1

Here's a piece of code to get you started:

public static void main(String[] args) {
    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

    Random random = new Random();
    String randomRank = ranks[random.nextInt(ranks.length)];
    String randomSuit = suits[random.nextInt(suits.length)];
    System.out.println(randomRank + " " + randomSuit);
}

Example output: 6 Clubs

If you don't want null to be picked:

String randomRank = ranks[random.nextInt(ranks.length - 1) + 1];
String randomSuit = suits[random.nextInt(suits.length - 1) + 1];
Malt
  • 28,965
  • 9
  • 65
  • 105
0

First, putting "null" as the 0th element in the array is bad form (in Java, and all other 0-index languages). You'll need to get comfortable that counting begins at zero - as all the other code you will encounter in the Java universe will be based on that assumption.

String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; 
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

Essentially your problem reduces to producing a pair of random numbers. The first from 0-4, inclusive of 0 but excluding 4, the second from 0-14, inclusive of zero but exclusive of 14.

Fortunately, Java provides such a function Random.nextInt(int bound). You'd use it as follows:

Random r = new Random();
int suite = r.nextInt(4);
int rank  = r.nextInt(14);

You can read about that method https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-