Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Random;
  2. /**
  3.  * Write a description of class Die here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class Die implements DieInterface
  9. {
  10.     private static Random randomiser;
  11.     private Integer value; // null if unrolled
  12.     public Die()
  13.     {
  14.         value = 0;
  15.         if(!(randomiser instanceof Random)){
  16.             randomiser = new Random();
  17.         }
  18.     }
  19.    
  20.     public boolean hasRolled(){
  21.         if(value != 0){
  22.             return true;
  23.         }
  24.         else{
  25.             return false;
  26.         }
  27.     }
  28.    
  29.     public void roll(){
  30.         value = randomiser.nextInt(NUMBER_OF_SIDES_ON_DIE) + 1;    
  31.     }
  32.    
  33.     public int getValue()throws NotRolledYetException{
  34.         if(hasRolled()){
  35.             return value;
  36.         }
  37.         else{
  38.             throw new NotRolledYetException("You haven't rolled the DIE");
  39.         }
  40.    
  41.     }
  42.    
  43.     public void setValue(int value){
  44.          if(1 <= value && value <= NUMBER_OF_SIDES_ON_DIE){
  45.              this.value = value;
  46.          }
  47.          else{
  48.              this.value = 0;
  49.          }
  50.     }
  51.    
  52.     public void clear(){
  53.         value = 0;
  54.     }
  55.    
  56.     public void setSeed(long seed){
  57.         randomiser.setSeed(seed);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement