hw3
Class RatPolyStack

java.lang.Object
  extended by hw3.RatPolyStack
All Implemented Interfaces:
Iterable<RatPoly>

public final class RatPolyStack
extends Object
implements Iterable<RatPoly>

RatPolyStack is a mutable finite sequence of RatPoly objects.

Each RatPolyStack can be described by [p1, p2, ... ], where [] is an empty stack, [p1] is a one element stack containing the Poly 'p1', and so on. RatPolyStacks can also be described constructively, with the append operation, ':'. such that [p1]:S is the result of putting p1 at the front of the RatPolyStack S.

A finite sequence has an associated size, corresponding to the number of elements in the sequence. Thus the size of [] is 0, the size of [p1] is 1, the size of [p1, p1] is 2, and so on.


Constructor Summary
RatPolyStack()
           
 
Method Summary
 void add()
          Pops two elements off of the stack, adds them, and places the result on top of the stack.
 void clear()
          Clears the stack.
 void differentiate()
          Pops the top element off of the stack, differentiates it, and places the result on top of the stack.
 void div()
          Divides the next from top poly by the top poly, pops both off the stack, and places the result on top of the stack.
 void dup()
          Duplicates the top RatPoly on this.
 RatPoly getNthFromTop(int index)
          Returns the RatPoly that is 'index' elements from the top of the stack.
 void integrate()
          Pops the top element off of the stack, integrates it, and places the result on top of the stack.
 Iterator<RatPoly> iterator()
          Returns an iterator of the elements contained in the stack.
 void mul()
          Pops two elements off of the stack, multiplies them, and places the result on top of the stack.
 RatPoly pop()
          Removes and returns the top RatPoly.
 void push(RatPoly p)
          Pushes a RatPoly onto the top of this.
 int size()
          Returns the number of RayPolys in this RatPolyStack.
 void sub()
          Subtracts the top poly from the next from top poly, pops both off the stack, and places the result on top of the stack.
 void swap()
          Swaps the top two elements of this.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RatPolyStack

public RatPolyStack()
Effects:
Constructs a new RatPolyStack, [].
Method Detail

size

public int size()
Returns the number of RayPolys in this RatPolyStack.

Returns:
the size of this sequence.

push

public void push(RatPoly p)
Pushes a RatPoly onto the top of this.

Requires:
p != null
Effects:
this_post = [p]:this
Modifies:
this

pop

public RatPoly pop()
Removes and returns the top RatPoly.

Returns:
p where this = [p]:S
Requires:
this.size() > 0
Effects:
If this = [p]:S then this_post = S
Modifies:
this

dup

public void dup()
Duplicates the top RatPoly on this.

Requires:
this.size() > 0
Effects:
If this = [p]:S then this_post = [p, p]:S
Modifies:
this

swap

public void swap()
Swaps the top two elements of this.

Requires:
this.size() >= 2
Effects:
If this = [p1, p2]:S then this_post = [p2, p1]:S
Modifies:
this

clear

public void clear()
Clears the stack.

Effects:
this_post = []
Modifies:
this

getNthFromTop

public RatPoly getNthFromTop(int index)
Returns the RatPoly that is 'index' elements from the top of the stack.

Returns:
If this = S:[p]:T where S.size() = index, then returns p.
Requires:
index >= 0 && index < this.size()

add

public void add()
Pops two elements off of the stack, adds them, and places the result on top of the stack.

Requires:
this.size() >= 2
Effects:
If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 + p2
Modifies:
this

sub

public void sub()
Subtracts the top poly from the next from top poly, pops both off the stack, and places the result on top of the stack.

Requires:
this.size() >= 2
Effects:
If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 - p1
Modifies:
this

mul

public void mul()
Pops two elements off of the stack, multiplies them, and places the result on top of the stack.

Requires:
this.size() >= 2
Effects:
If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 * p2
Modifies:
this

div

public void div()
Divides the next from top poly by the top poly, pops both off the stack, and places the result on top of the stack.

Requires:
this.size() >= 2
Effects:
If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 / p1
Modifies:
this

differentiate

public void differentiate()
Pops the top element off of the stack, differentiates it, and places the result on top of the stack.

Requires:
this.size() >= 1
Effects:
If this = [p1]:S then this_post = [p2]:S where p2 = derivative of p1
Modifies:
this

integrate

public void integrate()
Pops the top element off of the stack, integrates it, and places the result on top of the stack.

Requires:
this.size() >= 1
Effects:
If this = [p1]:S then this_post = [p2]:S where p2 = indefinite integral of p1 with integration constant 0
Modifies:
this

iterator

public Iterator<RatPoly> iterator()
Returns an iterator of the elements contained in the stack.

Specified by:
iterator in interface Iterable<RatPoly>
Returns:
an iterator of the elements contained in the stack in order from the bottom of the stack to the top of the stack.