1) Design a class Date: Provide 3fields (int) to store the month number, the day
ID: 3580047 • Letter: 1
Question
1) Design a class Date: Provide 3fields (int) to store the month number, the day number, and the year Provide the following methods: Default constructor sets date to 1, 1, 1500 Constructor with parameters if parameters are not valid, set date to 1.1.1500 setDate-if parameters are not valid, set to 1.1.1500 Accessors for each field Mutators for each field is not valid, do not set A method to print a date in the form mm-dd-yyy A method to print a date with the month as a string (ex March 20, 2010) Method to return the number of days in the month is Leap- test whether the year is a leap year EXTRA CREDITS METHODS: Method to return the number of days passed in the year from the current date Method to return the number of days remaining in the year-from the current date Method to calculate a new date by adding a fixed number of days to the date A method to print the calendar for the particular month (can print a calendar for any month starting January 1, 1500. Note that the day for January 1 of the year 1500 was a Monday. To calculate the first day of the month, you can add the appropriate number of days to Monday, January 1. 1500.) 2) Write a menu-driven program to test your Date class thoroughly. If the user wishes to print the calendar use the format below September 2014 Sun Mon Tue Wed Thu Fri Sat 17 18 19 20 15 16 25 26 27 23 21 22 29Explanation / Answer
package temp;
// DataExample class
public class DateExample {
int month;
int day;
int year;
// default Constructor
public DateExample(){
this.month = 1;
this.day = 1;
this.year = 1500;
}
// Constructor with parameters
public DateExample(int month, int day, int year) {
if (month == 0)
this.month = 0;
else
this.month = month;
if (day == 0)
this.day = 0;
else
this.day = day;
if (year == 0)
this.year = 0;
else
this.year = year;
}
// Accessor for month
public int getMonth() {
return month;
}
// Accessor for day
public int getDay() {
return day;
}
// Accessor for year
public int getYear() {
return year;
}
// Mutator for month
public void setMonth(int month) {
this.month = month;
}
// Mutator for day
public void setDay(int day) {
this.day = day;
}
// Mutator for year
public void setYear(int year) {
this.year = year;
}
// Print date in mm-dd-yyyy format
public void displayDate(){
System.out.println(month + "-" + day + "-" + year);
}
// Get the month name in String corresponding to month number
public String theMonth(int month){
String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return monthNames[month];
}
// Display date in String format
public void displayAsString(){
System.out.println(theMonth(month) + " " + day + "," + year);
}
// Check if the year is leap year or not.
public boolean isLeap(int year){
return (year/4 == 0);
}
public static void main(String[] args){
// Create a instance of the class.
DateExample de = new DateExample();
de.displayDate();
de.displayAsString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.