Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Chethan N V

21BCE0427

Java CAT 1

Create a class Date with member variables day, month and year to store the date as numbers where
the day, month and year should be accessed only through methods. Create default constructor,
constructor which accepts values day, month and year member variables, constructor that will
create a new object from an existing Date object. Write a method that will display point in
DD/MM/YYYY. Write a method validate that will check whether the day is between 1 and 31, month
between 1 and 12, year between 1990 and 2002 inclusive. The method should display the details of
errors in the date value or display that the date is valid. Test the class developed with suitable
instructions.

import java.util.*;

class Date

int day,month,year;

Date() // default constructor

day=0;

month=0;

year=0;

Date(int d, int m, int y) //parameterized constructor

day=d;

month=m;

year=y;

void check() //to check if entered dates are valid


{

if(day>=1&&day<=31)

System.out.println("Day is valid");

else

System.out.println("Day is out of bound");

if(month>=1&&month<=12)

System.out.println("Month is valid");

else

System.out.println("Month is out of bound");

if(year>=1990&&year<=2002)

System.out.println("Year is valid");

else

System.out.println("Year is out of bound");

void display() // to display the date in DD/MM/YYYY order

System.out.println("Date is "+day+"/"+month+"/"+year);

public static void main(String args[]) //main function

int d,m,y;

Scanner sc=new Scanner(System.in); // Scanner class

System.out.print("Enter the day: ");

d=sc.nextInt();// Input date

System.out.print("Enter the month: ");

m=sc.nextInt();// Input month

System.out.print("Enter the year: ");

y=sc.nextInt();// Input Year

Date d1= new Date();//Default constructor(Object 1)


Date d2= new Date(d,m,y);//Parameterized constructor(Object 2)

d2.check();//calling check function

d2.display();//calling display function

Date d3=new Date();

d3=d2;//Copy constructor(Object 3)

System.out.println("Copy constructor Date Check");

d3.check();

System.out.print("Copy constructor ");

d3.display();

Output 1:
Output 2:

Output 3:

You might also like