Académique Documents
Professionnel Documents
Culture Documents
CONCEPT
OBJECTIVES
1
describe the various tools used in
software development
Why JAVA ?
2
2.1 A JAVA PROGRAM
3
A class Three objects
Equations ---- Linear Equations
---- Quadratic Equations
---- Cubic Equations
4
Declarations
int NoOfEquations
5
Methods
6
1. invocation - using a method to be
performed.
8
For example:
{
method1();
method2 ();
}
.. method1 () {
??????
} // end of method1
9
The form of a program
class classname {
public static void main ( String [ ] arg)
{
declarations and statements
}
}
class DoNothing
{ // Begin of class
public static void main ( String [ ] arg )
{ // Begin of main
// Do nothing in main
} // End of main
} // End of class
11
// A simple program that prints a message
class Lincoln
{
public static void main ( String [ ] args )
{
System.out.println (How are you ?);
}
}
13
Functions defined inside a class are called
method, i.e. all JAVA functions are
methods.
The main method definition always
preceded by the words public, static, void.
Also the use of String and args.
main method invoke another method called
println.
println method is part of the JAVA
Applications Programming Interface
(API)
14
White space
15
The lines of a program should be divided
in logical places, with certain lines
indented and aligned, so that the program's
underlying structure is clear.
16
// A simple program that prints a message
class
Lincoln
{
public
static
void
main
(
String
[ ] args )
{ System.out.println
(
How are you?
);
}}
17
Comment
Key Concept
// This is a comment ,
// or another form
/* This is also a comment */
Comments should be well-written, usually in
complete sentence.
// Print Hello
System.out.println (hello);
20
abstract default goto operator synchronized
boolean do if outer this
break continue import package switch
byvalue else inner private throw
case extends int protected true
cast else interface public try
catch final long return var
char float nave short void
class for new static volatile
const generic null super while
double finally nave rest transient
21
An identifier is a word that a programmer
creates in a program, such as class or method
name.
22
An identifier can be composed of any
combination of letters, digits, the underscore
(_), and the dollar sign ($), but it cannot begin
with a digit. It may be of any length.
For example :
23
Both uppercase and lowercase letters can be
used in an identifier, and the difference is
important. JAVA is case sensitive, i.e. total,
Total, ToTal, and TOTAL are all different
identifiers.
Important : Do not use unique identifiers that
differ by only case because they can be easily
misunderstood.
Always choose some meaningful identifiers.
Avoid choosing a long word for identifier.
24
It is a good practice to use underscore
character to separate words in an identifier.
For example : current_time, final_score.
currentTime, finalScore
25
2.2 Program Execution
A program can be written in a variety of
programming languages, such as JAVA,
PASCAL, C and C++. The task of the
program is essentially the same, but the
particular constructs used to express the
program instructions vary.
26
Programming language Levels
Programming languages are often
categorized into the following four groups :
machine language
assembly language
high-level language
fourth-generation language
Key Concept
All programs must be translated to a
particular computers machine language in
order to be executed.
27
Compilers and Interpreters
28
The original code is called source code,
and the language to which it is translated
is called the targeted language.
29
In JAVA, the targeted language for the
compiler is called Java bytecode, which is
a representation of the program in a low-
level form similar to a machine language
code.
30
The Java translation and
Java Source execution process
Code
Java Bytecode
Interpreter compiler
Machine code31
The difference between Java bytecode and
true machine language code is that Java
bytecode is not tied to any particular
processor type.
32
Syntax and Semantic
The syntax rules of a language exactly
dictate how the vocabulary elements of the
language can be put together to form
statements.
If a program is not syntactically correct,
the compiler will issue error messages and
will not produce bytecote.
The semantics of a statement in a
programming language define what will
happen when the statement is executed,
and must be well-defined.
33
Errors
compile-time error
run-time error
logical error
34
Compiling and Executing a Java
Program
NameOfJavaProgram.java
javac NameOfJavaProgram.java
NameOfJavaProgram.class
36
Let the following program : Lincoln.java
class Lincoln {
public static void main ( String [ ] args )
{
System.out.println (How are you ?);
}
}
38
Using Command Line Arguments
class Lincoln {
public static void main ( String [ ] args )
{
System.out.print (How are you
+ args[0]+ ,);
System.out.println (+ I am
+ args[1]));
}
}
40
2.3. Why JAVA ?
Early high-level languages FORTRAN,
PASCAL embody the procedural approach
to developing software, in which programs
are decomposed into manageable pieces
called procedures (or functions)
To improve the large-scale software
development was the introduction of
object oriented programming (OOP)
41
* A fundamental concept behind OOP is
the use of objects.
JAVA is an OOP.
42
Software Components
43
Key Concept
44
2.4. Class Libraries
A class library is a set of classes, usually
related by inheritance, that support the
development of programs.
The compiler for a programming language
often comes with a class library.
The classes in a class library define methods
that are often invaluable to a programmer.
45
The JAVA API
46
All of the packages in Java API are
collected into a single package called
java.
To refer to a package inside java package,
a period (or a dot .) is used to separate the
names. Some packages in the Java API :
java.applet :
contains classes that support the
development of applets, which are Java
programs that are intended to be linked
to HTML documents and made
accessible over the WEB.
47
java.awt :
package support the use of graphic in a
Java program.
java.io :
package contains classes that assist the
programmer in accomplishing various
kinds of input and output.
java.util :
package contains a set of general
purpose utility classes that serve many
purposes.
java.lang :
package contains several classes that
support primary language issues, such
as mathematical functions. The System
class, through the println is invoked. 48
The import Statement
The import statement identifies the packages
and classes of the Java API that will be used
in a program. Two general form :
import package.class or
import package.*
49
Example
import java.io.BufferedReader.*;
or *
class Echo {
public static void main (String [] args)
throws IOException {
BufferedReader stdin =
new BufferedReader (
new InputStreamReader
(System.in));
50
String message;
Message = stdin.readLine();
System.out.println (Message);
} // end main
} // end class Echo
51