Results 1 to 8 of 8
Thread: Please help!
- 12-23-2012, 11:57 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
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.
-
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.
- 12-23-2012, 09:05 PM #3
Re: Please help!
Please go through the Forum Rules, particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-01-2013, 03:18 AM #4
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Re: Please help!
but only if year is like 2012 only 12 is not allowed everything else should be handled automatically by the try catchJava Code:public boolean validDate (int day, int month, int year){ try{ Date.valueOf(year+"-"+month+"-"+day); return true; }catch(IllegalArgumentException e){ return false; } }
-
Re: Please help!
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.
- 01-01-2013, 03:24 PM #6
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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)
- 01-01-2013, 03:52 PM #7
- 01-01-2013, 05:39 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks