Skip to content
Snippets Groups Projects
Commit 13bbf199 authored by Goik Martin's avatar Goik Martin
Browse files

void methods returning nothing

parent 84726fd1
No related branches found
No related tags found
No related merge requests found
File added
......@@ -149,7 +149,7 @@
#1</link> to <link xlink:href="???"><link
xlink:href="http://blogs.kent.ac.uk/mik/2012/07/joc-33/">Joc
#33</link></link>. The order of items is somewhat
confused.</para>
confusing.</para>
</listitem>
<listitem>
......@@ -173,7 +173,8 @@
</section>
<section xml:id="sd1StartGreenfoot">
<title>Working with <productname>Greenfoot</productname></title>
<title><abbrev>GF</abbrev> book 1. : Getting to know
<productname>Greenfoot</productname></title>
<para>Quote from <xref linkend="bibKoelling2010"/>, p. 3:</para>
......@@ -183,59 +184,71 @@
open on your screen and the book open on your desk.</para>
</blockquote>
<itemizedlist>
<listitem>
<para>Compilation: .java --&gt; .class</para>
</listitem>
<listitem>
<para>Classes and objects</para>
</listitem>
<listitem>
<para/>
</listitem>
</itemizedlist>
<qandaset>
<title>Automatic initialization on startup</title>
<qandadiv>
<qandaentry>
<question>
<para>The <quote>leaves and wombats</quote> scenario does not
supply leaves and wombats automatically when starting the game.
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 suggests that this method may 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 discover that this is a so called
constructor. As the name suggests a constructor is being
executed whenever a new instance of a class is being
created.</para>
</question>
<answer>
<para>To achieve the desired behaviour we have to call the
method <methodname>populate()</methodname> <coref
linkend="sw1CallPopulate"/> from within the constructor's
code:</para>
<programlisting>public class WombatWorld {
public WombatWorld() {
<section xml:id="sw1ObjClass">
<title>GF 1.1: Classes an objects</title>
<itemizedlist>
<listitem>
<para>Compilation: .java --&gt; .class</para>
</listitem>
<listitem>
<para>Classes and objects</para>
</listitem>
<listitem>
<para>Methods</para>
<figure xml:id="sw1FigMethodVoidVoid">
<title/>
<mediaobject>
<imageobject>
<imagedata fileref="Fig/methVoidVoid.svg"/>
</imageobject>
</mediaobject>
</figure>
</listitem>
</itemizedlist>
<qandaset>
<title>Automatic initialization on startup</title>
<qandadiv>
<qandaentry>
<question>
<para>The <quote>leaves and wombats</quote> scenario does not
create leaves and wombats automatically on startup. 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 each time 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
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 is being created</emphasis>
super(8, 8, 60);
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.
......@@ -244,12 +257,27 @@
Wombat w1 = new Wombat();
addObject(w1, 3, 3);
...</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<para/>
</section>
<section xml:id="sw1InteractClasss">
<title><abbrev>GF</abbrev> 1.3: Interacting with objects</title>
<para/>
</section>
</section>
</chapter>
<chapter xml:id="ideas">
<title>Ideas</title>
<para/>
</chapter>
<xi:include href="bibliography.xml" xpointer="element(/1)"/>
</book>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment