Copyright © 2012, Oracle. All rights reserved. Using Methods, Variables and Parameters
Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
2Copyright © 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
3Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Why Learn It?Purpose Imagine that you’ve 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.
4Copyright © 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.
5Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
MethodsIn 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 thesource code.
6Copyright © 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.
7Copyright © 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.
8Copyright © 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 drop-
down menu. 4. Scroll down to the Method Summary.
9Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Method Summary The method summary displays inherited methods for the object.
10Copyright © 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.
11Copyright © 2012, Oracle. All rights reserved.
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.
void move (int)
Return Type
Method NameParameter List
12Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Return TypesThe 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)
13Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Void Return TypesVoid 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
14Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Invoking Methods with Void Return TypesInvoke 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.
15Copyright © 2012, Oracle. All rights reserved.
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.
16Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Non-void Return TypesMethods 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
17Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Examples of Non-void Return TypesExamples 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?
18Copyright © 2012, Oracle. All rights reserved.
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 whattype of response we expect when we ask objects a question.
19Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Method Parameter ListsParameter 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)
20Copyright © 2012, Oracle. All rights reserved.
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.
21Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
View Object PropertiesView object properties in the class documentation.
22Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
VariablesElephants 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 tostore information to use immediately or later.
Instance variables are the memory that belong to the instance of the class. That memory can besaved and accessed later as long as the instance exists.
23Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
View Instance VariablesRight click on an instance, then click Inspect to view the instance's variables in the object inspector.
24Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Programming SyntaxThe 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.
25Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Display Class Source CodeSelect Open Editor from the class's menu to display the code editor. The source code defines what objects in the class can do.
26Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Act MethodWhenever the Act or Run execution controls are pressed in the environment, the object will do what is programmed in the act method.
27Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Body of Act MethodThe 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.
28Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Act Method ExampleCall 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.
29Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Invoke Methods in Act MethodWrite the method(s) in sequence as follows: 1. Name of the method in lowercase characters2. Parentheses, with parameter list if required3. Semicolon, to end the statement
30Copyright © 2012, Oracle. All rights reserved.
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 bugs—or errors—in a computer program.
31Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Syntax Error ExampleThe move method is missing a semicolon.
32Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Error Message ExampleAfter the Compile button is clicked, an error message appears at the bottom of the screen, and the incorrect code is highlighted.
33Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
Error ExplanationsClick 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.
34Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
TerminologyKey terms used in this lesson included:DebugInheritance Instance variableMethodMethod call ParameterReturn typeMethod signatureVariable
35Copyright © 2012, Oracle. All rights reserved.
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
36Copyright © 2012, Oracle. All rights reserved.
Using Methods, Variables and Parameters
PracticeThe 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