© M. Badella, G. Malnati, L. Tessitore 2003-05 Programmazione ad Oggetti A.A. 2004-05 GESTIONE DEL...

Post on 02-May-2015

215 views 0 download

Transcript of © M. Badella, G. Malnati, L. Tessitore 2003-05 Programmazione ad Oggetti A.A. 2004-05 GESTIONE DEL...

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

A.A. 2004-05

GESTIONE DEL TESTO

2

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Argomenti della lezione

Linguaggio naturale

Il pakage java.text

3

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Linguaggio naturale

4

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Interfacce utente

Un programma comunica con i suoi utilizzatoriAttraverso immagini, suoni e testi

I messaggi non sono sempre completamente predefinitiSpesso il loro contenuto è parametrico

Anche l’utente comunicaUtilizza “segni convenzionali”:

o movimenti del mouse, sequenze di caratteriOccorre riconoscerne il significato

5

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Comunicazione testuale

Sfrutta il linguaggio naturale dell’utilizzatoreÈ legata alla sua “cultura”Lingua adottata, convenzioni di rappresentazione

Impone pesanti vincoli quando occorre realizzare programmi “multiculturali”

6

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Sintesi di messaggio

Un messaggio testuale è composto da una sequenza di caratteri In parte prefissati In parte determinati in fase di esecuzione

Occorre tenere presenti le regole della sintassiConcordanze (genere, numero, persona, …)

7

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

//Restituisce il messaggio adattato//Restituisce il messaggio adattato

String getMessage(int i) {String getMessage(int i) { String s;String s; if (i!=1)if (i!=1) s=“s=“Sono stati letti ”+i+“ elementi..”;”; elseelse s=“s=“È stato letto un elemento.”;”; return s; return s; }}

//Restituisce il messaggio adattato//Restituisce il messaggio adattato

String getMessage(int i) {String getMessage(int i) { String s;String s; if (i!=1)if (i!=1) s=“s=“Sono stati letti ”+i+“ elementi..”;”; elseelse s=“s=“È stato letto un elemento.”;”; return s; return s; }}

8

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Rappresentare i numeri

Le classi “wrapper” offrono il metodo statico toString(…)Restituisce la stringa corrispondente al

parametroNon si può controllare la formattazione

o Numero di cifre, separatore decimale, notazione, …

9

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Analisi di testi

Estremamente più complessaOccorre suddividere, a priori, il testo in unità

elementari

In alcuni casi occorre trattare informazioni strutturateData, ora, valuta, …

La disposizione ed il contenuto dipendono dalla cultura dell’interlocutore

10

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

StringTokenizer st = new StringTokenizer(

“Questa è una prova”);

while (st.hasMoreTokens()) System.out.println(st.nextToken());

StringTokenizer st = new StringTokenizer(

“Questa è una prova”);

while (st.hasMoreTokens()) System.out.println(st.nextToken());

QuestaQuestaèèunaunaprovaprova

QuestaQuestaèèunaunaprovaprova

Scandire una stringa

Si può usare la classe java.util.StringTokenizerSuddivisione basata sugli spaziPochi controlli disponibili

11

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Analizzare un numero Le classi “wrapper” offrono metodi per convertire

stringhe in numeri Integer.parseInt(String val) Double.parseDouble(String v) Possono lanciare NumberFormatException

Basati sulla rappresentazione anglosassone

String s=“123,456”;double d;try {

d=Double.parseDouble(s);} catch (NumberFormatException nfe) {

d=0.0;}

String s=“123,456”;double d;try {

d=Double.parseDouble(s);} catch (NumberFormatException nfe) {

d=0.0;}

12

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Confronto

La classe String offre il metodo compareToBasato sulla rappresentazione UNICODE dei

caratteriNon fornisce i risultati attesi con le lettere

accentate

13

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Internazionalizzazione

Fattori da gestire quando si realizzano versioni per più culture

Messaggi Componenti grafici Suggerimenti SuoniColoriIcone

Numeri (valute/misure)Data/oraIndirizziNumeri telefoniciImpaginazione

14

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Culture

La classe java.util.Locale modella in ambito culturale specifico In termini di lingua e nazione

La conoscenza del locale permette di adattare le operazioni di analisi e sintesi

15

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String getMessage() { Locale loc=Locale.getDefault();

//formato ISO 639: codice a due lettere String lang=loc.getLanguage();

String msg; if ( “it”.equals(lang) )

msg=“Premere <invio>”; else msg=“Press <enter>”; return msg;}

String getMessage() { Locale loc=Locale.getDefault();

//formato ISO 639: codice a due lettere String lang=loc.getLanguage();

String msg; if ( “it”.equals(lang) )

msg=“Premere <invio>”; else msg=“Press <enter>”; return msg;}

16

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Il package java.text

Insieme di classi specializzate nella gestione del testoAnalisi/sintesi/confrontoNumeri, date, messaggi testuali In dipendenza di una cultura specifica (Locale)

17

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

java.text.java.text.FormatFormatjava.text.java.text.FormatFormatformat(…)parseObject(…)

DecimalDecimalFormatFormat

DecimalDecimalFormatFormat

ChoiceChoiceFormatFormatChoiceChoiceFormatFormat

SimpleDate SimpleDate FormatFormat

SimpleDate SimpleDate FormatFormat

DateDate FormatFormat

DateDate FormatFormat

NumberNumber FormatFormat

NumberNumber FormatFormat

MessageMessage FormatFormat

MessageMessage FormatFormat

18

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

NumberFormat

Fornisce metodi per l’analisi e la sintesi di valori numerici:Numeri frazionariPercentuali Valuta

Fornisce risultati differenti in funzione della cultura indicata

19

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String getValue(double d) { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getNumberInstance(loc); String s=nf.format(d);

return s;}

String getValue(double d) { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getNumberInstance(loc); String s=nf.format(d);

return s;} getValue(99.95)getValue(99.95)

99,9599,95

getValue(99.95)getValue(99.95)

99,9599,95

20

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

double parse(String s) throws ParseException { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getNumberInstance(loc); Number n=nf.parse(s);

return n.doubleValue();}

double parse(String s) throws ParseException { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getNumberInstance(loc); Number n=nf.parse(s);

return n.doubleValue();}

parse(“99,95”)parse(“99,95”)

99.9599.95

parse(“99,95”)parse(“99,95”)

99.9599.95

21

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String getPrice(double d) { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getCurrencyInstance(loc); String s=nf.format(d);

return s;}

String getPrice(double d) { Locale loc=Locale.ITALIAN;

NumberFormat nf= NumberFormat.getCurrencyInstance(loc); String s=nf.format(d);

return s;} getPrice(99.95)getPrice(99.95)

€ € 99.9599.95

getPrice(99.95)getPrice(99.95)

€ € 99.9599.95

22

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

DateFormat

Analisi e rappresentazione di data e/o oraDiversi formati (breve, medio, lungo, completo)Permette di impostare il fuso orario (time zone)Supporta calendari arbitrari (di classe

java.util.Calendar)

23

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String getTime(Date d) { Locale loc=Locale.ITALIAN;

DateFormat df= DateFormat.getTimeInstance( DateFormat.SHORT, loc); String s=df.format(d);

return s;}

String getTime(Date d) { Locale loc=Locale.ITALIAN;

DateFormat df= DateFormat.getTimeInstance( DateFormat.SHORT, loc); String s=df.format(d);

return s;}

getTime(getTime( new Date())new Date())

17.3317.33

getTime(getTime( new Date())new Date())

17.3317.33

24

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

MessageFormat

Classe che offre funzionalità sofisticate per costruire ed analizzare messaggiUtilizza una stringa di formato ed un array di

parametriSimile alla funzione sprintf() del linguaggio C

25

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String getMsg(String s, int i) { String pattern= “La cartella {0} contiene {1} file”; MessageFormat mf=

new MessageFormat(pattern);

Object[] args= new Object[] {s, new Integer(i)};

return mf.format(args));}

String getMsg(String s, int i) { String pattern= “La cartella {0} contiene {1} file”; MessageFormat mf=

new MessageFormat(pattern);

Object[] args= new Object[] {s, new Integer(i)};

return mf.format(args));}

getMsg(“c:\”,3)getMsg(“c:\”,3)

La cartella c:\La cartella c:\contiene 3 filecontiene 3 file

getMsg(“c:\”,3)getMsg(“c:\”,3)

La cartella c:\La cartella c:\contiene 3 filecontiene 3 file

26

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

Collator

Classe specializzata nel confrontare stringhe in un dato linguaggioRiordina le lettere accentate, in modo da

garantirne l’opportuna sequenzaOffre vari livelli di precisione nel confronto

27

© M

. Bad

ella

, G. M

alna

ti, L

. Tes

sito

re 2

003-

05

Programmazione ad Oggetti

String[] parole= {“Squadra”,”sì”, “si”};

Locale loc= Locale.ITALIAN;

Collator c= Collator.getInstance(loc);c.setStrength(Collator.SECONDARY);

Arrays.sort(parole,c);

for (int i=0; i<parole.length; i++) System.out.println(parole[i]);

String[] parole= {“Squadra”,”sì”, “si”};

Locale loc= Locale.ITALIAN;

Collator c= Collator.getInstance(loc);c.setStrength(Collator.SECONDARY);

Arrays.sort(parole,c);

for (int i=0; i<parole.length; i++) System.out.println(parole[i]);

sisisìsìSquadraSquadra

sisisìsìSquadraSquadra