National 4 Livecode notes - Forrester High School Computing

fullfattruckMobile - Wireless

Dec 10, 2013 (3 years and 8 months ago)

151 views

National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
1
Programming
with
National 4 Computing Science
Materials produced at GHS
By Mr S. whyte
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
2
The Software Development Process
Introduction

A
nalysis

D
esign

I
mplementation

T
esting

D
ocumentation

E
valuation

M
aintenance
The Software Development Process (
SDP
) can be split into
7

main
steps which are carried out in
order
.
These steps should be carried out when creating
any
software project and are summarised below.
A statement about
what
your program is going to do. You will also include a description of the
main

steps
of the problem.
This involves designing both the
user

interface
and the
structure
of the
program

code
.
For the purpose of Intermediate 2 Computing, more emphasis will be placed on designing the
structure
of
the
program

code
rather than the design of the user interface. We will be using a design notation known
as
pseudocode
to achieve this. More is mentioned about pseudocode on the next page.
The implementation stage involves keying in the program code using the built in
text

editor
within the
p r o g r a mmi n g e n v i r o n me n t. We w i l l u s e
LiveCode
to create our programs.
Testing is an important part of any project. Testing ensures that your program is
reliable
and
robust
in
the sense that it should produce the
correct

results
and
not

crash
due to
unexpected

input
.
We should test our program with
three
sets of test data. These are:

Normal
(accepted data within a set range)

Extreme
(accepted data on the boundaries)

Exceptional
(data that is not accepted).
An evaluation is usually a
review
which shows that your program is
fit

for

purpose
, in other words, it
does
exactly
what it was
designed
to do.
The evaluation should also focus on the
readability
of your program code. For example, if
another

programmer
was asked to
maintain
your program code at a later date, would they be able to understand
what was going on? You should always ensure your program is
readable
by doing the following:

Use
of
meaningful

identifiers
for
variable
and
array

names

Use
of
internal

commentary

(
// This subroutine will do the following...
.)

Effective
use of
white

space
between
subroutines
to
space

out
the
program
.

Indentation
to show the
start
and
end
of any control structures such as a fixed
loop
.
Documentation is usually produced in the form of a
user

guide
and a
technical

guide
. The user guide
shows the user how to
use
the
f unct i ons
and
f eat ures
of the
software
whereas the
technical

guide

gives the user information on how to
install
the
software
as well as the
minimum

system

requirements
.
A
D
ance
I
n
T
he
D
ark
E
very
M
onday
Maintenance is performed at the
very

end
of the project. You will not be required to perform any
maintenance on your programs but you will need to know about
Corrective
,
Adaptive
and
Perfective

maintenance. These are covered in the Software Development theory notes.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
3
LiveCode
LiveCode is a modern programming environment that has been created by an
Edinburgh-based company called Runtime Revolution,
www.runrev.com
.
LiveCode is advertised as being a
very

high

level

language
and is considered to be
even

closer
to the way we speak and write as opposed to the sometimes
complex

commands
and
syntax
used in other high-level programming environments.
Users can use LiveCode to create
any
type of program. This could range from a
simple application which performs addition to a more advanced game application that
could be run on a desktop computer or mobile phone.
LiveCode is an
event-driven programming language
which means that it involves
the triggering of
events
such as a mouse click on a button or text entry into an output
field.
The LiveCode programming environment very portable which means that it can run on
a
variety
of operating system
platforms
. This includes a
PC
running Windows XP,
Vista, Windows 7 and 8 or Linux as well as on a
Mac
running OS X.
At least
400MB
of
hard

disk
space and
256MB
of
RAM
is required in order for the
programming language to run.
The LiveCode programming environment has already been
installed
in the
A p p l i c a t i o n s
f o l d e r:
You will need to
copy
the
LiveCode Programming Tasks
folder from the
resources

area of the server to
your

own

programming
folder within your user space on the
server. This folder contains the National 4 LiveCode stacks for all 10 tasks that you will
do during this programming unit.
Copy the
LiveCode Programming Tasks
folder from the server
now
and

make

sure

you
work
from
this folder
from
now on
. It is in:
National 4 > Software Development > N4 LiveCode Programming
IMPORTANT
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
4
Getting Started
Task 1A: Display My Message (Your First Program!)
Specification
A simple program is required to ask for and display a simple
message in an output box.
The user will be prompted for their
message
and it will be
di spl ayed
five

times
.
In order to display the message five times, a simple
REPEAT...END REPEAT
loop will need to be added.
A programmer always begins by writing out an
analysis
of the problem. This is when
they come up with the steps which need to be carried out by the program in order to
solve the problem.
Here is the
analysis
for the
Display My Message
Program:
I have been asked to create a simple program which asks for a message and then
displays the message on the screen 5 times. I will carry out the following
three
steps:
Step 1
: I will setup the required variable.
Step 2
: I will prompt the user for the message they require.
Step 3
: The users message will be displayed in an output box five times using a repeat...until loop
Analysis
Design
After
the analysis comes the
design
of the program. The design goes into more
detail with regards to the
main

steps
of the program.
The design of a program is also known as the
Pseudocode
(pronounced sue-do-
code). Pseudocode is a cross between
our

language
and
programming
language
and helps the programmer think about the program to be created in a step-by-step
manner.
Here is the
design
showing steps for the
Display My Message Program
:
Stepwise Design
(the main steps of the program)
:
1.!Setup the Variables (areas) to store the users message
2.!Ask for the Users Message
3.!Display Message Five Times
Stepwise Refinement

(breaking down the main steps into smaller steps)
:
1.
Setup the Variables (areas) to store the users message
1.1!Setup the variable called my_message
2.
Ask for the Users Message
2.1!Ask for the users message
2.2!User puts in their message and this is stored in my_message
3.
Display Message Five Times
3.1!
Start a

Repeat

with loop
1 to 5
3.2! Display my_message and a return after field “output”
3.3!
End

Repeat
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
5
Getting Started
Task 1A: Display My Message (Your
FIRST EVER
Program!)
Open the “
Display My Message
” program stack. It can be found in:
N4 LiveCode Programming Tasks > 1A...Display My Message.livecode
Once the program has been opened, select the “
Edit Mode

to
edit
the script for the “
Display My Message
” button.
Implementation
Select the “
Display My Message
” button to edit the object’s
script.
Select the “
Code
” button at the top left of the toolbar.
You will now enter the lines of program code on the next page
very

carefully
.
The implementation involves going onto the computer and actually creating the program
using the analysis and design sections to help.
For the purpose of these exercises, the program code will be supplied for you.
However, you will need to create the program code from scratch when you come onto
doing your SQA coursework.
Edit Mode
Run Mode
Follow these simple instructions below in order to a produce the script for the “Display
My Message” button.
Your program script will
be placed into this button
Your message will be displayed 5
times in this field labelled “output”
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
6
Task 1A: Display My Message (Your
FIRST EVER
Program!)
// Setup the variable to be used in this event
global
my_message
on
mouseUp

// A list of the subroutines that will occur when the button has been pressed
get_message
display_message_five_times
end
mouseUp
on
get_message

// Ask the user for the simple message they wish to be displayed

ask
"Please print your simple message, i.e. Hello World"


put
it
into
my_message
end
get_message
on
display_message_five_times

// Display users message on the screen 5 times using a loop

repeat
with loop = 1 to 5

put
my_message &
return

after

field

"output"

end

repeat
end
display_message_five_times
Implementation (continued)
When you edit the “
Display My Message
” button, you see the area where the program
script is to be created. The script consists of lines of program code that will be
executed when the button is pressed using the mouse.
Your task is to add the program code shown below carefully. Watch your spelling as
all

your code will need to be
correct
in order for your program to run successfully!
Testing
Test that your program produces the correct output by running it. If the program works
correctly, it will display the message you keyed in
five
times.
To run a LiveCode program, go to the
File
menu and
save
and
close
the script and
exit edit mode by selecting the following button:
Save your program as 1A...Display My Message.
Select the “
Display My Message
” button
and a pop-up window will appear. From
there, key in the message you would like
displayed.
This message should appear
five

times
in
the
output
field as shown on the right.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
7
Specification
Your teacher will go over this simple program before you begin. It’s a simple program
to ask the user a
couple
of
questions
. This includes the user’s
name
and
favourite

subject
. The program will then produce an answer and ask another question.
Once you have ran the program to see what happens, your task is to add a couple of
more questions on at the end.
This could include the persons favourite film, football team or colour, etc.
Task 1B: Conversation
Open the “
Conversation Program
” stack. It can be found in:
N4 LiveCode Programming Tasks > 1B...Conversation.livecode
Click on the “
Edit Mode
” tool to
edit
the script for the Conversation
program.
Now select the “
Edit Script
” button at the top on the toolbar.
Add
at least another
two
questions of your choice to the program script
using the
ask

command
.
You will also need to
put
the users answer into a
variable
. Variables will
be talked about later but in this task, they are basically a
space
(or
box
)
for storing the users
answer
.
Save and close your script. Run the program by selecting the
run

mode

button. Test it to make sure it works as it should. Edit the script if if the
program does not work as it should.
Select the ‘Talk To Me’ button to edit
the object script.
Implementation
Save your program as 1B...Conversation.
Edit Mode
Run Mode
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
8
Specification
A program is required to take in the title, location, time and date of a party
and display these details in the form of a party invitation on the screen.
The graphical user interface for the program has already been created for
you. All you need to do is to key in the program for the button “Create
Invitation”.
Before starting, familiarise yourself with both the analysis and design below.
Task 2: Party Invitation Program
Analysis
I have been asked to create a program which will get the the title, location, time and
date of a party and display these details in an output box in the form of a party invitation.
I will complete this task by carrying out the following
three
steps:
Step 1
: Setup the variables required to store the party details.
Step 2
: Display a message asking for the user to enter the title of their party. Once the
user has entered this, I will then display a message asking for party location and these
details will be entered. I will then ask the user a further two questions, these questions will ask for the
time and date of the party. I will expect the user to key in these details also.
Step 3
: All details entered will then be displayed in the form of an invitation in a field called “output”
Design
Here is the
design
showing the steps for the
Party Invitation Program
:
Stepwise Design
(the main steps of the program)
:
1.!Setup the variables (areas) to store the invitation details
2.!Get Invitation Details
3.!Display Invitation Details
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the variables (areas) to store the invitation details
1.1!Setup the variables called invitation_title, invitation_location, invitation_time, invitation_date
2.
Get Invitation Details
2.1!Ask for the title of the user’s party
2.2!User puts in the title of the party and this is stored in invitation_title
2.3!Ask for the location of the user’s party
2.4!User puts in the location of the party and this is stored in invitation_location
2.5!Ask for the time of the user’s party
2.6!User puts in the time of the party and this is stored in invitation_time
2.7!Ask for the date of the user’s party
2.8!User puts in the date of the party and this is stored in invitation_date
3.
Display Invitation Details
3.1!Display the text “You’re invited to ” followed by invitation_title and a return after field “output”
3.2!Display the text “The location is ” followed by invitation_location and a return after field “output”
3.3!Display the text “The time is ” followed by invitation_time and a return after field “output”
3.4!Display the text “The date is ” followed by invitation_date and a return after field “output”
3.5!Display the text “Don’t be late!!” and a return after field “output”
3.6!Display a blank line
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
9
Task 2: Party Invitation Program
Open the “
Party Invitation
” stack. It can be found in:
N4 LiveCode Programming Tasks > 2...Party Invitation.livecode
Your teacher will go over this program with you before you begin. Make sure you listen
carefully and work out what the code on the next page is causing the program to do.
As you progress through the tasks, they will start to get a little harder. If you run into
difficulties, for example, your program does not run, the line the error is on will be
highlighted for you. Try to work out for yourself what the problem could be
before

calling on the help of the teacher.
Implementation
Please Read!
Your
program will
go into the
script of this
button
Your invitation will be displayed in
this field labelled “output”
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
10
// Setup the variables to be used in this event.
global
party_title, party_location, party_time, party_date
on
mouseUp

// A list of the subroutines that will occur when the button has been pressed.
Get_Invitation_Details
Display_Invitation_Details
end
mouseUp
on
Get_Invitation_Details

// Get the party invitation details from the user.

ask

"Please enter the title of your party, for example, Easter Chicks Party"

put
it
into
party_title

ask

"Please enter the location of your party, for example, Electric Circus, Edinburgh"

put
it
into
party_location

ask

"Please enter the time of your party, for example, 8pm until 1am"

put
it
into
party_time

ask

"Please enter the date of your party, for example, 8th April 2010"

put
it
into
party_date
end
Get_Invitation_Details
on
Display_Invitation_Details

// Display the details of the party invitation in the output field.

put

"You're invited to "
& party_title &
return

after

field

"output"


put

"The location is "
& party_location &
return

after

field

"output"


put

"The time is "
& party_time &
return

after

field

"output"


put

"The date is "
& party_date &
return

after

field

"output"


put

"Don't be late!!"
&
return

after

field

"output"

put

return

after

field

"output"
end
Display_Invitation_Details
Click on the
“Edit Mode”
tool to
edit
the script for the Invitation
program.
Select the “
Create Invitation
” button to
edit
the
object

script
using the button on the top
toolbar.
Task 2: Party Invitation Program
Implementation (continued)
Your task is to add the program code shown below carefully. Watch your spelling as
all

your code will need to be
correct
in order for your program to run successfully!
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
11
Once your program is complete, check that it works correctly by clicking
on the ‘
Apply
’ button.
Fix
any errors in your program. The lines that contain
errors will be picked up by the translator for you to fix.
When you have no errors, you are ready to test that
your application program works correctly by running it.
Click on the “
Run Tool
” to run your application program. Click on
the ‘Create Invitation’ button and key in any data you wish. Your
program should show similar output to the screenshot below.
Task 2 (b): Party Invitation Program - 4 party Tickets using a loop
Save your program as 2...Party Invitation.
Testing
Task 2: Party Invitation Program
Go back into the script of the “
Create

Invitation
” button and
enter the
three

new

lines
shown below. This will setup a
fixed

loop
to produce the party ticket 4 times as well as
print

the number of the party ticket. Test that your program works
by ensuring that it produces four tickets with the ticket number.
REPEAT...
END REPEAT
Run Tool
Edit Tool
on
Display_Invitation_Details

// Display the details of the party invitation in the output field.

repeat
with ticket = 1 to 4

put
"This is ticket number: " & ticket &
return
after field "output"

put

"You're invited to "
& party_title &
return

after

field

"output"


put

"The location is "
& party_location &
return

after

field

"output"


put

"The time of this party is "
& party_time &
return

after

field

"output"


put

"The date of this party is "
& party_date &
return

after

field

"output"


put

"Don't be late!!"
&
return

after

field

"output"

put

return

after

field

"output"


end

repeat
end
Display_Invitation_Details
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
12
What are Variables?
Let’s talk about variables as they are
very
important in
programming.
To put it simply, a variable is a “box” into which data can be
placed whilst a program is
running
. We give these boxes
names which suggest or give us a clue as to what data is
being held in the box.
PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE
Here are the variables that we used in the Party Invitation Program.
Variable names
cannot
contain any
spaces
and must
not
be a
reserved
command in
LiveCode. You can tell if a variable has been accepted as it will appear in
black

font

colour when typed into the script window as shown below:
In order for the program to know which data is a variable and which is text to be printed
in a put statement, the ampersand
&
is used. This
separates
both the
variable
and the
text
to be
put
in an
output

field
as shown below:
put

"You’re invited to "

&
party_title
&

return

after

field

"output"
Variable Name
Data to be stored
put

"You’re invited to "
&
party_title
&
return

after

field

"output"

put

"The location is "
& party_location &
return

after

field

"output"

put

"The time of this party is "
&
party_time
&
return

after

field

"output"


put

"The date of this party is "
&
party_date
&
return

after

field

"output"
party_title
party_time
party_date
Steve’s 30th
Birthday Party
The Balmoral
Hotel
6pm until 1am
10th September
2014
party_location
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
13
So, what have we learned so far?
The LiveCode program area has three areas:!
1.!The
variable
list - lists all variables used in the program
2.!The
event
- this is a list of all subroutines which are run
!in order when the button is clicked on by the user.
3.!The
subroutines
- contain the lines of code to be executed.
PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE PAUSE
ASK
is a command that allows the programmer to ask the user a question
or ask the user for a response. For example:
ask

"Please enter the title of your party, for example, Easter Chicks
Party"
PUT
is a command that allows the programmer to transfer the users
response
(it)
into a meaningful
variable
. For example:
put

it
into
Invitation_Title
//

are used to put internal commentary into a program or to space out
different parts of the program to make it easier to read. For example:
// Print out the details of the party ticket
On
and
End
are used to
begin
and
end
a
subroutine
within an
event
.
For example:
on
Display_Invitation_Details

put

"You're invited to "
& Party_Title &
return

after

field

"output"


put

"The location is "
& Party_Location &
return

after

field

"output"


put

"The time is "
& Party_Time &
return

after

field

"output"


put

"The date is "
& Party_Date &
return

after

field

"output"


put

"Don't be late!!"
&
return

after

field

"output"

put

return

after

field

"output"
end
Display_Invitation_Details
LOOP
We know that one way to get one or more lines of code to repeat is a
loop
.
A
Repeat/End Repeat

l oop can be used to repeat as many ti mes as we
wi sh.
repeat

with
loop =
1

to

4

put

"Hello!"
end
repeat
Well Done! You are
now
a computer programmer!
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
14
Task 3: Address Labels Program
Specification
A program is required in order to produce
5 address labels
. The
details to be entered and displayed in a
label
are the contact name,
address, post code, city and phone number.
Analysis
I will do this task by dividing it up into
three
steps:
Step 1
:!I will setup the variables to be used in this program.
Step 2
:!I will display a message on the screen prompting for the user to enter the name. Once
the user has entered this, I will then display a series of other messages asking the user for their
address, post code, city and phone number. I will expect the user to key in these details also.
Step 3
:!All details entered will then be displayed 5 times using a loop in a field called “output”.
Design
Here is the
design
showing the steps for the
Address Labels Program
:
Stepwise Design

(the main steps of the program)
:
1.!Setup the variables (areas) to store the contact details
2.!Get Contact Details
3.!Display Contact Details
Stepwise Refinement

(breaking down the main steps into smaller steps)
:
1.
Setup the variables (areas) to store the contact details
1.1!Setup the variables called contact_name, contact address, contact_post_code, contact_city, contact_phone
2.
Get Contact Details
2.1!Ask for the name of the user
2.2!User puts in the name and this is stored in contact_name
2.3!Ask for the address of the user
2.4!User puts in the address and this is stored in contact_address
2.5!Ask for the post code of the user
2.6!User puts in the time and this is stored in invitation_time
2.7!Ask for the city of the user
2.8!User puts in the date and this is stored in the variable invitation_date
2.9!Ask for the telephone number of the user
2.10!User puts in the telephone number and this is stored in contact_phone_number
3.
Display Contact Details
3.1!
Start

a Repeat

with loop
1 to 5
3.2! Display the text “Label Number: ” followed by loop and a return after field “output”
3.3! Display the text “Name: ” followed by contact_name and a return after field “output”
3.4! Display the text “Address: ” followed by contact_address and a return after field “output”
3.5! Display the text “Post Code: ” followed by contact_post_code and a return after field “output”
3.6! Display the text “City: ” followed by contact_city and a return after field “output”
3.7! Display the text “Phone Number: ” followed by contact_phone and a return after field “output”
3.8! Display a blank line
3.9!
End Repeat
1
This is address label: 1
Name: Steve Whyte
Address: 12 High Street
Post Code: EH4 7FL
City: Edinburgh
Telephone Number: 01316656545
2
3
4
5
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
15
Task 3: Address Labels Program
Use the analysis and the design on the previous page to help you complete this task.
You will also find the code from
Task 2
helpful as both of these programs are very
similar.
Open the “
Address

Labels
” stack. It can be found in:
N4 LiveCode Programming Tasks > 3...Address Labels.livecode
Like the previous task, double click on the “
Create

Address

Label
” button and enter the
script for this button.
Test that your program works correctly by producing 5 address labels in the
output
field.
Save your program as 3...Address Labels
Implementation
Testing
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
16
Task 4: Apple Store
Specification
A program is required to ask for an employee name and work out the
amount of money that person gets over the course of a week
depending on their hourly wage and amount of hours worked.
The program should ask for the amount of hours worked over the week
and then the hourly rate. It should then calculate the amount that they
get a week and then display this information on the screen along with a suitable message.
Analysis
I have been asked to create a program to get an employees name and then work out
the amount of money they get over the course of a week depending on their hourly
wage and amount of hours worked. I will complete this task in
four
steps:
Step 1
:!Setup the variables to be used in this program
Step 2
:!Prompt the user for the employees name. Once the user has entered this, I will
then display another message asking for the number of hours they have worked. I will
then ask the user for their hourly rate. Each answer will be placed into a different
variable.
Step 3
:!In order to calculate the total pay, I will multiply the hours variable by the rate and place this into
the variable total_pay.
Step 4
:!The final step will involve displaying the users name and the total amount they have earned that
week in a field called “output”
Design
Here is the
design
showing the steps for the
Apple Store Program
:
Stepwise Design
(the main steps of the program)
:
1.!Setup the variables (areas) to store the employee details
2.!Get Data
3.!Calculate Total Pay
4.!Display Total Pay
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the variables (areas) to store the employee details
1.1!Setup the variables name_of_person, hours, rate and total_pay
2.
Get Data
2.1!Ask for the users name
2.2!User puts in their name and this is stored in name_of_person
2.3!Ask for the number of hours worked
2.4!User puts in the hours worked and this is stored in hours
2.5!Ask for their hourly rate
2.6!User puts in the hourly rate and this is stored in rate
3.
Calculate Total Pay
3.1!Put hours multiplied by rate into the variable total_pay
4.
Display Total Pay
4.1!Set the number format to pounds and pence
4.2!Put a message telling the name_of_person how much money they get using the variable
!total_pay into the field “output”
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
17
Task 4: Apple Store
Open the “
Apple Store
” stack. It can be found in:
N4

LiveCode Programming Tasks > 4...Apple Store.livecode
Implementation
Implementation (continued)
Assign
the following code to the “
Calculate Total Pay

button:
// Setup the global variables to be used in this event
global
name_of_person, hours, rate, total_pay
on
mouseUp

// A list of the subroutines that will occur when the button has been pressed.
get_data
calculate_total_pay
display_total_pay
end
mouseUp
on
get_data

// Get the users name, amount of hours worked that week and the users hourly rate.

ask

"Please enter your name, e.g. Steve Jobs: "

put
it
into
name_of_person

ask

"Please enter the number of hours you have worked in the Apple Store this week: "

put
it
into
hours

ask

"Please enter your hourly rate, e.g. 10.50 (no pound signs please!): "

put
it
into
rate
end
get_data
on
calculate_total_pay

// Multiply the hours by the rate. The answer will be placed in to total_pay.

put
hours * rate
into
total_pay
end
calculate_total_pay
on
display_total_pay

// Display the users name and total pay in the field "Output".

put

"Well "
& name_of_person &
":"
&
return

after

field

"output"

// Set total_pay to be displayed as pounds and pence

set

numberformat

to

"00.00"

put

"This week you have earned: £"
& total_pay &
return

after

field

"output"
end
display_total_pay
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
18
Task 4: Apple Store
Save your program as 4...Apple Store
Testing (continued)
Testing is a very important part of any programming project. A program would not sell
and would be useless if it did not produce the correct output when it was run.
Your Apple Store program is simple but must be tested to ensure that it produces the
correct results.
It is advisable that you check your calculations using a calculator first. You should then
run your program to check that you get the same results. This is a good indication that
your program is indeed working correctly.
You should now run your program three times with data below. Enter the sample data
for name, hours worked and hourly rate below and check that the results for “Total Pay”
are the same as the results produced in your program.
Name
Hours
Worked
Hourly
Rate
Calculated Total
Pay
Programs Total Pay
Susan
Wright
12
£8.00
£96.00
£96.00
Kevin
Robertson
30
£10.00
£300.00
£300.00
Allan Drain
39
£20.00
£780.00
£780.00
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
19
Task 5: Edinburgh Bowling Club
Specification
An Edinburgh Bowling Club would
like a simple program to work out if
their members qualify for a special
bowling tournament that takes
place on an annual basis.
The program should take in the
players
name
and the
scores
the
pl ay er obt ai ned i n
f our
games.
If
the
average

score
of their four
games is
greater
or
equal
to the
qualifying

score
which is set at
100
, then they qualify
to compete in the bowling tournament.
If
their
average

score
is
less

than

100
, then they are asked to train harder and try
again for the competition next year.
Analysis
The
first
step is to setup the variables used in the program.
The
second
step is to prompt the user for their name and take in the
scores from each of their four games.
The
third
step is to calculate the average score of the four games by
adding up the scores from each game and dividing by four.
The
fourth and final
step involves making a decision as to whether the player qualifies
for the bowling tournament by using an IF statement to compare the players average
score with the qualifying score and displaying a suitable message.
The Design for the above program is on the next page
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
20
Task 5: Edinburgh Bowling Club
Design
Here is the
design
showing the steps for the
Edinburgh Bowling Club
:
Stepwise Design

(the main steps of the program)
:
1.!Setup the Variables
2.!Setup Qualifying Score
3.!Get Player Data
4.!Calculate Average Score
5.!Decide if Qualified
Stepwise Refinement

(breaking down the main steps into smaller steps)
:
1.
Setup the Variables
1.1!Setup the variables called players_name, game1, game2, game3, game4,
!average_score and qualifying_score
2.
Setup Qualifying Score
2.1!Put the value 100 into the variable qualifying_score
3.
Get Player Data
3.1!Ask for the the players name
3.2!User puts in the players name and this is stored in the variable players_name
3.3!Ask for the score for game 1
3.4!User puts in the score and this is stored in the variable game1
3.5!Ask for the score for game 2
3.6!User puts in the score and this is stored in the variable game2
3.7!Ask for the score for game 3
3.8!User puts in the score and this is stored in the variable game3
3.9!Ask for the score for game 4
3.10!User puts in the score and this is stored in the variable game4
4.
Calculate Average Score
4.1!Put game1 plus game2 plus game3 plus game4 divided by 4 into the variable
!average_score
5.
Decide if Qualified
5.1!Display the message “Here is the decision for player named ” followed by the variable
!players_name and a return after field “output”
5.2!
If
the average_score is greater or equal to the qualifying_score
then
5.3! Display the message “You’ve qualified as your average score was ” followed by the
! variable average_score “, well done.” after field “output”
5.4!
Else
5.5! Display the message “You have not qualified as your average was” followed by the
! variable average_score “, try harder next year.” after field “output”
5.6!
End If
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
21
Task 5: Edinburgh Bowling Club
Open the “
Edinburgh Bowling Club
” stack. It can be found
in:
N4 LiveCode Programming Tasks > 5...Edinburgh Bowling Club.livecode
Implementation
Implementation
Assign
the following code to the “
Enter Scores
” button:
// Setup the global variables to be used in this event
global
players_name, game1, game2, game3, game4, average_score, qualifying_score
on
mouseUp
setup_qualifying_score
get_player_data
calculate_average_score
decide_if_qualified
end
mouseUp
on
setup_qualifying_score

// Assign it the value of 100 to the variable Marathon_Miles

put

100

into
qualifying_score
end
setup_qualifying_score
on
get_player_data

// Get the players name and scores for each of their four games

ask
"Please enter the players name"

put
it
into
players_name

ask
"Please enter score for game 1"

put
it
into
game1

ask
"Please enter score for game 2"

put
it
into
game2

ask
"Please enter score for game 3"

put
it
into
game3

ask
"Please enter score for game 4"

put
it
into
game4
end
get_player_data
on
calculate_average_score

// Find the average of the four game by adding all them up and then

// dividing the total by 4. Put the answer in the variable average_score

put
(game1+game2+game3+game4) /
4

into
average_score
end
calculate_average_score
on
decide_if_qualified

// Display a message with the players name

put

"Here is the decision for player named "
& players_name &
return

after

field

"output"

// Use an IF statement to determine if the player qualifies for the bowling tournament.

if
average_score >=qualifying_score
then

put

"You’ve qualified as your average was "
& average_score &
". Well done!"

after

field

"output"

else

put

"You’ve not qualified as your average was "
& average_score &
". Try harder!"

after

field

"output"

end

if
end
decide_if_qualified
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
22
Task 5: Edinburgh Bowling Club
Names
Points From
Four Games
Calculated
Average
Expected
Decision
Program
Average
Program
Decision
Allan Drain
90
120
101
99
102.5
Ready for
Tournament
102.5
Ready for
Tournament
Steven Whyte
50
80
89
92
77.75
Not Ready
for
Tournament
77.75
Not Ready
for
Tournament
Shona Valentine
120
127
123
115
121.25
Ready for
Tournament
121.25
Ready for
Tournament
Louise Taylor
55
75
68
63
65.25
Not Ready
for
Tournament
65.25
Not Ready
for
Tournament
You should now test your program with the following sets of test data in the table below.
Remember - when it comes to your coursework, you will be expected to work out the answers using a
calculator first and then run your program to ensure you get the same answer.
Testing
Save your program as 5...Edinburgh Bowling Club
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
23
Task 6: Cabins R Us
Specification
A program has to be created to allow the user to choose a log cabin.
Cabins can be booked for up to 7 days and come in three types and
prices per night - Standard, Premier and Luxury.
The program will also ask for the surname of the customer. It will then
display the cost of the cabin over a 7 day period. If the customer rents
a Luxury Cabin then they get a free bottle of champagne with the cabin
rental.
Stepwise Design
(the main steps of the program)
:
1.!Setup the variables
2.!Get Cabin Details
3. !Display Cabin Details
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the variables
1.1!Set the variables
cabin_type
,
surname
and
cabin_cost
2.
Get Cabin Details
2.2!Put the result into the variable cabin_type
2.3!Ask the user for their surname
2.1!Ask the the user for the cabin type (Standard, Premier or Luxury)
2.4!Put the result into the variable surname
2.5!Ask the user for the cabin cost per day
2.6!Put the result into the variable cabin_cost
!
3.
Display Cabin Details
3.1!Display “Customer Surname: ” followed by the variable surname in line 1 of field “output1”
3.2!Display ‘Cabin Type: ” followed by the variable cabin_type in line 2 of field “output1”
3.3!Display “Day”
tab
“Cost(£)” an a return after field “output2”
3.5!
Start

a

Repeat with

loop
1 to 7 days
3.6! Put a message onto the screen with the day number
tab
and day * cabin cost into field “output2”
3.7!
End

Repeat
3.4!
If
cabin_type is equal to “Luxury”
then
display “Free bottle of champagne!” into line 3 of field “output2”
Design
The design (pseudocode) of the program is shown below. Study it carefully and make sure you know what is going
on:
Analysis
I have been asked to create a program which will allow a user to choose a log cabin.
The
first
step will involve setting up the variables to be used in the program.
The
second
step will involve getting the details of the cabin. The details will include the type of
cabin the user is after followed by customers surname and the cost of the cabin per day.
The
third

and

final
step will involve putting a message on the screen displaying the cabin type,
number of occupants and cabin cost.
I will then use an IF statement to display a message informing the user that they get a bottle of champagne if the
type of cabin ordered is a Luxury Cabin.
I will then start a loop for 1 to 7 days. Within this loop I will multiply the number of days by the cabin cost to show the
cost of the cabin over a 7 day period.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
24
Task 6: Cabins R Us
Implementation - Creating the user interface
You are now going to
create
the
buttons
to go onto the stack.
Read
the following instructions
carefully
to show you what to do:
Ensure that you are in
Edit

Mode
. Create
two
buttons named
“Get
Details”
and
“Clear”
as shown in the stack stack below. To do this,
drag
the
button icon from the
tools
panel and place it on the
right

hand

side
of the
stack
as shown in the example stack below.
Double

click
on the
“ G e t D e t a i l s ”
button to open up the
Basic

Properties
and ensure the
name
and
label and tooltip
for each box reads as
Get Details
.
Do the same for the
“Clear”
button, expect label it as
Clear
.
Open the “
Cabins R Us
” stack. It can be found in:
N4 LiveCode Programming Tasks > 6...Cabins R Us.livecode
Once you have created the buttons and the output fields, enter the code on the next page into
the “
Get Details
” button.
This field is called
output1
This field is
called
output2
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
25
Task 6: Cabins R Us
Implementation - Creating the program code
You are now ready to begin developing the program script for the “
Get

Details
” button.
This program is below and should be keyed carefully. The teacher will go over the
program with you below before you being the coding.
Test that your program is working correctly using the test data supplied on the next
page.
// Setup the global variables to be used in the Cabins R Us program
global
surname, cabin_type, cabin_cost,
on
mouseUp
get_details
display_details
end
mouseUp
on
get_details

// Get the cabin details from the user

ask

"Please enter your surname: "

put
it
into
surname

ask

"Please enter cabin type (Standard, Premier, Luxury): "

put
it
into
cabin_type

ask

"Please enter cost per day: "

put
it
into
cabin_cost
end
get_details
on
display_details

// Display the details the user has entered in the Output Details field

put

"Customer Surname: "
& surname
into

line

1

of

field

"output1"

put

"Cabin Type: "
& cabin_type
into

line

2

of

field

"output1"


// Display the headings Day and Cost

put

"Day "
&
tab
&
"Cost (£)"
&
return

after

field

"output2"


// Loop for the 7 days of the week the price of the cabin

// The price for each day is equal to the day * the cost per day

repeat

with
day =
1

to

7

put

"Day "
& day &
tab
&
"£"
& day * cabin_cost &
return

after

field

"output2"

end

repeat


// If the user chooses the Luxury Cabin then they get a a free bottle of champagne

if
cabin_type =
"Luxury"

then

put

"Free bottle of champagne!"

into

line

3

of

field

"output2"
end
display_details
You are now going to create the code to allow the user to clear the output fields.
Remember
this code as it will be used in the same way in other programs depending
on the name of your
output

fields
. Key the following code into the “
Clear
” button:
Create the Clear button
on
mouseUp

put

empty

into

field

"output1"

put

empty

into

field

"output2"
end
mouseUp
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
26
Testing
You should now fully test your program with the test data below. Make sure your
program

output
is the
same as the
expected

output
which is
worked
out in your
head
or by using a
calculator
.
Save your program as 6...Cabins R Us
Fit for Purpose
The Cabins R Us Program is fit for purpose. It successfully asks for and takes in the type of
cabin, number of occupants and cabin cost. The program also successfully works out the cost
of the cabin over a seven day period and displays the results on the screen.
The program makes use of an IF statement to decide whether or not the customer gets a
complimentary bottle of champagne and food hamper if they select a Luxury Cabin. This also
works correctly as shown above.
Suggested Improvements
The program could be improved by asking the user which dates they would like to hire the cabin
from. The program could also include a picture of the cabin the customer intends to hire along
with the exact location of the cabin.
Evaluation
Task 6: Cabins R Us
Customer
Surname
Type of
Cabin
Cabin
Cost
(£)
Expected Output
Expected Output
Expected Output
Program Output
Program Output
Program Output
Drain
Standard
20
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£20
£40
£60
£80
£100
£120
£140
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£20
£40
£60
£80
£100
£120
£140
Whyte
Premier
50
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£50
£100
£150
£200
£250
£300
£350
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£50
£100
£150
£200
£250
£300
£350
McKay
Luxury
Message
Produced
100
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£100
£200
£300
£400
£500
£600
£700
1 Day
2 Days
3 Days
4 Days
5 Days
6 Days
7 Days
£100
£200
£300
£400
£500
£600
£700
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
27
Task 7: Who’s on Detention?
Specification
A program has to be created to work out the number of pupils
who should be placed on a
lates

detention
in a class of
8
pupils
.
A pupil is placed on a lates detention
IF
their number of lates
over Monday to Friday is
greater
than the
lates_trigger
. Thi s i s
set at 2 and wi l l tri gger that the pupi l has a detenti on to compl ete.
The program wi l l then di spl ay the number of pupi l s who have
been pl aced on a l ates detenti on.
Stepwise Design
(the mai n steps of the program)
:
1.
Setup the Variables
2. Enter Data
3. Display Result
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the Variables
1.1!Setup the variables lates, lates_trigger, and detentions
1.2!Put the value 0 into the variable detentions
1.3!Put the value 2 into the variable lates_trigger
2.
Enter Data
2.1!
Start a Repeat

with loop
1 to 8 pupils
2.2! Prompt the user for the number of lates for the pupil
2.3! Put the value lates into a new line in the field called “output1”
2.4!
If
the value of lates is greater than the lates_trigger
then
add 1 to detentions variable
2.5!
End Repeat !
3.
Display Result
3.1!Display a message “ The number of pupils on detention this week is: ” followed by the variable
!detentions into the field “output2”
Design
The design (pseudocode) of the program is shown below. Study it carefully and make sure you know
what is going on:
Analysis
I have been asked to create a program which will work out the number of pupils in a
class of 8 who should be placed on lates detention.
The
first

step
will involve setting up the variables to be used in the program.
The
second

step
will involve setting up a loop for the 8 pupils. For each pupil, the user
will be prompted for the number of times (if any) they were late to registration over the
course of the five day week.
If the value is greater than the number of lates required for detention (2) then one will be added to the
detentions.
The
third

and

final
step will involve displaying the number of pupils of the 8 that should be placed on
detention.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
28
Task 7: Who’s on Detention?
‘Create
two

buttons
and label them
Number of Lates
and
Clear
.
Once your program buttons have been added, your stack
should look similar to the screenshot shown below.
Implementation - Creating the user interface
You are now ready to begin developing the program script for the “
Number of Lates

button. This program is on the next page and should be keyed carefully into the script
for “
Number of Lates
”.
You should also create a script for the “
Clear
” button. Use the previous program to help
you create the simple script to clear both the “
output1
” and “
output2
” fields.
Open the “
Who’s on Detention
” stack. It can be found in:
N4 LiveCode Programming Tasks > 7...Who’s on Detention.livecode
Implementation - Creating the program code
This field is called
output2
This field is called
output1
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
29
Task 7: Who’s on Detention
Implementation - Creating the program code
// Setup the global variables to be used in this event
global
lates, lates_trigger, detentions
on
mouseUp
set_up
enter_data
display_result
end
mouseUp
on
set_up

// This value will change for each pupil depending on the number of lates

put

0

into
detentions

// Setup the lates trigger to equal the value of 2.

put

2

into
lates_trigger
end
set_up
on
enter_data

// Setup a fixed loop of 8 pupils

repeat

with
loop =
1

to

8

ask

"Please enter the number of lates by pupil number "
& loop


// If the cancel button is pressed, go back to the start of the program.

if

the

result
=
"Cancel"

then

exit

to

top

put
it
into
lates

put
lates
into

line
loop
of

field

"output1"

// If the number of lates entered by the user is greater than the lates trigger

// then add 1 to the variable detentions.

IF
lates > lates_trigger
THEN

add

1

to
detentions

end

repeat
end
enter_data
on
display_result

put

"The number of pupils on detention this week is "
& detentions
into

field

"output2"
end
display_result
Once you have keyed in all of the code for the
“Get Details”
button,
create the script to clear the
“output1”
and
“output2”
fields when the Clear button is
pressed. Use the script at the bottom of page 24 to help you if you have forgotten.
Test that your program produces the correct output using the test data supplied on the
next page.
Create the Clear button
Testing
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
30
Task 7: Who’s on Detention
Test that your program produces the correct output using the test data supplied below.
Number of Lates by
Each Pupil
Expected Number of
Detentions
Programs Number of
Detentions
0, 1, 0, 0, 2, 0, 1, 1
0
0
2, 3, 2, 2, 3, 3, 5, 4
5
5
3, 3, 5, 4, 4, 5, 4, 3
8
8
Save your program as 7...Who’s on Detention
Fit for Purpose
The Who’s on Detention Program is fit for purpose. It successfully asks for and takes in
the number of lates for each of the 8 pupils. It will then successfully compare the lates
value with the lates trigger and if it is over two, a detention will be issued. The program
issues the correct number of detentions and this has been shown in the testing table
above.
Suggested Improvements
The program could be improved upon by asking the user for the names of the pupils on
detention so teachers knew who was going to be turning up.
Evaluation
Testing (continued)
Congratulations!
You are now ready to move onto the two General Coursework exercises.
Credit level students should complete the next three tasks.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
31
Task 8A: Pass or Fail
Specification
A program is to be written to work out
whether or not a student has passed or
failed a Computing course.
The program should take in the students
two marks which are out of 40.
The program should then work out the
average of these two marks by adding
them together and diving the answer by 2.
Finally, the student’s average mark should
be compared with the passmark.
If the average mark is greater or equal to the passmark, the student should pass the
course, else the student should fail the course.
Analysis
I have been asked to create a program which will work out whether or not
a computing student has passed or failed a Computing course.
The
first

step
is to setup the variables to be used in the program.
The
second

step
will involve setting up the passmark to hold the value of
20 as a pass in the course is half marks or more.
The
third

step
will involve asking the user for the student’s marks in the two exams out
of 40.
Once each mark has been entered, the
fourth

step
will involve working out the
student’s average mark by adding up the two marks and dividing them both by 2. This
answer will be placed into the average mark variable.
The
fifth and final step
of this program will involve using an IF statement to make a
decision as to whether or not the student has passed or failed the first two sections of
the course. I will do this by comparing the passmark with the average mark.
A suitable message will be displayed of passed or failed depending on the students
percentage mark along with their average mark.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
32
Stepwise Design
(the main steps of the program)
:
1.!Setup the Variables
2.!Set the Passmark
3.!Get the Marks
4.!Calculate Average
5.!Make Decision
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the Variables
1.1!Setup the variables of passmark, computer_software, software_development, student_average
2.
Set the Passmark
2.1!Set Passmark to 20
3.
Get the Marks
3.1!Ask for the student’s test mark in the Computer Software unit (0-40)
3.2!Put the mark entered into the variable computer_software
3.3!Ask for the student’s test mark in the Software Development unit (0-40)
3.4!Put the mark entered into the variable software_development
4.
Calculate Average
4.1!Put the computer_software mark plus the software_development_mark divided by 2 into the variable
!student_average
5.
Make Decision
5.1!Display “The student’s Computer Software mark was: ” followed by the variable computer_software
5.2!Display “The student’s Software Development mark was: ” followed by the variable software_development
5.3!
If
the student_average is greater or equal to the passmark
then
5.4! Display “The student has passed :-) with an average of ” student_average into line 4 of field “output”
5.5!
Else
5.6! Display “The student has failed :-( with an average of ” student_average into line 4 of field “output”
5.7!
End If
Task 8A: Pass or Fail
Design
Remember, the design of a program is very important as it allows the programmer to think about the
structure of the program before the code is produced.
As mentioned earlier, the design of a program is known as
Pseudocode
. Pseudocode is a cross
between
programming

language
and our own
Engl i sh

language
and makes a program
easier
to
u n d e r s t a n d
w i t h o u t r e l y i n g o n t h e u s e o f a p r o g r a m s c o m p l e x c o m m a n d s.
T h e d e s i g n i s b u i l t u p o f t w o p a r t s, t h e fi r s t i s t h e
S t e p w i s e d e s i g n
. T h i s s h o w s t h e
ma i n s t e p s
of the
program. The second part is the
Stepwise Refinement
. This involves breaking these main steps into
even smaller steps so it can be taken and programmed in any programming environment.
The following design is for
Computing Exam Marks Program
. Study it closely as you will need to
produce a similar design for your programs when it comes to the practical coursework.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Implementation - Creating the user interface
Open the “
Computing Exam Marks
” stack.
It can be found in:
N4 LiveCode Programming Tasks > 8...Pass or Fail.livecode
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
33
Task 8A: Pass or Fail
‘Create
two

buttons
and label them “
Enter Marks
” and “
Clear
”.
Once the objects have been added, your stack should look similar to
the screenshot shown below.
You are now ready to begin developing the program script for the

Enter Marks
” button. This program is on the next page and should be
keyed
carefully
.
You should also create a script for the “
Clear
” button. Use the previous programs to
help you create the simple script to clear the “
output


field.
Implementation - Creating the program code
Implementation - Creating the user interface (continued)
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
This field is called
output
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
34
Task 8A: Pass or Fail
// Setup the global variables to be used in this event
global
passmark, computer_software, software_development, student_average
on
mouseUp
set_the_passmark
enter_results
calculate_average
make_decision
end
mouseUp
on
set_the_passmark

// Set the passmark of the Computing course

put

20

into
passmark
end
set_the_passmark
on
enter_results

// Get the student's Computer Software mark out of 40

ask
"What was the Computer Software mark for this student (out of 40)?"

put
it
into
computer_software

// Get the student's Software Development mark out of 40

ask
"What was the Software Development mark for this student (out of 40)?"

put
it
into
software_development
end
enter_results
on
calculate_average

// Calculate the student's average mark

put
(computer_software+software_development)/
2

into
student_average
end
calculate_average
on
make_decision

// Put the marks into the output field

put

"The student’s Computer Software mark was: "
& computer_software
into

line

1

of

field

"output"

put

"The student’s Software Development mark was: "
& software_development
into

line

2

of

field

"output"

// Using an IF statement to compare the student's average mark with the passmark
// Make a decision as to whether the student has passed or failed the course

if
student_average>=passmark
then

put

"The student has passed :-) with an average of "
& student_average
into

line

4

of

field

"output"


else

put

"The student has failed :-( with an average of "
& student_average
into

line

4

of

field

"output"

end

if
end
make_decision
Implementation - Creating the program code (continued)
Assign the following script to your “
Enter Marks
” button.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Now create a button to
clear
the “
output
” field. Use previous programs
to help you create this very simple one line script. Apply this script to
the clear button and
test
that it works correctly by using the test data
shown on the next page.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
35
Task 8A: Pass or Fail
Testing
You should now test your program with the test data below. Ensure that your program produces
the correct results for the average mark and the course decision.
It is always best to check your program’s calculations using a calculator first before running your
program to get the answers. If your program’s answer matches the answer that was produced
on the calculator, then you can assume your program is working correctly.
Test your program using the same test data below. If you get the same answers then you can
move on to task 8(b) which involves adding some more features to this program.
Computer
Systems Mark
Software
Development Mark
Calculated
Average Mark
Program
Average Mark
Course Decision
(Pass/Fail)
12
12
12
12
Fail
20
20
20
20
Pass
40
40
40
40
Pass
19
20
19.5
19.5
Fail
Save your program as 8...Pass or Fail
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
36
Task 8B: Pass or Fail (Percentage and Validation)
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Specification
The program that you have just created needs
some
new

features
added to it.
The teacher who has been using the program
would like each of the two marks entered to
be
validated
as being
whole

numbers

between the
range
of
0
to
40
. If a user enters
an invalid mark, e.g. 12.5 or 42 then an error
message will appear and the user will be
given the opportunity to
re-enter
the mark.
The teacher would also like the program to
work out and display the students overall
percentage

mark
.
This
percentage

mark
should be
displayed
along with the
pass/fail

decision
.
Analysis
I have been asked to expand on my previous program by adding two new
features.
The
first

step
will be to Setup two additional variables to be used
alongside the original ones, these will be called student_percentage and
check_number.
The
second

step
will involve calling up the validation function for each of the marks
each of the marks the user enters. Each mark will be placed into a temporary variable
called check_number. The check_number will then be validated as being a whole
number between 0 and 40. When valid check_number will be put into the original
variable for the marks.
Once each mark has been entered and validated, the
third

step
will involve working out
the student’s percentage mark by adding up the two marks and dividing the answer by
80 (the total of both tests), and then multiplying the answer by 100%. This answer will
be placed into the student_percentage variable.
The
fourth and final step
of this program will involve displaying the percentage along
with the decision based on the students average mark.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
37
Task 8B: Pass or Fail (Percentage and Validation)
Stepwise Design
(the main steps of the program)
:
1.!Setup the Variables
2.!Set the Passmark
3.!Enter Results
4.!Validate
5.!Calculate Average
6.!Calculate Percentage
7.!Make Decision
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the variables
1.1!Setup the variables of passmark, computer_software, software_development, student_average,
!
student_percentage and passmark
2.
Set the Passmark
2.1!Set Passmark to 20
3.
Enter Results
3.1!Ask for the student’s test mark in the Computer Software unit (0-40)
3.2!Put the mark entered into the temporary variable check_number
3.3!Call the
validate
function
3.4!Put the check_number into the variable computer_software
3.5!Ask for the student’s test mark in the Software Development unit (0-40)
3.6!Put the check_number into the temporary variable check_number
3.7!Call the
validate
function
3.8!Put the check_number into the variable software_development
4. Validate
4.1!
Repeat
until check_number is between 0 and 40 and is a whole number
4.2! Display “Your number is not allowed, please re-enter a whole number between 0 and 40”
4.3! Put it into check_number
4.4!
End Repeat
5.
Calculate Average
5.1!Put the computer_software mark plus the software_development_mark divided by 2 into the variable
!student_average
6. Calculate Percentage
6.1!Put the computer_software mark plus the software_development_mark divided by 80 multiplied by 100%
!into the variable student_percentage
7.
Make Decision
7.1!Display “The student’s Computer Software mark was: ” followed by the variable computer_software
7.2!Display “The student’s Software Development mark was: ” followed by the variable software_development
7.3!
If
the student_average is greater or equal to the passmark
then
7.4! Display “The student has passed :-) with an average of ” student_average into line 4 of field “output”
7.5!
Else
7.6! Display “The student has failed :-( with an average of ” student_average into line 4 of field “output”
7.7!
End If
7.8!Display “The student’s overall percentage was ” followed by the variable student_percentage
Design
The design for the program is shown below. The
new

lines
that have been added to the
program design have been
highlighted
. Study the design below carefully and note how
the validate function works. The code is supplied on the next page.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
38
Task 8B: Pass or Fail (Percentage and Validation)
global
passmark, computer_software, software_development, student_average,
student_percentage,
check_number
on
mouseUp
set_the_passmark
enter_results

validate
calculate_average

calculate_percentage
make_decision
end
mouseUp
on
set_the_passmark

put

20

into
passmark
end
set_the_passmark
on
enter_results

ask
“What was the Computer Software mark for this student (out of 40)?"

put
it into check_number

// Start the validate function
validate

put
check_number into computer_software

ask
“What was the Software Development mark for this student (out of 40)?"

put
it into check_number

// Start the validate function
validate

put
check_number into software_development
end
enter_results
on
validate

// Validate each mark as being a whole number between 0 and 40

repeat
until check_number >= 0 and check_number <= 40 and check_number is an integer

ask
"Your number is not allowed, please re-enter a whole number between 0 and 40"

// If the cancel button is pressed, go back to the start of the program.

if
the result = "Cancel"
then

exit
to top

put
it into check_number

end

repeat
end
validate
on
calculate_average

put
(computer_software+software_development)/
2

into
student_average
end
calculate_average
on
calculate_percentage

put
(computer_software+software_development)/80 * 100 into student_percentage
end
calculate_percentage
on
make_decision

put

"The student’s Computer Software mark was: "
& computer_software
into

line

1

of

field

"output"

put

"The student’s Software Development mark was: "
& software_development
into

line

2

of

field

"output"

if
student_average>=passmark
then

put

"The student has passed :-) with an average of "
& student_average
into

line

4

of

field

"output"

else

put

"The student has failed :-( with an average of "
& student_average
into

line

4

of

field

"output"

end

if

put
"The student’s overall percentage was " & student_percentage & "%" into line 5 of field "output"
end
make_decision
Implementation - Creating the program code
Add
the
highlighted
lines to the script of your “
Enter Marks
” button.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
39
Task 8B: Pass or Fail (Percentage and Validation)
Testing
Save your program as 8...Pass or Fail
Fit for Purpose
The Pass or Fail Program is fit for purpose. It successfully validates the two marks as being whole numbers between
0 and 40.
The program also successfully works out the course average and percentage as can be seen with the table above. I
worked out the average and percentage marks using a calculator and then ran my program with the test data to
ensure that the same results were given.
The program makes use of an IF statement to successfully decide whether the student has passed or failed the
Computing course based on their average as shown above.
Suggested Improvements
The program can be improved by perhaps taking in the student’s name so the teacher knew exactly who passed and
who failed the Computing course.
Evaluation
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Computer
Software
Mark
Software
Development
Mark
Calculated
Average
Mark
Calculated
Percentage
Mark
Program
Average Mark
Program
Percentage
Mark
Course
Decision
51
N/A
Invalid
Computer
Software
Mark
Invalid
Computer
Software Mark
Invalid
Computer
Software Mark
Invalid
Computer
Software Mark
N/A
12
10.5
Invalid
Software
Development
Mark
Invalid
Software
Development
Mark
Invalid
Software
Development
Mark
Invalid Software
Development
Mark
N/A
-10
N/A
Invalid
Computer
Software
Mark
Invalid
Computer
Software Mark
Invalid
Computer
Software Mark
Invalid
Computer
Software Mark
N/A
28
1001
Invalid
Software
Development
Mark
Invalid
Software
Development
Mark
Invalid
Software
Development
Mark
Invalid Software
Development
Mark
N/A
28
38
33
82.5%
33
82.5%
PASS
12
10
11
27.5%
11
27.5%
FAIL
20
40
30
75%
30
75%
PASS
Complete the testing table below by keying in the values for Computer Systems and Software Development. This will
ensure your validation function works correctly and your program calculates the average and percentage correctly.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
40
Task 9A: High, Low, Spot On!
Specification
The computer has chosen a secret number
which is between 0 and 100 and you have
to try to guess what it is.
You will create a program that will take in
the users
guess
and decide if the guess is
lower
,
higher
or
equal
to that of the
r a n d o m n u m b e r t h e c o m p u t e r i s t h i n g o f.
A m e s s a g e o f “
Too

High
”, “
Too

Low
” or

Spot

On
” will be displayed in the programs
output field. If the users guess is too high
or too low, they will be given the opportunity to re-enter another number.
This simple game will then count up the number of guesses and display this number in
the programs output field once the user has correctly guessed the computers number.
Analysis
I have been asked to create a program which will allow a user to try and
guess a randomly generated number between 0 and 100.
I will do this task in
three
steps:
The
first
step will involve setting up the variables to be used in the
program.
The
second
step is to put a randomly generated number into the variable
random_number and then put the value 0 into the variable guess_count.
The
third
step will involve the use of a repeat...until loop to ask the user their guess
between 0 and 100. Each time a guess is made, it will be compared to the randomly
generated number and display a suitable message of “Too High”, “Too Low” or “Spot
On”.
The game will terminate when the user has guessed the correct random number and
they will be shown how many guesses they made.
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
41
Task 9A: High, Low, Spot On!
Design
Here is the
design
showing the steps for the
High, Low, Spot On Program
:
Stepwise Design
(the main steps of the program)
:
1. Setup the variables
2. Setup Game
3. Play Game
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Setup the variables
1.1!Setup the variables guess_count, random_number and my_guess
2.
Setup Game
2.1!Put the value 0 into the variable guess_count
2.2!Put a random number between 0 and 100 into the variable random_number
3.
Play Game
3.1!
Start a Repeat

loop

until
the my_guess is equal to random_number
3.2! Ask for the users guess between 0 and 100.
3.3!
If
the cancel button is pressed
then
exit to the top of the program
3.4! Put the result into the variable my_guess
3.5!
If
my_guess is greater than the random_number
then
put “Too High” and a return
! before field “output1”
3.6!
If
my_guess is less than the random_number
then
put “Too Low” and a return
! before field “output1”
3.7!
If
my_guess is equal to the random_number
then
put “Spot On!” and a return
! before field “output1”
3.8! Add 1 to the variable guess_count after each guess
3.9!
End Repeat
3.10!Put a message showing the users guess_count into field “output2”
Implementation
Open the “
High Low Spot On
” stack. It can be found in:
N4 LiveCode Programming Tasks > 9...High Low Spot On.livecode
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
42
Before you begin creating the program script, you need to
create

two

buttons
to assign
the program script to. The first button will be called
Play Game
and the second button
will be called
Clear
. This will clear the output fields (white boxes) so the game can be
played again.
Make sure you are in “
Edit Mode
”. Drag the button object
onto the stack and double click it and change the name,
label and tooltip to “Play Game”. Do the same with the

Clear
” button.
Your program stack should look similar to the screenshot
below:
Implementation (continued) - Creating the user interface
Task 9A: High, Low, Spot On!
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
This field is called
output1
This field is called
output2
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
43
Implementation
// Setup the global variables to be used in the High, Low Spot On program
global
guess_count, random_number, my_guess
on
mouseUp
setup_game
play_game
end
mouseUp
on
setup_game

// This variable will count the number of guesses the user has taken.

put

0

into
guess_count

// This variable will hold a random number up to 100.

put

random
(
100
)
into
random_number
end
setup_game
on
play_game

// This loop will keep repeating until the user guesses the correct number.

repeat

until
my_guess = random_number

ask

"Please enter your guess between 0 and 100"

if

the

result
=
"Cancel"

then

exit

to

top

put
it
into
my_guess

// The IF statements will compare the users guess with the programs

// randomly generated number and display an appropriate message.

if
my_guess>random_number
then

put
my_guess &
" --> Too High"
&
return

before

field

"output1"

if
my_guess<random_number
then

put
my_guess &
" --> Too Low"
&
return

before

field

"output1"

if
my_guess=random_number
then

put
my_guess &
" --> Spot On!!"
&
return

before

field

"output1"

// Once the user has had a guess, 1 will be added to the variable "guess_count".

add

1

to
guess_count

end

repeat

put

"You found the number in "
& guess_count &
" guesses."

into

field

"output2"
end
play_game
Assign
the following code to the “
Play Game
” button:
Task 9A: High, Low, Spot On!
on
mouseUp

// This program will put "empty" into the output fields

put

empty

into

field

"output1"

put

empty

into

field

"output2"
end
mouseUp
Assign
the following code to the “
Clear
” button:
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
44
Testing
You should now test your program with similar test data to that below. The test data
table below
assumes
that the
number

8
has to be found.
You should test your program with similar test data making sure that both the correct
messages are displayed when a guess is made and the correct guess count is given at
the end of the game.
My Guess
Output
Guess Count
150
Too High
1
100
Too High
2
60
Too High
3
0
Too Low
4
1
Too Low
5
20
Too High
6
15
Too High
7
10
Too High
8
8
Spot on!
9
Save your program as 9...High Low Spot On
Task 9A: High, Low, Spot On!
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
45
Task 9B: High, Low, Spot On (Extension Exercise)
Specification
The High, Low Spot On program needs
one

further
improvement
.
At the moment, it is possible for the user to
enter a guess below 0 or above 100.
You should create a validate function to
validate the users guess as being a whole
number between 0 and 100 using the same
function as you used in task 8(b).
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Testing
You should now test your program with similar test data to that below. The test data
table below
assumes
that the
number

8
has to be found.
You should test your program with similar test data making sure that the validate
function works correctly. Notice that the guess count stays the same after an invalid
guess has been made. It will only increase by one once a valid guess has been made.
My Guess
Valid Guess?
Output
Guess Count
150
No
Invalid Number
0
100
Yes
Too High
1
-50
No
Invalid Number
1
50
Yes
Too High
2
0
Yes
Too Low
3
25
Yes
Too High
4
15.24
No
Invalid Number
4
10
Yes
Too High
5
8
Yes
Spot on!
6
Save your program as 9...High Low Spot On
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
46
Task 10: Music Shop Takings (Logical Operators)
A music store sells CD’s, DVD’s and Blu-Ray disks. A program is required
to take in the total number of CD’s, DVD’s and Blu-Ray Disks sold over the
course of the working day. The program should then find the combined total
cost of CD’s (£7.99), DVD’s (£10.99) and Blu-Ray Disks (£14.99) sold.

IF
the team’s total profit is greater than
£1000
and either the
DVD’ s
sol d
i s gr eat er or equal t o
50
or the
Bl u - Ra y ’ s
s o l d i s g r e a t e r o r e q u a l t o
30

then
the team are entitled to a
bonus
.

ELSE

They have
not
made
enough

money
on today’s takings to be eligible for a
bonus
.
Pay special attention to the
IF

statement
within the
decide

subroutine
as it uses the
logical

operators

of
AND
and
OR
in order to decide
IF
:
The
total

income
is
over

£1000

and
they have sold over
50

CD’s

or

30

Blu
-
Rays
to deserves a
bonus

ELSE
They have not sold enough to get a
bonus
Specification
Stepwise Design
(main steps of the program):
1.
Setup the Variables
2.
Get Sales Details
3.
Calculate Total Income
4.
Decide on Bonus
Stepwise Refinement
(breaking down the main steps into smaller steps)
:
1.
Get Sales Details
1.1!Setup the variables cd_sales, dvd_sales, bluray_sales and total_sales
2.
Get Sales Details
2.1!Ask the the user for the number of CD’s sold
2.2!Put the result into the variable cd_sales
2.3!Ask the the user for the number of DVD’s sold
2.4!Put the result into the variable dvd_sales
2.5!Ask the the user for the number of Blu-Ray’s sold
2.6!Put the result into the variable bluray_sales
3.
Calculate Total Income
3.1!Put the CDs_Sold multiplied by £7.99 plus the DVDs_Sold multiplied by £10.99 plus the
!BluRays_Sold multiplied by £14.99 into a variable called total_sales
4.
Decide on Bonus
4.1!Display “Your total sales today were £” followed by variable total_sales in line 1 of field “output”
4.2!Display CD sales were: ” followed by variable cd_sales in line 2 of field “output”
4.3!Display DVD sales were: ” followed by variable dvd_sales in line 3 of field “output”
4.4!Display Blu-Ray sales were: ” followed by variable bluray_sales in line 4 of field “output”
4.5!
If

total_sales
is greater than
£1000

and

dvd_sales
is greater or equal
50

or
bluray_sales
is
!greater or equal to
30 then
display a message saying that the team has earned a bonus.
4.6!
Else
4.7!Display a message saying that the team has not earned a bonus on today’s takings.
4.8!
End If
Design
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
47
Task 10: Music Shop Takings (Logical Operators)
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
// Setup the global variables to be used in this event
global
cd_sales, dvd_sales, bluray_sales, total_sales
on
mouseUp
get_sales_details
calculate_total_income
decide_on_bonus
end
mouseUp
on
get_sales_details

// Get amount of each product sold

ask
"How many CD sales were there today ?"

put
it
into
cd_sales

ask
"How many DVD sales were there today ?"

put
it
into
dvd_sales

ask
"How many BLU-Ray sales were there today ?"

put
it
into
bluray_sales
end
get_sales_details
on
calculate_total_income

// Calculate the total income

put
cd_sales *
7.99
+ dvd_sales *
10.99
+ bluray_sales *
14.99

into
total_sales
end
calculate_total_income
on
decide_on_bonus

// Display totals

put

"Your total sales today were £"
& total_sales
into

line

1

of

field

"output"

put

"CD sales were: "
& cd_sales
into

line

2

of

field

"output"

put

"DVD sales were: "
& dvd_sales
into

line

3

of

field

"output"

put

"BLU Ray sales were: "
& bluray_sales
into

line

4

of

field

"output"


// Make a decision on the bonus using an IF statement

if
total_sales >
1000

and
(dvd_sales >=
50

or
bluray_sales >=
30
)
then


put

"Well done team, you get your bonus today!"

into

line

6

of

field

"output"

else

put

"Hard luck team, you do not get your bonus today!"

into

line

6

of

field

"output"

end

if
end
decide_on_bonus
Implementation
Open the “
Music Shop Takings
” stack. It can be found in:
N4 LiveCode Programming Tasks > 10...Music Shop Takings.livecode
Assign the code below to the
“Go”
button.
National 4 Computing Science ! Programming with LiveCode (Community Edition)
Gracemount High School!Page
48
Task 10: Music Shop Takings (Logical Operators)
CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY CREDIT ONLY
Testing
CD’s
Sold
(£7.99)
DVD’s
Sold
(£10.99)
Blu-Ray’s
Sold
(£14.99)
Calculated Output and
Expect Decision
(use a calculator)
Calculated Output and
Expect Decision
(use a calculator)
Program Output and Expected
Decision
(run your program)
Program Output and Expected
Decision
(run your program)
100
50
50
Total Cost:
£2547.70
“Well done team,
you get your
bonus today”
Total Cost:
£2547.70
“Well done team,
you get your
bonus today”
10
20
30
Total Cost:
£749.40
“Hard luck team,
you do not get
your bonus today”
Total Cost: £749.40
“Hard luck team,
you do not get
your bonus today”
400
49
29
Total Cost:
£4169.22
“Hard luck team,
you do not get
your bonus today”
Total Cost:
£4169.22
“Hard luck team,
you do not get
your bonus today”
10
50
29
Total Cost:
£1064.11
“Well done team,
you get your
bonus today”
Total Cost:
£1064.11
“Well done team,
you get your
bonus today”
If both answers produced from the “
Expected Output
” and “
Actual Output

match
then you
know
that
your program is working
correctly
.
Fit for Purpose
The Music Shop Takings Program is fit for purpose. It successfully asks for and takes in the number of CD’s, DVD’s
and Blu-Ray Disks sold and displays the total cost in an output field for the user to see.
In addition, the program successfully makes use of an IF statement with logical operators to decide whether or not the
employee gets a bonus if the total profit is over £1000 and they sell over or equal to 50 DVD’s or over or equal to 30
Blu-Ray Disks. This is shown in the testing table above.
Suggested Improvements
The program could be improved upon by displaying a message to the team showing the bonus that each member is
entitled to based on the total sales for that day.
Evaluation
Save your program as 10...Music Shop Takings
Congratulations!
You are now ready to move onto the two Assessment Exercises.