( Date Class) Create a class called Date that includes three instance variablesa 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 application named DateTest that demonstrates class Date s
capabilities.
1
Expert's answer
2017-10-13T15:17:07-0400
public class Date {
private final static int DEFAULT_MONTH= 1; private final static int DEFAULT_DAY= 1;
private int month; private int day; private int year;
private boolean checkDay(int day) { return day >= 1 && day <= maxDay(); }
private int maxDay() { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28; default: return 31; } }
public Date(int month, int day, int year) { this.year = year; this.month = checkMonth(month) ? month : DEFAULT_MONTH; this.day = checkDay(day) ? day : DEFAULT_DAY; }
Comments
Leave a comment