aboutsummaryrefslogtreecommitdiff
path: root/Week7/src/polynomial
diff options
context:
space:
mode:
Diffstat (limited to 'Week7/src/polynomial')
-rw-r--r--Week7/src/polynomial/Polynomial.java189
-rw-r--r--Week7/src/polynomial/Term.java145
2 files changed, 0 insertions, 334 deletions
diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java
deleted file mode 100644
index c87b96a..0000000
--- a/Week7/src/polynomial/Polynomial.java
+++ /dev/null
@@ -1,189 +0,0 @@
-package polynomial;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Scanner;
-
-/**
- * A class for representing Polynomials
- *
- * @author Sjaak Smetsers
- * @date 10-03-2015
- *
- * @author Camil Staps, s4498062
- * @date 17-03-2015
- */
-public class Polynomial {
-
- /**
- * A polynomial is a sequence of terms here kept in an List.
- * The assignment tells us 'it's handy' when terms are ordered by exponent.
- * We don't seem to need that though, so there's no need to implement it.
- */
- List<Term> terms;
-
- /**
- * A constructor for creating the zero Polynomial zero is presented as an
- * empty list of terms and not as a single term with 0 as a coefficient
- */
- public Polynomial() {
- terms = new ArrayList<>();
- }
-
- /**
- * 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
- */
- public Polynomial( String s ) {
- terms = new ArrayList<>();
- Scanner scan = new Scanner(s);
-
- 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());
- for (Term t : p.terms) {
- terms.add(new Term(t));
- }
- }
-
- /**
- * A straightforward conversion of a Polynomial into a string based on the
- * toString for terms
- *
- * @return a readable string representation of this
- */
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- boolean not_first = false;
- for (Term t : terms) {
- if (not_first && t.getCoef() >= 0)
- sb.append("+");
- sb.append(t);
- not_first = true;
- }
- 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);
- }
-
- /**
- * Add a term
- * @param t the term to add
- */
- private void plus(Term t) {
- if (t.getCoef() == 0)
- return;
-
- Iterator<Term> iter = terms.iterator();
- while (iter.hasNext()) {
- Term this_t = iter.next();
- if (this_t.getExp() == t.getExp()) {
- this_t.plus(t);
- if (this_t.getCoef() == 0)
- iter.remove();
- return;
- }
- }
- terms.add(new Term(t));
- }
-
- /**
- * Subtract a polynomial from this polynomial
- * @param p the polynomial to subtract
- */
- public void minus(Polynomial p) {
- Polynomial temp = new Polynomial(p);
- temp.times(new Polynomial("-1 0"));
- plus(temp);
- }
-
- /**
- * 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 : p.terms)
- for (Term this_t : terms)
- result.plus(new Term(that_t.getCoef() * this_t.getCoef(), that_t.getExp() + this_t.getExp()));
- terms = result.terms;
- }
-
- public void divide(Polynomial b) {
- }
-
- /**
- * Check equality
- * @param other_poly the object to check with
- * @return true iff other_poly represents the same polynomial as this instance
- */
- @Override
- public boolean equals(Object other_poly) {
- if (other_poly == null || other_poly.getClass() != getClass())
- return false;
-
- 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) {
- Iterator<Term> that_iter = that.terms.iterator();
- boolean found = false;
- while (that_iter.hasNext()) {
- if (this_t.equals(that_iter.next())) {
- found = true;
- that_iter.remove();
- break;
- }
- }
- if (!found) {
- return false;
- }
- }
- 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)
- result += t.apply(x);
- return result;
- }
-
-}
diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java
deleted file mode 100644
index 60c6381..0000000
--- a/Week7/src/polynomial/Term.java
+++ /dev/null
@@ -1,145 +0,0 @@
-package polynomial;
-
-import java.util.Scanner;
-
-/**
- * For representing terms in a polynomial.
- *
- * @author Sjaak Smetsers
- * @version 1.0
- * @date 15-02-2012
- *
- * @author Camil Staps, s4498062
- * @date 17-03-2015
- */
-public class Term {
-
- /**
- * Each term consists of a coefficient and an exponent
- */
- private double coefficient;
- private int exponent;
-
- /**
- * a get-method for the exponent
- *
- * @return exponent
- */
- public int getExp() {
- return exponent;
- }
-
- /**
- * a get-method for the coefficient
- *
- * @return coefficient
- */
- public double getCoef() {
- return coefficient;
- }
-
- /**
- * A two-argument constructor
- *
- * @param c the value for the coefficient
- * @param e the value for the exponent
- */
- public Term(double c, int e) {
- coefficient = c;
- exponent = e;
- }
-
- /**
- * A copy-constructor
- *
- * @param t the term to be copied
- */
- public Term(Term t) {
- this(t.coefficient, t.exponent);
- }
-
- /**
- * For adding two terms with equal exponents
- *
- * @param t the term added to this
- * @require exponent == t.exponent
- */
- public void plus(Term t) {
- coefficient += t.coefficient;
- }
-
- /**
- * For multiplying two terms
- *
- * @param t the multiplier
- */
- public void times(Term t) {
- exponent += t.exponent;
- coefficient *= t.coefficient;
- }
-
- /**
- * 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("%s", nice_coefficient, exponent);
- } else if (exponent == 1) {
- return String.format("%sx", nice_coefficient, exponent);
- } else {
- return String.format("%sx^%d", nice_coefficient, exponent);
- }
- }
-
- /**
- * 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) {
- if (may_be_term == null || getClass() != may_be_term.getClass()) {
- return false;
- } else {
- Term term = (Term) may_be_term;
- return exponent == term.exponent
- && coefficient == term.coefficient;
- }
- }
-
- /**
- * 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);
- }
-
- /**
- * A static method for converting scanner input into a term. The expected
- * format is two numbers (coef, exp) separated by whitespaces. The coef
- * should be either an integer or a decimal number. The exp is an integer.
- *
- * @param s the scanner providing the input
- * @return null if no term could be found, the found term otherwise
- */
- public static Term scanTerm(Scanner s) {
- String coef_patt = "\\-?\\d+(\\.\\d+)?";
- if (s.hasNext(coef_patt)) {
- String coef = s.next(coef_patt);
- int exp = s.nextInt();
- return new Term(Double.parseDouble(coef), exp);
- } else {
- return null;
- }
- }
-}