Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me to change the game as an Bag ADT. Use a bag that contains the integers chosen by the game. And ask

Can someone help me to change the game as an Bag ADT. Use a bag that contains the integers chosen by the game.

And ask user whether they want to play another game (creating a control variable for the do-while loop). Example for this will be like:

You won! Do you want to play again?

No

Bye!

Here is the codes:

package DataStructuresProject1;

import java.util.LinkedList; import java.util.Scanner;

public class GuessingGame{

//Task1 //Crease two private field private LinkedList user; private LinkedList game;

//Task2 //Constructor prompt the user //and add two empty bags private GuessingGame(){ user = new LinkedList<>(); game = new LinkedList<>(); System.out.println("In this game you select a number of integers to guess and their range (2 to whatever you select)"); System.out.println("The System generates this number of random integers. They are not necessarily distinct"); System.out.println("You will try to guess them in several attempts"); }

//Task 3 //initialize a game with size number of random nubmer range from 1 to range private void initializeGame(int size, int range){ user = new LinkedList<>(); game = new LinkedList<>(); int min = 1; for(int i=0;i

//return size of game bag; private int getSize(){ return game.size(); }

//adds the value to the user bag private void addFromUser(int value){ user.add(value); }

//Number of intersections of two bags private int numIntersections(){ int num=0; for(int i :user){ for (int j:game){ if(i==j){ num++; break; } } } return num; }

//clear the user bag private void clearUser(){ user = new LinkedList<>(); }

//Task4 //create a play method public static void play(GuessingGame g,Scanner input){ //determine game size int size = g.getSize();

//create a loop while(true){

//ask user for size of integer input System.out.println(" Enter " + size + " Integers."); for(int i=0;i

//add integer to user bag g.addFromUser(num); }

//now check number of intersection int numIntersections = g.numIntersections(); //if num of intersection equal to size of game bag //then print won and return if(numIntersections == g.getSize()){ System.out.println("You won!"); return; } //if not clear user bag g.clearUser(); } }

public static void main(String[] args) { //Task 5 //create a guessing game object GuessingGame g = new GuessingGame();

//create scanner object Scanner input = new Scanner(System.in); int op;

//do while loop do{

//ask user for number to guess System.out.print(" How many numbers do you want to guess? "); int size = input.nextInt();

//ask user for range System.out.print("Specify the range: 2 to (your choice): "); int range = input.nextInt();

//initialize the gaussing game g.initializeGame(size, range);

//call play g.play(g, input);

//ask user whether they want to play another game System.out.print("Want to play another game: (Enter 0): "); op = input.nextInt(); }while(op==0); }

}

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

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

=+such as the dirigenti in Italy?

Answered: 1 week ago