Esercizio 20 – Simple Calendario
- Febbraio 01, 2010
- by
- Donatantonio
Realizzare una classe Year con metodo predicativo isLeapYear() e metodo getYear().
Classe Month con metodi getDays() che restituisce il numero di giorni del mese e getMonth() che restituisce il nome del mese.
Scrivere un programma che dato il numero di un mese e un anno restituisce il nome del mese, l’anno, se l’anno è bisestile e infine stampi l’elenco dei giorni.
La classe Year.java
public class Year
{
int anno;
// Metodo Costruttore inizializzato con anno
public Year(int a)
{
if(a>=1980 && a<=2100)
{
anno = a;
}
else
{
// Lancia un'eccezione se l'anno non è compreso tra 1980 e 2100
throw new IllegalArgumentException("Formato anno non valido");
}
}
// Metodo che restituisce l'anno
public int getYear()
{
return anno;
}
// Metodo che verifica se l'anno è bisestile
public String isLeapYear()
{
String verifica = "NO";
int annoCorrente = this.getYear();
if(annoCorrente % 4 == 0){
System.out.println("Anno bisestile");
verifica = "bisestile";
}
return verifica;
}
}
La classe Month.java
public class Month
{
int mese;
int anno;
Year ann;
// Metodo Costruttore inizializzato con il numero del mese e un oggetto Year
public Month(Year a,int m)
{
mese = m;
ann = a;
anno = a.getYear();
}
// Metodo che restituisce il nome del mese
public void getMonth()
{
if (mese >=1 && mese < 13)
{
if (mese==1){ System.out.print("GENNAIO ");}
if (mese==2){ System.out.print("FEBBRAIO ");}
if (mese==3){ System.out.print("MARZO ");}
if (mese==4){ System.out.print("APRILE ");}
if (mese==5){ System.out.print("MAGGIO ");}
if (mese==6){ System.out.print("GIUGNO ");}
if (mese==7){ System.out.print("LUGLIO ");}
if (mese==8){ System.out.print("AGOSTO ");}
if (mese==9){ System.out.print("SETTEMBRE ");}
if (mese==10){ System.out.print("OTTOBRE ");}
if (mese==11){ System.out.print("NOVEMBRE ");}
if (mese==12){ System.out.print("DICEMBRE ");}
}
else
{
System.out.println("Formato mese non valido");
}
}
// Metodo che restituisce il numero di giorni corrispondente al mese
public void getDays()
{
String v = ann.isLeapYear();
if(mese == 2)
{
// Se il mese è Febbraio verifica l'anno se bisestile
if(v.equalsIgnoreCase("bisestile"))
{
System.out.println(" 1 2 3 4 5 6 7 ");
System.out.println(" 8 9 10 11 12 13 14");
System.out.println(" 15 16 17 18 19 20 21");
System.out.println(" 22 23 24 25 26 27 28");
System.out.println(" 29 ");
}
else
{
System.out.println(" 1 2 3 4 5 6 7 ");
System.out.println(" 8 9 10 11 12 13 14");
System.out.println(" 15 16 17 18 19 20 21");
System.out.println(" 22 23 24 25 26 27 28");
}
}
if(mese==1 || mese==3 || mese==5 || mese==7 || mese==8 || mese==10 || mese==12)
{
System.out.println(" 1 2 3 4 5 6 7 ");
System.out.println(" 8 9 10 11 12 13 14");
System.out.println(" 15 16 17 18 19 20 21");
System.out.println(" 22 23 24 25 26 27 28");
System.out.println(" 29 30 31 ");
}
if(mese==4 || mese==6 || mese==9 || mese ==11)
{
System.out.println(" 1 2 3 4 5 6 7 ");
System.out.println(" 8 9 10 11 12 13 14");
System.out.println(" 15 16 17 18 19 20 21");
System.out.println(" 22 23 24 25 26 27 28");
System.out.println(" 29 30 ");
}
}
}
La classe di test TestCalendario.java
import java.util.Scanner;
public class TestCalendario
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Prendo in input l'anno
System.out.println("Dammi l'anno :");
int a = in.nextInt();
// Prendo in input il mese
System.out.println("Dammi il mese (numero): ");
int m = in.nextInt();
// Creo un oggetto Year e un oggetto Month
Year ye = new Year(a);
Month mo = new Month(ye,m);
// Visualizzo i dati completi
mo.getMonth();
System.out.println(ye.getYear());
mo.getDays();
}
}

