1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import java.util.Hashtable; public class Poly implements Cloneable { //the first integer is the degree, the second integer is the coefficient Hashtable<Integer, Integer> poly = new Hashtable<Integer, Integer>(); @Override //print this as a polynomial public String toString() { String str = "" ; int degree = 0 ; for ( int i = 0 ; i < this .poly.size(); i++) { //find next degree while (! this .poly.containsKey(degree)) { degree++; } //construct output str += this .poly.get(degree) + " x^" + degree; //append + if needed if (i != ( this .poly.size() - 1 )) { str += " + " ; } //increment degree degree++; } return str; } //constructor for 0*x^0 public Poly() { //initialise polynomial this .poly.clear(); this .poly.put( 0 , 0 ); } //constructor for c*x^n public Poly( int c, int n) throws Exception { //throw less than 0 exception if (n< 0 ) { throw new Exception( "n must be greater or equal to 0" ); } //initialise polynomial this .poly.clear(); this .poly.put(n, c); } //return the degree of the polynomial public int degree() { int degree = 0 ; for ( int i = 0 ; i < this .poly.size(); i++) { //find next degree while (! this .poly.containsKey(degree)) { degree++; } //increment degree if needed if (i != ( this .poly.size() - 1 )) { degree++; } } return degree; } //return coefficient with exponent n public int coeff( int n) throws Exception { //throw less than 0 exception if (n< 0 ) { throw new Exception( "n must be greater or equal to 0" ); } //return coefficient if ( this .poly.containsKey(n)) { return this .poly.get(n); } //return 0 otherwise else { return 0 ; } } //sum of this and a polynomial public Poly add(Poly q) throws Exception { Poly sum = new Poly(); sum.poly.clear(); //throw null exception if (q.poly.isEmpty()) { throw new Exception( "q must not be null" ); } //get degree of polynomials int qdeg = q.degree(); int thisdeg = this .degree(); //find highest degree int maxdeg = qdeg; if (maxdeg < thisdeg) {maxdeg = thisdeg;} //add polynomials for ( int i = 0 ; i < (maxdeg + 1 ); i++) { //addition will take place iff q has a coef with such degree if (q.poly.containsKey(i)) { //case where this and q have coef with same degree if ( this .poly.containsKey(i)) { int s = this .poly.get(i) + q.poly.get(i); sum.poly.put(i, s); } //case where this doesn't have a coef q does else { sum.poly.put(i, q.poly.get(i)); } } //case where q doesn't have a coef this does if ( this .poly.containsKey(i)) { sum.poly.put(i, this .poly.get(i)); } } return sum; } //difference of this and a polynomial public Poly sub(Poly q) throws Exception { Poly diff = new Poly(); diff.poly.clear(); //throw null exception if (q.poly.isEmpty()) { throw new Exception( "q must not be null" ); } //get degree of polynomials int qdeg = q.degree(); int thisdeg = this .degree(); //find highest degree int maxdeg = qdeg; if (maxdeg < thisdeg) {maxdeg = thisdeg;} //sub polynomials for ( int i = 0 ; i < (maxdeg + 1 ); i++) { //addition will take place iff q has a coef with such degree if (q.poly.containsKey(i)) { //case where this and q have coef with same degree if ( this .poly.containsKey(i)) { int s = this .poly.get(i) - q.poly.get(i); diff.poly.put(i, s); } //case where this doesn't have a coef q does else { diff.poly.put(i, q.poly.get(i)); } } //case where q doesn't have a coef this does else if ( this .poly.containsKey(i)) { diff.poly.put(i, this .poly.get(i)); } } return diff; } public boolean equals(Poly q) { //find degree of polynomials int qdeg = q.degree(); int thisdeg = this .degree(); //polynomials are not equal if they don't have the same degree if (qdeg != thisdeg) { return false ; } //compare polynomials else { for ( int i = 0 ; i < (thisdeg + 1 ); i++) { //this has a coef if ( this .poly.containsKey(i)) { if (! q.poly.containsKey(i)) { return false ; } else { if ( this .poly.get(i) != q.poly.get(i)) { return false ; } } } //q has a coef if (q.poly.containsKey(i)) { if (! this .poly.containsKey(i)) { return false ; } else { if ( this .poly.get(i) != q.poly.get(i)) { return false ; } } } //skip if no coef } } return true ; } @Override public Poly clone() { Poly clone; //try to clone try { clone = (Poly) super .clone(); } catch (CloneNotSupportedException e) { System.out.println( "Cannot clone this" ); return null ; } return clone; } public static void main(String[] args) { //start of code execution. You can have some test code in //here that you may have written to test your class. try { Poly p1 = new Poly(); //0 x^0 Poly p2 = new Poly( 2 , 4 ); //2 x^4 Poly p3 = p1.add(p2); //0 x^0 + 2 x^4 Poly p4 = new Poly(- 1 , 4 ); //-1 x^4 Poly p5 = p2.sub(p4); //3 x^4 System.out.println(p1.toString()); System.out.println(p2.toString()); System.out.println(p3.toString()); System.out.println(p4.toString()); System.out.println(p5.toString()); } catch (Exception e) { System.out.println( "invalid action" ); } } } |