Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions