Sei sulla pagina 1di 4

Algorithm for Datediffernece(Question 10)

Create a class Date_Difference() wherein you declare class BufferedReader and also take an array of int
dataype of name month storing the number of days in each moth i.e. from January to February.

Note: The no. of days in February is taken as 28.

Objective of 1st function : To check whether year is leap or not by taking a parameter y as the year and
returning no. of days in February month as per the validity of the y passed

isLeap(y)

Step 1: Start of algorithm

Step 2: if(y mod 400=0) or ((y mod 100 not equal to 0 ) and (y mod 4=0))THEN

Step 2.1: return 29

Else

Step 2.2: return 28

[End of if structure]

Step 3:End of algorithm

Objective of 2nd function :To check the validity of the date by taking date in dd , mm , yyyy format as a
parameter and returning true or false as per is valid

dateValidate(d,m,y)

Step 1: month[2]←isLeap(y)

Step 2: if(m<1 or m>12 or d<1 or d>month[m] or y<1000 or y>9999)THEN

Step 2.1: return false

Else

Step 2.2: return true

[End of if structure]

Step 3:End of algorithm

Objective of 3rd function :Function to return the day number from the year by taking date , month , year
as the parameters.

Dayno(d,m,y)
Step 1: dn←0

Step 2:month[2]=isLeap(y)

Step 3: i←1

Step 4:Repeat step 5 to 6 till i<m

Step 5: dn←dn+month[i]

Step 6:i←i+1

[End of loop structure]

Step 7:dn←dn+d

Step 8: I←1000

Step 9: Repeat steps 10 to till I<y

Step 10: if(isLeap(I)=29)THEN

Step 10.1: dn←dn+366

Else

Step 10.2:dn←dn+365

[End of if structure]

Step 11:I←I+1

[End of loop structure]

Step 12:return dn

Step 13:End of algorithm

Main function : To call the functions and perform the deemed prerequisites

Public static void main(String args[])throws IOException

Step 1: Start of algorithm

Step 2: Create an object ob of class Date_Difference

Step 3: D← “Enter the 1st date in (dd/mm/yyyy) format”

Step 4: date1 is initialized and spaces are trimmed.


Step 5:p←0 , q←0, d1←0 , m1←0 , y1←0 , d2←0 , m2←0, y2←0

Step 6: p←stores index position of “/” in date 1.

Step 7: d1←extracts string date1 from 0 to p and converts it to integer.

Step 7: q←stores index position of last occurrence of “/” in string date1.

Step 8:m1← extracts string date1 from p+1 to q and converts it to integer.

Step 9: y1← extracts string date1 from q+1 to end and converts it to integer.

Step 10: String date2←”25/12/”+extracted string date1 from p+1 till end

Step 11: p← stores index position of “/” in date 2.

Step 12: d2← extracts string date 2 from 0 to p and converts it to integer.

Step 13: q← stores index position of last occurrence of “/” in string date2.

Step 14: m2← extracts string date2 from p+1 to q and converts it to integer.

Step 15: y2← extracts string date2 from q+1 to end and converts it to integer.

Step 16:t←0,w←0,a←0,b←0

Step 17.1: if(d1,m1 and y1 are passed as parameters to dateValidate()=true and d2,m2 and y2 are
passed as parameters to dateValidate()=true)THEN

Step 17.1.1: if(d1>25 and m1==12)THEN

Step 17.1.1.1:if(y1 is passed as parameter to function isLeap=29)THEN

Step 17.1.1.1.1: t←366-(d1-25)

Step 17.1.1.1.2: D← “Days left until Christmas”

[End of if structure]

Else

Step 17.1.1.2: w←365-(d1-25)

Step 17.1.1.2.1: D←”Days left until Christmas”

[End of if structure]

Else

Step 17.1.2: a← d1,m1 and y1 are passed as parameters to dayno()


Step 17.1.2.1: b←d2,m2 and y2 are passed as parameters to dayno()

Step 17.1.2.1.2: D←”Output”+Math.abs(a-b)+”Days left until Christmas”)

[End of if structure]

Else

Step 17.2: D←”Invalid date , either syntactically or you haven’t entered the current year number”)

[End of if structure]

Step 18: End of algorithm

Step 19:End of program

[Program intend closed]

Potrebbero piacerti anche