In: Computer Science
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; 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; public void setDate(int month, int day, int year) { if (year >= 1) dYear = year; else dYear = 1900; if (1 <= month && month <= 12) dMonth = month; else dMonth = 1; switch (dMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (1 <= day && day <= 31) dDay = day; else dDay = 1; break; case 4: case 6: case 9: case 11: if (1 <= day && day <= 30) dDay = day; else dDay = 1; break; case 2: if (isLeapYear()) { if (1 <= day && day <= 29) dDay = day; else dDay = 1; } else { if (1 <= day && day <= 28) dDay = day; else dDay = 1; } } } //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); } public boolean isLeapYear() { if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 == 0)) return true; else return false; } public void setMonth(int m) { dMonth = m; } public void setDay(int d) { dDay = d; } public void setYear(int y) { dYear = y; } public int getDaysInMonth() { int noOfDays = 0; switch (dMonth) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: noOfDays = 31; break; case 4: case 6: case 9: case 11: noOfDays = 30; break; case 2: if (isLeapYear()) noOfDays = 29; else noOfDays = 28; } return noOfDays; } public int numberOfDaysPassed() { int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int sumDays = 0; int i; for (i = 1; i < dMonth; i++) sumDays = sumDays + monthArr[i]; if (isLeapYear() && dMonth > 2) sumDays = sumDays + dDay + 1; else sumDays = sumDays + dDay; return sumDays; } int numberOfDaysLeft() { if (isLeapYear()) return 366 - numberOfDaysPassed(); else return 365 - numberOfDaysPassed(); } public void incrementDate(int nDays) { int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int daysLeftInMonth; daysLeftInMonth = monthArr[dMonth] - dDay; if (daysLeftInMonth >= nDays) dDay = dDay + nDays; else { dDay = 1; dMonth++; nDays = nDays - (daysLeftInMonth + 1); while (nDays > 0) if (nDays >= monthArr[dMonth]) { nDays = nDays - monthArr[dMonth]; if ((dMonth == 2) && isLeapYear()) nDays--; dMonth++; if (dMonth > 12) { dMonth = 1; dYear++; } } else { dDay = dDay+nDays; nDays = 0; } } } public void makeCopy(Date otherDate) { dMonth = otherDate.dMonth; dDay = otherDate.dDay; dDay = otherDate.dDay; } public Date getCopy() { Date temp = new Date(); temp.dMonth = dMonth; temp.dDay = dDay; temp.dYear = dYear; return temp; } }
*************************************************************************************************************************************************************************
Given: the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following:
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== //Derive the class ExtDate so that the date can be printed either form. public class ExtDate extends Date { //Add a data member to the class ExtDate so that the // month can also be stored in string form. private String monthName; public ExtDate(){ super(); } public ExtDate(int month, int day, int year) { super(month, day, year); } public ExtDate(String monthName, int day, int year) { int monthNumber = getMonthNumber(monthName); setDate(monthNumber, day, year); } // getter method public String getMonthName() { return monthName; } // setter method public void setMonthName(String monthName) { this.monthName = monthName; int monthNumber = getMonthNumber(monthName); setMonth(monthNumber); } // returns the month number for the month name private int getMonthNumber(String name) { if (name.equals("January")) return 1; else if (name.equals("February")) return 2; else if (name.equals("March")) return 3; else if (name.equals("April")) return 4; else if (name.equals("May")) return 5; else if (name.equals("June")) return 6; else if (name.equals("July")) return 7; else if (name.equals("August")) return 8; else if (name.equals("September")) return 9; else if (name.equals("October")) return 10; else if (name.equals("November")) return 11; else if (name.equals("December")) return 12; else return 1; } // update the month name for a given month number private void updateMonthName() { if (getMonth() == 1) monthName = "January"; else if (getMonth() == 2) monthName = "February"; else if (getMonth() == 3) monthName = "March"; else if (getMonth() == 4) monthName = "April"; else if (getMonth() == 5) monthName = "May"; else if (getMonth() == 6) monthName = "June"; else if (getMonth() == 7) monthName = "July"; else if (getMonth() == 8) monthName = "August"; else if (getMonth() == 9) monthName = "September"; else if (getMonth() == 10) monthName = "October"; else if (getMonth() == 11) monthName = "November"; else if (getMonth() == 12) monthName = "December"; } // Add a method to output the month in the string format // followed by the year, for instance, in the form March 2005. public void printFormattedDate() { updateMonthName(); System.out.println(monthName + " " + getYear()); } }
==========================================================================
public class ExDateTester { public static void main(String[] args) { //Write the definition of the methods to implement the operations for the class ExtDate and write a test program to test your program. ExtDate extDate = new ExtDate(); extDate.setDate(11,19,1994); extDate.printFormattedDate(); extDate.incrementDate(12); extDate.printFormattedDate(); extDate.setMonthName("April"); extDate.printFormattedDate(); extDate.incrementDate(30); extDate.printFormattedDate(); System.out.println("Month Name: "+extDate.getMonthName()); } }
=============================================================================