QuestionQuestion

Transcribed TextTranscribed Text

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, a. Subtract the earlier year from the later year to get the year difference. b. Subtract the month associated with the earlier year from the month associated with the later year. The month difference can be negative (see examples in the table below). 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. Hint: It is important to store the month as a String value and as an int value (1 for January, 2 for February …). The String value is used for month checking; the int value is used for calculating a date difference. if (firstMonth.equalsIgnoreCase(“January”)){ firstMonthNumber = 1; }else if(firstMonth.equalsIgnoreCase(“February”)){ firstMonthNumber = 2; } … else{ … } No. Dates Subtracting months and years Real Date Difference 1 August, 2011 March, 1999 12 years and 5 months These dates are 12 years and 5 months apart. 2 June, 1999 April, 2002 3 years and -2 months These dates are 2 years and 10 months apart. 3 July, 1998 July, 1995 3 years and 0 months These dates are 3 years and 0 months apart. 4 March, 1995 July, 1995 0 year and 4 months These dates are 0 years and 4 months apart. 5 December, 1998 December, 1998 0 year and 0 month These dates are 0 years and 0 months apart. Sample Run 1: For the first date, Enter month: August Enter year: 2011 For the second date, Enter month: March Enter year: 1999 These dates are 12 years and 5 months apart. Sample Run 2: For the first date, Enter month: June Enter year: 1999 For the second date, Enter month: April Enter year: 2002 These dates are 2 years and 10 months apart. Sample Run 3: For the first date, Enter month: July Enter year: 1998 For the second date, Enter month: July Enter year: 1995 These dates are 3 years and 0 months apart. Sample Run 4: For the first date, Enter month: March Enter year: 1995 For the second date, Enter month: July Enter year: 1995 These dates are 0 years and 4 months apart. Sample Run 5: For the first date, Enter month: December Enter year: 1998 For the second date, Enter month: December Enter year: 1998 These dates are 0 years and 0 months apart.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class DateDifferenceCalculator {

    /*
    * Helper method which converts a month string to a month integer
    * @param monthString string of month
    * @returns int value of month
    */
    public static int getMonthFromString(String monthString) {
       // Series of if ... else if .. statements to convert string to int
       if (monthString.equalsIgnoreCase("January"))
            return 1;
       else if (monthString.equalsIgnoreCase("February"))
            return 2;
       else if (monthString.equalsIgnoreCase("March"))
            return 3;
       else if (monthString.equalsIgnoreCase("April"))
            return 4;
       else if (monthString.equalsIgnoreCase("May"))
            return 5;
       else if (monthString.equalsIgnoreCase("June"))
            return 6;
       else if (monthString.equalsIgnoreCase("July"))
            return 7;
       else if (monthString.equalsIgnoreCase("August"))
            return 8;
       else if (monthString.equalsIgnoreCase("September"))
            return 9;
       else if (monthString.equalsIgnoreCase("October"))
            return 10;
       else if (monthString.equalsIgnoreCase("November"))
            return 11;
       else if (monthString.equalsIgnoreCase("December"))
            return 12;
       // If no valid month is specified, return 0
       else
            return 0;
    }...

By purchasing this solution you'll be able to access the following files:
Solution.java and SolutionAlgorithm.txt.

50% discount

Hours
Minutes
Seconds
$45.00 $22.50
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats