Results 1 to 8 of 8

Thread: Please help!

  1. #1
    morrisey43 is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Please help!

    Can somebody please help me with the code for this question? I know it'll be a very simple question for most people but I don't know where to begin :(

    Write the code for the following methods in the newDate class:
    public boolean validDate (int day, int month, int year)
    The month can only be in the range 1-12 and a month must have the correct
    number of days eg 30.2.1997 is invalid because February can only have 28
    days, or 29 days if the year is a leap year.
    This method must return true if the date is valid and false if the date is
    invalid.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please help!

    Of course we won't do this for you, but we can help you with your code once you show us your attempt. I would start by creating the skeleton of the method, in other words the return type, method name and parameters just as the assignment gives you and then start writing code to implement the logic required. Break down the logic into very small steps and try to solve each small step one at a time, not moving to the next step until the current one is solved.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Please help!

    Quote Originally Posted by morrisey43 View Post
    Please help!
    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Please help!

    Java Code:
    public boolean validDate (int day, int month, int year){
    	try{
    		Date.valueOf(year+"-"+month+"-"+day);
    		return true;
    	}catch(IllegalArgumentException e){
    		return false;
    	}
    }
    but only if year is like 2012 only 12 is not allowed everything else should be handled automatically by the try catch

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please help!

    Quote Originally Posted by alexanderschill View Post
    Java Code:
    public boolean validDate (int day, int month, int year){
    	try{
    		Date.valueOf(year+"-"+month+"-"+day);
    		return true;
    	}catch(IllegalArgumentException e){
    		return false;
    	}
    }
    but only if year is like 2012 only 12 is not allowed everything else should be handled automatically by the try catch
    Where did you get the static method Date.valueOf(...)? That's not part of the java.util.Date or the java.sql.Date API. In fact your entire answer doesn't make sense to me. To the original poster, I think that you'll be better off ignoring Alexander's well-meaning but misleading advice.
    Last edited by Fubarable; 01-01-2013 at 06:12 AM.

  6. #6
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Please help!

    Sorry but it is part of java.sql.Date;

    at least in java 7

    http://docs.oracle.com/javase/1.4.2/...a.lang.String)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Please help!

    Quote Originally Posted by alexanderschill View Post
    Sorry but it is part of java.sql.Date;

    at least in java 7

    Date (Java 2 Platform SE v1.4.2)
    And that links to the API for Java 1.4.2. And the question wasn't about JDBC, which is where you might use a java.sql.Date.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Please help!

    Start with the most interesting part: leap years; given a full year number (including the century), determine whether or not the number represents a leap year. Build a separate boolean method for it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Tags for this Thread

Posting Permissions

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