removeAllElement s; addElement new St ring“1”;
Software Design Java Tutorial Software Design Java Tutorial
© SERG
The Vector Class
• The vector class may be used as a dynamic
array.
• The vector class can hold any Java object.
import j ava. ut il. Vect or; Vect or v = new Vect or;
v. removeAllElement s; v. addElement new St ring“1”;
v. addElement new St ring“2”; v. addElement new St ring“3”;
v. addElement new St ring“4”;
f or int i = 0; i v. size; i++ Syst em. out . print ln“Dat a = “ + v. element At i;
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Data Conversion Functions
• Java contains a robust set of classes to
convert from one data type to another.
• There is a data encapsulation class for each
scalar type:
– Long
for long –
I nt eger
for int –
Float
for float –
Double
for double –
Boolean
for boolean
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Data Conversion Functions
Cont’d
• Typical functionality includes:
– Convert tofrom bases
– Convert tofrom a string
– Equality checking
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Java Events and GUI Programming
Tutorial for Beginners by SERG
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Create the Worker
• We start by creating a worker which will
actually do the work
• The example worker is computing all the
multiplications of i
and j
from 1 to 10
package Hello; impor t j ava.ut il.Vect or ;
impor t j ava.ut il.Enumer at ion;
public class claculat or { pr ivat e int pr og;
public claculat or { pr og = 0;
}
public void wor k {
f or int i=1;i11;i++ {
f or int j =1;j 11;j ++ { pr og = i j ;
not if yList ener spr og, f alse; }
} }
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Creating the Main Program
• The main program will simulate the user
interface that we will create later
• The main program is responsible to start the
worker and receive the results
package Hello;
public class Hello_main {
public st at ic void mainSt r ing ar gs[ ] {
Hello_main hm = new Hello_main; Syst em.out .pr int lnSt ar t ;
hm.wor kNoThr ead; Syst em.out .pr int lnEnd;
}
public Hello_main { }
public void wor kNoThr ead {
f inal claculat or cc = new claculat or ; Syst em.out .pr int lnNo Thr ead;
cc.wor k; }
}
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Why Isn’t This Enough
• So far we have a main program that starts a
worker.
• The main program can receive no
information from the worker until the worker had finished the calculation.
• This situation creates a feeling that the
program is stuck and the user is likely to restart it.
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Adding the Event
• The next step is to add an event. The event object
will carry the information from the worker to the main program.
• The event should be customized to fit your needs.
• Our event includes:
– Progress: let the main program know the progress of the
worker.
– Finished flag: let the main program know when the
worker is done.
package Hello;
public class I t er at ionEvent { pr ivat e int value_;
pr ivat e boolean f inished_;
public I t er at ionEvent int value, boolean f inished { value_ = value;
f inished_ = f inished; }
public int get Value {
r et ur n value_; }
public boolean isFinished {
r et ur n f inished_; }
}
Software Design Java Tutorial Software Design Java Tutorial
© SERG
Adding the Listener
• Now that we have an event object we can
build an interface of a listener.
• A listerner will be any object that can
receive events.
• The interface should include any function
interface that will be implemented in the actual listening object.
package Hello; public int er f ace I t er at ionList ener {
public void next I t er at ionI t er at ionEvent e; }
Software Design Java Tutorial Software Design Java Tutorial
© SERG
The Main Program Revisited
• Now that we have the listener interface we
should change the main program to implement the interface.
• We need to add the implementation of the
interface and all the functions that we declared.
Note: don’t try to run the new program before you change the worker as well.
package Hello; public class Hello_main
implement s I t er at ionList ener implement s I t er at ionList ener
{ public st at ic void mainSt r ing ar gs[ ]
{ Hello_main hm = new Hello_main;
Syst em.out .pr int lnSt ar t ; hm.wor kNoThr ead;
Syst em.out .pr int lnEnd; }
public Hello_main { }
public void wor kNoThr ead {
f inal claculat or cc = new claculat or ;
cc. addList ener t his; cc. addList ener t his;
Syst em.out .pr int lnNo Thr ead; cc.wor k;
cc. r emoveList ener t his; cc. r emoveList ener t his;
}
public void next I t erat ionI t erat ionEvent e public void next I t erat ionI t erat ionEvent e
{ {
Syst em. out . print lnFrom Main: Syst em. out . print lnFrom Main:
+e. get Value; +e. get Value;
if e. isFinished { if e. isFinished {
Syst em. out . print lnFinished; Syst em. out . print lnFinished;
} }
} }
}
Software Design Java Tutorial Software Design Java Tutorial
© SERG
The Worker Revisited
• As you might have noticed there are new functions
that we need to add to the worker object:
– addListenerIterationListener listener
• Add a new listener to the worker the listener will receive
messages from the worker you might want more than one.
– removeListenerIterationListener listener
• Remove a listener from the worker.
– notifyListenersint value, boolean finished
• The function that will actually notify the listener
package Hello; impor t j ava.ut il.Vect or ;
impor t j ava.ut il.Enumer at ion;
public class claculat or { pr ivat e int pr og;
pr ivat e Vect or list ener s_ = new Vect or ;
public claculat or { pr og = 0;
}
public void wor k {
f or int i=1;i11;i++ {
f or int j =1;j 11;j ++ { pr og = i j ;
not if yList ener sprog, f alse; not if yList ener sprog, f alse;
} }
not if yList ener sprog, t r ue; not if yList ener sprog, t r ue;
}
public void addList ener I t erat ionList ener list ener public void addList ener I t erat ionList ener list ener
{ {
list ener s_. addElement list ener ; list ener s_. addElement list ener ;
} }
public void r emoveList ener I t er at ionList ener list ener public void r emoveList ener I t er at ionList ener list ener
{ {
list ener s_. r emoveElement list ener ; list ener s_. r emoveElement list ener ;
} }
privat e void not if yList ener sint value, boolean f inished privat e void not if yList ener sint value, boolean f inished
{ {
I t erat ionEvent e = new I t erat ionEvent value, f inished; I t erat ionEvent e = new I t erat ionEvent value, f inished;
f or Enumer at ion list ener s = list ener s_. element s; f or Enumer at ion list ener s = list ener s_. element s;
list ener s. hasMor eElement s; list ener s. hasMor eElement s;
{ {
I t er at ionList ener l = I t er at ionList ener I t er at ionList ener l = I t er at ionList ener
list ener s. next Element ; list ener s. next Element ;
l. next I t er at ione; l. next I t er at ione;