Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL SPECIMEN QUESTION PAPER 2021 QUESTION 1 : DATE AFTER

Sunday, September 26, 2021

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN QUESTION PAPER 2021 QUESTION 1 : DATE AFTER

 


As per the given criteria we will first check the inputs before passing the variables to the constructor.

do {
           System.out.println("Enter the day number:");
           day = in.nextInt();
           if(day < 1 || day > 366)
                System.out.println("INVALID DAY:");
           System.out.println("Enter the year:");
           yr = in.nextInt();
           if(Integer.toString(yr).length() !=4)
                System.out.println("INVALID YEAR:");
           System.out.println("Enter the value of N:");
           N = in.nextInt();
           if(N < 1 || N > 100)
                System.out.println("INVALID DATA:");
       }while(day < 1 || day > 366 || Integer.toString(yr).length() !=4 ||  N < 1 || N > 100 );
       DaysCalculation obj = new DaysCalculation(dayyrN);

Now the main task of the program is to calculate the date in the format: Day: Month: Year
for given day ( 1<= day <= 366).
To this I have use the function obj.currentDay(); the logic is as follows:
I am assuming that the entered year is a non-leap year, the same goes for the leap-year.
Between to check a year is leap or not I have employed the method
private boolean checkLeap(int year)
which is easy to decode.
Now coming back to the main theme, suppose a user enters 70, 2017 and 50 as the input.
First we need to calculate the date of 70th day of the year 2017 and then the date after 50
days from the current date.
Since 2017 is a non-leap year, the months will have the following days:
private int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
First we have to calculate the month in which the 70th day fall, for this add up the
elements of the array mdays starting from 0th position until the sum exceeds the day number 70.
So in our example, 31+28+31 = 90 > 70
So the 70th day of the year falls in the 3rd month.
This has been implemented by the following snippet of code:
if(checkLeap(year)) {
        sum += ldays[counter];
        whilesum  < dayNumber) {  
            monthCounter++;
            sum +=ldays[counter+1];
            counter++;
        }
    }
    else{
        sum += mdays[counter];
        whilesum  < dayNumber) {  
            monthCounter++;
            sum +=mdays[counter+1];
            counter++;  
        }
    }

Once we get the month, the next task is to calculate the day of the current month.
This is too easy, just subtract the day number ( 70th in our case ) from the
sum of the days of all the previous months.
In our case this will give 70-(31+28) = 11th day of the 3rd month.
    sum = 0;
    if(checkLeap(year))
        for(int i = 0i < monthCounteri++)
            sum +=ldays[i];
    else
        for(int i = 0i < monthCounteri++)
            sum +=mdays[i];
    int fdayNumber;
    fdayNumber = dayNumber
    fdayNumber -= sum;

Note fdayNumber is just a temporary variable to store the value of dayNumber.
After this we just need to print the day and month and year;

Now the next task is to find the day after 50 days, so the logic is as follows:
add the dayNumber and 50 ( 70 + 50 = 120 ) in our case. Check if this value crosses
365 ( for non-leap year), if so, subtract 365 from it and increment the year.
So now we have a day number and year so just call the function discussed above!

public void dateAfter() {
        dayNumber +=dayAfter;
        if(checkLeap(year)) {
            if(dayNumber > 366 ) {
                dayNumber -= 366;
                year++;
                }
            currentDay();   
            }
        else {
            if(dayNumber > 365) {
                dayNumber -= 365;
                year++;
                }
            currentDay();
        }   
    
    }  

JAVA CODE



import java.util.*;
public class ISC2021SpecimenQuestion1 {
    public static void main(String[] args) {
       int day;
       int yr;
       int N;
       Scanner in = new Scanner(System.in);
       do {
           System.out.println("Enter the day number:");
           day = in.nextInt();
           if(day < 1 || day > 366)
                System.out.println("INVALID DAY:");
           System.out.println("Enter the year:");
           yr = in.nextInt();
           if(Integer.toString(yr).length() !=4)
                System.out.println("INVALID YEAR:");
           System.out.println("Enter the value of N:");
           N = in.nextInt();
           if(N < 1 || N > 100)
                System.out.println("INVALID DATA:");
       }while(day < 1 || day > 366 || Integer.toString(yr).length() !=4 ||  N < 1 || N > 100 );
       DaysCalculation obj = new DaysCalculation(dayyrN);
       System.out.println("OUTPUT:");
       obj.currentDay();
       System.out.println("DATE AFTER "+N+" DAYS");
       obj.dateAfter();
       in.close();
    }  
}
class DaysCalculation {
    public void currentDay() {
    int counter = 0;
    int monthCounter = 0;
    int sum =0;
    if(checkLeap(year)) {
        sum += ldays[counter];
        whilesum  < dayNumber) {  
            monthCounter++;
            sum +=ldays[counter+1];
            counter++;
        }
    }
    else{
        sum += mdays[counter];
        whilesum  < dayNumber) {  
            monthCounter++;
            sum +=mdays[counter+1];
            counter++;  
        }
    }
    sum = 0;
    if(checkLeap(year))
        for(int i = 0i < monthCounteri++)
            sum +=ldays[i];
    else
        for(int i = 0i < monthCounteri++)
            sum +=mdays[i];
    int fdayNumber;
    fdayNumber = dayNumber
    fdayNumber -= sum;
    String s = fdayNumber%10 == 1 ? "st" : fdayNumber%10 == 2 ? "nd" : fdayNumber%10 == 3 ? "rd" : "th";
    if(fdayNumber >= 11 && fdayNumber <= 19)
        s"th";
    System.out.println(fdayNumber+s+" "+months[monthCounter]+" "+year); 
    }
    public void dateAfter() {
        dayNumber +=dayAfter;
        if(checkLeap(year)) {
            if(dayNumber > 366 ) {
                dayNumber -= 366;
                year++;
                }
            currentDay();   
            }
        else {
            if(dayNumber > 365) {
                dayNumber -= 365;
                year++;
                }
            currentDay();
        }   
    
    }  
    private boolean checkLeap(int year) {
        if(year%400==0)
           leap=true;
        else if (year%100==0)
           leap=false;
        else if (year%4==0)
           leap=true;
        else
           leap=false;
        return leap;
    }   
    DaysCalculation(int dayint yrint N) {
        this.dayNumber = day;
        this.year = yr;
        this.dayAfter =N;
    }
    private boolean leap;  
    private int dayNumber;
    private int year;
    private int dayAfter;
    private String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
    private int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    private int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};  
}


No comments:

Post a Comment