Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3698411 • Letter: #

Question

(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.

Explanation / Answer

(a).
i.
    //here the sequence is an A.P. of common difference 3.

#include <iostream>
using namespace std;

int main() {

   int i,n=2;
   for(i=0;i<5;i++){

       cout<<n<<" ";
       n+=3;                        //adding 3 to each number of sequence to get next.

   }
   return 0;
}


ii.

// the sequence is formed by squaring the previous number.

#include <iostream>
using namespace std;

int main() {

   int i,n=2;
   for(i=0;i<4;i++){

       cout<<n<<" ";
       n*=n;                         //here squaring the number for getting its next term.

   }
   return 0;
}


iii.


// here we need to divide the each term by 2 of the sequence and
//taking its floor value to get its next term.
// since we chose datatype of number as <int> so we don't need to floor it.

#include <iostream>
using namespace std;

int main() {

   int i,n=25;
   for(i=0;i<5;i++){

       cout<<n<<" ";
       n/=2; //dividing each number by 2.

   }
   return 0;
}

(b)(i)


       public class IntegerGuess{   //defining a class named IntegerGuess

           private int secretinteger;                   //setting an attribute named secretinteger.

            public int getsecretinteger() {            //getter method
                       return this.secretinteger;
                   }
           public void setsecretinteger( int num) {               // setter method
                       this.secretinteger = num ;
                   }
       }

-->answered only 4 subparts as per the rules of chegg.com

-->please do upvote