programming iii (mblock)plk83.edu.hk/cy/mblock/programming iii (mblock).pdf · 2016. 6. 30. · 2....

14
Computer Literacy Programming III (mBlock) __________________________________________________________________________________________ Page 1 of 14 Programming III (mBlock) http://www.plk83.edu.hk/cy/mblock Contents 1. Introduction (Page 1) 2. IPO Model (Page 2) 3. Variable (Page 2) 4. Operators (Page 3) 5. Decision Making (Page 5) 6. Loops (Page 8) 7. Block (Page 12) Introduction A computer program is a collection of instructions that performs a specific task when executed by a computer. Script language is a programming language that supports program (called a ‘script’) written for a special run-time environment. The following script contains 4 instructions that should be executed under the run-time environment of mblock (http://www.mblock.cc/) only.

Upload: others

Post on 02-Feb-2021

7 views

Category:

Documents


3 download

TRANSCRIPT

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 1 of 14

    Programming III (mBlock) http://www.plk83.edu.hk/cy/mblock

    Contents

    1. Introduction (Page 1)

    2. IPO Model (Page 2)

    3. Variable (Page 2)

    4. Operators (Page 3)

    5. Decision Making (Page 5)

    6. Loops (Page 8)

    7. Block (Page 12)

    Introduction

    A computer program is a collection of instructions that performs a specific task when executed

    by a computer.

    Script language is a programming language that supports program (called a ‘script’) written

    for a special run-time environment.

    The following script contains 4 instructions that should be executed under the run-time

    environment of mblock (http://www.mblock.cc/) only.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 2 of 14

    IPO Model

    A program using the input-process-output (IPO) model receives inputs from a user or other

    sources, does some computations on the input, and outputs the results of the computations.

    Try the following example.

    Describe the input, process and the output of the above script.

    Input: __________________________________________________________________

    Process: __________________________________________________________________

    Output: __________________________________________________________________

    Variable

    The input of the instruction is stored in the variable

    .

    You can make a new variable (named as ‘age’) as below.

    Then, execute the following script to store the input into the variable ‘age’.

    In addition, you can set and change the value of the variable.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 3 of 14

    Activity 1

    Design a script to output the sum of two inputted integers as below.

    Operators

    There are different kinds of operators.

    Arithmetic Operators

    perform ‘addition’, ‘subtraction’, ‘multiplication’ and ‘division’ respectively.

    State the instructions to calculate the followings.

    1. (2 * 3 + 4) / 5 ___________________________________________________________

    2. 2 * 3 + 4 / 5 ___________________________________________________________

    Check your answers by using the instruction.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 4 of 14

    Relational Operators

    perform ‘less than’, ‘equal to’ and ‘greater than’ comparisons respectively.

    The results can be either ‘true’ or ‘false’.

    State the results of the followings.

    1. 6 < 8 ___________

    2. 4 = 4 ___________

    3. (5 + 6) > 7 ___________

    Check your answers by using the instruction.

    Logical Operators

    perform ‘and’, ‘or’ and ‘not’ operations respectively.

    The results can be either ‘true’ or ‘false’.

    The following ‘truth tables’ describe the relationships between the inputs and outputs of

    various logical operators.

    Inputs Output Inputs Output Input Output

    P Q P and Q P Q P or Q P not P

    true true true true true true true false

    true false false true false true false true

    false true false false true true

    false false false false false false

    Activity 2

    Design a script to meet the following requirements.

    1. Generate two random numbers between 1 and 10. The first number should be less than or

    equal to the second number.

    2. Input an integer.

    3. If the input is within the two random numbers, output ‘true’. Otherwise, output ‘false’.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 5 of 14

    Decision Making

    There are two popular decision making structures.

    if-then

    This structure requires that the programmer specifies the condition to be tested by the program,

    along with a statement or statements to be executed if the condition is determined to be true.

    If the condition is determined to be false, nothing will be executed.

    if-then-else

    This structure requires that the programmer the condition to be tested by the program, along

    with a statement or statements to be executed if the condition is determined to be true, and

    other statements to be executed if the condition is determined to be false.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 6 of 14

    Example

    In the given script, if the input is larger

    than 100, the output is “Too large!”.

    Otherwise, the mark is outputted.

    Nested If structure

    One if structure may itself contain another if structure.

    The following shows some possible nested if structures.

    Example

    The first condition to be tested is

    . If it is true,

    is

    executed. Otherwise, the second

    condition is

    tested and execute one of the last two

    “say” instructions according to the

    results of the second condition.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 7 of 14

    Activity 3

    Design a script to give the appropriate feedback as below.

    Activity 4

    Design a script to meet the following requirements.

    1. Input an integer.

    2. If the integer is an odd number, output “It is an odd number”. Otherwise, output “It is an

    even number”.

    Hint: Use the instruction to find the remainder when the dividend is divided

    by the divisor.

    For example, returns 1; returns 0.

    Activity 5

    The following table shows the ticket prices of a theme park.

    Ticket price (HK$)

    Children (age: 3 - 17) $100

    Adults (age: 18 - 60) $350

    Others $0

    Design a script to meet the following requirements.

    1. Input the age.

    2. Output the corresponding ticket price.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 8 of 14

    Loops

    If some instructions needs to be executed again and again, a loop structure can be used.

    There are different kinds of loops.

    Forever Loop

    The instructions will be executed endlessly.

    Given that number is a variable, what will

    happen when the following script is

    executed?

    ___________________________________________________________________________

    ___________________________________________________________________________

    Repeat Loop

    We can specify the number of times to execute the loop.

    Given that number and sum are two variables,

    complete the following script to find the sum of

    integers from 1 to 100.

    Note: the output should be 5050.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 9 of 14

    Repeat-until Loop

    At the end of each loop, the condition is tested. If it

    is false, the loop continues; if it is true, the loop

    stops.

    Example

    Giving that number is a variable, the following script

    performs a countdown from 10 to 0.

    Wait-until Loop

    This is special form of repeat-until loop in which there is nothing inside the loop. The function

    of the wait-until loop is to pause the script until a specified condition is true.

    Example

    The following script gives a 3 seconds delay to the execution of the ‘say’ instruction by using

    the built-in timer.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 10 of 14

    Activity 6

    Paint the backdrop with an ‘M’

    shaped route and a red coloured circle

    acts as the finished spot.

    Insert a ‘Star’ shaped sprite. Shrink

    the sprite such that it is inside the

    route.

    Design a script to meet the following

    requirements.

    1. Touch the Star to start the game.

    2. The star can follow the movement

    of the mouse pointer.

    3. If the star is outside the route, a

    warning sound will be played and the game failed. The star should go back to the starting

    position.

    4. If the star can reach the finished spot, you can win the game. An appropriate message

    should be given.

    Hint: You may use the following instructions to construct the script.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 11 of 14

    Activity 7

    Insert two paddles (by modifying the

    size of the built-in button sprite) and a

    ball onto the stage.

    Make two variables Player_1 and

    Player_2 to store the scores.

    Two players can use the keyboard to

    move the paddles up and down.

    If the opponent cannot hit the ball, a

    player can get one mark. The first player

    who can get 5 marks is the winner.

    For the upward movement control of the

    paddle, which of the following sprits is

    better? Why?

    ___________________________________________________________________________

    ___________________________________________________________________________

    Design the scripts for all the sprites.

    Hint: You may use the following instructions to construct the script of the ball.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 12 of 14

    Block

    You can define a block to store a set of instructions. The following figure shows how a block

    (named as ‘jump’) is defined.

    Hence, you can assign the instructions to the block.

    Then, you can execute the block.

    In order to control the height in the block, add a number input (named as ‘height’).

    Then, modify the block as below.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 13 of 14

    Now, you can execute the block of instructions with different number inputs.

    Activity 8

    Pythagoras Theorem states the

    relationship of three sides of a right

    angled-triangle.

    Design a block to find the hypotenuse c

    from the inputs of a and b.

  • Computer Literacy Programming III (mBlock)

    __________________________________________________________________________________________

    Page 14 of 14

    Activity 9

    Flappy bird is a popular game. You have to design the game to meet the following requirements.

    1. The bird has to fly through the gaps between the tubes.

    2. Use the keyboard to control the upward and downward movements of the bird.

    3. The positions of the gaps should be randomly generated.

    4. The tubes are moving towards the L.H.S. of the stage.

    5. One mark is scored when the bird can fly through one gap.

    6. If the bird touches any tube, the game is over.

    Hint: The following script makes one of the tubes moving towards the L.H.S and the tube will

    come out at a random position.