Question: JAVA L List.java import java.util.Iterator; /** * A simplified version of the java.util.List interface. */ public interface List extends Iterable { /** * Returns the

JAVA

JAVA L List.java import java.util.Iterator; /** * A simplified version of thejava.util.List interface. */ public interface List extends Iterable { /** * ReturnsLthe number of elements in the list. * * @return number of

List.java

import java.util.Iterator;

/**

* A simplified version of the java.util.List interface.

*/

public interface List extends Iterable {

/**

* Returns the number of elements in the list.

*

* @return number of elements in the list

*/

int size();

/**

* Tests whether the list is empty.

*

* @return true if the list is empty, false otherwise

*/

boolean isEmpty();

/**

* Returns (but does not remove) the element at index i.

*

* @param i the index of the element to return

* @return the element at the specified index

* @throws IndexOutOfBoundsException if the index is negative or greater

* than size()-1

*/

E get(int i) throws IndexOutOfBoundsException;

/**

* Replaces the element at the specified index, and returns the element

* previously stored.

*

* @param i the index of the element to replace

* @param e the new element to be stored

* @return the previously stored element

* @throws IndexOutOfBoundsException if the index is negative or greater

* than size()-1

*/

E set(int i, E e) throws IndexOutOfBoundsException;

/**

* Inserts the given element at the specified index of the list, shifting

* all subsequent elements in the list one position further to make room.

*

* @param i the index at which the new element should be stored

* @param e the new element to be stored

* @throws IndexOutOfBoundsException if the index is negative or greater

* than size()

*/

void add(int i, E e) throws IndexOutOfBoundsException;

/**

* Removes and returns the element at the given index, shifting all

* subsequent elements in the list one position closer to the front.

*

* @param i the index of the element to be removed

* @return the element that had be stored at the given index

* @throws IndexOutOfBoundsException if the index is negative or greater

* than size()

*/

E remove(int i) throws IndexOutOfBoundsException;

/**

* Returns an iterator of the elements stored in the list.

*

* @return iterator of the list's elements

*/

Iterator iterator();

}

Write a simple text-based version of the classic board game Mastermind, where a single player is the code breaker, and the system is the code maker. The system selects a code of four coloured pegs and the player tries to guess the secret code. In each round, the player makes a guess, and the system tells the player how many pegs of the guess were exact matches to the code (correct in both color and position, marked ' x '), and how many colours were partial matches to the code (correct color placed in the wrong position, marked ' '). The feedback is displayed in a 22 grid format similar to the board game. e.g., suppose the code is black red blue green Guess \#1: blue red green yellow x 0 - The feedback shows that there is one exact match and 2 partial matches. Note that this configuration does not indicate which pegs are exact matches. The player makes guesses until either: a) the player breaks the code (player wins!) or b) 10 guesses are made but did not result in a full match (system wins). 1. Create the generic ArrayList class that implements the provided List interface (note that List extends Iterable) a. Overload the add method: include another add method that will have one parameter, an element that adds to the end of the list. b. Make your Arraylist dynamic: the array should grow to double its current capacity if it runs out of space and shrink to half its current capacity when the number of elements in the array goes below N/4, where N is the current capacity. Modify add () and remove () methods and include a resize () method to support the dynamic structure. Set the default capacity to 4. c. Override the equals method that checks if the ArrayList is equivalent to the given instance. 2. Create a class named Peg with field colour. Include any other fields/methods to help with gameplay. Override the equals method to return true if the colours match. 3. Write a Game class that acts as the code maker and handles the mechanics of the Mastermind game. Include a minimal main method that instantiates the game and invokes gameplay. 4. In the main method, also illustrate how the capacity of your array would changes as objects are added and removed. Your program should have the following: a. An instance of ArrayList that holds a set of 4 pegs of which colours are randomly generated. Each peg has a colour of 6 different possibilities (duplicates are allowed, blanks are not). b. Another ArrayList that holds pegs that represent the player's guess. c. A game loop that prompts the user for their guess and determines if the 2 ArrayLists are equal: i. if so, notify the player and end the game ii. if not, provide the user feedback on their guess: - Determine whether if each peg of the guess is a match and mark it accordingly. You will need to compare the guess against the code and determine the number of exact and partial matches. d. After their 10th guess, if it is not a full match, inform the player that the system won. Note: - You may assume that the player input is valid i.e., if it is an invalid colour, the player loses and the game is over. - Enums are optional (colour, match status) Suggestions: - Display the generated code for testing (and remove before submitting) - For guess feedback: must be careful to avoid counting any of the pegs twice; make at least two passes to compare the guess and the code. In the first pass, look for exact matches and in the second pass, look for partial matches. Sample output: [code: white blue yellow green] System: Guess \#1: Player: blue blue blue blue System: x - - - System: Guess \#2: Player: blue red red red System: - - System:Player:System:System:Player:System:System:Player:System:System:Player:System:Guess#3:yellowblueyellowyellowxxGuess#4:greenblueyellowgreenxxx-Guess#5:greenblueyellowblackxxoGuess#6:whiteblueyellowgreenYoucrackedthecode

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!