From 2b89f297e4a90aec737600941c23b17f8573263c Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Thu, 5 Jun 2014 19:35:19 +0200 Subject: [PATCH] Arraydif example --- Sd1/P/Etest/MaxArrayDiff/Solution/.gitignore | 4 ++ Sd1/P/Etest/MaxArrayDiff/Solution/pom.xml | 58 +++++++++++++++++++ .../mi/sd1/maxarraydiff/Calc.java | 33 +++++++++++ .../mi/sd1/maxarraydiff/package-info.java | 5 ++ .../mi/sd1/fraction/CalcTest.java | 33 +++++++++++ 5 files changed, 133 insertions(+) create mode 100644 Sd1/P/Etest/MaxArrayDiff/Solution/.gitignore create mode 100644 Sd1/P/Etest/MaxArrayDiff/Solution/pom.xml create mode 100644 Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/Calc.java create mode 100644 Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/package-info.java create mode 100644 Sd1/P/Etest/MaxArrayDiff/Solution/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/CalcTest.java diff --git a/Sd1/P/Etest/MaxArrayDiff/Solution/.gitignore b/Sd1/P/Etest/MaxArrayDiff/Solution/.gitignore new file mode 100644 index 000000000..4e247eee2 --- /dev/null +++ b/Sd1/P/Etest/MaxArrayDiff/Solution/.gitignore @@ -0,0 +1,4 @@ +/.settings +/target +/.classpath +/.project diff --git a/Sd1/P/Etest/MaxArrayDiff/Solution/pom.xml b/Sd1/P/Etest/MaxArrayDiff/Solution/pom.xml new file mode 100644 index 000000000..35aaf2a42 --- /dev/null +++ b/Sd1/P/Etest/MaxArrayDiff/Solution/pom.xml @@ -0,0 +1,58 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>de.hdm-stuttgart.de.sd1</groupId> + <artifactId>maxarraydiff</artifactId> + <version>2.0</version> + <packaging>jar</packaging> + + <name>vowel</name> + <url>http://www.mi.hdm-stuttgart.de/freedocs/topic/de.hdm_stuttgart.mi.swd1/index.html</url> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <javadocDestdir>~/tmp</javadocDestdir> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.3.2</version> + <configuration> + <source>1.7</source> + <target>1.7</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <configuration> + <linksource>true</linksource> + </configuration> + </plugin> + </plugins> + + <resources> + <resource> + <directory>src/main/java</directory> + <includes> + <include> **/*.properties</include> + </includes> + </resource> + </resources> + </build> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/Calc.java b/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/Calc.java new file mode 100644 index 000000000..eb1e4a4bb --- /dev/null +++ b/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/Calc.java @@ -0,0 +1,33 @@ +package de.hdm_stuttgart.mi.sd1.maxarraydiff; + +/** + * Array calculations. + * + */ +public class Calc { + + /** + * Calculate the maximum of all mutual absolute + * difference values. Example: + * + * values = {1, 7, 2, -3, 4} + * + * The maximum is 7 - (-3) = 10 + * + * + * @param values An unordered list of integer values + * @return The number of vowels being contained in s. + */ + public static int maxArrayDiff (final int [] values) { + + int maxDiff = 0; + for (int i = 0; i < values.length; i++) { + for (int j = i + 1; j < values.length; j++) { + if (maxDiff < Math.abs(values[i] -values[j])) { + maxDiff = Math.abs(values[i] -values[j]); + } + } + } + return maxDiff; + } +} diff --git a/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/package-info.java b/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/package-info.java new file mode 100644 index 000000000..caccbbf6b --- /dev/null +++ b/Sd1/P/Etest/MaxArrayDiff/Solution/src/main/java/de/hdm_stuttgart/mi/sd1/maxarraydiff/package-info.java @@ -0,0 +1,5 @@ +/** + * Computing array value differences + * + */ +package de.hdm_stuttgart.mi.sd1.maxarraydiff; \ No newline at end of file diff --git a/Sd1/P/Etest/MaxArrayDiff/Solution/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/CalcTest.java b/Sd1/P/Etest/MaxArrayDiff/Solution/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/CalcTest.java new file mode 100644 index 000000000..acca1325c --- /dev/null +++ b/Sd1/P/Etest/MaxArrayDiff/Solution/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/CalcTest.java @@ -0,0 +1,33 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import org.junit.Assert; +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.maxarraydiff.Calc; + +@SuppressWarnings("javadoc") +public class CalcTest { + + @Test public void testZero() { + Assert.assertEquals(0, Calc.maxArrayDiff(new int[]{})); + } + + @Test public void testOne() { + Assert.assertEquals(0, Calc.maxArrayDiff(new int[]{-3})); + } + + @Test public void testTwo() { + Assert.assertEquals(4, Calc.maxArrayDiff(new int[]{1, -3})); + } + + @Test public void testThree() { + Assert.assertEquals(13, Calc.maxArrayDiff(new int[]{1, 4, -9})); + } + + @Test public void testMulti() { + Assert.assertEquals(17, Calc.maxArrayDiff(new int[]{ -1, 0, -5, 3, 5, 12, 8})); + } + + + +} -- GitLab