maygapixel

Untitled

Feb 26th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.  public class Dice
  2. 2.  {
  3. 3.     private int face;
  4. 4.     public Dice()
  5. 5.    {
  6. 6.       face = 1;
  7. 7.    }
  8. 8.    public void roll()
  9. 9.    {
  10. 10.      face = (int)(Math.random()*6)+1;
  11. 11.   }
  12. 12.   public int getFace()
  13. 13.   {
  14. 14.      return face;
  15. 15.   }
  16. 16.   public String toString()
  17. 17.   {
  18. 18.      return "F = " + face;
  19. 19.   }
  20. 20. }
  21.  
  22.  
  23.  
  24.  
  25. 1.  public class DiceBox
  26. 2.  {
  27. 3.    public static void main(String[] args)
  28. 4.    {
  29. 5.      Dice d1 = new Dice();
  30. 6.      System.out.println(d1);
  31. 7.      d1.roll();
  32. 8.      System.out.println(d1);
  33. 9.   
  34. 10.     DiceBox box = new DiceBox();
  35. 11.     box.add(d1);
  36. 12.     box.add(new Dice());
  37. 13.     box.add(new Dice());
  38. 14.  
  39. 15.     System.out.println("Before shaking: total = " + box.getTotal());
  40. 16.     box.shake();
  41. 17.     System.out.println("After shaking: total = " + box.getTotal());
  42. 18.  
  43. 19.     box.printDices();
  44. 20.   }
  45. 21.  private ArrayList<Dice> box = new ArrayList<Dice>();
  46. 22.  public DiceBox()
  47. 23.  {
  48. 24.  }
  49. 25.  public void add (Dice d)
  50. 26.  {
  51. 27.     box.add(d);
  52. 28.  }
  53. 29.  public void shake()
  54. 30.  {
  55. 31.     for(int i=0; i<box.size();i++)
  56. 32.         box.get(i).roll();
  57. 33.  }
  58. 34.
  59. 35.  public int getTotal()
  60. 36.  {
  61. 37.     int total = 0;
  62. 38.     for(int i=0; i<box.size();i++)
  63. 39.         total += box.get(i).getFace();
  64. 40.     return total;
  65. 41.  }
  66. 42.  public void printDices()
  67. 43.  {
  68. 44.     for(int i=0; i<box.size();i++)
  69. 45.     System.out.println((i+1)+" . Face = " +box.get(i).getFace());
  70. 46.  }
  71. 47. }
Advertisement
Add Comment
Please, Sign In to add comment