Beruflich Dokumente
Kultur Dokumente
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.
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.
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.
Method Summary
The method summary displays inherited methods for the
object.
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.
10
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
11
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)
12
13
14
15
16
Boolean
True and false values
Can you move?
Are you at the edge of the world?
17
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.
18
19
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.
20
21
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.
22
23
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.
24
25
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.
26
27
28
29
30
31
32
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.
33
Terminology
Key terms used in this lesson included:
Debug
Inheritance
Instance variable
Method
Method call
Parameter
Return type
Method signature
Variable
34
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
35
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
36