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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Suggerimento: trovate l’elemento maggiore nel sottoinsieme che contiene tutti gli elementi tranne<br />

l’ultimo; quin<strong>di</strong>, confrontate tale massimo con il valore dell’ultimo elemento.<br />

* <strong>Esercizi</strong>o P12.7. Usando la ricorsione, calcolate la somma <strong>di</strong> tutti i valori presenti in un array.<br />

public class DataSet<br />

{<br />

public DataSet(int[] values, int fi rst, int last) {...}<br />

public int getSum() {...}<br />

...<br />

}<br />

** <strong>Esercizi</strong>o P12.8. Usando la ricorsione, calcolate l’area <strong>di</strong> un poligono. Identifi catene una porzione<br />

triangolare e sfruttate il fatto che un triangolo con vertici (x 1, y 1), (x 2, y 2) e (x 3, y 3) ha area:<br />

xy 1 2+ xy 2 3+ xy 2 1−yx 1 2−yx 2 3−yx 3 1<br />

2<br />

*** <strong>Esercizi</strong>o P12.9. Realizzate una classe SubstringGenerator che generi tutte le sottostringhe <strong>di</strong><br />

una stringa. Ad esempio, le sottostringhe della stringa “rum” sono queste sette stringhe:<br />

“r”, “ru”, “rum”, “u”, “um”, “m”, “”<br />

Suggerimento: dapprima trovate tutte le sottostringhe che iniziano con il primo carattere, che sono<br />

n se la stringa ha lunghezza n; poi, trovate le sottostringhe della stringa che si ottiene eliminando<br />

il primo carattere.<br />

*** <strong>Esercizi</strong>o P12.10. Realizzate una classe SubsetGenerator che generi tutti i sottoinsiemi <strong>di</strong> caratteri<br />

<strong>di</strong> una stringa. Ad esempio, i sottoinsiemi <strong>di</strong> caratteri della stringa “rum” sono queste otto<br />

stringhe:<br />

“rum”, “ru”, “rm”, “r”, “um”, “u”, “m”, “”<br />

Notate che non è necessario che i sottoinsiemi siano sottostringhe: ad esempio, “rm” non è una<br />

sottostringa <strong>di</strong> “rum”.<br />

*** <strong>Esercizi</strong>o P12.11. In questo esercizio mo<strong>di</strong>fi cherete la classe PermutationGenerator vista nel<br />

Paragrafo 12.4 (che aveva un metodo per il calcolo <strong>di</strong> tutte le permutazioni <strong>di</strong> una stringa), trasformandola<br />

nella classe PermutationIterator (che calcola una permutazione per volta).<br />

public class PermutationIterator<br />

{<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!