Details of this Paper

Programming 5

Description

solution


Question

Programming 5;In programming exercise 2, the class Date was designed implemented to keep track of the date, but it has very limited operations. Redefine the class Date so that, in addition to the operations already define, it can perform the following operations on a date;a. Set the month;b. Set the day;c. Set the year;d. Return the month;e. Return the day;f. Return the year;g. Tests whether a year is a leap year;h. Return the number of days in the month. For example, if the day is 3 ? 12 ? 2015, the number of days to be returned is 31, because there are 31 days in March.;i. Return the number of days passed in the year. For example, if the date is 3 ? 18 ? 2015, the number of days past in the year is 77. Note that the number of days returned also includes the current day.;j. Return the number of days remaining in year. For example, if the date is 3 ? 18 ? 2015, the number of days remaining in the year is 288.;k. Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3 ? 18 ? 2015 and the days to be added on 25, the new date is 4 ? 12 ? 2015.;l. Return a reference to the object containing a copy of the date.;m. Make a copy of another date.Given a reference to an object containing a date, copy the data members of the object into the corresponding data members of this object.;n. Write the definitions of the methods to implement the operations defined for the class Date.;Exercise 2 Answer;//Data.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;// default constructor;// The instant variables dMonth, dDay,dYear are set to;// the default values.;// Postcondition, dMonth = 1, dDay = 1, dYear = 1900;public Date ();dMonth = 1;dDay = 1;dYear = 1900;/**;* a method isLeapYear to check whether a year is a leap year;* @param year;* @return;*/;public boolean isLeapYear(int year);// Check if the year is a leap year;return (((year % 4 == 0) && (year % 100 != 0)) || ((year % 100 == 0) && (year % 400 == 0)));// Constructor to set the date;// The instance variables 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;// The instance variables 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);int[] days={31,28,31, 30,31,30,31,31,30,31,30,31};//if leap year;if(isLeapYear(year));days[1]=29;if(month 12);System.out.println("Month is not valid. It should be between 1 and 12 inclusive!");return;if(day days[month-1]);System.out.println("Day is invalid. It should be between 1 and "+days[month-1]+" inclusive!");return;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: 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)

 

Paper#68146 | Written in 18-Jul-2015

Price : $32
SiteLock