The Programming Project

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2001 SOLVED QUESTION 3 : DAYS LAPSED

 Input consists of the month number [MM], day of the month [DD] and the year [YYY]. Write a program to calculate and print the corresponding day of the year [in the range of 1 to 366]
Example :

INPUT : Month number  05
               Day 03
               Year 1996 
               OUTPUT :    CORRESPONDING DAY OF THE YEAR IS 124

The problem is quite simple, after taking the input just test whether the year is leap or not:
Following this we will add number of days of each month till the month just before
the input month from the corresponding array of month days for leap no non leap year.
The following code does exactly this
 while(counter < month-1) {
                    days_lapsed += ldays[counter];
                    counter++;
                    }
then we will add days before the input day: for example
if input is 16/5/2019
Since 2019 is non-leap
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
all days of months before the month of May will be added
31+28+31+30 (till april) = 120
then since the day is 16th, 16 more days will be added to 120
to get 120+16=136


import java.util.*;
public class ISC2015Q1 {
    public static void main(String[] args) {
       int day;
             int yr;
             int month;
        Scanner in = new Scanner(System.in);
        DaysCalculation date = new DaysCalculation();
             do {
                    System.out.println("Enter the day (between 1 and 31)");
                    day = in.nextInt();
                    if(day < 1 || day > 31)
                                 System.out.println("INVALID DAY:");
                    }while(day < 1 || day > 31);
             do {
                System.out.println("Enter the month:");
                month = in.nextInt();
                if(month < 1 || month > 12)
                             System.out.println("INVALID MONTH (between 1 and 12):");
                }while(month < 1 || month > 12);
           
             do {
                    System.out.println("Enter the year");
                    yr = in.nextInt();
                    if(Integer.toString(yr).length()!=4)
                                 System.out.println("INVALID YEAR:");
                    }while(Integer.toString(yr).length()!=4);
            
        date.setDate(day,yr,month); 
        System.out.println("OUTPUT:");
        date.daysElapsed();
        in.close();
        }  
    }
class DaysCalculation {
    public void setDate(int day, int yr, int month) {
        this.dayNumber = day;
        this.year = yr;
        this.month = month;
        }
    public void daysElapsed() {
       int counter = 0;
       int days_lapsed=0;
       if(checkLeap(year))     {
             while(counter < month-1) {
                    days_lapsed += ldays[counter];
                    counter++;
                    }
             days_lapsed +=dayNumber;
             }
        else {
             while(counter < month-1) {
                    days_lapsed += mdays[counter];
                    counter++;
                    }
             days_lapsed +=dayNumber;
             }
        System.out.println("Corresponding day of the year:"+days_lapsed);
        }  
    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;
        }   
    //private boolean flag;
    private static boolean leap;  
    private int month;
    private int year;
    private int dayNumber;
    //String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
    int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};  
    }

ISC COMPUTER SCIENCE PRACTICAL 2001 SOLVED QUESTION 2 : MERGE ARRAY


Write a program which input natural number N and M followed by integer arrays A[] and B[], each consisting of N and M number of elements respectively.
 Sort the arrays A[] and B[] in descending order of magnitude.
 Use the sorted arrays to generate a merged array C[]. Array C[] should be generated in descending order.
 Assume that input arrays comprise maximum 20 elements each, with no duplicates.
 Common elements should be included in the merged array only once.
 The output format is as follows:-
 SORTED ARRAYS A[ ] B[ ]
 MERGED ARRAY C[ ] 

The logic is to after taking input, sort both the array A and B using
any standard sorting technique. the add the lements of A in C.
Following this add the elements of B in C while checking whether
it is already present in C or not and increasing the counter or not based on whether 
the element is not present or is present.
Then just sort C to ge the result.

import java.util.*;
public class ISC2001Q2 {
       public static void main(String[] args) {
             int M,N;
             Scanner in = new Scanner (System.in);
             System.out.println("Enter the numebr of elemenst in Array A");
             M = in.nextInt();
             while(M > 20) {
                    System.out.println("INVALID INPUT, TRY AGAIN");
                    M = in.nextInt();
             }
             System.out.println("Enter the numebr of elemenst in Array B");
             N= in.nextInt();
             while(N > 20) {
                    System.out.println("INVALID INPUT, TRY AGAIN");
                    N = in.nextInt();
             }
             MergeArray obj = new MergeArray(M,N);
             obj.inputArray();
             obj.mergeArray();
             in.close();
       }
}
class MergeArray{
       public void mergeArray() {
             sortArray(arrayA,order_A);
             sortArray(arrayB,order_B);
             System.out.println("Array A after sorting:");
             display(arrayA,order_A);
             System.out.println("Array B after sorting:");
             display(arrayB,order_B);
             int counter;
             boolean flag;
             //adding elements of A in C
             for(int i=0;i<order_A;i++)
                    arrayC[i]=arrayA[i];
             counter=order_A;
             // adding elements of B in C
             for(int j=0;j<order_B;j++) {
                    flag = false;
                    for(int k=0;k<order_A;k++) {
                          if(arrayB[j]==arrayC[k]) // checking whether element is already present
                                 flag = true;
                    }
                    if(flag==false)
                          arrayC[counter++]=arrayB[j];
             }
             System.out.println("Array C after merging");
             sortArray(arrayC,counter);
             display(arrayC,counter);
       }
       private void sortArray(int[] arr,int order) {
             int temp;
             for(int i = 0; i < order; i++) {
                for(int j=1; j < (order-i); j++) {
                   if(arr[j-1] < arr[j]) {
                      temp = arr[j-1];
                      arr[j-1] = arr[j];
                      arr[j] = temp;
                   }
                }
             }
       }
       private void display(int[] arr,int order) {
             for(int i=0;i<order;i++)
                    System.out.print(arr[i]+" ");
             System.out.println();
       }
       public void inputArray() {
             int i;
             Scanner in = new Scanner (System.in);
             System.out.println("Enter the elements of array A:");
             for(i=0;i<order_A;i++)
                    arrayA[i]=in.nextInt();
             System.out.println("Enter the elements of array B:");
             for(i=0;i<order_B;i++)
                    arrayB[i]=in.nextInt();
             in.close();
       }
       MergeArray(int M,int N){
       order_A = M;
       order_B = N;
       arrayA = new int[order_A];
       arrayB = new int[order_B];
       arrayC = new int[order_A+order_B];
       }
       private int order_A;
       private int order_B;
       private int[] arrayA;
       private int[] arrayB;
       private int[] arrayC;
}