Vous êtes sur la page 1sur 51

CHAPTER 2 : SOFTWARE

CONCEPT
OBJECTIVES

dissect some initial Java programs,


exploring the ideas of syntax and
semantics

explain the purpose and evolution of


programming languages

1
describe the various tools used in
software development

define various categories of


programming errors

Why JAVA ?

2
2.1 A JAVA PROGRAM

A JAVA program consists of a set of


one or more inter-dependent classes.

Classes are a means for describing the


properties and capabilities of the object in
real life that the program has to deal with.

An object is a concrete realization of a


class description. For example

3
A class Three objects
Equations ---- Linear Equations
---- Quadratic Equations
---- Cubic Equations

In JAVA, the properties and capabilities of


a class are called declaration and
methods.

4
Declarations

The properties of a class are given by


mean of declarations of data items that its
objects can use to store information.

In JAVA, declarations indicate both the


name of the data item and its types. For
example :

int NoOfEquations

5
Methods

The capabilities of class are expressed in


one or more methods.

A method is a named sequence of


instructions to the computer, written out in
the language we are using, in this case
JAVA.

The instructions are called statements, and


fall into the following categories:

6
1. invocation - using a method to be
performed.

2. assignment - changing the state of a


variable by using another value of
some type.

3. repetition - performing certain


statements over and over again.

4. selection - deciding whether to


perform certain statements or not.

In addition, a method can also declare its


own variables which are said to be local to
it, and therefore not accessible to other
methods. 7
Invoking a method

When we mention the name of a method,


control within the program is transferred
to that method, which then progresses
through its statement until it reaches the
end. The method then passes control back
to where it was called from. This process
is called method invocation.

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
}
}

bold text -- JAVA keywords that must


be there. e.g. class.
italics identifiers and other parts that we
fill in. e.g. classname
plain words - identifiers that have to be
there but not keywords, e.g. main and
String 10
First JAVA Program !!!!!

// My First JAVA Program

class DoNothing
{ // Begin of class
public static void main ( String [ ] arg )
{ // Begin of main
// Do nothing in main
} // End of main
} // End of class

A simple , but complete, JAVA Program.

11
// A simple program that prints a message

class Lincoln
{
public static void main ( String [ ] args )
{
System.out.println (How are you ?);
}
}

When executed, the program prints the


following line of text :
How are you?
12
Examine the important features in the program:
Comment statements
class definition : { . }
main function ( A function in a
programming language is a group of
instructions that are given a name ), also
delimited by { . }.

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

All JAVA program uses white space to


separate words. White space consists of
blanks, tabs, and newline characters.

Except when separating words, the


computer ignores white space and it does
not affect the execution of our program.
This fact gives us a great deal of flexibility
in how we structure a program.

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.

For example : (previous program)

// A simple program that prints a message

class Lincoln {public static void main (


String [ ] args ) { System.out.println
(How are you?);
}}

16
// A simple program that prints a message
class
Lincoln
{
public
static
void
main
(
String
[ ] args )
{ System.out.println
(
How are you?
);
}}
17
Comment

Comment are the only language feature


that allow programmers to compose and
communicate their thoughts, independent
of the code.
Comments should provide insight to the
authors intent when writing the program.
Good documentation implies easy for
future maintenance.

Key Concept

Insightful documentation and appropriate


use of white space make your programs
easier to read and understand. 18
In JAVA, two different forms of
comments:

// This is a comment ,
// or another form
/* This is also a comment */
Comments should be well-written, usually in
complete sentence.

They should not belabor ideas that are


obvious. For example :

// Print Hello
System.out.println (hello);

// Will modify later


System.out.println (test);
19
Reserved Words, Identifiers, and literals
Reserved words are words that have
special meaning in a programming
language and can only be used in
predefined ways.

Some JAVA reserved words :

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.

An identifier has no predefined meaning in the


language itself. For example, in the Lincoln
program, we have identifiers : Lincoln, main,
String, args, and System.out.println.

Note that : String, System.out.println are


identifiers defined in JAVA API.

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 :

Valid identifiers : total, $lable1, bye_bye


Invalid identifiers : 4MM, coin#value

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.

or separated each of the word by an


uppercase, for example

currentTime, finalScore

Literals are explicit data values that used in a


program. For example, in the program
Lincoln, the only literal is "How are you?"
which is a string of characters. Literals can
also be numeric data values, such as 3.14159.

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.

Furthermore, a computer must be able to


understand the instructions in order to
carry them out.

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

Several special-purpose programs are


needed to help with the process of
developing new programs. They are
sometimes called software tool,

e.g. editor, compiler, interpreter.


An editor is used to type a program into a
computer and store it inside a file.
A compiler is a program that translates
code in one language to equivalent code in
another language.

28
The original code is called source code,
and the language to which it is translated
is called the targeted language.

An interpreter is similar to a compiler.


Instead of separating the translation and
execution activities, an interpreter
interweaves them.

A small part of the source code, such as


one statement, is translated and executed.
Then another statement is translated and
executed and so on.

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.

The Java interpreter reads Java bytecodes


and executes it on a specific machine.

30
The Java translation and
Java Source execution process
Code

Java Compiler Java


bytecode

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.

This approach has the distinct advantage


of making Java architecture-neutral, and
therefore easily portable from one
machine type to another.

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

There are three kinds of errors that you will


likely encounter while developing programs:

compile-time error
run-time error
logical error

34
Compiling and Executing a Java
Program

The name of a Java compiler is called


javac.

To compile a Java program, the source


code of a Java program must be stored in a
file as

NameOfJavaProgram.java

The file name for a Java program must be


the same as the class which contains the
main method.
35
To compile a Java program, just enter at
the command line :

javac NameOfJavaProgram.java

If there is no compilation errors, a new file


is created called

NameOfJavaProgram.class

which contains Java bytecode.

To execute, just type at the command line


java NameOfJavaProgram

36
Let the following program : Lincoln.java

// A simple program that prints a message

class Lincoln {
public static void main ( String [ ] args )
{
System.out.println (How are you ?);
}
}

In our sunfire system :


hengak@sf3 > javac Lincoln.java
hengak@sf3 > java Lincoln
How are you ?
hengak@sf3 >
37
Same program with some errors :

// A simple program that prints a message


class Lincoln {
public static void main ( String [ ] args )
{
System.out.println (How are you ?)
}
}

hengak@sf3 > javac Lincoln.java


Lincoln.java:6: ; expected.
System.out.println (How are you ?)

1 error
hengak@sf3 >

38
Using Command Line Arguments

The main method always accepts a


potential list of command line arguments.
Any information typed after the file name
when the program is submitted to the
interpreter for execution is considered to
be a command line arguments and can be
referenced in the program.

In the Lincoln program, we call the list of


arguments args.
The arguments are always considered as a
list of character strings.
1st argument : args[0]
2nd argument : args[1] and etc
39
// A simple program that prints a message

class Lincoln {
public static void main ( String [ ] args )
{
System.out.print (How are you
+ args[0]+ ,);
System.out.println (+ I am
+ args[1]));
}
}

hengak@sf3 > java Lincoln John Heng


How are you John, I am Heng
hengak@sf3>

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.

* An object is a programming entity that


groups related methods and the data those
methods use.

* easy to represent objects in the real


objects in software.

* An object is designed to be a reusable


entity that can be easily incorporated
into multiple program.

JAVA is an OOP.
42
Software Components

The basic purpose of software is to turn


input into output.
A software component performs a specific
task, transforming input into output and
can be combined with other component.
A software component might be a piece of
larger program, or it might be a program
that cooperates with other programs to
accomplish a task.

43
Key Concept

Modern software systems are constructed


from interacting and cooperating
components. The output of one component is
used as the input to another.

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

The Java programming interface (API) is a


set of libraries.
The classes of Java API are grouped into
packages.
A package is a Java language element
used to group related classes under a
common name.
Packages can be nested inside one another.

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

e.g import java.awt


import java.util.

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

Vous aimerez peut-être aussi