Question: Still having trouble getting my code to run after making some changes. I keep getting the following error: Please help me understand what I have
Still having trouble getting my code to run after making some changes.
I keep getting the following error:

Please help me understand what I have wrong. It looks like my enqueue method is implemented correctly but it still is throwing IllegalStateException.
My code is below:
Main.java:
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List allCards = readCSV("pokemon.csv"); Collections.shuffle(allCards); Queue player = new ArrayQueue(); Queue computer = new ArrayQueue(); for (int i = 0; i computerCard.getDefense()) { System.out.println("You win!"); playerScore += 2; player.enqueue(playerCard); player.enqueue(computerCard); } else if (computerCard.getDefense() > playerCard.getAttack()) { System.out.println("Computer wins!"); computerScore += 2; player.enqueue(playerCard); player.enqueue(computerCard); } } } static List readCSV(String filename) { File file = new File(filename); List pokemonList = new ArrayList(); try { Scanner scan = new Scanner(file); scan.nextLine(); while (scan.hasNextLine()) { String[] info = scan.nextLine().split(","); // #,Name,Type 1,Type 2,Total,HP,Attack,Defense,Sp. Atk,Sp. Def,Speed,Generation,Legendary Pokemon poke = new Pokemon(info[1], Integer.parseInt(info[6]),Integer.parseInt(info[7])); pokemonList.add(poke); } } catch (FileNotFoundException e) { System.out.println("File not found!"); e.printStackTrace(); } return pokemonList; } }
ArrayQueue.java
public class ArrayQueue
private final int DEFAULT_CAPACITY = 7;
private T[] queue;
private int front, back, count = 0;
// front of the queue is left
// back of the queue is right
public ArrayQueue() {
queue = (T[])(new Object[DEFAULT_CAPACITY]);
}
public ArrayQueue(int size) {
queue = (T[])(new Object[size]);
}
/*
What is the big o runtime efficiency/complexity of enqueue
in a circular array queue?
*/
@Override
public void enqueue(T it) throws IllegalStateException {
if (count
queue[back % queue.length] = it;
count++;
back++;
} else {
throw new IllegalStateException("Queue is full");
}
}
@Override
public T dequeue() {
if (isEmpty()) {
return null;
} else {
T item = queue[front % queue.length];
queue[front % queue.length] = null;
front++;
count--;
return item;
}
}
@Override
public T frontValue() {
if (isEmpty()) {
return null;
} else {
return queue[front % queue.length];
}
}
@Override
public boolean isEmpty() {
return count == 0;
}
@Override
public int length() {
return count;
}
@Override
public String toString() {
StringBuilder output = new StringBuilder();
int i = front;
while (i
output.append(queue[i % queue.length] + " ");
i++;
}
return output.toString();
}
}
Queue.java
/** * @author Delaware Technical Community College * Starter and/or reference code provided for Delaware Technical Community College courses. * Queue ADT * based on https://people.cs.vt.edu/shaffer/Book/JAVA3elatest.pdf * Page 125 */ public interface Queue{ /** * Place an element at the rear of the queue. * * @param it The element being enqueued. * @throws IllegalStateException- if the element cannot be added at this time due to capacity restrictions */ public void enqueue(E it) throws IllegalStateException;; /** * Remove and return element at the front of the queue. * * @return The element at the front of the queue. * @return null if the queue is empty */ public E dequeue(); /** * @return The front element. * @return null if the queue is empty */ public E frontValue(); /** * Detects whether this queue is empty. * * @return True if the queue is empty. * @return False if the queue has one or more items in it. */ public boolean isEmpty(); /** * @return The number of elements in the queue. */ public int length(); String toString(); }
Pokemon.java
public class Pokemon { private String name; private int attack, defense; public Pokemon(String name, int attack, int defense) { this.name = name; this.attack = attack; this.defense = defense; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAttack() { return attack; } public void setAttack(int attack) { this.attack = attack; } public int getDefense() { return defense; } public void setDefense(int defense) { this.defense = defense; } public String toString() { return "Pokemon{name: " + name + ", attack: " + attack + ", defense: " + defense + "}"; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
