Professional Documents
Culture Documents
Interacting with a computer: GUI (Windows, Mac) and Command based (or text based
– DOS, UNIX).
Algorithms: A well-ordered collection of unambiguous and effectively computable
operations that, when executed, produces a result and halts in a finite
amount of time.
Algorithms could be developed using 1. Natural Languages; 2. Programming Languages
or 3. Pseudocode.
1
Use an editor to write the program – either the DOS editor or NotePad. Save the
program as name.java. The name of the program should match the name of the main
class of the program.
An example.
To compile this program go to the DOS prompt – as shown and type the following
C:\java JavaRules
Note: you must have the JDK compiler installed in your computer. The word javac lets
you invoke the java compiler to compile the program. This will compile your program
and create a class file of the same name of your file. Example the above program would
2
have a JavaRules.Class file created. This file would be made up of bytecode. It is this
bytecode that make Java versatile. This file could now be transferred to any platform and
be executed so long as the Java Virtual Machine is installed on that machine.
To execute the program, by using the java command, we are invoking the interpreter, that
is with the Java Virtual Machine. This interpret the bytecodes according to the machine’s
architecture. So Java is like a hybrid language – it is both a compilable and interpretive
language.
When a compiler compiles a program it group the characters into tokens, i.e. it would
start reading the program until it reaches a blank. All previously read characters (from the
start) to the first blank is one token. It will then skip all blanks (or lines) and then read the
next character. Note: When the compiler encounters a comment symbol, it skips
everything within the symbols (opening & closing).
Declaring variables – every variable must be declared before it can be used. Variables are
used to store data, hence one of the most important properties of variables is their data
type. Another important property is their name. Even though a variable can have any
name, they should always be named to reflect whatever they are representing. In
declaring a variable, the data type is declared first followed by the variable name and then
a semincolon. Example:
int i, j, k;
3 variables have been declared all of type integer. Note the use of the key word int
where the i is not capilaized. Also we could declare one or more variables on the same
line separated by commas. Also at the end of the declaration, we use a semicolon. We
could have initialized the variables upon declaration as well:-
int i = 0, j, k = 5;
3
Literals:
The actual value that may be associated with a variable example if the variable k is
assigned the value 5 then 5 is the literal value. Note a variable could be assigned a
character as a literal or a string as well.
May contain any letters (Upper or lower), digits or underscores. It cannot begin with a
digit. There is no limit to the length of an identifier. Note also that lower case letter is not
the same as upper case.
These are defined within the language whereby they are given special meanings. They
cannot be used as identifiers.
Calculations:
4
Types of Errors:
Compile Time errors: This is basically a syntax error for example a variable was use but
not declared, or a semicolon is missing, etc.
Run Time errors: These errors are harder to detect. They only occur when the program
is running. One of such errors is dividing by zero (0). A formula may be used to calculate
a value which is to be used to divide a number. It happens that the calculated value is 0,
thus a run time error. Another instance is if the user is supposed to enter an integer and
instead he/she enters a double.
Incorrect behavior: The cause is similar to that of run time errors. For example, you are
expecting the answer to be x but you get y instead. This may be because instead of a
multiplication operator, a addition operator was used.