0% found this document useful (0 votes)
90 views2 pages

Q.3.15 (Date Class) Create A Class Called Date That Includes Three Instance Variables-A Month (Type

The document describes a Date class that includes instance variables for month, day, and year. It includes a constructor to initialize the variables, get and set methods for each variable, and a displayDate method to output the date separated by slashes. It also includes a DateTest class that demonstrates using the Date class by creating a Date object, displaying the current date, allowing the user to modify the date fields, and displaying the new date.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views2 pages

Q.3.15 (Date Class) Create A Class Called Date That Includes Three Instance Variables-A Month (Type

The document describes a Date class that includes instance variables for month, day, and year. It includes a constructor to initialize the variables, get and set methods for each variable, and a displayDate method to output the date separated by slashes. It also includes a DateTest class that demonstrates using the Date class by creating a Date object, displaying the current date, allowing the user to modify the date fields, and displaying the new date.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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