Do NOT modify the MAINSolutionPlease follow the code and com

Do NOT modify the MAIN

Solution

Please follow the code and comments for description :

CODE :

import java.util.Scanner; // required imports

public class DayOfWeek { // class to run the code

    public static int userInteger(String message) { // method to get the date entered by the user
        Scanner in = new Scanner(System.in);
        int result;
        while (true) { // checking for the date entered
            System.out.printf(message); // printing the message
            String s = in.next();
            if (s.equals(\"q\")) {
                System.out.printf(\"Exiting...\ \");
                System.exit(0); // exiting the code
            }

            try {
                result = Integer.parseInt(s); // getting the result
            } catch (Exception e) {
                System.out.printf(\"%s is not a valid number, try again.\ \ \", s);
                continue;
            }

            if (result <= 0) {
                System.out.printf(\"%s is <= 0, try again.\ \ \", s);
                continue;
            }
            return result; // returning the result
        }
    }

    public static int yearDays(int year) { // getting the year days for a given year
        int result = 0;
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {             // checking for the leap year condition
            result = 366;          
        } else {
            result = 365;          
        }
        return result;
    }

    public static int monthDays(int year, int month) { // returning the days for a given month

        if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) {// checking for the mont count
            int result2 = 31;
            return result2;
        }

        if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
            int result2 = 30;
            return result2;
        }
        if ((month == 2) && (!((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))) {
            int result2 = 28;
            return result2;
        } else {
            int result2 = 29;
            return result2;
        }
    }

    public static int daysPassed(int year, int month, int day) { // calculating the days that have passed from a given date
        int totalYrDays = 0, totalDays = 0; // required initialisations
        for (int i = 1000; i < year; i++) { // iterating over the loop from 100 to entered year
            int yrDays = yearDays(i);
            totalYrDays = totalYrDays + yrDays; // calculating the totalDays
        }
      
        int totalMnthDays = 0;
        for (int j = 1; j < month; j++) { // iterating over the month to get the days
            totalMnthDays = totalMnthDays + monthDays(year, j);       // calculating the totalDays
        }
      
        totalDays = totalYrDays + totalMnthDays + day; // summing the total
        return totalDays; // returning the total days
    }

    public static String dayOfWeek(int year, int month, int day) { // method to get the day of the date
        String dayOfTheWeek = null; // required initialisations
        int totalDiffDays = daysPassed(year, month, day); // getting the total difference days
        int rem = totalDiffDays % 7; // getting the remainder
      
        if (rem == 0) { // checking based on the remainder
            dayOfTheWeek = \"Tuesday\";
        } else if (rem == 1) {
            dayOfTheWeek = \"Wednesday\";
        } else if (rem == 2) {
            dayOfTheWeek = \"Thursday\";
        } else if (rem == 3) {
            dayOfTheWeek = \"Friday\";
        } else if (rem == 4) {
            dayOfTheWeek = \"Saturday\";
        } else if (rem == 5) {
            dayOfTheWeek = \"Sunday\";
        } else {
            dayOfTheWeek = \"Monday\";
        }
        return dayOfTheWeek; // return the day of the week
    }

    public static void main(String[] args) { // driver method
        Scanner in = new Scanner(System.in);

        while (true) { // checking for the condition
            int year = userInteger(\"Enter a year (must be >= 1000): \");
            if (year < 1000) {
                System.out.printf(\"Invalid year.\ \ \");
                continue;
            }

            int month = userInteger(\"Enter a month(must be between 1 and 12): \");
            if (month > 12) {
                System.out.printf(\"Invalid month.\ \ \");
                continue;
            }

            int day = userInteger(\"Enter a day: \");

            int result = daysPassed(year, month, day);
            System.out.printf(\"%d day(s) have passed from 12/31/999 to %d/%d/%d.\ \",
                    result, month, day, year); // print the result

            String day_name = dayOfWeek(year, month, day);
            System.out.printf(\"%d/%d/%d is a %s.\ \ \", month, day, year, day_name); // print the result
        }
    }
}


OUTPUT :

Enter a year (must be >= 1000): 2015
Enter a month(must be between 1 and 12): 8
Enter a day: 4
370937 day(s) have passed from 12/31/999 to 8/4/2015.
8/4/2015 is a Tuesday.

Enter a year (must be >= 1000): 1000
Enter a month(must be between 1 and 12): 1
Enter a day: 1
1 day(s) have passed from 12/31/999 to 1/1/1000.
1/1/1000 is a Wednesday.

Enter a year (must be >= 1000): q
Exiting...


Hope this is helpful.

Do NOT modify the MAINSolutionPlease follow the code and comments for description : CODE : import java.util.Scanner; // required imports public class DayOfWeek
Do NOT modify the MAINSolutionPlease follow the code and comments for description : CODE : import java.util.Scanner; // required imports public class DayOfWeek
Do NOT modify the MAINSolutionPlease follow the code and comments for description : CODE : import java.util.Scanner; // required imports public class DayOfWeek

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site