Lectures 1 6

181
ƩLjƾNJİǍdž 1 ƪLjıαDŽǔDŽǀ ƪπLjıljǗπdžıdž ƾıljdžıdžǐ ǻȚάȜεȟη 1 Τȝήȝα ΠȜηȡȠφȠȡȚțήȢ țαȚ ΤηȜεπȚțȠȚȞωȞȚȫȞ ΑȞάπIJυȟη ΛȠγȚıȝȚțȠȪ ΧεȚȝεȡȚȞȩ ǼȟάȝηȞȠ 2013-2014 1

description

 

Transcript of Lectures 1 6

Page 1: Lectures 1 6

1

α

π π

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

1

Page 2: Lectures 1 6

α

α χ α α π α α αφ υα

π π α. α π α α α α α

υα α φα α υ, α α α υ .

φα α υ π αφ π α α α α α φ α

π α α α α π ( χ π.χ. J2εE, Android)

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 3: Lectures 1 6

α π υ π

α π α α υ α α α α υ π α π υπ α cloud υπ . χ υ υ α α χ π α α υπ α

π φ . υ α α απ α απ 3 π υ υπ υ α α:

απ υ , πα υ α α αχ π φ α αφ α αφ π χ (Points of Interest - POI).

πα υ α α α υπ .

πα υ α α α υ υ Android.

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 4: Lectures 1 6

χ υ α

ά ε η 1

UE1

UE2

Web server

Data storage

L1 D2D1

AP2

R1

R3

AP3

R2

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 5: Lectures 1 6

χ

χ π υ α α π α α υ φ α α

Java SDK

Web Services

Android SDK

απα α

IP Addressing

Linux

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 6: Lectures 1 6

α α – α (1/3) πα α π α υ π απ α 3

α ( ). πα α α α

20-01-2014, 10-02-2014, 10-03-2014

α π πα , α α υ α α α α π υ α χ α ο α υ οπο α α

α υ αφ α απ α α α . Θα π π α α π π α α υ π α α

π α - α π π ω ω α α αφ α υ π απο υχ α

Η α ο ο α α π ο π ο απ ου α φα ο ο α απ

χ α .

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 7: Lectures 1 6

α α – α (2/3)

π πα α α α α α α α α υ α υ

υ π υ α υ πα α . Γ α π υ απ α υ α

π α α α αφ . π υ απ α α α α π α α

α .

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 8: Lectures 1 6

α α – α (3/3)

2 α α: 11:00-14:00 – 1

17:00-20:00 – φ α

αφ α α α α eclass

Η πα υ α α α α απα α ( υ χ 1 απ α) 3 απ υ = απ υχ α α

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 9: Lectures 1 6

α α

Η α α α : 0.3*1 πα α + 0.2*2 πα α + 0.35*3

πα α + 0.15* υ υ π υχ α π α (α π α, α αφ

π) α αυ α α α απ υχ α α. πα α αυ χ απ

υ α φ υ α π . Θα απ , α

π . υ χ αυ α χ υ π χ υ .

Η απ α α α pass/fail. π υχ α α υ υ α α

α υ α α .

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 10: Lectures 1 6

α α α (1/2) 17/12 π ωΐ:

Introduction, assignment presentation etc

Java Programming – Basic classes

17/12 απ υ α: Java Programming – Inheritance - Threads

07/01: Web Services, SOAP Web Services ( ), J2SE Web Services, πα α

14/01 π ωΐ: JDBC, Swing, π α α πα α

14/01 απ υ α: JDBC, Swing, π α α πα α

Brainstorming & Questions on Exercise 1

20/01: Exercise 1 delivery

21/01: Examination of 1st Exercise

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 11: Lectures 1 6

α α α (2/2) 28/01: Android

04/02: Brainstorming & Questions on Exercise 2

10/02: Exercise 2 Delivery

11/02: Examination of 2nd Exercise

18/02: Android

25/02: Android

04/03: Brainstorming & Questions on Exercise 3

10/03: Exercise 3 Delivery

11/03: Examination of 3rd Exercise

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 12: Lectures 1 6

π α

e-class

mailing list υ α α

info-k23b[at]di.uoa.gr

αφ MONO @di.uoa.gr email α

υ : α π υ ([email protected])

α Χα ([email protected])

α α α ([email protected])

Γ π ([email protected])

ά ε η 1 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 13: Lectures 1 6

2

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α

α α υ π

ά ε η 2 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

1

Page 14: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

2

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 15: Lectures 1 6

Η α Java (1/2)

Η Java απ α υ π π υ α π α α .

απ π υ α α υ : α φ , α α , α υ ,

... α π α α Java α .java

α α.

α α α α α υ α π α α java

α : α .java α α α α .class απ java compiler α π υ bytecodes.

: Java Virtual Machine (JVM) α α bytecodes α π α α α .

3

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 16: Lectures 1 6

Η α Java (2/2)

Η Java VM:

α α απ υ α π υ π . π π .class α α π α

π π π . Η Java α, π α απ Java VM, α πα α α

υ (Java Application Programming Interface (Java API)) α π υ πα

υ α α φα α .

MyClass.java

Java API

Java Virtual Machine

Operating System

Java Platform

4

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 17: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

5

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 18: Lectures 1 6

α α α φ π α α

α α π υ α α α υ α υ

α π α α π α α α υ υ .

α α α φ π α α : ο ο α (inheritance): α α α

π α α α α υ υ α υ. Ό α υ υ

ο υ ο φ (polymorphism): Η α α υ π α αφ π απ π υ

. υ ά ω (encapsulation): Η απ υ α α

α υ α π π α υ απ α παφ .

6

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 19: Lectures 1 6

αφ π α α Java - α

Η α : public MyClass{} α public απ α α α π υ φ α ,

α MyClass.java α π α α private/protected α α π α

α public (inner class).

α /π α: π α α α public: π α απ υ . protected: π α απ α υπ . package-private: π α απ υ υ πα υ. private: π α απ α . static: α α α α α α . final: α π α α φ α α υ .

: υ α α .

: π α α υ α α απ α υπ . MyClass myObject = new MyClass(); new α υ α α myObject.

7

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 20: Lectures 1 6

Η Η α α υ φ

<Access Modifier> class <Class Name> {

// field, constructor, and

// method declarations

}

α α π α α α υ υ α υ α .

Constructors: α α α υ α α υ. Η υ αυ π υ υ α α α

α π φ υ α υ . π α υ α α, α α π υ constructors α α ,

π υ α αφ υ α πα α . υ α α, Java α πα default no-argument

constructor.

α α α α α π α α . Η υ α υ φ :

<Access Modifier> <field type> <field name>

α Method overloading

α υ φ

<Access Modifier> <return type> <method name> (<argument list>)

<exception list>{

//body

}

α α υ static π υ numberOfBicycles? α this?

8

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 21: Lectures 1 6

Η Java VM φα απ main

. Η Bicycle π α υ main. Γ

αυ απ α π φα , α απ π α .

Γ α υ α α υ α α α: ω α α π υ ...Bicycle

ε ε ω α α αυ ( υ new)

Α χ οπο ή ω α αυ ( constructor )

π α π α υ

π α π π α απ υ α α public π α α υ α :

objectName.fieldName

π α π π α α private π α α υ α α :

objectName.fieldGetter() / objectName.fieldSetter()

α υ.

Η Java α π π α υ α α α υ α φα α , α π π α

απ υ . Java runtime environment απ α α α α π α π . υ α α α garbage collection.

υπ π α α α ? υ + System.out.println()?

9

α α αυ , Ω π α α Φ α

απ υ π υ π υ υ .

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 22: Lectures 1 6

public static void main(String[] args)

Η main απα α α π α α α . public: υ π α αυ

static: α α α α .

void: π φ .

String[] args: α α α α π α α απ υ . α α α α απ α .

φα πα υ α υ Integer.parseInt()

Double.parseDouble()

Γ α π α java.lang πα υ Java API

10

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 23: Lectures 1 6

static

Ω static π α υ π α α υ α

static π α α π α π υ α υ α α α α .

α α π α α π α α φ α α α π υ π α α .

π α α π υ α α α α α.

Η π π α static π υ α υ α α π υ α υ: <class name>.<static field name>.

static α π υ α υ α α α α .

π α π π υ static π α α α π π .

π α α υ static υ .

Η α static υ α υ α α π υ α υ: <class name>.<static method name>.

π α α αφ α this & super keywords.

H main α static α α π α α π απ π α α υ α π υ α υ.

11

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 24: Lectures 1 6

this

Η υ this α αφ α α α this.<field name>

π π π υ α π α απ π α απ α α πα α υ ( , constructor)

this.<constructor>

α constructor π α α υ α constructor (explicit constructor invocation).

Γ α α α

α π υ α α α α υ obj.itIsMe(this);

Γ α α π υ α α υ MyClass getInstance() {return this;}

Γ α α π π υ α υ α

Class className = this.getClass();

12

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 25: Lectures 1 6

π α α

Η Java υπ 8 primitive data types byte: 8-bit α α

short: 16-bit α α

int: 32-bit α α

long: 64-bit α α

float: 32-bit α

double: 64-bit α

char: 16-bit α α α (υπ υ Unicode)

boolean: (true/false)

Η Java πα α υ α α

=

+, -, *. /, %

α α ++, --, !

α α υ == , != , >, >=, <,<=

α υ &&, ||

πoυ objectName instanceof MyClass

υ α α object α υ υ π υ ( π φ

true/false)

13

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 26: Lectures 1 6

υ

Η Java α πα α υ: If-then-else, switch, for, while, break, continue, return;

if (condition) { … } / if (condition) { … } else { … } !! condition α π υ boolean true false. αυ π υ

υ απ C, α πα α α if α απ α α α , α α α α α 0 α υ

switch (expression){ case value1: …; break; …. default: …; }

expression π α α α α , String enumerated type

while(condition){}

υ α υ infinite loop while(true){…} α χ while(1){…} πως χ ε ην C

for(initialization; termination; increment){} π π π υ α υ arrays Collections, α υ α α υ for loop

α α υ

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

for (int item : numbers) {

System.out.println("Count is: " + item);

}

14

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 27: Lectures 1 6

α (1/2) π α α α α α π υ α α

απ α α υ υ π υ Η α υ π α π α απ 0. Η π α α α υ υ υ

α π υ α φ α [] int[] myArray

π α α α π α α α υ new

int[] myArray = new int[10]

α α int[] myArray= {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}

π α α π α α . π π π υ π α π α α

α π απ υ primitive types Java, α π π α υ π α α α υ

MyObject[] myArray = new MyObject[10];

myArray[0] = new MyObject();

15

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 28: Lectures 1 6

α (2/2) π υ α π α Java αφ υ απ υ α υ

C.

2- α π α α απ α π α π υ φ υ α α π α . String[][] names = {{Οεr.Π, Οεrs.Π, Οεs.Π}, {ΟSmithΠ, ΟJonesΠ}}; names[0][0] = Οεr.Π;

α π α Java α . Η πα α α φ π α α names π α α newNames.

String[][] newNames = names;

α names, α newNames υ (references).

Η υ arraycopy System α π π α α υ α π α π α α α .

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

! Η αυ deep-copy α υ α α α α α α π α υ π υ υ π α α α

υ α Collections.

α π α α α απ built-in property length.

myArray.length, names[0].length

16

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 29: Lectures 1 6

α α υπ π α α α ? α System.gc()?

17

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 30: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

18

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 31: Lectures 1 6

Number H Java Number α α υπ

α α π α primitive type.

αυ α α υ wrap α primitive data value α α .

π π π υ π α α π Number object α α α primitive:

α α υ π υ π υ π object

Γ α α π υ maximum α minimum values α data types π Integer.MAX_VALUE, Integer.MIN_VALUE.

Γ α α π υ απ α π string ( υ υ atoi()!!) α υ π υ

α π : java.lang.Math

19

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 32: Lectures 1 6

String H Java α πα String α α α

υ α α α. υ α String α

String greeting = ΟHello world!Π; String greeting = new String(ΟHello world!Π); .. α α 11 constructor α α υ α α π υ String

Η String α immutable, αυ α π α α string α υ π α α .

Χ υ α ... String palindrome = "Dot saw I was Tod"; int len = palindrome.length();

String name = "εy name is Ο.concat("Rumplestiltskin");

υ string π υ α υ α + ( υ υ α πα α α α System.out.println)

char character = ΟhelloΠ.charAt(0);

ΟhelloΠ.equals(Οhello Π); α π α Number α String

String to Number <NumberSubclass>.parseXXXX π Float.parsefFoat(myString)

Number to String String str = 1+ΠΠ; String str = String.valueOf(1); String str = Integer.toString(1)

20

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 33: Lectures 1 6

Java.lang.System

α π υ α πα Java API α α System.

α α απ static υ α π α. Χ α α π υ αφ α

υ π α in:"standard" input stream

out:"standard" output stream

err :"standard" error output stream.

currentTimeMillis(): π φ α υ π α π υ π α α απ 1/1/1970. gc(): α π garbage collector.

exit(int code): α υ π α α π φ α α α run time π .

arraycopy(Object src, int srcPos, Object dest, int destPos, int length): α αφ α π .

21

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 34: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

22

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 35: Lectures 1 6

α

Java α π α α α π α π α α υ α .

SuperClass/ParentClass/BaseClass

SubClass/ChildClass/ExtendedClass

H Object (java.lang.Object) α super class .

α π α Α . π υ υ π α π α ,

α υπ Object.

π α α α π υπ υ α α π υ π π α α α α α

α α υ α. extends: SubClass extends SuperClass

extends

23

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 36: Lectures 1 6

α α

24

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 37: Lectures 1 6

Χα α α

α υπ α α α υπ (π α, υ , φ υ ), α direct access α public α protected .

constructors α α α π α , π α απ υπ .

α α π α π α π α π π π π .

π α υ π π π α α υπ π υ α υπ .

π α υ π α υπ α αυ υπ α α α απ υ (hiding) .

π α απ υπ .

π α υ υ α υπ π υ φα α υπ .

π α υ υ υπ α υπ αφ αυ υπ (method overriding). π α α υ αυ υπ

super.

π α υ α static υπ α υπ αφ α static υπ α α απ υ (hiding).

constructor α υπ π α α constructor υπ α super.

25

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 38: Lectures 1 6

α α

υπ π α α α ? α υ α αφ

π α α α ?

26

Method hiding

Method overriding

Η Η The class method in Animal. The instance method in Cat. The instance method in Dog.

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 39: Lectures 1 6

super

π υ π π π υ α α υ α π υ υ override, α constructor.

Χ super α Class ParentClass {

void methodA() {…}

}

Class ChildClass extends ParentCLass {

void methodA(){

super.methodA();

….}

}

Χ super α constructors Η super(<argument list>); α π α π . π α α απ π α α α super.

To run time π α α non-argument constructor (default constructor).

π α α υ constructors απ πα α φ υ υ Object constructor (constructor chaining). α α

α α α απ Object π α .

27

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 40: Lectures 1 6

Type casting

α α πα υ π υ . Ω type casting υ α π α υ απ

α α π α .

Type casting reference υ υ π υ α υ.

Upcasting: α υ α α α Child α reference π υ Parent, π υ Child extends Parent.

π π αυ , π α α υ π π υ α π α π υ α Child α Parent. π

α α υ overridden methods Child!!

Downcasting: α υ α α Parent α reference π υ Child. αυ π π α α υπ compile-time error α α α α. Η

α Parent obj = new Child(); Parent obj = new Child();

Child child = (Child) obj; if(obj instanceof Child)

Child child = (Child) obj

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 41: Lectures 1 6

Object

α π υ α α Java. α υφ α α .

Η αυ υ π φ π υ α α α α π π α α υ .

απ α υ Object π υ α απ π α override α α π αυ α:

equals(): π φ true false α α α υ α α α α. α α υ α ( ).

α override αυ π α υ π υ α α α α α α υ α α α.

getClass(): α α runtime representation π υ α α α α α α υ π φ α αυ ,

π . myObject.getClass().getName()

toString(): α πα α string α α απα α object. υ υ override α α πα υ υ α π α

π α α π .

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 42: Lectures 1 6

Constructors

α α private π α

getters α

setters

toString() α α υπ υ

π φ α υ

equals() α α υ υ υ

α α

Is std1 equal with std2: false Student id: 123 Student name: Maria Student surname: Papadopoulou Student id: 456 Student name: Dimitris Student surname: Nikolaou

Page 43: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α

α α υ π

31

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 2

Page 44: Lectures 1 6

παφ (Interfaces)

π υ π υ υ α υ φ υ α α α α π α π α α α α

υ υ π ! α interfaces α πα υ αυ υ α α. Java α interface απ α reference type, πα α

, α π α (static & final α ) α . υ π α Α Α .

π α υ υ α α απ interfaces.

Έ α interface π α implement απ α extend απ α interface.

α π α implement πα απ απ α interfaces ( α α, π υ π α extend α

).

Ό α υ implement α interface, π π α υ α α υ π υ α .

: interface, implements

ά ε η 2 32

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 45: Lectures 1 6

υ

ά ε η 2

implements

extends

33

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 46: Lectures 1 6

φ (Abstract)

π α π abstract υ (π υ υ υ π ).

public abstract class MyAbstractClass {

// declare fields

// declare non-abstract methods

abstract void abstractMethod();

}

α π α extend α α υ π abstract υ .

α interfaces, abstract classes π υ π α non static α final, α πα υ υ π α π απ υ

υ .

π υ υπ υ α α α αφ υ π α α π υ extend υ α α υ π υ .

α abstract π υ π abstract υ α α α interface.

ά ε η 2 34

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 47: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

35

ά ε η 2 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 48: Lectures 1 6

α α (Packages) package υ α π υ αφ α

interfaces α α . Java API πα α α π πα α (java.lang,

java.util...)

α α απ πα

υ φ α α π . Modularity..

υπ υ Δ υ Ε α α , α package υ namespace.

π π υ α α π α α package α π υ π α απ αυ .

:package

α α α : α package α α π υ α α package α φ υ

α υ Internet domain υ π. package gr.uoa.di

υ α α αφ α α π υ πα υ φ υ import gr.uoa.di.*

ά ε η 2 36

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 49: Lectures 1 6

α (Scope) Η Java α πα α απ access modifiers α α

υ α α π υ α α α α π / α α .

Η α πα π α α . α

α π α public α α υπ π α απ , α α απ πα π υ α .

υπ υ modifier, default (package-private) π α α α πα .

α π (Φ υ α αυ α) private: υ π α π αυ .

package-private: α α π υ α υ πα υ π α π αυ .

protected:π α αυ υ α , π υ α υ πα α π υ (α πα υ)

public: , α α πα υ, υ π α π αυ .

ά ε η 2 37

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 50: Lectures 1 6

α Java

αφ π α α α υ α α

Χ

α

παφ , φ

α α α α α α υ π

38

ά ε η 2 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 51: Lectures 1 6

υ αφ , α α π α

ά ε η 2

πα α α: Java Development Kit (JDK):

http://www.oracle.com/technetwork/java/javase/downloads Έ α φ α IDE:

Notepad, Textpad, WordPad Eclipse, Netbeans

α JAVA_HOME: C:\jdk1.7.0_21\bin PATH: C:\jdk1.7.0_21\bin CLASSPATH: C:\jdk1.7.0_21\jre\lib

α π α υ CLASSPATH π α α .

α α απ α javac Νcp path_to_libraries;path_to_classes Νsourcepath path_to_sources java Νcp path_to_libraries;path_to_classes MainClass arg1 arg2 … argn π Linux α α α α : α

α ; Windows.

39

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 52: Lectures 1 6

α α

α javac.exe src\gr\uoa\PrinterClass.java -d classes javac.exe -cp classes src\gr\uoa\di\FirstClass.java -d classes

java -cp classes/ gr.uoa.di.FirstClass "Hello World"

π α Hello World

40

ά ε η 2 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 53: Lectures 1 6

Αναφο έ

Javadoc http://download.oracle.com/javase/7/docs/api/

Oracle tutorial http://docs.oracle.com/javase/tutorial/index.html

ά ε η 2 41

Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 54: Lectures 1 6

3

ά ε η 3 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

1

Ν α α (Threads) η Java

Συγχ ό

ω γα

Collections & Generics

αχ η φα ω η Java

Page 55: Lectures 1 6

α α (Threads) Java υ α

Collections & Generics

α φα Java

2

ά ε η 3 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 56: Lectures 1 6

α α α α υπ π α φα Δα α α Ε α

απ υ υ α α π α α CPU (concurrent programming).

υ υ α υ , α (processes) α α α α (threads).

α α απ αυ φα π υ α υ υ memory space.

υ α υπ πα π α π α α α (Inter Process Communication- PC resources) α

αφ υπ (π pipes, socketsΖ).

α α π α α α α lightweight processes. α υ α αυ α

π υ υ α thread απα υ π υ α υ απ υ α.

α α α α α α α α. υ α, α α α π α υ α π α α α α π tasks Δ αυ αΕ.

α α α α π υ α α π υ α υ π , α α π α α υ α υ address space.

α α π π α CPU α threads α α (context switching) α .

ά ε η 3

3

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 57: Lectures 1 6

α α α Java (1/2) H Java α πα υ π υ α υ α

α υ extend Thread (java.lang.Thread)

α υ implement Runnable interface (java.lang.Runnable)

Extends Thread α π α extend Thread α α override run()

π α α α α thread. ( α υ π Thread implement Runnable Interface)

O constructor αυ π α α constructor Thread super().

α start() π υ α Thread α απ α α π α υ α thread.

ά ε η 3

4

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 58: Lectures 1 6

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

α α α Java (2/2) Implements Runnable

α π α implement interface Runnable α α αφ α α run() π υ α thread.

Α , α α π υ Thread υ α α υ π α α constructor υ α α απ π υ υ α α

implement Runnable.

start() υ α υ π υ Thread α α α π φ thread

ά ε η 3

5

υ α α threads π υ υ implement Runnable interface α α

Thread Class: 1. Α oυ extend Thread,

π α υ extend α α

2. To Runnable interface α υπα α υ implement α

( run) α απ φ υ α υ extra overhead

Thread.

Page 59: Lectures 1 6

thread

ά ε η 3

6

Start: α α α π α υ α α π υ Thread α π α υ start()

Runnable (Ready-to-run): α π υ α α start() α απ thread. αυ α α π π α CPU. Α , αυ α α π α απ υ απ non-runnable α running

Running: αυ α α thread α . scheduler π α απ runnable pool.

Dead: thread αυ α α πα α α α join απ α α π υ .

Non-runnable state:

• K static: void sleep(long millisecond) throws InterruptedException

• notification απ thread: final void wait() throws InterruptedException

• To thread π α I/O π υ

• α υ thread α α α (joint completion)

• α lock π π . Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 60: Lectures 1 6

ά ε η 4

Static π υ υπ α υ thread π υ α α υ α π υ

α πα

run υ thread

H main απ α α απ α thread α π α α threadMessage

π π π υ thread interrupt απ π thread

To α t π υ Thread α πα υ α α thread π υ

α

Page 61: Lectures 1 6

α α (Threads) Java υ α

Collections & Generics

α φα Java

8

ά ε η 3 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 62: Lectures 1 6

υ α α α υ π α π π υ α α απ

threads α α α π α αυ α υ α.

Java α πα α α α π υ υ π α π υ.

π π π α υ α υ π α , α α απ φυ α α

π deadlocks α starvation.

ά ε η 3

9

start()

thread1 thread2

Shared object

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 63: Lectures 1 6

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

υ α

ά ε η 3

υ α α α π α t

υ υ α α π υ π α α α απ

υ υ.

υ synchronized υ . φ α α α π α π α π

synchronized υ α υ, π α π α π α

α υπ π synchronized υ . υ α υ synchronized α α πα

intrinsic lock π υ α α

π α π υ π α π απ αυ α α α π φα α starvation, α α

απα υ π α α α π α α α synchronized υ. α 2 α α α BLOCKED

Page 64: Lectures 1 6

υ α block

ά ε η 3

11

π π synchronized statements π π α υ π π α π α υ lock .

Fine-grained υ . π α α α π α π υ

υ α α α υ

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 65: Lectures 1 6

υ α

ά ε η 4

Τ α Π η φ α Τη επ ω ω

Α άπ υ η Λ γ

Χε ε ά η 2ί12-2013

12

α απ α υ αφ π

π α α π απ αυ π π α υ υ

Page 66: Lectures 1 6

Α (Atomic execution)

ά ε η 3 13

Α π (Atomic action) υ π π υ α επ υχώ α ε α.

π α α α . α ο ό α ό ου.

Α α α απ π π α α α υ απ υ α π υ ( α α α α α ). α α: double d; d++;

Retrieve the current value of d.

Increment the retrieved value by 1.

Store the incremented value back in d.

α α αυ πα α π α α : Γ α primitive variables ( long, double) α references, π read/write α

α .

Γ α α π υ α volatile (π ) ( υ π α α long, double).

α α π υ υ volatile variables α α α α α.

α volatile α α φα υ υ !!!

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 67: Lectures 1 6

14

ά ε η 3

α α (Threads) Java υ α

Collections & Generics

α φα Java

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 68: Lectures 1 6

α Java π α πα φα α α α

απ π . Ό , υπ υ π π π υ υ α υ α

φα π υ α α υ α π υ α .

α π υ α υ α α υ α α α α α φα φ α π υ α platform dependent

. Runtime απ α παφ π π υ

φα α getRuntime(): α π φ α α π υ Runtime α α α φα

exec(String[] cmdAndΑrgs): υ α α α π υ πα α υ π υ π α α

Process p = Runtime.getRuntime().exec("/bin/ls");

!! java α α α υ α stdin, stdout, stderr. Γ α α α π υ α α π π α π υ

υ getInputStream(), getOutputStream() α getErrorStream() π υ α πα α π υ Process.

ά ε η 3

15

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 69: Lectures 1 6

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

ά ε η 3

16

α α ls π υ α

α /bin

getInputStream α πα π α output stream

α α πα . α α αυ απ input stream α main

υ buffered input stream α α υ α - α α α

υ π α !!

Page 70: Lectures 1 6

17

ά ε η 3

α α (Threads) Java υ α

Collections & Generics

α φα Java

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 71: Lectures 1 6

Collections

Ω Collections α α α π υ α π α α α α α. α Collections α πα υ π π α framework α α απ υ α α α α α α αυ .

Έ α collection framework π α : Interfaces: παφ α α /π π α υ π

collections π υ α υ αφ π υ π

Κ ά ε : π α υ υ π Interfaces. υ α φα α .

Α γο ί ου : α π α α , α α υ π α αφ π αφ . α αυ α π υ φ α α α αφ α α collection interface.

απ α α α α π α α απ collection framework: Έ α α απ φα α

Data structures π υ α α υ α π π α

α API α , α απ π υ π

ά ε η 3 18

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 72: Lectures 1 6

Collection interfaces

αφ collection interfaces υ α α α α α υ αφ π υ απ υ α α .

Collection: α α α α collections α υ π α απ π α . Χ π α υ α π α collections π .

Set: α υ π α π α υπ υ π υπα α α. List: υ α υ απ α α π υ φα υ α . π α

π π υ α α π υ index.

Queue: collection π υ φα υ . Map: collection π υ α π α α . SortedSet: α Set, π υ α α α α

. SortedMap: α Map, α π α α

α α.

ά ε η 3 19

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 73: Lectures 1 6

Collection implementation υ π Collection interfaces π α αφ

π υ . υπ υ α υ α πα α : General-purpose implementations αυ α α υ π υ

π α collections.

Special-purpose implementations α π φα α α α αυ π υ πα υ υ π φ απ υ π φ .

Concurrent implementations υ π π υ πα α α φα αυ π π α α αυ α α απ π υ α π α (java.util.concurrent package).

Wrapper implementations π α υ υα general-purpose υ π α α πα υ π π υ α.

Convenience implementations collections π υ α α α static υ π φ (π singleton sets).

Abstract implementations πα υ abstract α custom υ π απ .

υ α general-purpose implementations α υ α

Set HashSet, TreeSet, LinkedHashSet

List ArrayList, LinkedList

Map HashMap, TreeMap, LinkedHashMap

ά ε η 3 20

α απ πα υ π π υ υ υ φ

interface. Δε α thread-safe. α υ iterators α π π α

. α Serializable α υπ υ clone().

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 74: Lectures 1 6

π α Collection

π υ υ π α α π π α π collection α , α α υ π α α α π υ απ , α α απ π υ π : υ for-each

Iterator.

υ for-each α π π α π π υ υ π α collection ( π α α array).

for (Object o : collection) System.out.println(o);

Iterator α α interface π υ α υ α α π π υ α collection α α α α υ α υ. Collection υ π iterator() α π φ α α

π υ Iterator.

ά ε η 3

public interface Iterator<E> { boolean hasNext(); E next(); void remove(); //optional }

static void filter(Collection<?> c) { for (Iterator<?> it = c.iterator(); it.hasNext(); ) if (!cond(it.next())) it.remove(); }

21

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 75: Lectures 1 6

Ά α α α α ... Bulk operations

containsAll(): υ α α collection π α α α υ.

addAll(): Δ υ Ε υ collections.

removeAll(): α α απ α collection π υ φα α α ( αφ υ ).

retainAll(): α φ α α α απ removeAll.

Clear(): α υ α collection απ α α υ. size()

α π φ π π υ α collection

toArray()

π υ α π π α α υ α α collection π α α απ α α Collection<String> c

Object[] a = c.toArray();

α

String[] a = (String[]) c.toArray(new String[c.size()]);

ά ε η 3 22

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 76: Lectures 1 6

Generics

Χα α π υ φα Java SE 5.

α πα Οtype-safety at compile timeΠ α α υ π α α απ φ υ υ α ΟClassCastExceptionΠ α .

α generics α α υ α Collections.

υ compiler πα generics (Type Erasure) α generics α α υ raw types, α πα α ArrayList<String>

ArrayList.

type casting α α α α α α.

α generics α πα υ α υ α α υ α α α α π φ α π α α

α .

ά ε η 3

List<Integer> myIntList = new LinkedList<Integer>(); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next();

List myIntList = new LinkedList(); myIntList.add(new Integer(0)); Integer x = (Integer) myIntList.iterator().next();

23

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 77: Lectures 1 6

Generics α απ πα α α α

ά ε η 3

Set setOfRawType = new HashSet<String>(); setOfRawType = new HashSet<Integer>();

Set<Object> setOfAnyType = new HashSet<Object>(); setOfAnyType.add("abc"); setOfAnyType.add(new Float(3.0f));

Set<?> setOfUnknownType = new LinkedHashSet<String>(); setOfUnknownType = new LinkedHashSet<Integer>();

Set<String> setOfString = new HashSet<String>();

setOfString = new LinkedHashSet<String>();

Set<Object> SetOfObject = new HashSet<String>(); //compiler error

24

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 78: Lectures 1 6

25

ά ε η 3

α α (Threads) Java υ α

Collections & Generics

α φα Java

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 79: Lectures 1 6

Σφ α α η Java (1/2)

α φ α α (Bugs) α π υπ π α π α

α α

α

Α α υ

, πα

π α α π π α α α α α : C: υ υ π α πα α

perror υπ φ α π υ υ . Java: Χ α (exceptions). π υ ,

α (checked) α (unchecked).

ά ε η 3 26

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 80: Lectures 1 6

Σφ α α η Java (2/2) α π α α υ φ α π α

υ π υ α , αυ υ α α (exception object) α πα α runtime system ( throw α exception).

Έ α exception α π π φ α υ φ α π υ υ , α α υ π α α

υ exception .α... To runtime system α π handler απ

π α α π υ α α αυ φ α ( catch exception). α α α απ υ α υ π υ υ φ α α φ main.

ά ε η 3 27

method3()

method2()

method1()

main()

Throws exception

Forwards exception

Catch exception

Call stack trace

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 81: Lectures 1 6

Κα ηγ exceptions Java α πα exceptions.

Ό α υ υπ java.lang.Throwable

Checked exceptions: υ exceptional conditions π υ α φα α π π α recover.

π α ΟCatch or Specify RequirementΠ, α α π α α α α π π α α handlers α α α catch α α π π α

υ α. Ό α α exceptions α checked, απ π υ α υ Error,

RunTimeException α α υπ υ

Error: υ exceptional conditions π υ φ α απ φα

πα α φα υ π α π α α recover.

υ α α α hardware.

υπ α ΟCatch or Specify RequirementΠ α π π α α α υπ stack trace π υ υ exception α π α α α α

α .

Runtime exceptions/Unchecked exceptions: υ exceptional conditions π υ φ α υ π α α . π φ exception αυ α α α NullPointerException!!

υπ α ΟCatch or Specify RequirementΠ α α υ RunTimeException

ά ε η 3 28

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 82: Lectures 1 6

Exception handler

Java α πα α exception handler components. α try, catch, α finally blocks α π π υ α υ α exception

handler.

try

{

code

} catch (ExceptionType name) {

System.err.println(.....);

....

}

catch (ExceptionType name) {

System.err.println(.....);

....

} finally {

.....

}

ά ε η 3

α try block φα υ π υ

υπ π α α α throw α exception.

catch block α exception handler, α α

π exception. υ υπ υ α

υ α π .

finally block α π α, υ exception .

υ φ υ Σ αυ clean up α.

υ α α π /α ΟExceptionTypeΠ,

υ header υ Ζ throw

ExceptionType

29

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 83: Lectures 1 6

ά ε η 3

Χ 2 α exceptions α α α υ π π

απ α υ α υ α φ α αφ .

φ α α α α α .

α α α α α α 2 π α φ α α.

Γ α α υ π α ;

30

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 84: Lectures 1 6

ά ε η 3

α α exception π α

.

φ α α 53! π α φα

α α π .

φ α α 58! α απ

α 38.

υ stack trace π υ α exception π π

απ φα υ π α α . α α α α :

1. Α α π ώ α π υ

π α (απ π π α ).

2. Α α υ α α υ α .

αυ π α. 3. Α α π α

υ .

31

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 85: Lectures 1 6

Απ φα Exceptions - α α

exceptions π φ υ α α π π υ α υ φ α

printStackTrace();

υπ α α π υ α υ φ α.

Ν ΝΑ Α Η Α Τ Η: α π α α α α α

φα ( α Exception).

α π α throws main.

απ α π α α π υ α πα exceptions:

α υ α π υ exception handling απ α φα α .

α exception υ α call stack, απ throws.

α π α exceptions πα α α (π α exceptions FileNotFoundException α

InterruptedIOException π α α α α α απ IOException)

ά ε η 3 32

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 86: Lectures 1 6

4

Web Services α XML

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

1

Page 87: Lectures 1 6

Περ εχό ε α

XML

Web Services

2

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 88: Lectures 1 6

3

XML

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 89: Lectures 1 6

α XML

Η XML (eXtensible Markup Language) α α markup meta- α (υπ SGML) π υ π α

α π αφ , α απα α α α α α . α α παφ π α α α α α

α φα αφ υ α . φα π α XML

Parser α α α α υ α α υ XML φ υ.

π π α α :

π α απ α

built-in data validation

υπ Unicode

4

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 90: Lectures 1 6

Έ α xml α

<Employee_Info>

<Employee Employee_σumber=Ο1Π> <Name> George Kakavoulis</Name>

<Department> HR Dept </Department>

<Telephone> (+30) 210 - 3453432</Telephone>

<Email> g.kakavoulis@some_company.com</Email>

</Employee>

<Employee Employee_σumber=Ο2Π> <Name> Maria Kokkinou</Name>

<Department> Logistics Dept </Department>

<Telephone> (+30) 210 - 3453458</Telephone>

<Email> m.kokkinou@some_company.com</Email>

</Employee> .

.

.

</Employee_Info>

5

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 91: Lectures 1 6

α xml φ υ

Elements: tags <aTag>, <aTag/> π υ π α π υ data. Απ α απ "start tag," "content tag," α "end tagΠ, π. .

Element χω content: <element_name/> (χω start α end tag)

Attributes: π υ π φ α α elements, π. . <aTag id=“123”>

α elements π α υ attributes, content

α α child elements.

content: predefined entity references α υ υ α α : [< &lt;] [> &gt;] .α.

6

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 92: Lectures 1 6

XML Schema H XML Schema α α schema definition α π υ π

απ α W3C π α π XML φ υ. Κα α αυ π α α α .

Έ α .xsd αφ απ α απ elements α attributes.

Π α α:

α XML α

Object-oriented

π υ π (built-in,

απ )

π Namespaces

Έ α element υ π υ π α π child elements α attributes, α element απ π υ π α π text.

7

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 93: Lectures 1 6

XML Schema πα α <?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >

<xs:element name="Employee_Info" type="EmployeeInfoType" />

<xs:complexType name="EmployeeInfoType">

<xs:sequence>

<xs:element ref="Employee" minOccurs="0" maxOccurs="unbounded" />

</xs:sequence>

</xs:complexType>

<xs:element name="Employee" type="EmployeeType" />

<xs:complexType name="EmployeeType">

<xs:sequence >

<xs:element ref="Name" />

<xs:element ref="Department" />

<xs:element ref="Telephone" />

<xs:element ref="Email" />

</xs:sequence>

<xs:attribute name="Employee_Number" type="xs:int" use="required"/>

</xs:complexType>

<xs:element name="Name" type="xs:string" />

<xs:element name="Department" type="xs:string" />

<xs:element name="Telephone" type="xs:string" />

<xs:element name="Email" type="xs:string" />

</xs:schema>

employee.xsd

8

ά ε η 4

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 94: Lectures 1 6

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

XML Schema

Simple Type Elements:

Complex Type Elements:

Reference Elements:

Attributes:

9

ά ε η 4

Page 95: Lectures 1 6

XML Schema π (1/2)

Γ ο built-in πο ο ω :

π π : π α α απα α ,

DTD-compatible, .α.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί12-2013

Page 96: Lectures 1 6

11

XML Schema π (2/2)

Ο απ πω απ ο χ :

<xs:simpleType name="belongType"> <xs:restriction base="xs:string"> <xs:enumeration value="Sales"/> <xs:enumeration value="Development"/> <xs:enumeration value="Service"/> </xs:restriction> </xs:simpleType>

Μ χ extension α απ π υ α υ α α π attribute

Απ π απ

χ

Ο ω πω απ ο χ :

Α user-defined π υ element : <xs:element name=“Emp" type=“EmpType"/>

ω ω .xs ω α χ υ: <xs:include schemaLocation=“EmpType.xs”>

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 97: Lectures 1 6

XML Schema Namespaces (1/2)

<emp:employee xmlns:emp="urn:corp:emp"> <emp:personInfo> … other emp:tags … </emp:personInfo> </emp:employee>

12

Αφ α π α (abstract containers) π υ π υ α α xs elements α attributes α α α α υ

α .

Χ π α prefix α α α elements/ attributes, π π α

elements/ attributes απ π α υ Namespaces conflicts α α.

Χα α α απ α α α, υ URI.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 98: Lectures 1 6

XML Schema Namespaces (2/2)

<employeeList xmlns="urn:corp:list"

xmlns:emp="urn:corp:emp"

xmlns:sec="urn:corp:sec">

<personList>

<emp:empID>AB272654</emp:empID>

<sec:name sec:ID=ΟAB121Π>Sales</sec:name>

<emp:name>John Smith</emp:name>

</list:personList>

<personList>

<emp:empID>E231290</emp:empID>

<sec:name sec:ID=ΟAB125Π>Development</sec:name>

<emp:name>Ichiro Tanaka</emp:name>

</list:personList>

</list:employeeList>

13

υπ πω Namespaces

Attribute Namespace

Default Namespace (χω prefix)

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 99: Lectures 1 6

.xs .xml

Ω α :

root element υ .xml α π : <Employee_Info xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance" xsi:noNamespaceSchemaLocation="employee.xs">

α : <?xml version="1.0"?>

<class xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="#mySchema">

<xs:schema id="mySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="class" type="xs:any"/>

<xs:element name="student" type="xs:string"/>

</xs:schema>

<student>Miltos Karras</student>

<student>John Smith</student>

</class>

14

root element

xs tags

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 100: Lectures 1 6

XML Binding (JAXB)

15

Η Java™Α χ ο γ α XML Binding (JAXB) πα

α π XML schemas Java α απα α α

XML data φα Java.

Η JAXB πα υ α αυ α πα α (unmarshal) Java content trees απ XML instance αφα α α φα (marshal).

Η JAXB χ π α υ υα Java API for XML (JAX-WS) α

υ α Web Services

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 101: Lectures 1 6

16

Web Services

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 102: Lectures 1 6

α α Web Services

17

α α α α α α α υπ υ α α ( π : CORBA, RMI, EJB, .α.)

αφ υπ request/response.

α α υ α software components π υ πα υ α π α π α α υ α φα αφ , α

π standard XML - .

Γ α υ α Web Services π υ HTTP.

α α: α υ α, XML

υ α α firewalls, Http ( fixed port 80 π υ α firewalls π π υ α π )

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 103: Lectures 1 6

Publish, find, bind paradigm

18

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 104: Lectures 1 6

…απ α π

19

Η υ υ Web services α URI:

π. . http://webservices.mysite.com/

weather/us/WeatherService

WS υ π π α α απ

α ( α π υ )

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 105: Lectures 1 6

υπ Web Service α

20

WSDL (UDDI)

SOAP reqs-responses

UDDI (WSDL)

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 106: Lectures 1 6

Web Services Network Stack

21

UDDI (Universal Description Discovery Integration) πα α α απ Web Services, α α υ υ clients α α α α υ α WS π υ .

π υ WS π αφ υ WS (WSDL) α α α α υ UDDI. client π UDDI α α i α WS.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 107: Lectures 1 6

<definitions>: Root element υ WSDL

WSDL Η Web Service Description Language (WSDL) α α W3C standard

XML-based α π υ π π φ α interface, α α α υ α Web service.

Η WSDL α Web services α α υ απ ports α operations (abstract interface). α π αφ α

π αφ υ α π φ π υ υ Web service.

22

<types>: π υ α α α ;

<message>: α α α α α ;

<portType>: υ ( ) υπ α ;

<binding>: α α α α α; SOAP-specific π υπ υ ;

<service>: π α π α α WS;

Αφ υ α Web service, π αφ υ α α

π αυ UDDI. Γ α α χ π π WS

WSDL α χ υ α π α υ WS, υ

α π α π π . υ χ α χ π π φ

αυ α α υ α SOAP α α π .

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 108: Lectures 1 6

WSDL -

<definitions> <types> definition of types........ </types> <message> definition of a message.... </message> <portType> definition of a port....... </portType> <binding> definition of a binding.... </binding> </definitions>

23

Element Defines

<types> The data types used by the web service

<message> The messages used by the web service

<portType> The operations performed by the web service

<binding> The communication protocols used by the web service

<service> The name and location of the web Service

<message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType>

http://www.w3schools.com/wsdl/default.asp

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 109: Lectures 1 6

WSDL Ports

<message name="newTermValues"> <part name="term" type="xs:string"/> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="setTerm"> <input name="newTerm" message="newTermValues"/> </operation> </portType >

ά ε η 7 Τ α Π η φ α Τη επ ω ω

Α άπ υ η Λ γ

Χε ε ά η 2ί1ί-2011

<message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType>

Type Definition

One-way The operation can receive a message but will not return a response

Request-response The operation can receive a request and will return a response

Solicit-response The operation can send a request and will wait for a response

Notification The operation can send a message but will not wait for a response

One-way Request-Response

Page 110: Lectures 1 6

WSDL Binding

binding element name attribute: defines the name of the binding

type attribute: points to the port for the binding

soap:binding element style attribute: can be "rpc" or "documentΠ transport attribute: defines the SOAP protocol to use

operation element a SOAP action for each operation

definition of the encoding for the input and output

<binding type="glossaryTerms" name="b1">

<soap:binding style="documentΠ transport="http://schemas.xmlsoap.org/soap/http" />

<operation>

<soap:operation soapAction="http://example.com/getTerm"/>

<input><soap:body use="literal"/></input>

<output><soap:body use="literal"/></output>

</operation>

</binding>

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 111: Lectures 1 6

WSDL Ν α α (1/3) namespace α υ

web service

complex types π υ α π

απ wsdl

Έ

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 112: Lectures 1 6

WSDL Ν α α (2/3)

ά ε η ι 27

Namespaces π υ α π

π XML Schema α α WSDL

Request

Response υ, α π υ

π φ

ά ε η 6 ά ε η 8

Page 113: Lectures 1 6

WSDL Ν α α (3/3)

α υ web service α π υ

α

π soap υ

π α α π

SOAP WSDL.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 114: Lectures 1 6

Respond with price 22.50

SOAP Simple Object Access Protocol (SOAP) α α W3C standard XML-based

π α απ υ α υ π απ α υ α α α π . υ SOAP α α

π α α α α φ υπ π αφ ( υ HTTP).

SOAP απ α π π α π α π υ π π α α υ α α αφ υ αφ π α α data.

ά ε η 6 Τ α Π η φ α Τη επ ω ω

Α άπ υ η Λ γ

Χε ε ά η 2ί11-2012

29

<SOAP-ENV: Envelope xmlns: SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV: encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Header> π φ α α υ processing nodes (π.χ. authentication or authorization server .α.) – Header π χ π φ α χ body

</SOAP-ENV:Header>

<!-- Request --> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m="some-URI"> <symbol>DEF</Symbol> </m: GetLastTradePrice> </SOAP-ENV:Body>

<!-- Response --> <SOAP-ENV:Body> <m:GetLastTradePriceResponse xmlns:m="some-URI"> <price>22.50</price> </m: GetLastTradePriceResponse> </SOAP-ENV:Body>

</SOAP-Envelope>

SOAP Schema namespace

SOAP encodings

Get Last Trade

Price for symbol

DEF

Page 115: Lectures 1 6

Stubs

31

α WS φα , α SOAP π α υ α α αφ α αυ α α. Ό α α

α client φα α π π α Web Service, α α α α α α α software π υ α stub.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 116: Lectures 1 6

Έ α Web Service π

32

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 117: Lectures 1 6

α α υ π WS (1/4)

ά ε η 6 Τ α Π η φ α Τη επ ω ω

Α άπ υ η Λ γ

Χε ε ά η 2ί11-2012

Web application code:

AddNumbers.wsdl

1.1. υ α WDSL α WS

Server-side

Page 118: Lectures 1 6

Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

ά ε η 6 ά ε η 8

α α υ π WS (2/4)

ά ε η 6

AddNumbers.wsdl

1.2. υ α WDSL α WS

Server-side

Page 119: Lectures 1 6

α α υ π WS (3/4)

AddNumbersImpl.java

2.Αυ α πα α WS α απ WSDL (π. . Axis

with ADB (Axis Databinding))

Server-side

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 120: Lectures 1 6

α α υ π WS (4/4)

3. υ α client code

AddNumbersCall.java

Client-side

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 121: Lectures 1 6

Quick ΣnΣ Dirty (1/3) Η Java απ 6 Endpoint

(javax.xml.ws.Endpoint)

Αυ α π α α α WebService

Server Side:

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 122: Lectures 1 6

Quick ΣnΣ Dirty (2/3) main :

Endpoint.publish("http://127.0.0.1:8081/ChannelSelection", new WebChannelSelectionInterface());

Η ndpoint υ α α WebChannelSelectionInterface α α υ πα απ URL

WSDL: http://127.0.0.1:8081/ChannelSelection/?wsdl

XSD: http://127.0.0.1:8081/ChannelSelection/?xsd=1

α πα α α WSDL α XSD α π π υ α υ υ αυ α α o client.

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 123: Lectures 1 6

Quick ΣnΣ Dirty (3/3)

α project:

ew Web Service Client

υ α wsdl

α α π α α υ client

Invocation:

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 124: Lectures 1 6

Web Services α 2 α α

..... Θα π : WSDL

XSD

SOAP XML

Web Services Learning Trail

http://netbeans.org/kb/trails/web.html

http://netbeans.org/kb/docs/websvc/jax-ws.html

40

ά ε η 4 Τ α Π η φ α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2ί13-2014

Page 125: Lectures 1 6

5

Java DataBase Connectivity

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

1

Page 126: Lectures 1 6

α

α π α π

π π α

α παφ - Κ

2 5

Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 127: Lectures 1 6

α π

Κ π α υπ α π α π υ π α

π φ . π α υ

π α π α α υ . Χα α π

απ υ

π α απ απ α υ υ υπ

π α α α α

3

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 128: Lectures 1 6

α α α α π

α α υ απ υ α α.

α α π υ π α α α αφ α π υ υ π α

υπ α α1. α:

α α απα π α π υ π α α υ

α α υ υ π α π π

α α α α Oracle, MySQL, Access

α α α α π Excel

4

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 129: Lectures 1 6

α α

έ υ Ό α π υ

1 Πα α α α

Φο

ω Ό α Π αφ

1 ω

α ω α υ α α ω ω

Μα α α

Φ α

1 1

ώ

Κ : Έ α π π υ α α ο α

α αφ .

: Έ α π π α αφ α (Δ Ε) π α α.

5

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 130: Lectures 1 6

π α

απ υ π φ α α α α α π φ α

υ 1; α α α α αυ ;

α π υ φ

α αφ φ α υ 1 απ φ

πα απ π α απ υ φ απ υ α α α α

υπ

6

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 131: Lectures 1 6

π α

α π π α απ υ , π /

α παφ π υ π π π α υ υ α α π υπ π α

φα

π α

7

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 132: Lectures 1 6

π JDBC παφ JDBC (Java DataBase Connectivity) π φ α

π α π α φα Java

φα

π α

JDBC παφ

JDBC ( π παφ ) παφ

Ν Γ φυ α

φα α παφ π υ πα

π JDBC

αφ α παφ υ π υ π α

α απ υ α αφ α

απ α φυ α υ π υ υ π α απ π

( α π α π α α α πα υ )

1

2

3

8

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 133: Lectures 1 6

α SQL

H SQL (Structured Query Language) α α απ α π υ α π π α

α α /α α υ α α απ υ α απ /π

/π . υπ α α α π φ α

φ π α α

α :SELECT Υ FROM X WHERE Z π απ : Δ π

απ α υ π α α Χ α π υ Ε

9

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 134: Lectures 1 6

α SQL

έ υ Ό α π υ

1 Πα α α α

Φο

ω Ό α Π αφ

1 ω

α ω α υ α α ω ω

Μα α α

Φ α

1 1

ώ

: α φ α υ 1; SQL: SELECT Ό ο α, Επώ υ ο FROM Φο τ τ WHERE Α .Μ τ ώου=1

: α α 1; SQL: SELECT Ό ο α FROM Μα ατα WHERE Κωδ ό =1

α α α απ : 1. υ π α α π υ π α

FROM 2. π α υ π α α α

π υ υ WHERE 3. π α π υ π α

α 2 α α π υ α SELECT

10

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 135: Lectures 1 6

α παφ Ν Κ π JDBC, π υ πα Java, α

παφ π π π υ π π α π . υ π παφ α απ

(driver) π υ π π α α α α π π υ π α .

java.lang.Class υ forName(String className) Οφ α Π driver . java.sql.DriverManager υ getConnection(String url) αυ α α

υπ υ α α Οα Π α π . URL = jdbc: :π π. . jdbc:odbc:test java.sql.Connection αυ υ π π . π π

υ createStatement() π φ α α Statement π υ α α α παφ π

π . java.sql.Statement αυ υ execureQuery(String sql) α π α α.

11

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 136: Lectures 1 6

α παφ Ν Κ java.sql.ResultSet

απ α π φ π α α. π α π π υ α α απ υ α (π. . getInt(i))

α π υ α υ . α α

Connection con = DriverManager.getConnection(connectionURL);

Statement st = con.createStatement();

ResultSet rs = st.executeQuery(query);

while(rs.next()){

String s = rs.getString(ΟσameτfRow1Π); int i = rs.getInt(ΟNameOfRow2Π); }

π javadoc

υ α

π π

π (α υπ )

12

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 137: Lectures 1 6

π 1 π ώ : α α α α α π υ υ πα α ;

Απ : SELECT Element FROM elements;

13

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 138: Lectures 1 6

π 2 π ώ : α α α α α π υ υ πα α

π α α υ 2; Απ : SELECT Element FROM elements WHERE Quantity>2;

14

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 139: Lectures 1 6

π 3 π ώ : α α α α α π υ υ πα α π α

α υ 2 α 50 Euro; Απ : SELECT Element FROM elements WHERE Quantity>2 AND Prize<50;

15

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 140: Lectures 1 6

π 4

π ώ : α α α α α π υ υ πα α π α α υ 2 α 50 Euro; υ ;

Απ : SELECT Element, Prize FROM elements WHERE Quantity>2 AND Prize<50;

16

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 141: Lectures 1 6

JDBC α υ π

..... Θα π MySQL (free &

open source) http://www.mysql.com http://www.mysql.com/downloads/mysql/ MySQL 5.0 Reference Manual & Tutorial

http://dev.mysql.com/doc/refman/5.5/en/preface.html http://dev.mysql.com/doc/refman/5.5/en/tutorial.html JDBC Driver for MySQL

http://www.mysql.com/products/connector

17

5 Τ α Π φ α Τ π ω

π υ

Χ 2013-2014

Page 142: Lectures 1 6

6

Γ αφ ά ε Java

ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

1

Page 143: Lectures 1 6

α

Γ αφ παφ Χ

awt, swing

α Ν α α α α

α Θ

α υ (listeners)

2

ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 144: Lectures 1 6

Γ αφ Java

3 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 145: Lectures 1 6

Γ αφ παφ

Γ αφ παφ Χ (Graphical User Interface - GUI) α α απ , α π π α

α π υ α υπ . Η α υ υπ α π

αφ παφ : MacOS, δinux, Windows….

GUI αφα π υπ α απ α α α π α υ υπ π α

αφ α π α α : α α υ π παφ α α υ π α α α

Ό α α πα απ π π α υ π Δ α Ε υπ υ α πα υ

π α Η φα π π α α α α Δ υ Ε α

π α

4 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 146: Lectures 1 6

α υ α

π α α απ α απ υπ α α

α υ

π α α α

α α υ Δ α Ε - ΔΌ Ε

α α α π

απ α αφ

απ α π π

α

υ (π α π υ, ….)

α φα

α φα 1

α φα n

α υ

5 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 147: Lectures 1 6

Abstract Window Toolkit - AWT

To AWT α Core Java API, α απ α απ α π α α α α (java.awt.*).

π παφ α α α java φα α υ α

Η φ α φα αφ α α α υ υ α α, α υ υ πα υ υ π α αφ

π

φ α α α

Page 148: Lectures 1 6

javax.swing

Η swing α π

awt.

α υ π Σ υ Java

Cross platform compatibility

Ί α φ ( φ απα απ π α α )

π α αφ α α

υ

α ! (Γ α ;;)

7 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 149: Lectures 1 6

πα swing α

/ π υ AWT

π φα π α α α

πα α

υ απ : http://dn.codegear.com/el/article/26970

8 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 150: Lectures 1 6

α α α παφ JFrame: πα υ α

π α υ π αφ παφ α .

JPanel: υ JFrame α π α φα υ

α παφ α .

•Η α α JPanel JFrame ( Panel Frame α) α N-1, α α Frame π α π π α υ Panel. Σ

Frame π α Panel α α α π α α α .

•Γ JFrame α πα υ φα , JPanel α υ, α π υ π παφ ( υ π , …)

9 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 151: Lectures 1 6

α - α α α α

10 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 152: Lectures 1 6

JComponent α απ α υ πα υ swing

π α π υ υ .

α container α α JFrame, JDialog α JApplet απ α α υ πα υ

• α : get/setName() : / π φ α υ component get/setSize() : / π φ υ component get/setMaximumSize() : / π φ π π get/setMinimumSize() : / π φ π π getWidth() : π φ π υ component getHeight() : π φ υ component getX() : π φ υ Χ (π α υ component) getY() : π φ υ Y (π α υ component)

Ό γ αφ χ υ α α ω . Ε πα υ α α π υπ υ . Γ α π π

α α ίω http://docs.oracle.com/javase/6/docs/api/

11 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 153: Lectures 1 6

JFrame Η JFrame α swing υ π Frame.

Frame α πα υ φα . υ απ Component υ π

JComponent

• α : •JFrame(String name): α α υ α πα υ π . •getContentPane: π φ Panel, α , α π α

α α αφ α υ Frame •setDefaultCloseOperation(int i): υ π φ υ Frame α πα X •setVisible(boolean s): α Frame α φα •setResizable(boolean s): υ π φ υ Frame α πα

υ υ α υ υ π α φ υ

Frame π π

α υ setDefaultCloseOperation: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE EXIT_ON_CLOSE

12 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 154: Lectures 1 6

JPanel To JPanel α π α π α α α α π υ

υ α φα υ παφ α . Η α π α α add (Component c)

π φα α α α α π α απ Layout

π υ υα α α α α υ π φα α α πα α α α (α α α α) :

13 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 155: Lectures 1 6

JButton

To JButton α π απ α π π υ πα υ.

α απ υ π υ π !

α : JButton(String s): υ α υ π υ s π υ α

α απ JComponent addActionListener(ActionListener l): α α

υ υ π . α π π α α φα π α υ υ π .

φ υ α υ πα υ : α πα α υ υ π . α π υ (getContentPane()) υ Frame αυ υ υ π . φ υ Frame.

14 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 156: Lectures 1 6

α α 1 υ α JFrame α π υ αυ α

JButton.

Imports: JButton, JFrame, Dimensions, Panel

Η α α π JFrame

constructor JFrame

υ α π α

υ π φ α υ X

υ , α , υ α υ

π υ α υ υ Frame

υ

α panel υ TestFrame α π αυ υ υ π

φ υ Frame

15 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 157: Lectures 1 6

Menus (I) α π υ α

π π υ υ α πα α υ α . JMenuBar

JMenu

JMenuItem

JSeparator

υ α menus:

υ α α α α menus (JMenuItem)

α υ υπ α (JMenu)

α π υ JMenu π α α JMenuBar

Έ α JMenu π α π α α JMenu (pop up υπ -

)

JSeparator α α

JMenuBar

JMenu

JMenuItem

JSeparator

setEnabled(false)

setEnabled(true)

setText(ΟReplaceΠ)

16 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 158: Lectures 1 6

Menus (II)

Η JMenuBar ν π α α υ JFrame α απ α υ,

α α πα α υ φα

JFrame: setJMenuBar(JMenuBar bar)

H setEnabled π υ φα α α α α α π υ υπ υ α JMenuBar π π

π απ π υ α α α φα

H add υ JMenu π π π JMenuItem JMenu.

17 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 159: Lectures 1 6

ά ε η 7

α α 2 Θα υ υ menu υ

PowerPoint. Θα α απ υ π π π File α α υ υ π υ.

π π π Save As α View

18 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 160: Lectures 1 6

JTextField JLabel

To JTextField α π υ π π α υ αφ παφ :

set/getColumns: / π φ α π υ α JTextField.

set/getText: / π φ π υ φα α JTextField.

π α α π υ α π .

To JLabel α π υ π π φ υ (π αφ π α π α υ) παφ . set/getText: / π φ π υ φα α

JLabel.

set/getIcon: / π φ π υ π JLabel.

π α α π υ α π .

19 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 161: Lectures 1 6

α α 3 π α π αφ α .

π π α α JFrame υ α πα α α α φα α α α.

υ JLabel α φ

υ JTextField α φ

πα υ

π α α

Η υ α y α Ί α α α α 20 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 162: Lectures 1 6

JTextArea

To JTextArea α α π υ π π υ α π α υ ( α υ )

αφ παφ α πα φ α

JTextField. φ α α α υ

append(String s): υ s υ υπ υ

setLineWrap(boolean): υ α α α α π α υ

getLineWrap(): α π πα απ υ α α

getLineCount(): α α π υ π α JTextField

setEditable(boolean): π α α π υ JTextArea

21 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 163: Lectures 1 6

α α 4

α υ α α φα υ α πα υ π α υ α α φα α α JLabel, α JTextField α α JButton.

α π π α υ π α α π α α πα υ π . α α JTextArea π υ α

φα α α απ φ α . π α α π π α π α α α .

22 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 164: Lectures 1 6

α α

23 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 165: Lectures 1 6

α α

α απ υ υ α π υ α π υ , Java υ α α π α α

πα υ α α α αυ α α , α π (Layout Manager)

FlowLayout α Σ α π α

π α α α α π α πα α υ. π π . .π.

α α α α απ υ υ α α. GridLayout

α υ πα α υ α π α nxn

Η π α απ π α υ π α α α υ α π α . Ό α υ α π α

π . .π.

BorderLayout α α α υ α! α α α α α α υπ πα

π α , α , π α αυ .

24 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 166: Lectures 1 6

α α FlowLayout α πα α υ πα α υ

υ Panel π α υ υ ( Χ default)

υ Layout π υ υ α π υ . Η πα

π π υ α φα α α α α (α -

) υ Layout π υ υ α

π Panel α .

υ α υ α π υ Panel. π α π α

απ α .

υ Σ υ υ πα α υ. π α Panel (Container) α αυ π υ JFrame

25 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 167: Lectures 1 6

α α GridLayout α πα α υ πα α υ

υ Panel π α υ υ ( Χ default)

υ Layout π υ υ α π υ . πα υ

α υ π α α α απ α α α

υ Layout π υ υ α π Panel α .

υ α υ α π υ Panel. π α π α

απ α .

υ Σ υ υ πα α υ. π α Panel (Container) α αυ π υ JFrame

26 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 168: Lectures 1 6

α α BorderLayout

α πα α υ πα α υ

α π υ υ α π υ

υ Layout π υ υ α π υ .

υ Layout π υ υ α π Panel α .

υ α υ α π υ Panel. π α π α

α απ π α α .

υ Σ υ υ πα α υ. π α Panel (Container) α αυ π υ JFrame

27 ά ε η 6

Page 169: Lectures 1 6

υ υα Layout Managers

π α π π π α π υ

FlowLayout: π απ α α α α α πα υ α .

GridLayout: απ ! BorderLayout: υ α π π

α α . π α υ υα π υ α !

υ π α panel πα υ α

GridLayout (2x4)

FlowLayout

BorderLayout

28 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 170: Lectures 1 6

ά ε η 6

υ υα Layout Managers

29 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 171: Lectures 1 6

Listeners

30 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 172: Lectures 1 6

Listeners = Interfaces

Actions:

α υ π

π menu item

Enter α text field

α π υ

π α Action…

Event

α α α Events? Listeners

31 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 173: Lectures 1 6

Listeners

α listeners? Interfaces

M αυ π π α α π α πα π abstract

υ . π α π

υ Listeners (= interface) α α αφ υ . π π α

υπ α box π υ α α , π α α υ α.

32 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 174: Lectures 1 6

α α ActionListener (1/2)

33 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 175: Lectures 1 6

α α ActionListener (2/2)

34 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 176: Lectures 1 6

Listeners Interface KeyListener

keyPressed(KeyEvent e) keyReleased(KeyEvent e) keyTyped(KeyEvent e)

Interface MouseListener mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e)

Interface MouseMotionListener mouseDragged(MouseEvent e) mouseMoved(MouseEvent e)

Interface WindowListener windowActivated(WindowEvent e) windowClosed(WindowEvent e) windowClosing(WindowEvent e) windowDeactivated(WindowEvent e) windowDeiconified(WindowEvent e) windowIconified(WindowEvent e) windowOpened(WindowEvent e)

35 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 177: Lectures 1 6

α α Calculator (1/5)

Graphical User Interface Frame

Functionalities Ν Actions Action Listeners

36 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 178: Lectures 1 6

α α Calculator (2/5)

Frame Creation

37 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 179: Lectures 1 6

α α Calculator (1/X)

Frame Creation

38 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 180: Lectures 1 6

α α Calculator (1/X)

Action Listeners

39 ά ε η 6 Τ ή α Π η φ ή α Τη επ ω

Α άπ υ η Λ γ

Χε ε ά η 2013-2014

Page 181: Lectures 1 6

ά ε η 7 40

α α Calculator (5/5)

Action Listeners