473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. public class Date
  2. {
  3. private int dMonth;
  4. private int dDay;
  5. private in dYear;
  6.  
  7. public Date()
  8. {
  9. dMonth = 1;
  10. dDay = 1 ;
  11. dYear = 1900;
  12.  
  13. }
  14.  
  15. public Date(int month, int day, int year)
  16. {
  17. dMonth = month;
  18. dDay = day;
  19. dYear = year;
  20.  
  21. }
  22.  
  23. public void setDate(int month, int day, int year)
  24. {
  25. dMonth = month;
  26. dDay = day;
  27. dYear = year;
  28.  
  29. }
  30.  
  31. public int getMonth()
  32. {
  33. return dMonth;
  34.  
  35. }
  36.  
  37. public int getDay()
  38. {
  39. return dDay;
  40.  
  41. }
  42.  
  43. public int getYear()
  44. {
  45. return dYear;
  46.  
  47. }
  48.  
  49. public String toString()
  50. {
  51. return (dMonth + "-" + dDay + "-" + dYear);
  52.  
  53. }
  54.  
  55. }
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!?!
Dec 4 '09 #1
7 4137
Frinavale
9,735 Expert Mod 8TB
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
Dec 4 '09 #2
I have not learned inheritance..??
Dec 7 '09 #3
Frinavale
9,735 Expert Mod 8TB
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
Dec 7 '09 #4
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... :-(
Dec 7 '09 #5
Frinavale
9,735 Expert Mod 8TB
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 GregorianCalendar 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 gregorianCalendar.getActualMaximum() method:
Expand|Select|Wrap|Line Numbers
  1. gregCalObject.getActualMaximum(gregCalObject.DAY_OF_MONTH);
Please continue to research each task that you have to do. I'm pretty sure that the GregorianCalendar will help you out a lot. That article has a lot of information on dates and how the modern calender works.

-Frinny
Dec 7 '09 #6
i've looked around at the GregorianCalendar and still have no clue what i'm doing hah...
Dec 9 '09 #7
RedSon
5,000 Expert 4TB
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.
Dec 9 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
18
by: Jeremy Weiss | last post by:
I'm trying to build a database that will handle the monthly billing needs of a small company. I'm charting everything out and here's what I see: table for customers sub table to track payments...
1
by: Anthony Esochaghi | last post by:
I need some constructive input to be able to finish my program. I have a field called (1) DateRec, another (2) Municipality and another (3) Type. These three fields invariably will...
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
8
by: john | last post by:
To test a new piece of software designed to help with (among other things) eCommerce WWW site development. The software is fairly easy to use but you must fit a profile. Retail price is 120 GBP and...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
7
by: erekose666 | last post by:
I need a java prog to do the following: Create class Date with the following capabilities: a) Output the date in multiple formats, such as: MM/DD/YYYY June 14, 2005 DDD YYYY b) Use...
1
by: Bikini Browser | last post by:
Folks: I am not a programmer so I need some help here... I need a small program written that tells me how many days old my daughter is since she was born. She was born September 26th, 2000. ...
6
by: shaunb | last post by:
Hi, just new to this forum, and i was wondering if anyone could help me. I've got a piece of work to do, i have done the majority of it, just the last, and the most important thing lol, that i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.