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

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., Li

ID: 3751259 • Letter: F

Question

Fully develop the classes for the Linked Implementation of the ADT Bag (i.e., LinkedBag<T>, Node or Node<T>).
Test your classes well (call all methods) before you proceed.

Use the following name for the files: Bag Interface, Linked Bag, and Test Bag.

Guessing Game


1. Design and implement a one-person guessing game that chooses n random integers in the range from 1 to N and asks user to guess them. The same integer might be chosen more than once.


2. For example, the game might choose the following 4 integers that range from 1 to 10: 1, 1, 6, 8. The following interaction might occur between the user and the game:
Enter 4 integers in the range from 1 to 10. Entries may be duplicate.
1 2 3 4
2 of your guesses are correct. Guess again.
5 6 7 8
2 of your guesses are correct
1 2 7 8
2 of your guesses are correct
1 1 1 1
2 of your guesses are correct
1 1 7 7
2 of your guesses are correct
1 1 6 8
You are correct! Play again?
No
Good Bye!


3. Design the game as an ADT. Use a bag that contains the integers chosen by the game. The integers m and n are specified by the client.

Explanation / Answer

Here is the working code for the requirement.

Please comment below for any queries about the program or any modifications needed. Thank you.

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class guessingGame {
public static void main(String[] args) {   
System.out.println("The computer has generate a unique 4 digit number. "
+ "You can try to guess the 4 digits number in 5 attempts. ");
System.out.println("_______________________________________________________ ");
int[] random=numberGenerator();
int max_try=5;
int index_match=0;
int matching=0;
while(max_try>0 && index_match!=4){
int[] guesss=getGuess();
index_match=0;
matching=0;
for(int i=0;i<guesss.length;i++){
if(guesss[i]==random[i]){
index_match++;
}
else if(guesss[i]==random[0] || guesss[i]==random[1] || guesss[i]==random[2] || guesss[i]==random[3]){
matching++;
}
}
if(index_match==4){
System.out.print("Well done! Your guesss is Correct! The number is: ");
for(int i=0;i<guesss.length;i++){
System.out.print(guesss[i]);
}
}
else{
max_try--;
if(max_try>1){
System.out.println("You have guesss "+index_match+" correct number in correct position,"+
" and "+matching+" correct number in incorrect position. "+max_try+" attempt remaining.");
}
else if(max_try==1){
System.out.println("You have guesss "+index_match+" correct number in correct position,"+
" and "+matching+" correct number in incorrect position. Last attempt!. Good luck");
}
else{
System.out.println("Sorry, you failed to guesss the number in 5 attempts.");
System.out.print("The number is: ");
for(int i=0;i<random.length;i++){
System.out.print(random[i]);
}
}
}
}
}

public static int[] getGuess(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your guesss: ");
String input = keyboard.nextLine();
if(input.length()!=4 || input.replaceAll("\D","").length()!=4){
System.out.println("Invalid number. You must enter 4 digits between 0-9 only.");
return getGuess();
}
int[] guesss = new int[4];
for (int i = 0; i < 4; i++) {
guesss[i] = Integer.parseInt(String.valueOf(input.charAt(i)));
}
return guesss;
}

public static int[] numberGenerator() {
Random randy = new Random();
int[] randArray = {10,10,10,10};

for(int i=0;i<randArray.length;i++){
int temp = randy.nextInt(9);
while(temp == randArray[0] || temp == randArray[1] || temp == randArray[2] || temp == randArray[3]){
temp=randy.nextInt(9);
}
randArray[i]=temp;   
}
return randArray;
}
}