You are on page 1of 2

Q.3.

15 (Date Class) Create a class called Date that includes three instance variables—a month (type
int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance
variables and assumes that the values provided are correct. Provide a set and a get method for each
instance variable. Provide a method displayDate that displays the month, day and year separated by
forward slashes (/). Write a test application named DateTest that demonstrates class Date’s capabilities.

public class Date {


private int day;
private int month;
private int year;

public Date(int m, int d, int y)


{
day = d;
month = m;
year = y;
}

//set method of day


public void setDay(int d)
{
day = d;
}

//set method of month


public void setMonth(int m)
{
month = m;
}

//set method of year


public void setYear(int y)
{
year = y;
}

//get method of day


public int getDay()
{
return day;
}

//get method of month


public int getMonth()
{
return month;
}

//get method of year


public int getYear()
{
return year;
}
public void DisplayDate()
{
System.out.println(" "+month+"/"+day+"/"+year);
System.out.println("");
}
}

import java.util.Scanner;
public class DateTest {
public static void main(String[] args) {
int d,m,y;
Scanner sc = new Scanner(System.in);

Date dt = new Date(10, 21, 2019);

System.out.print("Current Date is : ");dt.DisplayDate();

System.out.print("Enter Month MM : ");


m = sc.nextInt();
dt.setMonth(m);
System.out.print("Enter Day DD : ");
d = sc.nextInt();
dt.setDay(d);
System.out.print("Enter Year YYYY : ");
y = sc.nextInt();
dt.setYear(y);
System.out.println("");
System.out.println("");
System.out.print("New Date is : "+dt.getMonth()+"/"+dt.getDay()+"/"+dt.getYear());
System.out.println("");
}
}

You might also like