public class Date
{
private int month;
private int day;
private int year;
public Date( int monthValue, int dayValue, int yearValue )
{
month = monthValue;
day = dayValue;
year = yearValue;
}
public void setMonth( int monthValue )
{
month = monthValue;
}
public int getMonth()
{
return month;
}
public void setDay( int dayValue )
{
day = dayValue;
}
public int getDay()
{
return day;
}
public void setYear( int yearValue )
{
year = yearValue;
}
public int getYear()
{
return year;
}
public void displayDate()
{
System.out.printf( "%d/%d/%d", getMonth(), getDay(), getYear() ); } }
Problem: Given the above Date class, write a Test Program to test the class. The program should initialize data fields of two objects, first using Constructor and second using Set methods. Using DisplayDate() Method, print object fields.
public class Date { private int month; private int day; private int year; public Date(int monthValue, int dayValue, int yearValue) { month = monthValue; day = dayValue; year = yearValue; } public void setMonth(int monthValue) { month = monthValue; } public int getMonth() { return month; } public void setDay(int dayValue) { day = dayValue; } public int getDay() { return day; } public void setYear(int yearValue) { year = yearValue; } public int getYear() { return year; } public void displayDate() { System.out.printf("%d/%d/%d", getMonth(), getDay(), getYear()); } } class DateTest { public static void main(String[] args) { Date date1 = new Date(10, 14, 2020); Date date2 = new Date(0, 0, 0); date2.setDay(30); date2.setMonth(7); date2.setYear(2020); date1.displayDate(); System.out.println(); date2.displayDate(); System.out.println(); } }
Get Answers For Free
Most questions answered within 1 hours.