RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs

lect01a
a first program

computing vs. programming

Have you computed before? (Have you computed things that weren't numeric?) You certainly have!

These are all examples of computing — running a program. You followed a set of instructions (a program) to do each of these tasks.

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.

A first program

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, FXStudios want locations relative to the upper-left corner. After some head-scratching, we realize that we need to know the height of some objects: the ufo-base, the entire screen, and even the UFO itself. You ask the FXStudio (by right-clicking FXStudio > Inspect), and find out that the secret base is 102 pixels high, the screen is 400 pixels high, and the UFO is 82 pixels tall.

Test cases

Where should the UFO be drawn at…

input:output:
frame#elevation
0??
1??
2??
3??
16??
100??
(lect01a-soln)

the general solution, programmed in arithmetic

In arithmetic, we'd write this as something like:

h(x) = 400-102-x-82
where 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.

The general solution, programmed in Java

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:

We have succesfully translated the solution written in algebra into the solution writtein in Java.

Running our program

Okay, now that we have the general program, let's use it to compute some specific cases: we create a new RocketScientist and call her, say, raki.
[We demo the function in class, calling methods from BlueJ's code pad.]
Then we can ask raki to compute ufoHeightAt(0), or ufoHeightAt(3), etc..
Congratulations, you've written your first Java program!

While it's nice and good that we can ask questions of our RocketScientist, it can get better. What if the movie studio asked our RocketScientist where to draw the ufo, for every single frame of the scene? Without showing the innards, we provide code for FXStudios. This code has no idea where to draw a ufo, but once they're told where3, they can make a fabulously beautiful picture of it. If century23 is a particular FXStudio, then we can ask century23 to do things like createStillFrame(17), or even the first two second of the movie: createMovieClip(0,48). The FXStudio asks its internal consultant RocketScientist what ufoHeightAt to use for every each frame it creates.

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).

Reflecting on the process

In writing ufoHeightAt, we started by computing some sample cases by hand, before writing the general-case program. This approach wasn't accidental:

The Fourth4 Law of Programming
Before writing a program, first compute some test cases by hand.
There are two reasons for this law: Clearly, it's easier to do a specific calculation than write a general program for the same task. If you ever find yourself at a loss as to how start writing a program, you might well have forgotten to do some specific cases first. And if you have trouble with deciding how to compute a specific case, then you shouldn't be skipping to the harder problem of solving the general case!


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 FXStudio has their own internal RocketScientist which they consult—asking him to compute ufoHeightAt—in order to render each movie frame.      

4Fear not -- we'll encounter the preceding four laws over the next few weeks.      

homeinfolectslabsexamshws
tutor/PIsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Aug.29 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme