| 0 | kirasiris | Categories: Tags: , , , , , , , , , , ,

Java Class

Die:

//********************************************************************
//  Die.java       Author: Kevin Uriel Azuara Fonseca
//
//  Represents one die (singular of dice) with faces showing values
//  between 1 and 6.
//********************************************************************

public class Die {
    private final int MAX = 6;  // maximum face value

    private int faceValue;  // current value showing on the die

    //-----------------------------------------------------------------
    //  Constructor: Sets the initial face value.
    //-----------------------------------------------------------------
    public Die() {
        faceValue = 1;
    }

    //-----------------------------------------------------------------
    //  Rolls the die and returns the result.
    //-----------------------------------------------------------------
    public int roll() {
        faceValue = (int)(Math.random() * MAX) + 1;

        return faceValue;
    }

    //-----------------------------------------------------------------
    //  Face value mutator.
    //-----------------------------------------------------------------
    public void setFaceValue(int value) {
        faceValue = value;
    }

    //-----------------------------------------------------------------
    //  Face value accessor.
    //-----------------------------------------------------------------
    public int getFaceValue() {
        return faceValue;
    }

    //-----------------------------------------------------------------
    //  Returns a string representation of this die.
    //-----------------------------------------------------------------
    public String toString() {
        String result = Integer.toString(faceValue);

        return result;
    }
}

Pair Of Dice:

//********************************************************************
//  PairOfDice.java       Author: Kevin Uriel Azuara Fonseca
//
//  Using the Die class defined in this chapter, write a class called PairOfDice,
//  composed of two Die objects. Include methods to set and get the individual die values,
//  a method to roll the dice, and a method that returns the current sum of the two die values.
//  Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
//********************************************************************

public class PairOfDice {
    
    // Declare two die objects
    private Die die1;
    private Die die2;
    
    public PairOfDice(){
        // Initiate Die objetcs
        die1 = new Die();
        die2 = new Die();
        roll();
    }
    
    //-----------------------------------------------------------------
    //  Get value of die 1.
    //-----------------------------------------------------------------
    public int getDieOneValue() {
        return die1.getFaceValue();
    }
    
    //-----------------------------------------------------------------
    //  Get value of die 2.
    //-----------------------------------------------------------------
    public int getDieTwoValue() {
        return die2.getFaceValue();
    }
    
    //-----------------------------------------------------------------
    //  Roll values of dice.
    //-----------------------------------------------------------------
    
    public void roll() {
        
        // Roll die1
        die1.setFaceValue(die1.roll());
        // Roll die2
        die2.setFaceValue(die2.roll());
    }
    
    //-----------------------------------------------------------------
    //  Return sum of values from dice.
    //-----------------------------------------------------------------
    public int sum() {
        return die1.getFaceValue() + die2.getFaceValue();
    }
}

Rolling Dice:

//********************************************************************
//  RollingDice2.java       Author: Kevin Uriel Azuara Fonseca
//
//  Using the Die class defined in this chapter, write a class called PairOfDice,
//  composed of two Die objects. Include methods to set and get the individual die values,
//  a method to roll the dice, and a method that returns the current sum of the two die values.
//  Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
//********************************************************************

public class RollingDice2 {
    //-----------------------------------------------------------------
    //  Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.
    //-----------------------------------------------------------------
    public static void main(String[] args) {        
        PairOfDice roll = new PairOfDice();
        System.out.println(roll.sum());
    }
}

That is it!. Bye-Bye 🙂 .

Leave a Reply

Back to Top