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

Written in Java Eclipse. Java compiler requires all local variables are initiali

ID: 3733385 • Letter: W

Question

Written in Java Eclipse. Java compiler requires all local variables are initialized all the time before they can be used. In this project, in addition to storing first month and second month as strings, you will need two variables to store the two months as int values so that months can be subtracted. For Example,

int firstMonthNumber, secongMonthNumber;

These two variables will be changed to numbers corresponding to the months users enter. For example,

if( firstMonth.equalIgnoreCase(“January“){ firstMonthNumber =1;}

Because the assignment is a conditional statement, firstMonthNumber is initialized conditionally. And then Java compiler will complain that variable firstMonthNumber may not have been initialized. To fix this, initialize firstMonthNumber to be 0, a default int value. For example,

int firstMonthNumber =0;

Show transcribed image text

iles to be submitte . An algorithm - a regular text file Java Source code a Java source file Supporting files if any Write a program to prompt the user for 2 dates consisting of a month and a year and display the number of years and months between these 2 dates. Either date could be the earlier date. It is REQUIRED to use the following solution to calculate the difference between two dates that are not in the same year 1. To calculate a positive year difference and the corresponding month difference, Subtract the earlier year from the later year to get the year difference. Subtract the month associated with the earlier year from the month associated with the later year a. b. The month difference can be negative (see examples in the table below) No. Dates Real Date Difference These dates are 12 years and 5 months apart These dates are 2 years and 10 months apart These dates are 3 years and 0 months apart These dates are 0 years and 4 months apart These dates are 0 years and 0 months apart Subtracting months and years August, 2011 March,1999 June, 1999 ril, 2002 July, 1998 July, 1995 March, 1995 July, 1995 5 December, 1998 December, 1998 12 years and 5 months 3 years and -2 months 3 years and 0 months 0 year and 4 months 0 year and 0 month In sample 2, the month difference is negative. The difference, 3 years and -2 months, means it would be 3-year difference if this were 2 months later. A date difference should not contain a negative month difference. A negative can be adjusted by regrouping one year difference into 12 month difference 2. To calculate a date difference between two dates that are in the same year, the year difference is always zero. The month difference should be calculated as a non-negative value

Explanation / Answer

package January;

import java.util.*;

import java.util.Date;

// Class DateDifference definition

public class DateDifference

{

// Static method to calculate month and year difference and return it in string format

static String getMonthYeaarDifference(Date start, Date end)

{

// Creates two instance of Calendar

Calendar fromDate=Calendar.getInstance();

Calendar toDate=Calendar.getInstance();

// Sets the date received as parameter to Calendar object

fromDate.setTime(start);

toDate.setTime(end);

int increment = 0;

// To store number of years and months

int year, month;

  

// Checks if the greater month and sets it increment

if (fromDate.get(Calendar.DAY_OF_MONTH) > toDate.get(Calendar.DAY_OF_MONTH))

increment = fromDate.getActualMaximum(Calendar.DAY_OF_MONTH);

// Months difference calculation

// Checks the from date added with increment is greater than the to date months

if ((fromDate.get(Calendar.MONTH) + increment) > toDate.get(Calendar.MONTH))

{

// Gets the difference of months by adding increment to from date month

month = (toDate.get(Calendar.MONTH) + 12) - (fromDate.get(Calendar.MONTH) + increment);

// Increment is set to one

increment = 1;

}// End of if condition

// Otherwise

else

{

// Gets the difference of months by adding increment to from date month

month = (toDate.get(Calendar.MONTH)) - (fromDate.get(Calendar.MONTH) + increment);

// Increment is set to one

increment = 0;

}// End of else

// Year difference calculation

// Checks for the biggest year

if(toDate.get(Calendar.YEAR) > fromDate.get(Calendar.YEAR))

year = toDate.get(Calendar.YEAR) - (fromDate.get(Calendar.YEAR) + increment);

else

year = fromDate.get(Calendar.YEAR) - (toDate.get(Calendar.YEAR) + increment);

// Returns the result

return " The dates are " + year + " years and " + month + " months apart ";

}// End of method

// Static method to receive a month name and returns the month number if it is valid

// Otherwise return zero

static int convertMonthNumber(String month)

{

// Checks if the received month is "January" then return one

if(month.equalsIgnoreCase("January"))

return 1;

// Otherwise checks if the received month is "February" then return two

else if(month.equalsIgnoreCase("February"))

return 2;

// Otherwise checks if the received month is "March" then return three

else if(month.equalsIgnoreCase("March"))

return 3;

// Otherwise checks if the received month is "April" then return four

else if(month.equalsIgnoreCase("April"))

return 4;

// Otherwise checks if the received month is "May" then return five

else if(month.equalsIgnoreCase("May"))

return 5;

// Otherwise checks if the received month is "June" then return six

else if(month.equalsIgnoreCase("June"))

return 6;

// Otherwise checks if the received month is "July" then return seven

else if(month.equalsIgnoreCase("July"))

return 7;

// Otherwise checks if the received month is "August" then return eight

else if(month.equalsIgnoreCase("August"))

return 8;

// Otherwise checks if the received month is "September" then return nine

else if(month.equalsIgnoreCase("September"))

return 9;

// Otherwise checks if the received month is "October" then return ten

else if(month.equalsIgnoreCase("October"))

return 10;

// Otherwise checks if the received month is "November" then return eleven

else if(month.equalsIgnoreCase("November"))

return 11;

// Otherwise checks if the received month is "December" then return twelve

else if(month.equalsIgnoreCase("December"))

return 12;

// Returns zero for invalid month name

return 0;

}// End of method

// main method definition

public static void main(String[] ss)

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// To store month names

String firstMonth = "";

String secondMonth = "";

// To store month numbers

int firstMonthNumber, secondMonthNumber;

// To store years

int firstYear, secondYear;

// Loops till first date valid month entered

do

{

// Accepts first date month name

System.out.print(" Enter the first date month name: ");

firstMonth = sc.next();

// Calls the method to convert the month name to month number

firstMonthNumber = convertMonthNumber(firstMonth);

// Checks if month number is not zero then come out of the loop

if(firstMonthNumber != 0)

break;

// Otherwise, display error message and asks to re enter

else

System.out.println("Invalid Month name. Tyr again. ");

}while(true);// End of do while loop

// Accepts the first date year

System.out.print(" Enter the first date year: ");

firstYear = sc.nextInt();

// Loops till second valid month entered

do

{

// Accepts second date month name

System.out.print(" Enter the second date month name: ");

secondMonth = sc.next();

// Calls the method to convert the month name to month number

secondMonthNumber = convertMonthNumber(secondMonth);

// Checks if month number is not zero then come out of the loop

if(secondMonthNumber != 0)

break;

// Otherwise, display error message and asks to re enter

else

System.out.println("Invalid Month name. Tyr again. ");

}while(true); // End of do while loop

// Accepts the second date year

System.out.print(" Enter the second date year: ");

secondYear = sc.nextInt();

// Creates two instances of Calendar class

Calendar first = Calendar.getInstance();

Calendar second = Calendar.getInstance();

// Sets the month and year

first.set(firstYear, firstMonthNumber, 3);

second.set(secondYear, secondMonthNumber, 3);

// Calls the method to display the difference month and year

System.out.println(getMonthYeaarDifference(first.getTime(), second.getTime()));

}// End of main method

}// End of class

Sample Output 1:

Enter the first date month name: Mune
Invalid Month name.
Tyr again.

Enter the first date month name: June

Enter the first date year: 1999

Enter the second date month name: april

Enter the second date year: 2002

The dates are 2 years and 10 months apart

Sample Output 2:


Enter the first date month name: juls
Invalid Month name.
Tyr again.

Enter the first date month name: july

Enter the first date year: 1998

Enter the second date month name: july

Enter the second date year: 1995

The dates are 3 years and 0 months apart