

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for cs1331 students to implement their own queue (alienqueue) for the spaceroundup game using generics and an array. Students must implement methods such as offer(), poll(), remove(), peek(), element(), size(), isempty(), iterator(), toarray(), clear(), and add(). The document also explains how to handle array resizing and iterator implementation. Extra credit is offered for adding powerups, score, new aliens and planets, and moving planets.
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


After homework 10’s deadline has passed, we will make a solution to hw 9&10 available on the course website, which you can use for this assignment. Feel free to use your own hw 9&10 solution to build upon. We’ve merely included a working solution in case you might’ve had problems with anything on the previous homework. Your grade for this homework will not be affected by the functionality of your past homework assignment. Since the solution will be posted as soon as the grace period is over, you will not be allowed to turn in late submissions for hw .
You may have noticed that with ArrayBlockingQueue, your ship can only hold a fixed number of Aliens before the game starts throwing exceptions. This is certainly not ideal behavior, so for this assignment you’ll be writing your own Queue implementation. Generics Java 1.5 uses what’s known as generics for all of its data structures. You’re seen an example of this with ArrayList, and the Queue’s we’ve used so far: ArrayList
To stub out these methods (just to get the class to compile), simply write the method signatures exactly as you see above, and then just return any arbitrary value of the appropriate type (for instance, for the first four you can return false, and for the last one you can return null). iterator() is the only tricky method, which you’ll need to write an additional class for. More on that later. AlienQueue implementation specifics You must use an array to store the contents of this Queue. You cannot use an ArrayList, a LinkedList, or any other built-in data structure for this homework. There are a few tricks to working with an array: Hints Keep an int pointer to the first empty index of the array. When the array is empty, it’ll be zero. When you add to the Queue, you place the current addition into the index the pointer is pointing to, and then increment the pointer. This pointer will also correspond to the size of the array. When you remove from the queue, you will store the value at index 0 in a temporary reference (to be returned), shift all the other Aliens up one array (i.e., the one at index 1 will now be in index 0, the one in index 2 goes to index 1, etc.), decrement pointer, and then return the temporary reference. You’ll also need to resize the array when it gets full. To do this, create a temporary Alien array of double the length of the original, copy all the contents of the original array over, and then assign the original’s reference to this new temporary array. A lot of the methods you’re implementing are very similar, and differ only in what they do when the Queue is empty. Use this fact to your advantage, and don’t re-write code! Iterators Just as toString() is called when you try and print an Object, iterator() is called when you use a class in a for-each loop. And Iterator does just what its name sounds like: it allows you to iterate over all the objects in a data structure. In order for the iterator() method of our AlienQueue class to work, we need to write a corresponding Iterator class for it. Because our Queue uses an array to store its data internally, it’s fairly simple for our iterator to iterate over all the aliens: it just keeps track of where it is in the array between successive calls to the next() method, and returns the Alien at the current index it’s progressed to. AlienQueueIterator.java AlienQueueIterator should implement the Iterator