Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please provide the code in JAVA ! thank you!! Using the BagInterface from the in-class examples as the starting point, implement an alternate version of
Please provide the code in JAVA ! thank you!!
Using the BagInterface from the in-class examples as the starting point, implement an alternate version of the Bag class that uses a linked list structure instead of an array. The new class should be named LinkedBag. Note that this new bag class should not simply contain a linked list object! Rather, the bag class should include its own Node class with a next reference, a head pointer, and the bag class should implement its own logic similar to a linked list. The purpose of this problem is to demonstrate how the general structure of a linked list can be applied to the specific methods that the bag class needs to implement. The bag class should implement all of the methods within the Baginterface and should also have the following two private methods: private int getIndexOf(T input) and private T removeAtIndex(int index). Part 2 (60 Points) One application for bags in general is in making guessing game applications. Bags won't store the elements in a particular order, but they can be used to keep track of previous guesses made by the user. This is useful because the contains() method of the bag can be easily used in order to check if a new guess is a duplicate or not. We are going to write a simple guessing game using the LinkedBag that will involve the user guessing individual letters in order to try and guess a hidden word. For the sake of this assignment, our application will be fairly simple, but you will be able to see how a more robust game could be built based upon the same concepts. The first thing our application should do is set up an empty LinkedBag object as well as an integer variable to keep track of the user's score. Our application will work by first taking in a string from the user. This string will be the word that the user is trying to guess. The idea with this is that it is a two-player guessing game, where one player will think of the word to guess and the other player will attempt to guess it. This will also make our program easier to test. For simplicity's sake, we will assume that all of the words that will be used will only have one of each letter. Once the word has been taken in, the program will start accepting guesses one character at a time. Note that the user should get one guess per letter in the mystery word. If the word to guess is "brew" then the user will have 4 guesses. The general procedure for processing a guess will be as follows: 1. Check if the guess has already been made using the contains() method. 2. If the guess has already been made, the guess is wasted and the user still loses their guess for this round. A message should be printed to let the user know. 3. If the guess has not already been made, it should be added to the bag. 4. Once the guess has been added to the bag, your code should check to see if the guess is actually in the mystery word, and if it is, the user's score will go up by one. 5. If it is not in the mystery word, the user's score will remain the same. 6. A status message should also be returned to the user so that they know whether or not their guess was found in the mystery word or not. 7. Finally, the preceding status message should include where the correct letters have been guessed. In other words, if the word is "brew" and the user guesses " r " then "_r_-_" should be printed. If the next guess is "w" then "_r_w" should be printed, and so on. A loop should be used for processing the guesses, so that you can loop through once for each guess that the user is allowed to make. Once all of the user's guesses are used up, your program will print the user's score, and also a message indicating whether or not they successfully guessed the entire word. public interface LinkedBagInterface T{ public boolean isEmpty (); public boolean isFuli (); public int getCurrentsize (); public boolean contains (T input); public int getCountof (T input); public void addAtHead (T input); public void addAtTail (T input); public void add (T input); /;adds to tail public boolean addAtIndex (T input, int index) public T getHead (); public T getTail (); public T getAtIndex (int index); public T removeHead (); public T removeTail (); public boolean removeSpecific (T input); public T removeRandom (); public void removeAll (); \}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started