(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 app named DateTest that demonstrates class Date’s capabilities.
Date Class in Java Program
/*
* Filename: Date.java
*
* Description: Exercise 3.15 - Date Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
public class Date{
private int month, day, year;
public Date(int month, int day, int year){
setMonth(month);
setDay(day);
setYear(year);
}
// setters
public void setMonth(int month){
this.month = month;
}
public void setDay(int day){
this.day = day;
}
public void setYear(int year){
this.year = year;
}
// getters
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
// display date
public void displayDate(){
System.out.printf("%d/%d/%d\n", getMonth(), getDay(), getYear());
}
}
DateTest.java
/*
* Filename: DateTest.java
*
* Description: Exercise 3.15 - Date Class
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;
public class DateTest{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Date dt = new Date(requestInput("Enter month: ", sc),
requestInput("Enter day: ", sc),
requestInput("Enter year: ", sc));
dt.displayDate();
}
public static int requestInput(String s, Scanner sc){
System.out.print(s);
return sc.nextInt();
}
}