JAVA HELP - Use the following template: Rewrite the definitions of the method se
ID: 3890469 • Letter: J
Question
JAVA HELP -
Use the following template:
Rewrite the definitions of the method setDate and the constructor with parameters so that the values for the month, day, and year are checked before storing the date into the data members.
Add a method member, isLeapYear, to check whether a year is a leap year.
Write a test program to test your class
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to the parameters //Postcondition: dMonth = month; dDay = day; // dYear = year; // This constructor needs to be rewitten **************************** public Date(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to set the date //Data members dMonth, dDay, and dYear are set //according to the parameters //Postcondition: dMonth = month; dDay = day; // dYear = year; // This constructor needs to be rewitten **************************** public void setDate(int month, int day, int year) { dMonth = month; dDay = day; dYear = year; } //Method to return the month //Postcondition: The value of dMonth is returned public int getMonth() { return dMonth; } //Method to return the day //Postcondition: The value of dDay is returned public int getDay() { return dDay; } //Method to return the year //Postcondition: The value of dYear is returned public int getYear() { return dYear; } //Method to return the date in the form mm-dd-yyyy public String toString() { return (dMonth + "-" + dDay + "-" + dYear); } // This method checks whether a year is a leap year // Write the code for this method *********************************** public boolean isLeapYear() { } }
Explanation / Answer
DateTest.java
public class DateTest {
public static void main(String[] args) {
Date d1 = new Date();
System.out.println(d1.toString());
System.out.println("Leap Year: "+d1.isLeapYear());
Date d2 = new Date(11,11, 2000);
System.out.println(d2.toString());
System.out.println("Leap Year: "+d2.isLeapYear());
Date d3 = new Date(14,33, 2010);
System.out.println(d3.toString());
System.out.println("Leap Year: "+d3.isLeapYear());
}
}
Date.java
public class Date {
private int dMonth; // variable to store the month
private int dDay; // variable to store the day
private int dYear; // variable to store the year
private static final int GREGORIAN_START_YEAR = 1582;
// Default constructor
// Data members dMonth, dDay, and dYear are set to
// the default values
// Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
public Date() {
dMonth = 1;
dDay = 1;
dYear = 1900;
}
// Constructor to set the date
// Data members dMonth, dDay, and dYear are set
// according to the parameters
// Postcondition: dMonth = month; dDay = day;
// dYear = year;
// This constructor needs to be rewitten ****************************
public Date(int month, int day, int year) {
setDate(month, day, year);
}
// Method to set the date
// Data members dMonth, dDay, and dYear are set
// according to the parameters
// Postcondition: dMonth = month; dDay = day;
// dYear = year;
// This constructor needs to be rewitten ****************************
public void setDate(int month, int day, int year) {
if(dMonth >=1 && dMonth <=12 && dDay >=1 && dDay <=31) {
dMonth = month;
dDay = day;
dYear = year;
}
}
// Method to return the month
// Postcondition: The value of dMonth is returned
public int getMonth() {
return dMonth;
}
// Method to return the day
// Postcondition: The value of dDay is returned
public int getDay() {
return dDay;
}
// Method to return the year
// Postcondition: The value of dYear is returned
public int getYear() {
return dYear;
}
// Method to return the date in the form mm-dd-yyyy
public String toString() {
return (dMonth + "-" + dDay + "-" + dYear);
}
// This method checks whether a year is a leap year
// Write the code for this method ***********************************
public boolean isLeapYear() {
if (dYear % 4 != 0)
return false;
if (dYear < GREGORIAN_START_YEAR)
return true;
return (dYear % 100 != 0) || (dYear % 400 == 0);
}
Output:
1-1-1900
Leap Year: false
0-0-0
Leap Year: true
0-0-0
Leap Year: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.