Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(a) In each of the following, write a program segment using a for loop to display the numbers. (i) 2 5 8 11 14 (ii)

(a) In each of the following, write a program segment using a for loop to display the numbers.

(i) 2 5 8 11 14

(ii) 2 4 16 256

(iii) 25 12 6 3 1

(b) In this part, we will write a simple game of guessing an integer. In a dialog box (see the bottom of p.36 for an example of such a dialog box) displaying "Guess a secret integer in the range of 1 to 9 inclusively (-1 to terminate)", the player of the game is asked to guess the correct secret integer in the range. Each time after an integer is entered, one of the following messages is given in another dialog box :

"Congratulations! You are correct. Number of guesses = xx. Bye.", where xx is the number of guesses made by the player.

"The secret number is larger than your guessed number. Please retry."

"The secret number is smaller than your guessed number. Please retry."

You are required to perform the following:

(i) Create a class IntegerGuess with an attribute secretInteger which stores the secret integer. Also write its setter/getter methods. Copy the content of the class as the answers to this part.

(ii) Add another attribute guessCount which stores the number of trials the player made. The attribute should be initialized to zero when it is declared. Write a method increaseCount() which adds 1 to guessCount and its getter method. Copy the declaration and the methods as the answers to this part.

(iii) Write a method oneGuess(int anInteger) which calls increaseCount() and returns

0 if secretInteger is the same as anInteger;

1 if secretInteger is larger than anInteger;

-1 if secretInteger is less than anInteger;

Copy the content of the method as the answers to this part.

(iv) Write a method play() to display the dialog box asking the player to enter an integer, display the appropriate message after checking the guess using oneGuess() and repeat the guessing process until the guess is correct or the player enter -1 to terminate. You can assume the input is a valid integer and no checking is required. Copy the content of the method as the answer to this part.

(v) Write a driver class TestIntegerGuess with a main() method to initialize secretInteger in the range 1 to 9 using random() and play the game. See p.73 of Unit 2 for an example of using random(). Copy the content of the class as the answers to this part.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions