Write a Java program to do the following: Specifics: Create a class called Date
ID: 669455 • Letter: W
Question
Write a Java program to do the following:
Specifics: Create a class called Date that includes three instance variables—a month, a day, and a year.
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 in-stance variable.
Create two date objects birthdate and hiredate and display each object’s data.
Provide a method displayDate that displays the month, day and year separated by for-ward slashes (/).
Then change the hiredate and display both dates again.
Don’t forget to create the application/project named DateTest that demonstrates class Date’s capabilities.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
class Date
{
int year,month,day;
public Ideone(int year,int month,int day)
{
this.year = year;
this.month = month;
this.day = day;
}
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 int getDay()
{
return this.day;
}
public int getMonth()
{
return this.month;
}
public int getYear()
{
return this.year;
}
public void displayData()
{
System.out.println(this.month+"/"+this.day+"/"+this.year);
}
public static void main (String[] args) throws java.lang.Exception
{
Date birthdate = new Ideone(1990,10,10);
Date hiredate = new Ideone(2014,7,7);
birthdate.displayData();
hiredate.displayData();
hiredate.setDay(20);
hiredate.displayData();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.