{"id":449,"date":"2010-02-01T17:24:28","date_gmt":"2010-02-01T16:24:28","guid":{"rendered":"http:\/\/www.donatantonio.net\/blog\/?p=449"},"modified":"2010-02-01T17:24:28","modified_gmt":"2010-02-01T16:24:28","slug":"esercizio-20-simple-calendario","status":"publish","type":"post","link":"https:\/\/www.donatantonio.net\/blog\/guide_tutorial\/java\/esercizio-20-simple-calendario\/","title":{"rendered":"Esercizio 20 &#8211; Simple Calendario"},"content":{"rendered":"<p>Realizzare una classe Year con metodo predicativo isLeapYear() e metodo getYear().<br \/>\nClasse Month con metodi getDays() che restituisce il numero di giorni del mese e getMonth() che restituisce il nome del mese.<br \/>\nScrivere un programma che dato il numero di un mese e un anno restituisce il nome del mese, l\u2019anno, se l&#8217;anno \u00e8 bisestile e infine stampi l&#8217;elenco dei giorni.<\/p>\n<p>La classe Year.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Year\r\n{\r\nint anno;\r\n\r\n  \/\/ Metodo Costruttore inizializzato con anno\r\n  public Year(int a)\r\n  {\r\n    if(a&gt;=1980 &amp;&amp; a&lt;=2100)\r\n    {\r\n        anno = a;\r\n    }\r\n    else\r\n    {\r\n        \/\/ Lancia un'eccezione se l'anno non \u00e8 compreso tra 1980 e 2100\r\n        throw new IllegalArgumentException(&quot;Formato anno non valido&quot;);\r\n    }\r\n  }\r\n\r\n  \/\/ Metodo che restituisce l'anno\r\n  public int getYear()\r\n  {\r\n    return anno;\r\n  }\r\n\r\n  \/\/ Metodo che verifica se l'anno \u00e8 bisestile\r\n  public String isLeapYear()\r\n  {\r\n    String verifica = &quot;NO&quot;;\r\n\r\n    int annoCorrente = this.getYear();\r\n    if(annoCorrente % 4 == 0){\r\n    \tSystem.out.println(&quot;Anno bisestile&quot;);\r\n        verifica = &quot;bisestile&quot;;\r\n    }\r\n    return verifica;\r\n  }\r\n}\r\n<\/pre>\n<p>La classe Month.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Month\r\n{\r\nint mese;\r\nint anno;\r\nYear ann;\r\n\r\n  \/\/ Metodo Costruttore inizializzato con il numero del mese e un oggetto Year\r\n  public Month(Year a,int m)\r\n  {\r\n    mese = m;\r\n    ann = a;\r\n    anno = a.getYear();\r\n  }\r\n\r\n  \/\/ Metodo che restituisce il nome del mese\r\n  public void getMonth()\r\n  {\r\n    if (mese &gt;=1 &amp;&amp; mese &lt; 13)\r\n    {\r\n      if (mese==1){  System.out.print(&quot;GENNAIO  &quot;);}\r\n      if (mese==2){  System.out.print(&quot;FEBBRAIO &quot;);}\r\n      if (mese==3){  System.out.print(&quot;MARZO  &quot;);}\r\n      if (mese==4){  System.out.print(&quot;APRILE &quot;);}\r\n      if (mese==5){  System.out.print(&quot;MAGGIO &quot;);}\r\n      if (mese==6){  System.out.print(&quot;GIUGNO &quot;);}\r\n      if (mese==7){  System.out.print(&quot;LUGLIO &quot;);}\r\n      if (mese==8){  System.out.print(&quot;AGOSTO &quot;);}\r\n      if (mese==9){  System.out.print(&quot;SETTEMBRE  &quot;);}\r\n      if (mese==10){  System.out.print(&quot;OTTOBRE &quot;);}\r\n      if (mese==11){  System.out.print(&quot;NOVEMBRE  &quot;);}\r\n      if (mese==12){  System.out.print(&quot;DICEMBRE  &quot;);}\r\n    }\r\n    else\r\n    {\r\n      System.out.println(&quot;Formato mese non valido&quot;);\r\n    }\r\n  }\r\n\r\n  \/\/ Metodo che restituisce il numero di giorni corrispondente al mese\r\n  public void getDays()\r\n  {\r\n    String v = ann.isLeapYear();\r\n    if(mese == 2)\r\n    {\r\n      \/\/ Se il mese \u00e8 Febbraio verifica l'anno se bisestile\r\n      if(v.equalsIgnoreCase(&quot;bisestile&quot;))\r\n      {\r\n        System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);\r\n        System.out.println(&quot;  8   9   10  11  12  13  14&quot;);\r\n        System.out.println(&quot;  15  16  17  18  19  20  21&quot;);\r\n        System.out.println(&quot;  22  23  24  25  26  27  28&quot;);\r\n        System.out.println(&quot;  29    &quot;);\r\n      }\r\n      else\r\n      {\r\n        System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);\r\n        System.out.println(&quot;  8   9   10  11  12  13  14&quot;);\r\n        System.out.println(&quot;  15  16  17  18  19  20  21&quot;);\r\n        System.out.println(&quot;  22  23  24  25  26  27  28&quot;);\r\n      }\r\n    }\r\n    if(mese==1 || mese==3 || mese==5 || mese==7 || mese==8 || mese==10 || mese==12)\r\n    {\r\n      System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);\r\n      System.out.println(&quot;  8   9   10  11  12  13  14&quot;);\r\n      System.out.println(&quot;  15  16  17  18  19  20  21&quot;);\r\n      System.out.println(&quot;  22  23  24  25  26  27  28&quot;);\r\n      System.out.println(&quot;  29  30  31  &quot;);\r\n    }\r\n    if(mese==4 || mese==6 || mese==9 || mese ==11)\r\n    {\r\n      System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);\r\n      System.out.println(&quot;  8   9   10  11  12  13  14&quot;);\r\n      System.out.println(&quot;  15  16  17  18  19  20  21&quot;);\r\n      System.out.println(&quot;  22  23  24  25  26  27  28&quot;);\r\n      System.out.println(&quot;  29  30  &quot;);\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>La classe di test TestCalendario.java<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\npublic class TestCalendario\r\n{\r\n  public static void main(String[] args)\r\n  {\r\n    Scanner in = new Scanner(System.in);\r\n\r\n    \/\/ Prendo in input l'anno\r\n    System.out.println(&quot;Dammi l'anno :&quot;);\r\n    int a = in.nextInt();\r\n\r\n    \/\/ Prendo in input il mese\r\n    System.out.println(&quot;Dammi il mese (numero): &quot;);\r\n    int m = in.nextInt();\r\n\r\n    \/\/ Creo un oggetto Year e un oggetto Month\r\n    Year ye = new Year(a);\r\n    Month mo = new Month(ye,m);\r\n\r\n    \/\/ Visualizzo i dati completi\r\n    mo.getMonth();\r\n    System.out.println(ye.getYear());\r\n    mo.getDays();\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Realizzare una classe Year con metodo predicativo isLeapYear() e metodo getYear().<br \/>\nClasse Month con metodi getDays() che restituisce il numero di giorni del mese e getMonth() che restituisce il nome del mese.<br \/>\nScrivere un programma che dato il numero di un mese e un anno restituisce il nome del mese, l\u2019anno, se l&#8217;anno \u00e8 bisestile e infine stampi l&#8217;elenco dei giorni.<\/p>\n<p>La classe Year.java<\/p>\n<p>public class Year<br \/>\n{<br \/>\nint anno;<\/p>\n<p>  \/\/ Metodo Costruttore inizializzato con anno<br \/>\n  public Year(int a)<br \/>\n  {<br \/>\n    if(a&gt;=1980 &amp;&amp; a&lt;=2100)<br \/>\n    {<br \/>\n        anno = a;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        \/\/ Lancia un&#8217;eccezione se l&#8217;anno non \u00e8 compreso tra 1980 e 2100<br \/>\n        throw new IllegalArgumentException(&quot;Formato anno non valido&quot;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/\/ Metodo che restituisce l&#8217;anno<br \/>\n  public int getYear()<br \/>\n  {<br \/>\n    return anno;<br \/>\n  }<\/p>\n<p>  \/\/ Metodo che verifica se l&#8217;anno \u00e8 bisestile<br \/>\n  public String isLeapYear()<br \/>\n  {<br \/>\n    String verifica = &quot;NO&quot;;<\/p>\n<p>    int annoCorrente = this.getYear();<br \/>\n    if(annoCorrente % 4 == 0){<br \/>\n    \tSystem.out.println(&quot;Anno bisestile&quot;);<br \/>\n        verifica = &quot;bisestile&quot;;<br \/>\n    }<br \/>\n    return verifica;<br \/>\n  }<br \/>\n}<\/p>\n<p>La classe Month.java<\/p>\n<p>public class Month<br \/>\n{<br \/>\nint mese;<br \/>\nint anno;<br \/>\nYear ann;<\/p>\n<p>  \/\/ Metodo Costruttore inizializzato con il numero del mese e un oggetto Year<br \/>\n  public Month(Year a,int m)<br \/>\n  {<br \/>\n    mese = m;<br \/>\n    ann = a;<br \/>\n    anno = a.getYear();<br \/>\n  }<\/p>\n<p>  \/\/ Metodo che restituisce il nome del mese<br \/>\n  public void getMonth()<br \/>\n  {<br \/>\n    if (mese &gt;=1 &amp;&amp; mese &lt; 13)<br \/>\n    {<br \/>\n      if (mese==1){  System.out.print(&quot;GENNAIO  &quot;);}<br \/>\n      if (mese==2){  System.out.print(&quot;FEBBRAIO &quot;);}<br \/>\n      if (mese==3){  System.out.print(&quot;MARZO  &quot;);}<br \/>\n      if (mese==4){  System.out.print(&quot;APRILE &quot;);}<br \/>\n      if (mese==5){  System.out.print(&quot;MAGGIO &quot;);}<br \/>\n      if (mese==6){  System.out.print(&quot;GIUGNO &quot;);}<br \/>\n      if (mese==7){  System.out.print(&quot;LUGLIO &quot;);}<br \/>\n      if (mese==8){  System.out.print(&quot;AGOSTO &quot;);}<br \/>\n      if (mese==9){  System.out.print(&quot;SETTEMBRE  &quot;);}<br \/>\n      if (mese==10){  System.out.print(&quot;OTTOBRE &quot;);}<br \/>\n      if (mese==11){  System.out.print(&quot;NOVEMBRE  &quot;);}<br \/>\n      if (mese==12){  System.out.print(&quot;DICEMBRE  &quot;);}<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n      System.out.println(&quot;Formato mese non valido&quot;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  \/\/ Metodo che restituisce il numero di giorni corrispondente al mese<br \/>\n  public void getDays()<br \/>\n  {<br \/>\n    String v = ann.isLeapYear();<br \/>\n    if(mese == 2)<br \/>\n    {<br \/>\n      \/\/ Se il mese \u00e8 Febbraio verifica l&#8217;anno se bisestile<br \/>\n      if(v.equalsIgnoreCase(&quot;bisestile&quot;))<br \/>\n      {<br \/>\n        System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);<br \/>\n        System.out.println(&quot;  8   9   10  11  12  13  14&quot;);<br \/>\n        System.out.println(&quot;  15  16  17  18  19  20  21&quot;);<br \/>\n        System.out.println(&quot;  22  23  24  25  26  27  28&quot;);<br \/>\n        System.out.println(&quot;  29    &quot;);<br \/>\n      }<br \/>\n      else<br \/>\n      {<br \/>\n        System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);<br \/>\n        System.out.println(&quot;  8   9   10  11  12  13  14&quot;);<br \/>\n        System.out.println(&quot;  15  16  17  18  19  20  21&quot;);<br \/>\n        System.out.println(&quot;  22  23  24  25  26  27  28&quot;);<br \/>\n      }<br \/>\n    }<br \/>\n    if(mese==1 || mese==3 || mese==5 || mese==7 || mese==8 || mese==10 || mese==12)<br \/>\n    {<br \/>\n      System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);<br \/>\n      System.out.println(&quot;  8   9   10  11  12  13  14&quot;);<br \/>\n      System.out.println(&quot;  15  16  17  18  19  20  21&quot;);<br \/>\n      System.out.println(&quot;  22  23  24  25  26  27  28&quot;);<br \/>\n      System.out.println(&quot;  29  30  31  &quot;);<br \/>\n    }<br \/>\n    if(mese==4 || mese==6 || mese==9 || mese ==11)<br \/>\n    {<br \/>\n      System.out.println(&quot;  1   2   3   4   5   6   7 &quot;);<br \/>\n      System.out.println(&quot;  8   9   10  11  12  13  14&quot;);<br \/>\n      System.out.println(&quot;  15  16  17  18  19  20  21&quot;);<br \/>\n      System.out.println(&quot;  22  23  24  25  26  27  28&quot;);<br \/>\n      System.out.println(&quot;  29  30  &quot;);<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>La classe di test TestCalendario.java<\/p>\n<p>import java.util.Scanner;<br \/>\npublic class TestCalendario<br \/>\n{<br \/>\n  public static void main(String[] args)<br \/>\n  {<br \/>\n    Scanner in = new Scanner(System.in);<\/p>\n<p>    \/\/ Prendo in input l&#8217;anno<br \/>\n    System.out.println(&quot;Dammi l&#8217;anno :&quot;);<br \/>\n    int a = in.nextInt();<\/p>\n<p>    \/\/ Prendo in input il mese<br \/>\n    System.out.println(&quot;Dammi il mese (numero): &quot;);<br \/>\n    int m = in.nextInt();<\/p>\n<p>    \/\/ Creo un oggetto Year e un oggetto Month<br \/>\n    Year ye = new Year(a);<br \/>\n    Month mo = new Month(ye,m);<\/p>\n<p>    \/\/ Visualizzo i dati completi<br \/>\n    mo.getMonth();<br \/>\n    System.out.println(ye.getYear());<br \/>\n    mo.getDays();<br \/>\n  }<br \/>\n}<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[69],"tags":[111,110],"_links":{"self":[{"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/posts\/449"}],"collection":[{"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":0,"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.donatantonio.net\/blog\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}