Code with Vinay

ISC practical papers › ISC 2007 › Question 1

Day on a given date

ISC 2007 Computer Science Practical, Question 1 · Date programs

The question

Write a program to accept a date in the string format dd/mm/yyyy and accept the name of the day on 1st of January of the corresponding year. Find the day for the given date.

Example:
INPUT
Date                : 5/7/2001
Day on 1st January  : MONDAY
OUTPUT
Day on 5/7/2001     : THURSDAY

Test run the program on the following inputs.

INPUT  DATE          DAY ON  1ST JANUARY        OUTPUT DAY FOR INPUT DATE
4/9/1998             THURSDAY                   FRIDAY
31/8/1999            FRIDAY                     TUESDAY
6/12/2000            SATURDAY                   WEDNESDAY

The program should include the part for validating the inputs namely the date and the day on 1st January of that year.

Question: ISC 2007 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC20007Q1.java Download GitHub
import java.util.*;

class MyDate {
    private int dd, mm, yyyy,index;
    private String inputDay;
    private static String weekDays[] = { "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY" };
    private static int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public MyDate(String inputDate, String inputDay) {
        String part[] = inputDate.split("/");
        dd = Integer.parseInt(part[0]);
        mm = Integer.parseInt(part[1]);
        yyyy = Integer.parseInt(part[2]);
        this.inputDay = inputDay;
        if (isLeap())
            days[1] = 29;
    }

    public boolean isLeap() {
        int divisor = yyyy % 100 == 0 ? 400 : 4;
        return yyyy % divisor == 0;
    }

    public boolean validate() {
        index = -1;
        for (int i = 0; i < weekDays.length; i++) {
            if (inputDay.equals(weekDays[i])) {
                index = i;
                break;
            }
        }
        if (index == -1)
            return false; // day invalid
        if (yyyy < 0)
            return false; // checking for -ve years
        if (mm < 1 || mm > 12)
            return false; // month must be between 1-12 (inclusive)
        if( dd<1 || dd>days[mm-1]) return false; // number of day check
        return true;
    }
    public int getDayNumber(){
        int dayNumber=dd;
        for( int i=0; i<mm-1; i++){
            dayNumber+=days[i];
        }
        return dayNumber;
    }
    private int getDayIndex(){
        return ((getDayNumber()-1+index)%7);
    }
    public String toString(){
        return weekDays[getDayIndex()];
    }
}

class ISC2007Q1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("INPUT");
        System.out.print("Date\t\t\t\t: ");
        String inputDate = in.nextLine();
        System.out.print("Day on 1st January\t\t: ");
        String inputDay = in.nextLine();
        MyDate dt = new MyDate(inputDate, inputDay);
        if (dt.validate()) {
            System.out.println("OUTPUT:\nDAY ON "+inputDate+"\t\t: "+dt);
        } else {
            System.out.println("INVALID DATE/DAY");
        }
        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.

Get the free PDF ₹0 ISC 2027 mock papers