Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Calculating number of dates and finding the next day Write a program that accept

ID: 3677239 • Letter: C

Question

Calculating number of dates and finding the next day

Write a program that accepts a date as input, for example:

Day: 28

Month: 2

Year: 2016

The program must do the followings:

Given the date, compute and print how many days have passed in that year.

Compute and print the date corresponding to the next day.

For example, for the input given above

The output should be:

58 days have passed since the beginning of the year.

The next day is February 29, 2016

Another example:

If the input are:

Day: 31

Month: 12

Year: 2015

The output should be:

364 days have passed since the beginning of the year.

The next day is January 1, 2016

Notes:

Your program should create and use the following or similar methods:

isInputValid (return true if all the input values are valid, otherwise return false)

getMonthName

isLeapYear

getDaysInMonth

calculateTotalDaysPassed

printNextDay

You can also have other methods if necessary. Your program must be tested for many different dates, for example Feb. 28th of a leap year, Feb. 28th of a non-leap year, Dec. 31st of a year, etc.

Explanation / Answer

/**
* The java program Main method that prompts day,
* month and year and prints the number of days elapsed
* and next day*/
//Main.java
import java.util.Scanner;
public class Main
{
   // Main method
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);

       System.out.print("Enter a day: ");
       int day = input.nextInt();

       System.out.print("Enter a month: ");
       int month = input.nextInt();

       System.out.print("Enter a year: ");
       int year = input.nextInt();

       //calculate calculateTotalDaysPassed
       int daysPassed=calculateTotalDaysPassed(month,year,day);

       System.out.println(daysPassed+" days have passed since the beginning of the year");

       //calculate getNextDay
       String nextDay=getNextDay(month,year,day);


       System.out.println("The next day is "+nextDay);
   }

   /**The method getNextDay that takes month, year and day and
   * returns the next day of the date*/
   private static String getNextDay(int month, int year, int day) {

       String nextDayString="";
       int nextDay=0;
       int nextMonth=0;
       int nextYear=0;

       //Check if year is leap year and month is 2 and days=28
       if(isLeapYear(year)&& month==2 && day==28)
       {
           //set next day as day+1
           nextDay=day+1;
           nextMonth=month;
       }
       else if(!isLeapYear(year)&& month==2 && day==28)
       {
           //set next day as day+1
           nextDay=1;
           nextMonth=month+1;
       }
       else if(isLeapYear(year)&& (month==2 && day==29))
       {
           //set next day as day+1
           nextDay=1;
           nextMonth=month+1;
       }
       //check if year is leap year and day is 31 and month is 12
       else if(isLeapYear(year) && day==31 &&month==12)
       {
           //set nextday=1
           nextDay=1;
           nextMonth=1;
       }
       //Check if year is not leap year and day is 30 and month is 12
       else if(!isLeapYear(year) && day==30 && month==12)
       {
           //set nextday=1
           nextDay=day+1;
           nextMonth=month;
           nextYear=year;
       }
       else if(!isLeapYear(year) && day==31 && month==12)
       {
           //set nextday=1
           nextDay=1;
           nextMonth=1;
       }
       else
       {
           //otherwise set nextday as 1
           nextDay=1;

          //set nextMonth as month+1
           nextMonth=month+1;
       }

              
       if(day==31 && month==12)
           nextYear=year+1;
       else
           nextYear=year;

       nextDayString=getMonthName(nextMonth)+" "+nextDay+","+nextYear;

       return nextDayString;
   }

   /**The method calculateTotalDaysPassed that takes month, year and day
   * and returns the number of days passed.*/
   private static int calculateTotalDaysPassed(int month, int year,int day)
   {
       int daysPassed=0;

       for (int m = 1; m <month ; m++) {
           daysPassed+=getDaysInMonth(m, year);
       }

       return daysPassed+day-1;
   }

   /**The method getDaysInMonth that takes month and year
   * and returns the number of days in month*/
   public static int getDaysInMonth(int month, int year)
   {
       switch(month)
       {
       case 1: return 31;

       case 2:
           if (isLeapYear(year))
               return 29;
           else
               return 28;
       case 3: return 31;
       case 4: return 30;
       case 5: return 31;
       case 6: return 30;
       case 7: return 31;
       case 8: return 31;
       case 9: return 30;
       case 10: return 31;
       case 11: return 30;
       case 12: return 31;
       default: return 0;
       }
   }

   // Check if leap year
   public static boolean isLeapYear(int year)
   {
       boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

       if (isLeapYear)
           return true;
       else
           return false;
   }

   /**The method getMonthName that takes month as argument and
   * returns the name of the string*/
   public static String getMonthName(int month)
   {
       switch(month)
       {
       case 1: return "January";

       case 2: return "February";

       case 3: return "March";

       case 4: return "April";

       case 5: return "May";

       case 6: return "June";

       case 7: return "July";

       case 8: return "August";

       case 9: return "September";

       case 10: return "October";

       case 11: return "November";

       case 12: return "December";

       default: return "Error: Invalid Month";
       }
   }

}

sample output:

Enter a day: 31
Enter a month: 12
Enter a year: 2015
364 days have passed since the beginning of the year
The next day is January 1,2016

Run2:

Enter a day: 29
Enter a month: 2
Enter a year: 2016
59 days have passed since the beginning of the year
The next day is March 1,2016


Run3:

Enter a day: 30
Enter a month: 12
Enter a year: 2015
363 days have passed since the beginning of the year
The next day is December 31,2015

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote