mercredi 1 mars 2017

Dice rolling program using set and get methods in Java

I am trying to write a program that creates two objects /instances of a class (Dice) to simulate a pair of dice. The program should simulate the rolling of the 2 dice and display their values using the OutputDice method.

The Value field holds the value of the dice. The SetValue method stores a value in the Value field.The GetValue method returns the value of the dice. The Roll method that generates a random number in the range of 1 through 6 for the value of the die. The OutputDice method output the value of the dice as text.

I realize that the following code is horribly incomplete but I cannot figure out how to encapsulate the random function into the output.

My two classes are as follows:

import java.util.Random;

public class Dice {

    private int Value;

    public void setValue(int diceValue) {
           Value = diceValue;
    }

    public int getValue() {
           return Value;
    }

    public void roll() {
        //I am not sure how to structure this section
    }
}

and

import java.util.Random;
import java.util.Scanner;

public class DiceRollOutput {

    public static void main(String[]args) {
        String firstDie;
        String secondDie;
        int firstNumber;
        int secondNumber;

        Scanner diceRoll = new Scanner(System.in);

        Random Value = new Random();

        firstNumber = Value.nextInt(6)+1;
        secondNumber = Value.nextInt(6)+1;
    }
}




Aucun commentaire:

Enregistrer un commentaire