Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 2. Create a class called Date that includes three pieces of information

ID: 3715015 • Letter: E

Question

Exercise 2. Create a class called Date that includes three pieces of information as data members-a month (type int), a day (type in have a constructor that initializes the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1-12; if it is not, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (). Write a test program that demonstrates class Date's capabilities ) and a year (type in). Your class should

Explanation / Answer

Solution :
Please find below the Date.java which also has a main() to showcase the Date class capabilities

public class Date {

private int month;
private int day;
private int year;

//paramterized constructor with all 3 params
public Date (int month, int day, int year) {
this.setMonth(month) ;
this.setDay(day);
this.setYear(year);
}

//getters and setters

public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month >= 1 && month <= 12 ? month : 1;
}

public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}

public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}

//display method
public void diaplayDate() {
System.out.println(month + "/" + day + "/" + year) ;
}

//main method that tests Date class
public static void main(String [] args) {
Date date = new Date (10, 2, 1992);
date.displayDate();
System.out.println();
date = new Date (100, 2, 1992);
date.displayDate();
}
}