Monty374

GameBooth

Nov 15th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. package carnival;
  2.  
  3. /**
  4. *
  5. * @author Monty
  6. */
  7. import java.lang.Math;
  8.  
  9. public class GameBooth {
  10. private double cost;
  11. private String firstPrize, consolationPrize;
  12. public String prizeCount;
  13.  
  14. /**
  15. * constructor
  16. * pre: none
  17. * post: A GameBooth object created.
  18. * The cost and prizes are set.
  19. */
  20. public GameBooth(double charge, String p1, String p2) {
  21. cost = charge;
  22. firstPrize = p1;
  23. consolationPrize = p2;
  24. }
  25.  
  26.  
  27. /**
  28. * Game is played and prize awarded.
  29. * pre: none
  30. * post: Player had three tries. Player successful all
  31. * three times received the first prize. A consolation
  32. * prize has been awarded otherwise.
  33. */
  34. public String start() {
  35. int toss;
  36. int successes = 0;
  37.  
  38. /* play game */
  39. for (int i = 0; i < 3; i++) { //player gets three tries
  40. toss = (int)(Math.random() + 0.5); //0 or 1
  41. if (toss == 1) {
  42. successes += 1; //1 is a successful toss
  43. }
  44. }
  45.  
  46. /* award prize */
  47. if (successes == 3) {
  48. return(firstPrize);
  49. } else {
  50. return(consolationPrize);
  51. }
  52. }
  53.  
  54.  
  55. /**
  56. * Returns the cost to play the game.
  57. * pre: none
  58. * post: Cost of the game has been returned.
  59. */
  60. public double getCost() {
  61. return(cost);
  62. }
  63.  
  64. public String prizesAwarded() {
  65. String prizeCount = firstPrize + consolationPrize;
  66. return(prizeCount);
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment