Date Program ISC Computer Science 2020 Theory

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 dayToDate(): 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 dayToDate() and void display(). Define a main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class Convert{
    int n;
    int d;
    int m;
    int y;
    public Convert(){
        n = 0;
        d = 0;
        m = 0;
        y = 0;
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Day number: ");
        n = Integer.parseInt(in.nextLine());
        System.out.print("Year: ");
        y = Integer.parseInt(in.nextLine());
    }
    public void dayToDate(){
        boolean isLeap = false;
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            isLeap = true;
        int dm[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeap)
            dm[2] = 29;
        int i = 1;
        while(n > dm[i]){
            n -= dm[i];
            i++;
        }
        d = n;
        m = i;
    }
    public void display(){
        String mn[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        System.out.println(mn[m] + " " + d + ", " + y);
    }
    public static void main(String args[]){
        Convert obj = new Convert();
        obj.accept();
        obj.dayToDate();
        obj.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory