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