Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. Upload the .java file for a class named GuessingGame which has the following features;
  2. A data field named secret of data type int which is private to the class
  3. A data field named bestScore of data type int which is also private to the class
  4. A constructor without parameters which initializes
  5. secret to a random value between 0 and 100 inclusive
  6. bestScore to the value 999
  7. A method named play which takes no parameters and returns no values
  8. The method play will do the following
  9. Define and initialize a local variable n to 0
  10. Ask the user to enter an integer between 0 and 100 inclusive and store it in a local variable named guess
  11. If the the value in guess is outside the above range, display "Sorry, your value needs to be in [0,100]" then keep asking the user for another number until their input is within acceptable range
  12. When the value in guess is within the valid range, then increase n by one to count it as a guess
  13. Compare the value in guess with secret
  14. If guess is smaller than secret, then display the message "Too Small"
  15. If guess is larger than secret, then display the message "Too Large"
  16. If guess is equal to secret, then display the message "You guessed the secret number!!!"
  17. Keep repeating steps 2 to 5 until the user guess the right number or the value of n is greater than 100.
  18. You will then display a message, based on how many attempts the user took to guess the secret number;
  19. If n is greater than or equal to bestScore, then display the message "You did not beat your best score"
  20. if n is lesser than bestScore, assign n to bestScore then display "New best score!!!"
  21. In addition, you will provide a static method main which will create a new object of class GuessingGame and call the play method on it 5 times in order to allow the user to play several games
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement