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.

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

bisestile se è <strong>di</strong>visibile per 400 (per esempio, il 2000). Scrivete un programma che chieda all’utente<br />

<strong>di</strong> inserire un anno e che calcoli se è bisestile. Scrivere una classe Year con un metodo pre<strong>di</strong>cativo<br />

boolean isLeapYear().<br />

* <strong>Esercizi</strong>o P5.13. Scrivete un programma che chieda all’utente <strong>di</strong> inserire il numero corrispondente<br />

a un mese (1 per gennaio, 2 per febbraio, e così via), e che quin<strong>di</strong> stampi il numero <strong>di</strong> giorni del<br />

mese. Nel caso <strong>di</strong> febbraio, stampate comunque “28 days”.<br />

Enter a month(1-12):<br />

5<br />

31 days<br />

Realizzate una classe Month con un metodo int getDays(). Non usate un <strong>di</strong>verso enunciato if<br />

o else per ogni mese, ma fate uso <strong>di</strong> operatori booleani.<br />

*** <strong>Esercizi</strong>o P5.14. Scrivete un programma che legga due numeri in virgola mobile, per poi <strong>di</strong>chiararli<br />

uguali se, quando vengono arrotondati con due cifre decimali dopo la virgola, <strong>di</strong>fferiscono<br />

per meno <strong>di</strong> 0.01. Ecco due esempi <strong>di</strong> esecuzione:<br />

Enter two fl oating-point numbers:<br />

2.0<br />

1.99998<br />

They are the same when rounded to two decimal places.<br />

They <strong>di</strong>ffer by less than 0.01.<br />

Enter two fl oating-point numbers:<br />

0.999<br />

0.991<br />

They are <strong>di</strong>fferent when rounded to two decimal places.<br />

They don’t <strong>di</strong>ffer by less than 0.01.<br />

* <strong>Esercizi</strong>o P5.15. Migliorate la classe BankAccount del Capitolo 3, in modo da:<br />

1. Rifi utare importi negativi nei meto<strong>di</strong> deposit e withdraw.<br />

2. Rifi utare prelievi che portino a un saldo negativo.<br />

* <strong>Esercizi</strong>o P5.16. Scrivete un programma che riceva la retribuzione oraria <strong>di</strong> un <strong>di</strong>pendente.<br />

Quin<strong>di</strong>, chiedete quante ore ha lavorato il <strong>di</strong>pendente la scorsa settimana. Assicuratevi <strong>di</strong> accettare<br />

le frazioni <strong>di</strong> ora e calcolate la retribuzione. Eventuali straor<strong>di</strong>nari (oltre le 40 ore settimanali) vanno<br />

pagati nella misura del 150 per cento rispetto alla retribuzione normale. Risolvete il problema<br />

scrivendo una classe Paycheck.<br />

** <strong>Esercizi</strong>o P5.17. Scrivete un programma per la conversione <strong>di</strong> unità <strong>di</strong> misura. Chiedete all’utente<br />

da quale unità vuole convertire e qual è l’unità <strong>di</strong> destinazione (in per pollice, ft per piede, mi per<br />

miglio, mm per millimetro, cm per centimetro, m per metro e km per chilometro). Usate due oggetti<br />

<strong>di</strong> una classe UnitConverter, uno che converta in metri e uno che converta da metri.<br />

Convert from:<br />

in<br />

Convert to:<br />

mm<br />

Value:<br />

10<br />

10 in = 254 mm<br />

*** <strong>Esercizi</strong>o P5.18. Una retta giacente in un piano può essere specifi cata in <strong>di</strong>versi mo<strong>di</strong>:<br />

me<strong>di</strong>ante un punto (x, y) e una pendenza m<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!