50
A Java introduction SDK, FC UMR AMAP 15/12/2009 SDK, FC (UMR AMAP) A Java introduction 15/12/2009 1 / 50

A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

A Java introduction

SDK, FC

UMR AMAP

15/12/2009

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 1 / 50

Page 2: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Plan

1 Introduction

2 BasesJava ApplicationVariables & ExpressionsSimple ArraysExceptionsCollectionsControl structureFunctions

3 ObjectsClassesInheritancePolymorphism

4 Java standard library

5 To go further

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 2 / 50

Page 3: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

History

James Gosling and Sun Microsystems

Java, May 20, 1995

Java 1 → Java 6

GPL with classpath exception since 2006

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 3 / 50

Page 4: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Characteristics

Object Oriented

Interpreted (need a virtual machine)

Portable (Linux, Mac, Windows)

Dynamic (introspection)

Static typing (checks during compilation)

Simplier than C++ (memory management, pointers, headers. . .)

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 4 / 50

Page 5: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Programming environment

Java Environnment

JRE (Java runtime environment)

JDK (Java Development Kit)

Jave SE (Standard edition)

Java EE (Enterprise edition → Web)

Java ME (Micro edition)

IDE - Editor

Eclipse, NetBeans : Full IDE (completion, compilation...)

Simple Editors : Notepad++, TextPad, Scite (coloration, indentation)

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 5 / 50

Page 6: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Installation

Windows

Download and install the last JDK (Java SE 6)

Environment variable (Computer → properties)

Add to PATH variable the bin directoryeg : “C :/Program Files/Java/jdk1.6.0 version/bin”

Install editor TextPad or Notepad++

Linuxsudo apt-get install sun-java6-jdk

sudo apt-get remove openjdk-6-jdk

Editor : use scite or gedit

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50

Page 7: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Java application

Java programs written in files with extension “.java”

Applications are .java files with public static void main(...) method

Compile and run Java application

Run the compiler on a .java file : javac package/MyProgram.java⇒ Produce a file of Java byte code : MyProgram.classRun the interperter on a .class file : java package.MyProgram

the tools javac java are part of the JDK

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 7 / 50

Page 8: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Workflow

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 8 / 50

Page 9: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Hello world

package h e l l o ;

/∗∗∗ App l i c a t i o n He l l o wor ld∗/

p u b l i c c l a s s Hel loWor ld {

/∗∗ Main Func ion ∗/s t a t i c p u b l i c v o i d main ( S t r i n g [ ] a r g s ) {

// p r i n t to s c r e e nSystem . out . p r i n t l n ( "Hello world !" ) ;

}}

Print to console : System.out.println(“a string”) ;

Commands finish with a ;

Comments : // or /* */

Package : hello = directory

java hello.HelloWorld

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 9 / 50

Page 10: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Create a new application

Copy directory

Rename directory and file :

class Name and file name should be identicalThe package is a namespace and corresponds to the directoriespackage name and directory should be identical

Exercice : Create a new application “package1.App1”

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 10 / 50

Page 11: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Variable & Simple type

Variable

A variable has a type and hold a value or an Object

A variable name starts with lowercase, eg : myVariable ;

boolean : true or falseint : signed integer 32 bitslong : signed integer 64 bitsfloat : floating point 32 bitsdouble : floating point 64 bitsObject : a generic object

initialization

by default : 0

int aVariable = 2 ;

float anOtherVariable = 3.56f ;

Use final for constants : final float CONSTANT = 0.1f ;SDK, FC (UMR AMAP) A Java introduction 15/12/2009 11 / 50

Page 12: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Operators

Arithmetic

+, -, *, /, %

myInt++, otherInt−− ;

+=, -=, *=, /= . . .

Precedence : use parenthesis ()

division

2 / 3 different of 2.0 / 3.0

Division par zero → Infinity or NaN

Exercise : arithmetic.DivideByZero

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 12 / 50

Page 13: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Math

import java.lang.Math

Constante : Math.PI, Math.E

Math.abs(), Math.pow(), Math.sqrt(), Math.exp(), Math.log(),

Math.cos(), Math.tan()...

Math.toDegrees(), Math.toRadians()

Javadoc : Java MathExercice : Calculate triangle hypothenus with pythagore

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 13 / 50

Page 14: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Simple Arrays

One or 2 dimensions arrays

Dynamic allocation : new keyword

null if not initialized

Not resizable

Access with [] operator

Index begin at 0

S t r i n g [ ] a r r a y1a = new S t r i n g [ 1 2 ] ;S t r i n g [ ] a r r a y1b = {"toto" , "tata" , "titi"} ;

i n t s i z e = 4 ;double [ ] a r r a y 1 c = new double [ s i z e ] ;double [ ] [ ] a r r a y 2 = new double [ 4 ] [ 6 ] ;

a r r a y1a [ 1 1 ] = "a string" ;a r r a y 2 [ 0 ] [ 2 ] = 3d ;a r r a y 2 [ 3 ] [ 5 ] = 1d ;System . out . p r i n t l n ( a r r a y 2 [ 4 ] [ 6 ] ) ; // i ndex e r r o r

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 14 / 50

Page 15: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Error during execution

Try to use an object not initialized

Try to acces to a non exisiting element in array

double [ ] a r r a y = n u l l ;System . out . p r i n t l n ( a r r a y . l e n g t h ) ;a r r a y = new double [ 1 0 ] ;System . out . p r i n t l n ( a r r a y [ 1 0 ] ) ;

Exceptions are raised and stop the program

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 15 / 50

Page 16: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Exceptions

Error can be catched anywhere in the program

Specific exceptions can be raised

double [ ] a r r a y = n u l l ;

t r y {System . out . p r i n t l n ( a r r a y . l e n g t h ) ;

} catch ( Excep t i on e ) {

System . e r r . p r i n t l n ( e ) ;}

t r y {double r e s u l t = a r r a y [ 1 0 ] ;

} catch ( Excep t i on e ) {r e s u l t = 0 . ;

}

System . out . p r i n t l n ( "Result : " + r e s u l t ) ;

// i f ( r e s u l t < 0) { throw new Excep t i on (” r e s u l t cannot be n e g a t i v e ” ; }

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 16 / 50

Page 17: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Complex types

Java manipulates Objects

An object is a concept (e.g a particular data structure)

Objects are instantiated with the keyword new

A variable contains a reference to an object

Affectation is a reference copy (the variable point to the same object)

Objects are destroyed when there is no reference on it

By default an object variable is set to null

Objects have properties and methods accessible with the . operator

MyObject o1 , o2 ; // n u l l ;o1 = new MyObject ( ) ;o1 . v a l u e ;o1 . sum ( ) ;o2 = o1 ;o1 = n u l l ;o2 = n u l l ; // Object w i l l be d e s t r o y ed by the garbage c o l l e c t o r

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 17 / 50

Page 18: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Collections

Collections are dynamic containers

Associated to a specific type

Specific behaviour : list, set, map..

Simple types (int, float, double...) are not objects ⇒ need a wrapper

int ⇒ Integer

float ⇒ Float

double ⇒ Double

Collection class has method to sort, shuffle, reverse, ... collections

See javadoc : Collection java 1.6

Co l l e c t i o n<St r i ng> c1 ;C o l l e c t i o n<I n t e g e r> c2 ;C o l l e c t i o n<Double> c2 ;

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 18 / 50

Page 19: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

ArrayList

An ordered collection of variable size

Growth automatically

get, add and set method

Insertion is slow

For a stack or a queue, use LinkedList

L i s t<St r i ng> l = new Ar r a yL i s t<St r i ng >();l . add ( "toto" ) ;l . add ( "tata" ) ;l . add ( "titi" , 1 ) ; // i n s e r t a t i nd ex 1l . s e t (1 , "tutu" ) ; // r e p l a c e at i nd ex 1

l . s i z e ( ) ; // 3l . ge t ( 0 ) ; // ” to to ”. . .

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 19 / 50

Page 20: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

HashSet

An unordered collection without duplicated elements

contains method is fast

Set<St r i ng> s = new HashSet<St r i ng >();s . add ( "toto" ) ;s . add ( "tata" ) ;s . add ( "titi" ) ;s . add ( "toto" ) ; // f a l s e

s . s i z e ( ) ; // 3s . c o n t a i n s ( "tutu" ) ; // f a l s e. . .

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 20 / 50

Page 21: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

HashMap

Map associate a key with an object

a key should be unique (like in a Set)

Map<St r i ng , Color> m = new HashMap<St r i ng , Color >();m. put ( "black" , new Co lo r (0 , 0 , 0 ) ) ;m. put ( "red" , new Co lo r (1 , 0 , 0 ) ) ;m. put ( "green" , new Co lo r (0 , 1 , 0 ) ) ;m. put ( "blue" , new Co lo r (0 , 0 , 1 ) ) ;

m. ge t ( "red" ) ; // r e t u r n a c o l o r o b j e c tm. conta i n sKey ( "black" ) ; // t r u em. keySet ( ) ; // s e t o f keys

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 21 / 50

Page 22: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Boolean arithmetic

boolean v = true ;

! && ||

<= >= < > == !=

== and equals() : reference vs contents

!= and !equals()

use () for precedence

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 22 / 50

Page 23: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

if else

Simple condition

Can be combined

// s imp l e i fi f ( c o n d i t i o n ) {

b l o ck}

// complex i fi f ( c o n d i t i o n ) {

b lock1} e l s e i f ( o t h e r c o n d i t i o n ) {

b lock2} e l s e {

b lock3}

i f ( v1 && ! v2 ) {System . out . p r i n t l n ( "v1 and not v2" ) ;

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 23 / 50

Page 24: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

while

Loop with condition

i n t coun t e r = 0 ;w h i l e ( coun t e r < 100 ) {

System . out . p r i n t l n ( "counter : " + coun t e r ) ;c oun t e r++;

}

i n t counte r2 = 0 ;i n t sum = 0 ;w h i l e ( coun te r2 < 100) {

sum += Math . random ( ) ;i f ( sum > 20) { break ; }

coun t e r += 1 ;}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 24 / 50

Page 25: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

for

Loop with index

// With an a r r a yi n t [ ] a r r a y = new a r r a y [ 1 2 ] ;

i n t sum = 0 ;f o r ( i n t i =0; i<a r r a y . l e n g t h ; i++) {

sum += a r r a y [ i ] ;}

Loop in a collection

// With a l i s tL i s t<I n t e g e r> l = new Ar r a yL i s t<I n t e g e r >();i n t sum = 0 ;f o r ( I n t e g e r v : l ) {

sum += v ;}

// With a MapMap<St r i ng , Object> m = new HashMap<St r i ng , Object >();f o r ( S t r i n g key : m. keySet ( ) ) {

System . out . p r i n t l n (m. ge t ( key ) ) ;}

Exercice : do the sum of a double array

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 25 / 50

Page 26: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Enumeration

A enumeration is a type with a limited number of value

Type checking

Better than using integer or constante values

// Type d e c l a r a t i o nenum StopL igh t {red , amber , g r een } ;

// Use typeStopL ig th s ;

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 26 / 50

Page 27: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

static Functions

Like a mathematical function

⇒ Reuse a block of code

Inputs Parameters : type + variable name

Output : return type

s t a t i c double aFunct i on ( double param1 , double param2 ) {r e t u r n param1 ∗ param1 + param2 ;

}

param1 and param2 are the parameters

their names have a local scope : they are only available in the function

double a = 2 ;double b = 3 ;double r e s u l t = aFunct i on ( a , b ) ; // aFunct i on (2 , 3 )System . out . p r i n t l n ( r e s u l t ) ;

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 27 / 50

Page 28: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Functions (2)

a class can contain several functions

if no parameters ⇒ use ()

if no return type ⇒ use void

p u b l i c c l a s s MyClass {

p u b l i c s t a t i c double f u n c t i o n 1 ( double param1 , double param2 ) {. . .

}

p u b l i c s t a t i c v o i d f u n c t i o n 2 ( ) {. . .

}

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {. . .}

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 28 / 50

Page 29: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Read a file

Scanner tool parses a list of token

import j a v a . u t i l . Scanner ;

. . .// i n a f u n c t i o nScanner sc = new Scanner (new F i l e ( "filename.txt" ) ) ;

w h i l e ( sc . hasNext ( ) ) {

S t r i n g s = sc . nex t ( ) ; // read a s t r i n gi n t i = sc . n e x t I n t ( ) ; // read an i n tf l o a t f = sc . n e x t F l o a t ( ) ; // read a f l o a t

}

// Read i n the s h e l lScanner sc2 = new Scanner ( System . i n ) ;System . out . p r i n t ( "Input a string : " ) ;S t r i n g s = sc2 . nex t ( ) ;

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 29 / 50

Page 30: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Exercices

Create a file with a list of number : numbers.txt

Create a application which read the file in an ArrayList of Float

ArrayList<Float>

Compute the mean (and the variance) of the data

Put the code in a function

Use args argument in main to pass the filename in the command line

Use the function to compute the mean for multiple files

Try to put strings in the file and recover it

Remove minimal and maximal values in the mean

. . .

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 30 / 50

Page 31: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Object Oriented programming

Java manipulates Objects

E.g : a list, a number, a car, a species, a plant, a plot. . .

Concepts

Class : a type of object

Instance : a real object

Instance data : the data an object contains

Data are different for each instanciated object

Methods : the functions associated to an object

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 31 / 50

Page 32: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

How to use Object

Object are instanciated with the new keyword

We can instantiate several objects of the same type

Tree t1 = new Tree ( "Spruce" ) ; // i n i t i a l i z a t i o n wi th paramete rTree t2 = new Tree ( "Larch" ) ;Tree t3 = new Tree ( "Fir" ) ;

// each i n s t a n c e has i t s own datat1 . getSpec iesName ( ) ; // ” Spruce ”t2 . getSpec iesName ( ) ; // ” Larch ”t3 . getSpec iesName ( ) ; // ” F i r ”

// P r o p e r t i e st3 . h e i g h t = 2 ;System . out . p r i n t l n ( t1 . h e i g h t ) ;

// Methodst2 . se tSpec ie sName ( "Spruce" ) ;

// t2 != t1 but t2 . e qu a l s ( t1 )

// a f f e c t a t i o nt3 = t1 ; // t3 and t1 po i n t to the same ob j e c t t3 == t1t3 . getSpec iesName ( ) ; // Spruce

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 32 / 50

Page 33: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Class

A class is a type description

It contains data and methods (members)

p u b l i c c l a s s Tree {

p u b l i c double h e i g h t ;p r i v a t e S t r i n g spec iesName ;

// Con s t r u c t o r sp u b l i c Tree ( S t r i n g n ) {

spec iesName = n ;}

// Acc e s s o r sp u b l i c v o i d se tSpec ie sName ( S t r i n g n ) {

spec iesName = n ;}p u b l i c S t r i n g getSpec iesName ( ) { r e t u r n spec iesName ; }

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 33 / 50

Page 34: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Class...

Access control

Public : no access restriction

Private : only accessible in the class method

Protected : accessible in the class methods and in the subclasses

Constructor

Called when the object is created

Can take parameters or not

Can be declined in different versions

No return type

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 34 / 50

Page 35: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Exercices

tree/Tree.java

Add a private data : diameter

Add corresponding accessor

Add a method which return the volume of the trunk (we consider acylinder)

Create an application which use this class and this function

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 35 / 50

Page 36: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

This

this keyword represents the current object

Usefull if there is ambiguity with the names

p u b l i c c l a s s Tree {

. . ./∗∗ Volume o f the t runk ∗/p u b l i c double getVolume ( ) {

double volume ;double r a d i u s = t h i s . d i amete r / 2d ;volume = r a d i u s ∗ r a d i u s ∗ 2d ∗ Math . PI ∗ t h i s . h e i g h t ;

r e t u r n volume ;} ;. . .

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 36 / 50

Page 37: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

static

static members are associated to the type

static datas are shared by all instance

static methods are not attached to an instance

c l a s s Tree {. . .s t a t i c p u b l i c i n t sha r ed

s t a t i c p u b l i c double g e t Su r f a c e ( double d i amete r ) {double r a d i u s = d iamete r / 2d ;double s u r f a c e = r a d i u s ∗ r a d i u s ∗ 2d ∗ Math . PI ;

r e t u r n s u r f a c e ;}. . .

}

Tree . g e t Su r f a c e ( 2 . )Tree . sha r ed

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 37 / 50

Page 38: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Inheritance

We can reuse a class to make more specific classes

e.g : a tree with crown, a tree with leaves, etc...

Inheritance corresponds to a “is a” relation

A sub-class has all the data and methods of its parent

All class inherit from the class Object

Multiple inheritance is not allowed

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 38 / 50

Page 39: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Inheritance

// Tree . j a v ac l a s s Tree { // ex t end s Object

p r o t e c t e d double h e i g h t ;

/∗∗ Con s t r u c t o r ∗/p u b l i c Tree ( double h ) {

h e i g h t = h ;}

p u b l i c double ge tHe i gh t ( ) ;

}

// CrownTree . j a v ac l a s s CrownTree extends Tree {

p r o t e c t e d double crownHeigth ;p r o t e c t e d double crownDiameter ;

/∗∗ Con s t r u c t o r ∗/p u b l i c CrownTree ( double h , double ch , double cd ) {

super ( h ) ; // pa r en t c o n s t r u c t o rcrownHeigth = ch ;crownDiameter = cd ;

}

p u b l i c double getCrownVolume ( ) {. . .

}

}

// TrunkTree . j a v ac l a s s TrunkTree extends Tree {

. . .}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 39 / 50

Page 40: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

instanceof

All class inherit from Object

instanceof tests the type of an object

Tree t = new Tree (10d ) ;CrownTree c t = new CrownTree (10d , 4d , 4d ) ;

c t i n s t a n c e o f CrownTree ; // t r u ec t i n s t a n c e o f Tree ; // t r u ec t i n s t a n c e o f Object ; // t r u e

t i n s t a n c e o f Tree ; // t r u et i n s t a n c e o f CrownTree ; // f a l s e

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 40 / 50

Page 41: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Cast

We can use the parent type in the code (“is a” relation)

we use a cast, when we have to change the type to a subtype

a cast can fail

Tree t1 = new Tree (10d ) ;Tree t2 = new CrownTree (10d , 4d , 4d ) ;

t1 . getCrownVolume ( ) ; // f a i l : not a c rownt r e et2 . getCrownVolume ( ) ; // f a i l : j a v a t h i n k i t i s a Tree

CrownTree c t1 = ( CrownTree ) t2 ; //okc t1 . getCrownVolume ( ) ; // ok

CrownTree c t1 = ( CrownTree ) t1 ; // f a i l : t1 i s not a CrownTree

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 41 / 50

Page 42: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Override

Method can be overriden in sub-class

Use annotation @Override

c l a s s Tree {

/∗∗ Tree growth ∗/p u b l i c v o i d growth ( i n t nbYear ) {

d i amete r += nbYear ∗ dbhInc ;}

}

/∗∗ Tree wi th crown ∗/c l a s s CrownTree extends Tree {

/∗∗ Tree growth ∗/p u b l i c v o i d growth ( i n t nbYear ) {

super . growth ( nbYear ) ;crownDiameter += nbYear ∗ c rownInc ;

}}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 42 / 50

Page 43: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Interface

An interface is a kind of contract

A class can implement several interface (multiple inheritance)

An interface do not provide any implementation

An interface is a type

// Growthab le . j a v ai n t e r f a c e Growthab le {

p u b l i c v o i d growth ( i n t nbyear ) ;}

// Tree . j a v ac l a s s Tree implements Growthable , C l on eab l e {

p u b l i c v o i d growth ( i n t nbyear ) {// do growth

}

p u b l i c Object c l o n e ( ) {// do c l o n e . . .

}}

. . .Growthab le g = new Tree ( . . . ) ;g . growth ( 1 0 ) ;g . ge tD iamete r ( ) ; // e r r o r : j a v a don ’ t know i f i t i s a Tree

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 43 / 50

Page 44: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Abstract class

An abstract class is an incomplete class

It cannot be instantiated

Like an interface but with some implementation

Can be useful to share common methods in an inheritance graph

c l a s s a b s t r a c t Abs t r a c tT re e {

p r i v a t e double h e i g h t ;p r i v a t e double d i amete r ;

p u b l i c double getVolume ( ) {

double volume ;double r a d i u s = t h i s . d i amete r / 2d ;r e t u r n = r a d i u s ∗ r a d i u s ∗ 2d ∗ Math . PI ∗ t h i s . h e i g h t ;

}

a b s t r a c t i n t getNbLeaves ( ) ;

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 44 / 50

Page 45: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Abstract class

Each sub-class implements abstract methods

c l a s s TreeWithNbLeaves extends Abs t r a c tT re e {

p r i v a t e i n t nbLeaves ;

@Over r idei n t getNbLeaves ( ) {

r e t u r n nbLeaves ;}

}

c l a s s TreeWi thL i s tOfLeave s extends Abs t r a c tT re e {

p r i v a t e L i s t<Leaf> l e a v e s ;

@Over r idei n t getNbLeaves ( ) {

r e t u r n l e a v e s . s i z e ( ) ;}

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 45 / 50

Page 46: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Polymorphism

Polymorphism means that we can use an object without knowing itsexact type

The correct function will be called

L i s t<Abst rac tTree> l = new Ar r a yL i s t<Abst rac tTree >();

l . add (new TreeWithNbLeaves ( ) ) ;l . add (new TreeWi thL i s tOfLeave s ( ) ) ;

f o r ( Abs t r a c tT re e t : l ) {System . out . p r i n t l n ( "nb leaves : " + t . getNbLeaves ( ) ) ;

}

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 46 / 50

Page 47: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Exercices

In the tree package

Create the sub-class CrownTree

Add a method to compule the volume of the CrownTree (use@Override)

Create 2 sub-classes SphericTree and ConicTree

Override the volume function

Create a class Forest containing a list of trees

Add function to build a random forest

Compute the volume of the forest

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 47 / 50

Page 48: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Java library

See javadoc : http ://java.sun.com/javase/6/docs/api/

GUI

Math

Data Structure (Collections)

Input Output

Networking

MultiThreading

Database

Intropection

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 48 / 50

Page 49: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Apache commons libraries

See web : http ://commons.apache.org/

Math

IO

Loggin

CLI

Collections

. . .

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 49 / 50

Page 50: A Java introduction - capsis.cirad.frcapsis.cirad.fr/capsis/_media/javaforcapsis151209.pdf · SDK, FC (UMR AMAP) A Java introduction 15/12/2009 6 / 50. Java application Java programs

Some links

Sun’s tutorials : http ://java.sun.com/docs/books/tutorial/

Coding conventions :http ://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

Resources on Capsis web site : http ://capsis.cirad.fr

Millions of books. . .

SDK, FC (UMR AMAP) A Java introduction 15/12/2009 50 / 50