Question

public class Date { private int month; private int day; private int year; /** default constructor...

public class Date {
private int month;
private int day;
private int year;

/** default constructor
* sets month to 1, day to 1 and year to 2000
*/
public Date( ) {
setDate( 1, 1, 2000 );
}

/** overloaded constructor
* @param mm initial value for month
* @param dd initial value for day
* @param yyyy initial value for year
*
* passes parameters to setDate method
*/
public Date( int mm, int dd, int yyyy ) {
setDate( mm, dd, yyyy );
}

/* accessor methods */
int getMonth( ) { return month; }
int getDay( ) { return day; }
int getYear( ) { return year; }

/** mutator method */
/** setDate
* @param mm new value for month
* @param dd new value for day
* @param yyyy new value for year
* passes parameters to setMonth, setDay, and setYear
*/
public void setDate( int mm, int dd, int yyyy ) {
setYear(yyyy);
setMonth( mm );
setDay( dd );
}
  
/** helper methods */
/** setDay
* @param dd new value for day
* if dd is legal day for current month, sets day to dd
* otherwise, sets day to 1
*/
private void setDay( int dd ) {
int [] validDays = { 0, 31, 29, 31, 30,
31, 30, 31, 31, 30,
31, 30, 31 };
day = ( dd >= 1 && dd <= validDays[month] ? dd : 1 );
}
/** setMonth
* @param mm new value for month
* if mm is between 1 and 12, sets month to mm
* otherwise, sets month to 1
*/
private void setMonth( int mm ) {
month = ( mm >= 1 && mm <= 12 ? mm : 1 );
}
/** setYear
* @param yyyy new value for year
* sets year to yyyy
*/
private void setYear( int yyyy ) {
year = yyyy;
}
  
/** toString
* @return String
* returns date in mm/dd/yyyy format
*/
public String toString( ) {
return month + "/" + day + "/" + year;
}

/** equals
* @param d Date object to compare to this
* @return true if d is equal to this
* false, otherwise
*/
public boolean equals( Date d ) {
if ( month == d.month
&& day == d.day
&& year == d.year )
return true;
else
return false;
}
}

We need to use this Data class to add to the DataClient class, which is

public class DateClient
{
public static void main( String [] args )
{
// add code to construct Data objects
   // and test and output if they are a
   // leap year or not
     
   // not leap year (not divisible by 4)


   // leap year (divisible by 4, but not by 100)


   // not leap year (divisible by 4 and by 100)


   // leap year (divisible by 4 and by 100 and by 400)


}

Below are the rules to determine if a year is a leap year.

  • Every year divisible by 4 is a leap year,

  • But every year divisible by 100 is NOT a leap year,

  • Unless the year is also divisible by 400, then it is still a leap year.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

public class DateClient <terminated> DateClient [Java Application] C:\Pr 1/1/2000 is a leap year 5/10/2006 is not a leap year

Program

DateClient.java

public class DateClient
{
public static void main( String [] args )
{
// add code to construct Date objects
// and test and output if they are a
// leap year or not
// not leap year (not divisible by 4)
// leap year (divisible by 4, but not by 100)
// not leap year (divisible by 4 and by 100)
// leap year (divisible by 4 and by 100 and by 400)
  
   //Test object1
   Date date1=new Date();
   if (((date1.getYear() % 4 == 0) && (date1.getYear() % 100!= 0)) || (date1.getYear()%400 == 0))
        System.out.println(date1+" is a leap year");
     else
        System.out.println(date1+" is not a leap year");
   //Test object2
       Date date2=new Date(5,10,2006);
       if (((date2.getYear() % 4 == 0) && (date2.getYear() % 100!= 0)) || (date2.getYear()%400 == 0))
            System.out.println(date2+" is a leap year");
         else
            System.out.println(date2+" is not a leap year");
       //Test object3
       Date date3=new Date(32,10,1978);
       if (((date3.getYear() % 4 == 0) && (date3.getYear() % 100!= 0)) || (date3.getYear()%400 == 0))
            System.out.println(date3+" is a leap year");
         else
            System.out.println(date3+" is not a leap year");
}
}

Date.java

public class Date {
private int month;
private int day;
private int year;

/** default constructor
* sets month to 1, day to 1 and year to 2000
*/
public Date( ) {
setDate( 1, 1, 2000 );
}

/** overloaded constructor
* @param mm initial value for month
* @param dd initial value for day
* @param yyyy initial value for year
*
* passes parameters to setDate method
*/
public Date( int mm, int dd, int yyyy ) {
setDate( mm, dd, yyyy );
}

/* accessor methods */
int getMonth( ) { return month; }
int getDay( ) { return day; }
int getYear( ) { return year; }

/** mutator method */
/** setDate
* @param mm new value for month
* @param dd new value for day
* @param yyyy new value for year
* passes parameters to setMonth, setDay, and setYear
*/
public void setDate( int mm, int dd, int yyyy ) {
setYear(yyyy);
setMonth( mm );
setDay( dd );
}

/** helper methods */
/** setDay
* @param dd new value for day
* if dd is legal day for current month, sets day to dd
* otherwise, sets day to 1
*/
private void setDay( int dd ) {
int [] validDays = { 0, 31, 29, 31, 30,
31, 30, 31, 31, 30,
31, 30, 31 };
day = ( dd >= 1 && dd <= validDays[month] ? dd : 1 );
}
/** setMonth
* @param mm new value for month
* if mm is between 1 and 12, sets month to mm
* otherwise, sets month to 1
*/
private void setMonth( int mm ) {
month = ( mm >= 1 && mm <= 12 ? mm : 1 );
}
/** setYear
* @param yyyy new value for year
* sets year to yyyy
*/
private void setYear( int yyyy ) {
year = yyyy;
}

/** toString
* @return String
* returns date in mm/dd/yyyy format
*/
public String toString( ) {
return month + "/" + day + "/" + year;
}

/** equals
* @param d Date object to compare to this
* @return true if d is equal to this
* false, otherwise
*/
public boolean equals( Date d ) {
if ( month == d.month
&& day == d.day
&& year == d.year )
return true;
else
return false;
}
}

------------------------------

Output

1/1/2000 is a leap year
5/10/2006 is not a leap year
1/10/1978 is not a leap year

Add a comment
Know the answer?
Add Answer to:
public class Date { private int month; private int day; private int year; /** default constructor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Consider the following Date class: #include <iostream> using namespace std; class MyDate { private:...

    In C++ Consider the following Date class: #include <iostream> using namespace std; class MyDate { private: int month, day, year; public: MyDate() {setDate(2,27,2006);} MyDate(int, int, int); void setDate(int mm, int dd, int yyyy); void showDate(); }; MyDate::MyDate(int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; }; void MyDate::setDate(int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; }; void MyDate::showDate() { cout << "The date is "...

  • Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 -...

    Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...

  • Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your...

    Take your MonthDay class from Assignment #6 and modify it to throw an IllegalArgumentException when the user of the class attempts to pass an invalid value for month and year. Make sure you have your main method(function) in a separate source file demonstrating use of the try/catch block to catch the IllegalArgumentException that the MonthDay class may throw in the event of an invalid month, or year. class MonthDays {    private int month;    private int year;    public MonthDays(int aMonth, int...

  • Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.

    Create a Java application named Problem14 using the two files Dr. Hanna provided to you—Date.java andProblem14.java—then change his code to add two non-trivial public, non-static methods to the Date class.1. Increment a Date to the next day (for example, 1-31-2011 increments to 2-1-2011; 12-31-2010 increments to 1-1-2011).2. Decrement a Date to the previous day (for example, 2-1-1953 decrements to 1-31-1953; 1-1-1954 decrements to 12-31-1953).. Extra Credit (up to 5 points) Re-write the Date member functions Input() and Output() to usedialog boxes to...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

  • Need comments added please. public class TenDate {    private int NMnth;    private int NDy;...

    Need comments added please. public class TenDate {    private int NMnth;    private int NDy;    private int NYr;    private String [] MnthNm =        {"", "January", "February", "March", "April", "May", "June", "July",            "August", "September", "October", "November", "December"};    private int [] MnthD =        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    public TenDate()    {        NMnth = 1;        NDy = 1;...

  • Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

    Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form. Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors...

    JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors and eliminate them identify each member of this class and write your answer in a comment There are four different types of class member: constant, instance variable, constructor, and method identify a method as class method if it is static /* * Java program: Date.java * * Define the class Date */ import java.time.LocalDateTime; public class Date { private static final int daysEachYear =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT