Copyright Elektor International Media B.V.
Copyright Elektor International Media B.V.
Table of Contents
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1. Lesson 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Installation of Small Basic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Your first program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The command structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Design a text window screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2. Lesson 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Using variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 The danger of mixing types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Calculations with the Math class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3. Lesson 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Repeating with the for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Repeating with the while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 If-Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The use and abuse of the Goto command . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4. Lesson 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Improve readability: formatting and comments. . . . . . . . . . . . . . . . . . . . . . . 35 Easter egg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Array (variable and object) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Bar graphs on a text screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5. Lesson 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Text (string) handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Make sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Display the time and measure elapsed time . . . . . . . . . . . . . . . . . . . . . . . . . 50
6. Lesson 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Reading and writing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Use the stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Arguments for your executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Batch file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Desktop shortcut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
1
Copyright Elektor International Media B.V.
PC Programming - A Small Basic Approach
7. Lesson 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Timed interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 A graphics window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Animation of Shapes objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
8. Lesson 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Keyboard and text events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Event interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Mouse events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Controls object events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9. Lesson 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Read data from the internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Put your program on Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
10. Lesson 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Using extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 A microcontroller rapid development board: Piccolino . . . . . . . . . . . . . . . . . 113
11. Examination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Automatic picture frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 The quizmaster problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Picture puzzle with sixteen pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Collect information from the internet . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
12. Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Lesson 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Lesson 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Lesson 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Lesson 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Lesson 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Lesson 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Lesson 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Lesson 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Lesson 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Lesson 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Examination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
2
Copyright Elektor International Media B.V.
Lesson 1
1. Lesson 1 In Lesson 1, we will learn how to install Small Basic, and write our very first PC program. We will learn about classes, objects, properties and the like, in addition to using fancy colours.
Installation of Small Basic Before we can do anything we need to install Small Basic. In order to install and use Small Basic you need a PC with Microsoft Windows XP SP3 or later. Go to the support website www.boekinfo.tk and download the Small Basic installer called “SmallBasic.msi”. Then run the installer. In order to use Small Basic you will need .NET 3.5. If this is not present on your machine the installer will inform you about this, and the installation will be aborted. If that happens go to the Microsoft website and download the .NET 3.5 installer “dotnetfx35setup.exe”. Run this installer first, then once you’re done retry the Small Basic installer.
Your first program Once Small Basic is installed start the program. Your screen should look like figure 1.1. Note that all screenshots in this book were made with the Dutch version of Small Basic, on a Windows 7 - 64 bit machine. Your screens may look a bit different, but they will contain similar information.
Figure 1-1 Small Basic start-up screen. This is an example of a Small Basic program:
TextWindow.WriteLine(“Hello World”)
This program prints “Hello World” on the screen. Note that even though we say “print” we don’t actually mean something gets printed on paper. Programmers use the term printing loosely to reflect showing something either on a screen or on paper, and sometimes even
5
Copyright Elektor International Media B.V.
Lesson 2
2. Lesson 2 In Lesson 2, we will learn to use variables so our program can remember things, and we explore the math class.
Using variables In pretty much every program so far you’ve had to change my name for your name, and if you give your program to a friend you’d have to change it again. It would be easier if the program would simply ask your name and then use it whenever needed. For this we need a new method: Read. Since Read is a method we can assign a value to, it uses brackets. However Read doesn’t need any values, so the brackets are there, but there’s nothing in between. Unlike the methods we have used so far, Read returns a text: it returns whatever text the user entered. That text needs to be stored. Let’s take a look at the following example: TextWindow.Write(“What is your name? “) MyName=TextWindow.Read() TextWindow.WriteLine(“Your name is “ + MyName)
Note: Read is for texts only, for numbers “ReadNumber” is used, this is discussed a little later. Since the Read method doesn’t print anything to the screen we start with a Write method to ask the user for his name. Otherwise the user would have no clue what to do. Once the question has been asked the program waits. At this point the user will type something, and then press the ENTER key. When that key is pressed, the information that the user entered is now called MyName. MyName is called a variable, because the content may vary. Variables are used quite extensively in computer programs because they are the only way for programs to remember things. So from now on whenever we use MyName, Small Basic will replace that with whatever it was that the user entered. In this case presumably his name. In the third line we print “Your name is “ followed by the content of MyName. Let’s take a closer look at that last line. How does Small Basic not only know that we want it to print Your name is literally, but that it should print what you entered instead of the literal text MyName. In short: why isn’t the result Your name is MyName? Information that needs to be used literally is placed between quotation marks. Variables that need to be replaced by their content are used without quotation marks. The plus sign between the two means “combine them”, the technical term for this is concatenation. If all the variables are numbers, the plus sign means add them up. If none of the variables are numbers it means print one after the other. Since the literal we want to print, and your name are not numbers they, are simply printed one after the other. We called our variable MyName, but in fact we could use any name we want.
15
Copyright Elektor International Media B.V.
Lesson 3
3.  Lesson 3 In lesson 3, we will learn how to change program flow with loops and conditional commands.
Repeating with the for loop All of our Small Basic programs were executed in the same way: starting at the top, and then line by line down. In the final question of the previous section this turned out to be quite inconvenient. This program consists of 5 identical lines. Imagine what the program would look like for 100 simulations. Fortunately it is possible to repeat the execution of the same command(s) multiple times. Commands like that are called flow control commands, because they control the flow of the program. These commands are: For While
Goto
This command repeats a series of commands for a certain number of times.
This command repeats a series of command while a certain condition is met. This is an extremely powerful command that you will use very often. We will discuss this command in more detail in the next section. This command makes the program jump to another location. We will discuss this command in a later section.
Small Basic uses the keys symbol for flow control commands.
Figure 3-1 Keyword symbol The following program is a revisit of the last question, that throws a die 5 times in a row. TextWindow.WriteLine(Math.GetRandomNumber(6)) TextWindow.WriteLine(Math.GetRandomNumber(6)) TextWindow.WriteLine(Math.GetRandomNumber(6)) TextWindow.WriteLine(Math.GetRandomNumber(6)) TextWindow.WriteLine(Math.GetRandomNumber(6))
21
Copyright Elektor International Media B.V.
Lesson 4
4. Lesson 4 In Lesson 4, we will make our programs easier to read, discover Easter Eggs, use arrays and bar graphs.
Improve readability: formatting and comments. By now you have probably discovered that keeping your program nicely formatted makes it easier to read. The indentation for example clearly shows where loops begin and where they end. However when you start making changes to your program the formatting may get lost and you will have to reformat the program.
Figure 4-1 The right-click menu. Fortunately there is a simple solution. When you right-click anywhere in your program a menu will pop up. It has the options you would expect - cut, copy and paste - and also two very nice ones: find and format. Simply click on the format option and your program is instantly formatted. Comments are notes to yourself, and others who may read your program. These notes are ignored by Small Basic, so they do not make your program slower. Comments are preceded by a ‘. ‘Print 20 dots, until the user aborts While(“True” And entry<>1) ‘If entry is 1 the user wants to abort so exit this loop For number = 1 To 20 ‘ Place a dot next to the previous one TextWindow.CursorTop=0 TextWindow.CursorLeft=number TextWindow.Write(“.”) TextWindow.CursorTop=2 TextWindow.CursorLeft=0 ‘Ask the user for input TextWindow.Write(“Enter a 0 to continue or a 1 to stop
“)
TextWindow.CursorLeft = TextWindow.CursorLeft - 2
35
Copyright Elektor International Media B.V.
Lesson 5
5. Lesson 5 In lesson 5, we will handle texts in all shapes and sizes, listen to sounds and discuss the clock.
Text (string) handling So far we have done nothing with variables that contain text, other than print them to the screen. In other languages variables that hold text are called strings. In Small Basic there is only one variable type, so this term is not used. However if you talk to other programmers the term may come up so it is still important to know what it means. For text handling the Text object can be used. In lesson 2 we discussed the dangers of mixing types, illustrating it with this example: TextWindow.Write(“Enter a number “) MyNumber=TextWindow.Read() TextWindow.WriteLine(“1 “ + MyNumber)
For example, if the user enters a 5 then the program will not print 1 5 but 6 instead. The reason is that MyNumber is a number, and Small Basic is trying to guess what you might want to do with a number and something that looks like a number - it guesses incorrectly that you want to add them. This can be solved by letting Small Basic know that we want to combine the number and text “1” as text, and not as a mathematical calculation by using the Append method. TextWindow.Write(“Enter a number “) MyNumber=TextWindow.Read() result=Text.Append(“1 “,MyNumber) TextWindow.WriteLine(result)
In this program MyNumber is appended to “1” as text, and the combined text is put into a variable result. This variable is subsequently printed. This time the print is indeed “1 5”. In fact this time Small Basic will completely ignore the actual contents of MyNumber. Even if you use the ReadNumber method to obtain the user input, thus explicitly telling Small Basic that it is a number, the Append method will still turn it into a text and append it. Another method of the Text object is ConvertToUpperCase. This will convert a text to upper case. There is of course another method that will do the exact opposite: ConvertToLowerCase. These commands come in very handy if you want to compare texts, but are not sure if the user will use capitals or not. TextWindow.Write(“Enter your name “) Name=TextWindow.Read() If Name=”Bert” Then
45
Copyright Elektor International Media B.V.
Lesson 6
6. Lesson 6 In Lesson 6, we will learn how to handle files, and store data. We will discuss more complicated subjects such as the stack and subroutines. We will compile our programs and use arguments to run them.
Reading and writing files The File object allows the reading and writing of files, as well as the creation or deletion of files and directories. Files are used to store data, because some data needs to be retained even after a program has terminated. This is unlike normal variables, which disappear when the program stops. WARNING! •
The DeleteFile method will happily delete any file you want. Including those that are vital to your computer, and those that you really wanted to keep.
•
Writing to a file automatically creates that file, and if it already exists, any content will mercilessly be erased without warning. Even if it contains the one and only copy of your life’s work.
•
It is probably a good idea to designate a directory especially for your experiments, and then create and delete files or directories inside that directory. This way you minimise the chances of overwriting or deleting something vital.
We will do all our experiments in the same directory that your program is in (so make sure you save the program first before running it). This means that the first step is to find out where the current program is. We use the Directory method from the Program object, and store the answer in the variable “here”. At the same time we add the name of our new file to it, with a leading backslash. If for example the current program is in c:\pcdev, and the filename is my.txt, then the content of variable here would be: c:\pcdev\my.txt
Next we use the WriteLine method of the File object. This method requires a line number, and a text that you want to write at that location. The correct filename is specified as contained in variable here, then line 1 and then the text “This is line 1”, followed by a similar command for line 2. If you don’t want to use linenumbers you can still write to the file, but only to the end of it. Your new data will be appended to the current content. The AppendContents method takes care of this. In the program it adds a final line of text “And this is added to the end” to the end of the file. The last line reads the file with the ReadContents method and displays it on the text screen. This way we can see if the program runs OK.
53
Copyright Elektor International Media B.V.
Lesson 7
7. Lesson 7 In lesson 7, we will discuss interrupts and (finally) a new type of window which is capable of graphics. We will discover shape objects and learn how to animate them.
Timed interrupts An interrupt is a mechanism that interrupts a running program to do something else, and then when it is done, returns control to the main program. An interrupt is bit like receiving a text message. You are doing something when a text message comes in, so you stop what you are doing, read the message, reply if needed, and then you return your attention to what you were doing before the text message interrupted you. Timer.Interval=500 Timer.Tick = CreateSound
While(“True”) For counter=1 To 10 TextWindow.Write(“.”) Program.Delay(900) EndFor counter=0 TextWindow.Clear() EndWhile
Sub CreateSound Sound.PlayClick() EndSub
This program is basically a metronome. It clicks with a very steady rhythm, regardless of what the main program is doing. Musicians use metronomes to help them keep the correct pace in their music. In this section we use the Timer object to control how often the interrupt is to occur. First the Interval property is set to the interval value. In this case 500 milliseconds. This means that every 0.5 seconds the main program will be interrupted, and a subroutine will run. The next step is to assign a subroutine to the Tick property. The main program does something rather useless; every 900 milliseconds it prints a dot to the screen, and when ten dots have been written the screen is erased and the loop restarts. This is for demonstration purposes only. In the subroutine a clicking sound is made using the PlayClick method of the Sound
71
Copyright Elektor International Media B.V.
Lesson 8
8. Lesson 8 In lesson 8, we will discuss interrupts, not based on time but events and we will take a close look at controls like buttons and text boxes.
Keyboard and text events The TextWindow object behaves a bit like old school programs. If you want to ask the user a question, the program will simply stop and wait for the answer. Most programs however do not work that way. Even when you are asked a question you can often opt to do something completely different, for example click on a button. This is the kind of behaviour that the GraphicsWindow object has too. This means that there is no Read or ReadNumber method in the GraphicsWindow object. So how do we get user input? While the programming is running, and happily doing other things, it can keep an eye on for example the keyboard. When a character is entered, the program will interrupt what it is doing, and run a special subroutine instead. An interrupt of this kind is called an event. An event means that something has happened that the program is interested in. GraphicsWindow.TextInput = OnText Sub OnText if GraphicsWindow.LastText=”B” then GraphicsWindow.DrawText(100,50,”You pressed a B”) EndIf EndSub
In this program the TextInput event is used to start the subroutine OnText whenever a character is entered on the keyboard. The subroutine uses the LastText property to see what the last character was that the user entered. If that was B then it prints the message “You pressed a B” to the screen (don’t forget the shift key, we are looking for a capital B). The program works, but it is not designed correctly. Imagine the following: you press a B. The OnText subroutine is started. Before the subroutine can finish you press a B again. A second instance of the same subroutine is started. So now we have two instances of the same subroutine running. Depending on what the subroutine does, it may not be much of a problem. In this program you will not be able to press the B key fast enough, and even if you did it wouldn’t matter. In other programs however, it may cause havoc, and prematurely terminate the program with a fatal error. For example when the subroutine plays a music file, or wants to open a COM port for communication. Another problem is that several methods, such as printing to the graphics screen, are executed when the subroutine exits, and not in the subroutine itself.
81
Copyright Elektor International Media B.V.
Lesson 9
9. Lesson 9 In lesson 9, we will work with pictures, link to the internet and share our programs with others.
Pictures The ImageList class allows you to load images into memory, in preparation for other commands, or to get dimensional properties. Table 9-1 ImageList class - overview of available methods ImageList class method LoadImage
GetHeightOfImage
GetWidthOfImage
Description Load an image into memory for further use, either in a GraphicsWindow or as Shapes object.
Get the height of the image. You would perhaps expect this to be a property, but it is a method of the ImageList class to obtain property of the image. Get the width of the image. The same method/property remark applies here as well.
In the free download you will find a small picture called ifs-fern.jpg. Copy this image to the same directory where your Small Basic sources are, like c:\PCdev.
Figure 9-1 The ifs-fern.jpg image The following program will load this image in memory, and then display it on the GraphicsWindow object. image=ImageList.LoadImage(Program.Directory+”\ifs-fern.jpg”) GraphicsWindow.DrawImage(image, 10, 10)
You cannot do much with this image, other than display it. Fortunately images can also be loaded as Shapes object. This means the full functionality of Shapes objects methods such
101
Copyright Elektor International Media B.V.
Lesson 10
10. Lesson 10 In lesson 10, we will discover that Small Basic can be extended, and see how it can be used to communicate with a microcontroller rapid development board: the Piccolino.
Using extensions Although Small Basic is very powerful considering its ease of use, some obvious things are absent. For example communication using the COM port of your PC. Granted, most computers don’t even have COM ports anymore and rely on USB-to-COM connectors to provide COM services. Lots of hobby electronics projects however use COM ports, so it would be nice if Small Basic would support this. Fortunately Small Basic can be extended. Special extensions must be placed in a Lib directory, and will be automatically loaded in Small Basic. Proper extensions come in two parts: a DLL file that contains the actual commands, and an XML file that contains the IntelliSence help text. The download file contains the Piccolino extension. This extension contains three new objects: ComPort
KeyboardBuffer
MouseControl
This object contains methods to control the serial port of your PC. This can be a physical COM port, or an emulated COM port using a USB-to-Serial converter cable. This object contains methods to send keystrokes to the keyboard buffer. This buffer is read by the active application, so you can use this to copy data from the COM port directly into for example a spreadsheet.
This object contains methods to move the mouse(pointer) and push the mouse buttons.
To use the extension you need to follow the following two steps: 1. Go to the Small Basic directory on your PC and create a new directory called Lib. On my PC Small Basic is the directory: c:\programfiles (x86)\Microsoft\Small Basic
This means the Lib directory (mind the capital L in Lib) is here: c:\programfiles (x86)\Microsoft\Small Basic\Lib
On your PC, Small Basic may be in a different directory. If you cannot find it, let your PC search for the program sb.exe. 2. Move the content of the PiccolinoDLL directory (so the DLL and XML files) from the download to the Lib directory that you just created. You can test your work by starting Small Basic. Then type “comp” without the quotation
111
Copyright Elektor International Media B.V.
Examination
11. Examination The exam is not a walk in the park. You will be asked to write four programs. It is just like in real life: they all have to work, and they all have to be correct. So you either pass or you fail, there is nothing in between. For the third program you can get extra credit, which means you can also pass cum laude. You have to check your own exam results. Be honest otherwise there is not much point in taking the test. There is no time limit, take all the time you need. If you cannot find the answer, sleep on it and try again tomorrow. You can use any information source that you can lay your hands on: this book, IntelliSense, the internet. Just don’t ask help from someone who completed this exam before.
Automatic picture frame Write a program that loads and displays the picture of the moment from Flickr, every second. Make sure the user doesn’t see an empty screen when the picture is loading. Also take care of the window dimensions so that the picture will fit.
The quizmaster problem This is the situation: A person has won a quiz. The quizmaster leads him to a row of three curtains, marked A, B and C. Behind one curtain is a car, behind the other two is nothing. The curtains are of course closed. The person selects a curtain. The quizmaster now walks to the curtains and opens one which doesn’t have the car. Therefore, we now have one open curtain and two closed ones. Behind one of the closed ones the car must be hidden. Now the quizmaster says: “What would you like to do?” 1. Stick to your original choice of curtain. 2. Change your mind and pick the other curtain. 3. Make a new choice between the two closed curtains. Most people believe that it doesn’t make any difference; all choices will yield a 1/2 chance of a car. If you have extensive knowledge of statistics however, you know that the proper answer is 2, which yields a 2/3 chance of a car. Write a program that hides a car randomly behind a curtain. Then randomly select the curtain that the person chooses. Then see which of the three actions would have led to winning the car. Print the result. Repeat this experiment 5000 times, print the average result, and see what in the long run the odds of each action are.
117
Copyright Elektor International Media B.V.
Answers
12. Answers Lesson 1 1.1 Answers 1. IntelliSense is a help system that constantly monitors what you are doing and provides help. It does this by suggesting commands, and by providing instructions on how to use commands (on the right side of the screen). 2. Scroll up and down through the list of suggestions, and tap the TAB key to select one. 3. The program is the answer to question 3. 4.
TextWindow.WriteLine(“Hello, “) TextWindow.WriteLine(“my name is Bert”)
1.2 Answers 1. An object is an item that only exists when you use it. If you don’t use an object such as a text window, then that text window doesn’t exist. Your program has to create it first. Examples of objects are a text window, a button, a file, a graphics window. 2. The first part - TextWindow - is the object. It takes care of showing a window with black background. The second part - WriteLine() - is the method. It describes what the object will do (or in this case: what will happen to the object). This method displays a line with text on the black window. Third part - “Hello World” - is the actual text that will be printed. 3.
TextWindow.ForegroundColor = “red” TextWindow.WriteLine(“Red”) TextWindow.ForegroundColor = “blue” TextWindow.WriteLine(“Blue”) TextWindow.ForegroundColor = “yellow” TextWindow.WriteLine(“Yellow”)
Note that the last colour command is yellow, so the line “Press any key to continue...” is in yellow too. If you want this line in gray you need to add the following command to the end of the program. TextWindow.ForegroundColor = “gray”
119
Copyright Elektor International Media B.V. Index A And 28 Animate 77 Animation 76 Arguments 64 array 40 Array 37 Array object 41 ASCII 46
Sound.PlayMusic 48 TextWindow 6 While 21 WriteLine() 6 comments 35 ComPort 111 COM port 111 counter 22, 88 CursorLeft 12 CursorTop 12
D
Bar graphs 42 Batch file 65, 67
DeleteFile 53 Desktop shortcut 65, 68 DLL 111 DownloadFile 103
C
E
Ceiling 19 Class 8 clicked 94 Command line 65 Command line operation 65 Commands Ceiling 19 clicked 94 counter 22 CursorLeft 12 CursorTop 12 EndFor 22 EndWhile 24 Floor 19 For 21 GetCount 60 GetRandomNumber 18 Goto 21 GraphicsWindow 75 keyed 82 OnMouseDown 91 OnMouseMove 91 OnText 82 Read 15 Round 19 Shapes.Rotate 78 Sound.PlayChime 48
elapsed time 50 Else 28 ElseIf 29 EndFor 22 EndIf 28, 29 Endwhile 26 EndWhile 24 Event interrupts 87 Events 9 Examination 117 eXclusive 28 extensions 111
B
Index
F False 26 Files Reading 53 Writing 53 Floor 19 For 21 for loop 21 formatting 35
G GetCount 60 GetHeightOfImage 101
191
Copyright Elektor International Media B.V.
PC Programming - A Small Basic Approach GetPictureOfMoment 107 GetRandomNumber 18 GetRandomPicture 107 GetWebPageContents 103 GetWidthOfImage 101 Goto 21, 30 graphics window 73 GraphicsWindow 75, 81 GraphicsWindow object 74
object events 93 OnMouseDown 91 OnMouseMove 91 OnText 82 operator 17 Operators 24, 27 Or 28
I
Piccolino 112, 113 Pictures 101 Program.Delay 88 Properties 9, 11 Left 12 Top 12
If-Then 26, 28 Installation 5 Small Basic 5 IntelliSense 6 internet 102 interrupt 72 interrupts 71 IsSubText 46
K Keyboard 81 keyboard buffer 112 KeyboardBuffer 111 keyed 82 Keyword symbol 21
P
R Read 15 Read data 102 Reading 53 Round 19 rounding methods 19
S
N
Shapes.AddText 92 Shapes.Rotate 78 showcounter 88 Sound.PlayChime 48 Sound.PlayMusic 48 sounds 48 stack 58, 59 Stack empty 60 overflow 60 underflow 60 stack overflow 60 String handling 45 subroutine 62 Subroutines 60
Network object 103
T
O
text 81 Text 45 TextInput 82
L Left 12 LoadImage 101
M Math class 17 Methods 9, 11 MouseControl 111 Mouse events 90 myButton 93
object 37 Object 8
192
Copyright Elektor International Media B.V.
Index
TextWindow 6 time 50 Timed interrupts 71 Top 12 True 25 Truth table 28
V variable 22, 37, 38 variables 15
W While 21 while loop 24 WriteLine 54 WriteLine() 6 writing 53
X XML 111 Xor 28, 30 xpos 91
Y ypos 91
Z Zoom 79
193
Copyright Elektor International Media B.V.