Create a class called Date that includes three pieces of information as instance variables a month (type int), a day (type int) and a year (type int). Your class should have 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 application named DateTest that demonstrates class Date's capabilities.
Updated: June 4, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
Difficulty: Advance.
Companies who previously asked this: -.
Objective: Create a class called Date
that includes three pieces of information as instance variables a month
(type int
), a day
(type int
) and a year
(type int
). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a setter and a getter method for each instance variable. Provide a method displayDate
that displays the month, day and year separated by forward slashes (/).
Write a test application named DateTest
that demonstrates class Date's capabilities.
Input: Three different values to serve as the day, month, and year.
Expected Output: Displays the month, day and year separated by forward slashes based on the user input.
Sikademy Solution
Date.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class holds the information about date. It has setter and getter methods to set and get the instance variables.
public class Date {
private int day;
private int month;
private int year;
// This parameterized constructor is used to set the values of instance variables
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
// This method is used to display the date, separated by /
public void displayDate() {
System.out.println("The current date is: \t" + year + "/" + month + "/0" + day);
}
// This method is used to get the value of variable day
public int getDay() {
return day;
}
// This method is used to set the value of variable day
public void setDay(int day) {
this.day = day;
}
// This method is used to get the value of variable month
public int getMonth() {
return month;
}
// This method is used to set the value of variable month
public void setMonth(int month) {
this.month = month;
}
// This method is used to get the value of variable year
public int getYear() {
return year;
}
// This method is used to set the value of variable year
public void setYear(int year) {
this.year = year;
}
}
DateTest.java
package sikademy;
/**
*
* @author Archangel Macsika
*/
// This class is used to test the Date class
public class DateTest {
public static void main(String[] args) {
Date date=new Date(18,11,2020);
date.displayDate();
date.setDay(25);
date.setMonth(1);
date.setYear(2021);
date.displayDate();
}
}