Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
13bbf199
Commit
13bbf199
authored
11 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
void methods returning nothing
parent
84726fd1
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sw1/Fig/methVoidVoid.dia
+0
-0
0 additions, 0 deletions
Sw1/Fig/methVoidVoid.dia
Sw1/sw1.xml
+83
-55
83 additions, 55 deletions
Sw1/sw1.xml
with
83 additions
and
55 deletions
Sw1/Fig/methVoidVoid.dia
0 → 100644
+
0
−
0
View file @
13bbf199
File added
This diff is collapsed.
Click to expand it.
Sw1/sw1.xml
+
83
−
55
View file @
13bbf199
...
...
@@ -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
confus
ed
.
</para>
confus
ing
.
</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 --
>
.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 --
>
.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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment