ISC practical papers › ISC 2011 › Question 3
Date Validation
ISC 2011 Computer Science Practical, Question 3 · Date programs
The question
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.
Test your program for the given sample data and some random data.
Example 1 Input: Enter your date of birth in dd mm yyyy format 05 01 2010 Output: VALID DATE 5
Example 2 Input: Enter your date of birth in dd mm yyyy format 03 04 2010 Output: VALID DATE 93
Example 3 Input: Enter your date of birth in dd mm yyyy format 34 06 2010 Output: INVALID DATE
Question: ISC 2011 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class MyDate {
private int dd, mm, yyyy;
private static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public MyDate(int dd, int mm, int yyyy) {
this.dd = dd;
this.mm = mm;
this.yyyy = yyyy;
if (isLeap())
days[1] = 29;
}
public boolean isLeap() {
int divisor = (yyyy % 100 == 0) ? 400 : 4;
return yyyy % divisor == 0;
}
public boolean isValidDate() {
if (yyyy < 0)
return false;
if (mm < 0 || mm > 12)
return false;
if (dd < 1 || dd > days[mm - 1])
return false;
return true;
}
public int getDayNumber() {
int dayNumber = dd;
for (int i = 0; i < mm - 1; i++) {
dayNumber += days[i];
}
return dayNumber;
}
}
class ISC2011Q3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("INPUT:\tEnter your date of birth in dd mm yyyy format");
int dd, mm, yyyy;
System.out.print("\t");
dd = in.nextInt();
System.out.print("\t");
mm = in.nextInt();
System.out.print("\t");
yyyy = in.nextInt();
MyDate obj = new MyDate(dd, mm, yyyy);
if (obj.isValidDate()) {
System.out.println("OUTPUT:\n\tVALID DATE\n\t" + obj.getDayNumber());
} else {
System.out.println("Output:\n\tINVALID DATE");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.