Question: Using Java to structure a Guessing 1. Create a class GuessingGame with two private field: private LinkedBag user; private LinkedBag game; 2. The constructor issues

Using Java to structure a Guessing

1. Create a class GuessingGame with two private field:

private LinkedBag user; private LinkedBag game;

2. The constructor issues a prompt and allocates two empty bags. The prompt may output the instruction for a user:

"In this game you select a number of integers to guess and their range (2 to whatever you select) The system generates this number of random integers. They are not necessarily distinct You will try to guess them in several attempts "

3. You need to create additional private methods:

void initializeGame (int size, int range) (which adds size number of integers in the range 1 to range to the game bag)

int getSize() (which returns the size of the game bag);

void addFromUser (Integer value) (which adds the value to the user bag)

int numIntersections () (which returns number of intersections of two bags: game and user) (*)

void clearUser () (which clears the user bag)

4. After step 3, you can create the method play () :

public static void play (GuessingGame g, Scanner input)

This method is called when the game is properly initialized.

You determine the game size and create a loop, where in each iteration you ask user to enter this amount of integers,

add those integers to the user bag (calling g.addFromUser) and when the input is done, check the number of intersections.

If this number equals game size, issue the info string (You won!) and return; if not, clear the user bag (calling g.clearUser() )and perform the next iteration.

5. In the main method you

create a new GuessingGame;

create a new Scanner over System.in

build a do-while loop in which:

ask user "How many numbers do you want to guess? "

ask user "Specify the range : 2 to (your choice) "

initialize game;

call play

ask user whether they want to play another game (creating a control variable for the do-while loop).

(*) the method numIntersections() can create an intersection bag and return its size, but it is better to write a code without creating a new bag, but using the same loop and simply counting the common entries.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!