diff options
author | Camil Staps | 2015-03-17 14:29:35 +0100 |
---|---|---|
committer | Camil Staps | 2015-03-17 14:29:35 +0100 |
commit | 2c70918e505c597724961f0d5179c7878d5b68b7 (patch) | |
tree | 25ebadb260155ae25e63c4609d26e9e8c30ae003 /Week7/src/polynomial | |
parent | Week 7 framework (diff) |
Week 7 framework + startzip
Diffstat (limited to 'Week7/src/polynomial')
-rw-r--r-- | Week7/src/polynomial/Polynomial.java | 86 | ||||
-rw-r--r-- | Week7/src/polynomial/Term.java | 127 |
2 files changed, 213 insertions, 0 deletions
diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java new file mode 100644 index 0000000..3b327f4 --- /dev/null +++ b/Week7/src/polynomial/Polynomial.java @@ -0,0 +1,86 @@ +package polynomial; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +/** + * A skeleton class for representing Polynomials + * + * @author Sjaak Smetsers + * @date 10-03-2015 + */ +public class Polynomial { + + /** + * A polynomial is a sequence of terms here kept in an List + */ + 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. + * + * @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)) { + terms.add(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() { + return null; + } + + public void plus(Polynomial b) { + } + + + public void minus(Polynomial b) { + } + + + public void times(Polynomial b) { + } + + public void divide(Polynomial b) { + } + + @Override + public boolean equals(Object other_poly) { + return false; + } + +} diff --git a/Week7/src/polynomial/Term.java b/Week7/src/polynomial/Term.java new file mode 100644 index 0000000..67bcf04 --- /dev/null +++ b/Week7/src/polynomial/Term.java @@ -0,0 +1,127 @@ +package polynomial; + +import java.util.Scanner; + +/** + * For representing terms in a polynomial. + * + * @author Sjaak Smetsers + * @version 1.0 + * @date 15-02-2012 + */ +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 + * + * @return the string representing the term + */ + @Override + public String toString() { + if (exponent == 0) { + return String.format("%f", coefficient, exponent); + } else if (exponent == 1) { + return String.format("%fx", coefficient, exponent); + } else { + return String.format("%fx^%d", coefficient, exponent); + } + } + + /** + * Standard implementation of equality + */ + @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; + } + } + + /** + * 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; + } + } +} |