001package hw3;
002
003import java.util.Iterator;
004import java.util.Stack;
005
006/**
007 * <b>RatPolyStack</B> is a mutable finite sequence of RatPoly objects.
008 * <p>
009 * Each RatPolyStack can be described by [p1, p2, ... ], where [] is an empty
010 * stack, [p1] is a one element stack containing the Poly 'p1', and so on.
011 * RatPolyStacks can also be described constructively, with the append
012 * operation, ':'. such that [p1]:S is the result of putting p1 at the front of
013 * the RatPolyStack S.
014 * <p>
015 * A finite sequence has an associated size, corresponding to the number of
016 * elements in the sequence. Thus the size of [] is 0, the size of [p1] is 1,
017 * the size of [p1, p1] is 2, and so on.
018 * <p>
019 */
020public final class RatPolyStack implements Iterable<RatPoly> {
021
022    private final Stack<RatPoly> polys;
023
024    // Abstraction Function:
025    // Each element of a RatPolyStack, s, is mapped to the
026    // corresponding element of polys.
027    //
028    // RepInvariant:
029    // polys != null &&
030    // forall i such that (0 <= i < polys.size(), polys.get(i) != null
031
032    /**
033     * @effects Constructs a new RatPolyStack, [].
034     */
035    public RatPolyStack() {
036        polys = new Stack<RatPoly>();
037        checkRep();
038    }
039
040    /**
041     * Returns the number of RayPolys in this RatPolyStack.
042     *
043     * @return the size of this sequence.
044     */
045    public int size() {
046        // TODO: Fill in this method, then remove the RuntimeException
047        throw new RuntimeException("RatPolyStack->size() is not yet implemented");
048    }
049
050    /**
051     * Pushes a RatPoly onto the top of this.
052     *
053     * @param p The RatPoly to push onto this stack.
054     * @requires p != null
055     * @modifies this
056     * @effects this_post = [p]:this
057     */
058    public void push(RatPoly p) {
059        // TODO: Fill in this method, then remove the RuntimeException
060        throw new RuntimeException("RatPolyStack->push() is not yet implemented");
061    }
062
063    /**
064     * Removes and returns the top RatPoly.
065     *
066     * @requires this.size() > 0
067     * @modifies this
068     * @effects If this = [p]:S then this_post = S
069     * @return p where this = [p]:S
070     */
071    public RatPoly pop() {
072        // TODO: Fill in this method, then remove the RuntimeException
073        throw new RuntimeException("RatPolyStack->pop() is not yet implemented");
074    }
075
076    /**
077     * Duplicates the top RatPoly on this.
078     *
079     * @requires this.size() > 0
080     * @modifies this
081     * @effects If this = [p]:S then this_post = [p, p]:S
082     */
083    public void dup() {
084        // TODO: Fill in this method, then remove the RuntimeException
085        throw new RuntimeException("RatPolyStack->dup() is not yet implemented");
086    }
087
088    /**
089     * Swaps the top two elements of this.
090     *
091     * @requires this.size() >= 2
092     * @modifies this
093     * @effects If this = [p1, p2]:S then this_post = [p2, p1]:S
094     */
095    public void swap() {
096        // TODO: Fill in this method, then remove the RuntimeException
097        throw new RuntimeException("RatPolyStack->swap() is not yet implemented");
098    }
099
100    /**
101     * Clears the stack.
102     *
103     * @modifies this
104     * @effects this_post = []
105     */
106    public void clear() {
107        // TODO: Fill in this method, then remove the RuntimeException
108        throw new RuntimeException("RatPolyStack->clear() is not yet implemented");
109    }
110
111    /**
112     * Returns the RatPoly that is 'index' elements from the top of the stack.
113     *
114     * @param index The index of the RatPoly to be retrieved.
115     * @requires index >= 0 && index < this.size()
116     * @return If this = S:[p]:T where S.size() = index, then returns p.
117     */
118    public RatPoly getNthFromTop(int index) {
119        // TODO: Fill in this method, then remove the RuntimeException
120        throw new RuntimeException(
121                "RatPolyStack->getNthFromTop() unimplemented!\n");
122    }
123
124    /**
125     * Pops two elements off of the stack, adds them, and places the result on
126     * top of the stack.
127     *
128     * @requires this.size() >= 2
129     * @modifies this
130     * @effects If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 + p2
131     */
132    public void add() {
133        // TODO: Fill in this method, then remove the RuntimeException
134        throw new RuntimeException("RatPolyStack->add() is not yet implemented");
135    }
136
137    /**
138     * Subtracts the top poly from the next from top poly, pops both off the
139     * stack, and places the result on top of the stack.
140     *
141     * @requires this.size() >= 2
142     * @modifies this
143     * @effects If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 - p1
144     */
145    public void sub() {
146        // TODO: Fill in this method, then remove the RuntimeException
147        throw new RuntimeException("RatPolyStack->sub() is not yet implemented");
148    }
149
150    /**
151     * Pops two elements off of the stack, multiplies them, and places the
152     * result on top of the stack.
153     *
154     * @requires this.size() >= 2
155     * @modifies this
156     * @effects If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 * p2
157     */
158    public void mul() {
159        // TODO: Fill in this method, then remove the RuntimeException
160        throw new RuntimeException("RatPolyStack->mul() is not yet implemented");
161    }
162
163    /**
164     * Divides the next from top poly by the top poly, pops both off the stack,
165     * and places the result on top of the stack.
166     *
167     * @requires this.size() >= 2
168     * @modifies this
169     * @effects If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 / p1
170     */
171    public void div() {
172        // TODO: Fill in this method, then remove the RuntimeException
173        throw new RuntimeException("RatPolyStack->div() is not yet implemented");
174    }
175
176    /**
177     * Pops the top element off of the stack, differentiates it, and places the
178     * result on top of the stack.
179     *
180     * @requires this.size() >= 1
181     * @modifies this
182     * @effects If this = [p1]:S then this_post = [p2]:S where p2 = derivative
183     *          of p1
184     */
185    public void differentiate() {
186        // TODO: Fill in this method, then remove the RuntimeException
187        throw new RuntimeException(
188                "RatPolyStack->differentiate() unimplemented!\n");
189    }
190
191    /**
192     * Pops the top element off of the stack, integrates it, and places the
193     * result on top of the stack.
194     *
195     * @requires this.size() >= 1
196     * @modifies this
197     * @effects If this = [p1]:S then this_post = [p2]:S where p2 = indefinite
198     *          integral of p1 with integration constant 0
199     */
200    public void integrate() {
201        // TODO: Fill in this method, then remove the RuntimeException
202        throw new RuntimeException("RatPolyStack->integrate() is not yet implemented");
203    }
204
205    /**
206     * Returns an iterator of the elements contained in the stack.
207     *
208     * @return an iterator of the elements contained in the stack in order from
209     *         the bottom of the stack to the top of the stack.
210     */
211    @Override
212    public Iterator<RatPoly> iterator() {
213        return polys.iterator();
214    }
215
216    /**
217     * Checks that the representation invariant holds (if any).
218     */
219    // Throws a RuntimeException if the rep invariant is violated.
220    private void checkRep() throws RuntimeException {
221        /*assert polys != null: "polys should never be null.";
222        for (RatPoly p : polys) {
223                assert p != null: "polys should never contain a null element.";
224        }*/
225        
226        
227        if (polys == null) {
228            throw new RuntimeException("polys should never be null.");
229        }
230        for (RatPoly p : polys) {
231            if (p == null) {
232                throw new RuntimeException(
233                        "polys should never contain a null element.");
234            }
235        }
236    }
237}