Esercizi e progetti di programmazione - Apogeo
Esercizi e progetti di programmazione - Apogeo
Esercizi e progetti di programmazione - Apogeo
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
ESERCIZI E PROGETTI DI PROGRAMMAZIONE E–45<br />
**T <strong>Esercizi</strong>o P9.18. Dovete tradurre in HTML un testo <strong>di</strong>sponibile nel sito del Progetto Gutenberg<br />
(http://gutenberg.org). Ecco, ad esempio, l’inizio del primo capitolo <strong>di</strong> Anna Karenina, <strong>di</strong><br />
Tolstoy:<br />
Chapter 1<br />
Happy families are all alike; every unhappy family is unhappy in<br />
its own way.<br />
Everything was in confusion in the Oblonskys’ house. The wife<br />
had <strong>di</strong>scovered that the husband was carrying on an intrigue with<br />
a French girl, who had been a governess in their family, and she<br />
had announced to her husband that she could not go on living in<br />
the same house with him ...<br />
L’equivalente in HTML è:<br />
Chapter 1<br />
Happy families are all alike; every unhappy family is unhappy in<br />
its own way.<br />
Everything was in confusion in the Oblonskys&rsquo house. The wife<br />
had <strong>di</strong>scovered that the husband was carrying on an intrigue with<br />
a French girl, who had been a governess in their family, and she<br />
had announced to her husband that she could not go on living in<br />
the same house with him ...<br />
La conversione in HTML può essere portata a termine in due fasi. Dapprima il testo viene decomposto<br />
in segmenti, cioè blocchi <strong>di</strong> testo dello stesso tipo (intestazione, paragrafo, e così via), poi<br />
ciascun segmento viene convertito, racchudendolo tra i corretti marcatori HTML e convertendo<br />
i caratteri speciali presenti.<br />
Recuperare il testo da Internet e scomporlo in segmenti è un compito arduo, per cui occorre<br />
defi nire un’interfaccia e progettare un oggetto semplifi cato, con cui collaudare la classe che si<br />
occupa <strong>di</strong> portare a termine la conversione in HTML.<br />
Testo originario HTML<br />
“ ” “ (a sinistra) oppure ” (a destra)<br />
‘ ’ ‘ (a sinistra) oppure ’ (a destra)<br />
—<br />
&emdash;<br />
<<br />
<<br />
><br />
><br />
&<br />
&<br />
***G <strong>Esercizi</strong>o P9.19. Scrivete un metodo randomShape che generi casualmente oggetti che realizzano<br />
l’interfaccia Shape: un miscuglio <strong>di</strong> rettangoli, ellissi e linee, con posizioni casuali. Invocatelo 10<br />
volte e <strong>di</strong>segnate tutte le forme.<br />
*G <strong>Esercizi</strong>o P9.20. Migliorate il programma ButtonViewer in modo che visualizzi il messaggio<br />
“I was clicked n times!” ogni volta che viene premuto il pulsante. Il valore <strong>di</strong> n deve aumentare a<br />
ogni pressione.<br />
**G <strong>Esercizi</strong>o P9.21. Migliorate il programma ButtonViewer in modo che abbia due pulsanti, ciascuno<br />
dei quali visualizzi il messaggio “I was clicked n times!” ogni volta che viene premuto. Ogni<br />
pulsante deve avere il proprio contatore.<br />
Cay Horstmann: Concetti <strong>di</strong> informatica e fondamenti <strong>di</strong> Java 5 a ed. - Copyright 2010 <strong>Apogeo</strong> srl