Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 4-4 Create an object and use it with the Number Guessing Game In this exercise, youll convert a number guessing game so it uses

Exercise 4-4 Create an object and use it with the Number Guessing Game

In this exercise, youll convert a number guessing game so it uses some object-oriented features. The class we create will have 3 private instancevariables, 2 constructors, 3 getmethodswhich return a value, and 1 methodthat acts as a sub procedure. The logic for creating a random number and tracking the # of guesses is moved to this new class.

Review the project

  1. Start NetBeans and open the project named ch04_ex4_GuessingGame.
  2. Run the project and make sure it works correctly. Also, make sure you understand the code in the main method.

Create a class that stores the random # and guess count data for the game

  1. In the murach.games package, create a new class named NumberGame.
  2. In this class, create one instancevariable for storing the upper limit of the number, a second instancevariable for storing the target number, and a third for the number of guessesthe user has made.
  3. Add a constructorto the class that has a single parameterfor input into the constructor method. This parameter is an integer variable for passing the value of the upper limitinto the constructor. Write a line of code to assign the parameter to the upper limit instance variable. Next generate the number that the user should try to guess and set the targetinstance variable. Cut and paste the relevant lines of code from the main method into the constructor to create this random target number. Finally, initialize the instance variablefor the number of guesses to 1. There will be 4 lines of code in this constructor.
  4. Add getmethodsfor all three instance variables. Dont create setmethodsfor the instance variables because the instance variables are meant to be read only.
  5. Add a methodnamed incrementGuessCount that adds 1 to the instance variablefor the number of guesses. This method will do the same thing as the line of code in the main method that increase the count by 1, and it has a void data type.

Use the Number Game class in the Main class

  1. In the Main class modify the code so it createsand usesa new Number Gameobject. The main class will call the constructorafter the user enters the upper limit to create the new object. Use this objects methodsto increase the count, getthe upper limit value and getthe number of guesses value. For example, use the getUpperLimit method to display the upper limit to the user. Then, comment out any unnecessary code in the main method that is now being done by the object.
  2. Run the project again and makes sure it still works correctly.

Add a second constructor and use it

  1. In the NumberGameclass, add a second default constructor to the class that has no parameters. The code for this default constructorwill call the other constructor in this class and pass it a value of 50 for the upper limit as an argument. (Hint: See page 124 -127 for an example of a no parameter constructor.)

  1. In the Main class, comment out the code that calls the single argument constructor, and then add a line of code that 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.
  2. Run the project again and makes sure it works correctly. It should set an upper limit of 50 by default.
  3. Include sample outputfor the code that asks the user for the upper limit (step 9) and for the code that uses the default constructor (step 12) in a Word document.

This is the file that it comes with:

package murach.games;

import java.util.Random; import java.util.Scanner;

public class Main {

public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println();

Scanner sc = new Scanner(System.in);

// Get upper limit System.out.print("Enter the upper limit for the number: "); int upperLimit = Integer.parseInt(sc.nextLine()); System.out.println("OK, I'm thinking of a number between 0 and " + upperLimit); System.out.println(); // Generate a random number between 0 and the upperLimit variable Random random = new Random(); int number = random.nextInt(upperLimit + 1); int count = 1; System.out.print("Enter your guess: "); int guess = Integer.parseInt(sc.nextLine()); while (guess != number) { if (guess < number) { System.out.println("Your guess is too low. "); } else if (guess > number) { System.out.println("Your guess is too high. "); } count = count + 1; System.out.print("Enter your guess: "); guess = Integer.parseInt(sc.nextLine()); } System.out.println("Correct! "); System.out.println("You guessed the correct number in " + count + " guesses. "); System.out.println("Bye!"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Question

Decision Making in Groups Leadership in Meetings

Answered: 1 week ago