Q. Create a class called Date having following properties:
a. Three instance variables (integer) – date, month and year
b. Three constructors
i. Date() – initializes to 1 January, 1970
ii. Date(int date, int month) – initializes to specified date and month of 2013.
iii.Date(int date, int month, int year) – initializes to specified date, month and year
c. A method called setDate() to take all the date values from user at runtime d. A get method for each instance variable e. A method to display the date in the format – dd/mm/yyyy Write a JAVA program that will test the Date class.
import java.util.Scanner;
class date
{
private int d,m,y;
date()
{
d=1;
m=1;
y=1970;
}
date(int a,int b)
{
d=a;
m=b;
y=2013;
}
date(int x,int y,int z)
{
d=x;
m=y;
y=z;
}
void setdd()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the date ");
d=sc.nextInt();
System.out.println("enter the month ");
m=sc.nextInt();
System.out.println("enter the year");
y=sc.nextInt();
}
String getdd()
{
return (d+"/"+m+"/"+y);
}
public void dis()
{
System.out.println("date "+d+"/"+m+"/"+y);
}
}
class prog3
{
public static void main(String args[])
{
date ob1=new date();
date ob2=new date(2,3);
ob1.dis();
ob2.dis();
date ob3=new date();
ob3.setdd();
System.out.println("the date is "+ob3.getdd());
}
}
Output:
enter the date: 07
enter the month: 06
enter the year: 2018
date: 1/1/1997
date: 2/3/2016
the date is: 07/06/2018
//if you have any problem, comment below
a. Three instance variables (integer) – date, month and year
b. Three constructors
i. Date() – initializes to 1 January, 1970
ii. Date(int date, int month) – initializes to specified date and month of 2013.
iii.Date(int date, int month, int year) – initializes to specified date, month and year
c. A method called setDate() to take all the date values from user at runtime d. A get method for each instance variable e. A method to display the date in the format – dd/mm/yyyy Write a JAVA program that will test the Date class.
import java.util.Scanner;
class date
{
private int d,m,y;
date()
{
d=1;
m=1;
y=1970;
}
date(int a,int b)
{
d=a;
m=b;
y=2013;
}
date(int x,int y,int z)
{
d=x;
m=y;
y=z;
}
void setdd()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the date ");
d=sc.nextInt();
System.out.println("enter the month ");
m=sc.nextInt();
System.out.println("enter the year");
y=sc.nextInt();
}
String getdd()
{
return (d+"/"+m+"/"+y);
}
public void dis()
{
System.out.println("date "+d+"/"+m+"/"+y);
}
}
class prog3
{
public static void main(String args[])
{
date ob1=new date();
date ob2=new date(2,3);
ob1.dis();
ob2.dis();
date ob3=new date();
ob3.setdd();
System.out.println("the date is "+ob3.getdd());
}
}
Output:
enter the date: 07
enter the month: 06
enter the year: 2018
date: 1/1/1997
date: 2/3/2016
the date is: 07/06/2018
//if you have any problem, comment below
Comments
Post a Comment