From eb7906fdc92d1fe81b2f1ed5d79886a71a97a49f Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Tue, 1 Apr 2014 07:17:27 +0200 Subject: [PATCH] adding lastAnswerId mechanism --- Sd1/Makefile | 7 +- Sd1/swd1.xml | 644 +++++++++++++++++++++++---------------------------- 2 files changed, 297 insertions(+), 354 deletions(-) diff --git a/Sd1/Makefile b/Sd1/Makefile index bdfbc8fcd..9f35b41b4 100644 --- a/Sd1/Makefile +++ b/Sd1/Makefile @@ -30,13 +30,14 @@ jdoc: `find Jdbc -name \*.java|grep -v \.metadata` `find Hibintro -name \*.java|grep -v \.metadata` -${BUILDNAVI}/index.html ${BUILDNAVI}/printversion.pdf:${SCRIPTBASENAME}.xml +${BUILDNAVI}/index.html ${BUILDNAVI}/printversion.pdf:${SCRIPTBASENAME}.xml lastAnswerId.xml rm -rf $(BUILDBASE) || true mkdir -p ${BUILDNAVI} ${BUILDNONAVI} #cd Ref/Fig;make all - /usr/share/xmlmind/bin/convertdoc -p eclipse.plugin.id $(PLUGINID) ${PPARAM} db5.toEclipseHelp ${SCRIPTBASENAME}.xml -u $(BUILDNAVI) + saxon9he-xslt -xi -s:${SCRIPTBASENAME}.xml -xsl:../ws/Docbook/Extensions/Xsl/docbook2docbook.xsl > ${SCRIPTBASENAME}.answer.xml + /usr/share/xmlmind/bin/convertdoc -p eclipse.plugin.id $(PLUGINID) ${PPARAM} db5.toEclipseHelp ${SCRIPTBASENAME}.answer.xml -u $(BUILDNAVI) perl -i -p -e '$$/=undef;s/\n[ \t]*<!DOCTYPE[^>]+\n[^>]*>$$//m' ${BUILDNAVI}/*.xml ${BUILDNONAVI}/*.xml - /usr/share/xmlmind/bin/convertdoc db5.toPSFile ${SCRIPTBASENAME}.xml \ + /usr/share/xmlmind/bin/convertdoc db5.toPSFile ${SCRIPTBASENAME}.answer.xml \ -s pdf -s "|pdf" -u $(BUILDNAVI)/printversion.pdf # j2h -d ${BUILDNAVI}/src/classes -js ~/workspace/StructuredData/ws/eclipse/Jdbc/src \ -jd /usr/share/doc/openjdk-7-doc/api http://java.sun.com/javase/7/docs/api diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index 3913bad8c..3aad882f9 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -161,7 +161,7 @@ </chapter> <chapter xml:id="sd1IntroGreenfoot"> - <title>Lecture 2, Introduction to <link + <title>Lecture 2 - Introduction to <link xlink:href="http://www.greenfoot.org">Greenfoot</link></title> <section xml:id="swd1IntrGreePrepare"> @@ -296,132 +296,140 @@ by sitting at your computer with <productname>Greenfoot</productname> open on your screen and the book open on your desk.</para> </blockquote> + </section> + </chapter> - <section xml:id="sw1ObjClass"> - <title>GF 1.1: Classes an objects</title> + <chapter xml:id="sd1CrabExample"> + <title>Lecture 3 - Wombats and crabs</title> - <itemizedlist> - <listitem> - <para>Compilation: .java --> .class</para> - </listitem> + <section xml:id="sw1ObjClass"> + <title>GF 1.1: Classes an objects</title> - <listitem> - <para>Classes and objects</para> - </listitem> + <itemizedlist> + <listitem> + <para>Compilation: .java --> .class</para> + </listitem> - <listitem> - <para>Methods</para> + <listitem> + <para>Classes and objects</para> + </listitem> - <figure xml:id="sw1FigMethodVoidVoid"> - <title>A method named <code>move</code> having no arguments - returning nothing.</title> + <listitem> + <para>Methods</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/turnLeft.fig"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sw1FigMethodVoidVoid"> + <title>A method named <code>move</code> having no arguments + returning nothing.</title> - <figure xml:id="sd1returnIntVoid"> - <title>A method named <methodname>getLeavesEaten</methodname> - having no arguments returning an integer value</title> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/turnLeft.fig"/> + </imageobject> + </mediaobject> + </figure> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/returnValue.fig"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1returnIntVoid"> + <title>A method named <methodname>getLeavesEaten</methodname> + having no arguments returning an integer value</title> - <figure xml:id="sd1setDirection"> - <title>A method named <methodname>setDirection</methodname> - setting an object's direction of movement returning no - value.</title> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/returnValue.fig"/> + </imageobject> + </mediaobject> + </figure> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/setDirection.fig"/> - </imageobject> - </mediaobject> - </figure> + <figure xml:id="sd1setDirection"> + <title>A method named <methodname>setDirection</methodname> + setting an object's direction of movement returning no + value.</title> - <figure xml:id="sd1getAverageSpeed"> - <title>A method named <methodname>getAveragespeed</methodname> - having two arguments distance in kilometers and time in minutes - returning the average speed as kilometers per hour</title> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/setDirection.fig"/> + </imageobject> + </mediaobject> + </figure> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/getAverageSpeed.fig"/> - </imageobject> - </mediaobject> - </figure> - </listitem> + <figure xml:id="sd1getAverageSpeed"> + <title>A method named <methodname>getAveragespeed</methodname> + having two arguments distance in kilometers and time in minutes + returning the average speed as kilometers per hour</title> + + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/getAverageSpeed.fig"/> + </imageobject> + </mediaobject> + </figure> + </listitem> - <listitem> - <para>Command like methods may change an object's internal - state.</para> - </listitem> + <listitem> + <para>Command like methods may change an object's internal + state.</para> + </listitem> - <listitem> - <para>Inspecting internal states of objects in Greenfoot.</para> - </listitem> + <listitem> + <para>Inspecting internal states of objects in + <productname>Greenfoot</productname>.</para> + </listitem> - <listitem> - <para>Building models: Abstraction of real world entities</para> + <listitem> + <para>Building models: Abstraction of real world entities</para> - <itemizedlist> - <listitem> - <para>Books</para> - </listitem> + <itemizedlist> + <listitem> + <para>Books</para> + </listitem> - <listitem> - <para>Humans</para> - </listitem> - </itemizedlist> - </listitem> - </itemizedlist> - - <qandaset> - <title>Automatic initialization on startup</title> - - <qandadiv> - <qandaentry> - <question> - <para>On startup the <quote>leaves and wombats</quote> - scenario does not create leaves and wombats automatically. - Open the <classname>WombatWorld</classname> class (right click - <quote>Open editor</quote>). You will discover a method being - declared as <methodname>public void - populate()</methodname>.</para> - - <para>The name already suggests that this method may be used - to populate the game's world (with wombats and leaves) on - startup. Modify the code to achieve this behaviour. Hint: - Whenever a new instance of <classname>WombatWorld</classname> - is being created the method <methodname>public - WombatWorld()</methodname> is being executed.</para> - - <para>We will later explain the meaning of<methodname> public - WombatWorld()</methodname> being a so called constructor. As - the name suggests a constructor is being executed whenever a - new instance of a given class gets created.</para> - </question> - - <answer> - <para>Calling the method <methodname>populate()</methodname> - <coref linkend="sw1CallPopulate"/> from within the - <classname>WombatWorld</classname> constructor's code achieves - the desired behaviour:</para> - - <programlisting>public class WombatWorld { + <listitem> + <para>Humans</para> + </listitem> + </itemizedlist> + </listitem> + </itemizedlist> + + <qandaset> + <title>Automatic initialization on startup</title> + + <qandadiv> + <qandaentry> + <question> + <para>On startup the <quote>leaves and wombats</quote> scenario + does not create leaves and wombats automatically. Open the + <classname>WombatWorld</classname> class (right click + <quote>Open editor</quote>). You will discover a method being + declared as <methodname>public void + populate()</methodname>.</para> + + <para>The name already suggests that this method may be used to + create wombats and leaves <emphasis>automatically</emphasis> on + startup. Modify the code accordingly to achieve this + behaviour.</para> + + <para>Hint: Whenever a new instance of + <classname>WombatWorld</classname> is being created the method + <methodname>public WombatWorld()</methodname> is being executed. + We will later explain the meaning of<methodname> public + WombatWorld()</methodname> being a so called + <emphasis>constructor</emphasis>. As the name suggests a + constructor is being executed whenever a new instance of a given + class is being created. </para> + </question> + + <answer> + <para>Calling the method <methodname>populate()</methodname> + <coref linkend="sw1CallPopulate"/> from within the + <classname>WombatWorld</classname> constructor's code achieves + the desired behaviour:</para> + + <programlisting>public class WombatWorld { public WombatWorld() { <emphasis role="bold">// Constructor will be called each time a WombatWorld</emphasis> super(8, 8, 60); <emphasis role="bold">// is being created</emphasis> setBackground("cell.jpg"); setPaintOrder(Wombat.class, Leaf.class); <emphasis role="bold">populate()</emphasis>; <co - xml:id="sw1CallPopulate"/> + xml:id="sw1CallPopulate"/> } /** * Populate the world with a fixed scenario of wombats and leaves. @@ -430,207 +438,140 @@ Wombat w1 = new Wombat(); addObject(w1, 3, 3); ...</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + + <qandaset> + <title>Representation of directions</title> - <qandaset> - <title>Representation of directions</title> + <qandadiv> + <qandaentry> + <question> + <para>Our Wombat class does have a method + <methodname>setDirection(int direction)</methodname>. This + allows for setting the direction a wombat is going to move. Call + it inserting different values by right clicking on a wombat + instance. How do entered values relate to directions of a + wombat's movement? You may evaluate the result by actually + watching its moving direction. Is there another way?</para> - <qandadiv> - <qandaentry> - <question> - <para>Our Wombat class does have a method - <methodname>setDirection(int direction)</methodname>. Call it - inserting different values by right clicking on a wombat - instance. How do entered values relate to directions of a - wombat's movement? You may evaluate the result by actually - watching its moving direction. Is there another way?</para> - </question> + <para>Hint: The <productname>Greenfoot</productname> framework + allows you to inspect an object's internal attribute + values.</para> + </question> - <answer> - <para>Only integer values from {0,1,2,3} relate to - directions:</para> + <answer> + <para>Only integer values from {0,1,2,3} relate to + directions:</para> - <informaltable border="1" width="40%"> - <colgroup width="45%"/> + <informaltable border="1" width="40%"> + <colgroup width="45%"/> - <colgroup width="55%"/> + <colgroup width="55%"/> - <tr> - <th>Integer value</th> + <tr> + <th>Integer value</th> - <th>Direction</th> - </tr> + <th>Direction</th> + </tr> - <tr> - <td>0</td> + <tr> + <td>0</td> - <td>East</td> - </tr> + <td>East</td> + </tr> - <tr> - <td>1</td> + <tr> + <td>1</td> - <td>West</td> - </tr> + <td>West</td> + </tr> - <tr> - <td>2</td> + <tr> + <td>2</td> - <td>North</td> - </tr> + <td>North</td> + </tr> - <tr> - <td>3</td> + <tr> + <td>3</td> - <td>South</td> - </tr> + <td>South</td> + </tr> - <tr> - <td>other</td> + <tr> + <td>other</td> - <td>No effect</td> - </tr> - </informaltable> + <td>No effect</td> + </tr> + </informaltable> - <para>We may right click on a wombat to inspect its internal - state as in the following screenshot:</para> + <para>We may right click on a wombat to inspect its internal + state as in the following screenshot:</para> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/wombatDirection.png"/> - </imageobject> - </mediaobject> - </answer> - </qandaentry> - </qandadiv> - </qandaset> - </section> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/wombatDirection.png"/> + </imageobject> + </mediaobject> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </section> - </chapter> - <chapter xml:id="sd1CrabExample"> - <title>Gf 2. The first program: Little crab</title> - - <itemizedlist> - <listitem> - <para>Inheritance</para> - </listitem> - - <listitem> - <para>assignments</para> - </listitem> - - <listitem> - <para>logical expressions / booleans / conditional execution</para> - </listitem> - - <listitem> - <para>if statements evaluate boolean conditions. Variants:</para> - - <itemizedlist> - <listitem> - <para>complex boolean conditions</para> - </listitem> - - <listitem> - <para>Nested if statements</para> - </listitem> - - <listitem> - <para>possible else branch</para> - </listitem> - </itemizedlist> - </listitem> - - <listitem> - <para>Replacing expressions in <code>if</code> clauses by single - <code>boolean</code> values.</para> - </listitem> - - <listitem> - <para>Code conventions:</para> - - <itemizedlist> - <listitem> - <para>Indentation</para> - </listitem> - - <listitem> - <para>Capital/small letter conventions</para> - </listitem> - - <listitem> - <para><code>snake_case</code> vs. <code>CamelCase</code>.</para> - </listitem> - - <listitem> - <para>Comment formats <code>// this is a comment</code> and - <code>/* I like comments! */</code>.</para> - </listitem> - - <listitem> - <para>Comment conventions / Comments and - <productname>Javadoc</productname></para> - </listitem> - </itemizedlist> - </listitem> - - <listitem> - <para>Repeating object inspection in - <productname>Greenfoot</productname>.</para> - </listitem> - </itemizedlist> - - <qandaset xml:id="sd1QandCrabStatesIf"> - <title>Adding complex behaviour</title> + <section xml:id="sd1EnhanceCrabMove"> + <title>Enhancing crabs' movements</title> - <qandadiv> - <qandaentry> - <question> - <para>After finishing <productname>Greenfoot</productname> - exercise 2.14 you are asked to change a crab's behaviour with - respect to edges. Instead of turning by a constant angle when - hitting an edge our crab shall:</para> + <qandaset xml:id="sd1QandCrabStatesIf"> + <title>Adding complex behaviour</title> - <itemizedlist> - <listitem> - <para>Turn by 5 degrees when hitting an edge for the first - time</para> - </listitem> + <qandadiv> + <qandaentry> + <question> + <para>After finishing <productname>Greenfoot</productname> + exercise 2.14 you are asked to change a crab's behaviour with + respect to edges. Instead of turning by a constant angle when + hitting an edge our crab shall:</para> - <listitem> - <para>Turn by 15 degrees when hitting an edge for the second - time</para> - </listitem> + <itemizedlist> + <listitem> + <para>Turn by 5 degrees when hitting an edge for the first + time</para> + </listitem> - <listitem> - <para>Turn by 20 degrees when hitting an edge for the third - time</para> - </listitem> + <listitem> + <para>Turn by 15 degrees when hitting an edge for the second + time</para> + </listitem> - <listitem> - <para>Restart this sequence with 5 degrees again.</para> - </listitem> - </itemizedlist> + <listitem> + <para>Turn by 20 degrees when hitting an edge for the third + time</para> + </listitem> - <para>The above sequence may be characterized by letting our crabs - cycle through the following three internal states:</para> + <listitem> + <para>Restart this sequence with 5 degrees again.</para> + </listitem> + </itemizedlist> - <mediaobject> - <imageobject> - <imagedata fileref="Ref/Fig/turningStates.fig"/> - </imageobject> - </mediaobject> + <para>The above sequence may be characterized by letting our + crabs cycle through the following three internal states:</para> - <para>In order to achieve this behaviour you have to keep track of - a crab's internal state. This may be achieved by introducing a new - attribute <code>nextTurningAngle</code> of type - <code>int</code>:</para> + <mediaobject> + <imageobject> + <imagedata fileref="Ref/Fig/turningStates.fig"/> + </imageobject> + </mediaobject> - <programlisting>public class Crab extends Animal { + <para>In order to achieve this behaviour you have to keep track + of a crab's internal state. This may be achieved by introducing + a new attribute <code>nextTurningAngle</code> of type + <code>int</code>:</para> + + <programlisting>public class Crab extends Animal { <emphasis role="bold">int nexturningAngle = 5;</emphasis> // Initial value @@ -643,21 +584,21 @@ } }</programlisting> - <para>Though not strictly required your code will become cleaner - when using multiple <code>if ... else</code> if statements. Note - also that <code>if</code> statements may be nested.</para> - </question> - - <answer> - <para>The desired behaviour may be achieved by introducing a - nested if clause <coref linkend="sd1ExtendCrabStates"/> within the - given if clause testing for reaching our world's end. We also - introduce a variable <code>nextTurningAngle</code> <coref - linkend="sd1VarCrabState"/> of integer type to keep track of each - crab's current state and initialize its value to the first of our - three states:</para> - - <programlisting>public class Crab extends Animal { + <para>Though not strictly required your code will become cleaner + when using multiple <code>if ... else</code> if statements. Note + also that <code>if</code> statements may be nested.</para> + </question> + + <answer> + <para>The desired behaviour may be achieved by introducing a + nested if clause <coref linkend="sd1ExtendCrabStates"/> within + the given if clause testing for reaching our world's end. We + also introduce a variable <code>nextTurningAngle</code> <coref + linkend="sd1VarCrabState"/> of integer type to keep track of + each crab's current state and initialize its value to the first + of our three states:</para> + + <programlisting>public class Crab extends Animal { int nextTurningAngle = 5; <co xml:id="sd1VarCrabState"/> // Crabs start in state 1 @@ -678,70 +619,70 @@ } }</programlisting> - <para>Although the above code satisfies our demands completely it - suffers from the following flaws:<glosslist> - <glossentry> - <glossterm>Readability</glossterm> - - <glossdef> - <para>Without further explanation this code is difficult - to understand. The connection between these three numeric - values is not obvious.</para> - </glossdef> - </glossentry> - - <glossentry> - <glossterm>Maintainability</glossterm> - - <glossdef> - <para>Changing the underlying three directions to a new - set of different values (i.e. 7, 42, 13) in a consistent - manner is error prone: For (nearly) each value we will - have to change both the if clause and the corresponding - assignment.</para> - </glossdef> - </glossentry> - </glosslist></para> - - <para>We may improve both our code's readability and - maintainability by introducing three integer constants - corresponding to our crabs distinct states:</para> - - <programlisting>public class Crab extends Animal { + <para>Although the above code satisfies our demands completely + it suffers from the following flaws:<glosslist> + <glossentry> + <glossterm>Readability</glossterm> + + <glossdef> + <para>Without further explanation this code is difficult + to understand. The connection between these three + numeric values is not obvious.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Maintainability</glossterm> + + <glossdef> + <para>Changing the underlying three directions to a new + set of different values (i.e. 7, 42, 13) in a consistent + manner is error prone: For (nearly) each value we will + have to change both the if clause and the corresponding + assignment.</para> + </glossdef> + </glossentry> + </glosslist></para> + + <para>We may improve both our code's readability and + maintainability by introducing three integer constants + corresponding to our crabs distinct states:</para> + + <programlisting>public class Crab extends Animal { // These definitions are not strictly required but do help // understanding the code final <co linkends="sd1CrabStateConstants-1" - xml:id="sd1CrabStateConstants-1-co"/> static <co - linkends="sd1CrabStateConstants-2" - xml:id="sd1CrabStateConstants-2-co"/> int state1 = 5, + xml:id="sd1CrabStateConstants-1-co"/> static <co + linkends="sd1CrabStateConstants-2" + xml:id="sd1CrabStateConstants-2-co"/> int state1 = 5, state2 = 15, state3 = 20; ...</programlisting> - <calloutlist> - <callout arearefs="sd1CrabStateConstants-1-co" - xml:id="sd1CrabStateConstants-1"> - <para>The Java keyword <code>final</code> asserts that all - following value assignments must not be changed subsequently. - So a later assignment attempt like e.g. <code>state2 = - 1000;</code> will be flagged as an (compile time) error: We - want these values to be constant!</para> - </callout> - - <callout arearefs="sd1CrabStateConstants-2-co" - xml:id="sd1CrabStateConstants-2"> - <para>The Java keyword <code>static</code> instructs the - compiler to share these three values among all possible crab - instances: They are being defined on class level rather than - on object (instant) level. Each crab may be i a different - state but there are only three possible state values</para> - </callout> - </calloutlist> - - <para>Our complete implementation now reads:</para> - - <programlisting>public class Crab extends Animal { + <calloutlist> + <callout arearefs="sd1CrabStateConstants-1-co" + xml:id="sd1CrabStateConstants-1"> + <para>The Java keyword <code>final</code> asserts that all + following value assignments must not be changed + subsequently. So a later assignment attempt like e.g. + <code>state2 = 1000;</code> will be flagged as an (compile + time) error: We want these values to be constant!</para> + </callout> + + <callout arearefs="sd1CrabStateConstants-2-co" + xml:id="sd1CrabStateConstants-2"> + <para>The Java keyword <code>static</code> instructs the + compiler to share these three values among all possible crab + instances: They are being defined on class level rather than + on object (instant) level. Each crab may be i a different + state but there are only three possible state values</para> + </callout> + </calloutlist> + + <para>Our complete implementation now reads:</para> + + <programlisting>public class Crab extends Animal { // A crab is in exactly one of these states: final static int state1 = 5, @@ -766,10 +707,11 @@ move(); } }</programlisting> - </answer> - </qandaentry> - </qandadiv> - </qandaset> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> <section xml:id="sw1Gf3.1"> <title><productname>Greenfoot</productname> 3.1, <quote>Adding random -- GitLab