To begin learning about programming, we are going to work with a number of micro-world examples. This chapter introduces the basic concepts using one such example: the Light-Bot.
Lightbot is a very simple robot programming simulation game. It runs right in your web browser, and looks like this:
In the Light-Bot game, you create a program to control a little robot to solve a simple task in the miniature block-based world where the Light-Bot exists. The programming in Light-Bot is iconic. The icons at the top right of the game's display represent instructions to the robot. Each icon instructs the robot to do something--go one square forward, turn right, turn left, jump forward, and light up the square the robot standing on. Of course the square will only light up if the robot is on a blue square. The speaker icon in the upper left corner will allow you to toggle the game sound on and off. The reset button in the lower left corner will delete all of your instructions and start the level over.
Your task on each level of the Light-Bot game is to get the robot to light up all of the blue squares. To create a program to control the robot simply drag the icons at the top and drop them on the main method squares left to right.
Play the first two levels of Light-Bot to try things out. (Click the "play" link below the righthand advertisement.)
The Light-Bot game illustrates several basic programming concepts. The robot itself is an object, and we can request it to perform specific actions that it knows how to carry out. We can write a series of these requests down as a program. The series of actions that we write down is a plan for solving a specific problem (an algorithm).
The Light-Bot game is an example of a micro-world. A micro-world is a miniature simulated environment that is populated with one or more objects with clearly defined behaviors, and that can be visually represented so that you can see how the objects move around and behave over time.
Micro-worlds typically use a grid to represent locations in the simulated world. You can see this in the Light-Bot game, where the robot walks on square tiles. In this course, we will use the same approach. We can refer to any location on a grid using x and y coordinates:
We will use a variation of Cartesian coordinates to write any location as an (x, y) pair, where x (the first coordinate) represents the distance from the origin horizontally, and y (the second coordinate) represents the distance from the origin vertically. The only catch is that the y axis points downward in our grid, making this a left-handed Cartesian plane. We use a left-handed orientation for our axes because that convention is predominant in computer programming contexts (even though it is the opposite of what is typical in studying geometry!).
So the highlighted cell in the grid shown above is at location (4, 2), since it is 4 cells horizontally to the right of the origin, and 2 cells vertically down from the origin. Also notice that the origin (0, 0) is explicitly represented in our grid and is located at the top left corner. The grid above is 10 by 10 in size, where coordinates run from 0 up to and including 9. This means we will always used zero-based indexing when referring to grid positions--i.e., we always start counting from zero at the origin (top left), rather than starting with one.
If you only played the first two levels of Light-Bot, there's definitely more interesting stuff you haven't seen! As the levels progress, the puzzles get more complicated. They are harder to solve using the limited number of slots in the program memory for the little robot. Fortunately, the Light-Bot game provides a few more icons for you to use: f1 and f2.
The f1 and f2 icons represent the two functions (icon areas) below the main method in the Light-Bot game. These two functions are where you will place instructions that the robot will need to perform multiple times. Anywhere you place the f1 or f2 icon, the robot will perform the entire sequence of actions associated with that function. This will allow you to build more complex programs which are necessary to solve the more challenging tasks in the later levels.
Return to the Light-Bot game above in Exercise 1. Continue to play through Level 7. Once you have completed Level 7, stop. You'll return to finish Level 8 in the next exercise.
Computer programs are most often written in textual form--in fact, one of the most important goals of programming is to write programs so that they communicate well to other people. So let's look at how we can represent Light-Bot puzzle solutions as text.
At its most basic, a computer program is simply a sequence of
instructions for a computer to follow. Our Light-Bot level solutions
are just that: programs. All we need in order to write them textually
is names that correspond to the icons. For example, if we call our
andy, we could use the following names to
correspond to his icons (from left to right):
Each action is phrased as a request to an object that
performs the desired behavior. So each request goes to our robot,
andy. For example, consider Level 2:
One solution to this level is:
andy.move(); andy.turnRight(); andy.move(); andy.turnLeft(); andy.move(); andy.move(); andy.turnLeft(); andy.move(); andy.turnRight(); andy.move(); andy.turnLightOn();
To Turn In Return to the Light-Bot game above in Exercise 1. Complete Level 7 if you have not already done so. Once you have completed Level 7, think carefully about your solution for Level 8. Compose your solution for Level 8 but don't run it yet. Write down your solution for Level 8 in textual form:
main: put your instructions here f1: put your instructions here f2: put your instructions here
After writing down your solution, then run it. If it does not work the way you planned, revise it and revise your textual version, too before running it again. Your goal is to have a textual representation of your final solution to Level 8, which will be your answer to this Exercise.
The Light-Bot game illustrates several basic programming concepts:
In this course, we are going to focus on learning object-oriented programming in Java. This section describes several general concepts about object-oriented programs that we have seen in this first example. As you read subsequent chapters, refer back to this chapter to review the meaning of important words and phrases.
It doesn't matter whether we are sending email, surfing the net, listening to music, writing an essay, or playing a game, whenever we use a computer we are using one or more computer programs. Each program is simply a set of instructions for the computer.
Every program is written by one or more programmers. Programmers use a programming language, such as Java, C++, Python, or Visual Basic to write a computer program. For Light-Bot, you first used an iconic language, and then learned how to use a simple textual language that represents exactly the same actions.
Programs exist in many forms. The form that a programmer writes is called the source code for the program. Unfortunately, a computer cannot use source code directly. Source code must be translated into machine language before it can be executed (run) by the computer.
There are several kinds of translation. A compiler translates a program, as a whole, from one form to another, but not necessarily into machine language. An interpreter translates a program into machine language one statement at a time. Each statement is executed as soon as it has been translated. Light-Bot is an example of an interpreter, since it converts each source code action directly into computer behavior, one statement at a time.
Every computer program starts with a plan. That plan is called an algorithm. There are many ways to write an algorithm. Some are very informal, some are quite formal and mathematical in nature, and some are quite graphical. The form is not particularly important as long as it provides a good way to describe and check the logic of the plan.
It should come as no surprise to learn that an object-oriented programming language works with objects. But what is an object? Unfortunately, this concept is difficult to define because an object is simultaneously something that only exists within a computer program and a logical representation of something else. A good approach is to define an object in terms of what it represents.
A typical program works with several different objects. Some of these may represent the same kind of thing. For example, in Light-Bot it seems obvious that the robot itself is an object. But there are also bricks that block the robot's path and that can be stacked. There are also blue squares that can be lighted. There might be multiple bricks, or multiple blue squares on a level, and these are all individual objects, even though they are of the same kind.
An object represents something, but we, as programmers, need to determine what characteristics of that thing are important to the problem we are trying to solve. There are two parts to an object, facts about the thing it represents (Is the blue square lit yet, or still dim? Which direction is the robot facing?), and tasks that the object can perform (the robot can move, turn left or right, and so on). The facts are called attributes (we'll cover those later) and the tasks are called methods.
When we design an object, we need to determin what tasks it should perform. In doing so, we tend to anthropomorphize the item that the object represents. (To anthropomorphize means to ascribe human characteristics to non-human things.) For example, we might want the Light-Bot to move from one location to another, or jump up on an obstacle.
For example, our Light-Bot supports a number of methods that correspond to the icons we can use, and which we gave textual names in the previous section. Also, some methods only work under certain conditions: the robot can only jump (up or down) if the square immediately in front of it is exactly one block higher or lower than where the robot is standing. This is a precondition. Similarly, the icon to light up the current square only works when the robot is standing on a blue square (also a precondition). However, if the robot is indeed standing on a blue square, and then it executes its "turn light on" behavior, then afterward the blue square will be lit (a postcondition, which describes the outcome of executing a specific behavior or method).
When we write an object-oriented program, we instantiate appropriate objects and ask them to perform specific tasks. We use message to make these requests.