|
home—info—lects—labs—exams—hws
tutor/PIs—java.lang docs—java.util docs
Have you computed before? (Have you computed things that weren't numeric?) You certainly have!
Have you programmed before? (Have you programmed without using a computer?) Yes:
A computation is taking a general set of instructions for a task, and applying it to a specific situation (instance) of the task.We call the specific situation the input argument(s) (for example, a room's measurements of 25′ × 20′ × 10′, or a specific species “aardvark”). You've been computing since kindergarten.
A program is a general set of instructions for some task which can be applied to any specific instance of the task.A computation solves one problem — but a program solves an infinite family of problems! Programs are more complicated than computations, because they're phrased in terms of parameters (or variables): the room-dimensions l, h, w, or a book-name titlebook. This difference between a specific instance and the general, abstract case is a powerful concept, but more difficult: humans can't really make that distinction until around age 13 or so (which is why algebra isn't taught before that age: the idea of working with x and y instead of specific numbers is an abstraction).
In this class we'll learn programming, which is bit like algebra except that instead of working with numbers, it's been enriched to talk about any information: book-names, game-show boards, etc.. We'll happen to write our programs in a language called Java, which is nice because it lets the (boring, repetitive) computation side of things be carried out by a machine.
You are a rocket scientist. You have been fired by a special effects firm, to help make the first scene of their awesome upcoming UFO movie: They want to show the UFO dramatically rising from the secret UFO base. They can certainly making cool pictures, but they want to ask you where to draw the UFO on the screen.
Some domain-specific knowledge about movies: A movie is a series of still frames, projected rapidly. Each frame of the movie is created separately. The special effects folk will ask you questions like “where should the UFO be drawn, on frame 43?” and “where should the UFO be drawn, on frame 17?” You answer should be the y-coordinate of the UFO (it's altitude).
Zero seconds into the movie—at frame #0—the UFO should be sitting
right on top the secret base.
But what screen-location is this?
It turns out,
Where should the UFO be drawn at…
input: | output: |
---|---|
frame# | elevation |
0 | ?? |
1 | ?? |
2 | ?? |
3 | ?? |
16 | ?? |
100 | ?? |
In arithmetic, we'd write this as something like:
h(x) = 400-102-x-82where h (for “height”) is the function's name, and x represents the general case -- x can stand for whatever number is being asked about (0, 3, 17, whatever). We say that x is a parameter.
Note how the test cases correspond to individual computations; the general case (involving x) is programming.
Okay, if we can write the solution in arithmetic, then it's actually not difficult to write the same solution in the computer language Java:
ufoHeightAt( frameNum ) |
ufoHeightAt( frameNum ) 400-102-frameNum-82 |
ufoHeightAt( frameNum ) return 400-102-frameNum-82 |
ufoHeightAt( frameNum ) { return 400-102-frameNum-82; } |
Finally, Java wants one more piece of information:
the type of information being handled.
(In arithmetic, everything used numbers, but in programming we'll extend
that idea to text, true/false values,
as well as customized bundles of other data.)
In this case, the
ufoHeightAt( int frameNum ) { return 400-102-frameNum-82; } |
int ufoHeightAt( int frameNum ) { return 400-102-frameNum-82; } |
(We'll see other types of data in upcoming lessons:
Okay, now that we have the general program, let's use it to
compute some specific cases:
we create a new
[We demo the function in class,
calling methods from BlueJ's code pad.]
Then we can ask
Congratulations, you've written your first Java program!
While it's nice and good that we can ask questions of our
In tomorrow's lab, we'll see how to type in and run a program ourselves (using BlueJ).
(If you're adventurous, here is the FXStudio's jar file, and directions for downloading a jar file into BlueJ).
In writing
The Fourth4 Law of ProgrammingThere are two reasons for this law:
Before writing a program, first compute some test cases by hand.
1A set of instructions for playing a game is called a strategy. ↩
2 Your math classes only used a few functions at a time, so they got by with the name like f, g, h. Occasionally they used descriptive names like cos or min. In this class, though, we'll be writing programs with a dozen functions, so for the sake of our sanity, it's important to choose function names which actually describe what they compute. ↩
3
The
4Fear not -- we'll encounter the preceding four laws over the next few weeks. ↩
home—info—lects—labs—exams—hws
tutor/PIs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Aug.29 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |