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