Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER ENGINEERING
JAVAPROGRAMMING
CHAPTER1 ProgrammingFundamentals
Duration:2Weeks(6Hrs.)
(
)
PreparedBy,
Dr.TamerTULGAR
WhatisJAVA?
HISTORY
Java began at Sun Microsystems around 1990 and was developed primarily
by James Gosling with inspiration from Bill Joy.
Called
C
ll d Oak
O k originally,
i i ll the
th language
l
was intended
i t d d as control
t l software
ft
for
f
embedded microprocessors in consumer items like cable set-top
boxes,VCR's, toasters, etc, as well as personal data assistants (PDA). This
meant that it should be
Platform independent since the processors would come from multiple
manufacturers.
Extremely reliable and compact.
compact
The interactive TV and PDA markets never took off, but in 1993 the internet
and the Web began to explode and the Java emphasis was quickly shifted to
internet applications.
Not much attention paid until the HotJava browser, written in Java at Sun in
a few months, appeared in 1994. It illustrated the power of applets, Java
programs downloaded over the net and run in the browser at the client end.
JAVAPROGRAMMING
PreparedBy,Dr.TamerTULGAR
WhatisJAVA?
HISTORY
In 1996 the Java Development Kit (JDK) Version 1.0 was released by Sun and
available free for downloading. Version 1.1, with substantially greater
capabilities, was released in 1997.
Netscape 2.0-4.0 included Java 1.0. Microsoft and other companies also
licensed Java.
Battles began over control of Java. Microsoft added capabilities to its
version of Java to take advantage of native Windows functions but this
reduced Java
s platform independence
independence. Sun came out with the "100%
100% Pure
Java's
Java" campaign.
Sun is trying to turn Java over to a standards body yet still maintain status
as the primary source. Sun also wants control of the copyrights to logos,
etc.
JAVAPROGRAMMING
PreparedBy,Dr.TamerTULGAR
WhatisJAVA?
JAVA Features
Simplicity
Si
li i - (at
( least
l
compared
d to C and
d C++
C )
no pointers,
no preprocessor
automatic garbage collection
Familiarity - similar enough to C and C++ that programmers can get going quickly.
PreparedBy,Dr.TamerTULGAR
WhatisJAVA?
JAVA Features
Platform Independence - once the virtual interpreter is ported to a given machine, the
Java routines will run on that platform without code modification or re-compilation.
Good Performance - interpretation of bytecodes instead of text, as in Basic, provides
fast performance, although can still be substantially slower than programs compiled
to native machine code. However, just-in-time compilers, which compile the Java
bytecodes to native code on the fly, will provide performance at 50-90% the speed of
C/C
C/C++ programs.
Built-in Networking - since Java was designed with networking in mind, it comes with
many classes available for internet communications. Can easily design routines to
communicate via URL's as well as design standalone server/client systems using
more direct communications.
Th di - multiple
lti l Lightweight
Li ht i ht processes, called
ll d threads,
th d can easily
il be
b spun off
ff to
t
Threading
perform multi-processing. These are used, for example, in multi-media programs
where one thread presents graphics and another plays the sound recordings.
Dynamic - objects of new classes can be added dynamically at run-time. For example,
a routine implementing a new kind of compression/decompression format could be
downloaded along with the data file to carry out the decompression of the picture
JAVAPROGRAMMING
PreparedBy,Dr.TamerTULGAR
WhatisJAVA?
JAVA Features
PreparedBy,Dr.TamerTULGAR
YOURFIRSTJAVAPROGRAM
The main program must be invoked without being called as the first function. Only static functions can be called
without creating
g the instance to the classes. So we are declaring
g it as static.
Because public is an access specifier and static is the keyword that executes instantly when java program run ...we
can access the main () in the another class if we need
JAVAPROGRAMMING
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Classical Way
1 . Write
W it your source code
d in
i
any text editor and save it in a
folder you want with .java
extension.
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Netbeans IDE
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Netbeans IDE
JAVAPROGRAMMING
10
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Netbeans IDE
JAVAPROGRAMMING
11
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Netbeans IDE
12
PreparedBy,Dr.TamerTULGAR
HOWTOCOMPILEANDRUNAJAVAPROGRAM
Netbeans IDE
3 . Write your source code, compile run and see the output. (IDE)
JAVAPROGRAMMING
13
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
Programming Fundamentals
14
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
1. Display
y Hello World 10 times
JAVAPROGRAMMING
15
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
2. Display
p y Hello World only
y on the even counts of the loop
p
JAVAPROGRAMMING
16
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
3. Display
p y the for loop
p counter
JAVAPROGRAMMING
17
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
18
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
19
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
20
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
21
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
!! HOMEWORK !!
1
1.
Create a random integer and display the prime numbers between 0 and that random
number.
2.
Declare a matrix, fill it by user input, display the sum and the avarage of the
elements.
3.
Declare three 10 by 10 matrices, assign the matrix product of the first two to the third
matrix Display the product.
product
matrix.
4.
Take integers from the user until he/she enters 1000, display the sum and the avarage
of the numbers.
5.
Declare an integer array of 100 elements. Fill your array with random numbers and
sort your array in ascending order using:
I
I.
Bubble Sort
II.
Quick Sort
http://www.oracle.com/technetwork/java/javase/downloads/index.html
22
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
Escape Codes
Lowest
Precedence
JAVAPROGRAMMING
23
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
Integer data can be stored with int type. There are various sizes of int;
The char type is used for individual characters, such as letters, numbers,
punctuation, and other symbols.
Lastly, in Java we have boolean type which can be either true or false.
JAVAPROGRAMMING
24
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
eg
e.g.
final float a = 3.56;
JAVAPROGRAMMING
25
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
object creation.
JAVAPROGRAMMING
26
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
27
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
array2[9]
JAVAPROGRAMMING
28
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
sample[2]
sample[3]
sample[4]
l [5]
sample[5]
29
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
30
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
31
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
32
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
33
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
34
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
JAVAPROGRAMMING
35
PreparedBy,Dr.TamerTULGAR
JAVALANGUAGE
Arrays of Strings
JAVAPROGRAMMING
36
PreparedBy,Dr.TamerTULGAR