I need some help with a date program can anyone help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kr151080
    New Member
    • Dec 2009
    • 6

    I need some help with a date program can anyone help?

    Ok so I am messing around with a program and have no idea how to go about doing this but here is the code for the class date....

    Code:
    public class Date
    {
    private int dMonth;
    private int dDay;
    private in dYear;
    
    public Date()
    {
    dMonth = 1;
    dDay = 1 ;
    dYear = 1900;
    
    }
    
    public Date(int month, int day, int year)
    {
    dMonth = month;
    dDay = day;
    dYear = year;
    
    }
    
    public void setDate(int month, int day, int year)
    {
    dMonth = month;
    dDay = day;
    dYear = year;
    
    }
    
    public int getMonth()
    {
    return dMonth;
    
    }
    
    public int getDay()
    {
    return dDay;
    
    }
    
    public int getYear()
    {
    return dYear;
    
    }
    
    public String toString()
    {
    return (dMonth + "-" + dDay + "-" + dYear);
    
    }
    
    }
    From what I've been told the class Date was designed and implemented to keep track of a date, but it has very limited operations. I have to redefine the class Date so that, in addition to the operations already defined, it can perform the following operations on a date:

    -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 the month. For example, if the date is 3-12-2005, the number of days to be returned is 31 because there are 31 days in March.

    -Return the number of days passed in the year. For example if the date is 3-18-2005, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.

    -Return the number of days remaining in the year. For example, if the date is 3-18-2005, the number of days remaining in the year is 288.

    -Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2005 and the days to be added are 25, the new date is 4-12-2005.

    -Return a reference to the object containing a copy of the date.

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

    -Write the definitions of the methods to implement the operations defined for the class Date.

    Please if anyone can help that would be amazing!?!
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I would recommend that you use inheritance for this.
    Create a new class that derives from the Java Date class and extend upon it's functionality.

    Have you learned about inheritance?

    -Frinny

    Comment

    • kr151080
      New Member
      • Dec 2009
      • 6

      #3
      I have not learned inheritance..??

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well this is a classic situation where inheritance would be used.

        There is already a Date class.
        It has a whole bunch of functionality already defined and it works well.
        You want to add functionality to the Date class but you don't want to reinvent the wheel. This is where inheritance comes into the works. Inheritances lets you reuse the existing class so that you don't have to reinvent the same thing with additional functionality.

        Your class will inherit (derive) from the existing Date class. When you inherit from a class, your class will use the existing class as it's base class. Your new class is referred to as the derived class.

        So, the existing Date class becomes the base class for your new class and it provides all of the existing functionality to the derived class so that you don't have to re-write it. All you have to do now is add the new methods to your class. These methods are probably going to use the base class's methods in order to provide the extended functionality.

        It's a pretty large concept to explain in a single post so please research the topic.

        It's pretty cool stuff.

        Cheers!

        -Frinny

        Comment

        • kr151080
          New Member
          • Dec 2009
          • 6

          #5
          I understand the concept you're telling me, and i've already begun to write the new program....i have:

          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

          all finished....Now i don't really have any idea where to start writing the code for:

          -Return the number of days in the month. For example, if the date is 3-12-2005, the number of days to be returned is 31 because there are 31 days in March.

          -Return the number of days passed in the year. For example if the date is 3-18-2005, the number of days passed in the year is 77. Note that the number of days returned also includes the current day.

          -Return the number of days remaining in the year. For example, if the date is 3-18-2005, the number of days remaining in the year is 288.

          -Calculate the new date by adding a fixed number of days to the date. For example, if the date is 3-18-2005 and the days to be added are 25, the new date is 4-12-2005.

          -Return a reference to the object containing a copy of the date.

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

          -Write the definitions of the methods to implement the operations defined for the class Date.

          ???? I'm soo lost... :-(

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            So I guess you decided not to use inheritance then.
            That's too bad.

            You have asked for Way to much.
            Let's take a look at the first requirement:
            • Test whether the year is a leap year


            Let's see how to test for this by doing research on what needs to be done!
            According to wikipedia on Leap Year:
            Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years

            Seems pretty simple to me. Preform a "mod 100" on the year...and a "mod 400" on the year. If the year is not evenly divisible by 100 and it is evenly divisible 400 then the year is a leap year.


            Now your second requirement:
            • Return the number of days in the month


            Well, this is a little more complicated.
            So, lets do some more research on what tools are out there to help you with solving this problem.

            A simple google search turned up the GregorianCalend ar class.

            This class has a bunch of methods in it that let you determine things like determining the number of days in a month.
            The method that does this is the gregorianCalend ar.getActualMax imum() method:
            Code:
            gregCalObject.getActualMaximum(gregCalObject.DAY_OF_MONTH);
            Please continue to research each task that you have to do. I'm pretty sure that the GregorianCalend ar will help you out a lot. That article has a lot of information on dates and how the modern calender works.

            -Frinny

            Comment

            • kr151080
              New Member
              • Dec 2009
              • 6

              #7
              i've looked around at the GregorianCalend ar and still have no clue what i'm doing hah...

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                If you have looked at the documentation and still do not understand what to do then your next step should be to visit your school's computer lab and talk with one of the lab assistants there to help you through your problems. If your school does not have a lab then you should talk with your instructor directly and tell them the troubles you are having. They should recommend some steps for you to take to increase your knowledge and understanding.

                Comment

                Working...