You are on page 1of 5

Chapters 1 & 2: Notes

Computers consist of Hardware and Software.

Hardware: Processors, Memory, and Peripheral Devices.

Software: Programs:- Operating Systems, Applications.


The combination of an operating system and a particular type of CPU is called a
platform. For example wintel (combination of windows and Intel processor). The
platform is very important for development of application. Until JAVA, when a program
was written to run in windows environment, it would not or cannot run in for example on
a Macintosh. That program had to be rewritten in a format compatible with a Mac.

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.

Storing Data: Machine storage using Binary numbers using Unicode.


Variables: Locations used to store data within a program are called variables.

Programming Languages: Basic (Beginner’s All-purpose Symbolic Instruction Code).


Ada, C, C++, Fortran (Formula Translation), VB, Pascal,
Cobol (Common Business-Oriented Language), etc.
We are of course interested in JAVA. A very versatile language that is fairly small to
handle. It lets you develop applications (as in C or C++) as well as enable the
development of applets (for use on the Web) and Database manipulation.

Why JAVA? Page 20.

The programming process.


1. Write a specification.
2. Design the program
3. Choose algorithms and decide how to store data.
4. Write the program.
5. Compile the program and correct compilation errors
6. Execute the program
7. Debug the program.
8. Maintain the program.

Writing a JAVA program.

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.

Since we would be using JDK 1.2.2, there is no IDE (Integrated Development


Environment)

An example.

public class JavaRules


{
public static void main (String[] args)
{
System.out.println(“Java Rules”);
}
}

This program should be saved as JavaRules.java ,


Note:
1. The upper case J and R. Java is very picky about the case of letters.
2. The word class should start with lower case c.
3. The name of the class and the name of the program (file) is the same except with the
extension – java.
4. The class that contains the main method should be the name given to the program.
5. The class that contains the main method is the only class allowed to be public.
6. The key word static should always be used with the main method.
7. The key word void is used in the main method because main is not returning any
value.
8. Always use the argument (Parameter) args of type String array in the main method.
Also note that string begins with upper case S.
9. The word system also begins with upper case S.
10. The use of semi-colon at the end of the statement.
11. The opening and closing of curly braces.

To compile this program go to the DOS prompt – as shown and type the following

C:\ javac JavaRules.java

To run the program, at the DOS prompt 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.

The building blocks of a Java program are:


1. Classes – A collection of related variables and/or methods.
2. Methods – A series of statements.
3. Statements – A statement is a single command.

Comments and Layout:

There are 3 comment styles:


1. // - single line comment.
2. /* */ Multiline comment
3. /** */ Doc Comment – designed to be used with javadoc whereby the comments
are extracted to produce on-line documentation.

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

Indentation and Brace Placement:


Varies, but they should make the program presentable and clearly readable.

Variables and Data Types:

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.

Rules for Identifiers:

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.

Keywords and reserved words:

These are defined within the language whereby they are given special meanings. They
cannot be used as identifiers.

Calculations:

1. The ‘=’ sign is used as the assignment operator.


2. The left side of the assignment operator must be a variable.
3. The right side can be:
a. Another variable – example x = y;
b. An expression – example x = a + b – c * d;
c. A function – example x = Math.pow(5,2);
4. Using the assignment operator to for example increment:
a. x += 1;
b. x -= 1;
c. x *= 2;
d. x /= 2;
e. x %= 3;
5. The operators used for Math are
*, /, %
+, -
6. Associativity and Precedence: Left to right and *, /, %, +, -
7. Mixing Double and Integer:
a. 6.1 + 2.2  8.3  Legal.
b. 6.1 + 2  8.3  Legal.
c. 6.1 * 2  12.2  Legal.
d. 6.1 * 2.2  13.42  Legal.
8. Round Off Errors. Example it is difficult to store a number such as 2.2 in binary, thus
when doing some amount of math involving 2.2 there may exist round off errors.
9. To declare Constants use the key word FINAL instead of simply declaring a variable
example: Instead of declaring
double pi = 3.145 it is better to declare:
final double pi = 3.145

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.

You might also like