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

Part I package numberguessing; import java.util.Random; public class NumberGame

ID: 3873495 • Letter: P

Question

Part I

package numberguessing;

import java.util.Random;

public class NumberGame {

    // Set to Private, so that the only access is through the constructor.
     private int guessNumber;
     private int randomNumber;
     private int upperLimit;

    // The Constructor. All access to the variables will be through here.
     public NumberGame(int x){
         guessNumber = 1;
         upperLimit = x;
         Random rand = new Random();
         randomNumber = rand.nextInt(x);   
        
     }
    // Get and Set methods, for all the variables in the constructor.
     public int getGuessNumber() {
         return guessNumber;
        
     }
    
     public void incrementGuessCount() {
         guessNumber = guessNumber +1;
     }

    public void setGuessNumber(int guessNumber) {
         this.guessNumber = guessNumber;
     }

    public int getRandomNumber() {
         return randomNumber;
     }

    public void setRandomNumber(int randomNumber) {
         this.randomNumber = randomNumber;
     }

    public int getUpperLimit() {
         return upperLimit;
     }

    public void setUpperLimit(int upperLimit) {
         this.upperLimit = upperLimit;
     }

}

Part II

1. In this class, create one instance variable for storing the upper limit of the number, a second for storing the number, and a third for the number of guesses the user has made.

2. Add a constructor to the class that takes an integer value for the upper limit and uses it to set the upper limit instance variable. Then, generate the number that the user should try to guess and set that instance variable. You can copy and modify the relevant lines of code from the main method to do this. Finally, initialize the instance variable for the number of guesses to 1.

3. Add get methods for all three instance variables. Use your IDE to do this if possible. You don’t need to create set methods.

4. Add a method named incrementGuessCount that adds 1 to the instance variable for the number of guesses. Use the class

5. In the Main class modify the code so it uses the new object. For example, use the getUpperLimit method to display the upper limit to the user. Then, remove any unnecessary code.

6. Run the project again and makes sure it still works correctly. Add a second constructor and use it

7. In the NumberGame class, add a constructor to the class that takes no arguments. The code for this constructor can call the other constructor in this class and pass it a value of 50 for the upper limit.

8. In the Main class, modify the code so it uses the zero-argument constructor. Then, comment out the statements that get the upper limit from the user. These statements are no longer necessary since the constructor automatically sets the upper limit to 50.

9. Run the project again and makes sure it works correctly. It should set an upper limit of 50 by default.

Explanation / Answer

// Main Class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote