Here is a copy of the requirements. They are below. I am fairly new to Java. I am switching from Ada and they threw me straight into a Data Structures class using Java. Any suggestions would be helpful.
Understand the Pool interface
The util package declares the Pool interface:
public interface Pool {
boolean isEmpty();
void add(E e);
E remove();
}
Pool is being used here in the sense of “pool of candidates” or “motor pool”, not “swimming pool” or “billiards”!
This interface is a generalization of stacks and queues. A Pool<E> is a container for holding elements (of type E) that are available to be chosen. You can add a new element to the pool (like pushing an element onto a stack or enqueueing an element in a queue), or remove an element from the pool (like popping an element off a stack or dequeueing an element from a queue).
However, the pool interface does not specify which element should be removed from the pool, so different implementations of the interface can make this choice in different ways. For example, a stack implementation should remove the most recently added element, whereas a queue implementation should remove the least recently added element.
All implementations of the remove method should throw NoSuchElementException when called on an empty pool. (See page 675 of the textbook for how to throw an exception.) The NoSuchElementException class is defined in the java.util package, so you'll probably want to include the line
import java.util.NoSuchElementException;
or (if you are using other parts of java.util as well)
import java.util.*;
somewhere before your class declaration and after the package declaration (if any).
Note that Pool is a simplified version of java.util.AbstractQueue, cut down to its bare essentials.
Important: All implementations of Pool must support all three methods in O(1) time.
(For implementations that use an array under the covers—such as an ArrayList or Vector—you can ignore the occasional linear time blip that happens when the underlying array is reallocated and copied, because the reallocations happen infrequently enough that the time averages out to O(1). For a more detailed discussion of this issue, see the section “Performance of the KWArrayList Algorithms” on page 86, which refers back to the reallocate() method on page 76.)
Develop and test the Stack class
Use the New Class wizard in Eclipse to create a new Stack class inside the util package that implements the Pool interface.
Click New → Class.
Fill in the Package field with util. (This step is new)
Fill in the Name field with Stack.
Next to the Interfaces box, click Add..., then select Pool - util. Click OK. (This step is new)
Click Finish.
Note that Java stores files in directories that mimic the package structure of the classes. Because the Stack class is in the util package, the new Stack.java file will be stored in the Pool/src/util directory.
Now, inside the new Stackclass, implement the three Pool methods so they provide the expected LIFO behavior.
Use the New
JUnit Test Case wizard to make a new StackTest class in the Pool/test folder. Choose util.Stack as the Class under test. Important: Leave the Package field blank!
Create enough tests inside StackTest to give the mythical “reasonable person” confidence that your implementation is correct. Remember that each separate test will have the form
@Test
public void testXXX() {
...
}
See the Testing Advice section below for suggestions about how to test stacks and queues.
Because the StackTest class should not be inside the util package, you'll probably need to add the line
import util.Stack;
at the top of StackTest.java.
Develop and test the Queue class
Follow the same process as above to develop a Queue class with the expected FIFO behavior, and a QueueTest class to test it.
Save your test results
When you are ready to turn in the exercise, create a file with your test results.
From the shell, cd into the Pool directory in your workspace. (If you do an ls,
you should see the bin, src, and test directories.)
Run the following command:
java -cp bin:/usr/local/junit/junit-4.11.jar org.junit.runner.JUnitCore StackTest QueueTest
This should print the test results to the screen.
Save the test results to a file by running the command
java -cp bin:/usr/local/junit/junit-4.11.jar org.junit.runner.JUnitCore StackTest QueueTest > testresults.txt
Instead of typing this from scratch, you can use an up-arrow to bring back the previous command, and then add “> testresults.txt” to the end of the line. This is called file redirection—it saves the text that would have been printed to the screen into a file instead.
You can verify the file contents by running
cat testresults.txt
to display the file on the screen.