Thursday, March 12, 2020

BlueJ Program On dd mm yyyy Format Date And Validity Checking


Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is valid or not. If it is valid, display “VALID DATE”, also compute and display the day number of the year for the date of birth. If it is invalid, display “INVALID DATE” and then terminate the program.


Testing of the program


Input: Enter your date of birth in dd mm yyyy format

05
01
2010

Output: VALID DATE
5

Input: Enter your date of birth in dd mm yyyy format

03
04
2010

Output: VALID DATE
93


Input: Enter your date of birth in dd mm yyyy format

34
06
2010

Output: INVALID DATE


Codes of this BlueJ program on displaying number of days

import java.io.*;
class Program1

{
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int d,m,y;
public void takeDate() throws Exception
{
System.out.println("Enter your date of birth in dd mm yyyy format:");
d=Integer.parseInt(br.readLine());
m=Integer.parseInt(br.readLine());
y=Integer.parseInt(br.readLine());
if(m<=0 || m>12)
{
System.out.println("INVALID MONTH");
return;
}
else if(d<=0 || d>12)
{
System.out.println("INVALID DATE");
return;
}
y=validDate(d,m,y);
if(y>0)
{
System.out.println(" VALID DATE\n"+y);
}
else
{
System.out.println(" INVALID DATE");
return;
}
}
int validDate(int d1,int m1,int y1)
{

int li,i;
li=leap(y1);
if(d1>days[m1-1])
return 0;
else
{
for(i=0;i< m1-1;i++)
{
d1=d1+days[i];
}
d1=d1+li;
return d1;

}
}
int leap(int y)
{
if(y%100==0 && y%400==0)
return 1;
else if(y%100!=0 && y%4==0)
return 1;
else
return 0;
}
public static void main(String args[]) throws Exception
{
Program1 ob=new Program1();
ob.takeDate();
}
}

Back To 2011 Computer Practical Paper: CLICK

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner