Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Week1;
- import java.util.Scanner;
- public class Week1Assignment {
- public static void main(String[] args) {
- // Test code
- Scanner keyboard = new Scanner(System.in);
- System.out.print("Enter your Month by Number");
- int userMonth = keyboard.nextInt();
- System.out.print("Enter your Year by Number");
- int userYear = keyboard.nextInt();
- int userDay = getNumberOfDays(userMonth, userYear);
- }
- }
- public class MonthDays {
- private int January = 1;
- private int Feburary = 2;
- private int March = 3;
- private int April = 4;
- private int May = 5;
- private int June = 6;
- private int July = 7;
- private int August = 8;
- private int September = 9;
- private int October = 10;
- private int November = 11;
- private int December = 12;
- private int theYear = 2020;
- private int theDays = 1;
- /**
- * @param theMonth represent Month of the years
- * @param theYear represen the Years
- * @param theDyas return the Days of the
- */
- public int getNumberOfDays(int theMonth, int theYear) {
- if (theMonth <= 0) {
- throw new IllegalArgumentException("Month cannot be less than zero!");
- }
- if (theMonth >= 13) {
- throw new IllegalArgumentException("Month cannot be larger than thirteen");
- }
- if (theMonth == January || theMonth == March || theMonth == May || theMonth == July || theMonth == August
- || theMonth == October || theMonth == December) {
- return theDays = 31;
- } else if (theMonth == April || theMonth == June || theMonth == September || theMonth == November) {
- return theDays = 30;
- }
- // From here check it is leap year or not
- else if ((theYear % 400) == 0) {
- //it is leap year
- theDays = 29;
- } else if ((theYear % 100) == 0) {
- // It is not leap year;
- theDays = 28;
- } else if ((theYear % 4) == 0) {
- // It is leap year
- theDays = 29;
- } else {
- // It is not leap year
- theDays = 28;
- }
- return theDays;
- }
- }
- /*
- * public static int MonthDays(int theYear, int theMonth) {
- *
- * int theDays = 1; if (theMonth <= 0 ){ throw new
- * IllegalArgumentException("Month cannot be less than zero!"); } if (theMonth
- * >= 13){ throw new
- * IllegalArgumentException("Month cannot be larger than thirteen"); } if
- * (theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 || theMonth
- * == 8 || theMonth == 10 || theMonth == 12){ theDays = 31; } else if (theMonth
- * == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11){ theDays = 30; }
- * //Feburary else if((theYear % 400) == 0){ //If it is leap year theDays = 29;
- * } else if((theYear % 100) == 0){ //It is not leap year; theDays = 28; } else
- * if((theYear % 4) == 0){ //It is leap year theDays = 29; } else { //It is not
- * leap year theDays = 28; }
- *
- * return theDays; }
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement