Transcript Document

Greenfoot
Computer programming
Lesson 1
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are taken
from http://www.greenfoot.org/doc/ so credit to them too!
Objectives
• Understand what programming is and its
purpose.
• Develop awareness of basic programming
skills and concepts.
• Create a simple program in Greenfoot.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
What is a program?
• A program is a step-by-step set of instructions
that tells a computer how to do something.
• A more geeky term is “algorithm” which is a
step-by-step description of how to accomplish
some task.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
7 key points
1.
2.
3.
4.
5.
6.
7.
One of the first differences you have to understand between giving a set
of instructions for a person and one for the computer, is that the
computer is an empty box that does nothing w/o programs or
instructions.
A computer has little capacity to interpret things – it has to be told
directly.
The instructions you give to them have to be very precise.
Computers do exactly what you tell them to do, nothing more, nothing
less (its not the computers fault you keep dying / losing / crashing, its
you!).
When you tell a computer to do something you also have to break it
down into steps that it can understand .
The only steps a computer really understands are steps that are very
small and exact.
This means that in order to write programs we don’t use English – in this
instance we use a language called JAVA.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Why bother to learn to program?
• Nearly every activity you do in your daily life
involves a computer, whether you realize it or
not. When you think about computers in your
life, you might only think of your desktop or
laptop. However, your phone, iPod, and every
other electronic gadget you might own these
days is really a digital computer.
Quick challenge: List on paper how many computers you come
across or use in a normal day.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Why bother to learn to program?
• Learning how to program is a great
way for anyone to improve your
problem solving capabilities (A bit
like when your stuck in a game or
trying to plan out the fastest route
to somewhere).
• Most programs solve a type of
problem for us. The first step in any
programming task is figuring out
how to solve the problem yourself in
sufficient detail that you can give
the instructions to a computer.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Greenfoot Chapter One Scenario
• After copying the Greenfoot Book Scenarios to your network
folder, we will open chapter01/leaves-and-wombats ->double
click on the icon for Greenfoot to execute the project.
1. When the program
opens click on the
COMPILE button.
•
We need to right-click on the Wombat Class to add an object to the
world. Your instructor will show you how to add the Wombat to the
World.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Greenfoot Chapter One Scenario
• \
• We will go through Chapter One
and discuss terms and concepts.
We will add a
Wombat to our
world. We click
RUN to make the
Wombat move!
The Greenfoot interface
The World
The Classes
Execution controls
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Lets make a simple world
1.Open Greenfoot and choose “create a new scenario”.
2. Name your game and save into
your workspace
3. Right click on “world” and choose “new subclass”
4. Choose the
background
“sand.jpg. Then give it
a suitable name.
5. Click on “compile
to see your new
world.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Now lets add an object.
1.Right click
on Actor and
choose New
subclass.
2. Under animals choose “ant3” and name it
“Ant” (at the top!).
3.Click on the Any object. Now
HOLD SHIFT and click in your world
to place the ant.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Lets move our object.
• You have noticed if we press RUN nothing happens –
that’s because we haven't told our ant to do anything
(stupid ant is a computer and can’t think for itself)
RIGHT CLICK on the Ant object
and choose the “open editor”
option. This is where all the fun
begins! This is what is known as
the code.
Lets move onto making our Ant
(object move) move.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Adding code
• Add the following code into your Ant.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
• Click on compile and place your ant into the
world again. Now look what happens when you
click run.
• You can change the speed of the ant by changing
the number 4 in the code to a different number.
Higher makes it faster, lower makes the ant
slower -- see if you can guess what happens with
a negative number. Then try it!
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Some points to note when coding
• You have noticed you must precisely match what
is written in the example.
• Common mistakes include:
–
–
–
–
–
capitalising the m
Capitalisation matters in Java!
Missing the semi-colon;;;;;;
using the wrong brackets (more in a second!)
or accidentally deleting the curly brackets. If you get
an error look for one of these errors that you might
have made when copying the code.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Some points on brackets.
• A quick note about brackets. There are three main
bracket types used in Java; let's see some blown-up
images of them:
• Round brackets, known in the USA as parentheses,
but in the UK simply as brackets. In Java, they are
used in mathematical expressions, and to surround
the lists for method calls (we'll get to those shortly).
• Square brackets, known in the USA simply as
brackets (see where confusion can arise?). In Java,
they are used for arrays.
• Curly brackets, also known as braces or squiggly
brackets. In Java, these are used to surround blocks
of code, such as methods or the contents of classes.
()
[]
{}
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!
Changing the code
• Now see what happens if you change the code
to the following:
Ok – finally lets try this…
Objectives
• Understand what programming is and its
purpose.
• Develop awareness of basic programming
skills and concepts.
• Create a simple programme in Greenfoot.
Created by NW 2012 – please note all copyright on images used is property of copyright holder. Note: some of the more complicated descriptions are
taken from http://www.greenfoot.org/doc/ so credit to them too!