Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble getting my code to run. I keep getting the following error: Exception in thread main java.lang.IllegalStateException: Queue is full Please help

I am having trouble getting my code to run. I keep getting the following error: Exception in thread "main" java.lang.IllegalStateException: Queue is full

Please help me understand what I have wrong. 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 < 10; i++) { player.enqueue(allCards.get(i)); } for (int i = 10; i < 20; i++) { computer.enqueue(allCards.get(i)); } Scanner scan = new Scanner(System.in); System.out.print("How many rounds do you want to play? "); int rounds = scan.nextInt(); System.out.println(); int playerScore = 0; int computerScore = 0; for (int i = 0; i < rounds; i++) { if (player.isEmpty() || computer.isEmpty()) { break; } Pokemon playerCard = player.dequeue(); Pokemon computerCard = computer.dequeue(); System.out.println("Round " + (i + 1) + ":"); System.out.println("You played " + playerCard); System.out.println("Computer played " + computerCard); if (playerCard.getAttack() > 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) { 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 < front + count) { 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

How many lone pair electrons are on the indicated oxygen atom

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago