Using Enum in Java

Asked

Viewed 254 times

-1

Hey there, guys. The program should randomly generate a "Heads" or "Tails" value, but using Enum. It is not clear to me how to implement the statement below using Enum. Can someone give me a light? To randomly generate heads and tails options, create an ENUM that contains the two values and a method that makes use of the java.util.Random class to select one of the values from the one of the ENUM values.

I’ve already done it here: (I don’t know if that’s it)

    public enum Jogo {
     CARA(0), COROA(1);
    private int indice;

     Jogo(int i) {
         this.indice = i;
     }

    public int getIndice() {
         return this.indice;
     }

 }

2 answers

3

Flipping heads or tails presupposes randomness, so your program needs to have a method that can randomly choose one of the Enum values you created. Java has a class called Random, that cares about generating random values for you. More specifically, it has this method here:

public int nextInt(int bound)

It will generate a random integer between 0 and the limit you specify in the parameter exclusively, that is, to generate a random number between 0 and 10, you must enter 11 in the parameter.

Another thing you need is a method that the compiler creates for you every time you use an Enum: values(). This method returns an array of all Enum values in the order you declared them inside it:

Moeda.values()[0]; // vai retornar Moeda.CARA
Moeda.values()[1]; // vai retornar Moeda.COROA

Attention: this is unrelated to this variable indice that you created. Here you are accessing position 0 and position 1 of the array. As you declared HEADS and then TAILS, the values creates the array with CARA at the first position of the array (0) and COROA at the second position (1). Actually, this variable indice is unnecessary.

Now it’s easy: you’ll generate a random number between 0 and 1, go to the value array, retrieve the value of Enum stored at that position and that’s it.

This light is enough for you to do your exercise. Break your head a little and, if you evolve, edit the code in your question, updating with what you got and, if you have any questions, comment on this answer here.

0

Just generate a random number between 0 and 1. Use this value as the enumeration index using the method values().

package moeda;

import java.util.Random;

public class CaraCoroa {

    public enum Moeda {
        CARA, COROA;
    }

    public static void main(String[] args) {
        System.out.println("sortear() = " + sortear());
        System.out.println("sortear() = " + sortear());
        System.out.println("sortear() = " + sortear());
        System.out.println("sortear() = " + sortear());
        System.out.println("sortear() = " + sortear());
    }

    private static Moeda sortear(){
        Random random = new Random();
        int indice = random.nextInt(2);
        
        Moeda moeda = Moeda.values()[indice];
        return moeda;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.