diff options
Diffstat (limited to 'Week7/src')
| -rw-r--r-- | Week7/src/polynomial/Polynomial.java | 11 | ||||
| -rw-r--r-- | Week7/src/polynomial/Term.java | 10 | 
2 files changed, 13 insertions, 8 deletions
diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java index f7b1194..f7f9860 100644 --- a/Week7/src/polynomial/Polynomial.java +++ b/Week7/src/polynomial/Polynomial.java @@ -116,7 +116,7 @@ public class Polynomial {                  return;              }          } -        terms.add(t); +        terms.add(new Term(t));      }      /** @@ -124,8 +124,9 @@ public class Polynomial {       * @param p the polynomial to subtract       */      public void minus(Polynomial p) { -        p.times(new Polynomial("-1 0")); -        plus(p); +        Polynomial temp = new Polynomial(p); +        temp.times(new Polynomial("-1 0")); +        plus(temp);      }      /** @@ -136,7 +137,7 @@ public class Polynomial {          Polynomial result = new Polynomial();          for (Term that_t : p.terms)               for (Term this_t : terms) -                result.plus(new Polynomial((that_t.getCoef() * this_t.getCoef()) + " " + (that_t.getExp() + this_t.getExp()))); +                result.plus(new Term(that_t.getCoef() * this_t.getCoef(), that_t.getExp() + this_t.getExp()));          terms = result.terms;      } @@ -153,7 +154,7 @@ public class Polynomial {          if (other_poly == null || other_poly.getClass() != getClass())              return false; -        Polynomial that = (Polynomial) other_poly; +        Polynomial that = new Polynomial((Polynomial) other_poly);      // We need to copy because later we'll remove elements          if (terms.size() != that.terms.size())               return false;          for (Term this_t : terms) { diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java index 7cde927..60c6381 100644 --- a/Week7/src/polynomial/Term.java +++ b/Week7/src/polynomial/Term.java @@ -81,17 +81,21 @@ public class Term {      /**       * Converts a term into a readable representation the standard format is       * %fx^%d +     * I changed the implementation to remove trailing zeroes from the coefficient.       *       * @return the string representing the term       */      @Override      public String toString() { +        String nice_coefficient = Double.toString(coefficient); +        if (Math.round(coefficient) == coefficient) +            nice_coefficient =  Integer.toString((int) coefficient);          if (exponent == 0) { -            return String.format("%f", coefficient, exponent); +            return String.format("%s", nice_coefficient, exponent);          } else if (exponent == 1) { -            return String.format("%fx", coefficient, exponent); +            return String.format("%sx", nice_coefficient, exponent);          } else { -            return String.format("%fx^%d", coefficient, exponent); +            return String.format("%sx^%d", nice_coefficient, exponent);          }      }  | 
