Top Banner
Copyright © 2012, Oracle. All rights reserved. Using Methods, Variables and Parameters
36

JF_V01_S03_L02

Jan 16, 2016

Download

Documents

edukasi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JF_V01_S03_L02

Copyright © 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Page 2: JF_V01_S03_L02

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

Page 3: JF_V01_S03_L02

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.

Page 4: JF_V01_S03_L02

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.

Page 5: JF_V01_S03_L02

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.

Page 6: JF_V01_S03_L02

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.

Page 7: JF_V01_S03_L02

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.

Page 8: JF_V01_S03_L02

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.

Page 9: JF_V01_S03_L02

9Copyright © 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

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

Page 10: JF_V01_S03_L02

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.

Page 11: JF_V01_S03_L02

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

Page 12: JF_V01_S03_L02

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)

Page 13: JF_V01_S03_L02

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

Page 14: JF_V01_S03_L02

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.

Page 15: JF_V01_S03_L02

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.

Page 16: JF_V01_S03_L02

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

Page 17: JF_V01_S03_L02

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?

Page 18: JF_V01_S03_L02

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.

Page 19: JF_V01_S03_L02

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)

Page 20: JF_V01_S03_L02

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.

Page 21: JF_V01_S03_L02

21Copyright © 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

View Object PropertiesView object properties in the class documentation.

Page 22: JF_V01_S03_L02

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.

Page 23: JF_V01_S03_L02

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.

Page 24: JF_V01_S03_L02

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.

Page 25: JF_V01_S03_L02

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.

Page 26: JF_V01_S03_L02

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.

Page 27: JF_V01_S03_L02

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.

Page 28: JF_V01_S03_L02

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.

Page 29: JF_V01_S03_L02

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

Page 30: JF_V01_S03_L02

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.

Page 31: JF_V01_S03_L02

31Copyright © 2012, Oracle. All rights reserved.

Using Methods, Variables and Parameters

Syntax Error ExampleThe move method is missing a semicolon.

Page 32: JF_V01_S03_L02

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.

Page 33: JF_V01_S03_L02

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.

Page 34: JF_V01_S03_L02

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

Page 35: JF_V01_S03_L02

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

Page 36: JF_V01_S03_L02

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