Programming Project 3

In your textbook, on page 319, a class MyStack is given. For your convenience, here is the code. It implements the data structure stack, for Object. Notice that it must be part of a package called chapter9.

Your problem is to use MyStack to create a class called EvenOdd. This class should use a MyStack object to store Integer objects, with evens on the bottom and odds on top. It should have the following two methods, both returning nothing.

  1. printOut()
  2. add(Integer)
Their behavior is specified as follows.
  1. printOut causes the list of integers added so far to be printed to the console. It does this in stack order, with odds first and evens afterward. After being called, the state of the EvenOdd object is as it was before, so that more integers can be added to those already present.
  2. add takes an Integer parameter and adds it to the stack that the EvenOdd object is managing. It does this so that the stack always has evens on the bottom and odds on the top.

To test your code, use the following Tester. Both Tester and the EvenOdd code you write should be in a package called evenodd.

Solution: