Question: Please help me understand what I have wrong. It looks like my enqueue method is implemented correctly but it still is throwing IllegalStateException as seen

 Please help me understand what I have wrong. It looks like

Please help me understand what I have wrong. It looks like my enqueue method is implemented correctly but it still is throwing IllegalStateException as seen below.

Please run this code and ensure that it runs with no errors.

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 implements Queue { 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.length) { throw new IllegalStateException("Queue is full"); } queue[back] = it; back = (back + 1) % queue.length; count++; } @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  

Queue.java

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(); int size(); }
Still having trouble getting my code to run after making some changes. I keep getting the following error: Exception in thread "main" java.lang.IllegalstateException Creste breelpoint : Queve is full at Arrayqueue enqueue (hrravqueue, java: 28) at Main.nain(Hain. java:17) Process finished with exit code 1

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!