Background
jLogo Programming
- Commanding a Turtle
- Pseudocode
- Adding New Commands
- Iteration & Animation
- Hierarchical Structure
- Procedure Inputs
- Primitive Operators
- Defining Operators
- Words & Sentences
- User Interface Events
- What If? (Predicates)
- Recursion
- Local Variables
- Global Variables
- Word/Sentence Iteration
- Mastermind Project
- Multiple Turtles
- Arrays
Java
- A Java Program
- What's a Class?
- Extending Existing Classes
- Types
- Turtle Graphics
- Control Flow
- User Interface Events
Appendices
Lastly
What Is Computer Programming?
Introduction
Writing software, computer programs, is describing how to do something. In its simplest form, it is a lot like writing down the steps it takes to do something - a process. But, if what you need to do is not obvious or it involves multiple objects (each with their own process) writing the program will challenge you like when you are solving puzzles.
So, writing a computer program can be like composing music, like building a house, like creating lots of stuff. It has been argued that in its current state, it is an Art, not engineering.
An important reason to consider learning a bit about how to program a computer is that the concepts underlying it will be valuable to you, regardless of whether or not you go on to make a career out of it. One thing that you will learn quickly is that a computer is very dumb. It does exactly what you tell it to do, which is not necessarily what you wanted. Programming will help you learn the importance of clarity of expression.
A deep understanding of programming, in particular the
notions of successive decomposition as a mode of analysis
and debugging of trial solutions, results in significant
educational benefits in many domains of discourse,
including those unrelated to computers and information
technology per se.
(Seymour Papert, in "Mindstorms")
It has often been said that a person does not really
understand something until he teaches it to someone else.
Actually a person does not really understand something
until after teaching it to a computer, i.e., express it
as an algorithm."
(Donald Knuth, in "American Mathematical Monthly," 81)
Computers have proven immensely effective as aids to clear
thinking. Muddled and half-baked ideas have sometimes
survived for centuries because luminaries have deluded
themselves as much as their followers or because lesser
lights, fearing ridicule, couldn't summon up the nerve to
admit that they didn't know what the Master was talking
about. A test as near foolproof as one could get of whether
you understand something as well as you think is to express
it as a computer program and then see if the program does
what it is supposed to. Computers are not sycophants and
won't make enthusiastic noises to ensure their promotion
or camouflage what they don't know. What you get is what
you said.
(James P. Hogan in "Mind Matters")
But, most of all, it can be lots of fun!!!
Inside Computers - Bits and Pieces
Today, most people don't need to know how a computer works. Most people can simply turn on a computer or a mobile phone and point at some little graphical image on the display, click another button, and the computer does something. An example would be for it to get weather information from the net and display it. This is all the average person needs to know about using a computer (or any device that's computer based).
But, since you are going to learn how to write computer programs, you need to know a little bit about how a computer works. Your job will be to instruct the computer to do things. The lists of instructions that you will write are computer programs, and the stuff that these instructions manipulate are different types of objects.
Basically, computers perform operations on groups of bits. A bit is either on or off, like a lightbulb. A microprocessor, which is the heart of a computer, is very primitive but very fast. It takes groups of bits and moves them around, adds pairs of groups of bits together, subtracts one group of bits from another, compares a pair of groups, etc... - that sort of stuff.
Computers manipulate numbers, symbolic information (think characters), visual things (pictures), sound (heard of MP3?), and sets of instructions (the computer's native language).
But at the lowest level, everything is simply a bunch of bits, things that are either on or off! Let's look at groups of bits that form numbers.
Numeric Representation of Bits
There are only 10 different kinds of people in the world:
those who know binary and those who don't.
- Anonymous
Computers are full of zillions of bits that are either on or off. The way we talk about the value of a bit in the electical engineering and computer science communities is first as a logical value (true if on, false if off) and secondly as a binary number (1 if the bit is on and 0 if it's off). Bits in a computer are manipulated in groups, so we humans need a way to describe groups of bits, things/objects a computer manipulates. For mostly historical reasons, bits are most often grouped in quantities of 8, 16, and 32. At the very high end of performance today we even have groups of 64 bits for big numbers.
Think about how you write down sequential numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, etc... Our decimal number system has ten symbols. Above, when we ran out of symbols, we combined them. You learned how to do this so long ago, in grade school, that today you just naturally think in terms of single digit numbers, then tens, hundreds, thousands, etc... The decimal number 1234 is one thousand, two hundreds, three tens, and four units.
So, how does the computer's binary number system work?
Well, with only two symbols, we would write the same sequential numbers as above: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011. The decimal number 1234 in binary is 10011010010.
Since even reasonable numbers that we use all the time make for very long binary numbers, the bits are grouped in 3s and 4s which are simple to convert into numbers in the octal and hexadecimal number systems. For octal, we group three bits together. Take the binary equivilent of decimal 1234, 10011010010, and put spaces in between each group of three bits - starting at the right and going left.
10011010010 = 10 011 010 010
Now use the symbols 0, 1, 2, 3, 4, 5, 6, and 7 (eight symbols, so OCTAL) to replace each group.
10 011 010 010 = 2 3 2 2 = 2322
The octal representations of the binary patterns are certainly easier to read and write than the binary counterparts. An even more compact representation can be achieved by grouping the bits in chunks of four and converting these to hexNumerals.
When you group four bits together and use sixteen symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, A, B, C, D, E, and F) as their abbreviations, you have a hexadecimal representation.
10011010010 = 100 1101 0010 = 4C2
As you continue to explore how computers work, you'll hear more about numbers expressed in octal and hex; these are just more manageable representations of binary information - the digital world.
That's about as deep as I want to get into binary, octal and hexadecimal number systems. If you want to read more, I googled and found what looks like a good place for you to read more. Start at All About Circuits - Systems of numeration and read through it and continue on for a few more web pages in the series.
I'll end this section with Table 1.1, comparing the number systems.
| Decimal Number |
in Binary |
in Octal |
in Hex |
1 |
1 |
1 |
1 |
2 |
10 |
2 |
2 |
3 |
11 |
3 |
3 |
4 |
100 |
4 |
4 |
5 |
101 |
5 |
5 |
6 |
110 |
6 |
6 |
7 |
111 |
7 |
7 |
8 |
1000 |
10 |
8 |
9 |
1001 |
11 |
9 |
10 |
1010 |
12 |
A |
11 |
1011 |
13 |
B |
12 |
1100 |
14 |
C |
13 |
1101 |
15 |
D |
14 |
1110 |
16 |
E |
15 |
1111 |
17 |
F |
16 |
10000 |
20 |
10 |
|
|
|||
Symbols as Bits - ASCII Characters
Ok, the colored pixels that make up the display are just numbers made up of three groups of eight bits. Numbers are simply groups of bits. What about the symbols that make up an alphabet?
It should come as no surprise that symbols that make up alphabets are just numbers, groups of bits, too. But how do we know which numbers are used to represent which symbols, or characters as I'm going to call them from this point on?
It's all about standards. In these lessons, we will use the American Standard Code for Information Interchange (ASCII) standard. It is so ubiquious that it even has its own web page, www.asciitable.com.
Let's walk through a couple of examples of entries in the table. Here are some characters, their decimal value, and their binary value which is then transformed into an octal number.
Uppercase 'A' = decimal 65 = binary 01000001 = 01 000 001 = octal 101
Uppercase 'Z' = decimal 90 = binary 01011010 = 01 011 010 = octal 132
The digit '1' = decimal 49 = binary 00110001 = 00 110 001 = octal 061
Table 1.2 is a small slice from the full ASCII character set, just enough to give you a flavor of its organization.
| ASCII Character |
in Binary |
in Octal |
in Decimal |
in Hex |
space |
00100000 |
040 |
32 |
20 |
( |
00101000 |
050 |
40 |
28 |
) |
00101001 |
051 |
41 |
29 |
* |
00101010 |
052 |
42 |
2A |
0 |
00110000 |
060 |
48 |
30 |
1 |
00110001 |
061 |
49 |
31 |
2 |
00110010 |
062 |
50 |
32 |
9 |
00111001 |
071 |
57 |
39 |
A |
01000001 |
101 |
65 |
41 |
B |
01000010 |
102 |
66 |
42 |
C |
01000011 |
103 |
67 |
43 |
Z |
01011010 |
132 |
90 |
5A |
a |
01100001 |
141 |
97 |
61 |
b |
01100010 |
142 |
98 |
62 |
c |
01100011 |
143 |
99 |
63 |
z |
01111010 |
172 |
122 |
7A |
| |
||||
Pixels
A computer display consists of a bunch of colored points called pixels. A pixel is an object. It has a position (its coordinates) which consists of the row and column it is in. Figure 1.1 shows an artist's rendition, a magnification of a display with a circle drawn in yellow. The tiny black dots are the pixels and the big yellow dots are the pixels that have been colored.
As an example, to display a thin vertical line, the color values of a column of pixels are set to the desired color of the line. If you want a thicker vertical line, you set the color values of the pixels of a group of consecutive columns to the desired color. Figure 1.2 shows a red line that's a single pixel wide and an orange line that's three pixels wide. The orange line is actually a very thin rectangle.
So, the location of each pixel is obviously specified by a pair of numbers; what about the pixel's color?
Well... a pixel's color is also specified as numbers, three of them, called RGB (Red, Green, Blue) values. Play with the following Java applet which lets you see what number values generate which colors. What color do you get if you set red to 170, green to 85, and blue to 255? What's the RGB value for your favorite color?
So, just as groups of bits represent numbers and symbols, they are used to form pixels. Now let's look at one more thing that is composed of bits: a computer's instructions.
Color Numbers AppletProgramming Languages
(The Microprocessor's Language)
So, all a computer has in it is bits. You've seen how they are used to represent stuff, pixels, numbers and characters. I've mentioned that computers perform operations on the bits, like move them around, add pairs of them together, etc... One final obvious question is: how are instructions that a computer performs represented?
Well, if you instructed a computer in its native language (machine language), you would have to write instructions in the form of (yes, once again) binary numbers. This is very, VERY hard to do. Although the pioneers of computer science did this, no one does this these days. How this is done would take too long of an explanation to fit into this lesson. If you really want the details now, here is a side lesson from one of my favorite introductory computer science books: The Robot Computer.
One step above machine language is assembler language. In assembler language, the operations that the microprocessor knows how to do are given symbolic names. Addresses in memory are also given meaningful names. This is a big step over binary, but still very tedious to use for any large software program. It still has its place for little snipits of software that need to interact directly with the microprocessor and/or those that are executed many, many, many times.
Table 1.3 is an example of DEC PDP-10 assembler language, a procedure that returns the largest integer in an array named NUMARY, with NCOUNT elements.
| Label | OpCode | Register | Memory Address |
Index Register |
Comment |
| GETMAX: | MOVSI | T1 | 400000 | ; init T1 to smallest integer | |
| MOVE | T2 | NCOUNT | ; get number of array elements | ||
| GTMAX2: | SOJL | T2 | [POPJ P,] | ; decr idx, if -1 then done | |
| CAMG | T1 | NUMARY | (T2) | ; skip if T1 > array element | |
| JRST | GTMAX2 | ; continue with next number | |||
| MOVE | T1 | NUMARY | (T2) | ; T2 gets new max number | |
| JRST | GTMAX2 | ; continue with next number | |||
|
|
|||||
I'm showing you this so that you will have a feel for how primitive computer instruction sets are. I'm not going to go into the details of every instruction. If you want to go through it in detail on your own, the PDP-10 Machine Language is on the Internet here.
A few points I want to expose you to are the general kinds of things being done.
- moving groups of bits into the computer's registers - very fast temporary storage,
- decrementing the value in a register,
- comparing the contents of a register to some value, and
- transfering control to an instruction that's not in the standard sequential order - down the page.
Higher-level programming languages provide similar functionality. But, as you will see, you get a lot more done with a lot less typing (detail). There is also a very big problem with assembler language, it is unique for every computer architecture. Although most deskside and notebook computers these days use the Intel architecture, this is only recently the case. A variety of computer architectures are still used in game systems, mobile phones, automobiles, appliances, etc...
Programming Languages (High-Level Languages)
Most software written today is written in high-level languages. There are many and some are quite old. COBOL, FORTRAN, and Lisp were written in the 1950s!!! Higher-level languages make it easier to describe the pieces of the program you are creating. They abstract away the specifics of the microprocessor in your computer. Most come with large sets of common stuff you need to do, called libraries.
In this introduction to programming, you will work with two computer languages: Logo and Java. Logo comes from Bolt, Beranek & Newman (BBN) and Massachusetts Institute of Technology (MIT). Seymour Papert, a scientist at MIT's Artificial Intelligence Laboratory, championed the computer programming language in the 70s. Java is a fairly recent programming language. It appeared in 1995 just as the Internet was starting to get lots of attention. Java was invented by James Gosling, working at Sun Microsystems. It's sort-of a medium-level language.
I like Logo as an initial programming language because it is simple to learn. The faster you get to write interesting computer programs the more fun you will have. As an example, here is the instruction in jLogo that does the same thing as the assembler language code in Table 1.3.
|
But don't let the simplicity fool you into thinking it is just a toy programming language. Logo is a derivative of the Lisp programming language, a very powerful language still used today to tackle some of the most advanced research being performed. Brian Harvey shows the power of Logo in his Computer Science Logo Style series of books. Volume 3: Beyond Programming covers six college-level computer science topics.
One of the advantages of learning Java is that there is a lot of software already written which will help you write graphical programs that run on the Internet. You get to take advantage of software that thousands of programmers have already written. Java is used in a variety of applications, from mobile phones to massive Internet data manipulation. You get to work with window objects, Internet connection objects, database access objects and thousands of others.
Both Logo and Java have the same sort of tools that you saw in the previous section. The have the ability to move things around, compare things and do different things depending on the outcome of the comparison, and they provide ways to control what instructions get executed. And... that's what programming is really all about... as you will see.
Programming Using the English Language
Remember what I said in the Introduction to this lesson?
Writing software, computer programs, is a lot like
writing down the steps it takes to do something.
Before we see what the Logo computer language looks like, let's use the English language to describe how to do something as a series of steps. A common exercise that really gets you thinking about what computer programming can be like is to describe a process you are familiar with.
Describe how to make a peanut butter and jelly sandwich.
Rather than write my own version of this exercise, I searched the Internet for the words "computer programming sandwich" using Google. One of the hits returned was http://teachers.net/lessons/posts/2166.html. At the link, Deb Sweeney (Tamaqua Area Middle School, Tamaqua, PA) described the problem as:
Objective: Students will write specific and sequential steps
on how to make a peanut butter and jelly sandwich.
Procedure: Students will write a very detailed and step-by-step
paragraph on how to make a peanut butter and jelly
sandwich for homework. The next day, the students will
then input (read) their instructions to the computer
(teacher). The teacher will then "make" the programs,
being sure to do exactly what the students said...
This exercise is excellent for demonstrating how careful you need to be, how detailed you need to be, when writing a computer program.
Hierarchy of Computer Languages
In summary, a computer is simply a very fast manipulator of bits, ones and zeros. Through the power of abstraction, we have layered levels of object representation and functionality, one on top of another. We have now been working on refining and extending these layers for over half of a century. Table 1.4 gives you a feel for where we are today.
|
Examples: Mathematica, SQL |
|
Examples: Logo, Python |
|
Example: C |
|
Example: Intel X86 |
| |
Go to the Table of Contents
On to Introductory Logo Commands