tag:blogger.com,1999:blog-5877885748754516692024-02-20T06:27:54.895-08:00Random Jotting's Processing BlogiCowboyhttp://www.blogger.com/profile/03710545954910136343noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-587788574875451669.post-47494155020670359112008-10-30T16:06:00.000-07:002008-10-30T16:23:24.392-07:00Block 7 - first 2000 words (rough)<style type="text/css"><!-- .rebuiltBody { font-family: Verdana; font-size: 10px; } --></style>Well this is going to be ugly - getting the right formatting is a massive task. Anyway here are the first 2000 words or so <span style="font-style: italic;">before</span> I start having to say 'Talar þu ensku?'<br /><span class="rebuiltBody"><br /><p></p><br /><p><span style="font-size:34;"><b>Variables</b></span></p><p>Computer programs need places to store information these places are called <b>variables</b> (because their value may change during the execution of the program). Before you can use a variable, you must <b>declare</b> it. This is usually done at the top of a program. Here is a simple variable declaration:</p><br /><p> </p><br /><p>Processing allows you to be extremely creative with variable names, so take advantage of that flexibility to choose appropriate names for the ones in your programs; whilst it is very tempting to call variables a, b or temp; these names aren’t terribly meaningful when you come back to your program many months later. See below for more guidance about naming variables.</p><br /><p> <img src="http://www.blogger.com/Variables_files/image001.png" /> <b>int</b> numberOfWheels;</p><br /><p>The first word, <b>int</b>, tells processing that the variable will hold integer (whole number) values. Processing is a typed language that is variables may belong to one of a number of different types we will return to types below. The second word numberOfWheels is the name we have chosen for the variable. And that’s it! You can now use the variable numberOfWheels in your program.</p><br /><p>One question that might have occurred to you is, what is the value of numberOfWheels now we have declared it? The answer is we can’t be sure; although Processing can access our new variable, it has not been given an initial value. If you try to run the following program:</p><br /><p> <b>int</b> numberOfWheels;</p><br /><p><b> print</b> (numberOfWheels);</p><br /><p>You will see a warning at the bottom of the Processing window:</p><br /><p>Semantic Error: The variable "numberOfWheels" may be accessed here before having been definitely assigned a value.</p><br /><p>To avoid this error, it is good practice to <b>initialise</b> a variable at the same time as declaring it. As the name suggests, initialisation sets a starting value for the variable, doing this is a great help when debugging a program you can track the values of variables from the moment they are created and check that their values are what you expected. Initialisation is extremely simple, we can rewrite our declaration as follows:</p><br /><p> <b>int</b> numberOfWheels = 4;</p><br /><p>If you rewrite the simple program above with this declaration and initialisation, then run it, you should see the number 4 appear at the bottom of the Processing window.</p><br /><p></p><br /><p><span style="font-size:24;"><b>Naming variables</b></span></p><br /><p>Processing is a 'case sensitive' language meaning that it treats upper and lower case letters as different characters. So if you declare the variables myname, myName, Myname, MyName and MYNAME you create five different variables.</p><br /><p>Most Processing programmers use so-called 'camel case' when naming variables. Camel-case is a method of naming where individual words are joined without spaces, the first letter is in lower case with the first letter of the subsequent words being capitalised; so a variable used to store the number of wheels on a toy car could be named numberOfWheels.</p><br /><p>Processing does have some simple rules for naming variables:</p><br /><li> Variable names may only contain letters, numbers, hyphens ( - ) and the underscore character ( _ );<br /></li><li> You cannot use any of the reserved keywords as a variable name. A complete list of these words can be found here: (<u>http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html</u>).<br /><p>In addition to these strict rules it is recommended that avoid confusion by not giving variables the same name as any of your functions or objects.</p><br /><p>Finally, although Processing is case sensitive and allows you to create two variables with near identical names such as myName and MyName, we strongly recommend that you do NOT do so as it will inevitably lead to confusion.</p><br /><p></p><br /><p><span style="font-size:24;"><b>Variable types</b></span></p><br /><p>As we mentioned before, variables in Processing belong to one of several types. Processing is a so-called typed language which means it treats different forms of data in different ways - called types. Although types can make life ever-so-slightly more difficult for programmers they do prevent problems when programs are run by restricting the kind of operations which can be performed on each kind of variable.</p><br /><p>In this section we will introduce you to the most common types used in Processing and then show you how data can be transformed from one type to another. By the end of this section you should be able to select an appropriate type for your data and use these variables in small programs.</p><br /><p>There are two broad categories of types; the simplest types are known as <b>primitive types</b> whilst more complex types of information are unsurprisingly known as <b>complex types</b>.</p><br /><p>Here are Processing’s primitive types. </p><br /><p></p><br /><p><span style="font-size:18;"><b>Integer</b></span></p><br /><p><b>Explanation</b> </p><br /><p>Whole number values (including negative numbers) </p><br /><p><b>Declaration</b> </p><br /><p><b>int</b> <i>variableName</i>; </p><br /><p><b>Example initialisation</b> </p><br /><p><b>int</b> numberOfWheels = 4; </p><br /><p><b>Range</b> </p><br /><p>-2147483648 to 2147483647 </p><br /><br /><p><span style="font-size:18;"><b>Float</b></span></p><br /><br /><p><b>Explanation</b> </p><br /><p>Decimal values (including negative numbers) </p><br /><p><b>Declaration</b> </p><br /><p><b>float</b> <i>variableName</i>; </p><br /><p><b>Example initialisation</b> </p><br /><p><b>float</b> maximumPrice = 12249.99; </p><br /><p><b>Range</b> </p><br /><p>-3.40282347E+38 to 3.40282347E+38 </p><br /><br /><p><span style="font-size:18;"><b>Character</b></span></p><br /><p><b>Explanation</b> </p><br /><p>Individual alphanumeric and symbolic characters </p><br /><p><b>Declaration</b> </p><br /><p><b>char</b> <i>variableName</i>; </p><br /><p><b>Example initialisation</b> </p><br /><p><b>char</b> shirtSize = ‘L’; (don’t forget to use the single quotes) </p><br /><p><b>Range</b> </p><br /><p>Unicode character range. See <u>http://unicode.org/charts/</u> </p><br /><p><span style="font-size:18;"><b>Boolean</b></span></p><br /><p><b>Explanation</b> </p><br /><p>Logical values </p><br /><p><b>Declaration</b> </p><br /><p><b>boolean</b> <i>variableName</i>; </p><br /><p><b>Example initialisation</b> </p><br /><p><b>boolean</b> isFamousArtist = <b>true</b>; </p><br /><p><b>Range</b> </p><br /><p>Only two values permitted; <b>true</b> or <b>false</b>. </p><br /><p><span style="font-size:18;"><b>Colour</b></span></p><br /><p><b>Explanation</b> </p><br /><p>Colour values expressed in terms of Red, Green and Blue components </p><br /><p><b>Declaration</b> </p><br /><p><b>color</b> <i>variableName</i>; (<b>color</b> is spelt using the American convention) </p><br /><p><b>Example initialisation</b> </p><br /><p><b>color</b> canvasColour = (255,0,255); (sets canvasColour to magenta) </p><br /><p><b>Range</b> </p><br /><p>Values must be given in triplets of Red, Green and Blue in that order. Each component has a minimum value of 0 and a maximum value of 255. </p><br /><p></p><br /><p><span style="font-size:24;"><b>A quick note about colour values</b></span></p><br /><p>If you're unfamiliar with colour values, a quick explanation is needed. Every possible colour can be produced by combining different brightnesses of the three primary colours Red, Green and Blue (usually abbreviated to R, G and B). If one of the three primaries is completely absent then it is given the value 0, the maximum intensity for one of the primaries is given the value 255.</p><br /><p>Fortunately, you don't need to work out these values for yourself. Processing includes a Color Selector window which can be opened from the <b>Tools</b> menu. Use the mouse pointer to find the colour you want to use, the corresponding RGB value is given in the three boxes R, G and B.</p><br /><br /><p></p><br /><p><span style="font-size:24;"><b>Exercise</b></span></p><br /><p>Declare and initialise the following variables.</p><br /><p>1. An integer variable called distanceHome with an initial value of 0;</p><br /><p>2. A floating point variable called currentTemperature with an initial value of 12.3;</p><br /><p>3. A character variable called letterOfTheAlphabet with an initial value of M;</p><br /><p>4. A Boolean variable called returnTicket with an initial value of <b>true</b>;</p><br /><p>5. A colour variable called brightWhite whose Red, Green and Blue values are set to their maximum.</p><br /><p></p><br /><p><span style="font-size:24;"><b>Answers</b></span></p><br /><p>1. <b>int</b> distanceHome = 0;</p><br /><p>2. <b>float</b> currentTemperature = 12.3;</p><br /><p>3. <b>char</b> letterOfTheAlphabet = ‘M’; (don’t forget the single quotes)</p><br /><p>4. <b>boolean</b> returnTicket = <b>true</b>;</p><br /><p>5. <b>color</b> brightWhite = (255,255,255);</p><br /><p></p><br /><p><span style="font-size:24;"><b>Complex data types</b></span></p><br /><p></p><br /><p><span style="font-size:18;"><b>Arrays</b></span></p><br /><p>An array is an ordered list of data items. You can create an array of any type of data, but a single array can only hold one type of data - so you can create an array of integers or Boolean values, but not one containing both arrays and Booleans.</p><br /><p>Each element in an array has a unique index number. These numbers are allocated sequentially; the first element having an index of 0, the second having an index of 1 and so on…</p><br /><p>Arrays are created using the keyword <b>new</b> and their length must be stipulated at that time. An example piece of code that creates an array is:</p><br /><p> <b>int</b> numberArray = <b>new int</b>[5];</p><br /><p>the formal specification to create an array is:</p><br /><p> <i>datatype</i>[] <i>name of array</i> = <b>new</b> <i>datatype</i>[<i>length of array</i>];</p><br /><p>Array elements are accessed by giving the name of the array and the index number of the element enclosed in square brackets. For example, to access the third element in an array called numberArray:</p><br /><p> myValue = numberArray[2];</p><br /><p>to assign a value to the first element of numberArray:</p><br /><p> numberArray[0] = myValue;</p><br /><p>One important attribute of all arrays is their length. You can retrieve this value using the command <i>name of array<b>.length</b></i><b> </b>for example:</p><br /><p> numberOfPlayers = playerArray<b>.length</b>;</p><br /><p></p><br /><p><span style="font-size:18;"><b>Initialising arrays</b></span></p><br /><p></p><br /><p></p><br /><p>It is good practice to set initial values for each of the elements in an array before the array is used. Processing allows you to do this in two ways. The first is providing all of the initial values during the array declaration. The values are enclosed in curly brackets { }, each separated from the next value using commas, such as:</p><br /><p> <b>float</b>[] bankBalanceArray = {100.0, 250.5, 500.1};</p><br /><p>Alternatively, you can set initial values using a simple <b>for</b> loop:</p><br /><p><b>int</b>[] numberArray = <b>new</b> <b>int</b>[5];</p><br /><p><b>for</b> (<b>int</b> elementNumber = 0; elementNumber < numberArray<b>.length</b>; elementNumber = elementNumber + 1) {</p><br /><p>numberArray[elementNumber] = 0;</p><br /><p>}</p><br /><p>The second approach is ideal when you are creating very large arrays.</p><br /><p></p><br /><p><span style="font-size:18;"><b>Strings</b></span></p><br /><p></p><br /><p></p><br /><p></p><br /><p><span style="font-size:24;"><b>Converting between types</b></span></p><br /><p></p><br /><p></p><br /><p>Try entering the following code into a processing window then pressing <b>Run</b>.</p><br /><p><b>int</b> aValue = 5;</p><br /><p><b>print </b>(aValue / 2);</p><br /><p>The result is 2! You were hopefully expecting a result of 2.5, so has the computer made a mistake? Actually no, the incorrect result is down to aValue being declared as an integer. Dividing an integer produces an integer result, in this case, the wrong result.</p><br /><p>Before we can perform accurate divisions, we must convert the variable to a different type - in this case a floating point number. Fortunately Processing has a number of functions for converting between types. Try this program:</p><br /><p><b>int</b> aValue = 5;</p><br /><p><b>print</b> (<b>float</b>(aValue) / 2);</p><br /><p>The function <b>float</b>() converts the integer value aValue into a floating point number which can then be divided accurately by 2.</p><br /><p>The most useful conversion functions are given below. </p><br /><p></p><br /><p><span style="font-size:18;"><b>int(<i>value</i>)</b></span></p><br /><p><b>Input types</b> </p><br /><p><b>int</b>, <b>char</b>, <b>byte</b>, <b>boolean</b>, <b>String</b>, <b>int</b>[], <b>char</b>[], <b>byte</b>[], <b>boolean</b>[], <b>String</b>[] </p><br /><p><b>Output type</b> </p><br /><p>Floating point number </p><br /><p><b>Examples</b> </p><br /><p><b>int</b>(3.75), <b>int</b>(<i>aValue</i>) </p><br /><p><b>Notes</b> </p><br /><p>Returned values are always rounded down to an integer value - e.g. <b>int</b>(5.75) returns 5. </p><br /><p> </p><br /><p><span style="font-size:18;"><b>float(<i>value</i>)</b></span></p><br /><p><b>Input types</b> </p><br /><p><b>int</b>, <b>char</b>, <b>byte</b>, <b>boolean</b>, <b>String</b>, <b>int</b>[], <b>char</b>[], <b>byte</b>[], <b>boolean</b>[], <b>String</b>[] </p><br /><p><b>Output type</b> </p><br /><p>Floating point number </p><br /><p><b>Examples</b> </p><br /><p><b>float</b>(7), <b>float</b>(<i>aValue</i>) </p><br /><p></p><br /><p><span style="font-size:18;"><b>char(<i>value</i>)</b></span></p><br /><p><b>Input types</b> </p><br /><p><b>int</b>, <b>byte</b>, <b>int</b>[], <b>byte</b>[] </p><br /><p><b>Output type</b> </p><br /><p>Character </p><br /><p><b>Examples</b> </p><br /><p><b>char</b>(5), <b>char</b>(<i>aValue</i>) </p><br /><p> </p><br /><p><span style="font-size:18;"><b>boolean(<i>value</i>)</b></span></p><br /><p><b>Input types</b> </p><br /><p><b>int</b>,<b> String</b>, <b>String</b>[] </p><br /><p><b>Output type</b> </p><br /><p>Boolean </p><br /><p><b>Examples</b> </p><br /><p><b>boolean</b>(“Hello”), <b>boolean</b>(<i>aValue</i>) </p><br /><p><b>Notes</b> </p><br /><p>An integer value of 0 evaluates to <b>false</b>, all other values evaluate to <b>true</b>. </p><br /><br /><p>Most programmers choose to declare variables near the top of a program or function. The only common exception to this practise are the variables used to control the operation of a <b>for</b> loop. As you learned in SECTION X, a <b>for</b> loop is used to iterate a series of instructions; the number of repetitions being governed by an integer loop variable. For example:</p><br /><p> <b>for</b> (<b>int</b> i = 0; i < 40; i = I +1 ) {</p><br /><p>...</p><br /><p>...</p><br /><p> }</p><br /><p>Many programmers give the loop variable a single letter name such as i, j or k (this is a hang-over from the ancient Fortran programming language), but you may prefer to give your loop variables meaningful names.</p><br /><p>One restriction on loop variables is that they have a limited scope - that is they can only be used inside the loop itself. Copy the following program section into a Processing window and Run it:</p><br /><p> <b>for</b> (<b>int</b> i = 0; i < 40; i = i + 1) {</p><br /><p> <b>line</b>(30, i, 80, i);</p><br /><p> }</p><br /><p>You should see a black rectangle in a new Sketch window. A brief explanation of the program might be needed. The variable i sets the y coordinates for the beginning and ending of the lines, beginning at 0 and ending at 40. After each line is draw, the program returns to the beginning of the loop, the value of i is incremented and the next line is drawn.</p><br /><p>If you modify your program as follows, then Run it:</p><br /><p> <b>for</b> (<b>int</b> i = 0; i < 40; i = i + 1) {</p><br /><p> <b>line</b>(30, i, 80, i);</p><br /><p> }</p><br /><p> <b>print</b> (i);</p><br /><p>You will see a warning message about a Semantic Error in your program; Processing is telling you that it cannot access the variable i outside of the loop where it is declared.</p><br /><p></p><br /><p><span style="font-size:24;"><b>Literals</b></span></p><br /><p>Variables get their name because they can assume different values at different times and places in a program. However, sometimes you will need to refer to a constant value known as a literal. Unlike languages such as Smalltalk and Ada, Processing does not distinguish between variables and literals. The declaration of a literal is exactly the same as that for a variable:</p><br /><p><b>int</b> daysInWorkingWeek = 5;</p><br /><p><b>String</b> startOfWeek = "<i>Sunday</i>";</p><br /><p>You may have already realised that since literals are treated exactly the same as variables it is entirely possible to write a program that assigns a new value to the literal. It is therefore your responsibility to ensure that any literal values remain unchanged throughout your program. You may find it useful to name your literals in a way that reminds you they are unchangeable literals. For instance:</p><br /><p><b>int</b> literalDaysInWorkingWeek = 5;</p><br /><p><b>String</b> literalStartOfWeek = "<i>Sunday</i>";</p><br /><p>or perhaps use the old-fashioned approach of writing the whole of the variable name in upper case, such as:</p><br /><p> <b>int</b> DAYSINWORKINGWEEK = 5;</p><br /><p>although you may find this approach makes for less readable code.</p><br /><p></p><br /><p><span style="font-size:24;"><b>Constants</b></span></p><br /><p>Processing has a number of pre-defined mathematical constants which should not be confused with literals. These constants are PI, HALF_PI and TWO_PI. </p><br /></li></span>iCowboyhttp://www.blogger.com/profile/03710545954910136343noreply@blogger.com2tag:blogger.com,1999:blog-587788574875451669.post-39994625202889877842008-10-24T05:54:00.000-07:002008-10-24T06:07:27.351-07:00Mass mobilisationSo whilst we're writing a book as a collaborative effort, 1/9 of the population of Iceland has got together online to protest at Britain's (mis)use of <a href="hhttp://p10.hostingprod.com/@spyblog.org.uk/blog/2008/10/icelanders-are-not-terrorists.html">Anti-Terror legislation</a> to seize assets held in Britain and collapse one of their working banks. So if you fancy joining in this 21st Century Vikingry, click on one of the images below:<br /><br />Book content soon - I promise - just as soon as I'm back from - ummmm - Iceland.<table width=" 100%"><tbody><tr><td><br /><a href="http://www.indefence.is/"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 140px; height: 110px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmNyzDK-fvxMhvGF7cVG2G6edyawu5ObZtUFd5c7CFu3rPdL6SPqccmeCvWD1jLt8sw9rISgGGjdHRvE7igSS0-Ln_3KsL76pc-RvATx6ym77eARlhSsyV56f_Vl845QlqMpCRL95NomI/s320/banner_isl_140x110pt.gif" border="0" alt="" id="BLOGGER_PHOTO_ID_5260704032738728194" /><br /></a><br /></td><td><br /><a href="http://www.indefence.is/"><img style="cursor:pointer; cursor:hand;width: 140px; height: 127px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhRGD2gkMZH1E7_nKRmQ_45uFUserKYZAcwvI8kt-DBXv8wM1RV98pIIOFTMT51IOx1tEZmNEPFuc0WB12jACpZChv6Lb38fQ_yg8e0BdFEWXBJ7nzu7Fys4UHAF48vgU2kfQMVvTRb9sE/s320/bannereng_140x127pt.gif" border="0" alt="" id="BLOGGER_PHOTO_ID_5260704616274350658" /></a><br /></td></tr></tbody><tbody></tbody></table>iCowboyhttp://www.blogger.com/profile/03710545954910136343noreply@blogger.com1