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

Files to be submitted: * An algorithm - a regular text file » Java Source code -

ID: 3733333 • Letter: F

Question

Files to be submitted: * 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 vears and 5 months apart. These dates are 2 vears 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 December, 1998 December, 1998 12 years and 5 months 3 vears and-2 months 3 vears and 0 months 2 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

Algorithm

1.Fetch two strings of month,year and store them

2. Parse them as months m1,m2 and years y1,y2

3.Now get the month number from month name

4.Compare years and adjust the larger one in y1,m1 and smaller one in y2,m2

5.Now substract y2 from y1 and m2 from m2

6.Print as it is in one line and for another column if (m1-m2)<0 then take one year as 12 months do calculations..

Here is the code ... Thanks in advance.....


import java.util.Scanner;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author SON OF HEAVEN
*/
public class w2 {
   static int month(String month){
        switch(month){
            case "January":return 1;
            case "February":return 2;
            case "March":return 3;
            case "April":return 4;
            case "May":return 5;
            case "June":return 6;
            case "July":return 7;
            case "August":return 8;
            case "September":return 9;
            case "October":return 10;
            case "November":return 11;
            default:return 12;
        }
    }
    static void difference(int y1,int m1,int y2,int m2){
        System.out.print((y1-y2)+" years and "+(m1-m2)+" months --- ");
        if(m1-m2<0){
            System.out.println("These dates are "+(y1-y2-1)+"apart and " + (12+m1-m2)+ " months apart");
        }
        else
            System.out.println("These dates are "+(y1-y2)+"apart and " + (m1-m2)+ " months apart");
      
          
    }
    public static void main(String ars[])
    {
        String d1,d2;
        Scanner scan=new Scanner(System.in);
        d1=scan.nextLine();
        d2=scan.nextLine();
        String one[]=d1.split(", ");
        String two[]=d2.split(", ");
        int y1=Integer.parseInt(one[1]);
        int y2=Integer.parseInt(two[1]);
        int m1=month(one[0]);
        int m2=month(two[0]);
        if(y1>y2)difference(y1,m1,y2,m2);
        else difference(y2,m2,y1,m1);
    }
}

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