top of page
Click here to go to the home page of AskTheCode.

Create a Date.java file - OOPs Example in Java | AskTheCode

Team ATC

Updated: Jul 18, 2021

Create a Date.java file using OOPs | Object-Oriented Programming Example | AskTheCode

 

Problem:

Create a new file called Date.java and construct a Date class that models a calendar date with day, month, and year. It contains the following members:

  • 3 private integer instance variables: day, month (1 - 12), and year (4-digit).

  • a default constructor which will set the date to 01/01/1972. When called, this also prints the string "Default Constructor".

  • an overloaded constructor that takes in the day, month, and year as arguments. When called, this also prints the string "Overloaded Constructor".

  • public getters and setters for the private instance variables.

  • public void setDate(int day, int month, int year) - sets the day, month, and year based on the arguments.

  • public String toString() - returns "DD/MM/YYYY", with leading zero for DD and MM if applicable (note: return the string and do not display it from the method itself).

  • public Day dayOfTheWeek() - returns the day of the week (MON, TUE, WED, THU, FRI, SAT, SUN) for the date. Create an enum Day to handle this. Follow the formula below for this.

To get the day of the week, one can make use of the JD (Julian Day). Based on the given date, Y M D, perform the following computations:

If the month is January or February, subtract 1 from the year to get a new Y and add 12 to the month to get a new M.

Dropping the fractional part of all results of all multiplications and divisions, let:

A = Y / 100
B = A / 4
C = 2 - A + B
E = 365.25 x (Y + 4716)
F = 30.6001 x (M + 1)
JD = C + D + E + F - 1524.5

The day of the week is JD % 7 (notice that JD ends in .5. Investigate this properly to get the correct day of the week).

 

Input Format:

The first line contains an integer which is either 1 or 2. If it is a 1, it means to create a Date via the default constructor. If it is a 2, 3 integers follow in the next line representing the day, month, and year of the Date to be created.

This is then followed by a number m representing the number of operations to be performed followed by the operations themselves as specified below:

1 - setDay
2 - setMonth
3 - setYear
4 - setDate
5 - getDay
6 - getMonth
7 - getYear
8 - toString
9 - dayOfTheWeek

For operations 1, 2, 3, one integer follows.

For setDate, 3 integers follow (D M Y).

For the rest of the operations, no other inputs follow.



Output Format:

The default Constructor should output "Default Constructor", and the overloaded constructor should output "Overloaded Constructor". All the setter operations will not output anything. For the getter operations (5 - 7), the values they return must be printed. Operations 8 and 9 should print their outputs as well.

 

Input Sample:

1
7
8
9
1 20
2 8
3 1980
8
9


Output Sample:

Default Constructor
01/01/1972
SAT
20/08/1980
WED

 

Code:

import java.util.Scanner;

enum Day{
    MON, TUE, WED, THU, FRI, SAT, SUN;
}

public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int const_opt = in.nextInt();
        Date date = null;
        if (const_opt == 1)
            date = new Date();
        else if (const_opt == 2)
            date = new Date(in.nextInt(), in.nextInt(), in.nextInt());
        
        int m = in.nextInt();
        
        for (int i=1; i<=m; i++){
            int opt = in.nextInt();
            // 1 - setDay
            if (opt == 1) date.setDay(in.nextInt());
            // 2 - setMonth
            else if (opt == 2) date.setMonth(in.nextInt());
            // 3 - setYear
            else if (opt == 3) date.setYear(in.nextInt());
            // 4 - setDate
            else if (opt == 4) date.setDate(in.nextInt(), in.nextInt(), in.nextInt());
            // 5 - getDay
            else if (opt == 5) println(date.getDay());
            // 6 - getMonth
            else if (opt == 6) println(date.getMonth());
            // 7 - getYear
            else if (opt == 7) println(date.getYear());
            // 8 - toString
            else if (opt == 8) println(date.toString());
            // 9 - dayOfTheWeek
            else if (opt == 9) System.out.println(date.dayOfTheWeek());
        }
    }
    public static void println(String s){
        System.out.println(s);
    }
}

class Date{
    private int day, month, year;
    public Date(){
        System.out.println("Default Constructor");
        setDate(1, 1, 1972);
    }
    public Date(int day, int month, int year){
        System.out.println("Overloaded Constructor");
        setDate(day, month, year);
    }
    public void setDay(int day){
        this.day = day;
    }
    public void setMonth(int month){
        this.month = month;
    }
    public void setYear(int year){
        this.year = year;
    }
    public void setDate(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }
    
    public String getDay(){
        return String.format("%02d", this.day);
    }
    public String getMonth(){
        return String.format("%02d", this.month);
    }
    public String getYear(){
        return String.format("%04d", this.year);
    }
    
    public String toString(){
        return String.format("%02d/%02d/%04d", this.day, this.month, this.year);
    }
    
    public Day dayOfTheWeek(){
        int Y = this.year, M = this.month, D = this.day;
        if (this.month == 1 || this.month == 2){
            --Y;
            M += 12;
        }
        int A = Y / 100;
        int B = A / 4;
        int C = 2 - A + B;
        int E = (int)(365.25 * (Y + 4716));
        int F = (int)(30.6001 * (M + 1));

        int JD = (C + D + E + F - 1524);
        return Day.values()[(JD % 7)];
    }
}

Recent Posts

See All

Comentarios


bottom of page