diff options
author | Camil Staps | 2015-03-17 18:45:26 +0100 |
---|---|---|
committer | Camil Staps | 2015-03-17 18:45:26 +0100 |
commit | 4e7d3754b69f6af8f84622a17d28103ce70021d1 (patch) | |
tree | 0692608903feb6111eafe8a8583795c7b82ae847 | |
parent | Week 7 framework + startzip (diff) |
Implementation with limited tests; no javadoc yet
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Week7/manifest.mf | 3 | ||||
-rw-r--r-- | Week7/nbproject/private/private.xml | 6 | ||||
-rw-r--r-- | Week7/nbproject/project.properties | 7 | ||||
-rw-r--r-- | Week7/src/polynomial/Polynomial.java | 62 | ||||
-rw-r--r-- | Week7/src/polynomial/Term.java | 4 | ||||
-rw-r--r-- | Week7/test/polynomial/PolynomialTest.java | 145 |
7 files changed, 218 insertions, 12 deletions
@@ -2,4 +2,5 @@ /Week4/build/ /Week5/nbproject/private/ /Week6/nbproject/private/ -/Week6/build/
\ No newline at end of file +/Week6/build/ +/Week7/build/
\ No newline at end of file diff --git a/Week7/manifest.mf b/Week7/manifest.mf deleted file mode 100644 index 328e8e5..0000000 --- a/Week7/manifest.mf +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -X-COMMENT: Main-Class will be added automatically by build - diff --git a/Week7/nbproject/private/private.xml b/Week7/nbproject/private/private.xml new file mode 100644 index 0000000..2eee429 --- /dev/null +++ b/Week7/nbproject/private/private.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project-private xmlns="http://www.netbeans.org/ns/project-private/1"> + <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> + <group/> + </open-files> +</project-private> diff --git a/Week7/nbproject/project.properties b/Week7/nbproject/project.properties index d03e3e3..2a8bf83 100644 --- a/Week7/nbproject/project.properties +++ b/Week7/nbproject/project.properties @@ -39,7 +39,8 @@ javac.source=1.7 javac.target=1.7 javac.test.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${build.classes.dir}:\ + ${libs.junit_4.classpath} javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= @@ -53,10 +54,8 @@ javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= -main.class= -manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF -mkdist.disabled=false +mkdist.disabled=true platform.active=default_platform run.classpath=\ ${javac.classpath}:\ diff --git a/Week7/src/polynomial/Polynomial.java b/Week7/src/polynomial/Polynomial.java index 3b327f4..2a1b28e 100644 --- a/Week7/src/polynomial/Polynomial.java +++ b/Week7/src/polynomial/Polynomial.java @@ -1,6 +1,7 @@ package polynomial; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Scanner; @@ -61,18 +62,47 @@ public class Polynomial { */ @Override public String toString() { - return null; + 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(); } public void plus(Polynomial b) { + for (Term t : b.terms) + plus(t); + } + + public void plus(Term t) { + 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(t); } - public void minus(Polynomial b) { + b.times(new Polynomial("-1 0")); + plus(b); } - public void times(Polynomial b) { + Polynomial result = new Polynomial(); + for (Term that_t : b.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; } public void divide(Polynomial b) { @@ -80,7 +110,31 @@ public class Polynomial { @Override public boolean equals(Object other_poly) { - return false; + Polynomial that = (Polynomial) other_poly; + 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; + } + + 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 index 67bcf04..b8d9f16 100644 --- a/Week7/src/polynomial/Term.java +++ b/Week7/src/polynomial/Term.java @@ -105,6 +105,10 @@ public class Term { && coefficient == term.coefficient; } } + + public double apply(double x) { + return coefficient * Math.pow(x, exponent); + } /** * A static method for converting scanner input into a term. The expected diff --git a/Week7/test/polynomial/PolynomialTest.java b/Week7/test/polynomial/PolynomialTest.java new file mode 100644 index 0000000..dc31900 --- /dev/null +++ b/Week7/test/polynomial/PolynomialTest.java @@ -0,0 +1,145 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package polynomial; + +import java.util.Scanner; +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.*; + +/** + * + * @author camilstaps + */ +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"); + Polynomial instance = new Polynomial("3 5 -4 1 2.5 0 -3.4 -7"); // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients + String expResult = "3.000000x^5-4.000000x+2.500000-3.400000x^-7"; + String result = instance.toString(); + assertEquals(expResult, result); + } + + /** + * Test of minus method, of class Polynomial. + */ + @Test + public void testMinus() { + System.out.println("minus"); + Polynomial b = new Polynomial("5 3 4 2 3 1 10 0"); // 5x^3 + 4x^2 + 3x + 10 + Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0"); // 7x^4 - 4x^2 + 3x + 5 + instance.minus(b); + assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance); // 7x^4 - 5x^3 - 8x^2 - 5 + } + + /** + * Test of times method, of class Polynomial. + */ + @Test + public void testTimes() { + System.out.println("times"); + Polynomial b = new Polynomial("5 3 4 2"); + Polynomial instance = new Polynomial("7 1 6 3"); + instance.times(b); + assertEquals(new Polynomial("30 6 24 5 35 4 28 3"), instance); + } + + /** + * 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"); + Object other_poly = new Polynomial("3 5 4 2"); + Polynomial instance = new Polynomial("4 2 3 5"); + boolean expResult = true; + boolean result = instance.equals(other_poly); + assertEquals(expResult, result); + } + + /** + * Test of plus method, of class Polynomial. + */ + @Test + public void testPlus_Polynomial() { + System.out.println("plus"); + Polynomial b = new Polynomial("5 3 4 2 3 1 10 0"); // 5x^3 + 4x^2 + 3x + 10 + Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0"); // 7x^4 - 4x^2 + 7x + 5 + instance.plus(b); + assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance); // 7x^4 + 5x^3 + 10x + 15 + } + + /** + * Test of plus method, of class Polynomial. + */ + @Test + public void testPlus_Term() { + System.out.println("plus"); + Term t = Term.scanTerm(new Scanner("5 3")); + + Polynomial instance = new Polynomial("4 7 2 3 1 0"); // normal addition + instance.plus(t); + assertEquals(new Polynomial("4 7 7 3 1 0"), instance); + + instance = new Polynomial("4 7 -5 3 1 0"); // addition when the term cancels out + instance.plus(t); + assertEquals(new Polynomial("4 7 1 0"), instance); + } + + /** + * Test of apply method, of class Polynomial. + */ + @Test + public void testApply() { + System.out.println("apply"); + double x = 17.0; + Polynomial instance = new Polynomial("5 4 0.5 3 9 2 8 0"); + double expResult = 422670.5; + double result = instance.apply(x); + assertEquals(expResult, result, 0.0); + } + +} |