diff options
| author | Camil Staps | 2015-03-17 19:10:07 +0100 | 
|---|---|---|
| committer | Camil Staps | 2015-03-17 19:10:07 +0100 | 
| commit | 5dca7b0f4aed3ae3ba90cdf205ca38e9269cfb37 (patch) | |
| tree | 8b38d7785371d384646330dbd2d84f46d8e9696c /Week7/src | |
| parent | Added MIT license w7 (diff) | |
Added javadoc
Diffstat (limited to 'Week7/src')
| -rw-r--r-- | Week7/src/polynomial/Polynomial.java | 65 | ||||
| -rw-r--r-- | Week7/src/polynomial/Term.java | 10 | 
2 files changed, 64 insertions, 11 deletions
diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java index 2a1b28e..40c1448 100644 --- a/Week7/src/polynomial/Polynomial.java +++ b/Week7/src/polynomial/Polynomial.java @@ -6,10 +6,13 @@ import java.util.List;  import java.util.Scanner;  /** - * A skeleton class for representing Polynomials + * A class for representing Polynomials   *   * @author Sjaak Smetsers   * @date 10-03-2015 + *  + * @author Camil Staps, s4498062 + * @date 17-03-2015   */  public class Polynomial { @@ -28,6 +31,18 @@ public class Polynomial {      /**       * A Constructor creating a polynomial from the argument string. +     *  +     * This implementation, in contrast to the given implementation, allows for  +     * entering polynomials with two or more terms with the same exponent. Even  +     * though this is strictly not necessary and these inputs aren't legit, this +     * behaviour is always better than the behaviour of the given implementation  +     * which would happily add two terms with the same exponent to our list. +     *  +     * This implementation also directly checks that the coefficient unequals 0, +     * which is also not need-to-have, but still nice-to-have. +     *  +     * Both changes to the given implementation do not change the behaviour on +     * legit input but do allow a user to mess up in some cases.       *       * @param s a String representing a list of terms which is converted to a       * scanner and passed to scanTerm for reading each individual term @@ -36,16 +51,15 @@ public class Polynomial {          terms = new ArrayList<>();          Scanner scan = new Scanner(s); -        for (Term t = Term.scanTerm(scan); t != null; t = Term.scanTerm(scan)) { -            terms.add(t); -        } +        for (Term t = Term.scanTerm(scan); t != null; t = Term.scanTerm(scan)) +            if (t.getCoef() != 0) +                plus(t);      }      /**       * The copy constructor for making a deep copy       *       * @param p the copied polynomial -     *       */      public Polynomial( Polynomial p ) {          terms = new ArrayList<>(p.terms.size()); @@ -73,12 +87,20 @@ public class Polynomial {          return sb.toString();      } +    /** +     * Add a polynomial to this polynomial +     * @param b the polynomial to add +     */      public void plus(Polynomial b) {          for (Term t : b.terms)              plus(t);      } -    public void plus(Term t) { +    /** +     * Add a term +     * @param t the term to add +     */ +    private void plus(Term t) {          Iterator<Term> iter = terms.iterator();          while (iter.hasNext()) {              Term this_t = iter.next(); @@ -92,14 +114,22 @@ public class Polynomial {          terms.add(t);      } -    public void minus(Polynomial b) { -        b.times(new Polynomial("-1 0")); -        plus(b); +    /** +     * Subtract a polynomial from this polynomial +     * @param p the polynomial to subtract +     */ +    public void minus(Polynomial p) { +        p.times(new Polynomial("-1 0")); +        plus(p);      } -    public void times(Polynomial b) { +    /** +     * Multiply a polynomial with this polynomial +     * @param p the polynomial to multiply with +     */ +    public void times(Polynomial p) {          Polynomial result = new Polynomial(); -        for (Term that_t : b.terms)  +        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())));          terms = result.terms; @@ -108,8 +138,16 @@ public class Polynomial {      public void divide(Polynomial b) {      } +    /** +     * Check equality +     * @param other_poly the object to check with +     * @return true iff other_poly this instance +     */      @Override      public boolean equals(Object other_poly) { +        if (other_poly == null || other_poly.getClass() != getClass()) +            return false; +                  Polynomial that = (Polynomial) other_poly;          if (terms.size() != that.terms.size())               return false; @@ -130,6 +168,11 @@ public class Polynomial {          return true;      } +    /** +     * Apply the polynomial to a concrete variable +     * @param x the variable +     * @return the result of the polynomial applied on x +     */      public double apply(double x) {          double result = 0;          for (Term t : terms) diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java index b8d9f16..7cde927 100644 --- a/Week7/src/polynomial/Term.java +++ b/Week7/src/polynomial/Term.java @@ -8,6 +8,9 @@ import java.util.Scanner;   * @author Sjaak Smetsers   * @version 1.0   * @date 15-02-2012 + *  + * @author Camil Staps, s4498062 + * @date 17-03-2015   */  public class Term { @@ -94,6 +97,8 @@ public class Term {      /**       * Standard implementation of equality +     * @param may_be_term the object to check equality with +     * @return true iff may_be_term represents the same term as this term       */      @Override      public boolean equals(Object may_be_term) { @@ -106,6 +111,11 @@ public class Term {          }      } +    /** +     * Apply a term to a concrete variable +     * @param x the variable to apply the term to +     * @return the result of the application of the term to x +     */      public double apply(double x) {          return coefficient * Math.pow(x, exponent);      }  | 
