Вы находитесь на странице: 1из 55

1

Advanced Programming
Methods
Lecture 1
2
Grading
Laboratory:--50%
- Assignments at each lab about 10 lab assigments! -- "5%
- #irst Practical $est %&am at LA'5 1 hour( o)en boo*!: -- 5%
- +econd Practical $est %&am at LA'11 or LA'12 or LA'1" 2
hours( o)en boo*!: --10%
#inal e&am: -- 50%
- ,ritten %&am about 1 hour( closed boo*s!: --20%
- Practical %&am about - hours( one )roblem in .ava( one
)roblem in /0( o)en boo*s!: --"0%
+eminar:
- your contribution 1ill be ta*en into account 2or the rounding o2
the 2inal mar*
"
3ules
- you have to )resent each assignment at its deadline
- each 1ee* o2 the assignment delay means -1)oints 2rom that
assigment mar*
- i2 you delay an assignment more than - 1ee*s you 1ill get the
grade 1 2or it
- i2 you 1ill not come to a lab )ractical e&am you 1ill get 1 2or that
e&am
- in order to get into the 2inal e&am your average grade 2or the lab
activities assigments 4 2 lab )ractical tests! must be at least 5
- in order to )ass the 2inal e&am you must have at least 5 at the
2inal 1ritten e&am and at least 5 at the 2inal )ractical e&am such
that the 2inal grade must be at least 5
- you can )ass either both the 2inal 1ritten e&am and the 2inal
)ractical e&am or nothing

-
3ules 2or the second e&am
5restanta6!

- in order to )ass the 2inal second e&am you must have at least 5
at the 2inal 1ritten e&am and at least 5 at the 2inal )ractical e&am
- you can )ass either both the second 2inal 1ritten e&am and the
second 2inal )ractical e&am or nothing
- at the second e&am 5restanta6 you can )resent or re-)resent
ma&imum 5 lab assigments in order to im)rove your lab
activities mar*



5
/ourse in2ormation
htt):771118cs8ubbclu98ro7:craciun27MetodeAvansate;ePr
ogramare7<n2o%ng
=
Lectures >vervie1
>b9ect-oriented languages:
- .ava
- /0
Lecture 1 and Lecture 2 Lab 1( Lab2( and +em1!:
.ava basics
Lecture " and Lecture - Lab "( Lab -( and +em2!:
/0 basics
A2ter - lectures and labs! :
- )ractical e&am at the lab( you 1ill be able to
1rite sim)le >> )rograms in .ava and /0
?
Lectures 142 'ibliogra2y

'ruce %c*el( Thinking in Java

.ava $utorials 2rom >racle:


htt):77docs8oracle8com79avase7tutorial7

.ava $echnology

.ava language
@ariables
;eclaration:
type name_var1[=expr1][, name_var2[=expr2]];
Primitive ;ata $y)es
$y)e Ar8 byte @alues ;e2ault value
boolean - true( 2alse 2alse
byte 1 -12B C 412? byte!0
short 2 -2
15
C2
15
-1 short!0
int - -2
"1
C2
"1
-1 0
long B -2
="
C2
="
-1 0L
2loat - <%%%?5- 0802
double B <%%%?5- 080d
char 2 Dnicode 0( Dnicode 2
1=
-1 EFu0000G null!

.ava Language
%&am)les:
boolean gasit=true;
int numar=34, suma;
float eroare=04!;
"#ar litera=$"%;
litera=$f%;
litera=litera&1;

.ava /omments
18 '' entire line
28 '( multiple
lines ('
"8 '(( use) by )o"umentation *ava)o" tool
multiple
lines('
>bs: /omments canot be nested into strings8
'( '(
(' (' +,- ,./
'(
''
''
(' ,.//

.ava /onstants
final type name [=value];
%&am)les:
a! final int 012=100;
b! final int 012;

012=100;
C
012=1!0; ''error

Array
Array 1ith one dimension
type[] name;
type name[];
Array allocation:
array_name=ne3 type[)im]; ''memory allo"ation
''in)ex 0 )im41
Accessing an array element: arrayHname[in)ex]
%&am)les:
float[] ve";
ve"=ne3 float[10];
int[] sir=ne3 int[3];
float tmp[];
tmp=ve"; ''ve" an) tmp refer to t#e same array

>ne dimension Array
Built-in length: returns the array dimension8
int[] sir=ne3 int[!];
int lung_sir=sirlengt#; ''lung=!;
sir[0]=sirlengt#;
sirlengt#=2; ''error

int[] y;
int lung_y=ylengt#; ''error, y 3as not "reate)
)ouble[] t=ne3 )ouble[0];
int lung_t=tlengt#; ''lung_t=0;
t[0]=23 ''error5 in)ex out of boun)s
t#e s#ort"ut syntax to "reate an) initiali6e an array5
int[] )x=741,0, 18;

3ectangular multidimensional array
;eclaration:
type name[][][][];
type[][][][] name;
/reation:
name=ne3 type[)im
1
][)im
2
][)im
n
];
Accessing an element:
name[in)ex
1
][in)ex
2
][in)ex
n
];
%&am)les:
int[][] a;
a=ne3 int[!][!];
a[0][0]=2;
int x=a[2][2]; ''x=9

Aon-3ectangular Multidimensional Array
%&am)les:
int[][] a=ne3 int[3][];
for:int i=0;i;3;i&&<
a[i]=ne3 int[i&1];
int x=alengt#; ''x=9
int y=a[2]lengt#; ''y=9
;eclaration4creation4initialiIation:
"#ar[][] lit=77$a%8,7$b%88;
int[][] b=771,28,
72,!,=8,
7188;
)ouble[][] mat=ne3 )ouble[][]7713, 0!8, 723, 4!88;

/har and +tring
"#ar[] sir=7$a%,%n%,%a%8; ''"omparison an) printing
''is )one "#ara"ter by "#ara"ter
sir[0]=$1%;
A constant +eJuence o2 /hars :
>1na are mere?; ''ob@e"t of type Atring
+tring class is immutable:
Atring s=>ab"?;
s=s&?123?; ''"on"atenating strings
Atring t=s;''t=>ab"123?
t&=>12?; ''t=9, s=9
+tring content can not be changed: t[0]=$1%;
"#ar "=t"#ar1t:0<;
met#o) lengt#:<5 int lun=slengt#:<;
teBuals:s<
'(Ceturns true if an) only if t#e argument is a Atring ob@e"t t#at
represents t#e same seBuen"e of "#ara"ters as t#is ob@e"t ('
"ompare-o:<5 int re6=t"ompare-o:s<
'(Dompares t3o strings lexi"ograp#i"ally Ceturns an integer in)i"ating
3#et#er t#is string is greater t#an :result is E 0<, eBual to :result is =
0<, or less t#an :result is ; 0< t#e argument('

>)erators
arihtmetic: &, 4, (, ', F
relational: E, E=, ;, ;=, /=, ==
increment7decrement: &&, 44
prefix5 int a=2;
int b=&&a; ''a=2, b=2
postfix5 int a=2;
int b=a&&; ''a=3, b=2
assignment: =, &=, 4=, (=, '=
conditional: GG, HH, /
bit3ise5 4 shi2t EE, ;;, EEE,
- conditional G, H, I :not<, J :ex"lusive or<
ternary o)erator: 95
ex5 logi"al_expr 9 expr_1 5 expr_2
Kf logi"al_expr is -CLM t#en expr1 else expr2

>)erator )recedence
>)erators Precedence higher )recedence on to)( the same )recedence on the same line!
18 Post2i& expr&& expr44
28 Dnari &&expr 44expr &expr 4expr I /
"8 ( ' F
-8 & 4
58 ;; EE EEE
=8 ; E ;= E= instan"eof
?8 == /=
B8 G
K8 J
108 H
118 GG
128 HH
1"8 9 5
1-8 = &= 4= (= '= F= G= J= H= ;;= EE= EEE=

+tatements
+eJuential /om)osition:
7
instr1;
instr2;
8
/onditional:
if :logi"a_expr<
instr;
if :logi"al_expr<
instr1;
else
instr2;
>bs: logi"al_expr is evaluated to true or false8 Aumerical values are
not allo1ed8

Loo) +tatements
,hile statement:
3#ile:logi"al_expr<
instr
do-1hile statement:
)o
instr
3#ile:logi"al_expr<;
>bs: Knstr is e&ecuted as long as logi"al_expr is true

Loo) +tatement
#>3 statement:
for:initiali6ation;termination; step<
instr
>bs: none o2 the initiali6ation, termination, step are mandatory
int suma=0L
for:int i=1;i;10;i&&<
suma&=i;
for:int i=0,suma=0;i;10;i&&<
suma&=i;
for:;;<
'' instru"tion

%nhanced #>3 %A/M! statement
+ynta&.+% NO5!:
for:-ype elem+ame 5 table+ame<
instr;
int[] x=71, 4, N, O, 108;
for:int el5x<
Aystemoutprintln:el<;
for:int i=0;i;xlengt#;i&&<
Aystemoutprintln:x[i]<;
>bs: $able elements cannot be modi2ied by using enhanced 2or statement
int[] x=71,4,N,108;
for:int el5x<7
Aystemoutprint:> ?&el<;
el&=2;
8
''1 4 N 10
for:int e5x<7
Aystemoutprint:> ?&e<; ''9
8

Return statement:
return;
return value;
Break statement: terminates the execution of a loop
int[] x= 7 2, =, 3, !, 12, =, N28;
int elem = =;
boolean gasit = false;
for :int i = 0; i ; xlengt#; i&&< 7
if :x[i] == elem< 7
gasit = true;
breaP;
8
8

/ontinue statement
- s*i)s the current iteration o2 a loo) statement
- sto)s the e&ecution o2 the loo) instructions and 2orces
the re-evaluation o2 the loo) termination condition
int[] x= 7 2, =, 3, !, 12, =, N28;
int elem = =;
int nr1par=0;
for :int i = 0; i ; xlengt#; i&&< 7
if :x[i] /= elem<
"ontinue;
nr1par&&;
8

+1itch statement
s3it"#:integral4sele"tor< 7
"ase integral4value1 5 statement; [breaP;]
"ase integral4value2 5 statement; [breaP;]
"ase integral4value3 5 statement; [breaP;]
"ase integral4value4 5 statement; [breaP;]
"ase integral4value! 5 statement; [breaP;]
''
)efault5 statement;
8

+1itch e&am)le
s3it"# :luna< 7
"ase 15
"ase 35
"ase !5
"ase Q5
"ase =5
"ase 105
"ase 125 nrRile = 31; breaP;
"ase 45
"ase N5
"ase O5
"ase 115 nrRile = 30; breaP;
"ase 25 if : anSise"t:an< <
nrRile = 2O;
else
nrRile = 2=;
breaP;
)efault5
Aystemoutprintln:>Tuna invali)aU<;
8

A sim)le .ava )rogram
''-est@ava
publi" "lass -est 7
publi" stati" voi) main:Atring[] args< 7
Aystemoutprintln:UVelloU<;
for:Atring el 5 args<
Aystemoutprintln:el<;
8
8
/om)ilation:
@ava" -est@ava
%&ecution:
@ava -est
@ava -est ana 1 2 3
/// Wou "an use int value=KntegerparseKnt:args[i]< in or)er to
transform a string value into an int value

>b9ect-oriented )rogramming /once)ts
Class: re)resents a ne1 data ty)e
/orres)onds to an im)lementation o2 an A;$8
Object: is an instance o2 a class8
$he ob9ects interact by messages8
Message: used by ob9ects to communicate8
A message is a method call8
Encapsulationhiding!
data state!
>)erations behaviour!
Inheritance: code reusing
Polyorphis P the ability o2 an entity to react di22erently de)ending on the
conte&t

.ava /lasses and >b9ects

/lass ;eclaration7;e2inition:
''Dlass+ame@ava
[publi"] [final] "lass Dlass+ame7
[)ata :fiel)s< )e"laration]
[met#o)s )e"laration an) implementation]
8
18 A class de2ined using publi" modi2ier it is saved into a 2ile 1ith the class
name Dlass+ame@ava
28 A 2ile @ava may contain multi)le class de2initions( but only one can be )ublic8
"8 .ava vs8 /44:

Ao 2 di22erent 2iles 8h( 8c))!8

Methods are im)lemented 1hen are declared8

A class declaration does not end 1ith ;



.ava /lasses and >b9ects

%&am)les:
''Xersoana@ava
publi" "lass Xersoana7
''
8
'' Domplex@ava
"lass Cational7
''
8
"lass +atural7
''
8
publi" "lass Domplex7
''
8

.ava /lasses and >b9ects

/lass Members #ields! declaration:


"lass Dlass+ame7
[a""ess_mo)ifier][stati"][final] -ype name[=init_val];
8
mo)ia""ess_mo)ifier "an be publi", prote"te), private8
18 /lass members can be declared any1here inside a class8
28 Access modi2ier must be given 2or each 2ield8
"8 <2 the access modi2ier is missing( the 2ield is visible inside the
)ac*age directory!8

.ava /lasses and >b9ects

%&am)les:
''Xersoana@ava
publi" "lass Xersoana7
private Atring nume;
private int varsta;
''
8
''Xun"t@ava
publi" "lass Xun"t7
private )ouble x;
private )ouble y;
''
8

.ava /lasses and >b9ects

<nitialiIing 2ields

At declaration-site:
private )ouble x=0;

in a s)ecial initialiIation bloc*:


publi" "lass Cational7
private int numarator;
private int numitor;
7
numarator=0;
numitor=1;
8
''
8

in constructor8
Any 2ield that is not e&)licitly initialiIed 1ill ta*e the de2ault value o2
its ty)e8

/onstructors

$he constructor body is e&ecuted a2ter the ob9ect memory s)ace is allocated in
order to initialiIe that s)ace8
[] "lass Dlass+ame7
[a""ess_mo)ifier] Dlass+ame:[list_formal_parameters]<7
''bo)y
8
8
a""es_mo)ifier 7publi", prote"te), private8
list_formal_parameters taPes t#e follo3ing form:
-ype1 name1[, -ype2 name2[,]]
18 $he constructor has the same name as the class name case sensitive!8
28 $he constructor does not have a return ty)e8
"8 #or a class 1ithout any declared constructor( the com)iler generates an
im)licit )ublic constructor 1ithout )arameters!8

>verloading /onstructors

A class can have many constructors( but they must have di22erent signatures8 8
''Domplex@ava
publi" "lass Domplex7
private )ouble real, imag;
publi" Domplex:<7 ''impli"it "onstru"tor
real=0;
imag=0;
8
publi" Domplex:)ouble real<7
t#isreal=real;
imag=0;
8
publi" Domplex:)ouble real, )ouble imag<7 ''
8
publi" Domplex:Domplex "<7 ''
8
8

this

<t re2ers to the current receiver! ob9ect8

<t is a reserved *ey1ord used to re2er the 2ields and the methods o2 a class8
''Domplex@ava
publi" "lass Domplex7
private )ouble real, imag;
''
publi" Domplex:)ouble real<7
t#isreal=real;
imag=0;
8
publi" Domplex:)ouble real, )ouble imag<7
t#isreal=real;
t#isimag=imag;
8
publi" Domplex suma:Domplex "<7
''
return t#is;
8
8

/alling another constructor

t#is can be used to call another constructor 2rom a given constructor8


''Domplex@ava
publi" "lass Domplex7
private )ouble real, imag;

publi" Domplex:<7
t#is:0,0<;
8

publi" Domplex:)ouble real<7
t#is:real,0<;
8
publi" Domplex:)ouble real, )ouble imag<7
t#isreal=real;
t#isimag=imag;
8
''
8

/alling another constructor
18 $he call o2 another constructor must be the 2irst instruction in the caller constructor8
28 $he callee constructor cannot be called t1ice8
"8 <t is not )ossible to call t1o di22erent constructors8
-8 A constructor cannot be called 2rom a method8
''Xun"t@ava
publi" "lass Xun"t7
private int x, y;
publi" Xun"t:<7
t#is:0,0<;
8
publi" Xun"t:int x, int y<7
t#isx=x;
t#isy=y;
8
publi" voi) muta:int )x, int )y<7
t#is:x&)x, y&)y<;
8
8
''Mrorrs9

/reating ob9ects

>)erator ne35
Xun"t p=ne3 Xun"t:<; ''t#e parent#eses are "ompulsory
Domplex "1=ne3 Domplex:<;
Domplex "2=ne3 Domplex:23<;
Domplex "3=ne3 Domplex:1,1!<;
Domplex ""; ''""=null, "" )oes not refer any ob@e"t
""="3; ''"3 si "" refer to t#e same ob@e"t in t#e memory
18 $he ob9ects are created into the hea) memory8
28 $he o)erator ne1 allocates the memory 2or an ob9ectL

;e2ining methods
[] "lass Dlass+ame7
[a""ess_mo)ifier] Cesult_-ype met#o)+ame:[list_formal_param]<7
''met#o) bo)y
8
8
a""ess_mo)ifier 7publi", prote"te), private8
list_formal_param taPes t#e form -ype1 name1[, -ype2 name2[, ]]
Cesult_-ype )oate can be any )rimitiv ty)e( re2erence ty)e( array( or voi)
18 <2 the accessHmodi2ier is missing( that method can be called by any class
de2ined in that )ac*age director!8
28 <2 the return ty)e is not voi),then each e&ecution branch o2 that method must
end 1ith the statement return

;e2ining methods
''Xersoana@ava
publi" "lass Xersoana7
private byte varsta;
private Atring nume;
publi" Xersoana:<7
t#is:>?,0<;
8
publi" Xersoana:Atring nume, byte varsta<7
t#isnume=nume;
t#isvarsta=varsta;
8
publi" byte getYarsta:<7
return varsta;
8
publi" voi) set+ume:Atring nume<7
t#isnume=nume;
8
publi" boolean mai-anara:Xersoana p<7''
8
8

>verloading methods

A class may contain multi)le methods 1ith the same name but 1ith di22erent signature8
A signature O return ty)e and the list o2 the 2ormal )arameters
publi" "lass Domplex7
private )ouble real, imag;
'' "onstru"tors
publi" voi) a)una :)ouble real<7
t#isreal&=real;
8
publi" voi) a)una:Domplex "<7
t#isreal&="real;
t#isimag&="imag;
8
publi" Domplex a)una:Domplex ""<7
t#isreal&=""real;
t#isimag&=""imag;
return t#is;
8
8
''Mrorrs9

.ava does not allo1 the o)erators overloading8

/lass +tring has overloaded o)erators & and &=8


Atring s=Uab"U;
Atring t=UMZ[U;
Atring r=s&t;
s&=23;
s&=\ \;
s&=4!;
''s=>ab"23 4!?;
''r=>ab"MZ[?

;estructor: <n .ava there is no any destructor8

$he garbage collector deallocates the memory 8



>b9ects as Parameters

>b9ects can be 2ormal )arameters 2or the methods

A method can return an ob9ect or an array o2 ob9ects8


publi" "lass Cational7
private int numarator, numitor;
''Donstru"tors
publi" voi) a)una:Cational r<7
''
8
publi" Cational s"a)ere:Cational r<7
''
8
8

Passing arguments

Primitive ty)e arguments boolean( int( byte( long( double! are )assed by value8 $heir
values are co)ied on the stac*8

Arguments o2 re2erence ty)e are )assed by value8 A re2erence to them is co)ied on the
stac*( but their content 2ields 2or ob9ects( locations 2or array! can be modi2ied i2 the
method has the rights to accces them8
18 $here is not any 1ay to change the )assing mode li*e Q in /44!8
"lass Xarametrii7
stati" voi) inters"#imba:int x, int y<7
int tmp=x;
x=y;
y=tmp;
8
publi" stati" voi) main:Atring[] args< 7
int x=2, y=4;
inters"#imba:x,y<;
Aystemoutprintln:Ux=U&x&U y=U&y<; ''9
8
8

Passing arguments
"lass S7
int val;
publi" S:int x<7
t#isval=x;
8
publi" Atring toAtring:<7
return UU&val;
8
stati" voi) inters"#imba:S x, S y<7
S tmp=x;
x=y;
y=tmp;
Aystemoutprintln:U[Knters"#imba S] x=U&x&U y=U&y<;
8
publi" stati" voi) main:Atring[] args< 7
S bx=ne3 S:2<;
S by=ne3 S:4<;
Aystemoutprintln:Ubx=U&bx&U by=U&by<;
inters"#imba:bx,by<;
Aystemoutprintln:Ubx=U&bx&U by=U&by<; ''9
8
8

Passing arguments
"lass S7
int val;
publi" S:int x<7
t#isval=x;
8
publi" Atring toAtring:<7
return UU&val;
8
stati" voi) inters"#imba]ata:S x, S y<7
int tmp=xval;
xval=yval;
yval=tmp;
Aystemoutprintln:U[Knters"#imba]ata] x=U&x&U y=U&y<;
8
publi" stati" voi) main:Atring[] args< 7
S bx=ne3 S:2<;
S by=ne3 S:4<;
Aystemoutprintln:Ubx=U&bx&U by=U&by<;
inters"#imba]ata:bx,by<;
Aystemoutprintln:Ubx=U&bx&U by=U&by<; ''9
8
8

Array o2 ob9ects

%ach array element must be allocated and intialiIed8


publi" "lass -ablouri,bie"te 7
stati" voi) generea6a:int nrMlem, Domplex[] t<7
t=ne3 Domplex[nrMlem];
for:int i=0;i;nrMlem;i&&<
t[i]=ne3 Domplex:i,i<;
8
stati" Domplex[] generea6a:int nrMlem<7
Domplex[] t=ne3 Domplex[nrMlem];
for:int i=0;i;nrMlem;i&&<
t[i]=ne3 Domplex:i,i<;
return t;
8
stati" voi) mo)ifi"a:Domplex[] t<7
for:int i=0;i;tlengt#;i&&<
t[i]suma:t[i]<;
8
''

Array o2 ob9ects
stati" Domplex suma:Domplex[] t<7
Domplex suma=ne3 Domplex:0,0<;
for:int i=0; i;tlengt#;i&&<
sumaa)una:t[i]<;
return suma;
8
publi" stati" voi) main:Atring[] args< 7
Domplex[] t=generea6a:3<;
Domplex "s=suma:t<;
Aystemoutprintln:Usuma U&"s<;
Domplex[] t1=null;
generea6a:3,t1<;
Domplex "s1=suma:t1<;
Aystemoutprintln:Usuma U&"s1<;
mo)ifi"a:t<;
Aystemoutprintln:Usuma )upa mo)ifi"are U&suma:t<<;
8
8

$he methods to+tring and eJuals
publi" "lass Domplex7
private )ouble real, imag;
publi" Domplex:)ouble re, )ouble im<7
''
8
publi" Atring toAtring:<7
if :imagE=0<
return U:U&real&U&U&imag&Ui<U;
else
return U:U&real&imag&Ui<U;
8
publi" boolean eBuals:,b@e"t ob@<7
if :ob@ instan"eof Domplex<7
Domplex "=:Domplex<ob@;
return :real=="real< GG :imag=="imag<;
8
return false;
8
''
8

+tatic methods

Are declared using the *ey1ord stati"

-#ey are s#are) by all "lass instan"es


publi" "lass Domplex7
private )ouble real, imag;
publi" Domplex:)ouble re, )ouble im<7
''
8
publi" stati" Domplex suma:Domplex a, Domplex b<7
return ne3 Domplex:areal&breal, aimag&bimag<;
8
''
8

+tatic methods

$hey are called using the class name:


Domplex a,b;
'' initiali6ation a an) b
Domplex "=Domplexa)una:a, b<;
18 A static method cannot use those 2ields or call those methods! 1hich are not
static8 <t can use or call only the static members8
28 $his does not ma*e sense inside the body o2 a static method

+tatic 2ields
publi" "lass +atural7
private long val;
publi" stati" long 012=232 ''2JN341
''
8
publi" "lass Xro)us 7
private stati" long "ounter;
private final long i)="ounter&&;
publi" Atring toAtring:<7
return UU&i);
8
''
+tatic 2ields are shared by all class instances8 $hey are allocated only once in
the memory8

+tatic 2ields

<nitialiIation:

At declaration site:
publi" stati" long 012=2000;

<n a s)ecial intialiIation bloc*


publi" "lass +atural 7
publi" stati" long 012;
stati" 7
012=2000;
8
<2 a static 2ield is not intialiIed( it 1ill ta*e the de2ault value o2 its ty)e:
private stati" long "ounter; ''0

Вам также может понравиться