Small Basic Homework Booklet - Forest Hill School Computing

advertisement
Year 8 Programming - Small Basic
Your homework booklet
Homework is set every two weeks in computer science. Homework is
always due in at the start of your next computer science lesson. These
homework tasks form part of your ongoing assessment and it is important
that you apply yourself to each task to the best of your ability.
Small Basic software
Small Basic is free software. It is available for Windows only. You can
download the installer from:
http://smallbasic.com
If you do not have access to a computer at home, or would just prefer to
do these tasks at school, you may use a computer in one of the three
Computer Science rooms – 137, 144 or 145. See your teacher to arrange
this.
Always try to do homework soon after it is set rather than just before it
is due. The ideas, concepts and skills will be fresher in your mind and you
will find it much easier to succeed at your task.
Tasks that involve analyzing code are much easier to complete
successfully if you run the code in Small Basic.
Homework 1 – Text Output
Look at the following short program and answer the questions.
TextWindow.ForegroundColor = "Red"
TextWindow.CursorLeft = 0
TextWindow.CursorTop = 0
TextWindow.Write("One")
TextWindow.ForegroundColor = "Green"
TextWindow.CursorLeft = 30
TextWindow.CursorTop = 0
TextWindow.Write("Two")
TextWindow.ForegroundColor = "Blue"
TextWindow.CursorLeft = 0
TextWindow.CursorTop = 30
TextWindow.Write("Three")
1. The word ‘One’ appears in the top-left corner. Which word appears in the
bottom-left corner?
2. When the program runs, the word ‘Two’ appears on the screen in what
colour?
3. Explain the difference between TextWindow.Write() and
TextWindow.WriteLine()
4. Write the lines of code that would need to be added in order to display the
word ‘Four’ in blue in the bottom-right corner.
Homework 2 – Variables and Conditionals
Look at the following short program and answer the questions.
TextWindow.Write("What is your name? ")
name = TextWindow.Read()
TextWindow.Write("Hello " + name + ". How old are you? ")
age = TextWindow.Read()
If age < 13 Then
TextWindow.WriteLine("You are a child.")
Else
If age < 18 Then
TextWindow.WriteLine("You are a teenager.")
Else
TextWindow.WriteLine("You are an adult.")
EndIf
EndIf
1. What variables have been used in the code above?
2. What will be displayed if the user enters 13 for their age?
3. The last 9 lines could have been written like this (without using 'Else'):
If age < 13 Then
TextWindow.WriteLine("You are a child.")
EndIf
If age >= 13 And age < 18 Then
TextWindow.WriteLine("You are a teenager.")
Endif
If age >= 18 Then
TextWindow.WriteLine("You are an adult.")
EndIf
Explain why writing code using the first version is less likely to have errors:
The next four pages contain a similar task at two different levels of difficulty.
Choose the level of challenge appropriate for you.
Homework 3a – Analysis
10 of these sentences describe a line of the program that appears in the table
on the next page. For each row in the table, write the number of the
statement that describes that line of code.
1. Prints the third number entered, a minus sign, the first number entered,
an equals sign and the value of the variable ‘answer_2’
2. Adds the three numbers together and stores the result in a variable
called ‘answer_1’
3. Asks the user to enter their third number
4. Stores the inputted numerical value in a variable called ‘num_1’
5. Asks the user to enter their first number
6. Subtracts the first number entered from the third number entered and
stores the result in a variable called ‘answer_2’
7. Stores the inputted numerical value in a variable called ‘num_3’
8. Prints the second number entered, a plus sign, the third number
entered, an equals sign and the value of the variable ‘answer_1’
9. Adds the first number entered to the second number entered and stores
the result in a variable called ‘answer_1’
10.
Asks the user to enter their name
11.
Stores the inputted numerical value in a variable called ‘num3’
12.
Stores the inputted name in a variable called ‘num_1’
13.
Asks the user to enter their second number
14.
Prints the first number entered, a plus sign, the second number
entered, an equals sign and the value of the variable ‘answer_1’
15.
Stores the inputted numerical value in a variable called ‘num_2’
The line from the program
TextWindow.Write(“Enter your first number: ”)
num_1 = TextWindow.ReadNumber()
TextWindow.Write(“Enter your second number: ”)
num_2 = TextWindow.ReadNumber()
TextWindow.Write(“Enter your third number: ”)
num_3 = TextWindow.ReadNumber()
answer_1 = num_1 + num_2
answer_2 = num_3 - num_1
TextWindow.WriteLine(num_1 + " + " + num_2 + " = "
+ answer_1)
TextWindow.WriteLine (num_3 + " - " + num_1 + " = "
+ answer_2)
Statement number
Homework 3b – Analysis
1. Below is a simple Small Basic program. In the table, describe what each
line of the program does. The first one has been done for you as an example.
Code
What the line does
TextWindow.Write("What is your name? ") Asks the user what their name is
name = TextWindow.Read()
Stores their answer in a variable
called ‘name’
TextWindow.WriteLine("Hello" + name)
TextWindow.Write("Enter a number: ")
radius = TextWindow.ReadNumber()
pi = 3.14
circumference = pi * 2 * radius
area = pi * radius * radius
TextWindow.Write("A circle with a radius
of " + radius)
TextWindow.Write(" has a circumference
of " + circumference)
TextWindow.WriteLine(" and an area of "
+ area)
Continues over page…
2. The last three lines could be written as just one TextWindow.WriteLine()
statement. In the box below, write one line of code to do the same job as the
last three lines:
Download