Results 1 to 6 of 6
  1. #1
    andrewkay is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default hello guys i need help im a newbie!!!!

    hi guys ive come across a problem that i cant solve its probably easy but im new to java so some help would be appreciated
    thanks.!! the question is about "class Date"

    class Date

    private variables

    private int day, month, year

    constructors

    Date()
    // post day = month = year = 0

    Date(int dd, int mm, int yy)
    // post, day = dd, month = mm; year = yy

    accessor

    boolean earlierThan(Date other)

    question is i know the variables but how would i code the constructors and the accessor thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: hello guys i need help im a newbie!!!!

    Java Code:
    // post, day = dd, month = mm; year = yy
    That line of code is basically correct. All you need to do is make it legal Java syntax. So I don't understand your problem. As for accessor methods all they do is provide access to your private variables. Use setter methods to assign new values to the variables. Use getters to retrieve (return) the value of the variables. You will need one method per variable.

  3. #3
    andrewkay is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: hello guys i need help im a newbie!!!!

    k well thats the part i dont understand so was wondering what someone would code for that specific accessor thanks

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: hello guys i need help im a newbie!!!!

    Java Code:
    public class Foo {
        private int value = 42;
    
        public int getValue() {
            return value;
        }
    
        public static void main(String[] args) {
            Foo bar = new Foo();
            // System.out.println(bar.value); error
            System.out.println(bar.getValue());
        }
    }
    It is not that hard.

  5. #5
    andrewkay is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: hello guys i need help im a newbie!!!!

    so would the constructors be

    public Date()
    { day = month = year = 0}

    public Date (int dd, int mm, int yy)
    {
    day = dd;
    month = mm;
    year = yy;
    }

    but what would i code for "boolean earlierThan(Date other)" would it be....

    public boolean earlierThan(Date other)
    {
    if (year < other.year)
    {
    return true;
    }
    else if (year == other.year && month != other.month)
    {
    if (month < other.month)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    else if (year == other.year && month == other.month)
    {
    if (day < other.day)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    else
    {
    return false;
    }
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: hello guys i need help im a newbie!!!!

    Quote Originally Posted by andrewkay View Post
    so would the constructors be
    Compile and run your code to find out.
    Java Code:
    day = month = year = 0
    Personally I have 2 issue with the above line. I hate code like that and would make 3 separate statements. Also, the default value of instance variables is 0 so that line of code is completely unnecessary.
    but what would i code for "boolean earlierThan(Date other)" would it be....
    Once again compile and run your code to see what it does.

Similar Threads

  1. Please help me guys. You must know how.
    By I Want To Kill Java in forum Java Applets
    Replies: 4
    Last Post: 12-30-2010, 03:02 PM
  2. Guys please i need help !!! :d
    By omar_6630 in forum New To Java
    Replies: 7
    Last Post: 11-23-2010, 12:55 AM
  3. Replies: 6
    Last Post: 02-12-2009, 01:45 PM
  4. hi guys, newbie need some guide on web service client
    By fiqueudrue in forum New To Java
    Replies: 0
    Last Post: 12-22-2008, 02:56 AM
  5. hi guys!!
    By durga in forum Advanced Java
    Replies: 5
    Last Post: 11-15-2008, 12:29 AM

Posting Permissions

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