From 0239cb06a963db9f2b0ced62720947c4351b6f7c Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Fri, 2 May 2014 01:03:18 +0200 Subject: [PATCH] Interegr container exercises --- Sd1/P/Array/integerStore/.gitignore | 4 + Sd1/P/Array/integerStore/pom.xml | 69 +++++ .../de/hdm_stuttgart/mi/sd1/main/Driver.java | 29 ++ .../mi/sd1/store/BoundedIntegerStore.java | 59 ++++ .../mi/sd1/store/package-info.java | 5 + .../mi/sd1/fraction/IntStoreTest.java | 34 +++ Sd1/P/Array/integerStoreSkeleton/.gitignore | 4 + Sd1/P/Array/integerStoreSkeleton/pom.xml | 69 +++++ .../de/hdm_stuttgart/mi/sd1/main/Driver.java | 29 ++ .../mi/sd1/store/BoundedIntegerStore.java | 59 ++++ .../mi/sd1/store/package-info.java | 5 + .../mi/sd1/fraction/IntStoreTest.java | 34 +++ Sd1/P/Array/integerStoreStat/.gitignore | 5 + Sd1/P/Array/integerStoreStat/pom.xml | 69 +++++ .../de/hdm_stuttgart/mi/sd1/main/Driver.java | 29 ++ .../mi/sd1/store/IntegerStore.java | 94 +++++++ .../mi/sd1/store/package-info.java | 5 + .../mi/sd1/fraction/IntStoreTest.java | 78 ++++++ Sd1/P/Array/integerStoreUnbounded/.gitignore | 5 + Sd1/P/Array/integerStoreUnbounded/pom.xml | 69 +++++ .../de/hdm_stuttgart/mi/sd1/main/Driver.java | 29 ++ .../mi/sd1/store/IntegerStore.java | 78 ++++++ .../mi/sd1/store/package-info.java | 5 + .../mi/sd1/fraction/IntStoreTest.java | 47 ++++ Sd1/swd1.xml | 259 +++++++++++++++++- 25 files changed, 1157 insertions(+), 15 deletions(-) create mode 100644 Sd1/P/Array/integerStore/.gitignore create mode 100644 Sd1/P/Array/integerStore/pom.xml create mode 100644 Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java create mode 100644 Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java create mode 100644 Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java create mode 100644 Sd1/P/Array/integerStore/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java create mode 100644 Sd1/P/Array/integerStoreSkeleton/.gitignore create mode 100644 Sd1/P/Array/integerStoreSkeleton/pom.xml create mode 100644 Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java create mode 100644 Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java create mode 100644 Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java create mode 100644 Sd1/P/Array/integerStoreSkeleton/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java create mode 100644 Sd1/P/Array/integerStoreStat/.gitignore create mode 100644 Sd1/P/Array/integerStoreStat/pom.xml create mode 100644 Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java create mode 100644 Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java create mode 100644 Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java create mode 100644 Sd1/P/Array/integerStoreStat/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java create mode 100644 Sd1/P/Array/integerStoreUnbounded/.gitignore create mode 100644 Sd1/P/Array/integerStoreUnbounded/pom.xml create mode 100644 Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java create mode 100644 Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java create mode 100644 Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java create mode 100644 Sd1/P/Array/integerStoreUnbounded/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java diff --git a/Sd1/P/Array/integerStore/.gitignore b/Sd1/P/Array/integerStore/.gitignore new file mode 100644 index 000000000..4e247eee2 --- /dev/null +++ b/Sd1/P/Array/integerStore/.gitignore @@ -0,0 +1,4 @@ +/.settings +/target +/.classpath +/.project diff --git a/Sd1/P/Array/integerStore/pom.xml b/Sd1/P/Array/integerStore/pom.xml new file mode 100644 index 000000000..965309796 --- /dev/null +++ b/Sd1/P/Array/integerStore/pom.xml @@ -0,0 +1,69 @@ +<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>intstore</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>IntegerStore</name> + <url>http://maven.apache.org</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.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <configuration> + <linksource>true</linksource> + <!-- Setting destDir interferes with taglet class --> + <additionalJOptions> + <additionalJOption>-d ${javadocDestdir}</additionalJOption> + </additionalJOptions> + + <taglets> + <taglet> + <tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass> + </taglet> + </taglets> + <tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath> + </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/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java new file mode 100644 index 000000000..50cccc930 --- /dev/null +++ b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java @@ -0,0 +1,29 @@ +package de.hdm_stuttgart.mi.sd1.main; + +import de.hdm_stuttgart.mi.sd1.store.BoundedIntegerStore; + +/** + * Playing with fraction objects, producing some output. + * + */ +public class Driver { + /** + * @param args unused + */ + public static void main(String[] args) { + + // Create a new Store + final BoundedIntegerStore store = new BoundedIntegerStore(4); + + // Fill in some values + store.addValue(32); + store.addValue(-5); + store.addValue(24); + + System.out.println("Store contains " + store.getNumValues() + " values"); + + for (int i = 0; i < store.getNumValues(); i++) { + System.out.println(i + ":" + store.getNumValues() + " values"); + } + } +} diff --git a/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java new file mode 100644 index 000000000..c9fb14441 --- /dev/null +++ b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java @@ -0,0 +1,59 @@ +package de.hdm_stuttgart.mi.sd1.store; + +/** + * A container holding a fixed + * number of integer values. + * + */ +public class BoundedIntegerStore { + + final int[] values ; // Array containing our values + int numValues = 0; // Number of values present in the container so far. + + /** + * Create a new integer store being able to + * hold a fixed number of values. + * + * @param capacity The number of values this store may contain + */ + public BoundedIntegerStore(int capacity) { + values = new int[capacity]; + } + + /** + * @return The number of elements the store may + * hold, see {@link #BoundedIntegerStore(int)}. + */ + public int getCapacity() { + return values.length; + } + + /** + * @return The number of values being contained. + */ + public int getNumValues() { + return numValues; + } + + /** + * Insert a new value into our container + * + * @param value The value to be inserted. This will increment + * {@link #getNumValues()} by one. + * + */ + public void addValue(int value) { + values[numValues++] = value; + } + + /** + * Access the value at a given index + * + * @param index The desired value's index + * @return The desired value. Precondition: index < {@link #getNumValues()}} + * + */ + public int getValue(int index) { + return values[index]; + } +} \ No newline at end of file diff --git a/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java new file mode 100644 index 000000000..94590c084 --- /dev/null +++ b/Sd1/P/Array/integerStore/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java @@ -0,0 +1,5 @@ +/** + * Dealing with integer stores. + * + */ +package de.hdm_stuttgart.mi.sd1.store; \ No newline at end of file diff --git a/Sd1/P/Array/integerStore/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java b/Sd1/P/Array/integerStore/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java new file mode 100644 index 000000000..f5b70ac97 --- /dev/null +++ b/Sd1/P/Array/integerStore/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java @@ -0,0 +1,34 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.store.BoundedIntegerStore; + +public class IntStoreTest { + + @Test + public void testPositiveNegative() { + final int capacity = 4; + BoundedIntegerStore store = new BoundedIntegerStore(capacity); + + + assertEquals(capacity, store.getCapacity()); // The store's capacity + assertEquals(0, store.getNumValues()); // Store is initially empty + + final int v1 = -17, v2 = 3, v3 = -4; + + store.addValue(v1); // Add a single value + assertEquals(1, store.getNumValues()); // Store contains one value + assertEquals(v1, store.getValue(0)); // Value should be present at index 0 + + store.addValue(v2); // Add two more values + store.addValue(v3); + + assertEquals(3, store.getNumValues()); // Store contains three values + assertEquals(v1, store.getValue(0)); // Retrieve all values + assertEquals(v2, store.getValue(1)); + assertEquals(v3, store.getValue(2)); + } +} diff --git a/Sd1/P/Array/integerStoreSkeleton/.gitignore b/Sd1/P/Array/integerStoreSkeleton/.gitignore new file mode 100644 index 000000000..4e247eee2 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/.gitignore @@ -0,0 +1,4 @@ +/.settings +/target +/.classpath +/.project diff --git a/Sd1/P/Array/integerStoreSkeleton/pom.xml b/Sd1/P/Array/integerStoreSkeleton/pom.xml new file mode 100644 index 000000000..965309796 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/pom.xml @@ -0,0 +1,69 @@ +<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>intstore</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>IntegerStore</name> + <url>http://maven.apache.org</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.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <configuration> + <linksource>true</linksource> + <!-- Setting destDir interferes with taglet class --> + <additionalJOptions> + <additionalJOption>-d ${javadocDestdir}</additionalJOption> + </additionalJOptions> + + <taglets> + <taglet> + <tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass> + </taglet> + </taglets> + <tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath> + </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/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java new file mode 100644 index 000000000..50cccc930 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java @@ -0,0 +1,29 @@ +package de.hdm_stuttgart.mi.sd1.main; + +import de.hdm_stuttgart.mi.sd1.store.BoundedIntegerStore; + +/** + * Playing with fraction objects, producing some output. + * + */ +public class Driver { + /** + * @param args unused + */ + public static void main(String[] args) { + + // Create a new Store + final BoundedIntegerStore store = new BoundedIntegerStore(4); + + // Fill in some values + store.addValue(32); + store.addValue(-5); + store.addValue(24); + + System.out.println("Store contains " + store.getNumValues() + " values"); + + for (int i = 0; i < store.getNumValues(); i++) { + System.out.println(i + ":" + store.getNumValues() + " values"); + } + } +} diff --git a/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java new file mode 100644 index 000000000..5ba398210 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/BoundedIntegerStore.java @@ -0,0 +1,59 @@ +package de.hdm_stuttgart.mi.sd1.store; + +/** + * A container holding a fixed + * number of integer values. + * + */ +public class BoundedIntegerStore { + + final int[] values ; // Array containing our values + int numValues = 0; // Number of values present in the container so far. + + /** + * Create a new integer store being able to + * hold a fixed number of values. + * + * @param capacity The number of values this store may contain + */ + public BoundedIntegerStore(int capacity) { + values = null; // TODO + } + + /** + * @return The number of elements the store may + * hold, see {@link #BoundedIntegerStore(int)}. + */ + public int getCapacity() { + return 0; // TODO + } + + /** + * @return The number of values being contained. + */ + public int getNumValues() { + return 0; // TODO + } + + /** + * Insert a new value into our container + * + * @param value The value to be inserted. This will increment + * {@link #getNumValues()} by one. + * + */ + public void addValue(int value) { + // TODO; + } + + /** + * Access the value at a given index + * + * @param index The desired value's index + * @return The desired value. Precondition: index < {@link #getNumValues()}} + * + */ + public int getValue(int index) { + return 0; // TODO + } +} \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java new file mode 100644 index 000000000..94590c084 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java @@ -0,0 +1,5 @@ +/** + * Dealing with integer stores. + * + */ +package de.hdm_stuttgart.mi.sd1.store; \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreSkeleton/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java b/Sd1/P/Array/integerStoreSkeleton/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java new file mode 100644 index 000000000..f5b70ac97 --- /dev/null +++ b/Sd1/P/Array/integerStoreSkeleton/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java @@ -0,0 +1,34 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.store.BoundedIntegerStore; + +public class IntStoreTest { + + @Test + public void testPositiveNegative() { + final int capacity = 4; + BoundedIntegerStore store = new BoundedIntegerStore(capacity); + + + assertEquals(capacity, store.getCapacity()); // The store's capacity + assertEquals(0, store.getNumValues()); // Store is initially empty + + final int v1 = -17, v2 = 3, v3 = -4; + + store.addValue(v1); // Add a single value + assertEquals(1, store.getNumValues()); // Store contains one value + assertEquals(v1, store.getValue(0)); // Value should be present at index 0 + + store.addValue(v2); // Add two more values + store.addValue(v3); + + assertEquals(3, store.getNumValues()); // Store contains three values + assertEquals(v1, store.getValue(0)); // Retrieve all values + assertEquals(v2, store.getValue(1)); + assertEquals(v3, store.getValue(2)); + } +} diff --git a/Sd1/P/Array/integerStoreStat/.gitignore b/Sd1/P/Array/integerStoreStat/.gitignore new file mode 100644 index 000000000..128b1c231 --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/.gitignore @@ -0,0 +1,5 @@ +/.settings +/target +/.classpath +/.project +/bin diff --git a/Sd1/P/Array/integerStoreStat/pom.xml b/Sd1/P/Array/integerStoreStat/pom.xml new file mode 100644 index 000000000..965309796 --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/pom.xml @@ -0,0 +1,69 @@ +<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>intstore</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>IntegerStore</name> + <url>http://maven.apache.org</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.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <configuration> + <linksource>true</linksource> + <!-- Setting destDir interferes with taglet class --> + <additionalJOptions> + <additionalJOption>-d ${javadocDestdir}</additionalJOption> + </additionalJOptions> + + <taglets> + <taglet> + <tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass> + </taglet> + </taglets> + <tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath> + </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/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java new file mode 100644 index 000000000..8d77b2bfc --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java @@ -0,0 +1,29 @@ +package de.hdm_stuttgart.mi.sd1.main; + +import de.hdm_stuttgart.mi.sd1.store.IntegerStore; + +/** + * Playing with fraction objects, producing some output. + * + */ +public class Driver { + /** + * @param args unused + */ + public static void main(String[] args) { + + // Create a new Store + final IntegerStore store = new IntegerStore(4); + + // Fill in some values + store.addValue(32); + store.addValue(-5); + store.addValue(24); + + System.out.println("Store contains " + store.getNumValues() + " values"); + + for (int i = 0; i < store.getNumValues(); i++) { + System.out.println(i + ":" + store.getNumValues() + " values"); + } + } +} diff --git a/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java new file mode 100644 index 000000000..279241e2e --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java @@ -0,0 +1,94 @@ +package de.hdm_stuttgart.mi.sd1.store; + +/** + * A container holding a fixed + * number of integer values. + * + */ +public class IntegerStore { + + final static int defaultCapacity = 4; + int[] values ; // Array containing our values + int numValues = 0; // Number of values present in the container so far. + + /** + * Create a new store being able to + * hold integer values. + * + */ + public IntegerStore() { + values = new int[defaultCapacity]; + } + /** + * Create a new store being able to + * hold integer values. + * + * @param capacity The store's initial capacity. If more values + * are being added via {@link #addValue(int)} the capacity + * will be increased dynamically. + * + */ + public IntegerStore(int capacity) { + values = new int[capacity]; + } + + /** + * @return The number of elements the store may + * hold, see {@link #IntegerStore(int)}. + */ + public int getCapacity() { + return values.length; + } + + /** + * @return The number of values being contained. + */ + public int getNumValues() { + return numValues; + } + + /** + * Insert a new value into our container + * + * @param value The value to be inserted. This will increment + * {@link #getNumValues()} by one. + * + */ + public void addValue(int value) { + if (values.length <= numValues) { // Sufficient capacity available to add a new value? + final int[] currentArray = values; + values = new int[2 * currentArray.length]; // Double the current array's size. + for (int i = 0; i < currentArray.length; i++) { + values[i] = currentArray[i]; + } + } + values[numValues++] = value; + } + + /** + * Access the value at a given index + * + * @param index The desired value's index + * @return The desired value. Precondition: index < {@link #getNumValues()}} + * + */ + public int getValue(int index) { + return values[index]; + } + + /** + * Empty the current set of values and set the store + * to its initial state. + */ + public void clear() { + numValues = 0; + } + + public double getAverage() { + double sum = 0; + for (int i = 0; i < numValues; i++) { + sum += values[i]; + } + return sum / numValues; + } +} \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java new file mode 100644 index 000000000..94590c084 --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java @@ -0,0 +1,5 @@ +/** + * Dealing with integer stores. + * + */ +package de.hdm_stuttgart.mi.sd1.store; \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreStat/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java b/Sd1/P/Array/integerStoreStat/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java new file mode 100644 index 000000000..7a5982b30 --- /dev/null +++ b/Sd1/P/Array/integerStoreStat/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java @@ -0,0 +1,78 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.store.IntegerStore; + +public class IntStoreTest { + + @Test + public void testClear() { + IntegerStore store = new IntegerStore(4); + + assertEquals(0, store.getNumValues()); // Store is initially empty + + store.addValue(2); + store.addValue(4); + assertEquals(2, store.getNumValues()); // Store is initially empty + + store.clear(); + + assertEquals(0, store.getNumValues()); // Store is initially empty + store.addValue(2); + store.addValue(4); + store.addValue(4); + assertEquals(3, store.getNumValues()); // Store is initially empty + } + @Test + public void testStatistics() { + + IntegerStore store = new IntegerStore(4); + + store.addValue(2); + store.addValue(4); + store.addValue(-3); + + assertTrue(Math.abs(1. - store.getAverage()) < 1.E-50); + + } + + @Test + public void testPositiveNegative() { + final int capacity = 3; + IntegerStore store = new IntegerStore(capacity); + + + assertEquals(capacity, store.getCapacity()); // The store's initial capacity + assertEquals(0, store.getNumValues()); // Store is initially empty + + final int v1 = -17, v2 = 3, v3 = -4, v4 = 7; + + store.addValue(v1); // Add a single value + assertEquals(1, store.getNumValues()); // Store contains one value + assertEquals(v1, store.getValue(0)); // Value should be present at index 0 + + store.addValue(v2); // Add two more values + store.addValue(v3); + + assertEquals(capacity, store.getCapacity()); // The store's initial capacity + assertEquals(3, store.getNumValues()); // Store contains three values + assertEquals(v1, store.getValue(0)); // Retrieve all values + assertEquals(v2, store.getValue(1)); + assertEquals(v3, store.getValue(2)); + + // Now we breach the container's initial capacity + store.addValue(v4); + assertEquals(2 * capacity, store.getCapacity()); // Twice the store's initial capacity + + // add 99996 more values + for (int i = 4; i < 100_000; i++) { + store.addValue(i); + } + assertEquals(100_000, store.getNumValues()); + assertTrue(100_000 <= store.getCapacity()); // Capacity should at least be 100_000 + + } +} diff --git a/Sd1/P/Array/integerStoreUnbounded/.gitignore b/Sd1/P/Array/integerStoreUnbounded/.gitignore new file mode 100644 index 000000000..128b1c231 --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/.gitignore @@ -0,0 +1,5 @@ +/.settings +/target +/.classpath +/.project +/bin diff --git a/Sd1/P/Array/integerStoreUnbounded/pom.xml b/Sd1/P/Array/integerStoreUnbounded/pom.xml new file mode 100644 index 000000000..965309796 --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/pom.xml @@ -0,0 +1,69 @@ +<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>intstore</artifactId> + <version>1.0</version> + <packaging>jar</packaging> + + <name>IntegerStore</name> + <url>http://maven.apache.org</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.8</source> + <target>1.8</target> + </configuration> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <version>2.9.1</version> + <configuration> + <linksource>true</linksource> + <!-- Setting destDir interferes with taglet class --> + <additionalJOptions> + <additionalJOption>-d ${javadocDestdir}</additionalJOption> + </additionalJOptions> + + <taglets> + <taglet> + <tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass> + </taglet> + </taglets> + <tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath> + </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/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java new file mode 100644 index 000000000..8d77b2bfc --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/main/Driver.java @@ -0,0 +1,29 @@ +package de.hdm_stuttgart.mi.sd1.main; + +import de.hdm_stuttgart.mi.sd1.store.IntegerStore; + +/** + * Playing with fraction objects, producing some output. + * + */ +public class Driver { + /** + * @param args unused + */ + public static void main(String[] args) { + + // Create a new Store + final IntegerStore store = new IntegerStore(4); + + // Fill in some values + store.addValue(32); + store.addValue(-5); + store.addValue(24); + + System.out.println("Store contains " + store.getNumValues() + " values"); + + for (int i = 0; i < store.getNumValues(); i++) { + System.out.println(i + ":" + store.getNumValues() + " values"); + } + } +} diff --git a/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java new file mode 100644 index 000000000..e8af31ed6 --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java @@ -0,0 +1,78 @@ +package de.hdm_stuttgart.mi.sd1.store; + +/** + * A container holding a fixed + * number of integer values. + * + */ +public class IntegerStore { + + final static int defaultCapacity = 4; + int[] values ; // Array containing our values + int numValues = 0; // Number of values present in the container so far. + + /** + * Create a new store being able to + * hold integer values. + * + */ + public IntegerStore() { + values = new int[defaultCapacity]; + } + /** + * Create a new store being able to + * hold integer values. + * + * @param capacity The store's initial capacity. If more values + * are being added via {@link #addValue(int)} the capacity + * will be increased dynamically. + * + */ + public IntegerStore(int capacity) { + values = new int[capacity]; + } + + /** + * @return The number of elements the store may + * hold, see {@link #IntegerStore(int)}. + */ + public int getCapacity() { + return values.length; + } + + /** + * @return The number of values being contained. + */ + public int getNumValues() { + return numValues; + } + + /** + * Insert a new value into our container + * + * @param value The value to be inserted. This will increment + * {@link #getNumValues()} by one. + * + */ + public void addValue(int value) { + if (values.length <= numValues) { // Sufficient capacity available to add a new value? + final int[] currentArray = values; + values = new int[2 * currentArray.length]; // Double the current array's size. + for (int i = 0; i < currentArray.length; i++) { + values[i] = currentArray[i]; + } + } + values[numValues++] = value; + } + + /** + * Access the value at a given index + * + * @param index The desired value's index + * @return The desired value. Precondition: index < {@link #getNumValues()}} + * + */ + public int getValue(int index) { + return values[index]; + } +} \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java new file mode 100644 index 000000000..94590c084 --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/package-info.java @@ -0,0 +1,5 @@ +/** + * Dealing with integer stores. + * + */ +package de.hdm_stuttgart.mi.sd1.store; \ No newline at end of file diff --git a/Sd1/P/Array/integerStoreUnbounded/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java b/Sd1/P/Array/integerStoreUnbounded/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java new file mode 100644 index 000000000..5b5d300db --- /dev/null +++ b/Sd1/P/Array/integerStoreUnbounded/src/test/java/de/hdm_stuttgart/mi/sd1/fraction/IntStoreTest.java @@ -0,0 +1,47 @@ +package de.hdm_stuttgart.mi.sd1.fraction; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import de.hdm_stuttgart.mi.sd1.store.IntegerStore; + +public class IntStoreTest { + + @Test + public void testPositiveNegative() { + final int capacity = 3; + IntegerStore store = new IntegerStore(capacity); + + + assertEquals(capacity, store.getCapacity()); // The store's initial capacity + assertEquals(0, store.getNumValues()); // Store is initially empty + + final int v1 = -17, v2 = 3, v3 = -4, v4 = 7; + + store.addValue(v1); // Add a single value + assertEquals(1, store.getNumValues()); // Store contains one value + assertEquals(v1, store.getValue(0)); // Value should be present at index 0 + + store.addValue(v2); // Add two more values + store.addValue(v3); + + assertEquals(capacity, store.getCapacity()); // The store's initial capacity + assertEquals(3, store.getNumValues()); // Store contains three values + assertEquals(v1, store.getValue(0)); // Retrieve all values + assertEquals(v2, store.getValue(1)); + assertEquals(v3, store.getValue(2)); + + // Now we breach the container's initial capacity + store.addValue(v4); + assertEquals(2 * capacity, store.getCapacity()); // Twice the store's initial capacity + + // add 99996 more values + for (int i = 4; i < 100_000; i++) { + store.addValue(i); + } + assertEquals(100_000, store.getNumValues()); + assertTrue(100_000 <= store.getCapacity()); // Capacity should at least be 100_000 + + } +} diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index 534ae38c4..193f2a9b1 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -4820,6 +4820,196 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</programlisting> <para>Chapter 4 <xref linkend="bibHorton2011"/> excluding <quote>Mutable Strings</quote>.</para> </section> + + <section xml:id="sd1ArrayiExercise"> + <title>Exercises</title> + + <section xml:id="sd1IntStore"> + <title>Storing integer values</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaIntStore"> + <title>A container of fixed capacity holding integer values </title> + + <qandadiv> + <qandaentry> + <question> + <para>Implement a class + <classname>BoundedIntegerStore</classname> being able to hold + a fixed number of integer values. Internally these values will + be stored in an array. The design has been chosen to be + extended in later exercises.</para> + + <para>A skeleton project archive is available at:</para> + + <para role="eclipse">P/Array/integerStoreSkeleton</para> + + <para>The above link contains a skeleton file + <filename>project.zip</filename>. You may import this project + into your eclipse workspace by:</para> + + <itemizedlist> + <listitem> + <para>Creating an empty directory e.g. + <quote>myProject</quote>.</para> + </listitem> + + <listitem> + <para>Unzip <filename>project.zip</filename> into + <quote>myProject</quote>.</para> + </listitem> + + <listitem> + <para>Choose File-->Import-->Maven-->Existing + maven projects in Eclipse and navigate to the + <quote>myProject</quote> folder to import it.</para> + </listitem> + </itemizedlist> + + <para>This skeleton project contains:</para> + + <itemizedlist> + <listitem> + <para>A class <classname>Driver</classname> which allows + you to execute some output generating tests.</para> + </listitem> + + <listitem> + <para>A <productname>Junit</productname> test + <classname>IntStoreTest</classname> which allows you to + test your ongoing implementation of + <classname>BoundedIntegerStore</classname>. This class + contains several <code>// TODO</code> comments indicating + positions to be completed.</para> + </listitem> + + <listitem> + <para>The tests will fail initially. After successful + implementation they should however run + successfully.</para> + </listitem> + </itemizedlist> + </question> + + <answer> + <para role="eclipse">P/Array/integerStoreSkeleton</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sd1IntStoreUnbounded"> + <title>Let the store grow dynamically</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaIntStoreUnbounded"> + <title>A container of variable capacity holding integer + values</title> + + <qandadiv> + <qandaentry> + <question> + <para>In <xref linkend="sd1QandaIntStore"/> once a store has + been created its capacity is fixed. We'd like to extend our + class to support dynamic growth while adding new values. To + achieve this goal follow the following steps:</para> + + <orderedlist> + <listitem> + <para>Copy the completed project to a new one e.g. named + <quote>UnboundedContainer</quote>. This way you still have + the bounded implementation available.</para> + </listitem> + + <listitem> + <para>Rename your class + <classname>BoundedIntegerStore</classname> to + <classname>IntegerStore</classname> reflecting the + possibility of dynamic growth.</para> + </listitem> + + <listitem> + <para>You have to modify the method <code>void + addValue(int value)</code>: If the array's size gets + exceeded (e.g. adding the fifth value having array size of + just 4) do:</para> + + <itemizedlist> + <listitem> + <para>Create a second array offering twice the current + capacity. This technique is referred to as + <quote>amortized doubling</quote>.</para> + </listitem> + + <listitem> + <para>Copy all existing values from the current array + to the new array</para> + </listitem> + + <listitem> + <para>Assign the new array to <code>int[] + values</code>. You thereby implicitly discard the old + array.</para> + </listitem> + + <listitem> + <para>copy the latest argument to the array as + usual.</para> + </listitem> + </itemizedlist> + + <para>Basically you have to compare the number of already + inserted elements and the current capacity prior to + inserting any new value.</para> + </listitem> + + <listitem> + <para>Add a default constructor providing an initial + default capacity of 4.</para> + </listitem> + </orderedlist> + + <para>Modify and add <productname>Junit</productname> tests + accordingly to reflect the new behaviour. Especially test + correct capacity reallocation for larger numbers of values + being added.</para> + </question> + + <answer> + <para role="eclipse">P/Array/integerStoreSkeleton</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + + <section xml:id="sdnStoreStatistics"> + <title>Providing statistical data</title> + + <qandaset defaultlabel="qanda" xml:id="sd1QandaIntStoreStat"> + <title>Adding support to retrieve statistical data.</title> + + <qandadiv> + <qandaentry> + <question> + <para>Extend <xref linkend="sd1QandaIntStoreUnbounded"/> by + providing a method <methodname>double + getAverage()</methodname> to provide statistical data on a + given set of integer values. In addition provide a method + <methodname>void clear()</methodname> enabling a user to + support different sets of values.</para> + + <para>Do not forget to extend your + <productname>Junit</productname> tests.</para> + </question> + + <answer> + <para role="eclipse">P/Array/integerStoreStat</para> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + </section> </chapter> <chapter xml:id="sd1ArrayII"> @@ -4828,27 +5018,66 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</programlisting> <section xml:id="sd1ArrayIprepare"> <title>Preparations</title> - <para>Unfortunately <xref linkend="bibHorton2011"/> is somewhat - reluctant regarding Java annotations:</para> + <glosslist> + <glossentry> + <glossterm>Java Annotations</glossterm> - <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> + <glossdef> + <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 Java annotations by reading + <link + xlink:href="http://docs.oracle.com/javase/tutorial/java/annotations">Lesson: + Annotations</link>. Don't worry if you do not understand the + complete article. The primary intention is understanding the + <code>@Test</code> annotation in <productname>Junit</productname>, + see next topic.</para> + </glossdef> + </glossentry> - <para>With respect to unit testing using (not defining!) annotations is - very helpful.</para> + <glossentry> + <glossterm>Unit testing using + <productname>Junit</productname></glossterm> - <para>Get a basic understanding of Annotations by reading .</para> + <glossdef> + <para>Read the introduction at least till <quote>Assert + Methods</quote>. You may want to read the section <quote>Code + Coverage</quote> as well.</para> + </glossdef> + </glossentry> - <para>tests</para> + <glossentry> + <glossterm>Inheritance</glossterm> + + <glossdef> + <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> + </glossdef> + </glossentry> - <para>main()</para> + <glossentry> + <glossterm><productname>Greenfoot</productname> and + loops</glossterm> - <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> + <glossdef> + <para>Read chapter 5 of <xref + linkend="bibKoelling2010Ger"/>.</para> + </glossdef> + </glossentry> + </glosslist> </section> </chapter> -- GitLab