Advertisement
KeiroKamioka

Note

Sep 30th, 2020
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package Week1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Week1Assignment {
  6.     public static void main(String[] args) {
  7.         // Test code
  8.         Scanner keyboard = new Scanner(System.in);
  9.  
  10.         System.out.print("Enter your Month by Number");
  11.  
  12.         int userMonth = keyboard.nextInt();
  13.  
  14.         System.out.print("Enter your Year by Number");
  15.  
  16.         int userYear = keyboard.nextInt();
  17.  
  18.         int userDay = getNumberOfDays(userMonth, userYear);
  19.  
  20.     }
  21. }
  22.  
  23. public class MonthDays {
  24.     private int January = 1;
  25.     private int Feburary = 2;
  26.     private int March = 3;
  27.     private int April = 4;
  28.     private int May = 5;
  29.     private int June = 6;
  30.     private int July = 7;
  31.     private int August = 8;
  32.     private int September = 9;
  33.     private int October = 10;
  34.     private int November = 11;
  35.     private int December = 12;
  36.     private int theYear = 2020;
  37.     private int theDays = 1;
  38.  
  39.     /**
  40.      * @param theMonth represent Month of the years
  41.      * @param theYear  represen the Years
  42.      * @param theDyas  return the Days of the
  43.      */
  44.     public int getNumberOfDays(int theMonth, int theYear) {
  45.  
  46.         if (theMonth <= 0) {
  47.             throw new IllegalArgumentException("Month cannot be less than zero!");
  48.         }
  49.         if (theMonth >= 13) {
  50.             throw new IllegalArgumentException("Month cannot be larger than thirteen");
  51.         }
  52.         if (theMonth == January || theMonth == March || theMonth == May || theMonth == July || theMonth == August
  53.                 || theMonth == October || theMonth == December) {
  54.             return theDays = 31;
  55.         } else if (theMonth == April || theMonth == June || theMonth == September || theMonth == November) {
  56.             return theDays = 30;
  57.         }
  58.         // From here check it is leap year or not
  59.  
  60.         else if ((theYear % 400) == 0) {
  61.             //it is leap year
  62.             theDays = 29;
  63.         } else if ((theYear % 100) == 0) {
  64.             // It is not leap year;
  65.             theDays = 28;
  66.         } else if ((theYear % 4) == 0) {
  67.             // It is leap year
  68.             theDays = 29;
  69.         } else {
  70.             // It is not leap year
  71.             theDays = 28;
  72.         }
  73.  
  74.         return theDays;
  75.  
  76.     }
  77. }
  78.  
  79. /*
  80.  * public static int MonthDays(int theYear, int theMonth) {
  81.  *
  82.  * int theDays = 1; if (theMonth <= 0 ){ throw new
  83.  * IllegalArgumentException("Month cannot be less than zero!"); } if (theMonth
  84.  * >= 13){ throw new
  85.  * IllegalArgumentException("Month cannot be larger than thirteen"); } if
  86.  * (theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 || theMonth
  87.  * == 8 || theMonth == 10 || theMonth == 12){ theDays = 31; } else if (theMonth
  88.  * == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11){ theDays = 30; }
  89.  * //Feburary else if((theYear % 400) == 0){ //If it is leap year theDays = 29;
  90.  * } else if((theYear % 100) == 0){ //It is not leap year; theDays = 28; } else
  91.  * if((theYear % 4) == 0){ //It is leap year theDays = 29; } else { //It is not
  92.  * leap year theDays = 28; }
  93.  *
  94.  * return theDays; }
  95.  *
  96.  */
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement