Question: JAVA ONLY DO PART 3 AND 4 ArrayList.java import java.util.Iterator; import java.util.Objects; public class ArrayList implements List { // instance variables public static final int
JAVA
ONLY DO PART 3 AND 4


ArrayList.java
import java.util.Iterator;
import java.util.Objects;
public class ArrayList
public static final int CAPACITY = 4;
private E[] data;
private int size = 0;
// constructors
public ArrayList() {this(CAPACITY);}
public ArrayList(int capacity) {
data = (E[]) new Object[capacity];
}
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E get(int i) throws IndexOutOfBoundsException {
checkIndex(i, size);
return data[i];
}
public E set(int i, E e) throws IndexOutOfBoundsException {
checkIndex(i, size);
E temp = data[i];
data[i] = e;
return temp;
}
public void add(E e) throws IllegalStateException {
if (size == data.length) {
resize(2 * data.length);
}
data[size] = e;
size++;
}
public void add(int i, E e) throws IndexOutOfBoundsException,IllegalStateException {
checkIndex(i, size + 1);
if (size == data.length) {
resize(2 * data.length);
throw new IllegalStateException("Array is full");
}
for (int k=size-1; k >= i; k--) {
data[k+1] = data[k];
}
data[i] = e;
size++;
}
public E remove(int i) throws IndexOutOfBoundsException {
checkIndex(i, size);
E temp = data[i];
for (int k=i; k
data[k] = data[k+1];
data[size-1] = null;
size--;
if (size
resize(data.length / 2);
}
return temp;
}
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i
newData[i] = data[i];
}
data = newData;
}
protected void checkIndex(int i, int n) throws IndexOutOfBoundsException {
if (i = n)
throw new IndexOutOfBoundsException("Illegal index: " + i);
}
@Override
public Iterator
// TODO Auto-generated method stub
return null;
}
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof ArrayList))
return false;
ArrayList> other = (ArrayList>) o;
if (size != other.size())
return false;
for (int i = 0; i
if (!Objects.equals(data[i], other.get(i))) {
return false;
}
}
return true;
}
Peg.java
}
public class Peg {
private final String color;
private boolean isSet;
public Peg(String color) {
this.color = color;
this.isSet = false;
}
public String getColor() {
return color;
}
public boolean isSet() {
return isSet;
}
public void set() {
this.isSet = true;
}
public void unset() {
this.isSet = false;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Peg)) {
return false;
}
Peg other = (Peg) o;
return this.color.equals(other.color);
}
}
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
Get step-by-step solutions from verified subject matter experts
