Advertisement
Guest User

Die

a guest
Apr 5th, 2012
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Die {
  4.      private int faceValue;
  5.       private final int MAX=6;
  6.           private Random dieGen; //generator
  7.      
  8.       public Die()
  9.       {
  10.         faceValue = 1;
  11.                 dieGen = new Random();
  12.       }
  13.      
  14.       //Math.random creates random integers from 1 - 6
  15.       public void roll()
  16.       {
  17.         faceValue = dieGen.nextInt(MAX) +1;
  18.       }
  19.      
  20.       //Sets the face value
  21.       public void setFaceValue(int value)
  22.       {
  23.         faceValue=value;
  24.       }
  25.      
  26.       //Gets the face value using the setFaceValue method
  27.       public int getFaceValue()
  28.       {
  29.         return faceValue;
  30.       }
  31.      
  32.       //Converts values into a String
  33.       public String toString()
  34.       {
  35.         String result = "You rolled a " + faceValue;
  36.    
  37.         return result;
  38.       }
  39.      
  40. }// end class Die
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement