|
Spring Semester 2002
|
Lecture Notes Two: Algorithms. Programs.
Mechanics of implementation in Java.
Before we look at the mechanics of implementing computations
let us consider the planning process that precedes the implementation.
|
If you can't give instructions for someone to solve a problem by
hand, there is no way the computer can magically solve the problem.
|
The computer can only do what you do by hand. It just does it faster, and it doesn't
get bored or exhausted.
|
I'd like to see an example.
|
OK, let's consider the following investment problem:
|
|
You put $10,000 into a bank account that earns 5% interest per year. Interest is
compounded annually. How many years does it take for the account balance to be double
the original?
|
One could solve this problem by hand.
|
Sure, let's do that. After the first year you earn $500 (5% of $10,000). The
interest gets added to your bank account and your balance becomes $10,500.00.
Next year, the interest is $525 (5% of $10,500)...
|
... and your balance is $11,025.
|
You can keep going that way and build a table:
|
|
Year | Balance |
0 | $10,000.00 | $10,000.00 |
1 | $10,000.00 + 0.05 * $10,000 = | $10,500.00 |
2 | $10,500.00 + 0.05 * $10,500.00 = | $11,025.00 |
3 | $11,025.00 + 0.05 * $11,025.00 = | $11,576.25 |
4 | $11,576.25 + 0.05 * $11,576.25 = | $12,155.06 |
... | ... |
|
You keep going until the balance goes over $20,000.00 and when
it does you look into the "Year" column and you have the answer.
|
Of course, carrying out this computation is intensely boring.
|
Yes, but the fact that a computation is boring or tedious is irrelevant to a computer.
Computers are very good at carrying out repetitive calculations quickly and flawlessly.
|
What is important to the computer is the existence of a systematic
approach for finding the solution. The answer can be found just by following
a series of steps that involves no guesswork.
|
|
|
Here's such a series of steps:
|
Step 1 |
Start with the table:
Year | Balance |
0 | $10,000.00 |
|
Step 2 |
Repeat steps 2a, 2b, 2c while the balance is less than $20,000.00
Step 2a. | Add a new row to the table. |
Step 2b. | In column 1 of the new row, labeled "Year",
put one more than the preceding year value. |
Step 2c. |
In column 2 of the new row, labeled "Balance", place
the value of the preceding balance value, multiplied by
1.05 (1 + 5%)
|
|
Step 3 |
Read the last number in the year column
and report it as the number of years required
to double the investment.
|
Of course, these steps are not yet in a language
that a computer can understand, but you will learn
soon how to formulate them in Java.
|
Is this collection of steps an algorithm?
|
Yes, because the description is unambigous...
|
There are precise instructions what to do in every step
and where to go next. There is no room for guesswork or
creativity.
|
... executable, and terminating.
|
Because each step can be carried out in practice, and the computation
can be shown to come to an end: with every step, the balance goes up by
at least $500.00, so eventually it must reach $20,000.00
|
Now we start looking at the mechanics of implementing computations in Java.
|
|
|
Let's analyze our first program.
|
public class Hello
{ public static void main(String[] args)
{ System.out.println("Hello, World!");
}
}
You know that you should make a new program file and call it
Hello.java , enter the program instructions, then
compile and run the program.
|
That's clear, the contents is more intriguing.
|
It is composed of words and symbols separated by spaces.
|
The words and symbols are important and atomic: they're like
words and symbols in an English sentence. Bring me a glass
of water!
|
Yes, but it's a lot stricter. Java is case-sensitive.
You must enter upper- and lowercase letters exactly as
they appear in the program listing.
|
On the other hand Java has free-form layout.
Spaces and line breaks are not important, except to separate words.
|
However, good taste dictates that you lay out your programs in readable
fashion, so you should follow the layout in the program listing.
|
Now that we've seen the program, it's time to understand its makeup.
|
The part in blue introduces a class,
called Hello .
|
|
public class Hello
{ public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
|
In Java, classes are the central organizing mechanism for code.
|
You can't do anything in Java without
defining at least a class.
|
That is why we introduce the Hello
class as the holder of the main method.
|
Java, like most programming languages, requires that
all program statements must be placed inside methods.
|
A method is a collection of programming instructions
that describe how to carry out a particular task.
|
The part in blue further defines the main method.
|
|
public class Hello
{ public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
|
Every Java application must have a main method.
|
Most Java programs contain other methods beside main ,
but it will take us a while to learn how to write other methods. For
the time being, simply put all instructions that you want to have
executed inside the main method of a class.
|
|
|
I have them here marked in blue.
|
public class Hello
{ public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The instructions or statements in the
body of the main method (that
is, the statements inside the curly braces {
} ) are executed one by one. Note that each
statement ends in a semicolon (; ).
|
|
|
Our method has a single statement:
|
System.out.println("Hello, World!");
Yes, but it, too, has a structure.
|
The statement is supposed to print a line of text. I presume
the text is enclosed by double quotes (" ).
|
Yes, a sequence of characters in quotation marks is called a string.
You must enclose the contents of the string inside quotation marks so that the
compiler considers them plain text and does not try to interpret them as program
instructions.
|
To print the text you call a method println as if you'd
call Papa John's for a large pizza. But which Papa John's? You need
to precisely locate it. Suppose you say: the one on 3rd
Street. But which 3rd: most towns have a 3rd
Street. So you need to add Bloomington, and then IN.
|
All of this is apparent in their telephone number:
812 | 323 | PAPA |
System | out | println |
|
|
|
From this analogy it looks like System contains
out which contains println .
|
Yes, we call the println method that
is part of the out object, that is part
of the System class, and we pass it the
string that we wanted printed.
|
So that's what the parentheses are for...
|
Yes, in fact that's how we tell that
out is merely data, and
not a method: method names are always
followed by a pair of parentheses.
|
Why is System uppercased
and out lowercased?
|
This is only a convention, that object variables (or
names) start with a lowercase letter, while classes names
should start with an upper case. Using this convention is
strongly encouraged, as part of the style guide.
|
So let's summarize: designers of the Java libraries defined a
System class in which they've put useful objects
and methods. One of these objects is called out
and it lets you access the terminal window (also called the
standard output). To use the out
object in the System class you must refer
to it as System.out ; it has a method inside
it, by the name println which we can use,
and so we do.
|
That's correct.
|
Asking the computer to execute a method is also known
as calling or invoking the method.
|
When we call a method we can pass it information
in between parentheses. If we pass no information
there will be an empty pair of parentheses.
|
In this case we pass one string.
|
Have you looked at the exercises yet?
|
What for?
|
How would you print
Hello, "World"!
|
|
|
You need to escape the quotation marks inside the string
with a backslash (\ ), like that:
|
public class Hello
{ public static void main(String[] args) {
System.out.println("Hello, \"World\"!");
}
}
It becomes harder to read, but it's also more precise. The
computer won't mistake any of the two escaped double quotes
as being the end of the string.
|
What other escape sequences are there?
|
Since the backslash character is used as an escape
character, it needs to be escaped itself, if we need
it in output.
Another escape sequence used ocasionally
is \n which is the same as new line or
line feed character.
|
Are there any other things that we could pass to println
for printing?
|
Yes, for example arithmetic expressions.
|
Such as:
-
3 + 4
-
(2.5 - 1) / 4
-
(3 + 4) * (2 - 5)
|
Yes. What's the asterisk (* ) for?
|
It's for multiplication, and / for division.
|
Very good. You could even "add" strings by listing them with
a + between them. println will actually
concatenate them (or string them together).
|
OK, maybe I won't do that right away. What's the difference between
println and print ?
|
The out object contains a second method called
print . You can see the difference between the two
methods if you consider the following program:
|
What does it do?
|
public class Test
{ public static void main(String[] args)
{ System.out.println("Hello, ");
System.out.println("World! ");
}
}
The println method prints a string or a number
and then starts a new line. The print method does
the same printing, without starting a new line afterwards.
|
|
|
I see... putting all of the things we've learned together
I could write the same program as follows:
|
public class Test
{ public static void main(String[] args)
{ System.out.print("Hello, \n");
System.out.print("World! \n");
}
}
Yes, and in fact in many other ways.
|
I won't count how many...
|
Last updated: Jan 9, 2002 by Adrian German for A201