From e1cc454a9014461f877e2690ca5ec820c0dc171e Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Mon, 22 Jun 2015 08:09:35 +0200 Subject: [PATCH] Completing exercise documentation --- Doc/Sd1/interfacesAbstractClasses.xml | 58 ++++++++++++++++--- .../mi/sd1/plot/DriverInterface.java | 3 +- .../mi/sd1/plot/DriverLambda.java | 3 +- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Doc/Sd1/interfacesAbstractClasses.xml b/Doc/Sd1/interfacesAbstractClasses.xml index 9af805742..5dcd0edc1 100644 --- a/Doc/Sd1/interfacesAbstractClasses.xml +++ b/Doc/Sd1/interfacesAbstractClasses.xml @@ -136,18 +136,60 @@ public void testApp() { }</programlisting> <para>This interface may be used by the plotter class as a - <quote>placeholder</quote> for the intended plot function to be - defined as e.g.:</para> + <quote>placeholder</quote> for the intended plot function. + Plotting e.g. <function>y=sin(x)</function> will then be effected + by:</para> + + <programlisting language="none">class MySin implements DoubleOfDoubleFunction { + + @Override + public double compute(double x) { + return Math.sin(x); + } +}</programlisting> + + <para>Plotting will then require an instance:</para> + + <programlisting language="none">final DoubleOfDoubleFunction sinFunction = new MySin(); +plotter.plot(sinFunction);</programlisting> </question> <answer> - <para/> + <annotation role="make"> + <para role="eclipse">Sd1/plot/Basic</para> + + <para>A <quote>standard</quote> plotting example is being + provided in class <classname>DriverInterface</classname>.</para> + </annotation> - <para>The above solution contains a variant using Java 8 Lambda - expressions which allows for supplying functions as arguments to - the plotting facility. This solution will not be covered in the - current lecture but you may catch a glimpse of upcoming topics in - <quote xml:lang="de">Softwareentwicklung 2</quote>.</para> + <para>The solution in addition contains a variant + <classname>DriverLambda</classname> using <link + xlink:href="http://tutorials.jenkov.com/java/lambda-expressions.html">Java + 8 lambda expressions</link> which allows for supplying functions + as arguments to the plotting facility. This solution will not be + covered in the current lecture but you may catch a glimpse with + respect to upcoming topics in <quote + xml:lang="de">Softwareentwicklung 2</quote>:</para> + + <programlisting language="none">public class DriverLambda { + + /** + * @param args Unused + */ + public static void main( String[] args ) { + final Plotter plotter = new Plotter(); + + plotter.setNumTics(80, 40);// 80 characters vertical, 40 characters horizontal + plotter.setXrange(0, 2 * Math.PI); + plotter.setYrange(-1, 1); + + // Function implementing the underlying interface + // de.hdm_stuttgart.mi.sd1.plot.DoubleOfDoubleFunction + // are being conveniently passed as arguments. + plotter.plot(<emphasis role="bold">x -> Math.sin(x)</emphasis>); + plotter.plot(<emphasis role="bold">x -> Math.cos(x)</emphasis>); + } +}</programlisting> </answer> </qandaentry> </qandadiv> diff --git a/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverInterface.java b/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverInterface.java index 97616c1cf..7d0fe9e31 100644 --- a/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverInterface.java +++ b/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverInterface.java @@ -25,8 +25,7 @@ public class DriverInterface { plotter.setXrange(0, 2 * Math.PI); plotter.setYrange(-1, 1); - DoubleOfDoubleFunction sinFunction = new MySin(); - + final DoubleOfDoubleFunction sinFunction = new MySin(); plotter.plot(sinFunction); } } diff --git a/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverLambda.java b/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverLambda.java index 23942425b..8089a4815 100644 --- a/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverLambda.java +++ b/P/Sd1/plot/Interface/src/main/java/de/hdm_stuttgart/mi/sd1/plot/DriverLambda.java @@ -17,7 +17,8 @@ public class DriverLambda { plotter.setXrange(0, 2 * Math.PI); plotter.setYrange(-1, 1); - // Function implementing the underlying interface + // Function implementing the underlying interface + // de.hdm_stuttgart.mi.sd1.plot.DoubleOfDoubleFunction // are being conveniently passed as arguments. plotter.plot(x -> Math.sin(x)); plotter.plot(x -> Math.cos(x)); -- GitLab