31.05.2013 Views

Esercizi e progetti di programmazione - Apogeo

Esercizi e progetti di programmazione - Apogeo

Esercizi e progetti di programmazione - Apogeo

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

E–52 ESERCIZI E PROGETTI DI PROGRAMMAZIONE<br />

* <strong>Esercizi</strong>o P11.16. Mo<strong>di</strong>fi cate la classe DataSetReader in modo che non invochi hasNextInt<br />

né hasNextDouble. Semplicemente, lasciate che nextInt e nextDouble lancino un’eccezione <strong>di</strong><br />

tipo NoSuchElementException, catturandola nel metodo main.<br />

** <strong>Esercizi</strong>o P11.17. Scrivete un programma che legga un insieme <strong>di</strong> descrizioni <strong>di</strong> monete da un<br />

fi le avente il formato seguente:<br />

nomeMoneta1 valoreMoneta1<br />

nomeMoneta2 valoreMoneta2<br />

...<br />

Aggiungete alla classe Coin il metodo<br />

void read(Scanner in) throws FileNotFoundException<br />

che lanci un’eccezione se la riga letta non ha il formato corretto. Quin<strong>di</strong>, realizzate il metodo<br />

static ArrayList readFile(String fi lename)<br />

throws FileNotFoundException<br />

Nel metodo main invocate readFile. Se viene lanciata un’eccezione, date all’utente la possibilità<br />

<strong>di</strong> selezionare un altro fi le. Se tutte le monete vengono lette correttamente, visualizzate il loro<br />

valore totale.<br />

*** <strong>Esercizi</strong>o P11.18. Progettate una classe Bank che contenga un certo numero <strong>di</strong> conti bancari.<br />

Ciascun conto ha un numero <strong>di</strong> conto e un saldo. Aggiungete una variabile <strong>di</strong> esemplare accountNumber<br />

alla classe BankAccount e memorizzate i conti bancari in un vettore. Scrivete un<br />

metodo readFile per la classe Bank che legga un fi le scritto con il seguente formato:<br />

numeroDiConto1 saldo1<br />

numeroDiConto2 saldo2<br />

...<br />

Realizzate meto<strong>di</strong> read nelle classi Bank e BankAccount. Scrivete un programma <strong>di</strong> prova che<br />

legga un fi le <strong>di</strong> conti correnti, per poi visualizzare il conto con il saldo maggiore. Se il fi le non<br />

contiene dati corretti, date all’utente la possibilità <strong>di</strong> selezionare un <strong>di</strong>verso fi le.<br />

Progetti <strong>di</strong> <strong>programmazione</strong><br />

Progetto 11.1. Con la seguente sequenza <strong>di</strong> coman<strong>di</strong> potete leggere i contenuti <strong>di</strong> una pagina<br />

web:<br />

String address = “http://java.sun.com/index.html”;<br />

URL u = new URL(address);<br />

Scanner in = new Scanner(u.openStream());<br />

...<br />

Alcuni <strong>di</strong> questi meto<strong>di</strong> possono lanciare eccezioni: consultate la documentazione della libreria<br />

standard. Progettate una classe LinkFinder che cerchi tutti i collegamenti ipertestuali (hyperlink),<br />

aventi la forma:<br />

testo del collegamento<br />

Cay Horstmann: Concetti <strong>di</strong> informatica e fondamenti <strong>di</strong> Java 5 a ed. - Copyright 2010 <strong>Apogeo</strong> srl

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!