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