CS1331 Homework 11: Implementing a Custom Queue (AlienQueue) for the SpaceRoundup Game - P, Assignments of Computer Science

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

Pre 2010

Uploaded on 08/04/2009

koofers-user-8yp
koofers-user-8yp 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1331 Homework 11: Space Roundup (part 3)
(Due: Friday, Apr. 27, 2006 @ 6:00 pm)
HW 9 & 10 Solution
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 hw10
.
Requirements
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<Alien> theAliens = new
ArrayList<Alien>(); The < and > business is specifying a more specific type of ArrayList. The one
above is an ArrayList of Aliens. To write a class that uses generics is a little bit of a hassle and
beyond the scope of this class. So, for your Queue, you’ll actually be implementing Queue<Alien>:
public class AlienQueue implements Queue<Alien> {
AlienQueue.java
There are several methods that you’ll need to implement, and other method that we won’t
require you to do anything with for this assignment. The methods that need to function
in your class are:
• public boolean offer(Alien a);
• public Alien poll();
• public Alien remove();
• public Alien peek();
• public Alien element();
• public int size();
• public boolean isEmpty();
• public Iterator<Alien> iterator();
• public Object[] toArray();
• public void clear();
• public boolean add(Alien a);
See the API for the Queue interface to see what each of these methods is supposed to do.
The methods that you can stub out are:
• public boolean containsAll(Collection<?> c);
• public boolean addAll(Collection<? extends Alien> c);
• public boolean removeAll(Collection<?> c);
• public boolean retainAll(Collection<?> c);
• public <T> T[] toArray(T[] arr);
pf3

Partial preview of the text

Download CS1331 Homework 11: Implementing a Custom Queue (AlienQueue) for the SpaceRoundup Game - P and more Assignments Computer Science in PDF only on Docsity!

CS1331 Homework 11: Space Roundup (part 3)

(Due: Friday, Apr. 27, 2006 @ 6:00 pm)

HW 9 & 10 Solution

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 .

Requirements

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 theAliens = new ArrayList(); The < and > business is specifying a more specific type of ArrayList. The one above is an ArrayList of Aliens. To write a class that uses generics is a little bit of a hassle and beyond the scope of this class. So, for your Queue, you’ll actually be implementing Queue: public class AlienQueue implements Queue { AlienQueue.java There are several methods that you’ll need to implement, and other method that we won’t require you to do anything with for this assignment. The methods that need to function in your class are:

  • public boolean offer(Alien a);
  • public Alien poll();
  • public Alien remove();
  • public Alien peek();
  • public Alien element();
  • public int size();
  • public boolean isEmpty();
  • public Iterator iterator();
  • public Object[] toArray();
  • public void clear();
  • public boolean add(Alien a); See the API for the Queue interface to see what each of these methods is supposed to do. The methods that you can stub out are:
  • public boolean containsAll(Collection<?> c);
  • public boolean addAll(Collection<? extends Alien> c);
  • public boolean removeAll(Collection<?> c);
  • public boolean retainAll(Collection<?> c);
  • public T[] toArray(T[] arr);

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 interface, which has three methods, two of which we’re concerned with:

  • public boolean hasNext();
  • public Alien next();
  • public void remove(); //this one can be empty An iterator’s purpose is to allow you to make repeated calls to next(), getting each of the Aliens in the queue, one at a time, until hasNext() returns false: AlienQueue a = new AlienQueue(); Iterator it = a.iterator(); while (it.hasNext()) { Alien a = it.next(); //do something with each Alien } This is essentially what a for-each loop does behind the scenes, java 1.5 has just lessened the work for you!