Project dependencies and Maven
Helper.java |
pom.xml |
---|---|
|
|
goik@goiki Helper> mvn install [INFO] Scanning for projects... [INFO] ------------------------------------------- [INFO] Building helper 0.9 .. -------------------------------------------------- T E S T S -------------------------------------------------- Running de.hdm_stuttgart.de.mi.sd1.HelperTest Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, ... [INFO] Installing .../Helper/target/helper-0.9.jar to /ma/goik/.m2/repository/de/hdm_stuttgart/mi/sd1/helper/0.9/helper-0.9.jar
goik@goiki tmp> unzip ...hdm_stuttgart/de/mi/sd1/helper/0.9/helper-0.9.jar
Archive: .../.m2/repository/.../sd1/helper/0.9/helper-0.9.jar
creating: META-INF/
inflating: META-INF/MANIFEST.MF
creating: de/
creating: de/hdm_stuttgart/
creating: de/hdm_stuttgart/mi/
creating: de/hdm_stuttgart/mi/sd1/
inflating: de/hdm_stuttgart/mi/sd1/Helper.class
creating: META-INF/maven/
creating: META-INF/maven/de.hdm_stuttgart.mi.sd1/
creating: META-INF/maven/de.hdm_stuttgart.mi.sd1/helper/
inflating: META-INF/maven/de.hdm_stuttgart.mi.sd1/helper/pom.xml
inflating: META-INF/maven/de.hdm_stuttgart.mi.sd1/helper/pom.properties
<project ...> ... <groupId>de.hdm_stuttgart.mi.sd1</groupId> <artifactId>lottery</artifactId> <version>0.9</version> <packaging>jar</packaging> <name>lottery</name> <dependencies> <dependency> <groupId>de.hdm_stuttgart.mi.sd1</groupId> <artifactId>helper</artifactId> <version>0.9</version> </dependency> ... </project>
static public long binomial(int n, int k) { return (Helper.factorial(n) / Helper.factorial(k) / Helper.factorial(n - k)); } public static void main(String[] args) { System.out.println("There are " + binomial(5, 2) + " ways to draw 2 out of 5 numbers"); System.out.println("There are " + binomial(49, 6) + " ways to draw 6 out of 49 numbers"); }
No. 113
Cancelling fractions
This exercise requires the import of the previous Maven based exercise Finding the greatest common divisor of two integer values . The import may be effected by:
-
Creating a local Maven jar archive export by executing “mvn
install
” in project Finding the greatest common divisor of two integer values at the command line. Alternatively you may right click on your pom.xml file in Eclipse hitting “Run as Maven build” usinginstall
as goal. -
Defining Finding the greatest common divisor of two integer values as a dependency ❶ in your current project:
<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>fraction</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>fraction</name> ... <dependencies> <dependency> ❶ <groupId>de.hdm-stuttgart.de.sd1</groupId> <artifactId>gcd</artifactId> <version>1.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> ... </dependency> </dependencies> </project>
Q: |
We have implemented GCD computation in
Finding the greatest common divisor of two integer
values . The current exercises idea is to
implement cancelling of fractions by using the method
Test your results. |
A: |
Modifying the constructor is straightforward: On creating a fraction we simply divide both numerator and denominator by the GCD value:
Its tempting to implement
This is however too shortsighted. Consider the example
. Our simple implementation proposal would
call We may instead transform the term in question by
exchanging the numerators like
to enable cancelling prior to multiplying. Now the call
Similar reflections lead to the clue decomposing the
denominators when implementing
See complete implementation here. We may re-use out test:
|
- CDN
- Company local
No. 114
Dealing with local Maven dependencies
Q: |
This exercise is actually a preparation for ongoing exercises relying on importing local Maven artifacts. Follow this section's description and create two projects A and B. Project B shall import some implementation from project A as being previously described. If you lack an idea you may just use the given Lottery project using the Helper project. |