MrSnoopy

LoadedDice- java example of cheated dice

Jan 17th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4.  * extend je klíčové slovo pro dědičnost - potomek extend předek
  5.  * Loaded dice je potomkem třídy Dice, navíc obsahuje jméno cheatera
  6.  *
  7.  */
  8. public class LoadedDice extends Dice {
  9.     private String cheater;
  10.  
  11.     /**
  12.      * Konstruktor- super volá parametrizovaný konstruktor předka plus
  13.      * naastavuje cheatera
  14.      *
  15.      * @param sidesCount
  16.      * @param cheater
  17.      */
  18.     public LoadedDice(int sidesCount, String cheater) {
  19.         super(sidesCount);
  20.         this.cheater = cheater;
  21.     }
  22.  
  23.     @Override
  24.     public int roll() {
  25.         int min = super.getNumOfSides() - 2;
  26.         // min + rndGenerator.nextInt(max - min + 1) (ano, vazne to funguje)
  27.         int hodnota = min
  28.                 + new Random().nextInt(super.getNumOfSides() - min + 1);
  29.         super.setActualValue(hodnota);
  30.         return hodnota;
  31.     }
  32.  
  33.     // zavola toString predka + daný string
  34.     @Override
  35.     public String toString() {
  36.         return super.toString() + " (I am loaded)";
  37.     }
  38.  
  39.     public String getCheater() {
  40.         return cheater;
  41.     }
  42.  
  43.     public void setCheater(String cheater) {
  44.         this.cheater = cheater;
  45.     }
  46. }
Add Comment
Please, Sign In to add comment