Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 7

10 marks

Computer ScienceISC 2020 Computer Science Paper 1ProgramObjects

Write the program described below.

Design a class Convert to find the date and the month from a given day number for a particular…

Design a class Convert to find the date and the month from a given day number for a particular year. Example: If day number is 64 and the year is 2020, then the corresponding date would be: March 4, 2020 i.e. (31 + 29 + 4 = 64) Some of the members of the class are given below: Class name : Convert Data members/instance variables: n : integer to store the day number d : integer to store the day of the month (date) m : integer to store the month y : integer to store the year Methods/Member functions: Convert ( ) : constructor to initialize the data members with legal initial values void accept( ) : to accept the day number and the year void day_to_date( ) : converts the day number to its corresponding date for a particular year and stores the date in ‘d’ and the month in ‘m’ void display( ) : displays the month name, date and year Specify the class Convert giving details of the constructor( ), void accept( ), void day_to_date( ) and void display( ). Define a main( ) function to create an object and call the functions accordingly to enable the task.

Answer

Answer

import java.util.Scanner;

class Convert
{
    int n, d, m, y;

    Convert()
    {
        n = 0;
        d = 0;
        m = 0;
        y = 0;
    }

    void accept()
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter day number: ");
        n = sc.nextInt();
        System.out.print("Enter year: ");
        y = sc.nextInt();
    }

    void day_to_date()
    {
        int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if ((y % 400 == 0) || (y % 100 != 0 && y % 4 == 0))
            days[1] = 29;
        int rem = n;
        m = 0;
        while (m < 12 && rem > days[m])
        {
            rem = rem - days[m];
            m++;
        }
        d = rem;
        m = m + 1;
    }

    void display()
    {
        String mn[] = {"January", "February", "March", "April", "May", "June", "July",
                       "August", "September", "October", "November", "December"};
        System.out.println(mn[m - 1] + " " + d + ", " + y);
    }

    public static void main(String args[])
    {
        Convert ob = new Convert();
        ob.accept();
        ob.day_to_date();
        ob.display();
    }
}
Explanation: The constructor sets all data members to 0. accept() reads the day number and the year. day_to_date() first checks whether the year is a leap year (divisible by 400, or divisible by 4 but not by 100) and sets February to 29 days if so. It then subtracts the days of each month from the day number as long as the remaining number is larger than that month's days; what remains is the date d and the month is m + 1. display() prints the month name, date and year. Tested: for day number 64 and year 2020 the output is March 4, 2020, matching the example (31 + 29 + 4 = 64).