Package hw4
Class RatPolyStack
- java.lang.Object
-
- hw4.RatPolyStack
-
- All Implemented Interfaces:
java.lang.Iterable<RatPoly>
public final class RatPolyStack extends java.lang.Object implements java.lang.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
Constructors Constructor Description RatPolyStack()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd()Pops two elements off of the stack, adds them, and places the result on top of the stack.voidclear()Clears the stack.voiddifferentiate()Pops the top element off of the stack, differentiates it, and places the result on top of the stack.voiddiv()Divides the next from top poly by the top poly, pops both off the stack, and places the result on top of the stack.voiddup()Duplicates the top RatPoly on this.RatPolygetNthFromTop(int index)Returns the RatPoly that is 'index' elements from the top of the stack.voidintegrate()Pops the top element off of the stack, integrates it, and places the result on top of the stack.java.util.Iterator<RatPoly>iterator()Returns an iterator of the elements contained in the stack.voidmul()Pops two elements off of the stack, multiplies them, and places the result on top of the stack.RatPolypop()Removes and returns the top RatPoly.voidpush(RatPoly p)Pushes a RatPoly onto the top of this.intsize()Returns the number of RayPolys in this RatPolyStack.voidsub()Subtracts the top poly from the next from top poly, pops both off the stack, and places the result on top of the stack.voidswap()Swaps the top two elements of this.
-
-
-
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.- Parameters:
p- The RatPoly to push onto this stack.- Spec.modifies:
- this
- Spec.effects:
- this_post = [p]:this
- Spec.requires:
- p != null
-
pop
public RatPoly pop()
Removes and returns the top RatPoly.- Returns:
- p where this = [p]:S
- Spec.modifies:
- this
- Spec.effects:
- If this = [p]:S then this_post = S
- Spec.requires:
- this.size() > 0
-
dup
public void dup()
Duplicates the top RatPoly on this.- Spec.modifies:
- this
- Spec.effects:
- If this = [p]:S then this_post = [p, p]:S
- Spec.requires:
- this.size() > 0
-
swap
public void swap()
Swaps the top two elements of this.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1, p2]:S then this_post = [p2, p1]:S
- Spec.requires:
- this.size() >= 2
-
clear
public void clear()
Clears the stack.- Spec.modifies:
- this
- Spec.effects:
- this_post = []
-
getNthFromTop
public RatPoly getNthFromTop(int index)
Returns the RatPoly that is 'index' elements from the top of the stack.- Parameters:
index- The index of the RatPoly to be retrieved.- Returns:
- If this = S:[p]:T where S.size() = index, then returns p.
- Spec.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.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 + p2
- Spec.requires:
- this.size() >= 2
-
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.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 - p1
- Spec.requires:
- this.size() >= 2
-
mul
public void mul()
Pops two elements off of the stack, multiplies them, and places the result on top of the stack.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1, p2]:S then this_post = [p3]:S where p3 = p1 * p2
- Spec.requires:
- this.size() >= 2
-
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.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1, p2]:S then this_post = [p3]:S where p3 = p2 / p1
- Spec.requires:
- this.size() >= 2
-
differentiate
public void differentiate()
Pops the top element off of the stack, differentiates it, and places the result on top of the stack.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1]:S then this_post = [p2]:S where p2 = derivative of p1
- Spec.requires:
- this.size() >= 1
-
integrate
public void integrate()
Pops the top element off of the stack, integrates it, and places the result on top of the stack.- Spec.modifies:
- this
- Spec.effects:
- If this = [p1]:S then this_post = [p2]:S where p2 = indefinite integral of p1 with integration constant 0
- Spec.requires:
- this.size() >= 1
-
iterator
public java.util.Iterator<RatPoly> iterator()
Returns an iterator of the elements contained in the stack.- Specified by:
iteratorin interfacejava.lang.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.
-
-