Space for google add

Define a class MyDate (day, month, year) with methods to accept and display a MyDate object.





 Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid.
Examples of invalid dates : 12 15 2015, 31 6 1990, 29 2 2001

date.java

import java .io.*;

class InvalidDateException extends Exception

{}

class MyDate

{

int day,mon,yr;

void accept(int d,int m,int y)

{

day=d;mon=m; yr=y;

}

void display()

{

SOP("Date is valid : "+day+"/"+mon+"/"+yr);

}

}

class date

{

main ()

{

SOP("Enter Date : dd mm yyyy ");

int day=Integer.parseInt(br.readLine());

int mon=Integer.parseInt(br.readLine());

int yr=Integer.parseInt(br.readLine());

int flag=0;

try

{

if(mon<=0 || mon>12)

throw new InvalidDateException();

else

{

if(mon==1 || mon==3 || mon==5 || mon==7 ||

mon==8 || mon==10 || mon == 12)

{

if(day>=1 && day <=31)

flag=1;

else

throw new InvalidDateException();

}

else if (mon==2)

{

if(yr%4==0)

{

if(day>=1 && day<=29)

flag=1;

else throw new

InvalidDateException();

}

else

{

if(day>=1 && day<=28)

flag=1;

else throw new

InvalidDateException();

}

}

else

{

if(mon==4 || mon == 6 || mon== 9 ||

mon==11)

{

if(day>=1 && day <=30)

flag=1;

else throw new

InvalidDateException();

}

}

}

if(flag== 1)

{

MyDate dt = new MyDate();

dt.accept(day,mon,yr);

dt.display();

}

}

catch (InvalidDateException mm)

{

    SOP("Invalid Date");

}

}

}


Post a Comment

0 Comments