From 6a44b074f0169a1b0f9e92347af929c5e471746e Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sat, 18 Apr 2015 13:44:44 +0200 Subject: Reorganised projects --- Week7 Polynomials/.gitignore | 3 + Week7 Polynomials/Assignment (in Dutch).pdf | Bin 0 -> 132898 bytes Week7 Polynomials/Week7.tar.gz | Bin 0 -> 5487 bytes Week7 Polynomials/build.xml | 73 + Week7 Polynomials/nbproject/build-impl.xml | 1413 ++++++++++++++++++++ Week7 Polynomials/nbproject/genfiles.properties | 8 + Week7 Polynomials/nbproject/project.properties | 73 + Week7 Polynomials/nbproject/project.xml | 15 + Week7 Polynomials/src/main/Main.java | 23 + Week7 Polynomials/src/polynomial/Polynomial.java | 189 +++ Week7 Polynomials/src/polynomial/Term.java | 145 ++ .../test/polynomial/PolynomialTest.java | 226 ++++ 12 files changed, 2168 insertions(+) create mode 100644 Week7 Polynomials/.gitignore create mode 100644 Week7 Polynomials/Assignment (in Dutch).pdf create mode 100644 Week7 Polynomials/Week7.tar.gz create mode 100644 Week7 Polynomials/build.xml create mode 100644 Week7 Polynomials/nbproject/build-impl.xml create mode 100644 Week7 Polynomials/nbproject/genfiles.properties create mode 100644 Week7 Polynomials/nbproject/project.properties create mode 100644 Week7 Polynomials/nbproject/project.xml create mode 100644 Week7 Polynomials/src/main/Main.java create mode 100644 Week7 Polynomials/src/polynomial/Polynomial.java create mode 100644 Week7 Polynomials/src/polynomial/Term.java create mode 100644 Week7 Polynomials/test/polynomial/PolynomialTest.java (limited to 'Week7 Polynomials') diff --git a/Week7 Polynomials/.gitignore b/Week7 Polynomials/.gitignore new file mode 100644 index 0000000..4523fc2 --- /dev/null +++ b/Week7 Polynomials/.gitignore @@ -0,0 +1,3 @@ +nbproject/private/ +build/ +dist/ diff --git a/Week7 Polynomials/Assignment (in Dutch).pdf b/Week7 Polynomials/Assignment (in Dutch).pdf new file mode 100644 index 0000000..5c9bd0c Binary files /dev/null and b/Week7 Polynomials/Assignment (in Dutch).pdf differ diff --git a/Week7 Polynomials/Week7.tar.gz b/Week7 Polynomials/Week7.tar.gz new file mode 100644 index 0000000..eaa77df Binary files /dev/null and b/Week7 Polynomials/Week7.tar.gz differ diff --git a/Week7 Polynomials/build.xml b/Week7 Polynomials/build.xml new file mode 100644 index 0000000..a54a461 --- /dev/null +++ b/Week7 Polynomials/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Week7 Polynomials. + + + diff --git a/Week7 Polynomials/nbproject/build-impl.xml b/Week7 Polynomials/nbproject/build-impl.xml new file mode 100644 index 0000000..28a95e1 --- /dev/null +++ b/Week7 Polynomials/nbproject/build-impl.xml @@ -0,0 +1,1413 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week7 Polynomials/nbproject/genfiles.properties b/Week7 Polynomials/nbproject/genfiles.properties new file mode 100644 index 0000000..54a54b5 --- /dev/null +++ b/Week7 Polynomials/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=e009bca9 +build.xml.script.CRC32=6ab8560a +build.xml.stylesheet.CRC32=8064a381@1.75.2.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=e009bca9 +nbproject/build-impl.xml.script.CRC32=918db01b +nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48 diff --git a/Week7 Polynomials/nbproject/project.properties b/Week7 Polynomials/nbproject/project.properties new file mode 100644 index 0000000..bbf02d2 --- /dev/null +++ b/Week7 Polynomials/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Week7_Polynomials.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit_4.classpath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=main.Main +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=true +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/Week7 Polynomials/nbproject/project.xml b/Week7 Polynomials/nbproject/project.xml new file mode 100644 index 0000000..18f6583 --- /dev/null +++ b/Week7 Polynomials/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Week7 Polynomials + + + + + + + + + diff --git a/Week7 Polynomials/src/main/Main.java b/Week7 Polynomials/src/main/Main.java new file mode 100644 index 0000000..725b8f3 --- /dev/null +++ b/Week7 Polynomials/src/main/Main.java @@ -0,0 +1,23 @@ +package main; + +import polynomial.Polynomial; + +/** + * + * @author Sjaak Smetsers + */ +public class Main { + + public static void main(String[] args) { + Polynomial p1 = new Polynomial( "3.0 1 2 3" ); + Polynomial p2 = new Polynomial( "4 0 5 3" ); + Polynomial p3 = new Polynomial( "4 0 -5 3" ); + Polynomial p4 = new Polynomial( "6 1" ); + + System.out.println( p1 ); + System.out.println( p2 ); + System.out.println( p3 ); + System.out.println( p4 ); + } + +} diff --git a/Week7 Polynomials/src/polynomial/Polynomial.java b/Week7 Polynomials/src/polynomial/Polynomial.java new file mode 100644 index 0000000..c87b96a --- /dev/null +++ b/Week7 Polynomials/src/polynomial/Polynomial.java @@ -0,0 +1,189 @@ +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 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 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 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 Polynomials/src/polynomial/Term.java b/Week7 Polynomials/src/polynomial/Term.java new file mode 100644 index 0000000..60c6381 --- /dev/null +++ b/Week7 Polynomials/src/polynomial/Term.java @@ -0,0 +1,145 @@ +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; + } + } +} diff --git a/Week7 Polynomials/test/polynomial/PolynomialTest.java b/Week7 Polynomials/test/polynomial/PolynomialTest.java new file mode 100644 index 0000000..c00f165 --- /dev/null +++ b/Week7 Polynomials/test/polynomial/PolynomialTest.java @@ -0,0 +1,226 @@ +package polynomial; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * A test class for the polynomial.Polynomial class + * @author Camil Staps, s4498062 + */ +public class PolynomialTest { + + public PolynomialTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of toString method, of class Polynomial. + */ + @Test + public void testToString() { + System.out.println("toString"); + // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients all at once + assertEquals("3x^5-4x+2.5-3.4x^-7", new Polynomial("3 5 -4 1 2.5 0 -3.4 -7").toString()); + // Test zero polynomial + assertEquals("", new Polynomial().toString()); + } + + /** + * Test of minus method, of class Polynomial. + */ + @Test + public void testMinus() { + System.out.println("minus"); + // Test cancelling out and exponents that don't exist in either of the two polynomials all at once + Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0"); // 7x^4 - 4x^2 + 3x + 5 + instance.minus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10 + assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance); // 7x^4 - 5x^3 - 8x^2 - 5 + + // Test minus = plus (times -1) on many small 2-term polynomials + for (float c1 = -10; c1 < 10; c1 += 2.3) + for (int exp1 = 5; exp1 < 10; exp1++) + for (float c2 = -10; c2 < 10; c2 += 2.3) + for (int exp2 = 5; exp2 < 10; exp2++) { + Polynomial fst = new Polynomial(c1 + " " + exp1 + " " + c2 + " " + exp2); + Polynomial snd = new Polynomial(c1 + " " + exp2 + " " + c2 + " " + exp1); + Polynomial fst_2 = new Polynomial(fst); + Polynomial snd_2 = new Polynomial(snd); + fst.minus(snd); + snd_2.times(new Polynomial("-1 0")); + fst_2.plus(snd_2); + assertEquals(true, fst.equals(fst_2)); + } + + // Test associativity for some random polynomials + Polynomial a = new Polynomial("5 4 2 5 20 8 4 0"); + Polynomial b = new Polynomial("9 2 48 2 10 8 28 2"); + Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0"); + Polynomial a_bc = new Polynomial(a); + a_bc.minus(b); + a_bc.minus(c); + Polynomial a_cb = new Polynomial(a); + a_cb.minus(c); + a_cb.minus(b); + assertEquals(true, a_bc.equals(a_cb)); + } + + /** + * Test of times method, of class Polynomial. + */ + @Test + public void testTimes() { + System.out.println("times"); + + // Simple case + Polynomial instance = new Polynomial("7 1 6 3"); + instance.times(new Polynomial("5 3 4 2")); + assertEquals(new Polynomial("30 6 24 5 35 4 28 3"), instance); + + // Simple case with zero polynomial + instance = new Polynomial("5 3"); + instance.times(new Polynomial()); + assertEquals(new Polynomial(), instance); + + // Advanced case with coefficient 0 in the result + instance = new Polynomial("4 4 2 3"); // 4x^4 + 2x^3 + instance.times(new Polynomial("2 2 -4 3")); // 2x^2 - 4x^3 + assertEquals(new Polynomial("4 5 -16 7"), instance); // 4x^5 - 16x^7 (exponent 6 cancels out) + + // Test associativity for some random polynomials + Polynomial a = new Polynomial("5 4 2 5 20 8 4 0"); + Polynomial b = new Polynomial("9 2 48 2 10 8 28 2"); + Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0"); + Polynomial a_bc = new Polynomial(a); + a_bc.times(b); + a_bc.times(c); + Polynomial a_cb = new Polynomial(a); + a_cb.times(c); + a_cb.times(b); + assertEquals(true, a_bc.equals(a_cb)); + + // Test commutativity + Polynomial a_b = new Polynomial(a); + a_b.times(b); + Polynomial b_a = new Polynomial(b); + b_a.times(a); + assertEquals(true, a_b.equals(b_a)); + + // Test left distributivity + Polynomial b_plus_c = new Polynomial(b); + b_plus_c.plus(c); + Polynomial a_times_b_plus_c = new Polynomial(a); + a_times_b_plus_c.times(b_plus_c); + Polynomial a_times_c = new Polynomial(a); + a_times_c.times(c); + Polynomial a_times_b_plus_a_times_c = new Polynomial(a); + a_times_b_plus_a_times_c.times(b); + a_times_b_plus_a_times_c.plus(a_times_c); + assertEquals(true, a_times_b_plus_c.equals(a_times_b_plus_a_times_c)); + + // Test right distributivity + Polynomial b_plus_c_times_a = new Polynomial(b); + b_plus_c_times_a.plus(c); + b_plus_c_times_a.times(a); + Polynomial c_times_a = new Polynomial(c); + c_times_a.times(a); + Polynomial b_times_a_plus_c_times_a = new Polynomial(b); + b_times_a_plus_c_times_a.times(a); + b_times_a_plus_c_times_a.plus(c_times_a); + assertEquals(true, b_plus_c_times_a.equals(b_times_a_plus_c_times_a)); + } + + /** + * Test of divide method, of class Polynomial. + */ + @Test + public void testDivide() { + System.out.println("divide"); + Polynomial b = null; + Polynomial instance = new Polynomial(); + instance.divide(b); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of equals method, of class Polynomial. + */ + @Test + public void testEquals() { + System.out.println("equals"); + // Order of terms irrelevant + assertEquals(true, new Polynomial("4 2 3 5").equals(new Polynomial("3 5 4 2"))); + // Terms with same exponent are added + assertEquals(true, new Polynomial("5 5 10 5").equals(new Polynomial("15 5"))); + // Simple coefficient 0 removed + assertEquals(true, new Polynomial("0 7").equals(new Polynomial())); + // Advanced coefficient 0 removed + assertEquals(true, new Polynomial("5 10 -5 10").equals(new Polynomial())); + // Difference in exponent matters + assertEquals(false, new Polynomial("10 0").equals(new Polynomial("10 1"))); + // Difference in coefficient matters + assertEquals(false, new Polynomial("10 0").equals(new Polynomial("5 0"))); + } + + /** + * Test of plus method, of class Polynomial. + */ + @Test + public void testPlus_Polynomial() { + System.out.println("plus"); + // Test cancelling out and exponents that don't exist in either of the two polynomials all at once + Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0"); // 7x^4 - 4x^2 + 7x + 5 + instance.plus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10 + assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance); // 7x^4 + 5x^3 + 10x + 15 + + // Test associativity for some random polynomials + Polynomial a = new Polynomial("5 4 2 5 20 8 4 0"); + Polynomial b = new Polynomial("9 2 7 5 10 8 28 2"); + Polynomial c = new Polynomial("3 8 4 20 8 2 4 5 0 0"); + Polynomial a_bc = new Polynomial(a); + a_bc.plus(b); + a_bc.plus(c); + Polynomial a_cb = new Polynomial(a); + a_cb.plus(c); + a_cb.plus(b); + assertEquals(true, a_bc.equals(a_cb)); + + // Test commutativity + Polynomial a_b = new Polynomial(a); + a_b.plus(b); + Polynomial b_a = new Polynomial(b); + b_a.plus(a); + assertEquals(true, a_b.equals(b_a)); + } + + /** + * Test of apply method, of class Polynomial. + */ + @Test + public void testApply() { + System.out.println("apply"); + // Test zero coefficient, floating point coefficient and negative coefficient all at once + Polynomial instance = new Polynomial("0 7 5 4 0.5 3 9 2 -8 0"); + assertEquals(422654.5, instance.apply(17.0), 0.0); + } + +} -- cgit v1.2.3