Topic 5 Methods and Data CSC 116 INTRODUCTION

  • Slides: 42
Download presentation
Topic 5 : Methods and Data CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING

Topic 5 : Methods and Data CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 1

Lesson Outcome • Methods • Data and expressions • Parameters CSC 116 - INTRODUCTION

Lesson Outcome • Methods • Data and expressions • Parameters CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 2

Methods CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 3

Methods CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 3

Methods • As discussed before, object consist of properties and methods. This topic will

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 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 4

Methods • Methods are essentially instruction sets for objects • A method call is

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 5

Methods in Alice 3 • There are two (2) method categories in Alice 3

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). CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 6

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

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 also methods that cause the snowman to turn and face to other object, and move towards the object. CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 7

Methods - Procedures Primitive Procedure • Primitive methods are the methods that have already

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 8

Methods – Procedures Example in Alice 3 • Example of primitive methods in Alice

Methods – Procedures Example in Alice 3 • Example of primitive methods in Alice 3, Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 9

Methods - Functions Function • A function is a special type of method. Everything

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 10

Methods - Functions • A Function will yield a value when it is called.

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). CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 11

Methods Example in Alice 3 • In previous Topic 4, a demo for Do-Together

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 – get. Height() • User-defined method – hopping() CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 12

Methods - Function Example in Alice 3 • Function will return a value, hence

Methods - Function Example in Alice 3 • Function will return a value, hence in this example, we use get. Height() which will return the bunny’s height. • get. Height() will return the bunny’s height from the properties value. CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 13

Methods - Procedure Example in Alice 3 • To add a new (user-defined) method,

Methods - Procedure Example in Alice 3 • To add a new (user-defined) method, it can be added in Scene tab. 1 3 2 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 14

Methods - Procedure Example in Alice 3 • In this example, we create a

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 1) Give the appropriate method name 3) The new method (userdefine/editable) is appear in bunny object 2) Define some instructions CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 15

Methods Example in Alice 3 >> Test Yourself a) List one (1) possible procedure

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. CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 16

Methods and Arguments Method Calls and Arguments (Parameters) • When programmers execute a method,

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 17

Methods and Arguments • Example, a method call with arguments Arguments Name of the

Methods and Arguments • Example, a method call with arguments Arguments Name of the object Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming Name of the method CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 18

Data and Expression CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 19

Data and Expression CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 19

Data and Expression • Computer process required input process output mechanism. • Data is

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. CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 20

Variables • When the program performs these calculations, it stores their results in the

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 21

Variables • Type of variables in Alice 3: • Local Variables: belongs to a

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 22

Variables and Data • Data type in Alice 3 are: 1. 2. 3. 4.

Variables and Data • Data type in Alice 3 are: 1. 2. 3. 4. Decimal. Number – number with decimal points. Whole. Number – an integer number Boolean – value of True or False only Text. String – any text • Noted that Text. String is using the double quote, “”. • 7 and “ 7” are different data type. • 7 – is a Whole. Number, can be used in calculation • “ 7” – is a Tex. String, cannot be used in calculation CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 23

Variable and Data Example in Alice • This data need to be stored in

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 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 24

Variable and Data Example in Alice • Previously, we use get. Height() to diplay

Variable and Data Example in Alice • Previously, we use get. Height() 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 bunny 1’s height and bunny 2’s height. 1) Give the appropriate variable data type, name and initial value CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 25

Variable and Data Assignment • While the method is running, the variable value can

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 26

Variables and Data Example in Alice • Use assign block to store bunny’s height

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 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 27

Variables and Data Input • Programming require input data value. • This value can

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). CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 28

Variables and Data Input • Generally, in Alice 3 there are three (3) ways

Variables and Data Input • Generally, in Alice 3 there are three (3) ways of asking the user input CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 29

Variables and Data Input Example in Alice 3 • The list of primitive function

Variables and Data Input Example in Alice 3 • The list of primitive function to ask the user to input data : 1. Ask a number • get. Double. From. User • get. Integer. From. User 2. Ask for yes or no • get. Boolean. From. User 3. Ask for a string • get. String. From. User CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 30

Variables and Data Input Example in Alice 3 1 3 2 1. 1) Prepare

Variables and Data Input Example in Alice 3 1 3 2 1. 1) Prepare a variable to store the input value (eg: bunny 1 Height) 1. 2) Choose the appropriate prompt primitive-function 3) The outcome for the input process when you run the program 2) Prepare an appropriate prompt text (optional) CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 31

Data and Expression • A math expression performs a calculation and return a value

Data and Expression • A math expression performs a calculation and return a value (result). • Example of math expression : • 12 + 2 (using number literal) • pay. Rate * hours. Worked (using variable value) • This expression calculation result can be assign to a variable. Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 32

Data and Expression • A math expression needs math operators to perform the calculation.

Data and Expression • A math expression needs math operators to perform the calculation. Operators Description + Addition - Subtraction * Multiplication / Division CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 33

Data and Expression Example in Alice 3 • Supposed we want to increase the

Data and Expression Example in Alice 3 • Supposed we want to increase the bunny 1’s height by adding 0. 25 meter to previous data input. Set the Math Expression through the assign block and find the appropriate Math expression : Expression form : bunny 1 Height = bunny 1 Height + 0. 25 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 34

Data and Expression Example in Alice 3 CSC 116 - INTRODUCTION TO COMPUTERS AND

Data and Expression Example in Alice 3 CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 35

Parameters CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 36

Parameters CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 36

Parameters • Each method is its own small block of instruction, design to perform

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: • Dann, Cooper and Pausch, Learning to Program with Alice CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 37

Parameters • We arrange to communicate values (e. g: number, string, name of the

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: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 38

Parameters Example in Alice 3 • Example parameters: The instruction calls the bunny 1.

Parameters Example in Alice 3 • Example parameters: The instruction calls the bunny 1. move method and passes the arguments FORWARD and 0. 25 meter Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 39

Parameters Arguments / Parameters Name of the object Reference: • Tony Gaddis, Starting Out

Parameters Arguments / Parameters Name of the object Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming Name of the method CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 40

Parameters from Input Value • In Alice 3, we also can set the parameter

Parameters from Input Value • In Alice 3, we also can set the parameter value from the input value. • Using previous input example, set the bunny 1’s height from the input data. Parameter from input data Reference: • Tony Gaddis, Starting Out With Alice – A Visual Introduction to Programming CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 41

END CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 42

END CSC 116 - INTRODUCTION TO COMPUTERS AND PROGRAMMING 42