Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Upload the .java file for a class named GuessingGame which has the following features;
- A data field named secret of data type int which is private to the class
- A data field named bestScore of data type int which is also private to the class
- A constructor without parameters which initializes
- secret to a random value between 0 and 100 inclusive
- bestScore to the value 999
- A method named play which takes no parameters and returns no values
- The method play will do the following
- Define and initialize a local variable n to 0
- Ask the user to enter an integer between 0 and 100 inclusive and store it in a local variable named guess
- 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
- When the value in guess is within the valid range, then increase n by one to count it as a guess
- Compare the value in guess with secret
- If guess is smaller than secret, then display the message "Too Small"
- If guess is larger than secret, then display the message "Too Large"
- If guess is equal to secret, then display the message "You guessed the secret number!!!"
- Keep repeating steps 2 to 5 until the user guess the right number or the value of n is greater than 100.
- You will then display a message, based on how many attempts the user took to guess the secret number;
- If n is greater than or equal to bestScore, then display the message "You did not beat your best score"
- if n is lesser than bestScore, assign n to bestScore then display "New best score!!!"
- 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