Closed Thread
Results 1 to 8 of 8

Thread: Constant in a Class

  1. #1
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Constant in a Class

    Hi,
    I'm trying to write a Date Class, but I'm having trouble defining it.

    So my class have the following fields:
    -Year
    -Month
    -Day

    I want a method to calculate the number of days since that date. And I want a few constants (i.e. leap = 4). But I'm not sure where should I put the method and the constant?? Should I keep it in the Date class? If I keep the constant in the date class, will it increase the size of Date?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,489
    Blog Entries
    75
    Rep Power
    143

    Re: Constant in a Class

    Start with a different perspective: What methods does your date class need to support? Your internal representation might just be the integer number of days since 1-1-1970, or something else completely. Until we know what you need to accomplish, telling you how best to accomplish it is almost impossible.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: Constant in a Class

    To be more specific, I'm trying to convert a Gregorian date to a Hebrew date, vice versa. I want the following method:

    - gregYearToHebrew(int year)
    - hebrewYearToGreg(int year)
    - gregMonthToHebrew(int month)
    - hebrewMonthToGreg(int month)

    Another one for day, hour, minute, second (halakhim in Hebrew). To convert these, I need constants such as:

    year = 365 days
    day = 24 hours
    hour = 60 minutes
    minute = 60 seconds

    And of course, constant related to Hebrew dates. Lolz, is this any clearer? I feel like I'm back at spot one.
    Last edited by sourlemon; 02-06-2010 at 10:00 PM.

  5. #4
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Constant in a Class

    defining constants:

    Code:
    final static public int YEAR = 365;
    static variables aren't instanced, so no excessive memory is allocated.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,489
    Blog Entries
    75
    Rep Power
    143

    Re: Constant in a Class

    Is this meant to be a conversion class, or actually store dates? The two are quite different. A conversion class won't actually require any data storage, but a date object should really have "assign[type]" and "return[type]" methods.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: Constant in a Class

    Sinipull, how can I use that variable in nonstatic method?

    WingedPanther, I'm not sure I understand what you mean, but I think mine lean toward the store dates. It's similar to a GregorianCalendar class, only with one extra constructor, which is to take an input of a Gregorian date and turn it into a Hebrew date. It also has a method to display the Hebrew instance into a Gregorian Date. Something like this:

    HebrewCalendar date = new HebrewCalendar(GregorianCalendar x);
    date.toString() - display as hebrew date
    date.toGregorianString() - display as Gregorian date


    Of course, these methods has to call to other methods which may use the same constant.

    Lolz, I'm not sure if that makes it any clearer. Thank you for helping me out. I really appreciate this
    Last edited by sourlemon; 02-07-2010 at 08:23 PM.

  8. #7
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Constant in a Class

    Code:
    Sinipull, how can I use that variable in nonstatic method?
    You can use any static variable in non-static method. Static variables are available from anywhere. You can access them:

    Code:
    YourClass.yourVariable
    if they are located in the same class, as the method accessing them, then no Class name is needed.

  9. #8
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: Constant in a Class

    you're awesome! I totally forgot about that. I was too focus on the class. Thank you.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Non-constant file name
    By Jyggalag in forum C and C++
    Replies: 5
    Last Post: 04-16-2011, 03:42 PM
  2. Is a reference a constant pointer?
    By espdev-darkness in forum C and C++
    Replies: 5
    Last Post: 11-17-2010, 06:44 AM
  3. e constant in Visual studio?
    By idioteque2131 in forum C and C++
    Replies: 3
    Last Post: 04-24-2010, 03:10 PM
  4. c++ constant questions
    By jwxie518 in forum C and C++
    Replies: 8
    Last Post: 02-14-2009, 07:59 AM
  5. GAS AT&T non-constant expresssion
    By cvfirefox in forum General Programming
    Replies: 4
    Last Post: 10-27-2008, 08:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts