diff --git a/Sd1/P/Gcd/V1/pom.xml b/Sd1/P/Gcd/V1/pom.xml index 08d0936d0ed9304ec225c2fb75889e321ca89de9..45af7a6a996d8b7de8bc5432fbca2d20dc4145a7 100644 --- a/Sd1/P/Gcd/V1/pom.xml +++ b/Sd1/P/Gcd/V1/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Driver.java b/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Driver.java index 30f09ea62a8c161a163d48a0bc99b47678beae69..d1fbb223a3b6481a0a7129494aff4de145585719 100644 --- a/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Driver.java +++ b/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Driver.java @@ -9,6 +9,6 @@ public class Driver { * @param args unused */ public static void main(String[] args) { - System.out.println("GCD(12, 18) = " + Math.getGcd(12, 18)); + System.out.println("GCD(49, -21) = " + Math.getGcd(49, -21)); } } diff --git a/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Math.java b/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Math.java index 25f706d51886d53d015e2a10adc9d92ea32064d8..44e885a4cfcf3fbafc231bb585ea90c86d93adf6 100644 --- a/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Math.java +++ b/Sd1/P/Gcd/V1/src/main/java/de/hdm_stuttgart/mi/sd1/gcd/Math.java @@ -18,19 +18,31 @@ public class Math { * @return The GCD of a and b */ public static long getGcd(long a, long b) { - - // Following http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html - if (a < b) { // Swap the values of a and b - long tmp = a; - a = b; - b = tmp; - } - while (0 != b) { - long r = a % b; - a = b; - b = r; + if (a == 0 && b == 0) { + return 1; + } else { + a = java.lang.Math.abs(a); // More efficient than if (a < 0) {a = -a;} + b = java.lang.Math.abs(b); + if (a == 0) { // b != 0, see condition before + return b; + } else if (b == 0) { + return a; + } else { + // Now we actually implement Euclid's algorithm by following + // http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html + + if (a < b) { // Swap the values of a and b if required + final long tmp = a; // Save a's value temporarily + a = b; + b = tmp; + } + while (0 != b) { + long r = a % b; + a = b; + b = r; + } + return a; + } } - return a; } - } diff --git a/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java b/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java deleted file mode 100644 index 9720dba420ea57fa7e790bc711fe4291589ce8c8..0000000000000000000000000000000000000000 --- a/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package de.hdm_stuttgart.mi.sd1.fraction; - -import de.hdm_stuttgart.mi.sd1.gcd.Math; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for Gcd. - */ -public class FractionTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing GCD computation"); - testsuite.addTestSuite(FractionTest.class); - return testsuite; } - - /** - * - */ - public void testApp() { - - assertEquals(Math.getGcd(18, 12), 6); - assertEquals(Math.getGcd(56, 21), 7); - - } -} diff --git a/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/GcdTest.java b/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/GcdTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2014e5ca42083ae526ad31d71d0d09657b04a7a6 --- /dev/null +++ b/Sd1/P/Gcd/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/GcdTest.java @@ -0,0 +1,46 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.gcd.Math; + +public class GcdTest { + + @Test + public void testTwoPositiveIntegers() { + assertEquals(6, Math.getGcd(18, 12)); + } + + @Test + public void testReverseOrder() { + assertEquals(6, Math.getGcd(12, 18)); + } + + @Test + public void testBothZero() { + assertEquals(1, Math.getGcd(0, 0)); + } + + @Test + public void testNonzeroZero() { + assertEquals(1, Math.getGcd(0, 0)); + } + @Test + public void testZeroNonzero() { + assertEquals(7, Math.getGcd(0, 7)); + } + + @Test + public void testNegativePositive() { + assertEquals(9, Math.getGcd(-36, 27)); + } + + @Test + public void testPositiveNegative() { + assertEquals(7, Math.getGcd(49, -21)); + } + + +} diff --git a/Sd1/P/crab/V1/pom.xml b/Sd1/P/crab/V1/pom.xml index 1a597a0352ff7c8b77fcf0e71acb559e40389cb3..8b0bd43b1066e8b9675607aa8da7f9443dbeb13e 100644 --- a/Sd1/P/crab/V1/pom.xml +++ b/Sd1/P/crab/V1/pom.xml @@ -65,7 +65,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/crab/V2/pom.xml b/Sd1/P/crab/V2/pom.xml index 007787c2e83b3ab60f0e46877f1cc79e4ec69a43..0d632f5a4bc3aec6175d710992f7cc9fc0fb1f46 100644 --- a/Sd1/P/crab/V2/pom.xml +++ b/Sd1/P/crab/V2/pom.xml @@ -65,7 +65,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/fraction/V1/pom.xml b/Sd1/P/fraction/V1/pom.xml index ed320843959f3087d2f90de63b762337923aabd3..de7d235b1d036fa9663e066d7eb6be51dabc894c 100644 --- a/Sd1/P/fraction/V1/pom.xml +++ b/Sd1/P/fraction/V1/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/fraction/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java b/Sd1/P/fraction/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java index 103492ba8b2752b30d2434f0d6fac3b5c47e8e0a..7955fc4bc186b41e62e27a2e6cd373658e1649dc 100644 --- a/Sd1/P/fraction/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java +++ b/Sd1/P/fraction/V1/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java @@ -1,25 +1,15 @@ package de.hdm_stuttgart.mi.sd1.fraction; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; -/** - * Unit test for simple App. - */ -public class FractionTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing Fractions (sum + product)"); - testsuite.addTestSuite(FractionTest.class); - return testsuite; } +import org.junit.Test; +public class FractionTest { /** * Sums and products of fractions */ - public void testApp() { + @Test + public void testAdditionMultiply() { // Input final Fraction twoThird = new Fraction(2, 3), // Construct a fraction object (2/3) diff --git a/Sd1/P/fraction/V2/pom.xml b/Sd1/P/fraction/V2/pom.xml index 2d1c51cf093099ec0ca8c721bd50ca4d1ad8e4a7..2922241e9c50d9e0b0b9a781361bc38dff013d8f 100644 --- a/Sd1/P/fraction/V2/pom.xml +++ b/Sd1/P/fraction/V2/pom.xml @@ -68,7 +68,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/fraction/V2/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java b/Sd1/P/fraction/V2/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java index 84c4c9edca059028262c081b69faf2d3c580a311..7ae7865d501626a1fceda3949063c6609701c28a 100644 --- a/Sd1/P/fraction/V2/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java +++ b/Sd1/P/fraction/V2/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/FractionTest.java @@ -1,44 +1,43 @@ package de.hdm_stuttgart.mi.sd1.fraction; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; -/** - * Unit test for simple App. - */ -public class FractionTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing Fractions (sum + product)"); - testsuite.addTestSuite(FractionTest.class); - return testsuite; } +import org.junit.Test; + +public class FractionTest { /** * Sums and products of fractions */ - public void testApp() { + @Test + public void testAddMultiplyReduce() { + + // Just constructor + final Fraction twoThirds = new Fraction(4, 6); + assertEquals(2, twoThirds.getNumerator()); + assertEquals(3, twoThirds.getDenominator()); // Input final Fraction - twoThird = new Fraction(2, 3), // Construct a fraction object (2/3) - threeSeven = new Fraction(3, 7); // Construct a fraction object (3/7) + twoThird = new Fraction(5, 6), // Construct a fraction object (2/3) + threeSeven = new Fraction(2, 9); // Construct a fraction object (3/7) // Computed results final Fraction - sum = twoThird.add(threeSeven), // (2/3) + (3/7) + sum = twoThird.add(threeSeven), // (5/6) + (2/9) product = twoThird.mult(threeSeven); // (2/3) * (3/7) - assertTrue(sum.getNumerator() == 23 && sum.getDenominator() == 21); - assertTrue(product.getNumerator() == 2 && product.getDenominator() == 7); + assertTrue(sum.getNumerator() == 19 && sum.getDenominator() == 18); + assertTrue(product.getNumerator() == 5 && product.getDenominator() == 27); final Fraction sum2 = new Fraction(5, 18).add(new Fraction(7, 12)); assertTrue(sum2.getNumerator() == 31 && sum2.getDenominator() == 36); final Fraction product2 = new Fraction(5, 18).mult(new Fraction(12, 7)); assertTrue(product2.getNumerator() == 10 && product2.getDenominator() == 21); + + + } } diff --git a/Sd1/P/interest/V1/pom.xml b/Sd1/P/interest/V1/pom.xml index 794db0113deb14ef0541e8869a6ed7be8a7ecc37..8468b13b53a7dfe92488f56b7818e95195e51195 100644 --- a/Sd1/P/interest/V1/pom.xml +++ b/Sd1/P/interest/V1/pom.xml @@ -63,7 +63,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/interest/V1/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java b/Sd1/P/interest/V1/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java index 9121a70887d64d79155215b0e6f7c1311eaee181..0a040792f3b155be1c37e43491ed77c07ad606af 100644 --- a/Sd1/P/interest/V1/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java +++ b/Sd1/P/interest/V1/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java @@ -1,25 +1,16 @@ package de.hdm_stuttgart.mi.sd1.interest; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; +import org.junit.Test; /** * Unit test for simple App. */ -public class AccountTest extends TestCase { +public class AccountTest { /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing accounts"); - testsuite.addTestSuite(AccountTest.class); - return testsuite; - } - - /** - * Testing account balance and interest calculatios + * Testing account balance and interest calculations */ + @Test public void testApp() { Account account = new Account(20.); // Create a new instance of class Account diff --git a/Sd1/P/interest/V2/pom.xml b/Sd1/P/interest/V2/pom.xml index 794db0113deb14ef0541e8869a6ed7be8a7ecc37..8468b13b53a7dfe92488f56b7818e95195e51195 100644 --- a/Sd1/P/interest/V2/pom.xml +++ b/Sd1/P/interest/V2/pom.xml @@ -63,7 +63,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/interest/V2/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java b/Sd1/P/interest/V2/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java index 934c7c105f4374508f4686190426f8b8b1a32609..501664acbcc877ec6fc0ba94eaef3370bb38c33b 100644 --- a/Sd1/P/interest/V2/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java +++ b/Sd1/P/interest/V2/src/test/java/de/hdm_stuttgart/mi/sd1/interest/AccountTest.java @@ -1,27 +1,19 @@ package de.hdm_stuttgart.mi.sd1.interest; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; /** * Unit test for simple App. */ -public class AccountTest extends TestCase { +public class AccountTest { /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing accounts"); - testsuite.addTestSuite(AccountTest.class); - return testsuite; - } - - /** - * Testing account balance and interest calculatios + * Testing account balance and interest calculations */ - public void testApp() { + @Test + public void testBalanceInterest() { Account account = new Account(20.); // Create a new instance of class Account diff --git a/Sd1/P/loop/answer/pom.xml b/Sd1/P/loop/answer/pom.xml index a78338126f1c9ca05794a4691a1f8cf02f98dfdd..d0cf5046487594aee08d888627aa388291ef8cf7 100644 --- a/Sd1/P/loop/answer/pom.xml +++ b/Sd1/P/loop/answer/pom.xml @@ -50,7 +50,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/loop/answer/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java b/Sd1/P/loop/answer/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java index 494099cab919df72477cda3efe341428fc51d1fb..6d035096aac1f000a27fa991451c895cbdc03eb0 100644 --- a/Sd1/P/loop/answer/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java +++ b/Sd1/P/loop/answer/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java @@ -1,38 +1,15 @@ package de.hdm_stuttgart.de.sd1.loop; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; /** - * Unit test for simple App. + * Empty! */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } +public class AppTest { + @Test + public void testApp() { + assertTrue(true); + } } diff --git a/Sd1/P/loop/question/pom.xml b/Sd1/P/loop/question/pom.xml index fcc56ff0a98663266e3384c3e119c14047a8de80..4de46f4454ab17c17d82daa5314aa9df280a8585 100644 --- a/Sd1/P/loop/question/pom.xml +++ b/Sd1/P/loop/question/pom.xml @@ -49,7 +49,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/loop/question/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java b/Sd1/P/loop/question/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java index 494099cab919df72477cda3efe341428fc51d1fb..d2b6234b7c0b1c84f074d68ecc850ef3ed2d37fa 100644 --- a/Sd1/P/loop/question/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java +++ b/Sd1/P/loop/question/src/test/java/de/hdm_stuttgart/de/sd1/loop/AppTest.java @@ -1,38 +1,18 @@ package de.hdm_stuttgart.de.sd1.loop; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; /** * Unit test for simple App. */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } +public class AppTest { + /** + * Empty + */ + @Test + public void testApp() { + assertTrue(true); + } } diff --git a/Sd1/P/math/V0_5/pom.xml b/Sd1/P/math/V0_5/pom.xml index 3bb2ff32bae9e91c28a9d65311eba46f349ab7eb..a60ae3429dca1756bdd5cca77896cb2d48d2469e 100644 --- a/Sd1/P/math/V0_5/pom.xml +++ b/Sd1/P/math/V0_5/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java b/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java deleted file mode 100644 index 1476e3091562039cbbd1485c324d771361106772..0000000000000000000000000000000000000000 --- a/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.hdm_stuttgart.de.sd1.math; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Testing exponential implementation - * - */ -public class ExpTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing exponential"); - testsuite.addTestSuite(ExpTest.class); - return testsuite; } - - /** - * Sums and products of fractions - */ - public void testApp() { - } -} diff --git a/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/MaxAbsTest.java b/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/MaxAbsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e5bfd5a933f2ddbdf724e17060b5023f74aed5cb --- /dev/null +++ b/Sd1/P/math/V0_5/src/test/java/de/hdm_stuttgart/de/sd1/math/MaxAbsTest.java @@ -0,0 +1,52 @@ +package de.hdm_stuttgart.de.sd1.math; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Testing exponential implementation + * + */ +public class MaxAbsTest{ + + /** + * Absolute value. + */ + @Test + public void testAbsoluteValue() { + + assertTrue(nearlyEqualCheck(Math.abs(4.1), 4.1, 1E-14)); + assertTrue(nearlyEqualCheck(Math.abs(-3.1), 3.1, 1E-14)); + assertTrue(nearlyEqualCheck(Math.abs(0.), 0., 1E-14)); + } + + /** + * Max value. + */ + @Test + public void testMaxValue() { + assertTrue(nearlyEqualCheck(Math.max(4.1, 5.2), 5.2, 1E-14)); + assertTrue(nearlyEqualCheck(Math.max(-4.1, 3.2), 3.2, 1E-14)); + assertTrue(nearlyEqualCheck(Math.max(3.2, -4.1), 3.2, 1E-14)); + assertTrue(nearlyEqualCheck(Math.max(-5.7, -4.1), -4.1, 1E-14)); + assertTrue(nearlyEqualCheck(Math.max(-4.1, -5.7), -4.1, 1E-14)); + } + + /** + * Test, whether a given value value is within the + * interval [reference - epsilon, reference + epsilon] + * + * This is helpful to assure two values are identical within + * a given tolerance limit being impose by e.g. limited + * computational precision. + * + * @param value The value to be tested + * @param reference The center of the interval + * @param epsilon Twice the inverval's length + * @return + */ + static boolean nearlyEqualCheck(double value, double reference, double epsilon) { + return reference - epsilon < value && value < reference + epsilon; + } +} diff --git a/Sd1/P/math/V1/pom.xml b/Sd1/P/math/V1/pom.xml index fa79ae9caf4c5ee594dd1755cc310be8c379edfd..948ca4dda249c464b2b1fb2f192a087f00e6d044 100644 --- a/Sd1/P/math/V1/pom.xml +++ b/Sd1/P/math/V1/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/math/V1/src/main/java/de/hdm_stuttgart/de/sd1/math/Math.java b/Sd1/P/math/V1/src/main/java/de/hdm_stuttgart/de/sd1/math/Math.java index fcffff8ab0c548a8e5a23b7e5ccdbe92de173637..b445a5a22d46f4de2601175d7b735a44a5b2eeeb 100644 --- a/Sd1/P/math/V1/src/main/java/de/hdm_stuttgart/de/sd1/math/Math.java +++ b/Sd1/P/math/V1/src/main/java/de/hdm_stuttgart/de/sd1/math/Math.java @@ -15,7 +15,7 @@ public class Math { /** * * @param seriesLimit The last term's index of a power series to be included, - * {@link }}. + * */ public static void setSeriesLimit(int seriesLimit) { Math.seriesLimit = seriesLimit; diff --git a/Sd1/P/math/V1/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java b/Sd1/P/math/V1/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java index 7d3dd54f841d1521176a05403ec9749598af96a7..6a2572c7f50b2ce44b35b7da5adf3d47a14d5c75 100644 --- a/Sd1/P/math/V1/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java +++ b/Sd1/P/math/V1/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java @@ -1,27 +1,18 @@ package de.hdm_stuttgart.de.sd1.math; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; /** * Testing exponential implementation * */ -public class ExpTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing exponential"); - testsuite.addTestSuite(ExpTest.class); - return testsuite; } +public class ExpTest { - /** - * Sums and products of fractions - */ + @Test public void testApp() { Math.setSeriesLimit(8); - assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy! + assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-5); // Mind limited computational accuracy! } } diff --git a/Sd1/P/math/V2/pom.xml b/Sd1/P/math/V2/pom.xml index ced5ff2e5bd794abc4c3a9a5ce9e0b429b3f1739..b6cefbddf888540090971f2237089076e399909d 100644 --- a/Sd1/P/math/V2/pom.xml +++ b/Sd1/P/math/V2/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/math/V2/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java b/Sd1/P/math/V2/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java index 7d3dd54f841d1521176a05403ec9749598af96a7..bac0201f932e734b1c870202e2df0ae2474bef7f 100644 --- a/Sd1/P/math/V2/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java +++ b/Sd1/P/math/V2/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java @@ -1,25 +1,19 @@ package de.hdm_stuttgart.de.sd1.math; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; /** * Testing exponential implementation * */ -public class ExpTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing exponential"); - testsuite.addTestSuite(ExpTest.class); - return testsuite; } +public class ExpTest { /** * Sums and products of fractions */ + @Test public void testApp() { Math.setSeriesLimit(8); assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy! diff --git a/Sd1/P/math/V3/pom.xml b/Sd1/P/math/V3/pom.xml index f3955c105fd011384a61c0e750787c3ce6c44cc2..fc20d96772e60a6bef51be87539431e36b641c80 100644 --- a/Sd1/P/math/V3/pom.xml +++ b/Sd1/P/math/V3/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpSinSinoldTest.java b/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpSinSinoldTest.java new file mode 100644 index 0000000000000000000000000000000000000000..baf3977a54f083ba89be86a691a77b386ce52296 --- /dev/null +++ b/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpSinSinoldTest.java @@ -0,0 +1,28 @@ +package de.hdm_stuttgart.de.sd1.math; + +import static org.junit.Assert.*; + +import org.junit.Test; +/** + * Testing exponential implementation + * + */ +public class ExpSinSinoldTest { + + /** + * Sine and exponential + */ + @Test + public void testApp() { + Math.setSeriesLimit(9); + + assertTrue(java.lang.Math.abs( + Math.exp(1) - java.lang.Math.E) < 1.E-6); + + assertTrue(java.lang.Math.abs( + Math.sin(java.lang.Math.PI)) < 1.E-10); + + assertFalse(java.lang.Math.abs( // Lower precision + Math.sinOld(java.lang.Math.PI)) < 1.E-10); + } +} diff --git a/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java b/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java deleted file mode 100644 index 7d3dd54f841d1521176a05403ec9749598af96a7..0000000000000000000000000000000000000000 --- a/Sd1/P/math/V3/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.hdm_stuttgart.de.sd1.math; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Testing exponential implementation - * - */ -public class ExpTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing exponential"); - testsuite.addTestSuite(ExpTest.class); - return testsuite; } - - /** - * Sums and products of fractions - */ - public void testApp() { - Math.setSeriesLimit(8); - assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy! - } -} diff --git a/Sd1/P/math/V4/pom.xml b/Sd1/P/math/V4/pom.xml index a052ade03c6a3d34bb62d7a4db30aa43988b4d1e..b44b1f8d60adde0ade7077304c992364d762fa78 100644 --- a/Sd1/P/math/V4/pom.xml +++ b/Sd1/P/math/V4/pom.xml @@ -62,7 +62,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/math/V4/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java b/Sd1/P/math/V4/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java index 7d3dd54f841d1521176a05403ec9749598af96a7..f438c5f2c37abeb82f46ae826225e1ac08294b34 100644 --- a/Sd1/P/math/V4/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java +++ b/Sd1/P/math/V4/src/test/java/de/hdm_stuttgart/de/sd1/math/ExpTest.java @@ -1,26 +1,17 @@ package de.hdm_stuttgart.de.sd1.math; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; /** * Testing exponential implementation * */ -public class ExpTest extends TestCase { - /** - * @return the suite of tests being tested - */ - public static Test suite() { - final TestSuite testsuite = new TestSuite("Testing exponential"); - testsuite.addTestSuite(ExpTest.class); - return testsuite; } +public class ExpTest { - /** - * Sums and products of fractions - */ - public void testApp() { +@Test +public void testApp() { Math.setSeriesLimit(8); assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy! } diff --git a/Sd1/P/rounding/pom.xml b/Sd1/P/rounding/pom.xml index 7be3de119a41b611e9b2e58ce808bef8c16f2d3f..c23d0b1afd7802166a1a2eb3b08236c6fe5d5ea7 100644 --- a/Sd1/P/rounding/pom.xml +++ b/Sd1/P/rounding/pom.xml @@ -50,7 +50,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.11</version> <scope>test</scope> </dependency> </dependencies> diff --git a/Sd1/P/rounding/src/test/java/de/hdm_stuttgart/de/sd1/rounding/AppTest.java b/Sd1/P/rounding/src/test/java/de/hdm_stuttgart/de/sd1/rounding/AppTest.java index 0c83c06c5c0470501a33c4600836eb62d91e9287..95e28ae0823f20e42a813fd4dd87a99fc4d96163 100644 --- a/Sd1/P/rounding/src/test/java/de/hdm_stuttgart/de/sd1/rounding/AppTest.java +++ b/Sd1/P/rounding/src/test/java/de/hdm_stuttgart/de/sd1/rounding/AppTest.java @@ -1,38 +1,33 @@ package de.hdm_stuttgart.de.sd1.rounding; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.de.sd1.leap.LeapYear; +import de.hdm_stuttgart.de.sd1.sum.Summing; /** - * Unit test for simple App. + * Unit tests */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } +public class AppTest { + + @Test + public void testLeapYear() { + assertTrue(LeapYear.isLeapYear(2000)); + assertTrue(LeapYear.isLeapYear(1984)); + + assertFalse(LeapYear.isLeapYear(2001)); + assertFalse(LeapYear.isLeapYear(1800)); + } + + @Test + public void testSum() { + assertEquals(15, Summing.getSum(5)); + assertEquals(5050, Summing.getSum(100)); + + assertEquals(0, Summing.getSum(0)); + assertEquals(1, Summing.getSum(1)); + assertEquals(3, Summing.getSum(2)); + } } diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index b8fd6176de9d0275c77ec5a9b2084e61c3fae514..534ae38c4c8380f3f3429b4a2ff74436e37b9287 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -3070,9 +3070,9 @@ Is 2016 a leap year? true</programlisting> <qandaentry> <question> <para>We recall <xref linkend="sde1QandaFraction"/>. So far no - one demands fractions to be kept in cancelled state. The call - new Fraction(4,8) will create an instance internally being - represented as <inlineequation> + one demands fractions to be kept in maximally reduced state. The + call new Fraction(4,8) will create an instance internally being + represented by <inlineequation> <m:math display="inline"> <m:mfrac> <m:mi>4</m:mi> @@ -3080,7 +3080,7 @@ Is 2016 a leap year? true</programlisting> <m:mi>8</m:mi> </m:mfrac> </m:math> - </inlineequation> rather than <inlineequation> + </inlineequation> rather than by <inlineequation> <m:math display="inline"> <m:mfrac> <m:mi>1</m:mi> @@ -3090,7 +3090,8 @@ Is 2016 a leap year? true</programlisting> </m:math> </inlineequation>.</para> - <para>Adding cancellation requires a loop implementing the <link + <para>Reducing fractions requires a loop implementing e.g. the + <link xlink:href="http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html">Euclidean algorithm</link> in order to find the greatest common divisor (<acronym>GCD</acronym>) of two non-zero integer values.</para> @@ -3099,10 +3100,43 @@ Is 2016 a leap year? true</programlisting> getGcd(long, long) inside a class <classname>Math</classname>:</para> - <programlisting language="java"> public static long getGcd(long a, long b) { + <programlisting language="java"> public static long getGcd(long a, long b) <co + xml:id="sd1ListEuclidNeg"/> { // Following http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html return ??; }</programlisting> + + <para>With respect to fractions one or both parameters + <code>a</code> and <code>b</code> <coref + linkend="sd1ListEuclidNeg"/> may zero or negative. So we do have + several special cases to handle:</para> + + <glosslist> + <glossentry> + <glossterm>a == 0 and b == 0</glossterm> + + <glossdef> + <para>Return 1.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>a == 0 and b != 0</glossterm> + + <glossdef> + <para>Return absolute value of b.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>a != 0 and b != 0</glossterm> + + <glossdef> + <para>Return the gcd of the absolute values of a and + b</para> + </glossdef> + </glossentry> + </glosslist> </question> <answer> @@ -3136,29 +3170,29 @@ Is 2016 a leap year? true</programlisting> </section> <section xml:id="ed1FractionCancel"> - <title>Cancelling fractions</title> + <title>Reducing fractions</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaFractionCancel"> - <title>Implementing cancellation</title> + <title>Implementing reducing of fractions</title> <qandadiv> <qandaentry> <question> <para>We have implemented <acronym>GCD</acronym> computation in <xref linkend="sd1QandaGcd"/>. The current exercises idea is to - implement cancellation of fractions by using the method + implement reducing of fractions by using the method <methodname>long getGcd(long a, long b)</methodname>. Change the following implementation items:</para> <itemizedlist> <listitem> - <para>The constructor should provide cancellation if - required, see introductory remark.</para> + <para>The constructor should reduce a fraction if required, + see introductory remark.</para> </listitem> <listitem> <para>The Methods <methodname>mult(...)</methodname> and - <methodname>add(...)</methodname> should cancel any + <methodname>add(...)</methodname> should reduce any resulting Fraction instance. It might be worth to consider a defensive strategy to avoid unnecessary overflow errors.</para> @@ -3238,9 +3272,10 @@ Is 2016 a leap year? true</programlisting> </m:mfrac> </m:mrow> </m:math> - </inlineequation> to enable possible cancellations prior to - multiplying. Now the call <code>new Fraction(4,2)</code> will - construct the representation <inlineequation> + </inlineequation> to enable reducing <emphasis + role="bold">prior</emphasis> to multiplying. Now the call + <code>new Fraction(4,2)</code> will construct the representation + <inlineequation> <m:math display="inline"> <m:mfrac> <m:mi>2</m:mi> @@ -4776,8 +4811,8 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</programlisting> </section> </chapter> - <chapter xml:id="sd1Array"> - <title>Arrays</title> + <chapter xml:id="sd1ArrayI"> + <title>Arrays I (2.5.)</title> <section xml:id="sd1ArrayPrepare"> <title>Preparations</title> @@ -4787,6 +4822,36 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</programlisting> </section> </chapter> + <chapter xml:id="sd1ArrayII"> + <title>Arrays II (14.5.)</title> + + <section xml:id="sd1ArrayIprepare"> + <title>Preparations</title> + + <para>Unfortunately <xref linkend="bibHorton2011"/> is somewhat + reluctant regarding Java annotations:</para> + + <remark>A Java source file can contain annotations. An annotation is not + a Java language statement, but a special statement that changes the way + program statements are treated by the compiler or the libraries. You can + define your own annotations but most Java programmers never need to do + so, so I’m not going into the how.</remark> + + <para>With respect to unit testing using (not defining!) annotations is + very helpful.</para> + + <para>Get a basic understanding of Annotations by reading .</para> + + <para>tests</para> + + <para>main()</para> + + <para>Chapter 6 <xref linkend="bibHorton2011"/> until (including) + <quote>Using the final modifier</quote>. You may skip <quote>Methods + accepting a variable number of arguments</quote>.</para> + </section> + </chapter> + <chapter xml:id="ideas"> <title>Ideas</title>