RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect01a
a first program

Arithmetic, types, and a first program

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 the specific title “Fall and Rise of the Roman Empire”). 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 htat 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

the problem

You work for Krusteaze Pizza. As a server, people are constantly asking you:

(Your job isn't made easier by the owner, who is constantly introducing new sizes of pizzas, as an advertising schtick.)

solving specific cases, in Java

As a conscientious pizza server, how do you answer them? Well, we remember that the area of a circle is … well, if we forget, we can look it up: πr2. What is π? That's just a named constant; approximately 3.14. What is r? r is a parameter, standing for the radius.

So, when asked about a pizza with diameter 16″, then the radius must be 8″, so our answer is: π(16/2)². How to enter this expression, on a calculator? Even if there is no button for squaring, nor for π?

No problem. Knowing that π is approximately, 3.14, we can type 3.14 × 8 × 8, to get 200.96. Thus, pizzaArea(16) ∼ 200.96. (this is an approximation because π isn't exactly 3.14).

In java, it's even a bit worse though -- there's no × on the computer keyboard! What do we type instead? Yes -- 3.14 * 8 * 8. (See, you already knew some Java!)

Q: What do we type in Java, to compute the area of a 12″ pizza? A: 3.14 * 6 * 6
pizzaArea(16) ∼ 113.04.
Here we see why Java's “double” is a (doubly-precise) estimate: rather than 113.04, it reports 113.03999999999999 That's close to the true value of 3.14×36, but is admittedly wrong by 0.00000000000001. Java's doubly-precise arithmetic is usually correct to about 15 places, and usually that's good enough, but keep in mind it's not quite exact. We'll talk about Java arithmetic errors later.

Q: Calculate, by yourself, the area of a 20″ pizza. (Hint: write it out, and you'll realize you don't need a calculator.)
A: 3.14 * 10 * 10 = 314. pizzaArea(20) ∼ 314

the general solution, in Java

What is the general case? In high school algebra, we would write the general case as:

A(d) = pi · (d/2)2
This is a function, which abstracts the cases d=12, d=14, d=18, and any other diameter1.

How to translate this algebra into Java?
Attempt 1 (of several):

  pizzaArea(diam) {
    return 3.14 * (diam/2) * (diam/2);
    }
To call this Java function, we'll write (in a moment) something like:
pizzaArea(12) (this evaluates to ...)
pizzaArea(18) (this evaluates to ...)
pizzaArea(20) (this evaluates to ...)

This definition is close, but not quite complete Java yet. It does abstract the repeated calculation into a function, which makes us happy. diam is the function's “formal parameter”, and when we call the function we pass the function a “concrete argument” (e.g. 12 or 14 or 18).

This Java definition of pizzaArea is similar to the way it's specified in the arithmetic, but with some extra scaffolding: curly-brackets around the body of the function, the keyword return, and a semicolon at the end of the return statement. (What corresponds to the algebra's “=” in the Java version?) This scaffolding is simply things Java requires you to add, to write the function.

While some programming languages might accept the above, Java requires you to specify a bit more information: it needs you do specify the type of the argument given to the function, as well as the type which gets returned by the function. In this case, the type is “Number”, or more specifically, double (for “doubly-precise estimate”; we can tell it's an estimate2 because clearly 3.14 is not the correct value of π).

Other types are integers (int), true/false (boolean), or more sophisticated types like List or Dictionary or MouseEvent — We'll get into all that over the course of the class!

Okay, back to our program; this is how we specify that the argument diam is a double, and that the function returns a double: Attempt 2:

  double pizzaArea( double diam ) {
    return 3.14 * (diam/2) * (diam/2);
    }
This is a plausible Java program, and we can actually type this program into Java. From BlueJ, find the main window: Congratulations, you have written your program, gotten it running, and even run a couple of cursory test cases!

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

The Fourth3 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!


1well, non-negative diameter      

2Single-precision estimates are archaic and we won't use them; they are called “float”.      

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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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