Sie sind auf Seite 1von 36

Using Methods, Variables and Parameters

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

What Will I Learn?


Objectives
Define parameters and how they are used in methods
Understand the concept of inheritance
Describe properties of an object
Describe the purpose of a variable
Describe programming concepts and define
terminology

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Why Learn It?


Purpose
Imagine that youve learned to
cook and now you want to start
creating your own recipes. You
may start with a recipe and
then modify it to make it your
own.
Java methods will provide you
a means for creating and
enhancing your own games.
Variables will give you the
flexibility to store values and
make decisions on those
values.
Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Methods Example
In order to complete a task, such as math homework, there
are several subtasks. For example:
Student completes the math homework.
Student walks to school to turn in the homework to the
teacher.
Teacher grades the homework.
Because we are human (and because of learned
experiences from our teachers), we are programmed to
know how to complete these tasks.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Methods
In programming, each object has a set of operations, or
tasks it can perform, based on its class.
A programmer needs to write a program that tells the object
how and when to perform those tasks, such as:
Command an object to perform an action
Ask an object a question to learn more about what it
does
Methods are a set of operations or tasks that instances of a class can perform.
When a method is invoked, it will perform the operation or task specified in the
source code.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Display an Object's Methods


The object menu displays all of the methods that the
instance knows how to perform.
To display the object menu, right click on the instance.
Inherited From Actor means that the subclass inherits
all of the methods from the Actor superclass.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Inheritance
Like animals, objects inherit the characteristics of their
subclass and superclass. A Siamese cat inherits two types
of characteristics:
Characteristics of the cat class: four legs, two eyes,
fur, and the ability to meow
Characteristics of the Siamese cat subclass: slender
body frame, tan fur color, green eyes, etc.
Inheritance means that each subclass inherits the methods from its superclass.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Viewing Methods in the Code Editor


Follow these steps to view the methods of a class:
1. Right click on a class.
2. Click Open Editor.
3. In the code editor, select Documentation from the dropdown menu.
4. Scroll down to the Method Summary.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Method Summary
The method summary displays inherited methods for the
object.

Copyright 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Method Components
Methods have several components that describe the
operations or tasks that can be performed.
Methods start with a return type and end with a
parentheses, which tells us what information goes into the
method call (command) and what data comes back from it.
Examples:
void move()
void turnLeft()
A method call instructs the instance to perform an operation or task. You can read
method to understand what operation or task is to be performed.

Copyright 2012, Oracle. All rights reserved.

10

Using Methods, Variables and Parameters

Method Signature
A method signature describes the intentions of the method.
It includes:
Return type
Method name
Parameter list
Review the method signature in the object menu.
Return Type

void move (int)


Parameter List
Method Name

Copyright 2012, Oracle. All rights reserved.

11

Using Methods, Variables and Parameters

Return Types
The return type is the word at the beginning of the method
that tells us what information a method call will return.
There are two types of return types:
Void (commands)
Non-void (questions)

Copyright 2012, Oracle. All rights reserved.

12

Using Methods, Variables and Parameters

Void Return Types


Void return types:
Are methods that include the word void
Issue a command that carries out an action
Do not return information about the object
Are used to make the object do something

Copyright 2012, Oracle. All rights reserved.

13

Using Methods, Variables and Parameters

Invoking Methods with Void Return Types


Invoke methods with void return types:
To precisely position objects in your initial scenario (the
starting point of the game).
To command the objects to perform actions in the
game.

Copyright 2012, Oracle. All rights reserved.

14

Using Methods, Variables and Parameters

Ask Objects Questions


In school, teachers ask students questions to see if
students comprehend the material they learned in class
that day. Students respond with answers that let the
teachers know how much they learned.
As a programmer, we can ask objects questions in the form
of methods, with non-void return types, to learn more about
what an object can do or has done.

Copyright 2012, Oracle. All rights reserved.

15

Using Methods, Variables and Parameters

Non-void Return Types


Methods with non-void return types:
Do not include the word void
Ask a question
Return information to us about the object
Tell us something about the object, but don't change or
move it

Copyright 2012, Oracle. All rights reserved.

16

Using Methods, Variables and Parameters

Examples of Non-void Return Types


Examples of methods with non-void return types are:
Integer (displayed as int)
Refers to whole numbers
How many?

Boolean
True and false values
Can you move?
Are you at the edge of the world?

Copyright 2012, Oracle. All rights reserved.

17

Using Methods, Variables and Parameters

Method Parameters
Parameters provide methods additional data to make an
object perform a task. Parameters:
Used to provide additional information to a method,
when its required to invoke the method.
Defined by two components:
Parameter type
Parameter name

Parameter examples:
int to enter or display numbers
boolean to display true or false values
Parameters are used to command objects to move, or to tell objects what
type of response we expect when we ask objects a question.

Copyright 2012, Oracle. All rights reserved.

18

Using Methods, Variables and Parameters

Method Parameter Lists


Parameter lists:
Display as data within parentheses
Indicate whether the method requires additional
information to be invoked, and the type of information
Typically have two states:
Empty: No data expected to invoke the method (move
method)
Non-empty: Have data and expect one or more
parameters to invoke the method (turn method)

Copyright 2012, Oracle. All rights reserved.

19

Using Methods, Variables and Parameters

Object Properties
Properties describe the instance's appearance and abilities,
such as:
Size
Color
Range of movements
You can view and modify the class source code to change
or add to these properties.

Copyright 2012, Oracle. All rights reserved.

20

Using Methods, Variables and Parameters

View Object Properties


View object properties in the class documentation.

Copyright 2012, Oracle. All rights reserved.

21

Using Methods, Variables and Parameters

Variables
Elephants never forget. They
can remember how to do things
inherited from their animal
superclass, as well as their
elephant subclass, such as
blowing water out of their
trunks.
A variable, or field, allows the instance to
store information to use immediately or later.
Instance variables are the memory that belong
to the instance of the class. That memory can be
saved and accessed later as long as the
instance exists.

Copyright 2012, Oracle. All rights reserved.

22

Using Methods, Variables and Parameters

View Instance Variables


Right click on an instance, then click Inspect to view the
instance's variables in the object inspector.

Copyright 2012, Oracle. All rights reserved.

23

Using Methods, Variables and Parameters

Programming Syntax
The source code specifies all of the properties and
characteristics of a class and its objects.
Command objects in your scenario to perform tasks or
answer questions by writing source code, or syntax, in the
Java programming language.

Copyright 2012, Oracle. All rights reserved.

24

Using Methods, Variables and Parameters

Display Class Source Code


Select Open Editor from the class's menu to display the
code editor. The source code defines what objects in the
class can do.

Copyright 2012, Oracle. All rights reserved.

25

Using Methods, Variables and Parameters

Act Method
Whenever the Act or Run execution controls are pressed in
the environment, the object will do what is programmed in
the act method.

Copyright 2012, Oracle. All rights reserved.

26

Using Methods, Variables and Parameters

Body of Act Method


The curly brackets and content within them are the body of
the method. Here you can write code to instruct instances
of the class to act when the Act or Run buttons are clicked.

Copyright 2012, Oracle. All rights reserved.

27

Using Methods, Variables and Parameters

Act Method Example


Call the move and turn methods in the Act method to make
instances of the class move and turn. Methods must be
written correctly with no typos, missing characters, or
incorrect capitalization, or the source code will not compile.

Copyright 2012, Oracle. All rights reserved.

28

Using Methods, Variables and Parameters

Invoke Methods in Act Method


Write the method(s) in sequence as follows:
1. Name of the method in lowercase characters
2. Parentheses, with parameter list if required
3. Semicolon, to end the statement

Copyright 2012, Oracle. All rights reserved.

29

Using Methods, Variables and Parameters

Debug a Greenfoot Program


When debugging a Greenfoot program:
Incorrectly written or missing characters in your source
code will trigger error messages.
The compiler checks for errors in the source code.
If an error is found, an error message is displayed, and
must be corrected by the programmer before the
program will work.
Greenfoot provides these error messages so its easier
to correct mistakes, and learn from them.
Debugging is the process of finding and removing bugsor errors
in a computer program.

Copyright 2012, Oracle. All rights reserved.

30

Using Methods, Variables and Parameters

Syntax Error Example


The move method is missing a semicolon.

Copyright 2012, Oracle. All rights reserved.

31

Using Methods, Variables and Parameters

Error Message Example


After the Compile button is clicked, an error message
appears at the bottom of the screen, and the incorrect code
is highlighted.

Copyright 2012, Oracle. All rights reserved.

32

Using Methods, Variables and Parameters

Error Explanations
Click the question mark (?) to display a more detailed error
message that attempts to explain the error. Not all error
messages will be easy to understand.

Copyright 2012, Oracle. All rights reserved.

33

Using Methods, Variables and Parameters

Terminology
Key terms used in this lesson included:
Debug
Inheritance
Instance variable
Method
Method call
Parameter
Return type
Method signature
Variable

Copyright 2012, Oracle. All rights reserved.

34

Using Methods, Variables and Parameters

Summary
In this lesson, you learned how to:
Define parameters and how they are used in methods
Understand the concept of inheritance
Describe properties of an object
Describe the purpose of a variable
Describe programming concepts and define
terminology

Copyright 2012, Oracle. All rights reserved.

35

Using Methods, Variables and Parameters

Practice
The exercises for this lesson cover the following topics:
Invoking methods
Compiling and debugging programs
Identifying properties of an object
Dissecting and journaling subclass and superclass
concepts

Copyright 2012, Oracle. All rights reserved.

36