From cd78b40930a2aa6086e37b863fbd9d5d0cd7ee23 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sun, 16 Jul 2023 22:33:48 +0200 Subject: [PATCH] Awarding points for calculation polynomial values --- .../mi/sd1/task2/SquarePolynom.java | 45 ++- .../mi/sd1/ShowReachedPoints.java | 4 +- .../mi/sd1/task2/TestSquarePolynom.java | 302 ++++++++++++++++++ .../mi/sd1/task2/SquarePolynom.java | 183 +---------- .../mi/sd1/task2/TestSquarePolynom.java | 107 +++++-- 5 files changed, 441 insertions(+), 200 deletions(-) create mode 100644 Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java diff --git a/Klausuren/Sd1/2021winter/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java b/Klausuren/Sd1/2021winter/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java index 07a7379d9..0368a93d3 100644 --- a/Klausuren/Sd1/2021winter/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java +++ b/Klausuren/Sd1/2021winter/Exam/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java @@ -7,19 +7,47 @@ package de.hdm_stuttgart.mi.sd1.task2; * \(a\), \(b\) and \(c\). We assume all three to be of type <code>int</code>.</p> * * - * <h2>Providing zeroes (German: "Nullstellen") of square polynomials, - * case \( \mathbf{a \neq 0}\), true square polynomial</h2> + * <h2>Calculating polynomial values.</h2> + * + * <table class="goikTableDefaults"> + * <caption>Sample code calculating polynomial values</caption> + * <tbody> + * <tr> + * <th>Code</th> + * <th>Result</th> + * </tr> + * <tr> + * <td> + * <pre><code class="language-java"> final SquarePolynom poly = // Representing + * new SquarePolynom(-1, 2, 1); // p(x) = -x² + 2x + 1 + * + * double y = poly.get(3.0); // -1 * 3.0 * 3.0 + 2 * 3.0 + 1 + * + * System.out.println(y);</code></pre> + * </td> + * <td> + * <pre><code class="nohighlight">-2</code></pre> + * </td> + * </tr> + * </tbody> + * </table> + * + * <h2>Providing zeroes (German: "Nullstellen") of square polynomials.</h2> * * <p>Zeroes are being calculated using:</p> * - * <p>\[ x_{1,2} = {{-b \pm \sqrt{b^2 - 4 ac}} \over {2a}} \]</p> + * \[ x_{1,2} = {{-b \pm \sqrt{b^2 - 4 ac}} \over {2a}} \] * - * <p>There are thus three cases:</p> + * <p>There are two main cases \( \mathbf{a \neq 0}\) and \( \mathbf{a = 0}\):</p> + * + * <h3>Case \( \mathbf{a \neq 0}\), true square polynomial</h3> + * + * <p>There are three sub cases:</p> * * <dl> * <dt>\(\mathbf{ 0 < b^2 - 4 ac}\):</dt> * <dd> - * <p>Two zeroes \(x_1\) and \(x_2\) according to above formular:</p> + * <p>Two zeroes \(x_1\) and \(x_2\) according to the above formular:</p> * * <table class="goikTableDefaults"> * <caption>Sample code illustrating zero values calculation</caption> @@ -49,7 +77,8 @@ package de.hdm_stuttgart.mi.sd1.task2; * </tbody> * </table> * - * <p>Note the two values in the <code>double[] zeroes</code> array being ordered by value.</p> + * <p>Note the two values in the array being ordered by value like <code>double[] zeroes = {-1.25, 2.0}</code> + * rather than <code>... {2.0, -1.25}</code></p> * </dd> * * <dt>\(\mathbf{ 0 = b^2 - 4 ac}\):</dt> @@ -121,9 +150,9 @@ package de.hdm_stuttgart.mi.sd1.task2; * </dd> * </dl> * - * <h2>Case \( \mathbf{a = 0}\), linear polynomial</h2> + * <h3>Case \( \mathbf{a = 0}\), linear polynomial</h3> * - * <p>This leaves us with just a linear polynomial \(bx + c\) also resulting in three different cases:</p> + * <p>This leaves us with just a linear polynomial \(bx + c\) also resulting in three different sub cases:</p> * * * <dl> diff --git a/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/ShowReachedPoints.java b/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/ShowReachedPoints.java index b86901bc1..e17743839 100644 --- a/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/ShowReachedPoints.java +++ b/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/ShowReachedPoints.java @@ -3,7 +3,7 @@ package de.hdm_stuttgart.mi.sd1; import de.hdm_stuttgart.mi.exam.unitmarking.RunTests; import de.hdm_stuttgart.mi.sd1.task1.*; -import de.hdm_stuttgart.mi.sd1.task2.Test_SquarePolynom; +import de.hdm_stuttgart.mi.sd1.task2.TestSquarePolynom; public class ShowReachedPoints { @@ -17,6 +17,6 @@ public class ShowReachedPoints { "Task 1" , A_TrafficLightTest.class, B_StringHelperTest.class, C_ArrayHelperTest.class, D_TextFrameTest.class ); - RunTests.exec("Task 2", Test_SquarePolynom.class); + RunTests.exec("Task 2", TestSquarePolynom.class); } } \ No newline at end of file diff --git a/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java b/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java new file mode 100644 index 000000000..2909bfeb5 --- /dev/null +++ b/Klausuren/Sd1/2021winter/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java @@ -0,0 +1,302 @@ +package de.hdm_stuttgart.mi.sd1.task2; + +import de.hdm_stuttgart.mi.exam.unitmarking.ExaminationTestDefaults; +import de.hdm_stuttgart.mi.exam.unitmarking.Marking; +import de.hdm_stuttgart.mi.sd1.ignore_me.ObjectWrapper; +import org.junit.Assert; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +@FixMethodOrder(MethodSorters.NAME_ASCENDING) + +public class TestSquarePolynom extends ExaminationTestDefaults { + + @Test + @Marking(points = 5) + public void test_100_constructor_getValue() { + + assertPolynomValue(0, 0, 3, 20.5); + assertPolynomValue(0, 0, 3, -10.); + assertPolynomValue(0, 0, 3, 75.); + + assertPolynomValue(-1, 3, 5, 2.); + assertPolynomValue(4, -4, 10, -3); + + assertPolynomValue(-1, 2, 1, 1); + } + + @Test + @Marking(points = 5) + public void test_200_set_getValue() { + + assertPolynomValue(0, 0, -8, -10); + assertPolynomValue(0, 0, -8, 100.); + assertPolynomValue(0, 0, -8, -103.3); + { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, 0, 0, -8); + poly.invoke(void.class, "setA", 7); + poly.invoke(void.class, "setB", -1); + final double expected = 2.5 * (7 * 2.5 - 1) - 8; + final String msg = composeDescription(7, -1, -8).append(" should return " + expected).toString(); + Assert.assertEquals(msg, expected, poly.invoke(double.class, "get", 2.5), delta); + } + } + + @Test + @Marking(points = 4) + public void test_400_NoZero() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 1, 1); + assertNoZero(poly, composeDescription(1,2,1).toString()); + + final ObjectWrapper<SquarePolynom> poly2 = new ObjectWrapper<>(SquarePolynom.class,1, 2, 3); + assertNoZero(1, 6, 20, poly2); + assertNoZero(2, 13, 41, poly2); + } + + @Test + @Marking(points = 1) + public void test_500_OneZeroLinearOneZero() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 1, 1); + assertOneZero(poly, -1, composeDescription(0,1,1).toString()); + } + + @Test + @Marking(points = 1) + public void test_600_OneZeroLinearNoZero() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 0, 1); + assertNoZero(poly, composeDescription(0,0,1).toString()); + } + + @Test + @Marking(points = 1) + public void test_610_OneZero() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 2, 1); + assertOneZero(poly, -1, composeDescription(1,2,1).toString()); + + final ObjectWrapper<SquarePolynom> poly2 = new ObjectWrapper<>(SquarePolynom.class,1, 2, 3); + assertOneZero(6, -84, 294, poly2, 7); + assertOneZero(21, 126, 189, poly2, -3); + } + + @Test + @Marking(points = 1) + public void test_700_twoZeroes() { + + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 3, 2); + assertTwoZeroes(poly, -2, -1, "x² + 3x + 2"); + + assertTwoZeroes(3, 7, 4, poly, -4./3, -1); + + assertTwoZeroes(-2, 1, 3, poly, -1, 1.5); + + assertTwoZeroes(-1, 5, 6, poly, -1, 6); + + assertTwoZeroes(7, 11, 4, poly, -1, -4./7); + + assertTwoZeroes(4, -3, -10, poly, -1.25, 2); + + assertTwoZeroes(5, 6, 1, poly, -1, -0.2); + + } + + @Test + @Marking(points = 1) + public void test_800_Exception() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 0, 0); + + final Class<ArithmeticException> expectedArithmeticException = ArithmeticException.class; + + // Either of a, b or c must be non-zero + poly.invoke(double[].class, "getZeroes", expectedArithmeticException); + } + + @Test + @Marking(points = 1) + public void test_900_TwoZeroExzess() { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, + 46010, 598130,-1960026000); + assertTwoZeroes(poly,-213,200, + composeDescription(46010, 598130,-1960026000).toString()); + } + + // End of tests ------------------------------------------------------------------------------------------------- + // Test helper methods + // + static private final double delta = 1.E-12; + private static void assertPolynomValue(int a, int b, int c, double x) { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, a, b, c); + final double expected = x * (a * x + b) + c; + final String msg = composeDescription(a,b,c) + " for x == " + x + " should return " + expected; + Assert.assertEquals(msg, expected, poly.invoke(double.class, "get", x), delta); + } + + static private StringBuffer composeDescription(final int a, final int b, final int c) { + + final StringBuffer ret = new StringBuffer("p(x)="); + + boolean initialized = false; + + if (a != 0) { + if (-1 == a) { + ret.append("-"); + } else if (1 != a) { + ret.append(a); + } + ret.append("x²"); + initialized = true; + } + + if (0 != b) { + if (initialized) { + if (0 < b) { + ret.append("+"); + } + } else { + initialized = true; + } + if (-1 == b) { + ret.append("-"); + } else if (1 != b) { + ret.append(b); + } + ret.append("x"); + } + + if (0 != c) { + if (initialized) { + if (0 < c) { + ret.append("+"); + } + } + ret.append(c); + } + return ret; + } + + /** + * Testing given polynomial for no real zero value. + * + * @param poly Polynomial to be tested + * @param description E.g. "-2x² + 4x -3" + */ + static private void assertNoZero(final ObjectWrapper<SquarePolynom> poly, final String description) { + + Assert.assertEquals("No zero expected for polynom »" + description + "«", + 0, poly.invoke(double[].class, "getZeroes").length); + } + + /** + * Testing for no real valued zero solution. + * + * @param a Square coefficient + * @param b Linear coefficient + * @param c Constant coefficient + * @param poly Square polynomial + */ + static private void assertNoZero(final int a, + final int b, + final int c, + final ObjectWrapper<SquarePolynom> poly) { + poly.invoke(void.class, "setA", a); + poly.invoke(void.class, "setB", b); + poly.invoke(void.class, "setC", c); + Assert.assertEquals("One zero expected for polynom »" + composeDescription(a, b, c) + "«", + 0, poly.invoke(double[].class, "getZeroes").length); + + + } + + /** + * Testing given polynomial for one zeroes to be returned + * + * @param poly Polynomial to be tested + * @param expected zero value + * @param description E.g. "-2x² + 4x -3" + */ + static private void assertOneZero(final ObjectWrapper<SquarePolynom> poly, + double expected, + final String description) { + final double[] result = poly.invoke(double[].class, "getZeroes"); + + Assert.assertEquals("One zero expected for polynom »" + description + "«", + 1, result.length); + Assert.assertEquals("Expected zero of »" + description +"« to be " + expected, expected, result[0], delta); + } + + /** + * Like {@link #assertOneZero(ObjectWrapper, double, String)} but re-setting coefficients beforehand, + * @param a Square coefficient + * @param b Linear coefficient + * @param c Constant coefficient + * @param poly Square polynomial + */ + static private void assertOneZero(final int a, + final int b, + final int c, + final ObjectWrapper<SquarePolynom> poly, + double expected) { + poly.invoke(void.class, "setA", a); + poly.invoke(void.class, "setB", b); + poly.invoke(void.class, "setC", c); + final double[] result = poly.invoke(double[].class, "getZeroes"); + + final StringBuffer polynomDescription = composeDescription(a, b, c); + + Assert.assertEquals(2 + " zeroes expected for polynom »" + polynomDescription + "«", + 1, result.length); + Assert.assertEquals("Expected zero of »" + polynomDescription +"« to be " + expected, + expected, result[0], delta); + } + + /** + * Testing given polynomial for two zeroes to be returned + * + * @param poly Polynomial to be tested + * @param expectedLower Lower zero value + * @param expectedUpper Upper zero value + * @param description E.g. "-2x² + 4x -3" + */ + static private void assertTwoZeroes(final ObjectWrapper<SquarePolynom> poly, + double expectedLower, + double expectedUpper, + final String description) { + final double[] result = poly.invoke(double[].class, "getZeroes"); + + Assert.assertEquals(2 + " zeroes expected for polynom »" + description + "«", + 2, result.length); + Assert.assertEquals("Expected lower zero of »" + description +"« to be " + expectedLower, + expectedLower, result[0], delta); + Assert.assertEquals("Expected upper zero of »" + description +"« to be " + expectedUpper, + expectedUpper, result[1], delta); + } + + /** + * Like {@link #assertTwoZeroes(ObjectWrapper, double, double, String)} but re-setting coeficients beforehand, + * @param a Square coefficient + * @param b Linear coefficient + * @param c Constant coefficient + * @param poly Square polynomial + * @param expectedLower Expected lower zero value + * @param expectedUpper Expected upper zero value + */ + static private void assertTwoZeroes(final int a, + final int b, + final int c, + final ObjectWrapper<SquarePolynom> poly, + double expectedLower, + double expectedUpper) { + poly.invoke(void.class, "setA", a); + poly.invoke(void.class, "setB", b); + poly.invoke(void.class, "setC", c); + final double[] result = poly.invoke(double[].class, "getZeroes"); + + final StringBuffer polynomDescription = composeDescription(a, b, c); + + Assert.assertEquals(2 + " zeroes expected for polynom »" + polynomDescription + "«", + 2, result.length); + Assert.assertEquals("Expected lower zero of »" + polynomDescription +"« to be " + expectedLower, + expectedLower, result[0], delta); + Assert.assertEquals("Expected upper zero of »" + polynomDescription +"« to be " + expectedUpper, + expectedUpper, result[1], delta); + } +} diff --git a/Klausuren/Sd1/2021winter/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java b/Klausuren/Sd1/2021winter/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java index ec0c8b829..519f934e3 100644 --- a/Klausuren/Sd1/2021winter/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java +++ b/Klausuren/Sd1/2021winter/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task2/SquarePolynom.java @@ -3,7 +3,7 @@ package de.hdm_stuttgart.mi.sd1.task2; /** * <p>Representing square polynomials having coefficients of type <code>int</code>.</p> * - * <p>A square polynomial \( p(x) = a x² + b x + c \) is being defined by its three coefficients + * <p>A square polynomial \( p(x) = a x^2 + b x + c \) is being defined by its three coefficients * \(a\), \(b\) and \(c\). We assume all three to be of type <code>int</code>.</p> * */ @@ -11,7 +11,7 @@ public class SquarePolynom { private int a, b, c; /** - * <p>Defining a polynomial \( p(x) = a x² + b x + c \).</p> + * <p>Defining a polynomial \( p(x) = a x^2 + b x + c \).</p> * * @param a Square coefficient. * @param b Linear coefficient. @@ -23,6 +23,16 @@ public class SquarePolynom { setC(c); } + /** + * <p>The polynomial's value for a given argument of x.</p> + * + * @param x The argument i.e. x = 3.0 + * @return Returning e.g. \( a \cdot 3.0^2 + b \cdot 3.0 + c\) for \( x = 3.0 \). + */ + public double get(double x) { + return x * (a * x + b) + c; // Horner's scheme + } + /** * <p>Re- setting the square coefficient in \( a x² \).</p> * @@ -51,174 +61,11 @@ public class SquarePolynom { } /** - * <p>Providing zeroes (German: "Nullstellen") of the given square polynomial.</p> - * - * <p>There are two different cases \( \mathbf{a \neq 0}\) and \( \mathbf{a = 0}\):</p> - * - * <h2>Case \( \mathbf{a \neq 0}\), true square polynomial</h2> - * - * <p>Zeroes are being calculated using:</p> - * - * <p>\[ x_{1,2} = {{-b \pm \sqrt{b^2 - 4 ac}} \over {2a}} \]</p> - * - * <p>There are thus three cases:</p> - * - * <dl> - * <dt>\(\mathbf{ 0 < b^2 - 4 ac}\):</dt> - * <dd> - * <p>Two zeroes \(x_1\) and \(x_2\) according to above formular:</p> - * - * <table class="goikTableDefaults"> - * <caption>Sample code illustrating zero values calculation</caption> - * <tbody> - * <tr> - * <th>Code</th> - * <th>Result</th> - * </tr> - * <tr> - * <td> - * <pre><code class="language-java"> final SquarePolynom poly = // Representing - * new SquarePolynom(4, -3, -10); // p(x) = 4x² - 3x - 10 - * - * double[] zeroes = poly.getZeroes(); - * - * System.out.println("Found " + zeroes.length + " zeroes:"); - * - * System.out.println("x_1 = " + zeroes[0]); - * System.out.println("x_2 = " + zeroes[1]);</code></pre> - * </td> - * <td> - * <pre><code class="nohighlight"> Found 2 zeroes: - * x_1 = -1.25 - * x_2 = 2.0</code></pre> - * </td> - * </tr> - * </tbody> - * </table> - * - * <p>Note the two values in the <code>double[] zeroes</code> array being ordered by value.</p> - * </dd> - * - * <dt>\(\mathbf{ 0 = b^2 - 4 ac}\):</dt> - * - * <dd> - * <p>One zero \(x = {-b\over 2a}\).</p> - * - * <p>We allow for changing a polynomial's coefficients. Continuing from the above example we have:</p> - * - * <table class="goikTableDefaults"> - * <caption>Changing coefficients</caption> - * <tbody> - * <tr> - * <th>Code</th> - * <th>Result</th> - * </tr> - * <tr> - * <td> - * <pre><code class="java"> ... - * poly.setA(1); // Representing - * poly.setB(-8); // p(x) = x² -8x + 16 - * poly.setC(16); - * - * zeroes = poly.getZeroes(); - * System.out.println("Found " + zeroes.length + " zero:"); - * - * System.out.println("x_1 = " + zeroes[0]);</code></pre> - * </td> - * <td> - * <pre><code class="nohighlight"> ... - * Found 1 zero: - * x_1 = 4.0</code></pre> - * </td> - * </tr> - * </tbody> - * </table> - * <p> </p> - * </dd> - * - * <dt>\(\mathbf{ b^2 - 4 ac < 0 }\):</dt> - * - * <dd> - * <p>Square root of negative thus no real valued solution:</p> - * - * <table class="goikTableDefaults"> - * <caption>Changing coefficients again</caption> - * <tbody> - * <tr> - * <th>Code</th> - * <th>Result</th> - * </tr> - * <tr> - * <td> - * <pre><code class="java"> ... - * poly.setA(1); // Representing - * poly.setB(0); // p(x) = x² + 1 - * poly.setC(1); // (No real valued zero) - * - * zeroes = poly.getZeroes(); - * System.out.println("Found " + zeroes.length + " zero");</code></pre> - * </td> - * <td> - * <pre><code class="nohighlight"> ... - * Found 0 zero</code></pre> - * </td> - * </tr> - * </tbody> - * </table> - * </dd> - * </dl> - * - * <h2>Case \( \mathbf{a = 0}\), linear polynomial</h2> - * - * <p>This leaves us with just a linear polynomial \(bx + c\) also resulting in three different cases:</p> - * - * - * <dl> - * <dt>\( \mathbf{b \neq 0} :\)</dt> - * - * <dd> - * <p>Exactly one zero</p> - * </dd> - * - * <dt>\( \mathbf{b = 0 \ \text{and} \ c \neq 0 :}\)</dt> - * <dd> - * <p>No zero at all</p> - * </dd> - * - * <dt>\(\mathbf{b = 0 \ \text{and} \ c = 0} \):</dt> - * <dd> - * <p>An infinite number of zeroes. Since this cannot be represented by a finite <code>double[]</code> array - * an {@link ArithmeticException} is being thrown like in the subsequent code sample:</p> - * - * <table class="goikTableDefaults"> - * <caption>Dealing with \( \mathbf{a = b = c = 0} \)</caption> - * <tbody> - * <tr> - * <th>Code</th> - * <th>Result</th> - * </tr> - * <tr> - * <td> - * <pre><code class="java"> ... - * poly.setA(0); - * poly.setB(0); - * poly.setC(0); - * zeroes = poly.getZeroes();</code></pre> - * </td> - * <td> - * <pre><code class="nohighlight"> ... - * Exception in thread "main" java.lang.ArithmeticException: - * <b style="color:red;">Polynomial is identical to zero function</b></code></pre> - * </td> - * </tr> - * </tbody> - * </table> + * <p>Providing zeroes (German: "Nullstellen") of a given square polynomial.</p> * - * </dd> - * </dl> + * @return The ordered finite set of zeroes. In case of an infinite number of zeroes an {@link ArithmeticException} + * is being thrown. * - * @return The finite set of zeroes. In case of an infinite number of zeroes an {@link ArithmeticException} is being - * thrown. * @throws ArithmeticException In case of \(a = b = c = 0 \) an infinite number of zeroes exists which cannot be * represented by a finite array of double values. */ diff --git a/Klausuren/Sd1/2021winter/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java b/Klausuren/Sd1/2021winter/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java index 5f6de7b47..8c35e0519 100644 --- a/Klausuren/Sd1/2021winter/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java +++ b/Klausuren/Sd1/2021winter/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task2/TestSquarePolynom.java @@ -13,7 +13,38 @@ public class TestSquarePolynom extends ExaminationTestDefaults { @Test @Marking(points = 5) - public void test_100_NoZero() { + public void test_100_constructor_getValue() { + + assertPolynomValue(0, 0, 3, 20.5); + assertPolynomValue(0, 0, 3, -10.); + assertPolynomValue(0, 0, 3, 75.); + + assertPolynomValue(-1, 3, 5, 2.); + assertPolynomValue(4, -4, 10, -3); + + assertPolynomValue(-1, 2, 1, 1); + } + + @Test + @Marking(points = 5) + public void test_200_set_getValue() { + + assertPolynomValue(0, 0, -8, -10); + assertPolynomValue(0, 0, -8, 100.); + assertPolynomValue(0, 0, -8, -103.3); + { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, 0, 0, -8); + poly.invoke(void.class, "setA", 7); + poly.invoke(void.class, "setB", -1); + final double expected = 2.5 * (7 * 2.5 - 1) - 8; + final String msg = composeDescription(7, -1, -8).append(" should return " + expected).toString(); + Assert.assertEquals(msg, expected, poly.invoke(double.class, "get", 2.5), delta); + } + } + + @Test + @Marking(points = 4) + public void test_400_NoZero() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 1, 1); assertNoZero(poly, composeDescription(1,2,1).toString()); @@ -24,21 +55,21 @@ public class TestSquarePolynom extends ExaminationTestDefaults { @Test @Marking(points = 1) - public void test_200_OneZeroLinearOneZero() { + public void test_500_OneZeroLinearOneZero() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 1, 1); assertOneZero(poly, -1, composeDescription(0,1,1).toString()); } @Test @Marking(points = 1) - public void test_200_OneZeroLinearNoZero() { + public void test_600_OneZeroLinearNoZero() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 0, 1); assertNoZero(poly, composeDescription(0,0,1).toString()); } @Test - @Marking(points = 3) - public void test_210_OneZero() { + @Marking(points = 1) + public void test_610_OneZero() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 2, 1); assertOneZero(poly, -1, composeDescription(1,2,1).toString()); @@ -48,8 +79,8 @@ public class TestSquarePolynom extends ExaminationTestDefaults { } @Test - @Marking(points = 5) - public void test_300_twoZeroes() { + @Marking(points = 1) + public void test_700_twoZeroes() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,1, 3, 2); assertTwoZeroes(poly, -2, -1, "x² + 3x + 2"); @@ -69,8 +100,8 @@ public class TestSquarePolynom extends ExaminationTestDefaults { } @Test - @Marking(points = 2) - public void test_400_Exception() { + @Marking(points = 1) + public void test_800_Exception() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class,0, 0, 0); final Class<ArithmeticException> expectedArithmeticException = ArithmeticException.class; @@ -80,8 +111,8 @@ public class TestSquarePolynom extends ExaminationTestDefaults { } @Test - @Marking(points = 3) - public void test_500_TwoZeroExzess() { + @Marking(points = 1) + public void test_900_TwoZeroExzess() { final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, 46010, 598130,-1960026000); assertTwoZeroes(poly,-213,200, @@ -92,20 +123,52 @@ public class TestSquarePolynom extends ExaminationTestDefaults { // Test helper methods // static private final double delta = 1.E-12; + private static void assertPolynomValue(int a, int b, int c, double x) { + final ObjectWrapper<SquarePolynom> poly = new ObjectWrapper<>(SquarePolynom.class, a, b, c); + final double expected = x * (a * x + b) + c; + final String msg = composeDescription(a,b,c) + " for x == " + x + " should return " + expected; + Assert.assertEquals(msg, expected, poly.invoke(double.class, "get", x), delta); + } static private StringBuffer composeDescription(final int a, final int b, final int c) { - final StringBuffer ret = new StringBuffer(); - ret.append(a).append("x²"); - if (0 < b) { - ret.append(" + ").append(b).append("x"); - } else if (b < 0) { - ret.append(" ").append(b).append("x"); + + final StringBuffer ret = new StringBuffer("p(x)="); + + boolean initialized = false; + + if (a != 0) { + if (-1 == a) { + ret.append("-"); + } else if (1 != a) { + ret.append(a); + } + ret.append("x²"); + initialized = true; } - // Slight code duplication avoiding a method definition. Yepp: I'm lazy sometimes! - if (0 < c) { - ret.append(" + ").append(c); - } else if (c < 0) { - ret.append(" ").append(c); + + if (0 != b) { + if (initialized) { + if (0 < b) { + ret.append("+"); + } + } else { + initialized = true; + } + if (-1 == b) { + ret.append("-"); + } else if (1 != b) { + ret.append(b); + } + ret.append("x"); + } + + if (0 != c) { + if (initialized) { + if (0 < c) { + ret.append("+"); + } + } + ret.append(c); } return ret; } -- GitLab