Algorithms, Programming, and Compilers
A program starts as an algorithm, a plan a person could follow, and ends as code a computer runs. This lesson follows one algorithm, a parking garage's count of its free spaces, from written steps to Java.
Three ways to write an algorithm
The garage opens with 120 free spaces. By noon, 35 cars have come in and 8 have left. Written in ordinary language, the algorithm for the noon count has four parts. Start from the opening count, take away the cars that came in, add back the cars that left, and report what's left.
The same algorithm can be written as numbered steps.
Step 1: Set spaces to 120.
Step 2: Subtract 35 from spaces.
Step 3: Add 8 to spaces.
Step 4: Display spaces.
After Step 2, spaces holds 85. After Step 3 it holds 93, so Step 4 displays 93.
The order of the steps
The order is part of the algorithm. Put the Display step first and the same four steps report 120, because nothing has been subtracted or added when the display happens. Each step works with the value spaces holds at the moment it runs.
A flowchart
A diagram of the same algorithm is a flowchart. Each box holds one step, and the arrows give the order.
+---------------------------+
| START |
+---------------------------+
|
v
+---------------------------+
| Set spaces to 120 |
+---------------------------+
|
v
+---------------------------+
| Subtract 35 from spaces |
+---------------------------+
|
v
+---------------------------+
| Add 8 to spaces |
+---------------------------+
|
v
+---------------------------+
| Display spaces |
+---------------------------+
|
v
+---------------------------+
| END |
+---------------------------+
Following the arrows gives 85, then 93, and 93 is displayed. The flowchart holds the same steps as the numbered list, in the same order.
The algorithm in Java
A program is an algorithm written in a language a computer can carry out, and this course writes programs in Java. Here's the garage count as a Java program. Read it for now. The next few topics teach each line.
public class Garage
{
public static void main(String[] args)
{
int spaces = 120;
int cameIn = 35;
int left = 8;
System.out.println("Free spaces: " + (spaces - cameIn + left));
}
}
The first lines and the braces are a frame that every program in this unit has. There's a class named
Garage, and inside it a method named main, where the program begins. The statements inside main run one at a time, in order, like the numbered steps.The line
int spaces = 120; creates a variable named spaces that holds whole numbers, and stores 120 in it. The next two lines do the same for the cars that came in and the cars that left. The last line works out spaces - cameIn + left one operator at a time, starting at the left. Subtracting 35 from 120 gives 85, and adding 8 to that gives 93. Then System.out.println displays the quoted text with the number joined after it, so the program displays Free spaces: 93.The compiler
Java can be typed in any text editor. Before it runs, a compiler translates it into instructions the computer can carry out. On the way, the compiler checks the code for some kinds of error. It refuses to translate code that has one, so there's nothing to run until the code is corrected.
Syntax, logic, and run-time errors
Suppose the declaration is typed with a capital letter.
Int spaces = 120;
Java is case-sensitive, so
Int and int are different names, and there's no type named Int. The compiler reports the line and produces nothing to run. This is a syntax error.Now suppose the subtraction is typed as an addition.
System.out.println("Free spaces: " + (spaces + cameIn + left));Every rule of Java is followed, so the program compiles, runs to the end, and displays
Free spaces: 163. The compiler can't know that cars coming in should lower the count. This is a logic error. It shows up only when someone runs the program on data whose correct result they know. Here the count worked by hand is 93, and 163 doesn't match it.The third kind happens while the program runs. Suppose all three levels of the garage are closed for cleaning, and the program reports the free spaces per open level.
int spaces = 93;
int openLevels = 0;
System.out.println("Per level: " + spaces / openLevels);
To build the line it would display, Java has to divide 93 by 0, which it can't do. The program stops at that line, and Java reports
java.lang.ArithmeticException: / by zero. The code follows every rule, so the compiler had no reason to object. The problem exists only once the program is running and openLevels holds 0. An error like this is called an exception. It happens while the program runs, the compiler had no way to catch it, and it breaks off the program.