Java Write the class definition for a Date class that contains three integer dat
ID: 3861677 • Letter: J
Question
Java
Write the class definition for a Date class that contains three integer data members: month, day, and year. Include a default constructor that assigns the date 1/1/2000 to any new object that does not receive arguments. Also include a function that displays the Date object. Write a main() function in which you instantiate two Date objects -- one that you create using the default constructor values, and one that you create using three arguments -- and display its values. Save the file as Date.java. You will use the Date class in other assignments later.
Explanation / Answer
Date.java
package test1;
public class Date {
private int day;
private int month;
private int year;
public Date(){
day = 1;
month = 1;
year = 2000;
}
public Date(int day, int month, int year){
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public void display(){
System.out.println("Day: "+day+" Month: "+month+" Year: "+year);
}
public static void main(String arg[]){
Date d1 = new Date();
d1.display();
Date d2 = new Date(2,3,1900);
d2.display();
}
}
Output:
Day: 1 Month: 1 Year: 2000
Day: 2 Month: 3 Year: 1900
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.