Question

Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the followin

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

//Date.java

public class Date {

      

       private int day;

       private int month;

       private int year;

      

       // constructor

       public Date(int year, int month, int day)

       {

             this.year = year;

             this.month = month;

             this.day = day;

       }

      

       // method to add days in the current Date

       public void addDays(int days)

       {

             day += days;

             // loop that continues till we the valid date

             while(day > days_in_month(month))

             {     

                    day -= days_in_month(month);

                    month++;

                    if(month > 12)

                    {

                           year++;

                           month=1;

                    }

             }

       }

      

       // helper method to get the number of days in a given month

       private int days_in_month(int month)

       {

             if(month == 1 || month == 3 || month == 5 || month== 7 || month == 8 || month == 10 || month == 12)

                    return 31;

             else if(month == 4 || month == 6 || month == 9 || month == 11)

                    return 30;

             else {

                    if(isLeapYear()) // if month = 2, and it is a leap year

                           return 29;

                    else

                           return 28;

             }

       }

      

       // method to add weeks to the Date

       public void addWeeks(int weeks)

       {

             addDays(weeks*7);

       }

      

       // method to return the number of days that this Date must be adjusted to make it equal to the given Date

       public int daysTo(Date other)

       {

             int days = 0;

             // loop to add the number of days between this year and other’s year

             for(int i=year+1;i<other.getYear();i++)

             {

                    if((i%400 == 0) || ((i%4 == 0) && (i%100 != 0)))

                           days += 366;

                    else

                           days += 365;

             }

            

             // if both years are same

             if(year == other.getYear())

             {

                    // loop to add the number of days between the months

                    for(int i=month+1;i<other.getMonth();i++)

                           days += days_in_month(i);

      

                    days += days_in_month(month) - day; // add the days left in this month

                    days += other.getDay(); // add the days passed in other month

             }else

             {

                    // loop to add the number of days left in this year

                    for(int i=month+1;i<=12;i++)

                           days += days_in_month(i);

                   

                    days += days_in_month(month) - day; // add the days left in this year

                    // add the number of days passed in other year

                    for(int i=1;i<other.month;i++)

                           days += other.days_in_month(i);

                    days += other.getDay(); // add the number of days passed in other month

             }

            

             return days;

       }

      

       // method to return the day

       public int getDay()

       {

             return day;

       }

      

       // method to return the month

       public int getMonth()

       {

             return month;

       }

      

       // method to return the year

       public int getYear()

       {

             return year;

       }

      

       // method to check if the year is a leap year

       public boolean isLeapYear()

       {

             return((year%400 == 0) || ((year%4 == 0) && (year%100 != 0)));

       }

      

       public String toString()

       {

             return(year+"/"+String.format("%02d", month)+"/"+String.format("%02d", day));

       }

}

//end of Date.java

Add a comment
Know the answer?
Add Answer to:
Write a class called Date that represents a date consisting of a year, month, and day....
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
  • Use Java and creat proper classes to finish this problem Write a class called Date that...

    Use Java and creat proper classes to finish this problem Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by...

  • 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 )...

  • public Date(int month, int day, int year); public int getMonth(); public int getDay(); public int getYear();...

    public Date(int month, int day, int year); public int getMonth(); public int getDay(); public int getYear(); Can you check this code below and see if its correct Here is my answer below @Test public void testDate() {   Date date1 = newDate(“May”,23,2018);      Date1.getMonth();      assertNull(date1.getMonth());    assertEquals(“May”, date1.getMonth());    assertEquals(“May”,date1.getMonth());    assertEquals(23, date1.getPhoneNumer());       assertEquals(2018, date1.getyear());

  • 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...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

  • In Java You are to write a program that determines the day of the week for...

    In Java You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program: /** Interface for Date objects to be used by the Year3000 driver program. @author Jon Sorenson */ public interface DateInterface { /** @return the day of the month (1-31) */ public int getDay(); /** @return the day of...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

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