Sunteți pe pagina 1din 42

Topic 5 :

Methods and Data

CSC116 - INTRODUCTION TO COMPUTERS AND


1
PROGRAMMING
Lesson Outcome
• Methods
• Data and expressions
• Parameters

CSC116 - INTRODUCTION TO COMPUTERS AND


2
PROGRAMMING
Methods

CSC116 - INTRODUCTION TO COMPUTERS AND


3
PROGRAMMING
Methods
• As discussed before, object consist of properties and methods. This topic will discuss
further on the method.

• A method is a set of programming statements that an object can execute.

• A method usually consists of a sequence of statements to perform an action, a set of


input parameters, and possibly an output value of some kind

• The purpose of methods is to provide a mechanism for accessing (for both reading and
writing) the private data stored in an object or a class

CSC116 - INTRODUCTION TO COMPUTERS AND


4
PROGRAMMING
Methods
• Methods are essentially instruction sets for objects

• A method call is thus considered to be a request to an object to perform some task


• Method calls are often modeled as a means of passing a message to an object

• Methods commonly result in the object performing some action.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 5
PROGRAMMING
Methods in Alice 3
• There are two (2) method categories in Alice 3 :
1. Primitive – built in
2. User-defined – not yet exist, additional method by the programmer

• The method can be is two (2) characteristic in Alice 3 :


1. Procedure – no return value method (doing action without yielding a value).
2. Function – with return value method (doing action with yielding a value).

CSC116 - INTRODUCTION TO COMPUTERS AND


6
PROGRAMMING
Methods - Procedure
• Methods in Alice 3 is also known as Procedure

• For example, in the snow world, there is a method that causes the
snowman to raise his eyes.

• There are also methods that cause the snowman to turn and face to
other object, and move toward towards the object.

CSC116 - INTRODUCTION TO COMPUTERS AND


7
PROGRAMMING
Methods - Procedures
Primitive Procedure

• Primitive methods are the methods


that have already been created for
the user and there are built-in the
programming language.

• Example primitive procedures for


object bunny :

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 8
PROGRAMMING
Methods – Procedures Example in Alice 3
• Example of primitive methods in Alice 3,

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 9
PROGRAMMING
Methods - Functions
Function

• A function is a special type of method. Everything that you’ve learned about


methods so far also applies to functions.

• A function is a set of instructions that cause some action to take place

• To execute a function you create an instruction that calls it

• Some functions require additional pieces of data known as arguments (parameters)


similar to the methods.
Reference: CSC116 - INTRODUCTION TO COMPUTERS AND
• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 10
PROGRAMMING
Methods - Functions
• A Function will yield a value
when it is called.

• Here, the list of Functions for


object bunny.

• The listed functions are the


primitive-functions (built-in
functions).

CSC116 - INTRODUCTION TO COMPUTERS AND


11
PROGRAMMING
Methods Example in Alice 3
• In previous Topic 4, a demo for Do-Together has applied the primitive
method, move().

• In this Topic 5, we apply primitive function and create a new method


for the bunnies.
• Primitive function – getHeight()
• User-defined method – hopping()

CSC116 - INTRODUCTION TO COMPUTERS AND


12
PROGRAMMING
Methods - Function Example in Alice 3
• Function will return a value, hence in this example, we use
getHeight() which will return the bunny’s height.

• getHeight() will return the bunny’s height from the properties value.

CSC116 - INTRODUCTION TO COMPUTERS AND


PROGRAMMING 13
Methods - Procedure Example in Alice 3
• To add a new (user-defined) method, it can be added in Scene tab.
1

CSC116 - INTRODUCTION TO COMPUTERS AND


14
PROGRAMMING
Methods - Procedure Example in Alice 3
• In this example, we create a new method called hopping() to allow
the bunny hopping forward one time.
3

1
2 3) The new method (user-
define/editable) is appear in bunny
object
1) Give the appropriate method
name
2) Define some instructions

CSC116 - INTRODUCTION TO COMPUTERS AND


15
PROGRAMMING
Methods Example in Alice 3
>> Test Yourself

a) List one (1) possible procedure for the bunny.

b) List two (2) possible functions for the bunny.

CSC116 - INTRODUCTION TO COMPUTERS AND


16
PROGRAMMING
Methods and Arguments
Method Calls and Arguments (Parameters)

• When programmers execute a method, they commonly say that they are calling the method.

• An instruction that executes a method is commonly referred to as a method call.

• When you call a method and provide any necessary arguments, it is called passing the
arguments to the method.

• Arguments are also called as parameter.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 17
PROGRAMMING
Methods and Arguments
• Example, a method call with arguments

Arguments

Name of the Name of the


object method

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 18
PROGRAMMING
Data and Expression

CSC116 - INTRODUCTION TO COMPUTERS AND


19
PROGRAMMING
Data and Expression
• Computer process required input  process  output mechanism.

• Data is input then will be processes and output as the information.

• Hence, this data need to be stored in a variable, process based on


some mathematical expression during the calculation.

• There are various type of data involve in programming.

CSC116 - INTRODUCTION TO COMPUTERS AND


20
PROGRAMMING
Variables
Variables

• When the program performs these calculations, it stores their results in the
computer’s memory. When a program stores information in memory, it does so with
variables.

• A variable is a storage location that is represented by a name.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 21
PROGRAMMING
Variables
• Type of variables in Alice 3:
• Local Variables: belongs to a specific method. A local variable can be used
only by the instructions in the method that the variable belongs to. When a
method stops executing, its local variables cease to exist in memory.
• Parameter Variables: is used to hold an argument that is passed to a method
when the method is called.
• Class-Level Variables: is like a property that belongs to a specific object.
• World-Level Variables: is like a property that belongs to the world.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 22
PROGRAMMING
Variables and Data
• Data type in Alice 3 are:
1. DecimalNumber – number with decimal
points.
2. WholeNumber – an integer number
3. Boolean – value of True or False only
4. TextString – any text

• Noted that TextString is using the double


quote, “”.
• 7 and “7” are different data type.
• 7 – is a WholeNumber, can be used in calculation
• “7” – is a TexString, cannot be used in calculation

CSC116 - INTRODUCTION TO COMPUTERS AND


23
PROGRAMMING
Variable and Data Example in Alice
• This data need to be stored in variables. In Alice, three (3) steps are
required to create a variable :
1. Value Type
2. Variable’s name
3. Initializer value

Create the variable using the variable


block

CSC116 - INTRODUCTION TO COMPUTERS AND


24
PROGRAMMING
Variable and Data Example in Alice
• Previously, we use getHeight() to diplay bunny’s height. In this situation, we may
store the height in a variable and can perform some expression.

Data Assignment Process

2) Two (2) variables are created to store


bunny1’s height and bunny2’s height.

1) Give the appropriate variable data


type, name and initial value
CSC116 - INTRODUCTION TO COMPUTERS AND
25
PROGRAMMING
Variable and Data Assignment
• While the method is running, the variable value can be changed using
assignment process.

• The assignment process is required to set the variable to a new value.

• Variable assignment means an instruction that sets a variable to a


value.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 26
PROGRAMMING
Variables and Data Example in Alice
• Use assign block to store bunny’s height value into the created
variable.

Assign the value to variable using the


assign block

CSC116 - INTRODUCTION TO COMPUTERS AND


27
PROGRAMMING
Variables and Data Input
• Programming require input data value.

• This value can be assigned to the variable.

• Hence, in Alice 3, Primitive Function for each object will be used to


ask the user to input data value.

• The process of asking the data input is called prompt (prompting).

CSC116 - INTRODUCTION TO COMPUTERS AND


28
PROGRAMMING
Variables and Data Input
• Generally, in Alice 3 there are three (3) ways of asking the user input

CSC116 - INTRODUCTION TO COMPUTERS AND


29
PROGRAMMING
Variables and Data Input Example in Alice 3
• The list of primitive function to
ask the user to input data :
1. Ask a number
• getDoubleFromUser
• getIntegerFromUser
2. Ask for yes or no
• getBooleanFromUser
3. Ask for a string
• getStringFromUser

CSC116 - INTRODUCTION TO COMPUTERS AND


30
PROGRAMMING
Variables and Data Input Example in Alice 3
1 3

1.1) Prepare a variable to store


the input value (eg:
bunny1Height) 3) The outcome for the input process
1.2) Choose the appropriate when you run the program
prompt primitive-function
2) Prepare an appropriate prompt text
(optional)
CSC116 - INTRODUCTION TO COMPUTERS AND
31
PROGRAMMING
Data and Expression
• A math expression performs a calculation and return a value (result).

• Example of math expression :


• 12 + 2 (using number literal)
• payRate * hoursWorked (using variable value)

• This expression calculation result can be assign to a variable.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 32
PROGRAMMING
Data and Expression
• A math expression needs math operators to perform the calculation.

Operators Description
+ Addition
- Subtraction
* Multiplication
/ Division

CSC116 - INTRODUCTION TO COMPUTERS AND


33
PROGRAMMING
Data and Expression Example in Alice 3
• Supposed we want to increase the bunny1’s height by adding
0.25meter to previous data input.

Set the Math Expression through


the assign block and find the
appropriate Math expression :

Expression form :

bunny1Height = bunny1Height + 0.25

CSC116 - INTRODUCTION TO COMPUTERS AND


34
PROGRAMMING
Data and Expression Example in Alice 3

CSC116 - INTRODUCTION TO COMPUTERS AND


35
PROGRAMMING
Parameters

CSC116 - INTRODUCTION TO COMPUTERS AND


36
PROGRAMMING
Parameters
• Each method is its own small block of instruction, design to perform
specific task when requested (called).

• Therefore, some communication might need to occur when a method


is called.

• Parameters are used to communicate with a method.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Dann, Cooper and Pausch, Learning to Program with Alice 37
PROGRAMMING
Parameters
• We arrange to communicate values (e.g: number, string, name of the
object) from one method to another method through the parameters

• Recap the meaning of parameter :


• Parameter Variables - is used to hold an argument that is passed to a method
when the method is called.

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 38
PROGRAMMING
Parameters Example in Alice 3
• Example parameters:
The instruction calls the bunny1.move method and passes the arguments FORWARD
and 0.25 meter

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 39
PROGRAMMING
Parameters

Arguments / Parameters

Name of the Name of the


object method

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 40
PROGRAMMING
Parameters from Input Value
• In Alice 3, we also can set the parameter value from the input value.

• Using previous input example, set the bunny1’s height from the input data.

Parameter from
input data

Reference: CSC116 - INTRODUCTION TO COMPUTERS AND


• Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming 41
PROGRAMMING
END

CSC116 - INTRODUCTION TO COMPUTERS AND


42
PROGRAMMING

S-ar putea să vă placă și